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