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_JOYBUTTONDOWN:
436 if (event.jbutton.button == joystick_keymap.a_button)
438 else if (event.jbutton.button == joystick_keymap.b_button)
439 tux.input.fire = DOWN;
440 else if (event.jbutton.button == joystick_keymap.start_button)
443 case SDL_JOYBUTTONUP:
444 if (event.jbutton.button == joystick_keymap.a_button)
446 else if (event.jbutton.button == joystick_keymap.b_button)
459 GameSession::check_end_conditions()
461 Player* tux = currentsector->player;
464 Tile* endtile = collision_goal(tux->base);
466 if(end_sequence && !endsequence_timer.check())
468 exit_status = ES_LEVEL_FINISHED;
471 else if(end_sequence == ENDSEQUENCE_RUNNING && endtile && endtile->data >= 1)
473 end_sequence = ENDSEQUENCE_WAITING;
475 else if(!end_sequence && endtile && endtile->data == 0)
477 end_sequence = ENDSEQUENCE_RUNNING;
479 SoundManager::get()->play_music(level_end_song, 0);
480 endsequence_timer.start(7000); // 5 seconds until we finish the map
481 tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
483 else if (!end_sequence && tux->is_dead())
485 player_status.bonus = PlayerStatus::NO_BONUS;
487 if (player_status.lives < 0)
489 exit_status = ES_GAME_OVER;
492 { // Still has lives, so reset Tux to the levelstart
501 GameSession::action(double frame_ratio)
503 if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
505 // Update Tux and the World
506 currentsector->action(frame_ratio);
509 // respawning in new sector?
510 if(newsector != "" && newspawnpoint != "") {
511 Sector* sector = level->get_sector(newsector);
512 currentsector = sector;
513 currentsector->activate(newspawnpoint);
514 currentsector->play_music(LEVEL_MUSIC);
515 newsector = newspawnpoint = "";
522 currentsector->draw(*context);
523 drawstatus(*context);
527 int x = screen->h / 20;
528 for(int i = 0; i < x; ++i)
530 context->draw_filled_rect(
531 Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
532 -((pause_menu_frame * i)%screen->w)
533 ,(i*20+pause_menu_frame)%screen->h),
534 Vector(screen->w,10),
535 Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
537 context->draw_filled_rect(
538 Vector(0,0), Vector(screen->w, screen->h),
539 Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
540 context->draw_text_center(blue_text, _("PAUSE - Press 'P' To Play"),
541 Vector(0, 230), LAYER_FOREGROUND1+2);
545 sprintf(str1, _("Playing: "));
546 sprintf(str2, level->name.c_str());
548 context->draw_text(blue_text, str1,
549 Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
550 LAYER_FOREGROUND1+2);
551 context->draw_text(white_text, str2,
552 Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
553 LAYER_FOREGROUND1+2);
558 Menu::current()->draw(*context);
559 mouse_cursor->draw(*context);
562 context->do_drawing();
566 GameSession::process_menu()
568 Menu* menu = Menu::current();
573 if(menu == game_menu)
575 switch (game_menu->check())
580 case MNID_ABORTLEVEL:
582 exit_status = ES_LEVEL_ABORT;
586 else if(menu == options_menu)
588 process_options_menu();
590 else if(menu == load_game_menu )
592 process_load_game_menu();
597 GameSession::ExitStatus
600 Menu::set_current(0);
607 // Eat unneeded events
609 while (SDL_PollEvent(&event)) {}
613 while (exit_status == ES_NONE)
615 /* Calculate the movement-factor */
616 double frame_ratio = frame_rate.get();
618 if(!frame_timer.check())
620 frame_timer.start(25);
621 ++global_frame_counter;
625 currentsector->player->input.old_fire
626 = currentsector->player->input.fire;
631 // Update the world state and all objects in the world
632 // Do that with a constante time-delta so that the game will run
633 // determistic and not different on different machines
634 if(!game_pause && !Menu::current())
637 check_end_conditions();
638 if (end_sequence == ENDSEQUENCE_RUNNING)
639 action(frame_ratio/2);
640 else if(end_sequence == NO_ENDSEQUENCE)
651 /* Time stops in pause mode */
652 if(game_pause || Menu::current())
660 if (!time_left.check() && currentsector->player->dying == DYING_NOT
662 currentsector->player->kill(Player::KILL);
665 if(currentsector->player->invincible_timer.check() && !end_sequence)
667 currentsector->play_music(HERRING_MUSIC);
669 /* are we low on time ? */
670 else if (time_left.get_left() < TIME_WARNING && !end_sequence)
672 currentsector->play_music(HURRYUP_MUSIC);
674 /* or just normal music? */
675 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
677 currentsector->play_music(LEVEL_MUSIC);
680 /* Calculate frames per second */
684 fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
686 if(!fps_timer.check())
688 fps_timer.start(1000);
698 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
701 newspawnpoint = spawnpoint;
704 /* Bounce a brick: */
705 void bumpbrick(float x, float y)
707 Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
708 (int)(y / 32) * 32));
710 SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
715 GameSession::drawstatus(DrawingContext& context)
719 snprintf(str, 60, " %d", player_status.score);
720 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
721 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
723 if(st_gl_mode == ST_GL_TEST)
725 context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
729 if(!time_left.check()) {
730 context.draw_text_center(white_text, _("TIME's UP"), Vector(0, 0),
732 } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
733 sprintf(str, " %d", time_left.get_left() / 1000 );
734 context.draw_text_center(white_text, _("TIME"),
735 Vector(0, 0), LAYER_FOREGROUND1);
736 context.draw_text_center(gold_text, str,
737 Vector(4*16, 0), LAYER_FOREGROUND1);
740 sprintf(str, " %d", player_status.distros);
741 context.draw_text(white_text, _("COINS"),
742 Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width(" 99"), 0),
744 context.draw_text(gold_text, str,
745 Vector(screen->w - gold_text->get_text_width(" 99"), 0),LAYER_FOREGROUND1);
747 if (player_status.lives >= 5)
749 sprintf(str, "%dx", player_status.lives);
750 float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
751 context.draw_text(gold_text, str, Vector(x, 20), LAYER_FOREGROUND1);
752 context.draw_surface(tux_life, Vector(screen->w - 16, 20),
757 for(int i= 0; i < player_status.lives; ++i)
758 context.draw_surface(tux_life,
759 Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
763 context.draw_text(white_text, _("LIVES"),
764 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 20),
769 sprintf(str, "%2.1f", fps_fps);
770 context.draw_text(white_text, "FPS",
771 Vector(screen->w - white_text->get_text_width("FPS "), 40),
773 context.draw_text(gold_text, str,
774 Vector(screen->w-4*16, 40), LAYER_FOREGROUND1);
779 GameSession::drawresultscreen(void)
783 DrawingContext context;
784 currentsector->background->draw(context);
786 context.draw_text_center(blue_text, _("Result:"), Vector(0, 200),
789 sprintf(str, _("SCORE: %d"), player_status.score);
790 context.draw_text_center(gold_text, str, Vector(0, 224), LAYER_FOREGROUND1);
792 sprintf(str, _("COINS: %d"), player_status.distros);
793 context.draw_text_center(gold_text, str, Vector(0, 256), LAYER_FOREGROUND1);
795 context.do_drawing();
798 wait_for_event(event,2000,5000,true);
801 std::string slotinfo(int slot)
804 std::string slotfile;
806 std::stringstream stream;
808 slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
810 lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
813 LispReader reader(lisp_cdr(savegame));
814 reader.read_string("title", title);
818 if (access(slotfile.c_str(), F_OK) == 0)
821 tmp = "Slot " + stream.str() + " - " + title;
823 tmp = "Slot " + stream.str() + " - Savegame";
826 tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
831 bool process_load_game_menu()
833 int slot = load_game_menu->check();
835 if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
837 std::stringstream stream;
839 std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
841 if (access(slotfile.c_str(), F_OK) != 0)
846 // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
849 DrawingContext context;
850 context.draw_text_center(white_text, "Loading...",
851 Vector(0, screen->h/2), LAYER_FOREGROUND1);
852 context.do_drawing();
854 WorldMapNS::WorldMap worldmap;
856 // Load the game or at least set the savegame_file variable
857 worldmap.loadgame(slotfile);
861 Menu::set_current(main_menu);