applied netsrot's patch
[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 "app/globals.h"
27 #include "video/surface.h"
28 #include "video/screen.h"
29 #include "video/drawing_context.h"
30 #include "utils/lispreader.h"
31 #include "special/frame_rate.h"
32 #include "gameloop.h"
33 #include "app/setup.h"
34 #include "sector.h"
35 #include "worldmap.h"
36 #include "audio/sound_manager.h"
37 #include "resources.h"
38 #include "app/gettext.h"
39 #include "misc.h"
40
41 #define map_message_TIME 2800
42
43 Menu* worldmap_menu  = 0;
44
45 namespace WorldMapNS {
46
47 Direction reverse_dir(Direction direction)
48 {
49   switch(direction)
50     {
51     case D_WEST:
52       return D_EAST;
53     case D_EAST:
54       return D_WEST;
55     case D_NORTH:
56       return D_SOUTH;
57     case D_SOUTH:
58       return D_NORTH;
59     case D_NONE:
60       return D_NONE;
61     }
62   return D_NONE;
63 }
64
65 std::string
66 direction_to_string(Direction direction)
67 {
68   switch(direction)
69     {
70     case D_WEST:
71       return "west";
72     case D_EAST:
73       return "east";
74     case D_NORTH:
75       return "north";
76     case D_SOUTH:
77       return "south";
78     default:
79       return "none";
80     }
81 }
82
83 Direction
84 string_to_direction(const std::string& directory)
85 {
86   if (directory == "west")
87     return D_WEST;
88   else if (directory == "east")
89     return D_EAST;
90   else if (directory == "north")
91     return D_NORTH;
92   else if (directory == "south")
93     return D_SOUTH;
94   else
95     return D_NONE;
96 }
97
98 TileManager::TileManager()
99 {
100   std::string stwt_filename = datadir +  "/images/worldmap/antarctica.stwt";
101   lisp_object_t* root_obj = lisp_read_from_file(stwt_filename);
102  
103   if (!root_obj)
104     Termination::abort("Couldn't load file", stwt_filename);
105
106   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap-tiles") == 0)
107     {
108       lisp_object_t* cur = lisp_cdr(root_obj);
109
110       while(!lisp_nil_p(cur))
111         {
112           lisp_object_t* element = lisp_car(cur);
113
114           if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
115             {
116               int id = 0;
117               std::string filename = "<invalid>";
118
119               Tile* tile = new Tile;             
120               tile->north = tile->east = tile->south = tile->west = true;
121               tile->stop  = true;
122               tile->auto_walk = false;
123   
124               LispReader reader(lisp_cdr(element));
125               reader.read_int("id", id);
126
127               std::string temp;
128               reader.read_string("possible-directions", temp);
129               if(!temp.empty())
130                 {
131                 tile->north = tile->east = tile->south = tile->west = false;
132                 if(temp.find("north") != std::string::npos)
133                   tile->north = true;
134                 if(temp.find("south") != std::string::npos)
135                   tile->south = true;
136                 if(temp.find("east") != std::string::npos)
137                   tile->east = true;
138                 if(temp.find("west") != std::string::npos)
139                   tile->west = true;
140                 }
141
142               /* For backward compatibility */
143               reader.read_bool("north", tile->north);
144               reader.read_bool("south", tile->south);
145               reader.read_bool("west",  tile->west);
146               reader.read_bool("east",  tile->east);
147
148               reader.read_bool("stop",  tile->stop);
149               reader.read_bool("auto-walk",  tile->auto_walk);
150               reader.read_string("image", filename);
151
152               reader.read_string("one-way", temp);
153               tile->one_way = BOTH_WAYS;
154               if(!temp.empty())
155                 {
156                 if(temp == "north-south")
157                   tile->one_way = NORTH_SOUTH_WAY;
158                 else if(temp == "south-north")
159                   tile->one_way = SOUTH_NORTH_WAY;
160                 else if(temp == "east-west")
161                   tile->one_way = EAST_WEST_WAY;
162                 else if(temp == "west-east")
163                   tile->one_way = WEST_EAST_WAY;
164                 }
165
166               tile->sprite = new Surface(
167                            datadir +  "/images/worldmap/" + filename, 
168                            true);
169
170               if (id >= int(tiles.size()))
171                 tiles.resize(id+1);
172
173               tiles[id] = tile;
174             }
175           else
176             {
177               puts("Unhandled symbol");
178             }
179
180           cur = lisp_cdr(cur);
181         }
182     }
183   else
184     {
185       assert(0);
186     }
187
188   lisp_free(root_obj);
189 }
190
191 TileManager::~TileManager()
192 {
193   for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i)
194     delete *i;
195 }
196
197 Tile*
198 TileManager::get(int i)
199 {
200   assert(i >=0 && i < int(tiles.size()));
201   return tiles[i];
202 }
203
204 //---------------------------------------------------------------------------
205
206 Tux::Tux(WorldMap* worldmap_)
207   : worldmap(worldmap_)
208 {
209   largetux_sprite = new Surface(datadir +  "/images/worldmap/tux.png", true);
210   firetux_sprite = new Surface(datadir +  "/images/worldmap/firetux.png", true);
211   smalltux_sprite = new Surface(datadir +  "/images/worldmap/smalltux.png", true);
212
213   offset = 0;
214   moving = false;
215   tile_pos.x = worldmap->get_start_x();
216   tile_pos.y = worldmap->get_start_y();
217   direction = D_NONE;
218   input_direction = D_NONE;
219 }
220
221 Tux::~Tux()
222 {
223   delete smalltux_sprite;
224   delete firetux_sprite;
225   delete largetux_sprite;
226 }
227
228 void
229 Tux::draw(DrawingContext& context, const Vector& offset)
230 {
231   Vector pos = get_pos();
232   switch (player_status.bonus)
233     {
234     case PlayerStatus::GROWUP_BONUS:
235       context.draw_surface(largetux_sprite,
236           Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
237       break;
238     case PlayerStatus::FLOWER_BONUS:
239       context.draw_surface(firetux_sprite,
240           Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
241       break;
242     case PlayerStatus::NO_BONUS:
243       context.draw_surface(smalltux_sprite,
244           Vector(pos.x + offset.x, pos.y + offset.y - 10), LAYER_OBJECTS);
245       break;
246     }
247 }
248
249
250 Vector
251 Tux::get_pos()
252 {
253   float x = tile_pos.x * 32;
254   float y = tile_pos.y * 32;
255
256   switch(direction)
257     {
258     case D_WEST:
259       x -= offset - 32;
260       break;
261     case D_EAST:
262       x += offset - 32;
263       break;
264     case D_NORTH:
265       y -= offset - 32;
266       break;
267     case D_SOUTH:
268       y += offset - 32;
269       break;
270     case D_NONE:
271       break;
272     }
273   
274   return Vector((int)x, (int)y); 
275 }
276
277 void
278 Tux::stop()
279 {
280   offset = 0;
281   direction = D_NONE;
282   input_direction = D_NONE;
283   moving = false;
284 }
285
286 void
287 Tux::set_direction(Direction dir)
288 {
289 input_direction = dir;
290 }
291
292 void
293 Tux::action(float delta)
294 {
295   if (!moving)
296     {
297       if (input_direction != D_NONE)
298         { 
299           WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
300
301           // We got a new direction, so lets start walking when possible
302           Vector next_tile;
303           if ((!special_tile || special_tile->solved || special_tile->level_name.empty())
304               && worldmap->path_ok(input_direction, tile_pos, &next_tile))
305             {
306               tile_pos = next_tile;
307               moving = true;
308               direction = input_direction;
309               back_direction = reverse_dir(direction);
310             }
311           else if (input_direction == back_direction)
312             {
313               moving = true;
314               direction = input_direction;
315               tile_pos = worldmap->get_next_tile(tile_pos, direction);
316               back_direction = reverse_dir(direction);
317             }
318         }
319     }
320   else
321     {
322       // Let tux walk a few pixels (20 pixel/sec)
323       offset += 20.0f * delta;
324
325       if (offset > 32)
326         { // We reached the next tile, so we check what to do now
327           offset -= 32;
328
329           WorldMap::SpecialTile* special_tile = worldmap->at_special_tile();
330           if(special_tile && special_tile->passive_message)
331             {  // direction and the apply_action_ are opposites, since they "see"
332                // directions in a different way
333             if((direction == D_NORTH && special_tile->apply_action_south) ||
334                (direction == D_SOUTH && special_tile->apply_action_north) ||
335                (direction == D_WEST && special_tile->apply_action_east) ||
336                (direction == D_EAST && special_tile->apply_action_west))
337               {
338               worldmap->passive_message = special_tile->map_message;
339               worldmap->passive_message_timer.start(map_message_TIME);
340               }
341             }
342
343           if (worldmap->at(tile_pos)->stop || (special_tile && 
344               !special_tile->passive_message))
345             {
346               if(special_tile && !special_tile->map_message.empty() &&
347                 !special_tile->passive_message)
348                 worldmap->passive_message_timer.stop();
349               stop();
350             }
351           else
352             {
353               if (worldmap->at(tile_pos)->auto_walk || direction != input_direction)
354                 { // Turn to a new direction
355                   Tile* tile = worldmap->at(tile_pos);
356
357                   if(direction != input_direction && 
358                      ((tile->north && input_direction == D_NORTH) ||
359                      (tile->south && input_direction == D_SOUTH) ||
360                      (tile->east && input_direction == D_EAST) ||
361                      (tile->west && input_direction == D_WEST)))
362                     {  // player has changed direction during auto-movement
363                     direction = input_direction;
364                     back_direction = reverse_dir(direction);
365                     }
366                   else if(direction != input_direction)
367                     {  // player has changed to impossible tile
368                       back_direction = reverse_dir(direction);
369                       stop();
370                     }
371                   else
372                     {
373                     Direction dir = D_NONE;
374                   
375                     if (tile->north && back_direction != D_NORTH)
376                       dir = D_NORTH;
377                     else if (tile->south && back_direction != D_SOUTH)
378                       dir = D_SOUTH;
379                     else if (tile->east && back_direction != D_EAST)
380                       dir = D_EAST;
381                     else if (tile->west && back_direction != D_WEST)
382                       dir = D_WEST;
383
384                     if (dir != D_NONE)
385                       {
386                       direction = dir;
387                       input_direction = direction;
388                       back_direction = reverse_dir(direction);
389                       }
390                     else
391                       {
392                       // Should never be reached if tiledata is good
393                       stop();
394                       return;
395                       }
396                     }
397                   }
398
399               // Walk automatically to the next tile
400               if(direction != D_NONE)
401                 {
402                 Vector next_tile;
403                 if (worldmap->path_ok(direction, tile_pos, &next_tile))
404                   {
405                   tile_pos = next_tile;
406                   }
407                 else
408                   {
409                   puts("Tilemap data is buggy");
410                   stop();
411                   }
412                 }
413             }
414         }
415     }
416 }
417
418 //---------------------------------------------------------------------------
419 Tile::Tile()
420 {
421 }
422
423 Tile::~Tile()
424 {
425   delete sprite;
426 }
427
428 //---------------------------------------------------------------------------
429
430 WorldMap::WorldMap()
431 {
432   tile_manager = new TileManager();
433   //tux = new Tux(this);
434   
435   width  = 20;
436   height = 15;
437   
438   start_x = 4;
439   start_y = 5;
440   
441   leveldot_green = new Surface(datadir +  "/images/worldmap/leveldot_green.png", true);
442   leveldot_red = new Surface(datadir +  "/images/worldmap/leveldot_red.png", true);
443   messagedot   = new Surface(datadir +  "/images/worldmap/messagedot.png", true);
444   teleporterdot   = new Surface(datadir +  "/images/worldmap/teleporterdot.png", true);
445
446   enter_level = false;
447
448   name = "<no title>";
449   music = "SALCON.MOD";
450 }
451
452 WorldMap::~WorldMap()
453 {
454   delete tux;
455   delete tile_manager;
456
457   delete leveldot_green;
458   delete leveldot_red;
459   delete messagedot;
460   delete teleporterdot;
461 }
462
463 void
464 WorldMap::load_map()
465 {
466   lisp_object_t* root_obj = lisp_read_from_file(datadir + "/levels/worldmap/" + map_filename);
467   if (!root_obj)
468     Termination::abort("Couldn't load file", datadir + "/levels/worldmap/" + map_filename);
469
470   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-worldmap") == 0)
471     {
472       lisp_object_t* cur = lisp_cdr(root_obj);
473
474       while(!lisp_nil_p(cur))
475         {
476           lisp_object_t* element = lisp_car(cur);
477
478           if (strcmp(lisp_symbol(lisp_car(element)), "tilemap") == 0)
479             {
480               LispReader reader(lisp_cdr(element));
481               reader.read_int("width",  width);
482               reader.read_int("height", height);
483               reader.read_int_vector("data", tilemap);
484             }
485           else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
486             {
487               LispReader reader(lisp_cdr(element));
488               reader.read_string("name", name, true);
489               reader.read_string("music", music);
490               reader.read_int("start_pos_x", start_x);
491               reader.read_int("start_pos_y", start_y);
492             }
493           else if (strcmp(lisp_symbol(lisp_car(element)), "special-tiles") == 0 ||
494                    strcmp(lisp_symbol(lisp_car(element)), "levels") == 0)
495             {
496               lisp_object_t* cur = lisp_cdr(element);
497               
498               while(!lisp_nil_p(cur))
499                 {
500                   lisp_object_t* element = lisp_car(cur);
501
502                   if (strcmp(lisp_symbol(lisp_car(element)), "special-tile") == 0)
503                     {
504                       SpecialTile special_tile;
505                       LispReader reader(lisp_cdr(element));
506                       special_tile.solved = false;
507                       
508                       special_tile.north = true;
509                       special_tile.east  = true;
510                       special_tile.south = true;
511                       special_tile.west  = true;
512
513                       reader.read_int("x", special_tile.x);
514                       reader.read_int("y", special_tile.y);
515                       reader.read_string("level", special_tile.level_name, true);
516
517                       special_tile.vertical_flip = false;
518                       reader.read_bool("vertical-flip", special_tile.vertical_flip);
519
520                       special_tile.map_message.erase();
521                       reader.read_string("map-message", special_tile.map_message);
522                       special_tile.passive_message = false;
523                       reader.read_bool("passive-message", special_tile.passive_message);
524
525                       special_tile.teleport_dest_x = special_tile.teleport_dest_y = -1;
526                       reader.read_int("teleport-to-x", special_tile.teleport_dest_x);
527                       reader.read_int("teleport-to-y", special_tile.teleport_dest_y);
528
529                       special_tile.invisible = false;
530                       reader.read_bool("invisible-tile", special_tile.invisible);
531
532                       special_tile.apply_action_north = special_tile.apply_action_south =
533                           special_tile.apply_action_east = special_tile.apply_action_west =
534                           true;
535                       std::string apply_direction;
536                       reader.read_string("apply-to-direction", apply_direction);
537                       if(!apply_direction.empty())
538                         {
539                         special_tile.apply_action_north = special_tile.apply_action_south =
540                             special_tile.apply_action_east = special_tile.apply_action_west =
541                             false;
542                         if(apply_direction.find("north") != std::string::npos)
543                           special_tile.apply_action_north = true;
544                         if(apply_direction.find("south") != std::string::npos)
545                           special_tile.apply_action_south = true;
546                         if(apply_direction.find("east") != std::string::npos)
547                           special_tile.apply_action_east = true;
548                         if(apply_direction.find("west") != std::string::npos)
549                           special_tile.apply_action_west = true;
550                         }
551
552                       reader.read_string("extro-filename", special_tile.extro_filename);
553                       reader.read_string("next-world", special_tile.next_worldmap);
554                       special_tile.quit_worldmap = false;
555                       reader.read_bool("exit-game", special_tile.quit_worldmap);
556
557                       special_tile.auto_path = true;
558                       reader.read_bool("auto-path", special_tile.auto_path);
559
560                       special_tiles.push_back(special_tile);
561                     }
562
563                   /* Kept for backward compability */
564                   else if (strcmp(lisp_symbol(lisp_car(element)), "level") == 0)
565                     {
566                       SpecialTile special_tile;
567                       LispReader reader(lisp_cdr(element));
568                       special_tile.solved = false;
569                       
570                       special_tile.north = true;
571                       special_tile.east  = true;
572                       special_tile.south = true;
573                       special_tile.west  = true;
574
575                       special_tile.invisible = false;
576
577                       special_tile.apply_action_north = special_tile.apply_action_south =
578                           special_tile.apply_action_east = special_tile.apply_action_west =
579                           true;
580                       special_tile.vertical_flip = false;
581                       special_tile.teleport_dest_x = special_tile.teleport_dest_y = -1;
582
583                       reader.read_string("extro-filename", special_tile.extro_filename);
584                       if(!special_tile.extro_filename.empty())
585                         special_tile.quit_worldmap = true;
586                       reader.read_string("name", special_tile.level_name, true);
587                       reader.read_int("x", special_tile.x);
588                       reader.read_int("y", special_tile.y);
589
590
591                       special_tiles.push_back(special_tile);
592                     }
593                   
594                   cur = lisp_cdr(cur);      
595                 }
596             }
597           else
598             {
599               
600             }
601           
602           cur = lisp_cdr(cur);
603         }
604     }
605
606     lisp_free(root_obj);
607     tux = new Tux(this);
608 }
609
610 void WorldMap::get_level_title(SpecialTile& special_tile)
611 {
612   /** get special_tile's title */
613   special_tile.title = "<no title>";
614
615   LispReader* reader = LispReader::load(datadir + "/levels/" + special_tile.level_name, "supertux-level");
616   if(!reader)
617     {
618     std::cerr << "Error: Could not open level file. Ignoring...\n";
619     return;
620     }
621
622   reader->read_string("name", special_tile.title, true);
623   delete reader;
624 }
625
626 void
627 WorldMap::on_escape_press()
628 {
629   // Show or hide the menu
630   if(!Menu::current())
631     {
632     Menu::set_current(worldmap_menu); 
633     tux->set_direction(D_NONE);  // stop tux movement when menu is called
634     }
635   else
636     Menu::set_current(0); 
637 }
638
639 void
640 WorldMap::get_input()
641 {
642   enter_level = false;
643    
644   SDL_Event event;
645   while (SDL_PollEvent(&event))
646     {
647       if (Menu::current())
648         {
649           Menu::current()->event(event);
650         }
651       else
652         {
653           switch(event.type)
654             {
655             case SDL_QUIT:
656               Termination::abort("Received window close", "");
657               break;
658           
659             case SDL_KEYDOWN:
660               switch(event.key.keysym.sym)
661                 {
662                 case SDLK_ESCAPE:
663                   on_escape_press();
664                   break;
665                 case SDLK_LCTRL:
666                 case SDLK_RETURN:
667                   enter_level = true;
668                   break;
669
670                 case SDLK_LEFT:
671                   tux->set_direction(D_WEST);
672                   break;
673                 case SDLK_RIGHT:
674                   tux->set_direction(D_EAST);
675                   break;
676                 case SDLK_UP:
677                   tux->set_direction(D_NORTH);
678                   break;
679                 case SDLK_DOWN:
680                   tux->set_direction(D_SOUTH);
681                   break;
682
683                 default:
684                   break;
685                 }
686               break;
687
688             case SDL_JOYHATMOTION:
689               if(event.jhat.value & SDL_HAT_UP) {
690                 tux->set_direction(D_NORTH);
691               } else if(event.jhat.value & SDL_HAT_DOWN) {
692                 tux->set_direction(D_SOUTH);
693               } else if(event.jhat.value & SDL_HAT_LEFT) {
694                 tux->set_direction(D_WEST);
695               } else if(event.jhat.value & SDL_HAT_RIGHT) {
696                 tux->set_direction(D_EAST);
697               }
698               break;
699           
700             case SDL_JOYAXISMOTION:
701               if (event.jaxis.axis == joystick_keymap.x_axis)
702                 {
703                   if (event.jaxis.value < -joystick_keymap.dead_zone)
704                     tux->set_direction(D_WEST);
705                   else if (event.jaxis.value > joystick_keymap.dead_zone)
706                     tux->set_direction(D_EAST);
707                 }
708               else if (event.jaxis.axis == joystick_keymap.y_axis)
709                 {
710                   if (event.jaxis.value > joystick_keymap.dead_zone)
711                     tux->set_direction(D_SOUTH);
712                   else if (event.jaxis.value < -joystick_keymap.dead_zone)
713                     tux->set_direction(D_NORTH);
714                 }
715               break;
716
717             case SDL_JOYBUTTONDOWN:
718               if (event.jbutton.button == joystick_keymap.b_button)
719                 enter_level = true;
720               else if (event.jbutton.button == joystick_keymap.start_button)
721                 on_escape_press();
722               break;
723
724             default:
725               break;
726             }
727         }
728     }
729 }
730
731 Vector
732 WorldMap::get_next_tile(Vector pos, Direction direction)
733 {
734   switch(direction)
735     {
736     case D_WEST:
737       pos.x -= 1;
738       break;
739     case D_EAST:
740       pos.x += 1;
741       break;
742     case D_NORTH:
743       pos.y -= 1;
744       break;
745     case D_SOUTH:
746       pos.y += 1;
747       break;
748     case D_NONE:
749       break;
750     }
751   return pos;
752 }
753
754 bool
755 WorldMap::path_ok(Direction direction, Vector old_pos, Vector* new_pos)
756 {
757   *new_pos = get_next_tile(old_pos, direction);
758
759   if (!(new_pos->x >= 0 && new_pos->x < width
760         && new_pos->y >= 0 && new_pos->y < height))
761     { // New position is outsite the tilemap
762       return false;
763     }
764   else if(at(*new_pos)->one_way != BOTH_WAYS)
765     {
766 std::cerr << "one way only\n";
767       if((at(*new_pos)->one_way == NORTH_SOUTH_WAY && direction != D_SOUTH) ||
768          (at(*new_pos)->one_way == SOUTH_NORTH_WAY && direction != D_NORTH) ||
769          (at(*new_pos)->one_way == EAST_WEST_WAY && direction != D_WEST) ||
770          (at(*new_pos)->one_way == WEST_EAST_WAY && direction != D_EAST))
771         return false;
772       return true;
773     }
774   else
775     { // Check if we the tile allows us to go to new_pos
776       switch(direction)
777         {
778         case D_WEST:
779           return (at(old_pos)->west && at(*new_pos)->east);
780
781         case D_EAST:
782           return (at(old_pos)->east && at(*new_pos)->west);
783
784         case D_NORTH:
785           return (at(old_pos)->north && at(*new_pos)->south);
786
787         case D_SOUTH:
788           return (at(old_pos)->south && at(*new_pos)->north);
789
790         case D_NONE:
791           assert(!"path_ok() can't work if direction is NONE");
792         }
793       return false;
794     }
795 }
796
797 void
798 WorldMap::update(float delta)
799 {
800   if (enter_level && !tux->is_moving())
801     {
802       bool level_finished = true;
803       SpecialTile* special_tile = at_special_tile();
804       if (!special_tile)
805         {
806         std::cout << "Nothing to enter at: "
807           << tux->get_tile_pos().x << ", " << tux->get_tile_pos().y << std::endl;
808         return;
809         }
810
811
812       if(!special_tile->level_name.empty())
813         {
814           if (special_tile->x == tux->get_tile_pos().x && 
815               special_tile->y == tux->get_tile_pos().y)
816             {
817               PlayerStatus old_player_status = player_status;
818
819               std::cout << "Enter the current level: " << special_tile->level_name << std::endl;
820               // do a shriking fade to the special_tile
821               shrink_fade(Vector((special_tile->x*32 + 16 + offset.x),(special_tile->y*32 + 16
822                       + offset.y)), 500);
823               GameSession session(datadir +  "/levels/" + special_tile->level_name,
824                                   ST_GL_LOAD_LEVEL_FILE, special_tile->vertical_flip);
825
826               switch (session.run())
827                 {
828                 case GameSession::ES_LEVEL_FINISHED:
829                   {
830                     level_finished = true;
831                     bool old_level_state = special_tile->solved;
832                     special_tile->solved = true;
833
834                     if (session.get_current_sector()->player->got_power !=
835                           session.get_current_sector()->player->NONE_POWER)
836                       player_status.bonus = PlayerStatus::FLOWER_BONUS;
837                     else if (session.get_current_sector()->player->size == BIG)
838                       player_status.bonus = PlayerStatus::GROWUP_BONUS;
839                     else
840                       player_status.bonus = PlayerStatus::NO_BONUS;
841
842                     if (old_level_state != special_tile->solved && special_tile->auto_path)
843                       { // Try to detect the next direction to which we should walk
844                         // FIXME: Mostly a hack
845                         Direction dir = D_NONE;
846                     
847                         Tile* tile = at(tux->get_tile_pos());
848
849                         if (tile->north && tux->back_direction != D_NORTH)
850                           dir = D_NORTH;
851                         else if (tile->south && tux->back_direction != D_SOUTH)
852                           dir = D_SOUTH;
853                         else if (tile->east && tux->back_direction != D_EAST)
854                           dir = D_EAST;
855                         else if (tile->west && tux->back_direction != D_WEST)
856                           dir = D_WEST;
857
858                         if (dir != D_NONE)
859                           {
860                             tux->set_direction(dir);
861                             //tux->update(delta);
862                           }
863
864                         std::cout << "Walk to dir: " << dir << std::endl;
865                       }
866                   }
867
868                   break;
869                 case GameSession::ES_LEVEL_ABORT:
870                   level_finished = false;
871                   /* In case the player's abort the special_tile, keep it using the old
872                       status. But the minimum lives and no bonus. */
873                   player_status.score = old_player_status.score;
874                   player_status.distros = old_player_status.distros;
875                   player_status.lives = std::min(old_player_status.lives, player_status.lives);
876                   player_status.bonus = player_status.NO_BONUS;
877
878                   break;
879                 case GameSession::ES_GAME_OVER:
880                   {
881                   level_finished = false;
882                   /* draw an end screen */
883                   /* in the future, this should make a dialog a la SuperMario, asking
884                   if the player wants to restart the world map with no score and from
885                   special_tile 1 */
886                   char str[80];
887
888                   DrawingContext context;
889                   context.draw_gradient(Color (200,240,220), Color(200,200,220),
890                       LAYER_BACKGROUND0);
891
892                   context.draw_text_center(blue_text, _("GAMEOVER"), 
893                       Vector(0, 200), LAYER_FOREGROUND1);
894
895                   sprintf(str, _("SCORE: %d"), player_status.score);
896                   context.draw_text_center(gold_text, str,
897                       Vector(0, 230), LAYER_FOREGROUND1);
898
899                   sprintf(str, _("COINS: %d"), player_status.distros);
900                   context.draw_text_center(gold_text, str,
901                       Vector(0, screen->w - 32), LAYER_FOREGROUND1);
902
903                   context.do_drawing();
904   
905                   SDL_Event event;
906                   wait_for_event(event,2000,5000,true);
907
908                   quit = true;
909                   player_status.reset();
910                   break;
911                   }
912                 case GameSession::ES_NONE:
913                   assert(false);
914                   // Should never be reached 
915                   break;
916                 }
917
918               SoundManager::get()->play_music(song);
919               Menu::set_current(0);
920               if (!savegame_file.empty())
921                 savegame(savegame_file);
922             }
923         }
924       /* The porpose of the next checking is that if the player lost
925          the special_tile (in case there is one), don't show anything */
926       if(level_finished)
927         {
928         if (!special_tile->extro_filename.empty())
929           {
930           // Display a text file
931           display_text_file(special_tile->extro_filename, SCROLL_SPEED_MESSAGE, white_big_text , white_text, white_small_text, blue_text );
932           }
933         if (special_tile->teleport_dest_x != -1 && special_tile->teleport_dest_y != -1)
934           {
935           // TODO: an animation, camera scrolling or a fading would be a nice touch
936           SoundManager::get()->play_sound(IDToSound(SND_WARP));
937           tux->back_direction = D_NONE;
938           tux->set_tile_pos(Vector(special_tile->teleport_dest_x, special_tile->teleport_dest_y));
939           SDL_Delay(1000);
940           }
941         if (!special_tile->next_worldmap.empty())
942           {
943           // Load given worldmap
944           loadmap(special_tile->next_worldmap);
945           }
946         if (special_tile->quit_worldmap)
947           quit = true;
948         }
949     }
950   else
951     {
952       tux->action(delta);
953 //      tux->set_direction(input_direction);
954     }
955   
956   Menu* menu = Menu::current();
957   if(menu)
958     {
959       menu->action();
960
961       if(menu == worldmap_menu)
962         {
963           switch (worldmap_menu->check())
964             {
965             case MNID_RETURNWORLDMAP: // Return to game
966               break;
967             case MNID_QUITWORLDMAP: // Quit Worldmap
968               quit = true;
969               break;
970             }
971         }
972       else if(menu == options_menu)
973         {
974           process_options_menu();
975         }
976     }
977 }
978
979 Tile*
980 WorldMap::at(Vector p)
981 {
982   assert(p.x >= 0 
983          && p.x < width
984          && p.y >= 0
985          && p.y < height);
986
987   int x = int(p.x);
988   int y = int(p.y);
989   return tile_manager->get(tilemap[width * y + x]);
990 }
991
992 WorldMap::SpecialTile*
993 WorldMap::at_special_tile()
994 {
995   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
996     {
997       if (i->x == tux->get_tile_pos().x && 
998           i->y == tux->get_tile_pos().y)
999         return &*i; 
1000     }
1001
1002   return 0;
1003 }
1004
1005
1006 void
1007 WorldMap::draw(DrawingContext& context, const Vector& offset)
1008 {
1009   for(int y = 0; y < height; ++y)
1010     for(int x = 0; x < width; ++x)
1011       {
1012         Tile* tile = at(Vector(x, y));
1013         context.draw_surface(tile->sprite,
1014             Vector(x*32 + offset.x, y*32 + offset.y), LAYER_TILES);
1015       }
1016   
1017   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1018     {
1019       if(i->invisible)
1020         continue;
1021       if(i->level_name.empty())
1022         {
1023         if (i->teleport_dest_x != -1 && i->teleport_dest_y != -1)
1024           context.draw_surface(teleporterdot,
1025               Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
1026
1027         else if (!i->map_message.empty() && !i->passive_message)
1028           context.draw_surface(messagedot,
1029               Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
1030
1031         continue;
1032         }
1033
1034       if (i->solved)
1035         context.draw_surface(leveldot_green,
1036             Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
1037       else
1038         context.draw_surface(leveldot_red,
1039             Vector(i->x*32 + offset.x, i->y*32 + offset.y), LAYER_TILES+1);
1040     }
1041
1042   tux->draw(context, offset);
1043   draw_status(context);
1044 }
1045
1046 void
1047 WorldMap::draw_status(DrawingContext& context)
1048 {
1049   char str[80];
1050   sprintf(str, " %d", player_status.score);
1051
1052   context.draw_text(white_text, _("SCORE"), Vector(0, 0), LAYER_FOREGROUND1);
1053   context.draw_text(gold_text, str, Vector(96, 0), LAYER_FOREGROUND1);
1054
1055   sprintf(str, "%d", player_status.distros);
1056   context.draw_text(white_text, _("COINS"), Vector(screen->w/2 - 16*5, 0),
1057       LAYER_FOREGROUND1);
1058   context.draw_text(gold_text, str, Vector(screen->w/2 + (16*5)/2, 0),
1059         LAYER_FOREGROUND1);
1060
1061   if (player_status.lives >= 5)
1062     {
1063       sprintf(str, "%dx", player_status.lives);
1064       context.draw_text(gold_text, str, 
1065           Vector(screen->w - gold_text->get_text_width(str) - tux_life->w, 0),
1066           LAYER_FOREGROUND1);
1067       context.draw_surface(tux_life, Vector(screen->w -
1068             gold_text->get_text_width("9"), 0), LAYER_FOREGROUND1);
1069     }
1070   else
1071     {
1072       for(int i= 0; i < player_status.lives; ++i)
1073         context.draw_surface(tux_life,
1074             Vector(screen->w - tux_life->w*4 + (tux_life->w*i), 0),
1075             LAYER_FOREGROUND1);
1076     }
1077   context.draw_text(white_text, _("LIVES"),
1078       Vector(screen->w - white_text->get_text_width(_("LIVES")) - white_text->get_text_width("   99"), 0),
1079       LAYER_FOREGROUND1);
1080
1081   if (!tux->is_moving())
1082     {
1083       for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1084         {
1085           if (i->x == tux->get_tile_pos().x && 
1086               i->y == tux->get_tile_pos().y)
1087             {
1088               if(!i->level_name.empty())
1089                 {
1090                 if(i->title == "")
1091                   get_level_title(*i);
1092
1093                 context.draw_text_center(white_text, i->title, 
1094                     Vector(0, screen->h - white_text->get_height() - 30),
1095                     LAYER_FOREGROUND1);
1096                 }
1097
1098               /* Display an in-map message in the map, if any as been selected */
1099               if(!i->map_message.empty() && !i->passive_message)
1100                 context.draw_text_center(gold_text, i->map_message, 
1101                     Vector(0, screen->h - white_text->get_height() - 60),
1102                     LAYER_FOREGROUND1);
1103               break;
1104             }
1105         }
1106     }
1107   /* Display a passive message in the map, if needed */
1108   if(passive_message_timer.check())
1109     context.draw_text_center(gold_text, passive_message, 
1110             Vector(0, screen->h - white_text->get_height() - 60),
1111             LAYER_FOREGROUND1);
1112 }
1113
1114 void
1115 WorldMap::display()
1116 {
1117   Menu::set_current(0);
1118
1119   quit = false;
1120
1121   song = SoundManager::get()->load_music(datadir +  "/music/" + music);
1122   SoundManager::get()->play_music(song);
1123
1124   FrameRate frame_rate(10);
1125   frame_rate.set_frame_limit(false);
1126
1127   frame_rate.start();
1128
1129   DrawingContext context;
1130   while(!quit)
1131     {
1132       float delta = frame_rate.get();
1133
1134       delta *= 1.3f;
1135
1136       if (delta > 10.0f)
1137         delta = .3f;
1138         
1139       frame_rate.update();
1140
1141       Vector tux_pos = tux->get_pos();
1142       if (1)
1143         {
1144           offset.x = -tux_pos.x + screen->w/2;
1145           offset.y = -tux_pos.y + screen->h/2;
1146
1147           if (offset.x > 0) offset.x = 0;
1148           if (offset.y > 0) offset.y = 0;
1149
1150           if (offset.x < screen->w - width*32) offset.x = screen->w - width*32;
1151           if (offset.y < screen->h - height*32) offset.y = screen->h - height*32;
1152         } 
1153
1154       draw(context, offset);
1155       get_input();
1156       update(delta);
1157       
1158       if(Menu::current())
1159         {
1160           Menu::current()->draw(context);
1161           mouse_cursor->draw(context);
1162         }
1163
1164       context.do_drawing();
1165
1166       SDL_Delay(20);
1167     }
1168 }
1169
1170 void
1171 WorldMap::savegame(const std::string& filename)
1172 {
1173   if(filename == "")
1174     return;
1175
1176   std::cout << "savegame: " << filename << std::endl;
1177   std::ofstream out(filename.c_str());
1178
1179   int nb_solved_levels = 0;
1180   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1181     {
1182       if (i->solved)
1183         ++nb_solved_levels;
1184     }
1185
1186   out << "(supertux-savegame\n"
1187       << "  (version 1)\n"
1188       << "  (title  \"" << name << " - " << nb_solved_levels << "/" << special_tiles.size() << "\")\n"
1189       << "  (map    \"" << map_filename << "\")\n"
1190       << "  (lives   " << player_status.lives << ")\n"
1191       << "  (score   " << player_status.score << ")\n"
1192       << "  (distros " << player_status.distros << ")\n"
1193       << "  (tux (x " << tux->get_tile_pos().x << ") (y " << tux->get_tile_pos().y << ")\n"
1194       << "       (back \"" << direction_to_string(tux->back_direction) << "\")\n"
1195       << "       (bonus \"" << bonus_to_string(player_status.bonus) <<  "\"))\n"
1196       << "  (levels\n";
1197   
1198   for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1199     {
1200       if (i->solved && !i->level_name.empty())
1201         {
1202           out << "     (level (name \"" << i->level_name << "\")\n"
1203               << "            (solved #t))\n";
1204         }
1205     }  
1206
1207   out << "   )\n"
1208       << " )\n\n;; EOF ;;" << std::endl;
1209 }
1210
1211 void
1212 WorldMap::loadgame(const std::string& filename)
1213 {
1214   std::cout << "loadgame: " << filename << std::endl;
1215   savegame_file = filename;
1216   map_filename = "icyisland.stwm";
1217
1218   if (access(filename.c_str(), F_OK) != 0)
1219     {
1220     load_map();
1221     return;
1222     }
1223   
1224   lisp_object_t* savegame = lisp_read_from_file(filename);
1225   if (!savegame)
1226     {
1227       std::cout << "WorldMap:loadgame: File not found: " << filename << std::endl;
1228       load_map();
1229       return;
1230     }
1231
1232   lisp_object_t* cur = savegame;
1233
1234   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-savegame") != 0)
1235     {
1236     load_map();
1237     return;
1238     }
1239
1240   cur = lisp_cdr(cur);
1241   LispReader reader(cur);
1242
1243   /* Get the Map filename and then load it before setting special_tile settings */
1244   reader.read_string("map", map_filename);
1245   load_map(); 
1246
1247   reader.read_int("lives", player_status.lives);
1248   reader.read_int("score", player_status.score);
1249   reader.read_int("distros", player_status.distros);
1250
1251   if (player_status.lives < 0)
1252     player_status.lives = START_LIVES;
1253
1254   lisp_object_t* tux_cur = 0;
1255   if (reader.read_lisp("tux", tux_cur))
1256     {
1257       Vector p;
1258       std::string back_str = "none";
1259       std::string bonus_str = "none";
1260
1261       LispReader tux_reader(tux_cur);
1262       tux_reader.read_float("x", p.x);
1263       tux_reader.read_float("y", p.y);
1264       tux_reader.read_string("back", back_str);
1265       tux_reader.read_string("bonus", bonus_str);
1266       
1267       player_status.bonus = string_to_bonus(bonus_str);
1268       tux->back_direction = string_to_direction(back_str);      
1269       tux->set_tile_pos(p);
1270     }
1271
1272   lisp_object_t* level_cur = 0;
1273   if (reader.read_lisp("levels", level_cur))
1274     {
1275       while(level_cur)
1276         {
1277           lisp_object_t* sym  = lisp_car(lisp_car(level_cur));
1278           lisp_object_t* data = lisp_cdr(lisp_car(level_cur));
1279
1280           if (strcmp(lisp_symbol(sym), "level") == 0)
1281             {
1282               std::string name;
1283               bool solved = false;
1284
1285               LispReader level_reader(data);
1286               level_reader.read_string("name", name, true);
1287               level_reader.read_bool("solved", solved);
1288
1289               for(SpecialTiles::iterator i = special_tiles.begin(); i != special_tiles.end(); ++i)
1290                 {
1291                   if (name == i->level_name)
1292                     i->solved = solved;
1293                 }
1294             }
1295
1296           level_cur = lisp_cdr(level_cur);
1297         }
1298     }
1299  
1300   lisp_free(savegame);
1301 }
1302
1303 void
1304 WorldMap::loadmap(const std::string& filename)
1305 {
1306   savegame_file = "";
1307   map_filename = filename;
1308   load_map();
1309 }
1310
1311 } // namespace WorldMapNS
1312
1313 /* Local Variables: */
1314 /* mode:c++ */
1315 /* End: */
1316