| 1 |  | 
|---|
| 2 |  | 
|---|
| 3 | /*  | 
|---|
| 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 |  | 
|---|
| 18 |  | 
|---|
| 19 | #include "game_loader.h" | 
|---|
| 20 | #include "campaign.h" | 
|---|
| 21 | #include "world.h" | 
|---|
| 22 | #include "player.h" | 
|---|
| 23 | #include "orxonox.h" | 
|---|
| 24 | #include "camera.h" | 
|---|
| 25 | #include "command_node.h" | 
|---|
| 26 | #include "vector.h" | 
|---|
| 27 | #include "resource_manager.h" | 
|---|
| 28 | #include "factory.h" | 
|---|
| 29 |  | 
|---|
| 30 | #include <string.h> | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 | using namespace std; | 
|---|
| 34 |  | 
|---|
| 35 |  | 
|---|
| 36 | GameLoader* GameLoader::singletonRef = 0; | 
|---|
| 37 |  | 
|---|
| 38 |  | 
|---|
| 39 | GameLoader::GameLoader ()  | 
|---|
| 40 | { | 
|---|
| 41 |   first = NULL; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | GameLoader::~GameLoader () {} | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | /** | 
|---|
| 49 |    \brief this class is a singleton class | 
|---|
| 50 |    \returns an instance of itself | 
|---|
| 51 |  | 
|---|
| 52 |    if you are unsure about singleton classes, check the theory out on the internet :) | 
|---|
| 53 | */ | 
|---|
| 54 | GameLoader* GameLoader::getInstance() | 
|---|
| 55 | { | 
|---|
| 56 |   if(singletonRef == NULL) | 
|---|
| 57 |     singletonRef = new GameLoader(); | 
|---|
| 58 |   return singletonRef; | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 |  | 
|---|
| 62 | ErrorMessage GameLoader::init() | 
|---|
| 63 | { | 
|---|
| 64 |   if(this->currentCampaign != NULL) | 
|---|
| 65 |     this->currentCampaign->init(); | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 | /** | 
|---|
| 70 |    \brief reads a campaign definition file into a campaign class | 
|---|
| 71 |    \param filename to be loaded | 
|---|
| 72 |    \returns the loaded campaign | 
|---|
| 73 |  | 
|---|
| 74 |    this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns | 
|---|
| 75 | */ | 
|---|
| 76 | ErrorMessage GameLoader::loadCampaign(char* name) | 
|---|
| 77 | { | 
|---|
| 78 |   ErrorMessage errorCode; | 
|---|
| 79 |   char* campaignName; | 
|---|
| 80 |   if (ResourceManager::isFile(name)) | 
|---|
| 81 |     { | 
|---|
| 82 |       this->currentCampaign = this->fileToCampaign(name); | 
|---|
| 83 |     } | 
|---|
| 84 |   else | 
|---|
| 85 |     { | 
|---|
| 86 |       campaignName = new char[strlen(ResourceManager::getInstance()->getDataDir())+strlen(name)]; | 
|---|
| 87 |       sprintf(campaignName, "%s%s", ResourceManager::getInstance()->getDataDir(), name); | 
|---|
| 88 |       this->currentCampaign = this->fileToCampaign(campaignName); | 
|---|
| 89 |       delete campaignName; | 
|---|
| 90 |     } | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 |  | 
|---|
| 94 | /** | 
|---|
| 95 |    \brief loads a debug campaign for test purposes only. | 
|---|
| 96 |    \param the identifier of the campaign. | 
|---|
| 97 |    \returns error message if not able to do so. | 
|---|
| 98 | */ | 
|---|
| 99 | ErrorMessage GameLoader::loadDebugCampaign(Uint32 campaignID) | 
|---|
| 100 | { | 
|---|
| 101 |   switch(campaignID) | 
|---|
| 102 |     { | 
|---|
| 103 |       /*  | 
|---|
| 104 |          Debug Level 0: Debug level used to test the base frame work. | 
|---|
| 105 |          As you can see, all storyentity data is allocated before game | 
|---|
| 106 |          start. the storyentity will load themselfs shortly before start | 
|---|
| 107 |          through the StoryEntity::init() funtion. | 
|---|
| 108 |       */ | 
|---|
| 109 |     case DEBUG_CAMPAIGN_0: | 
|---|
| 110 |       { | 
|---|
| 111 |         Campaign* debugCampaign = new Campaign(); | 
|---|
| 112 |  | 
|---|
| 113 |         World* world0 = new World(DEBUG_WORLD_0); | 
|---|
| 114 |         world0->setNextStoryID(WORLD_ID_1); | 
|---|
| 115 |         debugCampaign->addEntity(world0, WORLD_ID_0); | 
|---|
| 116 |  | 
|---|
| 117 |         World* world1 = new World(DEBUG_WORLD_1); | 
|---|
| 118 |         world1->setNextStoryID(WORLD_ID_2); | 
|---|
| 119 |         debugCampaign->addEntity(world1, WORLD_ID_1); | 
|---|
| 120 |  | 
|---|
| 121 |         World* world2 = new World(DEBUG_WORLD_2); | 
|---|
| 122 |         world2->setNextStoryID(WORLD_ID_GAMEEND); | 
|---|
| 123 |         debugCampaign->addEntity(world2, WORLD_ID_2); | 
|---|
| 124 |  | 
|---|
| 125 |         this->currentCampaign = debugCampaign; | 
|---|
| 126 |         break; | 
|---|
| 127 |       } | 
|---|
| 128 |     } | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | ErrorMessage GameLoader::start() | 
|---|
| 132 | { | 
|---|
| 133 |   if(this->currentCampaign != NULL) | 
|---|
| 134 |     this->currentCampaign->start(); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 |  | 
|---|
| 138 | ErrorMessage GameLoader::stop() | 
|---|
| 139 | { | 
|---|
| 140 |   if(this->currentCampaign != NULL) | 
|---|
| 141 |     this->currentCampaign->stop(); | 
|---|
| 142 |   this->currentCampaign = NULL; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 | ErrorMessage GameLoader::pause() | 
|---|
| 147 | { | 
|---|
| 148 |   this->isPaused = true; | 
|---|
| 149 |   if(this->currentCampaign != NULL) | 
|---|
| 150 |     this->currentCampaign->pause(); | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 |  | 
|---|
| 154 | ErrorMessage GameLoader::resume() | 
|---|
| 155 | { | 
|---|
| 156 |   this->isPaused = false; | 
|---|
| 157 |   if(this->currentCampaign != NULL) | 
|---|
| 158 |     this->currentCampaign->resume(); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | /** | 
|---|
| 162 |    \brief release the mem | 
|---|
| 163 |  */ | 
|---|
| 164 | ErrorMessage GameLoader::destroy() | 
|---|
| 165 | {} | 
|---|
| 166 |  | 
|---|
| 167 |  | 
|---|
| 168 | /** | 
|---|
| 169 |    \brief reads a campaign definition file into a campaign class | 
|---|
| 170 |    \param filename to be loaded | 
|---|
| 171 |    \returns the loaded campaign | 
|---|
| 172 |  | 
|---|
| 173 |    this will interprete the map/campaign files and recursivly load a tree of worlds/campaigns | 
|---|
| 174 | */ | 
|---|
| 175 | Campaign* GameLoader::fileToCampaign(const char *name) | 
|---|
| 176 | { | 
|---|
| 177 |   /* do not entirely load the campaign. just the current world | 
|---|
| 178 |      before start of each world, it has to be initialized so it | 
|---|
| 179 |      can load everything it needs into memory then. | 
|---|
| 180 |   */ | 
|---|
| 181 |    | 
|---|
| 182 |   if( name == NULL) | 
|---|
| 183 |     { | 
|---|
| 184 |       PRINTF(2)("No filename specified for loading"); | 
|---|
| 185 |       return NULL; | 
|---|
| 186 |     } | 
|---|
| 187 |    | 
|---|
| 188 |   TiXmlDocument* XMLDoc = new TiXmlDocument( name); | 
|---|
| 189 |   // load the campaign document | 
|---|
| 190 |   if( !XMLDoc->LoadFile()) | 
|---|
| 191 |     { | 
|---|
| 192 |       // report an error | 
|---|
| 193 |       PRINTF(1)("Could not load XML File %s: %s @ %d:%d\n", name, XMLDoc->ErrorDesc(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol()); | 
|---|
| 194 |       delete XMLDoc; | 
|---|
| 195 |       return NULL; | 
|---|
| 196 |     } | 
|---|
| 197 |          | 
|---|
| 198 |   // check basic validity | 
|---|
| 199 |   TiXmlElement* root = XMLDoc->RootElement(); | 
|---|
| 200 |   assert( root != NULL); | 
|---|
| 201 |          | 
|---|
| 202 |   if( strcmp( root->Value(), "Campaign")) | 
|---|
| 203 |     { | 
|---|
| 204 |       // report an error | 
|---|
| 205 |       PRINTF(2)("Specified XML File is not an orxonox campaign file (Campaign element missing)\n"); | 
|---|
| 206 |       delete XMLDoc; | 
|---|
| 207 |       return NULL; | 
|---|
| 208 |     } | 
|---|
| 209 |          | 
|---|
| 210 |   // construct campaign | 
|---|
| 211 |   Campaign* c = new Campaign( root); | 
|---|
| 212 |          | 
|---|
| 213 |   // free the XML data | 
|---|
| 214 |   delete XMLDoc; | 
|---|
| 215 |          | 
|---|
| 216 |   return c; | 
|---|
| 217 | } | 
|---|
| 218 |  | 
|---|
| 219 |  | 
|---|
| 220 | /** | 
|---|
| 221 |    \brief handle keyboard commands  | 
|---|
| 222 |    \param cmd: the command to handle | 
|---|
| 223 |    \returns true if the command was handled by the system | 
|---|
| 224 | */ | 
|---|
| 225 | bool GameLoader::worldCommand (Command* cmd) | 
|---|
| 226 | { | 
|---|
| 227 |   if( !strcmp( cmd->cmd, CONFIG_NAME_NEXT_WORLD)) | 
|---|
| 228 |     { | 
|---|
| 229 |       if( !cmd->bUp)  | 
|---|
| 230 |         { | 
|---|
| 231 |           this->nextLevel(); | 
|---|
| 232 |         } | 
|---|
| 233 |       return true; | 
|---|
| 234 |     } | 
|---|
| 235 |   else if( !strcmp( cmd->cmd, CONFIG_NAME_PREV_WORLD)) | 
|---|
| 236 |     { | 
|---|
| 237 |       if( !cmd->bUp) | 
|---|
| 238 |         { | 
|---|
| 239 |           this->previousLevel(); | 
|---|
| 240 |         } | 
|---|
| 241 |       return true; | 
|---|
| 242 |     } | 
|---|
| 243 |   else if( !strcmp( cmd->cmd, CONFIG_NAME_PAUSE)) | 
|---|
| 244 |     { | 
|---|
| 245 |       if( !cmd->bUp) | 
|---|
| 246 |         { | 
|---|
| 247 |           if(this->isPaused) | 
|---|
| 248 |             this->resume(); | 
|---|
| 249 |           else | 
|---|
| 250 |             this->pause(); | 
|---|
| 251 |         } | 
|---|
| 252 |       return true; | 
|---|
| 253 |     } | 
|---|
| 254 |   else if( !strcmp( cmd->cmd, CONFIG_NAME_QUIT)) | 
|---|
| 255 |     { | 
|---|
| 256 |       if( !cmd->bUp) this->stop(); | 
|---|
| 257 |       return true; | 
|---|
| 258 |     } | 
|---|
| 259 |   return false; | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 |  | 
|---|
| 263 | /* | 
|---|
| 264 |   \brief this changes to the next level | 
|---|
| 265 | */ | 
|---|
| 266 | void GameLoader::nextLevel() | 
|---|
| 267 | { | 
|---|
| 268 |   if(this->currentCampaign != NULL) | 
|---|
| 269 |     this->currentCampaign->nextLevel(); | 
|---|
| 270 | } | 
|---|
| 271 |  | 
|---|
| 272 |  | 
|---|
| 273 | /* | 
|---|
| 274 |   \brief change to the previous level - not implemented | 
|---|
| 275 |  | 
|---|
| 276 |   this propably useless | 
|---|
| 277 | */ | 
|---|
| 278 | void GameLoader::previousLevel() | 
|---|
| 279 | { | 
|---|
| 280 |   if(this->currentCampaign != NULL) | 
|---|
| 281 |     this->currentCampaign->previousLevel(); | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | /** | 
|---|
| 285 |    \brief add a Factory to the Factory Q | 
|---|
| 286 |    \param factory a Factory to be registered | 
|---|
| 287 | */ | 
|---|
| 288 | void GameLoader::registerFactory( Factory* factory) | 
|---|
| 289 | { | 
|---|
| 290 |         assert( factory != NULL); | 
|---|
| 291 |          | 
|---|
| 292 |         PRINTF(4)("Registered factory for '%s'\n", factory->getFactoryName()); | 
|---|
| 293 |          | 
|---|
| 294 |         if( first == NULL) first = factory; | 
|---|
| 295 |         else first->registerFactory( factory); | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | /** | 
|---|
| 299 |    \brief load a StoryEntity | 
|---|
| 300 |    \param element a XMLElement containing all the needed info | 
|---|
| 301 | */ | 
|---|
| 302 | BaseObject* GameLoader::fabricate( TiXmlElement* element) | 
|---|
| 303 | { | 
|---|
| 304 |   assert( element != NULL); | 
|---|
| 305 |          | 
|---|
| 306 |   if( first == NULL) | 
|---|
| 307 |     { | 
|---|
| 308 |       PRINTF(1)("GameLoader does not know any factories, fabricate() failed\n"); | 
|---|
| 309 |       return NULL; | 
|---|
| 310 |     } | 
|---|
| 311 |          | 
|---|
| 312 |   if( element->Value() != NULL) | 
|---|
| 313 |     { | 
|---|
| 314 |       PRINTF(4)("Attempting fabrication of a '%s'\n", element->Value()); | 
|---|
| 315 |       BaseObject* b = first->fabricate( element); | 
|---|
| 316 |       if( b == NULL)  | 
|---|
| 317 |         PRINTF(2)("Failed to fabricate a '%s'\n", element->Value()); | 
|---|
| 318 |       else  | 
|---|
| 319 |         PRINTF(4)("Successfully fabricated a '%s'\n", element->Value()); | 
|---|
| 320 |       return b; | 
|---|
| 321 |     } | 
|---|
| 322 |          | 
|---|
| 323 |   PRINTF(2)("Fabricate failed, TiXmlElement did not contain a value\n"); | 
|---|
| 324 |          | 
|---|
| 325 |   return NULL; | 
|---|
| 326 | } | 
|---|