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 "nodetimer.h"
35 #include "map_settings_manager.h"
42 class ServerMapSector;
46 class IRollbackManager;
48 class ServerEnvironment;
55 #define MAPTYPE_BASE 0
56 #define MAPTYPE_SERVER 1
57 #define MAPTYPE_CLIENT 2
59 enum MapEditEventType{
60 // Node added (changed from air or something else to something)
62 // Node removed (changed to air)
64 // Node swapped (changed without metadata change)
66 // Node metadata of block changed (not knowing which node exactly)
67 // p stores block coordinate
68 MEET_BLOCK_NODE_METADATA_CHANGED,
69 // Anything else (modified_blocks are set unsent)
75 MapEditEventType type = MEET_OTHER;
77 MapNode n = CONTENT_AIR;
78 std::set<v3s16> modified_blocks;
79 u16 already_known_by_peer = 0;
81 MapEditEvent() = default;
83 MapEditEvent * clone()
85 MapEditEvent *event = new MapEditEvent();
89 event->modified_blocks = modified_blocks;
102 case MEET_BLOCK_NODE_METADATA_CHANGED:
104 v3s16 np1 = p*MAP_BLOCKSIZE;
105 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
106 return VoxelArea(np1, np2);
111 for (v3s16 p : modified_blocks) {
112 v3s16 np1 = p*MAP_BLOCKSIZE;
113 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
124 class MapEventReceiver
127 // event shall be deleted by caller after the call.
128 virtual void onMapEditEvent(MapEditEvent *event) = 0;
131 class Map /*: public NodeContainer*/
135 Map(std::ostream &dout, IGameDef *gamedef);
137 DISABLE_CLASS_COPY(Map);
139 virtual s32 mapType() const
145 Drop (client) or delete (server) the map.
152 void addEventReceiver(MapEventReceiver *event_receiver);
153 void removeEventReceiver(MapEventReceiver *event_receiver);
154 // event shall be deleted by caller after the call.
155 void dispatchEvent(MapEditEvent *event);
157 // On failure returns NULL
158 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
159 // Same as the above (there exists no lock anymore)
160 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
161 // On failure throws InvalidPositionException
162 MapSector * getSectorNoGenerate(v2s16 p2d);
163 // Gets an existing sector or creates an empty one
164 //MapSector * getSectorCreate(v2s16 p2d);
167 This is overloaded by ClientMap and ServerMap to allow
168 their differing fetch methods.
170 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
172 // Returns InvalidPositionException if not found
173 MapBlock * getBlockNoCreate(v3s16 p);
174 // Returns NULL if not found
175 MapBlock * getBlockNoCreateNoEx(v3s16 p);
177 /* Server overrides */
178 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
179 { return getBlockNoCreateNoEx(p); }
181 inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
183 // Returns InvalidPositionException if not found
184 bool isNodeUnderground(v3s16 p);
186 bool isValidPosition(v3s16 p);
188 // throws InvalidPositionException if not found
189 void setNode(v3s16 p, MapNode & n);
191 // Returns a CONTENT_IGNORE node if not found
192 // If is_valid_position is not NULL then this will be set to true if the
193 // position is valid, otherwise false
194 MapNode getNodeNoEx(v3s16 p, bool *is_valid_position = NULL);
197 These handle lighting but not faces.
199 void addNodeAndUpdate(v3s16 p, MapNode n,
200 std::map<v3s16, MapBlock*> &modified_blocks,
201 bool remove_metadata = true);
202 void removeNodeAndUpdate(v3s16 p,
203 std::map<v3s16, MapBlock*> &modified_blocks);
206 Wrappers for the latter ones.
208 Return true if succeeded, false if not.
210 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
211 bool removeNodeWithEvent(v3s16 p);
213 // Call these before and after saving of many blocks
214 virtual void beginSave() {}
215 virtual void endSave() {}
217 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
219 // Server implements these.
220 // Client leaves them as no-op.
221 virtual bool saveBlock(MapBlock *block) { return false; }
222 virtual bool deleteBlock(v3s16 blockpos) { return false; }
225 Updates usage timers and unloads unused blocks and sectors.
226 Saves modified blocks before unloading on MAPTYPE_SERVER.
228 void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
229 std::vector<v3s16> *unloaded_blocks=NULL);
232 Unloads all blocks with a zero refCount().
233 Saves modified blocks before unloading on MAPTYPE_SERVER.
235 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
237 // Deletes sectors and their blocks from memory
238 // Takes cache into account
239 // If deleted sector is in sector cache, clears cache
240 void deleteSectors(std::vector<v2s16> &list);
242 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
243 virtual void PrintInfo(std::ostream &out);
245 void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks,
246 ServerEnvironment *env);
250 These are basically coordinate wrappers to MapBlock
253 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
254 NodeMetadata *getNodeMetadata(v3s16 p);
257 * Sets metadata for a node.
258 * This method sets the metadata for a given node.
259 * On success, it returns @c true and the object pointed to
260 * by @p meta is then managed by the system and should
261 * not be deleted by the caller.
263 * In case of failure, the method returns @c false and the
264 * caller is still responsible for deleting the object!
266 * @param p node coordinates
267 * @param meta pointer to @c NodeMetadata object
268 * @return @c true on success, false on failure
270 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
271 void removeNodeMetadata(v3s16 p);
275 These are basically coordinate wrappers to MapBlock
278 NodeTimer getNodeTimer(v3s16 p);
279 void setNodeTimer(const NodeTimer &t);
280 void removeNodeTimer(v3s16 p);
285 std::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
291 void transforming_liquid_add(v3s16 p);
292 s32 transforming_liquid_size();
294 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
296 friend class LuaVoxelManip;
298 std::ostream &m_dout; // A bit deprecated, could be removed
302 std::set<MapEventReceiver*> m_event_receivers;
304 std::map<v2s16, MapSector*> m_sectors;
306 // Be sure to set this to NULL when the cached sector is deleted
307 MapSector *m_sector_cache = nullptr;
308 v2s16 m_sector_cache_p;
310 // Queued transforming water nodes
311 UniqueQueue<v3s16> m_transforming_liquid;
313 // This stores the properties of the nodes on the map.
314 const NodeDefManager *m_nodedef;
316 bool isOccluded(v3s16 p0, v3s16 p1, float step, float stepfac,
317 float start_off, float end_off, u32 needed_count);
320 f32 m_transforming_liquid_loop_count_multiplier = 1.0f;
321 u32 m_unprocessed_count = 0;
322 u64 m_inc_trending_up_start_time = 0; // milliseconds
323 bool m_queue_size_timer_started = false;
329 This is the only map class that is able to generate map.
332 class ServerMap : public Map
336 savedir: directory to which map data should be saved
338 ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge);
343 return MAPTYPE_SERVER;
347 Get a sector from somewhere.
349 - Check disk (doesn't load blocks)
352 MapSector *createSector(v2s16 p);
354 bool saoPositionOverLimit(const v3f &p);
357 Blocks are generated by using these and makeBlock().
359 bool blockpos_over_mapgen_limit(v3s16 p);
360 bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
361 void finishBlockMake(BlockMakeData *data,
362 std::map<v3s16, MapBlock*> *changed_blocks);
365 Get a block from somewhere.
369 MapBlock *createBlock(v3s16 p);
372 Forcefully get a block from somewhere.
375 - Create blank filled with CONTENT_IGNORE
378 MapBlock *emergeBlock(v3s16 p, bool create_blank=true);
382 If it does not exist in memory, add it to the emerge queue.
384 - Emerge Queue (deferred disk or generate)
386 MapBlock *getBlockOrEmerge(v3s16 p3d);
388 // Helper for placing objects on ground level
389 s16 findGroundLevel(v2s16 p2d);
392 Misc. helper functions for fiddling with directory and file
395 void createDirs(std::string path);
396 // returns something like "map/sectors/xxxxxxxx"
397 std::string getSectorDir(v2s16 pos, int layout = 2);
398 // dirname: final directory name
399 v2s16 getSectorPos(const std::string &dirname);
400 v3s16 getBlockPos(const std::string §ordir, const std::string &blockfile);
401 static std::string getBlockFilename(v3s16 p);
406 static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
408 // Returns true if the database file does not exist
409 bool loadFromFolders();
411 // Call these before and after saving of blocks
415 void save(ModifiedState save_level);
416 void listAllLoadableBlocks(std::vector<v3s16> &dst);
417 void listAllLoadedBlocks(std::vector<v3s16> &dst);
419 MapgenParams *getMapgenParams();
421 bool saveBlock(MapBlock *block);
422 static bool saveBlock(MapBlock *block, MapDatabase *db);
423 // This will generate a sector with getSector if not found.
424 void loadBlock(const std::string §ordir, const std::string &blockfile,
425 MapSector *sector, bool save_after_load=false);
426 MapBlock* loadBlock(v3s16 p);
428 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
430 bool deleteBlock(v3s16 blockpos);
432 void updateVManip(v3s16 pos);
434 // For debug printing
435 virtual void PrintInfo(std::ostream &out);
437 bool isSavingEnabled(){ return m_map_saving_enabled; }
443 * Fixes lighting in one map block.
444 * May modify other blocks as well, as light can spread
445 * out of the specified block.
446 * Returns false if the block is not generated (so nothing
447 * changed), true otherwise.
449 bool repairBlockLight(v3s16 blockpos,
450 std::map<v3s16, MapBlock *> *modified_blocks);
452 MapSettingsManager settings_mgr;
456 EmergeManager *m_emerge;
458 std::string m_savedir;
459 bool m_map_saving_enabled;
462 // Chunk size in MapSectors
463 // If 0, chunks are disabled.
466 core::map<v2s16, MapChunk*> m_chunks;
470 Metadata is re-written on disk only if this is true.
471 This is reset to false when written on disk.
473 bool m_map_metadata_changed = true;
474 MapDatabase *dbase = nullptr;
478 #define VMANIP_BLOCK_DATA_INEXIST 1
479 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
481 class MMVManip : public VoxelManipulator
485 virtual ~MMVManip() = default;
489 VoxelManipulator::clear();
490 m_loaded_blocks.clear();
493 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
494 bool load_if_inexistent = true);
496 // This is much faster with big chunks of generated data
497 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
498 bool overwrite_generated = true);
500 bool m_is_dirty = false;
506 value = flags describing the block
508 std::map<v3s16, u8> m_loaded_blocks;