make supertux accepts normal paths on the commandline
[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& levelfile_, int mode,
84     bool flip_level_, Statistics* statistics)
85   : level(0), currentsector(0), st_gl_mode(mode),
86     end_sequence(NO_ENDSEQUENCE), levelfile(levelfile_),
87     flip_level(flip_level_), best_level_statistics(statistics)
88 {
89   current_ = this;
90   
91   game_pause = false;
92   fps_fps = 0;
93
94   context = new DrawingContext();
95
96   if(flip_levels_mode)
97     flip_level = true;
98
99   last_swap_point = Vector(-1, -1);
100   last_swap_stats.reset();
101
102   restart_level();
103 }
104
105 void
106 GameSession::restart_level()
107 {
108   game_pause   = false;
109   exit_status  = ES_NONE;
110   end_sequence = NO_ENDSEQUENCE;
111
112   last_keys.clear();
113
114 #if 0
115   Vector tux_pos = Vector(-1,-1);
116   if (currentsector)
117     { 
118       // Tux has lost a life, so we try to respawn him at the nearest reset point
119       tux_pos = currentsector->player->base;
120     }
121 #endif
122   
123   delete level;
124   currentsector = 0;
125
126   level = new Level;
127   level->load(levelfile);
128   if(flip_level)
129     level->do_vertical_flip();
130
131   global_stats.reset();
132   global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
133   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
134   global_stats.set_total_points(TIME_NEEDED_STAT, level->timelimit);
135
136   currentsector = level->get_sector("main");
137   if(!currentsector)
138     Termination::abort("Level has no main sector.", "");
139   currentsector->activate("main");
140
141 #if 0
142   // Set Tux to the nearest reset point
143   if(tux_pos.x != -1)
144     {
145     tux_pos = currentsector->get_best_spawn_point(tux_pos);
146
147     if(last_swap_point.x > tux_pos.x)
148       tux_pos = last_swap_point;
149     else  // new swap point
150       {
151       last_swap_point = tux_pos;
152
153       last_swap_stats += global_stats;
154       }
155
156     currentsector->player->base.x = tux_pos.x;
157     currentsector->player->base.y = tux_pos.y;
158
159     // has to reset camera on swapping
160     currentsector->camera->reset(Vector(currentsector->player->base.x,
161                                         currentsector->player->base.y));
162     }
163 #endif
164
165   if (st_gl_mode != ST_GL_DEMO_GAME)
166     {
167       if(st_gl_mode == ST_GL_PLAY || st_gl_mode == ST_GL_LOAD_LEVEL_FILE)
168         levelintro();
169     }
170
171   start_timers();
172   currentsector->play_music(LEVEL_MUSIC);
173 }
174
175 GameSession::~GameSession()
176 {
177   delete level;
178   delete context;
179 }
180
181 void
182 GameSession::levelintro(void)
183 {
184   SoundManager::get()->halt_music();
185   
186   char str[60];
187
188   DrawingContext context;
189   if(currentsector->background)
190     currentsector->background->draw(context);
191
192 //  context.draw_text(gold_text, level->get_name(), Vector(screen->w/2, 160),
193 //      CENTER_ALLIGN, LAYER_FOREGROUND1);
194   context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
195       LAYER_FOREGROUND1);
196
197   sprintf(str, "TUX x %d", player_status.lives);
198   context.draw_text(white_text, str, Vector(screen->w/2, 210),
199       CENTER_ALLIGN, LAYER_FOREGROUND1);
200
201   if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
202     //TODO make author check case/blank-insensitive
203     context.draw_text(white_small_text,
204       std::string(_("contributed 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, "grid"))
467                           {    // toggle debug grid
468                           debug_grid = !debug_grid;
469                           last_keys.clear();
470                           }
471                         if(compare_last(last_keys, "hover"))
472                           {    // toggle hover ability on/off
473                           tux.enable_hover = !tux.enable_hover;
474                           last_keys.clear();
475                           }
476                         if(compare_last(last_keys, "gotoend"))
477                           {    // goes to the end of the level
478                           tux.move(Vector(
479                               (currentsector->solids->get_width()*32) 
480                                 - (screen->w*2),
481                                 0));
482                           currentsector->camera->reset(
483                               Vector(tux.get_pos().x, tux.get_pos().y));
484                           last_keys.clear();
485                           }
486                         // temporary to help player's choosing a flapping
487                         if(compare_last(last_keys, "marek"))
488                           {
489                           tux.flapping_mode = Player::MAREK_FLAP;
490                           last_keys.clear();
491                           }
492                         if(compare_last(last_keys, "ricardo"))
493                           {
494                           tux.flapping_mode = Player::RICARDO_FLAP;
495                           last_keys.clear();
496                           }
497                         if(compare_last(last_keys, "ryan"))
498                           {
499                           tux.flapping_mode = Player::RYAN_FLAP;
500                           last_keys.clear();
501                           }
502                   break;
503
504                 case SDL_JOYAXISMOTION:
505                   if (event.jaxis.axis == joystick_keymap.x_axis)
506                     {
507                       if (event.jaxis.value < -joystick_keymap.dead_zone)
508                         {
509                           tux.input.left  = DOWN;
510                           tux.input.right = UP;
511                         }
512                       else if (event.jaxis.value > joystick_keymap.dead_zone)
513                         {
514                           tux.input.left  = UP;
515                           tux.input.right = DOWN;
516                         }
517                       else
518                         {
519                           tux.input.left  = DOWN;
520                           tux.input.right = DOWN;
521                         }
522                     }
523                   else if (event.jaxis.axis == joystick_keymap.y_axis)
524                     {
525                       if (event.jaxis.value > joystick_keymap.dead_zone)
526                         {
527                         tux.input.up = DOWN;
528                         tux.input.down = UP;
529                         }
530                       else if (event.jaxis.value < -joystick_keymap.dead_zone)
531                         {
532                         tux.input.up = UP;
533                         tux.input.down = DOWN;
534                         }
535                       else
536                         {
537                         tux.input.up = DOWN;
538                         tux.input.down = DOWN;
539                         }
540                     }
541                   break;
542
543                 case SDL_JOYHATMOTION:
544                   if(event.jhat.value & SDL_HAT_UP) {
545                     tux.input.up = DOWN;
546                     tux.input.down = UP;
547                   } else if(event.jhat.value & SDL_HAT_DOWN) {
548                     tux.input.up = UP;
549                     tux.input.down = DOWN;
550                   } else if(event.jhat.value & SDL_HAT_LEFT) {
551                     tux.input.left = DOWN;
552                     tux.input.right = UP;
553                   } else if(event.jhat.value & SDL_HAT_RIGHT) {
554                     tux.input.left = UP;
555                     tux.input.right = DOWN;
556                   } else if(event.jhat.value == SDL_HAT_CENTERED) {
557                     tux.input.left = UP;
558                     tux.input.right = UP;
559                     tux.input.up = UP;
560                     tux.input.down = UP;
561                   }
562                   break;
563             
564                 case SDL_JOYBUTTONDOWN:
565                   if (event.jbutton.button == joystick_keymap.a_button)
566                     tux.input.jump = DOWN;
567                   else if (event.jbutton.button == joystick_keymap.b_button)
568                     tux.input.fire = DOWN;
569                   else if (event.jbutton.button == joystick_keymap.start_button)
570                     on_escape_press();
571                   break;
572                 case SDL_JOYBUTTONUP:
573                   if (event.jbutton.button == joystick_keymap.a_button)
574                     tux.input.jump = UP;
575                   else if (event.jbutton.button == joystick_keymap.b_button)
576                     tux.input.fire = UP;
577                   break;
578
579                 default:
580                   break;
581                 }  /* switch */
582             }
583         } /* while */
584     }
585 }
586
587 void
588 GameSession::check_end_conditions()
589 {
590   Player* tux = currentsector->player;
591
592   /* End of level? */
593   if(end_sequence && endsequence_timer.check()) {
594       exit_status = ES_LEVEL_FINISHED;
595       global_stats += last_swap_stats;  // add swap points stats
596       return;
597   } else if (!end_sequence && tux->is_dead()) {
598     player_status.bonus = PlayerStatus::NO_BONUS;
599
600     if (player_status.lives < 0) { // No more lives!?
601       exit_status = ES_GAME_OVER;
602     } else { // Still has lives, so reset Tux to the levelstart
603       restart_level();
604     }
605     
606     return;
607   }
608 }
609
610 void
611 GameSession::action(float elapsed_time)
612 {
613   // advance timers
614   if (exit_status == ES_NONE && !currentsector->player->growing_timer.check())
615     {
616       // Update Tux and the World
617       currentsector->action(elapsed_time);
618     }
619
620   // respawning in new sector?
621   if(newsector != "" && newspawnpoint != "") {
622     Sector* sector = level->get_sector(newsector);
623     currentsector = sector;
624     currentsector->activate(newspawnpoint);
625     currentsector->play_music(LEVEL_MUSIC);
626     newsector = newspawnpoint = "";
627   }
628 }
629
630 void 
631 GameSession::draw()
632 {
633   currentsector->draw(*context);
634   drawstatus(*context);
635
636   if(game_pause)
637     {
638       int x = screen->h / 20;
639       for(int i = 0; i < x; ++i)
640         {
641           context->draw_filled_rect(
642               Vector(i % 2 ? (pause_menu_frame * i)%screen->w :
643                 -((pause_menu_frame * i)%screen->w)
644                 ,(i*20+pause_menu_frame)%screen->h),
645               Vector(screen->w,10),
646               Color(20,20,20, rand() % 20 + 1), LAYER_FOREGROUND1+1);
647         }
648       context->draw_filled_rect(
649           Vector(0,0), Vector(screen->w, screen->h),
650           Color(rand() % 50, rand() % 50, rand() % 50, 128), LAYER_FOREGROUND1);
651       context->draw_text(blue_text, _("PAUSE - Press 'P' To Play"),
652           Vector(screen->w/2, 230), CENTER_ALLIGN, LAYER_FOREGROUND1+2);
653
654       char str1[60];
655       char str2[124];
656       sprintf(str1, _("Playing: "));
657       sprintf(str2, level->name.c_str());
658
659       context->draw_text(blue_text, str1,
660           Vector((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2, 340),
661           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
662       context->draw_text(white_text, str2,
663           Vector(((screen->w - (blue_text->get_text_width(str1) + white_text->get_text_width(str2)))/2)+blue_text->get_text_width(str1), 340),
664           LEFT_ALLIGN, LAYER_FOREGROUND1+2);
665     }
666
667   if(Menu::current())
668     {
669       Menu::current()->draw(*context);
670       mouse_cursor->draw(*context);
671     }
672
673   context->do_drawing();
674 }
675
676 void
677 GameSession::process_menu()
678 {
679   Menu* menu = Menu::current();
680   if(menu)
681     {
682       menu->action();
683
684       if(menu == game_menu)
685         {
686           switch (game_menu->check())
687             {
688             case MNID_CONTINUE:
689               Ticks::pause_stop();
690               break;
691             case MNID_ABORTLEVEL:
692               Ticks::pause_stop();
693               exit_status = ES_LEVEL_ABORT;
694               break;
695             }
696         }
697       else if(menu == options_menu)
698         {
699           process_options_menu();
700         }
701       else if(menu == load_game_menu )
702         {
703           process_load_game_menu();
704         }
705     }
706 }
707
708 GameSession::ExitStatus
709 GameSession::run()
710 {
711   Menu::set_current(0);
712   current_ = this;
713   
714   int fps_cnt = 0;
715
716   // Eat unneeded events
717   SDL_Event event;
718   while(SDL_PollEvent(&event))
719   {}
720
721   draw();
722
723   Uint32 lastticks = SDL_GetTicks();
724   fps_ticks = SDL_GetTicks();
725
726   while (exit_status == ES_NONE) {
727     Uint32 ticks = SDL_GetTicks();
728     float elapsed_time = float(ticks - lastticks) / 1000.;
729     if(!game_pause)
730       global_time += elapsed_time;
731     lastticks = ticks;
732
733     // 40fps is minimum
734     if(elapsed_time > .025)
735       elapsed_time = .025;
736     
737     /* Handle events: */
738     currentsector->player->input.old_fire = currentsector->player->input.fire;
739     currentsector->player->input.old_up = currentsector->player->input.old_up;
740
741     process_events();
742     process_menu();
743
744     // Update the world state and all objects in the world
745     // Do that with a constante time-delta so that the game will run
746     // determistic and not different on different machines
747     if(!game_pause && !Menu::current())
748     {
749       // Update the world
750       check_end_conditions();
751       if (end_sequence == ENDSEQUENCE_RUNNING)
752         action(elapsed_time/2);
753       else if(end_sequence == NO_ENDSEQUENCE)
754         action(elapsed_time);
755     }
756     else
757     {
758       ++pause_menu_frame;
759       SDL_Delay(50);
760     }
761
762     draw();
763
764     /* Time stops in pause mode */
765     if(game_pause || Menu::current())
766     {
767       continue;
768     }
769
770     //frame_rate.update();
771     
772     /* Handle time: */
773     if (time_left.check() && currentsector->player->dying == DYING_NOT
774         && !end_sequence)
775       currentsector->player->kill(Player::KILL);
776     
777     /* Handle music: */
778     if(currentsector->player->invincible_timer.check() && !end_sequence)
779     {
780       currentsector->play_music(HERRING_MUSIC);
781     }
782     /* are we low on time ? */
783     else if (time_left.get_timeleft() < TIME_WARNING && !end_sequence)
784     {
785       currentsector->play_music(HURRYUP_MUSIC);
786     }
787     /* or just normal music? */
788     else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
789     {
790       currentsector->play_music(LEVEL_MUSIC);
791     }
792
793     /* Calculate frames per second */
794     if(show_fps)
795     {
796       ++fps_cnt;
797       
798       if(SDL_GetTicks() - fps_ticks >= 500)
799       {
800         fps_fps = (float) fps_cnt / .5;
801         fps_cnt = 0;
802         fps_ticks = SDL_GetTicks();
803       }
804     }
805   }
806   
807   return exit_status;
808 }
809
810 void
811 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
812 {
813   newsector = sector;
814   newspawnpoint = spawnpoint;
815 }
816
817 void
818 GameSession::start_sequence(const std::string& sequencename)
819 {
820   if(sequencename == "endsequence") {
821     if(end_sequence)
822       return;
823     
824     end_sequence = ENDSEQUENCE_RUNNING;
825     endsequence_timer.start(7.0); // 7 seconds until we finish the map
826     last_x_pos = -1;
827     SoundManager::get()->play_music(level_end_song, 0);
828     currentsector->player->invincible_timer.start(7.0);
829
830     // add left time to stats
831     global_stats.set_points(TIME_NEEDED_STAT,
832         int(time_left.get_period() - time_left.get_timeleft()));
833
834     if(level->get_end_sequence_type() == Level::FIREWORKS_ENDSEQ_ANIM) {
835       currentsector->add_object(new Fireworks());
836     }
837   } else {
838     std::cout << "Unknown sequence '" << sequencename << "'.\n";
839   }
840 }
841
842 /* (Status): */
843 void
844 GameSession::drawstatus(DrawingContext& context)
845 {
846   char str[60];
847   
848   snprintf(str, 60, " %d", global_stats.get_points(SCORE_STAT));
849   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
850   context.draw_text(gold_text, str, Vector(96, 0), LEFT_ALLIGN, LAYER_FOREGROUND1);
851
852   if(st_gl_mode == ST_GL_TEST)
853     {
854       context.draw_text(white_text, _("Press ESC To Return"), Vector(0,20),
855           LEFT_ALLIGN, LAYER_FOREGROUND1);
856     }
857
858   if(time_left.check()) {
859     context.draw_text(white_text, _("TIME's UP"), Vector(screen->w/2, 0),
860         CENTER_ALLIGN, LAYER_FOREGROUND1);
861   } else if (time_left.get_timeleft() > TIME_WARNING
862       || int(global_time * 2.5) % 2) {
863     sprintf(str, " %d", int(time_left.get_timeleft()));
864     context.draw_text(white_text, _("TIME"),
865         Vector(screen->w/2, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
866     context.draw_text(gold_text, str,
867         Vector(screen->w/2 + 4*16, 0), CENTER_ALLIGN, LAYER_FOREGROUND1);
868   }
869
870   sprintf(str, " %d", player_status.distros);
871   context.draw_text(white_text, _("COINS"),
872       Vector(screen->w - white_text->get_text_width(_("COINS"))-white_text->get_text_width("   99"), 0),
873         LEFT_ALLIGN, LAYER_FOREGROUND1);
874   context.draw_text(gold_text, str,
875       Vector(screen->w - gold_text->get_text_width(" 99"), 0),LEFT_ALLIGN, LAYER_FOREGROUND1);
876
877   if (player_status.lives >= 5)
878     {
879       sprintf(str, "%dx", player_status.lives);
880       float x = screen->w - gold_text->get_text_width(str) - tux_life->w;
881       context.draw_text(gold_text, str, Vector(x, 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
882       context.draw_surface(tux_life, Vector(screen->w - 16, 20),
883           LAYER_FOREGROUND1);
884     }
885   else
886     {
887       for(int i= 0; i < player_status.lives; ++i)
888         context.draw_surface(tux_life, 
889             Vector(screen->w - tux_life->w*4 +(tux_life->w*i), 20),
890             LAYER_FOREGROUND1);
891     }
892
893   context.draw_text(white_text, _("LIVES"),
894       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 20),
895       LEFT_ALLIGN, LAYER_FOREGROUND1);
896
897   if(show_fps)
898     {
899       sprintf(str, "%2.1f", fps_fps);
900       context.draw_text(white_text, "FPS", 
901           Vector(screen->w - white_text->get_text_width("FPS     "), 40),
902           LEFT_ALLIGN, LAYER_FOREGROUND1);
903       context.draw_text(gold_text, str,
904           Vector(screen->w-4*16, 40), LEFT_ALLIGN, LAYER_FOREGROUND1);
905     }
906 }
907
908 void
909 GameSession::drawresultscreen(void)
910 {
911   char str[80];
912
913   DrawingContext context;
914   currentsector->background->draw(context);  
915
916   context.draw_text(blue_text, _("Result:"), Vector(screen->w/2, 200),
917       CENTER_ALLIGN, LAYER_FOREGROUND1);
918
919   sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
920   context.draw_text(gold_text, str, Vector(screen->w/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
921
922   sprintf(str, _("COINS: %d"), player_status.distros);
923   context.draw_text(gold_text, str, Vector(screen->w/2, 256), CENTER_ALLIGN, LAYER_FOREGROUND1);
924
925   context.do_drawing();
926   
927   SDL_Event event;
928   wait_for_event(event,2000,5000,true);
929 }
930
931 std::string slotinfo(int slot)
932 {
933   std::string tmp;
934   std::string slotfile;
935   std::string title;
936   std::stringstream stream;
937   stream << slot;
938   slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
939
940   lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str());
941   if (savegame)
942     {
943       LispReader reader(lisp_cdr(savegame));
944       reader.read_string("title", title);
945       lisp_free(savegame);
946     }
947
948   if (access(slotfile.c_str(), F_OK) == 0)
949     {
950       if (!title.empty())
951         tmp = "Slot " + stream.str() + " - " + title;
952       else
953         tmp = "Slot " + stream.str() + " - Savegame";
954     }
955   else
956     tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free"));
957
958   return tmp;
959 }
960
961 bool process_load_game_menu()
962 {
963   int slot = load_game_menu->check();
964
965   if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION)
966     {
967       std::stringstream stream;
968       stream << slot;
969       std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg";
970
971       if (access(slotfile.c_str(), F_OK) != 0)
972         {
973           shrink_fade(Vector(screen->w/2,screen->h/2), 600);
974           draw_intro();
975         }
976
977       fadeout(256);
978       DrawingContext context;
979       context.draw_text(white_text, "Loading...",
980                         Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
981       context.do_drawing();
982
983       WorldMapNS::WorldMap worldmap;
984
985       worldmap.set_map_filename("icyisland.stwm");
986       // Load the game or at least set the savegame_file variable
987       worldmap.loadgame(slotfile);
988
989       worldmap.display();
990
991       Menu::set_current(main_menu);
992
993       Ticks::pause_stop();
994       return true;
995     }
996   else
997     {
998       return false;
999     }
1000 }