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
61 enum MapEditEventType{
62 // Node added (changed from air or something else to something)
64 // Node removed (changed to air)
66 // Node swapped (changed without metadata change)
68 // Node metadata changed
69 MEET_BLOCK_NODE_METADATA_CHANGED,
70 // Anything else (modified_blocks are set unsent)
76 MapEditEventType type = MEET_OTHER;
78 MapNode n = CONTENT_AIR;
79 std::set<v3s16> modified_blocks;
80 bool is_private_change = false;
82 MapEditEvent() = default;
84 VoxelArea getArea() const
93 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);
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);
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*/
126 Map(IGameDef *gamedef);
128 DISABLE_CLASS_COPY(Map);
130 virtual s32 mapType() const
136 Drop (client) or delete (server) the map.
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);
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);
154 This is overloaded by ClientMap and ServerMap to allow
155 their differing fetch methods.
157 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
159 // Returns InvalidPositionException if not found
160 MapBlock * getBlockNoCreate(v3s16 p);
161 // Returns NULL if not found
162 MapBlock * getBlockNoCreateNoEx(v3s16 p);
164 /* Server overrides */
165 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
166 { return getBlockNoCreateNoEx(p); }
168 inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
170 // Returns InvalidPositionException if not found
171 bool isNodeUnderground(v3s16 p);
173 bool isValidPosition(v3s16 p);
175 // throws InvalidPositionException if not found
176 void setNode(v3s16 p, MapNode & n);
178 // Returns a CONTENT_IGNORE node if not found
179 // If is_valid_position is not NULL then this will be set to true if the
180 // position is valid, otherwise false
181 MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
184 These handle lighting but not faces.
186 void addNodeAndUpdate(v3s16 p, MapNode n,
187 std::map<v3s16, MapBlock*> &modified_blocks,
188 bool remove_metadata = true);
189 void removeNodeAndUpdate(v3s16 p,
190 std::map<v3s16, MapBlock*> &modified_blocks);
193 Wrappers for the latter ones.
195 Return true if succeeded, false if not.
197 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
198 bool removeNodeWithEvent(v3s16 p);
200 // Call these before and after saving of many blocks
201 virtual void beginSave() {}
202 virtual void endSave() {}
204 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
206 // Server implements these.
207 // Client leaves them as no-op.
208 virtual bool saveBlock(MapBlock *block) { return false; }
209 virtual bool deleteBlock(v3s16 blockpos) { return false; }
212 Updates usage timers and unloads unused blocks and sectors.
213 Saves modified blocks before unloading on MAPTYPE_SERVER.
215 void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
216 std::vector<v3s16> *unloaded_blocks=NULL);
219 Unloads all blocks with a zero refCount().
220 Saves modified blocks before unloading on MAPTYPE_SERVER.
222 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
224 // Deletes sectors and their blocks from memory
225 // Takes cache into account
226 // If deleted sector is in sector cache, clears cache
227 void deleteSectors(std::vector<v2s16> &list);
229 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
230 virtual void PrintInfo(std::ostream &out);
232 void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks,
233 ServerEnvironment *env);
237 These are basically coordinate wrappers to MapBlock
240 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
241 NodeMetadata *getNodeMetadata(v3s16 p);
244 * Sets metadata for a node.
245 * This method sets the metadata for a given node.
246 * On success, it returns @c true and the object pointed to
247 * by @p meta is then managed by the system and should
248 * not be deleted by the caller.
250 * In case of failure, the method returns @c false and the
251 * caller is still responsible for deleting the object!
253 * @param p node coordinates
254 * @param meta pointer to @c NodeMetadata object
255 * @return @c true on success, false on failure
257 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
258 void removeNodeMetadata(v3s16 p);
262 These are basically coordinate wrappers to MapBlock
265 NodeTimer getNodeTimer(v3s16 p);
266 void setNodeTimer(const NodeTimer &t);
267 void removeNodeTimer(v3s16 p);
273 void transforming_liquid_add(v3s16 p);
275 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
277 friend class LuaVoxelManip;
281 std::set<MapEventReceiver*> m_event_receivers;
283 std::map<v2s16, MapSector*> m_sectors;
285 // Be sure to set this to NULL when the cached sector is deleted
286 MapSector *m_sector_cache = nullptr;
287 v2s16 m_sector_cache_p;
289 // Queued transforming water nodes
290 UniqueQueue<v3s16> m_transforming_liquid;
292 // This stores the properties of the nodes on the map.
293 const NodeDefManager *m_nodedef;
295 bool determineAdditionalOcclusionCheck(const v3s16 &pos_camera,
296 const core::aabbox3d<s16> &block_bounds, v3s16 &check);
297 bool isOccluded(const v3s16 &pos_camera, const v3s16 &pos_target,
298 float step, float stepfac, float start_offset, float end_offset,
302 f32 m_transforming_liquid_loop_count_multiplier = 1.0f;
303 u32 m_unprocessed_count = 0;
304 u64 m_inc_trending_up_start_time = 0; // milliseconds
305 bool m_queue_size_timer_started = false;
311 This is the only map class that is able to generate map.
314 class ServerMap : public Map
318 savedir: directory to which map data should be saved
320 ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge, MetricsBackend *mb);
325 return MAPTYPE_SERVER;
329 Get a sector from somewhere.
331 - Check disk (doesn't load blocks)
334 MapSector *createSector(v2s16 p);
337 Blocks are generated by using these and makeBlock().
339 bool blockpos_over_mapgen_limit(v3s16 p);
340 bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
341 void finishBlockMake(BlockMakeData *data,
342 std::map<v3s16, MapBlock*> *changed_blocks);
345 Get a block from somewhere.
349 MapBlock *createBlock(v3s16 p);
352 Forcefully get a block from somewhere.
355 - Create blank filled with CONTENT_IGNORE
358 MapBlock *emergeBlock(v3s16 p, bool create_blank=true);
362 If it does not exist in memory, add it to the emerge queue.
364 - Emerge Queue (deferred disk or generate)
366 MapBlock *getBlockOrEmerge(v3s16 p3d);
371 static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
373 // Call these before and after saving of blocks
377 void save(ModifiedState save_level);
378 void listAllLoadableBlocks(std::vector<v3s16> &dst);
379 void listAllLoadedBlocks(std::vector<v3s16> &dst);
381 MapgenParams *getMapgenParams();
383 bool saveBlock(MapBlock *block);
384 static bool saveBlock(MapBlock *block, MapDatabase *db, int compression_level = -1);
385 MapBlock* loadBlock(v3s16 p);
387 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
389 bool deleteBlock(v3s16 blockpos);
391 void updateVManip(v3s16 pos);
393 // For debug printing
394 virtual void PrintInfo(std::ostream &out);
396 bool isSavingEnabled(){ return m_map_saving_enabled; }
401 * Fixes lighting in one map block.
402 * May modify other blocks as well, as light can spread
403 * out of the specified block.
404 * Returns false if the block is not generated (so nothing
405 * changed), true otherwise.
407 bool repairBlockLight(v3s16 blockpos,
408 std::map<v3s16, MapBlock *> *modified_blocks);
410 MapSettingsManager settings_mgr;
414 EmergeManager *m_emerge;
416 std::string m_savedir;
417 bool m_map_saving_enabled;
419 int m_map_compression_level;
421 std::set<v3s16> m_chunks_in_progress;
424 Metadata is re-written on disk only if this is true.
425 This is reset to false when written on disk.
427 bool m_map_metadata_changed = true;
428 MapDatabase *dbase = nullptr;
429 MapDatabase *dbase_ro = nullptr;
431 MetricCounterPtr m_save_time_counter;
435 #define VMANIP_BLOCK_DATA_INEXIST 1
436 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
438 class MMVManip : public VoxelManipulator
442 virtual ~MMVManip() = default;
446 VoxelManipulator::clear();
447 m_loaded_blocks.clear();
450 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
451 bool load_if_inexistent = true);
453 // This is much faster with big chunks of generated data
454 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
455 bool overwrite_generated = true);
457 bool m_is_dirty = false;
463 value = flags describing the block
465 std::map<v3s16, u8> m_loaded_blocks;