[6402] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD |
---|
| 17 | |
---|
[7036] | 18 | #include <fstream> |
---|
| 19 | #include <iostream> |
---|
| 20 | |
---|
[6402] | 21 | #include "game_world_data.h" |
---|
| 22 | |
---|
| 23 | #include "state.h" |
---|
| 24 | #include "substring.h" |
---|
| 25 | |
---|
[7193] | 26 | #include "util/loading/game_loader.h" |
---|
[9869] | 27 | #include "util/loading/resource_manager.h" |
---|
[6402] | 28 | |
---|
| 29 | #include "world_entity.h" |
---|
| 30 | #include "player.h" |
---|
[10591] | 31 | #include "tools/camera.h" |
---|
| 32 | #include "tools/cameraman.h" |
---|
| 33 | #include "environments/terrain.h" |
---|
| 34 | #include "environments/skybox.h" |
---|
[8490] | 35 | #include "md2/md2Model.h" |
---|
[7287] | 36 | #include "world_entities/projectiles/projectile.h" |
---|
| 37 | #include "npcs/npc_test1.h" |
---|
[6402] | 38 | #include "playable.h" |
---|
| 39 | |
---|
| 40 | #include "light.h" |
---|
| 41 | |
---|
[7193] | 42 | #include "util/loading/factory.h" |
---|
[9869] | 43 | #include "loading/fast_factory.h" |
---|
| 44 | #include "util/loading/load_param_xml.h" |
---|
[6402] | 45 | |
---|
| 46 | #include "graphics_engine.h" |
---|
[9869] | 47 | #include "graphics_effect.h" |
---|
| 48 | #include "weather_effects/atmospheric_engine.h" |
---|
[6402] | 49 | #include "event_handler.h" |
---|
| 50 | #include "sound_engine.h" |
---|
| 51 | #include "cd_engine.h" |
---|
| 52 | #include "network_manager.h" |
---|
| 53 | #include "physics_engine.h" |
---|
[10513] | 54 | #include "ai_engine.h" |
---|
[6402] | 55 | |
---|
| 56 | #include "glmenu_imagescreen.h" |
---|
| 57 | |
---|
[7020] | 58 | #include "game_rules.h" |
---|
| 59 | |
---|
[6402] | 60 | #include "ogg_player.h" |
---|
| 61 | #include "shader.h" |
---|
| 62 | |
---|
| 63 | |
---|
[9869] | 64 | ObjectListDefinition(GameWorldData); |
---|
[6402] | 65 | /** |
---|
| 66 | * constructor of the GameWorldData |
---|
| 67 | */ |
---|
| 68 | GameWorldData::GameWorldData() |
---|
| 69 | { |
---|
[9869] | 70 | this->registerObject(this, GameWorldData::_objectList); |
---|
[6402] | 71 | |
---|
| 72 | this->glmis = NULL; |
---|
| 73 | |
---|
[10379] | 74 | |
---|
[6402] | 75 | this->localPlayer = NULL; |
---|
| 76 | this->sky = NULL; |
---|
| 77 | this->terrain = NULL; |
---|
| 78 | |
---|
| 79 | this->music = NULL; |
---|
| 80 | this->objectManager = NULL; |
---|
[7035] | 81 | this->gameRule = NULL; |
---|
[6402] | 82 | } |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * destructor for the GameWorldData |
---|
| 87 | */ |
---|
| 88 | GameWorldData::~GameWorldData() |
---|
[6404] | 89 | {} |
---|
[6402] | 90 | |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * initialize the GameWorldData |
---|
| 95 | */ |
---|
| 96 | ErrorMessage GameWorldData::init() |
---|
| 97 | { |
---|
| 98 | this->objectManager = new ObjectManager(); |
---|
| 99 | State::setObjectManager(this->objectManager); |
---|
| 100 | |
---|
| 101 | PNode::getNullParent(); |
---|
[10379] | 102 | Camera* localCamera = new Camera(); |
---|
[10593] | 103 | localCamera->setName ("GameWorldCamera"); |
---|
[10379] | 104 | State::setCamera(localCamera, localCamera->getTarget()); |
---|
[10394] | 105 | //CameraMan* camMan = new CameraMan(); |
---|
| 106 | //State::setCameraman(camMan); |
---|
[6402] | 107 | LightManager::getInstance(); |
---|
[10513] | 108 | AIEngine::getInstance(); |
---|
[6402] | 109 | |
---|
[8975] | 110 | // GraphicsEngine::getInstance()->displayFPS(true); |
---|
[8717] | 111 | |
---|
| 112 | return ErrorMessage(); |
---|
[6402] | 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | * loads the data from the xml file |
---|
| 118 | * @param root reference to the xml root element |
---|
| 119 | */ |
---|
[7370] | 120 | ErrorMessage GameWorldData::loadData(const TiXmlElement* root) |
---|
[6402] | 121 | { |
---|
[9869] | 122 | assert (root != NULL); |
---|
[6402] | 123 | // load the parameters |
---|
| 124 | // name |
---|
[9869] | 125 | std::string string = LoadParamBase::grabParameter( root, "name"); |
---|
[7221] | 126 | if( string.empty() ) |
---|
[6402] | 127 | { |
---|
| 128 | PRINTF(2)("GameWorld is missing a proper 'name'\n"); |
---|
| 129 | this->setName("Unknown"); |
---|
| 130 | } |
---|
| 131 | else |
---|
[7221] | 132 | this->setName(string.c_str()); |
---|
[6402] | 133 | |
---|
[10513] | 134 | |
---|
[6402] | 135 | this->loadGUI(root); |
---|
| 136 | this->loadWorldEntities(root); |
---|
[10394] | 137 | this->loadCameras(root); |
---|
[6402] | 138 | this->loadScene(root); |
---|
[8717] | 139 | |
---|
| 140 | return ErrorMessage(); |
---|
[6402] | 141 | } |
---|
| 142 | |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * unloads the data from the xml file |
---|
| 146 | */ |
---|
| 147 | ErrorMessage GameWorldData::unloadData() |
---|
| 148 | { |
---|
| 149 | this->unloadGUI(); |
---|
| 150 | this->unloadWorldEntities(); |
---|
| 151 | this->unloadScene(); |
---|
[10394] | 152 | this->unloadCameras(); |
---|
[8717] | 153 | |
---|
[10513] | 154 | // killl ai engine |
---|
| 155 | delete AIEngine::getInstance(); |
---|
| 156 | |
---|
[8717] | 157 | return ErrorMessage(); |
---|
[6402] | 158 | } |
---|
| 159 | |
---|
[10394] | 160 | /** |
---|
| 161 | * @brief creates the cameras |
---|
| 162 | * @param root reference to the xml root element |
---|
| 163 | */ |
---|
| 164 | ErrorMessage GameWorldData::loadCameras(const TiXmlElement* root) |
---|
| 165 | { |
---|
| 166 | const TiXmlElement* element = root->FirstChildElement("CameraMan"); |
---|
| 167 | if( element != NULL) |
---|
| 168 | { |
---|
| 169 | CameraMan* camMan = new CameraMan(element); |
---|
| 170 | State::setCameraman(camMan); |
---|
| 171 | } |
---|
[10399] | 172 | else |
---|
| 173 | { |
---|
| 174 | CameraMan* camMan = new CameraMan(); |
---|
| 175 | State::setCameraman(camMan); |
---|
| 176 | } |
---|
[10394] | 177 | return ErrorMessage(); |
---|
| 178 | } |
---|
[6402] | 179 | |
---|
[10394] | 180 | |
---|
[6402] | 181 | /** |
---|
[10394] | 182 | * @brief unloads the camera |
---|
| 183 | */ |
---|
| 184 | ErrorMessage GameWorldData::unloadCameras() |
---|
| 185 | { |
---|
| 186 | CameraMan* camMan = State::getCameraman() ; |
---|
| 187 | if(camMan != NULL){ |
---|
| 188 | delete camMan; |
---|
| 189 | State::setCameraman(NULL); |
---|
| 190 | } |
---|
| 191 | return ErrorMessage(); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | |
---|
| 195 | /** |
---|
[7370] | 196 | * @brief loads the GUI data |
---|
[6402] | 197 | * @param root reference to the xml root element |
---|
| 198 | */ |
---|
[7370] | 199 | ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root) |
---|
[6402] | 200 | { |
---|
[7370] | 201 | const TiXmlElement* element = root->FirstChildElement("LoadScreen"); |
---|
[6402] | 202 | if( element == NULL) |
---|
| 203 | { |
---|
| 204 | PRINTF(2)("no LoadScreen specified, loading default\n"); |
---|
| 205 | |
---|
[10317] | 206 | glmis->setBackgroundImage("textures/load_screens/default.jpg"); |
---|
[6402] | 207 | this->glmis->setMaximum(8); |
---|
[6988] | 208 | // this->glmis->draw(); |
---|
[6402] | 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
| 212 | this->glmis->loadParams(element); |
---|
[6988] | 213 | // this->glmis->draw(); |
---|
[6402] | 214 | } |
---|
| 215 | this->glmis->draw(); |
---|
[8717] | 216 | |
---|
| 217 | return ErrorMessage(); |
---|
[6402] | 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | /** |
---|
[7370] | 222 | * @brief unloads the GUI data |
---|
[6402] | 223 | */ |
---|
| 224 | ErrorMessage GameWorldData::unloadGUI() |
---|
| 225 | { |
---|
| 226 | delete this->glmis; |
---|
[8717] | 227 | |
---|
| 228 | return ErrorMessage(); |
---|
[6402] | 229 | } |
---|
| 230 | |
---|
| 231 | |
---|
| 232 | /** |
---|
[7370] | 233 | * @brief loads the world entities from the xml file |
---|
[6402] | 234 | * @param root reference to the xml root parameter |
---|
| 235 | */ |
---|
[7370] | 236 | ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root) |
---|
[6402] | 237 | { |
---|
[7370] | 238 | const TiXmlElement* element = root->FirstChildElement("WorldEntities"); |
---|
[9406] | 239 | bool mouseCaptured = EventHandler::getInstance()->grabbedEvents(); |
---|
| 240 | EventHandler::getInstance()->grabEvents(false); |
---|
[6402] | 241 | |
---|
| 242 | if( element == NULL) |
---|
| 243 | { |
---|
| 244 | PRINTF(1)("GameWorld is missing 'WorldEntities'\n"); |
---|
| 245 | } |
---|
| 246 | else |
---|
| 247 | { |
---|
| 248 | element = element->FirstChildElement(); |
---|
| 249 | // load Players/Objects/Whatever |
---|
| 250 | PRINTF(4)("Loading WorldEntities\n"); |
---|
| 251 | while( element != NULL) |
---|
| 252 | { |
---|
| 253 | BaseObject* created = Factory::fabricate(element); |
---|
| 254 | if( created != NULL ) |
---|
[9406] | 255 | PRINTF(4)("Created a %s: %s\n", created->getClassCName(), created->getCName()); |
---|
[6402] | 256 | |
---|
| 257 | //todo do this more elegant |
---|
[9869] | 258 | if( element->Value() == "SkyBox" && created->isA(SkyBox::staticClassID())) |
---|
[6771] | 259 | { |
---|
[6402] | 260 | this->sky = dynamic_cast<WorldEntity*>(created); |
---|
[6771] | 261 | State::setSkyBox(dynamic_cast<SkyBox*>(this->sky)); |
---|
| 262 | } |
---|
[9869] | 263 | if( element->Value() == "Terrain" && created->isA(Terrain::staticClassID())) |
---|
[6402] | 264 | { |
---|
| 265 | this->terrain = dynamic_cast<Terrain*>(created); |
---|
| 266 | CDEngine::getInstance()->setTerrain(terrain); |
---|
| 267 | } |
---|
| 268 | element = element->NextSiblingElement(); |
---|
| 269 | this->glmis->step(); //! @todo temporary |
---|
| 270 | } |
---|
| 271 | PRINTF(4)("Done loading WorldEntities\n"); |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | // Create a Player |
---|
| 275 | this->localPlayer = new Player(); |
---|
[7076] | 276 | State::setPlayer(this->localPlayer); |
---|
[6402] | 277 | |
---|
[9869] | 278 | if (!Playable::objectList().empty()) |
---|
[6402] | 279 | { |
---|
[7370] | 280 | /// TODO Make this also loadable |
---|
[9869] | 281 | this->localPlayer->setPlayable(Playable::objectList().front()); |
---|
[6402] | 282 | } |
---|
| 283 | |
---|
[7370] | 284 | // Fill the EntityLists. Tick then Draw: |
---|
| 285 | this->tickLists.push_back(OM_DEAD_TICK); |
---|
| 286 | this->tickLists.push_back(OM_ENVIRON); |
---|
| 287 | this->tickLists.push_back(OM_COMMON); |
---|
| 288 | this->tickLists.push_back(OM_GROUP_00); |
---|
| 289 | this->tickLists.push_back(OM_GROUP_00_PROJ); |
---|
| 290 | this->tickLists.push_back(OM_GROUP_01); |
---|
| 291 | this->tickLists.push_back(OM_GROUP_01_PROJ); |
---|
[9235] | 292 | this->tickLists.push_back(OM_GROUP_02); |
---|
[7370] | 293 | |
---|
| 294 | this->drawLists.push_back(OM_ENVIRON_NOTICK); |
---|
| 295 | this->drawLists.push_back(OM_ENVIRON); |
---|
| 296 | this->drawLists.push_back(OM_GROUP_00); |
---|
| 297 | this->drawLists.push_back(OM_GROUP_00_PROJ); |
---|
| 298 | this->drawLists.push_back(OM_GROUP_01); |
---|
| 299 | this->drawLists.push_back(OM_GROUP_01_PROJ); |
---|
[9235] | 300 | this->drawLists.push_back(OM_GROUP_02); |
---|
| 301 | this->drawLists.push_back(OM_COMMON); |
---|
[7370] | 302 | |
---|
[6402] | 303 | /* init the pnode tree */ |
---|
| 304 | PNode::getNullParent()->init(); |
---|
[8717] | 305 | |
---|
[9406] | 306 | EventHandler::getInstance()->grabEvents(mouseCaptured); |
---|
| 307 | |
---|
[8717] | 308 | return ErrorMessage(); |
---|
[6402] | 309 | } |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | /** |
---|
[6404] | 313 | * unloads the world entities |
---|
[6402] | 314 | */ |
---|
| 315 | ErrorMessage GameWorldData::unloadWorldEntities() |
---|
| 316 | { |
---|
| 317 | FastFactory::flushAll(true); |
---|
[7029] | 318 | GraphicsEngine::getInstance()->displayFPS(false); |
---|
[6402] | 319 | // erease everything that is left. |
---|
| 320 | //secondary cleanup of PNodes; |
---|
[9869] | 321 | while (!PNode::objectList().empty()) |
---|
| 322 | delete PNode::objectList().front(); |
---|
| 323 | |
---|
[6402] | 324 | /* remove the player object */ |
---|
| 325 | if( this->localPlayer) |
---|
| 326 | delete this->localPlayer; |
---|
[7126] | 327 | State::setPlayer(NULL); |
---|
[7311] | 328 | this->localPlayer = NULL; |
---|
| 329 | State::setCamera(NULL, NULL); |
---|
| 330 | this->sky = NULL; |
---|
| 331 | this->terrain = NULL; |
---|
[6402] | 332 | |
---|
[9869] | 333 | while (!GraphicsEffect::objectList().empty()) |
---|
| 334 | delete GraphicsEffect::objectList().front(); |
---|
[6981] | 335 | |
---|
| 336 | |
---|
[9869] | 337 | while (!Element2D::objectList().empty()) |
---|
| 338 | delete Element2D::objectList().front(); |
---|
[6981] | 339 | |
---|
[7370] | 340 | // At this Point all the WorldEntites should be unloaded. |
---|
| 341 | this->tickLists.clear(); |
---|
| 342 | this->drawLists.clear(); |
---|
[6981] | 343 | |
---|
[7370] | 344 | // unload the resources loaded in this Level !! |
---|
[9869] | 345 | /// FIXME TODO HACK!! |
---|
| 346 | // ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL); |
---|
[6402] | 347 | |
---|
| 348 | if (State::getObjectManager() == this->objectManager) |
---|
| 349 | { |
---|
| 350 | State::setObjectManager(NULL); |
---|
| 351 | delete this->objectManager; |
---|
| 352 | } |
---|
[7311] | 353 | this->objectManager = NULL; |
---|
[6863] | 354 | |
---|
[6771] | 355 | if(State::getSkyBox()) |
---|
| 356 | State::setSkyBox(NULL); |
---|
[6863] | 357 | |
---|
[7311] | 358 | this->glmis = NULL; |
---|
[8717] | 359 | |
---|
| 360 | return ErrorMessage(); |
---|
[6402] | 361 | } |
---|
| 362 | |
---|
| 363 | |
---|
| 364 | /** |
---|
[7370] | 365 | * @brief loads the scene data |
---|
[6402] | 366 | * @param root reference to the xml root element |
---|
| 367 | */ |
---|
[7370] | 368 | ErrorMessage GameWorldData::loadScene(const TiXmlElement* root) |
---|
[6402] | 369 | { |
---|
| 370 | LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams); |
---|
[6815] | 371 | LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams); |
---|
[7810] | 372 | LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams); |
---|
[6402] | 373 | |
---|
[6827] | 374 | LoadParam(root, "Music", this, GameWorldData, setSoundTrack); |
---|
| 375 | |
---|
[7020] | 376 | LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule); |
---|
| 377 | |
---|
[10379] | 378 | LoadParam(root, "clip-region", State::getCameraman(), CameraMan, setClipRegion); |
---|
[7020] | 379 | |
---|
[9235] | 380 | |
---|
[7020] | 381 | //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams); |
---|
[6402] | 382 | //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams); |
---|
| 383 | |
---|
[8717] | 384 | |
---|
[10379] | 385 | OrxSound::SoundEngine::getInstance()->setListener(State::getCamera()); |
---|
| 386 | |
---|
[8717] | 387 | return ErrorMessage(); |
---|
[6402] | 388 | } |
---|
| 389 | |
---|
| 390 | |
---|
[7020] | 391 | |
---|
[6402] | 392 | /** |
---|
| 393 | * unloads the scene data |
---|
| 394 | */ |
---|
| 395 | ErrorMessage GameWorldData::unloadScene() |
---|
| 396 | { |
---|
| 397 | /* delete some garphics and scene eingines */ |
---|
| 398 | delete LightManager::getInstance(); |
---|
[7810] | 399 | delete AtmosphericEngine::getInstance(); |
---|
[6402] | 400 | |
---|
[7287] | 401 | if (this->music != NULL) |
---|
| 402 | this->setSoundTrack(""); |
---|
[6402] | 403 | |
---|
| 404 | /* unload the shaders */ |
---|
| 405 | Shader::suspendShader(); |
---|
[7488] | 406 | |
---|
| 407 | State::setGameRules(NULL); |
---|
[8717] | 408 | |
---|
| 409 | return ErrorMessage(); |
---|
[6402] | 410 | } |
---|
| 411 | |
---|
[6634] | 412 | |
---|
[7221] | 413 | void GameWorldData::setSoundTrack(const std::string& name) |
---|
[6634] | 414 | { |
---|
[6827] | 415 | if (this->music != NULL) |
---|
[7287] | 416 | delete this->music; |
---|
| 417 | this->music = NULL; |
---|
[6827] | 418 | |
---|
[7221] | 419 | if (!name.empty()) |
---|
[6987] | 420 | { |
---|
[7221] | 421 | PRINTF(3)("Setting Sound Track to %s\n", name.c_str()); |
---|
[9869] | 422 | std::string oggFile = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(name); |
---|
[7460] | 423 | this->music = new OrxSound::OggPlayer(oggFile); |
---|
[10428] | 424 | // if (this->localPlayer != NULL) |
---|
| 425 | // this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title()); |
---|
[6988] | 426 | |
---|
| 427 | //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL); |
---|
| 428 | //assert(this->music->isA(CL_SOUND_OGG_PLAYER)); |
---|
[6987] | 429 | } |
---|
[6634] | 430 | } |
---|
| 431 | |
---|
| 432 | |
---|
[7020] | 433 | void GameWorldData::loadGameRule(const TiXmlElement* root) |
---|
| 434 | { |
---|
[7036] | 435 | const TiXmlElement* element = root->FirstChildElement(); |
---|
| 436 | while( element != NULL) |
---|
[7020] | 437 | { |
---|
[7461] | 438 | PRINTF(2)("============ GameRules ==\n"); |
---|
| 439 | PRINTF(2)("creating %s\n", element->Value()); |
---|
[7036] | 440 | BaseObject* created = Factory::fabricate(element); |
---|
[9869] | 441 | if (created != NULL && created->isA(GameRules::staticClassID())) |
---|
[7020] | 442 | { |
---|
[7036] | 443 | this->gameRule = dynamic_cast<GameRules*>(created); |
---|
[7039] | 444 | State::setGameRules(this->gameRule); |
---|
[7482] | 445 | // if there is a valid game rule loaded, return because it is not thought to load multiple game rules |
---|
[7462] | 446 | return; |
---|
[7020] | 447 | } |
---|
[7036] | 448 | else |
---|
| 449 | { |
---|
| 450 | PRINTF(1)("Could not create a %s\n", element->Value()); |
---|
| 451 | delete created; |
---|
| 452 | } |
---|
| 453 | element = element->NextSiblingElement(); |
---|
[7020] | 454 | } |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | |
---|
| 458 | |
---|