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