| [1661] | 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 |  *      Reto Grieder | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
 | 29 | #include "GSRoot.h" | 
|---|
 | 30 |  | 
|---|
| [2896] | 31 | #include "core/Clock.h" | 
|---|
| [3196] | 32 | #include "core/CommandLine.h" | 
|---|
 | 33 | #include "core/ConsoleCommand.h" | 
|---|
| [2896] | 34 | #include "core/Game.h" | 
|---|
 | 35 | #include "core/GameMode.h" | 
|---|
| [3340] | 36 | #include "core/LuaBind.h" | 
|---|
| [3304] | 37 | #include "network/NetworkFunction.h" | 
|---|
| [3340] | 38 | #include "ToluaBindCore.h" | 
|---|
 | 39 | #include "ToluaBindOrxonox.h" | 
|---|
| [1826] | 40 | #include "tools/Timer.h" | 
|---|
| [3196] | 41 | #include "interfaces/TimeFactorListener.h" | 
|---|
 | 42 | #include "interfaces/Tickable.h" | 
|---|
| [3280] | 43 | #include "LevelManager.h" | 
|---|
| [1661] | 44 |  | 
|---|
 | 45 | namespace orxonox | 
|---|
 | 46 | { | 
|---|
| [3280] | 47 |     DeclareGameState(GSRoot, "root", true, false); | 
|---|
 | 48 |     SetCommandLineSwitch(console).information("Start in console mode (text IO only)"); | 
|---|
| [2896] | 49 |     // Shortcuts for easy direct loading | 
|---|
| [3280] | 50 |     SetCommandLineSwitch(server).information("Start in server mode"); | 
|---|
 | 51 |     SetCommandLineSwitch(client).information("Start in client mode"); | 
|---|
 | 52 |     SetCommandLineSwitch(dedicated).information("Start in dedicated server mode"); | 
|---|
 | 53 |     SetCommandLineSwitch(standalone).information("Start in standalone mode"); | 
|---|
| [1663] | 54 |  | 
|---|
| [3280] | 55 |     GSRoot::GSRoot(const GameStateConstrParams& params) | 
|---|
 | 56 |         : GameState(params) | 
|---|
| [2662] | 57 |         , timeFactor_(1.0f) | 
|---|
 | 58 |         , bPaused_(false) | 
|---|
 | 59 |         , timeFactorPauseBackup_(1.0f) | 
|---|
| [1661] | 60 |     { | 
|---|
| [2662] | 61 |         this->ccSetTimeFactor_ = 0; | 
|---|
 | 62 |         this->ccPause_ = 0; | 
|---|
| [3340] | 63 |  | 
|---|
 | 64 |         // Tell LuaBind about all tolua interfaces | 
|---|
 | 65 |         LuaBind::getInstance().addToluaInterface(&tolua_Core_open, "Core"); | 
|---|
 | 66 |         LuaBind::getInstance().addToluaInterface(&tolua_Orxonox_open, "Orxonox"); | 
|---|
| [1661] | 67 |     } | 
|---|
 | 68 |  | 
|---|
 | 69 |     GSRoot::~GSRoot() | 
|---|
 | 70 |     { | 
|---|
| [3304] | 71 |         NetworkFunctionBase::destroyAllNetworkFunctions(); | 
|---|
| [1661] | 72 |     } | 
|---|
 | 73 |  | 
|---|
| [2896] | 74 |     void GSRoot::activate() | 
|---|
| [1686] | 75 |     { | 
|---|
| [2662] | 76 |         // reset game speed to normal | 
|---|
| [2896] | 77 |         this->timeFactor_ = 1.0f; | 
|---|
| [2662] | 78 |  | 
|---|
 | 79 |         { | 
|---|
 | 80 |             // time factor console command | 
|---|
 | 81 |             FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::setTimeFactor); | 
|---|
 | 82 |             functor->setObject(this); | 
|---|
 | 83 |             this->ccSetTimeFactor_ = createConsoleCommand(functor, "setTimeFactor"); | 
|---|
 | 84 |             CommandExecutor::addConsoleCommandShortcut(this->ccSetTimeFactor_).accessLevel(AccessLevel::Offline).defaultValue(0, 1.0); | 
|---|
 | 85 |         } | 
|---|
 | 86 |  | 
|---|
 | 87 |         { | 
|---|
 | 88 |             // time factor console command | 
|---|
 | 89 |             FunctorMember<GSRoot>* functor = createFunctor(&GSRoot::pause); | 
|---|
 | 90 |             functor->setObject(this); | 
|---|
 | 91 |             this->ccPause_ = createConsoleCommand(functor, "pause"); | 
|---|
 | 92 |             CommandExecutor::addConsoleCommandShortcut(this->ccPause_).accessLevel(AccessLevel::Offline); | 
|---|
 | 93 |         } | 
|---|
| [2896] | 94 |  | 
|---|
| [3280] | 95 |         // create the global LevelManager | 
|---|
 | 96 |         this->levelManager_ = new LevelManager(); | 
|---|
 | 97 |  | 
|---|
| [2896] | 98 |         // Load level directly? | 
|---|
 | 99 |         bool loadLevel = false; | 
|---|
 | 100 |         if (CommandLine::getValue("standalone").getBool()) | 
|---|
 | 101 |         { | 
|---|
 | 102 |             Game::getInstance().requestStates("graphics, standalone, level"); | 
|---|
 | 103 |             loadLevel = true; | 
|---|
 | 104 |         } | 
|---|
 | 105 |         if (CommandLine::getValue("server").getBool()) | 
|---|
 | 106 |         { | 
|---|
 | 107 |             Game::getInstance().requestStates("graphics, server, level"); | 
|---|
 | 108 |             loadLevel = true; | 
|---|
 | 109 |         } | 
|---|
 | 110 |         if (CommandLine::getValue("client").getBool()) | 
|---|
 | 111 |         { | 
|---|
 | 112 |             Game::getInstance().requestStates("graphics, client, level"); | 
|---|
 | 113 |             loadLevel = true; | 
|---|
 | 114 |         } | 
|---|
 | 115 |         if (CommandLine::getValue("dedicated").getBool()) | 
|---|
 | 116 |         { | 
|---|
 | 117 |             Game::getInstance().requestStates("dedicated, level"); | 
|---|
 | 118 |             loadLevel = true; | 
|---|
 | 119 |         } | 
|---|
 | 120 |          | 
|---|
 | 121 |         // Determine where to start otherwise | 
|---|
 | 122 |         if (!loadLevel && !CommandLine::getValue("console").getBool()) | 
|---|
 | 123 |         { | 
|---|
 | 124 |             // Also load graphics | 
|---|
 | 125 |             Game::getInstance().requestState("graphics"); | 
|---|
 | 126 |         } | 
|---|
| [1661] | 127 |     } | 
|---|
 | 128 |  | 
|---|
| [2896] | 129 |     void GSRoot::deactivate() | 
|---|
| [1661] | 130 |     { | 
|---|
| [2928] | 131 | /* | 
|---|
| [2662] | 132 |         if (this->ccSetTimeFactor_) | 
|---|
 | 133 |         { | 
|---|
 | 134 |             delete this->ccSetTimeFactor_; | 
|---|
 | 135 |             this->ccSetTimeFactor_ = 0; | 
|---|
 | 136 |         } | 
|---|
 | 137 |  | 
|---|
 | 138 |         if (this->ccPause_) | 
|---|
 | 139 |         { | 
|---|
 | 140 |             delete this->ccPause_; | 
|---|
 | 141 |             this->ccPause_ = 0; | 
|---|
 | 142 |         } | 
|---|
| [2928] | 143 | */ | 
|---|
| [3280] | 144 |  | 
|---|
 | 145 |         delete this->levelManager_; | 
|---|
| [1661] | 146 |     } | 
|---|
 | 147 |  | 
|---|
| [2896] | 148 |     void GSRoot::update(const Clock& time) | 
|---|
| [1661] | 149 |     { | 
|---|
| [2896] | 150 |         if (this->getActivity().topState) | 
|---|
 | 151 |         { | 
|---|
 | 152 |             // This state can not 'survive' on its own. | 
|---|
 | 153 |             // Load a user interface therefore | 
|---|
 | 154 |             Game::getInstance().requestState("ioConsole"); | 
|---|
 | 155 |         } | 
|---|
 | 156 |  | 
|---|
| [2662] | 157 |         uint64_t timeBeforeTick = time.getRealMicroseconds(); | 
|---|
 | 158 |  | 
|---|
| [1826] | 159 |         for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) | 
|---|
 | 160 |             it->tick(time); | 
|---|
 | 161 |  | 
|---|
| [2171] | 162 |         /*** HACK *** HACK ***/ | 
|---|
 | 163 |         // Call the Tickable objects | 
|---|
| [2662] | 164 |         float leveldt = time.getDeltaTime(); | 
|---|
 | 165 |         if (leveldt > 1.0f) | 
|---|
 | 166 |         { | 
|---|
 | 167 |             // just loaded | 
|---|
 | 168 |             leveldt = 0.0f; | 
|---|
 | 169 |         } | 
|---|
| [2171] | 170 |         for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) | 
|---|
| [2662] | 171 |             it->tick(leveldt * this->timeFactor_); | 
|---|
| [2171] | 172 |         /*** HACK *** HACK ***/ | 
|---|
 | 173 |  | 
|---|
| [2662] | 174 |         uint64_t timeAfterTick = time.getRealMicroseconds(); | 
|---|
 | 175 |  | 
|---|
| [2896] | 176 |         // Also add our tick time | 
|---|
 | 177 |         Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); | 
|---|
| [1662] | 178 |     } | 
|---|
| [1674] | 179 |  | 
|---|
 | 180 |     /** | 
|---|
| [2662] | 181 |     @brief | 
|---|
 | 182 |         Changes the speed of Orxonox | 
|---|
 | 183 |     */ | 
|---|
 | 184 |     void GSRoot::setTimeFactor(float factor) | 
|---|
 | 185 |     { | 
|---|
| [2896] | 186 |         if (GameMode::isMaster()) | 
|---|
| [2662] | 187 |         { | 
|---|
 | 188 |             if (!this->bPaused_) | 
|---|
 | 189 |             { | 
|---|
 | 190 |                 TimeFactorListener::timefactor_s = factor; | 
|---|
 | 191 |  | 
|---|
 | 192 |                 for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it) | 
|---|
 | 193 |                     it->changedTimeFactor(factor, this->timeFactor_); | 
|---|
 | 194 |  | 
|---|
 | 195 |                 this->timeFactor_ = factor; | 
|---|
 | 196 |             } | 
|---|
 | 197 |             else | 
|---|
 | 198 |                 this->timeFactorPauseBackup_ = factor; | 
|---|
 | 199 |         } | 
|---|
 | 200 |     } | 
|---|
 | 201 |  | 
|---|
 | 202 |     void GSRoot::pause() | 
|---|
 | 203 |     { | 
|---|
| [2896] | 204 |         if (GameMode::isMaster()) | 
|---|
| [2662] | 205 |         { | 
|---|
 | 206 |             if (!this->bPaused_) | 
|---|
 | 207 |             { | 
|---|
 | 208 |                 this->timeFactorPauseBackup_ = this->timeFactor_; | 
|---|
 | 209 |                 this->setTimeFactor(0.0f); | 
|---|
 | 210 |                 this->bPaused_ = true; | 
|---|
 | 211 |             } | 
|---|
 | 212 |             else | 
|---|
 | 213 |             { | 
|---|
 | 214 |                 this->bPaused_ = false; | 
|---|
 | 215 |                 this->setTimeFactor(this->timeFactorPauseBackup_); | 
|---|
 | 216 |             } | 
|---|
 | 217 |         } | 
|---|
 | 218 |     } | 
|---|
| [1661] | 219 | } | 
|---|