Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8307 was 8271, checked in by bensch, 19 years ago

merge

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 *  this function gives the world a consistant state
415 *
416 * after ticking (updating the world state) this will give a constistant
417 * state to the whole system.
418 */
419void GameWorld::update()
420{
421  PNode::getNullParent()->updateNode (this->dtS);
422  OrxSound::SoundEngine::getInstance()->update();
423  GraphicsEngine::getInstance()->update(this->dtS);
424}
425
426
427/**
428 * kicks the CDEngine to detect the collisions between the object groups in the world
429 */
430void GameWorld::collisionDetection()
431{
432  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
433      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
434  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
435      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
436  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
437      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
438
439  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
440      this->dataTank->objectManager->getObjectList(OM_COMMON));
441  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
442      this->dataTank->objectManager->getObjectList(OM_COMMON));
443
444}
445
446
447void GameWorld::collisionReaction()
448{
449  CREngine::getInstance()->handleCollisions();
450}
451
452
453/**
454 *  check the game rules: winning conditions, etc.
455 *
456 */
457void GameWorld::checkGameRules()
458{
459  if( this->gameRules)
460    this->gameRules->tick(this->dtS);
461}
462
463
464/**
465 *  render the current frame
466 *
467 * clear all buffers and draw the world
468 */
469void GameWorld::display ()
470{
471  // render the reflection texture
472  this->renderPassReflection();
473  // redner the refraction texture
474  this->renderPassRefraction();
475  // render all
476  this->renderPassAll();
477
478  // flip buffers
479  GraphicsEngine::swapBuffers();
480}
481
482
483/**
484 * @brief draws all entities in the list drawList
485 * @param drawList the List of entities to draw.
486 */
487void GameWorld::drawEntityList(const ObjectManager::EntityList& drawList) const
488{
489  ObjectManager::EntityList::const_iterator entity;
490  for (entity = drawList.begin(); entity != drawList.end(); entity++)
491    if ((*entity)->isVisible())
492      (*entity)->draw();
493}
494
495
496
497/**
498 * reflection rendering for water surfaces
499 */
500void GameWorld::renderPassReflection()
501{
502    // clear buffer
503  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
504  glLoadIdentity();
505
506  const std::list<BaseObject*>* reflectedWaters;
507  MappedWater* mw;
508
509  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
510  {
511    std::list<BaseObject*>::const_iterator it;
512    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
513    {
514      mw =  dynamic_cast<MappedWater*>(*it);
515
516      // prepare for reflection rendering
517      mw->activateReflection();
518
519      //camera and light
520      this->dataTank->localCamera->apply ();
521      this->dataTank->localCamera->project ();
522      LightManager::getInstance()->draw();
523      // draw everything to be included in the reflection
524      this->drawEntityList(State::getObjectManager()->getReflectionList());
525//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
526//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
527
528      // clean up from reflection rendering
529      mw->deactivateReflection();
530    }
531  }
532
533}
534
535
536/**
537 *  refraction rendering for water surfaces
538 */
539void GameWorld::renderPassRefraction()
540{
541
542    // clear buffer
543  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
544  glLoadIdentity();
545
546  const std::list<BaseObject*>* reflectedWaters;
547  MappedWater* mw;
548
549  if( (reflectedWaters = ClassList::getList(CL_MAPPED_WATER)) != NULL)
550  {
551    std::list<BaseObject*>::const_iterator it;
552    for (it = reflectedWaters->begin(); it != reflectedWaters->end(); it++)
553    {
554      mw =  dynamic_cast<MappedWater*>(*it);
555
556      // prepare for reflection rendering
557      mw->activateRefraction();
558
559      //camera and light
560      this->dataTank->localCamera->apply ();
561      this->dataTank->localCamera->project ();
562      LightManager::getInstance()->draw();
563      // draw everything to be included in the reflection
564      this->drawEntityList(State::getObjectManager()->getReflectionList());
565//       for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
566//         this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
567
568      // clean up from reflection rendering
569      mw->deactivateRefraction();
570    }
571  }
572}
573
574
575/**
576 *  this render pass renders the whole wolrd
577 */
578void GameWorld::renderPassAll()
579{
580  // clear buffer
581  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
582  GraphicsEngine* engine = GraphicsEngine::getInstance();
583
584
585  //glEnable(GL_DEPTH_TEST);
586  //glEnable(GL_LIGHTING);
587
588  // set camera
589  this->dataTank->localCamera->apply ();
590  this->dataTank->localCamera->project ();
591  LightManager::getInstance()->draw();
592
593  this->drawEntityList(State::getObjectManager()->getObjectList(OM_BACKGROUND));
594  engine->drawBackgroundElements();
595
596  /* draw all WorldEntiy groups */
597  for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
598    this->drawEntityList(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]));
599
600  AtmosphericEngine::getInstance()->draw();
601 
602  if( unlikely( this->showBV))
603  {
604    CDEngine* engine = CDEngine::getInstance();
605    for (unsigned int i = 0; i < this->dataTank->drawLists.size(); ++i)
606      engine->drawBV(State::getObjectManager()->getObjectList(this->dataTank->drawLists[i]), this->showBVLevel);
607  }
608
609  if( unlikely(this->showPNodes))
610    PNode::getNullParent()->debugDraw(0);
611
612  engine->draw();
613
614  // draw the game ruls
615  if( likely(this->dataTank->gameRule != NULL))
616    this->dataTank->gameRule->draw();
617}
618
619
620/**
621 *  shows the loading screen
622 */
623void GameWorld::displayLoadScreen ()
624{
625  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
626  this->dataTank->glmis = new GLMenuImageScreen();
627  this->dataTank->glmis->setMaximum(8);
628  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
629}
630
631
632/**
633 *  removes the loadscreen, and changes over to the game
634 */
635void GameWorld::releaseLoadScreen ()
636{
637  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
638  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
639  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
640}
641
642
643
644/**
645 * @brief toggles the PNode visibility in the world (drawn as boxes)
646 */
647void GameWorld::togglePNodeVisibility()
648{
649  this->showPNodes = !this->showPNodes;
650};
651
652
653/**
654 * @brief toggles the bounding volume (BV) visibility
655*/
656void GameWorld::toggleBVVisibility(int level)
657{
658  if( level < 1)
659    this->showBV = false;
660  else
661  {
662    this->showBV = true;
663    this->showBVLevel = level;
664  }
665
666};
667
Note: See TracBrowser for help on using the repository browser.