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 for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
189 i != currentsector->gameobjects.end(); ++i) {
190 Background* background = dynamic_cast<Background*> (*i);
192 background->draw(context);
196 // context.draw_text(gold_text, level->get_name(), Vector(screen->w/2, 160),
197 // CENTER_ALLIGN, LAYER_FOREGROUND1);
198 context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
201 sprintf(str, "TUX x %d", player_status.lives);
202 context.draw_text(white_text, str, Vector(screen->w/2, 210),
203 CENTER_ALLIGN, LAYER_FOREGROUND1);
205 if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
206 //TODO make author check case/blank-insensitive
207 context.draw_text(white_small_text,
208 std::string(_("contributed by ")) + level->get_author(),
209 Vector(screen->w/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
213 context.draw_text(white_text,
214 _("Level Vertically Flipped!"),
215 Vector(screen->w/2, 310), CENTER_ALLIGN, LAYER_FOREGROUND1);
217 if(best_level_statistics != NULL)
218 best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
220 context.do_drawing();
223 wait_for_event(event,1000,3000,true);
228 GameSession::start_timers()
230 time_left.start(level->timelimit);
235 GameSession::on_escape_press()
237 if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
238 return; // don't let the player open the menu, when he is dying
243 if(st_gl_mode == ST_GL_TEST)
245 exit_status = ES_LEVEL_ABORT;
247 else if (!Menu::current())
249 /* Tell Tux that the keys are all down, otherwise
250 it could have nasty bugs, like going allways to the right
251 or whatever that key does */
252 Player& tux = *(currentsector->player);
253 tux.key_event((SDLKey)keymap.up, UP);
254 tux.key_event((SDLKey)keymap.down, UP);
255 tux.key_event((SDLKey)keymap.left, UP);
256 tux.key_event((SDLKey)keymap.right, UP);
257 tux.key_event((SDLKey)keymap.jump, UP);
258 tux.key_event((SDLKey)keymap.power, UP);
260 Menu::set_current(game_menu);
261 Ticks::pause_start();
266 GameSession::process_events()
268 if (end_sequence != NO_ENDSEQUENCE)
270 Player& tux = *currentsector->player;
274 tux.input.right = DOWN;
277 if (int(last_x_pos) == int(tux.get_pos().x))
282 last_x_pos = tux.get_pos().x;
285 while (SDL_PollEvent(&event))
287 /* Check for menu-events, if the menu is shown */
290 Menu::current()->event(event);
297 case SDL_QUIT: /* Quit event - quit: */
298 Termination::abort("Received window close", "");
301 case SDL_KEYDOWN: /* A keypress! */
303 SDLKey key = event.key.keysym.sym;
307 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
315 case SDL_JOYBUTTONDOWN:
316 if (event.jbutton.button == joystick_keymap.start_button)
324 if(!Menu::current() && !game_pause)
328 while (SDL_PollEvent(&event))
330 /* Check for menu-events, if the menu is shown */
333 Menu::current()->event(event);
339 Player& tux = *currentsector->player;
343 case SDL_QUIT: /* Quit event - quit: */
344 Termination::abort("Received window close", "");
347 case SDL_KEYDOWN: /* A keypress! */
349 SDLKey key = event.key.keysym.sym;
351 if(tux.key_event(key,DOWN))
356 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
364 case SDL_KEYUP: /* A keyrelease! */
366 SDLKey key = event.key.keysym.sym;
368 if(tux.key_event(key, UP))
377 snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
378 tux.get_pos().x, tux.get_pos().y);
379 context->draw_text(white_text, buf,
380 Vector(0, screen->h - white_text->get_height()),
381 LEFT_ALLIGN, LAYER_FOREGROUND1);
382 context->do_drawing();
389 // "lifeup" cheat activates pause cause of the 'p'
390 // so work around to ignore it
391 if(compare_last(last_keys, "lifeu"))
402 Ticks::pause_start();
411 /* Check if chacrater is ASCII */
413 if((event.key.keysym.unicode & 0xFF80) == 0)
415 ch[0] = event.key.keysym.unicode & 0x7F;
418 last_keys.append(ch); // add to cheat keys
420 // Cheating words (the goal of this is really for debugging,
421 // but could be used for some cheating, nothing wrong with that)
422 if(compare_last(last_keys, "grow"))
427 if(compare_last(last_keys, "fire"))
430 tux.got_power = tux.FIRE_POWER;
433 if(compare_last(last_keys, "ice"))
436 tux.got_power = tux.ICE_POWER;
439 if(compare_last(last_keys, "lifeup"))
441 player_status.lives++;
444 if(compare_last(last_keys, "lifedown"))
446 player_status.lives--;
449 if(compare_last(last_keys, "grease"))
451 tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
454 if(compare_last(last_keys, "invincible"))
455 { // be invincle for the rest of the level
456 tux.invincible_timer.start(10000);
459 if(compare_last(last_keys, "shrink"))
461 tux.kill(tux.SHRINK);
464 if(compare_last(last_keys, "kill"))
465 { // kill Tux, but without losing a life
466 player_status.lives++;
470 if(compare_last(last_keys, "grid"))
471 { // toggle debug grid
472 debug_grid = !debug_grid;
475 if(compare_last(last_keys, "hover"))
476 { // toggle hover ability on/off
477 tux.enable_hover = !tux.enable_hover;
480 if(compare_last(last_keys, "gotoend"))
481 { // goes to the end of the level
483 (currentsector->solids->get_width()*32)
486 currentsector->camera->reset(
487 Vector(tux.get_pos().x, tux.get_pos().y));
490 // temporary to help player's choosing a flapping
491 if(compare_last(last_keys, "marek"))
493 tux.flapping_mode = Player::MAREK_FLAP;
496 if(compare_last(last_keys, "ricardo"))
498 tux.flapping_mode = Player::RICARDO_FLAP;
501 if(compare_last(last_keys, "ryan"))
503 tux.flapping_mode = Player::RYAN_FLAP;
508 case SDL_JOYAXISMOTION:
509 if (event.jaxis.axis == joystick_keymap.x_axis)
511 if (event.jaxis.value < -joystick_keymap.dead_zone)
513 tux.input.left = DOWN;
514 tux.input.right = UP;
516 else if (event.jaxis.value > joystick_keymap.dead_zone)
519 tux.input.right = DOWN;
523 tux.input.left = DOWN;
524 tux.input.right = DOWN;
527 else if (event.jaxis.axis == joystick_keymap.y_axis)
529 if (event.jaxis.value > joystick_keymap.dead_zone)
534 else if (event.jaxis.value < -joystick_keymap.dead_zone)
537 tux.input.down = DOWN;
542 tux.input.down = DOWN;
547 case SDL_JOYHATMOTION:
548 if(event.jhat.value & SDL_HAT_UP) {
551 } else if(event.jhat.value & SDL_HAT_DOWN) {
553 tux.input.down = DOWN;
554 } else if(event.jhat.value & SDL_HAT_LEFT) {
555 tux.input.left = DOWN;
556 tux.input.right = UP;
557 } else if(event.jhat.value & SDL_HAT_RIGHT) {
559 tux.input.right = DOWN;
560 } else if(event.jhat.value == SDL_HAT_CENTERED) {
562 tux.input.right = UP;
568 case SDL_JOYBUTTONDOWN:
569 if (event.jbutton.button == joystick_keymap.a_button)
570 tux.input.jump = DOWN;
571 else if (event.jbutton.button == joystick_keymap.b_button)
572 tux.input.fire = DOWN;
573 else if (event.jbutton.button == joystick_keymap.start_button)
576 case SDL_JOYBUTTONUP:
577 if (event.jbutton.button == joystick_keymap.a_button)
579 else if (event.jbutton.button == joystick_keymap.b_button)
592 GameSession::check_end_conditions()
594 Player* tux = currentsector->player;
597 if(end_sequence && endsequence_timer.check()) {
598 exit_status = ES_LEVEL_FINISHED;
599 global_stats += last_swap_stats; // add swap points stats
601 } else if (!end_sequence && tux->is_dead()) {
602 player_status.bonus = PlayerStatus::NO_BONUS;
604 if (player_status.lives < 0) { // No more lives!?
605 exit_status = ES_GAME_OVER;
606 } else { // Still has lives, so reset Tux to the levelstart
615 GameSession::action(float elapsed_time)
618 if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
620 // Update Tux and the World
621 currentsector->action(elapsed_time);
624 // respawning in new sector?
625 if(newsector != "" && newspawnpoint != "") {
626 Sector* sector = level->get_sector(newsector);
627 currentsector = sector;
628 currentsector->activate(newspawnpoint);
629 currentsector->play_music(LEVEL_MUSIC);
630 newsector = newspawnpoint = "";
637 currentsector->draw(*context);
638 drawstatus(*context);
642 int x = screen->h / 20;
643 for(int i = 0; i < x; ++i)
645 context->draw_filled_rect(
646 Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
647 -((pause_menu_frame * i)%screen->w)
648 ,(i*20+pause_menu_frame)%screen->h),
649 Vector(screen->w,10),
650 Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
652 context->draw_filled_rect(
653 Vector(0,0), Vector(screen->w, screen->h),
654 Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
655 context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
656 Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
660 sprintf(str1, _("Playing: "));
661 sprintf(str2, level->name.c_str());
663 context->draw_text(blue_text, str1,
664 Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
665 LEFT_ALLIGN, LAYER_FOREGROUND1+2);
666 context->draw_text(white_text, str2,
667 Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
668 LEFT_ALLIGN, LAYER_FOREGROUND1+2);
673 Menu::current()->draw(*context);
674 mouse_cursor->draw(*context);
677 context->do_drawing();
681 GameSession::process_menu()
683 Menu* menu = Menu::current();
688 if(menu == game_menu)
690 switch (game_menu->check())
695 case MNID_ABORTLEVEL:
697 exit_status = ES_LEVEL_ABORT;
701 else if(menu == options_menu)
703 process_options_menu();
705 else if(menu == load_game_menu )
707 process_load_game_menu();
712 GameSession::ExitStatus
715 Menu::set_current(0);
720 // Eat unneeded events
722 while(SDL_PollEvent(&event))
727 Uint32 lastticks = SDL_GetTicks();
728 fps_ticks = SDL_GetTicks();
730 while (exit_status == ES_NONE) {
731 Uint32 ticks = SDL_GetTicks();
732 float elapsed_time = float(ticks - lastticks) / 1000.;
734 global_time += elapsed_time;
738 if(elapsed_time > .025)
742 currentsector->player->input.old_fire = currentsector->player->input.fire;
743 currentsector->player->input.old_up = currentsector->player->input.old_up;
748 // Update the world state and all objects in the world
749 // Do that with a constante time-delta so that the game will run
750 // determistic and not different on different machines
751 if(!game_pause && !Menu::current())
754 check_end_conditions();
755 if (end_sequence == ENDSEQUENCE_RUNNING)
756 action(elapsed_time/2);
757 else if(end_sequence == NO_ENDSEQUENCE)
758 action(elapsed_time);
768 /* Time stops in pause mode */
769 if(game_pause || Menu::current())
774 //frame_rate.update();
777 if (time_left.check() && currentsector->player->dying == DYING_NOT
779 currentsector->player->kill(Player::KILL);
782 if(currentsector->player->invincible_timer.check() && !end_sequence)
784 currentsector->play_music(HERRING_MUSIC);
786 /* are we low on time ? */
787 else if (time_left.get_timeleft() < TIME_WARNING && !end_sequence)
789 currentsector->play_music(HURRYUP_MUSIC);
791 /* or just normal music? */
792 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
794 currentsector->play_music(LEVEL_MUSIC);
797 /* Calculate frames per second */
802 if(SDL_GetTicks() - fps_ticks >= 500)
804 fps_fps = (float) fps_cnt / .5;
806 fps_ticks = SDL_GetTicks();
815 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
818 newspawnpoint = spawnpoint;
822 GameSession::start_sequence(const std::string& sequencename)
824 if(sequencename == "endsequence") {
828 end_sequence = ENDSEQUENCE_RUNNING;
829 endsequence_timer.start(7.0); // 7 seconds until we finish the map
831 SoundManager::get()->play_music(level_end_song, 0);
832 currentsector->player->invincible_timer.start(7.0);
834 // add left time to stats
835 global_stats.set_points(TIME_NEEDED_STAT,
836 int(time_left.get_period() - time_left.get_timeleft()));
838 if(level->get_end_sequence_type() == Level::FIREWORKS_ENDSEQ_ANIM) {
839 currentsector->add_object(new Fireworks());
842 std::cout << "Unknown sequence '" << sequencename << "'.\n";
848 GameSession::drawstatus(DrawingContext& context)
852 snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
853 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
854 context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
856 if(st_gl_mode == ST_GL_TEST)
858 context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
859 LEFT_ALLIGN, LAYER_FOREGROUND1);
862 if(time_left.check()) {
863 context.draw_text(white_text, _("TIME's UP"), Vector(screen->w/2, 0),
864 CENTER_ALLIGN, LAYER_FOREGROUND1);
865 } else if (time_left.get_timeleft() > TIME_WARNING
866 || int(global_time * 2.5) % 2) {
867 sprintf(str, " %d", int(time_left.get_timeleft()));
868 context.draw_text(white_text, _("TIME"),
869 Vector(screen->w/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
870 context.draw_text(gold_text, str,
871 Vector(screen->w/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
874 sprintf(str, " %d", player_status.distros);
875 context.draw_text(white_text, _("COINS"),
876 Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width(" 99"), 0),
877 LEFT_ALLIGN, LAYER_FOREGROUND1);
878 context.draw_text(gold_text, str,
879 Vector(screen->w - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
881 if (player_status.lives >= 5)
883 sprintf(str, "%dx", player_status.lives);
884 float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
885 context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
886 context.draw_surface(tux_life, Vector(screen->w - 16, 20),
891 for(int i= 0; i < player_status.lives; ++i)
892 context.draw_surface(tux_life,
893 Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
897 context.draw_text(white_text, _("LIVES"),
898 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 20),
899 LEFT_ALLIGN, LAYER_FOREGROUND1);
903 sprintf(str, "%2.1f", fps_fps);
904 context.draw_text(white_text, "FPS",
905 Vector(screen->w - white_text->get_text_width("FPS "), 40),
906 LEFT_ALLIGN, LAYER_FOREGROUND1);
907 context.draw_text(gold_text, str,
908 Vector(screen->w-4*16, 40), LEFT_ALLIGN, LAYER_FOREGROUND1);
913 GameSession::drawresultscreen()
917 DrawingContext context;
918 for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
919 i != currentsector->gameobjects.end(); ++i) {
920 Background* background = dynamic_cast<Background*> (*i);
922 background->draw(context);
926 context.draw_text(blue_text, _("Result:"), Vector(screen->w/2, 200),
927 CENTER_ALLIGN, LAYER_FOREGROUND1);
929 sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
930 context.draw_text(gold_text, str, Vector(screen->w/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
932 sprintf(str, _("COINS: %d"), player_status.distros);
933 context.draw_text(gold_text, str, Vector(screen->w/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
935 context.do_drawing();
938 wait_for_event(event,2000,5000,true);
941 std::string slotinfo(int slot)
944 std::string slotfile;
946 std::stringstream stream;
948 slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
950 lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
953 LispReader reader(lisp_cdr(savegame));
954 reader.read_string("title", title);
958 if (access(slotfile.c_str(), F_OK) == 0)
961 tmp = "Slot " + stream.str() + " - " + title;
963 tmp = "Slot " + stream.str() + " - Savegame";
966 tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
971 bool process_load_game_menu()
973 int slot = load_game_menu->check();
975 if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
977 std::stringstream stream;
979 std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
981 if (access(slotfile.c_str(), F_OK) != 0)
983 shrink_fade(Vector(screen->w/2,screen->h/2), 600);
988 DrawingContext context;
989 context.draw_text(white_text, "Loading...",
990 Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
991 context.do_drawing();
993 WorldMapNS::WorldMap worldmap;
995 worldmap.set_map_filename("icyisland.stwm");
996 // Load the game or at least set the savegame_file variable
997 worldmap.loadgame(slotfile);
1001 Menu::set_current(main_menu);
1003 Ticks::pause_stop();