Magic Blocks only turn solid when nothing's blocking them. /
[supertux.git] / src / sector.hpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef SUPERTUX_SECTOR_H
20 #define SUPERTUX_SECTOR_H
21
22 #include <string>
23 #include <vector>
24 #include <list>
25 #include <memory>
26 #include <squirrel.h>
27
28 #include "direction.hpp"
29 #include "math/vector.hpp"
30 #include "video/drawing_context.hpp"
31
32 namespace lisp {
33 class Lisp;
34 class Writer;
35 }
36 namespace collision {
37 class Constraints;
38 }
39
40 class Rect;
41 class Sprite;
42 class GameObject;
43 class Player;
44 class Camera;
45 class TileMap;
46 class Bullet;
47 class ScriptInterpreter;
48 class SpawnPoint;
49 class MovingObject;
50 class CollisionHit;
51 class Level;
52 class Portable;
53
54 enum MusicType {
55   LEVEL_MUSIC,
56   HERRING_MUSIC,
57   HERRING_WARNING_MUSIC
58 };
59
60 /**
61  * This class holds a sector (a part of a level) and all the game objects in
62  * the sector
63  */
64 class Sector
65 {
66 public:
67   Sector(Level* parent);
68   ~Sector();
69
70   /// get parent level
71   Level* get_level();
72
73   /// read sector from lisp file
74   void parse(const lisp::Lisp& lisp);
75   void parse_old_format(const lisp::Lisp& lisp);
76   /// write sector to lisp file
77   void write(lisp::Writer& writer);
78
79   /// activates this sector (change music, intialize player class, ...)
80   void activate(const std::string& spawnpoint);
81   void activate(const Vector& player_pos);
82   void deactivate();
83
84   void update(float elapsed_time);
85   void update_game_objects();
86
87   void draw(DrawingContext& context);
88
89   /**
90    * runs a script in the context of the sector (sector_table will be the
91    * roottable of this squirrel VM)
92    */
93   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
94
95   /// adds a gameobject
96   void add_object(GameObject* object);
97
98   void set_name(const std::string& name)
99   { this->name = name; }
100   const std::string& get_name() const
101   { return name; }
102
103   /**
104    * tests if a given rectangle is inside the sector
105    * (a rectangle that is on top of the sector is considered inside)
106    */
107   bool inside(const Rect& rectangle) const;
108
109   void play_music(MusicType musictype);
110   MusicType get_music_type();
111
112   bool add_bullet(const Vector& pos, float xm, Direction dir);
113   bool add_smoke_cloud(const Vector& pos);
114
115   /** get currently activated sector. */
116   static Sector* current()
117   { return _current; }
118
119   /** Get total number of badguys */
120   int get_total_badguys();
121
122   /** Get total number of GameObjects of given type */
123   template<class T> int get_total_count()
124   {
125     int total = 0;
126     for(GameObjects::iterator i = gameobjects.begin(); i != gameobjects.end(); ++i) {
127       if (dynamic_cast<T*>(*i)) total++;
128     }
129     return total;
130   }
131
132   void collision_tilemap(collision::Constraints* constraints,
133       const Vector& movement, const Rect& dest) const;
134
135   /** 
136    * Checks if the specified rectangle is free of (solid) tiles.
137    * Note that this does not include static objects, e.g. bonus blocks.
138    */
139   bool is_free_of_tiles(const Rect& rect) const;
140   /** 
141    * Checks if the specified rectangle is free of both 
142    * 1.) solid tiles and
143    * 2.) MovingObjects in COLGROUP_STATIC.
144    * Note that this does not include badguys or players.
145    */
146   bool is_free_of_statics(const Rect& rect, const MovingObject* ignore_object = 0) const;
147   /** 
148    * Checks if the specified rectangle is free of both 
149    * 1.) solid tiles and
150    * 2.) MovingObjects in COLGROUP_STATIC, COLGROUP_MOVINGSTATIC or COLGROUP_MOVING.
151    * This includes badguys and players.
152    */
153   bool is_free_of_movingstatics(const Rect& rect, const MovingObject* ignore_object = 0) const;
154
155   /**
156    * returns a list of players currently in the sector
157    */
158   std::vector<Player*> get_players() {
159     return std::vector<Player*>(1, this->player);
160   }
161
162   Rect get_active_region();
163
164   /**
165    * returns the width (in px) of a sector)
166    */
167   float get_width() const;
168
169   /**
170    * returns the height (in px) of a sector)
171    */
172   float get_height() const;
173
174   /**
175    * globally changes solid tilemaps' tile ids
176    */
177   void change_solid_tiles(uint32_t old_tile_id, uint32_t new_tile_id);
178
179   typedef std::vector<GameObject*> GameObjects;
180   typedef std::vector<MovingObject*> MovingObjects;
181   typedef std::vector<SpawnPoint*> SpawnPoints;
182   typedef std::vector<Portable*> Portables;
183
184 private:
185   Level* level; /**< Parent level containing this sector */
186   uint32_t collision_tile_attributes(const Rect& dest) const;
187
188   void before_object_remove(GameObject* object);
189   bool before_object_add(GameObject* object);
190
191   void try_expose(GameObject* object);
192   void try_unexpose(GameObject* object);
193
194   /** Checks for all possible collisions. And calls the
195       collision_handlers, which the collision_objects provide for this
196       case (or not). */
197   void handle_collisions();
198
199   /**
200    * Does collision detection between 2 objects and does instant
201    * collision response handling in case of a collision
202    */
203   void collision_object(MovingObject* object1, MovingObject* object2) const;
204
205   /**
206    * Does collision detection of an object against all other static
207    * objects (and the tilemap) in the level. Collision response is done
208    * for the first hit in time. (other hits get ignored, the function
209    * should be called repeatedly to resolve those)
210    *
211    * returns true if the collision detection should be aborted for this object
212    * (because of ABORT_MOVE in the collision response or no collisions)
213    */
214   void collision_static(collision::Constraints* constraints,
215       const Vector& movement, const Rect& dest, GameObject& object);
216
217   void collision_static_constrains(MovingObject& object);
218
219   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
220
221   void fix_old_tiles();
222
223   static Sector* _current;
224
225   std::string name;
226
227   std::vector<Bullet*> bullets;
228
229   std::string init_script;
230
231   /// container for newly created objects, they'll be added in Sector::update
232   GameObjects gameobjects_new;
233
234   MusicType currentmusic;
235
236   HSQOBJECT sector_table;
237   /// sector scripts
238   typedef std::vector<HSQOBJECT> ScriptList;
239   ScriptList scripts;
240
241   Color ambient_light;
242
243 public: // TODO make this private again
244   /// show collision rectangles of moving objects (for debugging)
245   static bool show_collrects;
246   static bool draw_solids_only;
247
248   GameObjects gameobjects;
249   MovingObjects moving_objects;
250   SpawnPoints spawnpoints;
251   Portables portables;
252
253   std::string music;
254   float gravity;
255
256   // some special objects, where we need direct access
257   // (try to avoid accessing them directly)
258   Player* player;
259   std::list<TileMap*> solid_tilemaps;
260   Camera* camera;
261 };
262
263 #endif