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