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.
24 #include <jmutexautolock.h>
32 #include "irrlichttypes_bloated.h"
34 #include "constants.h"
36 #include "mapgen.h" //for BlockMakeData and EmergeManager
37 #include "modifiedstate.h"
38 #include "util/container.h"
39 #include "nodetimer.h"
47 class ServerMapSector;
51 class IRollbackReportSink;
60 #define MAPTYPE_BASE 0
61 #define MAPTYPE_SERVER 1
62 #define MAPTYPE_CLIENT 2
64 enum MapEditEventType{
65 // Node added (changed from air or something else to something)
67 // Node removed (changed to air)
69 // Node metadata of block changed (not knowing which node exactly)
70 // p stores block coordinate
71 MEET_BLOCK_NODE_METADATA_CHANGED,
72 // Anything else (modified_blocks are set unsent)
78 MapEditEventType type;
81 std::set<v3s16> modified_blocks;
82 u16 already_known_by_peer;
86 already_known_by_peer(0)
90 MapEditEvent * clone()
92 MapEditEvent *event = new MapEditEvent();
96 event->modified_blocks = modified_blocks;
105 case MEET_REMOVENODE:
107 case MEET_BLOCK_NODE_METADATA_CHANGED:
109 v3s16 np1 = p*MAP_BLOCKSIZE;
110 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
111 return VoxelArea(np1, np2);
116 for(std::set<v3s16>::iterator
117 i = modified_blocks.begin();
118 i != modified_blocks.end(); ++i)
121 v3s16 np1 = p*MAP_BLOCKSIZE;
122 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
133 class MapEventReceiver
136 // event shall be deleted by caller after the call.
137 virtual void onMapEditEvent(MapEditEvent *event) = 0;
140 class Map /*: public NodeContainer*/
144 Map(std::ostream &dout, IGameDef *gamedef);
147 /*virtual u16 nodeContainerId() const
149 return NODECONTAINER_ID_MAP;
152 virtual s32 mapType() const
158 Drop (client) or delete (server) the map.
165 void addEventReceiver(MapEventReceiver *event_receiver);
166 void removeEventReceiver(MapEventReceiver *event_receiver);
167 // event shall be deleted by caller after the call.
168 void dispatchEvent(MapEditEvent *event);
170 // On failure returns NULL
171 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
172 // Same as the above (there exists no lock anymore)
173 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
174 // On failure throws InvalidPositionException
175 MapSector * getSectorNoGenerate(v2s16 p2d);
176 // Gets an existing sector or creates an empty one
177 //MapSector * getSectorCreate(v2s16 p2d);
180 This is overloaded by ClientMap and ServerMap to allow
181 their differing fetch methods.
183 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
184 virtual MapSector * emergeSector(v2s16 p,
185 std::map<v3s16, MapBlock*> &changed_blocks){ return NULL; }
187 // Returns InvalidPositionException if not found
188 MapBlock * getBlockNoCreate(v3s16 p);
189 // Returns NULL if not found
190 MapBlock * getBlockNoCreateNoEx(v3s16 p);
192 /* Server overrides */
193 virtual MapBlock * emergeBlock(v3s16 p, bool allow_generate=true)
194 { return getBlockNoCreateNoEx(p); }
196 // Returns InvalidPositionException if not found
197 bool isNodeUnderground(v3s16 p);
199 bool isValidPosition(v3s16 p);
201 // throws InvalidPositionException if not found
202 MapNode getNode(v3s16 p);
204 // throws InvalidPositionException if not found
205 void setNode(v3s16 p, MapNode & n);
207 // Returns a CONTENT_IGNORE node if not found
208 MapNode getNodeNoEx(v3s16 p);
210 void unspreadLight(enum LightBank bank,
211 std::map<v3s16, u8> & from_nodes,
212 std::set<v3s16> & light_sources,
213 std::map<v3s16, MapBlock*> & modified_blocks);
215 void unLightNeighbors(enum LightBank bank,
216 v3s16 pos, u8 lightwas,
217 std::set<v3s16> & light_sources,
218 std::map<v3s16, MapBlock*> & modified_blocks);
220 void spreadLight(enum LightBank bank,
221 std::set<v3s16> & from_nodes,
222 std::map<v3s16, MapBlock*> & modified_blocks);
224 void lightNeighbors(enum LightBank bank,
226 std::map<v3s16, MapBlock*> & modified_blocks);
228 v3s16 getBrightestNeighbour(enum LightBank bank, v3s16 p);
230 s16 propagateSunlight(v3s16 start,
231 std::map<v3s16, MapBlock*> & modified_blocks);
233 void updateLighting(enum LightBank bank,
234 std::map<v3s16, MapBlock*> & a_blocks,
235 std::map<v3s16, MapBlock*> & modified_blocks);
237 void updateLighting(std::map<v3s16, MapBlock*> & a_blocks,
238 std::map<v3s16, MapBlock*> & modified_blocks);
241 These handle lighting but not faces.
243 void addNodeAndUpdate(v3s16 p, MapNode n,
244 std::map<v3s16, MapBlock*> &modified_blocks);
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);
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 void saveBlock(MapBlock *block){};
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);
307 void transformLiquidsFinite(std::map<v3s16, MapBlock*> & modified_blocks);
311 These are basically coordinate wrappers to MapBlock
314 NodeMetadata* getNodeMetadata(v3s16 p);
315 void setNodeMetadata(v3s16 p, NodeMetadata *meta);
316 void removeNodeMetadata(v3s16 p);
320 These are basically coordinate wrappers to MapBlock
323 NodeTimer getNodeTimer(v3s16 p);
324 void setNodeTimer(v3s16 p, NodeTimer t);
325 void removeNodeTimer(v3s16 p);
330 std::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
336 void transforming_liquid_add(v3s16 p);
337 s32 transforming_liquid_size();
340 friend class LuaVoxelManip;
342 std::ostream &m_dout; // A bit deprecated, could be removed
346 std::set<MapEventReceiver*> m_event_receivers;
348 std::map<v2s16, MapSector*> m_sectors;
350 // Be sure to set this to NULL when the cached sector is deleted
351 MapSector *m_sector_cache;
352 v2s16 m_sector_cache_p;
354 // Queued transforming water nodes
355 UniqueQueue<v3s16> m_transforming_liquid;
361 This is the only map class that is able to generate map.
364 class ServerMap : public Map
368 savedir: directory to which map data should be saved
370 ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emerge);
375 return MAPTYPE_SERVER;
379 Get a sector from somewhere.
381 - Check disk (doesn't load blocks)
384 ServerMapSector * createSector(v2s16 p);
387 Blocks are generated by using these and makeBlock().
389 bool initBlockMake(BlockMakeData *data, v3s16 blockpos);
390 MapBlock *finishBlockMake(BlockMakeData *data,
391 std::map<v3s16, MapBlock*> &changed_blocks);
394 Get a block from somewhere.
398 MapBlock * createBlock(v3s16 p);
401 Forcefully get a block from somewhere.
404 - Create blank filled with CONTENT_IGNORE
407 MapBlock * emergeBlock(v3s16 p, bool create_blank=true);
409 // Helper for placing objects on ground level
410 s16 findGroundLevel(v2s16 p2d);
413 Misc. helper functions for fiddling with directory and file
416 void createDirs(std::string path);
417 // returns something like "map/sectors/xxxxxxxx"
418 std::string getSectorDir(v2s16 pos, int layout = 2);
419 // dirname: final directory name
420 v2s16 getSectorPos(std::string dirname);
421 v3s16 getBlockPos(std::string sectordir, std::string blockfile);
422 static std::string getBlockFilename(v3s16 p);
427 // Create the database structure
428 void createDatabase();
429 // Verify we can read/write to the database
430 void verifyDatabase();
431 // Get an integer suitable for a block
432 static sqlite3_int64 getBlockAsInteger(const v3s16 pos);
433 static v3s16 getIntegerAsBlock(sqlite3_int64 i);
435 // Returns true if the database file does not exist
436 bool loadFromFolders();
438 // Call these before and after saving of blocks
442 void save(ModifiedState save_level);
443 void listAllLoadableBlocks(std::list<v3s16> &dst);
444 void listAllLoadedBlocks(std::list<v3s16> &dst);
445 // Saves map seed and possibly other stuff
449 /*void saveChunkMeta();
450 void loadChunkMeta();*/
452 // The sector mutex should be locked when calling most of these
454 // This only saves sector-specific data such as the heightmap
456 // DEPRECATED? Sectors have no metadata anymore.
457 void saveSectorMeta(ServerMapSector *sector);
458 MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
459 bool loadSectorMeta(v2s16 p2d);
461 // Full load of a sector including all blocks.
462 // returns true on success, false on failure.
463 bool loadSectorFull(v2s16 p2d);
464 // If sector is not found in memory, try to load it from disk.
465 // Returns true if sector now resides in memory
466 //bool deFlushSector(v2s16 p2d);
468 void saveBlock(MapBlock *block);
469 // This will generate a sector with getSector if not found.
470 void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false);
471 MapBlock* loadBlock(v3s16 p);
473 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
475 // For debug printing
476 virtual void PrintInfo(std::ostream &out);
478 bool isSavingEnabled(){ return m_map_saving_enabled; }
480 u64 getSeed(){ return m_seed; }
482 MapgenParams *getMapgenParams(){ return m_mgparams; }
484 // Parameters fed to the Mapgen
485 MapgenParams *m_mgparams;
487 // Seed used for all kinds of randomness in generation
491 EmergeManager *m_emerge;
493 std::string m_savedir;
494 bool m_map_saving_enabled;
497 // Chunk size in MapSectors
498 // If 0, chunks are disabled.
501 core::map<v2s16, MapChunk*> m_chunks;
505 Metadata is re-written on disk only if this is true.
506 This is reset to false when written on disk.
508 bool m_map_metadata_changed;
511 SQLite database and statements
514 sqlite3_stmt *m_database_read;
515 sqlite3_stmt *m_database_write;
516 sqlite3_stmt *m_database_list;
519 #define VMANIP_BLOCK_DATA_INEXIST 1
520 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
522 class MapVoxelManipulator : public VoxelManipulator
525 MapVoxelManipulator(Map *map);
526 virtual ~MapVoxelManipulator();
530 VoxelManipulator::clear();
531 m_loaded_blocks.clear();
534 virtual void emerge(VoxelArea a, s32 caller_id=-1);
536 void blitBack(std::map<v3s16, MapBlock*> & modified_blocks);
542 value = flags describing the block
544 std::map<v3s16, u8> m_loaded_blocks;
547 class ManualMapVoxelManipulator : public MapVoxelManipulator
550 ManualMapVoxelManipulator(Map *map);
551 virtual ~ManualMapVoxelManipulator();
553 void setMap(Map *map)
556 virtual void emerge(VoxelArea a, s32 caller_id=-1);
558 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
559 bool load_if_inexistent = true);
561 // This is much faster with big chunks of generated data
562 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks);