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>
29 #include "irrlichttypes_bloated.h"
31 #include "constants.h"
33 #include "mapgen.h" //for BlockMakeData and EmergeManager
34 #include "modifiedstate.h"
35 #include "util/container.h"
36 #include "nodetimer.h"
44 class ServerMapSector;
48 class IRollbackReportSink;
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 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 core::map<v3s16, bool> modified_blocks;
79 u16 already_known_by_peer;
83 already_known_by_peer(0)
87 MapEditEvent * clone()
89 MapEditEvent *event = new MapEditEvent();
93 for(core::map<v3s16, bool>::Iterator
94 i = modified_blocks.getIterator();
95 i.atEnd()==false; i++)
97 v3s16 p = i.getNode()->getKey();
98 bool v = i.getNode()->getValue();
99 event->modified_blocks.insert(p, v);
109 case MEET_REMOVENODE:
111 case MEET_BLOCK_NODE_METADATA_CHANGED:
113 v3s16 np1 = p*MAP_BLOCKSIZE;
114 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
115 return VoxelArea(np1, np2);
120 for(core::map<v3s16, bool>::Iterator
121 i = modified_blocks.getIterator();
122 i.atEnd()==false; i++)
124 v3s16 p = i.getNode()->getKey();
125 v3s16 np1 = p*MAP_BLOCKSIZE;
126 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
137 class MapEventReceiver
140 // event shall be deleted by caller after the call.
141 virtual void onMapEditEvent(MapEditEvent *event) = 0;
144 class Map /*: public NodeContainer*/
148 Map(std::ostream &dout, IGameDef *gamedef);
151 /*virtual u16 nodeContainerId() const
153 return NODECONTAINER_ID_MAP;
156 virtual s32 mapType() const
162 Drop (client) or delete (server) the map.
169 void addEventReceiver(MapEventReceiver *event_receiver);
170 void removeEventReceiver(MapEventReceiver *event_receiver);
171 // event shall be deleted by caller after the call.
172 void dispatchEvent(MapEditEvent *event);
174 // On failure returns NULL
175 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
176 // Same as the above (there exists no lock anymore)
177 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
178 // On failure throws InvalidPositionException
179 MapSector * getSectorNoGenerate(v2s16 p2d);
180 // Gets an existing sector or creates an empty one
181 //MapSector * getSectorCreate(v2s16 p2d);
184 This is overloaded by ClientMap and ServerMap to allow
185 their differing fetch methods.
187 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
188 virtual MapSector * emergeSector(v2s16 p,
189 core::map<v3s16, MapBlock*> &changed_blocks){ return NULL; }
191 // Returns InvalidPositionException if not found
192 MapBlock * getBlockNoCreate(v3s16 p);
193 // Returns NULL if not found
194 MapBlock * getBlockNoCreateNoEx(v3s16 p);
196 /* Server overrides */
197 virtual MapBlock * emergeBlock(v3s16 p, bool allow_generate=true)
198 { return getBlockNoCreateNoEx(p); }
200 // Returns InvalidPositionException if not found
201 bool isNodeUnderground(v3s16 p);
203 bool isValidPosition(v3s16 p);
205 // throws InvalidPositionException if not found
206 MapNode getNode(v3s16 p);
208 // throws InvalidPositionException if not found
209 void setNode(v3s16 p, MapNode & n);
211 // Returns a CONTENT_IGNORE node if not found
212 MapNode getNodeNoEx(v3s16 p);
214 void unspreadLight(enum LightBank bank,
215 core::map<v3s16, u8> & from_nodes,
216 core::map<v3s16, bool> & light_sources,
217 core::map<v3s16, MapBlock*> & modified_blocks);
219 void unLightNeighbors(enum LightBank bank,
220 v3s16 pos, u8 lightwas,
221 core::map<v3s16, bool> & light_sources,
222 core::map<v3s16, MapBlock*> & modified_blocks);
224 void spreadLight(enum LightBank bank,
225 core::map<v3s16, bool> & from_nodes,
226 core::map<v3s16, MapBlock*> & modified_blocks);
228 void lightNeighbors(enum LightBank bank,
230 core::map<v3s16, MapBlock*> & modified_blocks);
232 v3s16 getBrightestNeighbour(enum LightBank bank, v3s16 p);
234 s16 propagateSunlight(v3s16 start,
235 core::map<v3s16, MapBlock*> & modified_blocks);
237 void updateLighting(enum LightBank bank,
238 core::map<v3s16, MapBlock*> & a_blocks,
239 core::map<v3s16, MapBlock*> & modified_blocks);
241 void updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
242 core::map<v3s16, MapBlock*> & modified_blocks);
245 These handle lighting but not faces.
247 void addNodeAndUpdate(v3s16 p, MapNode n,
248 core::map<v3s16, MapBlock*> &modified_blocks);
249 void removeNodeAndUpdate(v3s16 p,
250 core::map<v3s16, MapBlock*> &modified_blocks);
253 Wrappers for the latter ones.
255 Return true if succeeded, false if not.
257 bool addNodeWithEvent(v3s16 p, MapNode n);
258 bool removeNodeWithEvent(v3s16 p);
261 Takes the blocks at the edges into account
263 bool getDayNightDiff(v3s16 blockpos);
265 //core::aabbox3d<s16> getDisplayedBlockArea();
267 //bool updateChangedVisibleArea();
269 // Call these before and after saving of many blocks
270 virtual void beginSave() {return;};
271 virtual void endSave() {return;};
273 virtual void save(ModifiedState save_level){assert(0);};
275 // Server implements this.
276 // Client leaves it as no-op.
277 virtual void saveBlock(MapBlock *block){};
280 Updates usage timers and unloads unused blocks and sectors.
281 Saves modified blocks before unloading on MAPTYPE_SERVER.
283 void timerUpdate(float dtime, float unload_timeout,
284 core::list<v3s16> *unloaded_blocks=NULL);
286 // Deletes sectors and their blocks from memory
287 // Takes cache into account
288 // If deleted sector is in sector cache, clears cache
289 void deleteSectors(core::list<v2s16> &list);
294 = flush changed to disk and delete from memory, if usage timer of
295 block is more than timeout
297 void unloadUnusedData(float timeout,
298 core::list<v3s16> *deleted_blocks=NULL);
301 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
302 virtual void PrintInfo(std::ostream &out);
304 void transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks);
308 These are basically coordinate wrappers to MapBlock
311 NodeMetadata* getNodeMetadata(v3s16 p);
312 void setNodeMetadata(v3s16 p, NodeMetadata *meta);
313 void removeNodeMetadata(v3s16 p);
317 These are basically coordinate wrappers to MapBlock
320 NodeTimer getNodeTimer(v3s16 p);
321 void setNodeTimer(v3s16 p, NodeTimer t);
322 void removeNodeTimer(v3s16 p);
327 core::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
335 std::ostream &m_dout; // A bit deprecated, could be removed
339 core::map<MapEventReceiver*, bool> m_event_receivers;
341 core::map<v2s16, MapSector*> m_sectors;
343 // Be sure to set this to NULL when the cached sector is deleted
344 MapSector *m_sector_cache;
345 v2s16 m_sector_cache_p;
347 // Queued transforming water nodes
348 UniqueQueue<v3s16> m_transforming_liquid;
354 This is the only map class that is able to generate map.
357 class ServerMap : public Map
361 savedir: directory to which map data should be saved
363 ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emerge);
368 return MAPTYPE_SERVER;
372 Get a sector from somewhere.
374 - Check disk (doesn't load blocks)
377 ServerMapSector * createSector(v2s16 p);
380 Blocks are generated by using these and makeBlock().
382 void initBlockMake(BlockMakeData *data, v3s16 blockpos);
383 MapBlock *finishBlockMake(BlockMakeData *data,
384 core::map<v3s16, MapBlock*> &changed_blocks);
386 // A non-threaded wrapper to the above - DEFUNCT
387 /* MapBlock * generateBlock(
389 core::map<v3s16, MapBlock*> &modified_blocks
393 Get a block from somewhere.
397 MapBlock * createBlock(v3s16 p);
400 Forcefully get a block from somewhere.
403 - Create blank filled with CONTENT_IGNORE
406 MapBlock * emergeBlock(v3s16 p, bool create_blank=true);
408 // Helper for placing objects on ground level
409 s16 findGroundLevel(v2s16 p2d);
412 Misc. helper functions for fiddling with directory and file
415 void createDirs(std::string path);
416 // returns something like "map/sectors/xxxxxxxx"
417 std::string getSectorDir(v2s16 pos, int layout = 2);
418 // dirname: final directory name
419 v2s16 getSectorPos(std::string dirname);
420 v3s16 getBlockPos(std::string sectordir, std::string blockfile);
421 static std::string getBlockFilename(v3s16 p);
426 // Create the database structure
427 void createDatabase();
428 // Verify we can read/write to the database
429 void verifyDatabase();
430 // Get an integer suitable for a block
431 static sqlite3_int64 getBlockAsInteger(const v3s16 pos);
432 static v3s16 getIntegerAsBlock(sqlite3_int64 i);
434 // Returns true if the database file does not exist
435 bool loadFromFolders();
437 // Call these before and after saving of blocks
441 void save(ModifiedState save_level);
444 void listAllLoadableBlocks(core::list<v3s16> &dst);
446 // Saves map seed and possibly other stuff
450 /*void saveChunkMeta();
451 void loadChunkMeta();*/
453 // The sector mutex should be locked when calling most of these
455 // This only saves sector-specific data such as the heightmap
457 // DEPRECATED? Sectors have no metadata anymore.
458 void saveSectorMeta(ServerMapSector *sector);
459 MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
460 bool loadSectorMeta(v2s16 p2d);
462 // Full load of a sector including all blocks.
463 // returns true on success, false on failure.
464 bool loadSectorFull(v2s16 p2d);
465 // If sector is not found in memory, try to load it from disk.
466 // Returns true if sector now resides in memory
467 //bool deFlushSector(v2s16 p2d);
469 void saveBlock(MapBlock *block);
470 // This will generate a sector with getSector if not found.
471 void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false);
472 MapBlock* loadBlock(v3s16 p);
474 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
476 // For debug printing
477 virtual void PrintInfo(std::ostream &out);
479 bool isSavingEnabled(){ return m_map_saving_enabled; }
481 u64 getSeed(){ return m_seed; }
483 MapgenParams *getMapgenParams(){ return m_mgparams; }
485 // Parameters fed to the Mapgen
486 MapgenParams *m_mgparams;
488 // Seed used for all kinds of randomness in generation
492 EmergeManager *m_emerge;
494 std::string m_savedir;
495 bool m_map_saving_enabled;
498 // Chunk size in MapSectors
499 // If 0, chunks are disabled.
502 core::map<v2s16, MapChunk*> m_chunks;
506 Metadata is re-written on disk only if this is true.
507 This is reset to false when written on disk.
509 bool m_map_metadata_changed;
512 SQLite database and statements
515 sqlite3_stmt *m_database_read;
516 sqlite3_stmt *m_database_write;
517 sqlite3_stmt *m_database_list;
520 #define VMANIP_BLOCK_DATA_INEXIST 1
521 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
523 class MapVoxelManipulator : public VoxelManipulator
526 MapVoxelManipulator(Map *map);
527 virtual ~MapVoxelManipulator();
531 VoxelManipulator::clear();
532 m_loaded_blocks.clear();
535 virtual void emerge(VoxelArea a, s32 caller_id=-1);
537 void blitBack(core::map<v3s16, MapBlock*> & modified_blocks);
541 value = flags describing the block
543 core::map<v3s16, u8> m_loaded_blocks;
548 class ManualMapVoxelManipulator : public MapVoxelManipulator
551 ManualMapVoxelManipulator(Map *map);
552 virtual ~ManualMapVoxelManipulator();
554 void setMap(Map *map)
557 virtual void emerge(VoxelArea a, s32 caller_id=-1);
559 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max);
561 // This is much faster with big chunks of generated data
562 void blitBackAll(core::map<v3s16, MapBlock*> * modified_blocks);