Cheating handled by Console
[supertux.git] / src / worldmap.hpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 // 
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #ifndef SUPERTUX_WORLDMAP_H
21 #define SUPERTUX_WORLDMAP_H
22
23 #include <vector>
24 #include <string>
25
26 #include "math/vector.hpp"
27 #include "video/screen.hpp"
28 #include "lisp/lisp.hpp"
29 #include "control/controller.hpp"
30 #include "statistics.hpp"
31 #include "timer.hpp"
32 #include "tile_manager.hpp"
33 #include "game_object.hpp"
34 #include "console.hpp"
35
36 class Sprite;
37 class Menu;
38 class SpawnPoint;
39 class GameObject;
40 class TileMap;
41 extern Menu* worldmap_menu;
42
43 namespace WorldMapNS {
44
45 enum WorldMapMenuIDs {
46   MNID_RETURNWORLDMAP,
47   MNID_QUITWORLDMAP
48 };
49
50 // For one way tiles
51 enum {
52   BOTH_WAYS,
53   NORTH_SOUTH_WAY,
54   SOUTH_NORTH_WAY,
55   EAST_WEST_WAY,
56   WEST_EAST_WAY
57 };
58
59 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
60
61 std::string direction_to_string(Direction d);
62 Direction   string_to_direction(const std::string& d);
63 Direction reverse_dir(Direction d);
64
65 class WorldMap;
66
67 class Tux : public GameObject
68 {
69 public:
70   Direction back_direction;
71 private:
72   WorldMap* worldmap;
73   Sprite* tux_sprite;
74   Controller* controller;
75
76   Direction input_direction;
77   Direction direction;
78   Vector tile_pos;
79   /** Length by which tux is away from its current tile, length is in
80       input_direction direction */
81   float offset;
82   bool  moving;
83
84   void stop();
85
86   bool canWalk(const Tile* tile, Direction dir); /**< check if we can leave "tile" in direction "dir" */
87   void updateInputDirection(); /**< if controller was pressed, update input_direction */
88   void tryStartWalking(); /**< try starting to walk in input_direction */
89   void tryContinueWalking(float elapsed_time); /**< try to continue walking in current direction */
90
91 public: 
92   Tux(WorldMap* worldmap_);
93   ~Tux();
94   
95   void draw(DrawingContext& context);
96   void update(float elapsed_time);
97
98   void set_direction(Direction dir);
99
100   bool is_moving() const { return moving; }
101   Vector get_pos();
102   Vector get_tile_pos() const { return tile_pos; } 
103   void  set_tile_pos(Vector p) { tile_pos = p; } 
104 };
105
106 /** */
107 class WorldMap
108 {
109 private:
110   Tux* tux;
111
112   bool quit;
113
114   Surface* leveldot_green;
115   Surface* leveldot_red;
116   Surface* messagedot;
117   Sprite* teleporterdot;
118
119   std::string name;
120   std::string music;
121
122   typedef std::vector<GameObject*> GameObjects;
123   GameObjects game_objects;
124   TileMap* solids;
125   
126   TileManager* tile_manager;
127   
128   Console* console;
129
130 public:
131   struct SpecialTile
132   {
133     Vector pos;
134
135     /** Optional flags: */
136
137     /** Sprite to render instead of guessing what image to draw */
138     Sprite* sprite;
139
140     /** Position to swap to player */
141     Vector teleport_dest;
142
143     /** Message to show in the Map */
144     std::string map_message;
145     bool passive_message;
146
147     /** Hide special tile */
148     bool invisible;
149
150     /** Only applies actions (ie. passive messages) when going to that direction */
151     bool apply_action_north;
152     bool apply_action_east;
153     bool apply_action_south;
154     bool apply_action_west;
155   };
156
157   struct Level
158   {
159     Vector pos;
160
161     std::string name;
162     std::string title;
163     bool solved;
164
165     Sprite* sprite;
166
167     /** Statistics for level tiles */
168     Statistics statistics;
169
170     /** Optional flags: */
171
172     /** Check if this level should be vertically flipped */
173     bool vertical_flip;
174
175     /** Script that is run when the level is successfully finished */
176     std::string extro_script;
177
178     /** Go to this world */
179     std::string next_worldmap;
180
181     /** Quit the worldmap */
182     bool quit_worldmap;
183
184     /** If false, disables the auto walking after finishing a level */
185     bool auto_path;
186
187     // Directions which are walkable from this level
188     bool north;
189     bool east;
190     bool south;
191     bool west;
192   };
193
194   /** Variables to deal with the passive map messages */
195   Timer passive_message_timer;
196   std::string passive_message;
197
198 private:
199   std::string map_filename;
200   std::string levels_path;
201
202   typedef std::vector<SpecialTile> SpecialTiles;
203   SpecialTiles special_tiles;
204   typedef std::vector<Level> Levels;
205   Levels levels;
206   typedef std::vector<SpawnPoint*> SpawnPoints;
207   SpawnPoints spawn_points;
208
209   Vector offset;
210   std::string savegame_file;
211   
212   std::string intro_script;
213   bool intro_displayed;
214
215   void get_level_title(Level& level);
216
217   void draw_status(DrawingContext& context);
218
219   // to avoid calculating total stats all the time. This way only
220   // when need, it is calculated.
221   Statistics total_stats;
222   void calculate_total_stats();
223
224 public:
225   WorldMap();
226   ~WorldMap();
227
228   /** Busy loop */
229   void display();
230
231   void load_map();
232   
233   void get_input();
234
235   void add_object(GameObject* object);
236   void clear_objects();
237
238   /** Update Tux position */
239   void update(float delta);
240
241   /** Draw one frame */
242   void draw(DrawingContext& context);
243
244   Vector get_next_tile(Vector pos, Direction direction);
245   const Tile* at(Vector pos);
246
247   WorldMap::Level* at_level();
248   WorldMap::SpecialTile* at_special_tile();
249
250   /** Check if it is possible to walk from \a pos into \a direction,
251       if possible, write the new position to \a new_pos */
252   bool path_ok(Direction direction, Vector pos, Vector* new_pos);
253
254   /* Save map to slot */
255   void savegame(const std::string& filename);
256   /* Load map from slot
257      You should call set_map_filename() before this */
258   void loadgame(const std::string& filename);
259   /* Load map directly from file */
260   void loadmap(const std::string& filename);
261
262   const std::string& get_world_title() const
263   { return name; }
264     
265   void set_map_filename(std::string filename)
266   { map_filename = filename; }
267
268 private:
269   void on_escape_press();
270   void parse_special_tile(const lisp::Lisp* lisp);
271   void parse_level_tile(const lisp::Lisp* lisp);
272 };
273
274 } // namespace WorldMapNS
275
276 #endif
277
278 /* Local Variables: */
279 /* mode:c++ */
280 /* End: */