Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/items/MultiStateEngine.cc @ 6356

Last change on this file since 6356 was 6329, checked in by rgrieder, 15 years ago

Who the hell uses ints to synchronise part of a float vector? Well, I really don't know…

  • Property svn:eol-style set to native
File size: 9.1 KB
RevLine 
[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]32extern "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
46namespace 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
[6327]58        if (GameMode::isMaster())
[6313]59        {
60            this->defEngineSndNormal_ = new WorldSound(this);
[6327]61            this->defEngineSndBoost_  = new WorldSound(this);
[6313]62            this->defEngineSndNormal_->setLooping(true);
63            this->defEngineSndBoost_->setLooping(true);
[6327]64            this->lua_ = new LuaState();
[6313]65        }
66        else
67        {
68            this->defEngineSndBoost_ = 0;
69            this->defEngineSndNormal_ = 0;
[6327]70            this->lua_ = 0;
[6313]71        }
[2254]72        this->state_ = 0;
[6329]73        this->steeringDirectionZ_ = 0.0;
[2254]74
[6327]75        this->setSyncMode(ObjectDirection::Bidirectional);
[2254]76        this->registerVariables();
77    }
78
79    MultiStateEngine::~MultiStateEngine()
80    {
[6327]81        if (this->isInitialized())
[2254]82        {
[6327]83            if (!this->getShip())
84            {
85                // We have no ship, so the effects are not attached and won't be destroyed automatically
86                for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
87                    for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2)
88                        (*it2)->destroy();
89                if (this->defEngineSndNormal_)
90                    delete this->defEngineSndNormal_;
91                if (this->defEngineSndBoost_)
92                    delete this->defEngineSndBoost_;
93            }
94            if (this->lua_)
95                delete this->lua_;
[2254]96        }
97    }
98
99    void MultiStateEngine::XMLPort(Element& xmlelement, XMLPort::Mode mode)
100    {
101        SUPER(MultiStateEngine, XMLPort, xmlelement, mode);
[6187]102        XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode);
[6202]103        XMLPortParam(MultiStateEngine, "defEngineSndNormal",  setDefEngSndNormal,  getDefEngSndNormal,  xmlelement, mode);
[6207]104        XMLPortParam(MultiStateEngine, "defEngineSndBoost",  setDefEngSndBoost,  getDefEngSndBoost,  xmlelement, mode);
[2254]105    }
106
107    void MultiStateEngine::registerVariables()
108    {
[6327]109        registerVariable(this->steeringDirectionZ_, VariableDirection::ToServer);
[2254]110    }
111
112    void MultiStateEngine::tick(float dt)
113    {
114        if (this->getShip())
115        {
[6327]116            if (this->getShip()->hasLocalController())
[6329]117                this->steeringDirectionZ_ = this->getDirection().z;
[6327]118            if (GameMode::isMaster())
[2254]119            {
[2488]120                const Vector3& velocity = this->getShip()->getLocalVelocity();
[2254]121
[6202]122                float pitch = velocity.length();
[6329]123                bool forward = (this->steeringDirectionZ_ < 0.0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
[2254]124
[6187]125                int newState = 0;
126                if (this->getShip()->getBoost() && forward)
[6202]127                {
[6187]128                    newState = Boost;
[6327]129                    defEngineSndBoost_->setPitch(clamp(pitch/MAX_VELOCITY_BOOST + 1, 0.5f, 2.0f));
[6202]130                }
[6187]131                else if (forward && !newState) // newState == Boost
[6202]132                {
[6187]133                    newState = Normal;
[6327]134                    defEngineSndNormal_->setPitch(clamp(pitch/MAX_VELOCITY_NORMAL + 1, 0.5f, 2.0f));
[6202]135                }
[6329]136                else if (this->steeringDirectionZ_ > 0.0 && velocity.z < 0.0)
[6187]137                    newState = Brake;
[2254]138                else
[6187]139                    newState = Idle;
[2254]140
[6327]141                int changes = newState | this->state_;
142                if (changes & Idle)
[6187]143                {
[6327]144                    lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle);
145                    lua_setglobal(this->lua_->getInternalLuaState(), "idle");
146                }
147                if (changes & Normal)
148                {
149                    lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal);
150                    lua_setglobal(this->lua_->getInternalLuaState(), "normal");
151                    if (newState & Normal)
152                        defEngineSndNormal_->play();
153                    else
154                        defEngineSndNormal_->stop();
155                }
156                if (changes & Brake)
157                {
158                    lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake);
159                    lua_setglobal(this->lua_->getInternalLuaState(), "brake");
160                }
161                if (changes & Boost)
162                {
163                    lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost);
164                    lua_setglobal(this->lua_->getInternalLuaState(), "boost");
165                    if (newState & Boost)
166                        defEngineSndBoost_->play();
167                    else
168                        defEngineSndBoost_->stop();
169                }
[2254]170
[6327]171                this->state_ = newState;
[2254]172
[6327]173                // Update all effect conditions
174                for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
175                    (*it)->updateCondition();
[2254]176            }
177        }
178
[2809]179        SUPER(MultiStateEngine, tick, dt);
[2254]180    }
181
182    void MultiStateEngine::addToSpaceShip(SpaceShip* ship)
183    {
184        Engine::addToSpaceShip(ship);
185
186        if (!ship)
187            return;
188
[6313]189        if( this->defEngineSndNormal_ )
190            this->getShip()->attach(defEngineSndNormal_);
191        if( this->defEngineSndBoost_ )
192            this->getShip()->attach(defEngineSndBoost_);
[6202]193
[6187]194        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
195            for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2)
196                this->getShip()->attach(*it2);
[2254]197    }
198
[6187]199    void MultiStateEngine::addEffectContainer(EffectContainer* effect)
[2254]200    {
[6187]201        if (effect == NULL)
202            return;
203        effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size()));
204        this->effectContainers_.push_back(effect);
[2254]205        if (this->getShip())
206        {
[6187]207            for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it)
208                this->getShip()->attach(*it);
[2254]209        }
210    }
211
[6187]212    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
[2254]213    {
214        unsigned int i = 0;
[6187]215        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
[2254]216        {
217            if (i == index)
218                return (*it);
219        }
[6187]220        return NULL;
[2254]221    }
[6202]222
223    void MultiStateEngine::setDefEngSndNormal(const std::string &engineSound)
224    {
[6313]225        if( defEngineSndNormal_ )
226            defEngineSndNormal_->setSource(engineSound);
227        else
228            assert(0); // This should never happen, because soundpointer is only available on master
[6202]229    }
230
231    const std::string& MultiStateEngine::getDefEngSndNormal()
232    {
[6313]233        if( defEngineSndNormal_ )
234            return defEngineSndNormal_->getSource();
235        else
236            assert(0);
[6314]237        return BLANKSTRING;
[6202]238    }
[6207]239
240    void MultiStateEngine::setDefEngSndBoost(const std::string &engineSound)
241    {
[6313]242        if( defEngineSndBoost_ )
243            defEngineSndBoost_->setSource(engineSound);
244        else
245            assert(0);
[6207]246    }
247
248    const std::string& MultiStateEngine::getDefEngSndBoost()
249    {
[6313]250        if( this->defEngineSndBoost_ )
251            return defEngineSndBoost_->getSource();
252        else
253            assert(0);
[6314]254        return BLANKSTRING;
[6207]255    }
[2254]256}
Note: See TracBrowser for help on using the repository browser.