Minetest 5.10.0-dev
 
Loading...
Searching...
No Matches
mapblock.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22#include <vector>
23#include "irr_v3d.h"
24#include "mapnode.h"
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
32#include "settings.h"
33
34class Map;
36class IGameDef;
37class MapBlockMesh;
39
40#define BLOCK_TIMESTAMP_UNDEFINED 0xffffffff
41
45
67
71
73{
74public:
75 MapBlock(v3s16 pos, IGameDef *gamedef);
76 ~MapBlock();
77
78 // Any server-modding code can "delete" arbitrary blocks (i.e. with
79 // core.delete_area), which makes them orphan. Avoid using orphan blocks for
80 // anything.
81 bool isOrphan() const
82 {
83 return m_orphan;
84 }
85
87 {
88 m_orphan = true;
89 }
90
92 {
93 for (u32 i = 0; i < nodecount; i++)
96 }
97
99 {
100 return data;
101 }
102
106 void raiseModified(u32 mod, u32 reason=MOD_REASON_UNKNOWN)
107 {
108 if (mod > m_modified) {
109 m_modified = mod;
110 m_modified_reason = reason;
113 } else if (mod == m_modified) {
114 m_modified_reason |= reason;
115 }
116 if (mod == MOD_STATE_WRITE_NEEDED)
117 contents.clear();
118 }
119
120 inline u32 getModified()
121 {
122 return m_modified;
123 }
124
125 inline u32 getModifiedReason()
126 {
127 return m_modified_reason;
128 }
129
130 std::string getModifiedReasonString();
131
132 inline void resetModified()
133 {
136 }
137
141
142 // is_underground getter/setter
143 inline bool getIsUnderground()
144 {
145 return is_underground;
146 }
147
148 inline void setIsUnderground(bool a_is_underground)
149 {
150 is_underground = a_is_underground;
152 }
153
154 inline void setLightingComplete(u16 newflags)
155 {
156 if (newflags != m_lighting_complete) {
157 m_lighting_complete = newflags;
159 }
160 }
161
163 {
164 return m_lighting_complete;
165 }
166
167 inline void setLightingComplete(LightBank bank, u8 direction,
168 bool is_complete)
169 {
170 assert(direction <= 5);
171 if (bank == LIGHTBANK_NIGHT) {
172 direction += 6;
173 }
174 u16 newflags = m_lighting_complete;
175 if (is_complete) {
176 newflags |= 1 << direction;
177 } else {
178 newflags &= ~(1 << direction);
179 }
180 setLightingComplete(newflags);
181 }
182
183 inline bool isLightingComplete(LightBank bank, u8 direction)
184 {
185 assert(direction <= 5);
186 if (bank == LIGHTBANK_NIGHT) {
187 direction += 6;
188 }
189 return (m_lighting_complete & (1 << direction)) != 0;
190 }
191
192 inline bool isGenerated()
193 {
194 return m_generated;
195 }
196
197 inline void setGenerated(bool b)
198 {
199 if (b != m_generated) {
201 m_generated = b;
202 }
203 }
204
208
209 inline v3s16 getPos()
210 {
211 return m_pos;
212 }
213
215 {
216 return m_pos_relative;
217 }
218
219 inline core::aabbox3d<s16> getBox() {
220 return getBox(getPosRelative());
221 }
222
223 static inline core::aabbox3d<s16> getBox(const v3s16 &pos_relative)
224 {
225 return core::aabbox3d<s16>(pos_relative,
226 pos_relative
228 - v3s16(1,1,1));
229 }
230
234
235 inline bool isValidPosition(s16 x, s16 y, s16 z)
236 {
237 return x >= 0 && x < MAP_BLOCKSIZE
238 && y >= 0 && y < MAP_BLOCKSIZE
239 && z >= 0 && z < MAP_BLOCKSIZE;
240 }
241
243 {
244 return isValidPosition(p.X, p.Y, p.Z);
245 }
246
247 inline MapNode getNode(s16 x, s16 y, s16 z, bool *valid_position)
248 {
249 *valid_position = isValidPosition(x, y, z);
250
251 if (!*valid_position)
252 return {CONTENT_IGNORE};
253
254 return data[z * zstride + y * ystride + x];
255 }
256
257 inline MapNode getNode(v3s16 p, bool *valid_position)
258 {
259 return getNode(p.X, p.Y, p.Z, valid_position);
260 }
261
263 {
264 bool is_valid;
265 return getNode(p.X, p.Y, p.Z, &is_valid);
266 }
267
268 inline void setNode(s16 x, s16 y, s16 z, MapNode n)
269 {
270 if (!isValidPosition(x, y, z))
272
273 data[z * zstride + y * ystride + x] = n;
275 }
276
277 inline void setNode(v3s16 p, MapNode n)
278 {
279 setNode(p.X, p.Y, p.Z, n);
280 }
281
285
286 inline MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
287 {
288 return data[z * zstride + y * ystride + x];
289 }
290
292 {
293 return getNodeNoCheck(p.X, p.Y, p.Z);
294 }
295
296 inline void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode n)
297 {
298 data[z * zstride + y * ystride + x] = n;
300 }
301
303 {
304 setNodeNoCheck(p.X, p.Y, p.Z, n);
305 }
306
307 // Copies data to VoxelManipulator to getPosRelative()
308 void copyTo(VoxelManipulator &dst);
309
310 // Copies data from VoxelManipulator getPosRelative()
311 void copyFrom(VoxelManipulator &dst);
312
313 // Update is air flag.
314 // Sets m_is_air to appropriate value.
315 void actuallyUpdateIsAir();
316
317 // Call this to schedule what the previous function does to be done
318 // when the value is actually needed.
319 void expireIsAirCache();
320
321 inline bool isAir()
322 {
325 return m_is_air;
326 }
327
328 bool onObjectsActivation();
329 bool saveStaticObject(u16 id, const StaticObject &obj, u32 reason);
330
331 void step(float dtime, const std::function<bool(v3s16, MapNode, f32)> &on_timer_cb);
332
336
337 // NOTE: BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
338
344
345 inline void setTimestampNoChangedFlag(u32 time)
346 {
347 m_timestamp = time;
348 }
349
350 inline u32 getTimestamp()
351 {
352 return m_timestamp;
353 }
354
355 inline u32 getDiskTimestamp()
356 {
357 return m_disk_timestamp;
358 }
359
363
364 inline void resetUsageTimer()
365 {
366 m_usage_timer = 0;
367 }
368
369 inline void incrementUsageTimer(float dtime)
370 {
371 m_usage_timer += dtime;
372 }
373
374 inline float getUsageTimer()
375 {
376 return m_usage_timer;
377 }
378
382
383 inline void refGrab()
384 {
385 assert(m_refcount < SHRT_MAX);
386 m_refcount++;
387 }
388
389 inline void refDrop()
390 {
391 assert(m_refcount > 0);
392 m_refcount--;
393 }
394
395 inline short refGet()
396 {
397 return m_refcount;
398 }
399
403
405 {
406 return m_node_timers.get(p);
407 }
408
410 {
412 }
413
414 inline void setNodeTimer(const NodeTimer &t)
415 {
417 }
418
419 inline void clearNodeTimers()
420 {
422 }
423
427
428 // These don't write or read version by itself
429 // Set disk to true for on-disk format, false for over-the-network format
430 // Precondition: version >= SER_FMT_VER_LOWEST_WRITE
431 void serialize(std::ostream &result, u8 version, bool disk, int compression_level);
432 // If disk == true: In addition to doing other things, will add
433 // unknown blocks from id-name mapping to wndef
434 void deSerialize(std::istream &is, u8 version, bool disk);
435
436 void serializeNetworkSpecific(std::ostream &os);
437 void deSerializeNetworkSpecific(std::istream &is);
438
439 bool storeActiveObject(u16 id);
440 // clearObject and return removed objects count
441 u32 clearObjects();
442
443 static const u32 ystride = MAP_BLOCKSIZE;
444 static const u32 zstride = MAP_BLOCKSIZE * MAP_BLOCKSIZE;
445
447
448private:
449 /*
450 Private methods
451 */
452
453 void deSerialize_pre22(std::istream &is, u8 version, bool disk);
454
455 /*
456 * PLEASE NOTE: When adding something here be mindful of position and size
457 * of member variables! This is also the reason for the weird public-private
458 * interleaving.
459 * If in doubt consult `pahole` to see the effects.
460 */
461
462public:
463#ifndef SERVER // Only on client
464 MapBlockMesh *mesh = nullptr;
465
466 // marks the sides which are opaque: 00+Z-Z+Y-Y+X-X
468#endif
469
470private:
471 // see isOrphan()
472 bool m_orphan = false;
473
474 // Position in blocks on parent
476
477 /* Precalculated m_pos_relative value
478 * This caches the value, improving performance by removing 3 s16 multiplications
479 * at runtime on each getPosRelative call.
480 * For a 5 minutes runtime with valgrind this removes 3 * 19M s16 multiplications.
481 * The gain can be estimated in Release Build to 3 * 100M multiply operations for 5 mins.
482 */
484
485 /*
486 Reference count; currently used for determining if this block is in
487 the list of blocks to be drawn.
488 */
489 short m_refcount = 0;
490
491 /*
492 * Note that this is not an inline array because that has implications for
493 * heap fragmentation (the array is exactly 16K), CPU caches and/or
494 * optimizability of algorithms working on this array.
495 */
496 MapNode *const data; // of `nodecount` elements
497
498 // provides the item and node definitions
500
501 /*
502 When the block is accessed, this is set to 0.
503 Map will unload the block when this reaches a timeout.
504 */
505 float m_usage_timer = 0;
506
507public:
509 // True if we never want to cache content types for this block
511 // Cache of content types
512 // This is actually a set but for the small sizes we have a vector should be
513 // more efficient.
514 // Can be empty, in which case nothing was cached yet.
515 std::vector<content_t> contents;
516
517private:
518 // Whether day and night lighting differs
519 bool m_is_air = false;
520 bool m_is_air_expired = true;
521
522 /*
523 - On the server, this is used for telling whether the
524 block has been modified from the one on disk.
525 - On the client, this is used for nothing.
526 */
529
530 /*
531 When block is removed from active blocks, this is set to gametime.
532 Value BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
533 */
535 // The on-disk (or to-be on-disk) timestamp value
537
547
548 // Whether mapgen has generated the content of this block (persisted)
549 bool m_generated = false;
550
551 /*
552 When propagating sunlight and the above block doesn't exist,
553 sunlight is assumed if this is false.
554
555 In practice this is set to true if the block is completely
556 undeground with nothing visible above the ground except
557 caves.
558 */
559 bool is_underground = false;
560
561public:
564
565private:
567};
568
569typedef std::vector<MapBlock*> MapBlockVect;
570
572{
573 const float max_limit_bs = (MAX_MAP_GENERATION_LIMIT + 0.5f) * BS;
574 return p.X < -max_limit_bs ||
575 p.X > max_limit_bs ||
576 p.Y < -max_limit_bs ||
577 p.Y > max_limit_bs ||
578 p.Z < -max_limit_bs ||
579 p.Z > max_limit_bs;
580}
581
583{
584 const s16 max_limit_bp = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
585 return p.X < -max_limit_bp ||
586 p.X > max_limit_bp ||
587 p.Y < -max_limit_bp ||
588 p.Y > max_limit_bp ||
589 p.Z < -max_limit_bp ||
590 p.Z > max_limit_bp;
591}
592
593/*
594 Returns the position of the block where the node is located
595*/
600
601inline void getNodeBlockPosWithOffset(v3s16 p, v3s16 &block, v3s16 &offset)
602{
604}
605
606/*
607 Get a quick string to describe what a block actually contains
608*/
609std::string analyze_block(MapBlock *block);
Definition gamedef.h:51
Definition exceptions.h:123
Definition mapblock_mesh.h:181
Definition mapblock.h:73
bool is_underground
Definition mapblock.h:559
bool m_orphan
Definition mapblock.h:472
u16 getLightingComplete()
Definition mapblock.h:162
bool getIsUnderground()
Definition mapblock.h:143
MapNode *const data
Definition mapblock.h:496
MapNode getNodeNoCheck(s16 x, s16 y, s16 z)
Definition mapblock.h:286
float m_usage_timer
Definition mapblock.h:505
void incrementUsageTimer(float dtime)
Definition mapblock.h:369
void raiseModified(u32 mod, u32 reason=MOD_REASON_UNKNOWN)
Definition mapblock.h:106
u32 m_disk_timestamp
Definition mapblock.h:536
bool isValidPosition(s16 x, s16 y, s16 z)
Definition mapblock.h:235
MapBlockMesh * mesh
Definition mapblock.h:464
void removeNodeTimer(v3s16 p)
Definition mapblock.h:409
void setTimestamp(u32 time)
Definition mapblock.h:339
v3s16 getPos()
Definition mapblock.h:209
static const u32 ystride
Definition mapblock.h:443
short refGet()
Definition mapblock.h:395
void clearNodeTimers()
Definition mapblock.h:419
void setTimestampNoChangedFlag(u32 time)
Definition mapblock.h:345
u32 getModified()
Definition mapblock.h:120
MapNode getNodeNoEx(v3s16 p)
Definition mapblock.h:262
v3s16 m_pos_relative
Definition mapblock.h:483
u32 m_timestamp
Definition mapblock.h:534
u8 solid_sides
Definition mapblock.h:467
v3s16 m_pos
Definition mapblock.h:475
void reallocate()
Definition mapblock.h:91
void setGenerated(bool b)
Definition mapblock.h:197
NodeMetadataList m_node_metadata
Definition mapblock.h:562
bool isLightingComplete(LightBank bank, u8 direction)
Definition mapblock.h:183
u32 clearObjects()
Definition mapblock.cpp:609
MapNode getNode(s16 x, s16 y, s16 z, bool *valid_position)
Definition mapblock.h:247
u32 m_modified_reason
Definition mapblock.h:528
bool m_is_air
Definition mapblock.h:519
u16 m_modified
Definition mapblock.h:527
void setNode(s16 x, s16 y, s16 z, MapNode n)
Definition mapblock.h:268
v3s16 getPosRelative()
Definition mapblock.h:214
float getUsageTimer()
Definition mapblock.h:374
bool isGenerated()
Definition mapblock.h:192
bool m_generated
Definition mapblock.h:549
void setNodeNoCheck(v3s16 p, MapNode n)
Definition mapblock.h:302
static const u32 nodecount
Definition mapblock.h:446
void resetModified()
Definition mapblock.h:132
void copyTo(VoxelManipulator &dst)
Definition mapblock.cpp:171
void makeOrphan()
Definition mapblock.h:86
static const u32 zstride
Definition mapblock.h:444
void deSerialize_pre22(std::istream &is, u8 version, bool disk)
Definition mapblock.cpp:622
void refDrop()
Definition mapblock.h:389
bool m_is_air_expired
Definition mapblock.h:520
StaticObjectList m_static_objects
Definition mapblock.h:563
bool saveStaticObject(u16 id, const StaticObject &obj, u32 reason)
Definition mapblock.cpp:115
MapNode getNode(v3s16 p, bool *valid_position)
Definition mapblock.h:257
bool isValidPosition(v3s16 p)
Definition mapblock.h:242
void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode n)
Definition mapblock.h:296
void serialize(std::ostream &result, u8 version, bool disk, int compression_level)
Serialization.
Definition mapblock.cpp:323
bool isAir()
Definition mapblock.h:321
u32 getDiskTimestamp()
Definition mapblock.h:355
bool isOrphan() const
Definition mapblock.h:81
std::vector< content_t > contents
Definition mapblock.h:515
u16 m_lighting_complete
Definition mapblock.h:546
u32 getTimestamp()
Definition mapblock.h:350
std::string getModifiedReasonString()
Definition mapblock.cpp:149
void setLightingComplete(u16 newflags)
Definition mapblock.h:154
NodeTimer getNodeTimer(v3s16 p)
Definition mapblock.h:404
MapNode * getData()
Definition mapblock.h:98
void deSerialize(std::istream &is, u8 version, bool disk)
Definition mapblock.cpp:439
void resetUsageTimer()
Definition mapblock.h:364
void setLightingComplete(LightBank bank, u8 direction, bool is_complete)
Definition mapblock.h:167
void setNode(v3s16 p, MapNode n)
Definition mapblock.h:277
bool onObjectsActivation()
Definition mapblock.cpp:91
MapNode getNodeNoCheck(v3s16 p)
Definition mapblock.h:291
bool storeActiveObject(u16 id)
Definition mapblock.cpp:598
void deSerializeNetworkSpecific(std::istream &is)
Definition mapblock.cpp:584
MapBlock(v3s16 pos, IGameDef *gamedef)
Definition mapblock.cpp:68
void copyFrom(VoxelManipulator &dst)
Definition mapblock.cpp:181
void refGrab()
Definition mapblock.h:383
void setIsUnderground(bool a_is_underground)
Definition mapblock.h:148
u32 getModifiedReason()
Definition mapblock.h:125
void serializeNetworkSpecific(std::ostream &os)
Definition mapblock.cpp:434
~MapBlock()
Definition mapblock.cpp:78
void expireIsAirCache()
Definition mapblock.cpp:209
bool do_not_cache_contents
Definition mapblock.h:510
void setNodeTimer(const NodeTimer &t)
Definition mapblock.h:414
NodeTimerList m_node_timers
Definition mapblock.h:566
void actuallyUpdateIsAir()
Definition mapblock.cpp:191
IGameDef * m_gamedef
Definition mapblock.h:499
static core::aabbox3d< s16 > getBox(const v3s16 &pos_relative)
Definition mapblock.h:223
short m_refcount
Definition mapblock.h:489
core::aabbox3d< s16 > getBox()
Definition mapblock.h:219
void step(float dtime, const std::function< bool(v3s16, MapNode, f32)> &on_timer_cb)
Definition mapblock.cpp:133
Definition map.h:116
Definition nodemetadata.h:77
Definition nodetimer.h:58
void remove(v3s16 p)
Definition nodetimer.h:77
void set(const NodeTimer &timer)
Definition nodetimer.h:105
NodeTimer get(const v3s16 &p)
Definition nodetimer.h:67
void clear()
Definition nodetimer.h:110
Definition nodetimer.h:36
Definition staticobject.h:45
Definition voxel.h:381
#define BS
Definition constants.h:76
#define MAP_BLOCKSIZE
Definition constants.h:79
#define MAX_MAP_GENERATION_LIMIT
Definition constants.h:69
core::vector3d< s16 > v3s16
Definition irr_v3d.h:28
core::vector3df v3f
Definition irr_v3d.h:26
std::vector< MapBlock * > MapBlockVect
Definition mapblock.h:569
v3s16 getNodeBlockPos(v3s16 p)
Definition mapblock.h:596
bool objectpos_over_limit(v3f p)
Definition mapblock.h:571
std::string analyze_block(MapBlock *block)
Definition mapblock.cpp:829
#define BLOCK_TIMESTAMP_UNDEFINED
Definition mapblock.h:40
void getNodeBlockPosWithOffset(v3s16 p, v3s16 &block, v3s16 &offset)
Definition mapblock.h:601
bool blockpos_over_max_limit(v3s16 p)
Definition mapblock.h:582
ModReason
Definition mapblock.h:46
@ MOD_REASON_SET_TIMESTAMP
Definition mapblock.h:52
@ MOD_REASON_CLEAR_ALL_OBJECTS
Definition mapblock.h:54
@ MOD_REASON_UNKNOWN
Definition mapblock.h:65
@ MOD_REASON_STATIC_DATA_CHANGED
Definition mapblock.h:62
@ MOD_REASON_STATIC_DATA_REMOVED
Definition mapblock.h:61
@ MOD_REASON_SET_IS_UNDERGROUND
Definition mapblock.h:48
@ MOD_REASON_SET_NODE
Definition mapblock.h:51
@ MOD_REASON_TOO_MANY_OBJECTS
Definition mapblock.h:59
@ MOD_REASON_REMOVE_OBJECTS_REMOVE
Definition mapblock.h:57
@ MOD_REASON_REPORT_META_CHANGE
Definition mapblock.h:53
@ MOD_REASON_ADD_ACTIVE_OBJECT_RAW
Definition mapblock.h:56
@ MOD_REASON_REALLOCATE
Definition mapblock.h:47
@ MOD_REASON_EXPIRE_IS_AIR
Definition mapblock.h:63
@ MOD_REASON_BLOCK_EXPIRED
Definition mapblock.h:55
@ MOD_REASON_VMANIP
Definition mapblock.h:64
@ MOD_REASON_SET_GENERATED
Definition mapblock.h:50
@ MOD_REASON_REMOVE_OBJECTS_DEACTIVATE
Definition mapblock.h:58
@ MOD_REASON_STATIC_DATA_ADDED
Definition mapblock.h:60
@ MOD_REASON_SET_LIGHTING_COMPLETE
Definition mapblock.h:49
#define CONTENT_IGNORE
Definition mapnode.h:73
LightBank
Definition mapnode.h:95
@ LIGHTBANK_NIGHT
Definition mapnode.h:97
@ MOD_STATE_WRITE_AT_UNLOAD
Definition modifiedstate.h:30
@ MOD_STATE_CLEAN
Definition modifiedstate.h:27
@ MOD_STATE_WRITE_NEEDED
Definition modifiedstate.h:33
s16 getContainerPos(s16 p, s16 d)
Definition numeric.h:45
void getContainerPosWithOffset(s16 p, s16 d, s16 &container, s16 &offset)
Definition numeric.h:84
Definition mapnode.h:139
Definition staticobject.h:32
static std::string p(std::string path)
Definition test_filesys.cpp:66