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