Move patch to another directory
[supertux.git] / src / worldmap / worldmap.hpp
1 //  SuperTux
2 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmail.com>
3 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #ifndef HEADER_SUPERTUX_WORLDMAP_WORLDMAP_HPP
19 #define HEADER_SUPERTUX_WORLDMAP_WORLDMAP_HPP
20
21 #include <string>
22 #include <vector>
23
24 #include "control/controller.hpp"
25 #include "math/vector.hpp"
26 #include "supertux/console.hpp"
27 #include "supertux/game_object.hpp"
28 #include "supertux/game_object_ptr.hpp"
29 #include "supertux/level.hpp"
30 #include "supertux/screen.hpp"
31 #include "supertux/statistics.hpp"
32 #include "supertux/tile_manager.hpp"
33 #include "supertux/timer.hpp"
34 #include "util/reader_fwd.hpp"
35 #include "worldmap/direction.hpp"
36 #include "worldmap/spawn_point.hpp"
37 #include "worldmap/special_tile.hpp"
38 #include "worldmap/sprite_change.hpp"
39 #include "worldmap/teleporter.hpp"
40
41 class GameObject;
42 class PlayerStatus;
43 class Sprite;
44 class TileMap;
45 class Savegame;
46
47 namespace worldmap {
48
49 class Tux;
50 class LevelTile;
51 class SpecialTile;
52 class SpriteChange;
53
54 // For one way tiles
55 enum {
56   BOTH_WAYS,
57   NORTH_SOUTH_WAY,
58   SOUTH_NORTH_WAY,
59   EAST_WEST_WAY,
60   WEST_EAST_WAY
61 };
62
63 /**
64  * Screen that runs a WorldMap, which lets the player choose a Level.
65  */
66 class WorldMap : public Screen
67 {
68   static Color level_title_color;
69   static Color message_color;
70   static Color teleporter_message_color;
71
72 private:
73   typedef std::vector<SpecialTile*> SpecialTiles;
74   typedef std::vector<SpriteChange*> SpriteChanges;
75   typedef std::vector<SpawnPoint*> SpawnPoints;
76   typedef std::vector<LevelTile*> LevelTiles;
77   typedef std::vector<GameObjectPtr> GameObjects;
78   typedef std::vector<HSQOBJECT> ScriptList;
79
80   std::shared_ptr<Tux> tux;
81
82   Savegame& m_savegame;
83
84   TileSet *tileset;
85   bool     free_tileset;
86
87   static WorldMap* current_;
88
89   Vector camera_offset;
90
91   std::string name;
92   std::string music;
93   std::string init_script;
94
95   GameObjects game_objects;
96   std::list<TileMap*> solid_tilemaps;
97
98 public:
99   /** Variables to deal with the passive map messages */
100   Timer passive_message_timer;
101   std::string passive_message;
102
103 private:
104   std::string map_filename;
105   std::string levels_path;
106
107   SpecialTiles special_tiles;
108   LevelTiles levels;
109   SpriteChanges sprite_changes;
110   SpawnPoints spawn_points;
111   std::vector<Teleporter*> teleporters;
112
113   Statistics total_stats;
114
115   HSQOBJECT worldmap_table;
116   ScriptList scripts;
117
118   Color ambient_light;
119   std::string force_spawnpoint; /**< if set, spawnpoint will be forced to this value */
120
121   bool in_level;
122
123   /* variables to track panning to a spawn point */
124   Vector pan_pos;
125   bool panning;
126
127 public:
128   WorldMap(const std::string& filename, Savegame& savegame, const std::string& force_spawnpoint = "");
129   ~WorldMap();
130
131   void add_object(GameObjectPtr object);
132
133   void try_expose(const GameObjectPtr& object);
134   void try_unexpose(const GameObjectPtr& object);
135
136   static WorldMap* current()
137   { return current_; }
138
139   virtual void setup();
140   virtual void leave();
141
142   /** Update worldmap state */
143   virtual void update(float delta);
144   /** Draw worldmap */
145   virtual void draw(DrawingContext& context);
146
147   Vector get_next_tile(Vector pos, Direction direction);
148
149   /**
150    * gets a bitfield of Tile::WORLDMAP_NORTH | Tile::WORLDMAP_WEST | ... values,
151    * which indicates the directions Tux can move to when at the given position.
152    */
153   int available_directions_at(Vector pos);
154
155   /**
156    * returns a bitfield representing the union of all Tile::WORLDMAP_XXX values
157    * of all solid tiles at the given position
158    */
159   int tile_data_at(Vector pos);
160
161   size_t level_count();
162   size_t solved_level_count();
163
164   /**
165    * gets called from the GameSession when a level has been successfully
166    * finished
167    */
168   void finished_level(Level* level);
169
170   /** returns current Tux incarnation */
171   Tux* get_tux() { return tux.get(); }
172
173   Savegame& get_savegame() { return m_savegame; }
174
175   LevelTile* at_level();
176   SpecialTile* at_special_tile();
177   SpriteChange* at_sprite_change(const Vector& pos);
178   Teleporter* at_teleporter(const Vector& pos);
179
180   /** Check if it is possible to walk from \a pos into \a direction,
181       if possible, write the new position to \a new_pos */
182   bool path_ok(Direction direction, const Vector& pos, Vector* new_pos);
183
184   /**
185    * Save worldmap state to squirrel state table
186    */
187   void save_state();
188
189   /**
190    * Load worldmap state from squirrel state table
191    */
192   void load_state();
193
194   const std::string& get_title() const
195   { return name; }
196
197   /**
198    * runs a script in the context of the worldmap (and keeps a reference to
199    * the script (so the script gets destroyed when the worldmap is destroyed)
200    */
201   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
202
203   /**
204    * switch to another worldmap.
205    * filename is relative to data root path
206    */
207   void change(const std::string& filename, const std::string& force_spawnpoint="");
208
209   /**
210    * moves Tux to the given spawnpoint
211    */
212   void move_to_spawnpoint(const std::string& spawnpoint, bool pan =false);
213
214   /**
215    * returns the width (in tiles) of a worldmap
216    */
217   float get_width() const;
218
219   /**
220    * returns the height (in tiles) of a worldmap
221    */
222   float get_height() const;
223
224   /**
225    * Mark all levels as solved or unsolved
226    */
227   void set_levels_solved(bool solved, bool perfect);
228
229 private:
230   void get_level_title(LevelTile& level);
231   void get_level_target_time(LevelTile& level);
232   void draw_status(DrawingContext& context);
233   void calculate_total_stats();
234
235   void load(const std::string& filename);
236   void on_escape_press();
237
238   Vector get_camera_pos_for_tux();
239   void clamp_camera_position(Vector& c);
240   Vector last_position;
241   float last_target_time;
242
243 private:
244   WorldMap(const WorldMap&);
245   WorldMap& operator=(const WorldMap&);
246 };
247
248 } // namespace worldmap
249
250 #endif
251
252 /* EOF */