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 frame_rate.set_fps(100);
94 context = new DrawingContext();
103 GameSession::restart_level()
106 exit_status = ES_NONE;
107 end_sequence = NO_ENDSEQUENCE;
109 fps_timer.init(true);
110 frame_timer.init(true);
114 Vector tux_pos = Vector(-1,-1);
116 { // Tux has lost a life, so we try to respawn him at the nearest reset point
117 tux_pos = currentsector->player->base;
124 level->load(levelname);
126 level->do_vertical_flip();
128 currentsector = level->get_sector("main");
130 Termination::abort("Level has no main sector.", "");
131 currentsector->activate("main");
133 // Set Tux to the nearest reset point
136 tux_pos = currentsector->get_best_spawn_point(tux_pos);
137 currentsector->player->base.x = tux_pos.x;
138 currentsector->player->base.y = tux_pos.y;
140 // has to reset camera on swapping
141 currentsector->camera->reset(Vector(currentsector->player->base.x,
142 currentsector->player->base.y));
145 if (st_gl_mode != ST_GL_DEMO_GAME)
147 if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
151 time_left.init(true);
153 currentsector->play_music(LEVEL_MUSIC);
156 GameSession::~GameSession()
163 GameSession::levelintro(void)
165 SoundManager::get()->halt_music();
169 DrawingContext context;
170 currentsector->background->draw(context);
172 context.draw_text_center(gold_text, level->get_name(), Vector(0, 220),
175 sprintf(str, "TUX x %d", player_status.lives);
176 context.draw_text_center(white_text, str, Vector(0, 240),
179 if(level->get_author().size())
180 context.draw_text_center(white_small_text,
181 std::string(_("by ")) + level->get_author(),
182 Vector(0, 400), LAYER_FOREGROUND1);
186 context.draw_text_center(white_text,
187 _("Level Vertically Flipped!"),
188 Vector(0, 310), LAYER_FOREGROUND1);
190 context.do_drawing();
193 wait_for_event(event,1000,3000,true);
198 GameSession::start_timers()
200 time_left.start(level->time_left*1000);
206 GameSession::on_escape_press()
208 if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
209 return; // don't let the player open the menu, when he is dying
214 if(st_gl_mode == ST_GL_TEST)
216 exit_status = ES_LEVEL_ABORT;
218 else if (!Menu::current())
220 /* Tell Tux that the keys are all down, otherwise
221 it could have nasty bugs, like going allways to the right
222 or whatever that key does */
223 Player& tux = *(currentsector->player);
224 tux.key_event((SDLKey)keymap.jump, UP);
225 tux.key_event((SDLKey)keymap.duck, UP);
226 tux.key_event((SDLKey)keymap.left, UP);
227 tux.key_event((SDLKey)keymap.right, UP);
228 tux.key_event((SDLKey)keymap.fire, UP);
230 Menu::set_current(game_menu);
231 Ticks::pause_start();
236 GameSession::process_events()
238 if (end_sequence != NO_ENDSEQUENCE)
240 Player& tux = *currentsector->player;
244 tux.input.right = DOWN;
247 if (int(last_x_pos) == int(tux.base.x))
252 last_x_pos = tux.base.x;
255 while (SDL_PollEvent(&event))
257 /* Check for menu-events, if the menu is shown */
260 Menu::current()->event(event);
267 case SDL_QUIT: /* Quit event - quit: */
268 Termination::abort("Received window close", "");
271 case SDL_KEYDOWN: /* A keypress! */
273 SDLKey key = event.key.keysym.sym;
277 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
285 case SDL_JOYBUTTONDOWN:
286 if (event.jbutton.button == joystick_keymap.start_button)
294 if(!Menu::current() && !game_pause)
298 while (SDL_PollEvent(&event))
300 /* Check for menu-events, if the menu is shown */
303 Menu::current()->event(event);
309 Player& tux = *currentsector->player;
313 case SDL_QUIT: /* Quit event - quit: */
314 Termination::abort("Received window close", "");
317 case SDL_KEYDOWN: /* A keypress! */
319 SDLKey key = event.key.keysym.sym;
321 if(tux.key_event(key,DOWN))
326 case SDLK_ESCAPE: /* Escape: Open/Close the menu: */
334 case SDL_KEYUP: /* A keyrelease! */
336 SDLKey key = event.key.keysym.sym;
338 if(tux.key_event(key, UP))
347 snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
348 tux.base.x, tux.base.y);
349 context->draw_text(white_text, buf,
350 Vector(0, screen->h - white_text->get_height()),
352 context->do_drawing();
367 Ticks::pause_start();
376 /* Check if chacrater is ASCII */
378 if((event.key.keysym.unicode & 0xFF80) == 0)
380 ch[0] = event.key.keysym.unicode & 0x7F;
383 last_keys.append(ch); // add to cheat keys
385 // Cheating words (the goal of this is really for debugging,
386 // but could be used for some cheating, nothing wrong with that)
387 if(compare_last(last_keys, "grow"))
392 if(compare_last(last_keys, "fire"))
395 tux.got_power = tux.FIRE_POWER;
398 if(compare_last(last_keys, "ice"))
401 tux.got_power = tux.ICE_POWER;
404 if(compare_last(last_keys, "lifeup"))
406 player_status.lives++;
409 if(compare_last(last_keys, "lifedown"))
411 player_status.lives--;
414 if(compare_last(last_keys, "invincible"))
415 { // be invincle for the rest of the level
416 tux.invincible_timer.start(time_left.get_left());
422 case SDL_JOYAXISMOTION:
423 if (event.jaxis.axis == joystick_keymap.x_axis)
425 if (event.jaxis.value < -joystick_keymap.dead_zone)
427 tux.input.left = DOWN;
428 tux.input.right = UP;
430 else if (event.jaxis.value > joystick_keymap.dead_zone)
433 tux.input.right = DOWN;
437 tux.input.left = DOWN;
438 tux.input.right = DOWN;
441 else if (event.jaxis.axis == joystick_keymap.y_axis)
443 if (event.jaxis.value > joystick_keymap.dead_zone)
444 tux.input.down = DOWN;
445 else if (event.jaxis.value < -joystick_keymap.dead_zone)
452 case SDL_JOYHATMOTION:
453 if(event.jhat.value & SDL_HAT_UP) {
456 } else if(event.jhat.value & SDL_HAT_DOWN) {
458 tux.input.down = DOWN;
459 } else if(event.jhat.value & SDL_HAT_LEFT) {
460 tux.input.left = DOWN;
461 tux.input.right = UP;
462 } else if(event.jhat.value & SDL_HAT_RIGHT) {
464 tux.input.right = DOWN;
465 } else if(event.jhat.value == SDL_HAT_CENTERED) {
467 tux.input.right = UP;
473 case SDL_JOYBUTTONDOWN:
474 if (event.jbutton.button == joystick_keymap.a_button)
476 else if (event.jbutton.button == joystick_keymap.b_button)
477 tux.input.fire = DOWN;
478 else if (event.jbutton.button == joystick_keymap.start_button)
481 case SDL_JOYBUTTONUP:
482 if (event.jbutton.button == joystick_keymap.a_button)
484 else if (event.jbutton.button == joystick_keymap.b_button)
497 GameSession::check_end_conditions()
499 Player* tux = currentsector->player;
502 Tile* endtile = collision_goal(tux->base);
504 if(end_sequence && !endsequence_timer.check())
506 exit_status = ES_LEVEL_FINISHED;
509 else if(end_sequence == ENDSEQUENCE_RUNNING && endtile && endtile->data >= 1)
511 end_sequence = ENDSEQUENCE_WAITING;
513 else if(!end_sequence && endtile && endtile->data == 0)
515 end_sequence = ENDSEQUENCE_RUNNING;
517 SoundManager::get()->play_music(level_end_song, 0);
518 endsequence_timer.start(7000); // 5 seconds until we finish the map
519 tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
521 else if (!end_sequence && tux->is_dead())
523 player_status.bonus = PlayerStatus::NO_BONUS;
525 if (player_status.lives < 0)
527 exit_status = ES_GAME_OVER;
530 { // Still has lives, so reset Tux to the levelstart
539 GameSession::action(double frame_ratio)
541 if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
543 // Update Tux and the World
544 currentsector->action(frame_ratio);
547 // respawning in new sector?
548 if(newsector != "" && newspawnpoint != "") {
549 Sector* sector = level->get_sector(newsector);
550 currentsector = sector;
551 currentsector->activate(newspawnpoint);
552 currentsector->play_music(LEVEL_MUSIC);
553 newsector = newspawnpoint = "";
560 currentsector->draw(*context);
561 drawstatus(*context);
565 int x = screen->h / 20;
566 for(int i = 0; i < x; ++i)
568 context->draw_filled_rect(
569 Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
570 -((pause_menu_frame * i)%screen->w)
571 ,(i*20+pause_menu_frame)%screen->h),
572 Vector(screen->w,10),
573 Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
575 context->draw_filled_rect(
576 Vector(0,0), Vector(screen->w, screen->h),
577 Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
578 context->draw_text_center(blue_text, _("PAUSE - Press 'P' To Play"),
579 Vector(0, 230), LAYER_FOREGROUND1+2);
583 sprintf(str1, _("Playing: "));
584 sprintf(str2, level->name.c_str());
586 context->draw_text(blue_text, str1,
587 Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
588 LAYER_FOREGROUND1+2);
589 context->draw_text(white_text, str2,
590 Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
591 LAYER_FOREGROUND1+2);
596 Menu::current()->draw(*context);
597 mouse_cursor->draw(*context);
600 context->do_drawing();
604 GameSession::process_menu()
606 Menu* menu = Menu::current();
611 if(menu == game_menu)
613 switch (game_menu->check())
618 case MNID_ABORTLEVEL:
620 exit_status = ES_LEVEL_ABORT;
624 else if(menu == options_menu)
626 process_options_menu();
628 else if(menu == load_game_menu )
630 process_load_game_menu();
635 GameSession::ExitStatus
638 Menu::set_current(0);
645 // Eat unneeded events
647 while (SDL_PollEvent(&event)) {}
651 while (exit_status == ES_NONE)
653 /* Calculate the movement-factor */
654 double frame_ratio = frame_rate.get();
656 if(!frame_timer.check())
658 frame_timer.start(25);
659 ++global_frame_counter;
663 currentsector->player->input.old_fire
664 = currentsector->player->input.fire;
669 // Update the world state and all objects in the world
670 // Do that with a constante time-delta so that the game will run
671 // determistic and not different on different machines
672 if(!game_pause && !Menu::current())
675 check_end_conditions();
676 if (end_sequence == ENDSEQUENCE_RUNNING)
677 action(frame_ratio/2);
678 else if(end_sequence == NO_ENDSEQUENCE)
689 /* Time stops in pause mode */
690 if(game_pause || Menu::current())
698 if (!time_left.check() && currentsector->player->dying == DYING_NOT
700 currentsector->player->kill(Player::KILL);
703 if(currentsector->player->invincible_timer.check() && !end_sequence)
705 currentsector->play_music(HERRING_MUSIC);
707 /* are we low on time ? */
708 else if (time_left.get_left() < TIME_WARNING && !end_sequence)
710 currentsector->play_music(HURRYUP_MUSIC);
712 /* or just normal music? */
713 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
715 currentsector->play_music(LEVEL_MUSIC);
718 /* Calculate frames per second */
722 fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
724 if(!fps_timer.check())
726 fps_timer.start(1000);
736 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
739 newspawnpoint = spawnpoint;
742 /* Bounce a brick: */
743 void bumpbrick(float x, float y)
745 Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
746 (int)(y / 32) * 32));
748 SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
753 GameSession::drawstatus(DrawingContext& context)
757 snprintf(str, 60, " %d", player_status.score);
758 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
759 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
761 if(st_gl_mode == ST_GL_TEST)
763 context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
767 if(!time_left.check()) {
768 context.draw_text_center(white_text, _("TIME's UP"), Vector(0, 0),
770 } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
771 sprintf(str, " %d", time_left.get_left() / 1000 );
772 context.draw_text_center(white_text, _("TIME"),
773 Vector(0, 0), LAYER_FOREGROUND1);
774 context.draw_text_center(gold_text, str,
775 Vector(4*16, 0), LAYER_FOREGROUND1);
778 sprintf(str, " %d", player_status.distros);
779 context.draw_text(white_text, _("COINS"),
780 Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width(" 99"), 0),
782 context.draw_text(gold_text, str,
783 Vector(screen->w - gold_text->get_text_width(" 99"), 0),LAYER_FOREGROUND1);
785 if (player_status.lives >= 5)
787 sprintf(str, "%dx", player_status.lives);
788 float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
789 context.draw_text(gold_text, str, Vector(x, 20), LAYER_FOREGROUND1);
790 context.draw_surface(tux_life, Vector(screen->w - 16, 20),
795 for(int i= 0; i < player_status.lives; ++i)
796 context.draw_surface(tux_life,
797 Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
801 context.draw_text(white_text, _("LIVES"),
802 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 20),
807 sprintf(str, "%2.1f", fps_fps);
808 context.draw_text(white_text, "FPS",
809 Vector(screen->w - white_text->get_text_width("FPS "), 40),
811 context.draw_text(gold_text, str,
812 Vector(screen->w-4*16, 40), LAYER_FOREGROUND1);
817 GameSession::drawresultscreen(void)
821 DrawingContext context;
822 currentsector->background->draw(context);
824 context.draw_text_center(blue_text, _("Result:"), Vector(0, 200),
827 sprintf(str, _("SCORE: %d"), player_status.score);
828 context.draw_text_center(gold_text, str, Vector(0, 224), LAYER_FOREGROUND1);
830 sprintf(str, _("COINS: %d"), player_status.distros);
831 context.draw_text_center(gold_text, str, Vector(0, 256), LAYER_FOREGROUND1);
833 context.do_drawing();
836 wait_for_event(event,2000,5000,true);
839 std::string slotinfo(int slot)
842 std::string slotfile;
844 std::stringstream stream;
846 slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
848 lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
851 LispReader reader(lisp_cdr(savegame));
852 reader.read_string("title", title);
856 if (access(slotfile.c_str(), F_OK) == 0)
859 tmp = "Slot " + stream.str() + " - " + title;
861 tmp = "Slot " + stream.str() + " - Savegame";
864 tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
869 bool process_load_game_menu()
871 int slot = load_game_menu->check();
873 if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
875 std::stringstream stream;
877 std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
879 if (access(slotfile.c_str(), F_OK) != 0)
884 // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
887 DrawingContext context;
888 context.draw_text_center(white_text, "Loading...",
889 Vector(0, screen->h/2), LAYER_FOREGROUND1);
890 context.do_drawing();
892 WorldMapNS::WorldMap worldmap;
894 // Load the game or at least set the savegame_file variable
895 worldmap.loadgame(slotfile);
899 Menu::set_current(main_menu);