Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the gui back

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