[1672] | 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 "RootGameState.h" |
---|
| 30 | |
---|
[1690] | 31 | #include "util/String.h" |
---|
| 32 | #include "util/SubString.h" |
---|
[1755] | 33 | #include "util/Debug.h" |
---|
[1764] | 34 | #include "util/Exception.h" |
---|
[1755] | 35 | #include "Core.h" |
---|
[1674] | 36 | #include "Clock.h" |
---|
[1672] | 37 | #include "CommandLine.h" |
---|
| 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
[2003] | 41 | SetCommandLineArgument(state, "gui").shortcut("s"); |
---|
[1672] | 42 | |
---|
| 43 | RootGameState::RootGameState(const std::string& name) |
---|
[1689] | 44 | : GameState<GameStateBase>(name) |
---|
[1672] | 45 | , stateRequest_("") |
---|
| 46 | { |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | RootGameState::~RootGameState() |
---|
| 50 | { |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | @brief |
---|
| 55 | Internal method that actually makes the state transition. Since it is internal, |
---|
| 56 | the method can assume certain things to be granted (like 'this' is always active). |
---|
| 57 | */ |
---|
[1689] | 58 | void RootGameState::makeTransition(GameStateBase* source, GameStateBase* destination) |
---|
[1672] | 59 | { |
---|
| 60 | if (source != 0) |
---|
| 61 | { |
---|
| 62 | // transition was not initiated by root itself |
---|
| 63 | this->activeChild_ = 0; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | if (destination == this) |
---|
| 67 | { |
---|
| 68 | // this marks the end of the game. |
---|
| 69 | return; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | // Check for 'destination' in the children map first |
---|
[1689] | 73 | std::map<GameStateBase*, GameStateBase*>::const_iterator it |
---|
[1672] | 74 | = this->grandchildrenToChildren_.find(destination); |
---|
| 75 | if (it != this->grandchildrenToChildren_.end()) |
---|
| 76 | { |
---|
[1689] | 77 | OrxAssert(dynamic_cast<GameStateBase*>(it->second) != 0, |
---|
[1672] | 78 | "There was a mix with RootGameState and GameState, could not cast."); |
---|
[1689] | 79 | GameStateBase* child = static_cast<GameStateBase*>(it->second); |
---|
[1672] | 80 | // child state. Don't use 'state', might be a grandchild! |
---|
| 81 | this->activeChild_ = child; |
---|
| 82 | child->makeTransition(this, destination); |
---|
| 83 | } |
---|
| 84 | else |
---|
| 85 | { |
---|
| 86 | // root doesn't have a parent.. |
---|
| 87 | OrxAssert(false, "GameState '" + destination->getName() + "' not found in children list of Root."); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | void RootGameState::gotoState(const std::string& name) |
---|
| 92 | { |
---|
[1689] | 93 | GameStateBase* request = getState(name); |
---|
[1672] | 94 | if (request) |
---|
| 95 | { |
---|
[1689] | 96 | GameStateBase* current = getCurrentState(); |
---|
[1672] | 97 | if (current) |
---|
| 98 | { |
---|
| 99 | current->makeTransition(0, request); |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | // Root is not yet active. This is a violation. |
---|
| 104 | ThrowException(GameState, "Activate Root before requesting a state."); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | else |
---|
| 108 | { |
---|
| 109 | COUT(2) << "Warning: GameState '" << name << "' doesn't exist." << std::endl; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | @brief |
---|
| 115 | Makes a state transition according to the state tree. You can choose any state |
---|
| 116 | in the tree to do the call. The function finds the current state on its own. |
---|
| 117 | @param state |
---|
| 118 | The state to be entered, has to exist in the tree. |
---|
| 119 | */ |
---|
| 120 | void RootGameState::requestState(const std::string& name) |
---|
| 121 | { |
---|
| 122 | this->stateRequest_ = name; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | @brief |
---|
| 127 | Starts the game. The little 'while' denotes the main loop. |
---|
| 128 | Whenever the root state is selected, the game ends. |
---|
| 129 | @param name |
---|
| 130 | State to start with (usually main menu or specified by command line) |
---|
| 131 | */ |
---|
[1689] | 132 | void RootGameState::start(int argc, char** argv) |
---|
[1672] | 133 | { |
---|
[1824] | 134 | #ifdef NDEBUG |
---|
[1802] | 135 | try |
---|
| 136 | { |
---|
[1824] | 137 | #endif |
---|
[1802] | 138 | // start global orxonox time |
---|
| 139 | Clock clock; |
---|
[1695] | 140 | |
---|
[1802] | 141 | // create the Core settings to configure the output level |
---|
| 142 | Core::getInstance(); |
---|
[1756] | 143 | |
---|
[1802] | 144 | parseArguments(argc, argv); |
---|
[1689] | 145 | |
---|
[1802] | 146 | this->activate(); |
---|
[1672] | 147 | |
---|
[1802] | 148 | // get initial state from command line |
---|
[2003] | 149 | gotoState(CommandLine::getValue("state")); |
---|
[1672] | 150 | |
---|
[1802] | 151 | while (this->activeChild_) |
---|
| 152 | { |
---|
| 153 | clock.capture(); |
---|
[1672] | 154 | |
---|
[1802] | 155 | this->tick(clock); |
---|
[1672] | 156 | |
---|
[1802] | 157 | if (this->stateRequest_ != "") |
---|
| 158 | gotoState(stateRequest_); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | this->deactivate(); |
---|
[1824] | 162 | #ifdef NDEBUG |
---|
[1672] | 163 | } |
---|
[1802] | 164 | // Note: These are all unhandled exceptions that should not have made its way here! |
---|
| 165 | // almost complete game catch block to display the messages appropriately. |
---|
| 166 | catch (std::exception& ex) |
---|
| 167 | { |
---|
| 168 | COUT(1) << ex.what() << std::endl; |
---|
| 169 | COUT(1) << "Program aborted." << std::endl; |
---|
| 170 | abort(); |
---|
| 171 | } |
---|
| 172 | // anything that doesn't inherit from std::exception |
---|
| 173 | catch (...) |
---|
| 174 | { |
---|
| 175 | COUT(1) << "An unidentifiable exception has occured. Program aborted." << std::endl; |
---|
| 176 | abort(); |
---|
| 177 | } |
---|
[1824] | 178 | #endif |
---|
[1672] | 179 | } |
---|
[1689] | 180 | |
---|
[1690] | 181 | /** |
---|
| 182 | @brief |
---|
| 183 | Parses both command line and start.ini for CommandLineArguments. |
---|
| 184 | */ |
---|
| 185 | void RootGameState::parseArguments(int argc, char** argv) |
---|
[1689] | 186 | { |
---|
[1690] | 187 | // parse command line first |
---|
[1689] | 188 | std::vector<std::string> args; |
---|
| 189 | for (int i = 1; i < argc; ++i) |
---|
| 190 | args.push_back(argv[i]); |
---|
| 191 | |
---|
| 192 | try |
---|
| 193 | { |
---|
| 194 | orxonox::CommandLine::parse(args); |
---|
| 195 | } |
---|
| 196 | catch (orxonox::ArgumentException& ex) |
---|
| 197 | { |
---|
| 198 | COUT(1) << ex.what() << std::endl; |
---|
| 199 | COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl; |
---|
| 200 | } |
---|
[1690] | 201 | |
---|
| 202 | // look for additional arguments in start.ini |
---|
| 203 | std::ifstream file; |
---|
| 204 | file.open("start.ini"); |
---|
| 205 | args.clear(); |
---|
| 206 | if (file) |
---|
| 207 | { |
---|
| 208 | while (!file.eof()) |
---|
| 209 | { |
---|
| 210 | std::string line; |
---|
| 211 | std::getline(file, line); |
---|
| 212 | line = removeTrailingWhitespaces(line); |
---|
| 213 | //if (!(line[0] == '#' || line[0] == '%')) |
---|
| 214 | //{ |
---|
| 215 | SubString tokens(line, " ", " ", false, 92, false, 34, false, 40, 41, false, '#'); |
---|
| 216 | for (unsigned i = 0; i < tokens.size(); ++i) |
---|
| 217 | if (tokens[i][0] != '#') |
---|
| 218 | args.push_back(tokens[i]); |
---|
| 219 | //args.insert(args.end(), tokens.getAllStrings().begin(), tokens.getAllStrings().end()); |
---|
| 220 | //} |
---|
| 221 | } |
---|
| 222 | file.close(); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | try |
---|
| 226 | { |
---|
| 227 | orxonox::CommandLine::parse(args); |
---|
| 228 | } |
---|
| 229 | catch (orxonox::ArgumentException& ex) |
---|
| 230 | { |
---|
| 231 | COUT(1) << "An Exception occured while parsing start.ini" << std::endl; |
---|
| 232 | COUT(1) << ex.what() << std::endl; |
---|
| 233 | COUT(0) << "Usage:" << std::endl << "orxonox " << CommandLine::getUsageInformation() << std::endl; |
---|
| 234 | } |
---|
[1689] | 235 | } |
---|
[1672] | 236 | } |
---|