]> git.lizzy.rs Git - dragonfireclient.git/blob - src/environment.h
Handle ActiveBlockModifier intervals properly, down to 1s
[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
30         - etc.
31 */
32
33 #include <set>
34 #include "common_irrlicht.h"
35 #include "player.h"
36 #include "map.h"
37 #include <ostream>
38 #include "utility.h"
39 #include "activeobject.h"
40
41 class Server;
42 class ActiveBlockModifier;
43 class ServerActiveObject;
44 typedef struct lua_State lua_State;
45 class ITextureSource;
46 class IGameDef;
47
48 class Environment
49 {
50 public:
51         // Environment will delete the map passed to the constructor
52         Environment();
53         virtual ~Environment();
54
55         /*
56                 Step everything in environment.
57                 - Move players
58                 - Step mobs
59                 - Run timers of map
60         */
61         virtual void step(f32 dtime) = 0;
62
63         virtual Map & getMap() = 0;
64
65         virtual void addPlayer(Player *player);
66         void removePlayer(u16 peer_id);
67         Player * getPlayer(u16 peer_id);
68         Player * getPlayer(const char *name);
69         Player * getRandomConnectedPlayer();
70         Player * getNearestConnectedPlayer(v3f pos);
71         core::list<Player*> getPlayers();
72         core::list<Player*> getPlayers(bool ignore_disconnected);
73         void printPlayers(std::ostream &o);
74         
75         //void setDayNightRatio(u32 r);
76         u32 getDayNightRatio();
77         
78         // 0-23999
79         virtual void setTimeOfDay(u32 time)
80         {
81                 m_time_of_day = time;
82         }
83
84         u32 getTimeOfDay()
85         {
86                 return m_time_of_day;
87         }
88
89 protected:
90         // peer_ids in here should be unique, except that there may be many 0s
91         core::list<Player*> m_players;
92         // Brightness
93         //u32 m_daynight_ratio;
94         // Time of day in milli-hours (0-23999); determines day and night
95         u32 m_time_of_day;
96 };
97
98 /*
99         Active block modifier interface.
100
101         These are fed into ServerEnvironment at initialization time;
102         ServerEnvironment handles deleting them.
103 */
104
105 class ActiveBlockModifier
106 {
107 public:
108         ActiveBlockModifier(){};
109         virtual ~ActiveBlockModifier(){};
110         
111         virtual std::set<std::string> getTriggerContents()=0;
112         virtual float getTriggerInterval() = 0;
113         // chance of (1 / return value), 0 is disallowed
114         virtual u32 getTriggerChance() = 0;
115         // This is called usually at interval for 1/chance of the nodes
116         virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
117         virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
118                         u32 active_object_count, u32 active_object_count_wider){};
119 };
120
121 struct ABMWithState
122 {
123         ActiveBlockModifier *abm;
124         float timer;
125
126         ABMWithState(ActiveBlockModifier *abm_):
127                 abm(abm_)
128         {}
129 };
130
131 /*
132         List of active blocks, used by ServerEnvironment
133 */
134
135 class ActiveBlockList
136 {
137 public:
138         void update(core::list<v3s16> &active_positions,
139                         s16 radius,
140                         core::map<v3s16, bool> &blocks_removed,
141                         core::map<v3s16, bool> &blocks_added);
142
143         bool contains(v3s16 p){
144                 return (m_list.find(p) != NULL);
145         }
146
147         void clear(){
148                 m_list.clear();
149         }
150
151         core::map<v3s16, bool> m_list;
152
153 private:
154 };
155
156 class IBackgroundBlockEmerger
157 {
158 public:
159         virtual void queueBlockEmerge(v3s16 blockpos, bool allow_generate)=0;
160 };
161
162 /*
163         The server-side environment.
164
165         This is not thread-safe. Server uses an environment mutex.
166 */
167
168 class ServerEnvironment : public Environment
169 {
170 public:
171         ServerEnvironment(ServerMap *map, lua_State *L, IGameDef *gamedef,
172                         IBackgroundBlockEmerger *emerger);
173         ~ServerEnvironment();
174
175         Map & getMap()
176                 { return *m_map; }
177
178         ServerMap & getServerMap()
179                 { return *m_map; }
180
181         lua_State* getLua()
182                 { return m_lua; }
183
184         IGameDef *getGameDef()
185                 { return m_gamedef; }
186
187         float getSendRecommendedInterval()
188         {
189                 return 0.10;
190         }
191
192         /*
193                 Save players
194         */
195         void serializePlayers(const std::string &savedir);
196         void deSerializePlayers(const std::string &savedir);
197
198         /*
199                 Save and load time of day and game timer
200         */
201         void saveMeta(const std::string &savedir);
202         void loadMeta(const std::string &savedir);
203
204         /*
205                 External ActiveObject interface
206                 -------------------------------------------
207         */
208
209         ServerActiveObject* getActiveObject(u16 id);
210
211         /*
212                 Add an active object to the environment.
213                 Environment handles deletion of object.
214                 Object may be deleted by environment immediately.
215                 If id of object is 0, assigns a free id to it.
216                 Returns the id of the object.
217                 Returns 0 if not added and thus deleted.
218         */
219         u16 addActiveObject(ServerActiveObject *object);
220         
221         /*
222                 Add an active object as a static object to the corresponding
223                 MapBlock.
224                 Caller allocates memory, ServerEnvironment frees memory.
225                 Return value: true if succeeded, false if failed.
226         */
227         bool addActiveObjectAsStatic(ServerActiveObject *object);
228         
229         /*
230                 Find out what new objects have been added to
231                 inside a radius around a position
232         */
233         void getAddedActiveObjects(v3s16 pos, s16 radius,
234                         core::map<u16, bool> &current_objects,
235                         core::map<u16, bool> &added_objects);
236
237         /*
238                 Find out what new objects have been removed from
239                 inside a radius around a position
240         */
241         void getRemovedActiveObjects(v3s16 pos, s16 radius,
242                         core::map<u16, bool> &current_objects,
243                         core::map<u16, bool> &removed_objects);
244         
245         /*
246                 Get the next message emitted by some active object.
247                 Returns a message with id=0 if no messages are available.
248         */
249         ActiveObjectMessage getActiveObjectMessage();
250
251         /*
252                 Activate objects and dynamically modify for the dtime determined
253                 from timestamp and additional_dtime
254         */
255         void activateBlock(MapBlock *block, u32 additional_dtime=0);
256
257         /*
258                 ActiveBlockModifiers (TODO)
259                 -------------------------------------------
260                 NOTE: Not used currently (TODO: Use or remove)
261         */
262
263         void addActiveBlockModifier(ActiveBlockModifier *abm);
264
265         /* Other stuff */
266         
267         // Clear all objects, loading and going through every MapBlock
268         void clearAllObjects();
269         
270         void step(f32 dtime);
271         
272 private:
273
274         /*
275                 Internal ActiveObject interface
276                 -------------------------------------------
277         */
278
279         /*
280                 Add an active object to the environment.
281
282                 Called by addActiveObject.
283
284                 Object may be deleted by environment immediately.
285                 If id of object is 0, assigns a free id to it.
286                 Returns the id of the object.
287                 Returns 0 if not added and thus deleted.
288         */
289         u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed);
290         
291         /*
292                 Remove all objects that satisfy (m_removed && m_known_by_count==0)
293         */
294         void removeRemovedObjects();
295         
296         /*
297                 Convert stored objects from block to active
298         */
299         void activateObjects(MapBlock *block);
300         
301         /*
302                 Convert objects that are not in active blocks to static.
303
304                 If m_known_by_count != 0, active object is not deleted, but static
305                 data is still updated.
306
307                 If force_delete is set, active object is deleted nevertheless. It
308                 shall only be set so in the destructor of the environment.
309         */
310         void deactivateFarObjects(bool force_delete);
311
312         /*
313                 Member variables
314         */
315         
316         // The map
317         ServerMap *m_map;
318         // Lua state
319         lua_State *m_lua;
320         // Game definition
321         IGameDef *m_gamedef;
322         // Background block emerger (the server, in practice)
323         IBackgroundBlockEmerger *m_emerger;
324         // Active object list
325         core::map<u16, ServerActiveObject*> m_active_objects;
326         // Outgoing network message buffer for active objects
327         Queue<ActiveObjectMessage> m_active_object_messages;
328         // Some timers
329         float m_random_spawn_timer; // used for experimental code
330         float m_send_recommended_timer;
331         IntervalLimiter m_object_management_interval;
332         // List of active blocks
333         ActiveBlockList m_active_blocks;
334         IntervalLimiter m_active_blocks_management_interval;
335         IntervalLimiter m_active_block_modifier_interval;
336         IntervalLimiter m_active_blocks_nodemetadata_interval;
337         // Time from the beginning of the game in seconds.
338         // Incremented in step().
339         u32 m_game_time;
340         // A helper variable for incrementing the latter
341         float m_game_time_fraction_counter;
342         core::list<ABMWithState> m_abms;
343 };
344
345 #ifndef SERVER
346
347 #include "clientobject.h"
348
349 /*
350         The client-side environment.
351
352         This is not thread-safe.
353         Must be called from main (irrlicht) thread (uses the SceneManager)
354         Client uses an environment mutex.
355 */
356
357 enum ClientEnvEventType
358 {
359         CEE_NONE,
360         CEE_PLAYER_DAMAGE
361 };
362
363 struct ClientEnvEvent
364 {
365         ClientEnvEventType type;
366         union {
367                 struct{
368                 } none;
369                 struct{
370                         u8 amount;
371                 } player_damage;
372         };
373 };
374
375 class ClientEnvironment : public Environment
376 {
377 public:
378         ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
379                         ITextureSource *texturesource, IGameDef *gamedef);
380         ~ClientEnvironment();
381
382         Map & getMap()
383         { return *m_map; }
384
385         ClientMap & getClientMap()
386         { return *m_map; }
387
388         IGameDef *getGameDef()
389         { return m_gamedef; }
390
391         void step(f32 dtime);
392
393         virtual void addPlayer(Player *player);
394         LocalPlayer * getLocalPlayer();
395         
396         // Slightly deprecated
397         void updateMeshes(v3s16 blockpos);
398         void expireMeshes(bool only_daynight_diffed);
399
400         void setTimeOfDay(u32 time)
401         {
402                 u32 old_dr = getDayNightRatio();
403
404                 Environment::setTimeOfDay(time);
405
406                 if(getDayNightRatio() != old_dr)
407                 {
408                         /*infostream<<"ClientEnvironment: DayNightRatio changed"
409                                         <<" -> expiring meshes"<<std::endl;*/
410                         expireMeshes(true);
411                 }
412         }
413
414         /*
415                 ActiveObjects
416         */
417         
418         ClientActiveObject* getActiveObject(u16 id);
419
420         /*
421                 Adds an active object to the environment.
422                 Environment handles deletion of object.
423                 Object may be deleted by environment immediately.
424                 If id of object is 0, assigns a free id to it.
425                 Returns the id of the object.
426                 Returns 0 if not added and thus deleted.
427         */
428         u16 addActiveObject(ClientActiveObject *object);
429
430         void addActiveObject(u16 id, u8 type, const std::string &init_data);
431         void removeActiveObject(u16 id);
432
433         void processActiveObjectMessage(u16 id, const std::string &data);
434
435         /*
436                 Callbacks for activeobjects
437         */
438
439         void damageLocalPlayer(u8 damage);
440
441         /*
442                 Client likes to call these
443         */
444         
445         // Get all nearby objects
446         void getActiveObjects(v3f origin, f32 max_d,
447                         core::array<DistanceSortedActiveObject> &dest);
448         
449         // Get event from queue. CEE_NONE is returned if queue is empty.
450         ClientEnvEvent getClientEvent();
451         
452 private:
453         ClientMap *m_map;
454         scene::ISceneManager *m_smgr;
455         ITextureSource *m_texturesource;
456         IGameDef *m_gamedef;
457         core::map<u16, ClientActiveObject*> m_active_objects;
458         Queue<ClientEnvEvent> m_client_event_queue;
459         IntervalLimiter m_active_object_light_update_interval;
460         IntervalLimiter m_lava_hurt_interval;
461 };
462
463 #endif
464
465 #endif
466