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.
36 #include <sys/types.h>
41 #include "app/globals.h"
43 #include "video/screen.h"
44 #include "app/setup.h"
45 #include "high_scores.h"
53 #include "collision.h"
55 #include "particlesystem.h"
56 #include "resources.h"
57 #include "background.h"
59 #include "app/gettext.h"
65 GameSession* GameSession::current_ = 0;
67 bool compare_last(std::string& haystack, std::string needle)
69 int haystack_size = haystack.size();
70 int needle_size = needle.size();
72 if(haystack_size < needle_size)
75 if(haystack.compare(haystack_size-needle_size, needle_size, needle) == 0)
80 GameSession::GameSession(const std::string& levelname_, int mode, bool flip_level_)
81 : level(0), currentsector(0), st_gl_mode(mode),
82 end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_)
86 global_frame_counter = 0;
91 frame_timer.init(true);
92 random_timer.init(true);
93 frame_rate.set_fps(100);
95 context = new DrawingContext();
104 GameSession::restart_level()
107 exit_status = ES_NONE;
108 end_sequence = NO_ENDSEQUENCE;
110 fps_timer.init(true);
111 frame_timer.init(true);
112 random_timer.init(true);
116 Vector tux_pos = Vector(-1,-1);
118 { // Tux has lost a life, so we try to respawn him at the nearest reset point
119 tux_pos = currentsector->player->base;
126 level->load(levelname);
128 level->do_vertical_flip();
130 currentsector = level->get_sector("main");
132 Termination::abort("Level has no main sector.", "");
133 currentsector->activate("main");
135 // Set Tux to the nearest reset point
138 tux_pos = currentsector->get_best_spawn_point(tux_pos);
139 currentsector->player->base.x = tux_pos.x;
140 currentsector->player->base.y = tux_pos.y;
142 // has to reset camera on swapping
143 currentsector->camera->reset(Vector(currentsector->player->base.x,
144 currentsector->player->base.y));
147 if (st_gl_mode != ST_GL_DEMO_GAME)
149 if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
153 time_left.init(true);
155 currentsector->play_music(LEVEL_MUSIC);
158 GameSession::~GameSession()
165 GameSession::levelintro(void)
167 SoundManager::get()->halt_music();
171 DrawingContext context;
172 currentsector->background->draw(context);
174 context.draw_text_center(gold_text, level->get_name(), Vector(0, 220),
177 sprintf(str, "TUX x %d", player_status.lives);
178 context.draw_text_center(white_text, str, Vector(0, 240),
181 if(level->get_author().size())
182 context.draw_text_center(white_small_text,
183 std::string(_("by ")) + level->get_author(),
184 Vector(0, 400), LAYER_FOREGROUND1);
188 context.draw_text_center(white_text,
189 _("Level Vertically Flipped!"),
190 Vector(0, 310), LAYER_FOREGROUND1);
192 context.do_drawing();
195 wait_for_event(event,1000,3000,true);
200 GameSession::start_timers()
202 time_left.start(level->time_left*1000);
208 GameSession::on_escape_press()
210 if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
211 return; // don't let the player open the menu, when he is dying
216 if(st_gl_mode == ST_GL_TEST)
218 exit_status = ES_LEVEL_ABORT;
220 else if (!Menu::current())
222 /* Tell Tux that the keys are all down, otherwise
223 it could have nasty bugs, like going allways to the right
224 or whatever that key does */
225 Player& tux = *(currentsector->player);
226 tux.key_event((SDLKey)keymap.jump, UP);
227 tux.key_event((SDLKey)keymap.duck, UP);
228 tux.key_event((SDLKey)keymap.left, UP);
229 tux.key_event((SDLKey)keymap.right, UP);
230 tux.key_event((SDLKey)keymap.fire, UP);
232 Menu::set_current(game_menu);
233 Ticks::pause_start();
238 GameSession::process_events()
240 if (end_sequence != NO_ENDSEQUENCE)
242 Player& tux = *currentsector->player;
246 tux.input.right = DOWN;
249 if (int(last_x_pos) == int(tux.base.x))
254 last_x_pos = tux.base.x;
257 while (SDL_PollEvent(&event))
259 /* Check for menu-events, if the menu is shown */
262 Menu::current()->event(event);
269 case SDL_QUIT: /* Quit event - quit: */
270 Termination::abort("Received window close", "");
273 case SDL_KEYDOWN: /* A keypress! */
275 SDLKey key = event.key.keysym.sym;
279 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
287 case SDL_JOYBUTTONDOWN:
288 if (event.jbutton.button == joystick_keymap.start_button)
296 if(!Menu::current() && !game_pause)
300 while (SDL_PollEvent(&event))
302 /* Check for menu-events, if the menu is shown */
305 Menu::current()->event(event);
311 Player& tux = *currentsector->player;
315 case SDL_QUIT: /* Quit event - quit: */
316 Termination::abort("Received window close", "");
319 case SDL_KEYDOWN: /* A keypress! */
321 SDLKey key = event.key.keysym.sym;
323 if(tux.key_event(key,DOWN))
328 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
336 case SDL_KEYUP: /* A keyrelease! */
338 SDLKey key = event.key.keysym.sym;
340 if(tux.key_event(key, UP))
349 snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
350 tux.base.x, tux.base.y);
351 context->draw_text(white_text, buf,
352 Vector(0, screen->h - white_text->get_height()),
354 context->do_drawing();
369 Ticks::pause_start();
378 /* Check if chacrater is ASCII */
380 if((event.key.keysym.unicode & 0xFF80) == 0)
382 ch[0] = event.key.keysym.unicode & 0x7F;
385 last_keys.append(ch); // add to cheat keys
387 // Cheating words (the goal of this is really for debugging,
388 // but could be used for some cheating, nothing wrong with that)
389 if(compare_last(last_keys, "grow"))
394 if(compare_last(last_keys, "fire"))
397 tux.got_power = tux.FIRE_POWER;
400 if(compare_last(last_keys, "ice"))
403 tux.got_power = tux.ICE_POWER;
406 if(compare_last(last_keys, "lifeup"))
408 player_status.lives++;
410 // "lifeup" activates pause cause of the 'p'
411 // so work around to ignore it
420 Ticks::pause_start();
423 if(compare_last(last_keys, "lifedown"))
425 player_status.lives--;
428 if(compare_last(last_keys, "invincible"))
429 { // be invincle for the rest of the level
430 tux.invincible_timer.start(time_left.get_left());
436 case SDL_JOYAXISMOTION:
437 if (event.jaxis.axis == joystick_keymap.x_axis)
439 if (event.jaxis.value < -joystick_keymap.dead_zone)
441 tux.input.left = DOWN;
442 tux.input.right = UP;
444 else if (event.jaxis.value > joystick_keymap.dead_zone)
447 tux.input.right = DOWN;
451 tux.input.left = DOWN;
452 tux.input.right = DOWN;
455 else if (event.jaxis.axis == joystick_keymap.y_axis)
457 if (event.jaxis.value > joystick_keymap.dead_zone)
458 tux.input.down = DOWN;
459 else if (event.jaxis.value < -joystick_keymap.dead_zone)
466 case SDL_JOYHATMOTION:
467 if(event.jhat.value & SDL_HAT_UP) {
470 } else if(event.jhat.value & SDL_HAT_DOWN) {
472 tux.input.down = DOWN;
473 } else if(event.jhat.value & SDL_HAT_LEFT) {
474 tux.input.left = DOWN;
475 tux.input.right = UP;
476 } else if(event.jhat.value & SDL_HAT_RIGHT) {
478 tux.input.right = DOWN;
479 } else if(event.jhat.value == SDL_HAT_CENTERED) {
481 tux.input.right = UP;
487 case SDL_JOYBUTTONDOWN:
488 if (event.jbutton.button == joystick_keymap.a_button)
490 else if (event.jbutton.button == joystick_keymap.b_button)
491 tux.input.fire = DOWN;
492 else if (event.jbutton.button == joystick_keymap.start_button)
495 case SDL_JOYBUTTONUP:
496 if (event.jbutton.button == joystick_keymap.a_button)
498 else if (event.jbutton.button == joystick_keymap.b_button)
511 GameSession::check_end_conditions()
513 Player* tux = currentsector->player;
516 Tile* endtile = collision_goal(tux->base);
518 if(end_sequence && !endsequence_timer.check())
520 exit_status = ES_LEVEL_FINISHED;
523 else if(end_sequence == ENDSEQUENCE_RUNNING && endtile && endtile->data >= 1)
525 end_sequence = ENDSEQUENCE_WAITING;
527 else if(!end_sequence && endtile && endtile->data == 0)
529 end_sequence = ENDSEQUENCE_RUNNING;
530 random_timer.start(200); // start 1st firework
532 SoundManager::get()->play_music(level_end_song, 0);
533 endsequence_timer.start(7000); // 5 seconds until we finish the map
534 tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
536 else if (!end_sequence && tux->is_dead())
538 player_status.bonus = PlayerStatus::NO_BONUS;
540 if (player_status.lives < 0)
542 exit_status = ES_GAME_OVER;
545 { // Still has lives, so reset Tux to the levelstart
554 GameSession::action(double frame_ratio)
556 if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
558 // Update Tux and the World
559 currentsector->action(frame_ratio);
562 // respawning in new sector?
563 if(newsector != "" && newspawnpoint != "") {
564 Sector* sector = level->get_sector(newsector);
565 currentsector = sector;
566 currentsector->activate(newspawnpoint);
567 currentsector->play_music(LEVEL_MUSIC);
568 newsector = newspawnpoint = "";
571 // on end sequence make a few fireworks
572 if(end_sequence == ENDSEQUENCE_RUNNING && !random_timer.check())
574 Vector epicenter = currentsector->camera->get_translation();
575 epicenter.x += screen->w * ((float)rand() / RAND_MAX);
576 epicenter.y += (screen->h/2) * ((float)rand() / RAND_MAX);
578 int red = rand() % 255; // calculate firework color
579 int green = rand() % red;
580 currentsector->add_particles(epicenter, Vector(1.4,1.4), Vector(0,0),
581 45, Color(red,green,0), 3, 1300);
583 SoundManager::get()->play_sound(IDToSound(SND_FIREWORKS));
584 random_timer.start(rand() % 400 + 600); // next firework
591 currentsector->draw(*context);
592 drawstatus(*context);
596 int x = screen->h / 20;
597 for(int i = 0; i < x; ++i)
599 context->draw_filled_rect(
600 Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
601 -((pause_menu_frame * i)%screen->w)
602 ,(i*20+pause_menu_frame)%screen->h),
603 Vector(screen->w,10),
604 Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
606 context->draw_filled_rect(
607 Vector(0,0), Vector(screen->w, screen->h),
608 Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
609 context->draw_text_center(blue_text, _("PAUSE - Press 'P' To Play"),
610 Vector(0, 230), LAYER_FOREGROUND1+2);
614 sprintf(str1, _("Playing: "));
615 sprintf(str2, level->name.c_str());
617 context->draw_text(blue_text, str1,
618 Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
619 LAYER_FOREGROUND1+2);
620 context->draw_text(white_text, str2,
621 Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
622 LAYER_FOREGROUND1+2);
627 Menu::current()->draw(*context);
628 mouse_cursor->draw(*context);
631 context->do_drawing();
635 GameSession::process_menu()
637 Menu* menu = Menu::current();
642 if(menu == game_menu)
644 switch (game_menu->check())
649 case MNID_ABORTLEVEL:
651 exit_status = ES_LEVEL_ABORT;
655 else if(menu == options_menu)
657 process_options_menu();
659 else if(menu == load_game_menu )
661 process_load_game_menu();
666 GameSession::ExitStatus
669 Menu::set_current(0);
676 // Eat unneeded events
678 while (SDL_PollEvent(&event)) {}
682 while (exit_status == ES_NONE)
684 /* Calculate the movement-factor */
685 double frame_ratio = frame_rate.get();
687 if(!frame_timer.check())
689 frame_timer.start(25);
690 ++global_frame_counter;
694 currentsector->player->input.old_fire
695 = currentsector->player->input.fire;
700 // Update the world state and all objects in the world
701 // Do that with a constante time-delta so that the game will run
702 // determistic and not different on different machines
703 if(!game_pause && !Menu::current())
706 check_end_conditions();
707 if (end_sequence == ENDSEQUENCE_RUNNING)
708 action(frame_ratio/2);
709 else if(end_sequence == NO_ENDSEQUENCE)
720 /* Time stops in pause mode */
721 if(game_pause || Menu::current())
729 if (!time_left.check() && currentsector->player->dying == DYING_NOT
731 currentsector->player->kill(Player::KILL);
734 if(currentsector->player->invincible_timer.check() && !end_sequence)
736 currentsector->play_music(HERRING_MUSIC);
738 /* are we low on time ? */
739 else if (time_left.get_left() < TIME_WARNING && !end_sequence)
741 currentsector->play_music(HURRYUP_MUSIC);
743 /* or just normal music? */
744 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
746 currentsector->play_music(LEVEL_MUSIC);
749 /* Calculate frames per second */
753 fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
755 if(!fps_timer.check())
757 fps_timer.start(1000);
767 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
770 newspawnpoint = spawnpoint;
773 /* Bounce a brick: */
774 void bumpbrick(float x, float y)
776 Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
777 (int)(y / 32) * 32));
779 SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
784 GameSession::drawstatus(DrawingContext& context)
788 snprintf(str, 60, " %d", player_status.score);
789 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
790 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
792 if(st_gl_mode == ST_GL_TEST)
794 context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
798 if(!time_left.check()) {
799 context.draw_text_center(white_text, _("TIME's UP"), Vector(0, 0),
801 } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
802 sprintf(str, " %d", time_left.get_left() / 1000 );
803 context.draw_text_center(white_text, _("TIME"),
804 Vector(0, 0), LAYER_FOREGROUND1);
805 context.draw_text_center(gold_text, str,
806 Vector(4*16, 0), LAYER_FOREGROUND1);
809 sprintf(str, " %d", player_status.distros);
810 context.draw_text(white_text, _("COINS"),
811 Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width(" 99"), 0),
813 context.draw_text(gold_text, str,
814 Vector(screen->w - gold_text->get_text_width(" 99"), 0),LAYER_FOREGROUND1);
816 if (player_status.lives >= 5)
818 sprintf(str, "%dx", player_status.lives);
819 float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
820 context.draw_text(gold_text, str, Vector(x, 20), LAYER_FOREGROUND1);
821 context.draw_surface(tux_life, Vector(screen->w - 16, 20),
826 for(int i= 0; i < player_status.lives; ++i)
827 context.draw_surface(tux_life,
828 Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
832 context.draw_text(white_text, _("LIVES"),
833 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 20),
838 sprintf(str, "%2.1f", fps_fps);
839 context.draw_text(white_text, "FPS",
840 Vector(screen->w - white_text->get_text_width("FPS "), 40),
842 context.draw_text(gold_text, str,
843 Vector(screen->w-4*16, 40), LAYER_FOREGROUND1);
848 GameSession::drawresultscreen(void)
852 DrawingContext context;
853 currentsector->background->draw(context);
855 context.draw_text_center(blue_text, _("Result:"), Vector(0, 200),
858 sprintf(str, _("SCORE: %d"), player_status.score);
859 context.draw_text_center(gold_text, str, Vector(0, 224), LAYER_FOREGROUND1);
861 sprintf(str, _("COINS: %d"), player_status.distros);
862 context.draw_text_center(gold_text, str, Vector(0, 256), LAYER_FOREGROUND1);
864 context.do_drawing();
867 wait_for_event(event,2000,5000,true);
870 std::string slotinfo(int slot)
873 std::string slotfile;
875 std::stringstream stream;
877 slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
879 lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
882 LispReader reader(lisp_cdr(savegame));
883 reader.read_string("title", title);
887 if (access(slotfile.c_str(), F_OK) == 0)
890 tmp = "Slot " + stream.str() + " - " + title;
892 tmp = "Slot " + stream.str() + " - Savegame";
895 tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
900 bool process_load_game_menu()
902 int slot = load_game_menu->check();
904 if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
906 std::stringstream stream;
908 std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
910 if (access(slotfile.c_str(), F_OK) != 0)
915 // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
918 DrawingContext context;
919 context.draw_text_center(white_text, "Loading...",
920 Vector(0, screen->h/2), LAYER_FOREGROUND1);
921 context.do_drawing();
923 WorldMapNS::WorldMap worldmap;
925 // Load the game or at least set the savegame_file variable
926 worldmap.loadgame(slotfile);
930 Menu::set_current(main_menu);