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