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