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,
82 // This is set to the number of the actual values in this enum
86 extern u16 g_content_tiles[USEFUL_CONTENT_COUNT][6];
87 extern const char * g_content_inventory_textures[USEFUL_CONTENT_COUNT];
90 If true, the material allows light propagation and brightness is stored
93 inline bool light_propagates_content(u8 m)
95 return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_OCEAN);
99 If true, the material allows lossless sunlight propagation.
100 NOTE: It doesn't seem to go through torches regardlessly of this
102 inline bool sunlight_propagates_content(u8 m)
104 return (m == CONTENT_AIR || m == CONTENT_TORCH);
108 On a node-node surface, the material of the node with higher solidness
114 inline u8 content_solidness(u8 m)
116 // As of now, every pseudo node like torches are added to this
117 if(m == CONTENT_AIR || m == CONTENT_TORCH)
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_TORCH);
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 Returns true for contents that form the base ground that
154 follows the main heightmap
156 inline bool is_ground_content(u8 m)
161 && m != CONTENT_WATER
162 && m != CONTENT_TORCH
164 && m != CONTENT_LEAVES
165 && m != CONTENT_OCEAN
166 && m != CONTENT_CLOUD
170 inline bool is_mineral(u8 c)
172 return(c == CONTENT_MESE
173 || c == CONTENT_COALSTONE);
176 inline bool liquid_replaces_content(u8 c)
178 return (c == CONTENT_AIR || c == CONTENT_TORCH);
182 When placing a node, drection info is added to it if this is true
184 inline bool content_directional(u8 c)
186 return (c == CONTENT_TORCH);
190 Nodes make a face if contents differ and solidness differs.
193 1: Face uses m1's content
194 2: Face uses m2's content
196 inline u8 face_contents(u8 m1, u8 m2)
198 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
201 bool contents_differ = (m1 != m2);
202 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
203 bool makes_face = contents_differ && solidness_differs;
205 if(makes_face == false)
208 if(content_solidness(m1) > content_solidness(m2))
215 Packs directions like (1,0,0), (1,-1,0)
217 inline u8 packDir(v3s16 dir)
238 inline v3s16 unpackDir(u8 b)
260 inline u16 content_tile(u8 c, v3s16 dir)
262 if(c == CONTENT_IGNORE || c == CONTENT_AIR
263 || c >= USEFUL_CONTENT_COUNT)
268 if(dir == v3s16(0,1,0))
270 else if(dir == v3s16(0,-1,0))
272 else if(dir == v3s16(1,0,0))
274 else if(dir == v3s16(-1,0,0))
276 else if(dir == v3s16(0,0,1))
278 else if(dir == v3s16(0,0,-1))
285 return g_content_tiles[c][dir_i];
300 Misc parameter. Initialized to 0.
301 - For light_propagates() blocks, this is light intensity,
302 stored logarithmically from 0 to LIGHT_MAX.
303 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
315 Direction for torches and other stuff.
316 If possible, packed with packDir.
321 MapNode(const MapNode & n)
326 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_pressure=0)
330 pressure = a_pressure;
333 bool operator==(const MapNode &other)
336 && param == other.param
337 && pressure == other.pressure);
340 bool light_propagates()
342 return light_propagates_content(d);
345 bool sunlight_propagates()
347 return sunlight_propagates_content(d);
352 return content_solidness(d);
358 Note that a block that isn't light_propagates() can be a light source.
360 if(d == CONTENT_TORCH)
366 u8 getLightBanksWithSource()
368 // Select the brightest of [light source, propagated light]
371 if(light_propagates())
373 lightday = param & 0x0f;
374 lightnight = (param>>4)&0x0f;
376 if(light_source() > lightday)
377 lightday = light_source();
378 if(light_source() > lightnight)
379 lightnight = light_source();
380 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
383 void setLightBanks(u8 a_light)
388 u8 getLight(enum LightBank bank)
390 // Select the brightest of [light source, propagated light]
392 if(light_propagates())
394 if(bank == LIGHTBANK_DAY)
395 light = param & 0x0f;
396 else if(bank == LIGHTBANK_NIGHT)
397 light = (param>>4)&0x0f;
401 if(light_source() > light)
402 light = light_source();
406 // 0 <= daylight_factor <= 1000
407 // 0 <= return value <= LIGHT_SUN
408 u8 getLightBlend(u32 daylight_factor)
410 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
411 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
414 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
420 /*// 0 <= daylight_factor <= 1000
421 // 0 <= return value <= 255
422 u8 getLightBlend(u32 daylight_factor)
424 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
425 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
426 u8 mix = ((daylight_factor * daylight
427 + (1000-daylight_factor) * nightlight)
432 void setLight(enum LightBank bank, u8 a_light)
434 // If not transparent, can't set light
435 if(light_propagates() == false)
437 if(bank == LIGHTBANK_DAY)
440 param |= a_light & 0x0f;
442 else if(bank == LIGHTBANK_NIGHT)
445 param |= (a_light & 0x0f)<<4;
451 u16 getTile(v3s16 dir)
453 return content_tile(d, dir);
457 These serialization functions are used when informing client
461 static u32 serializedLength(u8 version)
463 if(!ser_ver_supported(version))
464 throw VersionMismatchException("ERROR: MapNode format not supported");
468 else if(version <= 9)
473 void serialize(u8 *dest, u8 version)
475 if(!ser_ver_supported(version))
476 throw VersionMismatchException("ERROR: MapNode format not supported");
482 else if(version <= 9)
494 void deSerialize(u8 *source, u8 version)
496 if(!ser_ver_supported(version))
497 throw VersionMismatchException("ERROR: MapNode format not supported");
503 else if(version == 1)
506 // This version doesn't support saved lighting
507 if(light_propagates() || light_source() > 0)
512 else if(version <= 9)
521 pressure = source[2];
527 Returns integer position of the node in given
528 floating point position.
530 inline v3s16 floatToInt(v3f p)
533 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
534 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
535 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
539 inline v3f intToFloat(v3s16 p)