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"
43 class ServerMapSector;
47 class IRollbackReportSink;
49 class ServerEnvironment;
58 #define MAPTYPE_BASE 0
59 #define MAPTYPE_SERVER 1
60 #define MAPTYPE_CLIENT 2
62 enum MapEditEventType{
63 // Node added (changed from air or something else to something)
65 // Node removed (changed to air)
67 // Node metadata of block changed (not knowing which node exactly)
68 // p stores block coordinate
69 MEET_BLOCK_NODE_METADATA_CHANGED,
70 // Anything else (modified_blocks are set unsent)
76 MapEditEventType type;
79 std::set<v3s16> modified_blocks;
80 u16 already_known_by_peer;
84 already_known_by_peer(0)
88 MapEditEvent * clone()
90 MapEditEvent *event = new MapEditEvent();
94 event->modified_blocks = modified_blocks;
103 case MEET_REMOVENODE:
105 case MEET_BLOCK_NODE_METADATA_CHANGED:
107 v3s16 np1 = p*MAP_BLOCKSIZE;
108 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
109 return VoxelArea(np1, np2);
114 for(std::set<v3s16>::iterator
115 i = modified_blocks.begin();
116 i != modified_blocks.end(); ++i)
119 v3s16 np1 = p*MAP_BLOCKSIZE;
120 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
131 class MapEventReceiver
134 // event shall be deleted by caller after the call.
135 virtual void onMapEditEvent(MapEditEvent *event) = 0;
138 class Map /*: public NodeContainer*/
142 Map(std::ostream &dout, IGameDef *gamedef);
145 /*virtual u16 nodeContainerId() const
147 return NODECONTAINER_ID_MAP;
150 virtual s32 mapType() const
156 Drop (client) or delete (server) the map.
163 void addEventReceiver(MapEventReceiver *event_receiver);
164 void removeEventReceiver(MapEventReceiver *event_receiver);
165 // event shall be deleted by caller after the call.
166 void dispatchEvent(MapEditEvent *event);
168 // On failure returns NULL
169 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
170 // Same as the above (there exists no lock anymore)
171 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
172 // On failure throws InvalidPositionException
173 MapSector * getSectorNoGenerate(v2s16 p2d);
174 // Gets an existing sector or creates an empty one
175 //MapSector * getSectorCreate(v2s16 p2d);
178 This is overloaded by ClientMap and ServerMap to allow
179 their differing fetch methods.
181 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
182 virtual MapSector * emergeSector(v2s16 p,
183 std::map<v3s16, MapBlock*> &changed_blocks){ return NULL; }
185 // Returns InvalidPositionException if not found
186 MapBlock * getBlockNoCreate(v3s16 p);
187 // Returns NULL if not found
188 MapBlock * getBlockNoCreateNoEx(v3s16 p);
190 /* Server overrides */
191 virtual MapBlock * emergeBlock(v3s16 p, bool allow_generate=true)
192 { return getBlockNoCreateNoEx(p); }
194 // Returns InvalidPositionException if not found
195 bool isNodeUnderground(v3s16 p);
197 bool isValidPosition(v3s16 p);
199 // throws InvalidPositionException if not found
200 MapNode getNode(v3s16 p);
202 // throws InvalidPositionException if not found
203 void setNode(v3s16 p, MapNode & n);
205 // Returns a CONTENT_IGNORE node if not found
206 MapNode getNodeNoEx(v3s16 p);
208 void unspreadLight(enum LightBank bank,
209 std::map<v3s16, u8> & from_nodes,
210 std::set<v3s16> & light_sources,
211 std::map<v3s16, MapBlock*> & modified_blocks);
213 void unLightNeighbors(enum LightBank bank,
214 v3s16 pos, u8 lightwas,
215 std::set<v3s16> & light_sources,
216 std::map<v3s16, MapBlock*> & modified_blocks);
218 void spreadLight(enum LightBank bank,
219 std::set<v3s16> & from_nodes,
220 std::map<v3s16, MapBlock*> & modified_blocks);
222 void lightNeighbors(enum LightBank bank,
224 std::map<v3s16, MapBlock*> & modified_blocks);
226 v3s16 getBrightestNeighbour(enum LightBank bank, v3s16 p);
228 s16 propagateSunlight(v3s16 start,
229 std::map<v3s16, MapBlock*> & modified_blocks);
231 void updateLighting(enum LightBank bank,
232 std::map<v3s16, MapBlock*> & a_blocks,
233 std::map<v3s16, MapBlock*> & modified_blocks);
235 void updateLighting(std::map<v3s16, MapBlock*> & a_blocks,
236 std::map<v3s16, MapBlock*> & modified_blocks);
239 These handle lighting but not faces.
241 void addNodeAndUpdate(v3s16 p, MapNode n,
242 std::map<v3s16, MapBlock*> &modified_blocks);
243 void removeNodeAndUpdate(v3s16 p,
244 std::map<v3s16, MapBlock*> &modified_blocks);
247 Wrappers for the latter ones.
249 Return true if succeeded, false if not.
251 bool addNodeWithEvent(v3s16 p, MapNode n);
252 bool removeNodeWithEvent(v3s16 p);
255 Takes the blocks at the edges into account
257 bool getDayNightDiff(v3s16 blockpos);
259 //core::aabbox3d<s16> getDisplayedBlockArea();
261 //bool updateChangedVisibleArea();
263 // Call these before and after saving of many blocks
264 virtual void beginSave() {return;};
265 virtual void endSave() {return;};
267 virtual void save(ModifiedState save_level){assert(0);};
269 // Server implements this.
270 // Client leaves it as no-op.
271 virtual void saveBlock(MapBlock *block){};
274 Updates usage timers and unloads unused blocks and sectors.
275 Saves modified blocks before unloading on MAPTYPE_SERVER.
277 void timerUpdate(float dtime, float unload_timeout,
278 std::list<v3s16> *unloaded_blocks=NULL);
281 Unloads all blocks with a zero refCount().
282 Saves modified blocks before unloading on MAPTYPE_SERVER.
284 void unloadUnreferencedBlocks(std::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(std::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(std::map<v3s16, MapBlock*> & modified_blocks);
305 void transformLiquidsFinite(std::map<v3s16, MapBlock*> & modified_blocks);
309 These are basically coordinate wrappers to MapBlock
312 NodeMetadata* getNodeMetadata(v3s16 p);
313 void setNodeMetadata(v3s16 p, NodeMetadata *meta);
314 void removeNodeMetadata(v3s16 p);
318 These are basically coordinate wrappers to MapBlock
321 NodeTimer getNodeTimer(v3s16 p);
322 void setNodeTimer(v3s16 p, NodeTimer t);
323 void removeNodeTimer(v3s16 p);
328 std::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
334 void transforming_liquid_add(v3s16 p);
335 s32 transforming_liquid_size();
337 virtual s16 getHeat(v3s16 p);
338 virtual s16 getHumidity(v3s16 p);
341 friend class LuaVoxelManip;
343 std::ostream &m_dout; // A bit deprecated, could be removed
347 std::set<MapEventReceiver*> m_event_receivers;
349 std::map<v2s16, MapSector*> m_sectors;
351 // Be sure to set this to NULL when the cached sector is deleted
352 MapSector *m_sector_cache;
353 v2s16 m_sector_cache_p;
355 // Queued transforming water nodes
356 UniqueQueue<v3s16> m_transforming_liquid;
362 This is the only map class that is able to generate map.
365 class ServerMap : public Map
369 savedir: directory to which map data should be saved
371 ServerMap(std::string savedir, IGameDef *gamedef, EmergeManager *emerge);
376 return MAPTYPE_SERVER;
380 Get a sector from somewhere.
382 - Check disk (doesn't load blocks)
385 ServerMapSector * createSector(v2s16 p);
388 Blocks are generated by using these and makeBlock().
390 bool initBlockMake(BlockMakeData *data, v3s16 blockpos);
391 MapBlock *finishBlockMake(BlockMakeData *data,
392 std::map<v3s16, MapBlock*> &changed_blocks);
395 Get a block from somewhere.
399 MapBlock * createBlock(v3s16 p);
402 Forcefully get a block from somewhere.
405 - Create blank filled with CONTENT_IGNORE
408 MapBlock * emergeBlock(v3s16 p, bool create_blank=true);
410 // Helper for placing objects on ground level
411 s16 findGroundLevel(v2s16 p2d);
414 Misc. helper functions for fiddling with directory and file
417 void createDirs(std::string path);
418 // returns something like "map/sectors/xxxxxxxx"
419 std::string getSectorDir(v2s16 pos, int layout = 2);
420 // dirname: final directory name
421 v2s16 getSectorPos(std::string dirname);
422 v3s16 getBlockPos(std::string sectordir, std::string blockfile);
423 static std::string getBlockFilename(v3s16 p);
428 // Create the database structure
429 void createDatabase();
430 // Verify we can read/write to the database
431 void verifyDatabase();
432 // Get an integer suitable for a block
433 static sqlite3_int64 getBlockAsInteger(const v3s16 pos);
434 static v3s16 getIntegerAsBlock(sqlite3_int64 i);
436 // Returns true if the database file does not exist
437 bool loadFromFolders();
439 // Call these before and after saving of blocks
443 void save(ModifiedState save_level);
444 void listAllLoadableBlocks(std::list<v3s16> &dst);
445 void listAllLoadedBlocks(std::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 virtual s16 getHeat(ServerEnvironment *env, v3s16 p, MapBlock *block = NULL);
489 virtual s16 getHumidity(ServerEnvironment *env, v3s16 p, MapBlock *block = NULL);
492 // Seed used for all kinds of randomness in generation
496 EmergeManager *m_emerge;
498 std::string m_savedir;
499 bool m_map_saving_enabled;
502 // Chunk size in MapSectors
503 // If 0, chunks are disabled.
506 core::map<v2s16, MapChunk*> m_chunks;
510 Metadata is re-written on disk only if this is true.
511 This is reset to false when written on disk.
513 bool m_map_metadata_changed;
516 SQLite database and statements
519 sqlite3_stmt *m_database_read;
520 sqlite3_stmt *m_database_write;
521 sqlite3_stmt *m_database_list;
524 #define VMANIP_BLOCK_DATA_INEXIST 1
525 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
527 class MapVoxelManipulator : public VoxelManipulator
530 MapVoxelManipulator(Map *map);
531 virtual ~MapVoxelManipulator();
535 VoxelManipulator::clear();
536 m_loaded_blocks.clear();
539 virtual void emerge(VoxelArea a, s32 caller_id=-1);
541 void blitBack(std::map<v3s16, MapBlock*> & modified_blocks);
547 value = flags describing the block
549 std::map<v3s16, u8> m_loaded_blocks;
552 class ManualMapVoxelManipulator : public MapVoxelManipulator
555 ManualMapVoxelManipulator(Map *map);
556 virtual ~ManualMapVoxelManipulator();
558 void setMap(Map *map)
561 virtual void emerge(VoxelArea a, s32 caller_id=-1);
563 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
564 bool load_if_inexistent = true);
566 // This is much faster with big chunks of generated data
567 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks);