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