Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/game_world.cc @ 8328

Last change on this file since 8328 was 8312, checked in by bensch, 18 years ago

trunk: merged the water-branche back here
removed the HACK in GameWorld in the Process (hope it is not needed anymore…

merged with command:
svn merge https://svn.orxonox.net/orxonoanches/water/src/lib/gui/gl_gui src/lib/gui/gl/ -r8063:HEAD

File size: 16.4 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
141#include "account.cc"
142#include "object.cc"
143/**
144 *  loads the GameWorld by initializing all resources, and set their default values.
145 */
146ErrorMessage GameWorld::loadData()
147{
148  this->displayLoadScreen();
149
150  PRINTF(0)("Loading the GameWorld\n");
151
152  PRINTF(3)("> Loading world: '%s'\n", getLoadFile().c_str());
153  TiXmlElement* element;
154  GameLoader* loader = GameLoader::getInstance();
155
156  if( getLoadFile().empty())
157  {
158    PRINTF(1)("GameWorld has no path specified for loading\n");
159    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
160  }
161
162  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
163  // load the xml world file for further loading
164  if( !XMLDoc->LoadFile())
165  {
166    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile().c_str(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
167    delete XMLDoc;
168    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
169  }
170  // check basic validity
171  TiXmlElement* root = XMLDoc->RootElement();
172  assert( root != NULL);
173  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
174  {
175    // report an error
176    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
177    delete XMLDoc;
178    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
179  }
180  /* the whole loading process for the GameWorld */
181  this->dataTank->loadData(root);
182  this->dataXML = (TiXmlElement*)root->Clone();
183
184
185  Object* obj= new Object();
186  obj->setName("Obj");
187  Account* a = new Account();
188  a->setName("a");
189  Account *b = new Account(30);
190  b->setName("b");
191 
192  LoadParamXML(root, "ScriptManager", &this->scriptManager, ScriptManager, loadParams);
193
194  delete XMLDoc;
195  this->releaseLoadScreen();
196}
197
198
199/**
200 *  unload the data of this GameWorld
201 */
202ErrorMessage GameWorld::unloadData()
203{
204
205  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
206  this->scriptManager.flush();
207
208  delete this->shell;
209
210  this->dataTank->unloadData();
211
212  this->shell = NULL;
213  delete AnimationPlayer::getInstance();
214  delete PhysicsEngine::getInstance();
215  delete CREngine::getInstance();
216
217  State::setCurrentStoryEntity(NULL);
218  if (this->dataXML)
219    delete this->dataXML;
220}
221
222
223/**
224 *  starts the GameWorld
225 */
226bool GameWorld::start()
227{
228  this->bPaused = false;
229  this->bRunning = true;
230
231  State::setScripManager(&this->scriptManager);
232
233  this->run();
234}
235
236
237/**
238 *  stops the world.
239 */
240bool GameWorld::stop()
241{
242  PRINTF(3)("GameWorld::stop() - got stop signal\n");
243  State::setScripManager(NULL);
244  this->bRunning = false;
245}
246
247
248/**
249 *  pauses the game
250 */
251bool GameWorld::pause()
252{
253  this->bPaused = true;
254}
255
256
257/**
258 *  ends the pause Phase
259 */
260bool GameWorld::resume()
261{
262  this->bPaused = false;
263}
264
265
266/**
267 *  main loop of the world: executing all world relevant function
268 *
269 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
270 * all other member-entities of the world (tick to player, enemies etc.), checking for
271 * collisions drawing everything to the screen.
272 */
273void GameWorld::run()
274{
275  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
276
277  // initialize Timing
278  this->cycle = 0;
279  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
280    this->frameTimes[i] = 0.01f;
281  this->dtS = 0.0f;
282  this->lastFrame = Timer::getNow();
283
284  if (this->dataTank->music != NULL)
285    this->dataTank->music->play();
286
287  while( this->bRunning) /* @todo implement pause */
288  {
289    /* process intput */
290    this->handleInput ();
291    if( !this->bRunning)
292      break;
293
294    /* network synchronisation */
295    this->synchronize ();
296    /* process time */
297    this->tick ();
298    /* collision detection */
299    this->collisionDetection ();
300    /* collision reaction */
301    this->collisionReaction ();
302    /* update the state */
303    this->update ();
304    /* check the game rules */
305    this->checkGameRules();
306    /* draw everything */
307    this->display ();
308
309  }
310
311  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
312}
313
314
315void GameWorld::setPlaymode(Playable::Playmode playmode)
316{
317  if (this->dataTank->localPlayer &&
318      this->dataTank->localPlayer->getPlayable() &&
319      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
320  {
321    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode).c_str());
322  }
323  else
324  {
325    PRINTF(1)("Unable to set Playmode %d:'%s'\n", playmode, Playable::playmodeToString(playmode).c_str());
326  }
327}
328
329void GameWorld::setPlaymode(const std::string& playmode)
330{
331  this->setPlaymode(Playable::stringToPlaymode(playmode));
332}
333
334/**
335 *  synchronize local data with remote data
336*/
337void GameWorld::synchronize ()
338{}
339
340
341/**
342 *  run all input processing
343
344   the command node is the central input event dispatcher. the node uses the even-queue from
345   sdl and has its own event-passing-queue.
346*/
347void GameWorld::handleInput ()
348{
349  EventHandler::getInstance()->process();
350}
351
352
353/**
354 * @brief ticks a WorldEntity list
355 * @param entityList list of the WorldEntities
356 * @param dt time passed since last frame
357 */
358void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
359{
360  ObjectManager::EntityList::iterator entity, next;
361  next = entityList.begin();
362  while (next != entityList.end())
363  {
364    entity = next++;
365    (*entity)->tick(dt);
366  }
367}
368
369
370/**
371 *  advance the timeline
372 *
373 * this calculates the time used to process one frame (with all input handling, drawing, etc)
374 * the time is mesured in ms and passed to all world-entities and other classes that need
375 * a heart-beat.
376 */
377void GameWorld::tick ()
378{
379  if( !this->bPaused)
380  {
381    // CALCULATE FRAMERATE
382    Uint32 frameTimesIndex;
383    Uint32 i;
384    double currentFrame = Timer::getNow();
385
386    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
387    this->frameTimes[frameTimesIndex] = currentFrame - this->lastFrame;
388    this->lastFrame = currentFrame;
389    ++this->cycle;
390    this->dtS = 0.0;
391    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
392      this->dtS += this->frameTimes[i];
393    this->dtS = this->dtS / TICK_SMOOTH_VALUE * speed;
394
395    // TICK everything
396    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
397      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
398
399    /* update tick the rest */
400    this->dataTank->localCamera->tick(this->dtS);
401    AnimationPlayer::getInstance()->tick(this->dtS);
402    PhysicsEngine::getInstance()->tick(this->dtS);
403
404    GraphicsEngine::getInstance()->tick(this->dtS);
405    AtmosphericEngine::getInstance()->tick(this->dtS);
406
407    if( likely(this->dataTank->gameRule != NULL))
408      this->dataTank->gameRule->tick(this->dtS);
409     
410  }
411}
412
413
414/**
415 *  this function gives the world a consistant state
416 *
417 * after ticking (updating the world state) this will give a constistant
418 * state to the whole system.
419 */
420void GameWorld::update()
421{
422  PNode::getNullParent()->updateNode (this->dtS);
423  OrxSound::SoundEngine::getInstance()->update();
424  GraphicsEngine::getInstance()->update(this->dtS);
425}
426
427
428/**
429 * kicks the CDEngine to detect the collisions between the object groups in the world
430 */
431void GameWorld::collisionDetection()
432{
433  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
434      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
435  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
436      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
437  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
438      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
439
440  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
441      this->dataTank->objectManager->getObjectList(OM_COMMON));
442  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
443      this->dataTank->objectManager->getObjectList(OM_COMMON));
444
445}
446
447
448void GameWorld::collisionReaction()
449{
450  CREngine::getInstance()->handleCollisions();
451}
452
453
454/**
455 *  check the game rules: winning conditions, etc.
456 *
457 */
458void GameWorld::checkGameRules()
459{
460  if( this->gameRules)
461    this->gameRules->tick(this->dtS);
462}
463
464
465/**
466 *  render the current frame
467 *
468 * clear all buffers and draw the world
469 */
470void GameWorld::display ()
471{
472  // render the reflection texture
473  this->renderPassReflection();
474  // redner the refraction texture
475  this->renderPassRefraction();
476  // render all
477  this->renderPassAll();
478
479  // flip buffers
480  GraphicsEngine::swapBuffers();
481}
482
483
484/**
485 * @brief draws all entities in the list drawList
486 * @param drawList the List of entities to draw.
487 */
488void GameWorld::drawEntityList(const ObjectManager::EntityList& drawList) const
489{
490  ObjectManager::EntityList::const_iterator entity;
491  for (entity = drawList.begin(); entity != drawList.end(); entity++)
492    if ((*entity)->isVisible())
493      (*entity)->draw();
494}
495
496
497
498/**
499 * reflection rendering for water surfaces
500 */
501void GameWorld::renderPassReflection()
502{
503    // clear buffer
504  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
505  glLoadIdentity();
506
507  const std::list<BaseObject*>* reflectedWaters;
508  MappedWater* mw;
509
510  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
511  {
512    std::list<BaseObject*>::const_iterator it;
513    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
514    {
515      mw =  dynamic_cast<MappedWater*>(*it);
516
517      //camera and light
518      this->dataTank->localCamera->apply ();
519      this->dataTank->localCamera->project ();
520
521      LightManager::getInstance()->draw();
522
523
524      // prepare for reflection rendering
525      mw->activateReflection();
526
527      // draw everything to be included in the reflection
528      this->drawEntityList(State::getObjectManager()->getReflectionList());
529//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
530//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
531
532      // clean up from reflection rendering
533      mw->deactivateReflection();
534    }
535  }
536
537}
538
539
540/**
541 *  refraction rendering for water surfaces
542 */
543void GameWorld::renderPassRefraction()
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      //camera and light
560      this->dataTank->localCamera->apply ();
561      this->dataTank->localCamera->project ();
562      // prepare for reflection rendering
563      mw->activateRefraction();
564
565
566      LightManager::getInstance()->draw();
567      // draw everything to be included in the reflection
568      this->drawEntityList(State::getObjectManager()->getReflectionList());
569//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
570//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
571
572      // clean up from reflection rendering
573      mw->deactivateRefraction();
574    }
575  }
576}
577
578
579/**
580 *  this render pass renders the whole wolrd
581 */
582void GameWorld::renderPassAll()
583{
584  // clear buffer
585  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
586  GraphicsEngine* engine = GraphicsEngine::getInstance();
587
588
589  glEnable(GL_DEPTH_TEST);
590  glEnable(GL_LIGHTING);
591
592  // set camera
593  this->dataTank->localCamera->apply ();
594  this->dataTank->localCamera->project ();
595  LightManager::getInstance()->draw();
596
597  this->drawEntityList(State::getObjectManager()->getObjectList(OM_BACKGROUND));
598  engine->drawBackgroundElements();
599
600  /* draw all WorldEntiy groups */
601  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
602    this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
603
604  AtmosphericEngine::getInstance()->draw();
605 
606  if( unlikely( this->showBV))
607  {
608    CDEngine* engine = CDEngine::getInstance();
609    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
610      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]), this->showBVLevel);
611  }
612
613  if( unlikely(this->showPNodes))
614    PNode::getNullParent()->debugDraw(0);
615
616  engine->draw();
617
618  // draw the game ruls
619  if( likely(this->dataTank->gameRule != NULL))
620    this->dataTank->gameRule->draw();
621}
622
623
624/**
625 *  shows the loading screen
626 */
627void GameWorld::displayLoadScreen ()
628{
629  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
630  this->dataTank->glmis = new GLMenuImageScreen();
631  this->dataTank->glmis->setMaximum(8);
632  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
633}
634
635
636/**
637 *  removes the loadscreen, and changes over to the game
638 */
639void GameWorld::releaseLoadScreen ()
640{
641  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
642  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
643  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
644}
645
646
647
648/**
649 * @brief toggles the PNode visibility in the world (drawn as boxes)
650 */
651void GameWorld::togglePNodeVisibility()
652{
653  this->showPNodes = !this->showPNodes;
654};
655
656
657/**
658 * @brief toggles the bounding volume (BV) visibility
659*/
660void GameWorld::toggleBVVisibility(int level)
661{
662  if( level < 1)
663    this->showBV = false;
664  else
665  {
666    this->showBV = true;
667    this->showBVLevel = level;
668  }
669
670};
671
Note: See TracBrowser for help on using the repository browser.