3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
28 #include "irrlichttypes_bloated.h"
30 #include "constants.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"
43 class ServerMapSector;
47 class IRollbackManager;
50 class ServerEnvironment;
57 #define MAPTYPE_BASE 0
58 #define MAPTYPE_SERVER 1
59 #define MAPTYPE_CLIENT 2
63 // Node added (changed from air or something else to something)
65 // Node removed (changed to air)
67 // Node swapped (changed without metadata change)
69 // Node metadata changed
70 MEET_BLOCK_NODE_METADATA_CHANGED,
71 // Anything else (modified_blocks are set unsent)
77 MapEditEventType type = MEET_OTHER;
79 MapNode n = CONTENT_AIR;
80 std::set<v3s16> modified_blocks;
81 bool is_private_change = false;
83 MapEditEvent() = default;
85 VoxelArea getArea() const
94 case MEET_BLOCK_NODE_METADATA_CHANGED: {
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);
101 for (v3s16 p : modified_blocks) {
102 v3s16 np1 = p * MAP_BLOCKSIZE;
103 v3s16 np2 = np1 + v3s16(1, 1, 1) * MAP_BLOCKSIZE -
115 class MapEventReceiver
118 // event shall be deleted by caller after the call.
119 virtual void onMapEditEvent(const MapEditEvent &event) = 0;
122 class Map /*: public NodeContainer*/
125 Map(std::ostream &dout, IGameDef *gamedef);
127 DISABLE_CLASS_COPY(Map);
129 virtual s32 mapType() const { return MAPTYPE_BASE; }
132 Drop (client) or delete (server) the map.
134 virtual void drop() { delete this; }
136 void addEventReceiver(MapEventReceiver *event_receiver);
137 void removeEventReceiver(MapEventReceiver *event_receiver);
138 // event shall be deleted by caller after the call.
139 void dispatchEvent(const MapEditEvent &event);
141 // On failure returns NULL
142 MapSector *getSectorNoGenerateNoLock(v2s16 p2d);
143 // Same as the above (there exists no lock anymore)
144 MapSector *getSectorNoGenerate(v2s16 p2d);
145 // Gets an existing sector or creates an empty one
146 // MapSector * getSectorCreate(v2s16 p2d);
149 This is overloaded by ClientMap and ServerMap to allow
150 their differing fetch methods.
152 virtual MapSector *emergeSector(v2s16 p) { return NULL; }
154 // Returns InvalidPositionException if not found
155 MapBlock *getBlockNoCreate(v3s16 p);
156 // Returns NULL if not found
157 MapBlock *getBlockNoCreateNoEx(v3s16 p);
159 /* Server overrides */
160 virtual MapBlock *emergeBlock(v3s16 p, bool create_blank = true)
162 return getBlockNoCreateNoEx(p);
165 inline const NodeDefManager *getNodeDefManager() { return m_nodedef; }
167 // Returns InvalidPositionException if not found
168 bool isNodeUnderground(v3s16 p);
170 bool isValidPosition(v3s16 p);
172 // throws InvalidPositionException if not found
173 void setNode(v3s16 p, MapNode &n);
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);
181 These handle lighting but not faces.
183 void addNodeAndUpdate(v3s16 p, MapNode n,
184 std::map<v3s16, MapBlock *> &modified_blocks,
185 bool remove_metadata = true);
186 void removeNodeAndUpdate(v3s16 p, std::map<v3s16, MapBlock *> &modified_blocks);
189 Wrappers for the latter ones.
191 Return true if succeeded, false if not.
193 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
194 bool removeNodeWithEvent(v3s16 p);
196 // Call these before and after saving of many blocks
197 virtual void beginSave() {}
198 virtual void endSave() {}
200 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
202 // Server implements these.
203 // Client leaves them as no-op.
204 virtual bool saveBlock(MapBlock *block) { return false; }
205 virtual bool deleteBlock(v3s16 blockpos) { return false; }
208 Updates usage timers and unloads unused blocks and sectors.
209 Saves modified blocks before unloading on MAPTYPE_SERVER.
211 void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
212 std::vector<v3s16> *unloaded_blocks = NULL);
215 Unloads all blocks with a zero refCount().
216 Saves modified blocks before unloading on MAPTYPE_SERVER.
218 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks = NULL);
220 // Deletes sectors and their blocks from memory
221 // Takes cache into account
222 // If deleted sector is in sector cache, clears cache
223 void deleteSectors(std::vector<v2s16> &list);
225 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
226 virtual void PrintInfo(std::ostream &out);
228 void transformLiquids(std::map<v3s16, MapBlock *> &modified_blocks,
229 ServerEnvironment *env);
233 These are basically coordinate wrappers to MapBlock
236 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
237 NodeMetadata *getNodeMetadata(v3s16 p);
240 * Sets metadata for a node.
241 * This method sets the metadata for a given node.
242 * On success, it returns @c true and the object pointed to
243 * by @p meta is then managed by the system and should
244 * not be deleted by the caller.
246 * In case of failure, the method returns @c false and the
247 * caller is still responsible for deleting the object!
249 * @param p node coordinates
250 * @param meta pointer to @c NodeMetadata object
251 * @return @c true on success, false on failure
253 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
254 void removeNodeMetadata(v3s16 p);
258 These are basically coordinate wrappers to MapBlock
261 NodeTimer getNodeTimer(v3s16 p);
262 void setNodeTimer(const NodeTimer &t);
263 void removeNodeTimer(v3s16 p);
268 std::map<v2s16, MapSector *> *getSectorsPtr() { return &m_sectors; }
274 void transforming_liquid_add(v3s16 p);
276 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
279 friend class LuaVoxelManip;
281 std::ostream &m_dout; // A bit deprecated, could be removed
285 std::set<MapEventReceiver *> m_event_receivers;
287 std::map<v2s16, MapSector *> m_sectors;
289 // Be sure to set this to NULL when the cached sector is deleted
290 MapSector *m_sector_cache = nullptr;
291 v2s16 m_sector_cache_p;
293 // Queued transforming water nodes
294 UniqueQueue<v3s16> m_transforming_liquid;
296 // This stores the properties of the nodes on the map.
297 const NodeDefManager *m_nodedef;
299 bool determineAdditionalOcclusionCheck(const v3s16 &pos_camera,
300 const core::aabbox3d<s16> &block_bounds, v3s16 &check);
301 bool isOccluded(const v3s16 &pos_camera, const v3s16 &pos_target, float step,
302 float stepfac, float start_offset, float end_offset,
306 f32 m_transforming_liquid_loop_count_multiplier = 1.0f;
307 u32 m_unprocessed_count = 0;
308 u64 m_inc_trending_up_start_time = 0; // milliseconds
309 bool m_queue_size_timer_started = false;
315 This is the only map class that is able to generate map.
318 class ServerMap : public Map
322 savedir: directory to which map data should be saved
324 ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge,
328 s32 mapType() const { return MAPTYPE_SERVER; }
331 Get a sector from somewhere.
333 - Check disk (doesn't load blocks)
336 MapSector *createSector(v2s16 p);
339 Blocks are generated by using these and makeBlock().
341 bool blockpos_over_mapgen_limit(v3s16 p);
342 bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
343 void finishBlockMake(
344 BlockMakeData *data, std::map<v3s16, MapBlock *> *changed_blocks);
347 Get a block from somewhere.
351 MapBlock *createBlock(v3s16 p);
354 Forcefully get a block from somewhere.
357 - Create blank filled with CONTENT_IGNORE
360 MapBlock *emergeBlock(v3s16 p, bool create_blank = true);
364 If it does not exist in memory, add it to the emerge queue.
366 - Emerge Queue (deferred disk or generate)
368 MapBlock *getBlockOrEmerge(v3s16 p3d);
370 // Helper for placing objects on ground level
371 s16 findGroundLevel(v2s16 p2d);
374 Misc. helper functions for fiddling with directory and file
377 void createDirs(const std::string &path);
382 static MapDatabase *createDatabase(const std::string &name,
383 const std::string &savedir, Settings &conf);
385 // Call these before and after saving of blocks
389 void save(ModifiedState save_level);
390 void listAllLoadableBlocks(std::vector<v3s16> &dst);
391 void listAllLoadedBlocks(std::vector<v3s16> &dst);
393 MapgenParams *getMapgenParams();
395 bool saveBlock(MapBlock *block);
396 static bool saveBlock(MapBlock *block, MapDatabase *db);
397 MapBlock *loadBlock(v3s16 p);
399 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector,
400 bool save_after_load = false);
402 bool deleteBlock(v3s16 blockpos);
404 void updateVManip(v3s16 pos);
406 // For debug printing
407 virtual void PrintInfo(std::ostream &out);
409 bool isSavingEnabled() { return m_map_saving_enabled; }
415 * Fixes lighting in one map block.
416 * May modify other blocks as well, as light can spread
417 * out of the specified block.
418 * Returns false if the block is not generated (so nothing
419 * changed), true otherwise.
421 bool repairBlockLight(
422 v3s16 blockpos, std::map<v3s16, MapBlock *> *modified_blocks);
424 MapSettingsManager settings_mgr;
428 EmergeManager *m_emerge;
430 std::string m_savedir;
431 bool m_map_saving_enabled;
434 // Chunk size in MapSectors
435 // If 0, chunks are disabled.
438 core::map<v2s16, MapChunk*> m_chunks;
442 Metadata is re-written on disk only if this is true.
443 This is reset to false when written on disk.
445 bool m_map_metadata_changed = true;
446 MapDatabase *dbase = nullptr;
447 MapDatabase *dbase_ro = nullptr;
449 MetricCounterPtr m_save_time_counter;
452 #define VMANIP_BLOCK_DATA_INEXIST 1
453 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
455 class MMVManip : public VoxelManipulator
459 virtual ~MMVManip() = default;
463 VoxelManipulator::clear();
464 m_loaded_blocks.clear();
467 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
468 bool load_if_inexistent = true);
470 // This is much faster with big chunks of generated data
471 void blitBackAll(std::map<v3s16, MapBlock *> *modified_blocks,
472 bool overwrite_generated = true);
474 bool m_is_dirty = false;
480 value = flags describing the block
482 std::map<v3s16, u8> m_loaded_blocks;