3 Copyright (C) 2010-2011 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 // Quickhands for simple materials
171 void setTexture(u16 i, const TextureSpec &spec, u8 alpha=255)
173 tiles[i].spec = spec;
176 tiles[i].alpha = alpha;
177 tiles[i].material_type = MATERIAL_ALPHA_VERTEX;
180 void setAllTextures(const TextureSpec &spec, u8 alpha=255)
182 for(u16 i=0; i<6; i++)
184 setTexture(i, spec, alpha);
187 // Set this too so it can be left as is most times
188 if(inventory_texture.empty())
189 inventory_texture = spec;
192 void setTile(u16 i, const TileSpec &tile)
196 void setAllTiles(const TileSpec &tile)
198 for(u16 i=0; i<6; i++)
204 void setInventoryTexture(const TextureSpec &spec)
206 inventory_texture = spec;
209 /*void setInventoryImage(std::string imgname)
211 inventory_image_path = porting::getDataPath(imgname.c_str());
216 Call this to access the ContentFeature list
218 ContentFeatures & content_features(u8 i);
221 If true, the material allows light propagation and brightness is stored
223 NOTE: Don't use, use "content_features(m).whatever" instead
225 inline bool light_propagates_content(u8 m)
227 return content_features(m).light_propagates;
228 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
232 If true, the material allows lossless sunlight propagation.
233 NOTE: It doesn't seem to go through torches regardlessly of this
234 NOTE: Don't use, use "content_features(m).whatever" instead
236 inline bool sunlight_propagates_content(u8 m)
238 return content_features(m).sunlight_propagates;
239 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
243 On a node-node surface, the material of the node with higher solidness
248 NOTE: Don't use, use "content_features(m).whatever" instead
250 inline u8 content_solidness(u8 m)
252 return content_features(m).solidness;
253 /*// As of now, every pseudo node like torches are added to this
254 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
256 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
261 // Objects collide with walkable contents
262 // NOTE: Don't use, use "content_features(m).whatever" instead
263 inline bool content_walkable(u8 m)
265 return content_features(m).walkable;
266 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
269 // NOTE: Don't use, use "content_features(m).whatever" instead
270 inline bool content_liquid(u8 m)
272 return content_features(m).liquid_type != LIQUID_NONE;
273 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
276 // NOTE: Don't use, use "content_features(m).whatever" instead
277 inline bool content_flowing_liquid(u8 m)
279 return content_features(m).liquid_type == LIQUID_FLOWING;
280 //return (m == CONTENT_WATER);
283 // NOTE: Don't use, use "content_features(m).whatever" instead
284 inline bool content_liquid_source(u8 m)
286 return content_features(m).liquid_type == LIQUID_SOURCE;
287 //return (m == CONTENT_WATERSOURCE);
290 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
291 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
292 inline u8 make_liquid_flowing(u8 m)
294 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
295 return CONTENT_WATER;
299 // Pointable contents can be pointed to in the map
300 // NOTE: Don't use, use "content_features(m).whatever" instead
301 inline bool content_pointable(u8 m)
303 return content_features(m).pointable;
304 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
307 // NOTE: Don't use, use "content_features(m).whatever" instead
308 inline bool content_diggable(u8 m)
310 return content_features(m).diggable;
311 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
314 // NOTE: Don't use, use "content_features(m).whatever" instead
315 inline bool content_buildable_to(u8 m)
317 return content_features(m).buildable_to;
318 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
322 Returns true for contents that form the base ground that
323 follows the main heightmap
325 /*inline bool is_ground_content(u8 m)
327 return content_features(m).is_ground_content;
331 Nodes make a face if contents differ and solidness differs.
334 1: Face uses m1's content
335 2: Face uses m2's content
337 inline u8 face_contents(u8 m1, u8 m2)
339 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
342 bool contents_differ = (m1 != m2);
344 // Contents don't differ for different forms of same liquid
345 if(content_liquid(m1) && content_liquid(m2)
346 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
347 contents_differ = false;
349 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
350 bool makes_face = contents_differ && solidness_differs;
352 if(makes_face == false)
355 if(content_solidness(m1) > content_solidness(m2))
362 Packs directions like (1,0,0), (1,-1,0)
364 inline u8 packDir(v3s16 dir)
385 inline v3s16 unpackDir(u8 b)
414 This is the stuff what the whole world consists of.
423 Misc parameter. Initialized to 0.
424 - For light_propagates() blocks, this is light intensity,
425 stored logarithmically from 0 to LIGHT_MAX.
426 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
427 - Contains 2 values, day- and night lighting. Each takes 4 bits.
434 The second parameter. Initialized to 0.
435 Direction for torches and flowing water.
441 MapNode(const MapNode & n)
446 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
453 /*MapNode & operator=(const MapNode &other)
457 param2 = other.param2;
461 bool operator==(const MapNode &other)
464 && param == other.param
465 && param2 == other.param2);
468 bool light_propagates()
470 return light_propagates_content(d);
473 bool sunlight_propagates()
475 return sunlight_propagates_content(d);
480 return content_solidness(d);
486 Note that a block that isn't light_propagates() can be a light source.
488 if(d == CONTENT_TORCH)
494 u8 getLightBanksWithSource()
496 // Select the brightest of [light source, propagated light]
499 if(light_propagates())
501 lightday = param & 0x0f;
502 lightnight = (param>>4)&0x0f;
504 if(light_source() > lightday)
505 lightday = light_source();
506 if(light_source() > lightnight)
507 lightnight = light_source();
508 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
511 void setLightBanks(u8 a_light)
516 u8 getLight(enum LightBank bank)
518 // Select the brightest of [light source, propagated light]
520 if(light_propagates())
522 if(bank == LIGHTBANK_DAY)
523 light = param & 0x0f;
524 else if(bank == LIGHTBANK_NIGHT)
525 light = (param>>4)&0x0f;
529 if(light_source() > light)
530 light = light_source();
534 // 0 <= daylight_factor <= 1000
535 // 0 <= return value <= LIGHT_SUN
536 u8 getLightBlend(u32 daylight_factor)
538 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
539 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
542 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
548 /*// 0 <= daylight_factor <= 1000
549 // 0 <= return value <= 255
550 u8 getLightBlend(u32 daylight_factor)
552 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
553 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
554 u8 mix = ((daylight_factor * daylight
555 + (1000-daylight_factor) * nightlight)
560 void setLight(enum LightBank bank, u8 a_light)
562 // If not transparent, can't set light
563 if(light_propagates() == false)
565 if(bank == LIGHTBANK_DAY)
568 param |= a_light & 0x0f;
570 else if(bank == LIGHTBANK_NIGHT)
573 param |= (a_light & 0x0f)<<4;
580 TileSpec getTile(v3s16 dir);
585 These serialization functions are used when informing client
589 static u32 serializedLength(u8 version)
591 if(!ser_ver_supported(version))
592 throw VersionMismatchException("ERROR: MapNode format not supported");
596 else if(version <= 9)
601 void serialize(u8 *dest, u8 version)
603 if(!ser_ver_supported(version))
604 throw VersionMismatchException("ERROR: MapNode format not supported");
610 else if(version <= 9)
622 void deSerialize(u8 *source, u8 version)
624 if(!ser_ver_supported(version))
625 throw VersionMismatchException("ERROR: MapNode format not supported");
631 else if(version == 1)
634 // This version doesn't support saved lighting
635 if(light_propagates() || light_source() > 0)
640 else if(version <= 9)
652 // Translate deprecated stuff
653 // NOTE: This doesn't get used because MapBlock handles node
654 // parameters directly
655 MapNode *translate_to = content_features(d).translate_to;
658 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
659 <<translate_to->d<<std::endl;
660 *this = *translate_to;
666 Returns integer position of the node in given
667 floating point position.
669 inline v3s16 floatToInt(v3f p)
672 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
673 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
674 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
679 The same thing backwards
681 inline v3f intToFloat(v3s16 p)