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