cleaned up font drawing code and added support for latin1 font of litespeed
[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
37 namespace WorldMapNS {
38
39 Direction reverse_dir(Direction direction)
40 {
41   switch(direction)
42     {
43     case D_WEST:
44       return D_EAST;
45     case D_EAST:
46       return D_WEST;
47     case D_NORTH:
48       return D_SOUTH;
49     case D_SOUTH:
50       return D_NORTH;
51     case D_NONE:
52       return D_NONE;
53     }
54   return D_NONE;
55 }
56
57 std::string
58 direction_to_string(Direction direction)
59 {
60   switch(direction)
61     {
62     case D_WEST:
63       return "west";
64     case D_EAST:
65       return "east";
66     case D_NORTH:
67       return "north";
68     case D_SOUTH:
69       return "south";
70     default:
71       return "none";
72     }
73 }
74
75 Direction
76 string_to_direction(const std::string& directory)
77 {
78   if (directory == "west")
79     return D_WEST;
80   else if (directory == "east")
81     return D_EAST;
82   else if (directory == "north")
83     return D_NORTH;
84   else if (directory == "south")
85     return D_SOUTH;
86   else
87     return D_NONE;
88 }
89
90 TileManager::TileManager()
91 {
92   std::string stwt_filename = datadir +  "/images/worldmap/antarctica.stwt";
93   lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
94  
95   if (!root_obj)
96     st_abort("Couldn't load file", stwt_filename);
97
98   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
99     {
100       lisp_object_t* cur = lisp_cdr(root_obj);
101
102       while(!lisp_nil_p(cur))
103         {
104           lisp_object_t* element = lisp_car(cur);
105
106           if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
107             {
108               int id = 0;
109               std::string filename = "<invalid>";
110
111               Tile* tile = new Tile;             
112               tile->north = true;
113               tile->east  = true;
114               tile->south = true;
115               tile->west  = true;
116               tile->stop  = true;
117               tile->auto_walk = false;
118   
119               LispReader reader(lisp_cdr(element));
120               reader.read_int("id", id);
121               reader.read_bool("north", tile->north);
122               reader.read_bool("south", tile->south);
123               reader.read_bool("west",  tile->west);
124               reader.read_bool("east",  tile->east);
125               reader.read_bool("stop",  tile->stop);
126               reader.read_bool("auto-walk",  tile->auto_walk);
127               reader.read_string("image", filename);
128
129               tile->sprite = new Surface(
130                            datadir +  "/images/worldmap/" + filename, 
131                            USE_ALPHA);
132
133               if (id >= int(tiles.size()))
134                 tiles.resize(id+1);
135
136               tiles[id] = tile;
137             }
138           else
139             {
140               puts("Unhandled symbol");
141             }
142
143           cur = lisp_cdr(cur);
144         }
145     }
146   else
147     {
148       assert(0);
149     }
150
151   lisp_free(root_obj);
152 }
153
154 TileManager::~TileManager()
155 {
156   for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
157     delete *i;
158 }
159
160 Tile*
161 TileManager::get(int i)
162 {
163   assert(i >=0 && i < int(tiles.size()));
164   return tiles[i];
165 }
166
167 //---------------------------------------------------------------------------
168
169 Tux::Tux(WorldMap* worldmap_)
170   : worldmap(worldmap_)
171 {
172   largetux_sprite = new Surface(datadir +  "/images/worldmap/tux.png", USE_ALPHA);
173   firetux_sprite = new Surface(datadir +  "/images/worldmap/firetux.png", USE_ALPHA);
174   smalltux_sprite = new Surface(datadir +  "/images/worldmap/smalltux.png", USE_ALPHA);
175
176   offset = 0;
177   moving = false;
178   tile_pos.x = 4;
179   tile_pos.y = 5;
180   direction = D_NONE;
181   input_direction = D_NONE;
182 }
183
184 Tux::~Tux()
185 {
186   delete smalltux_sprite;
187   delete firetux_sprite;
188   delete largetux_sprite;
189 }
190
191 void
192 Tux::draw(DrawingContext& context, const Vector& offset)
193 {
194   Vector pos = get_pos();
195   switch (player_status.bonus)
196     {
197     case PlayerStatus::GROWUP_BONUS:
198       context.draw_surface(largetux_sprite,
199           Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
200       break;
201     case PlayerStatus::FLOWER_BONUS:
202       context.draw_surface(firetux_sprite,
203           Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
204       break;
205     case PlayerStatus::NO_BONUS:
206       context.draw_surface(smalltux_sprite,
207           Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
208       break;
209     }
210 }
211
212
213 Vector
214 Tux::get_pos()
215 {
216   float x = tile_pos.x * 32;
217   float y = tile_pos.y * 32;
218
219   switch(direction)
220     {
221     case D_WEST:
222       x -= offset - 32;
223       break;
224     case D_EAST:
225       x += offset - 32;
226       break;
227     case D_NORTH:
228       y -= offset - 32;
229       break;
230     case D_SOUTH:
231       y += offset - 32;
232       break;
233     case D_NONE:
234       break;
235     }
236   
237   return Vector((int)x, (int)y); 
238 }
239
240 void
241 Tux::stop()
242 {
243   offset = 0;
244   direction = D_NONE;
245   moving = false;
246 }
247
248 void
249 Tux::action(float delta)
250 {
251   if (!moving)
252     {
253       if (input_direction != D_NONE)
254         { 
255           WorldMap::Level* level = worldmap->at_level();
256
257           // We got a new direction, so lets start walking when possible
258           Vector next_tile;
259           if ((!level || level->solved)
260               && worldmap->path_ok(input_direction, tile_pos, &next_tile))
261             {
262               tile_pos = next_tile;
263               moving = true;
264               direction = input_direction;
265               back_direction = reverse_dir(direction);
266             }
267           else if (input_direction == back_direction)
268             {
269               std::cout << "Back triggered" << std::endl;
270               moving = true;
271               direction = input_direction;
272               tile_pos = worldmap->get_next_tile(tile_pos, direction);
273               back_direction = reverse_dir(direction);
274             }
275         }
276     }
277   else
278     {
279       // Let tux walk a few pixels (20 pixel/sec)
280       offset += 20.0f * delta;
281
282       if (offset > 32)
283         { // We reached the next tile, so we check what to do now
284           offset -= 32;
285
286           if (worldmap->at(tile_pos)->stop || worldmap->at_level())
287             {
288               stop();
289             }
290           else
291             {
292               if (worldmap->at(tile_pos)->auto_walk)
293                 { // Turn to a new direction
294                   Tile* tile = worldmap->at(tile_pos);
295                   Direction dir = D_NONE;
296                   
297                   if (tile->north && back_direction != D_NORTH)
298                     dir = D_NORTH;
299                   else if (tile->south && back_direction != D_SOUTH)
300                     dir = D_SOUTH;
301                   else if (tile->east && back_direction != D_EAST)
302                     dir = D_EAST;
303                   else if (tile->west && back_direction != D_WEST)
304                     dir = D_WEST;
305
306                   if (dir != D_NONE)
307                     {
308                       direction = dir;
309                       back_direction = reverse_dir(direction);
310                     }
311                   else
312                     {
313                       // Should never be reached if tiledata is good
314                       stop();
315                       return;
316                     }
317                 }
318
319               // Walk automatically to the next tile
320               Vector next_tile;
321               if (worldmap->path_ok(direction, tile_pos, &next_tile))
322                 {
323                   tile_pos = next_tile;
324                 }
325               else
326                 {
327                   puts("Tilemap data is buggy");
328                   stop();
329                 }
330             }
331         }
332     }
333 }
334
335 //---------------------------------------------------------------------------
336 Tile::Tile()
337 {
338 }
339
340 Tile::~Tile()
341 {
342   delete sprite;
343 }
344
345 //---------------------------------------------------------------------------
346
347 WorldMap::WorldMap()
348 {
349   tile_manager = new TileManager();
350   tux = new Tux(this);
351
352   width  = 20;
353   height = 15;
354
355   level_sprite = new Surface(datadir +  "/images/worldmap/levelmarker.png", USE_ALPHA);
356   leveldot_green = new Surface(datadir +  "/images/worldmap/leveldot_green.png", USE_ALPHA);
357   leveldot_red = new Surface(datadir +  "/images/worldmap/leveldot_red.png", USE_ALPHA);
358
359   input_direction = D_NONE;
360   enter_level = false;
361
362   name = "<no file>";
363   music = "SALCON.MOD";
364
365   load_map();
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   std::string filename = datadir +  "/levels/default/worldmap.stwm";
382   
383   lisp_object_t* root_obj = lisp_read_from_file(filename);
384   if (!root_obj)
385     st_abort("Couldn't load file", filename);
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   context.draw_text(white_text, "LIVES",
859       Vector(screen->w - white_text->get_text_width("LIVES")*2, 0),
860       LAYER_FOREGROUND1);
861   if (player_status.lives >= 5)
862     {
863       sprintf(str, "%dx", player_status.lives);
864       context.draw_text(gold_text, str, 
865           Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
866           LAYER_FOREGROUND1);
867       context.draw_surface(tux_life, Vector(screen->w -
868             gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
869     }
870   else
871     {
872       for(int i= 0; i < player_status.lives; ++i)
873         context.draw_surface(tux_life,
874             Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
875             LAYER_FOREGROUND1);
876     }
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() - 50),
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  \"Icyisland - " << nb_solved_levels << "/" << levels.size() << "\")\n"
971       << "  (lives   " << player_status.lives << ")\n"
972       << "  (score   " << player_status.score << ")\n"
973       << "  (distros " << player_status.distros << ")\n"
974       << "  (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
975       << "       (back \"" << direction_to_string(tux->back_direction) << "\")\n"
976       << "       (bonus \"" << bonus_to_string(player_status.bonus) <<  "\"))\n"
977       << "  (levels\n";
978   
979   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
980     {
981       if (i->solved)
982         {
983           out << "     (level (name \"" << i->name << "\")\n"
984               << "            (solved #t))\n";
985         }
986     }  
987
988   out << "   )\n"
989       << " )\n\n;; EOF ;;" << std::endl;
990 }
991
992 void
993 WorldMap::loadgame(const std::string& filename)
994 {
995   std::cout << "loadgame: " << filename << std::endl;
996   savegame_file = filename;
997
998   if (access(filename.c_str(), F_OK) != 0)
999     return;
1000   
1001   lisp_object_t* savegame = lisp_read_from_file(filename);
1002   if (!savegame)
1003     {
1004       std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1005       return;
1006     }
1007
1008   lisp_object_t* cur = savegame;
1009
1010   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1011     return;
1012
1013   cur = lisp_cdr(cur);
1014   LispReader reader(cur);
1015
1016   reader.read_int("lives", player_status.lives);
1017   reader.read_int("score", player_status.score);
1018   reader.read_int("distros", player_status.distros);
1019
1020   if (player_status.lives < 0)
1021     player_status.lives = START_LIVES;
1022
1023   lisp_object_t* tux_cur = 0;
1024   if (reader.read_lisp("tux", tux_cur))
1025     {
1026       Vector p;
1027       std::string back_str = "none";
1028       std::string bonus_str = "none";
1029
1030       LispReader tux_reader(tux_cur);
1031       tux_reader.read_float("x", p.x);
1032       tux_reader.read_float("y", p.y);
1033       tux_reader.read_string("back", back_str);
1034       tux_reader.read_string("bonus", bonus_str);
1035       
1036       player_status.bonus = string_to_bonus(bonus_str);
1037       tux->back_direction = string_to_direction(back_str);      
1038       tux->set_tile_pos(p);
1039     }
1040
1041   lisp_object_t* level_cur = 0;
1042   if (reader.read_lisp("levels", level_cur))
1043     {
1044       while(level_cur)
1045         {
1046           lisp_object_t* sym  = lisp_car(lisp_car(level_cur));
1047           lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1048
1049           if (strcmp(lisp_symbol(sym), "level") == 0)
1050             {
1051               std::string name;
1052               bool solved = false;
1053
1054               LispReader level_reader(data);
1055               level_reader.read_string("name", name);
1056               level_reader.read_bool("solved", solved);
1057
1058               for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1059                 {
1060                   if (name == i->name)
1061                     i->solved = solved;
1062                 }
1063             }
1064
1065           level_cur = lisp_cdr(level_cur);
1066         }
1067     }
1068  
1069   lisp_free(savegame);
1070 }
1071
1072 } // namespace WorldMapNS
1073
1074 /* Local Variables: */
1075 /* mode:c++ */
1076 /* End: */
1077