3 Copyright (C) 2010-2011 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 "common_irrlicht.h"
31 #include "constants.h"
33 #include "utility.h" // Needed for UniqueQueue, a member of Map
34 #include "modifiedstate.h"
42 class ServerMapSector;
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 core::map<v3s16, bool> modified_blocks;
77 u16 already_known_by_peer;
81 already_known_by_peer(0)
85 MapEditEvent * clone()
87 MapEditEvent *event = new MapEditEvent();
91 for(core::map<v3s16, bool>::Iterator
92 i = modified_blocks.getIterator();
93 i.atEnd()==false; i++)
95 v3s16 p = i.getNode()->getKey();
96 bool v = i.getNode()->getValue();
97 event->modified_blocks.insert(p, v);
107 case MEET_REMOVENODE:
109 case MEET_BLOCK_NODE_METADATA_CHANGED:
111 v3s16 np1 = p*MAP_BLOCKSIZE;
112 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
113 return VoxelArea(np1, np2);
118 for(core::map<v3s16, bool>::Iterator
119 i = modified_blocks.getIterator();
120 i.atEnd()==false; i++)
122 v3s16 p = i.getNode()->getKey();
123 v3s16 np1 = p*MAP_BLOCKSIZE;
124 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
135 class MapEventReceiver
138 // event shall be deleted by caller after the call.
139 virtual void onMapEditEvent(MapEditEvent *event) = 0;
142 class Map /*: public NodeContainer*/
146 Map(std::ostream &dout, IGameDef *gamedef);
149 /*virtual u16 nodeContainerId() const
151 return NODECONTAINER_ID_MAP;
154 virtual s32 mapType() const
160 Drop (client) or delete (server) the map.
167 void addEventReceiver(MapEventReceiver *event_receiver);
168 void removeEventReceiver(MapEventReceiver *event_receiver);
169 // event shall be deleted by caller after the call.
170 void dispatchEvent(MapEditEvent *event);
172 // On failure returns NULL
173 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
174 // Same as the above (there exists no lock anymore)
175 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
176 // On failure throws InvalidPositionException
177 MapSector * getSectorNoGenerate(v2s16 p2d);
178 // Gets an existing sector or creates an empty one
179 //MapSector * getSectorCreate(v2s16 p2d);
182 This is overloaded by ClientMap and ServerMap to allow
183 their differing fetch methods.
185 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
186 virtual MapSector * emergeSector(v2s16 p,
187 core::map<v3s16, MapBlock*> &changed_blocks){ return NULL; }
189 // Returns InvalidPositionException if not found
190 MapBlock * getBlockNoCreate(v3s16 p);
191 // Returns NULL if not found
192 MapBlock * getBlockNoCreateNoEx(v3s16 p);
194 /* Server overrides */
195 virtual MapBlock * emergeBlock(v3s16 p, bool allow_generate=true)
196 { return getBlockNoCreateNoEx(p); }
198 // Returns InvalidPositionException if not found
199 bool isNodeUnderground(v3s16 p);
201 bool isValidPosition(v3s16 p);
203 // throws InvalidPositionException if not found
204 MapNode getNode(v3s16 p);
206 // throws InvalidPositionException if not found
207 void setNode(v3s16 p, MapNode & n);
209 // Returns a CONTENT_IGNORE node if not found
210 MapNode getNodeNoEx(v3s16 p);
212 void unspreadLight(enum LightBank bank,
213 core::map<v3s16, u8> & from_nodes,
214 core::map<v3s16, bool> & light_sources,
215 core::map<v3s16, MapBlock*> & modified_blocks);
217 void unLightNeighbors(enum LightBank bank,
218 v3s16 pos, u8 lightwas,
219 core::map<v3s16, bool> & light_sources,
220 core::map<v3s16, MapBlock*> & modified_blocks);
222 void spreadLight(enum LightBank bank,
223 core::map<v3s16, bool> & from_nodes,
224 core::map<v3s16, MapBlock*> & modified_blocks);
226 void lightNeighbors(enum LightBank bank,
228 core::map<v3s16, MapBlock*> & modified_blocks);
230 v3s16 getBrightestNeighbour(enum LightBank bank, v3s16 p);
232 s16 propagateSunlight(v3s16 start,
233 core::map<v3s16, MapBlock*> & modified_blocks);
235 void updateLighting(enum LightBank bank,
236 core::map<v3s16, MapBlock*> & a_blocks,
237 core::map<v3s16, MapBlock*> & modified_blocks);
239 void updateLighting(core::map<v3s16, MapBlock*> & a_blocks,
240 core::map<v3s16, MapBlock*> & modified_blocks);
243 These handle lighting but not faces.
245 void addNodeAndUpdate(v3s16 p, MapNode n,
246 core::map<v3s16, MapBlock*> &modified_blocks);
247 void removeNodeAndUpdate(v3s16 p,
248 core::map<v3s16, MapBlock*> &modified_blocks);
251 Wrappers for the latter ones.
253 Return true if succeeded, false if not.
255 bool addNodeWithEvent(v3s16 p, MapNode n);
256 bool removeNodeWithEvent(v3s16 p);
259 Takes the blocks at the edges into account
261 bool getDayNightDiff(v3s16 blockpos);
263 //core::aabbox3d<s16> getDisplayedBlockArea();
265 //bool updateChangedVisibleArea();
267 // Call these before and after saving of many blocks
268 virtual void beginSave() {return;};
269 virtual void endSave() {return;};
271 virtual void save(ModifiedState save_level){assert(0);};
273 // Server implements this.
274 // Client leaves it as no-op.
275 virtual void saveBlock(MapBlock *block){};
278 Updates usage timers and unloads unused blocks and sectors.
279 Saves modified blocks before unloading on MAPTYPE_SERVER.
281 void timerUpdate(float dtime, float unload_timeout,
282 core::list<v3s16> *unloaded_blocks=NULL);
284 // Deletes sectors and their blocks from memory
285 // Takes cache into account
286 // If deleted sector is in sector cache, clears cache
287 void deleteSectors(core::list<v2s16> &list);
292 = flush changed to disk and delete from memory, if usage timer of
293 block is more than timeout
295 void unloadUnusedData(float timeout,
296 core::list<v3s16> *deleted_blocks=NULL);
299 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
300 virtual void PrintInfo(std::ostream &out);
302 void transformLiquids(core::map<v3s16, MapBlock*> & modified_blocks);
306 These are basically coordinate wrappers to MapBlock
309 NodeMetadata* getNodeMetadata(v3s16 p);
310 void setNodeMetadata(v3s16 p, NodeMetadata *meta);
311 void removeNodeMetadata(v3s16 p);
316 core::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
324 std::ostream &m_dout; // A bit deprecated, could be removed
328 core::map<MapEventReceiver*, bool> m_event_receivers;
330 core::map<v2s16, MapSector*> m_sectors;
332 // Be sure to set this to NULL when the cached sector is deleted
333 MapSector *m_sector_cache;
334 v2s16 m_sector_cache_p;
336 // Queued transforming water nodes
337 UniqueQueue<v3s16> m_transforming_liquid;
343 This is the only map class that is able to generate map.
346 class ServerMap : public Map
350 savedir: directory to which map data should be saved
352 ServerMap(std::string savedir, IGameDef *gamedef);
357 return MAPTYPE_SERVER;
361 Get a sector from somewhere.
363 - Check disk (doesn't load blocks)
366 ServerMapSector * createSector(v2s16 p);
369 Blocks are generated by using these and makeBlock().
371 void initBlockMake(mapgen::BlockMakeData *data, v3s16 blockpos);
372 MapBlock* finishBlockMake(mapgen::BlockMakeData *data,
373 core::map<v3s16, MapBlock*> &changed_blocks);
375 // A non-threaded wrapper to the above
376 MapBlock * generateBlock(
378 core::map<v3s16, MapBlock*> &modified_blocks
382 Get a block from somewhere.
386 MapBlock * createBlock(v3s16 p);
389 Forcefully get a block from somewhere.
394 MapBlock * emergeBlock(v3s16 p, bool allow_generate=true);
396 // Helper for placing objects on ground level
397 s16 findGroundLevel(v2s16 p2d);
400 Misc. helper functions for fiddling with directory and file
403 void createDirs(std::string path);
404 // returns something like "map/sectors/xxxxxxxx"
405 std::string getSectorDir(v2s16 pos, int layout = 2);
406 // dirname: final directory name
407 v2s16 getSectorPos(std::string dirname);
408 v3s16 getBlockPos(std::string sectordir, std::string blockfile);
409 static std::string getBlockFilename(v3s16 p);
414 // Create the database structure
415 void createDatabase();
416 // Verify we can read/write to the database
417 void verifyDatabase();
418 // Get an integer suitable for a block
419 static sqlite3_int64 getBlockAsInteger(const v3s16 pos);
420 static v3s16 getIntegerAsBlock(sqlite3_int64 i);
422 // Returns true if the database file does not exist
423 bool loadFromFolders();
425 // Call these before and after saving of blocks
429 void save(ModifiedState save_level);
432 void listAllLoadableBlocks(core::list<v3s16> &dst);
434 // Saves map seed and possibly other stuff
438 /*void saveChunkMeta();
439 void loadChunkMeta();*/
441 // The sector mutex should be locked when calling most of these
443 // This only saves sector-specific data such as the heightmap
445 // DEPRECATED? Sectors have no metadata anymore.
446 void saveSectorMeta(ServerMapSector *sector);
447 MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
448 bool loadSectorMeta(v2s16 p2d);
450 // Full load of a sector including all blocks.
451 // returns true on success, false on failure.
452 bool loadSectorFull(v2s16 p2d);
453 // If sector is not found in memory, try to load it from disk.
454 // Returns true if sector now resides in memory
455 //bool deFlushSector(v2s16 p2d);
457 void saveBlock(MapBlock *block);
458 // This will generate a sector with getSector if not found.
459 void loadBlock(std::string sectordir, std::string blockfile, MapSector *sector, bool save_after_load=false);
460 MapBlock* loadBlock(v3s16 p);
462 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
464 // For debug printing
465 virtual void PrintInfo(std::ostream &out);
467 bool isSavingEnabled(){ return m_map_saving_enabled; }
469 u64 getSeed(){ return m_seed; }
472 // Seed used for all kinds of randomness in generation
475 std::string m_savedir;
476 bool m_map_saving_enabled;
479 // Chunk size in MapSectors
480 // If 0, chunks are disabled.
483 core::map<v2s16, MapChunk*> m_chunks;
487 Metadata is re-written on disk only if this is true.
488 This is reset to false when written on disk.
490 bool m_map_metadata_changed;
493 SQLite database and statements
496 sqlite3_stmt *m_database_read;
497 sqlite3_stmt *m_database_write;
498 sqlite3_stmt *m_database_list;
501 class MapVoxelManipulator : public VoxelManipulator
504 MapVoxelManipulator(Map *map);
505 virtual ~MapVoxelManipulator();
509 VoxelManipulator::clear();
510 m_loaded_blocks.clear();
513 virtual void emerge(VoxelArea a, s32 caller_id=-1);
515 void blitBack(core::map<v3s16, MapBlock*> & modified_blocks);
521 value = block existed when loaded
523 core::map<v3s16, bool> m_loaded_blocks;
526 class ManualMapVoxelManipulator : public MapVoxelManipulator
529 ManualMapVoxelManipulator(Map *map);
530 virtual ~ManualMapVoxelManipulator();
532 void setMap(Map *map)
535 virtual void emerge(VoxelArea a, s32 caller_id=-1);
537 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max);
539 // This is much faster with big chunks of generated data
540 void blitBackAll(core::map<v3s16, MapBlock*> * modified_blocks);