Index: code/branches/gui/src/core/CMakeLists.txt
===================================================================
--- code/branches/gui/src/core/CMakeLists.txt	(revision 1670)
+++ code/branches/gui/src/core/CMakeLists.txt	(revision 1672)
@@ -10,4 +10,5 @@
   OutputBuffer.cc
   OutputHandler.cc
+  RootGameState.cc
   Script.cc
   SignalHandler.cc
Index: code/branches/gui/src/core/CorePrereqs.h
===================================================================
--- code/branches/gui/src/core/CorePrereqs.h	(revision 1670)
+++ code/branches/gui/src/core/CorePrereqs.h	(revision 1672)
@@ -161,4 +161,9 @@
   class XMLPortParamContainer;
 
+  // game states
+  class BaseGameState;
+  class GameState;
+  class RootGameState;
+
   // input
   class BaseCommand;
Index: code/branches/gui/src/core/GameState.cc
===================================================================
--- code/branches/gui/src/core/GameState.cc	(revision 1670)
+++ code/branches/gui/src/core/GameState.cc	(revision 1672)
@@ -46,11 +46,7 @@
     GameState::GameState(const std::string& name)
         : name_(name)
-        , bPauseParent_(false)
-        //, bActive_(false)
-        //, bSuspended_(false)
-        //, bRunning_(false)
-        , scheduledTransition_(0)
         , parent_(0)
         , activeChild_(0)
+        //, bPauseParent_(false)
     {
         Operations temp = {false, false, false, false, false};
@@ -64,11 +60,5 @@
     GameState::~GameState()
     {
-        if (this->operation_.active)
-        {
-            if (this->parent_)
-                this->requestState(this->parent_->getName());
-            else
-                this->requestState("");
-        }
+        OrxAssert(!isInSubtree(getCurrentState()), "Deleting an active GameState is a very bad idea..");
     }
 
@@ -88,5 +78,5 @@
             it != state->allChildren_.end(); ++it)
         {
-            if (this->checkState(it->second->getName()))
+            if (this->getState(it->second->getName()))
             {
                 ThrowException(GameState, "Cannot add a GameState to the hierarchy twice.");
@@ -94,5 +84,5 @@
             }
         }
-        if (this->checkState(state->name_))
+        if (this->getState(state->name_))
         {
             ThrowException(GameState, "Cannot add a GameState to the hierarchy twice.");
@@ -109,15 +99,7 @@
         for (std::map<std::string, GameState*>::const_iterator it = state->allChildren_.begin();
             it != state->allChildren_.end(); ++it)
-        {
-            this->allChildren_[it->second->getName()] = it->second;
-            this->grandchildrenToChildren_[it->second] = state;
-            if (this->parent_)
-                this->parent_->grandchildAdded(this, it->second);
-        }
+            this->grandchildAdded(state, it->second);
         // merge 'state' into this tree
-        this->allChildren_[state->name_] = state;
-        this->grandchildrenToChildren_[state] = state;
-        if (this->parent_)
-            this->parent_->grandchildAdded(this, state);
+        this->grandchildAdded(state, state);
 
         // mark us as parent
@@ -137,9 +119,9 @@
         if (it != this->grandchildrenToChildren_.end())
         {
-            if (state->getOperation().active)
+            if (state->isInSubtree(getCurrentState()))
             {
-                ThrowException(GameState, "Cannot remove active game state child '"
+                ThrowException(GameState, "Cannot remove an active game state child '"
                     + state->getName() + "' from '" + name_ + "'.");
-                //COUT(2) << "Warning: Cannot remove active game state child '" << state->getName()
+                //COUT(2) << "Warning: Cannot remove an active game state child '" << state->getName()
                 //    << "' from '" << name_ << "'." << std::endl;
             }
@@ -148,7 +130,5 @@
                 for (std::map<GameState*, GameState*>::const_iterator it = state->grandchildrenToChildren_.begin();
                     it != state->grandchildrenToChildren_.end(); ++it)
-                {
                     this->grandchildRemoved(it->first);
-                }
                 this->grandchildRemoved(state);
             }
@@ -157,5 +137,5 @@
         {
             ThrowException(GameState, "Game state '" + name_ + "' doesn't have a child named '"
-                + state->getName() + "'. Removal skipped.");
+                + state->getName() + "'.");
             //COUT(2) << "Warning: Game state '" << name_ << "' doesn't have a child named '"
             //    << state->getName() << "'. Removal skipped." << std::endl;
@@ -173,5 +153,5 @@
     void GameState::removeChild(const std::string& name)
     {
-        GameState* state = checkState(name);
+        GameState* state = getState(name);
         if (state)
         {
@@ -194,5 +174,5 @@
         The child that has been added.
     */
-    void GameState::grandchildAdded(GameState* child, GameState* grandchild)
+    inline void GameState::grandchildAdded(GameState* child, GameState* grandchild)
     {
         // fill the two maps correctly.
@@ -212,5 +192,5 @@
         The child that has been removed.
     */
-    void GameState::grandchildRemoved(GameState* grandchild)
+    inline void GameState::grandchildRemoved(GameState* grandchild)
     {
         // adjust the two maps correctly.
@@ -227,8 +207,8 @@
         Remember that the every node has a map with all its child nodes.
     */
-    GameState* GameState::checkState(const std::string& name)
+    GameState* GameState::getState(const std::string& name)
     {
         if (this->parent_)
-            return this->parent_->checkState(name);
+            return this->parent_->getState(name);
         else
         {
@@ -244,4 +224,16 @@
     /**
     @brief
+        Returns the root node of the tree.
+    */
+    GameState* GameState::getRoot()
+    {
+        if (this->parent_)
+            return this->parent_->getRoot();
+        else
+            return this;
+    }
+
+    /**
+    @brief
         Returns the current active state.
     @remarks
@@ -260,6 +252,6 @@
         else
         {
-            if (this->parent_)
-                return this->parent_->getCurrentState();
+            if (this->getParent())
+                return this->getParent()->getCurrentState();
             else
                 return 0;
@@ -269,12 +261,10 @@
     /**
     @brief
-        Returns the root node of the tree.
-    */
-    GameState* GameState::getRootNode()
-    {
-        if (this->parent_)
-            return this->parent_->getRootNode();
-        else
-            return this;
+        Determines whether 'state' is in this subtree, including this node.
+    */
+    bool GameState::isInSubtree(GameState* state) const
+    {
+        return (grandchildrenToChildren_.find(state) != grandchildrenToChildren_.end()
+                || state == this);
     }
 
@@ -288,48 +278,6 @@
     void GameState::requestState(const std::string& name)
     {
-        GameState* current = getCurrentState();
-        if (current != 0 && (current->getOperation().entering || current->getOperation().leaving))
-        {
-            ThrowException(GameState, "Making state transitions during enter()/leave() is forbidden.");
-        }
-        //if (name == "")
-        //{
-        //    // user would like to leave every state.
-        //    if (current)
-        //    {
-        //        // Deactivate all states but root
-        //        GameState* root = getRootNode();
-        //        current->makeTransition(root);
-        //        //// Kick root too
-        //        //assert(!(root->getOperation().entering || root->getOperation().leaving));
-        //        //if (root->operation_.running)
-        //        //    root->scheduledTransition_ = 0;
-        //        //else
-        //        //    root->deactivate();
-        //    }
-        //}
-        else
-        {
-            GameState* request = checkState(name);
-            if (request)
-            {
-                if (current)
-                {
-                    // There is already an active state
-                    current->makeTransition(request);
-                }
-                else
-                {
-                    // no active state --> we have to activate the root node first.
-                    GameState* root = getRootNode();
-                    root->activate();
-                    root->makeTransition(request);
-                }
-            }
-            else
-            {
-                COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
-            }
-        }
+        assert(getRoot());
+        getRoot()->requestState(name);
     }
 
@@ -339,37 +287,41 @@
         the method can assume certain things to be granted (like 'this' is always active).
     */
-    void GameState::makeTransition(GameState* state)
-    {
-        // we're always already active
-        assert(this->operation_.active);
-
-        if (state == this)
+    void GameState::makeTransition(GameState* source, GameState* destination)
+    {
+        if (source == this->getParent())
+        {
+            // call is from the parent
+            this->activate();
+        }
+        else if (source == 0)
+        {
+            // call was just started by root
+            // don't do anyting yet
+        }
+        else
+        {
+            // call is from a child
+            this->activeChild_ = 0;
+        }
+
+        if (destination == this)
             return;
 
-        // Check for 'state' in the children map first
-        std::map<GameState*, GameState*>::const_iterator it = this->grandchildrenToChildren_.find(state);
+        // Check for 'destination' in the children map first
+        std::map<GameState*, GameState*>::const_iterator it
+            = this->grandchildrenToChildren_.find(destination);
         if (it != this->grandchildrenToChildren_.end())
         {
             // child state. Don't use 'state', might be a grandchild!
-            it->second->activate();
-            it->second->makeTransition(state);
+            this->activeChild_ = it->second;
+            it->second->makeTransition(this, destination);
         }
         else
         {
             // parent. We can be sure of this.
-            assert(this->parent_ != 0);
-
-            // only do the transition if we're not currently running
-            if (this->operation_.running)
-            {
-                //this->bDeactivationScheduled_ = true;
-                this->scheduledTransition_ = state;
-            }
-            else
-            {
-                this->deactivate();
-                this->parent_->makeTransition(state);
-            }
-
+            assert(this->getParent() != 0);
+
+            this->deactivate();
+            this->getParent()->makeTransition(this, destination);
         }
     }
@@ -381,6 +333,4 @@
     void GameState::activate()
     {
-        if (this->parent_)
-            this->parent_->activeChild_ = this;
         this->operation_.active = true;
         this->operation_.entering = true;
@@ -398,6 +348,4 @@
         this->operation_.leaving = false;
         this->operation_.active = false;
-        if (this->parent_)
-            this->parent_->activeChild_ = 0;
     }
 
@@ -411,18 +359,9 @@
         This method is not virtual! You cannot override it therefore.
     */
-    void GameState::tick(float dt)
+    void GameState::tick(float dt, uint64_t time)
     {
         this->operation_.running = true;
-        this->ticked(dt);
+        this->ticked(dt, time);
         this->operation_.running = false;
-
-        if (this->scheduledTransition_)
-        {
-            // state was requested to be deactivated when ticked.
-            this->makeTransition(this->scheduledTransition_);
-            this->scheduledTransition_ = 0;
-            this->deactivate();
-        }
-    }
-
+    }
 }
Index: code/branches/gui/src/core/GameState.h
===================================================================
--- code/branches/gui/src/core/GameState.h	(revision 1670)
+++ code/branches/gui/src/core/GameState.h	(revision 1672)
@@ -41,4 +41,5 @@
 #include <vector>
 #include <map>
+#include "util/Integers.h"
 
 namespace orxonox
@@ -60,5 +61,11 @@
     class _CoreExport GameState
     {
+        friend class RootGameState;
+
     public:
+        /**
+        @brief
+            Gives information about what the GameState is currently doing
+        */
         struct Operations
         {
@@ -70,56 +77,51 @@
         };
 
+    public:
         GameState(const std::string& name);
         virtual ~GameState();
 
         const std::string& getName() const { return name_; }
+        const Operations getOperation() const { return this->operation_; }
+        bool isInSubtree(GameState* state) const;
+
+        GameState* getState(const std::string& name);
+        GameState* getRoot();
+        GameState* getParent() const { return this->parent_; }
+        //! Returns the currently active game state
+        virtual GameState* getCurrentState();
+
+        virtual void requestState(const std::string& name);
 
         void addChild(GameState* state);
         void removeChild(GameState* state);
         void removeChild(const std::string& name);
-        void requestState(const std::string& name);
-
-        ////! Determines whether the state is active.
-        //bool isActive()       { return this->bActive_; }
-        ////! Determines whether the state is suspended.
-        //bool isSuspended()    { return this->bSuspended_; }
-        ////! Determines whether the state is the current
-        //bool isCurrentState() { return this->bActive_ && !this->activeChild_; }
-        const Operations getOperation() { return this->operation_; }
-
-        void tick(float dt);
-        void tickChild(float dt) { if (this->activeChild_) this->activeChild_->tick(dt); }
 
     protected:
         virtual void enter() = 0;
         virtual void leave() = 0;
-        virtual void ticked(float dt) = 0;
-        //virtual void enter() { }
-        //virtual void leave() { }
-        //virtual void ticked(float dt) { }
+        virtual void ticked(float dt, uint64_t time) = 0;
 
         GameState* getActiveChild() { return this->activeChild_; }
-        bool hasScheduledTransition() { return this->scheduledTransition_; }
+
+        void tickChild(float dt, uint64_t time) { if (this->getActiveChild()) this->getActiveChild()->tick(dt, time); }
 
     private:
-        GameState* checkState(const std::string& name);
-        GameState* getCurrentState();
-        GameState* getRootNode();
+        //! Performs a transition to 'destination'
+        virtual void makeTransition(GameState* source, GameState* destination);
+
         void grandchildAdded(GameState* child, GameState* grandchild);
         void grandchildRemoved(GameState* grandchild);
-        void makeTransition(GameState* state);
+
+        void tick(float dt, uint64_t time);
         void activate();
         void deactivate();
 
-        const std::string                    name_;
-        bool                                 bPauseParent_;
-
-        Operations                           operation_;
-
-        GameState*                           parent_;
-        GameState*                           activeChild_;
-        GameState*                           scheduledTransition_;
-        std::map<std::string, GameState*>    allChildren_;
-        std::map<GameState*, GameState*>     grandchildrenToChildren_;
+        const std::string                          name_;
+        Operations                                 operation_;
+        GameState*                             parent_;
+        GameState*                                 activeChild_;
+        //bool                                       bPauseParent_;
+        std::map<std::string, GameState*>      allChildren_;
+        std::map<GameState*, GameState*>   grandchildrenToChildren_;
     };
 }
Index: code/branches/gui/src/core/RootGameState.cc
===================================================================
--- code/branches/gui/src/core/RootGameState.cc	(revision 1672)
+++ code/branches/gui/src/core/RootGameState.cc	(revision 1672)
@@ -0,0 +1,173 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Reto Grieder
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#include "RootGameState.h"
+
+#include <OgreTimer.h>
+#include "util/Integers.h"
+#include "Debug.h"
+#include "Exception.h"
+#include "CommandLine.h"
+
+namespace orxonox
+{
+    SetCommandLineArgument(state, "standalone").setShortcut("s");
+
+    RootGameState::RootGameState(const std::string& name)
+        : GameState(name)
+        , stateRequest_("")
+    {
+    }
+
+    RootGameState::~RootGameState()
+    {
+    }
+
+    /**
+    @brief
+        Internal method that actually makes the state transition. Since it is internal,
+        the method can assume certain things to be granted (like 'this' is always active).
+    */
+    void RootGameState::makeTransition(GameState* source, GameState* destination)
+    {
+        if (source != 0)
+        {
+            // transition was not initiated by root itself
+            this->activeChild_ = 0;
+        }
+
+        if (destination == this)
+        {
+            // this marks the end of the game.
+            return;
+        }
+
+        // Check for 'destination' in the children map first
+        std::map<GameState*, GameState*>::const_iterator it
+            = this->grandchildrenToChildren_.find(destination);
+        if (it != this->grandchildrenToChildren_.end())
+        {
+            OrxAssert(dynamic_cast<GameState*>(it->second) != 0,
+                "There was a mix with RootGameState and GameState, could not cast.");
+            GameState* child = static_cast<GameState*>(it->second);
+            // child state. Don't use 'state', might be a grandchild!
+            this->activeChild_ = child;
+            child->makeTransition(this, destination);
+        }
+        else
+        {
+            // root doesn't have a parent..
+            OrxAssert(false, "GameState '" + destination->getName() + "' not found in children list of Root.");
+        }
+    }
+
+    void RootGameState::gotoState(const std::string& name)
+    {
+        GameState* request = getState(name);
+        if (request)
+        {
+            GameState* current = getCurrentState();
+            if (current)
+            {
+                //OrxAssert(dynamic_cast<GameState*>(current),
+                //    "RootGameState: There was a RootGameState in the subtree of Root");
+                //GameState* currentGS = static_cast<GameState*>(current);
+                //currentGS->makeTransition(this, request);
+                current->makeTransition(0, request);
+            }
+            else
+            {
+                // Root is not yet active. This is a violation.
+                ThrowException(GameState, "Activate Root before requesting a state.");
+            }
+        }
+        else
+        {
+            COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl;
+        }
+    }
+
+    /**
+    @brief
+        Makes a state transition according to the state tree. You can choose any state
+        in the tree to do the call. The function finds the current state on its own.
+    @param state
+        The state to be entered, has to exist in the tree.
+    */
+    void RootGameState::requestState(const std::string& name)
+    {
+        this->stateRequest_ = name;
+    }
+
+    /**
+    @brief
+        Starts the game. The little 'while' denotes the main loop.
+        Whenever the root state is selected, the game ends.
+    @param name
+        State to start with (usually main menu or specified by command line)
+    */
+    void RootGameState::start()
+    {
+        this->activate();
+
+        // get initial state from command line
+        std::string initialState;
+        CommandLine::getValue<std::string>("state", &initialState);
+        gotoState(initialState);
+
+        Ogre::Timer timer;
+        uint64_t storedTime = 0;
+        unsigned long lastTimersTime = 1;
+        timer.reset();
+        while (this->activeChild_)
+        {
+            // get current time
+            unsigned long timersTime = timer.getMicroseconds();
+            uint64_t realTime = storedTime + timersTime;
+            float dt = (float)(timersTime - lastTimersTime)/1000000.0f;
+            if (timersTime > 7000000)
+            {
+                // Under worst condition, the ogre timer will overflow right after 7 seconds
+                storedTime += timersTime;
+                lastTimersTime = 0;
+                timer.reset();
+            }
+            else
+            {
+                lastTimersTime = timersTime;
+            }
+
+            this->tick(dt, realTime);
+
+            if (this->stateRequest_ != "")
+                gotoState(stateRequest_);
+        }
+
+        this->deactivate();
+    }
+}
Index: code/branches/gui/src/core/RootGameState.h
===================================================================
--- code/branches/gui/src/core/RootGameState.h	(revision 1672)
+++ code/branches/gui/src/core/RootGameState.h	(revision 1672)
@@ -0,0 +1,62 @@
+/*
+ *   ORXONOX - the hottest 3D action shooter ever to exist
+ *                    > www.orxonox.net <
+ *
+ *
+ *   License notice:
+ *
+ *   This program is free software; you can redistribute it and/or
+ *   modify it under the terms of the GNU General Public License
+ *   as published by the Free Software Foundation; either version 2
+ *   of the License, or (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ *   Author:
+ *      Reto Grieder
+ *   Co-authors:
+ *      ...
+ *
+ */
+
+#ifndef _RootGameState_H__
+#define _RootGameState_H__
+
+#include "CorePrereqs.h"
+#include <OgrePrerequisites.h>
+#include "GameState.h"
+
+namespace orxonox
+{
+    class _CoreExport RootGameState : public GameState
+    {
+        //friend class GameState;
+
+    public:
+        RootGameState(const std::string& name);
+        ~RootGameState();
+
+        void requestState(const std::string& name);
+        void start();
+
+    protected:
+        //virtual void ticked(float dt) = 0;
+        //virtual void enter() = 0;
+        //virtual void leave() = 0;
+
+    private:
+        void makeTransition(GameState* source, GameState* destination);
+        void gotoState(const std::string& name);
+
+        std::string           stateRequest_;
+    };
+}
+
+#endif /* _RootGameState_H__ */
Index: code/branches/gui/src/core/input/InputManager.cc
===================================================================
--- code/branches/gui/src/core/input/InputManager.cc	(revision 1670)
+++ code/branches/gui/src/core/input/InputManager.cc	(revision 1672)
@@ -640,12 +640,12 @@
 
         // check for states to leave
-        for (std::set<InputState*>::reverse_iterator it = stateLeaveRequests_.rbegin();
-            it != stateLeaveRequests_.rend(); ++it)
-        {
-            (*it)->onLeave();
+        for (std::set<InputState*>::reverse_iterator rit = stateLeaveRequests_.rbegin();
+            rit != stateLeaveRequests_.rend(); ++rit)
+        {
+            (*rit)->onLeave();
             // just to be sure that the state actually is registered
-            assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end());
-
-            activeStates_.erase((*it)->getPriority());
+            assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end());
+
+            activeStates_.erase((*rit)->getPriority());
             _updateActiveStates();
         }
@@ -653,21 +653,21 @@
 
         // check for states to enter
-        for (std::set<InputState*>::reverse_iterator it = stateEnterRequests_.rbegin();
-            it != stateEnterRequests_.rend(); ++it)
+        for (std::set<InputState*>::reverse_iterator rit = stateEnterRequests_.rbegin();
+            rit != stateEnterRequests_.rend(); ++rit)
         {
             // just to be sure that the state actually is registered
-            assert(inputStatesByName_.find((*it)->getName()) != inputStatesByName_.end());
-
-            activeStates_[(*it)->getPriority()] = (*it);
+            assert(inputStatesByName_.find((*rit)->getName()) != inputStatesByName_.end());
+
+            activeStates_[(*rit)->getPriority()] = (*rit);
             _updateActiveStates();
-            (*it)->onEnter();
+            (*rit)->onEnter();
         }
         stateEnterRequests_.clear();
 
         // check for states to destroy
-        for (std::set<InputState*>::reverse_iterator it = stateDestroyRequests_.rbegin();
-            it != stateDestroyRequests_.rend(); ++it)
-        {
-            _destroyState((*it));
+        for (std::set<InputState*>::reverse_iterator rit = stateDestroyRequests_.rbegin();
+            rit != stateDestroyRequests_.rend(); ++rit)
+        {
+            _destroyState((*rit));
         }
 
