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