Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/story_entities/game_world.cc @ 8616

Last change on this file since 8616 was 8490, checked in by patrick, 18 years ago

merged the bsp branche back to trunk

File size: 16.8 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
302    /* collision detection */
303    this->collisionDetection ();
304    /* collision reaction */
305    this->collisionReaction ();
306
307    /* update the state */
308    this->update ();
309
310    /* check the game rules */
311    this->checkGameRules();
312    /* draw everything */
313    this->display ();
314
315  }
316
317  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
318}
319
320
321void GameWorld::setPlaymode(Playable::Playmode playmode)
322{
323  if (this->dataTank->localPlayer &&
324      this->dataTank->localPlayer->getPlayable() &&
325      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
326  {
327    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode).c_str());
328  }
329  else
330  {
331    PRINTF(1)("Unable to set Playmode %d:'%s'\n", playmode, Playable::playmodeToString(playmode).c_str());
332  }
333}
334
335void GameWorld::setPlaymode(const std::string& playmode)
336{
337  this->setPlaymode(Playable::stringToPlaymode(playmode));
338}
339
340/**
341 *  synchronize local data with remote data
342*/
343void GameWorld::synchronize ()
344{}
345
346
347/**
348 *  run all input processing
349
350   the command node is the central input event dispatcher. the node uses the even-queue from
351   sdl and has its own event-passing-queue.
352*/
353void GameWorld::handleInput ()
354{
355  EventHandler::getInstance()->process();
356}
357
358
359/**
360 * @brief ticks a WorldEntity list
361 * @param entityList list of the WorldEntities
362 * @param dt time passed since last frame
363 */
364void GameWorld::tick(ObjectManager::EntityList entityList, float dt)
365{
366  ObjectManager::EntityList::iterator entity, next;
367  next = entityList.begin();
368  while (next != entityList.end())
369  {
370    entity = next++;
371    (*entity)->tick(dt);
372  }
373}
374
375
376/**
377 *  advance the timeline
378 *
379 * this calculates the time used to process one frame (with all input handling, drawing, etc)
380 * the time is mesured in ms and passed to all world-entities and other classes that need
381 * a heart-beat.
382 */
383void GameWorld::tick ()
384{
385  if( !this->bPaused)
386  {
387    // CALCULATE FRAMERATE
388    Uint32 frameTimesIndex;
389    Uint32 i;
390    double currentFrame = Timer::getNow();
391
392    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
393    this->frameTimes[frameTimesIndex] = currentFrame - this->lastFrame;
394    this->lastFrame = currentFrame;
395    ++this->cycle;
396    this->dtS = 0.0;
397    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
398      this->dtS += this->frameTimes[i];
399    this->dtS = this->dtS / TICK_SMOOTH_VALUE * speed;
400
401    // TICK everything
402    for (i = 0; i < this->dataTank->tickLists.size(); ++i)
403      this->tick(this->dataTank->objectManager->getObjectList(this->dataTank->tickLists[i]), this->dtS);
404
405    /* update tick the rest */
406    this->dataTank->localCamera->tick(this->dtS);
407    AnimationPlayer::getInstance()->tick(this->dtS);
408    PhysicsEngine::getInstance()->tick(this->dtS);
409
410    GraphicsEngine::getInstance()->tick(this->dtS);
411    AtmosphericEngine::getInstance()->tick(this->dtS);
412
413    if( likely(this->dataTank->gameRule != NULL))
414      this->dataTank->gameRule->tick(this->dtS);
415     
416  }
417}
418
419
420/**
421 *  this function gives the world a consistant state
422 *
423 * after ticking (updating the world state) this will give a constistant
424 * state to the whole system.
425 */
426void GameWorld::update()
427{
428  PNode::getNullParent()->updateNode (this->dtS);
429  OrxSound::SoundEngine::getInstance()->update();
430  GraphicsEngine::getInstance()->update(this->dtS);
431}
432
433
434/**
435 * kicks the CDEngine to detect the collisions between the object groups in the world
436 */
437void GameWorld::collisionDetection()
438{
439  // object-object collision detection
440  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
441      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
442  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
443      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
444  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
445      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
446
447  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
448      this->dataTank->objectManager->getObjectList(OM_COMMON));
449  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
450      this->dataTank->objectManager->getObjectList(OM_COMMON));
451
452  // ground collision detection: BSP Model
453  CDEngine::getInstance()->checkCollisionGround(this->dataTank->objectManager->getObjectList(OM_GROUP_00));
454  CDEngine::getInstance()->checkCollisionGround(this->dataTank->objectManager->getObjectList(OM_GROUP_01));
455}
456
457
458void GameWorld::collisionReaction()
459{
460  CREngine::getInstance()->handleCollisions();
461}
462
463
464/**
465 *  check the game rules: winning conditions, etc.
466 *
467 */
468void GameWorld::checkGameRules()
469{
470  if( this->gameRules)
471    this->gameRules->tick(this->dtS);
472}
473
474
475/**
476 *  render the current frame
477 *
478 * clear all buffers and draw the world
479 */
480void GameWorld::display ()
481{
482  // render the reflection texture
483  this->renderPassReflection();
484  // redner the refraction texture
485  this->renderPassRefraction();
486  // render all
487  this->renderPassAll();
488
489  // flip buffers
490  GraphicsEngine::swapBuffers();
491}
492
493
494/**
495 * @brief draws all entities in the list drawList
496 * @param drawList the List of entities to draw.
497 */
498void GameWorld::drawEntityList(const ObjectManager::EntityList& drawList) const
499{
500  ObjectManager::EntityList::const_iterator entity;
501  for (entity = drawList.begin(); entity != drawList.end(); entity++)
502    if ((*entity)->isVisible())
503      (*entity)->draw();
504}
505
506
507
508/**
509 * reflection rendering for water surfaces
510 */
511void GameWorld::renderPassReflection()
512{
513    // clear buffer
514  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
515  glLoadIdentity();
516
517  const std::list<BaseObject*>* reflectedWaters;
518  MappedWater* mw;
519
520  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
521  {
522    std::list<BaseObject*>::const_iterator it;
523    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
524    {
525      mw =  dynamic_cast<MappedWater*>(*it);
526
527      //camera and light
528      this->dataTank->localCamera->apply ();
529      this->dataTank->localCamera->project ();
530
531      LightManager::getInstance()->draw();
532
533
534      // prepare for reflection rendering
535      mw->activateReflection();
536
537      // draw everything to be included in the reflection
538      this->drawEntityList(State::getObjectManager()->getReflectionList());
539//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
540//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
541
542      // clean up from reflection rendering
543      mw->deactivateReflection();
544    }
545  }
546
547}
548
549
550/**
551 *  refraction rendering for water surfaces
552 */
553void GameWorld::renderPassRefraction()
554{
555    // clear buffer
556  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
557  glLoadIdentity();
558
559  const std::list<BaseObject*>* reflectedWaters;
560  MappedWater* mw;
561
562  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
563  {
564    std::list<BaseObject*>::const_iterator it;
565    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
566    {
567      mw =  dynamic_cast<MappedWater*>(*it);
568
569      //camera and light
570      this->dataTank->localCamera->apply ();
571      this->dataTank->localCamera->project ();
572      // prepare for reflection rendering
573      mw->activateRefraction();
574
575
576      LightManager::getInstance()->draw();
577      // draw everything to be included in the reflection
578      this->drawEntityList(State::getObjectManager()->getReflectionList());
579//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
580//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
581
582      // clean up from reflection rendering
583      mw->deactivateRefraction();
584    }
585  }
586}
587
588
589/**
590 *  this render pass renders the whole wolrd
591 */
592void GameWorld::renderPassAll()
593{
594  // clear buffer
595  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
596  GraphicsEngine* engine = GraphicsEngine::getInstance();
597
598
599  glEnable(GL_DEPTH_TEST);
600  glEnable(GL_LIGHTING);
601
602  // set camera
603  this->dataTank->localCamera->apply ();
604  this->dataTank->localCamera->project ();
605  LightManager::getInstance()->draw();
606
607  this->drawEntityList(State::getObjectManager()->getObjectList(OM_BACKGROUND));
608  engine->drawBackgroundElements();
609
610  /* draw all WorldEntiy groups */
611  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
612    this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
613
614  AtmosphericEngine::getInstance()->draw();
615
616  if( unlikely( this->showBV))
617  {
618    CDEngine* engine = CDEngine::getInstance();
619    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
620      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]), this->showBVLevel);
621  }
622
623  if( unlikely(this->showPNodes))
624    PNode::getNullParent()->debugDraw(0);
625
626  engine->draw();
627
628  // draw the game ruls
629  if( likely(this->dataTank->gameRule != NULL))
630    this->dataTank->gameRule->draw();
631}
632
633
634/**
635 *  shows the loading screen
636 */
637void GameWorld::displayLoadScreen ()
638{
639  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
640  this->dataTank->glmis = new GLMenuImageScreen();
641  this->dataTank->glmis->setMaximum(8);
642  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
643}
644
645
646/**
647 *  removes the loadscreen, and changes over to the game
648 */
649void GameWorld::releaseLoadScreen ()
650{
651  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
652  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
653  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
654}
655
656
657
658/**
659 * @brief toggles the PNode visibility in the world (drawn as boxes)
660 */
661void GameWorld::togglePNodeVisibility()
662{
663  this->showPNodes = !this->showPNodes;
664};
665
666
667/**
668 * @brief toggles the bounding volume (BV) visibility
669*/
670void GameWorld::toggleBVVisibility(int level)
671{
672  if( level < 1)
673    this->showBV = false;
674  else
675  {
676    this->showBV = true;
677    this->showBVLevel = level;
678  }
679
680};
681
Note: See TracBrowser for help on using the repository browser.