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