Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/story_entities/game_world.cc @ 9152

Last change on this file since 9152 was 8697, checked in by patrick, 18 years ago

merged the terrain with the new trunk (MARK TERRAIN)

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