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 CONTENT_IGNORE 255
45 #define CONTENT_IGNORE_DEFAULT_PARAM 0
48 The common material through which the player can walk and which
49 is transparent to light
51 #define CONTENT_AIR 254
56 - Dynamics of gravel: if there is a drop of more than two
57 blocks on any side, it will drop in there. Is this doable?
60 - Material = irrlicht's Material class
61 - Content = (u8) content of a node
62 - Tile = (u16) Material ID at some side of a node
79 CONTENT_GRASS_FOOTSTEPS=6,
87 // This is set to the number of the actual values in this enum
92 If true, the material allows light propagation and brightness is stored
95 inline bool light_propagates_content(u8 m)
97 return (m == CONTENT_AIR || m == CONTENT_LIGHT || m == CONTENT_WATER || m == CONTENT_OCEAN);
101 If true, the material allows lossless sunlight propagation.
103 inline bool sunlight_propagates_content(u8 m)
105 return (m == CONTENT_AIR || m == CONTENT_LIGHT);
109 On a node-node surface, the material of the node with higher solidness
115 inline u8 content_solidness(u8 m)
119 if(m == CONTENT_WATER || m == CONTENT_OCEAN)
124 // Objects collide with walkable contents
125 inline bool content_walkable(u8 m)
127 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_OCEAN && m != CONTENT_LIGHT);
130 // A liquid resists fast movement
131 inline bool content_liquid(u8 m)
133 return (m == CONTENT_WATER || m == CONTENT_OCEAN);
136 // Pointable contents can be pointed to in the map
137 inline bool content_pointable(u8 m)
139 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_OCEAN);
142 inline bool content_diggable(u8 m)
144 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_OCEAN);
147 inline bool content_buildable_to(u8 m)
149 return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_OCEAN);
153 TODO: Make a mapper class for mapping every side of a content
155 This dumbily maps all sides of content to the tile of the same id.
157 inline u8 content_tile(u8 c)
159 if(c == CONTENT_IGNORE || c == CONTENT_LIGHT)
165 Returns true for contents that form the base ground that
166 follows the main heightmap
168 inline bool is_ground_content(u8 m)
171 m == CONTENT_STONE ||
172 m == CONTENT_GRASS ||
173 m == CONTENT_GRASS_FOOTSTEPS ||
180 Nodes make a face if contents differ and solidness differs.
183 1: Face uses m1's content
184 2: Face uses m2's content
186 inline u8 face_contents(u8 m1, u8 m2)
188 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
191 bool contents_differ = (m1 != m2);
192 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
193 bool makes_face = contents_differ && solidness_differs;
195 if(makes_face == false)
198 if(content_solidness(m1) > content_solidness(m2))
204 inline bool liquid_replaces_content(u8 c)
206 return (c == CONTENT_AIR || c == CONTENT_LIGHT);
210 When placing a node, drection info is added to it if this is true
212 inline bool content_directional(u8 c)
214 return (c == CONTENT_LIGHT);
218 Packs directions like (1,0,0), (1,-1,0)
220 inline u8 packDir(v3s16 dir)
241 inline v3s16 unpackDir(u8 b)
269 Misc parameter. Initialized to 0.
270 - For light_propagates() blocks, this is light intensity,
271 stored logarithmically from 0 to LIGHT_MAX.
272 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
284 Direction for torches and other stuff.
285 If possible, packed with packDir.
290 MapNode(const MapNode & n)
295 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_pressure=0)
299 pressure = a_pressure;
302 bool operator==(const MapNode &other)
305 && param == other.param
306 && pressure == other.pressure);
309 bool light_propagates()
311 return light_propagates_content(d);
314 bool sunlight_propagates()
316 return sunlight_propagates_content(d);
321 return content_solidness(d);
327 Note that a block that isn't light_propagates() can be a light source.
329 if(d == CONTENT_LIGHT)
337 // Select the brightest of [light source, propagated light]
339 if(light_propagates())
340 light = param & 0x0f;
341 if(light_source() > light)
342 light = light_source();
346 void setLight(u8 a_light)
348 // If not transparent, can't set light
349 if(light_propagates() == false)
355 These serialization functions are used when informing client
359 static u32 serializedLength(u8 version)
361 if(!ser_ver_supported(version))
362 throw VersionMismatchException("ERROR: MapNode format not supported");
366 else if(version <= 9)
371 void serialize(u8 *dest, u8 version)
373 if(!ser_ver_supported(version))
374 throw VersionMismatchException("ERROR: MapNode format not supported");
380 else if(version <= 9)
392 void deSerialize(u8 *source, u8 version)
394 if(!ser_ver_supported(version))
395 throw VersionMismatchException("ERROR: MapNode format not supported");
401 else if(version == 1)
404 // This version doesn't support saved lighting
405 if(light_propagates() || light_source() > 0)
410 else if(version <= 9)
419 pressure = source[2];
425 Returns integer position of the node in given
426 floating point position.
428 inline v3s16 floatToInt(v3f p)
431 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
432 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
433 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
437 inline v3f intToFloat(v3s16 p)