c4b048565c023eedd3d0e4d69e705a1cac498b53
[supertux.git] / src / gameloop.cpp
1 //  $Id$
2 // 
3 //  SuperTux
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>
7 //
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.
12 //
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.
17 // 
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.
21
22 #include <config.h>
23
24 #include <iostream>
25 #include <sstream>
26 #include <cassert>
27 #include <cstdio>
28 #include <cstdlib>
29 #include <cmath>
30 #include <cstring>
31 #include <cerrno>
32 #include <unistd.h>
33 #include <ctime>
34
35 #include "SDL.h"
36
37 #ifndef WIN32
38 #include <sys/types.h>
39 #include <ctype.h>
40 #endif
41
42 #include "defines.h"
43 #include "app/globals.h"
44 #include "gameloop.h"
45 #include "video/screen.h"
46 #include "app/setup.h"
47 #include "high_scores.h"
48 #include "gui/menu.h"
49 #include "sector.h"
50 #include "player.h"
51 #include "level.h"
52 #include "scene.h"
53 #include "collision.h"
54 #include "tile.h"
55 #include "particlesystem.h"
56 #include "resources.h"
57 #include "background.h"
58 #include "tilemap.h"
59 #include "app/gettext.h"
60 #include "worldmap.h"
61 #include "intro.h"
62 #include "misc.h"
63 #include "camera.h"
64 #include "statistics.h"
65 #include "timer.h"
66 #include "object/fireworks.h"
67
68 GameSession* GameSession::current_ = 0;
69
70 bool compare_last(std::string& haystack, std::string needle)
71 {
72   int haystack_size = haystack.size();
73   int needle_size = needle.size();
74
75   if(haystack_size < needle_size)
76     return false;
77
78   if(haystack.compare(haystack_size-needle_size, needle_size, needle) == 0)
79     return true;
80   return false;
81 }
82
83 GameSession::GameSession(const std::string& levelname_, int mode,
84     bool flip_level_, Statistics* statistics)
85   : level(0), currentsector(0), st_gl_mode(mode),
86     end_sequence(NO_ENDSEQUENCE), levelname(levelname_), flip_level(flip_level_),
87     best_level_statistics(statistics)
88 {
89   current_ = this;
90   
91   global_frame_counter = 0;
92   game_pause = false;
93   fps_fps = 0;
94
95   context = new DrawingContext();
96
97   if(flip_levels_mode)
98     flip_level = true;
99
100   last_swap_point = Vector(-1, -1);
101   last_swap_stats.reset();
102
103   restart_level();
104 }
105
106 void
107 GameSession::restart_level()
108 {
109   game_pause   = false;
110   exit_status  = ES_NONE;
111   end_sequence = NO_ENDSEQUENCE;
112
113   last_keys.clear();
114
115 #if 0
116   Vector tux_pos = Vector(-1,-1);
117   if (currentsector)
118     { 
119       // Tux has lost a life, so we try to respawn him at the nearest reset point
120       tux_pos = currentsector->player->base;
121     }
122 #endif
123   
124   delete level;
125   currentsector = 0;
126
127   level = new Level;
128   level->load(levelname);
129   if(flip_level)
130     level->do_vertical_flip();
131
132   global_stats.reset();
133   global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
134   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
135   global_stats.set_total_points(TIME_NEEDED_STAT, level->timelimit);
136
137   currentsector = level->get_sector("main");
138   if(!currentsector)
139     Termination::abort("Level has no main sector.", "");
140   currentsector->activate("main");
141
142 #if 0
143   // Set Tux to the nearest reset point
144   if(tux_pos.x != -1)
145     {
146     tux_pos = currentsector->get_best_spawn_point(tux_pos);
147
148     if(last_swap_point.x > tux_pos.x)
149       tux_pos = last_swap_point;
150     else  // new swap point
151       {
152       last_swap_point = tux_pos;
153
154       last_swap_stats += global_stats;
155       }
156
157     currentsector->player->base.x = tux_pos.x;
158     currentsector->player->base.y = tux_pos.y;
159
160     // has to reset camera on swapping
161     currentsector->camera->reset(Vector(currentsector->player->base.x,
162                                         currentsector->player->base.y));
163     }
164 #endif
165
166   if (st_gl_mode != ST_GL_DEMO_GAME)
167     {
168       if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
169         levelintro();
170     }
171
172   start_timers();
173   currentsector->play_music(LEVEL_MUSIC);
174 }
175
176 GameSession::~GameSession()
177 {
178   delete level;
179   delete context;
180 }
181
182 void
183 GameSession::levelintro(void)
184 {
185   SoundManager::get()->halt_music();
186   
187   char str[60];
188
189   DrawingContext context;
190   if(currentsector->background)
191     currentsector->background->draw(context);
192
193 //  context.draw_text(gold_text, level->get_name(), Vector(screen->w/2, 160),
194 //      CENTER_ALLIGN, LAYER_FOREGROUND1);
195   context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
196       LAYER_FOREGROUND1);
197
198   sprintf(str, "TUX x %d", player_status.lives);
199   context.draw_text(white_text, str, Vector(screen->w/2, 210),
200       CENTER_ALLIGN, LAYER_FOREGROUND1);
201
202   if(level->get_author().size())
203     context.draw_text(white_small_text,
204       std::string(_("by ")) + level->get_author(), 
205       Vector(screen->w/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
206
207
208   if(flip_level)
209     context.draw_text(white_text,
210       _("Level Vertically Flipped!"),
211       Vector(screen->w/2, 310), CENTER_ALLIGN, LAYER_FOREGROUND1);
212
213   if(best_level_statistics != NULL)
214     best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
215
216   context.do_drawing();
217
218   SDL_Event event;
219   wait_for_event(event,1000,3000,true);
220 }
221
222 /* Reset Timers */
223 void
224 GameSession::start_timers()
225 {
226   time_left.start(level->timelimit);
227   Ticks::pause_init();
228 }
229
230 void
231 GameSession::on_escape_press()
232 {
233   if(currentsector->player->dying || end_sequence != NO_ENDSEQUENCE)
234     return;   // don't let the player open the menu, when he is dying
235   
236   if(game_pause)
237     return;
238
239   if(st_gl_mode == ST_GL_TEST)
240     {
241       exit_status = ES_LEVEL_ABORT;
242     }
243   else if (!Menu::current())
244     {
245       /* Tell Tux that the keys are all down, otherwise
246         it could have nasty bugs, like going allways to the right
247         or whatever that key does */
248       Player& tux = *(currentsector->player);
249       tux.key_event((SDLKey)keymap.up, UP);
250       tux.key_event((SDLKey)keymap.down, UP);
251       tux.key_event((SDLKey)keymap.left, UP);
252       tux.key_event((SDLKey)keymap.right, UP);
253       tux.key_event((SDLKey)keymap.jump, UP);
254       tux.key_event((SDLKey)keymap.power, UP);
255
256       Menu::set_current(game_menu);
257       Ticks::pause_start();
258     }
259 }
260
261 void
262 GameSession::process_events()
263 {
264   if (end_sequence != NO_ENDSEQUENCE)
265     {
266       Player& tux = *currentsector->player;
267          
268       tux.input.fire  = UP;
269       tux.input.left  = UP;
270       tux.input.right = DOWN;
271       tux.input.down  = UP; 
272
273       if (int(last_x_pos) == int(tux.get_pos().x))
274         tux.input.up    = DOWN; 
275       else
276         tux.input.up    = UP; 
277
278       last_x_pos = tux.get_pos().x;
279
280       SDL_Event event;
281       while (SDL_PollEvent(&event))
282         {
283           /* Check for menu-events, if the menu is shown */
284           if (Menu::current())
285             {
286               Menu::current()->event(event);
287               if(!Menu::current())
288               Ticks::pause_stop();
289             }
290
291           switch(event.type)
292             {
293             case SDL_QUIT:        /* Quit event - quit: */
294               Termination::abort("Received window close", "");
295               break;
296               
297             case SDL_KEYDOWN:     /* A keypress! */
298               {
299                 SDLKey key = event.key.keysym.sym;
300            
301                 switch(key)
302                   {
303                   case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
304                     on_escape_press();
305                     break;
306                   default:
307                     break;
308                   }
309               }
310           
311             case SDL_JOYBUTTONDOWN:
312               if (event.jbutton.button == joystick_keymap.start_button)
313                 on_escape_press();
314               break;
315             }
316         }
317     }
318   else // normal mode
319     {
320       if(!Menu::current() && !game_pause)
321         Ticks::pause_stop();
322
323       SDL_Event event;
324       while (SDL_PollEvent(&event))
325         {
326           /* Check for menu-events, if the menu is shown */
327           if (Menu::current())
328             {
329               Menu::current()->event(event);
330               if(!Menu::current())
331                 Ticks::pause_stop();
332             }
333           else
334             {
335               Player& tux = *currentsector->player;
336   
337               switch(event.type)
338                 {
339                 case SDL_QUIT:        /* Quit event - quit: */
340                   Termination::abort("Received window close", "");
341                   break;
342
343                 case SDL_KEYDOWN:     /* A keypress! */
344                   {
345                     SDLKey key = event.key.keysym.sym;
346             
347                     if(tux.key_event(key,DOWN))
348                       break;
349
350                     switch(key)
351                       {
352                       case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
353                         on_escape_press();
354                         break;
355                       default:
356                         break;
357                       }
358                   }
359                   break;
360                 case SDL_KEYUP:      /* A keyrelease! */
361                   {
362                     SDLKey key = event.key.keysym.sym;
363
364                     if(tux.key_event(key, UP))
365                       break;
366
367                     switch(key)
368                       {
369                       case SDLK_a:
370                         if(debug_mode)
371                         {
372                           char buf[160];
373                           snprintf(buf, sizeof(buf), "P: %4.1f,%4.1f",
374                               tux.get_pos().x, tux.get_pos().y);
375                           context->draw_text(white_text, buf,
376                               Vector(0, screen->h - white_text->get_height()),
377                               LEFT_ALLIGN, LAYER_FOREGROUND1);
378                           context->do_drawing();
379                           SDL_Delay(1000);
380                         }
381                         break;
382                       case SDLK_p:
383                         if(!Menu::current())
384                           {
385                           // "lifeup" cheat activates pause cause of the 'p'
386                           // so work around to ignore it
387                             if(compare_last(last_keys, "lifeu"))
388                               break;
389
390                             if(game_pause)
391                               {
392                                 game_pause = false;
393                                 Ticks::pause_stop();
394                               }
395                             else
396                               {
397                                 game_pause = true;
398                                 Ticks::pause_start();
399                               }
400                           }
401                         break;
402                       default:
403                         break;
404                       }
405                   }
406
407                         /* Check if chacrater is ASCII */
408                         char ch[2];
409                         if((event.key.keysym.unicode & 0xFF80) == 0)
410                           {
411                           ch[0] = event.key.keysym.unicode & 0x7F;
412                           ch[1] = '\0';
413                           }
414                         last_keys.append(ch);  // add to cheat keys
415
416                         // Cheating words (the goal of this is really for debugging,
417                         // but could be used for some cheating, nothing wrong with that)
418                         if(compare_last(last_keys, "grow"))
419                           {
420                           tux.grow(false);
421                           last_keys.clear();
422                           }
423                         if(compare_last(last_keys, "fire"))
424                           {
425                           tux.grow(false);
426                           tux.got_power = tux.FIRE_POWER;
427                           last_keys.clear();
428                           }
429                         if(compare_last(last_keys, "ice"))
430                           {
431                           tux.grow(false);
432                           tux.got_power = tux.ICE_POWER;
433                           last_keys.clear();
434                           }
435                         if(compare_last(last_keys, "lifeup"))
436                           {
437                           player_status.lives++;
438                           last_keys.clear();
439                           }
440                         if(compare_last(last_keys, "lifedown"))
441                           {
442                           player_status.lives--;
443                           last_keys.clear();
444                           }
445                         if(compare_last(last_keys, "grease"))
446                           {
447                           tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
448                           last_keys.clear();
449                           }
450                         if(compare_last(last_keys, "invincible"))
451                           {    // be invincle for the rest of the level
452                           tux.invincible_timer.start(10000);
453                           last_keys.clear();
454                           }
455                         if(compare_last(last_keys, "shrink"))
456                           {    // remove powerups
457                           tux.kill(tux.SHRINK);
458                           last_keys.clear();
459                           }
460                         if(compare_last(last_keys, "kill"))
461                           {    // kill Tux, but without losing a life
462                           player_status.lives++;
463                           tux.kill(tux.KILL);
464                           last_keys.clear();
465                           }
466                         if(compare_last(last_keys, "hover"))
467                           {    // toggle hover ability on/off
468                           tux.enable_hover = !tux.enable_hover;
469                           last_keys.clear();
470                           }
471                         if(compare_last(last_keys, "gotoend"))
472                           {    // goes to the end of the level
473                           tux.move(Vector(
474                               (currentsector->solids->get_width()*32) 
475                                 - (screen->w*2),
476                                 0));
477                           currentsector->camera->reset(
478                               Vector(tux.get_pos().x, tux.get_pos().y));
479                           last_keys.clear();
480                           }
481                         // temporary to help player's choosing a flapping
482                         if(compare_last(last_keys, "marek"))
483                           {
484                           tux.flapping_mode = Player::MAREK_FLAP;
485                           last_keys.clear();
486                           }
487                         if(compare_last(last_keys, "ricardo"))
488                           {
489                           tux.flapping_mode = Player::RICARDO_FLAP;
490                           last_keys.clear();
491                           }
492                         if(compare_last(last_keys, "ryan"))
493                           {
494                           tux.flapping_mode = Player::RYAN_FLAP;
495                           last_keys.clear();
496                           }
497                   break;
498
499                 case SDL_JOYAXISMOTION:
500                   if (event.jaxis.axis == joystick_keymap.x_axis)
501                     {
502                       if (event.jaxis.value < -joystick_keymap.dead_zone)
503                         {
504                           tux.input.left  = DOWN;
505                           tux.input.right = UP;
506                         }
507                       else if (event.jaxis.value > joystick_keymap.dead_zone)
508                         {
509                           tux.input.left  = UP;
510                           tux.input.right = DOWN;
511                         }
512                       else
513                         {
514                           tux.input.left  = DOWN;
515                           tux.input.right = DOWN;
516                         }
517                     }
518                   else if (event.jaxis.axis == joystick_keymap.y_axis)
519                     {
520                       if (event.jaxis.value > joystick_keymap.dead_zone)
521                         {
522                         tux.input.up = DOWN;
523                         tux.input.down = UP;
524                         }
525                       else if (event.jaxis.value < -joystick_keymap.dead_zone)
526                         {
527                         tux.input.up = UP;
528                         tux.input.down = DOWN;
529                         }
530                       else
531                         {
532                         tux.input.up = DOWN;
533                         tux.input.down = DOWN;
534                         }
535                     }
536                   break;
537
538                 case SDL_JOYHATMOTION:
539                   if(event.jhat.value & SDL_HAT_UP) {
540                     tux.input.up = DOWN;
541                     tux.input.down = UP;
542                   } else if(event.jhat.value & SDL_HAT_DOWN) {
543                     tux.input.up = UP;
544                     tux.input.down = DOWN;
545                   } else if(event.jhat.value & SDL_HAT_LEFT) {
546                     tux.input.left = DOWN;
547                     tux.input.right = UP;
548                   } else if(event.jhat.value & SDL_HAT_RIGHT) {
549                     tux.input.left = UP;
550                     tux.input.right = DOWN;
551                   } else if(event.jhat.value == SDL_HAT_CENTERED) {
552                     tux.input.left = UP;
553                     tux.input.right = UP;
554                     tux.input.up = UP;
555                     tux.input.down = UP;
556                   }
557                   break;
558             
559                 case SDL_JOYBUTTONDOWN:
560                   if (event.jbutton.button == joystick_keymap.a_button)
561                     tux.input.jump = DOWN;
562                   else if (event.jbutton.button == joystick_keymap.b_button)
563                     tux.input.fire = DOWN;
564                   else if (event.jbutton.button == joystick_keymap.start_button)
565                     on_escape_press();
566                   break;
567                 case SDL_JOYBUTTONUP:
568                   if (event.jbutton.button == joystick_keymap.a_button)
569                     tux.input.jump = UP;
570                   else if (event.jbutton.button == joystick_keymap.b_button)
571                     tux.input.fire = UP;
572                   break;
573
574                 default:
575                   break;
576                 }  /* switch */
577             }
578         } /* while */
579     }
580 }
581
582 void
583 GameSession::check_end_conditions()
584 {
585   Player* tux = currentsector->player;
586
587   /* End of level? */
588   if(end_sequence && endsequence_timer.check()) {
589       exit_status = ES_LEVEL_FINISHED;
590       global_stats += last_swap_stats;  // add swap points stats
591       return;
592   } else if (!end_sequence && tux->is_dead()) {
593     player_status.bonus = PlayerStatus::NO_BONUS;
594
595     if (player_status.lives < 0) { // No more lives!?
596       exit_status = ES_GAME_OVER;
597     } else { // Still has lives, so reset Tux to the levelstart
598       restart_level();
599     }
600     
601     return;
602   }
603 }
604
605 void
606 GameSession::action(float elapsed_time)
607 {
608   // advance timers
609   if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
610     {
611       // Update Tux and the World
612       currentsector->action(elapsed_time);
613     }
614
615   // respawning in new sector?
616   if(newsector != "" && newspawnpoint != "") {
617     Sector* sector = level->get_sector(newsector);
618     currentsector = sector;
619     currentsector->activate(newspawnpoint);
620     currentsector->play_music(LEVEL_MUSIC);
621     newsector = newspawnpoint = "";
622   }
623 }
624
625 void 
626 GameSession::draw()
627 {
628   currentsector->draw(*context);
629   drawstatus(*context);
630
631   if(game_pause)
632     {
633       int x = screen->h / 20;
634       for(int i = 0; i < x; ++i)
635         {
636           context->draw_filled_rect(
637               Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
638                 -((pause_menu_frame * i)%screen->w)
639                 ,(i*20+pause_menu_frame)%screen->h),
640               Vector(screen->w,10),
641               Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
642         }
643       context->draw_filled_rect(
644           Vector(0,0), Vector(screen->w, screen->h),
645           Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
646       context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
647           Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
648
649       char str1[60];
650       char str2[124];
651       sprintf(str1, _("Playing: "));
652       sprintf(str2, level->name.c_str());
653
654       context->draw_text(blue_text, str1,
655           Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
656           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
657       context->draw_text(white_text, str2,
658           Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
659           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
660     }
661
662   if(Menu::current())
663     {
664       Menu::current()->draw(*context);
665       mouse_cursor->draw(*context);
666     }
667
668   context->do_drawing();
669 }
670
671 void
672 GameSession::process_menu()
673 {
674   Menu* menu = Menu::current();
675   if(menu)
676     {
677       menu->action();
678
679       if(menu == game_menu)
680         {
681           switch (game_menu->check())
682             {
683             case MNID_CONTINUE:
684               Ticks::pause_stop();
685               break;
686             case MNID_ABORTLEVEL:
687               Ticks::pause_stop();
688               exit_status = ES_LEVEL_ABORT;
689               break;
690             }
691         }
692       else if(menu == options_menu)
693         {
694           process_options_menu();
695         }
696       else if(menu == load_game_menu )
697         {
698           process_load_game_menu();
699         }
700     }
701 }
702
703 GameSession::ExitStatus
704 GameSession::run()
705 {
706   Menu::set_current(0);
707   current_ = this;
708   
709   int fps_cnt = 0;
710
711   // Eat unneeded events
712   SDL_Event event;
713   while(SDL_PollEvent(&event))
714   {}
715
716   draw();
717
718   Uint32 lastticks = SDL_GetTicks();
719   fps_ticks = SDL_GetTicks();
720
721   frame_timer.start(.025, true);
722   while (exit_status == ES_NONE) {
723     Uint32 ticks = SDL_GetTicks();
724     float elapsed_time = float(ticks - lastticks) / 1000.;
725     global_time += elapsed_time;
726     lastticks = ticks;
727     // 40fps is minimum
728     if(elapsed_time > .05)
729       elapsed_time = .05;
730     
731     if(frame_timer.check()) {
732       ++global_frame_counter;
733     }
734
735     /* Handle events: */
736     currentsector->player->input.old_fire = currentsector->player->input.fire;
737     currentsector->player->input.old_up = currentsector->player->input.old_up;
738
739     process_events();
740     process_menu();
741
742     // Update the world state and all objects in the world
743     // Do that with a constante time-delta so that the game will run
744     // determistic and not different on different machines
745     if(!game_pause && !Menu::current())
746     {
747       // Update the world
748       check_end_conditions();
749       if (end_sequence == ENDSEQUENCE_RUNNING)
750         action(elapsed_time/2);
751       else if(end_sequence == NO_ENDSEQUENCE)
752         action(elapsed_time);
753     }
754     else
755     {
756       ++pause_menu_frame;
757       SDL_Delay(50);
758     }
759
760     draw();
761
762     /* Time stops in pause mode */
763     if(game_pause || Menu::current())
764     {
765       continue;
766     }
767
768     //frame_rate.update();
769     
770     /* Handle time: */
771     if (time_left.check() && currentsector->player->dying == DYING_NOT
772         && !end_sequence)
773       currentsector->player->kill(Player::KILL);
774     
775     /* Handle music: */
776     if(currentsector->player->invincible_timer.check() && !end_sequence)
777     {
778       currentsector->play_music(HERRING_MUSIC);
779     }
780     /* are we low on time ? */
781     else if (time_left.get_timeleft() < TIME_WARNING && !end_sequence)
782     {
783       currentsector->play_music(HURRYUP_MUSIC);
784     }
785     /* or just normal music? */
786     else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
787     {
788       currentsector->play_music(LEVEL_MUSIC);
789     }
790
791     /* Calculate frames per second */
792     if(show_fps)
793     {
794       ++fps_cnt;
795       
796       if(SDL_GetTicks() - fps_ticks >= 500)
797       {
798         fps_fps = (float) fps_cnt / .5;
799         fps_cnt = 0;
800         fps_ticks = SDL_GetTicks();
801       }
802     }
803   }
804   
805   return exit_status;
806 }
807
808 void
809 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
810 {
811   newsector = sector;
812   newspawnpoint = spawnpoint;
813 }
814
815 void
816 GameSession::start_sequence(const std::string& sequencename)
817 {
818   if(sequencename == "endsequence") {
819     if(end_sequence)
820       return;
821     
822     end_sequence = ENDSEQUENCE_RUNNING;
823     endsequence_timer.start(7.0); // 7 seconds until we finish the map
824     last_x_pos = -1;
825     SoundManager::get()->play_music(level_end_song, 0);
826     currentsector->player->invincible_timer.start(7.0);
827
828     // add left time to stats
829     global_stats.set_points(TIME_NEEDED_STAT,
830         int(time_left.get_period() - time_left.get_timeleft()));
831
832     if(level->get_end_sequence_type() == Level::FIREWORKS_ENDSEQ_ANIM) {
833       currentsector->add_object(new Fireworks());
834     }
835   } else {
836     std::cout << "Unknown sequence '" << sequencename << "'.\n";
837   }
838 }
839
840 /* (Status): */
841 void
842 GameSession::drawstatus(DrawingContext& context)
843 {
844   char str[60];
845   
846   snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
847   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
848   context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
849
850   if(st_gl_mode == ST_GL_TEST)
851     {
852       context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
853           LEFT_ALLIGN, LAYER_FOREGROUND1);
854     }
855
856   if(time_left.check()) {
857     context.draw_text(white_text, _("TIME's UP"), Vector(screen->w/2, 0),
858         CENTER_ALLIGN, LAYER_FOREGROUND1);
859   } else if (time_left.get_timeleft() > TIME_WARNING
860       || (global_frame_counter % 10) < 5) {
861     sprintf(str, " %d", int(time_left.get_timeleft()));
862     context.draw_text(white_text, _("TIME"),
863         Vector(screen->w/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
864     context.draw_text(gold_text, str,
865         Vector(screen->w/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
866   }
867
868   sprintf(str, " %d", player_status.distros);
869   context.draw_text(white_text, _("COINS"),
870       Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
871         LEFT_ALLIGN, LAYER_FOREGROUND1);
872   context.draw_text(gold_text, str,
873       Vector(screen->w - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
874
875   if (player_status.lives >= 5)
876     {
877       sprintf(str, "%dx", player_status.lives);
878       float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
879       context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
880       context.draw_surface(tux_life, Vector(screen->w - 16, 20),
881           LAYER_FOREGROUND1);
882     }
883   else
884     {
885       for(int i= 0; i < player_status.lives; ++i)
886         context.draw_surface(tux_life, 
887             Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
888             LAYER_FOREGROUND1);
889     }
890
891   context.draw_text(white_text, _("LIVES"),
892       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
893       LEFT_ALLIGN, LAYER_FOREGROUND1);
894
895   if(show_fps)
896     {
897       sprintf(str, "%2.1f", fps_fps);
898       context.draw_text(white_text, "FPS", 
899           Vector(screen->w - white_text->get_text_width("FPS     "), 40),
900           LEFT_ALLIGN, LAYER_FOREGROUND1);
901       context.draw_text(gold_text, str,
902           Vector(screen->w-4*16, 40), LEFT_ALLIGN, LAYER_FOREGROUND1);
903     }
904 }
905
906 void
907 GameSession::drawresultscreen(void)
908 {
909   char str[80];
910
911   DrawingContext context;
912   currentsector->background->draw(context);  
913
914   context.draw_text(blue_text, _("Result:"), Vector(screen->w/2, 200),
915       CENTER_ALLIGN, LAYER_FOREGROUND1);
916
917   sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
918   context.draw_text(gold_text, str, Vector(screen->w/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
919
920   sprintf(str, _("COINS: %d"), player_status.distros);
921   context.draw_text(gold_text, str, Vector(screen->w/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
922
923   context.do_drawing();
924   
925   SDL_Event event;
926   wait_for_event(event,2000,5000,true);
927 }
928
929 std::string slotinfo(int slot)
930 {
931   std::string tmp;
932   std::string slotfile;
933   std::string title;
934   std::stringstream stream;
935   stream << slot;
936   slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
937
938   lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
939   if (savegame)
940     {
941       LispReader reader(lisp_cdr(savegame));
942       reader.read_string("title", title);
943       lisp_free(savegame);
944     }
945
946   if (access(slotfile.c_str(), F_OK) == 0)
947     {
948       if (!title.empty())
949         tmp = "Slot " + stream.str() + " - " + title;
950       else
951         tmp = "Slot " + stream.str() + " - Savegame";
952     }
953   else
954     tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
955
956   return tmp;
957 }
958
959 bool process_load_game_menu()
960 {
961   int slot = load_game_menu->check();
962
963   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
964     {
965       std::stringstream stream;
966       stream << slot;
967       std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
968
969       if (access(slotfile.c_str(), F_OK) != 0)
970         {
971           shrink_fade(Vector(screen->w/2,screen->h/2), 600);
972           draw_intro();
973         }
974
975       fadeout(256);
976       DrawingContext context;
977       context.draw_text(white_text, "Loading...",
978                         Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
979       context.do_drawing();
980
981       WorldMapNS::WorldMap worldmap;
982
983       worldmap.set_map_filename("icyisland.stwm");
984       // Load the game or at least set the savegame_file variable
985       worldmap.loadgame(slotfile);
986
987       worldmap.display();
988
989       Menu::set_current(main_menu);
990
991       Ticks::pause_stop();
992       return true;
993     }
994   else
995     {
996       return false;
997     }
998 }