Minetest  5.4.0
environment.h
Go to the documentation of this file.
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 
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.
9 
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.
14 
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.
18 */
19 
20 #pragma once
21 
22 /*
23  This class is the game's environment.
24  It contains:
25  - The map
26  - Players
27  - Other objects
28  - The current time in the game
29  - etc.
30 */
31 
32 #include <list>
33 #include <queue>
34 #include <map>
35 #include <atomic>
36 #include <mutex>
37 #include "irr_v3d.h"
38 #include "network/networkprotocol.h" // for AccessDeniedCode
39 #include "util/basic_macros.h"
40 
41 class IGameDef;
42 class Map;
43 struct PointedThing;
44 class RaycastState;
45 
47 {
48 public:
49  // Environment will delete the map passed to the constructor
50  Environment(IGameDef *gamedef);
51  virtual ~Environment() = default;
53 
54  /*
55  Step everything in environment.
56  - Move players
57  - Step mobs
58  - Run timers of map
59  */
60  virtual void step(f32 dtime) = 0;
61 
62  virtual Map &getMap() = 0;
63 
64  u32 getDayNightRatio();
65 
66  // 0-23999
67  virtual void setTimeOfDay(u32 time);
68  u32 getTimeOfDay();
69  float getTimeOfDayF();
70 
71  void stepTimeOfDay(float dtime);
72 
73  void setTimeOfDaySpeed(float speed);
74 
75  void setDayNightRatioOverride(bool enable, u32 value);
76 
77  u32 getDayCount();
78 
87  bool line_of_sight(v3f pos1, v3f pos2, v3s16 *p = nullptr);
88 
99  virtual void getSelectedActiveObjects(const core::line3d<f32> &shootline_on_map,
100  std::vector<PointedThing> &objects) = 0;
101 
107  void continueRaycast(RaycastState *state, PointedThing *result);
108 
109  // counter used internally when triggering ABMs
111 
113 
114 protected:
115  std::atomic<float> m_time_of_day_speed;
116 
117  /*
118  * Below: values managed by m_time_lock
119  */
120  // Time of day in milli-hours (0-23999), determines day and night
122  // Time of day in 0...1
124  // Stores the skew created by the float -> u32 conversion
125  // to be applied at next conversion, so that there is no real skew.
127  // Overriding the day-night ratio is useful for custom sky visuals
130  // Days from the server start, accounts for time shift
131  // in game (e.g. /time or bed usage)
132  std::atomic<u32> m_day_count;
133  /*
134  * Above: values managed by m_time_lock
135  */
136 
137  /* TODO: Add a callback function so these can be updated when a setting
138  * changes. At this point in time it doesn't matter (e.g. /set
139  * is documented to change server settings only)
140  *
141  * TODO: Local caching of settings is not optimal and should at some stage
142  * be updated to use a global settings object for getting thse values
143  * (as opposed to the this local caching). This can be addressed in
144  * a later release.
145  */
151 
153 
154 private:
155  std::mutex m_time_lock;
156 };
Definition: environment.h:47
float m_cache_active_block_mgmt_interval
Definition: environment.h:147
u32 m_added_objects
Definition: environment.h:110
virtual void getSelectedActiveObjects(const core::line3d< f32 > &shootline_on_map, std::vector< PointedThing > &objects)=0
virtual ~Environment()=default
void setDayNightRatioOverride(bool enable, u32 value)
Definition: environment.cpp:58
IGameDef * m_gamedef
Definition: environment.h:152
std::atomic< float > m_time_of_day_speed
Definition: environment.h:115
virtual void step(f32 dtime)=0
u32 m_time_of_day
Definition: environment.h:121
bool m_enable_day_night_ratio_override
Definition: environment.h:128
virtual Map & getMap()=0
u32 getDayNightRatio()
Definition: environment.cpp:45
std::atomic< u32 > m_day_count
Definition: environment.h:132
u32 getTimeOfDay()
Definition: environment.cpp:74
float m_time_conversion_skew
Definition: environment.h:126
IGameDef * getGameDef()
Definition: environment.h:112
DISABLE_CLASS_COPY(Environment)
float m_time_of_day_f
Definition: environment.h:123
float m_cache_abm_time_budget
Definition: environment.h:150
u32 m_day_night_ratio_override
Definition: environment.h:129
std::mutex m_time_lock
Definition: environment.h:155
void setTimeOfDaySpeed(float speed)
Definition: environment.cpp:53
bool m_cache_enable_shaders
Definition: environment.h:146
Environment(IGameDef *gamedef)
Definition: environment.cpp:30
bool line_of_sight(v3f pos1, v3f pos2, v3s16 *p=nullptr)
Definition: environment.cpp:86
u32 getDayCount()
Definition: environment.cpp:314
float m_cache_nodetimer_interval
Definition: environment.h:149
float m_cache_abm_interval
Definition: environment.h:148
void stepTimeOfDay(float dtime)
Definition: environment.cpp:280
virtual void setTimeOfDay(u32 time)
Definition: environment.cpp:65
float getTimeOfDayF()
Definition: environment.cpp:80
void continueRaycast(RaycastState *state, PointedThing *result)
Definition: environment.cpp:115
Definition: gamedef.h:49
Definition: map.h:123
Describes the state of a raycast.
Definition: raycast.h:33
core::vector3d< s16 > v3s16
Definition: irr_v3d.h:28
core::vector3df v3f
Definition: irr_v3d.h:26
An active object or node which is selected by a ray on the map.
Definition: pointedthing.h:36
std::string p(std::string path)
Definition: test_filepath.cpp:59