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