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 #define MATERIALS_COUNT 256
36 Anything that stores MapNodes doesn't have to preserve parameters
37 associated with this material.
39 Doesn't create faces with anything and is considered being
40 out-of-map in the game map.
42 #define CONTENT_IGNORE 255
43 #define CONTENT_IGNORE_DEFAULT_PARAM 0
46 The common material through which the player can walk and which
47 is transparent to light
49 #define CONTENT_AIR 254
57 - Material = irrlicht's Material class
58 - Content = (u8) content of a node
59 - Tile = (u16) Material ID at some side of a node
62 #define CONTENT_STONE 0
63 #define CONTENT_GRASS 1
64 #define CONTENT_WATER 2
65 #define CONTENT_TORCH 3
66 #define CONTENT_TREE 4
67 #define CONTENT_LEAVES 5
68 #define CONTENT_GRASS_FOOTSTEPS 6
69 #define CONTENT_MESE 7
71 #define CONTENT_WATERSOURCE 9
72 #define CONTENT_CLOUD 10
73 #define CONTENT_COALSTONE 11
74 #define CONTENT_WOOD 12
76 #define USEFUL_CONTENT_COUNT 13
78 extern u16 g_content_tiles[USEFUL_CONTENT_COUNT][6];
79 extern const char * g_content_inventory_texture_paths[USEFUL_CONTENT_COUNT];
80 // Initializes g_content_inventory_texture_paths
81 void init_content_inventory_texture_paths();
84 If true, the material allows light propagation and brightness is stored
87 inline bool light_propagates_content(u8 m)
89 return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
93 If true, the material allows lossless sunlight propagation.
94 NOTE: It doesn't seem to go through torches regardlessly of this
96 inline bool sunlight_propagates_content(u8 m)
98 return (m == CONTENT_AIR || m == CONTENT_TORCH);
102 On a node-node surface, the material of the node with higher solidness
108 inline u8 content_solidness(u8 m)
110 // As of now, every pseudo node like torches are added to this
111 if(m == CONTENT_AIR || m == CONTENT_TORCH)
113 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
118 // Objects collide with walkable contents
119 inline bool content_walkable(u8 m)
121 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
124 // A liquid resists fast movement
125 inline bool content_liquid(u8 m)
127 return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
130 // Pointable contents can be pointed to in the map
131 inline bool content_pointable(u8 m)
133 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
136 inline bool content_diggable(u8 m)
138 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
141 inline bool content_buildable_to(u8 m)
143 return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
147 Returns true for contents that form the base ground that
148 follows the main heightmap
150 inline bool is_ground_content(u8 m)
155 && m != CONTENT_WATER
156 && m != CONTENT_TORCH
158 && m != CONTENT_LEAVES
159 && m != CONTENT_WATERSOURCE
160 && m != CONTENT_CLOUD
164 inline bool is_mineral(u8 c)
166 return(c == CONTENT_MESE
167 || c == CONTENT_COALSTONE);
170 inline bool liquid_replaces_content(u8 c)
172 return (c == CONTENT_AIR || c == CONTENT_TORCH);
176 When placing a node, drection info is added to it if this is true
178 inline bool content_directional(u8 c)
180 return (c == CONTENT_TORCH);
184 Nodes make a face if contents differ and solidness differs.
187 1: Face uses m1's content
188 2: Face uses m2's content
190 inline u8 face_contents(u8 m1, u8 m2)
192 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
195 bool contents_differ = (m1 != m2);
196 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
197 bool makes_face = contents_differ && solidness_differs;
199 if(makes_face == false)
202 if(content_solidness(m1) > content_solidness(m2))
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];
289 #define DIR_PX 1 //X+
290 #define DIR_NX 2 //X-
291 #define DIR_PZ 4 //Z+
292 #define DIR_NZ 8 //Z-
293 #define DIR_PY 16 //Y+
294 #define DIR_NY 32 //Y-
296 inline void decode_dirs(u8 b, core::list<v3s16> &dirs)
299 dirs.push_back(v3s16(1,0,0));
301 dirs.push_back(v3s16(-1,0,0));
303 dirs.push_back(v3s16(0,0,1));
305 dirs.push_back(v3s16(0,0,-1));
307 dirs.push_back(v3s16(0,1,0));
309 dirs.push_back(v3s16(0,-1,0));
312 inline u8 encode_dirs(core::list<v3s16> &dirs)
315 for(core::list<v3s16>::Iterator
317 i != dirs.end(); i++)
319 if(*i == v3s16(1,0,0))
321 else if(*i == v3s16(-1,0,0))
323 else if(*i == v3s16(0,0,1))
325 else if(*i == v3s16(0,0,-1))
327 else if(*i == v3s16(0,1,0))
329 else if(*i == v3s16(0,-1,0))
342 Misc parameter. Initialized to 0.
343 - For light_propagates() blocks, this is light intensity,
344 stored logarithmically from 0 to LIGHT_MAX.
345 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
346 - Contains 2 values, day- and night lighting. Each takes 4 bits.
358 Direction for torches and other stuff.
359 Format is freeform. e.g. packDir or encode_dirs can be used.
364 MapNode(const MapNode & n)
369 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_pressure=0)
373 pressure = a_pressure;
376 bool operator==(const MapNode &other)
379 && param == other.param
380 && pressure == other.pressure);
383 bool light_propagates()
385 return light_propagates_content(d);
388 bool sunlight_propagates()
390 return sunlight_propagates_content(d);
395 return content_solidness(d);
401 Note that a block that isn't light_propagates() can be a light source.
403 if(d == CONTENT_TORCH)
409 u8 getLightBanksWithSource()
411 // Select the brightest of [light source, propagated light]
414 if(light_propagates())
416 lightday = param & 0x0f;
417 lightnight = (param>>4)&0x0f;
419 if(light_source() > lightday)
420 lightday = light_source();
421 if(light_source() > lightnight)
422 lightnight = light_source();
423 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
426 void setLightBanks(u8 a_light)
431 u8 getLight(enum LightBank bank)
433 // Select the brightest of [light source, propagated light]
435 if(light_propagates())
437 if(bank == LIGHTBANK_DAY)
438 light = param & 0x0f;
439 else if(bank == LIGHTBANK_NIGHT)
440 light = (param>>4)&0x0f;
444 if(light_source() > light)
445 light = light_source();
449 // 0 <= daylight_factor <= 1000
450 // 0 <= return value <= LIGHT_SUN
451 u8 getLightBlend(u32 daylight_factor)
453 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
454 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
457 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
463 /*// 0 <= daylight_factor <= 1000
464 // 0 <= return value <= 255
465 u8 getLightBlend(u32 daylight_factor)
467 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
468 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
469 u8 mix = ((daylight_factor * daylight
470 + (1000-daylight_factor) * nightlight)
475 void setLight(enum LightBank bank, u8 a_light)
477 // If not transparent, can't set light
478 if(light_propagates() == false)
480 if(bank == LIGHTBANK_DAY)
483 param |= a_light & 0x0f;
485 else if(bank == LIGHTBANK_NIGHT)
488 param |= (a_light & 0x0f)<<4;
494 u16 getTile(v3s16 dir)
496 return content_tile(d, dir);
500 These serialization functions are used when informing client
504 static u32 serializedLength(u8 version)
506 if(!ser_ver_supported(version))
507 throw VersionMismatchException("ERROR: MapNode format not supported");
511 else if(version <= 9)
516 void serialize(u8 *dest, u8 version)
518 if(!ser_ver_supported(version))
519 throw VersionMismatchException("ERROR: MapNode format not supported");
525 else if(version <= 9)
537 void deSerialize(u8 *source, u8 version)
539 if(!ser_ver_supported(version))
540 throw VersionMismatchException("ERROR: MapNode format not supported");
546 else if(version == 1)
549 // This version doesn't support saved lighting
550 if(light_propagates() || light_source() > 0)
555 else if(version <= 9)
564 pressure = source[2];
570 Returns integer position of the node in given
571 floating point position.
573 inline v3s16 floatToInt(v3f p)
576 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
577 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
578 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
582 inline v3f intToFloat(v3s16 p)