- added missing include
[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 "texture.h"
27 #include "screen.h"
28 #include "lispreader.h"
29 #include "gameloop.h"
30 #include "setup.h"
31 #include "worldmap.h"
32 #include "resources.h"
33
34 namespace WorldMapNS {
35
36 Direction reverse_dir(Direction direction)
37 {
38   switch(direction)
39     {
40     case D_WEST:
41       return D_EAST;
42     case D_EAST:
43       return D_WEST;
44     case D_NORTH:
45       return D_SOUTH;
46     case D_SOUTH:
47       return D_NORTH;
48     case D_NONE:
49       return D_NONE;
50     }
51   return D_NONE;
52 }
53
54 std::string
55 direction_to_string(Direction direction)
56 {
57   switch(direction)
58     {
59     case D_WEST:
60       return "west";
61     case D_EAST:
62       return "east";
63     case D_NORTH:
64       return "north";
65     case D_SOUTH:
66       return "south";
67     default:
68       return "none";
69     }
70 }
71
72 Direction
73 string_to_direction(const std::string& directory)
74 {
75   if (directory == "west")
76     return D_WEST;
77   else if (directory == "east")
78     return D_EAST;
79   else if (directory == "north")
80     return D_NORTH;
81   else if (directory == "south")
82     return D_SOUTH;
83   else
84     return D_NONE;
85 }
86
87 TileManager::TileManager()
88 {
89   std::string stwt_filename = datadir +  "/images/worldmap/antarctica.stwt";
90   lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
91  
92   if (!root_obj)
93     st_abort("Couldn't load file", stwt_filename);
94
95   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
96     {
97       lisp_object_t* cur = lisp_cdr(root_obj);
98
99       while(!lisp_nil_p(cur))
100         {
101           lisp_object_t* element = lisp_car(cur);
102
103           if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
104             {
105               int id = 0;
106               std::string filename = "<invalid>";
107
108               Tile* tile = new Tile;             
109               tile->north = true;
110               tile->east  = true;
111               tile->south = true;
112               tile->west  = true;
113               tile->stop  = true;
114               tile->auto_walk = false;
115   
116               LispReader reader(lisp_cdr(element));
117               reader.read_int("id",  &id);
118               reader.read_bool("north", &tile->north);
119               reader.read_bool("south", &tile->south);
120               reader.read_bool("west",  &tile->west);
121               reader.read_bool("east",  &tile->east);
122               reader.read_bool("stop",  &tile->stop);
123               reader.read_bool("auto-walk",  &tile->auto_walk);
124               reader.read_string("image",  &filename);
125
126               tile->sprite = new Surface(
127                            datadir +  "/images/worldmap/" + filename, 
128                            USE_ALPHA);
129
130               if (id >= int(tiles.size()))
131                 tiles.resize(id+1);
132
133               tiles[id] = tile;
134             }
135           else
136             {
137               puts("Unhandled symbol");
138             }
139
140           cur = lisp_cdr(cur);
141         }
142     }
143   else
144     {
145       assert(0);
146     }
147
148   lisp_free(root_obj);
149 }
150
151 TileManager::~TileManager()
152 {
153   for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
154     delete *i;
155 }
156
157 Tile*
158 TileManager::get(int i)
159 {
160   assert(i >=0 && i < int(tiles.size()));
161   return tiles[i];
162 }
163
164 //---------------------------------------------------------------------------
165
166 Tux::Tux(WorldMap* worldmap_)
167   : worldmap(worldmap_)
168 {
169   largetux_sprite = new Surface(datadir +  "/images/worldmap/tux.png", USE_ALPHA);
170   firetux_sprite = new Surface(datadir +  "/images/worldmap/firetux.png", USE_ALPHA);
171   smalltux_sprite = new Surface(datadir +  "/images/worldmap/smalltux.png", USE_ALPHA);
172
173   offset = 0;
174   moving = false;
175   tile_pos.x = 4;
176   tile_pos.y = 5;
177   direction = D_NONE;
178   input_direction = D_NONE;
179 }
180
181 Tux::~Tux()
182 {
183   delete smalltux_sprite;
184   delete firetux_sprite;
185   delete largetux_sprite;
186 }
187
188 void
189 Tux::draw(const Point& offset)
190 {
191   Point pos = get_pos();
192   switch (player_status.bonus)
193     {
194     case PlayerStatus::GROWUP_BONUS:
195       largetux_sprite->draw(pos.x + offset.x, 
196                             pos.y + offset.y - 10);
197       break;
198     case PlayerStatus::FLOWER_BONUS:
199       firetux_sprite->draw(pos.x + offset.x, 
200                            pos.y + offset.y - 10);
201       break;
202     case PlayerStatus::NO_BONUS:
203       smalltux_sprite->draw(pos.x + offset.x, 
204                             pos.y + offset.y - 10);
205       break;
206     }
207 }
208
209
210 Point
211 Tux::get_pos()
212 {
213   float x = tile_pos.x * 32;
214   float y = tile_pos.y * 32;
215
216   switch(direction)
217     {
218     case D_WEST:
219       x -= offset - 32;
220       break;
221     case D_EAST:
222       x += offset - 32;
223       break;
224     case D_NORTH:
225       y -= offset - 32;
226       break;
227     case D_SOUTH:
228       y += offset - 32;
229       break;
230     case D_NONE:
231       break;
232     }
233   
234   return Point((int)x, (int)y); 
235 }
236
237 void
238 Tux::stop()
239 {
240   offset = 0;
241   direction = D_NONE;
242   moving = false;
243 }
244
245 void
246 Tux::update(float delta)
247 {
248   if (!moving)
249     {
250       if (input_direction != D_NONE)
251         { 
252           WorldMap::Level* level = worldmap->at_level();
253
254           // We got a new direction, so lets start walking when possible
255           Point next_tile;
256           if ((!level || level->solved)
257               && worldmap->path_ok(input_direction, tile_pos, &next_tile))
258             {
259               tile_pos = next_tile;
260               moving = true;
261               direction = input_direction;
262               back_direction = reverse_dir(direction);
263             }
264           else if (input_direction == back_direction)
265             {
266               std::cout << "Back triggered" << std::endl;
267               moving = true;
268               direction = input_direction;
269               tile_pos = worldmap->get_next_tile(tile_pos, direction);
270               back_direction = reverse_dir(direction);
271             }
272         }
273     }
274   else
275     {
276       // Let tux walk a few pixels (20 pixel/sec)
277       offset += 20.0f * delta;
278
279       if (offset > 32)
280         { // We reached the next tile, so we check what to do now
281           offset -= 32;
282
283           if (worldmap->at(tile_pos)->stop || worldmap->at_level())
284             {
285               stop();
286             }
287           else
288             {
289               if (worldmap->at(tile_pos)->auto_walk)
290                 { // Turn to a new direction
291                   Tile* tile = worldmap->at(tile_pos);
292                   Direction dir = D_NONE;
293                   
294                   if (tile->north && back_direction != D_NORTH)
295                     dir = D_NORTH;
296                   else if (tile->south && back_direction != D_SOUTH)
297                     dir = D_SOUTH;
298                   else if (tile->east && back_direction != D_EAST)
299                     dir = D_EAST;
300                   else if (tile->west && back_direction != D_WEST)
301                     dir = D_WEST;
302
303                   if (dir != D_NONE)
304                     {
305                       direction = dir;
306                       back_direction = reverse_dir(direction);
307                     }
308                   else
309                     {
310                       // Should never be reached if tiledata is good
311                       stop();
312                       return;
313                     }
314                 }
315
316               // Walk automatically to the next tile
317               Point next_tile;
318               if (worldmap->path_ok(direction, tile_pos, &next_tile))
319                 {
320                   tile_pos = next_tile;
321                 }
322               else
323                 {
324                   puts("Tilemap data is buggy");
325                   stop();
326                 }
327             }
328         }
329     }
330 }
331
332 //---------------------------------------------------------------------------
333 Tile::Tile()
334 {
335 }
336
337 Tile::~Tile()
338 {
339   delete sprite;
340 }
341
342 //---------------------------------------------------------------------------
343
344 WorldMap::WorldMap()
345 {
346   tile_manager = new TileManager();
347   tux = new Tux(this);
348
349   width  = 20;
350   height = 15;
351
352   level_sprite = new Surface(datadir +  "/images/worldmap/levelmarker.png", USE_ALPHA);
353   leveldot_green = new Surface(datadir +  "/images/worldmap/leveldot_green.png", USE_ALPHA);
354   leveldot_red = new Surface(datadir +  "/images/worldmap/leveldot_red.png", USE_ALPHA);
355
356   input_direction = D_NONE;
357   enter_level = false;
358
359   name = "<no file>";
360   music = "SALCON.MOD";
361
362   load_map();
363 }
364
365 WorldMap::~WorldMap()
366 {
367   delete tux;
368   delete tile_manager;
369
370   delete level_sprite;
371   delete leveldot_green;
372   delete leveldot_red;
373 }
374
375 void
376 WorldMap::load_map()
377 {
378   std::string filename = datadir +  "/levels/default/worldmap.stwm";
379   
380   lisp_object_t* root_obj = lisp_read_from_file(filename);
381   if (!root_obj)
382     st_abort("Couldn't load file", filename);
383   
384   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
385     {
386       lisp_object_t* cur = lisp_cdr(root_obj);
387
388       while(!lisp_nil_p(cur))
389         {
390           lisp_object_t* element = lisp_car(cur);
391
392           if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
393             {
394               LispReader reader(lisp_cdr(element));
395               reader.read_int("width",  &width);
396               reader.read_int("height", &height);
397               reader.read_int_vector("data", &tilemap);
398             }
399           else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
400             {
401               LispReader reader(lisp_cdr(element));
402               reader.read_string("name",  &name);
403               reader.read_string("music", &music);
404             }
405           else if (strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
406             {
407               lisp_object_t* cur = lisp_cdr(element);
408               
409               while(!lisp_nil_p(cur))
410                 {
411                   lisp_object_t* element = lisp_car(cur);
412                   
413                   if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
414                     {
415                       Level level;
416                       LispReader reader(lisp_cdr(element));
417                       level.solved = false;
418                       
419                       level.north = true;
420                       level.east  = true;
421                       level.south = true;
422                       level.west  = true;
423
424                       reader.read_string("extro-filename",  &level.extro_filename);
425                       reader.read_string("name",  &level.name);
426                       reader.read_int("x", &level.x);
427                       reader.read_int("y", &level.y);
428
429                       get_level_title(&level);   // get level's title
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(Levels::pointer 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 Point
575 WorldMap::get_next_tile(Point 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, Point old_pos, Point* 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               std::cout << "Enter the current level: " << level->name << std::endl;;
642               GameSession session(datadir +  "/levels/" + level->name,
643                                   1, ST_GL_LOAD_LEVEL_FILE);
644
645               switch (session.run())
646                 {
647                 case GameSession::ES_LEVEL_FINISHED:
648                   {
649                     bool old_level_state = level->solved;
650                     level->solved = true;
651
652                     if (session.get_world()->get_tux()->got_power !=
653                           session.get_world()->get_tux()->NONE_POWER)
654                       player_status.bonus = PlayerStatus::FLOWER_BONUS;
655                     else if (session.get_world()->get_tux()->size == BIG)
656                       player_status.bonus = PlayerStatus::GROWUP_BONUS;
657                     else
658                       player_status.bonus = PlayerStatus::NO_BONUS;
659
660                     if (old_level_state != level->solved)
661                       { // Try to detect the next direction to which we should walk
662                         // FIXME: Mostly a hack
663                         Direction dir = D_NONE;
664                     
665                         Tile* tile = at(tux->get_tile_pos());
666
667                         if (tile->north && tux->back_direction != D_NORTH)
668                           dir = D_NORTH;
669                         else if (tile->south && tux->back_direction != D_SOUTH)
670                           dir = D_SOUTH;
671                         else if (tile->east && tux->back_direction != D_EAST)
672                           dir = D_EAST;
673                         else if (tile->west && tux->back_direction != D_WEST)
674                           dir = D_WEST;
675
676                         if (dir != D_NONE)
677                           {
678                             tux->set_direction(dir);
679                             //tux->update(delta);
680                           }
681
682                         std::cout << "Walk to dir: " << dir << std::endl;
683                       }
684
685                     if (!level->extro_filename.empty())
686                       { 
687                         MusicRef theme =
688                           music_manager->load_music(datadir + "/music/theme.mod");
689                         music_manager->play_music(theme);
690                         // Display final credits and go back to the main menu
691                         display_text_file(level->extro_filename,
692                                           "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
693                         display_text_file("CREDITS",
694                                           "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
695                         quit = true;
696                       }
697                   }
698
699                   break;
700                 case GameSession::ES_LEVEL_ABORT:
701                   // Reseting the player_status might be a worthy
702                   // consideration, but I don't think we need it
703                   // 'cause only the bad players will use it to
704                   // 'cheat' a few items and that isn't necesarry a
705                   // bad thing (ie. better they continue that way,
706                   // then stop playing the game all together since it
707                   // is to hard)
708                   break;
709                 case GameSession::ES_GAME_OVER:
710                   /* draw an end screen */
711                   /* in the future, this should make a dialog a la SuperMario, asking
712                   if the player wants to restart the world map with no score and from
713                   level 1 */
714                   char str[80];
715
716                   drawgradient(Color (0, 255, 0), Color (255, 0, 255));
717
718                   blue_text->drawf("GAMEOVER", 0, 200, A_HMIDDLE, A_TOP, 1);
719
720                   sprintf(str, "SCORE: %d", player_status.score);
721                   gold_text->drawf(str, 0, 224, A_HMIDDLE, A_TOP, 1);
722
723                   sprintf(str, "COINS: %d", player_status.distros);
724                   gold_text->drawf(str, 0, screen->w - gold_text->w*2, A_HMIDDLE, A_TOP, 1);
725
726                   flipscreen();
727   
728                   SDL_Event event;
729                   wait_for_event(event,2000,5000,true);
730
731                   quit = true;
732                   player_status.reset();
733                   break;
734                 case GameSession::ES_NONE:
735                   // Should never be reached 
736                   break;
737                 }
738
739               music_manager->play_music(song);
740               Menu::set_current(0);
741               if (!savegame_file.empty())
742                 savegame(savegame_file);
743               return;
744             }
745         }
746       else
747         {
748           std::cout << "Nothing to enter at: "
749                     << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
750         }
751     }
752   else
753     {
754       tux->update(delta);
755       tux->set_direction(input_direction);
756     }
757   
758   Menu* menu = Menu::current();
759   if(menu)
760     {
761       menu->action();
762
763       if(menu == worldmap_menu)
764         {
765           switch (worldmap_menu->check())
766             {
767             case MNID_RETURNWORLDMAP: // Return to game
768               break;
769             case MNID_QUITWORLDMAP: // Quit Worldmap
770               quit = true;
771               break;
772             }
773         }
774       else if(menu == options_menu)
775         {
776           process_options_menu();
777         }
778     }
779 }
780
781 Tile*
782 WorldMap::at(Point p)
783 {
784   assert(p.x >= 0 
785          && p.x < width
786          && p.y >= 0
787          && p.y < height);
788
789   return tile_manager->get(tilemap[width * p.y + p.x]);
790 }
791
792 WorldMap::Level*
793 WorldMap::at_level()
794 {
795   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
796     {
797       if (i->x == tux->get_tile_pos().x && 
798           i->y == tux->get_tile_pos().y)
799         return &*i; 
800     }
801
802   return 0;
803 }
804
805
806 void
807 WorldMap::draw(const Point& offset)
808 {
809   for(int y = 0; y < height; ++y)
810     for(int x = 0; x < width; ++x)
811       {
812         Tile* tile = at(Point(x, y));
813         tile->sprite->draw(x*32 + offset.x,
814                            y*32 + offset.y);
815       }
816   
817   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
818     {
819       if (i->solved)
820         leveldot_green->draw(i->x*32 + offset.x, 
821                              i->y*32 + offset.y);
822       else
823         leveldot_red->draw(i->x*32 + offset.x, 
824                            i->y*32 + offset.y);        
825     }
826
827   tux->draw(offset);
828   draw_status();
829 }
830
831 void
832 WorldMap::draw_status()
833 {
834   char str[80];
835   sprintf(str, "%d", player_status.score);
836   white_text->draw("SCORE", 0, 0);
837   gold_text->draw(str, 96, 0);
838
839   sprintf(str, "%d", player_status.distros);
840   white_text->draw_align("COINS", screen->w/2 - white_text->w*5, 0,  A_LEFT, A_TOP);
841   gold_text->draw_align(str, screen->w/2 + (white_text->w*5)/2, 0, A_RIGHT, A_TOP);
842
843   white_text->draw("LIVES", screen->w - white_text->w*9, 0);
844   if (player_status.lives >= 5)
845     {
846       sprintf(str, "%dx", player_status.lives);
847       gold_text->draw_align(str, screen->w - gold_text->w, 0, A_RIGHT, A_TOP);
848       tux_life->draw(screen->w - gold_text->w, 0);
849     }
850   else
851     {
852       for(int i= 0; i < player_status.lives; ++i)
853         tux_life->draw(screen->w - tux_life->w*4 +(tux_life->w*i),0);
854     }
855
856   if (!tux->is_moving())
857     {
858       for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
859         {
860           if (i->x == tux->get_tile_pos().x && 
861               i->y == tux->get_tile_pos().y)
862             {
863               white_text->draw_align(i->title.c_str(), screen->w/2, screen->h,  A_HMIDDLE, A_BOTTOM);
864               break;
865             }
866         }
867     }
868 }
869
870 void
871 WorldMap::display()
872 {
873   Menu::set_current(0);
874
875   quit = false;
876
877   song = music_manager->load_music(datadir +  "/music/" + music);
878   music_manager->play_music(song);
879
880   unsigned int last_update_time;
881   unsigned int update_time;
882
883   last_update_time = update_time = st_get_ticks();
884
885   while(!quit)
886     {
887       float delta = ((float)(update_time-last_update_time))/100.0;
888
889       delta *= 1.3f;
890
891       if (delta > 10.0f)
892         delta = .3f;
893       
894       last_update_time = update_time;
895       update_time      = st_get_ticks();
896
897       Point tux_pos = tux->get_pos();
898       if (1)
899         {
900           offset.x = -tux_pos.x + screen->w/2;
901           offset.y = -tux_pos.y + screen->h/2;
902
903           if (offset.x > 0) offset.x = 0;
904           if (offset.y > 0) offset.y = 0;
905
906           if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
907           if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
908         } 
909
910       draw(offset);
911       get_input();
912       update(delta);
913
914       if(Menu::current())
915         {
916           Menu::current()->draw();
917           mouse_cursor->draw();
918         }
919       flipscreen();
920
921       SDL_Delay(20);
922     }
923 }
924
925 void
926 WorldMap::savegame(const std::string& filename)
927 {
928   std::cout << "savegame: " << filename << std::endl;
929   std::ofstream out(filename.c_str());
930
931   int nb_solved_levels = 0;
932   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
933     {
934       if (i->solved)
935         ++nb_solved_levels;
936     }
937
938   out << "(supertux-savegame\n"
939       << "  (version 1)\n"
940       << "  (title  \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
941       << "  (lives   " << player_status.lives << ")\n"
942       << "  (score   " << player_status.score << ")\n"
943       << "  (distros " << player_status.distros << ")\n"
944       << "  (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
945       << "       (back \"" << direction_to_string(tux->back_direction) << "\")\n"
946       << "       (bonus \"" << bonus_to_string(player_status.bonus) <<  "\"))\n"
947       << "  (levels\n";
948   
949   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
950     {
951       if (i->solved)
952         {
953           out << "     (level (name \"" << i->name << "\")\n"
954               << "            (solved #t))\n";
955         }
956     }  
957
958   out << "   )\n"
959       << " )\n\n;; EOF ;;" << std::endl;
960 }
961
962 void
963 WorldMap::loadgame(const std::string& filename)
964 {
965   std::cout << "loadgame: " << filename << std::endl;
966   savegame_file = filename;
967
968   if (access(filename.c_str(), F_OK) != 0)
969     return;
970   
971   lisp_object_t* savegame = lisp_read_from_file(filename);
972   if (!savegame)
973     {
974       std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
975       return;
976     }
977
978   lisp_object_t* cur = savegame;
979
980   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
981     return;
982
983   cur = lisp_cdr(cur);
984   LispReader reader(cur);
985
986   reader.read_int("lives",  &player_status.lives);
987   reader.read_int("score",  &player_status.score);
988   reader.read_int("distros", &player_status.distros);
989
990   if (player_status.lives < 0)
991     player_status.lives = START_LIVES;
992
993   lisp_object_t* tux_cur = 0;
994   if (reader.read_lisp("tux", &tux_cur))
995     {
996       Point p;
997       std::string back_str = "none";
998       std::string bonus_str = "none";
999
1000       LispReader tux_reader(tux_cur);
1001       tux_reader.read_int("x", &p.x);
1002       tux_reader.read_int("y", &p.y);
1003       tux_reader.read_string("back", &back_str);
1004       tux_reader.read_string("bonus", &bonus_str);
1005       
1006       player_status.bonus = string_to_bonus(bonus_str);
1007       tux->back_direction = string_to_direction(back_str);      
1008       tux->set_tile_pos(p);
1009     }
1010
1011   lisp_object_t* level_cur = 0;
1012   if (reader.read_lisp("levels", &level_cur))
1013     {
1014       while(level_cur)
1015         {
1016           lisp_object_t* sym  = lisp_car(lisp_car(level_cur));
1017           lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1018
1019           if (strcmp(lisp_symbol(sym), "level") == 0)
1020             {
1021               std::string name;
1022               bool solved = false;
1023
1024               LispReader level_reader(data);
1025               level_reader.read_string("name",   &name);
1026               level_reader.read_bool("solved", &solved);
1027
1028               for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1029                 {
1030                   if (name == i->name)
1031                     i->solved = solved;
1032                 }
1033             }
1034
1035           level_cur = lisp_cdr(level_cur);
1036         }
1037     }
1038  
1039   lisp_free(savegame);
1040 }
1041
1042 } // namespace WorldMapNS
1043
1044 /* Local Variables: */
1045 /* mode:c++ */
1046 /* End: */
1047