| 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 |  | 
|---|
| 32 | #include "util/Exception.h" | 
|---|
| 33 | #include "util/Debug.h" | 
|---|
| 34 | #include "core/Clock.h" | 
|---|
| 35 | #include "core/Game.h" | 
|---|
| 36 | #include "core/GameMode.h" | 
|---|
| 37 | #include "core/CommandLine.h" | 
|---|
| 38 | #include "core/ConsoleCommand.h" | 
|---|
| 39 | #include "tools/TimeFactorListener.h" | 
|---|
| 40 | #include "tools/Timer.h" | 
|---|
| 41 | #include "objects/Tickable.h" | 
|---|
| 42 |  | 
|---|
| 43 | namespace orxonox | 
|---|
| 44 | { | 
|---|
| 45 |     AddGameState(GSRoot, "root"); | 
|---|
| 46 |     SetCommandLineSwitch(console); | 
|---|
| 47 |     // Shortcuts for easy direct loading | 
|---|
| 48 |     SetCommandLineSwitch(server); | 
|---|
| 49 |     SetCommandLineSwitch(client); | 
|---|
| 50 |     SetCommandLineSwitch(dedicated); | 
|---|
| 51 |     SetCommandLineSwitch(standalone); | 
|---|
| 52 |  | 
|---|
| 53 |     GSRoot::GSRoot(const std::string& name) | 
|---|
| 54 |         : GameState(name) | 
|---|
| 55 |         , timeFactor_(1.0f) | 
|---|
| 56 |         , bPaused_(false) | 
|---|
| 57 |         , timeFactorPauseBackup_(1.0f) | 
|---|
| 58 |     { | 
|---|
| 59 |         this->ccSetTimeFactor_ = 0; | 
|---|
| 60 |         this->ccPause_ = 0; | 
|---|
| 61 |     } | 
|---|
| 62 |  | 
|---|
| 63 |     GSRoot::~GSRoot() | 
|---|
| 64 |     { | 
|---|
| 65 |     } | 
|---|
| 66 |  | 
|---|
| 67 |     void GSRoot::activate() | 
|---|
| 68 |     { | 
|---|
| 69 |         // reset game speed to normal | 
|---|
| 70 |         this->timeFactor_ = 1.0f; | 
|---|
| 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 |         } | 
|---|
| 87 |  | 
|---|
| 88 |         // Load level directly? | 
|---|
| 89 |         bool loadLevel = false; | 
|---|
| 90 |         if (CommandLine::getValue("standalone").getBool()) | 
|---|
| 91 |         { | 
|---|
| 92 |             Game::getInstance().requestStates("graphics, standalone, level"); | 
|---|
| 93 |             loadLevel = true; | 
|---|
| 94 |         } | 
|---|
| 95 |         if (CommandLine::getValue("server").getBool()) | 
|---|
| 96 |         { | 
|---|
| 97 |             Game::getInstance().requestStates("graphics, server, level"); | 
|---|
| 98 |             loadLevel = true; | 
|---|
| 99 |         } | 
|---|
| 100 |         if (CommandLine::getValue("client").getBool()) | 
|---|
| 101 |         { | 
|---|
| 102 |             Game::getInstance().requestStates("graphics, client, level"); | 
|---|
| 103 |             loadLevel = true; | 
|---|
| 104 |         } | 
|---|
| 105 |         if (CommandLine::getValue("dedicated").getBool()) | 
|---|
| 106 |         { | 
|---|
| 107 |             Game::getInstance().requestStates("dedicated, level"); | 
|---|
| 108 |             loadLevel = true; | 
|---|
| 109 |         } | 
|---|
| 110 |          | 
|---|
| 111 |         // Determine where to start otherwise | 
|---|
| 112 |         if (!loadLevel && !CommandLine::getValue("console").getBool()) | 
|---|
| 113 |         { | 
|---|
| 114 |             // Also load graphics | 
|---|
| 115 |             Game::getInstance().requestState("graphics"); | 
|---|
| 116 |         } | 
|---|
| 117 |     } | 
|---|
| 118 |  | 
|---|
| 119 |     void GSRoot::deactivate() | 
|---|
| 120 |     { | 
|---|
| 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 |         } | 
|---|
| 132 |     } | 
|---|
| 133 |  | 
|---|
| 134 |     void GSRoot::update(const Clock& time) | 
|---|
| 135 |     { | 
|---|
| 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 |  | 
|---|
| 143 |         uint64_t timeBeforeTick = time.getRealMicroseconds(); | 
|---|
| 144 |  | 
|---|
| 145 |         for (ObjectList<TimerBase>::iterator it = ObjectList<TimerBase>::begin(); it; ++it) | 
|---|
| 146 |             it->tick(time); | 
|---|
| 147 |  | 
|---|
| 148 |         /*** HACK *** HACK ***/ | 
|---|
| 149 |         // Call the Tickable objects | 
|---|
| 150 |         float leveldt = time.getDeltaTime(); | 
|---|
| 151 |         if (leveldt > 1.0f) | 
|---|
| 152 |         { | 
|---|
| 153 |             // just loaded | 
|---|
| 154 |             leveldt = 0.0f; | 
|---|
| 155 |         } | 
|---|
| 156 |         for (ObjectList<Tickable>::iterator it = ObjectList<Tickable>::begin(); it; ++it) | 
|---|
| 157 |             it->tick(leveldt * this->timeFactor_); | 
|---|
| 158 |         /*** HACK *** HACK ***/ | 
|---|
| 159 |  | 
|---|
| 160 |         uint64_t timeAfterTick = time.getRealMicroseconds(); | 
|---|
| 161 |  | 
|---|
| 162 |         // Also add our tick time | 
|---|
| 163 |         Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); | 
|---|
| 164 |     } | 
|---|
| 165 |  | 
|---|
| 166 |     /** | 
|---|
| 167 |     @brief | 
|---|
| 168 |         Changes the speed of Orxonox | 
|---|
| 169 |     */ | 
|---|
| 170 |     void GSRoot::setTimeFactor(float factor) | 
|---|
| 171 |     { | 
|---|
| 172 |         if (GameMode::isMaster()) | 
|---|
| 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 |     { | 
|---|
| 190 |         if (GameMode::isMaster()) | 
|---|
| 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 |     } | 
|---|
| 205 | } | 
|---|