Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some nice fixes

File size: 13.7 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"
25#include "substring.h"
[6352]26
[7193]27#include "util/loading/game_loader.h"
[6404]28
[7287]29#include "p_node.h"
30#include "world_entity.h"
[6352]31#include "player.h"
32#include "camera.h"
33#include "environment.h"
34#include "terrain.h"
[7287]35#include "test_entity.h"
36#include "terrain.h"
[6404]37#include "md2Model.h"
[7287]38#include "world_entities/projectiles/projectile.h"
39#include "npcs/npc_test1.h"
[6404]40#include "playable.h"
41
[6352]42#include "light.h"
[6404]43
[7193]44#include "util/loading/factory.h"
[6404]45#include "fast_factory.h"
[7193]46#include "util/loading/load_param.h"
[6404]47#include "shell_command.h"
[6352]48
49#include "graphics_engine.h"
[6404]50#include "event_handler.h"
51#include "sound_engine.h"
52#include "cd_engine.h"
53#include "network_manager.h"
[6352]54#include "physics_engine.h"
[7287]55#include "fields.h"
[6352]56
57#include "glmenu_imagescreen.h"
[6404]58#include "shell.h"
[6352]59
[6404]60#include "animation_player.h"
[6352]61#include "animation3d.h"
62
63#include "ogg_player.h"
[6404]64#include "shader.h"
[6352]65
[7035]66#include "game_rules.h"
[6352]67
[6404]68using namespace std;
[6352]69
70
[6358]71SHELL_COMMAND(speed, GameWorld, setSpeed);
[7339]72SHELL_COMMAND(playmode, GameWorld, setPlaymode);
[7004]73SHELL_COMMAND_STATIC(togglePNodeVisibility, GameWorld, GameWorld::togglePNodeVisibility);
74SHELL_COMMAND_STATIC(toggleBVVisibility, GameWorld, GameWorld::toggleBVVisibility);
[6352]75
76
77
[6989]78GameWorld::GameWorld()
[6402]79    : StoryEntity()
[6352]80{
[6368]81  this->setClassID(CL_GAME_WORLD, "GameWorld");
82  this->setName("Preloaded World - no name yet");
83
84  this->gameTime = 0.0f;
[6370]85  this->setSpeed(1.0f);
[6368]86  this->shell = NULL;
87
88  this->showPNodes = false;
89  this->showBV = false;
90
[6845]91  this->dataXML = NULL;
[6352]92}
93
94/**
[6358]95 *  remove the GameWorld from memory
[6352]96 *
97 *  delete everything explicitly, that isn't contained in the parenting tree!
98 *  things contained in the tree are deleted automaticaly
99 */
[6358]100GameWorld::~GameWorld ()
[6352]101{
[6408]102  PRINTF(4)("Deleted GameWorld\n");
[7283]103
[6352]104}
105
106
107
108/**
[6358]109 * loads the parameters of a GameWorld from an XML-element
[6352]110 * @param root the XML-element to load from
111 */
[6358]112void GameWorld::loadParams(const TiXmlElement* root)
[6352]113{
[6512]114  StoryEntity::loadParams(root);
[6352]115
[6376]116  PRINTF(4)("Loaded GameWorld specific stuff\n");
[6352]117}
118
[6368]119
[6352]120/**
121 * this is executed just before load
122 *
123 * since the load function sometimes needs data, that has been initialized
124 * before the load and after the proceeding storyentity has finished
125*/
[6370]126ErrorMessage GameWorld::init()
[6352]127{
128  /* init the world interface */
129  this->shell = new Shell();
130
[6498]131  State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this));
[6407]132  this->dataTank->init();
[6352]133}
134
[7288]135
[6352]136/**
[6358]137 *  loads the GameWorld by initializing all resources, and set their default values.
[6352]138 */
[6372]139ErrorMessage GameWorld::loadData()
[6352]140{
[6370]141  this->displayLoadScreen();
142
[6407]143  PRINTF(0)("Loading the GameWorld\n");
144
[6992]145  PRINTF(3)("> Loading world: '%s'\n", getLoadFile());
[6352]146  TiXmlElement* element;
147  GameLoader* loader = GameLoader::getInstance();
148
[7221]149  if( getLoadFile().empty())
[6402]150  {
[6634]151    PRINTF(1)("GameWorld has no path specified for loading\n");
[6402]152    return (ErrorMessage){213,"Path not specified","GameWorld::load()"};
153  }
[6352]154
[7287]155  TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile());
[6402]156  // load the xml world file for further loading
[7287]157  if( !XMLDoc->LoadFile())
[6352]158  {
[7287]159    PRINTF(1)("loading XML File: %s @ %s:l%d:c%d\n", XMLDoc->ErrorDesc(), this->getLoadFile().c_str(), XMLDoc->ErrorRow(), XMLDoc->ErrorCol());
160    delete XMLDoc;
[6358]161    return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"};
[6352]162  }
163  // check basic validity
[7287]164  TiXmlElement* root = XMLDoc->RootElement();
[6352]165  assert( root != NULL);
166  if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile"))
167  {
[6402]168    // report an error
169    PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n");
[7287]170    delete XMLDoc;
[6402]171    return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"};
[6352]172  }
[6402]173  /* the whole loading process for the GameWorld */
[6407]174  this->dataTank->loadData(root);
[6845]175  this->dataXML = (TiXmlElement*)root->Clone();
[6352]176
[7287]177  delete XMLDoc;
[6386]178  this->releaseLoadScreen();
[6352]179}
180
181
182/**
[6387]183 *  unload the data of this GameWorld
184 */
185ErrorMessage GameWorld::unloadData()
186{
187  delete this->shell;
[7287]188  PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n");
[6387]189
[6981]190  this->dataTank->unloadData();
191
[6988]192  State::setCurrentStoryEntity(NULL);
[6845]193  if (this->dataXML)
194    delete this->dataXML;
[6387]195}
196
197
198/**
[6358]199 *  starts the GameWorld
[6352]200 */
[6387]201bool GameWorld::start()
[6352]202{
[7283]203  this->bPaused = false;
204  this->bRunning = true;
[6387]205
206  this->run();
[6352]207}
208
[6402]209
[6352]210/**
211 *  stops the world.
[6402]212 */
[6387]213bool GameWorld::stop()
[6352]214{
[6358]215  PRINTF(3)("GameWorld::stop() - got stop signal\n");
[7283]216  this->bRunning = false;
[6352]217}
218
[6402]219
[6352]220/**
[6402]221 *  pauses the game
222 */
[6387]223bool GameWorld::pause()
[6352]224{
[7283]225  this->bPaused = true;
[6352]226}
227
[6402]228
[6352]229/**
230 *  ends the pause Phase
[6409]231 */
[6387]232bool GameWorld::resume()
[6352]233{
[7283]234  this->bPaused = false;
[6352]235}
236
237
238/**
[6402]239 *  main loop of the world: executing all world relevant function
240 *
241 * in this loop we synchronize (if networked), handle input events, give the heart-beat to
242 * all other member-entities of the world (tick to player, enemies etc.), checking for
243 * collisions drawing everything to the screen.
244 */
245void GameWorld::run()
[6352]246{
[6402]247  PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n");
[6352]248
[7131]249  // initialize Timing
250  this->cycle = 0;
251  for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++)
[7132]252    this->frameTimes[i] = 100;
[7131]253  this->dtS = 0.0f;
254  this->lastFrame = SDL_GetTicks ();
255
[7304]256  if (this->dataTank->music != NULL)
257    this->dataTank->music->play();
258
[7283]259  while( this->bRunning) /* @todo implement pause */
[6402]260  {
261    /* process intput */
262    this->handleInput ();
[7322]263    if( !this->bRunning)
264      break;
[6386]265
[6402]266    /* network synchronisation */
267    this->synchronize ();
268    /* process time */
269    this->tick ();
270    /* process collision */
271    this->collide ();
272    /* update the state */
273    this->update ();
274    /* draw everything */
275    this->display ();
[7306]276
[6402]277  }
278
[7330]279  PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n");
[6352]280}
281
282
[7338]283void GameWorld::setPlaymode(Playable::Playmode playmode)
284{
285  if (this->dataTank->localPlayer &&
286      this->dataTank->localPlayer->getPlayable() &&
[7339]287      this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode))
[7338]288  {
[7339]289    PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode));
[7338]290  }
[7339]291  else
292  {
293    PRINTF(1)("Unable to set Playmode %d:%s\n", playmode, Playable::playmodeToString(playmode));
294  }
[7338]295}
296
[7339]297void GameWorld::setPlaymode(const std::string& playmode)
298{
299  this->setPlaymode(Playable::stringToPlaymode(playmode));
300}
[7338]301
[6352]302/**
303 *  synchronize local data with remote data
304*/
[6358]305void GameWorld::synchronize ()
[6402]306{}
[6352]307
308
309/**
310 *  run all input processing
311
312   the command node is the central input event dispatcher. the node uses the even-queue from
313   sdl and has its own event-passing-queue.
314*/
[6358]315void GameWorld::handleInput ()
[6352]316{
317  EventHandler::getInstance()->process();
318}
319
[6402]320
321/**
322 *  ticks a WorldEntity list
323 * @param entityList list of the WorldEntities
324 * @param dt time passed since last frame
325 */
[6358]326void GameWorld::tick(std::list<WorldEntity*> entityList, float dt)
[6352]327{
[6710]328  std::list<WorldEntity*>::iterator entity, next;
329  next = entityList.begin();
330  while (next != entityList.end())
331  {
332    entity = next++;
[6352]333    (*entity)->tick(dt);
[6710]334  }
[6352]335}
336
[7131]337
[6352]338/**
339 *  advance the timeline
[6402]340 *
341 * this calculates the time used to process one frame (with all input handling, drawing, etc)
342 * the time is mesured in ms and passed to all world-entities and other classes that need
343 * a heart-beat.
344 */
[6358]345void GameWorld::tick ()
[6352]346{
347  Uint32 currentFrame = SDL_GetTicks();
[6402]348
[7283]349  if( !this->bPaused)
[6402]350  {
[7131]351    // CALCULATE FRAMERATE
352    Uint32 frameTimesIndex;
353    Uint32 getTicks;
354    Uint32 i;
[6402]355
[7131]356    frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE;
357    getTicks = SDL_GetTicks();
358    this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame;
359    this->lastFrame = getTicks;
[7132]360    ++this->cycle;
[7131]361    this->dtS = 0;
[7132]362    for (i = 0; i < TICK_SMOOTH_VALUE; i++)
[7131]363      this->dtS += this->frameTimes[i];
[7132]364    this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed;
[6352]365
[7131]366    // TICK everything
[6407]367    this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS);
[6457]368    this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS);
[6407]369    this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS);
370    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS);
[6433]371    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS);
[6407]372    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01), this->dtS);
373    this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ), this->dtS);
[6352]374
[6402]375    /* update tick the rest */
[6407]376    this->dataTank->localCamera->tick(this->dtS);
[6402]377    AnimationPlayer::getInstance()->tick(this->dtS);
378    PhysicsEngine::getInstance()->tick(this->dtS);
[6352]379
[6402]380    GraphicsEngine::getInstance()->tick(this->dtS);
[7035]381
382    if( likely(this->dataTank->gameRule != NULL))
383      this->dataTank->gameRule->tick(this->dtS);
[6402]384  }
[6352]385  this->lastFrame = currentFrame;
386}
387
388
389/**
390 *  this function gives the world a consistant state
[6402]391 *
392 * after ticking (updating the world state) this will give a constistant
393 * state to the whole system.
394 */
[6358]395void GameWorld::update()
[6352]396{
397  GraphicsEngine::getInstance()->update(this->dtS);
398  PNode::getNullParent()->updateNode (this->dtS);
399  SoundEngine::getInstance()->update();
400}
401
402
[6402]403/**
404 * kicks the CDEngine to detect the collisions between the object groups in the world
405 */
[6358]406void GameWorld::collide()
[6352]407{
[6407]408  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7004]409      this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ));
[6407]410  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[7004]411      this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ));
[7078]412  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
[7288]413      this->dataTank->objectManager->getObjectList(OM_GROUP_01));
[6433]414
[7083]415  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00),
416      this->dataTank->objectManager->getObjectList(OM_COMMON));
[6433]417  CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01),
[6407]418      this->dataTank->objectManager->getObjectList(OM_COMMON));
[7083]419
[6352]420}
421
422/**
423 *  render the current frame
[6402]424 *
425 * clear all buffers and draw the world
426 */
[6358]427void GameWorld::display ()
[6352]428{
429  // clear buffer
430  glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
431  // draw world
432  this->draw();
433  // flip buffers
434  GraphicsEngine::swapBuffers();
435}
436
437
438/**
439 *  runs through all entities calling their draw() methods
440 */
[6358]441void GameWorld::draw ()
[6352]442{
443  GraphicsEngine* engine = GraphicsEngine::getInstance();
[6402]444
[6780]445  // set camera
446  this->dataTank->localCamera->apply ();
[7108]447  this->dataTank->localCamera->project ();
448  LightManager::getInstance()->draw();
[6416]449
450  /* draw all WorldEntiy groups */
451  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
452  engine->draw(State::getObjectManager()->getObjectList(OM_ENVIRON));
453  engine->draw(State::getObjectManager()->getObjectList(OM_COMMON));
454  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00));
[6433]455  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ));
[6416]456  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01));
457  engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
458
[6780]459
[6402]460  if( unlikely( this->showBV))
461  {
462    CDEngine* engine = CDEngine::getInstance();
463    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON_NOTICK));
464    engine->drawBV(State::getObjectManager()->getObjectList(OM_ENVIRON));
465    engine->drawBV(State::getObjectManager()->getObjectList(OM_COMMON));
466    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_00));
467    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01));
468    engine->drawBV(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ));
469  }
[6416]470
[6402]471  if( unlikely(this->showPNodes))
472    PNode::getNullParent()->debugDraw(0);
473
[6780]474  engine->draw();
[7035]475
[7108]476
[7035]477  // draw the game ruls
478  if( likely(this->dataTank->gameRule != NULL))
479    this->dataTank->gameRule->draw();
[6352]480}
481
482/**
[6402]483 *  shows the loading screen
484 */
485void GameWorld::displayLoadScreen ()
[6387]486{
[6402]487  PRINTF(3)("GameWorld::displayLoadScreen - start\n");
[6407]488  this->dataTank->glmis = new GLMenuImageScreen();
489  this->dataTank->glmis->setMaximum(8);
[6402]490  PRINTF(3)("GameWorld::displayLoadScreen - end\n");
[6387]491}
492
493
[6402]494/**
495 *  removes the loadscreen, and changes over to the game
496 */
497void GameWorld::releaseLoadScreen ()
[6387]498{
[6402]499  PRINTF(3)("GameWorld::releaseLoadScreen - start\n");
[6407]500  this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum());
[6402]501  PRINTF(3)("GameWorld::releaseLoadScreen - end\n");
[6387]502}
503
[7004]504
505
506/**
507 * @brief toggles the PNode visibility in the world (drawn as boxes)
508 */
509void GameWorld::togglePNodeVisibility()
510{
511  if (State::getCurrentStoryEntity() != NULL &&
512      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
513  {
514    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showPNodes;
515  }
516  else
517    PRINTF(2)("The Current Story entity is not a GameWorld\n");
518};
519
520
521/**
522 * @brief toggles the bounding volume (BV) visibility
523*/
524void GameWorld::toggleBVVisibility()
525{
526  if (State::getCurrentStoryEntity() != NULL &&
527      State::getCurrentStoryEntity()->isA(CL_GAME_WORLD))
528  {
529    dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV = !dynamic_cast<GameWorld*>(State::getCurrentStoryEntity())->showBV;
530  }
531  else
532    PRINTF(2)("The Current Story entity is not a GameWorld\n");
533};
534
Note: See TracBrowser for help on using the repository browser.