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