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; }
225 void saveLoadedPlayers();
226 void savePlayer(const std::string &playername);
227 Player *loadPlayer(const std::string &playername);
230 Save and load time of day and game timer
236 External ActiveObject interface
237 -------------------------------------------
240 ServerActiveObject* getActiveObject(u16 id);
243 Add an active object to the environment.
244 Environment handles deletion of object.
245 Object may be deleted by environment immediately.
246 If id of object is 0, assigns a free id to it.
247 Returns the id of the object.
248 Returns 0 if not added and thus deleted.
250 u16 addActiveObject(ServerActiveObject *object);
253 Add an active object as a static object to the corresponding
255 Caller allocates memory, ServerEnvironment frees memory.
256 Return value: true if succeeded, false if failed.
257 (note: not used, pending removal from engine)
259 //bool addActiveObjectAsStatic(ServerActiveObject *object);
262 Find out what new objects have been added to
263 inside a radius around a position
265 void getAddedActiveObjects(v3s16 pos, s16 radius,
267 std::set<u16> ¤t_objects,
268 std::set<u16> &added_objects);
271 Find out what new objects have been removed from
272 inside a radius around a position
274 void getRemovedActiveObjects(v3s16 pos, s16 radius,
276 std::set<u16> ¤t_objects,
277 std::set<u16> &removed_objects);
280 Get the next message emitted by some active object.
281 Returns a message with id=0 if no messages are available.
283 ActiveObjectMessage getActiveObjectMessage();
286 Activate objects and dynamically modify for the dtime determined
287 from timestamp and additional_dtime
289 void activateBlock(MapBlock *block, u32 additional_dtime=0);
293 -------------------------------------------
296 void addActiveBlockModifier(ActiveBlockModifier *abm);
300 -------------------------------------------
303 // Script-aware node setters
304 bool setNode(v3s16 p, const MapNode &n);
305 bool removeNode(v3s16 p);
306 bool swapNode(v3s16 p, const MapNode &n);
308 // Find all active objects inside a radius around a point
309 void getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius);
311 // Clear all objects, loading and going through every MapBlock
312 void clearAllObjects();
314 // This makes stuff happen
315 void step(f32 dtime);
317 //check if there's a line of sight between two positions
318 bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0, v3s16 *p=NULL);
320 u32 getGameTime() { return m_game_time; }
322 void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
323 float getMaxLagEstimate() { return m_max_lag_estimate; }
325 std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; };
330 Internal ActiveObject interface
331 -------------------------------------------
335 Add an active object to the environment.
337 Called by addActiveObject.
339 Object may be deleted by environment immediately.
340 If id of object is 0, assigns a free id to it.
341 Returns the id of the object.
342 Returns 0 if not added and thus deleted.
344 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
347 Remove all objects that satisfy (m_removed && m_known_by_count==0)
349 void removeRemovedObjects();
352 Convert stored objects from block to active
354 void activateObjects(MapBlock *block, u32 dtime_s);
357 Convert objects that are not in active blocks to static.
359 If m_known_by_count != 0, active object is not deleted, but static
360 data is still updated.
362 If force_delete is set, active object is deleted nevertheless. It
363 shall only be set so in the destructor of the environment.
365 void deactivateFarObjects(bool force_delete);
374 GameScripting* m_script;
378 const std::string m_path_world;
379 // Active object list
380 std::map<u16, ServerActiveObject*> m_active_objects;
381 // Outgoing network message buffer for active objects
382 std::queue<ActiveObjectMessage> m_active_object_messages;
384 float m_send_recommended_timer;
385 IntervalLimiter m_object_management_interval;
386 // List of active blocks
387 ActiveBlockList m_active_blocks;
388 IntervalLimiter m_active_blocks_management_interval;
389 IntervalLimiter m_active_block_modifier_interval;
390 IntervalLimiter m_active_blocks_nodemetadata_interval;
391 int m_active_block_interval_overload_skip;
392 // Time from the beginning of the game in seconds.
393 // Incremented in step().
395 // A helper variable for incrementing the latter
396 float m_game_time_fraction_counter;
397 std::vector<ABMWithState> m_abms;
398 // An interval for generally sending object positions and stuff
399 float m_recommended_send_interval;
400 // Estimate for general maximum lag as determined by server.
401 // Can raise to high values like 15s with eg. map generation mods.
402 float m_max_lag_estimate;
407 #include "clientobject.h"
408 #include "content_cao.h"
410 class ClientSimpleObject;
413 The client-side environment.
415 This is not thread-safe.
416 Must be called from main (irrlicht) thread (uses the SceneManager)
417 Client uses an environment mutex.
420 enum ClientEnvEventType
427 struct ClientEnvEvent
429 ClientEnvEventType type;
443 class ClientEnvironment : public Environment
446 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
447 ITextureSource *texturesource, IGameDef *gamedef,
448 IrrlichtDevice *device);
449 ~ClientEnvironment();
452 ClientMap & getClientMap();
454 IGameDef *getGameDef()
455 { return m_gamedef; }
457 void step(f32 dtime);
459 virtual void addPlayer(Player *player);
460 LocalPlayer * getLocalPlayer();
466 void addSimpleObject(ClientSimpleObject *simple);
472 GenericCAO* getGenericCAO(u16 id);
473 ClientActiveObject* getActiveObject(u16 id);
476 Adds an active object to the environment.
477 Environment handles deletion of object.
478 Object may be deleted by environment immediately.
479 If id of object is 0, assigns a free id to it.
480 Returns the id of the object.
481 Returns 0 if not added and thus deleted.
483 u16 addActiveObject(ClientActiveObject *object);
485 void addActiveObject(u16 id, u8 type, const std::string &init_data);
486 void removeActiveObject(u16 id);
488 void processActiveObjectMessage(u16 id, const std::string &data);
491 Callbacks for activeobjects
494 void damageLocalPlayer(u8 damage, bool handle_hp=true);
495 void updateLocalPlayerBreath(u16 breath);
498 Client likes to call these
501 // Get all nearby objects
502 void getActiveObjects(v3f origin, f32 max_d,
503 std::vector<DistanceSortedActiveObject> &dest);
505 // Get event from queue. CEE_NONE is returned if queue is empty.
506 ClientEnvEvent getClientEvent();
508 u16 m_attachements[USHRT_MAX];
510 std::list<std::string> getPlayerNames()
511 { return m_player_names; }
512 void addPlayerName(std::string name)
513 { m_player_names.push_back(name); }
514 void removePlayerName(std::string name)
515 { m_player_names.remove(name); }
516 void updateCameraOffset(v3s16 camera_offset)
517 { m_camera_offset = camera_offset; }
518 v3s16 getCameraOffset()
519 { return m_camera_offset; }
523 scene::ISceneManager *m_smgr;
524 ITextureSource *m_texturesource;
526 IrrlichtDevice *m_irr;
527 std::map<u16, ClientActiveObject*> m_active_objects;
528 std::vector<ClientSimpleObject*> m_simple_objects;
529 std::list<ClientEnvEvent> m_client_event_queue;
530 IntervalLimiter m_active_object_light_update_interval;
531 IntervalLimiter m_lava_hurt_interval;
532 IntervalLimiter m_drowning_interval;
533 IntervalLimiter m_breathing_interval;
534 std::list<std::string> m_player_names;
535 v3s16 m_camera_offset;