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"
30 // Size of node in rendering units
33 #define MATERIALS_COUNT 256
38 Anything that stores MapNodes doesn't have to preserve parameters
39 associated with this material.
41 Doesn't create faces with anything and is considered being
42 out-of-map in the game map.
44 #define MATERIAL_IGNORE 255
45 #define MATERIAL_IGNORE_DEFAULT_PARAM 0
48 The common material through which the player can walk and which
49 is transparent to light
51 #define MATERIAL_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?
60 TODO: These should be named to "content" or something like that
77 MATERIAL_GRASS_FOOTSTEPS,
85 // This is set to the number of the actual values in this enum
90 If true, the material allows light propagation and brightness is stored
93 inline bool light_propagates_material(u8 m)
95 return (m == MATERIAL_AIR || m == MATERIAL_LIGHT || m == MATERIAL_WATER || m == MATERIAL_OCEAN);
99 If true, the material allows lossless sunlight propagation.
101 inline bool sunlight_propagates_material(u8 m)
103 return (m == MATERIAL_AIR);
107 On a node-node surface, the material of the node with higher solidness
113 inline u8 material_solidness(u8 m)
115 if(m == MATERIAL_AIR)
117 if(m == MATERIAL_WATER || m == MATERIAL_OCEAN)
122 // Objects collide with walkable materials
123 inline bool material_walkable(u8 m)
125 return (m != MATERIAL_AIR && m != MATERIAL_WATER && m != MATERIAL_OCEAN && m != MATERIAL_LIGHT);
128 // A liquid resists fast movement
129 inline bool material_liquid(u8 m)
131 return (m == MATERIAL_WATER || m == MATERIAL_OCEAN);
134 // Pointable materials can be pointed to in the map
135 inline bool material_pointable(u8 m)
137 return (m != MATERIAL_AIR && m != MATERIAL_WATER && m != MATERIAL_OCEAN);
140 inline bool material_diggable(u8 m)
142 return (m != MATERIAL_AIR && m != MATERIAL_WATER && m != MATERIAL_OCEAN);
145 inline bool material_buildable_to(u8 m)
147 return (m == MATERIAL_AIR || m == MATERIAL_WATER || m == MATERIAL_OCEAN);
151 As of now, input is a "material" and the output is a "material"
153 inline u8 content_cube_material(u8 c)
155 if(c == MATERIAL_IGNORE || c == MATERIAL_LIGHT)
161 Returns true for materials that form the base ground that
162 follows the main heightmap
164 inline bool is_ground_material(u8 m)
167 m == MATERIAL_STONE ||
168 m == MATERIAL_GRASS ||
169 m == MATERIAL_GRASS_FOOTSTEPS ||
170 m == MATERIAL_MESE ||
176 Nodes make a face if materials differ and solidness differs.
179 1: Face uses m1's material
180 2: Face uses m2's material
182 inline u8 face_materials(u8 m1, u8 m2)
184 if(m1 == MATERIAL_IGNORE || m2 == MATERIAL_IGNORE)
187 bool materials_differ = (m1 != m2);
188 bool solidness_differs = (material_solidness(m1) != material_solidness(m2));
189 bool makes_face = materials_differ && solidness_differs;
191 if(makes_face == false)
194 if(material_solidness(m1) > material_solidness(m2))
202 //TODO: block type to differ from material
203 // (e.g. grass edges or something)
208 Misc parameter. Initialized to 0.
209 - For light_propagates() blocks, this is light intensity,
210 stored logarithmically from 0 to LIGHT_MAX.
211 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
217 MapNode(const MapNode & n)
222 MapNode(u8 data=MATERIAL_AIR, u8 a_param=0, u8 a_pressure=0)
226 pressure = a_pressure;
229 bool operator==(const MapNode &other)
232 && param == other.param
233 && pressure == other.pressure);
236 bool light_propagates()
238 return light_propagates_material(d);
241 bool sunlight_propagates()
243 return sunlight_propagates_material(d);
248 return material_solidness(d);
254 Note that a block that isn't light_propagates() can be a light source.
256 if(d == MATERIAL_LIGHT)
264 // Select the brightest of [light_source, transparent_light]
266 if(light_propagates())
267 light = param & 0x0f;
268 if(light_source() > light)
269 light = light_source();
273 void setLight(u8 a_light)
275 // If not transparent, can't set light
276 if(light_propagates() == false)
282 These serialization functions are used when informing client
286 static u32 serializedLength(u8 version)
288 if(!ser_ver_supported(version))
289 throw VersionMismatchException("ERROR: MapNode format not supported");
293 else if(version <= 9)
298 void serialize(u8 *dest, u8 version)
300 if(!ser_ver_supported(version))
301 throw VersionMismatchException("ERROR: MapNode format not supported");
307 else if(version <= 9)
319 void deSerialize(u8 *source, u8 version)
321 if(!ser_ver_supported(version))
322 throw VersionMismatchException("ERROR: MapNode format not supported");
328 else if(version == 1)
331 // This version doesn't support saved lighting
332 if(light_propagates() || light_source() > 0)
337 else if(version <= 9)
346 pressure = source[2];
352 Returns integer position of the node in given
353 floating point position.
355 inline v3s16 floatToInt(v3f p)
358 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
359 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
360 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
364 inline v3f intToFloat(v3s16 p)