]> git.lizzy.rs Git - minetest.git/blob - src/mapnode.h
8x block meshes (#13133)
[minetest.git] / src / mapnode.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #pragma once
21
22 #include "irrlichttypes_bloated.h"
23 #include "light.h"
24 #include "util/pointer.h"
25 #include <string>
26 #include <vector>
27
28 class NodeDefManager;
29 class 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 */
37 typedef 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 */
78 struct ContentLightingFlags {
79         u8 light_source : 4;
80         bool has_light : 1;
81         bool light_propagates : 1;
82         bool sunlight_propagates : 1;
83
84         bool operator==(const ContentLightingFlags &other) const
85         {
86                 return has_light == other.has_light && light_propagates == other.light_propagates &&
87                                 sunlight_propagates == other.sunlight_propagates &&
88                                 light_source == other.light_source;
89         }
90         bool operator!=(const ContentLightingFlags &other) const { return !(*this == other); }
91 };
92 static_assert(sizeof(ContentLightingFlags) == 1, "Unexpected ContentLightingFlags size");
93
94 enum LightBank
95 {
96         LIGHTBANK_DAY,
97         LIGHTBANK_NIGHT
98 };
99
100 /*
101         Simple rotation enum.
102 */
103 enum Rotation {
104         ROTATE_0,
105         ROTATE_90,
106         ROTATE_180,
107         ROTATE_270,
108         ROTATE_RAND,
109 };
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
131 struct ContentFeatures;
132
133 /*
134         This is the stuff what the whole world consists of.
135 */
136
137
138 struct alignas(u32) MapNode
139 {
140         /*
141                 Main content
142         */
143         u16 param0;
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         */
153         u8 param1;
154
155         /*
156                 The second parameter. Initialized to 0.
157                 E.g. direction for torches and flowing water.
158         */
159         u8 param2;
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
202         /*!
203          * Returns the color of the node.
204          *
205          * \param f content features of this node
206          * \param color output, contains the node's color.
207          */
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
225         /**
226          * Check if the light value for night differs from the light value for day.
227          *
228          * @return If the light values are equal, returns true; otherwise false
229          */
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
241         /*!
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.
245          */
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
266         /// @returns Rotation in range 0–239 (in 1.5° steps)
267         u8 getDegRotate(const NodeDefManager *nodemgr) const;
268
269         void rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot);
270
271         /*!
272          * Checks which neighbors does this node connect to.
273          *
274          * \param p coordinates of the node
275          */
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 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);
325
326 private:
327         // Deprecated serialization methods
328         void deSerialize_pre22(const u8 *source, u8 version);
329 };