ac521794df22d95996ca77db941bd16104b40408
[supertux.git] / src / game_session.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 #include <config.h>
22
23 #include <fstream>
24 #include <sstream>
25 #include <cassert>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cmath>
29 #include <cstring>
30 #include <cerrno>
31 #include <unistd.h>
32 #include <ctime>
33 #include <stdexcept>
34
35 #include <SDL.h>
36
37 #include "game_session.hpp"
38 #include "msg.hpp"
39 #include "video/screen.hpp"
40 #include "audio/sound_manager.hpp"
41 #include "gui/menu.hpp"
42 #include "sector.hpp"
43 #include "level.hpp"
44 #include "tile.hpp"
45 #include "player_status.hpp"
46 #include "object/particlesystem.hpp"
47 #include "object/background.hpp"
48 #include "object/gradient.hpp"
49 #include "object/tilemap.hpp"
50 #include "object/camera.hpp"
51 #include "object/player.hpp"
52 #include "object/level_time.hpp"
53 #include "lisp/lisp.hpp"
54 #include "lisp/parser.hpp"
55 #include "resources.hpp"
56 #include "worldmap.hpp"
57 #include "misc.hpp"
58 #include "statistics.hpp"
59 #include "timer.hpp"
60 #include "object/fireworks.hpp"
61 #include "textscroller.hpp"
62 #include "control/codecontroller.hpp"
63 #include "control/joystickkeyboardcontroller.hpp"
64 #include "main.hpp"
65 #include "file_system.hpp"
66 #include "gameconfig.hpp"
67 #include "gettext.hpp"
68 #include "exceptions.hpp"
69 #include "flip_level_transformer.hpp"
70
71 // the engine will be run with a logical framerate of 64fps.
72 // We chose 64fps here because it is a power of 2, so 1/64 gives an "even"
73 // binary fraction...
74 static const float LOGICAL_FPS = 64.0;
75
76 GameSession* GameSession::current_ = 0;
77
78 GameSession::GameSession(const std::string& levelfile_, GameSessionMode mode,
79     Statistics* statistics)
80   : level(0), currentsector(0), mode(mode),
81     end_sequence(NO_ENDSEQUENCE), end_sequence_controller(0),
82     levelfile(levelfile_), best_level_statistics(statistics),
83     capture_demo_stream(0), playback_demo_stream(0), demo_controller(0)
84 {
85   current_ = this;
86   currentsector = 0;
87   
88   game_pause = false;
89   fps_fps = 0;
90
91   context = new DrawingContext();
92   console = new Console(context);
93   Console::registerCommandReceiver(this);
94
95   restart_level(true);
96 }
97
98 void
99 GameSession::restart_level(bool fromBeginning)
100 {
101   game_pause   = false;
102   exit_status  = ES_NONE;
103   end_sequence = NO_ENDSEQUENCE;
104
105   main_controller->reset();
106
107   delete level;
108   currentsector = 0;
109
110   level = new Level;
111   level->load(levelfile);
112
113   global_stats.reset();
114   global_stats.set_total_points(COINS_COLLECTED_STAT, level->get_total_coins());
115   global_stats.set_total_points(BADGUYS_KILLED_STAT, level->get_total_badguys());
116   
117   // get time
118   int time = 0;
119   for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
120   {
121     Sector* sec = *i;
122
123     for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
124         j != sec->gameobjects.end(); ++j)
125     {
126       GameObject* obj = *j;
127       
128       LevelTime* lt = dynamic_cast<LevelTime*> (obj);
129       if(lt)
130         time += int(lt->get_level_time());
131     }
132   }
133   global_stats.set_total_points(TIME_NEEDED_STAT, (time == 0) ? -1 : time);
134
135   if (fromBeginning) reset_sector="";
136   if(reset_sector != "") {
137     currentsector = level->get_sector(reset_sector);
138     if(!currentsector) {
139       std::stringstream msg;
140       msg << "Couldn't find sector '" << reset_sector << "' for resetting tux.";
141       throw std::runtime_error(msg.str());
142     }
143     currentsector->activate(reset_pos);
144   } else {
145     currentsector = level->get_sector("main");
146     if(!currentsector)
147       throw std::runtime_error("Couldn't find main sector");
148     currentsector->activate("main");
149   }
150   
151   if(mode == ST_GL_PLAY || mode == ST_GL_LOAD_LEVEL_FILE)
152     levelintro();
153
154   currentsector->play_music(LEVEL_MUSIC);
155
156   if(capture_file != "")
157     record_demo(capture_file);
158 }
159
160 GameSession::~GameSession()
161 {
162   delete capture_demo_stream;
163   delete playback_demo_stream;
164   delete demo_controller;
165
166   delete end_sequence_controller;
167   delete level;
168   delete context;
169   Console::unregisterCommandReceiver(this);
170   delete console;
171
172   current_ = NULL;
173 }
174
175 void
176 GameSession::record_demo(const std::string& filename)
177 {
178   delete capture_demo_stream;
179   
180   capture_demo_stream = new std::ofstream(filename.c_str()); 
181   if(!capture_demo_stream->good()) {
182     std::stringstream msg;
183     msg << "Couldn't open demo file '" << filename << "' for writing.";
184     throw std::runtime_error(msg.str());
185   }
186   capture_file = filename;
187 }
188
189 void
190 GameSession::play_demo(const std::string& filename)
191 {
192   delete playback_demo_stream;
193   delete demo_controller;
194   
195   playback_demo_stream = new std::ifstream(filename.c_str());
196   if(!playback_demo_stream->good()) {
197     std::stringstream msg;
198     msg << "Couldn't open demo file '" << filename << "' for reading.";
199     throw std::runtime_error(msg.str());
200   }
201
202   Player& tux = *currentsector->player;
203   demo_controller = new CodeController();
204   tux.set_controller(demo_controller);
205 }
206
207 void
208 GameSession::levelintro()
209 {
210   char str[60];
211
212   sound_manager->stop_music();
213
214   DrawingContext context;
215   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
216       i != currentsector->gameobjects.end(); ++i) {
217     Background* background = dynamic_cast<Background*> (*i);
218     if(background) {
219       background->draw(context);
220     }
221     Gradient* gradient = dynamic_cast<Gradient*> (*i);
222     if(gradient) {
223       gradient->draw(context);
224     }
225   }
226
227 //  context.draw_text(gold_text, level->get_name(), Vector(SCREEN_WIDTH/2, 160),
228 //      CENTER_ALLIGN, LAYER_FOREGROUND1);
229   context.draw_center_text(gold_text, level->get_name(), Vector(0, 160),
230       LAYER_FOREGROUND1);
231
232   sprintf(str, "Coins: %d", player_status->coins);
233   context.draw_text(white_text, str, Vector(SCREEN_WIDTH/2, 210),
234       CENTER_ALLIGN, LAYER_FOREGROUND1);
235
236   if((level->get_author().size()) && (level->get_author() != "SuperTux Team"))
237     //TODO make author check case/blank-insensitive
238     context.draw_text(white_small_text,
239       std::string(_("contributed by ")) + level->get_author(), 
240       Vector(SCREEN_WIDTH/2, 350), CENTER_ALLIGN, LAYER_FOREGROUND1);
241
242
243   if(best_level_statistics != NULL)
244     best_level_statistics->draw_message_info(context, _("Best Level Statistics"));
245
246   console->draw();
247   context.do_drawing();
248
249   wait_for_event(1.0, 3.0);
250 }
251
252 void
253 GameSession::on_escape_press()
254 {
255   if(currentsector->player->is_dying() || end_sequence != NO_ENDSEQUENCE)
256     return;   // don't let the player open the menu, when he is dying
257   
258   if(mode == ST_GL_TEST) {
259     exit_status = ES_LEVEL_ABORT;
260   } else if (!Menu::current()) {
261     Menu::set_current(game_menu);
262     game_menu->set_active_item(MNID_CONTINUE);
263     game_pause = true;
264   } else {
265     game_pause = false;
266   }
267 }
268
269 void
270 GameSession::process_events()
271 {
272   Player& tux = *currentsector->player;
273   main_controller->update();
274
275   // end of pause mode?
276   if(!Menu::current() && game_pause) {
277     game_pause = false;
278   }
279
280   if (end_sequence != NO_ENDSEQUENCE) {
281     if(end_sequence_controller == 0) {
282       end_sequence_controller = new CodeController();
283       tux.set_controller(end_sequence_controller);
284     }
285
286     end_sequence_controller->press(Controller::RIGHT);
287     
288     if (int(last_x_pos) == int(tux.get_pos().x))
289       end_sequence_controller->press(Controller::JUMP);    
290     last_x_pos = tux.get_pos().x;
291   }
292
293   main_controller->update();
294   SDL_Event event;
295   while (SDL_PollEvent(&event)) {
296     /* Check for menu-events, if the menu is shown */
297     if (Menu::current())
298       Menu::current()->event(event);
299     main_controller->process_event(event);
300     if(event.type == SDL_QUIT)
301       throw graceful_shutdown();
302   }
303
304   // playback a demo?
305   if(playback_demo_stream != 0) {
306     demo_controller->update();
307     char left = false;
308     char right = false;
309     char up = false;
310     char down = false;
311     char jump = false;
312     char action = false;
313     playback_demo_stream->get(left);
314     playback_demo_stream->get(right);
315     playback_demo_stream->get(up);
316     playback_demo_stream->get(down);
317     playback_demo_stream->get(jump);
318     playback_demo_stream->get(action);
319     demo_controller->press(Controller::LEFT, left);
320     demo_controller->press(Controller::RIGHT, right);
321     demo_controller->press(Controller::UP, up);
322     demo_controller->press(Controller::DOWN, down);
323     demo_controller->press(Controller::JUMP, jump);
324     demo_controller->press(Controller::ACTION, action);
325   }
326
327   // save input for demo?
328   if(capture_demo_stream != 0) {
329     capture_demo_stream ->put(main_controller->hold(Controller::LEFT));
330     capture_demo_stream ->put(main_controller->hold(Controller::RIGHT));
331     capture_demo_stream ->put(main_controller->hold(Controller::UP));
332     capture_demo_stream ->put(main_controller->hold(Controller::DOWN));
333     capture_demo_stream ->put(main_controller->hold(Controller::JUMP));   
334     capture_demo_stream ->put(main_controller->hold(Controller::ACTION));
335   }
336 }
337
338 bool
339 GameSession::consoleCommand(std::string command)
340 {
341   if (command == "foo") {
342     msg_info("bar");
343     return true;
344   }
345
346   //TODO: Build command list automatically
347   if (command == "cmdlist") {
348     msg_info("foo, cmdlist, cheats, whereami, camera");
349     return true;
350   }
351   //TODO: remove (or at least hide) this before release
352   if (command == "cheats") {
353     msg_info("grow, fire, ice, lifeup, numberofthebeast, lifedown, grease, invincible, mortal, shrink, kill, gotoend, flip, finish");
354     return true;
355   }
356
357   if (currentsector == 0) return false;
358   Player& tux = *currentsector->player;
359   
360   // Cheating words (the goal of this is really for debugging,
361   // but could be used for some cheating, nothing wrong with that)
362   if (command == "grow") {
363     tux.set_bonus(GROWUP_BONUS, false);
364     return true;
365   }
366   if (command == "fire") {
367     tux.set_bonus(FIRE_BONUS, false);
368     return true;
369   }
370   if (command == "ice") {
371     tux.set_bonus(ICE_BONUS, false);
372     return true;
373   }
374   if (command == "lifeup") {
375     player_status->incLives();
376     return true;
377   }
378   if (command == "numberofthebeast") {
379     player_status->coins = 55;
380     return true;
381   }
382   if (command == "lifedown") {
383     player_status->coins = std::max(player_status->coins-25, 0);
384     return true;
385   }
386   if (command == "grease") {
387     tux.physic.set_velocity_x(tux.physic.get_velocity_x()*3);
388     return true;
389   }
390   if (command == "invincible") {
391     // be invincle for the rest of the level
392     tux.invincible_timer.start(10000);
393     return true;
394   }
395   if (command == "mortal") {
396     // give up invincibility
397     tux.invincible_timer.stop();
398     return true;
399   }
400   if (command == "shrink") {
401     // remove powerups
402     tux.kill(tux.SHRINK);
403     return true;
404   }
405   if (command == "kill") {
406     tux.kill(tux.KILL);
407     return true;
408   }
409   if (command == "whereami") {
410     msg_info("You are at x " << tux.get_pos().x << ", y " << tux.get_pos().y);
411     return true;
412   }
413 #if 0
414   if(command == "grid")) {
415     // toggle debug grid
416     debug_grid = !debug_grid;
417     return true;
418   }
419 #endif
420   if (command == "gotoend") {
421     // goes to the end of the level
422     tux.move(Vector(
423           (currentsector->solids->get_width()*32) - (SCREEN_WIDTH*2), 0));
424     currentsector->camera->reset(
425         Vector(tux.get_pos().x, tux.get_pos().y));
426     return true;
427   }
428   if (command == "flip") {
429         FlipLevelTransformer flip_transformer;
430     flip_transformer.transform(GameSession::current()->get_current_level());
431     return true;
432   }
433   if (command == "finish") {
434     // finish current sector
435     exit_status = ES_LEVEL_FINISHED;
436     // don't add points to stats though...
437     return true;
438   }
439   if (command == "camera") {
440     msg_info("Camera is at " 
441               << Sector::current()->camera->get_translation().x << "," 
442               << Sector::current()->camera->get_translation().y);
443     return true;
444   }
445
446   return false;
447 }
448
449 void
450 GameSession::check_end_conditions()
451 {
452   Player* tux = currentsector->player;
453
454   /* End of level? */
455   if(end_sequence && endsequence_timer.check()) {
456     exit_status = ES_LEVEL_FINISHED;
457     
458     // add time spent to statistics
459     int tottime = 0, remtime = 0;
460     for(std::vector<Sector*>::iterator i = level->sectors.begin(); i != level->sectors.end(); ++i)
461     {
462       Sector* sec = *i;
463
464       for(std::vector<GameObject*>::iterator j = sec->gameobjects.begin();
465           j != sec->gameobjects.end(); ++j)
466       {
467         GameObject* obj = *j;
468
469         LevelTime* lt = dynamic_cast<LevelTime*> (obj);
470         if(lt)
471         {
472           tottime += int(lt->get_level_time());
473           remtime += int(lt->get_remaining_time());
474         }
475       }
476     }
477     global_stats.set_points(TIME_NEEDED_STAT, (tottime == 0 ? -1 : (tottime-remtime)));
478
479     return;
480   } else if (!end_sequence && tux->is_dead()) {
481     if (player_status->coins < 0) { 
482       // No more coins: restart level from beginning
483       player_status->coins = 0;
484       restart_level(true);
485     } else { 
486       // Still has coins: restart level from last reset point
487       restart_level(false);
488     }
489     
490     return;
491   }
492 }
493
494 void
495 GameSession::update(float elapsed_time)
496 {
497   // handle controller
498   if(main_controller->pressed(Controller::PAUSE_MENU))
499     on_escape_press();
500   
501   // advance timers
502   if(!currentsector->player->growing_timer.started()) {
503     // Update Tux and the World
504     currentsector->update(elapsed_time);
505   }
506
507   // respawning in new sector?
508   if(newsector != "" && newspawnpoint != "") {
509     Sector* sector = level->get_sector(newsector);
510     if(sector == 0) {
511       msg_warning("Sector '" << newsector << "' not found");
512     }
513     sector->activate(newspawnpoint);
514     sector->play_music(LEVEL_MUSIC);
515     currentsector = sector;
516     newsector = "";
517     newspawnpoint = "";
518   }
519
520   // update sounds
521   sound_manager->set_listener_position(currentsector->player->get_pos());
522 }
523
524 void 
525 GameSession::draw()
526 {
527   currentsector->draw(*context);
528   drawstatus(*context);
529
530   if(game_pause)
531     draw_pause();
532
533   if(Menu::current()) {
534     Menu::current()->draw(*context);
535   }
536
537   console->draw();
538   context->do_drawing();
539 }
540
541 void
542 GameSession::draw_pause()
543 {
544   context->draw_filled_rect(
545       Vector(0,0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
546       Color(.2, .2, .2, .5), LAYER_FOREGROUND1);
547 }
548   
549 void
550 GameSession::process_menu()
551 {
552   Menu* menu = Menu::current();
553   if(menu) {
554     menu->update();
555
556     if(menu == game_menu) {
557       switch (game_menu->check()) {
558         case MNID_CONTINUE:
559           Menu::set_current(0);
560           break;
561         case MNID_ABORTLEVEL:
562           Menu::set_current(0);
563           exit_status = ES_LEVEL_ABORT;
564           break;
565       }
566     } else if(menu == options_menu) {
567       process_options_menu();
568     } else if(menu == load_game_menu ) {
569       process_load_game_menu();
570     }
571   }
572 }
573
574
575 GameSession::ExitStatus
576 GameSession::run()
577 {
578   Menu::set_current(0);
579   current_ = this;
580   
581   int fps_cnt = 0;
582
583   // Eat unneeded events
584   SDL_Event event;
585   while(SDL_PollEvent(&event))
586   {}
587
588   draw();
589
590   Uint32 fps_ticks = SDL_GetTicks();
591   Uint32 fps_nextframe_ticks = SDL_GetTicks();
592   Uint32 ticks;
593   bool skipdraw = false;
594
595   while (exit_status == ES_NONE) {
596     // we run in a logical framerate so elapsed time is a constant
597     static const float elapsed_time = 1.0 / LOGICAL_FPS;
598     // old code... float elapsed_time = float(ticks - lastticks) / 1000.;
599     if(!game_pause)
600       game_time += elapsed_time;
601
602     // regulate fps
603     ticks = SDL_GetTicks();
604     if(ticks > fps_nextframe_ticks) {
605       if(skipdraw == true) {
606         // already skipped last frame? we have to slow down the game then...
607         skipdraw = false;
608         fps_nextframe_ticks -= (Uint32) (1000.0 / LOGICAL_FPS);
609       } else {
610         // don't draw all frames when we're getting too slow
611         skipdraw = true;
612       }
613     } else {
614       skipdraw = false;
615       while(fps_nextframe_ticks > ticks) {
616         /* just wait */
617         // If we really have to wait long, then do an imprecise SDL_Delay()
618         Uint32 diff = fps_nextframe_ticks - ticks;
619         if(diff > 15) {
620           SDL_Delay(diff - 10);
621         } 
622         ticks = SDL_GetTicks();
623       }
624     }
625     fps_nextframe_ticks = ticks + (Uint32) (1000.0 / LOGICAL_FPS);
626
627 #if 0
628     float diff = SDL_GetTicks() - fps_nextframe_ticks;
629     if (diff > 5.0) {
630          // sets the ticks that must have elapsed
631         fps_nextframe_ticks = SDL_GetTicks() + (1000.0 / LOGICAL_FPS);
632     } else {
633         // sets the ticks that must have elapsed
634         // in order for the next frame to start.
635         fps_nextframe_ticks += 1000.0 / LOGICAL_FPS;
636     }
637 #endif
638
639     process_events();
640     process_menu();
641
642     // Update the world state and all objects in the world
643     // Do that with a constante time-delta so that the game will run
644     // determistic and not different on different machines
645     if(!game_pause && !Menu::current())
646     {
647       // Update the world
648       check_end_conditions();
649       if (end_sequence == ENDSEQUENCE_RUNNING)
650         update(elapsed_time/2);
651       else if(end_sequence == NO_ENDSEQUENCE)
652         update(elapsed_time);
653     }
654     else
655     {
656       ++pause_menu_frame;
657     }
658
659     if(!skipdraw)
660       draw();
661
662     // update sounds
663     sound_manager->update();
664
665     /* Time stops in pause mode */
666     if(game_pause || Menu::current())
667     {
668       continue;
669     }
670
671     //frame_rate.update();
672     
673     /* Handle music: */
674     if (currentsector->player->invincible_timer.started() && 
675             currentsector->player->invincible_timer.get_timeleft() 
676             > TUX_INVINCIBLE_TIME_WARNING && !end_sequence)
677     {
678       currentsector->play_music(HERRING_MUSIC);
679     }
680     /* or just normal music? */
681     else if(currentsector->get_music_type() != LEVEL_MUSIC && !end_sequence)
682     {
683       currentsector->play_music(LEVEL_MUSIC);
684     }
685
686     /* Calculate frames per second */
687     if(config->show_fps)
688     {
689       ++fps_cnt;
690       
691       if(SDL_GetTicks() - fps_ticks >= 500)
692       {
693         fps_fps = (float) fps_cnt / .5;
694         fps_cnt = 0;
695         fps_ticks = SDL_GetTicks();
696       }
697     }
698   }
699  
700   // just in case
701   currentsector = 0;
702   main_controller->reset();
703   return exit_status;
704 }
705
706 void
707 GameSession::finish(bool win)
708 {
709   if(win)
710     exit_status = ES_LEVEL_FINISHED;
711   else
712     exit_status = ES_LEVEL_ABORT;
713 }
714
715 void
716 GameSession::respawn(const std::string& sector, const std::string& spawnpoint)
717 {
718   newsector = sector;
719   newspawnpoint = spawnpoint;
720 }
721
722 void
723 GameSession::set_reset_point(const std::string& sector, const Vector& pos)
724 {
725   reset_sector = sector;
726   reset_pos = pos;
727 }
728
729 std::string
730 GameSession::get_working_directory()
731 {
732   return FileSystem::dirname(levelfile);
733 }
734
735 void
736 GameSession::display_info_box(const std::string& text)
737 {
738   InfoBox* box = new InfoBox(text);
739
740   bool running = true;
741   while(running)  {
742
743     main_controller->update();
744     SDL_Event event;
745     while (SDL_PollEvent(&event)) {
746       main_controller->process_event(event);
747       if(event.type == SDL_QUIT)
748         throw graceful_shutdown();
749     }
750
751     if(main_controller->pressed(Controller::JUMP)
752         || main_controller->pressed(Controller::ACTION)
753         || main_controller->pressed(Controller::PAUSE_MENU)
754         || main_controller->pressed(Controller::MENU_SELECT))
755       running = false;
756     else if(main_controller->pressed(Controller::DOWN))
757       box->scrolldown();
758     else if(main_controller->pressed(Controller::UP))
759       box->scrollup();
760     box->draw(*context);
761     draw();
762     sound_manager->update();
763   }
764
765   delete box;
766 }
767
768 void
769 GameSession::start_sequence(const std::string& sequencename)
770 {
771   if(sequencename == "endsequence" || sequencename == "fireworks") {
772     if(end_sequence)
773       return;
774
775     end_sequence = ENDSEQUENCE_RUNNING;
776     endsequence_timer.start(7.3);
777     last_x_pos = -1;
778     sound_manager->play_music("music/leveldone.ogg", false);
779     currentsector->player->invincible_timer.start(7.3);
780
781     // Stop all clocks.
782     for(std::vector<GameObject*>::iterator i = currentsector->gameobjects.begin();
783         i != currentsector->gameobjects.end(); ++i)
784     {
785       GameObject* obj = *i;
786
787       LevelTime* lt = dynamic_cast<LevelTime*> (obj);
788       if(lt)
789         lt->stop();
790     }
791
792     if(sequencename == "fireworks") {
793       currentsector->add_object(new Fireworks());
794     }
795   } else if(sequencename == "stoptux") {
796     if(!end_sequence) {
797       msg_warning("Final target reached without "
798         << "an active end sequence");
799       this->start_sequence("endsequence");
800     }
801     end_sequence =  ENDSEQUENCE_WAITING;
802   } else {
803     msg_warning("Unknown sequence '" << sequencename << "'");
804   }
805 }
806
807 /* (Status): */
808 void
809 GameSession::drawstatus(DrawingContext& context)
810 {
811   player_status->draw(context);
812
813   if(config->show_fps) {
814     char str[60];
815     snprintf(str, sizeof(str), "%3.1f", fps_fps);
816     const char* fpstext = "FPS";
817     context.draw_text(white_text, fpstext, Vector(SCREEN_WIDTH - white_text->get_text_width(fpstext) - gold_text->get_text_width(" 99999") - BORDER_X, BORDER_Y + 20), LEFT_ALLIGN, LAYER_FOREGROUND1);
818     context.draw_text(gold_text, str, Vector(SCREEN_WIDTH - BORDER_X, BORDER_Y + 20), RIGHT_ALLIGN, LAYER_FOREGROUND1);
819   }
820 }
821
822 void
823 GameSession::drawresultscreen()
824 {
825   char str[80];
826
827   DrawingContext context;
828   for(Sector::GameObjects::iterator i = currentsector->gameobjects.begin();
829       i != currentsector->gameobjects.end(); ++i) {
830     Background* background = dynamic_cast<Background*> (*i);
831     if(background) {
832       background->draw(context);
833     }
834   }
835
836   context.draw_text(blue_text, _("Result:"), Vector(SCREEN_WIDTH/2, 200),
837       CENTER_ALLIGN, LAYER_FOREGROUND1);
838
839 //  sprintf(str, _("SCORE: %d"), global_stats.get_points(SCORE_STAT));
840 //  context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
841
842   // y == 256 before removal of score
843   sprintf(str, _("COINS: %d"), player_status->coins);
844   context.draw_text(gold_text, str, Vector(SCREEN_WIDTH/2, 224), CENTER_ALLIGN, LAYER_FOREGROUND1);
845
846   console->draw();
847   context.do_drawing();
848   
849   wait_for_event(2.0, 5.0);
850 }
851
852 std::string slotinfo(int slot)
853 {
854   std::string tmp;
855   std::string slotfile;
856   std::string title;
857   std::stringstream stream;
858   stream << slot;
859   slotfile = "save/slot" + stream.str() + ".stsg";
860
861   try {
862     lisp::Parser parser;
863     std::auto_ptr<lisp::Lisp> root (parser.parse(slotfile));
864
865     const lisp::Lisp* savegame = root->get_lisp("supertux-savegame");
866     if(!savegame)
867       throw std::runtime_error("file is not a supertux-savegame.");
868
869     savegame->get("title", title);
870   } catch(std::exception& e) {
871     return std::string(_("Slot")) + " " + stream.str() + " - " +
872       std::string(_("Free"));
873   }
874
875   return std::string("Slot ") + stream.str() + " - " + title;
876 }
877
878 bool process_load_game_menu()
879 {
880   int slot = load_game_menu->check();
881
882   if(slot == -1)
883     return false;
884   
885   if(load_game_menu->get_item_by_id(slot).kind != MN_ACTION)
886     return false;
887   
888   std::stringstream stream;
889   stream << slot;
890   std::string slotfile = "save/slot" + stream.str() + ".stsg";
891
892   sound_manager->stop_music();
893   fadeout(256);
894   DrawingContext context;
895   context.draw_text(white_text, "Loading...",
896                     Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2),
897                     CENTER_ALLIGN, LAYER_FOREGROUND1);
898   context.do_drawing();
899
900   WorldMapNS::WorldMap worldmap;
901
902   worldmap.set_map_filename("/levels/world1/worldmap.stwm");
903   // Load the game or at least set the savegame_file variable
904   worldmap.loadgame(slotfile);
905
906   worldmap.display();
907
908   Menu::set_current(main_menu);
909
910   return true;
911 }