eliminated global scroll_x and scroll_y variables
[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               PlayerStatus old_player_status = player_status;
642
643               std::cout << "Enter the current level: " << level->name << std::endl;;
644               GameSession session(datadir +  "/levels/" + level->name,
645                                   1, ST_GL_LOAD_LEVEL_FILE);
646
647               switch (session.run())
648                 {
649                 case GameSession::ES_LEVEL_FINISHED:
650                   {
651                     bool old_level_state = level->solved;
652                     level->solved = true;
653
654                     if (session.get_world()->get_tux()->got_power !=
655                           session.get_world()->get_tux()->NONE_POWER)
656                       player_status.bonus = PlayerStatus::FLOWER_BONUS;
657                     else if (session.get_world()->get_tux()->size == BIG)
658                       player_status.bonus = PlayerStatus::GROWUP_BONUS;
659                     else
660                       player_status.bonus = PlayerStatus::NO_BONUS;
661
662                     if (old_level_state != level->solved)
663                       { // Try to detect the next direction to which we should walk
664                         // FIXME: Mostly a hack
665                         Direction dir = D_NONE;
666                     
667                         Tile* tile = at(tux->get_tile_pos());
668
669                         if (tile->north && tux->back_direction != D_NORTH)
670                           dir = D_NORTH;
671                         else if (tile->south && tux->back_direction != D_SOUTH)
672                           dir = D_SOUTH;
673                         else if (tile->east && tux->back_direction != D_EAST)
674                           dir = D_EAST;
675                         else if (tile->west && tux->back_direction != D_WEST)
676                           dir = D_WEST;
677
678                         if (dir != D_NONE)
679                           {
680                             tux->set_direction(dir);
681                             //tux->update(delta);
682                           }
683
684                         std::cout << "Walk to dir: " << dir << std::endl;
685                       }
686
687                     if (!level->extro_filename.empty())
688                       { 
689                         MusicRef theme =
690                           music_manager->load_music(datadir + "/music/theme.mod");
691                         music_manager->play_music(theme);
692                         // Display final credits and go back to the main menu
693                         display_text_file(level->extro_filename,
694                                           "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
695                         display_text_file("CREDITS",
696                                           "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
697                         quit = true;
698                       }
699                   }
700
701                   break;
702                 case GameSession::ES_LEVEL_ABORT:
703                   /* In case the player's abort the level, keep it using the old
704                       status */
705                   player_status = old_player_status;
706                   break;
707                 case GameSession::ES_GAME_OVER:
708                   /* draw an end screen */
709                   /* in the future, this should make a dialog a la SuperMario, asking
710                   if the player wants to restart the world map with no score and from
711                   level 1 */
712                   char str[80];
713
714                   drawgradient(Color (0, 255, 0), Color (255, 0, 255));
715
716                   blue_text->drawf("GAMEOVER", 0, 200, A_HMIDDLE, A_TOP, 1);
717
718                   sprintf(str, "SCORE: %d", player_status.score);
719                   gold_text->drawf(str, 0, 224, A_HMIDDLE, A_TOP, 1);
720
721                   sprintf(str, "COINS: %d", player_status.distros);
722                   gold_text->drawf(str, 0, screen->w - gold_text->w*2, A_HMIDDLE, A_TOP, 1);
723
724                   flipscreen();
725   
726                   SDL_Event event;
727                   wait_for_event(event,2000,5000,true);
728
729                   quit = true;
730                   player_status.reset();
731                   break;
732                 case GameSession::ES_NONE:
733                   // Should never be reached 
734                   break;
735                 }
736
737               music_manager->play_music(song);
738               Menu::set_current(0);
739               if (!savegame_file.empty())
740                 savegame(savegame_file);
741               return;
742             }
743         }
744       else
745         {
746           std::cout << "Nothing to enter at: "
747                     << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
748         }
749     }
750   else
751     {
752       tux->update(delta);
753       tux->set_direction(input_direction);
754     }
755   
756   Menu* menu = Menu::current();
757   if(menu)
758     {
759       menu->action();
760
761       if(menu == worldmap_menu)
762         {
763           switch (worldmap_menu->check())
764             {
765             case MNID_RETURNWORLDMAP: // Return to game
766               break;
767             case MNID_QUITWORLDMAP: // Quit Worldmap
768               quit = true;
769               break;
770             }
771         }
772       else if(menu == options_menu)
773         {
774           process_options_menu();
775         }
776     }
777 }
778
779 Tile*
780 WorldMap::at(Point p)
781 {
782   assert(p.x >= 0 
783          && p.x < width
784          && p.y >= 0
785          && p.y < height);
786
787   return tile_manager->get(tilemap[width * p.y + p.x]);
788 }
789
790 WorldMap::Level*
791 WorldMap::at_level()
792 {
793   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
794     {
795       if (i->x == tux->get_tile_pos().x && 
796           i->y == tux->get_tile_pos().y)
797         return &*i; 
798     }
799
800   return 0;
801 }
802
803
804 void
805 WorldMap::draw(const Point& offset)
806 {
807   for(int y = 0; y < height; ++y)
808     for(int x = 0; x < width; ++x)
809       {
810         Tile* tile = at(Point(x, y));
811         tile->sprite->draw(x*32 + offset.x,
812                            y*32 + offset.y);
813       }
814   
815   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
816     {
817       if (i->solved)
818         leveldot_green->draw(i->x*32 + offset.x, 
819                              i->y*32 + offset.y);
820       else
821         leveldot_red->draw(i->x*32 + offset.x, 
822                            i->y*32 + offset.y);        
823     }
824
825   tux->draw(offset);
826   draw_status();
827 }
828
829 void
830 WorldMap::draw_status()
831 {
832   char str[80];
833   sprintf(str, "%d", player_status.score);
834   white_text->draw("SCORE", 0, 0);
835   gold_text->draw(str, 96, 0);
836
837   sprintf(str, "%d", player_status.distros);
838   white_text->draw_align("COINS", screen->w/2 - white_text->w*5, 0,  A_LEFT, A_TOP);
839   gold_text->draw_align(str, screen->w/2 + (white_text->w*5)/2, 0, A_RIGHT, A_TOP);
840
841   white_text->draw("LIVES", screen->w - white_text->w*9, 0);
842   if (player_status.lives >= 5)
843     {
844       sprintf(str, "%dx", player_status.lives);
845       gold_text->draw_align(str, screen->w - gold_text->w, 0, A_RIGHT, A_TOP);
846       tux_life->draw(screen->w - gold_text->w, 0);
847     }
848   else
849     {
850       for(int i= 0; i < player_status.lives; ++i)
851         tux_life->draw(screen->w - tux_life->w*4 +(tux_life->w*i),0);
852     }
853
854   if (!tux->is_moving())
855     {
856       for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
857         {
858           if (i->x == tux->get_tile_pos().x && 
859               i->y == tux->get_tile_pos().y)
860             {
861               white_text->draw_align(i->title.c_str(), screen->w/2, screen->h,  A_HMIDDLE, A_BOTTOM);
862               break;
863             }
864         }
865     }
866 }
867
868 void
869 WorldMap::display()
870 {
871   Menu::set_current(0);
872
873   quit = false;
874
875   song = music_manager->load_music(datadir +  "/music/" + music);
876   music_manager->play_music(song);
877
878   unsigned int last_update_time;
879   unsigned int update_time;
880
881   last_update_time = update_time = st_get_ticks();
882
883   while(!quit)
884     {
885       float delta = ((float)(update_time-last_update_time))/100.0;
886
887       delta *= 1.3f;
888
889       if (delta > 10.0f)
890         delta = .3f;
891       
892       last_update_time = update_time;
893       update_time      = st_get_ticks();
894
895       Point tux_pos = tux->get_pos();
896       if (1)
897         {
898           offset.x = -tux_pos.x + screen->w/2;
899           offset.y = -tux_pos.y + screen->h/2;
900
901           if (offset.x > 0) offset.x = 0;
902           if (offset.y > 0) offset.y = 0;
903
904           if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
905           if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
906         } 
907
908       draw(offset);
909       get_input();
910       update(delta);
911
912       if(Menu::current())
913         {
914           Menu::current()->draw();
915           mouse_cursor->draw();
916         }
917       flipscreen();
918
919       SDL_Delay(20);
920     }
921 }
922
923 void
924 WorldMap::savegame(const std::string& filename)
925 {
926   std::cout << "savegame: " << filename << std::endl;
927   std::ofstream out(filename.c_str());
928
929   int nb_solved_levels = 0;
930   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
931     {
932       if (i->solved)
933         ++nb_solved_levels;
934     }
935
936   out << "(supertux-savegame\n"
937       << "  (version 1)\n"
938       << "  (title  \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
939       << "  (lives   " << player_status.lives << ")\n"
940       << "  (score   " << player_status.score << ")\n"
941       << "  (distros " << player_status.distros << ")\n"
942       << "  (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
943       << "       (back \"" << direction_to_string(tux->back_direction) << "\")\n"
944       << "       (bonus \"" << bonus_to_string(player_status.bonus) <<  "\"))\n"
945       << "  (levels\n";
946   
947   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
948     {
949       if (i->solved)
950         {
951           out << "     (level (name \"" << i->name << "\")\n"
952               << "            (solved #t))\n";
953         }
954     }  
955
956   out << "   )\n"
957       << " )\n\n;; EOF ;;" << std::endl;
958 }
959
960 void
961 WorldMap::loadgame(const std::string& filename)
962 {
963   std::cout << "loadgame: " << filename << std::endl;
964   savegame_file = filename;
965
966   if (access(filename.c_str(), F_OK) != 0)
967     return;
968   
969   lisp_object_t* savegame = lisp_read_from_file(filename);
970   if (!savegame)
971     {
972       std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
973       return;
974     }
975
976   lisp_object_t* cur = savegame;
977
978   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
979     return;
980
981   cur = lisp_cdr(cur);
982   LispReader reader(cur);
983
984   reader.read_int("lives",  &player_status.lives);
985   reader.read_int("score",  &player_status.score);
986   reader.read_int("distros", &player_status.distros);
987
988   if (player_status.lives < 0)
989     player_status.lives = START_LIVES;
990
991   lisp_object_t* tux_cur = 0;
992   if (reader.read_lisp("tux", &tux_cur))
993     {
994       Point p;
995       std::string back_str = "none";
996       std::string bonus_str = "none";
997
998       LispReader tux_reader(tux_cur);
999       tux_reader.read_int("x", &p.x);
1000       tux_reader.read_int("y", &p.y);
1001       tux_reader.read_string("back", &back_str);
1002       tux_reader.read_string("bonus", &bonus_str);
1003       
1004       player_status.bonus = string_to_bonus(bonus_str);
1005       tux->back_direction = string_to_direction(back_str);      
1006       tux->set_tile_pos(p);
1007     }
1008
1009   lisp_object_t* level_cur = 0;
1010   if (reader.read_lisp("levels", &level_cur))
1011     {
1012       while(level_cur)
1013         {
1014           lisp_object_t* sym  = lisp_car(lisp_car(level_cur));
1015           lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1016
1017           if (strcmp(lisp_symbol(sym), "level") == 0)
1018             {
1019               std::string name;
1020               bool solved = false;
1021
1022               LispReader level_reader(data);
1023               level_reader.read_string("name",   &name);
1024               level_reader.read_bool("solved", &solved);
1025
1026               for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1027                 {
1028                   if (name == i->name)
1029                     i->solved = solved;
1030                 }
1031             }
1032
1033           level_cur = lisp_cdr(level_cur);
1034         }
1035     }
1036  
1037   lisp_free(savegame);
1038 }
1039
1040 } // namespace WorldMapNS
1041
1042 /* Local Variables: */
1043 /* mode:c++ */
1044 /* End: */
1045