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);
214 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
215 bool makes_face = contents_differ && solidness_differs;
217 if(makes_face == false)
220 if(content_solidness(m1) > content_solidness(m2))
227 Packs directions like (1,0,0), (1,-1,0)
229 inline u8 packDir(v3s16 dir)
250 inline v3s16 unpackDir(u8 b)
272 inline u16 content_tile(u8 c, v3s16 dir)
274 if(c == CONTENT_IGNORE || c == CONTENT_AIR
275 || c >= USEFUL_CONTENT_COUNT)
280 if(dir == v3s16(0,1,0))
282 else if(dir == v3s16(0,-1,0))
284 else if(dir == v3s16(1,0,0))
286 else if(dir == v3s16(-1,0,0))
288 else if(dir == v3s16(0,0,1))
290 else if(dir == v3s16(0,0,-1))
297 return g_content_tiles[c][dir_i];
307 #define DIR_PX 1 //X+
308 #define DIR_NX 2 //X-
309 #define DIR_PZ 4 //Z+
310 #define DIR_NZ 8 //Z-
311 #define DIR_PY 16 //Y+
312 #define DIR_NY 32 //Y-
314 inline void decode_dirs(u8 b, core::list<v3s16> &dirs)
317 dirs.push_back(v3s16(1,0,0));
319 dirs.push_back(v3s16(-1,0,0));
321 dirs.push_back(v3s16(0,0,1));
323 dirs.push_back(v3s16(0,0,-1));
325 dirs.push_back(v3s16(0,1,0));
327 dirs.push_back(v3s16(0,-1,0));
330 inline u8 encode_dirs(core::list<v3s16> &dirs)
333 for(core::list<v3s16>::Iterator
335 i != dirs.end(); i++)
337 if(*i == v3s16(1,0,0))
339 else if(*i == v3s16(-1,0,0))
341 else if(*i == v3s16(0,0,1))
343 else if(*i == v3s16(0,0,-1))
345 else if(*i == v3s16(0,1,0))
347 else if(*i == v3s16(0,-1,0))
360 Misc parameter. Initialized to 0.
361 - For light_propagates() blocks, this is light intensity,
362 stored logarithmically from 0 to LIGHT_MAX.
363 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
364 - Contains 2 values, day- and night lighting. Each takes 4 bits.
378 Direction for torches and other stuff.
379 Format is freeform. e.g. packDir or encode_dirs can be used.
384 MapNode(const MapNode & n)
389 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_pressure=0)
393 pressure = a_pressure;
396 bool operator==(const MapNode &other)
399 && param == other.param
400 && pressure == other.pressure);
403 bool light_propagates()
405 return light_propagates_content(d);
408 bool sunlight_propagates()
410 return sunlight_propagates_content(d);
415 return content_solidness(d);
421 Note that a block that isn't light_propagates() can be a light source.
423 if(d == CONTENT_TORCH)
429 u8 getLightBanksWithSource()
431 // Select the brightest of [light source, propagated light]
434 if(light_propagates())
436 lightday = param & 0x0f;
437 lightnight = (param>>4)&0x0f;
439 if(light_source() > lightday)
440 lightday = light_source();
441 if(light_source() > lightnight)
442 lightnight = light_source();
443 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
446 void setLightBanks(u8 a_light)
451 u8 getLight(enum LightBank bank)
453 // Select the brightest of [light source, propagated light]
455 if(light_propagates())
457 if(bank == LIGHTBANK_DAY)
458 light = param & 0x0f;
459 else if(bank == LIGHTBANK_NIGHT)
460 light = (param>>4)&0x0f;
464 if(light_source() > light)
465 light = light_source();
469 // 0 <= daylight_factor <= 1000
470 // 0 <= return value <= LIGHT_SUN
471 u8 getLightBlend(u32 daylight_factor)
473 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
474 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
477 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
483 /*// 0 <= daylight_factor <= 1000
484 // 0 <= return value <= 255
485 u8 getLightBlend(u32 daylight_factor)
487 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
488 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
489 u8 mix = ((daylight_factor * daylight
490 + (1000-daylight_factor) * nightlight)
495 void setLight(enum LightBank bank, u8 a_light)
497 // If not transparent, can't set light
498 if(light_propagates() == false)
500 if(bank == LIGHTBANK_DAY)
503 param |= a_light & 0x0f;
505 else if(bank == LIGHTBANK_NIGHT)
508 param |= (a_light & 0x0f)<<4;
514 u16 getTile(v3s16 dir)
516 return content_tile(d, dir);
520 These serialization functions are used when informing client
524 static u32 serializedLength(u8 version)
526 if(!ser_ver_supported(version))
527 throw VersionMismatchException("ERROR: MapNode format not supported");
531 else if(version <= 9)
536 void serialize(u8 *dest, u8 version)
538 if(!ser_ver_supported(version))
539 throw VersionMismatchException("ERROR: MapNode format not supported");
545 else if(version <= 9)
557 void deSerialize(u8 *source, u8 version)
559 if(!ser_ver_supported(version))
560 throw VersionMismatchException("ERROR: MapNode format not supported");
566 else if(version == 1)
569 // This version doesn't support saved lighting
570 if(light_propagates() || light_source() > 0)
575 else if(version <= 9)
584 pressure = source[2];
590 Returns integer position of the node in given
591 floating point position.
593 inline v3s16 floatToInt(v3f p)
596 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
597 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
598 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
602 inline v3f intToFloat(v3s16 p)