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 "materials.h"
34 - Material = irrlicht's Material class
35 - Content = (u8) content of a node
36 - Tile = TileSpec at some side of a node of some content type
41 0x000...0x07f: param2 is fully usable
42 0x800...0xfff: param2 lower 4 bytes are free
44 typedef u16 content_t;
47 Initializes all kind of stuff in here.
48 Many things depend on this.
50 This accesses g_texturesource; if it is non-NULL, textures are set.
52 Client first calls this with g_texturesource=NULL to run some
53 unit tests and stuff, then it runs this again with g_texturesource
54 defined to get the textures.
56 Server only calls this once with g_texturesource=NULL.
63 Anything that stores MapNodes doesn't have to preserve parameters
64 associated with this material.
66 Doesn't create faces with anything and is considered being
67 out-of-map in the game map.
69 //#define CONTENT_IGNORE 255
70 #define CONTENT_IGNORE 127
71 #define CONTENT_IGNORE_DEFAULT_PARAM 0
74 The common material through which the player can walk and which
75 is transparent to light
77 //#define CONTENT_AIR 254
78 #define CONTENT_AIR 126
89 // Direction for chests and furnaces and such
103 struct ContentFeatures
105 // If non-NULL, content is translated to this when deserialized
106 //MapNode *translate_to;
108 // Type of MapNode::param
109 ContentParamType param_type;
121 video::ITexture *inventory_texture;
123 bool is_ground_content;
124 bool light_propagates;
125 bool sunlight_propagates;
126 u8 solidness; // Used when choosing which face is drawn
127 // This is used for collision detection.
128 // Also for general solidness queries.
130 // Player can point to these
132 // Player can dig these
134 // Player can build on these
136 // Whether the node has no liquid, source liquid or flowing liquid
137 enum LiquidType liquid_type;
138 // If true, param2 is set to direction when placed. Used for torches.
139 // NOTE: the direction format is quite inefficient and should be changed
141 // If true, node is equivalent to air. Torches are, air is. Water is not.
142 // Is used for example to check whether a mud block can have grass on.
145 // Inventory item string as which the node appears in inventory when dug.
146 // Mineral overrides this.
147 std::string dug_item;
149 // Initial metadata is cloned from this
150 NodeMetadata *initial_metadata;
152 // If the content is liquid, this is the flowing version of the liquid.
153 // If content is flowing liquid, this is the same content.
154 u8 liquid_alternative_flowing;
155 // If the content is liquid, this is the source version of the liquid.
156 u8 liquid_alternative_source;
158 // Amount of light the node emits
161 // Digging properties for different tools
162 DiggingPropertiesList digging_properties;
164 // NOTE: Move relevant properties to here from elsewhere
168 //translate_to = NULL;
169 param_type = CPT_NONE;
170 inventory_texture = NULL;
171 is_ground_content = false;
172 light_propagates = false;
173 sunlight_propagates = false;
178 buildable_to = false;
179 liquid_type = LIQUID_NONE;
180 wall_mounted = false;
181 air_equivalent = false;
183 initial_metadata = NULL;
184 liquid_alternative_flowing = CONTENT_IGNORE;
186 digging_properties.clear();
197 Quickhands for simple materials
200 void setTexture(u16 i, std::string name, u8 alpha=255);
202 void setAllTextures(std::string name, u8 alpha=255)
204 for(u16 i=0; i<6; i++)
206 setTexture(i, name, alpha);
208 // Force inventory texture too
209 setInventoryTexture(name);
212 void setTile(u16 i, const TileSpec &tile)
216 void setAllTiles(const TileSpec &tile)
218 for(u16 i=0; i<6; i++)
224 void setInventoryTexture(std::string imgname);
226 void setInventoryTextureCube(std::string top,
227 std::string left, std::string right);
231 Call this to access the ContentFeature list
233 ContentFeatures & content_features(u8 i);
237 Here is a bunch of DEPRECATED functions.
241 If true, the material allows light propagation and brightness is stored
243 NOTE: Don't use, use "content_features(m).whatever" instead
245 inline bool light_propagates_content(u8 m)
247 return content_features(m).light_propagates;
250 If true, the material allows lossless sunlight propagation.
251 NOTE: It doesn't seem to go through torches regardlessly of this
252 NOTE: Don't use, use "content_features(m).whatever" instead
254 inline bool sunlight_propagates_content(u8 m)
256 return content_features(m).sunlight_propagates;
259 On a node-node surface, the material of the node with higher solidness
264 NOTE: Don't use, use "content_features(m).whatever" instead
266 inline u8 content_solidness(u8 m)
268 return content_features(m).solidness;
270 // Objects collide with walkable contents
271 // NOTE: Don't use, use "content_features(m).whatever" instead
272 inline bool content_walkable(u8 m)
274 return content_features(m).walkable;
276 // NOTE: Don't use, use "content_features(m).whatever" instead
277 inline bool content_liquid(u8 m)
279 return content_features(m).liquid_type != LIQUID_NONE;
281 // NOTE: Don't use, use "content_features(m).whatever" instead
282 inline bool content_flowing_liquid(u8 m)
284 return content_features(m).liquid_type == LIQUID_FLOWING;
286 // NOTE: Don't use, use "content_features(m).whatever" instead
287 inline bool content_liquid_source(u8 m)
289 return content_features(m).liquid_type == LIQUID_SOURCE;
291 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
292 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
293 // NOTE: Don't use, use "content_features(m).whatever" instead
294 inline u8 make_liquid_flowing(u8 m)
296 u8 c = content_features(m).liquid_alternative_flowing;
297 assert(c != CONTENT_IGNORE);
300 // Pointable contents can be pointed to in the map
301 // NOTE: Don't use, use "content_features(m).whatever" instead
302 inline bool content_pointable(u8 m)
304 return content_features(m).pointable;
306 // NOTE: Don't use, use "content_features(m).whatever" instead
307 inline bool content_diggable(u8 m)
309 return content_features(m).diggable;
311 // NOTE: Don't use, use "content_features(m).whatever" instead
312 inline bool content_buildable_to(u8 m)
314 return content_features(m).buildable_to;
318 Nodes make a face if contents differ and solidness differs.
321 1: Face uses m1's content
322 2: Face uses m2's content
324 inline u8 face_contents(u8 m1, u8 m2)
326 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
329 bool contents_differ = (m1 != m2);
331 // Contents don't differ for different forms of same liquid
332 if(content_liquid(m1) && content_liquid(m2)
333 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
334 contents_differ = false;
336 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
337 bool makes_face = contents_differ && solidness_differs;
339 if(makes_face == false)
342 if(content_solidness(m1) > content_solidness(m2))
349 Packs directions like (1,0,0), (1,-1,0)
351 inline u8 packDir(v3s16 dir)
372 inline v3s16 unpackDir(u8 b)
395 facedir: CPT_FACEDIR_SIMPLE param1 value
396 dir: The face for which stuff is wanted
397 return value: The face from which the stuff is actually found
399 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
408 Masks for MapNode.param2 of flowing liquids
410 #define LIQUID_LEVEL_MASK 0x07
411 #define LIQUID_FLOW_DOWN_MASK 0x08
414 This is the stuff what the whole world consists of.
422 0x00-0x7f: Short content type
423 0x80-0xff: Long content type (param2>>4 makes up low bytes)
432 Misc parameter. Initialized to 0.
433 - For light_propagates() blocks, this is light intensity,
434 stored logarithmically from 0 to LIGHT_MAX.
435 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
436 - Contains 2 values, day- and night lighting. Each takes 4 bits.
437 - Mineral content (should be removed from here)
438 - Uhh... well, most blocks have light or nothing in here.
447 The second parameter. Initialized to 0.
448 E.g. direction for torches and flowing water.
456 MapNode(const MapNode & n)
461 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
468 bool operator==(const MapNode &other)
471 && param == other.param
472 && param2 == other.param2);
475 // To be used everywhere
476 content_t getContent()
480 void setContent(content_t c)
486 These four are DEPRECATED I guess. -c55
488 bool light_propagates()
490 return light_propagates_content(d);
492 bool sunlight_propagates()
494 return sunlight_propagates_content(d);
498 return content_solidness(d);
502 return content_features(d).light_source;
505 u8 getLightBanksWithSource()
507 // Select the brightest of [light source, propagated light]
510 if(content_features(d).param_type == CPT_LIGHT)
512 lightday = param & 0x0f;
513 lightnight = (param>>4)&0x0f;
515 if(light_source() > lightday)
516 lightday = light_source();
517 if(light_source() > lightnight)
518 lightnight = light_source();
519 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
522 u8 getLight(enum LightBank bank)
524 // Select the brightest of [light source, propagated light]
526 if(content_features(d).param_type == CPT_LIGHT)
528 if(bank == LIGHTBANK_DAY)
529 light = param & 0x0f;
530 else if(bank == LIGHTBANK_NIGHT)
531 light = (param>>4)&0x0f;
535 if(light_source() > light)
536 light = light_source();
540 // 0 <= daylight_factor <= 1000
541 // 0 <= return value <= LIGHT_SUN
542 u8 getLightBlend(u32 daylight_factor)
544 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
545 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
548 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
554 /*// 0 <= daylight_factor <= 1000
555 // 0 <= return value <= 255
556 u8 getLightBlend(u32 daylight_factor)
558 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
559 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
560 u8 mix = ((daylight_factor * daylight
561 + (1000-daylight_factor) * nightlight)
566 void setLight(enum LightBank bank, u8 a_light)
568 // If node doesn't contain light data, ignore this
569 if(content_features(d).param_type != CPT_LIGHT)
571 if(bank == LIGHTBANK_DAY)
574 param |= a_light & 0x0f;
576 else if(bank == LIGHTBANK_NIGHT)
579 param |= (a_light & 0x0f)<<4;
587 Get tile of a face of the node.
588 dir: direction of face
589 Returns: TileSpec. Can contain miscellaneous texture coordinates,
590 which must be obeyed so that the texture atlas can be used.
592 TileSpec getTile(v3s16 dir);
595 Gets mineral content of node, if there is any.
596 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
601 Serialization functions
604 static u32 serializedLength(u8 version);
605 void serialize(u8 *dest, u8 version);
606 void deSerialize(u8 *source, u8 version);
611 Gets lighting value at face of node
613 Parameters must consist of air and !air.
614 Order doesn't matter.
616 If either of the nodes doesn't exist, light is 0.
619 daynight_ratio: 0...1000
621 n2: getNodeParent(p + face_dir)
622 face_dir: axis oriented unit vector from p to p2
624 returns encoded light value.
626 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,