3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #ifndef ENVIRONMENT_HEADER
21 #define ENVIRONMENT_HEADER
24 This class is the game's environment.
29 - The current time in the game
35 #include "irrlichttypes_extrabloated.h"
38 #include "activeobject.h"
39 #include "util/container.h"
40 #include "util/numeric.h"
44 class ServerEnvironment;
45 class ActiveBlockModifier;
46 class ServerActiveObject;
47 typedef struct lua_State lua_State;
58 // Environment will delete the map passed to the constructor
60 virtual ~Environment();
63 Step everything in environment.
68 virtual void step(f32 dtime) = 0;
70 virtual Map & getMap() = 0;
72 virtual void addPlayer(Player *player);
73 void removePlayer(u16 peer_id);
74 Player * getPlayer(u16 peer_id);
75 Player * getPlayer(const char *name);
76 Player * getRandomConnectedPlayer();
77 Player * getNearestConnectedPlayer(v3f pos);
78 std::list<Player*> getPlayers();
79 std::list<Player*> getPlayers(bool ignore_disconnected);
80 void printPlayers(std::ostream &o);
82 u32 getDayNightRatio();
85 virtual void setTimeOfDay(u32 time)
88 m_time_of_day_f = (float)time / 24000.0;
92 { return m_time_of_day; }
95 { return m_time_of_day_f; }
97 void stepTimeOfDay(float dtime);
99 void setTimeOfDaySpeed(float speed)
100 { m_time_of_day_speed = speed; }
102 float getTimeOfDaySpeed()
103 { return m_time_of_day_speed; }
106 // peer_ids in here should be unique, except that there may be many 0s
107 std::list<Player*> m_players;
108 // Time of day in milli-hours (0-23999); determines day and night
110 // Time of day in 0...1
111 float m_time_of_day_f;
112 float m_time_of_day_speed;
113 // Used to buffer dtime for adding to m_time_of_day
114 float m_time_counter;
118 Active block modifier interface.
120 These are fed into ServerEnvironment at initialization time;
121 ServerEnvironment handles deleting them.
124 class ActiveBlockModifier
127 ActiveBlockModifier(){};
128 virtual ~ActiveBlockModifier(){};
130 // Set of contents to trigger on
131 virtual std::set<std::string> getTriggerContents()=0;
132 // Set of required neighbors (trigger doesn't happen if none are found)
133 // Empty = do not check neighbors
134 virtual std::set<std::string> getRequiredNeighbors()
135 { return std::set<std::string>(); }
136 // Trigger interval in seconds
137 virtual float getTriggerInterval() = 0;
138 // Random chance of (1 / return value), 0 is disallowed
139 virtual u32 getTriggerChance() = 0;
140 // This is called usually at interval for 1/chance of the nodes
141 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
142 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
143 u32 active_object_count, u32 active_object_count_wider){};
148 ActiveBlockModifier *abm;
151 ABMWithState(ActiveBlockModifier *abm_);
155 List of active blocks, used by ServerEnvironment
158 class ActiveBlockList
161 void update(std::list<v3s16> &active_positions,
163 std::set<v3s16> &blocks_removed,
164 std::set<v3s16> &blocks_added);
166 bool contains(v3s16 p){
167 return (m_list.find(p) != m_list.end());
174 std::set<v3s16> m_list;
179 class IBackgroundBlockEmerger
182 virtual void queueBlockEmerge(v3s16 blockpos, bool allow_generate)=0;
186 The server-side environment.
188 This is not thread-safe. Server uses an environment mutex.
191 class ServerEnvironment : public Environment
194 ServerEnvironment(ServerMap *map, ScriptApi *iface, IGameDef *gamedef,
195 IBackgroundBlockEmerger *emerger);
196 ~ServerEnvironment();
200 ServerMap & getServerMap();
202 //TODO find way to remove this fct!
203 ScriptApi* getScriptIface()
206 IGameDef *getGameDef()
207 { return m_gamedef; }
209 float getSendRecommendedInterval()
210 { return m_recommended_send_interval; }
215 void serializePlayers(const std::string &savedir);
216 void deSerializePlayers(const std::string &savedir);
219 Save and load time of day and game timer
221 void saveMeta(const std::string &savedir);
222 void loadMeta(const std::string &savedir);
225 External ActiveObject interface
226 -------------------------------------------
229 ServerActiveObject* getActiveObject(u16 id);
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.
239 u16 addActiveObject(ServerActiveObject *object);
242 Add an active object as a static object to the corresponding
244 Caller allocates memory, ServerEnvironment frees memory.
245 Return value: true if succeeded, false if failed.
246 (note: not used, pending removal from engine)
248 //bool addActiveObjectAsStatic(ServerActiveObject *object);
251 Find out what new objects have been added to
252 inside a radius around a position
254 void getAddedActiveObjects(v3s16 pos, s16 radius,
255 std::set<u16> ¤t_objects,
256 std::set<u16> &added_objects);
259 Find out what new objects have been removed from
260 inside a radius around a position
262 void getRemovedActiveObjects(v3s16 pos, s16 radius,
263 std::set<u16> ¤t_objects,
264 std::set<u16> &removed_objects);
267 Get the next message emitted by some active object.
268 Returns a message with id=0 if no messages are available.
270 ActiveObjectMessage getActiveObjectMessage();
273 Activate objects and dynamically modify for the dtime determined
274 from timestamp and additional_dtime
276 void activateBlock(MapBlock *block, u32 additional_dtime=0);
280 -------------------------------------------
283 void addActiveBlockModifier(ActiveBlockModifier *abm);
287 -------------------------------------------
290 // Script-aware node setters
291 bool setNode(v3s16 p, const MapNode &n);
292 bool removeNode(v3s16 p);
294 // Find all active objects inside a radius around a point
295 std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
297 // Clear all objects, loading and going through every MapBlock
298 void clearAllObjects();
300 // This makes stuff happen
301 void step(f32 dtime);
303 //check if there's a line of sight between two positions
304 bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0);
306 u32 getGameTime() { return m_game_time; }
308 void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
309 float getMaxLagEstimate() { return m_max_lag_estimate; }
313 Internal ActiveObject interface
314 -------------------------------------------
318 Add an active object to the environment.
320 Called by addActiveObject.
322 Object may be deleted by environment immediately.
323 If id of object is 0, assigns a free id to it.
324 Returns the id of the object.
325 Returns 0 if not added and thus deleted.
327 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
330 Remove all objects that satisfy (m_removed && m_known_by_count==0)
332 void removeRemovedObjects();
335 Convert stored objects from block to active
337 void activateObjects(MapBlock *block, u32 dtime_s);
340 Convert objects that are not in active blocks to static.
342 If m_known_by_count != 0, active object is not deleted, but static
343 data is still updated.
345 If force_delete is set, active object is deleted nevertheless. It
346 shall only be set so in the destructor of the environment.
348 void deactivateFarObjects(bool force_delete);
360 // Background block emerger (the server, in practice)
361 IBackgroundBlockEmerger *m_emerger;
362 // Active object list
363 std::map<u16, ServerActiveObject*> m_active_objects;
364 // Outgoing network message buffer for active objects
365 Queue<ActiveObjectMessage> m_active_object_messages;
367 float m_random_spawn_timer; // used for experimental code
368 float m_send_recommended_timer;
369 IntervalLimiter m_object_management_interval;
370 // List of active blocks
371 ActiveBlockList m_active_blocks;
372 IntervalLimiter m_active_blocks_management_interval;
373 IntervalLimiter m_active_block_modifier_interval;
374 IntervalLimiter m_active_blocks_nodemetadata_interval;
375 int m_active_block_interval_overload_skip;
376 // Time from the beginning of the game in seconds.
377 // Incremented in step().
379 // A helper variable for incrementing the latter
380 float m_game_time_fraction_counter;
381 std::list<ABMWithState> m_abms;
382 // An interval for generally sending object positions and stuff
383 float m_recommended_send_interval;
384 // Estimate for general maximum lag as determined by server.
385 // Can raise to high values like 15s with eg. map generation mods.
386 float m_max_lag_estimate;
391 #include "clientobject.h"
392 class ClientSimpleObject;
395 The client-side environment.
397 This is not thread-safe.
398 Must be called from main (irrlicht) thread (uses the SceneManager)
399 Client uses an environment mutex.
402 enum ClientEnvEventType
409 struct ClientEnvEvent
411 ClientEnvEventType type;
425 class ClientEnvironment : public Environment
428 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
429 ITextureSource *texturesource, IGameDef *gamedef,
430 IrrlichtDevice *device);
431 ~ClientEnvironment();
434 ClientMap & getClientMap();
436 IGameDef *getGameDef()
437 { return m_gamedef; }
439 void step(f32 dtime);
441 virtual void addPlayer(Player *player);
442 LocalPlayer * getLocalPlayer();
448 void addSimpleObject(ClientSimpleObject *simple);
454 ClientActiveObject* getActiveObject(u16 id);
457 Adds an active object to the environment.
458 Environment handles deletion of object.
459 Object may be deleted by environment immediately.
460 If id of object is 0, assigns a free id to it.
461 Returns the id of the object.
462 Returns 0 if not added and thus deleted.
464 u16 addActiveObject(ClientActiveObject *object);
466 void addActiveObject(u16 id, u8 type, const std::string &init_data);
467 void removeActiveObject(u16 id);
469 void processActiveObjectMessage(u16 id, const std::string &data);
472 Callbacks for activeobjects
475 void damageLocalPlayer(u8 damage, bool handle_hp=true);
476 void updateLocalPlayerBreath(u16 breath);
479 Client likes to call these
482 // Get all nearby objects
483 void getActiveObjects(v3f origin, f32 max_d,
484 std::vector<DistanceSortedActiveObject> &dest);
486 // Get event from queue. CEE_NONE is returned if queue is empty.
487 ClientEnvEvent getClientEvent();
489 std::vector<core::vector2d<int> > attachment_list; // X is child ID, Y is parent ID
491 std::list<std::string> getPlayerNames()
492 { return m_player_names; }
493 void addPlayerName(std::string name)
494 { m_player_names.push_back(name); }
495 void removePlayerName(std::string name)
496 { m_player_names.remove(name); }
500 scene::ISceneManager *m_smgr;
501 ITextureSource *m_texturesource;
503 IrrlichtDevice *m_irr;
504 std::map<u16, ClientActiveObject*> m_active_objects;
505 std::list<ClientSimpleObject*> m_simple_objects;
506 Queue<ClientEnvEvent> m_client_event_queue;
507 IntervalLimiter m_active_object_light_update_interval;
508 IntervalLimiter m_lava_hurt_interval;
509 IntervalLimiter m_drowning_interval;
510 IntervalLimiter m_breathing_interval;
511 std::list<std::string> m_player_names;