Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/game_world_data.cc @ 10546

Last change on this file since 10546 was 10513, checked in by patrick, 18 years ago

ai segfault fixed

File size: 11.0 KB
Line 
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
18#include <fstream>
19#include <iostream>
20
21#include "game_world_data.h"
22
23#include "state.h"
24#include "substring.h"
25
26#include "util/loading/game_loader.h"
27#include "util/loading/resource_manager.h"
28
29#include "world_entity.h"
30#include "player.h"
31#include "camera.h"
32#include "cameraman.h"
33#include "terrain.h"
34#include "skybox.h"
35#include "md2/md2Model.h"
36#include "world_entities/projectiles/projectile.h"
37#include "npcs/npc_test1.h"
38#include "playable.h"
39
40#include "light.h"
41
42#include "util/loading/factory.h"
43#include "loading/fast_factory.h"
44#include "util/loading/load_param_xml.h"
45
46#include "graphics_engine.h"
47#include "graphics_effect.h"
48#include "weather_effects/atmospheric_engine.h"
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"
54#include "ai_engine.h"
55
56#include "glmenu_imagescreen.h"
57
58#include "game_rules.h"
59
60#include "ogg_player.h"
61#include "shader.h"
62
63
64ObjectListDefinition(GameWorldData);
65/**
66 * constructor of the GameWorldData
67 */
68GameWorldData::GameWorldData()
69{
70  this->registerObject(this, GameWorldData::_objectList);
71
72  this->glmis = NULL;
73
74
75  this->localPlayer = NULL;
76  this->sky = NULL;
77  this->terrain = NULL;
78
79  this->music = NULL;
80  this->objectManager = NULL;
81  this->gameRule = NULL;
82}
83
84
85/**
86 * destructor for the GameWorldData
87 */
88GameWorldData::~GameWorldData()
89{}
90
91
92
93/**
94 *  initialize the GameWorldData
95 */
96ErrorMessage GameWorldData::init()
97{
98  this->objectManager = new ObjectManager();
99  State::setObjectManager(this->objectManager);
100
101  PNode::getNullParent();
102  Camera* localCamera = new Camera();
103    localCamera->setName ("GameWorld-Camera");
104  State::setCamera(localCamera, localCamera->getTarget());
105  //CameraMan* camMan = new CameraMan();
106  //State::setCameraman(camMan);
107  LightManager::getInstance();
108  AIEngine::getInstance();
109
110//  GraphicsEngine::getInstance()->displayFPS(true);
111
112  return ErrorMessage();
113}
114
115
116/**
117 *  loads the data from the xml file
118 * @param root reference to the xml root element
119 */
120ErrorMessage GameWorldData::loadData(const TiXmlElement* root)
121{
122  assert (root != NULL);
123  // load the parameters
124  // name
125  std::string string = LoadParamBase::grabParameter( root, "name");
126  if( string.empty() )
127  {
128    PRINTF(2)("GameWorld is missing a proper 'name'\n");
129    this->setName("Unknown");
130  }
131  else
132    this->setName(string.c_str());
133
134
135  this->loadGUI(root);
136  this->loadWorldEntities(root);
137  this->loadCameras(root);
138  this->loadScene(root);
139
140  return ErrorMessage();
141}
142
143
144/**
145 *  unloads the data from the xml file
146 */
147ErrorMessage GameWorldData::unloadData()
148{
149  this->unloadGUI();
150  this->unloadWorldEntities();
151  this->unloadScene();
152  this->unloadCameras();
153
154  // killl ai engine
155  delete AIEngine::getInstance();
156
157  return ErrorMessage();
158}
159
160/**
161 * @brief creates the cameras
162 * @param root reference to the xml root element
163 */
164ErrorMessage 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  }
172  else
173  {
174    CameraMan* camMan = new CameraMan();
175    State::setCameraman(camMan);
176  }
177  return ErrorMessage();
178}
179
180
181/**
182 * @brief unloads the camera
183 */
184ErrorMessage 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/**
196 * @brief loads the GUI data
197 * @param root reference to the xml root element
198 */
199ErrorMessage GameWorldData::loadGUI(const TiXmlElement* root)
200{
201  const TiXmlElement* element = root->FirstChildElement("LoadScreen");
202  if( element == NULL)
203  {
204    PRINTF(2)("no LoadScreen specified, loading default\n");
205
206    glmis->setBackgroundImage("textures/load_screens/default.jpg");
207    this->glmis->setMaximum(8);
208    //     this->glmis->draw();
209  }
210  else
211  {
212    this->glmis->loadParams(element);
213    //     this->glmis->draw();
214  }
215  this->glmis->draw();
216
217  return ErrorMessage();
218}
219
220
221/**
222 * @brief unloads the GUI data
223 */
224ErrorMessage GameWorldData::unloadGUI()
225{
226  delete this->glmis;
227
228  return ErrorMessage();
229}
230
231
232/**
233 * @brief loads the world entities from the xml file
234 * @param root reference to the xml root parameter
235 */
236ErrorMessage GameWorldData::loadWorldEntities(const TiXmlElement* root)
237{
238  const TiXmlElement* element = root->FirstChildElement("WorldEntities");
239  bool mouseCaptured = EventHandler::getInstance()->grabbedEvents();
240  EventHandler::getInstance()->grabEvents(false);
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 )
255        PRINTF(4)("Created a %s: %s\n", created->getClassCName(), created->getCName());
256
257      //todo do this more elegant
258      if( element->Value() == "SkyBox" && created->isA(SkyBox::staticClassID()))
259      {
260        this->sky = dynamic_cast<WorldEntity*>(created);
261        State::setSkyBox(dynamic_cast<SkyBox*>(this->sky));
262      }
263      if( element->Value() == "Terrain" && created->isA(Terrain::staticClassID()))
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();
276  State::setPlayer(this->localPlayer);
277
278  if (!Playable::objectList().empty())
279  {
280    /// TODO Make this also loadable
281    this->localPlayer->setPlayable(Playable::objectList().front());
282  }
283
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);
292  this->tickLists.push_back(OM_GROUP_02);
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);
300  this->drawLists.push_back(OM_GROUP_02);
301  this->drawLists.push_back(OM_COMMON);
302
303  /* init the pnode tree */
304  PNode::getNullParent()->init();
305
306  EventHandler::getInstance()->grabEvents(mouseCaptured);
307
308  return ErrorMessage();
309}
310
311
312/**
313 *  unloads the world entities
314 */
315ErrorMessage GameWorldData::unloadWorldEntities()
316{
317  FastFactory::flushAll(true);
318  GraphicsEngine::getInstance()->displayFPS(false);
319  // erease everything that is left.
320  //secondary cleanup of PNodes;
321  while (!PNode::objectList().empty())
322    delete PNode::objectList().front();
323
324  /* remove the player object */
325  if( this->localPlayer)
326    delete this->localPlayer;
327  State::setPlayer(NULL);
328  this->localPlayer = NULL;
329  State::setCamera(NULL, NULL);
330  this->sky = NULL;
331  this->terrain = NULL;
332
333  while (!GraphicsEffect::objectList().empty())
334    delete GraphicsEffect::objectList().front();
335
336
337  while (!Element2D::objectList().empty())
338    delete Element2D::objectList().front();
339
340  // At this Point all the WorldEntites should be unloaded.
341  this->tickLists.clear();
342  this->drawLists.clear();
343
344  // unload the resources loaded in this Level !!
345  /// FIXME TODO HACK!!
346//  ResourceManager::getInstance()->unloadAllByPriority(RP_LEVEL);
347
348  if (State::getObjectManager() == this->objectManager)
349  {
350    State::setObjectManager(NULL);
351    delete this->objectManager;
352  }
353  this->objectManager = NULL;
354
355  if(State::getSkyBox())
356    State::setSkyBox(NULL);
357
358  this->glmis = NULL;
359
360  return ErrorMessage();
361}
362
363
364/**
365 * @brief loads the scene data
366 * @param root reference to the xml root element
367 */
368ErrorMessage GameWorldData::loadScene(const TiXmlElement* root)
369{
370  LoadParamXML(root, "LightManager", LightManager::getInstance(), LightManager, loadParams);
371  LoadParamXML(root, "GraphicsEngine", GraphicsEngine::getInstance(), GraphicsEngine, loadParams);
372  LoadParamXML(root, "AtmosphericEngine", AtmosphericEngine::getInstance(), AtmosphericEngine, loadParams);
373
374  LoadParam(root, "Music", this, GameWorldData, setSoundTrack);
375
376  LoadParamXML(root, "GameRule", this, GameWorldData, loadGameRule);
377
378  LoadParam(root, "clip-region", State::getCameraman(), CameraMan, setClipRegion);
379
380
381  //LoadParamXML(root, "ParticleEngine", ParticleEngine::getInstance(), ParticleEngine, loadParams);
382  //LoadParamXML(root, "PhysicsEngine", PhysicsEngine::getInstance(), PhysicsEngine, loadParams);
383
384
385  OrxSound::SoundEngine::getInstance()->setListener(State::getCamera());
386
387  return ErrorMessage();
388}
389
390
391
392/**
393 *  unloads the scene data
394 */
395ErrorMessage GameWorldData::unloadScene()
396{
397  /* delete some garphics and scene eingines */
398  delete LightManager::getInstance();
399  delete AtmosphericEngine::getInstance();
400
401  if (this->music != NULL)
402    this->setSoundTrack("");
403
404  /* unload the shaders */
405  Shader::suspendShader();
406
407  State::setGameRules(NULL);
408
409  return ErrorMessage();
410}
411
412
413void GameWorldData::setSoundTrack(const std::string& name)
414{
415  if (this->music != NULL)
416    delete this->music;
417  this->music = NULL;
418
419  if (!name.empty())
420  {
421    PRINTF(3)("Setting Sound Track to %s\n", name.c_str());
422    std::string oggFile = Resources::ResourceManager::getInstance()->prependAbsoluteMainPath(name);
423    this->music = new OrxSound::OggPlayer(oggFile);
424//     if (this->localPlayer != NULL)
425//       this->localPlayer->hud().notifyUser(std::string("Playing SoundTrack: ") + this->music->artist() + " - " + this->music->title());
426
427    //(OggPlayer*)ResourceManager::getInstance()->load(name, OGG, RP_LEVEL);
428    //assert(this->music->isA(CL_SOUND_OGG_PLAYER));
429  }
430}
431
432
433void GameWorldData::loadGameRule(const TiXmlElement* root)
434{
435  const TiXmlElement* element = root->FirstChildElement();
436  while( element != NULL)
437  {
438    PRINTF(2)("============ GameRules ==\n");
439    PRINTF(2)("creating %s\n", element->Value());
440    BaseObject* created = Factory::fabricate(element);
441    if (created != NULL && created->isA(GameRules::staticClassID()))
442    {
443      this->gameRule = dynamic_cast<GameRules*>(created);
444      State::setGameRules(this->gameRule);
445      // if there is a valid game rule loaded, return because it is not thought to load multiple game rules
446      return;
447    }
448    else
449    {
450      PRINTF(1)("Could not create a %s\n", element->Value());
451      delete created;
452    }
453    element = element->NextSiblingElement();
454  }
455}
456
457
458
Note: See TracBrowser for help on using the repository browser.