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