| [2636] | 1 |  | 
|---|
|  | 2 |  | 
|---|
| [4597] | 3 | /* | 
|---|
| [2636] | 4 | orxonox - the future of 3D-vertical-scrollers | 
|---|
|  | 5 |  | 
|---|
|  | 6 | Copyright (C) 2004 orx | 
|---|
|  | 7 |  | 
|---|
|  | 8 | This program is free software; you can redistribute it and/or modify | 
|---|
|  | 9 | it under the terms of the GNU General Public License as published by | 
|---|
|  | 10 | the Free Software Foundation; either version 2, or (at your option) | 
|---|
|  | 11 | any later version. | 
|---|
|  | 12 |  | 
|---|
|  | 13 | ### File Specific: | 
|---|
|  | 14 | main-programmer: Patrick Boenzli | 
|---|
|  | 15 | co-programmer: ... | 
|---|
|  | 16 | */ | 
|---|
|  | 17 |  | 
|---|
| [5300] | 18 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_LOAD | 
|---|
|  | 19 |  | 
|---|
| [2636] | 20 | #include "game_loader.h" | 
|---|
| [7193] | 21 | #include "util/loading/load_param.h" | 
|---|
| [5139] | 22 |  | 
|---|
|  | 23 | #include "shell_command.h" | 
|---|
| [2636] | 24 | #include "campaign.h" | 
|---|
| [6152] | 25 |  | 
|---|
| [7193] | 26 | #include "util/loading/resource_manager.h" | 
|---|
| [6152] | 27 |  | 
|---|
| [4410] | 28 | #include "event_handler.h" | 
|---|
| [2636] | 29 |  | 
|---|
|  | 30 |  | 
|---|
|  | 31 | using namespace std; | 
|---|
|  | 32 |  | 
|---|
|  | 33 |  | 
|---|
| [5195] | 34 | SHELL_COMMAND(quit, GameLoader, stop) | 
|---|
|  | 35 | ->describe("quits the game") | 
|---|
|  | 36 | ->setAlias("orxoquit"); | 
|---|
| [2636] | 37 |  | 
|---|
| [5162] | 38 |  | 
|---|
|  | 39 | GameLoader* GameLoader::singletonRef = NULL; | 
|---|
|  | 40 |  | 
|---|
|  | 41 |  | 
|---|
| [4445] | 42 | /** | 
|---|
| [4836] | 43 | *  simple constructor | 
|---|
| [6139] | 44 | */ | 
|---|
| [4597] | 45 | GameLoader::GameLoader () | 
|---|
| [4017] | 46 | { | 
|---|
| [4597] | 47 | this->setClassID(CL_GAME_LOADER, "GameLoader"); | 
|---|
|  | 48 | this->setName("GameLoader"); | 
|---|
| [6862] | 49 | this->bRun = true; | 
|---|
| [4017] | 50 | } | 
|---|
| [2636] | 51 |  | 
|---|
|  | 52 |  | 
|---|
| [4445] | 53 | /** | 
|---|
| [4836] | 54 | *  simple deconstructor | 
|---|
| [6139] | 55 | */ | 
|---|
| [4816] | 56 | GameLoader::~GameLoader () | 
|---|
|  | 57 | { | 
|---|
|  | 58 | if( this->currentCampaign) | 
|---|
|  | 59 | delete this->currentCampaign; | 
|---|
|  | 60 | this->currentCampaign = NULL; | 
|---|
|  | 61 | } | 
|---|
| [2636] | 62 |  | 
|---|
|  | 63 |  | 
|---|
| [3225] | 64 | /** | 
|---|
| [4836] | 65 | *  initializes the GameLoader | 
|---|
| [6139] | 66 | */ | 
|---|
| [3222] | 67 | ErrorMessage GameLoader::init() | 
|---|
| [2636] | 68 | { | 
|---|
|  | 69 | if(this->currentCampaign != NULL) | 
|---|
|  | 70 | this->currentCampaign->init(); | 
|---|
| [4411] | 71 |  | 
|---|
|  | 72 | this->eventHandler = EventHandler::getInstance(); | 
|---|
| [5093] | 73 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PAUSE); | 
|---|
| [5553] | 74 | this->eventHandler->subscribe(this, ES_ALL, EV_MAIN_QUIT);          //< External Quit Event | 
|---|
| [6854] | 75 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_QUIT); | 
|---|
| [5093] | 76 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_NEXT_WORLD); | 
|---|
|  | 77 | this->eventHandler->subscribe(this, ES_GAME, KeyMapper::PEV_PREVIOUS_WORLD); | 
|---|
| [2636] | 78 | } | 
|---|
|  | 79 |  | 
|---|
|  | 80 |  | 
|---|
| [3225] | 81 | /** | 
|---|
| [4836] | 82 | *  reads a campaign definition file into a campaign class | 
|---|
|  | 83 | * @param fileName to be loaded | 
|---|
|  | 84 | * @returns the loaded campaign | 
|---|
| [6139] | 85 | * | 
|---|
|  | 86 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns | 
|---|
|  | 87 | */ | 
|---|
| [7221] | 88 | ErrorMessage GameLoader::loadCampaign(const std::string& fileName) | 
|---|
| [2636] | 89 | { | 
|---|
| [3222] | 90 | ErrorMessage errorCode; | 
|---|
| [7221] | 91 | std::string campaignName = ResourceManager::getFullName(fileName); | 
|---|
|  | 92 | if (!campaignName.empty()) | 
|---|
| [4094] | 93 | { | 
|---|
|  | 94 | this->currentCampaign = this->fileToCampaign(campaignName); | 
|---|
|  | 95 | } | 
|---|
| [2636] | 96 | } | 
|---|
|  | 97 |  | 
|---|
| [6139] | 98 |  | 
|---|
| [3225] | 99 | /** | 
|---|
| [6139] | 100 | *  reads a campaign definition file into a campaign class | 
|---|
|  | 101 | * @param fileName to be loaded | 
|---|
|  | 102 | * @returns the loaded campaign | 
|---|
|  | 103 | * | 
|---|
|  | 104 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns | 
|---|
|  | 105 | */ | 
|---|
| [7221] | 106 | ErrorMessage GameLoader::loadNetworkCampaign(const std::string& fileName) | 
|---|
| [6139] | 107 | { | 
|---|
|  | 108 | ErrorMessage errorCode; | 
|---|
| [7221] | 109 | std::string campaignName = ResourceManager::getFullName(fileName); | 
|---|
|  | 110 | if (!campaignName.empty()) | 
|---|
| [6139] | 111 | { | 
|---|
| [6424] | 112 | this->currentCampaign = this->fileToCampaign(campaignName); | 
|---|
| [6139] | 113 | } | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 |  | 
|---|
|  | 117 | /** | 
|---|
| [4836] | 118 | *  loads a debug campaign for test purposes only. | 
|---|
|  | 119 | * @param campaignID the identifier of the campaign. | 
|---|
|  | 120 | * @returns error message if not able to do so. | 
|---|
| [6139] | 121 | */ | 
|---|
| [3222] | 122 | ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID) | 
|---|
| [2636] | 123 | { | 
|---|
|  | 124 | switch(campaignID) | 
|---|
|  | 125 | { | 
|---|
| [4597] | 126 | /* | 
|---|
|  | 127 | Debug Level 0: Debug level used to test the base frame work. | 
|---|
|  | 128 | As you can see, all storyentity data is allocated before game | 
|---|
|  | 129 | start. the storyentity will load themselfs shortly before start | 
|---|
|  | 130 | through the StoryEntity::init() funtion. | 
|---|
| [3629] | 131 | */ | 
|---|
| [2636] | 132 | case DEBUG_CAMPAIGN_0: | 
|---|
|  | 133 | { | 
|---|
| [6150] | 134 | /*        Campaign* debugCampaign = new Campaign(); | 
|---|
| [3220] | 135 |  | 
|---|
| [4597] | 136 | World* world0 = new World(DEBUG_WORLD_0); | 
|---|
|  | 137 | world0->setNextStoryID(WORLD_ID_1); | 
|---|
|  | 138 | debugCampaign->addEntity(world0, WORLD_ID_0); | 
|---|
| [3220] | 139 |  | 
|---|
| [4597] | 140 | World* world1 = new World(DEBUG_WORLD_1); | 
|---|
|  | 141 | world1->setNextStoryID(WORLD_ID_2); | 
|---|
|  | 142 | debugCampaign->addEntity(world1, WORLD_ID_1); | 
|---|
| [2636] | 143 |  | 
|---|
| [4597] | 144 | World* world2 = new World(DEBUG_WORLD_2); | 
|---|
|  | 145 | world2->setNextStoryID(WORLD_ID_GAMEEND); | 
|---|
|  | 146 | debugCampaign->addEntity(world2, WORLD_ID_2); | 
|---|
| [3727] | 147 |  | 
|---|
| [4597] | 148 | this->currentCampaign = debugCampaign; | 
|---|
| [6150] | 149 | break;*/ | 
|---|
| [2636] | 150 | } | 
|---|
|  | 151 | } | 
|---|
|  | 152 | } | 
|---|
|  | 153 |  | 
|---|
| [4443] | 154 |  | 
|---|
| [4597] | 155 | /** | 
|---|
| [6139] | 156 | *  starts the current entity | 
|---|
|  | 157 | * @returns error code if this action has caused a error | 
|---|
|  | 158 | */ | 
|---|
| [3222] | 159 | ErrorMessage GameLoader::start() | 
|---|
| [2636] | 160 | { | 
|---|
|  | 161 | if(this->currentCampaign != NULL) | 
|---|
| [6862] | 162 | { | 
|---|
| [2636] | 163 | this->currentCampaign->start(); | 
|---|
| [6862] | 164 | } | 
|---|
| [2636] | 165 | } | 
|---|
|  | 166 |  | 
|---|
|  | 167 |  | 
|---|
| [4597] | 168 | /** | 
|---|
| [6139] | 169 | *  stops the current entity | 
|---|
|  | 170 | * @returns error code if this action has caused a error | 
|---|
|  | 171 | * | 
|---|
|  | 172 | *  ATTENTION: this function shouldn't call other functions, or if so, they must return | 
|---|
|  | 173 | *  after finishing. If you ignore or forget to do so, the current entity is not able to | 
|---|
|  | 174 | *  terminate and it will run in the background or the ressources can't be freed or even | 
|---|
|  | 175 | *  worse: are freed and the program will end in a segmentation fault! | 
|---|
|  | 176 | *  hehehe, have ya seen it... :) | 
|---|
|  | 177 | */ | 
|---|
| [5139] | 178 | void GameLoader::stop() | 
|---|
| [2636] | 179 | { | 
|---|
|  | 180 | if(this->currentCampaign != NULL) | 
|---|
|  | 181 | this->currentCampaign->stop(); | 
|---|
|  | 182 | } | 
|---|
|  | 183 |  | 
|---|
|  | 184 |  | 
|---|
| [4597] | 185 | /** | 
|---|
| [6139] | 186 | *  pause the current entity | 
|---|
|  | 187 | * @returns error code if this action has caused a error | 
|---|
|  | 188 | * | 
|---|
|  | 189 | * this pauses the current entity or passes this call forth to the running entity. | 
|---|
|  | 190 | */ | 
|---|
| [3222] | 191 | ErrorMessage GameLoader::pause() | 
|---|
| [2636] | 192 | { | 
|---|
|  | 193 | this->isPaused = true; | 
|---|
|  | 194 | if(this->currentCampaign != NULL) | 
|---|
|  | 195 | this->currentCampaign->pause(); | 
|---|
|  | 196 | } | 
|---|
|  | 197 |  | 
|---|
|  | 198 |  | 
|---|
| [4597] | 199 | /** | 
|---|
| [6139] | 200 | *  resumes a pause | 
|---|
|  | 201 | * @returns error code if this action has caused a error | 
|---|
|  | 202 | * | 
|---|
|  | 203 | *  this resumess the current entity or passes this call forth to the running entity. | 
|---|
|  | 204 | */ | 
|---|
| [3222] | 205 | ErrorMessage GameLoader::resume() | 
|---|
| [2636] | 206 | { | 
|---|
|  | 207 | this->isPaused = false; | 
|---|
|  | 208 | if(this->currentCampaign != NULL) | 
|---|
|  | 209 | this->currentCampaign->resume(); | 
|---|
|  | 210 | } | 
|---|
|  | 211 |  | 
|---|
| [4443] | 212 |  | 
|---|
| [3225] | 213 | /** | 
|---|
| [4836] | 214 | *  reads a campaign definition file into a campaign class | 
|---|
|  | 215 | * @param fileName to be loaded | 
|---|
|  | 216 | * @returns the loaded campaign | 
|---|
| [6139] | 217 | * | 
|---|
|  | 218 | * this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns | 
|---|
|  | 219 | */ | 
|---|
| [7221] | 220 | Campaign* GameLoader::fileToCampaign(const std::string& fileName) | 
|---|
| [2636] | 221 | { | 
|---|
|  | 222 | /* do not entirely load the campaign. just the current world | 
|---|
|  | 223 | before start of each world, it has to be initialized so it | 
|---|
|  | 224 | can load everything it needs into memory then. | 
|---|
|  | 225 | */ | 
|---|
| [4597] | 226 |  | 
|---|
| [7221] | 227 | if( fileName.empty()) | 
|---|
| [4010] | 228 | { | 
|---|
| [4113] | 229 | PRINTF(2)("No filename specified for loading"); | 
|---|
| [4010] | 230 | return NULL; | 
|---|
|  | 231 | } | 
|---|
| [4597] | 232 |  | 
|---|
| [7221] | 233 | TiXmlDocument XMLDoc(fileName); | 
|---|
| [4010] | 234 | // load the campaign document | 
|---|
| [7221] | 235 | if( !XMLDoc.LoadFile(fileName)) | 
|---|
| [4010] | 236 | { | 
|---|
|  | 237 | // report an error | 
|---|
| [7221] | 238 | PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", fileName.c_str(), XMLDoc.ErrorDesc(), XMLDoc.ErrorRow(), XMLDoc.ErrorCol()); | 
|---|
| [4010] | 239 | return NULL; | 
|---|
|  | 240 | } | 
|---|
| [4597] | 241 |  | 
|---|
| [4010] | 242 | // check basic validity | 
|---|
| [7221] | 243 | TiXmlElement* root = XMLDoc.RootElement(); | 
|---|
| [4010] | 244 | assert( root != NULL); | 
|---|
| [4597] | 245 |  | 
|---|
| [4010] | 246 | if( strcmp( root->Value(), "Campaign")) | 
|---|
|  | 247 | { | 
|---|
|  | 248 | // report an error | 
|---|
| [4113] | 249 | PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); | 
|---|
| [4010] | 250 | return NULL; | 
|---|
|  | 251 | } | 
|---|
| [4597] | 252 |  | 
|---|
| [4010] | 253 | // construct campaign | 
|---|
| [7221] | 254 | return new Campaign( root); | 
|---|
| [2636] | 255 | } | 
|---|
|  | 256 |  | 
|---|
|  | 257 |  | 
|---|
| [6139] | 258 |  | 
|---|
|  | 259 | /** | 
|---|
| [4836] | 260 | *  handle keyboard commands | 
|---|
|  | 261 | * @param event the event to handle | 
|---|
| [5553] | 262 | */ | 
|---|
| [4410] | 263 | void GameLoader::process(const Event& event) | 
|---|
|  | 264 | { | 
|---|
|  | 265 | if( event.type == KeyMapper::PEV_NEXT_WORLD) | 
|---|
| [5553] | 266 | { | 
|---|
|  | 267 | if( likely(event.bPressed)) | 
|---|
| [4410] | 268 | { | 
|---|
| [6424] | 269 | this->switchToNextLevel(); | 
|---|
| [4410] | 270 | } | 
|---|
| [5553] | 271 | } | 
|---|
| [4410] | 272 | else if( event.type == KeyMapper::PEV_PAUSE) | 
|---|
| [5553] | 273 | { | 
|---|
|  | 274 | if( likely(event.bPressed)) | 
|---|
| [4410] | 275 | { | 
|---|
| [5553] | 276 | if(this->isPaused) | 
|---|
|  | 277 | this->resume(); | 
|---|
|  | 278 | else | 
|---|
|  | 279 | this->pause(); | 
|---|
| [4410] | 280 | } | 
|---|
| [5553] | 281 | } | 
|---|
| [4410] | 282 | else if( event.type == KeyMapper::PEV_QUIT) | 
|---|
| [5553] | 283 | { | 
|---|
|  | 284 | if( event.bPressed) this->stop(); | 
|---|
|  | 285 | } | 
|---|
|  | 286 | else if (event.type == EV_MAIN_QUIT) | 
|---|
|  | 287 | this->stop(); | 
|---|
| [4410] | 288 | } | 
|---|
|  | 289 |  | 
|---|
| [4443] | 290 |  | 
|---|
| [4487] | 291 | /** | 
|---|
| [6424] | 292 | *  this changes to the next level | 
|---|
| [6139] | 293 | */ | 
|---|
| [6424] | 294 | void GameLoader::switchToNextLevel() | 
|---|
| [2636] | 295 | { | 
|---|
|  | 296 | if(this->currentCampaign != NULL) | 
|---|
| [6424] | 297 | this->currentCampaign->switchToNextLevel(); | 
|---|
| [2636] | 298 | } | 
|---|
|  | 299 |  | 
|---|
| [3225] | 300 |  | 
|---|