87c83a9385fbb5914748c8b5a6a9746aac243946
[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   static Color level_title_color;
73   static Color message_color;
74   static Color teleporter_message_color;
75 private:
76   Tux* tux;
77
78   TileSet *tileset;
79   bool     free_tileset;
80
81   static WorldMap* current_;
82
83   std::auto_ptr<Menu> worldmap_menu;
84
85   Vector camera_offset;
86
87   std::string name;
88   std::string music;
89   std::string init_script;
90
91   typedef std::vector<GameObject*> GameObjects;
92   GameObjects game_objects;
93   std::list<TileMap*> solid_tilemaps;
94
95 public:
96   /** Variables to deal with the passive map messages */
97   Timer passive_message_timer;
98   std::string passive_message;
99
100 private:
101   std::string map_filename;
102   std::string levels_path;
103
104   typedef std::vector<SpecialTile*> SpecialTiles;
105   SpecialTiles special_tiles;
106   typedef std::vector<LevelTile*> LevelTiles;
107   LevelTiles levels;
108   typedef std::vector<SpriteChange*> SpriteChanges;
109   SpriteChanges sprite_changes;
110   typedef std::vector<SpawnPoint*> SpawnPoints;
111   SpawnPoints spawn_points;
112   std::vector<Teleporter*> teleporters;
113
114   Statistics total_stats;
115
116   HSQOBJECT worldmap_table;
117   typedef std::vector<HSQOBJECT> ScriptList;
118   ScriptList scripts;
119
120   Color ambient_light;
121   std::string force_spawnpoint; /**< if set, spawnpoint will be forced to this value */
122
123   bool in_level;
124
125   /* variables to track panning to a spawn point */
126   Vector pan_pos;
127   bool panning;
128
129 public:
130   WorldMap(const std::string& filename, const std::string& force_spawnpoint = "");
131   ~WorldMap();
132
133   void add_object(GameObject* object);
134   
135   void try_expose(GameObject* object);
136   void try_unexpose(GameObject* object);
137
138   static WorldMap* current()
139   { return current_; }
140
141   virtual void setup();
142   virtual void leave();
143
144   /** Update worldmap state */
145   virtual void update(float delta);
146   /** Draw worldmap */
147   virtual void draw(DrawingContext& context);
148
149   Vector get_next_tile(Vector pos, Direction direction);
150
151   /**
152    * gets a bitfield of Tile::WORLDMAP_NORTH | Tile::WORLDMAP_WEST | ... values, 
153    * which indicates the directions Tux can move to when at the given position.
154    */
155   int available_directions_at(Vector pos);
156
157   /**
158    * returns a bitfield representing the union of all Tile::WORLDMAP_XXX values 
159    * of all solid tiles at the given position
160    */
161   int tile_data_at(Vector pos);
162
163   size_t level_count();
164   size_t solved_level_count();
165
166   /**
167    * gets called from the GameSession when a level has been successfully
168    * finished
169    */
170   void finished_level(Level* level);
171
172   /** returns current Tux incarnation */
173   Tux* get_tux() { return tux; }
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 private:
225   void get_level_title(LevelTile& level);
226   void draw_status(DrawingContext& context);
227   void calculate_total_stats();
228
229   void load(const std::string& filename);
230   void on_escape_press();
231
232   Vector get_camera_pos_for_tux();
233   void clamp_camera_position(Vector& c);
234 };
235
236 } // namespace WorldMapNS
237
238 #endif