changed worldmap format a bit to be more consistent with level format
[supertux.git] / src / worldmap.h
1 //  $Id$
2 // 
3 //  SuperTux
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 #ifndef SUPERTUX_WORLDMAP_H
20 #define SUPERTUX_WORLDMAP_H
21
22 #include <vector>
23 #include <string>
24
25 #include "math/vector.h"
26 #include "audio/musicref.h"
27 #include "video/screen.h"
28 #include "lisp/lisp.h"
29 #include "control/controller.h"
30 #include "statistics.h"
31 #include "timer.h"
32 #include "tile_manager.h"
33
34 class Sprite;
35 class Menu;
36 extern Menu* worldmap_menu;
37
38 namespace WorldMapNS {
39
40 enum WorldMapMenuIDs {
41   MNID_RETURNWORLDMAP,
42   MNID_QUITWORLDMAP
43 };
44
45 // For one way tiles
46 enum {
47   BOTH_WAYS,
48   NORTH_SOUTH_WAY,
49   SOUTH_NORTH_WAY,
50   EAST_WEST_WAY,
51   WEST_EAST_WAY
52 };
53
54 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
55
56 std::string direction_to_string(Direction d);
57 Direction   string_to_direction(const std::string& d);
58 Direction reverse_dir(Direction d);
59
60 class WorldMap;
61
62 class Tux
63 {
64 public:
65   Direction back_direction;
66 private:
67   WorldMap* worldmap;
68   Sprite* tux_sprite;
69   Controller* controller;
70
71   Direction input_direction;
72   Direction direction;
73   Vector tile_pos;
74   /** Length by which tux is away from its current tile, length is in
75       input_direction direction */
76   float offset;
77   bool  moving;
78
79   void stop();
80 public: 
81   Tux(WorldMap* worldmap_);
82   ~Tux();
83   
84   void draw(DrawingContext& context);
85   void update(float elapsed_time);
86
87   void set_direction(Direction dir);
88
89   bool is_moving() const { return moving; }
90   Vector get_pos();
91   Vector get_tile_pos() const { return tile_pos; } 
92   void  set_tile_pos(Vector p) { tile_pos = p; } 
93 };
94
95 /** */
96 class WorldMap
97 {
98 private:
99   Tux* tux;
100
101   bool quit;
102
103   Surface* leveldot_green;
104   Surface* leveldot_red;
105   Surface* messagedot;
106   Surface* teleporterdot;
107
108   std::string name;
109   std::string music;
110
111   std::vector<int> tilemap;
112   int width;
113   int height;
114   
115   int start_x;
116   int start_y;
117
118   TileManager* tile_manager;
119
120 public:
121   struct SpecialTile
122   {
123     Vector pos;
124
125     /** Optional flags: */
126
127     /** Position to swap to player */
128     Vector teleport_dest;
129
130     /** Message to show in the Map */
131     std::string map_message;
132     bool passive_message;
133
134     /** Hide special tile */
135     bool invisible;
136
137     /** Only applies actions (ie. passive messages) when going to that direction */
138     bool apply_action_north;
139     bool apply_action_east;
140     bool apply_action_south;
141     bool apply_action_west;
142   };
143
144   struct Level
145   {
146     Vector pos;
147
148     std::string name;
149     std::string title;
150     bool solved;
151
152     /** Statistics for level tiles */
153     Statistics statistics;
154
155     /** Optional flags: */
156
157     /** Check if this level should be vertically flipped */
158     bool vertical_flip;
159
160     /** Filename of the extro text to show once the level is
161         successfully completed */
162     std::string extro_filename;
163
164     /** Go to this world */
165     std::string next_worldmap;
166
167     /** Quit the worldmap */
168     bool quit_worldmap;
169
170     /** If false, disables the auto walking after finishing a level */
171     bool auto_path;
172
173     // Directions which are walkable from this level
174     bool north;
175     bool east;
176     bool south;
177     bool west;
178   };
179
180   /** Variables to deal with the passive map messages */
181   Timer passive_message_timer;
182   std::string passive_message;
183
184 private:
185   std::string map_filename;
186   std::string levels_path;
187
188   typedef std::vector<SpecialTile> SpecialTiles;
189   SpecialTiles special_tiles;
190
191   typedef std::vector<Level> Levels;
192   Levels levels;
193
194   MusicRef song;
195
196   Vector offset;
197   std::string savegame_file;
198   
199   std::string intro_filename;
200   bool intro_displayed;
201
202   void get_level_title(Level& level);
203
204   void draw_status(DrawingContext& context);
205
206   // to avoid calculating total stats all the time. This way only
207   // when need, it is calculated.
208   Statistics total_stats;
209   void calculate_total_stats();
210
211 public:
212   WorldMap();
213   ~WorldMap();
214
215   /** Busy loop */
216   void display();
217
218   void load_map();
219   
220   void get_input();
221
222   /** Update Tux position */
223   void update(float delta);
224
225   /** Draw one frame */
226   void draw(DrawingContext& context);
227
228   Vector get_next_tile(Vector pos, Direction direction);
229   const Tile* at(Vector pos);
230
231   WorldMap::Level* at_level();
232   WorldMap::SpecialTile* at_special_tile();
233
234   /** Check if it is possible to walk from \a pos into \a direction,
235       if possible, write the new position to \a new_pos */
236   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
237
238   /* Save map to slot */
239   void savegame(const std::string& filename);
240   /* Load map from slot
241      You should call set_map_filename() before this */
242   void loadgame(const std::string& filename);
243   /* Load map directly from file */
244   void loadmap(const std::string& filename);
245
246   const std::string& get_world_title() const
247     { return name; }
248     
249   const int& get_start_x() const
250     { return start_x; }
251   
252   const int& get_start_y() const
253     { return start_y; }
254
255   void set_map_filename(std::string filename)
256     { map_filename = filename; }
257
258 private:
259   void on_escape_press();
260   void parse_special_tile(const lisp::Lisp* lisp);
261   void parse_level_tile(const lisp::Lisp* lisp);
262 };
263
264 } // namespace WorldMapNS
265
266 #endif
267
268 /* Local Variables: */
269 /* mode:c++ */
270 /* End: */