* "Outsourced" cheats from GameSession to scripting system.
[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 <memory>
25 #include <squirrel.h>
26
27 #include "direction.hpp"
28 #include "script_manager.hpp"
29 #include "math/vector.hpp"
30 #include "video/drawing_context.hpp"
31
32 namespace lisp {
33 class Lisp;
34 class Writer;
35 }
36
37 class Rect;
38 class Sprite;
39 class GameObject;
40 class Player;
41 class Camera;
42 class TileMap;
43 class Bullet;
44 class CollisionGrid;
45 class ScriptInterpreter;
46 class SpawnPoint;
47 class MovingObject;
48 class CollisionHit;
49
50 enum MusicType {
51   LEVEL_MUSIC,
52   HERRING_MUSIC,
53   HERRING_WARNING_MUSIC
54 };
55
56 /**
57  * This class holds a sector (a part of a level) and all the game objects in
58  * the sector
59  */
60 class Sector
61 {
62 public:
63   Sector();
64   ~Sector();
65
66   /// read sector from lisp file
67   void parse(const lisp::Lisp& lisp);
68   void parse_old_format(const lisp::Lisp& lisp);
69   /// write sector to lisp file
70   void write(lisp::Writer& writer);
71
72   /// activates this sector (change music, intialize player class, ...)
73   void activate(const std::string& spawnpoint);
74   void activate(const Vector& player_pos);
75   void deactivate();
76
77   void update(float elapsed_time);
78   void update_game_objects();
79
80   void draw(DrawingContext& context);
81
82   /**
83    * runs a script in the context of the sector (sector_table will be the
84    * roottable of this squirrel VM)
85    */
86   HSQUIRRELVM run_script(std::istream& in, const std::string& sourcename);
87
88   /// adds a gameobject
89   void add_object(GameObject* object);
90
91   void set_name(const std::string& name)
92   { this->name = name; }
93   const std::string& get_name() const
94   { return name; }
95
96   /// tests if a given rectangle is inside the sector
97   bool inside(const Rect& rectangle) const;
98
99   void play_music(MusicType musictype);
100   MusicType get_music_type();
101   
102   bool add_bullet(const Vector& pos, float xm, Direction dir);
103   bool add_smoke_cloud(const Vector& pos);
104   void add_floating_text(const Vector& pos, const std::string& text);
105                                                                                 
106   /** get currently activated sector. */
107   static Sector* current()
108   { return _current; }
109
110   /** Get total number of badguys */
111   int get_total_badguys();
112
113   void collision_tilemap(const Rect& dest, const Vector& movement, CollisionHit& hit) const;
114
115   /** Checks if at the specified rectangle are gameobjects with STATIC flag set
116    * (or solid tiles from the tilemap)
117    */
118   bool is_free_space(const Rect& rect) const;
119
120   /**
121    * returns a list of players currently in the sector
122    */
123   std::vector<Player*> get_players() {
124     return std::vector<Player*>(1, this->player);
125   }
126
127   Rect get_active_region();
128
129   typedef std::vector<GameObject*> GameObjects;
130   typedef std::vector<MovingObject*> MovingObjects;
131   typedef std::vector<SpawnPoint*> SpawnPoints;
132
133 private:
134   uint32_t collision_tile_attributes(const Rect& dest) const;
135
136   void before_object_remove(GameObject* object);
137   bool before_object_add(GameObject* object);
138
139   void try_expose(GameObject* object);
140   void try_unexpose(GameObject* object);
141   
142   bool collision_static(MovingObject* object, const Vector& movement);
143   
144   /** Checks for all possible collisions. And calls the
145       collision_handlers, which the collision_objects provide for this
146       case (or not). */
147   void handle_collisions();
148   
149   void collision_object(MovingObject* object1, MovingObject* object2) const;
150   GameObject* parse_object(const std::string& name, const lisp::Lisp& lisp);
151
152   void fix_old_tiles();
153
154   static Sector* _current;
155   
156   std::string name;
157
158   std::vector<Bullet*> bullets;
159
160   std::string init_script;
161
162   /// container for newly created objects, they'll be added in Sector::update
163   GameObjects gameobjects_new;
164  
165   MusicType currentmusic;
166
167   std::auto_ptr<CollisionGrid> grid;
168
169   HSQOBJECT sector_table;
170   /// sector scripts
171   std::auto_ptr<ScriptManager> script_manager;
172
173 public: // TODO make this private again
174   /// show collision rectangles of moving objects (for debugging)
175   static bool show_collrects;
176   static bool draw_solids_only;
177   
178   GameObjects gameobjects;
179   MovingObjects moving_objects;
180   SpawnPoints spawnpoints;                       
181
182   std::string music;
183   float gravity;
184
185   // some special objects, where we need direct access
186   // (try to avoid accessing them directly)
187   Player* player;
188   TileMap* solids;
189   Camera* camera;
190 };
191
192 #endif
193