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
110 enum ContentParamType
115 // Direction for chests and furnaces and such
129 struct ContentFeatures
131 // If non-NULL, content is translated to this when deserialized
132 MapNode *translate_to;
134 // Type of MapNode::param
135 ContentParamType param_type;
147 video::ITexture *inventory_texture;
149 bool is_ground_content;
150 bool light_propagates;
151 bool sunlight_propagates;
152 u8 solidness; // Used when choosing which face is drawn
153 // This is used for collision detection.
154 // Also for general solidness queries.
156 // Player can point to these
158 // Player can dig these
160 // Player can build on these
162 // Whether the node has no liquid, source liquid or flowing liquid
163 enum LiquidType liquid_type;
164 // If true, param2 is set to direction when placed. Used for torches.
165 // NOTE: the direction format is quite inefficient and should be changed
167 // If true, node is equivalent to air. Torches are, air is. Water is not.
168 // Is used for example to check whether a mud block can have grass on.
171 // Inventory item string as which the node appears in inventory when dug.
172 // Mineral overrides this.
173 std::string dug_item;
175 // Initial metadata is cloned from this
176 NodeMetadata *initial_metadata;
178 //TODO: Move more properties here
183 param_type = CPT_NONE;
184 inventory_texture = NULL;
185 is_ground_content = false;
186 light_propagates = false;
187 sunlight_propagates = false;
192 buildable_to = false;
193 liquid_type = LIQUID_NONE;
194 wall_mounted = false;
195 air_equivalent = false;
197 initial_metadata = NULL;
203 Quickhands for simple materials
206 void setTexture(u16 i, std::string name, u8 alpha=255);
208 void setAllTextures(std::string name, u8 alpha=255)
210 for(u16 i=0; i<6; i++)
212 setTexture(i, name, alpha);
216 void setTile(u16 i, const TileSpec &tile)
220 void setAllTiles(const TileSpec &tile)
222 for(u16 i=0; i<6; i++)
228 void setInventoryTexture(std::string imgname);
230 void setInventoryTextureCube(std::string top,
231 std::string left, std::string right);
235 Call this to access the ContentFeature list
237 ContentFeatures & content_features(u8 i);
240 If true, the material allows light propagation and brightness is stored
242 NOTE: Don't use, use "content_features(m).whatever" instead
244 inline bool light_propagates_content(u8 m)
246 return content_features(m).light_propagates;
247 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
251 If true, the material allows lossless sunlight propagation.
252 NOTE: It doesn't seem to go through torches regardlessly of this
253 NOTE: Don't use, use "content_features(m).whatever" instead
255 inline bool sunlight_propagates_content(u8 m)
257 return content_features(m).sunlight_propagates;
258 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
262 On a node-node surface, the material of the node with higher solidness
267 NOTE: Don't use, use "content_features(m).whatever" instead
269 inline u8 content_solidness(u8 m)
271 return content_features(m).solidness;
272 /*// As of now, every pseudo node like torches are added to this
273 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
275 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
280 // Objects collide with walkable contents
281 // NOTE: Don't use, use "content_features(m).whatever" instead
282 inline bool content_walkable(u8 m)
284 return content_features(m).walkable;
285 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
288 // NOTE: Don't use, use "content_features(m).whatever" instead
289 inline bool content_liquid(u8 m)
291 return content_features(m).liquid_type != LIQUID_NONE;
292 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
295 // NOTE: Don't use, use "content_features(m).whatever" instead
296 inline bool content_flowing_liquid(u8 m)
298 return content_features(m).liquid_type == LIQUID_FLOWING;
299 //return (m == CONTENT_WATER);
302 // NOTE: Don't use, use "content_features(m).whatever" instead
303 inline bool content_liquid_source(u8 m)
305 return content_features(m).liquid_type == LIQUID_SOURCE;
306 //return (m == CONTENT_WATERSOURCE);
309 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
310 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
311 inline u8 make_liquid_flowing(u8 m)
313 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
314 return CONTENT_WATER;
318 // Pointable contents can be pointed to in the map
319 // NOTE: Don't use, use "content_features(m).whatever" instead
320 inline bool content_pointable(u8 m)
322 return content_features(m).pointable;
323 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
326 // NOTE: Don't use, use "content_features(m).whatever" instead
327 inline bool content_diggable(u8 m)
329 return content_features(m).diggable;
330 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
333 // NOTE: Don't use, use "content_features(m).whatever" instead
334 inline bool content_buildable_to(u8 m)
336 return content_features(m).buildable_to;
337 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
341 Returns true for contents that form the base ground that
342 follows the main heightmap
344 /*inline bool is_ground_content(u8 m)
346 return content_features(m).is_ground_content;
350 Nodes make a face if contents differ and solidness differs.
353 1: Face uses m1's content
354 2: Face uses m2's content
356 inline u8 face_contents(u8 m1, u8 m2)
358 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
361 bool contents_differ = (m1 != m2);
363 // Contents don't differ for different forms of same liquid
364 if(content_liquid(m1) && content_liquid(m2)
365 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
366 contents_differ = false;
368 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
369 bool makes_face = contents_differ && solidness_differs;
371 if(makes_face == false)
374 if(content_solidness(m1) > content_solidness(m2))
381 Packs directions like (1,0,0), (1,-1,0)
383 inline u8 packDir(v3s16 dir)
404 inline v3s16 unpackDir(u8 b)
427 facedir: CPT_FACEDIR_SIMPLE param1 value
428 dir: The face for which stuff is wanted
429 return value: The face from which the stuff is actually found
431 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
440 This is the stuff what the whole world consists of.
449 Misc parameter. Initialized to 0.
450 - For light_propagates() blocks, this is light intensity,
451 stored logarithmically from 0 to LIGHT_MAX.
452 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
453 - Contains 2 values, day- and night lighting. Each takes 4 bits.
462 The second parameter. Initialized to 0.
463 E.g. direction for torches and flowing water.
471 MapNode(const MapNode & n)
476 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
483 /*MapNode & operator=(const MapNode &other)
487 param2 = other.param2;
491 bool operator==(const MapNode &other)
494 && param == other.param
495 && param2 == other.param2);
498 bool light_propagates()
500 return light_propagates_content(d);
503 bool sunlight_propagates()
505 return sunlight_propagates_content(d);
510 return content_solidness(d);
516 Note that a block that isn't light_propagates() can be a light source.
518 if(d == CONTENT_TORCH)
524 u8 getLightBanksWithSource()
526 // Select the brightest of [light source, propagated light]
529 if(content_features(d).param_type == CPT_LIGHT)
531 lightday = param & 0x0f;
532 lightnight = (param>>4)&0x0f;
534 if(light_source() > lightday)
535 lightday = light_source();
536 if(light_source() > lightnight)
537 lightnight = light_source();
538 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
541 void setLightBanks(u8 a_light)
546 u8 getLight(enum LightBank bank)
548 // Select the brightest of [light source, propagated light]
550 if(content_features(d).param_type == CPT_LIGHT)
552 if(bank == LIGHTBANK_DAY)
553 light = param & 0x0f;
554 else if(bank == LIGHTBANK_NIGHT)
555 light = (param>>4)&0x0f;
559 if(light_source() > light)
560 light = light_source();
564 // 0 <= daylight_factor <= 1000
565 // 0 <= return value <= LIGHT_SUN
566 u8 getLightBlend(u32 daylight_factor)
568 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
569 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
572 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
578 /*// 0 <= daylight_factor <= 1000
579 // 0 <= return value <= 255
580 u8 getLightBlend(u32 daylight_factor)
582 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
583 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
584 u8 mix = ((daylight_factor * daylight
585 + (1000-daylight_factor) * nightlight)
590 void setLight(enum LightBank bank, u8 a_light)
592 // If node doesn't contain light data, ignore this
593 if(content_features(d).param_type != CPT_LIGHT)
595 if(bank == LIGHTBANK_DAY)
598 param |= a_light & 0x0f;
600 else if(bank == LIGHTBANK_NIGHT)
603 param |= (a_light & 0x0f)<<4;
610 TileSpec getTile(v3s16 dir);
615 These serialization functions are used when informing client
619 static u32 serializedLength(u8 version)
621 if(!ser_ver_supported(version))
622 throw VersionMismatchException("ERROR: MapNode format not supported");
626 else if(version <= 9)
631 void serialize(u8 *dest, u8 version)
633 if(!ser_ver_supported(version))
634 throw VersionMismatchException("ERROR: MapNode format not supported");
640 else if(version <= 9)
652 void deSerialize(u8 *source, u8 version)
654 if(!ser_ver_supported(version))
655 throw VersionMismatchException("ERROR: MapNode format not supported");
661 else if(version == 1)
664 // This version doesn't support saved lighting
665 if(light_propagates() || light_source() > 0)
670 else if(version <= 9)
682 // Translate deprecated stuff
683 // NOTE: This doesn't get used because MapBlock handles node
684 // parameters directly
685 MapNode *translate_to = content_features(d).translate_to;
688 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
689 <<translate_to->d<<std::endl;
690 *this = *translate_to;