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