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"
64 GameSession* GameSession::current_ = 0;
66 GameSession::GameSession(const std::string& levelname_, int mode, bool flip_level_)
67 : level(0), currentsector(0), st_gl_mode(mode),
68 end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_)
72 global_frame_counter = 0;
77 frame_timer.init(true);
78 frame_rate.set_fps(100);
80 context = new DrawingContext();
89 GameSession::restart_level()
92 exit_status = ES_NONE;
93 end_sequence = NO_ENDSEQUENCE;
96 frame_timer.init(true);
101 { // Tux has lost a life, so we try to respawn him at the nearest reset point
102 old_x_pos = world->get_tux()->base.x;
110 level->load(levelname);
112 level->do_vertical_flip();
113 currentsector = level->get_sector("main");
115 Termination::abort("Level has no main sector.", "");
116 currentsector->activate("main");
119 // Set Tux to the nearest reset point
122 ResetPoint best_reset_point = { -1, -1 };
123 for(std::vector<ResetPoint>::iterator i = get_level()->reset_points.begin();
124 i != get_level()->reset_points.end(); ++i)
126 if (i->x < old_x_pos && best_reset_point.x < i->x)
127 best_reset_point = *i;
130 if (best_reset_point.x != -1)
132 world->get_tux()->base.x = best_reset_point.x;
133 world->get_tux()->base.y = best_reset_point.y;
138 if (st_gl_mode != ST_GL_DEMO_GAME)
140 if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
144 time_left.init(true);
146 currentsector->play_music(LEVEL_MUSIC);
149 GameSession::~GameSession()
156 GameSession::levelintro(void)
158 SoundManager::get()->halt_music();
162 DrawingContext context;
163 currentsector->background->draw(context);
165 context.draw_text_center(gold_text, level->get_name(), Vector(0, 220),
168 sprintf(str, "TUX x %d", player_status.lives);
169 context.draw_text_center(white_text, str, Vector(0, 240),
172 if(level->get_author().size())
173 context.draw_text_center(white_small_text,
174 std::string(_("by ")) + level->get_author(),
175 Vector(0, 400), LAYER_FOREGROUND1);
179 context.draw_text_center(white_text,
180 _("Level Vertically Flipped!"),
181 Vector(0, 310), LAYER_FOREGROUND1);
183 context.do_drawing();
186 wait_for_event(event,1000,3000,true);
191 GameSession::start_timers()
193 time_left.start(level->time_left*1000);
199 GameSession::on_escape_press()
201 if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
202 return; // don't let the player open the menu, when he is dying
207 if(st_gl_mode == ST_GL_TEST)
209 exit_status = ES_LEVEL_ABORT;
211 else if (!Menu::current())
213 /* Tell Tux that the keys are all down, otherwise
214 it could have nasty bugs, like going allways to the right
215 or whatever that key does */
216 Player& tux = *(currentsector->player);
217 tux.key_event((SDLKey)keymap.jump, UP);
218 tux.key_event((SDLKey)keymap.duck, UP);
219 tux.key_event((SDLKey)keymap.left, UP);
220 tux.key_event((SDLKey)keymap.right, UP);
221 tux.key_event((SDLKey)keymap.fire, UP);
223 Menu::set_current(game_menu);
224 Ticks::pause_start();
229 GameSession::process_events()
231 if (end_sequence != NO_ENDSEQUENCE)
233 Player& tux = *currentsector->player;
237 tux.input.right = DOWN;
240 if (int(last_x_pos) == int(tux.base.x))
245 last_x_pos = tux.base.x;
248 while (SDL_PollEvent(&event))
250 /* Check for menu-events, if the menu is shown */
253 Menu::current()->event(event);
260 case SDL_QUIT: /* Quit event - quit: */
261 Termination::abort("Received window close", "");
264 case SDL_KEYDOWN: /* A keypress! */
266 SDLKey key = event.key.keysym.sym;
270 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
278 case SDL_JOYBUTTONDOWN:
279 if (event.jbutton.button == joystick_keymap.start_button)
287 if(!Menu::current() && !game_pause)
291 while (SDL_PollEvent(&event))
293 /* Check for menu-events, if the menu is shown */
296 Menu::current()->event(event);
302 Player& tux = *currentsector->player;
306 case SDL_QUIT: /* Quit event - quit: */
307 Termination::abort("Received window close", "");
310 case SDL_KEYDOWN: /* A keypress! */
312 SDLKey key = event.key.keysym.sym;
314 if(tux.key_event(key,DOWN))
319 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
327 case SDL_KEYUP: /* A keyrelease! */
329 SDLKey key = event.key.keysym.sym;
331 if(tux.key_event(key, UP))
340 snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
341 tux.base.x, tux.base.y);
342 context->draw_text(white_text, buf,
343 Vector(0, screen->h - white_text->get_height()),
345 context->do_drawing();
360 Ticks::pause_start();
372 player_status.distros += 50;
376 tux.got_power = tux.FIRE_POWER;
380 tux.got_power = tux.ICE_POWER;
384 tux.invincible_timer.start(TUX_INVINCIBLE_TIME);
388 --player_status.lives;
392 player_status.score += 1000;
405 case SDL_JOYAXISMOTION:
406 if (event.jaxis.axis == joystick_keymap.x_axis)
408 if (event.jaxis.value < -joystick_keymap.dead_zone)
410 tux.input.left = DOWN;
411 tux.input.right = UP;
413 else if (event.jaxis.value > joystick_keymap.dead_zone)
416 tux.input.right = DOWN;
420 tux.input.left = DOWN;
421 tux.input.right = DOWN;
424 else if (event.jaxis.axis == joystick_keymap.y_axis)
426 if (event.jaxis.value > joystick_keymap.dead_zone)
427 tux.input.down = DOWN;
428 else if (event.jaxis.value < -joystick_keymap.dead_zone)
435 case SDL_JOYHATMOTION:
436 if(event.jhat.value & SDL_HAT_UP) {
439 } else if(event.jhat.value & SDL_HAT_DOWN) {
441 tux.input.down = DOWN;
442 } else if(event.jhat.value & SDL_HAT_LEFT) {
443 tux.input.left = DOWN;
444 tux.input.right = UP;
445 } else if(event.jhat.value & SDL_HAT_RIGHT) {
447 tux.input.right = DOWN;
448 } else if(event.jhat.value == SDL_HAT_CENTERED) {
450 tux.input.right = UP;
456 case SDL_JOYBUTTONDOWN:
457 if (event.jbutton.button == joystick_keymap.a_button)
459 else if (event.jbutton.button == joystick_keymap.b_button)
460 tux.input.fire = DOWN;
461 else if (event.jbutton.button == joystick_keymap.start_button)
464 case SDL_JOYBUTTONUP:
465 if (event.jbutton.button == joystick_keymap.a_button)
467 else if (event.jbutton.button == joystick_keymap.b_button)
480 GameSession::check_end_conditions()
482 Player* tux = currentsector->player;
485 Tile* endtile = collision_goal(tux->base);
487 if(end_sequence && !endsequence_timer.check())
489 exit_status = ES_LEVEL_FINISHED;
492 else if(end_sequence == ENDSEQUENCE_RUNNING && endtile && endtile->data >= 1)
494 end_sequence = ENDSEQUENCE_WAITING;
496 else if(!end_sequence && endtile && endtile->data == 0)
498 end_sequence = ENDSEQUENCE_RUNNING;
500 SoundManager::get()->play_music(level_end_song, 0);
501 endsequence_timer.start(7000); // 5 seconds until we finish the map
502 tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
504 else if (!end_sequence && tux->is_dead())
506 player_status.bonus = PlayerStatus::NO_BONUS;
508 if (player_status.lives < 0)
510 exit_status = ES_GAME_OVER;
513 { // Still has lives, so reset Tux to the levelstart
522 GameSession::action(double frame_ratio)
524 if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
526 // Update Tux and the World
527 currentsector->action(frame_ratio);
530 // respawning in new sector?
531 if(newsector != "" && newspawnpoint != "") {
532 Sector* sector = level->get_sector(newsector);
533 currentsector = sector;
534 currentsector->activate(newspawnpoint);
535 currentsector->play_music(LEVEL_MUSIC);
536 newsector = newspawnpoint = "";
543 currentsector->draw(*context);
544 drawstatus(*context);
548 int x = screen->h / 20;
549 for(int i = 0; i < x; ++i)
551 context->draw_filled_rect(
552 Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
553 -((pause_menu_frame * i)%screen->w)
554 ,(i*20+pause_menu_frame)%screen->h),
555 Vector(screen->w,10),
556 Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
558 context->draw_filled_rect(
559 Vector(0,0), Vector(screen->w, screen->h),
560 Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
561 context->draw_text_center(blue_text, _("PAUSE - Press 'P' To Play"),
562 Vector(0, 230), LAYER_FOREGROUND1+2);
566 sprintf(str1, _("Playing: "));
567 sprintf(str2, level->name.c_str());
569 context->draw_text(blue_text, str1,
570 Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
571 LAYER_FOREGROUND1+2);
572 context->draw_text(white_text, str2,
573 Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
574 LAYER_FOREGROUND1+2);
579 Menu::current()->draw(*context);
580 mouse_cursor->draw(*context);
583 context->do_drawing();
587 GameSession::process_menu()
589 Menu* menu = Menu::current();
594 if(menu == game_menu)
596 switch (game_menu->check())
601 case MNID_ABORTLEVEL:
603 exit_status = ES_LEVEL_ABORT;
607 else if(menu == options_menu)
609 process_options_menu();
611 else if(menu == load_game_menu )
613 process_load_game_menu();
618 GameSession::ExitStatus
621 Menu::set_current(0);
628 // Eat unneeded events
630 while (SDL_PollEvent(&event)) {}
634 while (exit_status == ES_NONE)
636 /* Calculate the movement-factor */
637 double frame_ratio = frame_rate.get();
639 if(!frame_timer.check())
641 frame_timer.start(25);
642 ++global_frame_counter;
646 currentsector->player->input.old_fire
647 = currentsector->player->input.fire;
652 // Update the world state and all objects in the world
653 // Do that with a constante time-delta so that the game will run
654 // determistic and not different on different machines
655 if(!game_pause && !Menu::current())
658 check_end_conditions();
659 if (end_sequence == ENDSEQUENCE_RUNNING)
660 action(frame_ratio/2);
661 else if(end_sequence == NO_ENDSEQUENCE)
672 /* Time stops in pause mode */
673 if(game_pause || Menu::current())
681 if (!time_left.check() && currentsector->player->dying == DYING_NOT
683 currentsector->player->kill(Player::KILL);
686 if(currentsector->player->invincible_timer.check() && !end_sequence)
688 currentsector->play_music(HERRING_MUSIC);
690 /* are we low on time ? */
691 else if (time_left.get_left() < TIME_WARNING && !end_sequence)
693 currentsector->play_music(HURRYUP_MUSIC);
695 /* or just normal music? */
696 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
698 currentsector->play_music(LEVEL_MUSIC);
701 /* Calculate frames per second */
705 fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
707 if(!fps_timer.check())
709 fps_timer.start(1000);
719 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
722 newspawnpoint = spawnpoint;
725 /* Bounce a brick: */
726 void bumpbrick(float x, float y)
728 Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
729 (int)(y / 32) * 32));
731 SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
736 GameSession::drawstatus(DrawingContext& context)
740 snprintf(str, 60, " %d", player_status.score);
741 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
742 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
744 if(st_gl_mode == ST_GL_TEST)
746 context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
750 if(!time_left.check()) {
751 context.draw_text_center(white_text, _("TIME's UP"), Vector(0, 0),
753 } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
754 sprintf(str, " %d", time_left.get_left() / 1000 );
755 context.draw_text_center(white_text, _("TIME"),
756 Vector(0, 0), LAYER_FOREGROUND1);
757 context.draw_text_center(gold_text, str,
758 Vector(4*16, 0), LAYER_FOREGROUND1);
761 sprintf(str, " %d", player_status.distros);
762 context.draw_text(white_text, _("COINS"),
763 Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width(" 99"), 0),
765 context.draw_text(gold_text, str,
766 Vector(screen->w - gold_text->get_text_width(" 99"), 0),LAYER_FOREGROUND1);
768 if (player_status.lives >= 5)
770 sprintf(str, "%dx", player_status.lives);
771 float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
772 context.draw_text(gold_text, str, Vector(x, 20), LAYER_FOREGROUND1);
773 context.draw_surface(tux_life, Vector(screen->w - 16, 20),
778 for(int i= 0; i < player_status.lives; ++i)
779 context.draw_surface(tux_life,
780 Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
784 context.draw_text(white_text, _("LIVES"),
785 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 20),
790 sprintf(str, "%2.1f", fps_fps);
791 context.draw_text(white_text, "FPS",
792 Vector(screen->w - white_text->get_text_width("FPS "), 40),
794 context.draw_text(gold_text, str,
795 Vector(screen->w-4*16, 40), LAYER_FOREGROUND1);
800 GameSession::drawresultscreen(void)
804 DrawingContext context;
805 currentsector->background->draw(context);
807 context.draw_text_center(blue_text, _("Result:"), Vector(0, 200),
810 sprintf(str, _("SCORE: %d"), player_status.score);
811 context.draw_text_center(gold_text, str, Vector(0, 224), LAYER_FOREGROUND1);
813 sprintf(str, _("COINS: %d"), player_status.distros);
814 context.draw_text_center(gold_text, str, Vector(0, 256), LAYER_FOREGROUND1);
816 context.do_drawing();
819 wait_for_event(event,2000,5000,true);
822 std::string slotinfo(int slot)
825 std::string slotfile;
827 std::stringstream stream;
829 slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
831 lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
834 LispReader reader(lisp_cdr(savegame));
835 reader.read_string("title", title);
839 if (access(slotfile.c_str(), F_OK) == 0)
842 tmp = "Slot " + stream.str() + " - " + title;
844 tmp = "Slot " + stream.str() + " - Savegame";
847 tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
852 bool process_load_game_menu()
854 int slot = load_game_menu->check();
856 if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
858 std::stringstream stream;
860 std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
862 if (access(slotfile.c_str(), F_OK) != 0)
867 // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
870 DrawingContext context;
871 context.draw_text_center(white_text, "Loading...",
872 Vector(0, screen->h/2), LAYER_FOREGROUND1);
873 context.do_drawing();
875 WorldMapNS::WorldMap worldmap;
877 // Load the game or at least set the savegame_file variable
878 worldmap.loadgame(slotfile);
882 Menu::set_current(main_menu);