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
37 #include "activeobject.h"
38 #include "util/numeric.h"
41 #include "jthread/jmutex.h"
43 class ServerEnvironment;
44 class ActiveBlockModifier;
45 class ServerActiveObject;
57 // Environment will delete the map passed to the constructor
59 virtual ~Environment();
62 Step everything in environment.
67 virtual void step(f32 dtime) = 0;
69 virtual Map & getMap() = 0;
71 virtual void addPlayer(Player *player);
72 void removePlayer(u16 peer_id);
73 void removePlayer(const char *name);
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);
81 u32 getDayNightRatio();
84 virtual void setTimeOfDay(u32 time)
87 m_time_of_day_f = (float)time / 24000.0;
91 { return m_time_of_day; }
94 { return m_time_of_day_f; }
96 void stepTimeOfDay(float dtime);
98 void setTimeOfDaySpeed(float speed);
100 float getTimeOfDaySpeed();
102 void setDayNightRatioOverride(bool enable, u32 value)
104 m_enable_day_night_ratio_override = enable;
105 m_day_night_ratio_override = value;
108 // counter used internally when triggering ABMs
112 // peer_ids in here should be unique, except that there may be many 0s
113 std::list<Player*> m_players;
114 // Time of day in milli-hours (0-23999); determines day and night
116 // Time of day in 0...1
117 float m_time_of_day_f;
118 float m_time_of_day_speed;
119 // Used to buffer dtime for adding to m_time_of_day
120 float m_time_counter;
121 // Overriding the day-night ratio is useful for custom sky visuals
122 bool m_enable_day_night_ratio_override;
123 u32 m_day_night_ratio_override;
131 Active block modifier interface.
133 These are fed into ServerEnvironment at initialization time;
134 ServerEnvironment handles deleting them.
137 class ActiveBlockModifier
140 ActiveBlockModifier(){};
141 virtual ~ActiveBlockModifier(){};
143 // Set of contents to trigger on
144 virtual std::set<std::string> getTriggerContents()=0;
145 // Set of required neighbors (trigger doesn't happen if none are found)
146 // Empty = do not check neighbors
147 virtual std::set<std::string> getRequiredNeighbors()
148 { return std::set<std::string>(); }
149 // Trigger interval in seconds
150 virtual float getTriggerInterval() = 0;
151 // Random chance of (1 / return value), 0 is disallowed
152 virtual u32 getTriggerChance() = 0;
153 // This is called usually at interval for 1/chance of the nodes
154 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
155 virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
156 u32 active_object_count, u32 active_object_count_wider){};
161 ActiveBlockModifier *abm;
164 ABMWithState(ActiveBlockModifier *abm_);
168 List of active blocks, used by ServerEnvironment
171 class ActiveBlockList
174 void update(std::list<v3s16> &active_positions,
176 std::set<v3s16> &blocks_removed,
177 std::set<v3s16> &blocks_added);
179 bool contains(v3s16 p){
180 return (m_list.find(p) != m_list.end());
187 std::set<v3s16> m_list;
188 std::set<v3s16> m_forceloaded_list;
194 The server-side environment.
196 This is not thread-safe. Server uses an environment mutex.
199 class ServerEnvironment : public Environment
202 ServerEnvironment(ServerMap *map, GameScripting *scriptIface,
203 IGameDef *gamedef, const std::string &path_world);
204 ~ServerEnvironment();
208 ServerMap & getServerMap();
210 //TODO find way to remove this fct!
211 GameScripting* getScriptIface()
214 IGameDef *getGameDef()
215 { return m_gamedef; }
217 float getSendRecommendedInterval()
218 { return m_recommended_send_interval; }
221 void saveLoadedPlayers();
222 void savePlayer(const std::string &playername);
223 Player *loadPlayer(const std::string &playername);
226 Save and load time of day and game timer
232 External ActiveObject interface
233 -------------------------------------------
236 ServerActiveObject* getActiveObject(u16 id);
239 Add an active object to the environment.
240 Environment handles deletion of object.
241 Object may be deleted by environment immediately.
242 If id of object is 0, assigns a free id to it.
243 Returns the id of the object.
244 Returns 0 if not added and thus deleted.
246 u16 addActiveObject(ServerActiveObject *object);
249 Add an active object as a static object to the corresponding
251 Caller allocates memory, ServerEnvironment frees memory.
252 Return value: true if succeeded, false if failed.
253 (note: not used, pending removal from engine)
255 //bool addActiveObjectAsStatic(ServerActiveObject *object);
258 Find out what new objects have been added to
259 inside a radius around a position
261 void getAddedActiveObjects(v3s16 pos, s16 radius,
263 std::set<u16> ¤t_objects,
264 std::set<u16> &added_objects);
267 Find out what new objects have been removed from
268 inside a radius around a position
270 void getRemovedActiveObjects(v3s16 pos, s16 radius,
272 std::set<u16> ¤t_objects,
273 std::set<u16> &removed_objects);
276 Get the next message emitted by some active object.
277 Returns a message with id=0 if no messages are available.
279 ActiveObjectMessage getActiveObjectMessage();
282 Activate objects and dynamically modify for the dtime determined
283 from timestamp and additional_dtime
285 void activateBlock(MapBlock *block, u32 additional_dtime=0);
289 -------------------------------------------
292 void addActiveBlockModifier(ActiveBlockModifier *abm);
296 -------------------------------------------
299 // Script-aware node setters
300 bool setNode(v3s16 p, const MapNode &n);
301 bool removeNode(v3s16 p);
302 bool swapNode(v3s16 p, const MapNode &n);
304 // Find all active objects inside a radius around a point
305 std::set<u16> getObjectsInsideRadius(v3f pos, float radius);
307 // Clear all objects, loading and going through every MapBlock
308 void clearAllObjects();
310 // This makes stuff happen
311 void step(f32 dtime);
313 //check if there's a line of sight between two positions
314 bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0, v3s16 *p=NULL);
316 u32 getGameTime() { return m_game_time; }
318 void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
319 float getMaxLagEstimate() { return m_max_lag_estimate; }
321 std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; };
326 Internal ActiveObject interface
327 -------------------------------------------
331 Add an active object to the environment.
333 Called by addActiveObject.
335 Object may be deleted by environment immediately.
336 If id of object is 0, assigns a free id to it.
337 Returns the id of the object.
338 Returns 0 if not added and thus deleted.
340 u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
343 Remove all objects that satisfy (m_removed && m_known_by_count==0)
345 void removeRemovedObjects();
348 Convert stored objects from block to active
350 void activateObjects(MapBlock *block, u32 dtime_s);
353 Convert objects that are not in active blocks to static.
355 If m_known_by_count != 0, active object is not deleted, but static
356 data is still updated.
358 If force_delete is set, active object is deleted nevertheless. It
359 shall only be set so in the destructor of the environment.
361 void deactivateFarObjects(bool force_delete);
370 GameScripting* m_script;
374 const std::string m_path_world;
375 // Active object list
376 std::map<u16, ServerActiveObject*> m_active_objects;
377 // Outgoing network message buffer for active objects
378 std::list<ActiveObjectMessage> m_active_object_messages;
380 float m_send_recommended_timer;
381 IntervalLimiter m_object_management_interval;
382 // List of active blocks
383 ActiveBlockList m_active_blocks;
384 IntervalLimiter m_active_blocks_management_interval;
385 IntervalLimiter m_active_block_modifier_interval;
386 IntervalLimiter m_active_blocks_nodemetadata_interval;
387 int m_active_block_interval_overload_skip;
388 // Time from the beginning of the game in seconds.
389 // Incremented in step().
391 // A helper variable for incrementing the latter
392 float m_game_time_fraction_counter;
393 std::list<ABMWithState> m_abms;
394 // An interval for generally sending object positions and stuff
395 float m_recommended_send_interval;
396 // Estimate for general maximum lag as determined by server.
397 // Can raise to high values like 15s with eg. map generation mods.
398 float m_max_lag_estimate;
403 #include "clientobject.h"
404 class ClientSimpleObject;
407 The client-side environment.
409 This is not thread-safe.
410 Must be called from main (irrlicht) thread (uses the SceneManager)
411 Client uses an environment mutex.
414 enum ClientEnvEventType
421 struct ClientEnvEvent
423 ClientEnvEventType type;
437 class ClientEnvironment : public Environment
440 ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
441 ITextureSource *texturesource, IGameDef *gamedef,
442 IrrlichtDevice *device);
443 ~ClientEnvironment();
446 ClientMap & getClientMap();
448 IGameDef *getGameDef()
449 { return m_gamedef; }
451 void step(f32 dtime);
453 virtual void addPlayer(Player *player);
454 LocalPlayer * getLocalPlayer();
460 void addSimpleObject(ClientSimpleObject *simple);
466 ClientActiveObject* getActiveObject(u16 id);
469 Adds an active object to the environment.
470 Environment handles deletion of object.
471 Object may be deleted by environment immediately.
472 If id of object is 0, assigns a free id to it.
473 Returns the id of the object.
474 Returns 0 if not added and thus deleted.
476 u16 addActiveObject(ClientActiveObject *object);
478 void addActiveObject(u16 id, u8 type, const std::string &init_data);
479 void removeActiveObject(u16 id);
481 void processActiveObjectMessage(u16 id, const std::string &data);
484 Callbacks for activeobjects
487 void damageLocalPlayer(u8 damage, bool handle_hp=true);
488 void updateLocalPlayerBreath(u16 breath);
491 Client likes to call these
494 // Get all nearby objects
495 void getActiveObjects(v3f origin, f32 max_d,
496 std::vector<DistanceSortedActiveObject> &dest);
498 // Get event from queue. CEE_NONE is returned if queue is empty.
499 ClientEnvEvent getClientEvent();
501 u16 m_attachements[USHRT_MAX];
503 std::list<std::string> getPlayerNames()
504 { return m_player_names; }
505 void addPlayerName(std::string name)
506 { m_player_names.push_back(name); }
507 void removePlayerName(std::string name)
508 { m_player_names.remove(name); }
509 void updateCameraOffset(v3s16 camera_offset)
510 { m_camera_offset = camera_offset; }
511 v3s16 getCameraOffset()
512 { return m_camera_offset; }
516 scene::ISceneManager *m_smgr;
517 ITextureSource *m_texturesource;
519 IrrlichtDevice *m_irr;
520 std::map<u16, ClientActiveObject*> m_active_objects;
521 std::list<ClientSimpleObject*> m_simple_objects;
522 std::list<ClientEnvEvent> m_client_event_queue;
523 IntervalLimiter m_active_object_light_update_interval;
524 IntervalLimiter m_lava_hurt_interval;
525 IntervalLimiter m_drowning_interval;
526 IntervalLimiter m_breathing_interval;
527 std::list<std::string> m_player_names;
528 v3s16 m_camera_offset;