fix cr/lfs and remove trailing whitespaces...
[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
40 class Sprite;
41 class Menu;
42 class SpawnPoint;
43 class GameObject;
44 class TileMap;
45
46 namespace WorldMapNS {
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 enum Direction { D_NONE, D_WEST, D_EAST, D_NORTH, D_SOUTH };
63
64 std::string direction_to_string(Direction d);
65 Direction   string_to_direction(const std::string& d);
66 Direction reverse_dir(Direction d);
67
68 /**
69  * Screen that displays a worldmap
70  */
71 class WorldMap : public Screen
72 {
73 private:
74   Tux* tux;
75
76   static WorldMap* current_;
77
78   std::auto_ptr<Menu> worldmap_menu;
79
80   Vector camera_offset;
81
82   std::string name;
83   std::string music;
84   std::string init_script;
85
86   typedef std::vector<GameObject*> GameObjects;
87   GameObjects game_objects;
88   TileMap* solids;
89
90   std::auto_ptr<TileManager> tile_manager;
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   std::string force_spawnpoint; /**< if set, spawnpoint will be forced to this value */
118
119 public:
120   WorldMap(const std::string& filename, const std::string& force_spawnpoint = "");
121   ~WorldMap();
122
123   void add_object(GameObject* object);
124
125   static WorldMap* current()
126   { return current_; }
127
128   virtual void setup();
129   virtual void leave();
130
131   /** Update worldmap state */
132   virtual void update(float delta);
133   /** Draw worldmap */
134   virtual void draw(DrawingContext& context);
135
136   Vector get_next_tile(Vector pos, Direction direction);
137   const Tile* at(Vector pos);
138
139   size_t level_count();
140   size_t solved_level_count();
141
142   /**
143    * gets called from the GameSession when a level has been successfully
144    * finished
145    */
146   void finished_level(Level* level);
147
148   LevelTile* at_level();
149   SpecialTile* at_special_tile();
150   SpriteChange* at_sprite_change(const Vector& pos);
151   Teleporter* at_teleporter(const Vector& pos);
152
153   /** Check if it is possible to walk from \a pos into \a direction,
154       if possible, write the new position to \a new_pos */
155   bool path_ok(Direction direction, const Vector& pos, Vector* new_pos);
156
157   /**
158    * Save worldmap state to squirrel state table
159    */
160   void save_state();
161
162   /**
163    * Load worldmap state from squirrel state table
164    */
165   void load_state();
166
167   const std::string& get_title() const
168   { return name; }
169
170   /**
171    * runs a script in the context of the worldmap (and keeps a reference to
172    * the script (so the script gets destroyed when the worldmap is destroyed)
173    */
174   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
175
176   /**
177    * switch to another worldmap.
178    * filename is relative to data root path
179    */
180   void change(const std::string& filename, const std::string& force_spawnpoint="");
181
182   /**
183    * moves Tux to the given spawnpoint
184    */
185   void move_to_spawnpoint(const std::string& spawnpoint);
186
187 private:
188   void get_level_title(LevelTile& level);
189   void draw_status(DrawingContext& context);
190   void calculate_total_stats();
191
192   void load(const std::string& filename);
193   void on_escape_press();
194
195 };
196
197 } // namespace WorldMapNS
198
199 #endif