]> git.lizzy.rs Git - dragonfireclient.git/blob - src/environment.h
Get rid of all the string format warnings caused by the DSTACK macro
[dragonfireclient.git] / src / environment.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef ENVIRONMENT_HEADER
21 #define ENVIRONMENT_HEADER
22
23 /*
24         This class is the game's environment.
25         It contains:
26         - The map
27         - Players
28         - Other objects
29         - The current time in the game (actually it only contains the brightness)
30         - etc.
31 */
32
33 #include <list>
34 #include "common_irrlicht.h"
35 #include "player.h"
36 #include "map.h"
37 #include <ostream>
38 #include "utility.h"
39
40 class Environment
41 {
42 public:
43         // Environment will delete the map passed to the constructor
44         Environment();
45         virtual ~Environment();
46
47         /*
48                 Step everything in environment.
49                 - Move players
50                 - Step mobs
51                 - Run timers of map
52         */
53         virtual void step(f32 dtime) = 0;
54
55         virtual Map & getMap() = 0;
56
57         virtual void addPlayer(Player *player);
58         void removePlayer(u16 peer_id);
59         Player * getPlayer(u16 peer_id);
60         Player * getPlayer(const char *name);
61         Player * getRandomConnectedPlayer();
62         Player * getNearestConnectedPlayer(v3f pos);
63         core::list<Player*> getPlayers();
64         core::list<Player*> getPlayers(bool ignore_disconnected);
65         void printPlayers(std::ostream &o);
66         
67         void setDayNightRatio(u32 r);
68         u32 getDayNightRatio();
69
70 protected:
71         // peer_ids in here should be unique, except that there may be many 0s
72         core::list<Player*> m_players;
73         // Brightness
74         u32 m_daynight_ratio;
75 };
76
77 /*
78         The server-side environment.
79
80         This is not thread-safe. Server uses an environment mutex.
81 */
82
83 #include "serverobject.h"
84
85 class Server;
86
87 class ServerEnvironment : public Environment
88 {
89 public:
90         ServerEnvironment(ServerMap *map, Server *server);
91         ~ServerEnvironment();
92
93         Map & getMap()
94         {
95                 return *m_map;
96         }
97
98         ServerMap & getServerMap()
99         {
100                 return *m_map;
101         }
102
103         Server * getServer()
104         {
105                 return m_server;
106         }
107
108         void step(f32 dtime);
109
110         void serializePlayers(const std::string &savedir);
111         void deSerializePlayers(const std::string &savedir);
112
113         /*
114                 ActiveObjects
115         */
116
117         ServerActiveObject* getActiveObject(u16 id);
118
119         /*
120                 Adds an active object to the environment.
121                 Environment handles deletion of object.
122                 Object may be deleted by environment immediately.
123                 If id of object is 0, assigns a free id to it.
124                 Returns the id of the object.
125                 Returns 0 if not added and thus deleted.
126         */
127         u16 addActiveObject(ServerActiveObject *object);
128         
129         /*
130                 Finds out what new objects have been added to
131                 inside a radius around a position
132         */
133         void getAddedActiveObjects(v3s16 pos, s16 radius,
134                         core::map<u16, bool> &current_objects,
135                         core::map<u16, bool> &added_objects);
136
137         /*
138                 Finds out what new objects have been removed from
139                 inside a radius around a position
140         */
141         void getRemovedActiveObjects(v3s16 pos, s16 radius,
142                         core::map<u16, bool> &current_objects,
143                         core::map<u16, bool> &removed_objects);
144         
145         /*
146                 Gets the next message emitted by some active object.
147                 Returns a message with id=0 if no messages are available.
148         */
149         ActiveObjectMessage getActiveObjectMessage();
150         
151 private:
152         ServerMap *m_map;
153         Server *m_server;
154         core::map<u16, ServerActiveObject*> m_active_objects;
155         Queue<ActiveObjectMessage> m_active_object_messages;
156         float m_random_spawn_timer;
157         float m_send_recommended_timer;
158         IntervalLimiter m_object_management_interval;
159 };
160
161 #ifndef SERVER
162
163 #include "clientobject.h"
164
165 /*
166         The client-side environment.
167
168         This is not thread-safe.
169         Must be called from main (irrlicht) thread (uses the SceneManager)
170         Client uses an environment mutex.
171 */
172
173 enum ClientEnvEventType
174 {
175         CEE_NONE,
176         CEE_PLAYER_DAMAGE
177 };
178
179 struct ClientEnvEvent
180 {
181         ClientEnvEventType type;
182         union {
183                 struct{
184                 } none;
185                 struct{
186                         u8 amount;
187                 } player_damage;
188         };
189 };
190
191 class ClientEnvironment : public Environment
192 {
193 public:
194         ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
195         ~ClientEnvironment();
196
197         Map & getMap()
198         {
199                 return *m_map;
200         }
201
202         ClientMap & getClientMap()
203         {
204                 return *m_map;
205         }
206
207         void step(f32 dtime);
208
209         virtual void addPlayer(Player *player);
210         LocalPlayer * getLocalPlayer();
211
212         void updateMeshes(v3s16 blockpos);
213         void expireMeshes(bool only_daynight_diffed);
214
215         /*
216                 ActiveObjects
217         */
218         
219         ClientActiveObject* getActiveObject(u16 id);
220
221         /*
222                 Adds an active object to the environment.
223                 Environment handles deletion of object.
224                 Object may be deleted by environment immediately.
225                 If id of object is 0, assigns a free id to it.
226                 Returns the id of the object.
227                 Returns 0 if not added and thus deleted.
228         */
229         u16 addActiveObject(ClientActiveObject *object);
230
231         void addActiveObject(u16 id, u8 type, const std::string &init_data);
232         void removeActiveObject(u16 id);
233
234         void processActiveObjectMessage(u16 id, const std::string &data);
235
236         /*
237                 Callbacks for activeobjects
238         */
239
240         void damageLocalPlayer(u8 damage);
241
242         /*
243                 Client likes to call these
244         */
245         
246         // Get all nearby objects
247         void getActiveObjects(v3f origin, f32 max_d,
248                         core::array<DistanceSortedActiveObject> &dest);
249         
250         // Get event from queue. CEE_NONE is returned if queue is empty.
251         ClientEnvEvent getClientEvent();
252         
253 private:
254         ClientMap *m_map;
255         scene::ISceneManager *m_smgr;
256         core::map<u16, ClientActiveObject*> m_active_objects;
257         Queue<ClientEnvEvent> m_client_event_queue;
258 };
259
260 #endif
261
262 #endif
263