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