| 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 |  | 
|---|
| 31 | #include "core/Clock.h" | 
|---|
| 32 | #include "core/CommandLine.h" | 
|---|
| 33 | #include "core/ConsoleCommand.h" | 
|---|
| 34 | #include "core/Game.h" | 
|---|
| 35 | #include "core/GameMode.h" | 
|---|
| 36 | #include "core/LuaBind.h" | 
|---|
| 37 | #include "network/NetworkFunction.h" | 
|---|
| 38 | #include "ToluaBindCore.h" | 
|---|
| 39 | #include "ToluaBindOrxonox.h" | 
|---|
| 40 | #include "tools/Timer.h" | 
|---|
| 41 | #include "interfaces/TimeFactorListener.h" | 
|---|
| 42 | #include "interfaces/Tickable.h" | 
|---|
| 43 | #include "LevelManager.h" | 
|---|
| 44 |  | 
|---|
| 45 | namespace orxonox | 
|---|
| 46 | { | 
|---|
| 47 |     DeclareGameState(GSRoot, "root", false, false); | 
|---|
| 48 |     SetCommandLineSwitch(console).information("Start in console mode (text IO only)"); | 
|---|
| 49 |     // Shortcuts for easy direct loading | 
|---|
| 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"); | 
|---|
| 54 |  | 
|---|
| 55 |     GSRoot::GSRoot(const GameStateInfo& info) | 
|---|
| 56 |         : GameState(info) | 
|---|
| 57 |         , timeFactor_(1.0f) | 
|---|
| 58 |         , bPaused_(false) | 
|---|
| 59 |         , timeFactorPauseBackup_(1.0f) | 
|---|
| 60 |     { | 
|---|
| 61 |         this->ccSetTimeFactor_ = 0; | 
|---|
| 62 |         this->ccPause_ = 0; | 
|---|
| 63 |  | 
|---|
| 64 |         // Tell LuaBind about all tolua interfaces | 
|---|
| 65 |         LuaBind::getInstance().addToluaInterface(&tolua_Core_open, "Core"); | 
|---|
| 66 |         LuaBind::getInstance().addToluaInterface(&tolua_Orxonox_open, "Orxonox"); | 
|---|
| 67 |     } | 
|---|
| 68 |  | 
|---|
| 69 |     GSRoot::~GSRoot() | 
|---|
| 70 |     { | 
|---|
| 71 |         NetworkFunctionBase::destroyAllNetworkFunctions(); | 
|---|
| 72 |     } | 
|---|
| 73 |  | 
|---|
| 74 |     void GSRoot::activate() | 
|---|
| 75 |     { | 
|---|
| 76 |         // reset game speed to normal | 
|---|
| 77 |         this->timeFactor_ = 1.0f; | 
|---|
| 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 |         } | 
|---|
| 94 |  | 
|---|
| 95 |         // create the global LevelManager | 
|---|
| 96 |         this->levelManager_ = new LevelManager(); | 
|---|
| 97 |  | 
|---|
| 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 |         } | 
|---|
| 127 |     } | 
|---|
| 128 |  | 
|---|
| 129 |     void GSRoot::deactivate() | 
|---|
| 130 |     { | 
|---|
| 131 | /* | 
|---|
| 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 |         } | 
|---|
| 143 | */ | 
|---|
| 144 |  | 
|---|
| 145 |         delete this->levelManager_; | 
|---|
| 146 |     } | 
|---|
| 147 |  | 
|---|
| 148 |     void GSRoot::update(const Clock& time) | 
|---|
| 149 |     { | 
|---|
| 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 |  | 
|---|
| 157 |         for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) | 
|---|
| 158 |             it->tick(time); | 
|---|
| 159 |  | 
|---|
| 160 |         /*** HACK *** HACK ***/ | 
|---|
| 161 |         // Call the Tickable objects | 
|---|
| 162 |         float leveldt = time.getDeltaTime(); | 
|---|
| 163 |         if (leveldt > 1.0f) | 
|---|
| 164 |         { | 
|---|
| 165 |             // just loaded | 
|---|
| 166 |             leveldt = 0.0f; | 
|---|
| 167 |         } | 
|---|
| 168 |         for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) | 
|---|
| 169 |             it->tick(leveldt * this->timeFactor_); | 
|---|
| 170 |         /*** HACK *** HACK ***/ | 
|---|
| 171 |     } | 
|---|
| 172 |  | 
|---|
| 173 |     /** | 
|---|
| 174 |     @brief | 
|---|
| 175 |         Changes the speed of Orxonox | 
|---|
| 176 |     @remark | 
|---|
| 177 |         This function is a hack when placed here! | 
|---|
| 178 |         Timefactor should be related to the scene (level or so), not the game | 
|---|
| 179 |     */ | 
|---|
| 180 |     void GSRoot::setTimeFactor(float factor) | 
|---|
| 181 |     { | 
|---|
| 182 |         if (GameMode::isMaster()) | 
|---|
| 183 |         { | 
|---|
| 184 |             if (!this->bPaused_) | 
|---|
| 185 |             { | 
|---|
| 186 |                 TimeFactorListener::timefactor_s = factor; | 
|---|
| 187 |  | 
|---|
| 188 |                 for (ObjectList<TimeFactorListener>::iterator it = ObjectList<TimeFactorListener>::begin(); it != ObjectList<TimeFactorListener>::end(); ++it) | 
|---|
| 189 |                     it->changedTimeFactor(factor, this->timeFactor_); | 
|---|
| 190 |  | 
|---|
| 191 |                 this->timeFactor_ = factor; | 
|---|
| 192 |             } | 
|---|
| 193 |             else | 
|---|
| 194 |                 this->timeFactorPauseBackup_ = factor; | 
|---|
| 195 |         } | 
|---|
| 196 |     } | 
|---|
| 197 |  | 
|---|
| 198 |     void GSRoot::pause() | 
|---|
| 199 |     { | 
|---|
| 200 |         if (GameMode::isMaster()) | 
|---|
| 201 |         { | 
|---|
| 202 |             if (!this->bPaused_) | 
|---|
| 203 |             { | 
|---|
| 204 |                 this->timeFactorPauseBackup_ = this->timeFactor_; | 
|---|
| 205 |                 this->setTimeFactor(0.0f); | 
|---|
| 206 |                 this->bPaused_ = true; | 
|---|
| 207 |             } | 
|---|
| 208 |             else | 
|---|
| 209 |             { | 
|---|
| 210 |                 this->bPaused_ = false; | 
|---|
| 211 |                 this->setTimeFactor(this->timeFactorPauseBackup_); | 
|---|
| 212 |             } | 
|---|
| 213 |         } | 
|---|
| 214 |     } | 
|---|
| 215 | } | 
|---|