| 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 "OrxonoxStableHeaders.h" | 
|---|
| 30 | #include "Trigger.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include <OgreBillboard.h> | 
|---|
| 33 | #include "util/Debug.h" | 
|---|
| 34 | #include "core/CoreIncludes.h" | 
|---|
| 35 | #include "core/ConsoleCommand.h" | 
|---|
| 36 | #include "core/XMLPort.h" | 
|---|
| 37 | #include "objects/Scene.h" | 
|---|
| 38 |  | 
|---|
| 39 | namespace orxonox | 
|---|
| 40 | { | 
|---|
| 41 |  | 
|---|
| 42 |   SetConsoleCommand(Trigger, debugFlares, false).defaultValues(false); | 
|---|
| 43 |  | 
|---|
| 44 |   CreateFactory(Trigger); | 
|---|
| 45 |  | 
|---|
| 46 |   Trigger::Trigger(BaseObject* creator) : PositionableEntity(creator) | 
|---|
| 47 |   { | 
|---|
| 48 |     RegisterObject(Trigger); | 
|---|
| 49 |  | 
|---|
| 50 |     this->mode_ = TM_EventTriggerAND; | 
|---|
| 51 |  | 
|---|
| 52 |     this->bActive_ = false; | 
|---|
| 53 |     this->bTriggered_ = false; | 
|---|
| 54 |     this->latestState_ = 0x0; | 
|---|
| 55 |  | 
|---|
| 56 |     this->bInvertMode_ = false; | 
|---|
| 57 |     this->bSwitch_ = false; | 
|---|
| 58 |     this->bStayActive_ = false; | 
|---|
| 59 |     this->delay_ = 0.0; | 
|---|
| 60 |     this->remainingActivations_ = -1; | 
|---|
| 61 |  | 
|---|
| 62 | //    this->bUpdating_ = false; | 
|---|
| 63 |  | 
|---|
| 64 |     if (this->getScene() && this->getScene()->getSceneManager()) | 
|---|
| 65 |     { | 
|---|
| 66 |       this->debugBillboard_.setBillboardSet(this->getScene()->getSceneManager(), "Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1); | 
|---|
| 67 |       this->debugBillboard_.setVisible(false); | 
|---|
| 68 |     } | 
|---|
| 69 |  | 
|---|
| 70 |     this->getNode()->attachObject(this->debugBillboard_.getBillboardSet()); | 
|---|
| 71 |   } | 
|---|
| 72 |  | 
|---|
| 73 |   Trigger::~Trigger() | 
|---|
| 74 |   { | 
|---|
| 75 |   } | 
|---|
| 76 |  | 
|---|
| 77 |   void Trigger::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 78 |   { | 
|---|
| 79 |     SUPER(Trigger, XMLPort, xmlelement, mode); | 
|---|
| 80 |  | 
|---|
| 81 |     XMLPortParam(Trigger, "delay",       setDelay,       getDelay,       xmlelement, mode).defaultValues(0.0f); | 
|---|
| 82 |     XMLPortParam(Trigger, "switch",      setSwitch,      getSwitch,      xmlelement, mode).defaultValues(false); | 
|---|
| 83 |     XMLPortParam(Trigger, "stayactive",  setStayActive,  getStayActive,  xmlelement, mode).defaultValues(false); | 
|---|
| 84 |     XMLPortParam(Trigger, "activations", setActivations, getActivations, xmlelement, mode).defaultValues(-1); | 
|---|
| 85 |     XMLPortParam(Trigger, "invert",      setInvert,      getInvert,      xmlelement, mode).defaultValues(false); | 
|---|
| 86 |     XMLPortParamTemplate(Trigger, "mode", setMode, getModeString, xmlelement, mode, const std::string&).defaultValues("or"); | 
|---|
| 87 |  | 
|---|
| 88 |     XMLPortObject(Trigger, Trigger, "", addTrigger, getTrigger, xmlelement, mode); | 
|---|
| 89 |   } | 
|---|
| 90 |  | 
|---|
| 91 |   void Trigger::tick(float dt) | 
|---|
| 92 |   { | 
|---|
| 93 |  | 
|---|
| 94 |     bool newTriggered = this->isTriggered(); | 
|---|
| 95 |  | 
|---|
| 96 |     // check if new triggering event is really new | 
|---|
| 97 |     if ((this->latestState_ & 0x1) != newTriggered) | 
|---|
| 98 |     { | 
|---|
| 99 |       // create new state | 
|---|
| 100 |       if (newTriggered) | 
|---|
| 101 |       { | 
|---|
| 102 |         this->latestState_ |= 1; // set trigger bit to 1 | 
|---|
| 103 |         this->switchState(); | 
|---|
| 104 |       } | 
|---|
| 105 |       else | 
|---|
| 106 |       { | 
|---|
| 107 |         this->latestState_ &= 0xFE; // set trigger bit to 0 | 
|---|
| 108 |         if (this->bSwitch_) | 
|---|
| 109 |           this->storeState(); | 
|---|
| 110 |         else | 
|---|
| 111 |           this->switchState(); | 
|---|
| 112 |       } | 
|---|
| 113 |     } | 
|---|
| 114 |  | 
|---|
| 115 |     if (this->remainingTime_ > 0.0) | 
|---|
| 116 |     { | 
|---|
| 117 |       this->remainingTime_ -= dt; | 
|---|
| 118 |       // only increase when acctually waiting for a state in the queue | 
|---|
| 119 |       if (this->timeSinceLastEvent_ >= 0.0) | 
|---|
| 120 |         this->timeSinceLastEvent_ += dt; | 
|---|
| 121 |     } | 
|---|
| 122 |  | 
|---|
| 123 |     while (this->remainingTime_ <= 0.0 && this->stateChanges_.size() > 0) | 
|---|
| 124 |     { | 
|---|
| 125 |       // time ran out, change state to new one | 
|---|
| 126 |       char newState = this->stateChanges_.front().second; | 
|---|
| 127 |       this->bTriggered_ = (newState & 0x1); | 
|---|
| 128 |       this->bActive_ = newState & 2; | 
|---|
| 129 |       this->stateChanges_.pop(); | 
|---|
| 130 |       if (this->stateChanges_.size() != 0) | 
|---|
| 131 |         this->remainingTime_ = this->stateChanges_.front().first; | 
|---|
| 132 |       else | 
|---|
| 133 |         this->timeSinceLastEvent_ = this->delay_; | 
|---|
| 134 |     } | 
|---|
| 135 |  | 
|---|
| 136 |     if (this->bTriggered_ && this->bActive_) | 
|---|
| 137 |       this->setBillboardColour(ColourValue(0.5, 1.0, 0.0)); | 
|---|
| 138 |     else if (!this->bTriggered_ && this->bActive_) | 
|---|
| 139 |       this->setBillboardColour(ColourValue(0.0, 1.0, 0.0)); | 
|---|
| 140 |     else if (this->bTriggered_ && !this->bActive_) | 
|---|
| 141 |       this->setBillboardColour(ColourValue(1.0, 0.5, 0.0)); | 
|---|
| 142 |     else | 
|---|
| 143 |       this->setBillboardColour(ColourValue(1.0, 0.0, 0.0)); | 
|---|
| 144 |   } | 
|---|
| 145 |  | 
|---|
| 146 |   bool Trigger::isTriggered(TriggerMode mode) | 
|---|
| 147 |   { | 
|---|
| 148 | //    if (this->bUpdating_) | 
|---|
| 149 | //      return this->bTriggered_; | 
|---|
| 150 |  | 
|---|
| 151 | //    this->bUpdating_ = true; | 
|---|
| 152 |     if (this->children_.size() != 0) | 
|---|
| 153 |     { | 
|---|
| 154 |       bool returnval = false; | 
|---|
| 155 |  | 
|---|
| 156 |       switch (mode) | 
|---|
| 157 |       { | 
|---|
| 158 |         case TM_EventTriggerAND: | 
|---|
| 159 |           returnval = checkAnd(); | 
|---|
| 160 |           break; | 
|---|
| 161 |         case TM_EventTriggerOR: | 
|---|
| 162 |           returnval = checkOr(); | 
|---|
| 163 |           break; | 
|---|
| 164 |         case TM_EventTriggerXOR: | 
|---|
| 165 |           returnval = checkXor(); | 
|---|
| 166 |           break; | 
|---|
| 167 |         default: | 
|---|
| 168 |           returnval = false; | 
|---|
| 169 |           break; | 
|---|
| 170 |       } | 
|---|
| 171 | //      this->bUpdating_ = false; | 
|---|
| 172 |  | 
|---|
| 173 |       if (this->bInvertMode_) | 
|---|
| 174 |         return !returnval; | 
|---|
| 175 |       else | 
|---|
| 176 |         return returnval; | 
|---|
| 177 |     } | 
|---|
| 178 |     return true; | 
|---|
| 179 |   } | 
|---|
| 180 |  | 
|---|
| 181 |   bool Trigger::checkAnd() | 
|---|
| 182 |   { | 
|---|
| 183 |     std::set<Trigger*>::iterator it; | 
|---|
| 184 |     for(it = this->children_.begin(); it != this->children_.end(); ++it) | 
|---|
| 185 |     { | 
|---|
| 186 |       if (!(*it)->isActive()) | 
|---|
| 187 |         return false; | 
|---|
| 188 |     } | 
|---|
| 189 |     return true; | 
|---|
| 190 |   } | 
|---|
| 191 |  | 
|---|
| 192 |   bool Trigger::checkOr() | 
|---|
| 193 |   { | 
|---|
| 194 |     std::set<Trigger*>::iterator it; | 
|---|
| 195 |     for(it = this->children_.begin(); it != this->children_.end(); ++it) | 
|---|
| 196 |     { | 
|---|
| 197 |       if ((*it)->isActive()) | 
|---|
| 198 |         return true; | 
|---|
| 199 |     } | 
|---|
| 200 |     return false; | 
|---|
| 201 |   } | 
|---|
| 202 |  | 
|---|
| 203 |   bool Trigger::checkXor() | 
|---|
| 204 |   { | 
|---|
| 205 |     std::set<Trigger*>::iterator it; | 
|---|
| 206 |     bool test = false; | 
|---|
| 207 |     for(it = this->children_.begin(); it != this->children_.end(); ++it) | 
|---|
| 208 |     { | 
|---|
| 209 |       if (test && (*it)->isActive()) | 
|---|
| 210 |         return false; | 
|---|
| 211 |       if ((*it)->isActive()) | 
|---|
| 212 |         test = true; | 
|---|
| 213 |     } | 
|---|
| 214 |     return test; | 
|---|
| 215 |   } | 
|---|
| 216 |  | 
|---|
| 217 |   bool Trigger::switchState() | 
|---|
| 218 |   { | 
|---|
| 219 |     if (( (this->latestState_ & 2) && this->bStayActive_ && (this->remainingActivations_ <= 0)) | 
|---|
| 220 |      || (!(this->latestState_ & 2))                      && (this->remainingActivations_ == 0)) | 
|---|
| 221 |       return false; | 
|---|
| 222 |     else | 
|---|
| 223 |     { | 
|---|
| 224 |       this->latestState_ ^= 2; // toggle state bit | 
|---|
| 225 |  | 
|---|
| 226 |       // increase activation count | 
|---|
| 227 |       if (this->latestState_ & 2 && this->remainingActivations_ > 0) | 
|---|
| 228 |         this->remainingActivations_--; | 
|---|
| 229 |  | 
|---|
| 230 |       this->storeState(); | 
|---|
| 231 |  | 
|---|
| 232 |       return true; | 
|---|
| 233 |     } | 
|---|
| 234 |   } | 
|---|
| 235 |  | 
|---|
| 236 |   void Trigger::storeState() | 
|---|
| 237 |   { | 
|---|
| 238 |     // put state change into queue | 
|---|
| 239 |     this->stateChanges_.push(std::pair<float, char>(this->timeSinceLastEvent_, this->latestState_)); | 
|---|
| 240 |     // reset time since last event | 
|---|
| 241 |     this->timeSinceLastEvent_ = 0.0; | 
|---|
| 242 |  | 
|---|
| 243 |     if (this->stateChanges_.size() == 1) | 
|---|
| 244 |       this->remainingTime_ = this->stateChanges_.front().first; | 
|---|
| 245 |   } | 
|---|
| 246 |  | 
|---|
| 247 |   void Trigger::setDelay(float delay) | 
|---|
| 248 |   { | 
|---|
| 249 |     this->delay_ = delay; | 
|---|
| 250 |     this->timeSinceLastEvent_ = delay; | 
|---|
| 251 |   } | 
|---|
| 252 |  | 
|---|
| 253 |   void Trigger::setMode(const std::string& modeName) | 
|---|
| 254 |   { | 
|---|
| 255 |     if (modeName == "and") | 
|---|
| 256 |       this->setMode(TM_EventTriggerAND); | 
|---|
| 257 |     else if (modeName == "or") | 
|---|
| 258 |       this->setMode(TM_EventTriggerOR); | 
|---|
| 259 |     else if (modeName == "xor") | 
|---|
| 260 |       this->setMode(TM_EventTriggerXOR); | 
|---|
| 261 |   } | 
|---|
| 262 |  | 
|---|
| 263 |   std::string Trigger::getModeString() const | 
|---|
| 264 |   { | 
|---|
| 265 |     if (this->mode_ == TM_EventTriggerAND) | 
|---|
| 266 |       return std::string("and"); | 
|---|
| 267 |     else if (this->mode_ == TM_EventTriggerOR) | 
|---|
| 268 |       return std::string("or"); | 
|---|
| 269 |     else if (this->mode_ == TM_EventTriggerXOR) | 
|---|
| 270 |       return std::string("xor"); | 
|---|
| 271 |     else | 
|---|
| 272 |       return std::string("and"); | 
|---|
| 273 |   } | 
|---|
| 274 |  | 
|---|
| 275 |   void Trigger::addTrigger(Trigger* trigger) | 
|---|
| 276 |   { | 
|---|
| 277 |     if (this != trigger) | 
|---|
| 278 |       this->children_.insert(trigger); | 
|---|
| 279 |   } | 
|---|
| 280 |  | 
|---|
| 281 |   const Trigger* Trigger::getTrigger(unsigned int index) const | 
|---|
| 282 |   { | 
|---|
| 283 |     if (this->children_.size() <= index) | 
|---|
| 284 |       return NULL; | 
|---|
| 285 |  | 
|---|
| 286 |     std::set<Trigger*>::const_iterator it; | 
|---|
| 287 |     it = this->children_.begin(); | 
|---|
| 288 |  | 
|---|
| 289 |     for (unsigned int i = 0; i != index; ++i) | 
|---|
| 290 |       ++it; | 
|---|
| 291 |  | 
|---|
| 292 |     return (*it); | 
|---|
| 293 |   } | 
|---|
| 294 |  | 
|---|
| 295 |   void Trigger::debugFlares(bool bVisible) | 
|---|
| 296 |   { | 
|---|
| 297 |     for (ObjectList<Trigger>::iterator it = ObjectList<Trigger>::begin(); it != ObjectList<Trigger>::end(); ++it) | 
|---|
| 298 |       it->setVisible(bVisible); | 
|---|
| 299 |   } | 
|---|
| 300 |  | 
|---|
| 301 |   void Trigger::setBillboardColour(const ColourValue& colour) | 
|---|
| 302 |   { | 
|---|
| 303 |     this->debugBillboard_.getBillboardSet()->getBillboard(0)->setColour(colour); | 
|---|
| 304 |   } | 
|---|
| 305 |  | 
|---|
| 306 |   void Trigger::changedVisibility() | 
|---|
| 307 |   { | 
|---|
| 308 |     SUPER(Trigger, changedVisibility); | 
|---|
| 309 |  | 
|---|
| 310 |     this->debugBillboard_.setVisible(this->isVisible()); | 
|---|
| 311 |   } | 
|---|
| 312 | } | 
|---|