93090a9fc3d7a57adb3517019aa4ba0e15cfdcc1
[supertux.git] / src / worldmap.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <iostream>
21 #include <fstream>
22 #include <vector>
23 #include <assert.h>
24 #include <unistd.h>
25 #include "globals.h"
26 #include "screen/texture.h"
27 #include "screen/screen.h"
28 #include "screen/drawing_context.h"
29 #include "lispreader.h"
30 #include "gameloop.h"
31 #include "setup.h"
32 #include "sector.h"
33 #include "worldmap.h"
34 #include "sound_manager.h"
35 #include "resources.h"
36 #include "gettext.h"
37
38 namespace WorldMapNS {
39
40 Direction reverse_dir(Direction direction)
41 {
42   switch(direction)
43     {
44     case D_WEST:
45       return D_EAST;
46     case D_EAST:
47       return D_WEST;
48     case D_NORTH:
49       return D_SOUTH;
50     case D_SOUTH:
51       return D_NORTH;
52     case D_NONE:
53       return D_NONE;
54     }
55   return D_NONE;
56 }
57
58 std::string
59 direction_to_string(Direction direction)
60 {
61   switch(direction)
62     {
63     case D_WEST:
64       return "west";
65     case D_EAST:
66       return "east";
67     case D_NORTH:
68       return "north";
69     case D_SOUTH:
70       return "south";
71     default:
72       return "none";
73     }
74 }
75
76 Direction
77 string_to_direction(const std::string& directory)
78 {
79   if (directory == "west")
80     return D_WEST;
81   else if (directory == "east")
82     return D_EAST;
83   else if (directory == "north")
84     return D_NORTH;
85   else if (directory == "south")
86     return D_SOUTH;
87   else
88     return D_NONE;
89 }
90
91 TileManager::TileManager()
92 {
93   std::string stwt_filename = datadir +  "/images/worldmap/antarctica.stwt";
94   lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
95  
96   if (!root_obj)
97     st_abort("Couldn't load file", stwt_filename);
98
99   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
100     {
101       lisp_object_t* cur = lisp_cdr(root_obj);
102
103       while(!lisp_nil_p(cur))
104         {
105           lisp_object_t* element = lisp_car(cur);
106
107           if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
108             {
109               int id = 0;
110               std::string filename = "<invalid>";
111
112               Tile* tile = new Tile;             
113               tile->north = true;
114               tile->east  = true;
115               tile->south = true;
116               tile->west  = true;
117               tile->stop  = true;
118               tile->auto_walk = false;
119   
120               LispReader reader(lisp_cdr(element));
121               reader.read_int("id", id);
122               reader.read_bool("north", tile->north);
123               reader.read_bool("south", tile->south);
124               reader.read_bool("west",  tile->west);
125               reader.read_bool("east",  tile->east);
126               reader.read_bool("stop",  tile->stop);
127               reader.read_bool("auto-walk",  tile->auto_walk);
128               reader.read_string("image", filename);
129
130               tile->sprite = new Surface(
131                            datadir +  "/images/worldmap/" + filename, 
132                            USE_ALPHA);
133
134               if (id >= int(tiles.size()))
135                 tiles.resize(id+1);
136
137               tiles[id] = tile;
138             }
139           else
140             {
141               puts("Unhandled symbol");
142             }
143
144           cur = lisp_cdr(cur);
145         }
146     }
147   else
148     {
149       assert(0);
150     }
151
152   lisp_free(root_obj);
153 }
154
155 TileManager::~TileManager()
156 {
157   for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
158     delete *i;
159 }
160
161 Tile*
162 TileManager::get(int i)
163 {
164   assert(i >=0 && i < int(tiles.size()));
165   return tiles[i];
166 }
167
168 //---------------------------------------------------------------------------
169
170 Tux::Tux(WorldMap* worldmap_)
171   : worldmap(worldmap_)
172 {
173   largetux_sprite = new Surface(datadir +  "/images/worldmap/tux.png", USE_ALPHA);
174   firetux_sprite = new Surface(datadir +  "/images/worldmap/firetux.png", USE_ALPHA);
175   smalltux_sprite = new Surface(datadir +  "/images/worldmap/smalltux.png", USE_ALPHA);
176
177   offset = 0;
178   moving = false;
179   tile_pos.x = 4;
180   tile_pos.y = 5;
181   direction = D_NONE;
182   input_direction = D_NONE;
183 }
184
185 Tux::~Tux()
186 {
187   delete smalltux_sprite;
188   delete firetux_sprite;
189   delete largetux_sprite;
190 }
191
192 void
193 Tux::draw(DrawingContext& context, const Vector& offset)
194 {
195   Vector pos = get_pos();
196   switch (player_status.bonus)
197     {
198     case PlayerStatus::GROWUP_BONUS:
199       context.draw_surface(largetux_sprite,
200           Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
201       break;
202     case PlayerStatus::FLOWER_BONUS:
203       context.draw_surface(firetux_sprite,
204           Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
205       break;
206     case PlayerStatus::NO_BONUS:
207       context.draw_surface(smalltux_sprite,
208           Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
209       break;
210     }
211 }
212
213
214 Vector
215 Tux::get_pos()
216 {
217   float x = tile_pos.x * 32;
218   float y = tile_pos.y * 32;
219
220   switch(direction)
221     {
222     case D_WEST:
223       x -= offset - 32;
224       break;
225     case D_EAST:
226       x += offset - 32;
227       break;
228     case D_NORTH:
229       y -= offset - 32;
230       break;
231     case D_SOUTH:
232       y += offset - 32;
233       break;
234     case D_NONE:
235       break;
236     }
237   
238   return Vector((int)x, (int)y); 
239 }
240
241 void
242 Tux::stop()
243 {
244   offset = 0;
245   direction = D_NONE;
246   moving = false;
247 }
248
249 void
250 Tux::action(float delta)
251 {
252   if (!moving)
253     {
254       if (input_direction != D_NONE)
255         { 
256           WorldMap::Level* level = worldmap->at_level();
257
258           // We got a new direction, so lets start walking when possible
259           Vector next_tile;
260           if ((!level || level->solved)
261               && worldmap->path_ok(input_direction, tile_pos, &next_tile))
262             {
263               tile_pos = next_tile;
264               moving = true;
265               direction = input_direction;
266               back_direction = reverse_dir(direction);
267             }
268           else if (input_direction == back_direction)
269             {
270               std::cout << "Back triggered" << std::endl;
271               moving = true;
272               direction = input_direction;
273               tile_pos = worldmap->get_next_tile(tile_pos, direction);
274               back_direction = reverse_dir(direction);
275             }
276         }
277     }
278   else
279     {
280       // Let tux walk a few pixels (20 pixel/sec)
281       offset += 20.0f * delta;
282
283       if (offset > 32)
284         { // We reached the next tile, so we check what to do now
285           offset -= 32;
286
287           if (worldmap->at(tile_pos)->stop || worldmap->at_level())
288             {
289               stop();
290             }
291           else
292             {
293               if (worldmap->at(tile_pos)->auto_walk)
294                 { // Turn to a new direction
295                   Tile* tile = worldmap->at(tile_pos);
296                   Direction dir = D_NONE;
297                   
298                   if (tile->north && back_direction != D_NORTH)
299                     dir = D_NORTH;
300                   else if (tile->south && back_direction != D_SOUTH)
301                     dir = D_SOUTH;
302                   else if (tile->east && back_direction != D_EAST)
303                     dir = D_EAST;
304                   else if (tile->west && back_direction != D_WEST)
305                     dir = D_WEST;
306
307                   if (dir != D_NONE)
308                     {
309                       direction = dir;
310                       back_direction = reverse_dir(direction);
311                     }
312                   else
313                     {
314                       // Should never be reached if tiledata is good
315                       stop();
316                       return;
317                     }
318                 }
319
320               // Walk automatically to the next tile
321               Vector next_tile;
322               if (worldmap->path_ok(direction, tile_pos, &next_tile))
323                 {
324                   tile_pos = next_tile;
325                 }
326               else
327                 {
328                   puts("Tilemap data is buggy");
329                   stop();
330                 }
331             }
332         }
333     }
334 }
335
336 //---------------------------------------------------------------------------
337 Tile::Tile()
338 {
339 }
340
341 Tile::~Tile()
342 {
343   delete sprite;
344 }
345
346 //---------------------------------------------------------------------------
347
348 WorldMap::WorldMap()
349 {
350   tile_manager = new TileManager();
351   tux = new Tux(this);
352
353   width  = 20;
354   height = 15;
355
356   level_sprite = new Surface(datadir +  "/images/worldmap/levelmarker.png", USE_ALPHA);
357   leveldot_green = new Surface(datadir +  "/images/worldmap/leveldot_green.png", USE_ALPHA);
358   leveldot_red = new Surface(datadir +  "/images/worldmap/leveldot_red.png", USE_ALPHA);
359
360   input_direction = D_NONE;
361   enter_level = false;
362
363   name = "<no file>";
364   music = "SALCON.MOD";
365 }
366
367 WorldMap::~WorldMap()
368 {
369   delete tux;
370   delete tile_manager;
371
372   delete level_sprite;
373   delete leveldot_green;
374   delete leveldot_red;
375 }
376
377 void
378 WorldMap::load_map()
379 {
380   lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
381   if (!root_obj)
382     st_abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
383
384   std::cout << "Loading map: " << datadir + "/levels/worldmap/" + map_filename << std::endl;
385
386   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
387     {
388       lisp_object_t* cur = lisp_cdr(root_obj);
389
390       while(!lisp_nil_p(cur))
391         {
392           lisp_object_t* element = lisp_car(cur);
393
394           if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
395             {
396               LispReader reader(lisp_cdr(element));
397               reader.read_int("width",  width);
398               reader.read_int("height", height);
399               reader.read_int_vector("data", tilemap);
400             }
401           else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
402             {
403               LispReader reader(lisp_cdr(element));
404               reader.read_string("name", name);
405               reader.read_string("music", music);
406             }
407           else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
408             {
409               lisp_object_t* cur = lisp_cdr(element);
410               
411               while(!lisp_nil_p(cur))
412                 {
413                   lisp_object_t* element = lisp_car(cur);
414                   
415                   if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
416                     {
417                       Level level;
418                       LispReader reader(lisp_cdr(element));
419                       level.solved = false;
420                       
421                       level.north = true;
422                       level.east  = true;
423                       level.south = true;
424                       level.west  = true;
425
426                       reader.read_string("extro-filename", level.extro_filename);
427                       reader.read_string("name", level.name);
428                       reader.read_int("x", level.x);
429                       reader.read_int("y", level.y);
430
431                       levels.push_back(level);
432                     }
433                   
434                   cur = lisp_cdr(cur);      
435                 }
436             }
437           else
438             {
439               
440             }
441           
442           cur = lisp_cdr(cur);
443         }
444     }
445
446     lisp_free(root_obj);
447 }
448
449 void WorldMap::get_level_title(Level& level)
450 {
451   /** get level's title */
452   level.title = "<no title>";
453
454   FILE * fi;
455   lisp_object_t* root_obj = 0;
456   fi = fopen((datadir +  "/levels/" + level.name).c_str(), "r");
457   if (fi == NULL)
458   {
459     perror((datadir +  "/levels/" + level.name).c_str());
460     return;
461   }
462
463   lisp_stream_t stream;
464   lisp_stream_init_file (&stream, fi);
465   root_obj = lisp_read (&stream);
466
467   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
468   {
469     printf("World: Parse Error in file %s", level.name.c_str());
470   }
471
472   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
473   {
474     LispReader reader(lisp_cdr(root_obj));
475     reader.read_string("name", level.title);
476   }
477
478   lisp_free(root_obj);
479
480   fclose(fi);
481 }
482
483 void
484 WorldMap::on_escape_press()
485 {
486   // Show or hide the menu
487   if(!Menu::current())
488     Menu::set_current(worldmap_menu); 
489   else
490     Menu::set_current(0); 
491 }
492
493 void
494 WorldMap::get_input()
495 {
496   enter_level = false;
497   input_direction = D_NONE;
498    
499   SDL_Event event;
500   while (SDL_PollEvent(&event))
501     {
502       if (Menu::current())
503         {
504           Menu::current()->event(event);
505         }
506       else
507         {
508           switch(event.type)
509             {
510             case SDL_QUIT:
511               st_abort("Received window close", "");
512               break;
513           
514             case SDL_KEYDOWN:
515               switch(event.key.keysym.sym)
516                 {
517                 case SDLK_ESCAPE:
518                   on_escape_press();
519                   break;
520                 case SDLK_LCTRL:
521                 case SDLK_RETURN:
522                   enter_level = true;
523                   break;
524                 default:
525                   break;
526                 }
527               break;
528           
529             case SDL_JOYAXISMOTION:
530               if (event.jaxis.axis == joystick_keymap.x_axis)
531                 {
532                   if (event.jaxis.value < -joystick_keymap.dead_zone)
533                     input_direction = D_WEST;
534                   else if (event.jaxis.value > joystick_keymap.dead_zone)
535                     input_direction = D_EAST;
536                 }
537               else if (event.jaxis.axis == joystick_keymap.y_axis)
538                 {
539                   if (event.jaxis.value > joystick_keymap.dead_zone)
540                     input_direction = D_SOUTH;
541                   else if (event.jaxis.value < -joystick_keymap.dead_zone)
542                     input_direction = D_NORTH;
543                 }
544               break;
545
546             case SDL_JOYBUTTONDOWN:
547               if (event.jbutton.button == joystick_keymap.b_button)
548                 enter_level = true;
549               else if (event.jbutton.button == joystick_keymap.start_button)
550                 on_escape_press();
551               break;
552
553             default:
554               break;
555             }
556         }
557     }
558
559   if (!Menu::current())
560     {
561       Uint8 *keystate = SDL_GetKeyState(NULL);
562   
563       if (keystate[SDLK_LEFT])
564         input_direction = D_WEST;
565       else if (keystate[SDLK_RIGHT])
566         input_direction = D_EAST;
567       else if (keystate[SDLK_UP])
568         input_direction = D_NORTH;
569       else if (keystate[SDLK_DOWN])
570         input_direction = D_SOUTH;
571     }
572 }
573
574 Vector
575 WorldMap::get_next_tile(Vector pos, Direction direction)
576 {
577   switch(direction)
578     {
579     case D_WEST:
580       pos.x -= 1;
581       break;
582     case D_EAST:
583       pos.x += 1;
584       break;
585     case D_NORTH:
586       pos.y -= 1;
587       break;
588     case D_SOUTH:
589       pos.y += 1;
590       break;
591     case D_NONE:
592       break;
593     }
594   return pos;
595 }
596
597 bool
598 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
599 {
600   *new_pos = get_next_tile(old_pos, direction);
601
602   if (!(new_pos->x >= 0 && new_pos->x < width
603         && new_pos->y >= 0 && new_pos->y < height))
604     { // New position is outsite the tilemap
605       return false;
606     }
607   else
608     { // Check if we the tile allows us to go to new_pos
609       switch(direction)
610         {
611         case D_WEST:
612           return (at(old_pos)->west && at(*new_pos)->east);
613
614         case D_EAST:
615           return (at(old_pos)->east && at(*new_pos)->west);
616
617         case D_NORTH:
618           return (at(old_pos)->north && at(*new_pos)->south);
619
620         case D_SOUTH:
621           return (at(old_pos)->south && at(*new_pos)->north);
622
623         case D_NONE:
624           assert(!"path_ok() can't work if direction is NONE");
625         }
626       return false;
627     }
628 }
629
630 void
631 WorldMap::update(float delta)
632 {
633   if (enter_level && !tux->is_moving())
634     {
635       Level* level = at_level();
636       if (level)
637         {
638           if (level->x == tux->get_tile_pos().x && 
639               level->y == tux->get_tile_pos().y)
640             {
641               PlayerStatus old_player_status = player_status;
642
643               std::cout << "Enter the current level: " << level->name << std::endl;
644               // do a shriking fade to the level
645               shrink_fade(Vector((level->x*32 + 16 + offset.x),(level->y*32 + 16
646                       + offset.y)), 500);
647               GameSession session(datadir +  "/levels/" + level->name,
648                                   ST_GL_LOAD_LEVEL_FILE);
649
650               switch (session.run())
651                 {
652                 case GameSession::ES_LEVEL_FINISHED:
653                   {
654                     bool old_level_state = level->solved;
655                     level->solved = true;
656
657                     if (session.get_current_sector()->player->got_power !=
658                           session.get_current_sector()->player->NONE_POWER)
659                       player_status.bonus = PlayerStatus::FLOWER_BONUS;
660                     else if (session.get_current_sector()->player->size == BIG)
661                       player_status.bonus = PlayerStatus::GROWUP_BONUS;
662                     else
663                       player_status.bonus = PlayerStatus::NO_BONUS;
664
665                     if (old_level_state != level->solved)
666                       { // Try to detect the next direction to which we should walk
667                         // FIXME: Mostly a hack
668                         Direction dir = D_NONE;
669                     
670                         Tile* tile = at(tux->get_tile_pos());
671
672                         if (tile->north && tux->back_direction != D_NORTH)
673                           dir = D_NORTH;
674                         else if (tile->south && tux->back_direction != D_SOUTH)
675                           dir = D_SOUTH;
676                         else if (tile->east && tux->back_direction != D_EAST)
677                           dir = D_EAST;
678                         else if (tile->west && tux->back_direction != D_WEST)
679                           dir = D_WEST;
680
681                         if (dir != D_NONE)
682                           {
683                             tux->set_direction(dir);
684                             //tux->update(delta);
685                           }
686
687                         std::cout << "Walk to dir: " << dir << std::endl;
688                       }
689
690                     if (!level->extro_filename.empty())
691                       { 
692                         MusicRef theme =
693                           sound_manager->load_music(datadir + "/music/theme.mod");
694                         sound_manager->play_music(theme);
695                         // Display final credits and go back to the main menu
696                         display_text_file(level->extro_filename,
697                                           "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
698                         display_text_file("CREDITS",
699                                           "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
700                         quit = true;
701                       }
702                   }
703
704                   break;
705                 case GameSession::ES_LEVEL_ABORT:
706                   /* In case the player's abort the level, keep it using the old
707                       status */
708                   player_status = old_player_status;
709                   break;
710                 case GameSession::ES_GAME_OVER:
711                 {
712                   /* draw an end screen */
713                   /* in the future, this should make a dialog a la SuperMario, asking
714                   if the player wants to restart the world map with no score and from
715                   level 1 */
716                   char str[80];
717
718                   DrawingContext context;
719                   context.draw_gradient(Color (0, 255, 0), Color (255, 0, 255),
720                       LAYER_BACKGROUND0);
721
722                   context.draw_text_center(blue_text, _("GAMEOVER"), 
723                       Vector(0, 200), LAYER_FOREGROUND1);
724
725                   sprintf(str, _("SCORE: %d"), player_status.score);
726                   context.draw_text_center(gold_text, str,
727                       Vector(0, 224), LAYER_FOREGROUND1);
728
729                   sprintf(str, _("COINS: %d"), player_status.distros);
730                   context.draw_text_center(gold_text, str,
731                       Vector(0, screen->w - 32), LAYER_FOREGROUND1);
732
733                   context.do_drawing();
734   
735                   SDL_Event event;
736                   wait_for_event(event,2000,5000,true);
737
738                   quit = true;
739                   player_status.reset();
740                   break;
741                 }
742                 case GameSession::ES_NONE:
743                   assert(false);
744                   // Should never be reached 
745                   break;
746                 }
747
748               sound_manager->play_music(song);
749               Menu::set_current(0);
750               if (!savegame_file.empty())
751                 savegame(savegame_file);
752               return;
753             }
754         }
755       else
756         {
757           std::cout << "Nothing to enter at: "
758                     << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
759         }
760     }
761   else
762     {
763       tux->action(delta);
764       tux->set_direction(input_direction);
765     }
766   
767   Menu* menu = Menu::current();
768   if(menu)
769     {
770       menu->action();
771
772       if(menu == worldmap_menu)
773         {
774           switch (worldmap_menu->check())
775             {
776             case MNID_RETURNWORLDMAP: // Return to game
777               break;
778             case MNID_QUITWORLDMAP: // Quit Worldmap
779               quit = true;
780               break;
781             }
782         }
783       else if(menu == options_menu)
784         {
785           process_options_menu();
786         }
787     }
788 }
789
790 Tile*
791 WorldMap::at(Vector p)
792 {
793   assert(p.x >= 0 
794          && p.x < width
795          && p.y >= 0
796          && p.y < height);
797
798   int x = int(p.x);
799   int y = int(p.y);
800   return tile_manager->get(tilemap[width * y + x]);
801 }
802
803 WorldMap::Level*
804 WorldMap::at_level()
805 {
806   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
807     {
808       if (i->x == tux->get_tile_pos().x && 
809           i->y == tux->get_tile_pos().y)
810         return &*i; 
811     }
812
813   return 0;
814 }
815
816
817 void
818 WorldMap::draw(DrawingContext& context, const Vector& offset)
819 {
820   for(int y = 0; y < height; ++y)
821     for(int x = 0; x < width; ++x)
822       {
823         Tile* tile = at(Vector(x, y));
824         context.draw_surface(tile->sprite,
825             Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
826       }
827   
828   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
829     {
830       if (i->solved)
831         context.draw_surface(leveldot_green,
832             Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
833       else
834         context.draw_surface(leveldot_red,
835             Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
836     }
837
838   tux->draw(context, offset);
839   draw_status(context);
840 }
841
842 void
843 WorldMap::draw_status(DrawingContext& context)
844 {
845   char str[80];
846   sprintf(str, " %d", player_status.score);
847
848   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
849   context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
850
851   sprintf(str, "%d", player_status.distros);
852   context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
853       LAYER_FOREGROUND1);
854   context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
855         LAYER_FOREGROUND1);
856
857   if (player_status.lives >= 5)
858     {
859       sprintf(str, "%dx", player_status.lives);
860       context.draw_text(gold_text, str, 
861           Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
862           LAYER_FOREGROUND1);
863       context.draw_surface(tux_life, Vector(screen->w -
864             gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
865     }
866   else
867     {
868       for(int i= 0; i < player_status.lives; ++i)
869         context.draw_surface(tux_life,
870             Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
871             LAYER_FOREGROUND1);
872     }
873   context.draw_text(white_text, _("LIVES"),
874       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 0),
875       LAYER_FOREGROUND1);
876
877   if (!tux->is_moving())
878     {
879       for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
880         {
881           if (i->x == tux->get_tile_pos().x && 
882               i->y == tux->get_tile_pos().y)
883             {
884               if(i->title == "")
885                 get_level_title(*i);
886
887               context.draw_text(white_text, i->title, 
888                   Vector(screen->w/2 - white_text->get_text_width(i->title)/2,
889                          screen->h - white_text->get_height() - 30),
890                   LAYER_FOREGROUND1);
891               break;
892             }
893         }
894     }
895 }
896
897 void
898 WorldMap::display()
899 {
900   Menu::set_current(0);
901
902   quit = false;
903
904   song = sound_manager->load_music(datadir +  "/music/" + music);
905   sound_manager->play_music(song);
906   
907   unsigned int last_update_time;
908   unsigned int update_time;
909
910   last_update_time = update_time = st_get_ticks();
911
912   DrawingContext context;
913   while(!quit)
914     {
915       float delta = ((float)(update_time-last_update_time))/100.0;
916
917       delta *= 1.3f;
918
919       if (delta > 10.0f)
920         delta = .3f;
921       
922       last_update_time = update_time;
923       update_time      = st_get_ticks();
924
925       Vector tux_pos = tux->get_pos();
926       if (1)
927         {
928           offset.x = -tux_pos.x + screen->w/2;
929           offset.y = -tux_pos.y + screen->h/2;
930
931           if (offset.x > 0) offset.x = 0;
932           if (offset.y > 0) offset.y = 0;
933
934           if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
935           if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
936         } 
937
938       draw(context, offset);
939       get_input();
940       update(delta);
941
942       if(Menu::current())
943         {
944           Menu::current()->draw(context);
945           mouse_cursor->draw(context);
946         }
947
948       context.do_drawing();
949
950       SDL_Delay(20);
951     }
952 }
953
954 void
955 WorldMap::savegame(const std::string& filename)
956 {
957   std::cout << "savegame: " << filename << std::endl;
958   std::ofstream out(filename.c_str());
959
960   int nb_solved_levels = 0;
961   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
962     {
963       if (i->solved)
964         ++nb_solved_levels;
965     }
966
967   out << "(supertux-savegame\n"
968       << "  (version 1)\n"
969       << "  (title  \"" << name << " - " << nb_solved_levels << "/" << levels.size() << "\")\n"
970       << "  (map    \"" << map_filename << "\")\n"
971       << "  (lives   " << player_status.lives << ")\n"
972       << "  (score   " << player_status.score << ")\n"
973       << "  (distros " << player_status.distros << ")\n"
974       << "  (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
975       << "       (back \"" << direction_to_string(tux->back_direction) << "\")\n"
976       << "       (bonus \"" << bonus_to_string(player_status.bonus) <<  "\"))\n"
977       << "  (levels\n";
978   
979   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
980     {
981       if (i->solved)
982         {
983           out << "     (level (name \"" << i->name << "\")\n"
984               << "            (solved #t))\n";
985         }
986     }  
987
988   out << "   )\n"
989       << " )\n\n;; EOF ;;" << std::endl;
990 }
991
992 void
993 WorldMap::loadgame(const std::string& filename)
994 {
995   std::cout << "loadgame: " << filename << std::endl;
996   savegame_file = filename;
997   map_filename = "icyisland.stwm";
998
999   if (access(filename.c_str(), F_OK) != 0)
1000     {
1001     load_map();
1002     return;
1003     }
1004   
1005   lisp_object_t* savegame = lisp_read_from_file(filename);
1006   if (!savegame)
1007     {
1008       std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1009       load_map();
1010       return;
1011     }
1012
1013   lisp_object_t* cur = savegame;
1014
1015   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1016     {
1017     load_map();
1018     return;
1019     }
1020
1021   cur = lisp_cdr(cur);
1022   LispReader reader(cur);
1023
1024   /* Get the Map filename and then load it before setting level settings */
1025   reader.read_string("map", map_filename);
1026   load_map(); 
1027
1028   reader.read_int("lives", player_status.lives);
1029   reader.read_int("score", player_status.score);
1030   reader.read_int("distros", player_status.distros);
1031
1032   if (player_status.lives < 0)
1033     player_status.lives = START_LIVES;
1034
1035   lisp_object_t* tux_cur = 0;
1036   if (reader.read_lisp("tux", tux_cur))
1037     {
1038       Vector p;
1039       std::string back_str = "none";
1040       std::string bonus_str = "none";
1041
1042       LispReader tux_reader(tux_cur);
1043       tux_reader.read_float("x", p.x);
1044       tux_reader.read_float("y", p.y);
1045       tux_reader.read_string("back", back_str);
1046       tux_reader.read_string("bonus", bonus_str);
1047       
1048       player_status.bonus = string_to_bonus(bonus_str);
1049       tux->back_direction = string_to_direction(back_str);      
1050       tux->set_tile_pos(p);
1051     }
1052
1053   lisp_object_t* level_cur = 0;
1054   if (reader.read_lisp("levels", level_cur))
1055     {
1056       while(level_cur)
1057         {
1058           lisp_object_t* sym  = lisp_car(lisp_car(level_cur));
1059           lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1060
1061           if (strcmp(lisp_symbol(sym), "level") == 0)
1062             {
1063               std::string name;
1064               bool solved = false;
1065
1066               LispReader level_reader(data);
1067               level_reader.read_string("name", name);
1068               level_reader.read_bool("solved", solved);
1069
1070               for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1071                 {
1072                   if (name == i->name)
1073                     i->solved = solved;
1074                 }
1075             }
1076
1077           level_cur = lisp_cdr(level_cur);
1078         }
1079     }
1080  
1081   lisp_free(savegame);
1082 }
1083
1084 } // namespace WorldMapNS
1085
1086 /* Local Variables: */
1087 /* mode:c++ */
1088 /* End: */
1089