[6800] | 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 | * Damian 'Mozork' Frick |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[6857] | 29 | /** |
---|
| 30 | @file DistanceMultiTrigger.cc |
---|
| 31 | @brief Implementation of the DistanceMultiTrigger class. |
---|
[7601] | 32 | @ingroup MultiTrigger |
---|
[6857] | 33 | */ |
---|
| 34 | |
---|
[6800] | 35 | #include "DistanceMultiTrigger.h" |
---|
| 36 | |
---|
| 37 | #include "core/CoreIncludes.h" |
---|
| 38 | #include "core/XMLPort.h" |
---|
[7601] | 39 | |
---|
[6906] | 40 | #include "DistanceTriggerBeacon.h" |
---|
[6800] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[7163] | 44 | |
---|
[8206] | 45 | /*static*/ const std::string DistanceMultiTrigger::beaconModeOff_s = "off"; |
---|
| 46 | /*static*/ const std::string DistanceMultiTrigger::beaconModeIdentify_s = "identify"; |
---|
| 47 | /*static*/ const std::string DistanceMultiTrigger::beaconModeExlcude_s = "exclude"; |
---|
| 48 | |
---|
[6800] | 49 | CreateFactory(DistanceMultiTrigger); |
---|
[6857] | 50 | |
---|
| 51 | /** |
---|
| 52 | @brief |
---|
[7163] | 53 | Default Constructor. Registers the object and initializes default values. |
---|
[6857] | 54 | */ |
---|
[8206] | 55 | DistanceMultiTrigger::DistanceMultiTrigger(BaseObject* creator) : MultiTrigger(creator), beaconMask_(NULL) |
---|
[6800] | 56 | { |
---|
| 57 | RegisterObject(DistanceMultiTrigger); |
---|
[7163] | 58 | |
---|
[6800] | 59 | this->distance_ = 100.0f; |
---|
[8206] | 60 | this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::off); |
---|
[8079] | 61 | this->targetName_ = ""; |
---|
[6800] | 62 | } |
---|
[6857] | 63 | |
---|
| 64 | /** |
---|
| 65 | @brief |
---|
| 66 | Destructor. |
---|
| 67 | */ |
---|
[6800] | 68 | DistanceMultiTrigger::~DistanceMultiTrigger() |
---|
| 69 | { |
---|
[8206] | 70 | if(this->beaconMask_ != NULL) |
---|
| 71 | delete this->beaconMask_; |
---|
[6800] | 72 | } |
---|
[6857] | 73 | |
---|
| 74 | /** |
---|
| 75 | @brief |
---|
| 76 | Method for creating a DistanceMultiTrigger object through XML. |
---|
| 77 | */ |
---|
[6800] | 78 | void DistanceMultiTrigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 79 | { |
---|
| 80 | SUPER(DistanceMultiTrigger, XMLPort, xmlelement, mode); |
---|
| 81 | |
---|
[6857] | 82 | XMLPortParam(DistanceMultiTrigger, "distance", setDistance, getDistance, xmlelement, mode); |
---|
[8206] | 83 | XMLPortParam(DistanceMultiTrigger, "beaconMode", setBeaconMode, getBeaconMode, xmlelement, mode); |
---|
[6906] | 84 | XMLPortParam(DistanceMultiTrigger, "targetname", setTargetName, getTargetName, xmlelement, mode); |
---|
[6800] | 85 | } |
---|
[6857] | 86 | |
---|
| 87 | /** |
---|
| 88 | @brief |
---|
| 89 | This method is called by the MultiTrigger to get information about new trigger events that need to be looked at. |
---|
| 90 | |
---|
| 91 | In this implementation we iterate through all possible objects and check whether the fact that they are in range or not has changed and fire and hand a state ofer to the MultiTrigger if so. |
---|
[7301] | 92 | @return |
---|
| 93 | Returns a pointer to a queue of MultiTriggerState pointers, containing all the necessary information to decide whether these states should indeed become new states of the MultiTrigger. |
---|
[6857] | 94 | */ |
---|
[6800] | 95 | std::queue<MultiTriggerState*>* DistanceMultiTrigger::letTrigger(void) |
---|
| 96 | { |
---|
[6857] | 97 | |
---|
[6800] | 98 | std::queue<MultiTriggerState*>* queue = NULL; |
---|
[6857] | 99 | |
---|
| 100 | // Check for objects that were in range but no longer are. Iterate through all objects, that are in range. |
---|
[6860] | 101 | for(std::map<WorldEntity*, WeakPtr<WorldEntity>* >::iterator it = this->range_.begin(); it != this->range_.end(); ) |
---|
[6857] | 102 | { |
---|
[6860] | 103 | WorldEntity* entity = it->second->get(); |
---|
| 104 | WorldEntity* key = it->first; |
---|
[7301] | 105 | it++; // Incrementing the iterator in advance, since we don't need the current anymore and we potentially are going to delete the current element thus invalidating the iterator. |
---|
| 106 | // If the entity no longer exists. |
---|
[6860] | 107 | if(entity == NULL) |
---|
| 108 | { |
---|
| 109 | this->removeFromRange(key); |
---|
| 110 | continue; |
---|
| 111 | } |
---|
[7163] | 112 | |
---|
[6860] | 113 | Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); |
---|
[6857] | 114 | // If the object is no longer in range. |
---|
| 115 | if (distanceVec.length() > this->distance_) |
---|
| 116 | { |
---|
[7301] | 117 | // If for some reason the entity could not be removed. |
---|
[6864] | 118 | if(!this->removeFromRange(key)) |
---|
[6857] | 119 | continue; |
---|
| 120 | |
---|
| 121 | // If no queue has been created, yet. |
---|
| 122 | if(queue == NULL) |
---|
| 123 | queue = new std::queue<MultiTriggerState*>(); |
---|
| 124 | |
---|
| 125 | // Create a state and append it to the queue. |
---|
| 126 | MultiTriggerState* state = new MultiTriggerState; |
---|
[7301] | 127 | state->bTriggered = false; // Because the entity went out of range. |
---|
[6860] | 128 | state->originator = entity; |
---|
[6857] | 129 | queue->push(state); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
[6800] | 133 | // Check for new objects that are in range |
---|
[8206] | 134 | ClassTreeMask targetMask = this->getTargetMask(); |
---|
| 135 | if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify) |
---|
| 136 | targetMask = *this->beaconMask_; |
---|
| 137 | |
---|
| 138 | for(ClassTreeMaskObjectIterator it = targetMask.begin(); it != targetMask.end(); ++it) |
---|
[6800] | 139 | { |
---|
[6860] | 140 | WorldEntity* entity = static_cast<WorldEntity*>(*it); |
---|
[6800] | 141 | |
---|
[8206] | 142 | // If the DistanceMultiTrigger is in identify mode and the DistanceTriggerBeacon attached to the object has the wrong name we ignore it. |
---|
| 143 | if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify) |
---|
[6906] | 144 | { |
---|
[8206] | 145 | if(entity->getName() != this->targetName_) |
---|
| 146 | continue; |
---|
| 147 | // If the object, the DistanceTriggerBeacon is attached to, is not a target of this DistanceMultiTrigger. |
---|
| 148 | else if(this->getTargetMask().isExcluded(entity->getParent()->getIdentifier())) |
---|
| 149 | continue; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | // If the DistanceMultiTrigger is in exclude mode and the DistanceTriggerBeacon attached to the object has the right name, we ignore it. |
---|
| 153 | if(this->beaconMode_ == distanceMultiTriggerBeaconMode::exclude) |
---|
| 154 | { |
---|
| 155 | |
---|
| 156 | const std::set<WorldEntity*> attached = entity->getAttachedObjects(); |
---|
| 157 | bool found = false; |
---|
| 158 | for(std::set<WorldEntity*>::const_iterator it = attached.begin(); it != attached.end(); it++) |
---|
[7301] | 159 | { |
---|
[8206] | 160 | if((*it)->isA(ClassIdentifier<DistanceTriggerBeacon>::getIdentifier()) && static_cast<DistanceTriggerBeacon*>(*it)->getName() == this->targetName_) |
---|
| 161 | { |
---|
| 162 | found = true; |
---|
| 163 | break; |
---|
| 164 | } |
---|
[7301] | 165 | } |
---|
[8206] | 166 | if(found) |
---|
[6906] | 167 | continue; |
---|
| 168 | } |
---|
| 169 | |
---|
[6800] | 170 | Vector3 distanceVec = entity->getWorldPosition() - this->getWorldPosition(); |
---|
[6857] | 171 | // If the object is in range. |
---|
| 172 | if (distanceVec.length() <= this->distance_) |
---|
[6800] | 173 | { |
---|
[6857] | 174 | // Add the object to the objects that are in range. |
---|
[7301] | 175 | // Objects that already are in range are not added twice, because in a map (this->range_) each key can only exist once and thus addToRange() will reject all attempts of duplicate entries. |
---|
[6800] | 176 | if(!this->addToRange(entity)) |
---|
| 177 | continue; |
---|
[6857] | 178 | |
---|
[8206] | 179 | // Change the entity to the parent of the DistanceTriggerBeacon (if in identify-mode), which is the entity to which the beacon is attached. |
---|
| 180 | if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify) |
---|
[6906] | 181 | entity = entity->getParent(); |
---|
[7163] | 182 | |
---|
[6857] | 183 | // If no queue has been created, yet. |
---|
[6800] | 184 | if(queue == NULL) |
---|
| 185 | queue = new std::queue<MultiTriggerState*>(); |
---|
[6857] | 186 | |
---|
| 187 | // Create a state and append it to the queue. |
---|
[6800] | 188 | MultiTriggerState* state = new MultiTriggerState; |
---|
[7301] | 189 | state->bTriggered = true; // Because the entity came into range. |
---|
[6800] | 190 | state->originator = entity; |
---|
| 191 | queue->push(state); |
---|
| 192 | } |
---|
| 193 | } |
---|
[7163] | 194 | |
---|
[6800] | 195 | return queue; |
---|
| 196 | } |
---|
[8206] | 197 | |
---|
[7301] | 198 | /** |
---|
| 199 | @brief |
---|
[8206] | 200 | Set the beacon mode. |
---|
| 201 | @param mode |
---|
| 202 | The mode as an enum. |
---|
[7301] | 203 | */ |
---|
[8206] | 204 | void DistanceMultiTrigger::setBeaconModeDirect(distanceMultiTriggerBeaconMode::Value mode) |
---|
[7301] | 205 | { |
---|
[8206] | 206 | this->beaconMode_ = mode; |
---|
| 207 | if(this->beaconMode_ == distanceMultiTriggerBeaconMode::identify && this->beaconMask_ == NULL) |
---|
| 208 | { |
---|
| 209 | this->beaconMask_ = new ClassTreeMask(); |
---|
| 210 | this->beaconMask_->exclude(Class(BaseObject)); |
---|
| 211 | this->beaconMask_->include(Class(DistanceTriggerBeacon)); |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | @brief |
---|
| 217 | Get the beacon mode. |
---|
| 218 | @return |
---|
| 219 | Returns the mode as a string. |
---|
| 220 | */ |
---|
| 221 | const std::string& DistanceMultiTrigger::getBeaconMode(void) const |
---|
| 222 | { |
---|
| 223 | switch(this->getBeaconModeDirect()) |
---|
| 224 | { |
---|
| 225 | case distanceMultiTriggerBeaconMode::off : |
---|
| 226 | return DistanceMultiTrigger::beaconModeOff_s; |
---|
| 227 | case distanceMultiTriggerBeaconMode::identify: |
---|
| 228 | return DistanceMultiTrigger::beaconModeIdentify_s; |
---|
| 229 | case distanceMultiTriggerBeaconMode::exclude: |
---|
| 230 | return DistanceMultiTrigger::beaconModeExlcude_s; |
---|
| 231 | default : |
---|
| 232 | assert(0); // This is impossible. |
---|
| 233 | return BLANKSTRING; |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | /** |
---|
| 238 | @brief |
---|
| 239 | Set the beacon mode. |
---|
| 240 | @param mode |
---|
| 241 | The mode as a string. |
---|
| 242 | */ |
---|
| 243 | void DistanceMultiTrigger::setBeaconMode(const std::string& mode) |
---|
| 244 | { |
---|
| 245 | if(mode == DistanceMultiTrigger::beaconModeOff_s) |
---|
| 246 | this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::off); |
---|
| 247 | else if(mode == DistanceMultiTrigger::beaconModeIdentify_s) |
---|
| 248 | this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::identify); |
---|
| 249 | else if(mode == DistanceMultiTrigger::beaconModeExlcude_s) |
---|
| 250 | this->setBeaconModeDirect(distanceMultiTriggerBeaconMode::exclude); |
---|
[7301] | 251 | else |
---|
[8206] | 252 | COUT(1) << "Invalid beacon mode in DistanceMultiTrigger." << endl; |
---|
[7301] | 253 | } |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | @brief |
---|
| 257 | Add a given entity to the entities, that currently are in range of the DistanceMultiTrigger. |
---|
| 258 | @param entity |
---|
| 259 | A pointer to the entity. |
---|
| 260 | @return |
---|
| 261 | Returns true if successful, false if not. |
---|
| 262 | */ |
---|
| 263 | bool DistanceMultiTrigger::addToRange(WorldEntity* entity) |
---|
| 264 | { |
---|
| 265 | WeakPtr<WorldEntity>* weakptr = new WeakPtr<WorldEntity>(entity); |
---|
| 266 | std::pair<std::map<WorldEntity*, WeakPtr<WorldEntity>* >::iterator, bool> pair = this->range_.insert(std::pair<WorldEntity*, WeakPtr<WorldEntity>* >(entity, weakptr)); |
---|
| 267 | |
---|
| 268 | if(!pair.second) |
---|
| 269 | { |
---|
| 270 | delete weakptr; |
---|
| 271 | return false; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | return true; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | @brief |
---|
| 279 | Remove a given entity from the set of entities, that currently are in range of the DistanceMultiTrigger. |
---|
| 280 | @param entity |
---|
| 281 | A pointer ot the entity. |
---|
| 282 | @return |
---|
| 283 | Returns true if successful. |
---|
| 284 | */ |
---|
| 285 | bool DistanceMultiTrigger::removeFromRange(WorldEntity* entity) |
---|
| 286 | { |
---|
| 287 | WeakPtr<WorldEntity>* weakptr = this->range_.find(entity)->second; |
---|
| 288 | bool erased = this->range_.erase(entity) > 0; |
---|
| 289 | if(erased) |
---|
| 290 | delete weakptr; |
---|
| 291 | return erased; |
---|
| 292 | } |
---|
| 293 | |
---|
[6800] | 294 | } |
---|