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.
28 #include "irrlichttypes_bloated.h"
30 #include "constants.h"
32 #include "modifiedstate.h"
33 #include "util/container.h"
34 #include "util/metricsbackend.h"
35 #include "nodetimer.h"
36 #include "map_settings_manager.h"
43 class ServerMapSector;
47 class IRollbackManager;
50 class ServerEnvironment;
57 enum MapEditEventType{
58 // Node added (changed from air or something else to something)
60 // Node removed (changed to air)
62 // Node swapped (changed without metadata change)
64 // Node metadata changed
65 MEET_BLOCK_NODE_METADATA_CHANGED,
66 // Anything else (modified_blocks are set unsent)
72 MapEditEventType type = MEET_OTHER;
74 MapNode n = CONTENT_AIR;
75 std::set<v3s16> modified_blocks;
76 bool is_private_change = false;
78 MapEditEvent() = default;
80 VoxelArea getArea() const
89 case MEET_BLOCK_NODE_METADATA_CHANGED:
91 v3s16 np1 = p*MAP_BLOCKSIZE;
92 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
93 return VoxelArea(np1, np2);
98 for (v3s16 p : modified_blocks) {
99 v3s16 np1 = p*MAP_BLOCKSIZE;
100 v3s16 np2 = np1 + v3s16(1,1,1)*MAP_BLOCKSIZE - v3s16(1,1,1);
111 class MapEventReceiver
114 // event shall be deleted by caller after the call.
115 virtual void onMapEditEvent(const MapEditEvent &event) = 0;
118 class Map /*: public NodeContainer*/
122 Map(IGameDef *gamedef);
124 DISABLE_CLASS_COPY(Map);
127 Drop (client) or delete (server) the map.
134 void addEventReceiver(MapEventReceiver *event_receiver);
135 void removeEventReceiver(MapEventReceiver *event_receiver);
136 // event shall be deleted by caller after the call.
137 void dispatchEvent(const MapEditEvent &event);
139 // On failure returns NULL
140 MapSector * getSectorNoGenerateNoLock(v2s16 p2d);
141 // Same as the above (there exists no lock anymore)
142 MapSector * getSectorNoGenerate(v2s16 p2d);
145 This is overloaded by ClientMap and ServerMap to allow
146 their differing fetch methods.
148 virtual MapSector * emergeSector(v2s16 p){ return NULL; }
150 // Returns InvalidPositionException if not found
151 MapBlock * getBlockNoCreate(v3s16 p);
152 // Returns NULL if not found
153 MapBlock * getBlockNoCreateNoEx(v3s16 p);
155 /* Server overrides */
156 virtual MapBlock * emergeBlock(v3s16 p, bool create_blank=true)
157 { return getBlockNoCreateNoEx(p); }
159 inline const NodeDefManager * getNodeDefManager() { return m_nodedef; }
161 bool isValidPosition(v3s16 p);
163 // throws InvalidPositionException if not found
164 void setNode(v3s16 p, MapNode n);
166 // Returns a CONTENT_IGNORE node if not found
167 // If is_valid_position is not NULL then this will be set to true if the
168 // position is valid, otherwise false
169 MapNode getNode(v3s16 p, bool *is_valid_position = NULL);
172 These handle lighting but not faces.
174 virtual void addNodeAndUpdate(v3s16 p, MapNode n,
175 std::map<v3s16, MapBlock*> &modified_blocks,
176 bool remove_metadata = true);
177 void removeNodeAndUpdate(v3s16 p,
178 std::map<v3s16, MapBlock*> &modified_blocks);
181 Wrappers for the latter ones.
183 Return true if succeeded, false if not.
185 bool addNodeWithEvent(v3s16 p, MapNode n, bool remove_metadata = true);
186 bool removeNodeWithEvent(v3s16 p);
188 // Call these before and after saving of many blocks
189 virtual void beginSave() {}
190 virtual void endSave() {}
192 virtual void save(ModifiedState save_level) { FATAL_ERROR("FIXME"); }
195 Return true unless the map definitely cannot save blocks.
197 virtual bool maySaveBlocks() { return true; }
199 // Server implements these.
200 // Client leaves them as no-op.
201 virtual bool saveBlock(MapBlock *block) { return false; }
202 virtual bool deleteBlock(v3s16 blockpos) { return false; }
205 Updates usage timers and unloads unused blocks and sectors.
206 Saves modified blocks before unloading if possible.
208 void timerUpdate(float dtime, float unload_timeout, s32 max_loaded_blocks,
209 std::vector<v3s16> *unloaded_blocks=NULL);
212 Unloads all blocks with a zero refCount().
213 Saves modified blocks before unloading if possible.
215 void unloadUnreferencedBlocks(std::vector<v3s16> *unloaded_blocks=NULL);
217 // Deletes sectors and their blocks from memory
218 // Takes cache into account
219 // If deleted sector is in sector cache, clears cache
220 void deleteSectors(std::vector<v2s16> &list);
222 // For debug printing. Prints "Map: ", "ServerMap: " or "ClientMap: "
223 virtual void PrintInfo(std::ostream &out);
227 These are basically coordinate wrappers to MapBlock
230 std::vector<v3s16> findNodesWithMetadata(v3s16 p1, v3s16 p2);
231 NodeMetadata *getNodeMetadata(v3s16 p);
234 * Sets metadata for a node.
235 * This method sets the metadata for a given node.
236 * On success, it returns @c true and the object pointed to
237 * by @p meta is then managed by the system and should
238 * not be deleted by the caller.
240 * In case of failure, the method returns @c false and the
241 * caller is still responsible for deleting the object!
243 * @param p node coordinates
244 * @param meta pointer to @c NodeMetadata object
245 * @return @c true on success, false on failure
247 bool setNodeMetadata(v3s16 p, NodeMetadata *meta);
248 void removeNodeMetadata(v3s16 p);
252 These are basically coordinate wrappers to MapBlock
255 NodeTimer getNodeTimer(v3s16 p);
256 void setNodeTimer(const NodeTimer &t);
257 void removeNodeTimer(v3s16 p);
263 bool isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes);
267 std::set<MapEventReceiver*> m_event_receivers;
269 std::unordered_map<v2s16, MapSector*> m_sectors;
271 // Be sure to set this to NULL when the cached sector is deleted
272 MapSector *m_sector_cache = nullptr;
273 v2s16 m_sector_cache_p;
275 // This stores the properties of the nodes on the map.
276 const NodeDefManager *m_nodedef;
278 // Can be implemented by child class
279 virtual void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) {}
281 bool determineAdditionalOcclusionCheck(const v3s16 &pos_camera,
282 const core::aabbox3d<s16> &block_bounds, v3s16 &check);
283 bool isOccluded(const v3s16 &pos_camera, const v3s16 &pos_target,
284 float step, float stepfac, float start_offset, float end_offset,
291 This is the only map class that is able to generate map.
294 class ServerMap : public Map
298 savedir: directory to which map data should be saved
300 ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge, MetricsBackend *mb);
304 Get a sector from somewhere.
306 - Check disk (doesn't load blocks)
309 MapSector *createSector(v2s16 p);
312 Blocks are generated by using these and makeBlock().
314 bool blockpos_over_mapgen_limit(v3s16 p);
315 bool initBlockMake(v3s16 blockpos, BlockMakeData *data);
316 void finishBlockMake(BlockMakeData *data,
317 std::map<v3s16, MapBlock*> *changed_blocks);
320 Get a block from somewhere.
324 MapBlock *createBlock(v3s16 p);
327 Forcefully get a block from somewhere.
330 - Create blank filled with CONTENT_IGNORE
333 MapBlock *emergeBlock(v3s16 p, bool create_blank=true) override;
337 If it does not exist in memory, add it to the emerge queue.
339 - Emerge Queue (deferred disk or generate)
341 MapBlock *getBlockOrEmerge(v3s16 p3d);
343 bool isBlockInQueue(v3s16 pos);
345 void addNodeAndUpdate(v3s16 p, MapNode n,
346 std::map<v3s16, MapBlock*> &modified_blocks,
347 bool remove_metadata) override;
352 static MapDatabase *createDatabase(const std::string &name, const std::string &savedir, Settings &conf);
354 // Call these before and after saving of blocks
355 void beginSave() override;
356 void endSave() override;
358 void save(ModifiedState save_level) override;
359 void listAllLoadableBlocks(std::vector<v3s16> &dst);
360 void listAllLoadedBlocks(std::vector<v3s16> &dst);
362 MapgenParams *getMapgenParams();
364 bool saveBlock(MapBlock *block) override;
365 static bool saveBlock(MapBlock *block, MapDatabase *db, int compression_level = -1);
366 MapBlock* loadBlock(v3s16 p);
368 void loadBlock(std::string *blob, v3s16 p3d, MapSector *sector, bool save_after_load=false);
370 bool deleteBlock(v3s16 blockpos) override;
372 void updateVManip(v3s16 pos);
374 // For debug printing
375 void PrintInfo(std::ostream &out) override;
377 bool isSavingEnabled(){ return m_map_saving_enabled; }
382 * Fixes lighting in one map block.
383 * May modify other blocks as well, as light can spread
384 * out of the specified block.
385 * Returns false if the block is not generated (so nothing
386 * changed), true otherwise.
388 bool repairBlockLight(v3s16 blockpos,
389 std::map<v3s16, MapBlock *> *modified_blocks);
391 void transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks,
392 ServerEnvironment *env);
394 void transforming_liquid_add(v3s16 p);
396 MapSettingsManager settings_mgr;
400 void reportMetrics(u64 save_time_us, u32 saved_blocks, u32 all_blocks) override;
403 friend class LuaVoxelManip;
406 EmergeManager *m_emerge;
408 std::string m_savedir;
409 bool m_map_saving_enabled;
411 int m_map_compression_level;
413 std::set<v3s16> m_chunks_in_progress;
415 // Queued transforming water nodes
416 UniqueQueue<v3s16> m_transforming_liquid;
417 f32 m_transforming_liquid_loop_count_multiplier = 1.0f;
418 u32 m_unprocessed_count = 0;
419 u64 m_inc_trending_up_start_time = 0; // milliseconds
420 bool m_queue_size_timer_started = false;
423 Metadata is re-written on disk only if this is true.
424 This is reset to false when written on disk.
426 bool m_map_metadata_changed = true;
427 MapDatabase *dbase = nullptr;
428 MapDatabase *dbase_ro = nullptr;
431 MetricGaugePtr m_loaded_blocks_gauge;
432 MetricCounterPtr m_save_time_counter;
433 MetricCounterPtr m_save_count_counter;
437 #define VMANIP_BLOCK_DATA_INEXIST 1
438 #define VMANIP_BLOCK_CONTAINS_CIGNORE 2
440 class MMVManip : public VoxelManipulator
444 virtual ~MMVManip() = default;
448 VoxelManipulator::clear();
449 m_loaded_blocks.clear();
452 void initialEmerge(v3s16 blockpos_min, v3s16 blockpos_max,
453 bool load_if_inexistent = true);
455 // This is much faster with big chunks of generated data
456 void blitBackAll(std::map<v3s16, MapBlock*> * modified_blocks,
457 bool overwrite_generated = true);
460 Creates a copy of this VManip including contents, the copy will not be
461 associated with a Map.
463 MMVManip *clone() const;
465 // Reassociates a copied VManip to a map
466 void reparent(Map *map);
468 // Is it impossible to call initialEmerge / blitBackAll?
469 inline bool isOrphan() const { return !m_map; }
471 bool m_is_dirty = false;
477 Map *m_map = nullptr;
480 value = flags describing the block
482 std::map<v3s16, u8> m_loaded_blocks;