Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merged the water back
merged with command
svn merge -r7798:HEAD https://svn.orxonox.net/orxonox/branches/water .

conflicts are all resolved

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