environment.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef ENVIRONMENT_HEADER
  17. #define ENVIRONMENT_HEADER
  18. /*
  19. This class is the game's environment.
  20. It contains:
  21. - The map
  22. - Players
  23. - Other objects
  24. - The current time in the game
  25. - etc.
  26. */
  27. #include <set>
  28. #include <list>
  29. #include <queue>
  30. #include <map>
  31. #include "irr_v3d.h"
  32. #include "activeobject.h"
  33. #include "util/numeric.h"
  34. #include "mapnode.h"
  35. #include "mapblock.h"
  36. #include "threading/mutex.h"
  37. #include "threading/atomic.h"
  38. #include "network/networkprotocol.h" // for AccessDeniedCode
  39. class ServerEnvironment;
  40. class ActiveBlockModifier;
  41. class ServerActiveObject;
  42. class ITextureSource;
  43. class IGameDef;
  44. class Map;
  45. class ServerMap;
  46. class ClientMap;
  47. class GameScripting;
  48. class Player;
  49. class RemotePlayer;
  50. class PlayerSAO;
  51. class Environment
  52. {
  53. public:
  54. // Environment will delete the map passed to the constructor
  55. Environment();
  56. virtual ~Environment();
  57. /*
  58. Step everything in environment.
  59. - Move players
  60. - Step mobs
  61. - Run timers of map
  62. */
  63. virtual void step(f32 dtime) = 0;
  64. virtual Map & getMap() = 0;
  65. u32 getDayNightRatio();
  66. // 0-23999
  67. virtual void setTimeOfDay(u32 time);
  68. u32 getTimeOfDay();
  69. float getTimeOfDayF();
  70. void stepTimeOfDay(float dtime);
  71. void setTimeOfDaySpeed(float speed);
  72. void setDayNightRatioOverride(bool enable, u32 value);
  73. u32 getDayCount();
  74. // counter used internally when triggering ABMs
  75. u32 m_added_objects;
  76. protected:
  77. GenericAtomic<float> m_time_of_day_speed;
  78. /*
  79. * Below: values managed by m_time_lock
  80. */
  81. // Time of day in milli-hours (0-23999); determines day and night
  82. u32 m_time_of_day;
  83. // Time of day in 0...1
  84. float m_time_of_day_f;
  85. // Stores the skew created by the float -> u32 conversion
  86. // to be applied at next conversion, so that there is no real skew.
  87. float m_time_conversion_skew;
  88. // Overriding the day-night ratio is useful for custom sky visuals
  89. bool m_enable_day_night_ratio_override;
  90. u32 m_day_night_ratio_override;
  91. // Days from the server start, accounts for time shift
  92. // in game (e.g. /time or bed usage)
  93. Atomic<u32> m_day_count;
  94. /*
  95. * Above: values managed by m_time_lock
  96. */
  97. /* TODO: Add a callback function so these can be updated when a setting
  98. * changes. At this point in time it doesn't matter (e.g. /set
  99. * is documented to change server settings only)
  100. *
  101. * TODO: Local caching of settings is not optimal and should at some stage
  102. * be updated to use a global settings object for getting thse values
  103. * (as opposed to the this local caching). This can be addressed in
  104. * a later release.
  105. */
  106. bool m_cache_enable_shaders;
  107. float m_cache_active_block_mgmt_interval;
  108. float m_cache_abm_interval;
  109. float m_cache_nodetimer_interval;
  110. private:
  111. Mutex m_time_lock;
  112. DISABLE_CLASS_COPY(Environment);
  113. };
  114. /*
  115. {Active, Loading} block modifier interface.
  116. These are fed into ServerEnvironment at initialization time;
  117. ServerEnvironment handles deleting them.
  118. */
  119. class ActiveBlockModifier
  120. {
  121. public:
  122. ActiveBlockModifier(){};
  123. virtual ~ActiveBlockModifier(){};
  124. // Set of contents to trigger on
  125. virtual std::set<std::string> getTriggerContents()=0;
  126. // Set of required neighbors (trigger doesn't happen if none are found)
  127. // Empty = do not check neighbors
  128. virtual std::set<std::string> getRequiredNeighbors()
  129. { return std::set<std::string>(); }
  130. // Trigger interval in seconds
  131. virtual float getTriggerInterval() = 0;
  132. // Random chance of (1 / return value), 0 is disallowed
  133. virtual u32 getTriggerChance() = 0;
  134. // Whether to modify chance to simulate time lost by an unnattended block
  135. virtual bool getSimpleCatchUp() = 0;
  136. // This is called usually at interval for 1/chance of the nodes
  137. virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
  138. virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n,
  139. u32 active_object_count, u32 active_object_count_wider){};
  140. };
  141. struct ABMWithState
  142. {
  143. ActiveBlockModifier *abm;
  144. float timer;
  145. ABMWithState(ActiveBlockModifier *abm_);
  146. };
  147. struct LoadingBlockModifierDef
  148. {
  149. // Set of contents to trigger on
  150. std::set<std::string> trigger_contents;
  151. std::string name;
  152. bool run_at_every_load;
  153. virtual ~LoadingBlockModifierDef() {}
  154. virtual void trigger(ServerEnvironment *env, v3s16 p, MapNode n){};
  155. };
  156. struct LBMContentMapping
  157. {
  158. typedef std::map<content_t, std::vector<LoadingBlockModifierDef *> > container_map;
  159. container_map map;
  160. std::vector<LoadingBlockModifierDef *> lbm_list;
  161. // Needs to be separate method (not inside destructor),
  162. // because the LBMContentMapping may be copied and destructed
  163. // many times during operation in the lbm_lookup_map.
  164. void deleteContents();
  165. void addLBM(LoadingBlockModifierDef *lbm_def, IGameDef *gamedef);
  166. const std::vector<LoadingBlockModifierDef *> *lookup(content_t c) const;
  167. };
  168. class LBMManager
  169. {
  170. public:
  171. LBMManager():
  172. m_query_mode(false)
  173. {}
  174. ~LBMManager();
  175. // Don't call this after loadIntroductionTimes() ran.
  176. void addLBMDef(LoadingBlockModifierDef *lbm_def);
  177. void loadIntroductionTimes(const std::string &times,
  178. IGameDef *gamedef, u32 now);
  179. // Don't call this before loadIntroductionTimes() ran.
  180. std::string createIntroductionTimesString();
  181. // Don't call this before loadIntroductionTimes() ran.
  182. void applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp);
  183. // Warning: do not make this std::unordered_map, order is relevant here
  184. typedef std::map<u32, LBMContentMapping> lbm_lookup_map;
  185. private:
  186. // Once we set this to true, we can only query,
  187. // not modify
  188. bool m_query_mode;
  189. // For m_query_mode == false:
  190. // The key of the map is the LBM def's name.
  191. // TODO make this std::unordered_map
  192. std::map<std::string, LoadingBlockModifierDef *> m_lbm_defs;
  193. // For m_query_mode == true:
  194. // The key of the map is the LBM def's first introduction time.
  195. lbm_lookup_map m_lbm_lookup;
  196. // Returns an iterator to the LBMs that were introduced
  197. // after the given time. This is guaranteed to return
  198. // valid values for everything
  199. lbm_lookup_map::const_iterator getLBMsIntroducedAfter(u32 time)
  200. { return m_lbm_lookup.lower_bound(time); }
  201. };
  202. /*
  203. List of active blocks, used by ServerEnvironment
  204. */
  205. class ActiveBlockList
  206. {
  207. public:
  208. void update(std::vector<v3s16> &active_positions,
  209. s16 radius,
  210. std::set<v3s16> &blocks_removed,
  211. std::set<v3s16> &blocks_added);
  212. bool contains(v3s16 p){
  213. return (m_list.find(p) != m_list.end());
  214. }
  215. void clear(){
  216. m_list.clear();
  217. }
  218. std::set<v3s16> m_list;
  219. std::set<v3s16> m_forceloaded_list;
  220. private:
  221. };
  222. /*
  223. Operation mode for ServerEnvironment::clearObjects()
  224. */
  225. enum ClearObjectsMode {
  226. // Load and go through every mapblock, clearing objects
  227. CLEAR_OBJECTS_MODE_FULL,
  228. // Clear objects immediately in loaded mapblocks;
  229. // clear objects in unloaded mapblocks only when the mapblocks are next activated.
  230. CLEAR_OBJECTS_MODE_QUICK,
  231. };
  232. /*
  233. The server-side environment.
  234. This is not thread-safe. Server uses an environment mutex.
  235. */
  236. typedef UNORDERED_MAP<u16, ServerActiveObject *> ActiveObjectMap;
  237. class ServerEnvironment : public Environment
  238. {
  239. public:
  240. ServerEnvironment(ServerMap *map, GameScripting *scriptIface,
  241. IGameDef *gamedef, const std::string &path_world);
  242. ~ServerEnvironment();
  243. Map & getMap();
  244. ServerMap & getServerMap();
  245. //TODO find way to remove this fct!
  246. GameScripting* getScriptIface()
  247. { return m_script; }
  248. IGameDef *getGameDef()
  249. { return m_gamedef; }
  250. float getSendRecommendedInterval()
  251. { return m_recommended_send_interval; }
  252. void kickAllPlayers(AccessDeniedCode reason,
  253. const std::string &str_reason, bool reconnect);
  254. // Save players
  255. void saveLoadedPlayers();
  256. void savePlayer(RemotePlayer *player);
  257. RemotePlayer *loadPlayer(const std::string &playername, PlayerSAO *sao);
  258. void addPlayer(RemotePlayer *player);
  259. void removePlayer(RemotePlayer *player);
  260. /*
  261. Save and load time of day and game timer
  262. */
  263. void saveMeta();
  264. void loadMeta();
  265. // to be called instead of loadMeta if
  266. // env_meta.txt doesn't exist (e.g. new world)
  267. void loadDefaultMeta();
  268. u32 addParticleSpawner(float exptime);
  269. u32 addParticleSpawner(float exptime, u16 attached_id);
  270. void deleteParticleSpawner(u32 id, bool remove_from_object = true);
  271. /*
  272. External ActiveObject interface
  273. -------------------------------------------
  274. */
  275. ServerActiveObject* getActiveObject(u16 id);
  276. /*
  277. Add an active object to the environment.
  278. Environment handles deletion of object.
  279. Object may be deleted by environment immediately.
  280. If id of object is 0, assigns a free id to it.
  281. Returns the id of the object.
  282. Returns 0 if not added and thus deleted.
  283. */
  284. u16 addActiveObject(ServerActiveObject *object);
  285. /*
  286. Add an active object as a static object to the corresponding
  287. MapBlock.
  288. Caller allocates memory, ServerEnvironment frees memory.
  289. Return value: true if succeeded, false if failed.
  290. (note: not used, pending removal from engine)
  291. */
  292. //bool addActiveObjectAsStatic(ServerActiveObject *object);
  293. /*
  294. Find out what new objects have been added to
  295. inside a radius around a position
  296. */
  297. void getAddedActiveObjects(PlayerSAO *playersao, s16 radius,
  298. s16 player_radius,
  299. std::set<u16> &current_objects,
  300. std::queue<u16> &added_objects);
  301. /*
  302. Find out what new objects have been removed from
  303. inside a radius around a position
  304. */
  305. void getRemovedActiveObjects(PlayerSAO *playersao, s16 radius,
  306. s16 player_radius,
  307. std::set<u16> &current_objects,
  308. std::queue<u16> &removed_objects);
  309. /*
  310. Get the next message emitted by some active object.
  311. Returns a message with id=0 if no messages are available.
  312. */
  313. ActiveObjectMessage getActiveObjectMessage();
  314. /*
  315. Activate objects and dynamically modify for the dtime determined
  316. from timestamp and additional_dtime
  317. */
  318. void activateBlock(MapBlock *block, u32 additional_dtime=0);
  319. /*
  320. {Active,Loading}BlockModifiers
  321. -------------------------------------------
  322. */
  323. void addActiveBlockModifier(ActiveBlockModifier *abm);
  324. void addLoadingBlockModifierDef(LoadingBlockModifierDef *lbm);
  325. /*
  326. Other stuff
  327. -------------------------------------------
  328. */
  329. // Script-aware node setters
  330. bool setNode(v3s16 p, const MapNode &n);
  331. bool removeNode(v3s16 p);
  332. bool swapNode(v3s16 p, const MapNode &n);
  333. // Find all active objects inside a radius around a point
  334. void getObjectsInsideRadius(std::vector<u16> &objects, v3f pos, float radius);
  335. // Clear objects, loading and going through every MapBlock
  336. void clearObjects(ClearObjectsMode mode);
  337. // This makes stuff happen
  338. void step(f32 dtime);
  339. //check if there's a line of sight between two positions
  340. bool line_of_sight(v3f pos1, v3f pos2, float stepsize=1.0, v3s16 *p=NULL);
  341. u32 getGameTime() { return m_game_time; }
  342. void reportMaxLagEstimate(float f) { m_max_lag_estimate = f; }
  343. float getMaxLagEstimate() { return m_max_lag_estimate; }
  344. std::set<v3s16>* getForceloadedBlocks() { return &m_active_blocks.m_forceloaded_list; };
  345. // Sets the static object status all the active objects in the specified block
  346. // This is only really needed for deleting blocks from the map
  347. void setStaticForActiveObjectsInBlock(v3s16 blockpos,
  348. bool static_exists, v3s16 static_block=v3s16(0,0,0));
  349. RemotePlayer *getPlayer(const u16 peer_id);
  350. RemotePlayer *getPlayer(const char* name);
  351. private:
  352. /*
  353. Internal ActiveObject interface
  354. -------------------------------------------
  355. */
  356. /*
  357. Add an active object to the environment.
  358. Called by addActiveObject.
  359. Object may be deleted by environment immediately.
  360. If id of object is 0, assigns a free id to it.
  361. Returns the id of the object.
  362. Returns 0 if not added and thus deleted.
  363. */
  364. u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
  365. /*
  366. Remove all objects that satisfy (m_removed && m_known_by_count==0)
  367. */
  368. void removeRemovedObjects();
  369. /*
  370. Convert stored objects from block to active
  371. */
  372. void activateObjects(MapBlock *block, u32 dtime_s);
  373. /*
  374. Convert objects that are not in active blocks to static.
  375. If m_known_by_count != 0, active object is not deleted, but static
  376. data is still updated.
  377. If force_delete is set, active object is deleted nevertheless. It
  378. shall only be set so in the destructor of the environment.
  379. */
  380. void deactivateFarObjects(bool force_delete);
  381. /*
  382. Member variables
  383. */
  384. // The map
  385. ServerMap *m_map;
  386. // Lua state
  387. GameScripting* m_script;
  388. // Game definition
  389. IGameDef *m_gamedef;
  390. // World path
  391. const std::string m_path_world;
  392. // Active object list
  393. ActiveObjectMap m_active_objects;
  394. // Outgoing network message buffer for active objects
  395. std::queue<ActiveObjectMessage> m_active_object_messages;
  396. // Some timers
  397. float m_send_recommended_timer;
  398. IntervalLimiter m_object_management_interval;
  399. // List of active blocks
  400. ActiveBlockList m_active_blocks;
  401. IntervalLimiter m_active_blocks_management_interval;
  402. IntervalLimiter m_active_block_modifier_interval;
  403. IntervalLimiter m_active_blocks_nodemetadata_interval;
  404. int m_active_block_interval_overload_skip;
  405. // Time from the beginning of the game in seconds.
  406. // Incremented in step().
  407. u32 m_game_time;
  408. // A helper variable for incrementing the latter
  409. float m_game_time_fraction_counter;
  410. // Time of last clearObjects call (game time).
  411. // When a mapblock older than this is loaded, its objects are cleared.
  412. u32 m_last_clear_objects_time;
  413. // Active block modifiers
  414. std::vector<ABMWithState> m_abms;
  415. LBMManager m_lbm_mgr;
  416. // An interval for generally sending object positions and stuff
  417. float m_recommended_send_interval;
  418. // Estimate for general maximum lag as determined by server.
  419. // Can raise to high values like 15s with eg. map generation mods.
  420. float m_max_lag_estimate;
  421. // peer_ids in here should be unique, except that there may be many 0s
  422. std::vector<RemotePlayer*> m_players;
  423. // Particles
  424. IntervalLimiter m_particle_management_interval;
  425. UNORDERED_MAP<u32, float> m_particle_spawners;
  426. UNORDERED_MAP<u32, u16> m_particle_spawner_attachments;
  427. };
  428. #ifndef SERVER
  429. #include "clientobject.h"
  430. #include "content_cao.h"
  431. class ClientSimpleObject;
  432. /*
  433. The client-side environment.
  434. This is not thread-safe.
  435. Must be called from main (irrlicht) thread (uses the SceneManager)
  436. Client uses an environment mutex.
  437. */
  438. enum ClientEnvEventType
  439. {
  440. CEE_NONE,
  441. CEE_PLAYER_DAMAGE,
  442. CEE_PLAYER_BREATH
  443. };
  444. struct ClientEnvEvent
  445. {
  446. ClientEnvEventType type;
  447. union {
  448. //struct{
  449. //} none;
  450. struct{
  451. u8 amount;
  452. bool send_to_server;
  453. } player_damage;
  454. struct{
  455. u16 amount;
  456. } player_breath;
  457. };
  458. };
  459. class ClientEnvironment : public Environment
  460. {
  461. public:
  462. ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
  463. ITextureSource *texturesource, IGameDef *gamedef,
  464. IrrlichtDevice *device);
  465. ~ClientEnvironment();
  466. Map & getMap();
  467. ClientMap & getClientMap();
  468. IGameDef *getGameDef()
  469. { return m_gamedef; }
  470. void step(f32 dtime);
  471. virtual void setLocalPlayer(LocalPlayer *player);
  472. LocalPlayer *getLocalPlayer() { return m_local_player; }
  473. /*
  474. ClientSimpleObjects
  475. */
  476. void addSimpleObject(ClientSimpleObject *simple);
  477. /*
  478. ActiveObjects
  479. */
  480. GenericCAO* getGenericCAO(u16 id);
  481. ClientActiveObject* getActiveObject(u16 id);
  482. /*
  483. Adds an active object to the environment.
  484. Environment handles deletion of object.
  485. Object may be deleted by environment immediately.
  486. If id of object is 0, assigns a free id to it.
  487. Returns the id of the object.
  488. Returns 0 if not added and thus deleted.
  489. */
  490. u16 addActiveObject(ClientActiveObject *object);
  491. void addActiveObject(u16 id, u8 type, const std::string &init_data);
  492. void removeActiveObject(u16 id);
  493. void processActiveObjectMessage(u16 id, const std::string &data);
  494. /*
  495. Callbacks for activeobjects
  496. */
  497. void damageLocalPlayer(u8 damage, bool handle_hp=true);
  498. void updateLocalPlayerBreath(u16 breath);
  499. /*
  500. Client likes to call these
  501. */
  502. // Get all nearby objects
  503. void getActiveObjects(v3f origin, f32 max_d,
  504. std::vector<DistanceSortedActiveObject> &dest);
  505. // Get event from queue. CEE_NONE is returned if queue is empty.
  506. ClientEnvEvent getClientEvent();
  507. u16 attachement_parent_ids[USHRT_MAX + 1];
  508. const std::list<std::string> &getPlayerNames() { return m_player_names; }
  509. void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
  510. void removePlayerName(const std::string &name) { m_player_names.remove(name); }
  511. void updateCameraOffset(v3s16 camera_offset)
  512. { m_camera_offset = camera_offset; }
  513. v3s16 getCameraOffset() const { return m_camera_offset; }
  514. private:
  515. ClientMap *m_map;
  516. LocalPlayer *m_local_player;
  517. scene::ISceneManager *m_smgr;
  518. ITextureSource *m_texturesource;
  519. IGameDef *m_gamedef;
  520. IrrlichtDevice *m_irr;
  521. UNORDERED_MAP<u16, ClientActiveObject*> m_active_objects;
  522. std::vector<ClientSimpleObject*> m_simple_objects;
  523. std::queue<ClientEnvEvent> m_client_event_queue;
  524. IntervalLimiter m_active_object_light_update_interval;
  525. IntervalLimiter m_lava_hurt_interval;
  526. IntervalLimiter m_drowning_interval;
  527. IntervalLimiter m_breathing_interval;
  528. std::list<std::string> m_player_names;
  529. v3s16 m_camera_offset;
  530. };
  531. #endif
  532. #endif