]> git.lizzy.rs Git - dragonfireclient.git/blob - src/environment.h
ac290f932cac31be4bbec9ec7be14e57a3ac3298
[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         // 0-23999
71         virtual void setTimeOfDay(u32 time)
72         {
73                 m_time_of_day = time;
74         }
75
76         u32 getTimeOfDay()
77         {
78                 return m_time_of_day;
79         }
80
81 protected:
82         // peer_ids in here should be unique, except that there may be many 0s
83         core::list<Player*> m_players;
84         // Brightness
85         //u32 m_daynight_ratio;
86         // Time of day in milli-hours (0-23999); determines day and night
87         u32 m_time_of_day;
88 };
89
90 /*
91         List of active blocks, used by ServerEnvironment
92 */
93
94 class ActiveBlockList
95 {
96 public:
97         void update(core::list<v3s16> &active_positions,
98                         s16 radius,
99                         core::map<v3s16, bool> &blocks_removed,
100                         core::map<v3s16, bool> &blocks_added);
101
102         bool contains(v3s16 p){
103                 return (m_list.find(p) != NULL);
104         }
105
106         void clear(){
107                 m_list.clear();
108         }
109
110         core::map<v3s16, bool> m_list;
111
112 private:
113 };
114
115 /*
116         Active block modifier interface
117 */
118
119 class ServerEnvironment;
120
121 class ActiveBlockModifier
122 {
123 public:
124         ActiveBlockModifier(){};
125         virtual ~ActiveBlockModifier(){};
126         
127         virtual u32 getTriggerContentCount(){ return 1;}
128         virtual u8 getTriggerContent(u32 i) = 0;
129         virtual float getActiveInterval() = 0;
130         virtual u32 getActiveChance() = 0;
131         virtual void triggerEvent(ServerEnvironment *env, v3s16 p) = 0;
132 };
133
134 /*
135         The server-side environment.
136
137         This is not thread-safe. Server uses an environment mutex.
138 */
139
140 #include "serverobject.h"
141
142 class Server;
143
144 class ServerEnvironment : public Environment
145 {
146 public:
147         ServerEnvironment(ServerMap *map, Server *server);
148         ~ServerEnvironment();
149
150         Map & getMap()
151         {
152                 return *m_map;
153         }
154
155         ServerMap & getServerMap()
156         {
157                 return *m_map;
158         }
159
160         Server * getServer()
161         {
162                 return m_server;
163         }
164
165         void step(f32 dtime);
166         
167         /*
168                 Save players
169         */
170         void serializePlayers(const std::string &savedir);
171         void deSerializePlayers(const std::string &savedir);
172
173         /*
174                 Save and load time of day and game timer
175         */
176         void saveMeta(const std::string &savedir);
177         void loadMeta(const std::string &savedir);
178
179         /*
180                 ActiveObjects
181         */
182
183         ServerActiveObject* getActiveObject(u16 id);
184
185         /*
186                 Add an active object to the environment.
187                 Environment handles deletion of object.
188                 Object may be deleted by environment immediately.
189                 If id of object is 0, assigns a free id to it.
190                 Returns the id of the object.
191                 Returns 0 if not added and thus deleted.
192         */
193         u16 addActiveObject(ServerActiveObject *object);
194         
195         /*
196                 Find out what new objects have been added to
197                 inside a radius around a position
198         */
199         void getAddedActiveObjects(v3s16 pos, s16 radius,
200                         core::map<u16, bool> &current_objects,
201                         core::map<u16, bool> &added_objects);
202
203         /*
204                 Find out what new objects have been removed from
205                 inside a radius around a position
206         */
207         void getRemovedActiveObjects(v3s16 pos, s16 radius,
208                         core::map<u16, bool> &current_objects,
209                         core::map<u16, bool> &removed_objects);
210         
211         /*
212                 Get the next message emitted by some active object.
213                 Returns a message with id=0 if no messages are available.
214         */
215         ActiveObjectMessage getActiveObjectMessage();
216
217 private:
218         /*
219                 Remove all objects that satisfy (m_removed && m_known_by_count==0)
220         */
221         void removeRemovedObjects();
222         
223         /*
224                 Convert stored objects from block to active
225         */
226         void activateObjects(MapBlock *block);
227         
228         /*
229                 Convert objects that are not in active blocks to static.
230
231                 If m_known_by_count != 0, active object is not deleted, but static
232                 data is still updated.
233
234                 If force_delete is set, active object is deleted nevertheless. It
235                 shall only be set so in the destructor of the environment.
236         */
237         void deactivateFarObjects(bool force_delete);
238
239         /*
240                 Member variables
241         */
242         
243         // The map
244         ServerMap *m_map;
245         // Pointer to server (which is handling this environment)
246         Server *m_server;
247         // Active object list
248         core::map<u16, ServerActiveObject*> m_active_objects;
249         // Outgoing network message buffer for active objects
250         Queue<ActiveObjectMessage> m_active_object_messages;
251         // Some timers
252         float m_random_spawn_timer; // used for experimental code
253         float m_send_recommended_timer;
254         IntervalLimiter m_object_management_interval;
255         // List of active blocks
256         ActiveBlockList m_active_blocks;
257         IntervalLimiter m_active_blocks_management_interval;
258         IntervalLimiter m_active_blocks_test_interval;
259         // Time from the beginning of the game in seconds.
260         // Incremented in step().
261         u32 m_game_time;
262         // A helper variable for incrementing the latter
263         float m_game_time_fraction_counter;
264 };
265
266 #ifndef SERVER
267
268 #include "clientobject.h"
269
270 /*
271         The client-side environment.
272
273         This is not thread-safe.
274         Must be called from main (irrlicht) thread (uses the SceneManager)
275         Client uses an environment mutex.
276 */
277
278 enum ClientEnvEventType
279 {
280         CEE_NONE,
281         CEE_PLAYER_DAMAGE
282 };
283
284 struct ClientEnvEvent
285 {
286         ClientEnvEventType type;
287         union {
288                 struct{
289                 } none;
290                 struct{
291                         u8 amount;
292                 } player_damage;
293         };
294 };
295
296 class ClientEnvironment : public Environment
297 {
298 public:
299         ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr);
300         ~ClientEnvironment();
301
302         Map & getMap()
303         {
304                 return *m_map;
305         }
306
307         ClientMap & getClientMap()
308         {
309                 return *m_map;
310         }
311
312         void step(f32 dtime);
313
314         virtual void addPlayer(Player *player);
315         LocalPlayer * getLocalPlayer();
316
317         void updateMeshes(v3s16 blockpos);
318         void expireMeshes(bool only_daynight_diffed);
319
320         void setTimeOfDay(u32 time)
321         {
322                 u32 old_dr = getDayNightRatio();
323
324                 Environment::setTimeOfDay(time);
325
326                 if(getDayNightRatio() != old_dr)
327                 {
328                         dout_client<<DTIME<<"ClientEnvironment: DayNightRatio changed"
329                                         <<" -> expiring meshes"<<std::endl;
330                         expireMeshes(true);
331                 }
332         }
333
334         /*
335                 ActiveObjects
336         */
337         
338         ClientActiveObject* getActiveObject(u16 id);
339
340         /*
341                 Adds an active object to the environment.
342                 Environment handles deletion of object.
343                 Object may be deleted by environment immediately.
344                 If id of object is 0, assigns a free id to it.
345                 Returns the id of the object.
346                 Returns 0 if not added and thus deleted.
347         */
348         u16 addActiveObject(ClientActiveObject *object);
349
350         void addActiveObject(u16 id, u8 type, const std::string &init_data);
351         void removeActiveObject(u16 id);
352
353         void processActiveObjectMessage(u16 id, const std::string &data);
354
355         /*
356                 Callbacks for activeobjects
357         */
358
359         void damageLocalPlayer(u8 damage);
360
361         /*
362                 Client likes to call these
363         */
364         
365         // Get all nearby objects
366         void getActiveObjects(v3f origin, f32 max_d,
367                         core::array<DistanceSortedActiveObject> &dest);
368         
369         // Get event from queue. CEE_NONE is returned if queue is empty.
370         ClientEnvEvent getClientEvent();
371         
372 private:
373         ClientMap *m_map;
374         scene::ISceneManager *m_smgr;
375         core::map<u16, ClientActiveObject*> m_active_objects;
376         Queue<ClientEnvEvent> m_client_event_queue;
377 };
378
379 #endif
380
381 #endif
382