3 Copyright (C) 2010 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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.
20 #ifndef MAPNODE_HEADER
21 #define MAPNODE_HEADER
24 #include "common_irrlicht.h"
27 #include "exceptions.h"
28 #include "serialization.h"
31 // Size of node in rendering units
34 #define MATERIALS_COUNT 256
39 Anything that stores MapNodes doesn't have to preserve parameters
40 associated with this material.
42 Doesn't create faces with anything and is considered being
43 out-of-map in the game map.
45 #define CONTENT_IGNORE 255
46 #define CONTENT_IGNORE_DEFAULT_PARAM 0
49 The common material through which the player can walk and which
50 is transparent to light
52 #define CONTENT_AIR 254
57 - Dynamics of gravel: if there is a drop of more than two
58 blocks on any side, it will drop in there. Is this doable?
61 - Material = irrlicht's Material class
62 - Content = (u8) content of a node
63 - Tile = (u16) Material ID at some side of a node
74 CONTENT_GRASS_FOOTSTEPS,
79 // This is set to the number of the actual values in this enum
83 extern u16 g_content_tiles[USEFUL_CONTENT_COUNT][6];
84 extern const char * g_content_inventory_textures[USEFUL_CONTENT_COUNT];
87 If true, the material allows light propagation and brightness is stored
90 inline bool light_propagates_content(u8 m)
92 return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_OCEAN);
96 If true, the material allows lossless sunlight propagation.
98 inline bool sunlight_propagates_content(u8 m)
100 return (m == CONTENT_AIR || m == CONTENT_TORCH);
104 On a node-node surface, the material of the node with higher solidness
110 inline u8 content_solidness(u8 m)
112 // As of now, every pseudo node like torches are added to this
113 if(m == CONTENT_AIR || m == CONTENT_TORCH)
115 if(m == CONTENT_WATER || m == CONTENT_OCEAN)
120 // Objects collide with walkable contents
121 inline bool content_walkable(u8 m)
123 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_OCEAN && m != CONTENT_TORCH);
126 // A liquid resists fast movement
127 inline bool content_liquid(u8 m)
129 return (m == CONTENT_WATER || m == CONTENT_OCEAN);
132 // Pointable contents can be pointed to in the map
133 inline bool content_pointable(u8 m)
135 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_OCEAN);
138 inline bool content_diggable(u8 m)
140 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_OCEAN);
143 inline bool content_buildable_to(u8 m)
145 return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_OCEAN);
149 Returns true for contents that form the base ground that
150 follows the main heightmap
152 inline bool is_ground_content(u8 m)
155 m == CONTENT_STONE ||
156 m == CONTENT_GRASS ||
157 m == CONTENT_GRASS_FOOTSTEPS ||
163 /*inline bool content_has_faces(u8 c)
165 return (m != CONTENT_IGNORE
167 && m != CONTENT_TORCH);
171 Nodes make a face if contents differ and solidness differs.
174 1: Face uses m1's content
175 2: Face uses m2's content
177 inline u8 face_contents(u8 m1, u8 m2)
179 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
182 bool contents_differ = (m1 != m2);
183 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
184 bool makes_face = contents_differ && solidness_differs;
186 if(makes_face == false)
189 if(content_solidness(m1) > content_solidness(m2))
195 inline bool liquid_replaces_content(u8 c)
197 return (c == CONTENT_AIR || c == CONTENT_TORCH);
201 When placing a node, drection info is added to it if this is true
203 inline bool content_directional(u8 c)
205 return (c == CONTENT_TORCH);
209 Packs directions like (1,0,0), (1,-1,0)
211 inline u8 packDir(v3s16 dir)
232 inline v3s16 unpackDir(u8 b)
254 inline u16 content_tile(u8 c, v3s16 dir)
256 if(c == CONTENT_IGNORE || c == CONTENT_AIR
257 || c >= USEFUL_CONTENT_COUNT)
262 if(dir == v3s16(0,1,0))
264 else if(dir == v3s16(0,-1,0))
266 else if(dir == v3s16(1,0,0))
268 else if(dir == v3s16(-1,0,0))
270 else if(dir == v3s16(0,0,1))
272 else if(dir == v3s16(0,0,-1))
279 return g_content_tiles[c][dir_i];
288 Misc parameter. Initialized to 0.
289 - For light_propagates() blocks, this is light intensity,
290 stored logarithmically from 0 to LIGHT_MAX.
291 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
303 Direction for torches and other stuff.
304 If possible, packed with packDir.
309 MapNode(const MapNode & n)
314 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_pressure=0)
318 pressure = a_pressure;
321 bool operator==(const MapNode &other)
324 && param == other.param
325 && pressure == other.pressure);
328 bool light_propagates()
330 return light_propagates_content(d);
333 bool sunlight_propagates()
335 return sunlight_propagates_content(d);
340 return content_solidness(d);
346 Note that a block that isn't light_propagates() can be a light source.
348 if(d == CONTENT_TORCH)
356 // Select the brightest of [light source, propagated light]
358 if(light_propagates())
359 light = param & 0x0f;
360 if(light_source() > light)
361 light = light_source();
365 void setLight(u8 a_light)
367 // If not transparent, can't set light
368 if(light_propagates() == false)
373 u16 getTile(v3s16 dir)
375 return content_tile(d, dir);
379 These serialization functions are used when informing client
383 static u32 serializedLength(u8 version)
385 if(!ser_ver_supported(version))
386 throw VersionMismatchException("ERROR: MapNode format not supported");
390 else if(version <= 9)
395 void serialize(u8 *dest, u8 version)
397 if(!ser_ver_supported(version))
398 throw VersionMismatchException("ERROR: MapNode format not supported");
404 else if(version <= 9)
416 void deSerialize(u8 *source, u8 version)
418 if(!ser_ver_supported(version))
419 throw VersionMismatchException("ERROR: MapNode format not supported");
425 else if(version == 1)
428 // This version doesn't support saved lighting
429 if(light_propagates() || light_source() > 0)
434 else if(version <= 9)
443 pressure = source[2];
449 Returns integer position of the node in given
450 floating point position.
452 inline v3s16 floatToInt(v3f p)
455 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
456 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
457 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
461 inline v3f intToFloat(v3s16 p)