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