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 | #include "substring.h" |
---|
26 | |
---|
27 | #include "util/loading/game_loader.h" |
---|
28 | |
---|
29 | #include "p_node.h" |
---|
30 | #include "world_entity.h" |
---|
31 | #include "player.h" |
---|
32 | #include "camera.h" |
---|
33 | #include "environment.h" |
---|
34 | #include "terrain.h" |
---|
35 | #include "test_entity.h" |
---|
36 | #include "terrain.h" |
---|
37 | #include "md2Model.h" |
---|
38 | #include "world_entities/projectiles/projectile.h" |
---|
39 | #include "npcs/npc_test1.h" |
---|
40 | #include "playable.h" |
---|
41 | |
---|
42 | #include "light.h" |
---|
43 | |
---|
44 | #include "util/loading/factory.h" |
---|
45 | #include "fast_factory.h" |
---|
46 | #include "util/loading/load_param.h" |
---|
47 | #include "shell_command.h" |
---|
48 | |
---|
49 | #include "graphics_engine.h" |
---|
50 | #include "event_handler.h" |
---|
51 | #include "sound_engine.h" |
---|
52 | #include "cd_engine.h" |
---|
53 | #include "network_manager.h" |
---|
54 | #include "physics_engine.h" |
---|
55 | #include "fields.h" |
---|
56 | |
---|
57 | #include "glmenu_imagescreen.h" |
---|
58 | #include "shell.h" |
---|
59 | |
---|
60 | #include "animation_player.h" |
---|
61 | #include "animation3d.h" |
---|
62 | |
---|
63 | #include "ogg_player.h" |
---|
64 | #include "shader.h" |
---|
65 | |
---|
66 | #include "game_rules.h" |
---|
67 | |
---|
68 | using namespace std; |
---|
69 | |
---|
70 | |
---|
71 | SHELL_COMMAND(speed, GameWorld, setSpeed); |
---|
72 | SHELL_COMMAND(playmode, GameWorld, setPlaymode); |
---|
73 | SHELL_COMMAND_STATIC(togglePNodeVisibility, GameWorld, GameWorld::togglePNodeVisibility); |
---|
74 | SHELL_COMMAND_STATIC(toggleBVVisibility, GameWorld, GameWorld::toggleBVVisibility); |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | GameWorld::GameWorld() |
---|
79 | : StoryEntity() |
---|
80 | { |
---|
81 | this->setClassID(CL_GAME_WORLD, "GameWorld"); |
---|
82 | this->setName("Preloaded World - no name yet"); |
---|
83 | |
---|
84 | this->gameTime = 0.0f; |
---|
85 | this->setSpeed(1.0f); |
---|
86 | this->shell = NULL; |
---|
87 | |
---|
88 | this->showPNodes = false; |
---|
89 | this->showBV = false; |
---|
90 | |
---|
91 | this->dataXML = NULL; |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * remove the GameWorld from memory |
---|
96 | * |
---|
97 | * delete everything explicitly, that isn't contained in the parenting tree! |
---|
98 | * things contained in the tree are deleted automaticaly |
---|
99 | */ |
---|
100 | GameWorld::~GameWorld () |
---|
101 | { |
---|
102 | PRINTF(4)("Deleted GameWorld\n"); |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | |
---|
108 | /** |
---|
109 | * loads the parameters of a GameWorld from an XML-element |
---|
110 | * @param root the XML-element to load from |
---|
111 | */ |
---|
112 | void GameWorld::loadParams(const TiXmlElement* root) |
---|
113 | { |
---|
114 | StoryEntity::loadParams(root); |
---|
115 | |
---|
116 | PRINTF(4)("Loaded GameWorld specific stuff\n"); |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
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 | */ |
---|
126 | ErrorMessage GameWorld::init() |
---|
127 | { |
---|
128 | /* init the world interface */ |
---|
129 | this->shell = new Shell(); |
---|
130 | |
---|
131 | State::setCurrentStoryEntity(dynamic_cast<StoryEntity*>(this)); |
---|
132 | this->dataTank->init(); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /** |
---|
137 | * loads the GameWorld by initializing all resources, and set their default values. |
---|
138 | */ |
---|
139 | ErrorMessage GameWorld::loadData() |
---|
140 | { |
---|
141 | this->displayLoadScreen(); |
---|
142 | |
---|
143 | PRINTF(0)("Loading the GameWorld\n"); |
---|
144 | |
---|
145 | PRINTF(3)("> Loading world: '%s'\n", getLoadFile()); |
---|
146 | TiXmlElement* element; |
---|
147 | GameLoader* loader = GameLoader::getInstance(); |
---|
148 | |
---|
149 | if( getLoadFile().empty()) |
---|
150 | { |
---|
151 | PRINTF(1)("GameWorld has no path specified for loading\n"); |
---|
152 | return (ErrorMessage){213,"Path not specified","GameWorld::load()"}; |
---|
153 | } |
---|
154 | |
---|
155 | TiXmlDocument* XMLDoc = new TiXmlDocument( getLoadFile()); |
---|
156 | // load the xml world file for further loading |
---|
157 | if( !XMLDoc->LoadFile()) |
---|
158 | { |
---|
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; |
---|
161 | return (ErrorMessage){213,"XML File parsing error","GameWorld::load()"}; |
---|
162 | } |
---|
163 | // check basic validity |
---|
164 | TiXmlElement* root = XMLDoc->RootElement(); |
---|
165 | assert( root != NULL); |
---|
166 | if( root == NULL || root->Value() == NULL || strcmp( root->Value(), "WorldDataFile")) |
---|
167 | { |
---|
168 | // report an error |
---|
169 | PRINTF(1)("Specified XML File is not an orxonox world data file (WorldDataFile element missing)\n"); |
---|
170 | delete XMLDoc; |
---|
171 | return (ErrorMessage){213,"Path not a WorldDataFile","GameWorld::load()"}; |
---|
172 | } |
---|
173 | /* the whole loading process for the GameWorld */ |
---|
174 | this->dataTank->loadData(root); |
---|
175 | this->dataXML = (TiXmlElement*)root->Clone(); |
---|
176 | |
---|
177 | delete XMLDoc; |
---|
178 | this->releaseLoadScreen(); |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | /** |
---|
183 | * unload the data of this GameWorld |
---|
184 | */ |
---|
185 | ErrorMessage GameWorld::unloadData() |
---|
186 | { |
---|
187 | delete this->shell; |
---|
188 | PRINTF(3)("GameWorld::~GameWorld() - unloading the current GameWorld\n"); |
---|
189 | |
---|
190 | this->dataTank->unloadData(); |
---|
191 | |
---|
192 | State::setCurrentStoryEntity(NULL); |
---|
193 | if (this->dataXML) |
---|
194 | delete this->dataXML; |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | /** |
---|
199 | * starts the GameWorld |
---|
200 | */ |
---|
201 | bool GameWorld::start() |
---|
202 | { |
---|
203 | this->bPaused = false; |
---|
204 | this->bRunning = true; |
---|
205 | |
---|
206 | this->run(); |
---|
207 | } |
---|
208 | |
---|
209 | |
---|
210 | /** |
---|
211 | * stops the world. |
---|
212 | */ |
---|
213 | bool GameWorld::stop() |
---|
214 | { |
---|
215 | PRINTF(3)("GameWorld::stop() - got stop signal\n"); |
---|
216 | this->bRunning = false; |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | /** |
---|
221 | * pauses the game |
---|
222 | */ |
---|
223 | bool GameWorld::pause() |
---|
224 | { |
---|
225 | this->bPaused = true; |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | /** |
---|
230 | * ends the pause Phase |
---|
231 | */ |
---|
232 | bool GameWorld::resume() |
---|
233 | { |
---|
234 | this->bPaused = false; |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | /** |
---|
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 | */ |
---|
245 | void GameWorld::run() |
---|
246 | { |
---|
247 | PRINTF(3)("GameWorld::mainLoop() - Entering main loop\n"); |
---|
248 | |
---|
249 | // initialize Timing |
---|
250 | this->cycle = 0; |
---|
251 | for (unsigned int i = 0; i < TICK_SMOOTH_VALUE; i++) |
---|
252 | this->frameTimes[i] = 100; |
---|
253 | this->dtS = 0.0f; |
---|
254 | this->lastFrame = SDL_GetTicks (); |
---|
255 | |
---|
256 | if (this->dataTank->music != NULL) |
---|
257 | this->dataTank->music->play(); |
---|
258 | |
---|
259 | while( this->bRunning) /* @todo implement pause */ |
---|
260 | { |
---|
261 | /* process intput */ |
---|
262 | this->handleInput (); |
---|
263 | if( !this->bRunning) |
---|
264 | break; |
---|
265 | |
---|
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 (); |
---|
276 | |
---|
277 | } |
---|
278 | |
---|
279 | PRINTF(0)("GameWorld::mainLoop() - Exiting the main loop\n"); |
---|
280 | } |
---|
281 | |
---|
282 | |
---|
283 | void GameWorld::setPlaymode(Playable::Playmode playmode) |
---|
284 | { |
---|
285 | if (this->dataTank->localPlayer && |
---|
286 | this->dataTank->localPlayer->getPlayable() && |
---|
287 | this->dataTank->localPlayer->getPlayable()->setPlaymode(playmode)) |
---|
288 | { |
---|
289 | PRINTF(3)("Set Playmode to %d:%s\n", playmode, Playable::playmodeToString(playmode)); |
---|
290 | } |
---|
291 | else |
---|
292 | { |
---|
293 | PRINTF(1)("Unable to set Playmode %d:%s\n", playmode, Playable::playmodeToString(playmode)); |
---|
294 | } |
---|
295 | } |
---|
296 | |
---|
297 | void GameWorld::setPlaymode(const std::string& playmode) |
---|
298 | { |
---|
299 | this->setPlaymode(Playable::stringToPlaymode(playmode)); |
---|
300 | } |
---|
301 | |
---|
302 | /** |
---|
303 | * synchronize local data with remote data |
---|
304 | */ |
---|
305 | void GameWorld::synchronize () |
---|
306 | {} |
---|
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 | */ |
---|
315 | void GameWorld::handleInput () |
---|
316 | { |
---|
317 | EventHandler::getInstance()->process(); |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | /** |
---|
322 | * ticks a WorldEntity list |
---|
323 | * @param entityList list of the WorldEntities |
---|
324 | * @param dt time passed since last frame |
---|
325 | */ |
---|
326 | void GameWorld::tick(std::list<WorldEntity*> entityList, float dt) |
---|
327 | { |
---|
328 | std::list<WorldEntity*>::iterator entity, next; |
---|
329 | next = entityList.begin(); |
---|
330 | while (next != entityList.end()) |
---|
331 | { |
---|
332 | entity = next++; |
---|
333 | (*entity)->tick(dt); |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | |
---|
338 | /** |
---|
339 | * advance the timeline |
---|
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 | */ |
---|
345 | void GameWorld::tick () |
---|
346 | { |
---|
347 | Uint32 currentFrame = SDL_GetTicks(); |
---|
348 | |
---|
349 | if( !this->bPaused) |
---|
350 | { |
---|
351 | // CALCULATE FRAMERATE |
---|
352 | Uint32 frameTimesIndex; |
---|
353 | Uint32 getTicks; |
---|
354 | Uint32 i; |
---|
355 | |
---|
356 | frameTimesIndex = this->cycle % TICK_SMOOTH_VALUE; |
---|
357 | getTicks = SDL_GetTicks(); |
---|
358 | this->frameTimes[frameTimesIndex] = getTicks - this->lastFrame; |
---|
359 | this->lastFrame = getTicks; |
---|
360 | ++this->cycle; |
---|
361 | this->dtS = 0; |
---|
362 | for (i = 0; i < TICK_SMOOTH_VALUE; i++) |
---|
363 | this->dtS += this->frameTimes[i]; |
---|
364 | this->dtS = this->dtS / TICK_SMOOTH_VALUE / 1000.0f * speed; |
---|
365 | |
---|
366 | // TICK everything |
---|
367 | this->tick(this->dataTank->objectManager->getObjectList(OM_DEAD_TICK), this->dtS); |
---|
368 | this->tick(this->dataTank->objectManager->getObjectList(OM_ENVIRON), this->dtS); |
---|
369 | this->tick(this->dataTank->objectManager->getObjectList(OM_COMMON), this->dtS); |
---|
370 | this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00), this->dtS); |
---|
371 | this->tick(this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ), this->dtS); |
---|
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); |
---|
374 | |
---|
375 | /* update tick the rest */ |
---|
376 | this->dataTank->localCamera->tick(this->dtS); |
---|
377 | AnimationPlayer::getInstance()->tick(this->dtS); |
---|
378 | PhysicsEngine::getInstance()->tick(this->dtS); |
---|
379 | |
---|
380 | GraphicsEngine::getInstance()->tick(this->dtS); |
---|
381 | |
---|
382 | if( likely(this->dataTank->gameRule != NULL)) |
---|
383 | this->dataTank->gameRule->tick(this->dtS); |
---|
384 | } |
---|
385 | this->lastFrame = currentFrame; |
---|
386 | } |
---|
387 | |
---|
388 | |
---|
389 | /** |
---|
390 | * this function gives the world a consistant state |
---|
391 | * |
---|
392 | * after ticking (updating the world state) this will give a constistant |
---|
393 | * state to the whole system. |
---|
394 | */ |
---|
395 | void GameWorld::update() |
---|
396 | { |
---|
397 | GraphicsEngine::getInstance()->update(this->dtS); |
---|
398 | PNode::getNullParent()->updateNode (this->dtS); |
---|
399 | SoundEngine::getInstance()->update(); |
---|
400 | } |
---|
401 | |
---|
402 | |
---|
403 | /** |
---|
404 | * kicks the CDEngine to detect the collisions between the object groups in the world |
---|
405 | */ |
---|
406 | void GameWorld::collide() |
---|
407 | { |
---|
408 | CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00), |
---|
409 | this->dataTank->objectManager->getObjectList(OM_GROUP_01_PROJ)); |
---|
410 | CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01), |
---|
411 | this->dataTank->objectManager->getObjectList(OM_GROUP_00_PROJ)); |
---|
412 | CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00), |
---|
413 | this->dataTank->objectManager->getObjectList(OM_GROUP_01)); |
---|
414 | |
---|
415 | CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_00), |
---|
416 | this->dataTank->objectManager->getObjectList(OM_COMMON)); |
---|
417 | CDEngine::getInstance()->checkCollisions(this->dataTank->objectManager->getObjectList(OM_GROUP_01), |
---|
418 | this->dataTank->objectManager->getObjectList(OM_COMMON)); |
---|
419 | |
---|
420 | } |
---|
421 | |
---|
422 | /** |
---|
423 | * render the current frame |
---|
424 | * |
---|
425 | * clear all buffers and draw the world |
---|
426 | */ |
---|
427 | void GameWorld::display () |
---|
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 | */ |
---|
441 | void GameWorld::draw () |
---|
442 | { |
---|
443 | GraphicsEngine* engine = GraphicsEngine::getInstance(); |
---|
444 | |
---|
445 | // set camera |
---|
446 | this->dataTank->localCamera->apply (); |
---|
447 | this->dataTank->localCamera->project (); |
---|
448 | LightManager::getInstance()->draw(); |
---|
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)); |
---|
455 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_00_PROJ)); |
---|
456 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01)); |
---|
457 | engine->draw(State::getObjectManager()->getObjectList(OM_GROUP_01_PROJ)); |
---|
458 | |
---|
459 | |
---|
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 | } |
---|
470 | |
---|
471 | if( unlikely(this->showPNodes)) |
---|
472 | PNode::getNullParent()->debugDraw(0); |
---|
473 | |
---|
474 | engine->draw(); |
---|
475 | |
---|
476 | |
---|
477 | // draw the game ruls |
---|
478 | if( likely(this->dataTank->gameRule != NULL)) |
---|
479 | this->dataTank->gameRule->draw(); |
---|
480 | } |
---|
481 | |
---|
482 | /** |
---|
483 | * shows the loading screen |
---|
484 | */ |
---|
485 | void GameWorld::displayLoadScreen () |
---|
486 | { |
---|
487 | PRINTF(3)("GameWorld::displayLoadScreen - start\n"); |
---|
488 | this->dataTank->glmis = new GLMenuImageScreen(); |
---|
489 | this->dataTank->glmis->setMaximum(8); |
---|
490 | PRINTF(3)("GameWorld::displayLoadScreen - end\n"); |
---|
491 | } |
---|
492 | |
---|
493 | |
---|
494 | /** |
---|
495 | * removes the loadscreen, and changes over to the game |
---|
496 | */ |
---|
497 | void GameWorld::releaseLoadScreen () |
---|
498 | { |
---|
499 | PRINTF(3)("GameWorld::releaseLoadScreen - start\n"); |
---|
500 | this->dataTank->glmis->setValue(this->dataTank->glmis->getMaximum()); |
---|
501 | PRINTF(3)("GameWorld::releaseLoadScreen - end\n"); |
---|
502 | } |
---|
503 | |
---|
504 | |
---|
505 | |
---|
506 | /** |
---|
507 | * @brief toggles the PNode visibility in the world (drawn as boxes) |
---|
508 | */ |
---|
509 | void 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 | */ |
---|
524 | void 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 | |
---|