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