4 // Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 // Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include "game_session.hpp"
39 #include "video/screen.hpp"
40 #include "audio/sound_manager.hpp"
41 #include "gui/menu.hpp"
45 #include "player_status.hpp"
46 #include "object/particlesystem.hpp"
47 #include "object/background.hpp"
48 #include "object/gradient.hpp"
49 #include "object/tilemap.hpp"
50 #include "object/camera.hpp"
51 #include "object/player.hpp"
52 #include "object/level_time.hpp"
53 #include "lisp/lisp.hpp"
54 #include "lisp/parser.hpp"
55 #include "resources.hpp"
56 #include "worldmap.hpp"
58 #include "statistics.hpp"
60 #include "object/fireworks.hpp"
61 #include "textscroller.hpp"
62 #include "control/codecontroller.hpp"
63 #include "control/joystickkeyboardcontroller.hpp"
65 #include "file_system.hpp"
66 #include "gameconfig.hpp"
67 #include "gettext.hpp"
68 #include "exceptions.hpp"
69 #include "flip_level_transformer.hpp"
71 // the engine will be run with a logical framerate of 64fps.
72 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
74 static const float LOGICAL_FPS = 64.0;
76 GameSession* GameSession::current_ = 0;
78 GameSession::GameSession(const std::string& levelfile_, GameSessionMode mode,
79 Statistics* statistics)
80 : level(0), currentsector(0), mode(mode),
81 end_sequence(NO_ENDSEQUENCE), end_sequence_controller(0),
82 levelfile(levelfile_), best_level_statistics(statistics),
83 capture_demo_stream(0), playback_demo_stream(0), demo_controller(0)
91 context = new DrawingContext();
92 console = new Console(context);
93 Console::registerCommandReceiver(this);
99 GameSession::restart_level()
102 exit_status = ES_NONE;
103 end_sequence = NO_ENDSEQUENCE;
105 main_controller->reset();
111 level->load(levelfile);
113 global_stats.reset();
114 global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
115 global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
119 for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
123 for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
124 j != sec->gameobjects.end(); ++j)
126 GameObject* obj = *j;
128 LevelTime* lt = dynamic_cast<LevelTime*> (obj);
130 time += int(lt->get_level_time());
133 global_stats.set_total_points(TIME_NEEDED_STAT, (time == 0) ? -1 : time);
135 if(reset_sector != "") {
136 currentsector = level->get_sector(reset_sector);
138 std::stringstream msg;
139 msg << "Couldn't find sector '" << reset_sector << "' for resetting tux.";
140 throw std::runtime_error(msg.str());
142 currentsector->activate(reset_pos);
144 currentsector = level->get_sector("main");
146 throw std::runtime_error("Couldn't find main sector");
147 currentsector->activate("main");
150 if(mode == ST_GL_PLAY || mode == ST_GL_LOAD_LEVEL_FILE)
153 currentsector->play_music(LEVEL_MUSIC);
155 if(capture_file != "")
156 record_demo(capture_file);
159 GameSession::~GameSession()
161 delete capture_demo_stream;
162 delete playback_demo_stream;
163 delete demo_controller;
165 delete end_sequence_controller;
168 Console::unregisterCommandReceiver(this);
175 GameSession::record_demo(const std::string& filename)
177 delete capture_demo_stream;
179 capture_demo_stream = new std::ofstream(filename.c_str());
180 if(!capture_demo_stream->good()) {
181 std::stringstream msg;
182 msg << "Couldn't open demo file '" << filename << "' for writing.";
183 throw std::runtime_error(msg.str());
185 capture_file = filename;
189 GameSession::play_demo(const std::string& filename)
191 delete playback_demo_stream;
192 delete demo_controller;
194 playback_demo_stream = new std::ifstream(filename.c_str());
195 if(!playback_demo_stream->good()) {
196 std::stringstream msg;
197 msg << "Couldn't open demo file '" << filename << "' for reading.";
198 throw std::runtime_error(msg.str());
201 Player& tux = *currentsector->player;
202 demo_controller = new CodeController();
203 tux.set_controller(demo_controller);
207 GameSession::levelintro()
211 sound_manager->stop_music();
213 DrawingContext context;
214 for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
215 i != currentsector->gameobjects.end(); ++i) {
216 Background* background = dynamic_cast<Background*> (*i);
218 background->draw(context);
220 Gradient* gradient = dynamic_cast<Gradient*> (*i);
222 gradient->draw(context);
226 // context.draw_text(gold_text, level->get_name(), Vector(SCREEN_WIDTH/2, 160),
227 // CENTER_ALLIGN, LAYER_FOREGROUND1);
228 context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
231 sprintf(str, "TUX x %d", player_status->lives);
232 context.draw_text(white_text, str, Vector(SCREEN_WIDTH/2, 210),
233 CENTER_ALLIGN, LAYER_FOREGROUND1);
235 if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
236 //TODO make author check case/blank-insensitive
237 context.draw_text(white_small_text,
238 std::string(_("contributed by ")) + level->get_author(),
239 Vector(SCREEN_WIDTH/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
242 if(best_level_statistics != NULL)
243 best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
246 context.do_drawing();
248 wait_for_event(1.0, 3.0);
252 GameSession::on_escape_press()
254 if(currentsector->player->is_dying() || end_sequence != NO_ENDSEQUENCE)
255 return; // don't let the player open the menu, when he is dying
257 if(mode == ST_GL_TEST) {
258 exit_status = ES_LEVEL_ABORT;
259 } else if (!Menu::current()) {
260 Menu::set_current(game_menu);
261 game_menu->set_active_item(MNID_CONTINUE);
269 GameSession::process_events()
271 Player& tux = *currentsector->player;
272 main_controller->update();
274 // end of pause mode?
275 if(!Menu::current() && game_pause) {
279 if (end_sequence != NO_ENDSEQUENCE) {
280 if(end_sequence_controller == 0) {
281 end_sequence_controller = new CodeController();
282 tux.set_controller(end_sequence_controller);
285 end_sequence_controller->press(Controller::RIGHT);
287 if (int(last_x_pos) == int(tux.get_pos().x))
288 end_sequence_controller->press(Controller::JUMP);
289 last_x_pos = tux.get_pos().x;
292 main_controller->update();
294 while (SDL_PollEvent(&event)) {
295 /* Check for menu-events, if the menu is shown */
297 Menu::current()->event(event);
298 main_controller->process_event(event);
299 if(event.type == SDL_QUIT)
300 throw graceful_shutdown();
304 if(playback_demo_stream != 0) {
305 demo_controller->update();
312 playback_demo_stream->get(left);
313 playback_demo_stream->get(right);
314 playback_demo_stream->get(up);
315 playback_demo_stream->get(down);
316 playback_demo_stream->get(jump);
317 playback_demo_stream->get(action);
318 demo_controller->press(Controller::LEFT, left);
319 demo_controller->press(Controller::RIGHT, right);
320 demo_controller->press(Controller::UP, up);
321 demo_controller->press(Controller::DOWN, down);
322 demo_controller->press(Controller::JUMP, jump);
323 demo_controller->press(Controller::ACTION, action);
326 // save input for demo?
327 if(capture_demo_stream != 0) {
328 capture_demo_stream ->put(main_controller->hold(Controller::LEFT));
329 capture_demo_stream ->put(main_controller->hold(Controller::RIGHT));
330 capture_demo_stream ->put(main_controller->hold(Controller::UP));
331 capture_demo_stream ->put(main_controller->hold(Controller::DOWN));
332 capture_demo_stream ->put(main_controller->hold(Controller::JUMP));
333 capture_demo_stream ->put(main_controller->hold(Controller::ACTION));
338 GameSession::consoleCommand(std::string command)
340 if (command == "foo") {
345 if (currentsector == 0) return false;
346 Player& tux = *currentsector->player;
348 // Cheating words (the goal of this is really for debugging,
349 // but could be used for some cheating, nothing wrong with that)
350 if (command == "grow") {
351 tux.set_bonus(GROWUP_BONUS, false);
354 if (command == "fire") {
355 tux.set_bonus(FIRE_BONUS, false);
358 if (command == "ice") {
359 tux.set_bonus(ICE_BONUS, false);
362 if (command == "lifeup") {
363 player_status->lives++;
366 if (command == "lifedown") {
367 player_status->lives--;
370 if (command == "grease") {
371 tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
374 if (command == "invincible") {
375 // be invincle for the rest of the level
376 tux.invincible_timer.start(10000);
379 if (command == "mortal") {
380 // give up invincibility
381 tux.invincible_timer.stop();
384 if (command == "shrink") {
386 tux.kill(tux.SHRINK);
389 if (command == "kill") {
390 // kill Tux, but without losing a life
391 player_status->lives++;
395 if (command == "whereami") {
396 msg_info("You are at x " << tux.get_pos().x << ", y " << tux.get_pos().y);
400 if(command == "grid")) {
402 debug_grid = !debug_grid;
406 if (command == "gotoend") {
407 // goes to the end of the level
409 (currentsector->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
410 currentsector->camera->reset(
411 Vector(tux.get_pos().x, tux.get_pos().y));
414 if (command == "flip") {
415 FlipLevelTransformer flip_transformer;
416 flip_transformer.transform(GameSession::current()->get_current_level());
419 if (command == "finish") {
420 // finish current sector
421 exit_status = ES_LEVEL_FINISHED;
422 // don't add points to stats though...
425 if (command == "camera") {
426 msg_info("Camera is at "
427 << Sector::current()->camera->get_translation().x << ","
428 << Sector::current()->camera->get_translation().y);
436 GameSession::check_end_conditions()
438 Player* tux = currentsector->player;
441 if(end_sequence && endsequence_timer.check()) {
442 exit_status = ES_LEVEL_FINISHED;
444 // add time spent to statistics
445 int tottime = 0, remtime = 0;
446 for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
450 for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
451 j != sec->gameobjects.end(); ++j)
453 GameObject* obj = *j;
455 LevelTime* lt = dynamic_cast<LevelTime*> (obj);
458 tottime += int(lt->get_level_time());
459 remtime += int(lt->get_remaining_time());
463 global_stats.set_points(TIME_NEEDED_STAT, (tottime == 0 ? -1 : (tottime-remtime)));
466 } else if (!end_sequence && tux->is_dead()) {
467 if (player_status->lives < 0) { // No more lives!?
468 exit_status = ES_GAME_OVER;
469 } else { // Still has lives, so reset Tux to the levelstart
478 GameSession::update(float elapsed_time)
481 if(main_controller->pressed(Controller::PAUSE_MENU))
485 if(!currentsector->player->growing_timer.started()) {
486 // Update Tux and the World
487 currentsector->update(elapsed_time);
490 // respawning in new sector?
491 if(newsector != "" && newspawnpoint != "") {
492 Sector* sector = level->get_sector(newsector);
494 msg_warning("Sector '" << newsector << "' not found");
496 sector->activate(newspawnpoint);
497 sector->play_music(LEVEL_MUSIC);
498 currentsector = sector;
504 sound_manager->set_listener_position(currentsector->player->get_pos());
510 currentsector->draw(*context);
511 drawstatus(*context);
516 if(Menu::current()) {
517 Menu::current()->draw(*context);
521 context->do_drawing();
525 GameSession::draw_pause()
527 context->draw_filled_rect(
528 Vector(0,0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
529 Color(.2, .2, .2, .5), LAYER_FOREGROUND1);
533 GameSession::process_menu()
535 Menu* menu = Menu::current();
539 if(menu == game_menu) {
540 switch (game_menu->check()) {
542 Menu::set_current(0);
544 case MNID_ABORTLEVEL:
545 Menu::set_current(0);
546 exit_status = ES_LEVEL_ABORT;
549 } else if(menu == options_menu) {
550 process_options_menu();
551 } else if(menu == load_game_menu ) {
552 process_load_game_menu();
558 GameSession::ExitStatus
561 Menu::set_current(0);
566 // Eat unneeded events
568 while(SDL_PollEvent(&event))
573 Uint32 fps_ticks = SDL_GetTicks();
574 Uint32 fps_nextframe_ticks = SDL_GetTicks();
576 bool skipdraw = false;
578 while (exit_status == ES_NONE) {
579 // we run in a logical framerate so elapsed time is a constant
580 static const float elapsed_time = 1.0 / LOGICAL_FPS;
581 // old code... float elapsed_time = float(ticks - lastticks) / 1000.;
583 game_time += elapsed_time;
586 ticks = SDL_GetTicks();
587 if(ticks > fps_nextframe_ticks) {
588 if(skipdraw == true) {
589 // already skipped last frame? we have to slow down the game then...
591 fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
593 // don't draw all frames when we're getting too slow
598 while(fps_nextframe_ticks > ticks) {
600 // If we really have to wait long, then do an imprecise SDL_Delay()
601 Uint32 diff = fps_nextframe_ticks - ticks;
603 SDL_Delay(diff - 10);
605 ticks = SDL_GetTicks();
608 fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
611 float diff = SDL_GetTicks() - fps_nextframe_ticks;
613 // sets the ticks that must have elapsed
614 fps_nextframe_ticks = SDL_GetTicks() + (1000.0 / LOGICAL_FPS);
616 // sets the ticks that must have elapsed
617 // in order for the next frame to start.
618 fps_nextframe_ticks += 1000.0 / LOGICAL_FPS;
625 // Update the world state and all objects in the world
626 // Do that with a constante time-delta so that the game will run
627 // determistic and not different on different machines
628 if(!game_pause && !Menu::current())
631 check_end_conditions();
632 if (end_sequence == ENDSEQUENCE_RUNNING)
633 update(elapsed_time/2);
634 else if(end_sequence == NO_ENDSEQUENCE)
635 update(elapsed_time);
646 sound_manager->update();
648 /* Time stops in pause mode */
649 if(game_pause || Menu::current())
654 //frame_rate.update();
657 if (currentsector->player->invincible_timer.started() &&
658 currentsector->player->invincible_timer.get_timeleft()
659 > TUX_INVINCIBLE_TIME_WARNING && !end_sequence)
661 currentsector->play_music(HERRING_MUSIC);
663 /* or just normal music? */
664 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
666 currentsector->play_music(LEVEL_MUSIC);
669 /* Calculate frames per second */
674 if(SDL_GetTicks() - fps_ticks >= 500)
676 fps_fps = (float) fps_cnt / .5;
678 fps_ticks = SDL_GetTicks();
685 main_controller->reset();
690 GameSession::finish(bool win)
693 exit_status = ES_LEVEL_FINISHED;
695 exit_status = ES_LEVEL_ABORT;
699 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
702 newspawnpoint = spawnpoint;
706 GameSession::set_reset_point(const std::string& sector, const Vector& pos)
708 reset_sector = sector;
713 GameSession::get_working_directory()
715 return FileSystem::dirname(levelfile);
719 GameSession::display_info_box(const std::string& text)
721 InfoBox* box = new InfoBox(text);
726 main_controller->update();
728 while (SDL_PollEvent(&event)) {
729 main_controller->process_event(event);
730 if(event.type == SDL_QUIT)
731 throw graceful_shutdown();
734 if(main_controller->pressed(Controller::JUMP)
735 || main_controller->pressed(Controller::ACTION)
736 || main_controller->pressed(Controller::PAUSE_MENU)
737 || main_controller->pressed(Controller::MENU_SELECT))
739 else if(main_controller->pressed(Controller::DOWN))
741 else if(main_controller->pressed(Controller::UP))
745 sound_manager->update();
752 GameSession::start_sequence(const std::string& sequencename)
754 if(sequencename == "endsequence" || sequencename == "fireworks") {
758 end_sequence = ENDSEQUENCE_RUNNING;
759 endsequence_timer.start(7.3);
761 sound_manager->play_music("music/leveldone.ogg", false);
762 currentsector->player->invincible_timer.start(7.3);
765 for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
766 i != currentsector->gameobjects.end(); ++i)
768 GameObject* obj = *i;
770 LevelTime* lt = dynamic_cast<LevelTime*> (obj);
775 if(sequencename == "fireworks") {
776 currentsector->add_object(new Fireworks());
778 } else if(sequencename == "stoptux") {
780 msg_warning("Final target reached without "
781 << "an active end sequence");
782 this->start_sequence("endsequence");
784 end_sequence = ENDSEQUENCE_WAITING;
786 msg_warning("Unknown sequence '" << sequencename << "'");
792 GameSession::drawstatus(DrawingContext& context)
794 player_status->draw(context);
796 if(config->show_fps) {
798 snprintf(str, sizeof(str), "%3.1f", fps_fps);
799 context.draw_text(white_text, "FPS",
800 Vector(SCREEN_WIDTH -
801 white_text->get_text_width("FPS ") - BORDER_X, BORDER_Y + 40),
802 LEFT_ALLIGN, LAYER_FOREGROUND1);
803 context.draw_text(gold_text, str,
804 Vector(SCREEN_WIDTH-4*16 - BORDER_X, BORDER_Y + 40),
805 LEFT_ALLIGN, LAYER_FOREGROUND1);
810 GameSession::drawresultscreen()
814 DrawingContext context;
815 for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
816 i != currentsector->gameobjects.end(); ++i) {
817 Background* background = dynamic_cast<Background*> (*i);
819 background->draw(context);
823 context.draw_text(blue_text, _("Result:"), Vector(SCREEN_WIDTH/2, 200),
824 CENTER_ALLIGN, LAYER_FOREGROUND1);
826 // sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
827 // context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
829 // y == 256 before removal of score
830 sprintf(str, _("COINS: %d"), player_status->coins);
831 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
834 context.do_drawing();
836 wait_for_event(2.0, 5.0);
839 std::string slotinfo(int slot)
842 std::string slotfile;
844 std::stringstream stream;
846 slotfile = "save/slot" + stream.str() + ".stsg";
850 std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
852 const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
854 throw std::runtime_error("file is not a supertux-savegame.");
856 savegame->get("title", title);
857 } catch(std::exception& e) {
858 return std::string(_("Slot")) + " " + stream.str() + " - " +
859 std::string(_("Free"));
862 return std::string("Slot ") + stream.str() + " - " + title;
865 bool process_load_game_menu()
867 int slot = load_game_menu->check();
872 if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
875 std::stringstream stream;
877 std::string slotfile = "save/slot" + stream.str() + ".stsg";
879 sound_manager->stop_music();
881 DrawingContext context;
882 context.draw_text(white_text, "Loading...",
883 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2),
884 CENTER_ALLIGN, LAYER_FOREGROUND1);
885 context.do_drawing();
887 WorldMapNS::WorldMap worldmap;
889 worldmap.set_map_filename("/levels/world1/worldmap.stwm");
890 // Load the game or at least set the savegame_file variable
891 worldmap.loadgame(slotfile);
895 Menu::set_current(main_menu);