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