environment.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #pragma once
  17. /*
  18. This class is the game's environment.
  19. It contains:
  20. - The map
  21. - Players
  22. - Other objects
  23. - The current time in the game
  24. - etc.
  25. */
  26. #include <list>
  27. #include <queue>
  28. #include <map>
  29. #include <atomic>
  30. #include <mutex>
  31. #include <optional>
  32. #include "irr_v3d.h"
  33. #include "util/basic_macros.h"
  34. #include "line3d.h"
  35. class IGameDef;
  36. class Map;
  37. struct PointedThing;
  38. class RaycastState;
  39. struct Pointabilities;
  40. class Environment
  41. {
  42. public:
  43. // Environment will delete the map passed to the constructor
  44. Environment(IGameDef *gamedef);
  45. virtual ~Environment() = default;
  46. DISABLE_CLASS_COPY(Environment);
  47. /*
  48. Step everything in environment.
  49. - Move players
  50. - Step mobs
  51. - Run timers of map
  52. */
  53. virtual void step(f32 dtime) = 0;
  54. virtual Map &getMap() = 0;
  55. u32 getDayNightRatio();
  56. // 0-23999
  57. virtual void setTimeOfDay(u32 time);
  58. u32 getTimeOfDay();
  59. float getTimeOfDayF();
  60. void stepTimeOfDay(float dtime);
  61. void setTimeOfDaySpeed(float speed);
  62. void setDayNightRatioOverride(bool enable, u32 value);
  63. u32 getDayCount();
  64. /*!
  65. * Returns false if the given line intersects with a
  66. * non-air node, true otherwise.
  67. * \param pos1 start of the line
  68. * \param pos2 end of the line
  69. * \param p output, position of the first non-air node
  70. * the line intersects
  71. */
  72. bool line_of_sight(v3f pos1, v3f pos2, v3s16 *p = nullptr);
  73. /*!
  74. * Gets the objects pointed by the shootline as
  75. * pointed things.
  76. * If this is a client environment, the local player
  77. * won't be returned.
  78. * @param[in] shootline_on_map the shootline for
  79. * the test in world coordinates
  80. *
  81. * @param[out] objects found objects
  82. */
  83. virtual void getSelectedActiveObjects(const core::line3d<f32> &shootline_on_map,
  84. std::vector<PointedThing> &objects,
  85. const std::optional<Pointabilities> &pointabilities) = 0;
  86. /*!
  87. * Returns the next node or object the shootline meets.
  88. * @param state current state of the raycast
  89. * @result output, will contain the next pointed thing
  90. */
  91. void continueRaycast(RaycastState *state, PointedThing *result);
  92. // counter used internally when triggering ABMs
  93. u32 m_added_objects;
  94. IGameDef *getGameDef() { return m_gamedef; }
  95. protected:
  96. std::atomic<float> m_time_of_day_speed;
  97. /*
  98. * Below: values managed by m_time_lock
  99. */
  100. // Time of day in milli-hours (0-23999), determines day and night
  101. u32 m_time_of_day;
  102. // Time of day in 0...1
  103. float m_time_of_day_f;
  104. // Stores the skew created by the float -> u32 conversion
  105. // to be applied at next conversion, so that there is no real skew.
  106. float m_time_conversion_skew = 0.0f;
  107. // Overriding the day-night ratio is useful for custom sky visuals
  108. bool m_enable_day_night_ratio_override = false;
  109. u32 m_day_night_ratio_override = 0.0f;
  110. // Days from the server start, accounts for time shift
  111. // in game (e.g. /time or bed usage)
  112. std::atomic<u32> m_day_count;
  113. /*
  114. * Above: values managed by m_time_lock
  115. */
  116. /* TODO: Add a callback function so these can be updated when a setting
  117. * changes. At this point in time it doesn't matter (e.g. /set
  118. * is documented to change server settings only)
  119. *
  120. * TODO: Local caching of settings is not optimal and should at some stage
  121. * be updated to use a global settings object for getting thse values
  122. * (as opposed to the this local caching). This can be addressed in
  123. * a later release.
  124. */
  125. bool m_cache_enable_shaders;
  126. float m_cache_active_block_mgmt_interval;
  127. float m_cache_abm_interval;
  128. float m_cache_nodetimer_interval;
  129. float m_cache_abm_time_budget;
  130. IGameDef *m_gamedef;
  131. private:
  132. std::mutex m_time_lock;
  133. };