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"
43 #include "network/networkprotocol.h" // for AccessDeniedCode
45 class ServerEnvironment;
46 class ActiveBlockModifier;
47 class ServerActiveObject;
59 // Environment will delete the map passed to the constructor
61 virtual ~Environment();
64 Step everything in environment.
69 virtual void step(f32 dtime) = 0;
71 virtual Map & getMap() = 0;
73 virtual void addPlayer(Player *player);
74 void removePlayer(u16 peer_id);
75 void removePlayer(const char *name);
76 Player * getPlayer(u16 peer_id);
77 Player * getPlayer(const char *name);
78 Player * getRandomConnectedPlayer();
79 Player * getNearestConnectedPlayer(v3f pos);
80 std::vector<Player*> getPlayers();
81 std::vector<Player*> getPlayers(bool ignore_disconnected);
83 u32 getDayNightRatio();
86 virtual void setTimeOfDay(u32 time);
88 float getTimeOfDayF();
90 void stepTimeOfDay(float dtime);
92 void setTimeOfDaySpeed(float speed);
93 float getTimeOfDaySpeed();
95 void setDayNightRatioOverride(bool enable, u32 value)
97 m_enable_day_night_ratio_override = enable;
98 m_day_night_ratio_override = value;
101 // counter used internally when triggering ABMs
105 // peer_ids in here should be unique, except that there may be many 0s
106 std::vector<Player*> m_players;
107 // Time of day in milli-hours (0-23999); determines day and night
109 // Time of day in 0...1
110 float m_time_of_day_f;
111 float m_time_of_day_speed;
112 // Used to buffer dtime for adding to m_time_of_day
113 float m_time_counter;
114 // Overriding the day-night ratio is useful for custom sky visuals
115 bool m_enable_day_night_ratio_override;
116 u32 m_day_night_ratio_override;
118 /* TODO: Add a callback function so these can be updated when a setting
119 * changes. At this point in time it doesn't matter (e.g. /set
120 * is documented to change server settings only)
122 * TODO: Local caching of settings is not optimal and should at some stage
123 * be updated to use a global settings object for getting thse values
124 * (as opposed to the this local caching). This can be addressed in
127 bool m_cache_enable_shaders;
130 JMutex m_timeofday_lock;
136 Active block modifier interface.
138 These are fed into ServerEnvironment at initialization time;
139 ServerEnvironment handles deleting them.
142 class ActiveBlockModifier
145 ActiveBlockModifier(){};
146 virtual ~ActiveBlockModifier(){};
148 // Set of contents to trigger on
149 virtual std::set<std::string> getTriggerContents()=0;
150 // Set of required neighbors (trigger doesn't happen if none are found)
151 // Empty = do not check neighbors
152 virtual std::set<std::string> getRequiredNeighbors()
153 { return std::set<std::string>(); }
154 // Trigger interval in seconds
155 virtual float getTriggerInterval() = 0;
156 // Random chance of (1 / return value), 0 is disallowed
157 virtual u32 getTriggerChance() = 0;
158 // This is called usually at interval for 1/chance of the nodes
159 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
160 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
161 u32 active_object_count, u32 active_object_count_wider){};
166 ActiveBlockModifier *abm;
169 ABMWithState(ActiveBlockModifier *abm_);
173 List of active blocks, used by ServerEnvironment
176 class ActiveBlockList
179 void update(std::vector<v3s16> &active_positions,
181 std::set<v3s16> &blocks_removed,
182 std::set<v3s16> &blocks_added);
184 bool contains(v3s16 p){
185 return (m_list.find(p) != m_list.end());
192 std::set<v3s16> m_list;
193 std::set<v3s16> m_forceloaded_list;
199 The server-side environment.
201 This is not thread-safe. Server uses an environment mutex.
204 class ServerEnvironment : public Environment
207 ServerEnvironment(ServerMap *map, GameScripting *scriptIface,
208 IGameDef *gamedef, const std::string &path_world);
209 ~ServerEnvironment();
213 ServerMap & getServerMap();
215 //TODO find way to remove this fct!
216 GameScripting* getScriptIface()
219 IGameDef *getGameDef()
220 { return m_gamedef; }
222 float getSendRecommendedInterval()
223 { return m_recommended_send_interval; }
225 void kickAllPlayers(AccessDeniedCode reason,
226 const std::string &str_reason, bool reconnect);
228 void saveLoadedPlayers();
229 void savePlayer(const std::string &playername);
230 Player *loadPlayer(const std::string &playername);
233 Save and load time of day and game timer
239 External ActiveObject interface
240 -------------------------------------------
243 ServerActiveObject* getActiveObject(u16 id);
246 Add an active object to the environment.
247 Environment handles deletion of object.
248 Object may be deleted by environment immediately.
249 If id of object is 0, assigns a free id to it.
250 Returns the id of the object.
251 Returns 0 if not added and thus deleted.
253 u16 addActiveObject(ServerActiveObject *object);
256 Add an active object as a static object to the corresponding
258 Caller allocates memory, ServerEnvironment frees memory.
259 Return value: true if succeeded, false if failed.
260 (note: not used, pending removal from engine)
262 //bool addActiveObjectAsStatic(ServerActiveObject *object);
265 Find out what new objects have been added to
266 inside a radius around a position
268 void getAddedActiveObjects(v3s16 pos, s16 radius,
270 std::set<u16> ¤t_objects,
271 std::set<u16> &added_objects);
274 Find out what new objects have been removed from
275 inside a radius around a position
277 void getRemovedActiveObjects(v3s16 pos, s16 radius,
279 std::set<u16> ¤t_objects,
280 std::set<u16> &removed_objects);
283 Get the next message emitted by some active object.
284 Returns a message with id=0 if no messages are available.
286 ActiveObjectMessage getActiveObjectMessage();
289 Activate objects and dynamically modify for the dtime determined
290 from timestamp and additional_dtime
292 void activateBlock(MapBlock *block, u32 additional_dtime=0);
296 -------------------------------------------
299 void addActiveBlockModifier(ActiveBlockModifier *abm);
303 -------------------------------------------
306 // Script-aware node setters
307 bool setNode(v3s16 p, const MapNode &n);
308 bool removeNode(v3s16 p);
309 bool swapNode(v3s16 p, const MapNode &n);
311 // Find all active objects inside a radius around a point
312 void getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius);
314 // Clear all objects, loading and going through every MapBlock
315 void clearAllObjects();
317 // This makes stuff happen
318 void step(f32 dtime);
320 //check if there's a line of sight between two positions
321 bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0, v3s16 *p=NULL);
323 u32 getGameTime() { return m_game_time; }
325 void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
326 float getMaxLagEstimate() { return m_max_lag_estimate; }
328 std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; };
333 Internal ActiveObject interface
334 -------------------------------------------
338 Add an active object to the environment.
340 Called by addActiveObject.
342 Object may be deleted by environment immediately.
343 If id of object is 0, assigns a free id to it.
344 Returns the id of the object.
345 Returns 0 if not added and thus deleted.
347 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
350 Remove all objects that satisfy (m_removed && m_known_by_count==0)
352 void removeRemovedObjects();
355 Convert stored objects from block to active
357 void activateObjects(MapBlock *block, u32 dtime_s);
360 Convert objects that are not in active blocks to static.
362 If m_known_by_count != 0, active object is not deleted, but static
363 data is still updated.
365 If force_delete is set, active object is deleted nevertheless. It
366 shall only be set so in the destructor of the environment.
368 void deactivateFarObjects(bool force_delete);
377 GameScripting* m_script;
381 const std::string m_path_world;
382 // Active object list
383 std::map<u16, ServerActiveObject*> m_active_objects;
384 // Outgoing network message buffer for active objects
385 std::queue<ActiveObjectMessage> m_active_object_messages;
387 float m_send_recommended_timer;
388 IntervalLimiter m_object_management_interval;
389 // List of active blocks
390 ActiveBlockList m_active_blocks;
391 IntervalLimiter m_active_blocks_management_interval;
392 IntervalLimiter m_active_block_modifier_interval;
393 IntervalLimiter m_active_blocks_nodemetadata_interval;
394 int m_active_block_interval_overload_skip;
395 // Time from the beginning of the game in seconds.
396 // Incremented in step().
398 // A helper variable for incrementing the latter
399 float m_game_time_fraction_counter;
400 std::vector<ABMWithState> m_abms;
401 // An interval for generally sending object positions and stuff
402 float m_recommended_send_interval;
403 // Estimate for general maximum lag as determined by server.
404 // Can raise to high values like 15s with eg. map generation mods.
405 float m_max_lag_estimate;
410 #include "clientobject.h"
411 #include "content_cao.h"
413 class ClientSimpleObject;
416 The client-side environment.
418 This is not thread-safe.
419 Must be called from main (irrlicht) thread (uses the SceneManager)
420 Client uses an environment mutex.
423 enum ClientEnvEventType
430 struct ClientEnvEvent
432 ClientEnvEventType type;
446 class ClientEnvironment : public Environment
449 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
450 ITextureSource *texturesource, IGameDef *gamedef,
451 IrrlichtDevice *device);
452 ~ClientEnvironment();
455 ClientMap & getClientMap();
457 IGameDef *getGameDef()
458 { return m_gamedef; }
460 void step(f32 dtime);
462 virtual void addPlayer(Player *player);
463 LocalPlayer * getLocalPlayer();
469 void addSimpleObject(ClientSimpleObject *simple);
475 GenericCAO* getGenericCAO(u16 id);
476 ClientActiveObject* getActiveObject(u16 id);
479 Adds an active object to the environment.
480 Environment handles deletion of object.
481 Object may be deleted by environment immediately.
482 If id of object is 0, assigns a free id to it.
483 Returns the id of the object.
484 Returns 0 if not added and thus deleted.
486 u16 addActiveObject(ClientActiveObject *object);
488 void addActiveObject(u16 id, u8 type, const std::string &init_data);
489 void removeActiveObject(u16 id);
491 void processActiveObjectMessage(u16 id, const std::string &data);
494 Callbacks for activeobjects
497 void damageLocalPlayer(u8 damage, bool handle_hp=true);
498 void updateLocalPlayerBreath(u16 breath);
501 Client likes to call these
504 // Get all nearby objects
505 void getActiveObjects(v3f origin, f32 max_d,
506 std::vector<DistanceSortedActiveObject> &dest);
508 // Get event from queue. CEE_NONE is returned if queue is empty.
509 ClientEnvEvent getClientEvent();
511 u16 attachement_parent_ids[USHRT_MAX + 1];
513 std::list<std::string> getPlayerNames()
514 { return m_player_names; }
515 void addPlayerName(std::string name)
516 { m_player_names.push_back(name); }
517 void removePlayerName(std::string name)
518 { m_player_names.remove(name); }
519 void updateCameraOffset(v3s16 camera_offset)
520 { m_camera_offset = camera_offset; }
521 v3s16 getCameraOffset()
522 { return m_camera_offset; }
526 scene::ISceneManager *m_smgr;
527 ITextureSource *m_texturesource;
529 IrrlichtDevice *m_irr;
530 std::map<u16, ClientActiveObject*> m_active_objects;
531 std::vector<ClientSimpleObject*> m_simple_objects;
532 std::list<ClientEnvEvent> m_client_event_queue;
533 IntervalLimiter m_active_object_light_update_interval;
534 IntervalLimiter m_lava_hurt_interval;
535 IntervalLimiter m_drowning_interval;
536 IntervalLimiter m_breathing_interval;
537 std::list<std::string> m_player_names;
538 v3s16 m_camera_offset;