Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
serverenvironment.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2017 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <memory> // std::unique_ptr
8#include <set>
9#include <unordered_map>
10#include <utility> // std::function
11#include <vector>
12
13#include "environment.h"
14#include "util/guid.h"
15#include "map.h" // MapEventReceiver
18#include "util/numeric.h"
19#include "util/metricsbackend.h"
20
21class AuthDatabase;
22class ActiveObject;
23class MetricsBackend;
24class PlayerDatabase;
25class PlayerSAO;
26class RemotePlayer;
27class Server;
30class ServerScripting;
31class Settings;
33struct GameParams;
34struct StaticObject;
35
36class ServerMap;
37
38enum AccessDeniedCode : u8;
39typedef u16 session_t;
40
41/*
42 List of active blocks, used by ServerEnvironment
43*/
44
46{
47public:
48 void update(std::vector<PlayerSAO*> &active_players,
49 s16 active_block_range,
50 s16 active_object_range,
51 std::set<v3s16> &blocks_removed,
52 std::set<v3s16> &blocks_added,
53 std::set<v3s16> &extra_blocks_added);
54
55 bool contains(v3s16 p) const {
56 return (m_list.find(p) != m_list.end());
57 }
58
59 auto size() const {
60 return m_list.size();
61 }
62
63 void clear() {
64 m_list.clear();
65 }
66
68 bool add(v3s16 p) {
69 if (m_list.insert(p).second) {
70 m_abm_list.insert(p);
71 return true;
72 }
73 return false;
74 }
75
76 void remove(v3s16 p) {
77 m_list.erase(p);
78 m_abm_list.erase(p);
79 }
80
81 // list of all active blocks
82 std::set<v3s16> m_list;
83 // list of blocks for ABM processing
84 // subset of `m_list` that does not contain view cone affected blocks
85 std::set<v3s16> m_abm_list;
86 // list of blocks that are always active, not modified by this class
87 std::set<v3s16> m_forceloaded_list;
88};
89
90/*
91 ServerEnvironment::m_on_mapblocks_changed_receiver
92*/
94 std::unordered_set<v3s16> modified_blocks;
95 bool receiving = false;
96
97 void onMapEditEvent(const MapEditEvent &event) override;
98};
99
100/*
101 Operation mode for ServerEnvironment::clearObjects()
102*/
104 // Load and go through every mapblock, clearing objects
106
107 // Clear objects immediately in loaded mapblocks;
108 // clear objects in unloaded mapblocks only when the mapblocks are next activated.
110};
111
112class ServerEnvironment final : public Environment
113{
114public:
115 ServerEnvironment(std::unique_ptr<ServerMap> map, Server *server, MetricsBackend *mb);
117
118 void init();
119
120 Map & getMap();
121
123
124 //TODO find way to remove this fct!
127
129 { return m_server; }
130
133
136
137 // Save players
138 void saveLoadedPlayers(bool force = false);
139 void savePlayer(RemotePlayer *player);
140 std::unique_ptr<PlayerSAO> loadPlayer(RemotePlayer *player, session_t peer_id);
141 void addPlayer(RemotePlayer *player);
142 void removePlayer(RemotePlayer *player);
143 bool removePlayerFromDatabase(const std::string &name);
144
145 /*
146 Save and load time of day and game timer
147 */
148 void saveMeta();
149 void loadMeta();
150
151 u32 addParticleSpawner(float exptime);
152 u32 addParticleSpawner(float exptime, u16 attached_id);
153 void deleteParticleSpawner(u32 id, bool remove_from_object = true);
154
155 /*
156 External ActiveObject interface
157 -------------------------------------------
158 */
159
164
165 /*
166 Add an active object to the environment.
167 Environment handles deletion of object.
168 Object may be deleted by environment immediately.
169 If id of object is 0, assigns a free id to it.
170 Returns the id of the object.
171 Returns 0 if not added and thus deleted.
172 */
173 u16 addActiveObject(std::unique_ptr<ServerActiveObject> object);
174
176
177 /*
178 Find out what new objects have been added to
179 inside a radius around a position
180 */
181 void getAddedActiveObjects(PlayerSAO *playersao, s16 radius,
182 s16 player_radius,
183 const std::set<u16> &current_objects,
184 std::vector<u16> &added_objects);
185
186 /*
187 Find out what new objects have been removed from
188 inside a radius around a position
189 */
190 void getRemovedActiveObjects(PlayerSAO *playersao, s16 radius,
191 s16 player_radius,
192 const std::set<u16> &current_objects,
193 std::vector<std::pair<bool /* gone? */, u16>> &removed_objects);
194
195 /*
196 Get the next message emitted by some active object.
197 Returns false if no messages are available, true otherwise.
198 */
200
201 virtual void getSelectedActiveObjects(
202 const core::line3d<f32> &shootline_on_map,
203 std::vector<PointedThing> &objects,
204 const std::optional<Pointabilities> &pointabilities
205 );
206
207 /*
208 Force a block to become active. It will probably be deactivated
209 the next time active blocks are re-calculated.
210 */
211 void forceActivateBlock(MapBlock *block);
212
213 /*
214 {Active,Loading}BlockModifiers
215 -------------------------------------------
216 */
217
220
221 /*
222 Other stuff
223 -------------------------------------------
224 */
225
226 // Script-aware node setters
227 bool setNode(v3s16 p, const MapNode &n);
228 bool removeNode(v3s16 p);
229 bool swapNode(v3s16 p, const MapNode &n);
230
231 // Find the daylight value at pos with a Depth First Search
232 u8 findSunlight(v3s16 pos) const;
233
234 void updateObjectPos(u16 id, v3f pos)
235 {
236 return m_ao_manager.updateObjectPos(id, pos);
237 }
238
239 // Find all active objects inside a radius around a point
240 void getObjectsInsideRadius(std::vector<ServerActiveObject *> &objects, const v3f &pos, float radius,
241 std::function<bool(ServerActiveObject *obj)> include_obj_cb)
242 {
243 return m_ao_manager.getObjectsInsideRadius(pos, radius, objects, include_obj_cb);
244 }
245
246 // Find all active objects inside a box
247 void getObjectsInArea(std::vector<ServerActiveObject *> &objects, const aabb3f &box,
248 std::function<bool(ServerActiveObject *obj)> include_obj_cb)
249 {
250 return m_ao_manager.getObjectsInArea(box, objects, include_obj_cb);
251 }
252
253 // Clear objects, loading and going through every MapBlock
255
256 // to be called before destructor
258
259 // This makes stuff happen
260 void step(f32 dtime);
261
262 u32 getGameTime() const { return m_game_time; }
263
265 float getMaxLagEstimate() const { return m_max_lag_estimate; }
266
268
269 // Sorted by how ready a mapblock is
274 BS_ACTIVE // always highest value
275 };
277
278 // Sets the static object status all the active objects in the specified block
279 // This is only really needed for deleting blocks from the map
281 bool static_exists, v3s16 static_block=v3s16(0,0,0));
282
283 RemotePlayer *getPlayer(const session_t peer_id);
284 RemotePlayer *getPlayer(const std::string &name, bool match_invalid_peer = false);
285 const std::vector<RemotePlayer *> getPlayers() const { return m_players; }
286 u32 getPlayerCount() const { return m_players.size(); }
287
288 static std::vector<std::string> getPlayerDatabaseBackends();
289 static bool migratePlayersDatabase(const GameParams &game_params,
290 const Settings &cmd_args);
291
293 static std::vector<std::string> getAuthDatabaseBackends();
294 static bool migrateAuthDatabase(const GameParams &game_params,
295 const Settings &cmd_args);
296
297private:
298
302 void loadDefaultMeta();
303
304 static PlayerDatabase *openPlayerDatabase(const std::string &name,
305 const std::string &savedir, const Settings &conf);
306 static AuthDatabase *openAuthDatabase(const std::string &name,
307 const std::string &savedir, const Settings &conf);
308
309 void activateBlock(MapBlock *block);
310
311 /*
312 Internal ActiveObject interface
313 -------------------------------------------
314 */
315
316 /*
317 Add an active object to the environment.
318
319 Called by addActiveObject.
320
321 Object may be deleted by environment immediately.
322 If id of object is 0, assigns a free id to it.
323 Returns the id of the object.
324 Returns 0 if not added and thus deleted.
325 */
326 u16 addActiveObjectRaw(std::unique_ptr<ServerActiveObject> object,
327 const StaticObject *from_static, u32 dtime_s);
328
329 /*
330 Remove all objects that satisfy (isGone() && m_known_by_count==0)
331 */
333
334 /*
335 Convert stored objects from block to active
336 */
337 void activateObjects(MapBlock *block, u32 dtime_s);
338
339 /*
340 Convert objects that are not in active blocks to static.
341
342 If m_known_by_count != 0, active object is not deleted, but static
343 data is still updated.
344
345 If force_delete is set, active object is deleted nevertheless. It
346 shall only be set so in the destructor of the environment.
347 */
348 void deactivateFarObjects(bool force_delete);
349
350 /*
351 A few helpers used by the three above methods
352 */
354 ServerActiveObject *obj, u16 id, u32 mod_reason, bool no_emerge);
355 bool saveStaticToBlock(v3s16 blockpos, u16 store_id,
356 ServerActiveObject *obj, const StaticObject &s_obj, u32 mod_reason);
357
359
360 /*
361 Member variables
362 */
363
364 // The map
365 std::unique_ptr<ServerMap> m_map;
366 // Lua state
368 // Server definition
369 Server *m_server = nullptr;
370 // Active Object Manager
372 // on_mapblocks_changed map event receiver
375 // Outgoing network message buffer for active objects
376 std::queue<ActiveObjectMessage> m_active_object_messages;
377 // Some timers
380 // List of active blocks
386 // Whether the variables below have been read from file yet
387 bool m_meta_loaded = false;
388 // Are we shutting down?
389 bool m_shutting_down = false;
390 // Time from the beginning of the game in seconds.
391 // Incremented in step().
392 u32 m_game_time = 0;
393 // A helper variable for incrementing the latter
395 // Time of last clearObjects call (game time).
396 // When a mapblock older than this is loaded, its objects are cleared.
398 // Active block modifiers
399 std::vector<ABMWithState> m_abms;
401 // An interval for generally sending object positions and stuff
403 // Estimate for general maximum lag as determined by server.
404 // Can raise to high values like 15s with eg. map generation mods.
405 float m_max_lag_estimate = 0.1f;
406
407
408 /*
409 * TODO: Add a callback function so these can be updated when a setting
410 * changes.
411 */
416
417 // peer_ids in here should be unique, except that there may be many 0s
418 std::vector<RemotePlayer*> m_players;
419
422
423 // Particles
425 std::unordered_map<u32, float> m_particle_spawners;
427 std::unordered_map<u32, u16> m_particle_spawner_attachments;
428
429 // Environment metrics
433
434 std::unique_ptr<ServerActiveObject> createSAO(ActiveObjectType type, v3f pos,
435 const std::string &data);
436};
ActiveObjectType
Definition activeobject.h:14
Definition serverenvironment.h:46
void clear()
Definition serverenvironment.h:63
bool add(v3s16 p)
Definition serverenvironment.h:68
void remove(v3s16 p)
Definition serverenvironment.h:76
void update(std::vector< PlayerSAO * > &active_players, s16 active_block_range, s16 active_object_range, std::set< v3s16 > &blocks_removed, std::set< v3s16 > &blocks_added, std::set< v3s16 > &extra_blocks_added)
Definition serverenvironment.cpp:83
std::set< v3s16 > m_forceloaded_list
Definition serverenvironment.h:87
bool contains(v3s16 p) const
Definition serverenvironment.h:55
std::set< v3s16 > m_abm_list
Definition serverenvironment.h:85
std::set< v3s16 > m_list
Definition serverenvironment.h:82
auto size() const
Definition serverenvironment.h:59
Definition blockmodifier.h:26
T * getActiveObject(u16 id)
Definition activeobjectmgr.h:46
Definition activeobject.h:149
Definition database.h:66
Definition environment.h:31
Definition guid.h:29
Definition numeric.h:386
Definition blockmodifier.h:133
Definition mapblock.h:58
Definition map.h:93
Definition map.h:100
Definition metricsbackend.h:39
Definition database.h:46
Definition player_sao.h:58
Definition remoteplayer.h:27
Definition serveractiveobject.h:41
Definition serverenvironment.h:113
bool swapNode(v3s16 p, const MapNode &n)
Definition serverenvironment.cpp:654
void removePlayer(RemotePlayer *player)
Definition serverenvironment.cpp:363
ServerScripting * getScriptIface()
Definition serverenvironment.h:125
IntervalLimiter m_object_management_interval
Definition serverenvironment.h:379
GUIDGenerator m_guid_generator
Definition serverenvironment.h:374
void getAddedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, const std::set< u16 > &current_objects, std::vector< u16 > &added_objects)
Definition serverenvironment.cpp:1236
void init()
Definition serverenvironment.cpp:201
static bool migratePlayersDatabase(const GameParams &game_params, const Settings &cmd_args)
Definition serverenvironment.cpp:1886
void saveMeta()
Definition serverenvironment.cpp:437
IntervalLimiter m_particle_management_interval
Definition serverenvironment.h:424
IntervalLimiter m_active_blocks_mgmt_interval
Definition serverenvironment.h:383
void savePlayer(RemotePlayer *player)
Definition serverenvironment.cpp:395
MetricGaugePtr m_active_block_gauge
Definition serverenvironment.h:431
std::vector< RemotePlayer * > m_players
Definition serverenvironment.h:418
void forceActivateBlock(MapBlock *block)
Definition serverenvironment.cpp:536
virtual void getSelectedActiveObjects(const core::line3d< f32 > &shootline_on_map, std::vector< PointedThing > &objects, const std::optional< Pointabilities > &pointabilities)
Definition serverenvironment.cpp:1343
std::unique_ptr< ServerMap > m_map
Definition serverenvironment.h:365
MetricGaugePtr m_active_object_gauge
Definition serverenvironment.h:432
static std::vector< std::string > getPlayerDatabaseBackends()
Definition serverenvironment.cpp:1842
float m_recommended_send_interval
Definition serverenvironment.h:402
float m_cache_nodetimer_interval
Definition serverenvironment.h:414
void processActiveObjectRemove(ServerActiveObject *obj)
Definition serverenvironment.cpp:1829
static std::vector< std::string > getAuthDatabaseBackends()
Definition serverenvironment.cpp:1973
server::ActiveObjectMgr m_ao_manager
Definition serverenvironment.h:371
void saveLoadedPlayers(bool force=false)
Definition serverenvironment.cpp:379
OnMapblocksChangedReceiver m_on_mapblocks_changed_receiver
Definition serverenvironment.h:373
float m_max_lag_estimate
Definition serverenvironment.h:405
bool getActiveObjectMessage(ActiveObjectMessage *dest)
Definition serverenvironment.cpp:1333
bool m_meta_loaded
Definition serverenvironment.h:387
void setStaticForActiveObjectsInBlock(v3s16 blockpos, bool static_exists, v3s16 static_block=v3s16(0, 0, 0))
Definition serverenvironment.cpp:1310
u32 addParticleSpawner(float exptime)
Definition serverenvironment.cpp:1173
u32 m_game_time
Definition serverenvironment.h:392
void updateObjectPos(u16 id, v3f pos)
Definition serverenvironment.h:234
std::queue< ActiveObjectMessage > m_active_object_messages
Definition serverenvironment.h:376
u8 findSunlight(v3s16 pos) const
Definition serverenvironment.cpp:665
float getMaxLagEstimate() const
Definition serverenvironment.h:265
AuthDatabase * getAuthDatabase()
Definition serverenvironment.h:292
const std::vector< RemotePlayer * > getPlayers() const
Definition serverenvironment.h:285
Server * getGameDef()
Definition serverenvironment.h:128
static AuthDatabase * openAuthDatabase(const std::string &name, const std::string &savedir, const Settings &conf)
Definition serverenvironment.cpp:1988
void removeRemovedObjects()
Definition serverenvironment.cpp:1484
std::unordered_map< u32, float > m_particle_spawners
Definition serverenvironment.h:425
u16 addActiveObjectRaw(std::unique_ptr< ServerActiveObject > object, const StaticObject *from_static, u32 dtime_s)
Definition serverenvironment.cpp:1416
BlockStatus getBlockStatus(v3s16 blockpos)
Definition serverenvironment.cpp:1158
float getSendRecommendedInterval()
Definition serverenvironment.h:131
float m_cache_active_block_mgmt_interval
Definition serverenvironment.h:412
Map & getMap()
Definition serverenvironment.cpp:315
void deactivateFarObjects(bool force_delete)
Definition serverenvironment.cpp:1651
RemotePlayer * getPlayer(const session_t peer_id)
Definition serverenvironment.cpp:325
void clearObjects(ClearObjectsMode mode)
Definition serverenvironment.cpp:750
void reportMaxLagEstimate(float f)
Definition serverenvironment.h:264
Server * m_server
Definition serverenvironment.h:369
void deactivateBlocksAndObjects()
Definition serverenvironment.cpp:277
bool setNode(v3s16 p, const MapNode &n)
Definition serverenvironment.cpp:596
MetricCounterPtr m_step_time_counter
Definition serverenvironment.h:430
void loadDefaultMeta()
called if env_meta.txt doesn't exist (e.g.
Definition serverenvironment.cpp:529
AuthDatabase * m_auth_database
Definition serverenvironment.h:421
std::unique_ptr< PlayerSAO > loadPlayer(RemotePlayer *player, session_t peer_id)
Definition serverenvironment.cpp:406
bool removePlayerFromDatabase(const std::string &name)
Definition serverenvironment.cpp:374
std::set< v3s16 > * getForceloadedBlocks()
Definition serverenvironment.h:267
ServerActiveObject * getActiveObject(u16 id)
Definition serverenvironment.h:160
void addLoadingBlockModifierDef(LoadingBlockModifierDef *lbm)
Definition serverenvironment.cpp:591
GUIDGenerator & getGUIDGenerator()
Definition serverenvironment.h:134
IntervalLimiter m_active_block_modifier_interval
Definition serverenvironment.h:384
void invalidateActiveObjectObserverCaches()
Definition serverenvironment.cpp:1227
float m_cache_abm_interval
Definition serverenvironment.h:413
u16 addActiveObject(std::unique_ptr< ServerActiveObject > object)
Definition serverenvironment.cpp:1214
bool removeNode(v3s16 p)
Definition serverenvironment.cpp:629
LBMManager m_lbm_mgr
Definition serverenvironment.h:400
float m_cache_abm_time_budget
Definition serverenvironment.h:415
void getObjectsInsideRadius(std::vector< ServerActiveObject * > &objects, const v3f &pos, float radius, std::function< bool(ServerActiveObject *obj)> include_obj_cb)
Definition serverenvironment.h:240
u32 getPlayerCount() const
Definition serverenvironment.h:286
ActiveBlockList m_active_blocks
Definition serverenvironment.h:381
void loadMeta()
Definition serverenvironment.cpp:465
std::unique_ptr< ServerActiveObject > createSAO(ActiveObjectType type, v3f pos, const std::string &data)
Definition serverenvironment.cpp:1566
bool saveStaticToBlock(v3s16 blockpos, u16 store_id, ServerActiveObject *obj, const StaticObject &s_obj, u32 mod_reason)
Definition serverenvironment.cpp:1800
void activateBlock(MapBlock *block)
Definition serverenvironment.cpp:544
static PlayerDatabase * openPlayerDatabase(const std::string &name, const std::string &savedir, const Settings &conf)
Definition serverenvironment.cpp:1857
std::vector< ABMWithState > m_abms
Definition serverenvironment.h:399
int m_fast_active_block_divider
Definition serverenvironment.h:382
ServerScripting * m_script
Definition serverenvironment.h:367
u32 m_particle_spawners_id_last_used
Definition serverenvironment.h:426
u32 getGameTime() const
Definition serverenvironment.h:262
ServerEnvironment(std::unique_ptr< ServerMap > map, Server *server, MetricsBackend *mb)
Definition serverenvironment.cpp:179
std::unordered_map< u32, u16 > m_particle_spawner_attachments
Definition serverenvironment.h:427
~ServerEnvironment()
Definition serverenvironment.cpp:290
float m_game_time_fraction_counter
Definition serverenvironment.h:394
void addPlayer(RemotePlayer *player)
Definition serverenvironment.cpp:347
u32 m_last_clear_objects_time
Definition serverenvironment.h:397
void deleteStaticFromBlock(ServerActiveObject *obj, u16 id, u32 mod_reason, bool no_emerge)
Definition serverenvironment.cpp:1775
ServerMap & getServerMap()
Definition serverenvironment.cpp:320
void getObjectsInArea(std::vector< ServerActiveObject * > &objects, const aabb3f &box, std::function< bool(ServerActiveObject *obj)> include_obj_cb)
Definition serverenvironment.h:247
void getRemovedActiveObjects(PlayerSAO *playersao, s16 radius, s16 player_radius, const std::set< u16 > &current_objects, std::vector< std::pair< bool, u16 > > &removed_objects)
Definition serverenvironment.cpp:1260
static bool migrateAuthDatabase(const GameParams &game_params, const Settings &cmd_args)
Definition serverenvironment.cpp:2013
void activateObjects(MapBlock *block, u32 dtime_s)
Definition serverenvironment.cpp:1581
float m_send_recommended_timer
Definition serverenvironment.h:378
void deleteParticleSpawner(u32 id, bool remove_from_object=true)
Definition serverenvironment.cpp:1200
PlayerDatabase * m_player_database
Definition serverenvironment.h:420
void addActiveBlockModifier(ActiveBlockModifier *abm)
Definition serverenvironment.cpp:586
bool m_shutting_down
Definition serverenvironment.h:389
BlockStatus
Definition serverenvironment.h:270
@ BS_EMERGING
Definition serverenvironment.h:272
@ BS_ACTIVE
Definition serverenvironment.h:274
@ BS_LOADED
Definition serverenvironment.h:273
@ BS_UNKNOWN
Definition serverenvironment.h:271
IntervalLimiter m_active_blocks_nodemetadata_interval
Definition serverenvironment.h:385
void step(f32 dtime)
Definition serverenvironment.cpp:863
Definition servermap.h:43
Definition scripting_server.h:34
Definition server.h:178
Definition settings.h:110
Definition activeobjectmgr.h:17
void updateObjectPos(u16 id, v3f pos)
Definition activeobjectmgr.cpp:119
void getObjectsInArea(const aabb3f &box, std::vector< ServerActiveObject * > &result, std::function< bool(ServerActiveObject *obj)> include_obj_cb)
Definition activeobjectmgr.cpp:145
void getObjectsInsideRadius(v3f pos, float radius, std::vector< ServerActiveObject * > &result, std::function< bool(ServerActiveObject *obj)> include_obj_cb)
Definition activeobjectmgr.cpp:128
core::aabbox3d< f32 > aabb3f
Definition irr_aabb3d.h:11
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
core::vector3df v3f
Definition irr_v3d.h:11
std::shared_ptr< MetricCounter > MetricCounterPtr
Definition metricsbackend.h:22
std::shared_ptr< MetricGauge > MetricGaugePtr
Definition metricsbackend.h:36
Definition activeobjectmgr.cpp:11
AccessDeniedCode
Definition networkprotocol.h:929
u16 session_t
Definition networkprotocol.h:27
ClearObjectsMode
Definition serverenvironment.h:103
@ CLEAR_OBJECTS_MODE_QUICK
Definition serverenvironment.h:109
@ CLEAR_OBJECTS_MODE_FULL
Definition serverenvironment.h:105
u16 session_t
Definition serverenvironment.h:39
Definition activeobject.h:33
Definition gameparams.h:12
Definition blockmodifier.h:93
Definition map.h:43
Definition mapnode.h:123
Definition serverenvironment.h:93
std::unordered_set< v3s16 > modified_blocks
Definition serverenvironment.h:94
void onMapEditEvent(const MapEditEvent &event) override
Definition serverenvironment.cpp:167
bool receiving
Definition serverenvironment.h:95
Definition staticobject.h:20
static std::string p(std::string path)
Definition test_filesys.cpp:64