Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/story_entities/game_world.cc @ 8391

Last change on this file since 8391 was 8391, checked in by snellen, 18 years ago

commented unessecary debug output

File size: 16.5 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   co-programmer: Christian Meyer
14   co-programmer: Benjamin Grauer
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
18
19#include "game_world.h"
20#include "game_world_data.h"
21
22#include "util/loading/resource_manager.h"
23#include "state.h"
24#include "class_list.h"
25
26#include "util/loading/game_loader.h"
27#include "util/timer.h"
28
29#include "player.h"
30#include "camera.h"
31#include "environment.h"
32#include "terrain.h"
33#include "test_entity.h"
34#include "terrain.h"
35#include "playable.h"
36#include "environments/mapped_water.h"
37
38#include "light.h"
39
40#include "util/loading/factory.h"
41#include "util/loading/load_param.h"
42#include "fast_factory.h"
43#include "shell_command.h"
44
45#include "graphics_engine.h"
46#include "effects/atmospheric_engine.h"
47#include "event_handler.h"
48#include "sound_engine.h"
49#include "cd_engine.h"
50#include "network_manager.h"
51#include "physics_engine.h"
52#include "fields.h"
53
54#include "glmenu_imagescreen.h"
55#include "shell.h"
56
57#include "ogg_player.h"
58#include "shader.h"
59
60#include "animation_player.h"
61
62#include "game_rules.h"
63
64using namespace std;
65
66
67SHELL_COMMAND(speed, GameWorld, setSpeed) ->describe("set the Speed of the Level");
68SHELL_COMMAND(playmode, GameWorld, setPlaymode)
69->describe("Set the Playmode of the current Level")
70->completionPlugin(0, OrxShell::CompletorStringArray(Playable::playmodeNames, Playable::PlaymodeCount));
71
72SHELL_COMMAND(togglePNodeVisibility, GameWorld, togglePNodeVisibility);
73SHELL_COMMAND(showBVLevel, GameWorld, toggleBVVisibility);
74
75
76
77GameWorld::GameWorld()
78    : StoryEntity()
79{
80  this->setClassID(CL_GAME_WORLD, "GameWorld");
81  this->setName("Preloaded World - no name yet");
82
83  this->gameTime = 0.0f;
84  this->setSpeed(1.0f);
85  this->shell = NULL;
86
87  this->showPNodes = false;
88  this->showBV = false;
89  this->showBVLevel = 3;
90
91  this->dataXML = NULL;
92  this->gameRules = NULL;
93}
94
95/**
96 *  remove the GameWorld from memory
97 *
98 *  delete everything explicitly, that isn't contained in the parenting tree!
99 *  things contained in the tree are deleted automaticaly
100 */
101GameWorld::~GameWorld ()
102{
103  PRINTF(4)("Deleted GameWorld\n");
104
105}
106
107
108
109/**
110 * loads the parameters of a GameWorld from an XML-element
111 * @param root the XML-element to load from
112 */
113void GameWorld::loadParams(const TiXmlElement* root)
114{
115  StoryEntity::loadParams(root);
116
117  PRINTF(4)("Loaded GameWorld specific stuff\n");
118}
119
120
121/**
122 * this is executed just before load
123 *
124 * since the load function sometimes needs data, that has been initialized
125 * before the load and after the proceeding storyentity has finished
126*/
127ErrorMessage GameWorld::init()
128{
129  /* init the world interface */
130  this->shell = new OrxShell::Shell();
131
132  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
133  this->dataTank->init();
134
135  /* initialize some engines and graphical elements */
136  AnimationPlayer::getInstance();
137  PhysicsEngine::getInstance();
138  CREngine::getInstance();
139
140  State::setScriptManager(&this->scriptManager);
141
142}
143
144#include "account.cc"
145#include "object.cc"
146/**
147 *  loads the GameWorld by initializing all resources, and set their default values.
148 */
149ErrorMessage GameWorld::loadData()
150{
151  this->displayLoadScreen();  State::setScriptManager(&this->scriptManager);
152
153
154  PRINTF(0)("Loading the GameWorld\n");
155
156  PRINTF(3)("> Loading world: '%s'\n", getLoadFile().c_str());
157  TiXmlElement* element;
158  GameLoader* loader = GameLoader::getInstance();
159
160  if( getLoadFile().empty())
161  {
162    PRINTF(1)("GameWorld has no path specified for loading\n");
163    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
164  }
165
166  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
167  // load the xml world file for further loading
168  if( !XMLDoc->LoadFile())
169  {
170    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile().c_str(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
171    delete XMLDoc;
172    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
173  }
174  // check basic validity
175  TiXmlElement* root = XMLDoc->RootElement();
176  assert( root != NULL);
177  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
178  {
179    // report an error
180    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
181    delete XMLDoc;
182    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
183  }
184  /* the whole loading process for the GameWorld */
185  this->dataTank->loadData(root);
186  this->dataXML = (TiXmlElement*)root->Clone();
187
188  //remove this after finished testing !!!!
189  Object* obj= new Object();
190  obj->setName("Obj");
191  Account* a = new Account();
192  a->setName("a");
193  Account *b = new Account(30);
194  b->setName("b");
195 
196 
197  LoadParamXML(root, "ScriptManager", &this->scriptManager, ScriptManager, loadParams);
198
199  delete XMLDoc;
200  this->releaseLoadScreen();
201}
202
203
204/**
205 *  unload the data of this GameWorld
206 */
207ErrorMessage GameWorld::unloadData()
208{
209
210  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
211  this->scriptManager.flush();
212
213  delete this->shell;
214
215  this->dataTank->unloadData();
216
217  this->shell = NULL;
218  delete AnimationPlayer::getInstance();
219  delete PhysicsEngine::getInstance();
220  delete CREngine::getInstance();
221
222  State::setCurrentStoryEntity(NULL);
223  if (this->dataXML)
224    delete this->dataXML;
225}
226
227
228/**
229 *  starts the GameWorld
230 */
231bool GameWorld::start()
232{
233  this->bPaused = false;
234  this->bRunning = true;
235  State::setScriptManager(&this->scriptManager); //make sure we have the right script manager
236  this->run();
237}
238
239
240/**
241 *  stops the world.
242 */
243bool GameWorld::stop()
244{
245  PRINTF(3)("GameWorld::stop() - got stop signal\n");
246  State::setScriptManager(NULL);
247  this->bRunning = false;
248}
249
250
251/**
252 *  pauses the game
253 */
254bool GameWorld::pause()
255{
256  this->bPaused = true;
257}
258
259
260/**
261 *  ends the pause Phase
262 */
263bool GameWorld::resume()
264{
265  this->bPaused = false;
266}
267
268
269/**
270 *  main loop of the world: executing all world relevant function
271 *
272 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
273 * all other member-entities of the world (tick to player, enemies etc.), checking for
274 * collisions drawing everything to the screen.
275 */
276void GameWorld::run()
277{
278  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
279
280  // initialize Timing
281  this->cycle = 0;
282  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
283    this->frameTimes[i] = 0.01f;
284  this->dtS = 0.0f;
285  this->lastFrame = Timer::getNow();
286
287  if (this->dataTank->music != NULL)
288    this->dataTank->music->play();
289
290  while( this->bRunning) /* @todo implement pause */
291  {
292    /* process intput */
293    this->handleInput ();
294    if( !this->bRunning)
295      break;
296
297    /* network synchronisation */
298    this->synchronize ();
299    /* process time */
300    this->tick ();
301    /* collision detection */
302    this->collisionDetection ();
303    /* collision reaction */
304    this->collisionReaction ();
305    /* update the state */
306    this->update ();
307    /* check the game rules */
308    this->checkGameRules();
309    /* draw everything */
310    this->display ();
311
312  }
313
314  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
315}
316
317
318void GameWorld::setPlaymode(Playable::Playmode playmode)
319{
320  if (this->dataTank->localPlayer &&
321      this->dataTank->localPlayer->getPlayable() &&
322      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
323  {
324    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode).c_str());
325  }
326  else
327  {
328    PRINTF(1)("Unable to set Playmode %d:'%s'\n", playmode, Playable::playmodeToString(playmode).c_str());
329  }
330}
331
332void GameWorld::setPlaymode(const std::string& playmode)
333{
334  this->setPlaymode(Playable::stringToPlaymode(playmode));
335}
336
337/**
338 *  synchronize local data with remote data
339*/
340void GameWorld::synchronize ()
341{}
342
343
344/**
345 *  run all input processing
346
347   the command node is the central input event dispatcher. the node uses the even-queue from
348   sdl and has its own event-passing-queue.
349*/
350void GameWorld::handleInput ()
351{
352  EventHandler::getInstance()->process();
353}
354
355
356/**
357 * @brief ticks a WorldEntity list
358 * @param entityList list of the WorldEntities
359 * @param dt time passed since last frame
360 */
361void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
362{
363  ObjectManager::EntityList::iterator entity, next;
364  next = entityList.begin();
365  while (next != entityList.end())
366  {
367    entity = next++;
368    (*entity)->tick(dt);
369  }
370}
371
372
373/**
374 *  advance the timeline
375 *
376 * this calculates the time used to process one frame (with all input handling, drawing, etc)
377 * the time is mesured in ms and passed to all world-entities and other classes that need
378 * a heart-beat.
379 */
380void GameWorld::tick ()
381{
382  if( !this->bPaused)
383  {
384    // CALCULATE FRAMERATE
385    Uint32 frameTimesIndex;
386    Uint32 i;
387    double currentFrame = Timer::getNow();
388
389    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
390    this->frameTimes[frameTimesIndex] = currentFrame - this->lastFrame;
391    this->lastFrame = currentFrame;
392    ++this->cycle;
393    this->dtS = 0.0;
394    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
395      this->dtS += this->frameTimes[i];
396    this->dtS = this->dtS / TICK_SMOOTH_VALUE * speed;
397
398    // TICK everything
399    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
400      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
401
402    /* update tick the rest */
403    this->dataTank->localCamera->tick(this->dtS);
404    AnimationPlayer::getInstance()->tick(this->dtS);
405    PhysicsEngine::getInstance()->tick(this->dtS);
406
407    GraphicsEngine::getInstance()->tick(this->dtS);
408    AtmosphericEngine::getInstance()->tick(this->dtS);
409
410    if( likely(this->dataTank->gameRule != NULL))
411      this->dataTank->gameRule->tick(this->dtS);
412  }
413}
414
415
416/**
417 *  this function gives the world a consistant state
418 *
419 * after ticking (updating the world state) this will give a constistant
420 * state to the whole system.
421 */
422void GameWorld::update()
423{
424  PNode::getNullParent()->updateNode (this->dtS);
425  OrxSound::SoundEngine::getInstance()->update();
426  GraphicsEngine::getInstance()->update(this->dtS);
427}
428
429
430/**
431 * kicks the CDEngine to detect the collisions between the object groups in the world
432 */
433void GameWorld::collisionDetection()
434{
435  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
436      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
437  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
438      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
439  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
440      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
441
442  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
443      this->dataTank->objectManager->getObjectList(OM_COMMON));
444  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
445      this->dataTank->objectManager->getObjectList(OM_COMMON));
446
447}
448
449
450void GameWorld::collisionReaction()
451{
452  CREngine::getInstance()->handleCollisions();
453}
454
455
456/**
457 *  check the game rules: winning conditions, etc.
458 *
459 */
460void GameWorld::checkGameRules()
461{
462  if( this->gameRules)
463    this->gameRules->tick(this->dtS);
464}
465
466
467/**
468 *  render the current frame
469 *
470 * clear all buffers and draw the world
471 */
472void GameWorld::display ()
473{
474  // render the reflection texture
475  this->renderPassReflection();
476  // redner the refraction texture
477  this->renderPassRefraction();
478  // render all
479  this->renderPassAll();
480
481  // flip buffers
482  GraphicsEngine::swapBuffers();
483}
484
485
486/**
487 * @brief draws all entities in the list drawList
488 * @param drawList the List of entities to draw.
489 */
490void GameWorld::drawEntityList(const ObjectManager::EntityList& drawList) const
491{
492  ObjectManager::EntityList::const_iterator entity;
493  for (entity = drawList.begin(); entity != drawList.end(); entity++)
494    if ((*entity)->isVisible())
495      (*entity)->draw();
496}
497
498
499
500/**
501 * reflection rendering for water surfaces
502 */
503void GameWorld::renderPassReflection()
504{
505    // clear buffer
506  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
507  glLoadIdentity();
508
509  const std::list<BaseObject*>* reflectedWaters;
510  MappedWater* mw;
511
512  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
513  {
514    std::list<BaseObject*>::const_iterator it;
515    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
516    {
517      mw =  dynamic_cast<MappedWater*>(*it);
518
519      // prepare for reflection rendering
520      mw->activateReflection();
521
522      //camera and light
523      this->dataTank->localCamera->apply ();
524      this->dataTank->localCamera->project ();
525      LightManager::getInstance()->draw();
526      // draw everything to be included in the reflection
527      this->drawEntityList(State::getObjectManager()->getReflectionList());
528//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
529//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
530
531      // clean up from reflection rendering
532      mw->deactivateReflection();
533    }
534  }
535
536}
537
538
539/**
540 *  refraction rendering for water surfaces
541 */
542void GameWorld::renderPassRefraction()
543{
544
545    // clear buffer
546  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
547  glLoadIdentity();
548
549  const std::list<BaseObject*>* reflectedWaters;
550  MappedWater* mw;
551
552  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
553  {
554    std::list<BaseObject*>::const_iterator it;
555    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
556    {
557      mw =  dynamic_cast<MappedWater*>(*it);
558
559      // prepare for reflection rendering
560      mw->activateRefraction();
561
562      //camera and light
563      this->dataTank->localCamera->apply ();
564      this->dataTank->localCamera->project ();
565      LightManager::getInstance()->draw();
566      // draw everything to be included in the reflection
567      this->drawEntityList(State::getObjectManager()->getReflectionList());
568//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
569//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
570
571      // clean up from reflection rendering
572      mw->deactivateRefraction();
573    }
574  }
575}
576
577
578/**
579 *  this render pass renders the whole wolrd
580 */
581void GameWorld::renderPassAll()
582{
583  // clear buffer
584  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
585  GraphicsEngine* engine = GraphicsEngine::getInstance();
586
587
588  //glEnable(GL_DEPTH_TEST);
589  //glEnable(GL_LIGHTING);
590
591  // set camera
592  this->dataTank->localCamera->apply ();
593  this->dataTank->localCamera->project ();
594  LightManager::getInstance()->draw();
595
596  this->drawEntityList(State::getObjectManager()->getObjectList(OM_BACKGROUND));
597  engine->drawBackgroundElements();
598
599  /* draw all WorldEntiy groups */
600  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
601    this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
602
603  AtmosphericEngine::getInstance()->draw();
604
605  if( unlikely( this->showBV))
606  {
607    CDEngine* engine = CDEngine::getInstance();
608    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
609      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]), this->showBVLevel);
610  }
611
612  if( unlikely(this->showPNodes))
613    PNode::getNullParent()->debugDraw(0);
614
615  engine->draw();
616
617  // draw the game ruls
618  if( likely(this->dataTank->gameRule != NULL))
619    this->dataTank->gameRule->draw();
620}
621
622
623/**
624 *  shows the loading screen
625 */
626void GameWorld::displayLoadScreen ()
627{
628  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
629  this->dataTank->glmis = new GLMenuImageScreen();
630  this->dataTank->glmis->setMaximum(8);
631  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
632}
633
634
635/**
636 *  removes the loadscreen, and changes over to the game
637 */
638void GameWorld::releaseLoadScreen ()
639{
640  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
641  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
642  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
643}
644
645
646
647/**
648 * @brief toggles the PNode visibility in the world (drawn as boxes)
649 */
650void GameWorld::togglePNodeVisibility()
651{
652  this->showPNodes = !this->showPNodes;
653};
654
655
656/**
657 * @brief toggles the bounding volume (BV) visibility
658*/
659void GameWorld::toggleBVVisibility(int level)
660{
661  if( level < 1)
662    this->showBV = false;
663  else
664  {
665    this->showBV = true;
666    this->showBVLevel = level;
667  }
668
669};
670
Note: See TracBrowser for help on using the repository browser.