| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Benjamin Knecht |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | @file DistanceTrigger.cc |
|---|
| 31 | @brief Implementation of the DistanceTrigger class. |
|---|
| 32 | @ingroup NormalTrigger |
|---|
| 33 | */ |
|---|
| 34 | |
|---|
| 35 | #include "DistanceTrigger.h" |
|---|
| 36 | |
|---|
| 37 | #include "core/CoreIncludes.h" |
|---|
| 38 | #include "core/XMLPort.h" |
|---|
| 39 | #include "worldentities/pawns/Pawn.h" |
|---|
| 40 | #include "DistanceTriggerBeacon.h" |
|---|
| 41 | |
|---|
| 42 | namespace orxonox |
|---|
| 43 | { |
|---|
| 44 | |
|---|
| 45 | /*static*/ const std::string DistanceTrigger::beaconModeOff_s = "off"; |
|---|
| 46 | /*static*/ const std::string DistanceTrigger::beaconModeIdentify_s = "identify"; |
|---|
| 47 | /*static*/ const std::string DistanceTrigger::beaconModeExlcude_s = "exclude"; |
|---|
| 48 | |
|---|
| 49 | CreateFactory(DistanceTrigger); |
|---|
| 50 | |
|---|
| 51 | DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator), beaconMask_(NULL) |
|---|
| 52 | { |
|---|
| 53 | RegisterObject(DistanceTrigger); |
|---|
| 54 | |
|---|
| 55 | this->distance_ = 100; |
|---|
| 56 | this->targetMask_.exclude(Class(BaseObject)); |
|---|
| 57 | this->targetName_ = ""; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | DistanceTrigger::~DistanceTrigger() |
|---|
| 61 | { |
|---|
| 62 | if(this->beaconMask_ != NULL) |
|---|
| 63 | delete this->beaconMask_; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 67 | { |
|---|
| 68 | SUPER(DistanceTrigger, XMLPort, xmlelement, mode); |
|---|
| 69 | |
|---|
| 70 | XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f); |
|---|
| 71 | XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("Pawn"); |
|---|
| 72 | XMLPortParam(DistanceTrigger, "beaconMode", setBeaconMode, getBeaconMode, xmlelement, mode); |
|---|
| 73 | XMLPortParam(DistanceTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void DistanceTrigger::addTarget(Ogre::Node* targetNode) |
|---|
| 77 | { |
|---|
| 78 | this->targetSet_.insert(targetNode); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | void DistanceTrigger::removeTarget(Ogre::Node* targetNode) |
|---|
| 82 | { |
|---|
| 83 | int returnval = this->targetSet_.erase(targetNode); |
|---|
| 84 | if (returnval == 0) |
|---|
| 85 | { |
|---|
| 86 | COUT(2) << "Warning: Node " << targetNode << " did not exist in targetSet of trigger " << this << " !" << std::endl; |
|---|
| 87 | COUT(4) << "Content of targetSet of trigger " << this << " :" << std::endl; |
|---|
| 88 | std::set<Ogre::Node*>::iterator it; |
|---|
| 89 | for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it) |
|---|
| 90 | { |
|---|
| 91 | COUT(4) << *it << std::endl; |
|---|
| 92 | } |
|---|
| 93 | COUT(4) << "End of targetSet of trigger " << this << std::endl; |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void DistanceTrigger::addTargets(const std::string& targets) |
|---|
| 98 | { |
|---|
| 99 | Identifier* targetId = ClassByString(targets); |
|---|
| 100 | |
|---|
| 101 | //! Checks whether the target is (or is derived from) a ControllableEntity. |
|---|
| 102 | Identifier* pawnId = Class(Pawn); |
|---|
| 103 | Identifier* distanceTriggerBeaconId = Class(DistanceTriggerBeacon); |
|---|
| 104 | if(targetId->isA(pawnId) || targetId->isA(distanceTriggerBeaconId)) |
|---|
| 105 | { |
|---|
| 106 | this->setForPlayer(true); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | if (!targetId) |
|---|
| 110 | { |
|---|
| 111 | COUT(1) << "Error: \"" << targets << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ')' << std::endl; |
|---|
| 112 | return; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | this->targetMask_.include(targetId); |
|---|
| 116 | |
|---|
| 117 | // trigger shouldn't react on itself or other triggers |
|---|
| 118 | this->targetMask_.exclude(Class(Trigger), true); |
|---|
| 119 | |
|---|
| 120 | // we only want WorldEntities |
|---|
| 121 | ClassTreeMask WEMask; |
|---|
| 122 | WEMask.include(Class(WorldEntity)); |
|---|
| 123 | this->targetMask_ *= WEMask; |
|---|
| 124 | |
|---|
| 125 | this->notifyMaskUpdate(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void DistanceTrigger::removeTargets(const std::string& targets) |
|---|
| 129 | { |
|---|
| 130 | Identifier* targetId = ClassByString(targets); |
|---|
| 131 | this->targetMask_.exclude(targetId); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | bool DistanceTrigger::checkDistance() |
|---|
| 135 | { |
|---|
| 136 | // Check for new objects that are in range |
|---|
| 137 | ClassTreeMask targetMask = this->targetMask_; |
|---|
| 138 | if(this->beaconMode_ == distanceTriggerBeaconMode::identify) |
|---|
| 139 | targetMask = *this->beaconMask_; |
|---|
| 140 | |
|---|
| 141 | // Iterate through all objects |
|---|
| 142 | for (ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it) |
|---|
| 143 | { |
|---|
| 144 | WorldEntity* entity = orxonox_cast<WorldEntity*>(*it); |
|---|
| 145 | if (!entity) |
|---|
| 146 | continue; |
|---|
| 147 | |
|---|
| 148 | // If the DistanceTrigger is in identify mode and the DistanceTriggerBeacon attached to the object has the wrong name we ignore it. |
|---|
| 149 | if(this->beaconMode_ == distanceTriggerBeaconMode::identify) |
|---|
| 150 | { |
|---|
| 151 | if(entity->getName() != this->targetName_) |
|---|
| 152 | continue; |
|---|
| 153 | // If the object, the DistanceTriggerBeacon is attached to, is not a target of this DistanceMultiTrigger. |
|---|
| 154 | else if(this->targetMask_.isExcluded(entity->getParent()->getIdentifier())) |
|---|
| 155 | continue; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | // If the DistanceTrigger is in exclude mode and the DistanceTriggerBeacon attached to the object has the right name, we ignore it. |
|---|
| 159 | if(this->beaconMode_ == distanceTriggerBeaconMode::exclude) |
|---|
| 160 | { |
|---|
| 161 | |
|---|
| 162 | const std::set<WorldEntity*> attached = entity->getAttachedObjects(); |
|---|
| 163 | bool found = false; |
|---|
| 164 | for(std::set<WorldEntity*>::const_iterator it = attached.begin(); it != attached.end(); it++) |
|---|
| 165 | { |
|---|
| 166 | if((*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()) && static_cast<DistanceTriggerBeacon*>(*it)->getName() == this->targetName_) |
|---|
| 167 | { |
|---|
| 168 | found = true; |
|---|
| 169 | break; |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | if(found) |
|---|
| 173 | continue; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); |
|---|
| 177 | if (distanceVec.length() < this->distance_) |
|---|
| 178 | { |
|---|
| 179 | |
|---|
| 180 | // If the target is a player (resp. is a, or is derived from a, Pawn) the triggeringPlayer is set to the target entity. |
|---|
| 181 | if(this->isForPlayer()) |
|---|
| 182 | { |
|---|
| 183 | |
|---|
| 184 | // Change the entity to the parent of the DistanceTriggerBeacon (if in identify-mode), which is the entity to which the beacon is attached. |
|---|
| 185 | if(this->beaconMode_ == distanceTriggerBeaconMode::identify) |
|---|
| 186 | entity = entity->getParent(); |
|---|
| 187 | |
|---|
| 188 | Pawn* player = orxonox_cast<Pawn*>(entity); |
|---|
| 189 | this->setTriggeringPlayer(player); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | return true; |
|---|
| 193 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | return false; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /** |
|---|
| 200 | @brief |
|---|
| 201 | Set the beacon mode. |
|---|
| 202 | @param mode |
|---|
| 203 | The mode as an enum. |
|---|
| 204 | */ |
|---|
| 205 | void DistanceTrigger::setBeaconModeDirect(distanceTriggerBeaconMode::Value mode) |
|---|
| 206 | { |
|---|
| 207 | this->beaconMode_ = mode; |
|---|
| 208 | if(this->beaconMode_ == distanceTriggerBeaconMode::identify && this->beaconMask_ == NULL) |
|---|
| 209 | { |
|---|
| 210 | this->beaconMask_ = new ClassTreeMask(); |
|---|
| 211 | this->beaconMask_->exclude(Class(BaseObject)); |
|---|
| 212 | this->beaconMask_->include(Class(DistanceTriggerBeacon)); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /** |
|---|
| 217 | @brief |
|---|
| 218 | Get the beacon mode. |
|---|
| 219 | @return |
|---|
| 220 | Returns the mode as a string. |
|---|
| 221 | */ |
|---|
| 222 | const std::string& DistanceTrigger::getBeaconMode(void) const |
|---|
| 223 | { |
|---|
| 224 | switch(this->getBeaconModeDirect()) |
|---|
| 225 | { |
|---|
| 226 | case distanceTriggerBeaconMode::off : |
|---|
| 227 | return DistanceTrigger::beaconModeOff_s; |
|---|
| 228 | case distanceTriggerBeaconMode::identify: |
|---|
| 229 | return DistanceTrigger::beaconModeIdentify_s; |
|---|
| 230 | case distanceTriggerBeaconMode::exclude: |
|---|
| 231 | return DistanceTrigger::beaconModeExlcude_s; |
|---|
| 232 | default : |
|---|
| 233 | assert(0); // This is impossible. |
|---|
| 234 | return BLANKSTRING; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | /** |
|---|
| 239 | @brief |
|---|
| 240 | Set the beacon mode. |
|---|
| 241 | @param mode |
|---|
| 242 | The mode as a string. |
|---|
| 243 | */ |
|---|
| 244 | void DistanceTrigger::setBeaconMode(const std::string& mode) |
|---|
| 245 | { |
|---|
| 246 | if(mode == DistanceTrigger::beaconModeOff_s) |
|---|
| 247 | this->setBeaconModeDirect(distanceTriggerBeaconMode::off); |
|---|
| 248 | else if(mode == DistanceTrigger::beaconModeIdentify_s) |
|---|
| 249 | this->setBeaconModeDirect(distanceTriggerBeaconMode::identify); |
|---|
| 250 | else if(mode == DistanceTrigger::beaconModeExlcude_s) |
|---|
| 251 | this->setBeaconModeDirect(distanceTriggerBeaconMode::exclude); |
|---|
| 252 | else |
|---|
| 253 | COUT(1) << "Invalid beacon mode in DistanceTrigger." << endl; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | bool DistanceTrigger::isTriggered(TriggerMode::Value mode) |
|---|
| 257 | { |
|---|
| 258 | if (Trigger::isTriggered(mode)) |
|---|
| 259 | { |
|---|
| 260 | return checkDistance(); |
|---|
| 261 | } |
|---|
| 262 | else |
|---|
| 263 | return false; |
|---|
| 264 | } |
|---|
| 265 | } |
|---|