Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/story_entities/game_world.cc @ 8337

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

bsp: collision detection still not working perfectly

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