Fixed crash when LANG system's variable is not defined.
[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 title>";
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   std::cout << "Loading map: " << datadir + "/levels/worldmap/" + map_filename << std::endl;
382
383   lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
384   if (!root_obj)
385     st_abort("Couldn't load file", datadir + "/levels/worldmap/" + map_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, true);
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, true);
429                       reader.read_int("x", level.x);
430                       reader.read_int("y", level.y);
431                       level.vertical_flip = false;
432                       reader.read_bool("flip", level.vertical_flip);
433
434                       levels.push_back(level);
435                     }
436                   
437                   cur = lisp_cdr(cur);      
438                 }
439             }
440           else
441             {
442               
443             }
444           
445           cur = lisp_cdr(cur);
446         }
447     }
448
449     lisp_free(root_obj);
450 }
451
452 void WorldMap::get_level_title(Level& level)
453 {
454   /** get level's title */
455   level.title = "<no title>";
456
457   FILE * fi;
458   lisp_object_t* root_obj = 0;
459   fi = fopen((datadir +  "/levels/" + level.name).c_str(), "r");
460   if (fi == NULL)
461   {
462     perror((datadir +  "/levels/" + level.name).c_str());
463     return;
464   }
465
466   lisp_stream_t stream;
467   lisp_stream_init_file (&stream, fi);
468   root_obj = lisp_read (&stream);
469
470   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
471   {
472     printf("World: Parse Error in file %s", level.name.c_str());
473   }
474
475   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-level") == 0)
476   {
477     LispReader reader(lisp_cdr(root_obj));
478     reader.read_string("name", level.title, true);
479   }
480
481   lisp_free(root_obj);
482
483   fclose(fi);
484 }
485
486 void
487 WorldMap::on_escape_press()
488 {
489   // Show or hide the menu
490   if(!Menu::current())
491     Menu::set_current(worldmap_menu); 
492   else
493     Menu::set_current(0); 
494 }
495
496 void
497 WorldMap::get_input()
498 {
499   enter_level = false;
500   input_direction = D_NONE;
501    
502   SDL_Event event;
503   while (SDL_PollEvent(&event))
504     {
505       if (Menu::current())
506         {
507           Menu::current()->event(event);
508         }
509       else
510         {
511           switch(event.type)
512             {
513             case SDL_QUIT:
514               st_abort("Received window close", "");
515               break;
516           
517             case SDL_KEYDOWN:
518               switch(event.key.keysym.sym)
519                 {
520                 case SDLK_ESCAPE:
521                   on_escape_press();
522                   break;
523                 case SDLK_LCTRL:
524                 case SDLK_RETURN:
525                   enter_level = true;
526                   break;
527                 default:
528                   break;
529                 }
530               break;
531           
532             case SDL_JOYAXISMOTION:
533               if (event.jaxis.axis == joystick_keymap.x_axis)
534                 {
535                   if (event.jaxis.value < -joystick_keymap.dead_zone)
536                     input_direction = D_WEST;
537                   else if (event.jaxis.value > joystick_keymap.dead_zone)
538                     input_direction = D_EAST;
539                 }
540               else if (event.jaxis.axis == joystick_keymap.y_axis)
541                 {
542                   if (event.jaxis.value > joystick_keymap.dead_zone)
543                     input_direction = D_SOUTH;
544                   else if (event.jaxis.value < -joystick_keymap.dead_zone)
545                     input_direction = D_NORTH;
546                 }
547               break;
548
549             case SDL_JOYBUTTONDOWN:
550               if (event.jbutton.button == joystick_keymap.b_button)
551                 enter_level = true;
552               else if (event.jbutton.button == joystick_keymap.start_button)
553                 on_escape_press();
554               break;
555
556             default:
557               break;
558             }
559         }
560     }
561
562   if (!Menu::current())
563     {
564       Uint8 *keystate = SDL_GetKeyState(NULL);
565   
566       if (keystate[SDLK_LEFT])
567         input_direction = D_WEST;
568       else if (keystate[SDLK_RIGHT])
569         input_direction = D_EAST;
570       else if (keystate[SDLK_UP])
571         input_direction = D_NORTH;
572       else if (keystate[SDLK_DOWN])
573         input_direction = D_SOUTH;
574     }
575 }
576
577 Vector
578 WorldMap::get_next_tile(Vector pos, Direction direction)
579 {
580   switch(direction)
581     {
582     case D_WEST:
583       pos.x -= 1;
584       break;
585     case D_EAST:
586       pos.x += 1;
587       break;
588     case D_NORTH:
589       pos.y -= 1;
590       break;
591     case D_SOUTH:
592       pos.y += 1;
593       break;
594     case D_NONE:
595       break;
596     }
597   return pos;
598 }
599
600 bool
601 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
602 {
603   *new_pos = get_next_tile(old_pos, direction);
604
605   if (!(new_pos->x >= 0 && new_pos->x < width
606         && new_pos->y >= 0 && new_pos->y < height))
607     { // New position is outsite the tilemap
608       return false;
609     }
610   else
611     { // Check if we the tile allows us to go to new_pos
612       switch(direction)
613         {
614         case D_WEST:
615           return (at(old_pos)->west && at(*new_pos)->east);
616
617         case D_EAST:
618           return (at(old_pos)->east && at(*new_pos)->west);
619
620         case D_NORTH:
621           return (at(old_pos)->north && at(*new_pos)->south);
622
623         case D_SOUTH:
624           return (at(old_pos)->south && at(*new_pos)->north);
625
626         case D_NONE:
627           assert(!"path_ok() can't work if direction is NONE");
628         }
629       return false;
630     }
631 }
632
633 void
634 WorldMap::update(float delta)
635 {
636   if (enter_level && !tux->is_moving())
637     {
638       Level* level = at_level();
639       if (level)
640         {
641           if (level->x == tux->get_tile_pos().x && 
642               level->y == tux->get_tile_pos().y)
643             {
644               PlayerStatus old_player_status = player_status;
645
646               std::cout << "Enter the current level: " << level->name << std::endl;
647               // do a shriking fade to the level
648               shrink_fade(Vector((level->x*32 + 16 + offset.x),(level->y*32 + 16
649                       + offset.y)), 500);
650               GameSession session(datadir +  "/levels/" + level->name,
651                                   ST_GL_LOAD_LEVEL_FILE, level->vertical_flip);
652
653               switch (session.run())
654                 {
655                 case GameSession::ES_LEVEL_FINISHED:
656                   {
657                     bool old_level_state = level->solved;
658                     level->solved = true;
659
660                     if (session.get_current_sector()->player->got_power !=
661                           session.get_current_sector()->player->NONE_POWER)
662                       player_status.bonus = PlayerStatus::FLOWER_BONUS;
663                     else if (session.get_current_sector()->player->size == BIG)
664                       player_status.bonus = PlayerStatus::GROWUP_BONUS;
665                     else
666                       player_status.bonus = PlayerStatus::NO_BONUS;
667
668                     if (old_level_state != level->solved)
669                       { // Try to detect the next direction to which we should walk
670                         // FIXME: Mostly a hack
671                         Direction dir = D_NONE;
672                     
673                         Tile* tile = at(tux->get_tile_pos());
674
675                         if (tile->north && tux->back_direction != D_NORTH)
676                           dir = D_NORTH;
677                         else if (tile->south && tux->back_direction != D_SOUTH)
678                           dir = D_SOUTH;
679                         else if (tile->east && tux->back_direction != D_EAST)
680                           dir = D_EAST;
681                         else if (tile->west && tux->back_direction != D_WEST)
682                           dir = D_WEST;
683
684                         if (dir != D_NONE)
685                           {
686                             tux->set_direction(dir);
687                             //tux->update(delta);
688                           }
689
690                         std::cout << "Walk to dir: " << dir << std::endl;
691                       }
692
693                     if (!level->extro_filename.empty())
694                       { 
695                         MusicRef theme =
696                           sound_manager->load_music(datadir + "/music/theme.mod");
697                         sound_manager->play_music(theme);
698                         // Display final credits and go back to the main menu
699                         display_text_file(level->extro_filename,
700                                           "/images/background/extro.jpg", SCROLL_SPEED_MESSAGE);
701                         display_text_file("CREDITS",
702                                           "/images/background/oiltux.jpg", SCROLL_SPEED_CREDITS);
703                         quit = true;
704                       }
705                   }
706
707                   break;
708                 case GameSession::ES_LEVEL_ABORT:
709                   /* In case the player's abort the level, keep it using the old
710                       status. But the minimum lives and no bonus. */
711                   player_status.score = old_player_status.score;
712                   player_status.distros = old_player_status.distros;
713                   player_status.lives = std::min(old_player_status.lives, player_status.lives);
714                   player_status.bonus = player_status.NO_BONUS;
715                   break;
716                 case GameSession::ES_GAME_OVER:
717                 {
718                   /* draw an end screen */
719                   /* in the future, this should make a dialog a la SuperMario, asking
720                   if the player wants to restart the world map with no score and from
721                   level 1 */
722                   char str[80];
723
724                   DrawingContext context;
725                   context.draw_gradient(Color (0, 255, 0), Color (255, 0, 255),
726                       LAYER_BACKGROUND0);
727
728                   context.draw_text_center(blue_text, _("GAMEOVER"), 
729                       Vector(0, 200), LAYER_FOREGROUND1);
730
731                   sprintf(str, _("SCORE: %d"), player_status.score);
732                   context.draw_text_center(gold_text, str,
733                       Vector(0, 224), LAYER_FOREGROUND1);
734
735                   sprintf(str, _("COINS: %d"), player_status.distros);
736                   context.draw_text_center(gold_text, str,
737                       Vector(0, screen->w - 32), LAYER_FOREGROUND1);
738
739                   context.do_drawing();
740   
741                   SDL_Event event;
742                   wait_for_event(event,2000,5000,true);
743
744                   quit = true;
745                   player_status.reset();
746                   break;
747                 }
748                 case GameSession::ES_NONE:
749                   assert(false);
750                   // Should never be reached 
751                   break;
752                 }
753
754               sound_manager->play_music(song);
755               Menu::set_current(0);
756               if (!savegame_file.empty())
757                 savegame(savegame_file);
758               return;
759             }
760         }
761       else
762         {
763           std::cout << "Nothing to enter at: "
764                     << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
765         }
766     }
767   else
768     {
769       tux->action(delta);
770       tux->set_direction(input_direction);
771     }
772   
773   Menu* menu = Menu::current();
774   if(menu)
775     {
776       menu->action();
777
778       if(menu == worldmap_menu)
779         {
780           switch (worldmap_menu->check())
781             {
782             case MNID_RETURNWORLDMAP: // Return to game
783               break;
784             case MNID_QUITWORLDMAP: // Quit Worldmap
785               quit = true;
786               break;
787             }
788         }
789       else if(menu == options_menu)
790         {
791           process_options_menu();
792         }
793     }
794 }
795
796 Tile*
797 WorldMap::at(Vector p)
798 {
799   assert(p.x >= 0 
800          && p.x < width
801          && p.y >= 0
802          && p.y < height);
803
804   int x = int(p.x);
805   int y = int(p.y);
806   return tile_manager->get(tilemap[width * y + x]);
807 }
808
809 WorldMap::Level*
810 WorldMap::at_level()
811 {
812   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
813     {
814       if (i->x == tux->get_tile_pos().x && 
815           i->y == tux->get_tile_pos().y)
816         return &*i; 
817     }
818
819   return 0;
820 }
821
822
823 void
824 WorldMap::draw(DrawingContext& context, const Vector& offset)
825 {
826   for(int y = 0; y < height; ++y)
827     for(int x = 0; x < width; ++x)
828       {
829         Tile* tile = at(Vector(x, y));
830         context.draw_surface(tile->sprite,
831             Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
832       }
833   
834   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
835     {
836       if (i->solved)
837         context.draw_surface(leveldot_green,
838             Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
839       else
840         context.draw_surface(leveldot_red,
841             Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
842     }
843
844   tux->draw(context, offset);
845   draw_status(context);
846 }
847
848 void
849 WorldMap::draw_status(DrawingContext& context)
850 {
851   char str[80];
852   sprintf(str, " %d", player_status.score);
853
854   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
855   context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
856
857   sprintf(str, "%d", player_status.distros);
858   context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
859       LAYER_FOREGROUND1);
860   context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
861         LAYER_FOREGROUND1);
862
863   if (player_status.lives >= 5)
864     {
865       sprintf(str, "%dx", player_status.lives);
866       context.draw_text(gold_text, str, 
867           Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
868           LAYER_FOREGROUND1);
869       context.draw_surface(tux_life, Vector(screen->w -
870             gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
871     }
872   else
873     {
874       for(int i= 0; i < player_status.lives; ++i)
875         context.draw_surface(tux_life,
876             Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
877             LAYER_FOREGROUND1);
878     }
879   context.draw_text(white_text, _("LIVES"),
880       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 0),
881       LAYER_FOREGROUND1);
882
883   if (!tux->is_moving())
884     {
885       for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
886         {
887           if (i->x == tux->get_tile_pos().x && 
888               i->y == tux->get_tile_pos().y)
889             {
890               if(i->title == "")
891                 get_level_title(*i);
892
893               context.draw_text(white_text, i->title, 
894                   Vector(screen->w/2 - white_text->get_text_width(i->title)/2,
895                          screen->h - white_text->get_height() - 30),
896                   LAYER_FOREGROUND1);
897               break;
898             }
899         }
900     }
901 }
902
903 void
904 WorldMap::display()
905 {
906   Menu::set_current(0);
907
908   quit = false;
909
910   song = sound_manager->load_music(datadir +  "/music/" + music);
911   sound_manager->play_music(song);
912   
913   unsigned int last_update_time;
914   unsigned int update_time;
915
916   last_update_time = update_time = st_get_ticks();
917
918   DrawingContext context;
919   while(!quit)
920     {
921       float delta = ((float)(update_time-last_update_time))/100.0;
922
923       delta *= 1.3f;
924
925       if (delta > 10.0f)
926         delta = .3f;
927       
928       last_update_time = update_time;
929       update_time      = st_get_ticks();
930
931       Vector tux_pos = tux->get_pos();
932       if (1)
933         {
934           offset.x = -tux_pos.x + screen->w/2;
935           offset.y = -tux_pos.y + screen->h/2;
936
937           if (offset.x > 0) offset.x = 0;
938           if (offset.y > 0) offset.y = 0;
939
940           if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
941           if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
942         } 
943
944       draw(context, offset);
945       get_input();
946       update(delta);
947
948       if(Menu::current())
949         {
950           Menu::current()->draw(context);
951           mouse_cursor->draw(context);
952         }
953
954       context.do_drawing();
955
956       SDL_Delay(20);
957     }
958 }
959
960 void
961 WorldMap::savegame(const std::string& filename)
962 {
963   if(filename != "")
964     return;
965
966   std::cout << "savegame: " << filename << std::endl;
967   std::ofstream out(filename.c_str());
968
969   int nb_solved_levels = 0;
970   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
971     {
972       if (i->solved)
973         ++nb_solved_levels;
974     }
975
976   out << "(supertux-savegame\n"
977       << "  (version 1)\n"
978       << "  (title  \"" << name << " - " << nb_solved_levels << "/" << levels.size() << "\")\n"
979       << "  (map    \"" << map_filename << "\")\n"
980       << "  (lives   " << player_status.lives << ")\n"
981       << "  (score   " << player_status.score << ")\n"
982       << "  (distros " << player_status.distros << ")\n"
983       << "  (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
984       << "       (back \"" << direction_to_string(tux->back_direction) << "\")\n"
985       << "       (bonus \"" << bonus_to_string(player_status.bonus) <<  "\"))\n"
986       << "  (levels\n";
987   
988   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
989     {
990       if (i->solved)
991         {
992           out << "     (level (name \"" << i->name << "\")\n"
993               << "            (solved #t))\n";
994         }
995     }  
996
997   out << "   )\n"
998       << " )\n\n;; EOF ;;" << std::endl;
999 }
1000
1001 void
1002 WorldMap::loadgame(const std::string& filename)
1003 {
1004   std::cout << "loadgame: " << filename << std::endl;
1005   savegame_file = filename;
1006   map_filename = "icyisland.stwm";
1007
1008   if (access(filename.c_str(), F_OK) != 0)
1009     {
1010     load_map();
1011     return;
1012     }
1013   
1014   lisp_object_t* savegame = lisp_read_from_file(filename);
1015   if (!savegame)
1016     {
1017       std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1018       load_map();
1019       return;
1020     }
1021
1022   lisp_object_t* cur = savegame;
1023
1024   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1025     {
1026     load_map();
1027     return;
1028     }
1029
1030   cur = lisp_cdr(cur);
1031   LispReader reader(cur);
1032
1033   /* Get the Map filename and then load it before setting level settings */
1034   reader.read_string("map", map_filename);
1035   load_map(); 
1036
1037   reader.read_int("lives", player_status.lives);
1038   reader.read_int("score", player_status.score);
1039   reader.read_int("distros", player_status.distros);
1040
1041   if (player_status.lives < 0)
1042     player_status.lives = START_LIVES;
1043
1044   lisp_object_t* tux_cur = 0;
1045   if (reader.read_lisp("tux", tux_cur))
1046     {
1047       Vector p;
1048       std::string back_str = "none";
1049       std::string bonus_str = "none";
1050
1051       LispReader tux_reader(tux_cur);
1052       tux_reader.read_float("x", p.x);
1053       tux_reader.read_float("y", p.y);
1054       tux_reader.read_string("back", back_str);
1055       tux_reader.read_string("bonus", bonus_str);
1056       
1057       player_status.bonus = string_to_bonus(bonus_str);
1058       tux->back_direction = string_to_direction(back_str);      
1059       tux->set_tile_pos(p);
1060     }
1061
1062   lisp_object_t* level_cur = 0;
1063   if (reader.read_lisp("levels", level_cur))
1064     {
1065       while(level_cur)
1066         {
1067           lisp_object_t* sym  = lisp_car(lisp_car(level_cur));
1068           lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1069
1070           if (strcmp(lisp_symbol(sym), "level") == 0)
1071             {
1072               std::string name;
1073               bool solved = false;
1074
1075               LispReader level_reader(data);
1076               level_reader.read_string("name", name, true);
1077               level_reader.read_bool("solved", solved);
1078
1079               for(Levels::iterator i = levels.begin(); i != levels.end(); ++i)
1080                 {
1081                   if (name == i->name)
1082                     i->solved = solved;
1083                 }
1084             }
1085
1086           level_cur = lisp_cdr(level_cur);
1087         }
1088     }
1089  
1090   lisp_free(savegame);
1091 }
1092
1093 void
1094 WorldMap::loadmap(const std::string& filename)
1095 {
1096   savegame_file = "";
1097   map_filename = filename;
1098   load_map();
1099 }
1100
1101 } // namespace WorldMapNS
1102
1103 /* Local Variables: */
1104 /* mode:c++ */
1105 /* End: */
1106