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 metadata of block changed (not knowing which node exactly)
65 // p stores block coordinate
66 MEET_BLOCK_NODE_METADATA_CHANGED,
67 // Anything else (modified_blocks are set unsent)
73 MapEditEventType type;
76 std::set<v3s16> modified_blocks;
77 u16 already_known_by_peer;
81 already_known_by_peer(0)
85 MapEditEvent * clone()
87 MapEditEvent *event = new MapEditEvent();
91 event->modified_blocks = modified_blocks;
100 case MEET_REMOVENODE:
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(std::set<v3s16>::iterator
112 i = modified_blocks.begin();
113 i != modified_blocks.end(); ++i)
116 v3s16 np1 = p*MAP_BLOCKSIZE;
117 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
128 class MapEventReceiver
131 // event shall be deleted by caller after the call.
132 virtual void onMapEditEvent(MapEditEvent *event) = 0;
135 class Map /*: public NodeContainer*/
139 Map(std::ostream &dout, IGameDef *gamedef);
142 /*virtual u16 nodeContainerId() const
144 return NODECONTAINER_ID_MAP;
147 virtual s32 mapType() const
153 Drop (client) or delete (server) the map.
160 void addEventReceiver(MapEventReceiver *event_receiver);
161 void removeEventReceiver(MapEventReceiver *event_receiver);
162 // event shall be deleted by caller after the call.
163 void dispatchEvent(MapEditEvent *event);
165 // On failure returns NULL
166 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
167 // Same as the above (there exists no lock anymore)
168 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
169 // On failure throws InvalidPositionException
170 MapSector * getSectorNoGenerate(v2s16 p2d);
171 // Gets an existing sector or creates an empty one
172 //MapSector * getSectorCreate(v2s16 p2d);
175 This is overloaded by ClientMap and ServerMap to allow
176 their differing fetch methods.
178 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
179 virtual MapSector * emergeSector(v2s16 p,
180 std::map<v3s16, MapBlock*> &changed_blocks){ return NULL; }
182 // Returns InvalidPositionException if not found
183 MapBlock * getBlockNoCreate(v3s16 p);
184 // Returns NULL if not found
185 MapBlock * getBlockNoCreateNoEx(v3s16 p);
187 /* Server overrides */
188 virtual MapBlock * emergeBlock(v3s16 p, bool allow_generate=true)
189 { return getBlockNoCreateNoEx(p); }
191 // Returns InvalidPositionException if not found
192 bool isNodeUnderground(v3s16 p);
194 bool isValidPosition(v3s16 p);
196 // throws InvalidPositionException if not found
197 MapNode getNode(v3s16 p);
199 // throws InvalidPositionException if not found
200 void setNode(v3s16 p, MapNode & n);
202 // Returns a CONTENT_IGNORE node if not found
203 MapNode getNodeNoEx(v3s16 p);
205 void unspreadLight(enum LightBank bank,
206 std::map<v3s16, u8> & from_nodes,
207 std::set<v3s16> & light_sources,
208 std::map<v3s16, MapBlock*> & modified_blocks);
210 void unLightNeighbors(enum LightBank bank,
211 v3s16 pos, u8 lightwas,
212 std::set<v3s16> & light_sources,
213 std::map<v3s16, MapBlock*> & modified_blocks);
215 void spreadLight(enum LightBank bank,
216 std::set<v3s16> & from_nodes,
217 std::map<v3s16, MapBlock*> & modified_blocks);
219 void lightNeighbors(enum LightBank bank,
221 std::map<v3s16, MapBlock*> & modified_blocks);
223 v3s16 getBrightestNeighbour(enum LightBank bank, v3s16 p);
225 s16 propagateSunlight(v3s16 start,
226 std::map<v3s16, MapBlock*> & modified_blocks);
228 void updateLighting(enum LightBank bank,
229 std::map<v3s16, MapBlock*> & a_blocks,
230 std::map<v3s16, MapBlock*> & modified_blocks);
232 void updateLighting(std::map<v3s16, MapBlock*> & a_blocks,
233 std::map<v3s16, MapBlock*> & modified_blocks);
236 These handle lighting but not faces.
238 void addNodeAndUpdate(v3s16 p, MapNode n,
239 std::map<v3s16, MapBlock*> &modified_blocks);
240 void removeNodeAndUpdate(v3s16 p,
241 std::map<v3s16, MapBlock*> &modified_blocks);
244 Wrappers for the latter ones.
246 Return true if succeeded, false if not.
248 bool addNodeWithEvent(v3s16 p, MapNode n);
249 bool removeNodeWithEvent(v3s16 p);
252 Takes the blocks at the edges into account
254 bool getDayNightDiff(v3s16 blockpos);
256 //core::aabbox3d<s16> getDisplayedBlockArea();
258 //bool updateChangedVisibleArea();
260 // Call these before and after saving of many blocks
261 virtual void beginSave() {return;};
262 virtual void endSave() {return;};
264 virtual void save(ModifiedState save_level){assert(0);};
266 // Server implements this.
267 // Client leaves it as no-op.
268 virtual void saveBlock(MapBlock *block){};
271 Updates usage timers and unloads unused blocks and sectors.
272 Saves modified blocks before unloading on MAPTYPE_SERVER.
274 void timerUpdate(float dtime, float unload_timeout,
275 std::list<v3s16> *unloaded_blocks=NULL);
278 Unloads all blocks with a zero refCount().
279 Saves modified blocks before unloading on MAPTYPE_SERVER.
281 void unloadUnreferencedBlocks(std::list<v3s16> *unloaded_blocks=NULL);
283 // Deletes sectors and their blocks from memory
284 // Takes cache into account
285 // If deleted sector is in sector cache, clears cache
286 void deleteSectors(std::list<v2s16> &list);
291 = flush changed to disk and delete from memory, if usage timer of
292 block is more than timeout
294 void unloadUnusedData(float timeout,
295 core::list<v3s16> *deleted_blocks=NULL);
298 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
299 virtual void PrintInfo(std::ostream &out);
301 void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks);
302 void transformLiquidsFinite(std::map<v3s16, MapBlock*> & modified_blocks);
306 These are basically coordinate wrappers to MapBlock
309 NodeMetadata* getNodeMetadata(v3s16 p);
312 * Sets metadata for a node.
313 * This method sets the metadata for a given node.
314 * On success, it returns @c true and the object pointed to
315 * by @p meta is then managed by the system and should
316 * not be deleted by the caller.
318 * In case of failure, the method returns @c false and the
319 * caller is still responsible for deleting the object!
321 * @param p node coordinates
322 * @param meta pointer to @c NodeMetadata object
323 * @return @c true on success, false on failure
325 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
326 void removeNodeMetadata(v3s16 p);
330 These are basically coordinate wrappers to MapBlock
333 NodeTimer getNodeTimer(v3s16 p);
334 void setNodeTimer(v3s16 p, NodeTimer t);
335 void removeNodeTimer(v3s16 p);
340 std::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
346 void transforming_liquid_add(v3s16 p);
347 s32 transforming_liquid_size();
349 virtual s16 getHeat(v3s16 p);
350 virtual s16 getHumidity(v3s16 p);
353 friend class LuaVoxelManip;
355 std::ostream &m_dout; // A bit deprecated, could be removed
359 std::set<MapEventReceiver*> m_event_receivers;
361 std::map<v2s16, MapSector*> m_sectors;
363 // Be sure to set this to NULL when the cached sector is deleted
364 MapSector *m_sector_cache;
365 v2s16 m_sector_cache_p;
367 // Queued transforming water nodes
368 UniqueQueue<v3s16> m_transforming_liquid;
374 This is the only map class that is able to generate map.
377 class ServerMap : public Map
381 savedir: directory to which map data should be saved
383 ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emerge);
388 return MAPTYPE_SERVER;
392 Get a sector from somewhere.
394 - Check disk (doesn't load blocks)
397 ServerMapSector * createSector(v2s16 p);
400 Blocks are generated by using these and makeBlock().
402 bool initBlockMake(BlockMakeData *data, v3s16 blockpos);
403 MapBlock *finishBlockMake(BlockMakeData *data,
404 std::map<v3s16, MapBlock*> &changed_blocks);
407 Get a block from somewhere.
411 MapBlock * createBlock(v3s16 p);
414 Forcefully get a block from somewhere.
417 - Create blank filled with CONTENT_IGNORE
420 MapBlock * emergeBlock(v3s16 p, bool create_blank=true);
422 // Carries out any initialization necessary before block is sent
423 void prepareBlock(MapBlock *block);
425 // Helper for placing objects on ground level
426 s16 findGroundLevel(v2s16 p2d);
429 Misc. helper functions for fiddling with directory and file
432 void createDirs(std::string path);
433 // returns something like "map/sectors/xxxxxxxx"
434 std::string getSectorDir(v2s16 pos, int layout = 2);
435 // dirname: final directory name
436 v2s16 getSectorPos(std::string dirname);
437 v3s16 getBlockPos(std::string sectordir, std::string blockfile);
438 static std::string getBlockFilename(v3s16 p);
443 // Verify we can read/write to the database
444 void verifyDatabase();
446 // Returns true if the database file does not exist
447 bool loadFromFolders();
449 // Call these before and after saving of blocks
453 void save(ModifiedState save_level);
454 void listAllLoadableBlocks(std::list<v3s16> &dst);
455 void listAllLoadedBlocks(std::list<v3s16> &dst);
456 // Saves map seed and possibly other stuff
460 /*void saveChunkMeta();
461 void loadChunkMeta();*/
463 // The sector mutex should be locked when calling most of these
465 // This only saves sector-specific data such as the heightmap
467 // DEPRECATED? Sectors have no metadata anymore.
468 void saveSectorMeta(ServerMapSector *sector);
469 MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
470 bool loadSectorMeta(v2s16 p2d);
472 // Full load of a sector including all blocks.
473 // returns true on success, false on failure.
474 bool loadSectorFull(v2s16 p2d);
475 // If sector is not found in memory, try to load it from disk.
476 // Returns true if sector now resides in memory
477 //bool deFlushSector(v2s16 p2d);
479 void saveBlock(MapBlock *block);
480 // This will generate a sector with getSector if not found.
481 void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false);
482 MapBlock* loadBlock(v3s16 p);
484 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
486 // For debug printing
487 virtual void PrintInfo(std::ostream &out);
489 bool isSavingEnabled(){ return m_map_saving_enabled; }
491 u64 getSeed(){ return m_seed; }
493 MapgenParams *getMapgenParams(){ return m_mgparams; }
495 // Parameters fed to the Mapgen
496 MapgenParams *m_mgparams;
498 virtual s16 updateBlockHeat(ServerEnvironment *env, v3s16 p, MapBlock *block = NULL);
499 virtual s16 updateBlockHumidity(ServerEnvironment *env, v3s16 p, MapBlock *block = NULL);
502 // Seed used for all kinds of randomness in generation
506 EmergeManager *m_emerge;
508 std::string m_savedir;
509 bool m_map_saving_enabled;
512 // Chunk size in MapSectors
513 // If 0, chunks are disabled.
516 core::map<v2s16, MapChunk*> m_chunks;
520 Metadata is re-written on disk only if this is true.
521 This is reset to false when written on disk.
523 bool m_map_metadata_changed;
527 #define VMANIP_BLOCK_DATA_INEXIST 1
528 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
530 class MapVoxelManipulator : public VoxelManipulator
533 MapVoxelManipulator(Map *map);
534 virtual ~MapVoxelManipulator();
538 VoxelManipulator::clear();
539 m_loaded_blocks.clear();
542 virtual void emerge(VoxelArea a, s32 caller_id=-1);
544 void blitBack(std::map<v3s16, MapBlock*> & modified_blocks);
550 value = flags describing the block
552 std::map<v3s16, u8> m_loaded_blocks;
555 class ManualMapVoxelManipulator : public MapVoxelManipulator
558 ManualMapVoxelManipulator(Map *map);
559 virtual ~ManualMapVoxelManipulator();
561 void setMap(Map *map)
564 virtual void emerge(VoxelArea a, s32 caller_id=-1);
566 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
567 bool load_if_inexistent = true);
569 // This is much faster with big chunks of generated data
570 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks);