3 Copyright (C) 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.
25 #include "exceptions.h"
26 #include "constants.h"
27 #include "staticobject.h"
28 #include "nodemetadata.h"
29 #include "nodetimer.h"
30 #include "modifiedstate.h"
31 #include "util/numeric.h" // getContainerPos
33 #include "mapgen/mapgen.h"
36 class NodeMetadataList;
39 class VoxelManipulator;
41 #define BLOCK_TIMESTAMP_UNDEFINED 0xffffffff
44 //// MapBlock modified reason flags
47 #define MOD_REASON_INITIAL (1 << 0)
48 #define MOD_REASON_REALLOCATE (1 << 1)
49 #define MOD_REASON_SET_IS_UNDERGROUND (1 << 2)
50 #define MOD_REASON_SET_LIGHTING_COMPLETE (1 << 3)
51 #define MOD_REASON_SET_GENERATED (1 << 4)
52 #define MOD_REASON_SET_NODE (1 << 5)
53 #define MOD_REASON_SET_NODE_NO_CHECK (1 << 6)
54 #define MOD_REASON_SET_TIMESTAMP (1 << 7)
55 #define MOD_REASON_REPORT_META_CHANGE (1 << 8)
56 #define MOD_REASON_CLEAR_ALL_OBJECTS (1 << 9)
57 #define MOD_REASON_BLOCK_EXPIRED (1 << 10)
58 #define MOD_REASON_ADD_ACTIVE_OBJECT_RAW (1 << 11)
59 #define MOD_REASON_REMOVE_OBJECTS_REMOVE (1 << 12)
60 #define MOD_REASON_REMOVE_OBJECTS_DEACTIVATE (1 << 13)
61 #define MOD_REASON_TOO_MANY_OBJECTS (1 << 14)
62 #define MOD_REASON_STATIC_DATA_ADDED (1 << 15)
63 #define MOD_REASON_STATIC_DATA_REMOVED (1 << 16)
64 #define MOD_REASON_STATIC_DATA_CHANGED (1 << 17)
65 #define MOD_REASON_EXPIRE_DAYNIGHTDIFF (1 << 18)
66 #define MOD_REASON_VMANIP (1 << 19)
67 #define MOD_REASON_UNKNOWN (1 << 20)
76 MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy=false);
79 /*virtual u16 nodeContainerId() const
81 return NODECONTAINER_ID_MAPBLOCK;
92 data = new MapNode[nodecount];
93 for (u32 i = 0; i < nodecount; i++)
94 data[i] = MapNode(CONTENT_IGNORE);
96 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_REALLOCATE);
105 //// Modification tracking methods
107 void raiseModified(u32 mod, u32 reason=MOD_REASON_UNKNOWN)
109 if (mod > m_modified) {
111 m_modified_reason = reason;
112 if (m_modified >= MOD_STATE_WRITE_AT_UNLOAD)
113 m_disk_timestamp = m_timestamp;
114 } else if (mod == m_modified) {
115 m_modified_reason |= reason;
119 inline u32 getModified()
124 inline u32 getModifiedReason()
126 return m_modified_reason;
129 std::string getModifiedReasonString();
131 inline void resetModified()
133 m_modified = MOD_STATE_CLEAN;
134 m_modified_reason = 0;
141 inline bool isDummy()
146 inline void unDummify()
148 assert(isDummy()); // Pre-condition
152 // is_underground getter/setter
153 inline bool getIsUnderground()
155 return is_underground;
158 inline void setIsUnderground(bool a_is_underground)
160 is_underground = a_is_underground;
161 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_IS_UNDERGROUND);
164 inline void setLightingComplete(u16 newflags)
166 if (newflags != m_lighting_complete) {
167 m_lighting_complete = newflags;
168 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_LIGHTING_COMPLETE);
172 inline u16 getLightingComplete()
174 return m_lighting_complete;
177 inline void setLightingComplete(LightBank bank, u8 direction,
180 assert(direction >= 0 && direction <= 5);
181 if (bank == LIGHTBANK_NIGHT) {
184 u16 newflags = m_lighting_complete;
186 newflags |= 1 << direction;
188 newflags &= ~(1 << direction);
190 setLightingComplete(newflags);
193 inline bool isLightingComplete(LightBank bank, u8 direction)
195 assert(direction >= 0 && direction <= 5);
196 if (bank == LIGHTBANK_NIGHT) {
199 return (m_lighting_complete & (1 << direction)) != 0;
202 inline bool isGenerated()
207 inline void setGenerated(bool b)
209 if (b != m_generated) {
210 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_GENERATED);
219 inline v3s16 getPos()
224 inline v3s16 getPosRelative()
226 return m_pos_relative;
229 inline core::aabbox3d<s16> getBox()
231 return core::aabbox3d<s16>(getPosRelative(),
233 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
238 //// Regular MapNode get-setters
241 inline bool isValidPosition(s16 x, s16 y, s16 z)
244 && x >= 0 && x < MAP_BLOCKSIZE
245 && y >= 0 && y < MAP_BLOCKSIZE
246 && z >= 0 && z < MAP_BLOCKSIZE;
249 inline bool isValidPosition(v3s16 p)
251 return isValidPosition(p.X, p.Y, p.Z);
254 inline MapNode getNode(s16 x, s16 y, s16 z, bool *valid_position)
256 *valid_position = isValidPosition(x, y, z);
258 if (!*valid_position)
259 return {CONTENT_IGNORE};
261 return data[z * zstride + y * ystride + x];
264 inline MapNode getNode(v3s16 p, bool *valid_position)
266 return getNode(p.X, p.Y, p.Z, valid_position);
269 inline MapNode getNodeNoEx(v3s16 p)
272 return getNode(p.X, p.Y, p.Z, &is_valid);
275 inline void setNode(s16 x, s16 y, s16 z, MapNode & n)
277 if (!isValidPosition(x, y, z))
278 throw InvalidPositionException();
280 data[z * zstride + y * ystride + x] = n;
281 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_NODE);
284 inline void setNode(v3s16 p, MapNode & n)
286 setNode(p.X, p.Y, p.Z, n);
290 //// Non-checking variants of the above
293 inline MapNode getNodeNoCheck(s16 x, s16 y, s16 z, bool *valid_position)
295 *valid_position = data != nullptr;
296 if (!*valid_position)
297 return {CONTENT_IGNORE};
299 return data[z * zstride + y * ystride + x];
302 inline MapNode getNodeNoCheck(v3s16 p, bool *valid_position)
304 return getNodeNoCheck(p.X, p.Y, p.Z, valid_position);
308 //// Non-checking, unsafe variants of the above
309 //// MapBlock must be loaded by another function in the same scope/function
310 //// Caller must ensure that this is not a dummy block (by calling isDummy())
313 inline const MapNode &getNodeUnsafe(s16 x, s16 y, s16 z)
315 return data[z * zstride + y * ystride + x];
318 inline const MapNode &getNodeUnsafe(v3s16 &p)
320 return getNodeUnsafe(p.X, p.Y, p.Z);
323 inline void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
326 throw InvalidPositionException();
328 data[z * zstride + y * ystride + x] = n;
329 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_NODE_NO_CHECK);
332 inline void setNodeNoCheck(v3s16 p, MapNode & n)
334 setNodeNoCheck(p.X, p.Y, p.Z, n);
337 // These functions consult the parent container if the position
338 // is not valid on this MapBlock.
339 bool isValidPositionParent(v3s16 p);
340 MapNode getNodeParent(v3s16 p, bool *is_valid_position = NULL);
341 void setNodeParent(v3s16 p, MapNode & n);
343 inline void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
345 for (u16 z = 0; z < d; z++)
346 for (u16 y = 0; y < h; y++)
347 for (u16 x = 0; x < w; x++)
348 setNode(x0 + x, y0 + y, z0 + z, node);
351 // Copies data to VoxelManipulator to getPosRelative()
352 void copyTo(VoxelManipulator &dst);
354 // Copies data from VoxelManipulator getPosRelative()
355 void copyFrom(VoxelManipulator &dst);
357 // Update day-night lighting difference flag.
358 // Sets m_day_night_differs to appropriate value.
359 // These methods don't care about neighboring blocks.
360 void actuallyUpdateDayNightDiff();
362 // Call this to schedule what the previous function does to be done
363 // when the value is actually needed.
364 void expireDayNightDiff();
366 inline bool getDayNightDiff()
368 if (m_day_night_differs_expired)
369 actuallyUpdateDayNightDiff();
370 return m_day_night_differs;
374 //// Miscellaneous stuff
378 Tries to measure ground level.
383 0...MAP_BLOCKSIZE-1 = ground level
385 s16 getGroundLevel(v2s16 p2d);
388 //// Timestamp (see m_timestamp)
391 // NOTE: BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
393 inline void setTimestamp(u32 time)
396 raiseModified(MOD_STATE_WRITE_AT_UNLOAD, MOD_REASON_SET_TIMESTAMP);
399 inline void setTimestampNoChangedFlag(u32 time)
404 inline u32 getTimestamp()
409 inline u32 getDiskTimestamp()
411 return m_disk_timestamp;
415 //// Usage timer (see m_usage_timer)
418 inline void resetUsageTimer()
423 inline void incrementUsageTimer(float dtime)
425 m_usage_timer += dtime;
428 inline float getUsageTimer()
430 return m_usage_timer;
434 //// Reference counting (see m_refcount)
437 inline void refGrab()
442 inline void refDrop()
456 inline NodeTimer getNodeTimer(const v3s16 &p)
458 return m_node_timers.get(p);
461 inline void removeNodeTimer(const v3s16 &p)
463 m_node_timers.remove(p);
466 inline void setNodeTimer(const NodeTimer &t)
468 m_node_timers.set(t);
471 inline void clearNodeTimers()
473 m_node_timers.clear();
480 // These don't write or read version by itself
481 // Set disk to true for on-disk format, false for over-the-network format
482 // Precondition: version >= SER_FMT_VER_LOWEST_WRITE
483 void serialize(std::ostream &os, u8 version, bool disk);
484 // If disk == true: In addition to doing other things, will add
485 // unknown blocks from id-name mapping to wndef
486 void deSerialize(std::istream &is, u8 version, bool disk);
488 void serializeNetworkSpecific(std::ostream &os);
489 void deSerializeNetworkSpecific(std::istream &is);
495 void deSerialize_pre22(std::istream &is, u8 version, bool disk);
498 Used only internally, because changes can't be tracked
501 inline MapNode &getNodeRef(s16 x, s16 y, s16 z)
503 if (!isValidPosition(x, y, z))
504 throw InvalidPositionException();
506 return data[z * zstride + y * ystride + x];
509 inline MapNode &getNodeRef(v3s16 &p)
511 return getNodeRef(p.X, p.Y, p.Z);
516 Public member variables
519 #ifndef SERVER // Only on client
520 MapBlockMesh *mesh = nullptr;
523 NodeMetadataList m_node_metadata;
524 NodeTimerList m_node_timers;
525 StaticObjectList m_static_objects;
527 static const u32 ystride = MAP_BLOCKSIZE;
528 static const u32 zstride = MAP_BLOCKSIZE * MAP_BLOCKSIZE;
530 static const u32 nodecount = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
534 Private member variables
537 // NOTE: Lots of things rely on this being the Map
539 // Position in blocks on parent
542 /* This is the precalculated m_pos_relative value
543 * This caches the value, improving performance by removing 3 s16 multiplications
544 * at runtime on each getPosRelative call
545 * For a 5 minutes runtime with valgrind this removes 3 * 19M s16 multiplications
546 * The gain can be estimated in Release Build to 3 * 100M multiply operations for 5 mins
548 v3s16 m_pos_relative;
553 If NULL, block is a dummy block.
554 Dummy blocks are used for caching not-found-on-disk blocks.
556 MapNode *data = nullptr;
559 - On the server, this is used for telling whether the
560 block has been modified from the one on disk.
561 - On the client, this is used for nothing.
563 u32 m_modified = MOD_STATE_WRITE_NEEDED;
564 u32 m_modified_reason = MOD_REASON_INITIAL;
567 When propagating sunlight and the above block doesn't exist,
568 sunlight is assumed if this is false.
570 In practice this is set to true if the block is completely
571 undeground with nothing visible above the ground except
574 bool is_underground = false;
577 * Each bit indicates if light spreading was finished
578 * in a direction. (Because the neighbor could also be unloaded.)
579 * Bits (most significant first):
580 * nothing, nothing, nothing, nothing,
581 * night X-, night Y-, night Z-, night Z+, night Y+, night X+,
582 * day X-, day Y-, day Z-, day Z+, day Y+, day X+.
584 u16 m_lighting_complete = 0xFFFF;
586 // Whether day and night lighting differs
587 bool m_day_night_differs = false;
588 bool m_day_night_differs_expired = true;
590 bool m_generated = false;
593 When block is removed from active blocks, this is set to gametime.
594 Value BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
596 u32 m_timestamp = BLOCK_TIMESTAMP_UNDEFINED;
597 // The on-disk (or to-be on-disk) timestamp value
598 u32 m_disk_timestamp = BLOCK_TIMESTAMP_UNDEFINED;
601 When the block is accessed, this is set to 0.
602 Map will unload the block when this reaches a timeout.
604 float m_usage_timer = 0;
607 Reference count; currently used for determining if this block is in
608 the list of blocks to be drawn.
613 typedef std::vector<MapBlock*> MapBlockVect;
615 inline bool objectpos_over_limit(v3f p)
617 const float max_limit_bs = MAX_MAP_GENERATION_LIMIT * BS;
618 return p.X < -max_limit_bs ||
619 p.X > max_limit_bs ||
620 p.Y < -max_limit_bs ||
621 p.Y > max_limit_bs ||
622 p.Z < -max_limit_bs ||
626 inline bool blockpos_over_max_limit(v3s16 p)
628 const s16 max_limit_bp = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
629 return p.X < -max_limit_bp ||
630 p.X > max_limit_bp ||
631 p.Y < -max_limit_bp ||
632 p.Y > max_limit_bp ||
633 p.Z < -max_limit_bp ||
638 Returns the position of the block where the node is located
640 inline v3s16 getNodeBlockPos(const v3s16 &p)
642 return getContainerPos(p, MAP_BLOCKSIZE);
645 inline void getNodeBlockPosWithOffset(const v3s16 &p, v3s16 &block, v3s16 &offset)
647 getContainerPosWithOffset(p, MAP_BLOCKSIZE, block, offset);
651 Get a quick string to describe what a block actually contains
653 std::string analyze_block(MapBlock *block);