Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/water/src/story_entities/game_world.cc @ 8622

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

trunk: merged the script_engine branche back here
merged with command
svn merge https://svn.orxonox.net/orxonox/branches/script_engine . -r8284:HEAD
no conflicts

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