]> git.lizzy.rs Git - dragonfireclient.git/blob - src/map.h
Remove a few unused functions reported by callcatcher (#11658)
[dragonfireclient.git] / src / map.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 #pragma once
21
22 #include <iostream>
23 #include <sstream>
24 #include <set>
25 #include <map>
26 #include <list>
27
28 #include "irrlichttypes_bloated.h"
29 #include "mapnode.h"
30 #include "constants.h"
31 #include "voxel.h"
32 #include "modifiedstate.h"
33 #include "util/container.h"
34 #include "util/metricsbackend.h"
35 #include "nodetimer.h"
36 #include "map_settings_manager.h"
37 #include "debug.h"
38
39 class Settings;
40 class MapDatabase;
41 class ClientMap;
42 class MapSector;
43 class ServerMapSector;
44 class MapBlock;
45 class NodeMetadata;
46 class IGameDef;
47 class IRollbackManager;
48 class EmergeManager;
49 class MetricsBackend;
50 class ServerEnvironment;
51 struct BlockMakeData;
52
53 /*
54         MapEditEvent
55 */
56
57 #define MAPTYPE_BASE 0
58 #define MAPTYPE_SERVER 1
59 #define MAPTYPE_CLIENT 2
60
61 enum MapEditEventType{
62         // Node added (changed from air or something else to something)
63         MEET_ADDNODE,
64         // Node removed (changed to air)
65         MEET_REMOVENODE,
66         // Node swapped (changed without metadata change)
67         MEET_SWAPNODE,
68         // Node metadata changed
69         MEET_BLOCK_NODE_METADATA_CHANGED,
70         // Anything else (modified_blocks are set unsent)
71         MEET_OTHER
72 };
73
74 struct MapEditEvent
75 {
76         MapEditEventType type = MEET_OTHER;
77         v3s16 p;
78         MapNode n = CONTENT_AIR;
79         std::set<v3s16> modified_blocks;
80         bool is_private_change = false;
81
82         MapEditEvent() = default;
83
84         VoxelArea getArea() const
85         {
86                 switch(type){
87                 case MEET_ADDNODE:
88                         return VoxelArea(p);
89                 case MEET_REMOVENODE:
90                         return VoxelArea(p);
91                 case MEET_SWAPNODE:
92                         return VoxelArea(p);
93                 case MEET_BLOCK_NODE_METADATA_CHANGED:
94                 {
95                         v3s16 np1 = p*MAP_BLOCKSIZE;
96                         v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
97                         return VoxelArea(np1, np2);
98                 }
99                 case MEET_OTHER:
100                 {
101                         VoxelArea a;
102                         for (v3s16 p : modified_blocks) {
103                                 v3s16 np1 = p*MAP_BLOCKSIZE;
104                                 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
105                                 a.addPoint(np1);
106                                 a.addPoint(np2);
107                         }
108                         return a;
109                 }
110                 }
111                 return VoxelArea();
112         }
113 };
114
115 class MapEventReceiver
116 {
117 public:
118         // event shall be deleted by caller after the call.
119         virtual void onMapEditEvent(const MapEditEvent &event) = 0;
120 };
121
122 class Map /*: public NodeContainer*/
123 {
124 public:
125
126         Map(IGameDef *gamedef);
127         virtual ~Map();
128         DISABLE_CLASS_COPY(Map);
129
130         virtual s32 mapType() const
131         {
132                 return MAPTYPE_BASE;
133         }
134
135         /*
136                 Drop (client) or delete (server) the map.
137         */
138         virtual void drop()
139         {
140                 delete this;
141         }
142
143         void addEventReceiver(MapEventReceiver *event_receiver);
144         void removeEventReceiver(MapEventReceiver *event_receiver);
145         // event shall be deleted by caller after the call.
146         void dispatchEvent(const MapEditEvent &event);
147
148         // On failure returns NULL
149         MapSector * getSectorNoGenerateNoLock(v2s16 p2d);
150         // Same as the above (there exists no lock anymore)
151         MapSector * getSectorNoGenerate(v2s16 p2d);
152
153         /*
154                 This is overloaded by ClientMap and ServerMap to allow
155                 their differing fetch methods.
156         */
157         virtual MapSector * emergeSector(v2s16 p){ return NULL; }
158
159         // Returns InvalidPositionException if not found
160         MapBlock * getBlockNoCreate(v3s16 p);
161         // Returns NULL if not found
162         MapBlock * getBlockNoCreateNoEx(v3s16 p);
163
164         /* Server overrides */
165         virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
166         { return getBlockNoCreateNoEx(p); }
167
168         inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
169
170         bool isValidPosition(v3s16 p);
171
172         // throws InvalidPositionException if not found
173         void setNode(v3s16 p, MapNode & n);
174
175         // Returns a CONTENT_IGNORE node if not found
176         // If is_valid_position is not NULL then this will be set to true if the
177         // position is valid, otherwise false
178         MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
179
180         /*
181                 These handle lighting but not faces.
182         */
183         void addNodeAndUpdate(v3s16 p, MapNode n,
184                         std::map<v3s16, MapBlock*> &modified_blocks,
185                         bool remove_metadata = true);
186         void removeNodeAndUpdate(v3s16 p,
187                         std::map<v3s16, MapBlock*> &modified_blocks);
188
189         /*
190                 Wrappers for the latter ones.
191                 These emit events.
192                 Return true if succeeded, false if not.
193         */
194         bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
195         bool removeNodeWithEvent(v3s16 p);
196
197         // Call these before and after saving of many blocks
198         virtual void beginSave() {}
199         virtual void endSave() {}
200
201         virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
202
203         // Server implements these.
204         // Client leaves them as no-op.
205         virtual bool saveBlock(MapBlock *block) { return false; }
206         virtual bool deleteBlock(v3s16 blockpos) { return false; }
207
208         /*
209                 Updates usage timers and unloads unused blocks and sectors.
210                 Saves modified blocks before unloading on MAPTYPE_SERVER.
211         */
212         void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
213                         std::vector<v3s16> *unloaded_blocks=NULL);
214
215         /*
216                 Unloads all blocks with a zero refCount().
217                 Saves modified blocks before unloading on MAPTYPE_SERVER.
218         */
219         void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
220
221         // Deletes sectors and their blocks from memory
222         // Takes cache into account
223         // If deleted sector is in sector cache, clears cache
224         void deleteSectors(std::vector<v2s16> &list);
225
226         // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
227         virtual void PrintInfo(std::ostream &out);
228
229         void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks,
230                         ServerEnvironment *env);
231
232         /*
233                 Node metadata
234                 These are basically coordinate wrappers to MapBlock
235         */
236
237         std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
238         NodeMetadata *getNodeMetadata(v3s16 p);
239
240         /**
241          * Sets metadata for a node.
242          * This method sets the metadata for a given node.
243          * On success, it returns @c true and the object pointed to
244          * by @p meta is then managed by the system and should
245          * not be deleted by the caller.
246          *
247          * In case of failure, the method returns @c false and the
248          * caller is still responsible for deleting the object!
249          *
250          * @param p node coordinates
251          * @param meta pointer to @c NodeMetadata object
252          * @return @c true on success, false on failure
253          */
254         bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
255         void removeNodeMetadata(v3s16 p);
256
257         /*
258                 Node Timers
259                 These are basically coordinate wrappers to MapBlock
260         */
261
262         NodeTimer getNodeTimer(v3s16 p);
263         void setNodeTimer(const NodeTimer &t);
264         void removeNodeTimer(v3s16 p);
265
266         /*
267                 Variables
268         */
269
270         void transforming_liquid_add(v3s16 p);
271
272         bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
273 protected:
274         friend class LuaVoxelManip;
275
276         IGameDef *m_gamedef;
277
278         std::set<MapEventReceiver*> m_event_receivers;
279
280         std::map<v2s16, MapSector*> m_sectors;
281
282         // Be sure to set this to NULL when the cached sector is deleted
283         MapSector *m_sector_cache = nullptr;
284         v2s16 m_sector_cache_p;
285
286         // Queued transforming water nodes
287         UniqueQueue<v3s16> m_transforming_liquid;
288
289         // This stores the properties of the nodes on the map.
290         const NodeDefManager *m_nodedef;
291
292         bool determineAdditionalOcclusionCheck(const v3s16 &pos_camera,
293                 const core::aabbox3d<s16> &block_bounds, v3s16 &check);
294         bool isOccluded(const v3s16 &pos_camera, const v3s16 &pos_target,
295                 float step, float stepfac, float start_offset, float end_offset,
296                 u32 needed_count);
297
298 private:
299         f32 m_transforming_liquid_loop_count_multiplier = 1.0f;
300         u32 m_unprocessed_count = 0;
301         u64 m_inc_trending_up_start_time = 0; // milliseconds
302         bool m_queue_size_timer_started = false;
303 };
304
305 /*
306         ServerMap
307
308         This is the only map class that is able to generate map.
309 */
310
311 class ServerMap : public Map
312 {
313 public:
314         /*
315                 savedir: directory to which map data should be saved
316         */
317         ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge, MetricsBackend *mb);
318         ~ServerMap();
319
320         s32 mapType() const
321         {
322                 return MAPTYPE_SERVER;
323         }
324
325         /*
326                 Get a sector from somewhere.
327                 - Check memory
328                 - Check disk (doesn't load blocks)
329                 - Create blank one
330         */
331         MapSector *createSector(v2s16 p);
332
333         /*
334                 Blocks are generated by using these and makeBlock().
335         */
336         bool blockpos_over_mapgen_limit(v3s16 p);
337         bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
338         void finishBlockMake(BlockMakeData *data,
339                 std::map<v3s16, MapBlock*> *changed_blocks);
340
341         /*
342                 Get a block from somewhere.
343                 - Memory
344                 - Create blank
345         */
346         MapBlock *createBlock(v3s16 p);
347
348         /*
349                 Forcefully get a block from somewhere.
350                 - Memory
351                 - Load from disk
352                 - Create blank filled with CONTENT_IGNORE
353
354         */
355         MapBlock *emergeBlock(v3s16 p, bool create_blank=true);
356
357         /*
358                 Try to get a block.
359                 If it does not exist in memory, add it to the emerge queue.
360                 - Memory
361                 - Emerge Queue (deferred disk or generate)
362         */
363         MapBlock *getBlockOrEmerge(v3s16 p3d);
364
365         bool isBlockInQueue(v3s16 pos);
366
367         /*
368                 Database functions
369         */
370         static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
371
372         // Call these before and after saving of blocks
373         void beginSave();
374         void endSave();
375
376         void save(ModifiedState save_level);
377         void listAllLoadableBlocks(std::vector<v3s16> &dst);
378         void listAllLoadedBlocks(std::vector<v3s16> &dst);
379
380         MapgenParams *getMapgenParams();
381
382         bool saveBlock(MapBlock *block);
383         static bool saveBlock(MapBlock *block, MapDatabase *db, int compression_level = -1);
384         MapBlock* loadBlock(v3s16 p);
385         // Database version
386         void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
387
388         bool deleteBlock(v3s16 blockpos);
389
390         void updateVManip(v3s16 pos);
391
392         // For debug printing
393         virtual void PrintInfo(std::ostream &out);
394
395         bool isSavingEnabled(){ return m_map_saving_enabled; }
396
397         u64 getSeed();
398
399         /*!
400          * Fixes lighting in one map block.
401          * May modify other blocks as well, as light can spread
402          * out of the specified block.
403          * Returns false if the block is not generated (so nothing
404          * changed), true otherwise.
405          */
406         bool repairBlockLight(v3s16 blockpos,
407                 std::map<v3s16, MapBlock *> *modified_blocks);
408
409         MapSettingsManager settings_mgr;
410
411 private:
412         // Emerge manager
413         EmergeManager *m_emerge;
414
415         std::string m_savedir;
416         bool m_map_saving_enabled;
417
418         int m_map_compression_level;
419
420         std::set<v3s16> m_chunks_in_progress;
421
422         /*
423                 Metadata is re-written on disk only if this is true.
424                 This is reset to false when written on disk.
425         */
426         bool m_map_metadata_changed = true;
427         MapDatabase *dbase = nullptr;
428         MapDatabase *dbase_ro = nullptr;
429
430         MetricCounterPtr m_save_time_counter;
431 };
432
433
434 #define VMANIP_BLOCK_DATA_INEXIST     1
435 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
436
437 class MMVManip : public VoxelManipulator
438 {
439 public:
440         MMVManip(Map *map);
441         virtual ~MMVManip() = default;
442
443         virtual void clear()
444         {
445                 VoxelManipulator::clear();
446                 m_loaded_blocks.clear();
447         }
448
449         void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
450                 bool load_if_inexistent = true);
451
452         // This is much faster with big chunks of generated data
453         void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
454                 bool overwrite_generated = true);
455
456         bool m_is_dirty = false;
457
458 protected:
459         Map *m_map;
460         /*
461                 key = blockpos
462                 value = flags describing the block
463         */
464         std::map<v3s16, u8> m_loaded_blocks;
465 };