| [2254] | 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 |  *      Fabian 'x3n' Landau | 
|---|
| [6417] | 24 |  *      Reto Grieder | 
|---|
| [2254] | 25 |  *   Co-authors: | 
|---|
 | 26 |  *      ... | 
|---|
 | 27 |  * | 
|---|
 | 28 |  */ | 
|---|
 | 29 |  | 
|---|
 | 30 | #include "MultiStateEngine.h" | 
|---|
 | 31 |  | 
|---|
| [6417] | 32 | extern "C" { | 
|---|
 | 33 | #include <lua.h> | 
|---|
 | 34 | } | 
|---|
 | 35 |  | 
|---|
 | 36 | #include "util/Convert.h" | 
|---|
| [3196] | 37 | #include "core/CoreIncludes.h" | 
|---|
| [2896] | 38 | #include "core/GameMode.h" | 
|---|
| [6417] | 39 | #include "core/LuaState.h" | 
|---|
| [2254] | 40 | #include "core/XMLPort.h" | 
|---|
| [6417] | 41 | #include "worldentities/EffectContainer.h" | 
|---|
| [5735] | 42 | #include "worldentities/pawns/SpaceShip.h" | 
|---|
| [6417] | 43 | #include "sound/WorldSound.h" | 
|---|
| [2254] | 44 |  | 
|---|
 | 45 | namespace orxonox | 
|---|
 | 46 | { | 
|---|
| [6417] | 47 |     static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 0; | 
|---|
 | 48 |     static const float MAX_VELOCITY_NORMAL = 111; | 
|---|
 | 49 |     static const float MAX_VELOCITY_BOOST = 221; | 
|---|
| [2254] | 50 |  | 
|---|
 | 51 |     CreateFactory(MultiStateEngine); | 
|---|
 | 52 |  | 
|---|
 | 53 |     MultiStateEngine::MultiStateEngine(BaseObject* creator) : Engine(creator) | 
|---|
 | 54 |     { | 
|---|
 | 55 |         RegisterObject(MultiStateEngine); | 
|---|
 | 56 |  | 
|---|
| [6417] | 57 |         if (GameMode::isMaster()) | 
|---|
 | 58 |         { | 
|---|
 | 59 |             this->defEngineSndNormal_ = new WorldSound(this); | 
|---|
 | 60 |             this->defEngineSndBoost_  = new WorldSound(this); | 
|---|
 | 61 |             this->defEngineSndNormal_->setLooping(true); | 
|---|
 | 62 |             this->defEngineSndBoost_->setLooping(true); | 
|---|
 | 63 |             this->lua_ = new LuaState(); | 
|---|
 | 64 |         } | 
|---|
 | 65 |         else | 
|---|
 | 66 |         { | 
|---|
 | 67 |             this->defEngineSndBoost_ = 0; | 
|---|
 | 68 |             this->defEngineSndNormal_ = 0; | 
|---|
 | 69 |             this->lua_ = 0; | 
|---|
 | 70 |         } | 
|---|
| [2254] | 71 |         this->state_ = 0; | 
|---|
| [6417] | 72 |         this->oldState_ = 0; | 
|---|
| [2254] | 73 |  | 
|---|
| [6417] | 74 |         this->setSyncMode(ObjectDirection::Bidirectional); | 
|---|
| [2254] | 75 |         this->registerVariables(); | 
|---|
 | 76 |     } | 
|---|
 | 77 |  | 
|---|
 | 78 |     MultiStateEngine::~MultiStateEngine() | 
|---|
 | 79 |     { | 
|---|
| [6417] | 80 |         if (this->isInitialized()) | 
|---|
| [2254] | 81 |         { | 
|---|
| [6417] | 82 |             if (!this->getShip()) | 
|---|
 | 83 |             { | 
|---|
 | 84 |                 // We have no ship, so the effects are not attached and won't be destroyed automatically | 
|---|
 | 85 |                 for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) | 
|---|
 | 86 |                     for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2) | 
|---|
 | 87 |                         (*it2)->destroy(); | 
|---|
 | 88 |                 if (this->defEngineSndNormal_) | 
|---|
 | 89 |                     this->defEngineSndNormal_->destroy(); | 
|---|
 | 90 |                 if (this->defEngineSndBoost_) | 
|---|
 | 91 |                     this->defEngineSndBoost_->destroy(); | 
|---|
 | 92 |             } | 
|---|
 | 93 |             if (this->lua_) | 
|---|
 | 94 |                 delete this->lua_; | 
|---|
| [2254] | 95 |         } | 
|---|
 | 96 |     } | 
|---|
 | 97 |  | 
|---|
 | 98 |     void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
 | 99 |     { | 
|---|
 | 100 |         SUPER(MultiStateEngine, XMLPort, xmlelement, mode); | 
|---|
| [6417] | 101 |         XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode); | 
|---|
 | 102 |         XMLPortParam(MultiStateEngine, "defEngineSndNormal",  setDefEngSndNormal,  getDefEngSndNormal,  xmlelement, mode); | 
|---|
 | 103 |         XMLPortParam(MultiStateEngine, "defEngineSndBoost",  setDefEngSndBoost,  getDefEngSndBoost,  xmlelement, mode); | 
|---|
| [2254] | 104 |     } | 
|---|
 | 105 |  | 
|---|
 | 106 |     void MultiStateEngine::registerVariables() | 
|---|
 | 107 |     { | 
|---|
| [3280] | 108 |         registerVariable(this->state_, VariableDirection::ToServer); | 
|---|
| [2254] | 109 |     } | 
|---|
 | 110 |  | 
|---|
| [8727] | 111 |     void MultiStateEngine::run(float dt) | 
|---|
| [2254] | 112 |     { | 
|---|
 | 113 |         if (this->getShip()) | 
|---|
 | 114 |         { | 
|---|
| [6417] | 115 |             const Vector3& velocity = this->getShip()->getLocalVelocity(); | 
|---|
 | 116 |  | 
|---|
| [2254] | 117 |             if (this->getShip()->hasLocalController()) | 
|---|
 | 118 |             { | 
|---|
| [8727] | 119 |                 const Vector3& direction = this->getSteering(); | 
|---|
| [6417] | 120 |                 bool forward = (direction.z < 0.0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD); | 
|---|
| [2254] | 121 |  | 
|---|
| [6417] | 122 |                 this->state_ = 0; | 
|---|
| [8727] | 123 |                 if (this->getShip()->isBoosting() && forward) | 
|---|
| [6417] | 124 |                     this->state_ = Boost; | 
|---|
 | 125 |                 else if (forward && !this->state_) // this->state_ == Boost | 
|---|
 | 126 |                     this->state_ = Normal; | 
|---|
 | 127 |                 else if (direction.z > 0.0 && velocity.z < 0.0) | 
|---|
 | 128 |                     this->state_ = Brake; | 
|---|
| [2254] | 129 |                 else | 
|---|
| [6417] | 130 |                     this->state_ = Idle; | 
|---|
| [6709] | 131 |  | 
|---|
 | 132 |                 if (this->state_ == Idle && this->getSpeedAdd() > 0) | 
|---|
 | 133 |                     this->state_ = Normal; | 
|---|
| [6417] | 134 |             } | 
|---|
| [2254] | 135 |  | 
|---|
| [6417] | 136 |             if (GameMode::isMaster()) | 
|---|
 | 137 |             { | 
|---|
 | 138 |                 int changes = this->state_ | this->oldState_; | 
|---|
| [2254] | 139 |  | 
|---|
| [6417] | 140 |                 float pitch = velocity.length(); | 
|---|
 | 141 |                 if (this->state_ & Normal) | 
|---|
 | 142 |                     defEngineSndNormal_->setPitch(clamp(pitch/MAX_VELOCITY_NORMAL + 1, 0.5f, 2.0f)); | 
|---|
 | 143 |                 if (this->state_ & Boost) | 
|---|
 | 144 |                     defEngineSndBoost_->setPitch(clamp(pitch/MAX_VELOCITY_BOOST + 1, 0.5f, 2.0f)); | 
|---|
| [2254] | 145 |  | 
|---|
| [6417] | 146 |                 if (changes & Idle) | 
|---|
 | 147 |                 { | 
|---|
 | 148 |                     lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Idle); | 
|---|
 | 149 |                     lua_setglobal(this->lua_->getInternalLuaState(), "idle"); | 
|---|
 | 150 |                 } | 
|---|
 | 151 |                 if (changes & Normal) | 
|---|
 | 152 |                 { | 
|---|
 | 153 |                     lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Normal); | 
|---|
 | 154 |                     lua_setglobal(this->lua_->getInternalLuaState(), "normal"); | 
|---|
 | 155 |                     if (this->state_ & Normal) | 
|---|
 | 156 |                         defEngineSndNormal_->play(); | 
|---|
 | 157 |                     else | 
|---|
 | 158 |                         defEngineSndNormal_->stop(); | 
|---|
 | 159 |                 } | 
|---|
 | 160 |                 if (changes & Brake) | 
|---|
 | 161 |                 { | 
|---|
 | 162 |                     lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Brake); | 
|---|
 | 163 |                     lua_setglobal(this->lua_->getInternalLuaState(), "brake"); | 
|---|
 | 164 |                 } | 
|---|
 | 165 |                 if (changes & Boost) | 
|---|
 | 166 |                 { | 
|---|
 | 167 |                     lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Boost); | 
|---|
 | 168 |                     lua_setglobal(this->lua_->getInternalLuaState(), "boost"); | 
|---|
 | 169 |                     if (this->state_ & Boost) | 
|---|
 | 170 |                         defEngineSndBoost_->play(); | 
|---|
 | 171 |                     else | 
|---|
 | 172 |                         defEngineSndBoost_->stop(); | 
|---|
 | 173 |                 } | 
|---|
| [2254] | 174 |  | 
|---|
| [6417] | 175 |                 this->oldState_ = this->state_; | 
|---|
 | 176 |  | 
|---|
 | 177 |                 // Update all effect conditions | 
|---|
 | 178 |                 for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) | 
|---|
 | 179 |                     (*it)->updateCondition(); | 
|---|
| [2254] | 180 |             } | 
|---|
 | 181 |         } | 
|---|
 | 182 |  | 
|---|
| [8727] | 183 |         Engine::run(dt); | 
|---|
| [2254] | 184 |     } | 
|---|
 | 185 |  | 
|---|
 | 186 |     void MultiStateEngine::addToSpaceShip(SpaceShip* ship) | 
|---|
 | 187 |     { | 
|---|
 | 188 |         Engine::addToSpaceShip(ship); | 
|---|
 | 189 |  | 
|---|
 | 190 |         if (!ship) | 
|---|
 | 191 |             return; | 
|---|
 | 192 |  | 
|---|
| [6417] | 193 |         if( this->defEngineSndNormal_ ) | 
|---|
 | 194 |             this->getShip()->attach(defEngineSndNormal_); | 
|---|
 | 195 |         if( this->defEngineSndBoost_ ) | 
|---|
 | 196 |             this->getShip()->attach(defEngineSndBoost_); | 
|---|
| [2254] | 197 |  | 
|---|
| [6417] | 198 |         for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) | 
|---|
 | 199 |             for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2) | 
|---|
 | 200 |                 this->getShip()->attach(*it2); | 
|---|
| [2254] | 201 |     } | 
|---|
 | 202 |  | 
|---|
| [6417] | 203 |     void MultiStateEngine::addEffectContainer(EffectContainer* effect) | 
|---|
| [2254] | 204 |     { | 
|---|
| [6417] | 205 |         if (effect == NULL) | 
|---|
 | 206 |             return; | 
|---|
 | 207 |         effect->setLuaState(this->lua_, 'f' + multi_cast<std::string>(this->effectContainers_.size())); | 
|---|
 | 208 |         this->effectContainers_.push_back(effect); | 
|---|
| [2254] | 209 |         if (this->getShip()) | 
|---|
| [6417] | 210 |         { | 
|---|
 | 211 |             for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it) | 
|---|
 | 212 |                 this->getShip()->attach(*it); | 
|---|
 | 213 |         } | 
|---|
| [2254] | 214 |     } | 
|---|
 | 215 |  | 
|---|
| [6417] | 216 |     EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const | 
|---|
| [2254] | 217 |     { | 
|---|
 | 218 |         unsigned int i = 0; | 
|---|
| [6417] | 219 |         for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) | 
|---|
| [2254] | 220 |         { | 
|---|
 | 221 |             if (i == index) | 
|---|
 | 222 |                 return (*it); | 
|---|
 | 223 |         } | 
|---|
| [6417] | 224 |         return NULL; | 
|---|
| [2254] | 225 |     } | 
|---|
 | 226 |  | 
|---|
| [6417] | 227 |     void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound) | 
|---|
| [2254] | 228 |     { | 
|---|
| [6417] | 229 |         if( defEngineSndNormal_ ) | 
|---|
 | 230 |             defEngineSndNormal_->setSource(engineSound); | 
|---|
 | 231 |         else | 
|---|
 | 232 |             assert(0); // This should never happen, because soundpointer is only available on master | 
|---|
| [2254] | 233 |     } | 
|---|
 | 234 |  | 
|---|
| [6417] | 235 |     const std::string& MultiStateEngine::getDefEngSndNormal() | 
|---|
| [2254] | 236 |     { | 
|---|
| [6417] | 237 |         if( defEngineSndNormal_ ) | 
|---|
 | 238 |             return defEngineSndNormal_->getSource(); | 
|---|
 | 239 |         else | 
|---|
 | 240 |             assert(0); | 
|---|
 | 241 |         return BLANKSTRING; | 
|---|
| [2254] | 242 |     } | 
|---|
 | 243 |  | 
|---|
| [6417] | 244 |     void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound) | 
|---|
| [2254] | 245 |     { | 
|---|
| [6417] | 246 |         if( defEngineSndBoost_ ) | 
|---|
 | 247 |             defEngineSndBoost_->setSource(engineSound); | 
|---|
 | 248 |         else | 
|---|
 | 249 |             assert(0); | 
|---|
| [2254] | 250 |     } | 
|---|
| [6417] | 251 |  | 
|---|
 | 252 |     const std::string& MultiStateEngine::getDefEngSndBoost() | 
|---|
 | 253 |     { | 
|---|
 | 254 |         if( this->defEngineSndBoost_ ) | 
|---|
 | 255 |             return defEngineSndBoost_->getSource(); | 
|---|
 | 256 |         else | 
|---|
 | 257 |             assert(0); | 
|---|
 | 258 |         return BLANKSTRING; | 
|---|
 | 259 |     } | 
|---|
| [2254] | 260 | } | 
|---|