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;
78 std::set<v3s16> modified_blocks;
79 u16 already_known_by_peer;
84 already_known_by_peer(0)
87 MapEditEvent * clone()
89 MapEditEvent *event = new MapEditEvent();
93 event->modified_blocks = modified_blocks;
102 case MEET_REMOVENODE:
106 case MEET_BLOCK_NODE_METADATA_CHANGED:
108 v3s16 np1 = p*MAP_BLOCKSIZE;
109 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
110 return VoxelArea(np1, np2);
115 for(std::set<v3s16>::iterator
116 i = modified_blocks.begin();
117 i != modified_blocks.end(); ++i)
120 v3s16 np1 = p*MAP_BLOCKSIZE;
121 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
132 class MapEventReceiver
135 // event shall be deleted by caller after the call.
136 virtual void onMapEditEvent(MapEditEvent *event) = 0;
139 class Map /*: public NodeContainer*/
143 Map(std::ostream &dout, IGameDef *gamedef);
145 DISABLE_CLASS_COPY(Map);
147 virtual s32 mapType() const
153 Drop (client) or delete (server) the map.
160 void addEventReceiver(MapEventReceiver *event_receiver);
161 void removeEventReceiver(MapEventReceiver *event_receiver);
162 // event shall be deleted by caller after the call.
163 void dispatchEvent(MapEditEvent *event);
165 // On failure returns NULL
166 MapSector * getSectorNoGenerateNoExNoLock(v2s16 p2d);
167 // Same as the above (there exists no lock anymore)
168 MapSector * getSectorNoGenerateNoEx(v2s16 p2d);
169 // On failure throws InvalidPositionException
170 MapSector * getSectorNoGenerate(v2s16 p2d);
171 // Gets an existing sector or creates an empty one
172 //MapSector * getSectorCreate(v2s16 p2d);
175 This is overloaded by ClientMap and ServerMap to allow
176 their differing fetch methods.
178 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
179 virtual MapSector * emergeSector(v2s16 p,
180 std::map<v3s16, MapBlock*> &changed_blocks){ return NULL; }
182 // Returns InvalidPositionException if not found
183 MapBlock * getBlockNoCreate(v3s16 p);
184 // Returns NULL if not found
185 MapBlock * getBlockNoCreateNoEx(v3s16 p);
187 /* Server overrides */
188 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
189 { return getBlockNoCreateNoEx(p); }
191 inline INodeDefManager * getNodeDefManager() { return m_nodedef; }
193 // Returns InvalidPositionException if not found
194 bool isNodeUnderground(v3s16 p);
196 bool isValidPosition(v3s16 p);
198 // throws InvalidPositionException if not found
199 void setNode(v3s16 p, MapNode & n);
201 // Returns a CONTENT_IGNORE node if not found
202 // If is_valid_position is not NULL then this will be set to true if the
203 // position is valid, otherwise false
204 MapNode getNodeNoEx(v3s16 p, bool *is_valid_position = NULL);
207 These handle lighting but not faces.
209 void addNodeAndUpdate(v3s16 p, MapNode n,
210 std::map<v3s16, MapBlock*> &modified_blocks,
211 bool remove_metadata = true);
212 void removeNodeAndUpdate(v3s16 p,
213 std::map<v3s16, MapBlock*> &modified_blocks);
216 Wrappers for the latter ones.
218 Return true if succeeded, false if not.
220 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
221 bool removeNodeWithEvent(v3s16 p);
224 Takes the blocks at the edges into account
226 bool getDayNightDiff(v3s16 blockpos);
228 //core::aabbox3d<s16> getDisplayedBlockArea();
230 //bool updateChangedVisibleArea();
232 // Call these before and after saving of many blocks
233 virtual void beginSave() { return; }
234 virtual void endSave() { return; }
236 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
238 // Server implements these.
239 // Client leaves them as no-op.
240 virtual bool saveBlock(MapBlock *block) { return false; }
241 virtual bool deleteBlock(v3s16 blockpos) { return false; }
244 Updates usage timers and unloads unused blocks and sectors.
245 Saves modified blocks before unloading on MAPTYPE_SERVER.
247 void timerUpdate(float dtime, float unload_timeout, u32 max_loaded_blocks,
248 std::vector<v3s16> *unloaded_blocks=NULL);
251 Unloads all blocks with a zero refCount().
252 Saves modified blocks before unloading on MAPTYPE_SERVER.
254 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
256 // Deletes sectors and their blocks from memory
257 // Takes cache into account
258 // If deleted sector is in sector cache, clears cache
259 void deleteSectors(std::vector<v2s16> &list);
261 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
262 virtual void PrintInfo(std::ostream &out);
264 void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks,
265 ServerEnvironment *env);
269 These are basically coordinate wrappers to MapBlock
272 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
273 NodeMetadata *getNodeMetadata(v3s16 p);
276 * Sets metadata for a node.
277 * This method sets the metadata for a given node.
278 * On success, it returns @c true and the object pointed to
279 * by @p meta is then managed by the system and should
280 * not be deleted by the caller.
282 * In case of failure, the method returns @c false and the
283 * caller is still responsible for deleting the object!
285 * @param p node coordinates
286 * @param meta pointer to @c NodeMetadata object
287 * @return @c true on success, false on failure
289 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
290 void removeNodeMetadata(v3s16 p);
294 These are basically coordinate wrappers to MapBlock
297 NodeTimer getNodeTimer(v3s16 p);
298 void setNodeTimer(const NodeTimer &t);
299 void removeNodeTimer(v3s16 p);
304 std::map<v2s16, MapSector*> *getSectorsPtr(){return &m_sectors;}
310 void transforming_liquid_add(v3s16 p);
311 s32 transforming_liquid_size();
313 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
315 friend class LuaVoxelManip;
317 std::ostream &m_dout; // A bit deprecated, could be removed
321 std::set<MapEventReceiver*> m_event_receivers;
323 std::map<v2s16, MapSector*> m_sectors;
325 // Be sure to set this to NULL when the cached sector is deleted
326 MapSector *m_sector_cache;
327 v2s16 m_sector_cache_p;
329 // Queued transforming water nodes
330 UniqueQueue<v3s16> m_transforming_liquid;
332 // This stores the properties of the nodes on the map.
333 INodeDefManager *m_nodedef;
335 bool isOccluded(v3s16 p0, v3s16 p1, float step, float stepfac,
336 float start_off, float end_off, u32 needed_count);
339 f32 m_transforming_liquid_loop_count_multiplier;
340 u32 m_unprocessed_count;
341 u64 m_inc_trending_up_start_time; // milliseconds
342 bool m_queue_size_timer_started;
348 This is the only map class that is able to generate map.
351 class ServerMap : public Map
355 savedir: directory to which map data should be saved
357 ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge);
362 return MAPTYPE_SERVER;
366 Get a sector from somewhere.
368 - Check disk (doesn't load blocks)
371 ServerMapSector *createSector(v2s16 p);
373 bool saoPositionOverLimit(const v3f &p);
376 Blocks are generated by using these and makeBlock().
378 bool blockpos_over_mapgen_limit(v3s16 p);
379 bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
380 void finishBlockMake(BlockMakeData *data,
381 std::map<v3s16, MapBlock*> *changed_blocks);
384 Get a block from somewhere.
388 MapBlock *createBlock(v3s16 p);
391 Forcefully get a block from somewhere.
394 - Create blank filled with CONTENT_IGNORE
397 MapBlock *emergeBlock(v3s16 p, bool create_blank=true);
401 If it does not exist in memory, add it to the emerge queue.
403 - Emerge Queue (deferred disk or generate)
405 MapBlock *getBlockOrEmerge(v3s16 p3d);
407 // Helper for placing objects on ground level
408 s16 findGroundLevel(v2s16 p2d);
411 Misc. helper functions for fiddling with directory and file
414 void createDirs(std::string path);
415 // returns something like "map/sectors/xxxxxxxx"
416 std::string getSectorDir(v2s16 pos, int layout = 2);
417 // dirname: final directory name
418 v2s16 getSectorPos(const std::string &dirname);
419 v3s16 getBlockPos(const std::string §ordir, const std::string &blockfile);
420 static std::string getBlockFilename(v3s16 p);
425 static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
427 // Returns true if the database file does not exist
428 bool loadFromFolders();
430 // Call these before and after saving of blocks
434 void save(ModifiedState save_level);
435 void listAllLoadableBlocks(std::vector<v3s16> &dst);
436 void listAllLoadedBlocks(std::vector<v3s16> &dst);
438 MapgenParams *getMapgenParams();
440 /*void saveChunkMeta();
441 void loadChunkMeta();*/
443 // The sector mutex should be locked when calling most of these
445 // This only saves sector-specific data such as the heightmap
447 // DEPRECATED? Sectors have no metadata anymore.
448 void saveSectorMeta(ServerMapSector *sector);
449 MapSector* loadSectorMeta(std::string dirname, bool save_after_load);
450 bool loadSectorMeta(v2s16 p2d);
452 bool saveBlock(MapBlock *block);
453 static bool saveBlock(MapBlock *block, MapDatabase *db);
454 // This will generate a sector with getSector if not found.
455 void loadBlock(const std::string §ordir, const std::string &blockfile,
456 MapSector *sector, bool save_after_load=false);
457 MapBlock* loadBlock(v3s16 p);
459 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
461 bool deleteBlock(v3s16 blockpos);
463 void updateVManip(v3s16 pos);
465 // For debug printing
466 virtual void PrintInfo(std::ostream &out);
468 bool isSavingEnabled(){ return m_map_saving_enabled; }
474 * Fixes lighting in one map block.
475 * May modify other blocks as well, as light can spread
476 * out of the specified block.
477 * Returns false if the block is not generated (so nothing
478 * changed), true otherwise.
480 bool repairBlockLight(v3s16 blockpos,
481 std::map<v3s16, MapBlock *> *modified_blocks);
483 MapSettingsManager settings_mgr;
487 EmergeManager *m_emerge;
489 std::string m_savedir;
490 bool m_map_saving_enabled;
493 // Chunk size in MapSectors
494 // If 0, chunks are disabled.
497 core::map<v2s16, MapChunk*> m_chunks;
501 Metadata is re-written on disk only if this is true.
502 This is reset to false when written on disk.
504 bool m_map_metadata_changed;
509 #define VMANIP_BLOCK_DATA_INEXIST 1
510 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
512 class MMVManip : public VoxelManipulator
520 VoxelManipulator::clear();
521 m_loaded_blocks.clear();
524 void setMap(Map *map)
527 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
528 bool load_if_inexistent = true);
530 // This is much faster with big chunks of generated data
531 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
532 bool overwrite_generated = true);
541 value = flags describing the block
543 std::map<v3s16, u8> m_loaded_blocks;