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 || m == CONTENT_WATER)
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 inline bool content_liquid(u8 m)
126 return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
129 inline bool content_flowing_liquid(u8 m)
131 return (m == CONTENT_WATER);
134 inline bool content_liquid_source(u8 m)
136 return (m == CONTENT_WATERSOURCE);
139 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
140 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
141 inline u8 make_liquid_flowing(u8 m)
143 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
144 return CONTENT_WATER;
148 // Pointable contents can be pointed to in the map
149 inline bool content_pointable(u8 m)
151 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
154 inline bool content_diggable(u8 m)
156 return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
159 inline bool content_buildable_to(u8 m)
161 return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
165 Returns true for contents that form the base ground that
166 follows the main heightmap
168 inline bool is_ground_content(u8 m)
173 && m != CONTENT_WATER
174 && m != CONTENT_TORCH
176 && m != CONTENT_LEAVES
177 && m != CONTENT_WATERSOURCE
178 && m != CONTENT_CLOUD
182 inline bool is_mineral(u8 c)
184 return(c == CONTENT_MESE
185 || c == CONTENT_COALSTONE);
188 inline bool liquid_replaces_content(u8 c)
190 return (c == CONTENT_AIR || c == CONTENT_TORCH);
194 When placing a node, drection info is added to it if this is true
196 inline bool content_directional(u8 c)
198 return (c == CONTENT_TORCH);
202 Nodes make a face if contents differ and solidness differs.
205 1: Face uses m1's content
206 2: Face uses m2's content
208 inline u8 face_contents(u8 m1, u8 m2)
210 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
213 bool contents_differ = (m1 != m2);
215 // Contents don't differ for different forms of same liquid
216 if(content_liquid(m1) && content_liquid(m2)
217 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
218 contents_differ = false;
220 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
221 bool makes_face = contents_differ && solidness_differs;
223 if(makes_face == false)
226 if(content_solidness(m1) > content_solidness(m2))
233 Packs directions like (1,0,0), (1,-1,0)
235 inline u8 packDir(v3s16 dir)
256 inline v3s16 unpackDir(u8 b)
278 inline u16 content_tile(u8 c, v3s16 dir)
280 if(c == CONTENT_IGNORE || c == CONTENT_AIR
281 || c >= USEFUL_CONTENT_COUNT)
286 if(dir == v3s16(0,1,0))
288 else if(dir == v3s16(0,-1,0))
290 else if(dir == v3s16(1,0,0))
292 else if(dir == v3s16(-1,0,0))
294 else if(dir == v3s16(0,0,1))
296 else if(dir == v3s16(0,0,-1))
303 return g_content_tiles[c][dir_i];
313 #define DIR_PX 1 //X+
314 #define DIR_NX 2 //X-
315 #define DIR_PZ 4 //Z+
316 #define DIR_NZ 8 //Z-
317 #define DIR_PY 16 //Y+
318 #define DIR_NY 32 //Y-
320 inline void decode_dirs(u8 b, core::list<v3s16> &dirs)
323 dirs.push_back(v3s16(1,0,0));
325 dirs.push_back(v3s16(-1,0,0));
327 dirs.push_back(v3s16(0,0,1));
329 dirs.push_back(v3s16(0,0,-1));
331 dirs.push_back(v3s16(0,1,0));
333 dirs.push_back(v3s16(0,-1,0));
336 inline u8 encode_dirs(core::list<v3s16> &dirs)
339 for(core::list<v3s16>::Iterator
341 i != dirs.end(); i++)
343 if(*i == v3s16(1,0,0))
345 else if(*i == v3s16(-1,0,0))
347 else if(*i == v3s16(0,0,1))
349 else if(*i == v3s16(0,0,-1))
351 else if(*i == v3s16(0,1,0))
353 else if(*i == v3s16(0,-1,0))
366 Misc parameter. Initialized to 0.
367 - For light_propagates() blocks, this is light intensity,
368 stored logarithmically from 0 to LIGHT_MAX.
369 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
370 - Contains 2 values, day- and night lighting. Each takes 4 bits.
379 Direction for torches and other stuff.
380 Format is freeform. e.g. packDir or encode_dirs can be used.
385 MapNode(const MapNode & n)
390 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
397 bool operator==(const MapNode &other)
400 && param == other.param
401 && param2 == other.param2);
404 bool light_propagates()
406 return light_propagates_content(d);
409 bool sunlight_propagates()
411 return sunlight_propagates_content(d);
416 return content_solidness(d);
422 Note that a block that isn't light_propagates() can be a light source.
424 if(d == CONTENT_TORCH)
430 u8 getLightBanksWithSource()
432 // Select the brightest of [light source, propagated light]
435 if(light_propagates())
437 lightday = param & 0x0f;
438 lightnight = (param>>4)&0x0f;
440 if(light_source() > lightday)
441 lightday = light_source();
442 if(light_source() > lightnight)
443 lightnight = light_source();
444 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
447 void setLightBanks(u8 a_light)
452 u8 getLight(enum LightBank bank)
454 // Select the brightest of [light source, propagated light]
456 if(light_propagates())
458 if(bank == LIGHTBANK_DAY)
459 light = param & 0x0f;
460 else if(bank == LIGHTBANK_NIGHT)
461 light = (param>>4)&0x0f;
465 if(light_source() > light)
466 light = light_source();
470 // 0 <= daylight_factor <= 1000
471 // 0 <= return value <= LIGHT_SUN
472 u8 getLightBlend(u32 daylight_factor)
474 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
475 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
478 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
484 /*// 0 <= daylight_factor <= 1000
485 // 0 <= return value <= 255
486 u8 getLightBlend(u32 daylight_factor)
488 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
489 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
490 u8 mix = ((daylight_factor * daylight
491 + (1000-daylight_factor) * nightlight)
496 void setLight(enum LightBank bank, u8 a_light)
498 // If not transparent, can't set light
499 if(light_propagates() == false)
501 if(bank == LIGHTBANK_DAY)
504 param |= a_light & 0x0f;
506 else if(bank == LIGHTBANK_NIGHT)
509 param |= (a_light & 0x0f)<<4;
515 u16 getTile(v3s16 dir)
517 return content_tile(d, dir);
521 These serialization functions are used when informing client
525 static u32 serializedLength(u8 version)
527 if(!ser_ver_supported(version))
528 throw VersionMismatchException("ERROR: MapNode format not supported");
532 else if(version <= 9)
537 void serialize(u8 *dest, u8 version)
539 if(!ser_ver_supported(version))
540 throw VersionMismatchException("ERROR: MapNode format not supported");
546 else if(version <= 9)
558 void deSerialize(u8 *source, u8 version)
560 if(!ser_ver_supported(version))
561 throw VersionMismatchException("ERROR: MapNode format not supported");
567 else if(version == 1)
570 // This version doesn't support saved lighting
571 if(light_propagates() || light_source() > 0)
576 else if(version <= 9)
591 Returns integer position of the node in given
592 floating point position.
594 inline v3s16 floatToInt(v3f p)
597 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
598 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
599 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
603 inline v3f intToFloat(v3s16 p)