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