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
38 #include "activeobject.h"
39 #include "util/numeric.h"
42 #include "jthread/jmutex.h"
44 class ServerEnvironment;
45 class ActiveBlockModifier;
46 class ServerActiveObject;
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 void removePlayer(const char *name);
75 Player * getPlayer(u16 peer_id);
76 Player * getPlayer(const char *name);
77 Player * getRandomConnectedPlayer();
78 Player * getNearestConnectedPlayer(v3f pos);
79 std::vector<Player*> getPlayers();
80 std::vector<Player*> getPlayers(bool ignore_disconnected);
82 u32 getDayNightRatio();
85 virtual void setTimeOfDay(u32 time);
87 float getTimeOfDayF();
89 void stepTimeOfDay(float dtime);
91 void setTimeOfDaySpeed(float speed);
92 float getTimeOfDaySpeed();
94 void setDayNightRatioOverride(bool enable, u32 value)
96 m_enable_day_night_ratio_override = enable;
97 m_day_night_ratio_override = value;
100 // counter used internally when triggering ABMs
104 // peer_ids in here should be unique, except that there may be many 0s
105 std::vector<Player*> m_players;
106 // Time of day in milli-hours (0-23999); determines day and night
108 // Time of day in 0...1
109 float m_time_of_day_f;
110 float m_time_of_day_speed;
111 // Used to buffer dtime for adding to m_time_of_day
112 float m_time_counter;
113 // Overriding the day-night ratio is useful for custom sky visuals
114 bool m_enable_day_night_ratio_override;
115 u32 m_day_night_ratio_override;
117 /* TODO: Add a callback function so these can be updated when a setting
118 * changes. At this point in time it doesn't matter (e.g. /set
119 * is documented to change server settings only)
121 * TODO: Local caching of settings is not optimal and should at some stage
122 * be updated to use a global settings object for getting thse values
123 * (as opposed to the this local caching). This can be addressed in
126 bool m_cache_enable_shaders;
129 JMutex m_timeofday_lock;
135 Active block modifier interface.
137 These are fed into ServerEnvironment at initialization time;
138 ServerEnvironment handles deleting them.
141 class ActiveBlockModifier
144 ActiveBlockModifier(){};
145 virtual ~ActiveBlockModifier(){};
147 // Set of contents to trigger on
148 virtual std::set<std::string> getTriggerContents()=0;
149 // Set of required neighbors (trigger doesn't happen if none are found)
150 // Empty = do not check neighbors
151 virtual std::set<std::string> getRequiredNeighbors()
152 { return std::set<std::string>(); }
153 // Trigger interval in seconds
154 virtual float getTriggerInterval() = 0;
155 // Random chance of (1 / return value), 0 is disallowed
156 virtual u32 getTriggerChance() = 0;
157 // This is called usually at interval for 1/chance of the nodes
158 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
159 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
160 u32 active_object_count, u32 active_object_count_wider){};
165 ActiveBlockModifier *abm;
168 ABMWithState(ActiveBlockModifier *abm_);
172 List of active blocks, used by ServerEnvironment
175 class ActiveBlockList
178 void update(std::vector<v3s16> &active_positions,
180 std::set<v3s16> &blocks_removed,
181 std::set<v3s16> &blocks_added);
183 bool contains(v3s16 p){
184 return (m_list.find(p) != m_list.end());
191 std::set<v3s16> m_list;
192 std::set<v3s16> m_forceloaded_list;
198 The server-side environment.
200 This is not thread-safe. Server uses an environment mutex.
203 class ServerEnvironment : public Environment
206 ServerEnvironment(ServerMap *map, GameScripting *scriptIface,
207 IGameDef *gamedef, const std::string &path_world);
208 ~ServerEnvironment();
212 ServerMap & getServerMap();
214 //TODO find way to remove this fct!
215 GameScripting* getScriptIface()
218 IGameDef *getGameDef()
219 { return m_gamedef; }
221 float getSendRecommendedInterval()
222 { return m_recommended_send_interval; }
224 void kickAllPlayers(const std::string &reason);
226 void saveLoadedPlayers();
227 void savePlayer(const std::string &playername);
228 Player *loadPlayer(const std::string &playername);
231 Save and load time of day and game timer
237 External ActiveObject interface
238 -------------------------------------------
241 ServerActiveObject* getActiveObject(u16 id);
244 Add an active object to the environment.
245 Environment handles deletion of object.
246 Object may be deleted by environment immediately.
247 If id of object is 0, assigns a free id to it.
248 Returns the id of the object.
249 Returns 0 if not added and thus deleted.
251 u16 addActiveObject(ServerActiveObject *object);
254 Add an active object as a static object to the corresponding
256 Caller allocates memory, ServerEnvironment frees memory.
257 Return value: true if succeeded, false if failed.
258 (note: not used, pending removal from engine)
260 //bool addActiveObjectAsStatic(ServerActiveObject *object);
263 Find out what new objects have been added to
264 inside a radius around a position
266 void getAddedActiveObjects(v3s16 pos, s16 radius,
268 std::set<u16> ¤t_objects,
269 std::set<u16> &added_objects);
272 Find out what new objects have been removed from
273 inside a radius around a position
275 void getRemovedActiveObjects(v3s16 pos, s16 radius,
277 std::set<u16> ¤t_objects,
278 std::set<u16> &removed_objects);
281 Get the next message emitted by some active object.
282 Returns a message with id=0 if no messages are available.
284 ActiveObjectMessage getActiveObjectMessage();
287 Activate objects and dynamically modify for the dtime determined
288 from timestamp and additional_dtime
290 void activateBlock(MapBlock *block, u32 additional_dtime=0);
294 -------------------------------------------
297 void addActiveBlockModifier(ActiveBlockModifier *abm);
301 -------------------------------------------
304 // Script-aware node setters
305 bool setNode(v3s16 p, const MapNode &n);
306 bool removeNode(v3s16 p);
307 bool swapNode(v3s16 p, const MapNode &n);
309 // Find all active objects inside a radius around a point
310 void getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius);
312 // Clear all objects, loading and going through every MapBlock
313 void clearAllObjects();
315 // This makes stuff happen
316 void step(f32 dtime);
318 //check if there's a line of sight between two positions
319 bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0, v3s16 *p=NULL);
321 u32 getGameTime() { return m_game_time; }
323 void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
324 float getMaxLagEstimate() { return m_max_lag_estimate; }
326 std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; };
331 Internal ActiveObject interface
332 -------------------------------------------
336 Add an active object to the environment.
338 Called by addActiveObject.
340 Object may be deleted by environment immediately.
341 If id of object is 0, assigns a free id to it.
342 Returns the id of the object.
343 Returns 0 if not added and thus deleted.
345 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
348 Remove all objects that satisfy (m_removed && m_known_by_count==0)
350 void removeRemovedObjects();
353 Convert stored objects from block to active
355 void activateObjects(MapBlock *block, u32 dtime_s);
358 Convert objects that are not in active blocks to static.
360 If m_known_by_count != 0, active object is not deleted, but static
361 data is still updated.
363 If force_delete is set, active object is deleted nevertheless. It
364 shall only be set so in the destructor of the environment.
366 void deactivateFarObjects(bool force_delete);
375 GameScripting* m_script;
379 const std::string m_path_world;
380 // Active object list
381 std::map<u16, ServerActiveObject*> m_active_objects;
382 // Outgoing network message buffer for active objects
383 std::queue<ActiveObjectMessage> m_active_object_messages;
385 float m_send_recommended_timer;
386 IntervalLimiter m_object_management_interval;
387 // List of active blocks
388 ActiveBlockList m_active_blocks;
389 IntervalLimiter m_active_blocks_management_interval;
390 IntervalLimiter m_active_block_modifier_interval;
391 IntervalLimiter m_active_blocks_nodemetadata_interval;
392 int m_active_block_interval_overload_skip;
393 // Time from the beginning of the game in seconds.
394 // Incremented in step().
396 // A helper variable for incrementing the latter
397 float m_game_time_fraction_counter;
398 std::vector<ABMWithState> m_abms;
399 // An interval for generally sending object positions and stuff
400 float m_recommended_send_interval;
401 // Estimate for general maximum lag as determined by server.
402 // Can raise to high values like 15s with eg. map generation mods.
403 float m_max_lag_estimate;
408 #include "clientobject.h"
409 #include "content_cao.h"
411 class ClientSimpleObject;
414 The client-side environment.
416 This is not thread-safe.
417 Must be called from main (irrlicht) thread (uses the SceneManager)
418 Client uses an environment mutex.
421 enum ClientEnvEventType
428 struct ClientEnvEvent
430 ClientEnvEventType type;
444 class ClientEnvironment : public Environment
447 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
448 ITextureSource *texturesource, IGameDef *gamedef,
449 IrrlichtDevice *device);
450 ~ClientEnvironment();
453 ClientMap & getClientMap();
455 IGameDef *getGameDef()
456 { return m_gamedef; }
458 void step(f32 dtime);
460 virtual void addPlayer(Player *player);
461 LocalPlayer * getLocalPlayer();
467 void addSimpleObject(ClientSimpleObject *simple);
473 GenericCAO* getGenericCAO(u16 id);
474 ClientActiveObject* getActiveObject(u16 id);
477 Adds an active object to the environment.
478 Environment handles deletion of object.
479 Object may be deleted by environment immediately.
480 If id of object is 0, assigns a free id to it.
481 Returns the id of the object.
482 Returns 0 if not added and thus deleted.
484 u16 addActiveObject(ClientActiveObject *object);
486 void addActiveObject(u16 id, u8 type, const std::string &init_data);
487 void removeActiveObject(u16 id);
489 void processActiveObjectMessage(u16 id, const std::string &data);
492 Callbacks for activeobjects
495 void damageLocalPlayer(u8 damage, bool handle_hp=true);
496 void updateLocalPlayerBreath(u16 breath);
499 Client likes to call these
502 // Get all nearby objects
503 void getActiveObjects(v3f origin, f32 max_d,
504 std::vector<DistanceSortedActiveObject> &dest);
506 // Get event from queue. CEE_NONE is returned if queue is empty.
507 ClientEnvEvent getClientEvent();
509 u16 attachement_parent_ids[USHRT_MAX + 1];
511 std::list<std::string> getPlayerNames()
512 { return m_player_names; }
513 void addPlayerName(std::string name)
514 { m_player_names.push_back(name); }
515 void removePlayerName(std::string name)
516 { m_player_names.remove(name); }
517 void updateCameraOffset(v3s16 camera_offset)
518 { m_camera_offset = camera_offset; }
519 v3s16 getCameraOffset()
520 { return m_camera_offset; }
524 scene::ISceneManager *m_smgr;
525 ITextureSource *m_texturesource;
527 IrrlichtDevice *m_irr;
528 std::map<u16, ClientActiveObject*> m_active_objects;
529 std::vector<ClientSimpleObject*> m_simple_objects;
530 std::list<ClientEnvEvent> m_client_event_queue;
531 IntervalLimiter m_active_object_light_update_interval;
532 IntervalLimiter m_lava_hurt_interval;
533 IntervalLimiter m_drowning_interval;
534 IntervalLimiter m_breathing_interval;
535 std::list<std::string> m_player_names;
536 v3s16 m_camera_offset;