[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 |
---|
[6412] | 24 | * Reto Grieder |
---|
[2254] | 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "MultiStateEngine.h" |
---|
| 31 | |
---|
[6412] | 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" |
---|
[6412] | 39 | #include "core/LuaState.h" |
---|
[2254] | 40 | #include "core/XMLPort.h" |
---|
[6412] | 41 | #include "worldentities/EffectContainer.h" |
---|
[5735] | 42 | #include "worldentities/pawns/SpaceShip.h" |
---|
[6412] | 43 | #include "sound/WorldSound.h" |
---|
[2254] | 44 | |
---|
| 45 | namespace orxonox |
---|
| 46 | { |
---|
[6412] | 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 | |
---|
[6412] | 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; |
---|
[6412] | 72 | this->oldState_ = 0; |
---|
[2254] | 73 | |
---|
[6412] | 74 | this->setSyncMode(ObjectDirection::Bidirectional); |
---|
[2254] | 75 | this->registerVariables(); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | MultiStateEngine::~MultiStateEngine() |
---|
| 79 | { |
---|
[6412] | 80 | if (this->isInitialized()) |
---|
[2254] | 81 | { |
---|
[6412] | 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); |
---|
[6412] | 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 | |
---|
| 111 | void MultiStateEngine::tick(float dt) |
---|
| 112 | { |
---|
| 113 | if (this->getShip()) |
---|
| 114 | { |
---|
[6412] | 115 | const Vector3& velocity = this->getShip()->getLocalVelocity(); |
---|
| 116 | |
---|
[2254] | 117 | if (this->getShip()->hasLocalController()) |
---|
| 118 | { |
---|
| 119 | const Vector3& direction = this->getDirection(); |
---|
[6412] | 120 | bool forward = (direction.z < 0.0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD); |
---|
[2254] | 121 | |
---|
[6412] | 122 | this->state_ = 0; |
---|
| 123 | if (this->getShip()->getBoost() && forward) |
---|
| 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 |
---|
[6412] | 130 | this->state_ = Idle; |
---|
| 131 | } |
---|
[2254] | 132 | |
---|
[6412] | 133 | if (GameMode::isMaster()) |
---|
| 134 | { |
---|
| 135 | int changes = this->state_ | this->oldState_; |
---|
[2254] | 136 | |
---|
[6412] | 137 | float pitch = velocity.length(); |
---|
| 138 | if (this->state_ & Normal) |
---|
| 139 | defEngineSndNormal_->setPitch(clamp(pitch/MAX_VELOCITY_NORMAL + 1, 0.5f, 2.0f)); |
---|
| 140 | if (this->state_ & Boost) |
---|
| 141 | defEngineSndBoost_->setPitch(clamp(pitch/MAX_VELOCITY_BOOST + 1, 0.5f, 2.0f)); |
---|
[2254] | 142 | |
---|
[6412] | 143 | if (changes & Idle) |
---|
| 144 | { |
---|
| 145 | lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Idle); |
---|
| 146 | lua_setglobal(this->lua_->getInternalLuaState(), "idle"); |
---|
| 147 | } |
---|
| 148 | if (changes & Normal) |
---|
| 149 | { |
---|
| 150 | lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Normal); |
---|
| 151 | lua_setglobal(this->lua_->getInternalLuaState(), "normal"); |
---|
| 152 | if (this->state_ & Normal) |
---|
| 153 | defEngineSndNormal_->play(); |
---|
| 154 | else |
---|
| 155 | defEngineSndNormal_->stop(); |
---|
| 156 | } |
---|
| 157 | if (changes & Brake) |
---|
| 158 | { |
---|
| 159 | lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Brake); |
---|
| 160 | lua_setglobal(this->lua_->getInternalLuaState(), "brake"); |
---|
| 161 | } |
---|
| 162 | if (changes & Boost) |
---|
| 163 | { |
---|
| 164 | lua_pushboolean(this->lua_->getInternalLuaState(), this->state_ & Boost); |
---|
| 165 | lua_setglobal(this->lua_->getInternalLuaState(), "boost"); |
---|
| 166 | if (this->state_ & Boost) |
---|
| 167 | defEngineSndBoost_->play(); |
---|
| 168 | else |
---|
| 169 | defEngineSndBoost_->stop(); |
---|
| 170 | } |
---|
[2254] | 171 | |
---|
[6412] | 172 | this->oldState_ = this->state_; |
---|
| 173 | |
---|
| 174 | // Update all effect conditions |
---|
| 175 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
| 176 | (*it)->updateCondition(); |
---|
[2254] | 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
[2809] | 180 | SUPER(MultiStateEngine, tick, dt); |
---|
[2254] | 181 | } |
---|
| 182 | |
---|
| 183 | void MultiStateEngine::addToSpaceShip(SpaceShip* ship) |
---|
| 184 | { |
---|
| 185 | Engine::addToSpaceShip(ship); |
---|
| 186 | |
---|
| 187 | if (!ship) |
---|
| 188 | return; |
---|
| 189 | |
---|
[6412] | 190 | if( this->defEngineSndNormal_ ) |
---|
| 191 | this->getShip()->attach(defEngineSndNormal_); |
---|
| 192 | if( this->defEngineSndBoost_ ) |
---|
| 193 | this->getShip()->attach(defEngineSndBoost_); |
---|
[2254] | 194 | |
---|
[6412] | 195 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
| 196 | for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2) |
---|
| 197 | this->getShip()->attach(*it2); |
---|
[2254] | 198 | } |
---|
| 199 | |
---|
[6412] | 200 | void MultiStateEngine::addEffectContainer(EffectContainer* effect) |
---|
[2254] | 201 | { |
---|
[6412] | 202 | if (effect == NULL) |
---|
| 203 | return; |
---|
| 204 | effect->setLuaState(this->lua_, 'f' + multi_cast<std::string>(this->effectContainers_.size())); |
---|
| 205 | this->effectContainers_.push_back(effect); |
---|
[2254] | 206 | if (this->getShip()) |
---|
[6412] | 207 | { |
---|
| 208 | for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it) |
---|
| 209 | this->getShip()->attach(*it); |
---|
| 210 | } |
---|
[2254] | 211 | } |
---|
| 212 | |
---|
[6412] | 213 | EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const |
---|
[2254] | 214 | { |
---|
| 215 | unsigned int i = 0; |
---|
[6412] | 216 | for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it) |
---|
[2254] | 217 | { |
---|
| 218 | if (i == index) |
---|
| 219 | return (*it); |
---|
| 220 | } |
---|
[6412] | 221 | return NULL; |
---|
[2254] | 222 | } |
---|
| 223 | |
---|
[6412] | 224 | void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound) |
---|
[2254] | 225 | { |
---|
[6412] | 226 | if( defEngineSndNormal_ ) |
---|
| 227 | defEngineSndNormal_->setSource(engineSound); |
---|
| 228 | else |
---|
| 229 | assert(0); // This should never happen, because soundpointer is only available on master |
---|
[2254] | 230 | } |
---|
| 231 | |
---|
[6412] | 232 | const std::string& MultiStateEngine::getDefEngSndNormal() |
---|
[2254] | 233 | { |
---|
[6412] | 234 | if( defEngineSndNormal_ ) |
---|
| 235 | return defEngineSndNormal_->getSource(); |
---|
| 236 | else |
---|
| 237 | assert(0); |
---|
| 238 | return BLANKSTRING; |
---|
[2254] | 239 | } |
---|
| 240 | |
---|
[6412] | 241 | void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound) |
---|
[2254] | 242 | { |
---|
[6412] | 243 | if( defEngineSndBoost_ ) |
---|
| 244 | defEngineSndBoost_->setSource(engineSound); |
---|
| 245 | else |
---|
| 246 | assert(0); |
---|
[2254] | 247 | } |
---|
[6412] | 248 | |
---|
| 249 | const std::string& MultiStateEngine::getDefEngSndBoost() |
---|
| 250 | { |
---|
| 251 | if( this->defEngineSndBoost_ ) |
---|
| 252 | return defEngineSndBoost_->getSource(); |
---|
| 253 | else |
---|
| 254 | assert(0); |
---|
| 255 | return BLANKSTRING; |
---|
| 256 | } |
---|
[2254] | 257 | } |
---|