| [1693] | 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 | #include "DistanceTrigger.h" | 
|---|
 | 30 |  | 
|---|
 | 31 | #include "core/CoreIncludes.h" | 
|---|
 | 32 | #include "core/XMLPort.h" | 
|---|
| [5735] | 33 | #include "worldentities/pawns/Pawn.h" | 
|---|
| [6906] | 34 | #include "DistanceTriggerBeacon.h" | 
|---|
| [2261] | 35 |  | 
|---|
| [2071] | 36 | namespace orxonox | 
|---|
 | 37 | { | 
|---|
| [1693] | 38 |   CreateFactory(DistanceTrigger); | 
|---|
 | 39 |  | 
|---|
| [5727] | 40 |   DistanceTrigger::DistanceTrigger(BaseObject* creator) : Trigger(creator) | 
|---|
| [1693] | 41 |   { | 
|---|
 | 42 |     RegisterObject(DistanceTrigger); | 
|---|
 | 43 |  | 
|---|
| [2029] | 44 |     this->distance_ = 100; | 
|---|
 | 45 |     this->targetMask_.exclude(Class(BaseObject)); | 
|---|
| [6906] | 46 |     this->targetName_ = BLANKSTRING; | 
|---|
 | 47 |     this->singleTargetMode_ = false; | 
|---|
| [5937] | 48 |     this->setForPlayer(false); //!< Normally hasn't just players as targets. | 
|---|
| [1693] | 49 |   } | 
|---|
 | 50 |  | 
|---|
 | 51 |   DistanceTrigger::~DistanceTrigger() | 
|---|
 | 52 |   { | 
|---|
 | 53 |   } | 
|---|
 | 54 |  | 
|---|
| [2029] | 55 |   void DistanceTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 56 |   { | 
|---|
 | 57 |     SUPER(DistanceTrigger, XMLPort, xmlelement, mode); | 
|---|
 | 58 |  | 
|---|
 | 59 |     XMLPortParam(DistanceTrigger, "distance", setDistance, getDistance, xmlelement, mode).defaultValues(100.0f); | 
|---|
| [6906] | 60 |     XMLPortParamLoadOnly(DistanceTrigger, "target", addTargets, xmlelement, mode).defaultValues("Pawn"); | 
|---|
 | 61 |     XMLPortParam(DistanceTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode); | 
|---|
| [2029] | 62 |   } | 
|---|
 | 63 |  | 
|---|
| [1693] | 64 |   void DistanceTrigger::addTarget(Ogre::Node* targetNode) | 
|---|
 | 65 |   { | 
|---|
 | 66 |     this->targetSet_.insert(targetNode); | 
|---|
 | 67 |   } | 
|---|
 | 68 |  | 
|---|
 | 69 |   void DistanceTrigger::removeTarget(Ogre::Node* targetNode) | 
|---|
 | 70 |   { | 
|---|
 | 71 |     int returnval = this->targetSet_.erase(targetNode); | 
|---|
 | 72 |     if (returnval == 0) | 
|---|
 | 73 |     { | 
|---|
 | 74 |       COUT(2) << "Warning: Node " << targetNode << " did not exist in targetSet of trigger " << this << " !" << std::endl; | 
|---|
 | 75 |       COUT(4) << "Content of targetSet of trigger " << this << " :" << std::endl; | 
|---|
 | 76 |       std::set<Ogre::Node*>::iterator it; | 
|---|
| [2029] | 77 |       for (it = this->targetSet_.begin(); it != this->targetSet_.end(); ++it) | 
|---|
| [1693] | 78 |       { | 
|---|
 | 79 |         COUT(4) << *it << std::endl; | 
|---|
 | 80 |       } | 
|---|
 | 81 |       COUT(4) << "End of targetSet of trigger " << this << std::endl; | 
|---|
 | 82 |     } | 
|---|
 | 83 |   } | 
|---|
 | 84 |  | 
|---|
| [1969] | 85 |   void DistanceTrigger::addTargets(const std::string& targets) | 
|---|
| [1693] | 86 |   { | 
|---|
| [1961] | 87 |     Identifier* targetId = ClassByString(targets); | 
|---|
| [3034] | 88 |  | 
|---|
| [2261] | 89 |     //! Checks whether the target is (or is derived from) a ControllableEntity. | 
|---|
| [6906] | 90 |     Identifier* pawnId = Class(Pawn); | 
|---|
 | 91 |     Identifier* distanceTriggerBeaconId = Class(DistanceTriggerBeacon); | 
|---|
 | 92 |     if(targetId->isA(pawnId) || targetId->isA(distanceTriggerBeaconId)) | 
|---|
| [2261] | 93 |     { | 
|---|
 | 94 |       this->setForPlayer(true); | 
|---|
 | 95 |     } | 
|---|
| [3034] | 96 |  | 
|---|
| [2069] | 97 |     if (!targetId) | 
|---|
| [2071] | 98 |     { | 
|---|
| [6417] | 99 |         COUT(1) << "Error: \"" << targets << "\" is not a valid class name to include in ClassTreeMask (in " << this->getName() << ", class " << this->getIdentifier()->getName() << ')' << std::endl; | 
|---|
| [2069] | 100 |         return; | 
|---|
| [2071] | 101 |     } | 
|---|
| [2069] | 102 |  | 
|---|
 | 103 |     this->targetMask_.include(targetId); | 
|---|
 | 104 |  | 
|---|
| [1693] | 105 |     // trigger shouldn't react on itself or other triggers | 
|---|
| [2069] | 106 |     this->targetMask_.exclude(Class(Trigger), true); | 
|---|
| [1693] | 107 |  | 
|---|
| [2069] | 108 |     // we only want WorldEntities | 
|---|
 | 109 |     ClassTreeMask WEMask; | 
|---|
 | 110 |     WEMask.include(Class(WorldEntity)); | 
|---|
 | 111 |     this->targetMask_ *= WEMask; | 
|---|
| [3033] | 112 |  | 
|---|
 | 113 |     this->notifyMaskUpdate(); | 
|---|
| [1693] | 114 |   } | 
|---|
 | 115 |  | 
|---|
| [1969] | 116 |   void DistanceTrigger::removeTargets(const std::string& targets) | 
|---|
| [1693] | 117 |   { | 
|---|
| [1961] | 118 |     Identifier* targetId = ClassByString(targets); | 
|---|
| [2069] | 119 |     this->targetMask_.exclude(targetId); | 
|---|
| [1693] | 120 |   } | 
|---|
 | 121 |  | 
|---|
 | 122 |   bool DistanceTrigger::checkDistance() | 
|---|
 | 123 |   { | 
|---|
| [2069] | 124 |     // Iterate through all objects | 
|---|
 | 125 |     for (ClassTreeMaskObjectIterator it = this->targetMask_.begin(); it != this->targetMask_.end(); ++it) | 
|---|
| [1693] | 126 |     { | 
|---|
| [3325] | 127 |       WorldEntity* entity = orxonox_cast<WorldEntity*>(*it); | 
|---|
| [2069] | 128 |       if (!entity) | 
|---|
 | 129 |         continue; | 
|---|
 | 130 |  | 
|---|
| [7301] | 131 |       // If the DistanceTrigger is in single-target mode. | 
|---|
| [6906] | 132 |       if(this->singleTargetMode_) | 
|---|
 | 133 |       { | 
|---|
| [7301] | 134 |         // If the object that is a target is no DistanceTriggerBeacon, then the DistanceTrigger can't be in single-target-mode. | 
|---|
| [6906] | 135 |         if(!(*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier())) | 
|---|
| [7301] | 136 |         { | 
|---|
| [6906] | 137 |           this->singleTargetMode_ = false; | 
|---|
| [7301] | 138 |           COUT(2) << "DistanceTrigger " << this->getName() << " (&" << this <<  ")" << "is in single-target mode but the target is '" << entity->getIdentifier()->getName() << "' instead of DistanceTriggerBeacon. Setting single-target mode to false." << std::endl; | 
|---|
 | 139 |         } | 
|---|
 | 140 |         // If the target name and the name of the DistancTriggerBeacon don't match. | 
|---|
| [6906] | 141 |         else if(entity->getName().compare(this->targetName_) != 0) | 
|---|
 | 142 |           continue; | 
|---|
 | 143 |       } | 
|---|
| [7163] | 144 |  | 
|---|
| [2069] | 145 |       Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); | 
|---|
 | 146 |       if (distanceVec.length() < this->distance_) | 
|---|
| [2261] | 147 |       { | 
|---|
| [3034] | 148 |  | 
|---|
 | 149 |         // If the target is a player (resp. is a, or is derived from a, ControllableEntity) the triggeringPlayer is set to the target entity. | 
|---|
| [2261] | 150 |         if(this->isForPlayer()) | 
|---|
| [3034] | 151 |         { | 
|---|
| [6906] | 152 |  | 
|---|
| [7301] | 153 |           // Change the entity to the parent of the DistanceTriggerBeacon (if in single-target-mode), which is the entity to which the beacon is attached. | 
|---|
| [6906] | 154 |           if(this->singleTargetMode_) | 
|---|
 | 155 |             entity = entity->getParent(); | 
|---|
 | 156 |  | 
|---|
| [3325] | 157 |           Pawn* player = orxonox_cast<Pawn*>(entity); | 
|---|
| [3034] | 158 |           this->setTriggeringPlayer(player); | 
|---|
 | 159 |         } | 
|---|
 | 160 |  | 
|---|
| [2069] | 161 |         return true; | 
|---|
| [2261] | 162 |       } | 
|---|
| [1693] | 163 |     } | 
|---|
| [7163] | 164 |  | 
|---|
| [1693] | 165 |     return false; | 
|---|
 | 166 |   } | 
|---|
 | 167 |  | 
|---|
| [3280] | 168 |   bool DistanceTrigger::isTriggered(TriggerMode::Value mode) | 
|---|
| [1693] | 169 |   { | 
|---|
| [2029] | 170 |     if (Trigger::isTriggered(mode)) | 
|---|
| [2261] | 171 |     { | 
|---|
| [1693] | 172 |       return checkDistance(); | 
|---|
| [2261] | 173 |     } | 
|---|
| [1693] | 174 |     else | 
|---|
 | 175 |       return false; | 
|---|
 | 176 |   } | 
|---|
 | 177 | } | 
|---|