Luanti 5.15.0-dev
 
Loading...
Searching...
No Matches
mapnode.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
8#include "light.h"
9#include "util/pointer.h"
10#include <vector>
11
12class NodeDefManager;
13class Map;
14
15/*
16 Naming scheme:
17 - Material = irrlicht's Material class
18 - Content = (content_t) content of a node
19 - Tile = TileSpec at some side of a node of some content type
20*/
21typedef u16 content_t;
22#define CONTENT_MAX UINT16_MAX
23
24/*
25 The maximum node ID that can be registered by mods. This must
26 be significantly lower than the maximum content_t value, so that
27 there is enough room for dummy node IDs, which are created when
28 a MapBlock containing unknown node names is loaded from disk.
29*/
30#define MAX_REGISTERED_CONTENT 0x7fffU
31
32/*
33 A solid walkable node with the texture unknown_node.png.
34
35 For example, used on the client to display unregistered node IDs
36 (instead of expanding the vector of node definitions each time
37 such a node is received).
38*/
39#define CONTENT_UNKNOWN 125
40
41/*
42 The common material through which the player can walk and which
43 is transparent to light
44*/
45#define CONTENT_AIR 126
46
47/*
48 Ignored node.
49
50 Unloaded chunks are considered to consist of this. Several other
51 methods return this when an error occurs. Also, during
52 map generation this means the node has not been set yet.
53
54 Doesn't create faces with anything and is considered being
55 out-of-map in the game map.
56*/
57#define CONTENT_IGNORE 127
58
59/*
60 Content lighting information that fits into a single byte.
61*/
64 bool has_light : 1;
67
68 bool operator==(const ContentLightingFlags &other) const
69 {
70 return has_light == other.has_light && light_propagates == other.light_propagates &&
73 }
74 bool operator!=(const ContentLightingFlags &other) const { return !(*this == other); }
75};
76static_assert(sizeof(ContentLightingFlags) == 1, "Unexpected ContentLightingFlags size");
77
83
84/*
85 Simple rotation enum.
86*/
94
95/*
96 Masks for MapNode.param2 of flowing liquids
97 */
98#define LIQUID_LEVEL_MASK 0x07
99#define LIQUID_FLOW_DOWN_MASK 0x08
100
101//#define LIQUID_LEVEL_MASK 0x3f // better finite water
102//#define LIQUID_FLOW_DOWN_MASK 0x40 // not used when finite water
103
104/* maximum amount of liquid in a block */
105#define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
106#define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
107
108#define LIQUID_INFINITY_MASK 0x80 //0b10000000
109
110// mask for leveled nodebox param2
111#define LEVELED_MASK 0x7F
112#define LEVELED_MAX LEVELED_MASK
113
114
115struct ContentFeatures;
116
117/*
118 This is the stuff what the whole world consists of.
119*/
120
121
122struct alignas(u32) MapNode
123{
124 /*
125 Main content
126 */
128
129 /*
130 Misc parameter. Initialized to 0.
131 - For light_propagates() blocks, this is light intensity,
132 stored logarithmically from 0 to LIGHT_MAX.
133 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
134 - Contains 2 values, day- and night lighting. Each takes 4 bits.
135 - Uhh... well, most blocks have light or nothing in here.
136 */
138
139 /*
140 The second parameter. Initialized to 0.
141 E.g. direction for torches and flowing water.
142 */
144
145 MapNode() = default;
146
147 constexpr MapNode(content_t content, u8 a_param1=0, u8 a_param2=0) noexcept
148 : param0(content),
149 param1(a_param1),
150 param2(a_param2)
151 { }
152
153 bool operator==(const MapNode &other) const noexcept
154 {
155 return (param0 == other.param0
156 && param1 == other.param1
157 && param2 == other.param2);
158 }
159 bool operator!=(const MapNode &other) const noexcept
160 {
161 return !(*this == other);
162 }
163
164 // To be used everywhere
165 content_t getContent() const noexcept
166 {
167 return param0;
168 }
169 void setContent(content_t c) noexcept
170 {
171 param0 = c;
172 }
173 u8 getParam1() const noexcept
174 {
175 return param1;
176 }
177 void setParam1(u8 p) noexcept
178 {
179 param1 = p;
180 }
181 u8 getParam2() const noexcept
182 {
183 return param2;
184 }
185 void setParam2(u8 p) noexcept
186 {
187 param2 = p;
188 }
189
190 inline void setLight(LightBank bank, u8 a_light, ContentLightingFlags f) noexcept
191 {
192 // If node doesn't contain light data, ignore this
193 if (!f.has_light)
194 return;
195 if (bank == LIGHTBANK_DAY) {
196 param1 &= 0xf0;
197 param1 |= a_light & 0x0f;
198 } else {
199 assert(bank == LIGHTBANK_NIGHT);
200 param1 &= 0x0f;
201 param1 |= (a_light & 0x0f)<<4;
202 }
203 }
204
210 inline bool isLightDayNightEq(ContentLightingFlags f) const noexcept
211 {
212 return !f.has_light || getLight(LIGHTBANK_DAY, f) == getLight(LIGHTBANK_NIGHT, f);
213 }
214
215 inline u8 getLight(LightBank bank, ContentLightingFlags f) const noexcept
216 {
217 u8 raw_light = getLightRaw(bank, f);
218 return MYMAX(f.light_source, raw_light);
219 }
220
226 inline u8 getLightRaw(LightBank bank, ContentLightingFlags f) const noexcept
227 {
228 if(f.has_light)
229 return bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
230 return 0;
231 }
232
233 // 0 <= daylight_factor <= 1000
234 // 0 <= return value <= LIGHT_SUN
235 u8 getLightBlend(u32 daylight_factor, ContentLightingFlags f) const
236 {
237 u8 lightday = getLight(LIGHTBANK_DAY, f);
238 u8 lightnight = getLight(LIGHTBANK_NIGHT, f);
239 return blend_light(daylight_factor, lightday, lightnight);
240 }
241
242 u8 getFaceDir(const NodeDefManager *nodemgr, bool allow_wallmounted = false) const;
243 u8 getWallMounted(const NodeDefManager *nodemgr) const;
244 v3s16 getWallMountedDir(const NodeDefManager *nodemgr) const;
245
247 u8 getDegRotate(const NodeDefManager *nodemgr) const;
248
249 void rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot);
250
256 u8 getNeighbors(v3s16 p, Map *map) const;
257
258 /*
259 Gets list of node boxes (used for rendering (NDT_NODEBOX))
260 */
261 void getNodeBoxes(const NodeDefManager *nodemgr, std::vector<aabb3f> *boxes,
262 u8 neighbors = 0) const;
263
264 /*
265 Gets list of selection boxes
266 */
267 void getSelectionBoxes(const NodeDefManager *nodemg,
268 std::vector<aabb3f> *boxes, u8 neighbors = 0) const;
269
270 /*
271 Gets list of collision boxes
272 */
273 void getCollisionBoxes(const NodeDefManager *nodemgr,
274 std::vector<aabb3f> *boxes, u8 neighbors = 0) const;
275
276 /*
277 Liquid/leveled helpers
278 */
279 u8 getMaxLevel(const NodeDefManager *nodemgr) const;
280 u8 getLevel(const NodeDefManager *nodemgr) const;
281 s8 setLevel(const NodeDefManager *nodemgr, s16 level = 1);
282 s8 addLevel(const NodeDefManager *nodemgr, s16 add = 1);
283
284 /*
285 Serialization functions
286 */
287
288 static u32 serializedLength(u8 version);
289 void serialize(u8 *dest, u8 version) const;
290 void deSerialize(const u8 *source, u8 version);
291
292 // Serializes or deserializes a list of nodes in bulk format (first the
293 // content of all nodes, then the param1 of all nodes, then the param2
294 // of all nodes).
295 // version = serialization version. Must be >= 22
296 // content_width = the number of bytes of content per node
297 // params_width = the number of bytes of params per node
298 // compressed = true to zlib-compress output
299 // is_mono_block = if true, nodes is array of size 1
300 static Buffer<u8> serializeBulk(int version,
301 const MapNode *nodes, u32 nodecount,
302 u8 content_width, u8 params_width, bool is_mono_block = false);
303 static void deSerializeBulk(std::istream &is, int version,
304 MapNode *nodes, u32 nodecount,
305 u8 content_width, u8 params_width);
306
307private:
308 // Deprecated serialization methods
309 void deSerialize_pre22(const u8 *source, u8 version);
310};
#define MYMAX(a, b)
Definition basic_macros.h:11
Definition pointer.h:15
Definition map.h:100
This class is for getting the actual properties of nodes from their content ID.
Definition nodedef.h:509
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
u8 blend_light(u32 daylight_factor, u8 lightday, u8 lightnight)
Definition light.h:61
Rotation
Definition mapnode.h:87
@ ROTATE_180
Definition mapnode.h:90
@ ROTATE_270
Definition mapnode.h:91
@ ROTATE_RAND
Definition mapnode.h:92
@ ROTATE_90
Definition mapnode.h:89
@ ROTATE_0
Definition mapnode.h:88
u16 content_t
Definition mapnode.h:21
LightBank
Definition mapnode.h:79
@ LIGHTBANK_NIGHT
Definition mapnode.h:81
@ LIGHTBANK_DAY
Definition mapnode.h:80
Definition nodedef.h:294
Definition mapnode.h:62
bool light_propagates
Definition mapnode.h:65
bool sunlight_propagates
Definition mapnode.h:66
u8 light_source
Definition mapnode.h:63
bool operator!=(const ContentLightingFlags &other) const
Definition mapnode.h:74
bool operator==(const ContentLightingFlags &other) const
Definition mapnode.h:68
bool has_light
Definition mapnode.h:64
Definition mapnode.h:123
u8 getParam2() const noexcept
Definition mapnode.h:181
bool isLightDayNightEq(ContentLightingFlags f) const noexcept
Check if the light value for night differs from the light value for day.
Definition mapnode.h:210
content_t getContent() const noexcept
Definition mapnode.h:165
bool operator==(const MapNode &other) const noexcept
Definition mapnode.h:153
u8 param1
Definition mapnode.h:137
u8 param2
Definition mapnode.h:143
u8 getParam1() const noexcept
Definition mapnode.h:173
void setContent(content_t c) noexcept
Definition mapnode.h:169
void setLight(LightBank bank, u8 a_light, ContentLightingFlags f) noexcept
Definition mapnode.h:190
u8 getLightRaw(LightBank bank, ContentLightingFlags f) const noexcept
Definition mapnode.h:226
u8 getLight(LightBank bank, ContentLightingFlags f) const noexcept
Definition mapnode.h:215
u16 param0
Definition mapnode.h:127
MapNode()=default
void setParam2(u8 p) noexcept
Definition mapnode.h:185
constexpr MapNode(content_t content, u8 a_param1=0, u8 a_param2=0) noexcept
Definition mapnode.h:147
void setParam1(u8 p) noexcept
Definition mapnode.h:177
bool operator!=(const MapNode &other) const noexcept
Definition mapnode.h:159
u8 getLightBlend(u32 daylight_factor, ContentLightingFlags f) const
Definition mapnode.h:235
static std::string p(std::string path)
Definition test_filesys.cpp:64