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"
54 #include "collision.h"
56 #include "particlesystem.h"
57 #include "resources.h"
58 #include "background.h"
60 #include "app/gettext.h"
65 #include "statistics.h"
67 #include "object/fireworks.h"
69 GameSession* GameSession::current_ = 0;
71 bool compare_last(std::string& haystack, std::string needle)
73 int haystack_size = haystack.size();
74 int needle_size = needle.size();
76 if(haystack_size < needle_size)
79 if(haystack.compare(haystack_size-needle_size, needle_size, needle) == 0)
84 GameSession::GameSession(const std::string& levelname_, int mode,
85 bool flip_level_, Statistics* statistics)
86 : level(0), currentsector(0), st_gl_mode(mode),
87 end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_),
88 best_level_statistics(statistics)
92 global_frame_counter = 0;
96 context = new DrawingContext();
101 last_swap_point = Vector(-1, -1);
102 last_swap_stats.reset();
108 GameSession::restart_level()
111 exit_status = ES_NONE;
112 end_sequence = NO_ENDSEQUENCE;
117 Vector tux_pos = Vector(-1,-1);
120 // Tux has lost a life, so we try to respawn him at the nearest reset point
121 tux_pos = currentsector->player->base;
129 level->load(levelname);
131 level->do_vertical_flip();
133 global_stats.reset();
134 global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
135 global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
136 global_stats.set_total_points(TIME_NEEDED_STAT, level->timelimit);
138 currentsector = level->get_sector("main");
140 Termination::abort("Level has no main sector.", "");
141 currentsector->activate("main");
144 // Set Tux to the nearest reset point
147 tux_pos = currentsector->get_best_spawn_point(tux_pos);
149 if(last_swap_point.x > tux_pos.x)
150 tux_pos = last_swap_point;
151 else // new swap point
153 last_swap_point = tux_pos;
155 last_swap_stats += global_stats;
158 currentsector->player->base.x = tux_pos.x;
159 currentsector->player->base.y = tux_pos.y;
161 // has to reset camera on swapping
162 currentsector->camera->reset(Vector(currentsector->player->base.x,
163 currentsector->player->base.y));
167 if (st_gl_mode != ST_GL_DEMO_GAME)
169 if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
174 currentsector->play_music(LEVEL_MUSIC);
177 GameSession::~GameSession()
184 GameSession::levelintro(void)
186 SoundManager::get()->halt_music();
190 DrawingContext context;
191 if(currentsector->background)
192 currentsector->background->draw(context);
194 // context.draw_text(gold_text, level->get_name(), Vector(screen->w/2, 160),
195 // CENTER_ALLIGN, LAYER_FOREGROUND1);
196 context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
199 sprintf(str, "TUX x %d", player_status.lives);
200 context.draw_text(white_text, str, Vector(screen->w/2, 210),
201 CENTER_ALLIGN, LAYER_FOREGROUND1);
203 if(level->get_author().size())
204 context.draw_text(white_small_text,
205 std::string(_("by ")) + level->get_author(),
206 Vector(screen->w/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
210 context.draw_text(white_text,
211 _("Level Vertically Flipped!"),
212 Vector(screen->w/2, 310), CENTER_ALLIGN, LAYER_FOREGROUND1);
214 if(best_level_statistics != NULL)
215 best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
217 context.do_drawing();
220 wait_for_event(event,1000,3000,true);
225 GameSession::start_timers()
227 time_left.start(level->timelimit);
232 GameSession::on_escape_press()
234 if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
235 return; // don't let the player open the menu, when he is dying
240 if(st_gl_mode == ST_GL_TEST)
242 exit_status = ES_LEVEL_ABORT;
244 else if (!Menu::current())
246 /* Tell Tux that the keys are all down, otherwise
247 it could have nasty bugs, like going allways to the right
248 or whatever that key does */
249 Player& tux = *(currentsector->player);
250 tux.key_event((SDLKey)keymap.up, UP);
251 tux.key_event((SDLKey)keymap.down, UP);
252 tux.key_event((SDLKey)keymap.left, UP);
253 tux.key_event((SDLKey)keymap.right, UP);
254 tux.key_event((SDLKey)keymap.jump, UP);
255 tux.key_event((SDLKey)keymap.power, UP);
257 Menu::set_current(game_menu);
258 Ticks::pause_start();
263 GameSession::process_events()
265 if (end_sequence != NO_ENDSEQUENCE)
267 Player& tux = *currentsector->player;
271 tux.input.right = DOWN;
274 if (int(last_x_pos) == int(tux.get_pos().x))
279 last_x_pos = tux.get_pos().x;
282 while (SDL_PollEvent(&event))
284 /* Check for menu-events, if the menu is shown */
287 Menu::current()->event(event);
294 case SDL_QUIT: /* Quit event - quit: */
295 Termination::abort("Received window close", "");
298 case SDL_KEYDOWN: /* A keypress! */
300 SDLKey key = event.key.keysym.sym;
304 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
312 case SDL_JOYBUTTONDOWN:
313 if (event.jbutton.button == joystick_keymap.start_button)
321 if(!Menu::current() && !game_pause)
325 while (SDL_PollEvent(&event))
327 /* Check for menu-events, if the menu is shown */
330 Menu::current()->event(event);
336 Player& tux = *currentsector->player;
340 case SDL_QUIT: /* Quit event - quit: */
341 Termination::abort("Received window close", "");
344 case SDL_KEYDOWN: /* A keypress! */
346 SDLKey key = event.key.keysym.sym;
348 if(tux.key_event(key,DOWN))
353 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
361 case SDL_KEYUP: /* A keyrelease! */
363 SDLKey key = event.key.keysym.sym;
365 if(tux.key_event(key, UP))
374 snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
375 tux.get_pos().x, tux.get_pos().y);
376 context->draw_text(white_text, buf,
377 Vector(0, screen->h - white_text->get_height()),
378 LEFT_ALLIGN, LAYER_FOREGROUND1);
379 context->do_drawing();
386 // "lifeup" cheat activates pause cause of the 'p'
387 // so work around to ignore it
388 if(compare_last(last_keys, "lifeu"))
399 Ticks::pause_start();
408 /* Check if chacrater is ASCII */
410 if((event.key.keysym.unicode & 0xFF80) == 0)
412 ch[0] = event.key.keysym.unicode & 0x7F;
415 last_keys.append(ch); // add to cheat keys
417 // Cheating words (the goal of this is really for debugging,
418 // but could be used for some cheating, nothing wrong with that)
419 if(compare_last(last_keys, "grow"))
424 if(compare_last(last_keys, "fire"))
427 tux.got_power = tux.FIRE_POWER;
430 if(compare_last(last_keys, "ice"))
433 tux.got_power = tux.ICE_POWER;
436 if(compare_last(last_keys, "lifeup"))
438 player_status.lives++;
441 if(compare_last(last_keys, "lifedown"))
443 player_status.lives--;
446 if(compare_last(last_keys, "grease"))
448 tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
451 if(compare_last(last_keys, "invincible"))
452 { // be invincle for the rest of the level
453 tux.invincible_timer.start(10000);
456 if(compare_last(last_keys, "shrink"))
458 tux.kill(tux.SHRINK);
461 if(compare_last(last_keys, "kill"))
462 { // kill Tux, but without losing a life
463 player_status.lives++;
467 if(compare_last(last_keys, "hover"))
468 { // toggle hover ability on/off
469 tux.enable_hover = !tux.enable_hover;
472 if(compare_last(last_keys, "gotoend"))
473 { // goes to the end of the level
475 (currentsector->solids->get_width()*32)
478 currentsector->camera->reset(
479 Vector(tux.get_pos().x, tux.get_pos().y));
482 // temporary to help player's choosing a flapping
483 if(compare_last(last_keys, "marek"))
485 tux.flapping_mode = Player::MAREK_FLAP;
488 if(compare_last(last_keys, "ricardo"))
490 tux.flapping_mode = Player::RICARDO_FLAP;
493 if(compare_last(last_keys, "ryan"))
495 tux.flapping_mode = Player::RYAN_FLAP;
500 case SDL_JOYAXISMOTION:
501 if (event.jaxis.axis == joystick_keymap.x_axis)
503 if (event.jaxis.value < -joystick_keymap.dead_zone)
505 tux.input.left = DOWN;
506 tux.input.right = UP;
508 else if (event.jaxis.value > joystick_keymap.dead_zone)
511 tux.input.right = DOWN;
515 tux.input.left = DOWN;
516 tux.input.right = DOWN;
519 else if (event.jaxis.axis == joystick_keymap.y_axis)
521 if (event.jaxis.value > joystick_keymap.dead_zone)
526 else if (event.jaxis.value < -joystick_keymap.dead_zone)
529 tux.input.down = DOWN;
534 tux.input.down = DOWN;
539 case SDL_JOYHATMOTION:
540 if(event.jhat.value & SDL_HAT_UP) {
543 } else if(event.jhat.value & SDL_HAT_DOWN) {
545 tux.input.down = DOWN;
546 } else if(event.jhat.value & SDL_HAT_LEFT) {
547 tux.input.left = DOWN;
548 tux.input.right = UP;
549 } else if(event.jhat.value & SDL_HAT_RIGHT) {
551 tux.input.right = DOWN;
552 } else if(event.jhat.value == SDL_HAT_CENTERED) {
554 tux.input.right = UP;
560 case SDL_JOYBUTTONDOWN:
561 if (event.jbutton.button == joystick_keymap.a_button)
562 tux.input.jump = DOWN;
563 else if (event.jbutton.button == joystick_keymap.b_button)
564 tux.input.fire = DOWN;
565 else if (event.jbutton.button == joystick_keymap.start_button)
568 case SDL_JOYBUTTONUP:
569 if (event.jbutton.button == joystick_keymap.a_button)
571 else if (event.jbutton.button == joystick_keymap.b_button)
584 GameSession::check_end_conditions()
586 Player* tux = currentsector->player;
589 if(end_sequence && endsequence_timer.check()) {
590 exit_status = ES_LEVEL_FINISHED;
591 global_stats += last_swap_stats; // add swap points stats
593 } else if (!end_sequence && tux->is_dead()) {
594 player_status.bonus = PlayerStatus::NO_BONUS;
596 if (player_status.lives < 0) { // No more lives!?
597 exit_status = ES_GAME_OVER;
598 } else { // Still has lives, so reset Tux to the levelstart
607 GameSession::action(float elapsed_time)
610 if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
612 // Update Tux and the World
613 currentsector->action(elapsed_time);
616 // respawning in new sector?
617 if(newsector != "" && newspawnpoint != "") {
618 Sector* sector = level->get_sector(newsector);
619 currentsector = sector;
620 currentsector->activate(newspawnpoint);
621 currentsector->play_music(LEVEL_MUSIC);
622 newsector = newspawnpoint = "";
629 currentsector->draw(*context);
630 drawstatus(*context);
634 int x = screen->h / 20;
635 for(int i = 0; i < x; ++i)
637 context->draw_filled_rect(
638 Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
639 -((pause_menu_frame * i)%screen->w)
640 ,(i*20+pause_menu_frame)%screen->h),
641 Vector(screen->w,10),
642 Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
644 context->draw_filled_rect(
645 Vector(0,0), Vector(screen->w, screen->h),
646 Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
647 context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
648 Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
652 sprintf(str1, _("Playing: "));
653 sprintf(str2, level->name.c_str());
655 context->draw_text(blue_text, str1,
656 Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
657 LEFT_ALLIGN, LAYER_FOREGROUND1+2);
658 context->draw_text(white_text, str2,
659 Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
660 LEFT_ALLIGN, LAYER_FOREGROUND1+2);
665 Menu::current()->draw(*context);
666 mouse_cursor->draw(*context);
669 context->do_drawing();
673 GameSession::process_menu()
675 Menu* menu = Menu::current();
680 if(menu == game_menu)
682 switch (game_menu->check())
687 case MNID_ABORTLEVEL:
689 exit_status = ES_LEVEL_ABORT;
693 else if(menu == options_menu)
695 process_options_menu();
697 else if(menu == load_game_menu )
699 process_load_game_menu();
704 GameSession::ExitStatus
707 Menu::set_current(0);
712 // Eat unneeded events
714 while(SDL_PollEvent(&event))
719 Uint32 lastticks = SDL_GetTicks();
720 fps_ticks = SDL_GetTicks();
722 frame_timer.start(.025, true);
723 while (exit_status == ES_NONE) {
724 Uint32 ticks = SDL_GetTicks();
725 float elapsed_time = float(ticks - lastticks) / 1000.;
726 global_time += elapsed_time;
729 if(elapsed_time > .05)
732 if(frame_timer.check()) {
733 ++global_frame_counter;
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 || (global_frame_counter % 10) < 5) {
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);