Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/story_entities/game_world.cc @ 8688

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

new gui implementing work

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