4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
35 #include "game_session.hpp"
37 #include "worldmap.hpp"
38 #include "mainloop.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"
57 #include "statistics.hpp"
59 #include "object/fireworks.hpp"
60 #include "textscroller.hpp"
61 #include "control/codecontroller.hpp"
62 #include "control/joystickkeyboardcontroller.hpp"
64 #include "file_system.hpp"
65 #include "gameconfig.hpp"
66 #include "gettext.hpp"
67 #include "console.hpp"
68 #include "flip_level_transformer.hpp"
70 // the engine will be run with a logical framerate of 64fps.
71 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
73 static const float LOGICAL_FPS = 64.0;
76 const char* consoleCommands[] = {
93 using namespace WorldMapNS;
95 GameSession* GameSession::current_ = 0;
97 GameSession::GameSession(const std::string& levelfile_, GameSessionMode mode,
98 Statistics* statistics)
99 : level(0), currentsector(0), mode(mode),
100 end_sequence(NO_ENDSEQUENCE), end_sequence_controller(0),
101 levelfile(levelfile_), best_level_statistics(statistics),
102 capture_demo_stream(0), playback_demo_stream(0), demo_controller(0)
110 for (uint16_t i=0; i < sizeof(::consoleCommands)/sizeof(typeof(consoleCommands[0])); i++) {
111 Console::instance->registerCommand(consoleCommands[i], this);
114 statistics_backdrop.reset(new Surface("images/engine/menu/score-backdrop.png"));
120 GameSession::restart_level(bool fromBeginning)
123 end_sequence = NO_ENDSEQUENCE;
125 main_controller->reset();
129 level.reset(new Level);
130 level->load(levelfile);
132 global_stats.reset();
133 global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
134 global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
138 for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
142 for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
143 j != sec->gameobjects.end(); ++j)
145 GameObject* obj = *j;
147 LevelTime* lt = dynamic_cast<LevelTime*> (obj);
149 time += int(lt->get_level_time());
152 global_stats.set_total_points(TIME_NEEDED_STAT, (time == 0) ? -1 : time);
154 if (fromBeginning) reset_sector="";
155 if(reset_sector != "") {
156 currentsector = level->get_sector(reset_sector);
158 std::stringstream msg;
159 msg << "Couldn't find sector '" << reset_sector << "' for resetting tux.";
160 throw std::runtime_error(msg.str());
162 currentsector->activate(reset_pos);
164 currentsector = level->get_sector("main");
166 throw std::runtime_error("Couldn't find main sector");
167 currentsector->activate("main");
170 if(mode == ST_GL_PLAY || mode == ST_GL_LOAD_LEVEL_FILE)
173 currentsector->play_music(LEVEL_MUSIC);
175 if(capture_file != "")
176 record_demo(capture_file);
179 GameSession::~GameSession()
181 delete capture_demo_stream;
182 delete playback_demo_stream;
183 delete demo_controller;
185 delete end_sequence_controller;
191 GameSession::record_demo(const std::string& filename)
193 delete capture_demo_stream;
195 capture_demo_stream = new std::ofstream(filename.c_str());
196 if(!capture_demo_stream->good()) {
197 std::stringstream msg;
198 msg << "Couldn't open demo file '" << filename << "' for writing.";
199 throw std::runtime_error(msg.str());
201 capture_file = filename;
205 GameSession::play_demo(const std::string& filename)
207 delete playback_demo_stream;
208 delete demo_controller;
210 playback_demo_stream = new std::ifstream(filename.c_str());
211 if(!playback_demo_stream->good()) {
212 std::stringstream msg;
213 msg << "Couldn't open demo file '" << filename << "' for reading.";
214 throw std::runtime_error(msg.str());
217 Player& tux = *currentsector->player;
218 demo_controller = new CodeController();
219 tux.set_controller(demo_controller);
223 GameSession::levelintro()
227 sound_manager->stop_music();
229 DrawingContext context;
230 for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
231 i != currentsector->gameobjects.end(); ++i) {
232 Background* background = dynamic_cast<Background*> (*i);
234 background->draw(context);
236 Gradient* gradient = dynamic_cast<Gradient*> (*i);
238 gradient->draw(context);
242 // context.draw_text(gold_text, level->get_name(), Vector(SCREEN_WIDTH/2, 160),
243 // CENTER_ALLIGN, LAYER_FOREGROUND1);
244 context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
247 sprintf(str, "Coins: %d", player_status->coins);
248 context.draw_text(white_text, str, Vector(SCREEN_WIDTH/2, 210),
249 CENTER_ALLIGN, LAYER_FOREGROUND1);
251 if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
252 //TODO make author check case/blank-insensitive
253 context.draw_text(white_small_text,
254 std::string(_("contributed by ")) + level->get_author(),
255 Vector(SCREEN_WIDTH/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
258 if(best_level_statistics != NULL)
259 best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
261 wait_for_event(1.0, 3.0);
265 GameSession::on_escape_press()
267 if(currentsector->player->is_dying() || end_sequence != NO_ENDSEQUENCE)
268 return; // don't let the player open the menu, when he is dying
270 if(mode == ST_GL_TEST) {
271 main_loop->exit_screen();
272 } else if (!Menu::current()) {
273 Menu::set_current(game_menu);
274 game_menu->set_active_item(MNID_CONTINUE);
282 GameSession::process_events()
284 Player& tux = *currentsector->player;
286 // end of pause mode?
287 if(!Menu::current() && game_pause) {
291 if (end_sequence != NO_ENDSEQUENCE) {
292 if(end_sequence_controller == 0) {
293 end_sequence_controller = new CodeController();
294 tux.set_controller(end_sequence_controller);
297 end_sequence_controller->press(Controller::RIGHT);
299 if (int(last_x_pos) == int(tux.get_pos().x))
300 end_sequence_controller->press(Controller::JUMP);
301 last_x_pos = tux.get_pos().x;
305 if(playback_demo_stream != 0) {
306 demo_controller->update();
313 playback_demo_stream->get(left);
314 playback_demo_stream->get(right);
315 playback_demo_stream->get(up);
316 playback_demo_stream->get(down);
317 playback_demo_stream->get(jump);
318 playback_demo_stream->get(action);
319 demo_controller->press(Controller::LEFT, left);
320 demo_controller->press(Controller::RIGHT, right);
321 demo_controller->press(Controller::UP, up);
322 demo_controller->press(Controller::DOWN, down);
323 demo_controller->press(Controller::JUMP, jump);
324 demo_controller->press(Controller::ACTION, action);
327 // save input for demo?
328 if(capture_demo_stream != 0) {
329 capture_demo_stream ->put(main_controller->hold(Controller::LEFT));
330 capture_demo_stream ->put(main_controller->hold(Controller::RIGHT));
331 capture_demo_stream ->put(main_controller->hold(Controller::UP));
332 capture_demo_stream ->put(main_controller->hold(Controller::DOWN));
333 capture_demo_stream ->put(main_controller->hold(Controller::JUMP));
334 capture_demo_stream ->put(main_controller->hold(Controller::ACTION));
339 GameSession::consoleCommand(std::string command, std::vector<std::string>)
341 if (command == "foo") {
342 log_info << "bar" << std::endl;
346 if (currentsector == 0) return false;
347 Player& tux = *currentsector->player;
349 // Cheating words (the goal of this is really for debugging,
350 // but could be used for some cheating, nothing wrong with that)
351 if (command == "grease") {
352 tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
355 if (command == "invincible") {
356 // be invincle for the rest of the level
357 tux.invincible_timer.start(10000);
360 if (command == "mortal") {
361 // give up invincibility
362 tux.invincible_timer.stop();
365 if (command == "shrink") {
367 tux.kill(tux.SHRINK);
370 if (command == "kill") {
374 if (command == "restart") {
378 if (command == "whereami") {
379 log_info << "You are at x " << tux.get_pos().x << ", y " << tux.get_pos().y << std::endl;
382 if (command == "gotoend") {
383 // goes to the end of the level
385 (currentsector->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
386 currentsector->camera->reset(
387 Vector(tux.get_pos().x, tux.get_pos().y));
390 if (command == "flip") {
391 FlipLevelTransformer flip_transformer;
392 flip_transformer.transform(GameSession::current()->get_current_level());
395 if (command == "finish") {
399 if (command == "camera") {
400 log_info << "Camera is at " << Sector::current()->camera->get_translation().x << "," << Sector::current()->camera->get_translation().y << std::endl;
403 if (command == "quit") {
412 GameSession::check_end_conditions()
414 Player* tux = currentsector->player;
417 if(end_sequence && endsequence_timer.check()) {
420 } else if (!end_sequence && tux->is_dead()) {
421 if (player_status->coins < 0) {
422 // No more coins: restart level from beginning
423 player_status->coins = 0;
426 // Still has coins: restart level from last reset point
427 restart_level(false);
435 GameSession::draw(DrawingContext& context)
437 currentsector->draw(context);
445 GameSession::draw_pause(DrawingContext& context)
447 context.draw_filled_rect(
448 Vector(0,0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
449 Color(.2, .2, .2, .5), LAYER_FOREGROUND1);
453 GameSession::process_menu()
455 Menu* menu = Menu::current();
459 if(menu == game_menu) {
460 switch (game_menu->check()) {
462 Menu::set_current(0);
464 case MNID_ABORTLEVEL:
465 Menu::set_current(0);
466 main_loop->exit_screen();
476 Menu::set_current(NULL);
479 // Eat unneeded events
481 while(SDL_PollEvent(&event))
486 GameSession::update(float elapsed_time)
491 check_end_conditions();
494 if(main_controller->pressed(Controller::PAUSE_MENU))
497 // respawning in new sector?
498 if(newsector != "" && newspawnpoint != "") {
499 Sector* sector = level->get_sector(newsector);
501 log_warning << "Sector '" << newsector << "' not found" << std::endl;
503 sector->activate(newspawnpoint);
504 sector->play_music(LEVEL_MUSIC);
505 currentsector = sector;
510 // Update the world state and all objects in the world
513 if (end_sequence == ENDSEQUENCE_RUNNING) {
514 currentsector->update(elapsed_time/2);
515 } else if(end_sequence == NO_ENDSEQUENCE) {
516 if(!currentsector->player->growing_timer.started())
517 currentsector->update(elapsed_time);
522 sound_manager->set_listener_position(currentsector->player->get_pos());
528 if(currentsector->player->invincible_timer.started()) {
529 if(currentsector->player->invincible_timer.get_timeleft() <=
530 TUX_INVINCIBLE_TIME_WARNING) {
531 currentsector->play_music(HERRING_WARNING_MUSIC);
533 currentsector->play_music(HERRING_MUSIC);
535 } else if(currentsector->get_music_type() != LEVEL_MUSIC) {
536 currentsector->play_music(LEVEL_MUSIC);
541 GameSession::ExitStatus
544 Menu::set_current(0);
549 // Eat unneeded events
551 while(SDL_PollEvent(&event))
556 Uint32 fps_ticks = SDL_GetTicks();
557 Uint32 fps_nextframe_ticks = SDL_GetTicks();
559 bool skipdraw = false;
561 while (exit_status == ES_NONE) {
562 // we run in a logical framerate so elapsed time is a constant
563 // This will make the game run determistic and not different on different
565 static const float elapsed_time = 1.0 / LOGICAL_FPS;
566 // old code... float elapsed_time = float(ticks - lastticks) / 1000.;
568 game_time += elapsed_time;
571 ticks = SDL_GetTicks();
572 if(ticks > fps_nextframe_ticks) {
573 if(skipdraw == true) {
574 // already skipped last frame? we have to slow down the game then...
576 fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
578 // don't draw all frames when we're getting too slow
583 while(fps_nextframe_ticks > ticks) {
585 // If we really have to wait long, then do an imprecise SDL_Delay()
586 Uint32 diff = fps_nextframe_ticks - ticks;
588 SDL_Delay(diff - 10);
590 ticks = SDL_GetTicks();
593 fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
598 // Update the world state and all objects in the world
602 check_end_conditions();
603 if (end_sequence == ENDSEQUENCE_RUNNING)
604 update(elapsed_time/2);
605 else if(end_sequence == NO_ENDSEQUENCE)
606 update(elapsed_time);
613 sound_manager->update();
615 /* Time stops in pause mode */
616 if(game_pause || Menu::current())
622 if (currentsector->player->invincible_timer.started() &&
623 currentsector->player->invincible_timer.get_timeleft()
624 > TUX_INVINCIBLE_TIME_WARNING && !end_sequence)
626 currentsector->play_music(HERRING_MUSIC);
628 /* or just normal music? */
629 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
631 currentsector->play_music(LEVEL_MUSIC);
634 /* Calculate frames per second */
639 if(SDL_GetTicks() - fps_ticks >= 500)
641 fps_fps = (float) fps_cnt / .5;
643 fps_ticks = SDL_GetTicks();
650 main_controller->reset();
656 GameSession::finish(bool win)
659 if(WorldMap::current())
660 WorldMap::current()->finished_level(levelfile);
663 main_loop->exit_screen();
667 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
670 newspawnpoint = spawnpoint;
674 GameSession::set_reset_point(const std::string& sector, const Vector& pos)
676 reset_sector = sector;
681 GameSession::get_working_directory()
683 return FileSystem::dirname(levelfile);
687 GameSession::display_info_box(const std::string& text)
689 InfoBox* box = new InfoBox(text);
692 DrawingContext context;
696 // TODO make a screen out of this, another mainloop is ugly
697 main_controller->update();
699 while (SDL_PollEvent(&event)) {
700 main_controller->process_event(event);
701 if(event.type == SDL_QUIT)
705 if(main_controller->pressed(Controller::JUMP)
706 || main_controller->pressed(Controller::ACTION)
707 || main_controller->pressed(Controller::PAUSE_MENU)
708 || main_controller->pressed(Controller::MENU_SELECT))
710 else if(main_controller->pressed(Controller::DOWN))
712 else if(main_controller->pressed(Controller::UP))
716 context.do_drawing();
717 sound_manager->update();
724 GameSession::start_sequence(const std::string& sequencename)
726 if(sequencename == "endsequence" || sequencename == "fireworks") {
730 end_sequence = ENDSEQUENCE_RUNNING;
731 endsequence_timer.start(7.3);
733 sound_manager->play_music("music/leveldone.ogg", false);
734 currentsector->player->invincible_timer.start(7.3);
737 for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
738 i != currentsector->gameobjects.end(); ++i)
740 GameObject* obj = *i;
742 LevelTime* lt = dynamic_cast<LevelTime*> (obj);
747 // add time spent to statistics
748 int tottime = 0, remtime = 0;
749 for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
753 for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
754 j != sec->gameobjects.end(); ++j)
756 GameObject* obj = *j;
758 LevelTime* lt = dynamic_cast<LevelTime*> (obj);
761 tottime += int(lt->get_level_time());
762 remtime += int(lt->get_remaining_time());
766 global_stats.set_points(TIME_NEEDED_STAT, (tottime == 0 ? -1 : (tottime-remtime)));
768 if(sequencename == "fireworks") {
769 currentsector->add_object(new Fireworks());
771 } else if(sequencename == "stoptux") {
773 log_warning << "Final target reached without an active end sequence" << std::endl;
774 this->start_sequence("endsequence");
776 end_sequence = ENDSEQUENCE_WAITING;
778 log_warning << "Unknown sequence '" << sequencename << "'" << std::endl;
784 GameSession::drawstatus(DrawingContext& context)
786 player_status->draw(context);
788 if(config->show_fps) {
790 snprintf(str, sizeof(str), "%3.1f", fps_fps);
791 const char* fpstext = "FPS";
792 context.draw_text(white_text, fpstext, Vector(SCREEN_WIDTH - white_text->get_text_width(fpstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
793 context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_FOREGROUND1);
796 // draw level stats while end_sequence is running
798 global_stats.draw_endseq_panel(context, best_level_statistics, statistics_backdrop.get());