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
113 enum ContentParamType
118 // Direction for chests and furnaces and such
132 struct ContentFeatures
134 // If non-NULL, content is translated to this when deserialized
135 MapNode *translate_to;
137 // Type of MapNode::param
138 ContentParamType param_type;
150 video::ITexture *inventory_texture;
152 bool is_ground_content;
153 bool light_propagates;
154 bool sunlight_propagates;
155 u8 solidness; // Used when choosing which face is drawn
156 // This is used for collision detection.
157 // Also for general solidness queries.
159 // Player can point to these
161 // Player can dig these
163 // Player can build on these
165 // Whether the node has no liquid, source liquid or flowing liquid
166 enum LiquidType liquid_type;
167 // If true, param2 is set to direction when placed. Used for torches.
168 // NOTE: the direction format is quite inefficient and should be changed
170 // If true, node is equivalent to air. Torches are, air is. Water is not.
171 // Is used for example to check whether a mud block can have grass on.
174 // Inventory item string as which the node appears in inventory when dug.
175 // Mineral overrides this.
176 std::string dug_item;
178 // Initial metadata is cloned from this
179 NodeMetadata *initial_metadata;
181 //TODO: Move more properties here
186 param_type = CPT_NONE;
187 inventory_texture = NULL;
188 is_ground_content = false;
189 light_propagates = false;
190 sunlight_propagates = false;
195 buildable_to = false;
196 liquid_type = LIQUID_NONE;
197 wall_mounted = false;
198 air_equivalent = false;
200 initial_metadata = NULL;
206 Quickhands for simple materials
209 void setTexture(u16 i, std::string name, u8 alpha=255);
211 void setAllTextures(std::string name, u8 alpha=255)
213 for(u16 i=0; i<6; i++)
215 setTexture(i, name, alpha);
219 void setTile(u16 i, const TileSpec &tile)
223 void setAllTiles(const TileSpec &tile)
225 for(u16 i=0; i<6; i++)
231 void setInventoryTexture(std::string imgname);
233 void setInventoryTextureCube(std::string top,
234 std::string left, std::string right);
238 Call this to access the ContentFeature list
240 ContentFeatures & content_features(u8 i);
243 If true, the material allows light propagation and brightness is stored
245 NOTE: Don't use, use "content_features(m).whatever" instead
247 inline bool light_propagates_content(u8 m)
249 return content_features(m).light_propagates;
250 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
254 If true, the material allows lossless sunlight propagation.
255 NOTE: It doesn't seem to go through torches regardlessly of this
256 NOTE: Don't use, use "content_features(m).whatever" instead
258 inline bool sunlight_propagates_content(u8 m)
260 return content_features(m).sunlight_propagates;
261 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
265 On a node-node surface, the material of the node with higher solidness
270 NOTE: Don't use, use "content_features(m).whatever" instead
272 inline u8 content_solidness(u8 m)
274 return content_features(m).solidness;
275 /*// As of now, every pseudo node like torches are added to this
276 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
278 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
283 // Objects collide with walkable contents
284 // NOTE: Don't use, use "content_features(m).whatever" instead
285 inline bool content_walkable(u8 m)
287 return content_features(m).walkable;
288 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
291 // NOTE: Don't use, use "content_features(m).whatever" instead
292 inline bool content_liquid(u8 m)
294 return content_features(m).liquid_type != LIQUID_NONE;
295 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
298 // NOTE: Don't use, use "content_features(m).whatever" instead
299 inline bool content_flowing_liquid(u8 m)
301 return content_features(m).liquid_type == LIQUID_FLOWING;
302 //return (m == CONTENT_WATER);
305 // NOTE: Don't use, use "content_features(m).whatever" instead
306 inline bool content_liquid_source(u8 m)
308 return content_features(m).liquid_type == LIQUID_SOURCE;
309 //return (m == CONTENT_WATERSOURCE);
312 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
313 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
314 inline u8 make_liquid_flowing(u8 m)
316 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
317 return CONTENT_WATER;
321 // Pointable contents can be pointed to in the map
322 // NOTE: Don't use, use "content_features(m).whatever" instead
323 inline bool content_pointable(u8 m)
325 return content_features(m).pointable;
326 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
329 // NOTE: Don't use, use "content_features(m).whatever" instead
330 inline bool content_diggable(u8 m)
332 return content_features(m).diggable;
333 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
336 // NOTE: Don't use, use "content_features(m).whatever" instead
337 inline bool content_buildable_to(u8 m)
339 return content_features(m).buildable_to;
340 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
344 Returns true for contents that form the base ground that
345 follows the main heightmap
347 /*inline bool is_ground_content(u8 m)
349 return content_features(m).is_ground_content;
353 Nodes make a face if contents differ and solidness differs.
356 1: Face uses m1's content
357 2: Face uses m2's content
359 inline u8 face_contents(u8 m1, u8 m2)
361 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
364 bool contents_differ = (m1 != m2);
366 // Contents don't differ for different forms of same liquid
367 if(content_liquid(m1) && content_liquid(m2)
368 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
369 contents_differ = false;
371 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
372 bool makes_face = contents_differ && solidness_differs;
374 if(makes_face == false)
377 if(content_solidness(m1) > content_solidness(m2))
384 Packs directions like (1,0,0), (1,-1,0)
386 inline u8 packDir(v3s16 dir)
407 inline v3s16 unpackDir(u8 b)
430 facedir: CPT_FACEDIR_SIMPLE param1 value
431 dir: The face for which stuff is wanted
432 return value: The face from which the stuff is actually found
434 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
443 This is the stuff what the whole world consists of.
452 Misc parameter. Initialized to 0.
453 - For light_propagates() blocks, this is light intensity,
454 stored logarithmically from 0 to LIGHT_MAX.
455 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
456 - Contains 2 values, day- and night lighting. Each takes 4 bits.
465 The second parameter. Initialized to 0.
466 E.g. direction for torches and flowing water.
474 MapNode(const MapNode & n)
479 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
486 /*MapNode & operator=(const MapNode &other)
490 param2 = other.param2;
494 bool operator==(const MapNode &other)
497 && param == other.param
498 && param2 == other.param2);
501 bool light_propagates()
503 return light_propagates_content(d);
506 bool sunlight_propagates()
508 return sunlight_propagates_content(d);
513 return content_solidness(d);
519 Note that a block that isn't light_propagates() can be a light source.
521 if(d == CONTENT_TORCH)
527 u8 getLightBanksWithSource()
529 // Select the brightest of [light source, propagated light]
532 if(content_features(d).param_type == CPT_LIGHT)
534 lightday = param & 0x0f;
535 lightnight = (param>>4)&0x0f;
537 if(light_source() > lightday)
538 lightday = light_source();
539 if(light_source() > lightnight)
540 lightnight = light_source();
541 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
544 void setLightBanks(u8 a_light)
549 u8 getLight(enum LightBank bank)
551 // Select the brightest of [light source, propagated light]
553 if(content_features(d).param_type == CPT_LIGHT)
555 if(bank == LIGHTBANK_DAY)
556 light = param & 0x0f;
557 else if(bank == LIGHTBANK_NIGHT)
558 light = (param>>4)&0x0f;
562 if(light_source() > light)
563 light = light_source();
567 // 0 <= daylight_factor <= 1000
568 // 0 <= return value <= LIGHT_SUN
569 u8 getLightBlend(u32 daylight_factor)
571 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
572 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
575 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
581 /*// 0 <= daylight_factor <= 1000
582 // 0 <= return value <= 255
583 u8 getLightBlend(u32 daylight_factor)
585 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
586 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
587 u8 mix = ((daylight_factor * daylight
588 + (1000-daylight_factor) * nightlight)
593 void setLight(enum LightBank bank, u8 a_light)
595 // If node doesn't contain light data, ignore this
596 if(content_features(d).param_type != CPT_LIGHT)
598 if(bank == LIGHTBANK_DAY)
601 param |= a_light & 0x0f;
603 else if(bank == LIGHTBANK_NIGHT)
606 param |= (a_light & 0x0f)<<4;
613 TileSpec getTile(v3s16 dir);
618 These serialization functions are used when informing client
622 static u32 serializedLength(u8 version)
624 if(!ser_ver_supported(version))
625 throw VersionMismatchException("ERROR: MapNode format not supported");
629 else if(version <= 9)
634 void serialize(u8 *dest, u8 version)
636 if(!ser_ver_supported(version))
637 throw VersionMismatchException("ERROR: MapNode format not supported");
643 else if(version <= 9)
655 void deSerialize(u8 *source, u8 version)
657 if(!ser_ver_supported(version))
658 throw VersionMismatchException("ERROR: MapNode format not supported");
664 else if(version == 1)
667 // This version doesn't support saved lighting
668 if(light_propagates() || light_source() > 0)
673 else if(version <= 9)
685 // Translate deprecated stuff
686 // NOTE: This doesn't get used because MapBlock handles node
687 // parameters directly
688 MapNode *translate_to = content_features(d).translate_to;
691 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
692 <<translate_to->d<<std::endl;
693 *this = *translate_to;