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"
36 #include "map_settings_manager.h"
42 class ServerMapSector;
46 class IRollbackManager;
48 class ServerEnvironment;
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 swapped (changed without metadata change)
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 = MEET_OTHER;
77 MapNode n = CONTENT_AIR;
78 std::set<v3s16> modified_blocks;
79 u16 already_known_by_peer = 0;
83 MapEditEvent * clone()
85 MapEditEvent *event = new MapEditEvent();
89 event->modified_blocks = modified_blocks;
102 case MEET_BLOCK_NODE_METADATA_CHANGED:
104 v3s16 np1 = p*MAP_BLOCKSIZE;
105 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
106 return VoxelArea(np1, np2);
111 for(std::set<v3s16>::iterator
112 i = modified_blocks.begin();
113 i != modified_blocks.end(); ++i)
116 v3s16 np1 = p*MAP_BLOCKSIZE;
117 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
128 class MapEventReceiver
131 // event shall be deleted by caller after the call.
132 virtual void onMapEditEvent(MapEditEvent *event) = 0;
135 class Map /*: public NodeContainer*/
139 Map(std::ostream &dout, IGameDef *gamedef);
141 DISABLE_CLASS_COPY(Map);
143 virtual s32 mapType() const
149 Drop (client) or delete (server) the map.
156 void addEventReceiver(MapEventReceiver *event_receiver);
157 void removeEventReceiver(MapEventReceiver *event_receiver);
158 // event shall be deleted by caller after the call.
159 void dispatchEvent(MapEditEvent *event);
161 // On failure returns NULL
162 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
163 // Same as the above (there exists no lock anymore)
164 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
165 // On failure throws InvalidPositionException
166 MapSector * getSectorNoGenerate(v2s16 p2d);
167 // Gets an existing sector or creates an empty one
168 //MapSector * getSectorCreate(v2s16 p2d);
171 This is overloaded by ClientMap and ServerMap to allow
172 their differing fetch methods.
174 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
175 virtual MapSector * emergeSector(v2s16 p,
176 std::map<v3s16, MapBlock*> &changed_blocks){ return NULL; }
178 // Returns InvalidPositionException if not found
179 MapBlock * getBlockNoCreate(v3s16 p);
180 // Returns NULL if not found
181 MapBlock * getBlockNoCreateNoEx(v3s16 p);
183 /* Server overrides */
184 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
185 { return getBlockNoCreateNoEx(p); }
187 inline INodeDefManager * getNodeDefManager() { return m_nodedef; }
189 // Returns InvalidPositionException if not found
190 bool isNodeUnderground(v3s16 p);
192 bool isValidPosition(v3s16 p);
194 // throws InvalidPositionException if not found
195 void setNode(v3s16 p, MapNode & n);
197 // Returns a CONTENT_IGNORE node if not found
198 // If is_valid_position is not NULL then this will be set to true if the
199 // position is valid, otherwise false
200 MapNode getNodeNoEx(v3s16 p, bool *is_valid_position = NULL);
203 These handle lighting but not faces.
205 void addNodeAndUpdate(v3s16 p, MapNode n,
206 std::map<v3s16, MapBlock*> &modified_blocks,
207 bool remove_metadata = true);
208 void removeNodeAndUpdate(v3s16 p,
209 std::map<v3s16, MapBlock*> &modified_blocks);
212 Wrappers for the latter ones.
214 Return true if succeeded, false if not.
216 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
217 bool removeNodeWithEvent(v3s16 p);
220 Takes the blocks at the edges into account
222 bool getDayNightDiff(v3s16 blockpos);
224 //core::aabbox3d<s16> getDisplayedBlockArea();
226 //bool updateChangedVisibleArea();
228 // Call these before and after saving of many blocks
229 virtual void beginSave() { return; }
230 virtual void endSave() { return; }
232 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
234 // Server implements these.
235 // Client leaves them as no-op.
236 virtual bool saveBlock(MapBlock *block) { return false; }
237 virtual bool deleteBlock(v3s16 blockpos) { return false; }
240 Updates usage timers and unloads unused blocks and sectors.
241 Saves modified blocks before unloading on MAPTYPE_SERVER.
243 void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
244 std::vector<v3s16> *unloaded_blocks=NULL);
247 Unloads all blocks with a zero refCount().
248 Saves modified blocks before unloading on MAPTYPE_SERVER.
250 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
252 // Deletes sectors and their blocks from memory
253 // Takes cache into account
254 // If deleted sector is in sector cache, clears cache
255 void deleteSectors(std::vector<v2s16> &list);
257 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
258 virtual void PrintInfo(std::ostream &out);
260 void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks,
261 ServerEnvironment *env);
265 These are basically coordinate wrappers to MapBlock
268 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
269 NodeMetadata *getNodeMetadata(v3s16 p);
272 * Sets metadata for a node.
273 * This method sets the metadata for a given node.
274 * On success, it returns @c true and the object pointed to
275 * by @p meta is then managed by the system and should
276 * not be deleted by the caller.
278 * In case of failure, the method returns @c false and the
279 * caller is still responsible for deleting the object!
281 * @param p node coordinates
282 * @param meta pointer to @c NodeMetadata object
283 * @return @c true on success, false on failure
285 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
286 void removeNodeMetadata(v3s16 p);
290 These are basically coordinate wrappers to MapBlock
293 NodeTimer getNodeTimer(v3s16 p);
294 void setNodeTimer(const NodeTimer &t);
295 void removeNodeTimer(v3s16 p);
300 std::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
306 void transforming_liquid_add(v3s16 p);
307 s32 transforming_liquid_size();
309 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
311 friend class LuaVoxelManip;
313 std::ostream &m_dout; // A bit deprecated, could be removed
317 std::set<MapEventReceiver*> m_event_receivers;
319 std::map<v2s16, MapSector*> m_sectors;
321 // Be sure to set this to NULL when the cached sector is deleted
322 MapSector *m_sector_cache = nullptr;
323 v2s16 m_sector_cache_p;
325 // Queued transforming water nodes
326 UniqueQueue<v3s16> m_transforming_liquid;
328 // This stores the properties of the nodes on the map.
329 INodeDefManager *m_nodedef;
331 bool isOccluded(v3s16 p0, v3s16 p1, float step, float stepfac,
332 float start_off, float end_off, u32 needed_count);
335 f32 m_transforming_liquid_loop_count_multiplier = 1.0f;
336 u32 m_unprocessed_count = 0;
337 u64 m_inc_trending_up_start_time = 0; // milliseconds
338 bool m_queue_size_timer_started = false;
344 This is the only map class that is able to generate map.
347 class ServerMap : public Map
351 savedir: directory to which map data should be saved
353 ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge);
358 return MAPTYPE_SERVER;
362 Get a sector from somewhere.
364 - Check disk (doesn't load blocks)
367 ServerMapSector *createSector(v2s16 p);
369 bool saoPositionOverLimit(const v3f &p);
372 Blocks are generated by using these and makeBlock().
374 bool blockpos_over_mapgen_limit(v3s16 p);
375 bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
376 void finishBlockMake(BlockMakeData *data,
377 std::map<v3s16, MapBlock*> *changed_blocks);
380 Get a block from somewhere.
384 MapBlock *createBlock(v3s16 p);
387 Forcefully get a block from somewhere.
390 - Create blank filled with CONTENT_IGNORE
393 MapBlock *emergeBlock(v3s16 p, bool create_blank=true);
397 If it does not exist in memory, add it to the emerge queue.
399 - Emerge Queue (deferred disk or generate)
401 MapBlock *getBlockOrEmerge(v3s16 p3d);
403 // Helper for placing objects on ground level
404 s16 findGroundLevel(v2s16 p2d);
407 Misc. helper functions for fiddling with directory and file
410 void createDirs(std::string path);
411 // returns something like "map/sectors/xxxxxxxx"
412 std::string getSectorDir(v2s16 pos, int layout = 2);
413 // dirname: final directory name
414 v2s16 getSectorPos(const std::string &dirname);
415 v3s16 getBlockPos(const std::string §ordir, const std::string &blockfile);
416 static std::string getBlockFilename(v3s16 p);
421 static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
423 // Returns true if the database file does not exist
424 bool loadFromFolders();
426 // Call these before and after saving of blocks
430 void save(ModifiedState save_level);
431 void listAllLoadableBlocks(std::vector<v3s16> &dst);
432 void listAllLoadedBlocks(std::vector<v3s16> &dst);
434 MapgenParams *getMapgenParams();
436 /*void saveChunkMeta();
437 void loadChunkMeta();*/
439 // The sector mutex should be locked when calling most of these
441 // This only saves sector-specific data such as the heightmap
443 // DEPRECATED? Sectors have no metadata anymore.
444 void saveSectorMeta(ServerMapSector *sector);
445 MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
446 bool loadSectorMeta(v2s16 p2d);
448 bool saveBlock(MapBlock *block);
449 static bool saveBlock(MapBlock *block, MapDatabase *db);
450 // This will generate a sector with getSector if not found.
451 void loadBlock(const std::string §ordir, const std::string &blockfile,
452 MapSector *sector, bool save_after_load=false);
453 MapBlock* loadBlock(v3s16 p);
455 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
457 bool deleteBlock(v3s16 blockpos);
459 void updateVManip(v3s16 pos);
461 // For debug printing
462 virtual void PrintInfo(std::ostream &out);
464 bool isSavingEnabled(){ return m_map_saving_enabled; }
470 * Fixes lighting in one map block.
471 * May modify other blocks as well, as light can spread
472 * out of the specified block.
473 * Returns false if the block is not generated (so nothing
474 * changed), true otherwise.
476 bool repairBlockLight(v3s16 blockpos,
477 std::map<v3s16, MapBlock *> *modified_blocks);
479 MapSettingsManager settings_mgr;
483 EmergeManager *m_emerge;
485 std::string m_savedir;
486 bool m_map_saving_enabled;
489 // Chunk size in MapSectors
490 // If 0, chunks are disabled.
493 core::map<v2s16, MapChunk*> m_chunks;
497 Metadata is re-written on disk only if this is true.
498 This is reset to false when written on disk.
500 bool m_map_metadata_changed = true;
501 MapDatabase *dbase = nullptr;
505 #define VMANIP_BLOCK_DATA_INEXIST 1
506 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
508 class MMVManip : public VoxelManipulator
516 VoxelManipulator::clear();
517 m_loaded_blocks.clear();
520 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
521 bool load_if_inexistent = true);
523 // This is much faster with big chunks of generated data
524 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
525 bool overwrite_generated = true);
527 bool m_is_dirty = false;
533 value = flags describing the block
535 std::map<v3s16, u8> m_loaded_blocks;