3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "irrlichttypes_bloated.h"
24 #include "util/pointer.h"
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
37 typedef u16 content_t;
38 #define CONTENT_MAX UINT16_MAX
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.
46 #define MAX_REGISTERED_CONTENT 0x7fffU
49 A solid walkable node with the texture unknown_node.png.
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).
55 #define CONTENT_UNKNOWN 125
58 The common material through which the player can walk and which
59 is transparent to light
61 #define CONTENT_AIR 126
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.
70 Doesn't create faces with anything and is considered being
71 out-of-map in the game map.
73 #define CONTENT_IGNORE 127
76 Content lighting information that fits into a single byte.
78 struct ContentLightingFlags {
81 bool light_propagates : 1;
82 bool sunlight_propagates : 1;
84 bool operator==(const ContentLightingFlags &other) const
86 return has_light == other.has_light && light_propagates == other.light_propagates &&
87 sunlight_propagates == other.sunlight_propagates &&
88 light_source == other.light_source;
90 bool operator!=(const ContentLightingFlags &other) const { return !(*this == other); }
92 static_assert(sizeof(ContentLightingFlags) == 1, "Unexpected ContentLightingFlags size");
101 Simple rotation enum.
112 Masks for MapNode.param2 of flowing liquids
114 #define LIQUID_LEVEL_MASK 0x07
115 #define LIQUID_FLOW_DOWN_MASK 0x08
117 //#define LIQUID_LEVEL_MASK 0x3f // better finite water
118 //#define LIQUID_FLOW_DOWN_MASK 0x40 // not used when finite water
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)
124 #define LIQUID_INFINITY_MASK 0x80 //0b10000000
126 // mask for leveled nodebox param2
127 #define LEVELED_MASK 0x7F
128 #define LEVELED_MAX LEVELED_MASK
131 struct ContentFeatures;
134 This is the stuff what the whole world consists of.
138 struct alignas(u32) MapNode
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.
156 The second parameter. Initialized to 0.
157 E.g. direction for torches and flowing water.
163 MapNode(content_t content, u8 a_param1=0, u8 a_param2=0) noexcept
169 bool operator==(const MapNode &other) const noexcept
171 return (param0 == other.param0
172 && param1 == other.param1
173 && param2 == other.param2);
176 // To be used everywhere
177 content_t getContent() const noexcept
181 void setContent(content_t c) noexcept
185 u8 getParam1() const noexcept
189 void setParam1(u8 p) noexcept
193 u8 getParam2() const noexcept
197 void setParam2(u8 p) noexcept
203 * Returns the color of the node.
205 * \param f content features of this node
206 * \param color output, contains the node's color.
208 void getColor(const ContentFeatures &f, video::SColor *color) const;
210 inline void setLight(LightBank bank, u8 a_light, ContentLightingFlags f) noexcept
212 // If node doesn't contain light data, ignore this
215 if (bank == LIGHTBANK_DAY) {
217 param1 |= a_light & 0x0f;
219 assert(bank == LIGHTBANK_NIGHT);
221 param1 |= (a_light & 0x0f)<<4;
226 * Check if the light value for night differs from the light value for day.
228 * @return If the light values are equal, returns true; otherwise false
230 inline bool isLightDayNightEq(ContentLightingFlags f) const noexcept
232 return !f.has_light || getLight(LIGHTBANK_DAY, f) == getLight(LIGHTBANK_NIGHT, f);
235 inline u8 getLight(LightBank bank, ContentLightingFlags f) const noexcept
237 u8 raw_light = getLightRaw(bank, f);
238 return MYMAX(f.light_source, raw_light);
242 * Returns the node's light level from param1.
243 * If the node emits light, it is ignored.
244 * \param f the ContentLightingFlags of this node.
246 inline u8 getLightRaw(LightBank bank, ContentLightingFlags f) const noexcept
249 return bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
253 // 0 <= daylight_factor <= 1000
254 // 0 <= return value <= LIGHT_SUN
255 u8 getLightBlend(u32 daylight_factor, ContentLightingFlags f) const
257 u8 lightday = getLight(LIGHTBANK_DAY, f);
258 u8 lightnight = getLight(LIGHTBANK_NIGHT, f);
259 return blend_light(daylight_factor, lightday, lightnight);
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;
266 /// @returns Rotation in range 0–239 (in 1.5° steps)
267 u8 getDegRotate(const NodeDefManager *nodemgr) const;
269 void rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot);
272 * Checks which neighbors does this node connect to.
274 * \param p coordinates of the node
276 u8 getNeighbors(v3s16 p, Map *map) const;
279 Gets list of node boxes (used for rendering (NDT_NODEBOX))
281 void getNodeBoxes(const NodeDefManager *nodemgr, std::vector<aabb3f> *boxes,
282 u8 neighbors = 0) const;
285 Gets list of selection boxes
287 void getSelectionBoxes(const NodeDefManager *nodemg,
288 std::vector<aabb3f> *boxes, u8 neighbors = 0) const;
291 Gets list of collision boxes
293 void getCollisionBoxes(const NodeDefManager *nodemgr,
294 std::vector<aabb3f> *boxes, u8 neighbors = 0) const;
297 Liquid/leveled helpers
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);
305 Serialization functions
308 static u32 serializedLength(u8 version);
309 void serialize(u8 *dest, u8 version) const;
310 void deSerialize(u8 *source, u8 version);
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
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 SharedBuffer<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);
327 // Deprecated serialization methods
328 void deSerialize_pre22(const u8 *source, u8 version);