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
114 enum ContentParamType
119 // Direction for chests and furnaces and such
133 struct ContentFeatures
135 // If non-NULL, content is translated to this when deserialized
136 MapNode *translate_to;
138 // Type of MapNode::param
139 ContentParamType param_type;
151 video::ITexture *inventory_texture;
153 bool is_ground_content;
154 bool light_propagates;
155 bool sunlight_propagates;
156 u8 solidness; // Used when choosing which face is drawn
157 // This is used for collision detection.
158 // Also for general solidness queries.
160 // Player can point to these
162 // Player can dig these
164 // Player can build on these
166 // Whether the node has no liquid, source liquid or flowing liquid
167 enum LiquidType liquid_type;
168 // If true, param2 is set to direction when placed. Used for torches.
169 // NOTE: the direction format is quite inefficient and should be changed
171 // If true, node is equivalent to air. Torches are, air is. Water is not.
172 // Is used for example to check whether a mud block can have grass on.
175 // Inventory item string as which the node appears in inventory when dug.
176 // Mineral overrides this.
177 std::string dug_item;
179 // Initial metadata is cloned from this
180 NodeMetadata *initial_metadata;
182 //TODO: Move more properties here
187 param_type = CPT_NONE;
188 inventory_texture = NULL;
189 is_ground_content = false;
190 light_propagates = false;
191 sunlight_propagates = false;
196 buildable_to = false;
197 liquid_type = LIQUID_NONE;
198 wall_mounted = false;
199 air_equivalent = false;
201 initial_metadata = NULL;
207 Quickhands for simple materials
210 void setTexture(u16 i, std::string name, u8 alpha=255);
212 void setAllTextures(std::string name, u8 alpha=255)
214 for(u16 i=0; i<6; i++)
216 setTexture(i, name, alpha);
220 void setTile(u16 i, const TileSpec &tile)
224 void setAllTiles(const TileSpec &tile)
226 for(u16 i=0; i<6; i++)
232 void setInventoryTexture(std::string imgname);
234 void setInventoryTextureCube(std::string top,
235 std::string left, std::string right);
239 Call this to access the ContentFeature list
241 ContentFeatures & content_features(u8 i);
244 If true, the material allows light propagation and brightness is stored
246 NOTE: Don't use, use "content_features(m).whatever" instead
248 inline bool light_propagates_content(u8 m)
250 return content_features(m).light_propagates;
251 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
255 If true, the material allows lossless sunlight propagation.
256 NOTE: It doesn't seem to go through torches regardlessly of this
257 NOTE: Don't use, use "content_features(m).whatever" instead
259 inline bool sunlight_propagates_content(u8 m)
261 return content_features(m).sunlight_propagates;
262 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
266 On a node-node surface, the material of the node with higher solidness
271 NOTE: Don't use, use "content_features(m).whatever" instead
273 inline u8 content_solidness(u8 m)
275 return content_features(m).solidness;
276 /*// As of now, every pseudo node like torches are added to this
277 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
279 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
284 // Objects collide with walkable contents
285 // NOTE: Don't use, use "content_features(m).whatever" instead
286 inline bool content_walkable(u8 m)
288 return content_features(m).walkable;
289 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
292 // NOTE: Don't use, use "content_features(m).whatever" instead
293 inline bool content_liquid(u8 m)
295 return content_features(m).liquid_type != LIQUID_NONE;
296 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
299 // NOTE: Don't use, use "content_features(m).whatever" instead
300 inline bool content_flowing_liquid(u8 m)
302 return content_features(m).liquid_type == LIQUID_FLOWING;
303 //return (m == CONTENT_WATER);
306 // NOTE: Don't use, use "content_features(m).whatever" instead
307 inline bool content_liquid_source(u8 m)
309 return content_features(m).liquid_type == LIQUID_SOURCE;
310 //return (m == CONTENT_WATERSOURCE);
313 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
314 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
315 inline u8 make_liquid_flowing(u8 m)
317 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
318 return CONTENT_WATER;
322 // Pointable contents can be pointed to in the map
323 // NOTE: Don't use, use "content_features(m).whatever" instead
324 inline bool content_pointable(u8 m)
326 return content_features(m).pointable;
327 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
330 // NOTE: Don't use, use "content_features(m).whatever" instead
331 inline bool content_diggable(u8 m)
333 return content_features(m).diggable;
334 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
337 // NOTE: Don't use, use "content_features(m).whatever" instead
338 inline bool content_buildable_to(u8 m)
340 return content_features(m).buildable_to;
341 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
345 Returns true for contents that form the base ground that
346 follows the main heightmap
348 /*inline bool is_ground_content(u8 m)
350 return content_features(m).is_ground_content;
354 Nodes make a face if contents differ and solidness differs.
357 1: Face uses m1's content
358 2: Face uses m2's content
360 inline u8 face_contents(u8 m1, u8 m2)
362 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
365 bool contents_differ = (m1 != m2);
367 // Contents don't differ for different forms of same liquid
368 if(content_liquid(m1) && content_liquid(m2)
369 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
370 contents_differ = false;
372 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
373 bool makes_face = contents_differ && solidness_differs;
375 if(makes_face == false)
378 if(content_solidness(m1) > content_solidness(m2))
385 Packs directions like (1,0,0), (1,-1,0)
387 inline u8 packDir(v3s16 dir)
408 inline v3s16 unpackDir(u8 b)
431 facedir: CPT_FACEDIR_SIMPLE param1 value
432 dir: The face for which stuff is wanted
433 return value: The face from which the stuff is actually found
435 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
444 This is the stuff what the whole world consists of.
453 Misc parameter. Initialized to 0.
454 - For light_propagates() blocks, this is light intensity,
455 stored logarithmically from 0 to LIGHT_MAX.
456 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
457 - Contains 2 values, day- and night lighting. Each takes 4 bits.
466 The second parameter. Initialized to 0.
467 E.g. direction for torches and flowing water.
475 MapNode(const MapNode & n)
480 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
487 /*MapNode & operator=(const MapNode &other)
491 param2 = other.param2;
495 bool operator==(const MapNode &other)
498 && param == other.param
499 && param2 == other.param2);
502 bool light_propagates()
504 return light_propagates_content(d);
507 bool sunlight_propagates()
509 return sunlight_propagates_content(d);
514 return content_solidness(d);
520 Note that a block that isn't light_propagates() can be a light source.
522 if(d == CONTENT_TORCH)
528 u8 getLightBanksWithSource()
530 // Select the brightest of [light source, propagated light]
533 if(content_features(d).param_type == CPT_LIGHT)
535 lightday = param & 0x0f;
536 lightnight = (param>>4)&0x0f;
538 if(light_source() > lightday)
539 lightday = light_source();
540 if(light_source() > lightnight)
541 lightnight = light_source();
542 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
545 void setLightBanks(u8 a_light)
550 u8 getLight(enum LightBank bank)
552 // Select the brightest of [light source, propagated light]
554 if(content_features(d).param_type == CPT_LIGHT)
556 if(bank == LIGHTBANK_DAY)
557 light = param & 0x0f;
558 else if(bank == LIGHTBANK_NIGHT)
559 light = (param>>4)&0x0f;
563 if(light_source() > light)
564 light = light_source();
568 // 0 <= daylight_factor <= 1000
569 // 0 <= return value <= LIGHT_SUN
570 u8 getLightBlend(u32 daylight_factor)
572 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
573 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
576 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
582 /*// 0 <= daylight_factor <= 1000
583 // 0 <= return value <= 255
584 u8 getLightBlend(u32 daylight_factor)
586 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
587 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
588 u8 mix = ((daylight_factor * daylight
589 + (1000-daylight_factor) * nightlight)
594 void setLight(enum LightBank bank, u8 a_light)
596 // If node doesn't contain light data, ignore this
597 if(content_features(d).param_type != CPT_LIGHT)
599 if(bank == LIGHTBANK_DAY)
602 param |= a_light & 0x0f;
604 else if(bank == LIGHTBANK_NIGHT)
607 param |= (a_light & 0x0f)<<4;
614 TileSpec getTile(v3s16 dir);
619 These serialization functions are used when informing client
623 static u32 serializedLength(u8 version)
625 if(!ser_ver_supported(version))
626 throw VersionMismatchException("ERROR: MapNode format not supported");
630 else if(version <= 9)
635 void serialize(u8 *dest, u8 version)
637 if(!ser_ver_supported(version))
638 throw VersionMismatchException("ERROR: MapNode format not supported");
644 else if(version <= 9)
656 void deSerialize(u8 *source, u8 version)
658 if(!ser_ver_supported(version))
659 throw VersionMismatchException("ERROR: MapNode format not supported");
665 else if(version == 1)
668 // This version doesn't support saved lighting
669 if(light_propagates() || light_source() > 0)
674 else if(version <= 9)
686 // Translate deprecated stuff
687 // NOTE: This doesn't get used because MapBlock handles node
688 // parameters directly
689 MapNode *translate_to = content_features(d).translate_to;
692 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
693 <<translate_to->d<<std::endl;
694 *this = *translate_to;