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++;
411 if(compare_last(last_keys, "lifedown"))
413 player_status.lives--;
416 if(compare_last(last_keys, "invincible"))
417 { // be invincle for the rest of the level
418 tux.invincible_timer.start(time_left.get_left());
424 case SDL_JOYAXISMOTION:
425 if (event.jaxis.axis == joystick_keymap.x_axis)
427 if (event.jaxis.value < -joystick_keymap.dead_zone)
429 tux.input.left = DOWN;
430 tux.input.right = UP;
432 else if (event.jaxis.value > joystick_keymap.dead_zone)
435 tux.input.right = DOWN;
439 tux.input.left = DOWN;
440 tux.input.right = DOWN;
443 else if (event.jaxis.axis == joystick_keymap.y_axis)
445 if (event.jaxis.value > joystick_keymap.dead_zone)
446 tux.input.down = DOWN;
447 else if (event.jaxis.value < -joystick_keymap.dead_zone)
454 case SDL_JOYHATMOTION:
455 if(event.jhat.value & SDL_HAT_UP) {
458 } else if(event.jhat.value & SDL_HAT_DOWN) {
460 tux.input.down = DOWN;
461 } else if(event.jhat.value & SDL_HAT_LEFT) {
462 tux.input.left = DOWN;
463 tux.input.right = UP;
464 } else if(event.jhat.value & SDL_HAT_RIGHT) {
466 tux.input.right = DOWN;
467 } else if(event.jhat.value == SDL_HAT_CENTERED) {
469 tux.input.right = UP;
475 case SDL_JOYBUTTONDOWN:
476 if (event.jbutton.button == joystick_keymap.a_button)
478 else if (event.jbutton.button == joystick_keymap.b_button)
479 tux.input.fire = DOWN;
480 else if (event.jbutton.button == joystick_keymap.start_button)
483 case SDL_JOYBUTTONUP:
484 if (event.jbutton.button == joystick_keymap.a_button)
486 else if (event.jbutton.button == joystick_keymap.b_button)
499 GameSession::check_end_conditions()
501 Player* tux = currentsector->player;
504 Tile* endtile = collision_goal(tux->base);
506 if(end_sequence && !endsequence_timer.check())
508 exit_status = ES_LEVEL_FINISHED;
511 else if(end_sequence == ENDSEQUENCE_RUNNING && endtile && endtile->data >= 1)
513 end_sequence = ENDSEQUENCE_WAITING;
515 else if(!end_sequence && endtile && endtile->data == 0)
517 end_sequence = ENDSEQUENCE_RUNNING;
518 random_timer.start(200); // start 1st firework
520 SoundManager::get()->play_music(level_end_song, 0);
521 endsequence_timer.start(7000); // 5 seconds until we finish the map
522 tux->invincible_timer.start(7000); //FIXME: Implement a winning timer for the end sequence (with special winning animation etc.)
524 else if (!end_sequence && tux->is_dead())
526 player_status.bonus = PlayerStatus::NO_BONUS;
528 if (player_status.lives < 0)
530 exit_status = ES_GAME_OVER;
533 { // Still has lives, so reset Tux to the levelstart
542 GameSession::action(double frame_ratio)
544 if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
546 // Update Tux and the World
547 currentsector->action(frame_ratio);
550 // respawning in new sector?
551 if(newsector != "" && newspawnpoint != "") {
552 Sector* sector = level->get_sector(newsector);
553 currentsector = sector;
554 currentsector->activate(newspawnpoint);
555 currentsector->play_music(LEVEL_MUSIC);
556 newsector = newspawnpoint = "";
559 // on end sequence make a few fireworks
560 if(end_sequence == ENDSEQUENCE_RUNNING && !random_timer.check())
562 Vector epicenter = currentsector->camera->get_translation();
563 epicenter.x += screen->w * ((float)rand() / RAND_MAX);
564 epicenter.y += (screen->h/2) * ((float)rand() / RAND_MAX);
566 int red = rand() % 255; // calculate firework color
567 int green = rand() % red;
568 currentsector->add_particles(epicenter, Vector(1.4,1.4), Vector(0,0),
569 45, Color(red,green,0), 3, 1300);
571 SoundManager::get()->play_sound(IDToSound(SND_FIREWORKS));
572 random_timer.start(rand() % 400 + 600); // next firework
579 currentsector->draw(*context);
580 drawstatus(*context);
584 int x = screen->h / 20;
585 for(int i = 0; i < x; ++i)
587 context->draw_filled_rect(
588 Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
589 -((pause_menu_frame * i)%screen->w)
590 ,(i*20+pause_menu_frame)%screen->h),
591 Vector(screen->w,10),
592 Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
594 context->draw_filled_rect(
595 Vector(0,0), Vector(screen->w, screen->h),
596 Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
597 context->draw_text_center(blue_text, _("PAUSE - Press 'P' To Play"),
598 Vector(0, 230), LAYER_FOREGROUND1+2);
602 sprintf(str1, _("Playing: "));
603 sprintf(str2, level->name.c_str());
605 context->draw_text(blue_text, str1,
606 Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
607 LAYER_FOREGROUND1+2);
608 context->draw_text(white_text, str2,
609 Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
610 LAYER_FOREGROUND1+2);
615 Menu::current()->draw(*context);
616 mouse_cursor->draw(*context);
619 context->do_drawing();
623 GameSession::process_menu()
625 Menu* menu = Menu::current();
630 if(menu == game_menu)
632 switch (game_menu->check())
637 case MNID_ABORTLEVEL:
639 exit_status = ES_LEVEL_ABORT;
643 else if(menu == options_menu)
645 process_options_menu();
647 else if(menu == load_game_menu )
649 process_load_game_menu();
654 GameSession::ExitStatus
657 Menu::set_current(0);
664 // Eat unneeded events
666 while (SDL_PollEvent(&event)) {}
670 while (exit_status == ES_NONE)
672 /* Calculate the movement-factor */
673 double frame_ratio = frame_rate.get();
675 if(!frame_timer.check())
677 frame_timer.start(25);
678 ++global_frame_counter;
682 currentsector->player->input.old_fire
683 = currentsector->player->input.fire;
688 // Update the world state and all objects in the world
689 // Do that with a constante time-delta so that the game will run
690 // determistic and not different on different machines
691 if(!game_pause && !Menu::current())
694 check_end_conditions();
695 if (end_sequence == ENDSEQUENCE_RUNNING)
696 action(frame_ratio/2);
697 else if(end_sequence == NO_ENDSEQUENCE)
708 /* Time stops in pause mode */
709 if(game_pause || Menu::current())
717 if (!time_left.check() && currentsector->player->dying == DYING_NOT
719 currentsector->player->kill(Player::KILL);
722 if(currentsector->player->invincible_timer.check() && !end_sequence)
724 currentsector->play_music(HERRING_MUSIC);
726 /* are we low on time ? */
727 else if (time_left.get_left() < TIME_WARNING && !end_sequence)
729 currentsector->play_music(HURRYUP_MUSIC);
731 /* or just normal music? */
732 else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
734 currentsector->play_music(LEVEL_MUSIC);
737 /* Calculate frames per second */
741 fps_fps = (1000.0 / (float)fps_timer.get_gone()) * (float)fps_cnt;
743 if(!fps_timer.check())
745 fps_timer.start(1000);
755 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
758 newspawnpoint = spawnpoint;
761 /* Bounce a brick: */
762 void bumpbrick(float x, float y)
764 Sector::current()->add_bouncy_brick(Vector(((int)(x + 1) / 32) * 32,
765 (int)(y / 32) * 32));
767 SoundManager::get()->play_sound(IDToSound(SND_BRICK), Vector(x, y), Sector::current()->player->get_pos());
772 GameSession::drawstatus(DrawingContext& context)
776 snprintf(str, 60, " %d", player_status.score);
777 context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
778 context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
780 if(st_gl_mode == ST_GL_TEST)
782 context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
786 if(!time_left.check()) {
787 context.draw_text_center(white_text, _("TIME's UP"), Vector(0, 0),
789 } else if (time_left.get_left() > TIME_WARNING || (global_frame_counter % 10) < 5) {
790 sprintf(str, " %d", time_left.get_left() / 1000 );
791 context.draw_text_center(white_text, _("TIME"),
792 Vector(0, 0), LAYER_FOREGROUND1);
793 context.draw_text_center(gold_text, str,
794 Vector(4*16, 0), LAYER_FOREGROUND1);
797 sprintf(str, " %d", player_status.distros);
798 context.draw_text(white_text, _("COINS"),
799 Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width(" 99"), 0),
801 context.draw_text(gold_text, str,
802 Vector(screen->w - gold_text->get_text_width(" 99"), 0),LAYER_FOREGROUND1);
804 if (player_status.lives >= 5)
806 sprintf(str, "%dx", player_status.lives);
807 float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
808 context.draw_text(gold_text, str, Vector(x, 20), LAYER_FOREGROUND1);
809 context.draw_surface(tux_life, Vector(screen->w - 16, 20),
814 for(int i= 0; i < player_status.lives; ++i)
815 context.draw_surface(tux_life,
816 Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
820 context.draw_text(white_text, _("LIVES"),
821 Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width(" 99"), 20),
826 sprintf(str, "%2.1f", fps_fps);
827 context.draw_text(white_text, "FPS",
828 Vector(screen->w - white_text->get_text_width("FPS "), 40),
830 context.draw_text(gold_text, str,
831 Vector(screen->w-4*16, 40), LAYER_FOREGROUND1);
836 GameSession::drawresultscreen(void)
840 DrawingContext context;
841 currentsector->background->draw(context);
843 context.draw_text_center(blue_text, _("Result:"), Vector(0, 200),
846 sprintf(str, _("SCORE: %d"), player_status.score);
847 context.draw_text_center(gold_text, str, Vector(0, 224), LAYER_FOREGROUND1);
849 sprintf(str, _("COINS: %d"), player_status.distros);
850 context.draw_text_center(gold_text, str, Vector(0, 256), LAYER_FOREGROUND1);
852 context.do_drawing();
855 wait_for_event(event,2000,5000,true);
858 std::string slotinfo(int slot)
861 std::string slotfile;
863 std::stringstream stream;
865 slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
867 lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
870 LispReader reader(lisp_cdr(savegame));
871 reader.read_string("title", title);
875 if (access(slotfile.c_str(), F_OK) == 0)
878 tmp = "Slot " + stream.str() + " - " + title;
880 tmp = "Slot " + stream.str() + " - Savegame";
883 tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
888 bool process_load_game_menu()
890 int slot = load_game_menu->check();
892 if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
894 std::stringstream stream;
896 std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
898 if (access(slotfile.c_str(), F_OK) != 0)
903 // shrink_fade(Point((screen->w/2),(screen->h/2)), 1000);
906 DrawingContext context;
907 context.draw_text_center(white_text, "Loading...",
908 Vector(0, screen->h/2), LAYER_FOREGROUND1);
909 context.do_drawing();
911 WorldMapNS::WorldMap worldmap;
913 // Load the game or at least set the savegame_file variable
914 worldmap.loadgame(slotfile);
918 Menu::set_current(main_menu);