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.
29 #include "irrlichttypes_bloated.h"
31 #include "constants.h"
33 #include "modifiedstate.h"
34 #include "util/container.h"
35 #include "nodetimer.h"
40 class ServerMapSector;
44 class IRollbackReportSink;
46 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;
78 std::set<v3s16> modified_blocks;
79 u16 already_known_by_peer;
83 already_known_by_peer(0)
87 MapEditEvent * clone()
89 MapEditEvent *event = new MapEditEvent();
93 event->modified_blocks = modified_blocks;
102 case MEET_REMOVENODE:
106 case MEET_BLOCK_NODE_METADATA_CHANGED:
108 v3s16 np1 = p*MAP_BLOCKSIZE;
109 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
110 return VoxelArea(np1, np2);
115 for(std::set<v3s16>::iterator
116 i = modified_blocks.begin();
117 i != modified_blocks.end(); ++i)
120 v3s16 np1 = p*MAP_BLOCKSIZE;
121 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
132 class MapEventReceiver
135 // event shall be deleted by caller after the call.
136 virtual void onMapEditEvent(MapEditEvent *event) = 0;
139 class Map /*: public NodeContainer*/
143 Map(std::ostream &dout, IGameDef *gamedef);
146 /*virtual u16 nodeContainerId() const
148 return NODECONTAINER_ID_MAP;
151 virtual s32 mapType() const
157 Drop (client) or delete (server) the map.
164 void addEventReceiver(MapEventReceiver *event_receiver);
165 void removeEventReceiver(MapEventReceiver *event_receiver);
166 // event shall be deleted by caller after the call.
167 void dispatchEvent(MapEditEvent *event);
169 // On failure returns NULL
170 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
171 // Same as the above (there exists no lock anymore)
172 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
173 // On failure throws InvalidPositionException
174 MapSector * getSectorNoGenerate(v2s16 p2d);
175 // Gets an existing sector or creates an empty one
176 //MapSector * getSectorCreate(v2s16 p2d);
179 This is overloaded by ClientMap and ServerMap to allow
180 their differing fetch methods.
182 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
183 virtual MapSector * emergeSector(v2s16 p,
184 std::map<v3s16, MapBlock*> &changed_blocks){ return NULL; }
186 // Returns InvalidPositionException if not found
187 MapBlock * getBlockNoCreate(v3s16 p);
188 // Returns NULL if not found
189 MapBlock * getBlockNoCreateNoEx(v3s16 p);
191 /* Server overrides */
192 virtual MapBlock * emergeBlock(v3s16 p, bool allow_generate=true)
193 { return getBlockNoCreateNoEx(p); }
195 // Returns InvalidPositionException if not found
196 bool isNodeUnderground(v3s16 p);
198 bool isValidPosition(v3s16 p);
200 // throws InvalidPositionException if not found
201 MapNode getNode(v3s16 p);
203 // throws InvalidPositionException if not found
204 void setNode(v3s16 p, MapNode & n);
206 // Returns a CONTENT_IGNORE node if not found
207 MapNode getNodeNoEx(v3s16 p);
209 void unspreadLight(enum LightBank bank,
210 std::map<v3s16, u8> & from_nodes,
211 std::set<v3s16> & light_sources,
212 std::map<v3s16, MapBlock*> & modified_blocks);
214 void unLightNeighbors(enum LightBank bank,
215 v3s16 pos, u8 lightwas,
216 std::set<v3s16> & light_sources,
217 std::map<v3s16, MapBlock*> & modified_blocks);
219 void spreadLight(enum LightBank bank,
220 std::set<v3s16> & from_nodes,
221 std::map<v3s16, MapBlock*> & modified_blocks);
223 void lightNeighbors(enum LightBank bank,
225 std::map<v3s16, MapBlock*> & modified_blocks);
227 v3s16 getBrightestNeighbour(enum LightBank bank, v3s16 p);
229 s16 propagateSunlight(v3s16 start,
230 std::map<v3s16, MapBlock*> & modified_blocks);
232 void updateLighting(enum LightBank bank,
233 std::map<v3s16, MapBlock*> & a_blocks,
234 std::map<v3s16, MapBlock*> & modified_blocks);
236 void updateLighting(std::map<v3s16, MapBlock*> & a_blocks,
237 std::map<v3s16, MapBlock*> & modified_blocks);
240 These handle lighting but not faces.
242 void addNodeAndUpdate(v3s16 p, MapNode n,
243 std::map<v3s16, MapBlock*> &modified_blocks,
244 bool remove_metadata = true);
245 void removeNodeAndUpdate(v3s16 p,
246 std::map<v3s16, MapBlock*> &modified_blocks);
249 Wrappers for the latter ones.
251 Return true if succeeded, false if not.
253 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
254 bool removeNodeWithEvent(v3s16 p);
257 Takes the blocks at the edges into account
259 bool getDayNightDiff(v3s16 blockpos);
261 //core::aabbox3d<s16> getDisplayedBlockArea();
263 //bool updateChangedVisibleArea();
265 // Call these before and after saving of many blocks
266 virtual void beginSave() {return;};
267 virtual void endSave() {return;};
269 virtual void save(ModifiedState save_level){assert(0);};
271 // Server implements this.
272 // Client leaves it as no-op.
273 virtual bool saveBlock(MapBlock *block) { return false; };
276 Updates usage timers and unloads unused blocks and sectors.
277 Saves modified blocks before unloading on MAPTYPE_SERVER.
279 void timerUpdate(float dtime, float unload_timeout,
280 std::list<v3s16> *unloaded_blocks=NULL);
283 Unloads all blocks with a zero refCount().
284 Saves modified blocks before unloading on MAPTYPE_SERVER.
286 void unloadUnreferencedBlocks(std::list<v3s16> *unloaded_blocks=NULL);
288 // Deletes sectors and their blocks from memory
289 // Takes cache into account
290 // If deleted sector is in sector cache, clears cache
291 void deleteSectors(std::list<v2s16> &list);
296 = flush changed to disk and delete from memory, if usage timer of
297 block is more than timeout
299 void unloadUnusedData(float timeout,
300 core::list<v3s16> *deleted_blocks=NULL);
303 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
304 virtual void PrintInfo(std::ostream &out);
306 void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks);
310 These are basically coordinate wrappers to MapBlock
313 NodeMetadata* getNodeMetadata(v3s16 p);
316 * Sets metadata for a node.
317 * This method sets the metadata for a given node.
318 * On success, it returns @c true and the object pointed to
319 * by @p meta is then managed by the system and should
320 * not be deleted by the caller.
322 * In case of failure, the method returns @c false and the
323 * caller is still responsible for deleting the object!
325 * @param p node coordinates
326 * @param meta pointer to @c NodeMetadata object
327 * @return @c true on success, false on failure
329 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
330 void removeNodeMetadata(v3s16 p);
334 These are basically coordinate wrappers to MapBlock
337 NodeTimer getNodeTimer(v3s16 p);
338 void setNodeTimer(v3s16 p, NodeTimer t);
339 void removeNodeTimer(v3s16 p);
344 std::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
350 void transforming_liquid_add(v3s16 p);
351 s32 transforming_liquid_size();
354 friend class LuaVoxelManip;
356 std::ostream &m_dout; // A bit deprecated, could be removed
360 std::set<MapEventReceiver*> m_event_receivers;
362 std::map<v2s16, MapSector*> m_sectors;
364 // Be sure to set this to NULL when the cached sector is deleted
365 MapSector *m_sector_cache;
366 v2s16 m_sector_cache_p;
368 // Queued transforming water nodes
369 UniqueQueue<v3s16> m_transforming_liquid;
375 This is the only map class that is able to generate map.
378 class ServerMap : public Map
382 savedir: directory to which map data should be saved
384 ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emerge);
389 return MAPTYPE_SERVER;
393 Get a sector from somewhere.
395 - Check disk (doesn't load blocks)
398 ServerMapSector * createSector(v2s16 p);
401 Blocks are generated by using these and makeBlock().
403 bool initBlockMake(BlockMakeData *data, v3s16 blockpos);
404 void finishBlockMake(BlockMakeData *data,
405 std::map<v3s16, MapBlock*> &changed_blocks);
408 Get a block from somewhere.
412 MapBlock * createBlock(v3s16 p);
415 Forcefully get a block from somewhere.
418 - Create blank filled with CONTENT_IGNORE
421 MapBlock * emergeBlock(v3s16 p, bool create_blank=true);
425 If it does not exist in memory, add it to the emerge queue.
427 - Emerge Queue (deferred disk or generate)
429 MapBlock *getBlockOrEmerge(v3s16 p3d);
431 // Carries out any initialization necessary before block is sent
432 void prepareBlock(MapBlock *block);
434 // Helper for placing objects on ground level
435 s16 findGroundLevel(v2s16 p2d);
438 Misc. helper functions for fiddling with directory and file
441 void createDirs(std::string path);
442 // returns something like "map/sectors/xxxxxxxx"
443 std::string getSectorDir(v2s16 pos, int layout = 2);
444 // dirname: final directory name
445 v2s16 getSectorPos(std::string dirname);
446 v3s16 getBlockPos(std::string sectordir, std::string blockfile);
447 static std::string getBlockFilename(v3s16 p);
452 // Verify we can read/write to the database
453 void verifyDatabase();
455 // Returns true if the database file does not exist
456 bool loadFromFolders();
458 // Call these before and after saving of blocks
462 void save(ModifiedState save_level);
463 void listAllLoadableBlocks(std::list<v3s16> &dst);
464 void listAllLoadedBlocks(std::list<v3s16> &dst);
465 // Saves map seed and possibly other stuff
469 /*void saveChunkMeta();
470 void loadChunkMeta();*/
472 // The sector mutex should be locked when calling most of these
474 // This only saves sector-specific data such as the heightmap
476 // DEPRECATED? Sectors have no metadata anymore.
477 void saveSectorMeta(ServerMapSector *sector);
478 MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
479 bool loadSectorMeta(v2s16 p2d);
481 // Full load of a sector including all blocks.
482 // returns true on success, false on failure.
483 bool loadSectorFull(v2s16 p2d);
484 // If sector is not found in memory, try to load it from disk.
485 // Returns true if sector now resides in memory
486 //bool deFlushSector(v2s16 p2d);
488 bool saveBlock(MapBlock *block, Database *db);
489 bool saveBlock(MapBlock *block);
490 // This will generate a sector with getSector if not found.
491 void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false);
492 MapBlock* loadBlock(v3s16 p);
494 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
496 void updateVManip(v3s16 pos);
498 // For debug printing
499 virtual void PrintInfo(std::ostream &out);
501 bool isSavingEnabled(){ return m_map_saving_enabled; }
508 EmergeManager *m_emerge;
510 std::string m_savedir;
511 bool m_map_saving_enabled;
514 // Chunk size in MapSectors
515 // If 0, chunks are disabled.
518 core::map<v2s16, MapChunk*> m_chunks;
522 Metadata is re-written on disk only if this is true.
523 This is reset to false when written on disk.
525 bool m_map_metadata_changed;
530 #define VMANIP_BLOCK_DATA_INEXIST 1
531 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
533 class ManualMapVoxelManipulator : public VoxelManipulator
536 ManualMapVoxelManipulator(Map *map);
537 virtual ~ManualMapVoxelManipulator();
541 VoxelManipulator::clear();
542 m_loaded_blocks.clear();
545 void setMap(Map *map)
548 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
549 bool load_if_inexistent = true);
551 // This is much faster with big chunks of generated data
552 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
553 bool overwrite_generated = true);
562 value = flags describing the block
564 std::map<v3s16, u8> m_loaded_blocks;