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