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"
32 Initializes all kind of stuff in here.
33 Many things depend on this.
35 This accesses g_texturesource; if it is non-NULL, textures are set.
37 Client first calls this with g_texturesource=NULL to run some
38 unit tests and stuff, then it runs this again with g_texturesource
39 defined to get the textures.
41 Server only calls this once with g_texturesource=NULL.
45 // Initializes g_content_inventory_texture_paths
46 void init_content_inventory_texture_paths();
49 // NOTE: This is not used appropriately everywhere.
50 #define MATERIALS_COUNT 256
55 Anything that stores MapNodes doesn't have to preserve parameters
56 associated with this material.
58 Doesn't create faces with anything and is considered being
59 out-of-map in the game map.
61 #define CONTENT_IGNORE 255
62 #define CONTENT_IGNORE_DEFAULT_PARAM 0
65 The common material through which the player can walk and which
66 is transparent to light
68 #define CONTENT_AIR 254
76 - Material = irrlicht's Material class
77 - Content = (u8) content of a node
78 - Tile = (u16) Material ID at some side of a node
81 #define CONTENT_STONE 0
82 #define CONTENT_GRASS 1
83 #define CONTENT_WATER 2
84 #define CONTENT_TORCH 3
85 #define CONTENT_TREE 4
86 #define CONTENT_LEAVES 5
87 #define CONTENT_GRASS_FOOTSTEPS 6
88 #define CONTENT_MESE 7
90 #define CONTENT_WATERSOURCE 9
91 // Pretty much useless, clouds won't be drawn this way
92 #define CONTENT_CLOUD 10
93 #define CONTENT_COALSTONE 11
94 #define CONTENT_WOOD 12
95 #define CONTENT_SAND 13
96 #define CONTENT_SIGN_WALL 14
97 #define CONTENT_CHEST 15
98 #define CONTENT_FURNACE 16
99 //#define CONTENT_WORKBENCH 17
100 #define CONTENT_COBBLE 18
101 #define CONTENT_STEEL 19
102 #define CONTENT_GLASS 20
103 #define CONTENT_FENCE 21
104 #define CONTENT_SANDSTONE 22
105 #define CONTENT_CACTUS 23
106 #define CONTENT_BRICK 24
107 #define CONTENT_CLAY 25
108 #define CONTENT_PAPYRUS 26
109 #define CONTENT_BOOKSHELF 27
115 enum ContentParamType
120 // Direction for chests and furnaces and such
134 struct ContentFeatures
136 // If non-NULL, content is translated to this when deserialized
137 MapNode *translate_to;
139 // Type of MapNode::param
140 ContentParamType param_type;
152 video::ITexture *inventory_texture;
154 bool is_ground_content;
155 bool light_propagates;
156 bool sunlight_propagates;
157 u8 solidness; // Used when choosing which face is drawn
158 // This is used for collision detection.
159 // Also for general solidness queries.
161 // Player can point to these
163 // Player can dig these
165 // Player can build on these
167 // Whether the node has no liquid, source liquid or flowing liquid
168 enum LiquidType liquid_type;
169 // If true, param2 is set to direction when placed. Used for torches.
170 // NOTE: the direction format is quite inefficient and should be changed
172 // If true, node is equivalent to air. Torches are, air is. Water is not.
173 // Is used for example to check whether a mud block can have grass on.
176 // Inventory item string as which the node appears in inventory when dug.
177 // Mineral overrides this.
178 std::string dug_item;
180 // Initial metadata is cloned from this
181 NodeMetadata *initial_metadata;
183 //TODO: Move more properties here
188 param_type = CPT_NONE;
189 inventory_texture = NULL;
190 is_ground_content = false;
191 light_propagates = false;
192 sunlight_propagates = false;
197 buildable_to = false;
198 liquid_type = LIQUID_NONE;
199 wall_mounted = false;
200 air_equivalent = false;
202 initial_metadata = NULL;
208 Quickhands for simple materials
211 void setTexture(u16 i, std::string name, u8 alpha=255);
213 void setAllTextures(std::string name, u8 alpha=255)
215 for(u16 i=0; i<6; i++)
217 setTexture(i, name, alpha);
221 void setTile(u16 i, const TileSpec &tile)
225 void setAllTiles(const TileSpec &tile)
227 for(u16 i=0; i<6; i++)
233 void setInventoryTexture(std::string imgname);
235 void setInventoryTextureCube(std::string top,
236 std::string left, std::string right);
240 Call this to access the ContentFeature list
242 ContentFeatures & content_features(u8 i);
245 If true, the material allows light propagation and brightness is stored
247 NOTE: Don't use, use "content_features(m).whatever" instead
249 inline bool light_propagates_content(u8 m)
251 return content_features(m).light_propagates;
252 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
256 If true, the material allows lossless sunlight propagation.
257 NOTE: It doesn't seem to go through torches regardlessly of this
258 NOTE: Don't use, use "content_features(m).whatever" instead
260 inline bool sunlight_propagates_content(u8 m)
262 return content_features(m).sunlight_propagates;
263 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
267 On a node-node surface, the material of the node with higher solidness
272 NOTE: Don't use, use "content_features(m).whatever" instead
274 inline u8 content_solidness(u8 m)
276 return content_features(m).solidness;
277 /*// As of now, every pseudo node like torches are added to this
278 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
280 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
285 // Objects collide with walkable contents
286 // NOTE: Don't use, use "content_features(m).whatever" instead
287 inline bool content_walkable(u8 m)
289 return content_features(m).walkable;
290 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
293 // NOTE: Don't use, use "content_features(m).whatever" instead
294 inline bool content_liquid(u8 m)
296 return content_features(m).liquid_type != LIQUID_NONE;
297 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
300 // NOTE: Don't use, use "content_features(m).whatever" instead
301 inline bool content_flowing_liquid(u8 m)
303 return content_features(m).liquid_type == LIQUID_FLOWING;
304 //return (m == CONTENT_WATER);
307 // NOTE: Don't use, use "content_features(m).whatever" instead
308 inline bool content_liquid_source(u8 m)
310 return content_features(m).liquid_type == LIQUID_SOURCE;
311 //return (m == CONTENT_WATERSOURCE);
314 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
315 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
316 inline u8 make_liquid_flowing(u8 m)
318 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
319 return CONTENT_WATER;
323 // Pointable contents can be pointed to in the map
324 // NOTE: Don't use, use "content_features(m).whatever" instead
325 inline bool content_pointable(u8 m)
327 return content_features(m).pointable;
328 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
331 // NOTE: Don't use, use "content_features(m).whatever" instead
332 inline bool content_diggable(u8 m)
334 return content_features(m).diggable;
335 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
338 // NOTE: Don't use, use "content_features(m).whatever" instead
339 inline bool content_buildable_to(u8 m)
341 return content_features(m).buildable_to;
342 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
346 Returns true for contents that form the base ground that
347 follows the main heightmap
349 /*inline bool is_ground_content(u8 m)
351 return content_features(m).is_ground_content;
355 Nodes make a face if contents differ and solidness differs.
358 1: Face uses m1's content
359 2: Face uses m2's content
361 inline u8 face_contents(u8 m1, u8 m2)
363 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
366 bool contents_differ = (m1 != m2);
368 // Contents don't differ for different forms of same liquid
369 if(content_liquid(m1) && content_liquid(m2)
370 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
371 contents_differ = false;
373 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
374 bool makes_face = contents_differ && solidness_differs;
376 if(makes_face == false)
379 if(content_solidness(m1) > content_solidness(m2))
386 Packs directions like (1,0,0), (1,-1,0)
388 inline u8 packDir(v3s16 dir)
409 inline v3s16 unpackDir(u8 b)
432 facedir: CPT_FACEDIR_SIMPLE param1 value
433 dir: The face for which stuff is wanted
434 return value: The face from which the stuff is actually found
436 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
445 This is the stuff what the whole world consists of.
454 Misc parameter. Initialized to 0.
455 - For light_propagates() blocks, this is light intensity,
456 stored logarithmically from 0 to LIGHT_MAX.
457 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
458 - Contains 2 values, day- and night lighting. Each takes 4 bits.
467 The second parameter. Initialized to 0.
468 E.g. direction for torches and flowing water.
476 MapNode(const MapNode & n)
481 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
488 /*MapNode & operator=(const MapNode &other)
492 param2 = other.param2;
496 bool operator==(const MapNode &other)
499 && param == other.param
500 && param2 == other.param2);
503 bool light_propagates()
505 return light_propagates_content(d);
508 bool sunlight_propagates()
510 return sunlight_propagates_content(d);
515 return content_solidness(d);
521 Note that a block that isn't light_propagates() can be a light source.
523 if(d == CONTENT_TORCH)
529 u8 getLightBanksWithSource()
531 // Select the brightest of [light source, propagated light]
534 if(content_features(d).param_type == CPT_LIGHT)
536 lightday = param & 0x0f;
537 lightnight = (param>>4)&0x0f;
539 if(light_source() > lightday)
540 lightday = light_source();
541 if(light_source() > lightnight)
542 lightnight = light_source();
543 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
546 void setLightBanks(u8 a_light)
551 u8 getLight(enum LightBank bank)
553 // Select the brightest of [light source, propagated light]
555 if(content_features(d).param_type == CPT_LIGHT)
557 if(bank == LIGHTBANK_DAY)
558 light = param & 0x0f;
559 else if(bank == LIGHTBANK_NIGHT)
560 light = (param>>4)&0x0f;
564 if(light_source() > light)
565 light = light_source();
569 // 0 <= daylight_factor <= 1000
570 // 0 <= return value <= LIGHT_SUN
571 u8 getLightBlend(u32 daylight_factor)
573 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
574 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
577 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
583 /*// 0 <= daylight_factor <= 1000
584 // 0 <= return value <= 255
585 u8 getLightBlend(u32 daylight_factor)
587 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
588 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
589 u8 mix = ((daylight_factor * daylight
590 + (1000-daylight_factor) * nightlight)
595 void setLight(enum LightBank bank, u8 a_light)
597 // If node doesn't contain light data, ignore this
598 if(content_features(d).param_type != CPT_LIGHT)
600 if(bank == LIGHTBANK_DAY)
603 param |= a_light & 0x0f;
605 else if(bank == LIGHTBANK_NIGHT)
608 param |= (a_light & 0x0f)<<4;
615 TileSpec getTile(v3s16 dir);
620 These serialization functions are used when informing client
624 static u32 serializedLength(u8 version)
626 if(!ser_ver_supported(version))
627 throw VersionMismatchException("ERROR: MapNode format not supported");
631 else if(version <= 9)
636 void serialize(u8 *dest, u8 version)
638 if(!ser_ver_supported(version))
639 throw VersionMismatchException("ERROR: MapNode format not supported");
645 else if(version <= 9)
657 void deSerialize(u8 *source, u8 version)
659 if(!ser_ver_supported(version))
660 throw VersionMismatchException("ERROR: MapNode format not supported");
666 else if(version == 1)
669 // This version doesn't support saved lighting
670 if(light_propagates() || light_source() > 0)
675 else if(version <= 9)
687 // Translate deprecated stuff
688 // NOTE: This doesn't get used because MapBlock handles node
689 // parameters directly
690 MapNode *translate_to = content_features(d).translate_to;
693 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
694 <<translate_to->d<<std::endl;
695 *this = *translate_to;