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.
20 #ifndef MAPBLOCK_HEADER
21 #define MAPBLOCK_HEADER
27 #include "exceptions.h"
28 #include "constants.h"
29 #include "staticobject.h"
30 #include "nodemetadata.h"
31 #include "nodetimer.h"
32 #include "modifiedstate.h"
33 #include "util/numeric.h" // getContainerPos
36 class NodeMetadataList;
39 class VoxelManipulator;
41 #define BLOCK_TIMESTAMP_UNDEFINED 0xffffffff
43 /*// Named by looking towards z+
53 // NOTE: If this is enabled, set MapBlock to be initialized with
55 /*enum BlockGenerationStatus
57 // Completely non-generated (filled with CONTENT_IGNORE).
59 // Trees or similar might have been blitted from other blocks to here.
60 // Otherwise, the block contains CONTENT_IGNORE
61 BLOCKGEN_FROM_NEIGHBORS=2,
62 // Has been generated, but some neighbors might put some stuff in here
63 // when they are generated.
64 // Does not contain any CONTENT_IGNORE
65 BLOCKGEN_SELF_GENERATED=4,
66 // The block and all its neighbors have been generated
67 BLOCKGEN_FULLY_GENERATED=6
73 NODECONTAINER_ID_MAPBLOCK,
74 NODECONTAINER_ID_MAPSECTOR,
76 NODECONTAINER_ID_MAPBLOCKCACHE,
77 NODECONTAINER_ID_VOXELMANIPULATOR,
83 virtual bool isValidPosition(v3s16 p) = 0;
84 virtual MapNode getNode(v3s16 p) = 0;
85 virtual void setNode(v3s16 p, MapNode & n) = 0;
86 virtual u16 nodeContainerId() const = 0;
88 MapNode getNodeNoEx(v3s16 p)
93 catch(InvalidPositionException &e){
94 return MapNode(CONTENT_IGNORE);
101 //// MapBlock modified reason flags
104 #define MOD_REASON_INITIAL (1 << 0)
105 #define MOD_REASON_REALLOCATE (1 << 1)
106 #define MOD_REASON_SET_IS_UNDERGROUND (1 << 2)
107 #define MOD_REASON_SET_LIGHTING_EXPIRED (1 << 3)
108 #define MOD_REASON_SET_GENERATED (1 << 4)
109 #define MOD_REASON_SET_NODE (1 << 5)
110 #define MOD_REASON_SET_NODE_NO_CHECK (1 << 6)
111 #define MOD_REASON_SET_TIMESTAMP (1 << 7)
112 #define MOD_REASON_REPORT_META_CHANGE (1 << 8)
113 #define MOD_REASON_CLEAR_ALL_OBJECTS (1 << 9)
114 #define MOD_REASON_BLOCK_EXPIRED (1 << 10)
115 #define MOD_REASON_ADD_ACTIVE_OBJECT_RAW (1 << 11)
116 #define MOD_REASON_REMOVE_OBJECTS_REMOVE (1 << 12)
117 #define MOD_REASON_REMOVE_OBJECTS_DEACTIVATE (1 << 13)
118 #define MOD_REASON_TOO_MANY_OBJECTS (1 << 14)
119 #define MOD_REASON_STATIC_DATA_ADDED (1 << 15)
120 #define MOD_REASON_STATIC_DATA_REMOVED (1 << 16)
121 #define MOD_REASON_STATIC_DATA_CHANGED (1 << 17)
122 #define MOD_REASON_EXPIRE_DAYNIGHTDIFF (1 << 18)
123 #define MOD_REASON_UNKNOWN (1 << 19)
129 class MapBlock /*: public NodeContainer*/
132 MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy=false);
135 /*virtual u16 nodeContainerId() const
137 return NODECONTAINER_ID_MAPBLOCK;
148 data = new MapNode[nodecount];
149 for (u32 i = 0; i < nodecount; i++)
150 data[i] = MapNode(CONTENT_IGNORE);
152 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_REALLOCATE);
156 //// Modification tracking methods
158 void raiseModified(u32 mod, u32 reason=MOD_REASON_UNKNOWN)
160 if (mod > m_modified) {
162 m_modified_reason = reason;
163 if (m_modified >= MOD_STATE_WRITE_AT_UNLOAD)
164 m_disk_timestamp = m_timestamp;
165 } else if (mod == m_modified) {
166 m_modified_reason |= reason;
170 inline u32 getModified()
175 inline u32 getModifiedReason()
177 return m_modified_reason;
180 std::string getModifiedReasonString();
182 inline void resetModified()
184 m_modified = MOD_STATE_CLEAN;
185 m_modified_reason = 0;
192 inline bool isDummy()
194 return (data == NULL);
197 inline void unDummify()
199 assert(isDummy()); // Pre-condition
203 // is_underground getter/setter
204 inline bool getIsUnderground()
206 return is_underground;
209 inline void setIsUnderground(bool a_is_underground)
211 is_underground = a_is_underground;
212 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_IS_UNDERGROUND);
215 inline void setLightingExpired(bool expired)
217 if (expired != m_lighting_expired){
218 m_lighting_expired = expired;
219 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_LIGHTING_EXPIRED);
223 inline bool getLightingExpired()
225 return m_lighting_expired;
228 inline bool isGenerated()
233 inline void setGenerated(bool b)
235 if (b != m_generated) {
236 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_GENERATED);
241 inline bool isValid()
243 if (m_lighting_expired)
254 inline v3s16 getPos()
259 inline v3s16 getPosRelative()
261 return m_pos_relative;
264 inline core::aabbox3d<s16> getBox()
266 return core::aabbox3d<s16>(getPosRelative(),
268 + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
273 //// Regular MapNode get-setters
276 inline bool isValidPosition(s16 x, s16 y, s16 z)
279 && x >= 0 && x < MAP_BLOCKSIZE
280 && y >= 0 && y < MAP_BLOCKSIZE
281 && z >= 0 && z < MAP_BLOCKSIZE;
284 inline bool isValidPosition(v3s16 p)
286 return isValidPosition(p.X, p.Y, p.Z);
289 inline MapNode getNode(s16 x, s16 y, s16 z, bool *valid_position)
291 *valid_position = isValidPosition(x, y, z);
293 if (!*valid_position)
294 return MapNode(CONTENT_IGNORE);
296 return data[z * zstride + y * ystride + x];
299 inline MapNode getNode(v3s16 p, bool *valid_position)
301 return getNode(p.X, p.Y, p.Z, valid_position);
304 inline MapNode getNodeNoEx(v3s16 p)
307 MapNode node = getNode(p.X, p.Y, p.Z, &is_valid);
308 return is_valid ? node : MapNode(CONTENT_IGNORE);
311 inline void setNode(s16 x, s16 y, s16 z, MapNode & n)
313 if (!isValidPosition(x, y, z))
314 throw InvalidPositionException();
316 data[z * zstride + y * ystride + x] = n;
317 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_NODE);
320 inline void setNode(v3s16 p, MapNode & n)
322 setNode(p.X, p.Y, p.Z, n);
326 //// Non-checking variants of the above
329 inline MapNode getNodeNoCheck(s16 x, s16 y, s16 z, bool *valid_position)
331 *valid_position = data != NULL;
333 return MapNode(CONTENT_IGNORE);
335 return data[z * zstride + y * ystride + x];
338 inline MapNode getNodeNoCheck(v3s16 p, bool *valid_position)
340 return getNodeNoCheck(p.X, p.Y, p.Z, valid_position);
343 inline void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
346 throw InvalidPositionException();
348 data[z * zstride + y * ystride + x] = n;
349 raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_NODE_NO_CHECK);
352 inline void setNodeNoCheck(v3s16 p, MapNode & n)
354 setNodeNoCheck(p.X, p.Y, p.Z, n);
357 // These functions consult the parent container if the position
358 // is not valid on this MapBlock.
359 bool isValidPositionParent(v3s16 p);
360 MapNode getNodeParent(v3s16 p, bool *is_valid_position = NULL);
361 void setNodeParent(v3s16 p, MapNode & n);
363 inline void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
365 for (u16 z = 0; z < d; z++)
366 for (u16 y = 0; y < h; y++)
367 for (u16 x = 0; x < w; x++)
368 setNode(x0 + x, y0 + y, z0 + z, node);
371 // See comments in mapblock.cpp
372 bool propagateSunlight(std::set<v3s16> &light_sources,
373 bool remove_light=false, bool *black_air_left=NULL);
375 // Copies data to VoxelManipulator to getPosRelative()
376 void copyTo(VoxelManipulator &dst);
378 // Copies data from VoxelManipulator getPosRelative()
379 void copyFrom(VoxelManipulator &dst);
381 // Update day-night lighting difference flag.
382 // Sets m_day_night_differs to appropriate value.
383 // These methods don't care about neighboring blocks.
384 void actuallyUpdateDayNightDiff();
386 // Call this to schedule what the previous function does to be done
387 // when the value is actually needed.
388 void expireDayNightDiff();
390 inline bool getDayNightDiff()
392 if (m_day_night_differs_expired)
393 actuallyUpdateDayNightDiff();
394 return m_day_night_differs;
398 //// Miscellaneous stuff
402 Tries to measure ground level.
407 0...MAP_BLOCKSIZE-1 = ground level
409 s16 getGroundLevel(v2s16 p2d);
412 //// Timestamp (see m_timestamp)
415 // NOTE: BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
417 inline void setTimestamp(u32 time)
420 raiseModified(MOD_STATE_WRITE_AT_UNLOAD, MOD_REASON_SET_TIMESTAMP);
423 inline void setTimestampNoChangedFlag(u32 time)
428 inline u32 getTimestamp()
433 inline u32 getDiskTimestamp()
435 return m_disk_timestamp;
439 //// Usage timer (see m_usage_timer)
442 inline void resetUsageTimer()
447 inline void incrementUsageTimer(float dtime)
449 m_usage_timer += dtime;
452 inline float getUsageTimer()
454 return m_usage_timer;
458 //// Reference counting (see m_refcount)
461 inline void refGrab()
466 inline void refDrop()
480 inline NodeTimer getNodeTimer(v3s16 p)
482 return m_node_timers.get(p);
485 inline void removeNodeTimer(v3s16 p)
487 m_node_timers.remove(p);
490 inline void setNodeTimer(v3s16 p, NodeTimer t)
492 m_node_timers.set(p,t);
495 inline void clearNodeTimers()
497 m_node_timers.clear();
504 // These don't write or read version by itself
505 // Set disk to true for on-disk format, false for over-the-network format
506 // Precondition: version >= SER_FMT_CLIENT_VER_LOWEST
507 void serialize(std::ostream &os, u8 version, bool disk);
508 // If disk == true: In addition to doing other things, will add
509 // unknown blocks from id-name mapping to wndef
510 void deSerialize(std::istream &is, u8 version, bool disk);
512 void serializeNetworkSpecific(std::ostream &os, u16 net_proto_version);
513 void deSerializeNetworkSpecific(std::istream &is);
520 void deSerialize_pre22(std::istream &is, u8 version, bool disk);
523 Used only internally, because changes can't be tracked
526 inline MapNode &getNodeRef(s16 x, s16 y, s16 z)
528 if (!isValidPosition(x, y, z))
529 throw InvalidPositionException();
531 return data[z * zstride + y * ystride + x];
534 inline MapNode &getNodeRef(v3s16 &p)
536 return getNodeRef(p.X, p.Y, p.Z);
541 Public member variables
544 #ifndef SERVER // Only on client
548 NodeMetadataList m_node_metadata;
549 NodeTimerList m_node_timers;
550 StaticObjectList m_static_objects;
552 static const u32 ystride = MAP_BLOCKSIZE;
553 static const u32 zstride = MAP_BLOCKSIZE * MAP_BLOCKSIZE;
555 static const u32 nodecount = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
559 Private member variables
562 // NOTE: Lots of things rely on this being the Map
564 // Position in blocks on parent
567 /* This is the precalculated m_pos_relative value
568 * This caches the value, improving performance by removing 3 s16 multiplications
569 * at runtime on each getPosRelative call
570 * For a 5 minutes runtime with valgrind this removes 3 * 19M s16 multiplications
571 * The gain can be estimated in Release Build to 3 * 100M multiply operations for 5 mins
573 v3s16 m_pos_relative;
578 If NULL, block is a dummy block.
579 Dummy blocks are used for caching not-found-on-disk blocks.
584 - On the server, this is used for telling whether the
585 block has been modified from the one on disk.
586 - On the client, this is used for nothing.
589 u32 m_modified_reason;
592 When propagating sunlight and the above block doesn't exist,
593 sunlight is assumed if this is false.
595 In practice this is set to true if the block is completely
596 undeground with nothing visible above the ground except
602 Set to true if changes has been made that make the old lighting
603 values wrong but the lighting hasn't been actually updated.
605 If this is false, lighting is exactly right.
606 If this is true, lighting might be wrong or right.
608 bool m_lighting_expired;
610 // Whether day and night lighting differs
611 bool m_day_night_differs;
612 bool m_day_night_differs_expired;
617 When block is removed from active blocks, this is set to gametime.
618 Value BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
621 // The on-disk (or to-be on-disk) timestamp value
622 u32 m_disk_timestamp;
625 When the block is accessed, this is set to 0.
626 Map will unload the block when this reaches a timeout.
631 Reference count; currently used for determining if this block is in
632 the list of blocks to be drawn.
637 typedef std::vector<MapBlock*> MapBlockVect;
639 inline bool blockpos_over_limit(v3s16 p)
642 (p.X < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
643 || p.X > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
644 || p.Y < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
645 || p.Y > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
646 || p.Z < -MAP_GENERATION_LIMIT / MAP_BLOCKSIZE
647 || p.Z > MAP_GENERATION_LIMIT / MAP_BLOCKSIZE);
651 Returns the position of the block where the node is located
653 inline v3s16 getNodeBlockPos(v3s16 p)
655 return getContainerPos(p, MAP_BLOCKSIZE);
658 inline v2s16 getNodeSectorPos(v2s16 p)
660 return getContainerPos(p, MAP_BLOCKSIZE);
663 inline s16 getNodeBlockY(s16 y)
665 return getContainerPos(y, MAP_BLOCKSIZE);
668 inline void getNodeBlockPosWithOffset(const v3s16 &p, v3s16 &block, v3s16 &offset)
670 getContainerPosWithOffset(p, MAP_BLOCKSIZE, block, offset);
673 inline void getNodeSectorPosWithOffset(const v2s16 &p, v2s16 &block, v2s16 &offset)
675 getContainerPosWithOffset(p, MAP_BLOCKSIZE, block, offset);
679 Get a quick string to describe what a block actually contains
681 std::string analyze_block(MapBlock *block);