Replaced std::auto_ptr<> with std::unique_ptr<>
[supertux.git] / src / worldmap / worldmap.hpp
1 //  SuperTux
2 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
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 "util/reader_fwd.hpp"
26 #include "math/vector.hpp"
27 #include "supertux/console.hpp"
28 #include "supertux/game_object.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 "worldmap/direction.hpp"
35 #include "worldmap/spawn_point.hpp"
36 #include "worldmap/special_tile.hpp"
37 #include "worldmap/sprite_change.hpp"
38 #include "worldmap/teleporter.hpp"
39
40 class Sprite;
41 class Menu;
42 class GameObject;
43 class TileMap;
44 class PlayerStatus;
45
46 namespace worldmap {
47
48 class Tux;
49 class LevelTile;
50 class SpecialTile;
51 class SpriteChange;
52
53 // For one way tiles
54 enum {
55   BOTH_WAYS,
56   NORTH_SOUTH_WAY,
57   SOUTH_NORTH_WAY,
58   EAST_WEST_WAY,
59   WEST_EAST_WAY
60 };
61
62 /**
63  * Screen that runs a WorldMap, which lets the player choose a Level.
64  */
65 class WorldMap : public Screen
66 {
67   static Color level_title_color;
68   static Color message_color;
69   static Color teleporter_message_color;
70
71 private:
72   typedef std::vector<SpecialTile*> SpecialTiles;
73   typedef std::vector<SpriteChange*> SpriteChanges;
74   typedef std::vector<SpawnPoint*> SpawnPoints;
75   typedef std::vector<LevelTile*> LevelTiles;
76   typedef std::vector<GameObject*> GameObjects;
77   typedef std::vector<HSQOBJECT> ScriptList;
78
79   Tux* tux;
80
81   PlayerStatus* player_status;
82
83   TileSet *tileset;
84   bool     free_tileset;
85
86   static WorldMap* current_;
87
88   std::unique_ptr<Menu> worldmap_menu;
89
90   Vector camera_offset;
91
92   std::string name;
93   std::string music;
94   std::string init_script;
95
96   GameObjects game_objects;
97   std::list<TileMap*> solid_tilemaps;
98
99 public:
100   /** Variables to deal with the passive map messages */
101   Timer passive_message_timer;
102   std::string passive_message;
103
104 private:
105   std::string map_filename;
106   std::string levels_path;
107
108   SpecialTiles special_tiles;
109   LevelTiles levels;
110   SpriteChanges sprite_changes;
111   SpawnPoints spawn_points;
112   std::vector<Teleporter*> teleporters;
113
114   Statistics total_stats;
115
116   HSQOBJECT worldmap_table;
117   ScriptList scripts;
118
119   Color ambient_light;
120   std::string force_spawnpoint; /**< if set, spawnpoint will be forced to this value */
121
122   bool in_level;
123
124   /* variables to track panning to a spawn point */
125   Vector pan_pos;
126   bool panning;
127
128 public:
129   WorldMap(const std::string& filename, PlayerStatus* player_status, const std::string& force_spawnpoint = "");
130   ~WorldMap();
131
132   void add_object(GameObject* object);
133   
134   void try_expose(GameObject* object);
135   void try_unexpose(GameObject* object);
136
137   static WorldMap* current()
138   { return current_; }
139
140   virtual void setup();
141   virtual void leave();
142
143   /** Update worldmap state */
144   virtual void update(float delta);
145   /** Draw worldmap */
146   virtual void draw(DrawingContext& context);
147
148   Vector get_next_tile(Vector pos, Direction direction);
149
150   /**
151    * gets a bitfield of Tile::WORLDMAP_NORTH | Tile::WORLDMAP_WEST | ... values, 
152    * which indicates the directions Tux can move to when at the given position.
153    */
154   int available_directions_at(Vector pos);
155
156   /**
157    * returns a bitfield representing the union of all Tile::WORLDMAP_XXX values 
158    * of all solid tiles at the given position
159    */
160   int tile_data_at(Vector pos);
161
162   size_t level_count();
163   size_t solved_level_count();
164
165   /**
166    * gets called from the GameSession when a level has been successfully
167    * finished
168    */
169   void finished_level(Level* level);
170
171   /** returns current Tux incarnation */
172   Tux* get_tux() { return tux; }
173
174   /** returns player status */
175   PlayerStatus* get_player_status() { return player_status; }
176
177   LevelTile* at_level();
178   SpecialTile* at_special_tile();
179   SpriteChange* at_sprite_change(const Vector& pos);
180   Teleporter* at_teleporter(const Vector& pos);
181
182   /** Check if it is possible to walk from \a pos into \a direction,
183       if possible, write the new position to \a new_pos */
184   bool path_ok(Direction direction, const Vector& pos, Vector* new_pos);
185
186   /**
187    * Save worldmap state to squirrel state table
188    */
189   void save_state();
190
191   /**
192    * Load worldmap state from squirrel state table
193    */
194   void load_state();
195
196   const std::string& get_title() const
197   { return name; }
198
199   /**
200    * runs a script in the context of the worldmap (and keeps a reference to
201    * the script (so the script gets destroyed when the worldmap is destroyed)
202    */
203   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
204
205   /**
206    * switch to another worldmap.
207    * filename is relative to data root path
208    */
209   void change(const std::string& filename, const std::string& force_spawnpoint="");
210
211   /**
212    * moves Tux to the given spawnpoint
213    */
214   void move_to_spawnpoint(const std::string& spawnpoint, bool pan =false);
215
216   /**
217    * returns the width (in tiles) of a worldmap
218    */
219   float get_width() const;
220
221   /**
222    * returns the height (in tiles) of a worldmap
223    */
224   float get_height() const;
225
226 private:
227   void get_level_title(LevelTile& level);
228   void get_level_target_time(LevelTile& level);
229   void draw_status(DrawingContext& context);
230   void calculate_total_stats();
231
232   void load(const std::string& filename);
233   void on_escape_press();
234
235   Vector get_camera_pos_for_tux();
236   void clamp_camera_position(Vector& c);
237   Vector last_position;
238   float last_target_time;
239
240 private:
241   WorldMap(const WorldMap&);
242   WorldMap& operator=(const WorldMap&);
243 };
244
245 } // namespace worldmap
246
247 #endif
248
249 /* EOF */