environment.h 4.2 KB

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