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.
38 #include <sys/types.h>
43 #include "app/globals.h"
45 #include "video/screen.h"
46 #include "app/setup.h"
47 #include "high_scores.h"
53 #include "object/particlesystem.h"
54 #include "object/background.h"
55 #include "object/tilemap.h"
56 #include "object/camera.h"
57 #include "object/player.h"
58 #include "resources.h"
59 #include "app/gettext.h"
63 #include "statistics.h"
65 #include "object/fireworks.h"
67 GameSession* GameSession::current_ = 0;
69 bool compare_last(std::string& haystack, std::string needle)
71 int haystack_size = haystack.size();
72 int needle_size = needle.size();
74 if(haystack_size < needle_size)
77 if(haystack.compare(haystack_size-needle_size, needle_size, needle) == 0)
82 GameSession::GameSession(const std::string& levelfile_, int mode,
83 bool flip_level_, Statistics* statistics)
84 : level(0), currentsector(0), st_gl_mode(mode),
85 end_sequence(NO_ENDSEQUENCE), levelfile(levelfile_),
86 flip_level(flip_level_), best_level_statistics(statistics)
93 context = new DrawingContext();
98 last_swap_point = Vector(-1, -1);
99 last_swap_stats.reset();
105 GameSession::restart_level()
108 exit_status = ES_NONE;
109 end_sequence = NO_ENDSEQUENCE;
114 Vector tux_pos = Vector(-1,-1);
117 // Tux has lost a life, so we try to respawn him at the nearest reset point
118 tux_pos = currentsector->player->base;
126 level->load(levelfile);
128 level->do_vertical_flip();
130 global_stats.reset();
131 global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
132 global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
133 global_stats.set_total_points(TIME_NEEDED_STAT, level->timelimit);
135 currentsector = level->get_sector("main");
137 Termination::abort("Level has no main sector.", "");
138 currentsector->activate("main");
141 // Set Tux to the nearest reset point
144 tux_pos = currentsector->get_best_spawn_point(tux_pos);
146 if(last_swap_point.x > tux_pos.x)
147 tux_pos = last_swap_point;
148 else // new swap point
150 last_swap_point = tux_pos;
152 last_swap_stats += global_stats;
155 currentsector->player->base.x = tux_pos.x;
156 currentsector->player->base.y = tux_pos.y;
158 // has to reset camera on swapping
159 currentsector->camera->reset(Vector(currentsector->player->base.x,
160 currentsector->player->base.y));
164 if (st_gl_mode != ST_GL_DEMO_GAME)
166 if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
171 currentsector->play_music(LEVEL_MUSIC);
174 GameSession::~GameSession()
181 GameSession::levelintro(void)
183 SoundManager::get()->halt_music();
187 DrawingContext context;
188 if(currentsector->background)
189 currentsector->background->draw(context);
191 // context.draw_text(gold_text, level->get_name(), Vector(screen->w/2, 160),
192 // CENTER_ALLIGN, LAYER_FOREGROUND1);
193 context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
196 sprintf(str, "TUX x %d", player_status.lives);
197 context.draw_text(white_text, str, Vector(screen->w/2, 210),
198 CENTER_ALLIGN, LAYER_FOREGROUND1);
200 if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
201 //TODO make author check case/blank-insensitive
202 context.draw_text(white_small_text,
203 std::string(_("contributed by ")) + level->get_author(),
204 Vector(screen->w/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
208 context.draw_text(white_text,
209 _("Level Vertically Flipped!"),
210 Vector(screen->w/2, 310), CENTER_ALLIGN, LAYER_FOREGROUND1);
212 if(best_level_statistics != NULL)
213 best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
215 context.do_drawing();
218 wait_for_event(event,1000,3000,true);
223 GameSession::start_timers()
225 time_left.start(level->timelimit);
230 GameSession::on_escape_press()
232 if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
233 return; // don't let the player open the menu, when he is dying
238 if(st_gl_mode == ST_GL_TEST)
240 exit_status = ES_LEVEL_ABORT;
242 else if (!Menu::current())
244 /* Tell Tux that the keys are all down, otherwise
245 it could have nasty bugs, like going allways to the right
246 or whatever that key does */
247 Player& tux = *(currentsector->player);
248 tux.key_event((SDLKey)keymap.up, UP);
249 tux.key_event((SDLKey)keymap.down, UP);
250 tux.key_event((SDLKey)keymap.left, UP);
251 tux.key_event((SDLKey)keymap.right, UP);
252 tux.key_event((SDLKey)keymap.jump, UP);
253 tux.key_event((SDLKey)keymap.power, UP);
255 Menu::set_current(game_menu);
256 Ticks::pause_start();
261 GameSession::process_events()
263 if (end_sequence != NO_ENDSEQUENCE)
265 Player& tux = *currentsector->player;
269 tux.input.right = DOWN;
272 if (int(last_x_pos) == int(tux.get_pos().x))
277 last_x_pos = tux.get_pos().x;
280 while (SDL_PollEvent(&event))
282 /* Check for menu-events, if the menu is shown */
285 Menu::current()->event(event);
292 case SDL_QUIT: /* Quit event - quit: */
293 Termination::abort("Received window close", "");
296 case SDL_KEYDOWN: /* A keypress! */
298 SDLKey key = event.key.keysym.sym;
302 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
310 case SDL_JOYBUTTONDOWN:
311 if (event.jbutton.button == joystick_keymap.start_button)
319 if(!Menu::current() && !game_pause)
323 while (SDL_PollEvent(&event))
325 /* Check for menu-events, if the menu is shown */
328 Menu::current()->event(event);
334 Player& tux = *currentsector->player;
338 case SDL_QUIT: /* Quit event - quit: */
339 Termination::abort("Received window close", "");
342 case SDL_KEYDOWN: /* A keypress! */
344 SDLKey key = event.key.keysym.sym;
346 if(tux.key_event(key,DOWN))
351 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
359 case SDL_KEYUP: /* A keyrelease! */
361 SDLKey key = event.key.keysym.sym;
363 if(tux.key_event(key, UP))
372 snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
373 tux.get_pos().x, tux.get_pos().y);
374 context->draw_text(white_text, buf,
375 Vector(0, screen->h - white_text->get_height()),
376 LEFT_ALLIGN, LAYER_FOREGROUND1);
377 context->do_drawing();
384 // "lifeup" cheat activates pause cause of the 'p'
385 // so work around to ignore it
386 if(compare_last(last_keys, "lifeu"))
397 Ticks::pause_start();
406 /* Check if chacrater is ASCII */
408 if((event.key.keysym.unicode & 0xFF80) == 0)
410 ch[0] = event.key.keysym.unicode & 0x7F;
413 last_keys.append(ch); // add to cheat keys
415 // Cheating words (the goal of this is really for debugging,
416 // but could be used for some cheating, nothing wrong with that)
417 if(compare_last(last_keys, "grow"))
422 if(compare_last(last_keys, "fire"))
425 tux.got_power = tux.FIRE_POWER;
428 if(compare_last(last_keys, "ice"))
431 tux.got_power = tux.ICE_POWER;
434 if(compare_last(last_keys, "lifeup"))
436 player_status.lives++;
439 if(compare_last(last_keys, "lifedown"))
441 player_status.lives--;
444 if(compare_last(last_keys, "grease"))
446 tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
449 if(compare_last(last_keys, "invincible"))
450 { // be invincle for the rest of the level
451 tux.invincible_timer.start(10000);
454 if(compare_last(last_keys, "shrink"))
456 tux.kill(tux.SHRINK);
459 if(compare_last(last_keys, "kill"))
460 { // kill Tux, but without losing a life
461 player_status.lives++;
465 if(compare_last(last_keys, "grid"))
466 { // toggle debug grid
467 debug_grid = !debug_grid;
470 if(compare_last(last_keys, "hover"))
471 { // toggle hover ability on/off
472 tux.enable_hover = !tux.enable_hover;
475 if(compare_last(last_keys, "gotoend"))
476 { // goes to the end of the level
478 (currentsector->solids->get_width()*32)
481 currentsector->camera->reset(
482 Vector(tux.get_pos().x, tux.get_pos().y));
485 // temporary to help player's choosing a flapping
486 if(compare_last(last_keys, "marek"))
488 tux.flapping_mode = Player::MAREK_FLAP;
491 if(compare_last(last_keys, "ricardo"))
493 tux.flapping_mode = Player::RICARDO_FLAP;
496 if(compare_last(last_keys, "ryan"))
498 tux.flapping_mode = Player::RYAN_FLAP;
503 case SDL_JOYAXISMOTION:
504 if (event.jaxis.axis == joystick_keymap.x_axis)
506 if (event.jaxis.value < -joystick_keymap.dead_zone)
508 tux.input.left = DOWN;
509 tux.input.right = UP;
511 else if (event.jaxis.value > joystick_keymap.dead_zone)
514 tux.input.right = DOWN;
518 tux.input.left = DOWN;
519 tux.input.right = DOWN;
522 else if (event.jaxis.axis == joystick_keymap.y_axis)
524 if (event.jaxis.value > joystick_keymap.dead_zone)
529 else if (event.jaxis.value < -joystick_keymap.dead_zone)
532 tux.input.down = DOWN;
537 tux.input.down = DOWN;
542 case SDL_JOYHATMOTION:
543 if(event.jhat.value & SDL_HAT_UP) {
546 } else if(event.jhat.value & SDL_HAT_DOWN) {
548 tux.input.down = DOWN;
549 } else if(event.jhat.value & SDL_HAT_LEFT) {
550 tux.input.left = DOWN;
551 tux.input.right = UP;
552 } else if(event.jhat.value & SDL_HAT_RIGHT) {
554 tux.input.right = DOWN;
555 } else if(event.jhat.value == SDL_HAT_CENTERED) {
557 tux.input.right = UP;
563 case SDL_JOYBUTTONDOWN:
564 if (event.jbutton.button == joystick_keymap.a_button)
565 tux.input.jump = DOWN;
566 else if (event.jbutton.button == joystick_keymap.b_button)
567 tux.input.fire = DOWN;
568 else if (event.jbutton.button == joystick_keymap.start_button)
571 case SDL_JOYBUTTONUP:
572 if (event.jbutton.button == joystick_keymap.a_button)
574 else if (event.jbutton.button == joystick_keymap.b_button)
587 GameSession::check_end_conditions()
589 Player* tux = currentsector->player;
592 if(end_sequence && endsequence_timer.check()) {
593 exit_status = ES_LEVEL_FINISHED;
594 global_stats += last_swap_stats; // add swap points stats
596 } else if (!end_sequence && tux->is_dead()) {
597 player_status.bonus = PlayerStatus::NO_BONUS;
599 if (player_status.lives < 0) { // No more lives!?
600 exit_status = ES_GAME_OVER;
601 } else { // Still has lives, so reset Tux to the levelstart
610 GameSession::action(float elapsed_time)
613 if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
615 // Update Tux and the World
616 currentsector->action(elapsed_time);
619 // respawning in new sector?
620 if(newsector != "" && newspawnpoint != "") {
621 Sector* sector = level->get_sector(newsector);
622 currentsector = sector;
623 currentsector->activate(newspawnpoint);
624 currentsector->play_music(LEVEL_MUSIC);
625 newsector = newspawnpoint = "";
632 currentsector->draw(*context);
633 drawstatus(*context);
637 int x = screen->h / 20;
638 for(int i = 0; i < x; ++i)
640 context->draw_filled_rect(
641 Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
642 -((pause_menu_frame * i)%screen->w)
643 ,(i*20+pause_menu_frame)%screen->h),
644 Vector(screen->w,10),
645 Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
647 context->draw_filled_rect(
648 Vector(0,0), Vector(screen->w, screen->h),
649 Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
650 context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
651 Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
655 sprintf(str1, _("Playing: "));
656 sprintf(str2, level->name.c_str());
658 context->draw_text(blue_text, str1,
659 Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
660 LEFT_ALLIGN, LAYER_FOREGROUND1+2);
661 context->draw_text(white_text, str2,
662 Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
663 LEFT_ALLIGN, LAYER_FOREGROUND1+2);
668 Menu::current()->draw(*context);
669 mouse_cursor->draw(*context);
672 context->do_drawing();
676 GameSession::process_menu()
678 Menu* menu = Menu::current();
683 if(menu == game_menu)
685 switch (game_menu->check())
690 case MNID_ABORTLEVEL:
692 exit_status = ES_LEVEL_ABORT;
696 else if(menu == options_menu)
698 process_options_menu();
700 else if(menu == load_game_menu )
702 process_load_game_menu();
707 GameSession::ExitStatus
710 Menu::set_current(0);
715 // Eat unneeded events
717 while(SDL_PollEvent(&event))
722 Uint32 lastticks = SDL_GetTicks();
723 fps_ticks = SDL_GetTicks();
725 while (exit_status == ES_NONE) {
726 Uint32 ticks = SDL_GetTicks();
727 float elapsed_time = float(ticks - lastticks) / 1000.;
729 global_time += elapsed_time;
733 if(elapsed_time > .025)
737 currentsector->player->input.old_fire = currentsector->player->input.fire;
738 currentsector->player->input.old_up = currentsector->player->input.old_up;
743 // Update the world state and all objects in the world
744 // Do that with a constante time-delta so that the game will run
745 // determistic and not different on different machines
746 if(!game_pause && !Menu::current())
749 check_end_conditions();
750 if (end_sequence == ENDSEQUENCE_RUNNING)
751 action(elapsed_time/2);
752 else if(end_sequence == NO_ENDSEQUENCE)
753 action(elapsed_time);
763 /* Time stops in pause mode */
764 if(game_pause || Menu::current())
769 //frame_rate.update();
772 if (time_left.check() && currentsector->player->dying == DYING_NOT
774 currentsector->player->kill(Player::KILL);
777 if(currentsector->player->invincible_timer.check() && !end_sequence)
779 currentsector->play_music(HERRING_MUSIC);
781 /* are we low on time ? */
782 else if (time_left.get_timeleft() < TIME_WARNING && !end_sequence)
784 currentsector->play_music(HURRYUP_MUSIC);
786 /* or just normal music? */
787 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
789 currentsector->play_music(LEVEL_MUSIC);
792 /* Calculate frames per second */
797 if(SDL_GetTicks() - fps_ticks >= 500)
799 fps_fps = (float) fps_cnt / .5;
801 fps_ticks = SDL_GetTicks();
810 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
813 newspawnpoint = spawnpoint;
817 GameSession::start_sequence(const std::string& sequencename)
819 if(sequencename == "endsequence") {
823 end_sequence = ENDSEQUENCE_RUNNING;
824 endsequence_timer.start(7.0); // 7 seconds until we finish the map
826 SoundManager::get()->play_music(level_end_song, 0);
827 currentsector->player->invincible_timer.start(7.0);
829 // add left time to stats
830 global_stats.set_points(TIME_NEEDED_STAT,
831 int(time_left.get_period() - time_left.get_timeleft()));
833 if(level->get_end_sequence_type() == Level::FIREWORKS_ENDSEQ_ANIM) {
834 currentsector->add_object(new Fireworks());
837 std::cout << "Unknown sequence '" << sequencename << "'.\n";
843 GameSession::drawstatus(DrawingContext& context)
847 snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
848 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
849 context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
851 if(st_gl_mode == ST_GL_TEST)
853 context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
854 LEFT_ALLIGN, LAYER_FOREGROUND1);
857 if(time_left.check()) {
858 context.draw_text(white_text, _("TIME's UP"), Vector(screen->w/2, 0),
859 CENTER_ALLIGN, LAYER_FOREGROUND1);
860 } else if (time_left.get_timeleft() > TIME_WARNING
861 || int(global_time * 2.5) % 2) {
862 sprintf(str, " %d", int(time_left.get_timeleft()));
863 context.draw_text(white_text, _("TIME"),
864 Vector(screen->w/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
865 context.draw_text(gold_text, str,
866 Vector(screen->w/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
869 sprintf(str, " %d", player_status.distros);
870 context.draw_text(white_text, _("COINS"),
871 Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width(" 99"), 0),
872 LEFT_ALLIGN, LAYER_FOREGROUND1);
873 context.draw_text(gold_text, str,
874 Vector(screen->w - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
876 if (player_status.lives >= 5)
878 sprintf(str, "%dx", player_status.lives);
879 float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
880 context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
881 context.draw_surface(tux_life, Vector(screen->w - 16, 20),
886 for(int i= 0; i < player_status.lives; ++i)
887 context.draw_surface(tux_life,
888 Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
892 context.draw_text(white_text, _("LIVES"),
893 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 20),
894 LEFT_ALLIGN, LAYER_FOREGROUND1);
898 sprintf(str, "%2.1f", fps_fps);
899 context.draw_text(white_text, "FPS",
900 Vector(screen->w - white_text->get_text_width("FPS "), 40),
901 LEFT_ALLIGN, LAYER_FOREGROUND1);
902 context.draw_text(gold_text, str,
903 Vector(screen->w-4*16, 40), LEFT_ALLIGN, LAYER_FOREGROUND1);
908 GameSession::drawresultscreen(void)
912 DrawingContext context;
913 currentsector->background->draw(context);
915 context.draw_text(blue_text, _("Result:"), Vector(screen->w/2, 200),
916 CENTER_ALLIGN, LAYER_FOREGROUND1);
918 sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
919 context.draw_text(gold_text, str, Vector(screen->w/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
921 sprintf(str, _("COINS: %d"), player_status.distros);
922 context.draw_text(gold_text, str, Vector(screen->w/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
924 context.do_drawing();
927 wait_for_event(event,2000,5000,true);
930 std::string slotinfo(int slot)
933 std::string slotfile;
935 std::stringstream stream;
937 slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
939 lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
942 LispReader reader(lisp_cdr(savegame));
943 reader.read_string("title", title);
947 if (access(slotfile.c_str(), F_OK) == 0)
950 tmp = "Slot " + stream.str() + " - " + title;
952 tmp = "Slot " + stream.str() + " - Savegame";
955 tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
960 bool process_load_game_menu()
962 int slot = load_game_menu->check();
964 if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
966 std::stringstream stream;
968 std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
970 if (access(slotfile.c_str(), F_OK) != 0)
972 shrink_fade(Vector(screen->w/2,screen->h/2), 600);
977 DrawingContext context;
978 context.draw_text(white_text, "Loading...",
979 Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
980 context.do_drawing();
982 WorldMapNS::WorldMap worldmap;
984 worldmap.set_map_filename("icyisland.stwm");
985 // Load the game or at least set the savegame_file variable
986 worldmap.loadgame(slotfile);
990 Menu::set_current(main_menu);