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 "util/cpp11_container.h"
36 #include "nodetimer.h"
37 #include "map_settings_manager.h"
43 class ServerMapSector;
47 class IRollbackManager;
49 class ServerEnvironment;
56 #define MAPTYPE_BASE 0
57 #define MAPTYPE_SERVER 1
58 #define MAPTYPE_CLIENT 2
60 enum MapEditEventType{
61 // Node added (changed from air or something else to something)
63 // Node removed (changed to air)
65 // Node swapped (changed without metadata change)
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;
85 already_known_by_peer(0)
88 MapEditEvent * clone()
90 MapEditEvent *event = new MapEditEvent();
94 event->modified_blocks = modified_blocks;
103 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 create_blank=true)
194 { return getBlockNoCreateNoEx(p); }
196 inline INodeDefManager * getNodeDefManager() { return m_nodedef; }
198 // Returns InvalidPositionException if not found
199 bool isNodeUnderground(v3s16 p);
201 bool isValidPosition(v3s16 p);
203 // throws InvalidPositionException if not found
204 void setNode(v3s16 p, MapNode & n);
206 // Returns a CONTENT_IGNORE node if not found
207 // If is_valid_position is not NULL then this will be set to true if the
208 // position is valid, otherwise false
209 MapNode getNodeNoEx(v3s16 p, bool *is_valid_position = NULL);
212 These handle lighting but not faces.
214 void addNodeAndUpdate(v3s16 p, MapNode n,
215 std::map<v3s16, MapBlock*> &modified_blocks,
216 bool remove_metadata = true);
217 void removeNodeAndUpdate(v3s16 p,
218 std::map<v3s16, MapBlock*> &modified_blocks);
221 Wrappers for the latter ones.
223 Return true if succeeded, false if not.
225 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
226 bool removeNodeWithEvent(v3s16 p);
229 Takes the blocks at the edges into account
231 bool getDayNightDiff(v3s16 blockpos);
233 //core::aabbox3d<s16> getDisplayedBlockArea();
235 //bool updateChangedVisibleArea();
237 // Call these before and after saving of many blocks
238 virtual void beginSave() { return; }
239 virtual void endSave() { return; }
241 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
243 // Server implements these.
244 // Client leaves them as no-op.
245 virtual bool saveBlock(MapBlock *block) { return false; }
246 virtual bool deleteBlock(v3s16 blockpos) { return false; }
249 Updates usage timers and unloads unused blocks and sectors.
250 Saves modified blocks before unloading on MAPTYPE_SERVER.
252 void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
253 std::vector<v3s16> *unloaded_blocks=NULL);
256 Unloads all blocks with a zero refCount().
257 Saves modified blocks before unloading on MAPTYPE_SERVER.
259 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
261 // Deletes sectors and their blocks from memory
262 // Takes cache into account
263 // If deleted sector is in sector cache, clears cache
264 void deleteSectors(std::vector<v2s16> &list);
266 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
267 virtual void PrintInfo(std::ostream &out);
269 void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks);
273 These are basically coordinate wrappers to MapBlock
276 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
277 NodeMetadata *getNodeMetadata(v3s16 p);
280 * Sets metadata for a node.
281 * This method sets the metadata for a given node.
282 * On success, it returns @c true and the object pointed to
283 * by @p meta is then managed by the system and should
284 * not be deleted by the caller.
286 * In case of failure, the method returns @c false and the
287 * caller is still responsible for deleting the object!
289 * @param p node coordinates
290 * @param meta pointer to @c NodeMetadata object
291 * @return @c true on success, false on failure
293 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
294 void removeNodeMetadata(v3s16 p);
298 These are basically coordinate wrappers to MapBlock
301 NodeTimer getNodeTimer(v3s16 p);
302 void setNodeTimer(const NodeTimer &t);
303 void removeNodeTimer(v3s16 p);
308 std::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
314 void transforming_liquid_add(v3s16 p);
315 s32 transforming_liquid_size();
317 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
319 friend class LuaVoxelManip;
321 std::ostream &m_dout; // A bit deprecated, could be removed
325 std::set<MapEventReceiver*> m_event_receivers;
327 std::map<v2s16, MapSector*> m_sectors;
329 // Be sure to set this to NULL when the cached sector is deleted
330 MapSector *m_sector_cache;
331 v2s16 m_sector_cache_p;
333 // Queued transforming water nodes
334 UniqueQueue<v3s16> m_transforming_liquid;
336 // This stores the properties of the nodes on the map.
337 INodeDefManager *m_nodedef;
339 bool isOccluded(v3s16 p0, v3s16 p1, float step, float stepfac,
340 float start_off, float end_off, u32 needed_count);
343 f32 m_transforming_liquid_loop_count_multiplier;
344 u32 m_unprocessed_count;
345 u32 m_inc_trending_up_start_time; // milliseconds
346 bool m_queue_size_timer_started;
348 DISABLE_CLASS_COPY(Map);
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 bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
383 void finishBlockMake(BlockMakeData *data,
384 std::map<v3s16, MapBlock*> *changed_blocks);
387 Get a block from somewhere.
391 MapBlock *createBlock(v3s16 p);
394 Forcefully get a block from somewhere.
397 - Create blank filled with CONTENT_IGNORE
400 MapBlock *emergeBlock(v3s16 p, bool create_blank=true);
404 If it does not exist in memory, add it to the emerge queue.
406 - Emerge Queue (deferred disk or generate)
408 MapBlock *getBlockOrEmerge(v3s16 p3d);
410 // Carries out any initialization necessary before block is sent
411 void prepareBlock(MapBlock *block);
413 // Helper for placing objects on ground level
414 s16 findGroundLevel(v2s16 p2d);
417 Misc. helper functions for fiddling with directory and file
420 void createDirs(std::string path);
421 // returns something like "map/sectors/xxxxxxxx"
422 std::string getSectorDir(v2s16 pos, int layout = 2);
423 // dirname: final directory name
424 v2s16 getSectorPos(std::string dirname);
425 v3s16 getBlockPos(std::string sectordir, std::string blockfile);
426 static std::string getBlockFilename(v3s16 p);
431 static Database *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
432 // Verify we can read/write to the database
433 void verifyDatabase();
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::vector<v3s16> &dst);
444 void listAllLoadedBlocks(std::vector<v3s16> &dst);
446 MapgenParams *getMapgenParams();
448 /*void saveChunkMeta();
449 void loadChunkMeta();*/
451 // The sector mutex should be locked when calling most of these
453 // This only saves sector-specific data such as the heightmap
455 // DEPRECATED? Sectors have no metadata anymore.
456 void saveSectorMeta(ServerMapSector *sector);
457 MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
458 bool loadSectorMeta(v2s16 p2d);
460 // Full load of a sector including all blocks.
461 // returns true on success, false on failure.
462 bool loadSectorFull(v2s16 p2d);
463 // If sector is not found in memory, try to load it from disk.
464 // Returns true if sector now resides in memory
465 //bool deFlushSector(v2s16 p2d);
467 bool saveBlock(MapBlock *block);
468 static bool saveBlock(MapBlock *block, Database *db);
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 bool deleteBlock(v3s16 blockpos);
477 void updateVManip(v3s16 pos);
479 // For debug printing
480 virtual void PrintInfo(std::ostream &out);
482 bool isSavingEnabled(){ return m_map_saving_enabled; }
487 MapSettingsManager settings_mgr;
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;
513 #define VMANIP_BLOCK_DATA_INEXIST 1
514 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
516 class MMVManip : public VoxelManipulator
524 VoxelManipulator::clear();
525 m_loaded_blocks.clear();
528 void setMap(Map *map)
531 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
532 bool load_if_inexistent = true);
534 // This is much faster with big chunks of generated data
535 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
536 bool overwrite_generated = true);
545 value = flags describing the block
547 std::map<v3s16, u8> m_loaded_blocks;