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