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.
35 #include <sys/types.h>
40 #include "app/globals.h"
42 #include "video/screen.h"
43 #include "app/setup.h"
44 #include "high_scores.h"
52 #include "collision.h"
54 #include "particlesystem.h"
55 #include "resources.h"
56 #include "background.h"
58 #include "app/gettext.h"
63 GameSession* GameSession::current_ = 0;
65 GameSession::GameSession(const std::string& levelname_, int mode, bool flip_level_)
66 : level(0), currentsector(0), st_gl_mode(mode),
67 end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_)
71 global_frame_counter = 0;
76 frame_timer.init(true);
77 frame_rate.set_fps(100);
79 context = new DrawingContext();
88 GameSession::restart_level()
91 exit_status = ES_NONE;
92 end_sequence = NO_ENDSEQUENCE;
95 frame_timer.init(true);
100 { // Tux has lost a life, so we try to respawn him at the nearest reset point
101 old_x_pos = world->get_tux()->base.x;
109 level->load(levelname);
111 level->do_vertical_flip();
112 currentsector = level->get_sector("main");
114 Termination::abort("Level has no main sector.", "");
115 currentsector->activate("main");
118 // Set Tux to the nearest reset point
121 ResetPoint best_reset_point = { -1, -1 };
122 for(std::vector<ResetPoint>::iterator i = get_level()->reset_points.begin();
123 i != get_level()->reset_points.end(); ++i)
125 if (i->x < old_x_pos && best_reset_point.x < i->x)
126 best_reset_point = *i;
129 if (best_reset_point.x != -1)
131 world->get_tux()->base.x = best_reset_point.x;
132 world->get_tux()->base.y = best_reset_point.y;
137 if (st_gl_mode != ST_GL_DEMO_GAME)
139 if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
143 time_left.init(true);
145 currentsector->play_music(LEVEL_MUSIC);
148 GameSession::~GameSession()
155 GameSession::levelintro(void)
157 SoundManager::get()->halt_music();
161 DrawingContext context;
162 currentsector->background->draw(context);
164 context.draw_text_center(gold_text, level->get_name(), Vector(0, 220),
167 sprintf(str, "TUX x %d", player_status.lives);
168 context.draw_text_center(white_text, str, Vector(0, 240),
171 if(level->get_author().size())
172 context.draw_text_center(white_small_text,
173 std::string(_("by ")) + level->get_author(),
174 Vector(0, 400), LAYER_FOREGROUND1);
178 context.draw_text_center(white_text,
179 _("Level Vertically Flipped!"),
180 Vector(0, 310), LAYER_FOREGROUND1);
182 context.do_drawing();
185 wait_for_event(event,1000,3000,true);
190 GameSession::start_timers()
192 time_left.start(level->time_left*1000);
198 GameSession::on_escape_press()
200 if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
201 return; // don't let the player open the menu, when he is dying
206 if(st_gl_mode == ST_GL_TEST)
208 exit_status = ES_LEVEL_ABORT;
210 else if (!Menu::current())
212 /* Tell Tux that the keys are all down, otherwise
213 it could have nasty bugs, like going allways to the right
214 or whatever that key does */
215 Player& tux = *(currentsector->player);
216 tux.key_event((SDLKey)keymap.jump, UP);
217 tux.key_event((SDLKey)keymap.duck, UP);
218 tux.key_event((SDLKey)keymap.left, UP);
219 tux.key_event((SDLKey)keymap.right, UP);
220 tux.key_event((SDLKey)keymap.fire, UP);
222 Menu::set_current(game_menu);
223 Ticks::pause_start();
228 GameSession::process_events()
230 if (end_sequence != NO_ENDSEQUENCE)
232 Player& tux = *currentsector->player;
236 tux.input.right = DOWN;
239 if (int(last_x_pos) == int(tux.base.x))
244 last_x_pos = tux.base.x;
247 while (SDL_PollEvent(&event))
249 /* Check for menu-events, if the menu is shown */
252 Menu::current()->event(event);
259 case SDL_QUIT: /* Quit event - quit: */
260 Termination::abort("Received window close", "");
263 case SDL_KEYDOWN: /* A keypress! */
265 SDLKey key = event.key.keysym.sym;
269 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
277 case SDL_JOYBUTTONDOWN:
278 if (event.jbutton.button == joystick_keymap.start_button)
286 if(!Menu::current() && !game_pause)
290 while (SDL_PollEvent(&event))
292 /* Check for menu-events, if the menu is shown */
295 Menu::current()->event(event);
301 Player& tux = *currentsector->player;
305 case SDL_QUIT: /* Quit event - quit: */
306 Termination::abort("Received window close", "");
309 case SDL_KEYDOWN: /* A keypress! */
311 SDLKey key = event.key.keysym.sym;
313 if(tux.key_event(key,DOWN))
318 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
326 case SDL_KEYUP: /* A keyrelease! */
328 SDLKey key = event.key.keysym.sym;
330 if(tux.key_event(key, UP))
339 snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
340 tux.base.x, tux.base.y);
341 context->draw_text(white_text, buf,
342 Vector(0, screen->h - white_text->get_height()),
344 context->do_drawing();
359 Ticks::pause_start();
371 player_status.distros += 50;
375 tux.got_power = tux.FIRE_POWER;
379 tux.got_power = tux.ICE_POWER;
383 tux.invincible_timer.start(TUX_INVINCIBLE_TIME);
387 --player_status.lives;
391 player_status.score += 1000;
404 case SDL_JOYAXISMOTION:
405 if (event.jaxis.axis == joystick_keymap.x_axis)
407 if (event.jaxis.value < -joystick_keymap.dead_zone)
409 tux.input.left = DOWN;
410 tux.input.right = UP;
412 else if (event.jaxis.value > joystick_keymap.dead_zone)
415 tux.input.right = DOWN;
419 tux.input.left = DOWN;
420 tux.input.right = DOWN;
423 else if (event.jaxis.axis == joystick_keymap.y_axis)
425 if (event.jaxis.value > joystick_keymap.dead_zone)
426 tux.input.down = DOWN;
427 else if (event.jaxis.value < -joystick_keymap.dead_zone)
434 case SDL_JOYBUTTONDOWN:
435 if (event.jbutton.button == joystick_keymap.a_button)
437 else if (event.jbutton.button == joystick_keymap.b_button)
438 tux.input.fire = DOWN;
439 else if (event.jbutton.button == joystick_keymap.start_button)
442 case SDL_JOYBUTTONUP:
443 if (event.jbutton.button == joystick_keymap.a_button)
445 else if (event.jbutton.button == joystick_keymap.b_button)
458 GameSession::check_end_conditions()
460 Player* tux = currentsector->player;
463 Tile* endtile = collision_goal(tux->base);
465 if(end_sequence && !endsequence_timer.check())
467 exit_status = ES_LEVEL_FINISHED;
470 else if(end_sequence == ENDSEQUENCE_RUNNING && endtile && endtile->data >= 1)
472 end_sequence = ENDSEQUENCE_WAITING;
474 else if(!end_sequence && endtile && endtile->data == 0)
476 end_sequence = ENDSEQUENCE_RUNNING;
478 SoundManager::get()->play_music(level_end_song, 0);
479 endsequence_timer.start(7000); // 5 seconds until we finish the map
480 tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
482 else if (!end_sequence && tux->is_dead())
484 player_status.bonus = PlayerStatus::NO_BONUS;
486 if (player_status.lives < 0)
488 exit_status = ES_GAME_OVER;
491 { // Still has lives, so reset Tux to the levelstart
500 GameSession::action(double frame_ratio)
502 if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
504 // Update Tux and the World
505 currentsector->action(frame_ratio);
508 // respawning in new sector?
509 if(newsector != "" && newspawnpoint != "") {
510 Sector* sector = level->get_sector(newsector);
511 currentsector = sector;
512 currentsector->activate(newspawnpoint);
513 currentsector->play_music(LEVEL_MUSIC);
514 newsector = newspawnpoint = "";
521 currentsector->draw(*context);
522 drawstatus(*context);
526 int x = screen->h / 20;
527 for(int i = 0; i < x; ++i)
529 context->draw_filled_rect(
530 Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
531 -((pause_menu_frame * i)%screen->w)
532 ,(i*20+pause_menu_frame)%screen->h),
533 Vector(screen->w,10),
534 Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
536 context->draw_filled_rect(
537 Vector(0,0), Vector(screen->w, screen->h),
538 Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
539 context->draw_text_center(blue_text, _("PAUSE - Press 'P' To Play"),
540 Vector(0, 230), LAYER_FOREGROUND1+2);
544 sprintf(str1, _("Playing: "));
545 sprintf(str2, level->name.c_str());
547 context->draw_text(blue_text, str1,
548 Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
549 LAYER_FOREGROUND1+2);
550 context->draw_text(white_text, str2,
551 Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
552 LAYER_FOREGROUND1+2);
557 Menu::current()->draw(*context);
558 mouse_cursor->draw(*context);
561 context->do_drawing();
565 GameSession::process_menu()
567 Menu* menu = Menu::current();
572 if(menu == game_menu)
574 switch (game_menu->check())
579 case MNID_ABORTLEVEL:
581 exit_status = ES_LEVEL_ABORT;
585 else if(menu == options_menu)
587 process_options_menu();
589 else if(menu == load_game_menu )
591 process_load_game_menu();
596 GameSession::ExitStatus
599 Menu::set_current(0);
606 // Eat unneeded events
608 while (SDL_PollEvent(&event)) {}
612 while (exit_status == ES_NONE)
614 /* Calculate the movement-factor */
615 double frame_ratio = frame_rate.get();
617 if(!frame_timer.check())
619 frame_timer.start(25);
620 ++global_frame_counter;
624 currentsector->player->input.old_fire
625 = currentsector->player->input.fire;
630 // Update the world state and all objects in the world
631 // Do that with a constante time-delta so that the game will run
632 // determistic and not different on different machines
633 if(!game_pause && !Menu::current())
636 check_end_conditions();
637 if (end_sequence == ENDSEQUENCE_RUNNING)
638 action(frame_ratio/2);
639 else if(end_sequence == NO_ENDSEQUENCE)
650 /* Time stops in pause mode */
651 if(game_pause || Menu::current())
659 if (!time_left.check() && currentsector->player->dying == DYING_NOT
661 currentsector->player->kill(Player::KILL);
664 if(currentsector->player->invincible_timer.check() && !end_sequence)
666 currentsector->play_music(HERRING_MUSIC);
668 /* are we low on time ? */
669 else if (time_left.get_left() < TIME_WARNING && !end_sequence)
671 currentsector->play_music(HURRYUP_MUSIC);
673 /* or just normal music? */
674 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
676 currentsector->play_music(LEVEL_MUSIC);
679 /* Calculate frames per second */
683 fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
685 if(!fps_timer.check())
687 fps_timer.start(1000);
697 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
700 newspawnpoint = spawnpoint;
703 /* Bounce a brick: */
704 void bumpbrick(float x, float y)
706 Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
707 (int)(y / 32) * 32));
709 SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
714 GameSession::drawstatus(DrawingContext& context)
718 snprintf(str, 60, " %d", player_status.score);
719 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
720 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
722 if(st_gl_mode == ST_GL_TEST)
724 context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
728 if(!time_left.check()) {
729 context.draw_text_center(white_text, _("TIME's UP"), Vector(0, 0),
731 } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
732 sprintf(str, " %d", time_left.get_left() / 1000 );
733 context.draw_text_center(white_text, _("TIME"),
734 Vector(0, 0), LAYER_FOREGROUND1);
735 context.draw_text_center(gold_text, str,
736 Vector(4*16, 0), LAYER_FOREGROUND1);
739 sprintf(str, " %d", player_status.distros);
740 context.draw_text(white_text, _("COINS"),
741 Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width(" 99"), 0),
743 context.draw_text(gold_text, str,
744 Vector(screen->w - gold_text->get_text_width(" 99"), 0),LAYER_FOREGROUND1);
746 if (player_status.lives >= 5)
748 sprintf(str, "%dx", player_status.lives);
749 float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
750 context.draw_text(gold_text, str, Vector(x, 20), LAYER_FOREGROUND1);
751 context.draw_surface(tux_life, Vector(screen->w - 16, 20),
756 for(int i= 0; i < player_status.lives; ++i)
757 context.draw_surface(tux_life,
758 Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
762 context.draw_text(white_text, _("LIVES"),
763 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 20),
768 sprintf(str, "%2.1f", fps_fps);
769 context.draw_text(white_text, "FPS",
770 Vector(screen->w - white_text->get_text_width("FPS "), 40),
772 context.draw_text(gold_text, str,
773 Vector(screen->w-4*16, 40), LAYER_FOREGROUND1);
778 GameSession::drawresultscreen(void)
782 DrawingContext context;
783 currentsector->background->draw(context);
785 context.draw_text_center(blue_text, _("Result:"), Vector(0, 200),
788 sprintf(str, _("SCORE: %d"), player_status.score);
789 context.draw_text_center(gold_text, str, Vector(0, 224), LAYER_FOREGROUND1);
791 sprintf(str, _("COINS: %d"), player_status.distros);
792 context.draw_text_center(gold_text, str, Vector(0, 256), LAYER_FOREGROUND1);
794 context.do_drawing();
797 wait_for_event(event,2000,5000,true);
800 std::string slotinfo(int slot)
805 sprintf(slotfile,"%s/slot%d.stsg",st_save_dir,slot);
807 lisp_object_t* savegame = lisp_read_from_file(slotfile);
810 LispReader reader(lisp_cdr(savegame));
811 reader.read_string("title", title);
815 if (access(slotfile, F_OK) == 0)
818 snprintf(tmp,1024,"Slot %d - %s",slot, title.c_str());
820 snprintf(tmp, 1024,_("Slot %d - Savegame"),slot);
823 sprintf(tmp,_("Slot %d - Free"),slot);
828 bool process_load_game_menu()
830 int slot = load_game_menu->check();
832 if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
835 snprintf(slotfile, 1024, "%s/slot%d.stsg", st_save_dir, slot);
837 if (access(slotfile, F_OK) != 0)
842 // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
845 DrawingContext context;
846 context.draw_text_center(white_text, "Loading...",
847 Vector(0, screen->h/2), LAYER_FOREGROUND1);
848 context.do_drawing();
850 WorldMapNS::WorldMap worldmap;
852 // Load the game or at least set the savegame_file variable
853 worldmap.loadgame(slotfile);
857 Menu::set_current(main_menu);