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