Index: code/branches/presentation2/src/orxonox/items/MultiStateEngine.cc
===================================================================
--- code/branches/presentation2/src/orxonox/items/MultiStateEngine.cc	(revision 6106)
+++ code/branches/presentation2/src/orxonox/items/MultiStateEngine.cc	(revision 6187)
@@ -22,4 +22,5 @@
  *   Author:
  *      Fabian 'x3n' Landau
+ *      Reto Grieder
  *   Co-authors:
  *      ...
@@ -29,7 +30,14 @@
 #include "MultiStateEngine.h"
 
+extern "C" {
+#include <lua.h>
+}
+
+#include "util/Convert.h"
 #include "core/CoreIncludes.h"
 #include "core/GameMode.h"
+#include "core/LuaState.h"
 #include "core/XMLPort.h"
+#include "worldentities/EffectContainer.h"
 #include "worldentities/pawns/SpaceShip.h"
 
@@ -37,9 +45,4 @@
 {
     static const float FORWARD_EFFECT_VELOCITY_THRESHOLD = 20;
-
-    static const unsigned char STATE_ACTIVE  = 1;
-    static const unsigned char STATE_FORWARD = 2;
-    static const unsigned char STATE_BOOST   = 4;
-    static const unsigned char STATE_BRAKE   = 8;
 
     CreateFactory(MultiStateEngine);
@@ -49,4 +52,5 @@
         RegisterObject(MultiStateEngine);
 
+        this->lua_ = new LuaState();
         this->state_ = 0;
 
@@ -59,12 +63,8 @@
         {
             // We have no ship, so the effects are not attached and won't be destroyed automatically
-            for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
-                (*it)->destroy();
-            for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
-                (*it)->destroy();
-            for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
-                (*it)->destroy();
-            for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
-                (*it)->destroy();
+            for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
+                for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsBegin(); ++it2)
+                    (*it2)->destroy();
+            delete this->lua_;
         }
     }
@@ -73,9 +73,5 @@
     {
         SUPER(MultiStateEngine, XMLPort, xmlelement, mode);
-
-        XMLPortObject(MultiStateEngine, WorldEntity, "active",  addActiveEffect,  getActiveEffect,  xmlelement, mode);
-        XMLPortObject(MultiStateEngine, WorldEntity, "forward", addForwardEffect, getForwardEffect, xmlelement, mode);
-        XMLPortObject(MultiStateEngine, WorldEntity, "boost",   addBoostEffect,   getBoostEffect,   xmlelement, mode);
-        XMLPortObject(MultiStateEngine, WorldEntity, "brake",   addBrakeEffect,   getBrakeEffect,   xmlelement, mode);
+        XMLPortObject(MultiStateEngine, EffectContainer, "",  addEffectContainer,  getEffectContainer,  xmlelement, mode);
     }
 
@@ -97,39 +93,49 @@
 
                 bool forward = (direction.z < 0 && velocity.z < -FORWARD_EFFECT_VELOCITY_THRESHOLD);
-                bool boost = (this->getShip()->getBoost() && forward);
-                bool brake = (direction.z > 0 && velocity.z < 0);
-                bool active = (direction != Vector3::ZERO && !brake);
 
-                if (active)
-                    this->state_ |= STATE_ACTIVE;
+                int newState = 0;
+                if (this->getShip()->getBoost() && forward)
+                    newState = Boost;
+                else if (forward && !newState) // newState == Boost
+                    newState = Normal;
+                else if (direction.z > 0 && velocity.z < 0)
+                    newState = Brake;
                 else
-                    this->state_ &= ~STATE_ACTIVE;
+                    newState = Idle;
 
-                if (forward)
-                    this->state_ |= STATE_FORWARD;
-                else
-                    this->state_ &= ~STATE_FORWARD;
+                if (newState != this->state_)
+                {
+                    int changes = newState | this->state_;
+                    if (changes & Idle)
+                    {
+                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Idle);
+                        lua_setglobal(this->lua_->getInternalLuaState(), "idle");
+                    }
+                    if (changes & Normal)
+                    {
+                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Normal);
+                        lua_setglobal(this->lua_->getInternalLuaState(), "normal");
+                    }
+                    if (changes & Brake)
+                    {
+                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Brake);
+                        lua_setglobal(this->lua_->getInternalLuaState(), "brake");
+                    }
+                    if (changes & Boost)
+                    {
+                        lua_pushboolean(this->lua_->getInternalLuaState(), newState & Boost);
+                        lua_setglobal(this->lua_->getInternalLuaState(), "boost");
+                    }
 
-                if (boost)
-                    this->state_ |= STATE_BOOST;
-                else
-                    this->state_ &= ~STATE_BOOST;
+                    // Update all effect conditions
+                    for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
+                        (*it)->updateCondition();
 
-                if (brake)
-                    this->state_ |= STATE_BRAKE;
-                else
-                    this->state_ &= ~STATE_BRAKE;
+                    this->state_ = newState;
+                }
             }
 
             if (GameMode::isMaster())
             {
-                for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
-                    (*it)->setMainState(this->state_ & STATE_ACTIVE);
-                for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
-                    (*it)->setMainState(this->state_ & STATE_FORWARD);
-                for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
-                    (*it)->setMainState(this->state_ & STATE_BOOST);
-                for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
-                    (*it)->setMainState(this->state_ & STATE_BRAKE);
             }
         }
@@ -145,88 +151,31 @@
             return;
 
-        for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
-            ship->attach(*it);
-        for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
-            ship->attach(*it);
-        for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
-            ship->attach(*it);
-        for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
-            ship->attach(*it);
+        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
+            for (std::vector<WorldEntity*>::const_iterator it2 = (*it)->getEffectsBegin(); it2 != (*it)->getEffectsEnd(); ++it2)
+                this->getShip()->attach(*it2);
     }
 
-    void MultiStateEngine::addActiveEffect(WorldEntity* effect)
+    void MultiStateEngine::addEffectContainer(EffectContainer* effect)
     {
-        this->activeEffects_.push_back(effect);
+        if (effect == NULL)
+            return;
+        effect->setLuaState(this->lua_, "f" + multi_cast<std::string>(this->effectContainers_.size()));
+        this->effectContainers_.push_back(effect);
         if (this->getShip())
-            this->getShip()->attach(effect);
+        {
+            for (std::vector<WorldEntity*>::const_iterator it = effect->getEffectsBegin(); it != effect->getEffectsBegin(); ++it)
+                this->getShip()->attach(*it);
+        }
     }
 
-    void MultiStateEngine::addForwardEffect(WorldEntity* effect)
-    {
-        this->forwardEffects_.push_back(effect);
-        if (this->getShip())
-            this->getShip()->attach(effect);
-    }
-
-    void MultiStateEngine::addBoostEffect(WorldEntity* effect)
-    {
-        this->boostEffects_.push_back(effect);
-        if (this->getShip())
-            this->getShip()->attach(effect);
-    }
-
-    void MultiStateEngine::addBrakeEffect(WorldEntity* effect)
-    {
-        this->brakeEffects_.push_back(effect);
-        if (this->getShip())
-            this->getShip()->attach(effect);
-    }
-
-    WorldEntity* MultiStateEngine::getActiveEffect(unsigned int index) const
+    EffectContainer* MultiStateEngine::getEffectContainer(unsigned int index) const
     {
         unsigned int i = 0;
-        for (std::list<WorldEntity*>::const_iterator it = this->activeEffects_.begin(); it != this->activeEffects_.end(); ++it)
+        for (std::vector<EffectContainer*>::const_iterator it = this->effectContainers_.begin(); it != this->effectContainers_.end(); ++it)
         {
             if (i == index)
                 return (*it);
-            ++i;
         }
-        return 0;
-    }
-
-    WorldEntity* MultiStateEngine::getForwardEffect(unsigned int index) const
-    {
-        unsigned int i = 0;
-        for (std::list<WorldEntity*>::const_iterator it = this->forwardEffects_.begin(); it != this->forwardEffects_.end(); ++it)
-        {
-            if (i == index)
-                return (*it);
-            ++i;
-        }
-        return 0;
-    }
-
-    WorldEntity* MultiStateEngine::getBoostEffect(unsigned int index) const
-    {
-        unsigned int i = 0;
-        for (std::list<WorldEntity*>::const_iterator it = this->boostEffects_.begin(); it != this->boostEffects_.end(); ++it)
-        {
-            if (i == index)
-                return (*it);
-            ++i;
-        }
-        return 0;
-    }
-
-    WorldEntity* MultiStateEngine::getBrakeEffect(unsigned int index) const
-    {
-        unsigned int i = 0;
-        for (std::list<WorldEntity*>::const_iterator it = this->brakeEffects_.begin(); it != this->brakeEffects_.end(); ++it)
-        {
-            if (i == index)
-                return (*it);
-            ++i;
-        }
-        return 0;
+        return NULL;
     }
 }
Index: code/branches/presentation2/src/orxonox/items/MultiStateEngine.h
===================================================================
--- code/branches/presentation2/src/orxonox/items/MultiStateEngine.h	(revision 6106)
+++ code/branches/presentation2/src/orxonox/items/MultiStateEngine.h	(revision 6187)
@@ -22,4 +22,5 @@
  *   Author:
  *      Fabian 'x3n' Landau
+ *      Reto Grieder
  *   Co-authors:
  *      ...
@@ -32,12 +33,23 @@
 #include "OrxonoxPrereqs.h"
 
-#include <list>
+#include <vector>
 #include "Engine.h"
 
 namespace orxonox
 {
+    // forward declaration
+    class EffectContainer;
+
     class _OrxonoxExport MultiStateEngine : public Engine
     {
         public:
+            enum EngineState
+            {
+                Idle    = 1,
+                Normal  = 2,
+                Brake   = 4,
+                Boost   = 8
+            };
+
             MultiStateEngine(BaseObject* creator);
             virtual ~MultiStateEngine();
@@ -50,20 +62,11 @@
             virtual void addToSpaceShip(SpaceShip* ship);
 
-            void addActiveEffect(WorldEntity* effect);
-            void addForwardEffect(WorldEntity* effect);
-            void addBoostEffect(WorldEntity* effect);
-            void addBrakeEffect(WorldEntity* effect);
-
-            WorldEntity* getActiveEffect(unsigned int index) const;
-            WorldEntity* getForwardEffect(unsigned int index) const;
-            WorldEntity* getBoostEffect(unsigned int index) const;
-            WorldEntity* getBrakeEffect(unsigned int index) const;
+            void addEffectContainer(EffectContainer* effect);
+            EffectContainer* getEffectContainer(unsigned int index) const;
 
         private:
-            unsigned char state_;
-            std::list<WorldEntity*> activeEffects_;
-            std::list<WorldEntity*> forwardEffects_;
-            std::list<WorldEntity*> boostEffects_;
-            std::list<WorldEntity*> brakeEffects_;
+            int state_;
+            LuaState* lua_;
+            std::vector<EffectContainer*> effectContainers_;
     };
 }
