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 #include "iirrlichtwrapper.h"
33 Initializes all kind of stuff in here.
34 Many things depend on this.
36 irrlicht: Used for getting texture ids.
38 void init_mapnode(IIrrlichtWrapper *irrlicht);
40 // Initializes g_content_inventory_texture_paths
41 void init_content_inventory_texture_paths();
44 // NOTE: This is not used appropriately everywhere.
45 #define MATERIALS_COUNT 256
50 Anything that stores MapNodes doesn't have to preserve parameters
51 associated with this material.
53 Doesn't create faces with anything and is considered being
54 out-of-map in the game map.
56 #define CONTENT_IGNORE 255
57 #define CONTENT_IGNORE_DEFAULT_PARAM 0
60 The common material through which the player can walk and which
61 is transparent to light
63 #define CONTENT_AIR 254
71 - Material = irrlicht's Material class
72 - Content = (u8) content of a node
73 - Tile = (u16) Material ID at some side of a node
76 #define CONTENT_STONE 0
77 #define CONTENT_GRASS 1
78 #define CONTENT_WATER 2
79 #define CONTENT_TORCH 3
80 #define CONTENT_TREE 4
81 #define CONTENT_LEAVES 5
82 #define CONTENT_GRASS_FOOTSTEPS 6
83 #define CONTENT_MESE 7
85 #define CONTENT_WATERSOURCE 9
86 // Pretty much useless, clouds won't be drawn this way
87 #define CONTENT_CLOUD 10
88 #define CONTENT_COALSTONE 11
89 #define CONTENT_WOOD 12
90 #define CONTENT_SAND 13
93 This is used by all kinds of things to allocate memory for all
94 contents except CONTENT_AIR and CONTENT_IGNORE
96 #define USEFUL_CONTENT_COUNT 14
102 enum ContentParamType
118 struct ContentFeatures
120 // If non-NULL, content is translated to this when deserialized
121 MapNode *translate_to;
123 // Type of MapNode::param
124 ContentParamType param_type;
136 //std::string inventory_image_path;
137 TextureSpec inventory_texture;
139 bool is_ground_content; //TODO: Remove, use walkable instead
140 bool light_propagates;
141 bool sunlight_propagates;
142 u8 solidness; // Used when choosing which face is drawn
147 enum LiquidType liquid_type;
148 bool wall_mounted; // If true, param2 is set to direction when placed
150 //TODO: Move more properties here
155 param_type = CPT_NONE;
156 is_ground_content = false;
157 light_propagates = false;
158 sunlight_propagates = false;
163 buildable_to = false;
164 liquid_type = LIQUID_NONE;
165 wall_mounted = false;
170 void setAllTextures(const TextureSpec &spec, u8 alpha=255)
172 for(u16 i=0; i<6; i++)
174 tiles[i].spec = spec;
175 tiles[i].alpha = alpha;
178 // Set this too so it can be left as is most times
179 /*if(inventory_image_path == "")
180 inventory_image_path = porting::getDataPath(imgname.c_str());*/
182 if(inventory_texture.empty())
183 inventory_texture = spec;
185 void setTexture(u16 i, const TextureSpec &spec, u8 alpha=255)
187 tiles[i].spec = spec;
188 tiles[i].alpha = alpha;
191 void setInventoryTexture(const TextureSpec &spec)
193 inventory_texture = spec;
196 /*void setInventoryImage(std::string imgname)
198 inventory_image_path = porting::getDataPath(imgname.c_str());
203 Call this to access the ContentFeature list
205 ContentFeatures & content_features(u8 i);
208 If true, the material allows light propagation and brightness is stored
210 NOTE: Don't use, use "content_features(m).whatever" instead
212 inline bool light_propagates_content(u8 m)
214 return content_features(m).light_propagates;
215 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
219 If true, the material allows lossless sunlight propagation.
220 NOTE: It doesn't seem to go through torches regardlessly of this
221 NOTE: Don't use, use "content_features(m).whatever" instead
223 inline bool sunlight_propagates_content(u8 m)
225 return content_features(m).sunlight_propagates;
226 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
230 On a node-node surface, the material of the node with higher solidness
235 NOTE: Don't use, use "content_features(m).whatever" instead
237 inline u8 content_solidness(u8 m)
239 return content_features(m).solidness;
240 /*// As of now, every pseudo node like torches are added to this
241 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
243 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
248 // Objects collide with walkable contents
249 // NOTE: Don't use, use "content_features(m).whatever" instead
250 inline bool content_walkable(u8 m)
252 return content_features(m).walkable;
253 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
256 // NOTE: Don't use, use "content_features(m).whatever" instead
257 inline bool content_liquid(u8 m)
259 return content_features(m).liquid_type != LIQUID_NONE;
260 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
263 // NOTE: Don't use, use "content_features(m).whatever" instead
264 inline bool content_flowing_liquid(u8 m)
266 return content_features(m).liquid_type == LIQUID_FLOWING;
267 //return (m == CONTENT_WATER);
270 // NOTE: Don't use, use "content_features(m).whatever" instead
271 inline bool content_liquid_source(u8 m)
273 return content_features(m).liquid_type == LIQUID_SOURCE;
274 //return (m == CONTENT_WATERSOURCE);
277 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
278 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
279 inline u8 make_liquid_flowing(u8 m)
281 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
282 return CONTENT_WATER;
286 // Pointable contents can be pointed to in the map
287 // NOTE: Don't use, use "content_features(m).whatever" instead
288 inline bool content_pointable(u8 m)
290 return content_features(m).pointable;
291 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
294 // NOTE: Don't use, use "content_features(m).whatever" instead
295 inline bool content_diggable(u8 m)
297 return content_features(m).diggable;
298 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
301 // NOTE: Don't use, use "content_features(m).whatever" instead
302 inline bool content_buildable_to(u8 m)
304 return content_features(m).buildable_to;
305 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
309 Returns true for contents that form the base ground that
310 follows the main heightmap
312 /*inline bool is_ground_content(u8 m)
314 return content_features(m).is_ground_content;
318 Nodes make a face if contents differ and solidness differs.
321 1: Face uses m1's content
322 2: Face uses m2's content
324 inline u8 face_contents(u8 m1, u8 m2)
326 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
329 bool contents_differ = (m1 != m2);
331 // Contents don't differ for different forms of same liquid
332 if(content_liquid(m1) && content_liquid(m2)
333 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
334 contents_differ = false;
336 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
337 bool makes_face = contents_differ && solidness_differs;
339 if(makes_face == false)
342 if(content_solidness(m1) > content_solidness(m2))
349 Packs directions like (1,0,0), (1,-1,0)
351 inline u8 packDir(v3s16 dir)
372 inline v3s16 unpackDir(u8 b)
401 This is the stuff what the whole world consists of.
410 Misc parameter. Initialized to 0.
411 - For light_propagates() blocks, this is light intensity,
412 stored logarithmically from 0 to LIGHT_MAX.
413 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
414 - Contains 2 values, day- and night lighting. Each takes 4 bits.
423 Direction for torches and other stuff.
424 Format is freeform. e.g. packDir or encode_dirs can be used.
429 MapNode(const MapNode & n)
434 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
441 /*MapNode & operator=(const MapNode &other)
445 param2 = other.param2;
449 bool operator==(const MapNode &other)
452 && param == other.param
453 && param2 == other.param2);
456 bool light_propagates()
458 return light_propagates_content(d);
461 bool sunlight_propagates()
463 return sunlight_propagates_content(d);
468 return content_solidness(d);
474 Note that a block that isn't light_propagates() can be a light source.
476 if(d == CONTENT_TORCH)
482 u8 getLightBanksWithSource()
484 // Select the brightest of [light source, propagated light]
487 if(light_propagates())
489 lightday = param & 0x0f;
490 lightnight = (param>>4)&0x0f;
492 if(light_source() > lightday)
493 lightday = light_source();
494 if(light_source() > lightnight)
495 lightnight = light_source();
496 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
499 void setLightBanks(u8 a_light)
504 u8 getLight(enum LightBank bank)
506 // Select the brightest of [light source, propagated light]
508 if(light_propagates())
510 if(bank == LIGHTBANK_DAY)
511 light = param & 0x0f;
512 else if(bank == LIGHTBANK_NIGHT)
513 light = (param>>4)&0x0f;
517 if(light_source() > light)
518 light = light_source();
522 // 0 <= daylight_factor <= 1000
523 // 0 <= return value <= LIGHT_SUN
524 u8 getLightBlend(u32 daylight_factor)
526 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
527 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
530 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
536 /*// 0 <= daylight_factor <= 1000
537 // 0 <= return value <= 255
538 u8 getLightBlend(u32 daylight_factor)
540 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
541 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
542 u8 mix = ((daylight_factor * daylight
543 + (1000-daylight_factor) * nightlight)
548 void setLight(enum LightBank bank, u8 a_light)
550 // If not transparent, can't set light
551 if(light_propagates() == false)
553 if(bank == LIGHTBANK_DAY)
556 param |= a_light & 0x0f;
558 else if(bank == LIGHTBANK_NIGHT)
561 param |= (a_light & 0x0f)<<4;
568 TileSpec getTile(v3s16 dir);
573 These serialization functions are used when informing client
577 static u32 serializedLength(u8 version)
579 if(!ser_ver_supported(version))
580 throw VersionMismatchException("ERROR: MapNode format not supported");
584 else if(version <= 9)
589 void serialize(u8 *dest, u8 version)
591 if(!ser_ver_supported(version))
592 throw VersionMismatchException("ERROR: MapNode format not supported");
598 else if(version <= 9)
610 void deSerialize(u8 *source, u8 version)
612 if(!ser_ver_supported(version))
613 throw VersionMismatchException("ERROR: MapNode format not supported");
619 else if(version == 1)
622 // This version doesn't support saved lighting
623 if(light_propagates() || light_source() > 0)
628 else if(version <= 9)
640 // Translate deprecated stuff
641 MapNode *translate_to = content_features(d).translate_to;
644 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
645 <<translate_to->d<<std::endl;
646 *this = *translate_to;
652 Returns integer position of the node in given
653 floating point position.
655 inline v3s16 floatToInt(v3f p)
658 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
659 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
660 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
665 The same thing backwards
667 inline v3f intToFloat(v3s16 p)