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 = (content_t) content of a node
36 - Tile = TileSpec at some side of a node of some content type
39 0x000...0x07f: param2 is fully usable
40 0x800...0xfff: param2 lower 4 bytes are free
42 typedef u16 content_t;
43 #define MAX_CONTENT 0xfff
46 Initializes all kind of stuff in here.
47 Many things depend on this.
49 This accesses g_texturesource; if it is non-NULL, textures are set.
51 Client first calls this with g_texturesource=NULL to run some
52 unit tests and stuff, then it runs this again with g_texturesource
53 defined to get the textures.
55 Server only calls this once with g_texturesource=NULL.
62 Anything that stores MapNodes doesn't have to preserve parameters
63 associated with this material.
65 Doesn't create faces with anything and is considered being
66 out-of-map in the game map.
68 //#define CONTENT_IGNORE 255
69 #define CONTENT_IGNORE 127
70 #define CONTENT_IGNORE_DEFAULT_PARAM 0
73 The common material through which the player can walk and which
74 is transparent to light
76 //#define CONTENT_AIR 254
77 #define CONTENT_AIR 126
88 // Direction for chests and furnaces and such
102 struct ContentFeatures
104 // Type of MapNode::param1
105 ContentParamType param_type;
117 video::ITexture *inventory_texture;
119 // True for all ground-like things like stone and mud, false for eg. trees
120 bool is_ground_content;
121 bool light_propagates;
122 bool sunlight_propagates;
123 u8 solidness; // Used when choosing which face is drawn
124 // This is used for collision detection.
125 // Also for general solidness queries.
127 // Player can point to these
129 // Player can dig these
131 // Player can build on these
133 // Whether the node has no liquid, source liquid or flowing liquid
134 enum LiquidType liquid_type;
135 // If true, param2 is set to direction when placed. Used for torches.
136 // NOTE: the direction format is quite inefficient and should be changed
138 // If true, node is equivalent to air. Torches are, air is. Water is not.
139 // Is used for example to check whether a mud block can have grass on.
142 // Inventory item string as which the node appears in inventory when dug.
143 // Mineral overrides this.
144 std::string dug_item;
146 // Initial metadata is cloned from this
147 NodeMetadata *initial_metadata;
149 // If the content is liquid, this is the flowing version of the liquid.
150 // If content is liquid, this is the same content.
151 content_t liquid_alternative_flowing;
152 // If the content is liquid, this is the source version of the liquid.
153 content_t liquid_alternative_source;
155 // Amount of light the node emits
158 // Digging properties for different tools
159 DiggingPropertiesList digging_properties;
161 // NOTE: Move relevant properties to here from elsewhere
165 param_type = CPT_NONE;
166 inventory_texture = NULL;
167 is_ground_content = false;
168 light_propagates = false;
169 sunlight_propagates = false;
174 buildable_to = false;
175 liquid_type = LIQUID_NONE;
176 wall_mounted = false;
177 air_equivalent = false;
179 initial_metadata = NULL;
180 liquid_alternative_flowing = CONTENT_IGNORE;
182 digging_properties.clear();
193 Quickhands for simple materials
196 void setTexture(u16 i, std::string name, u8 alpha=255);
198 void setAllTextures(std::string name, u8 alpha=255)
200 for(u16 i=0; i<6; i++)
202 setTexture(i, name, alpha);
204 // Force inventory texture too
205 setInventoryTexture(name);
208 void setTile(u16 i, const TileSpec &tile)
212 void setAllTiles(const TileSpec &tile)
214 for(u16 i=0; i<6; i++)
220 void setInventoryTexture(std::string imgname);
222 void setInventoryTextureCube(std::string top,
223 std::string left, std::string right);
227 Call this to access the ContentFeature list
229 ContentFeatures & content_features(content_t i);
230 ContentFeatures & content_features(MapNode &n);
233 Here is a bunch of DEPRECATED functions.
237 If true, the material allows light propagation and brightness is stored
239 NOTE: Don't use, use "content_features(m).whatever" instead
241 inline bool light_propagates_content(content_t m)
243 return content_features(m).light_propagates;
246 If true, the material allows lossless sunlight propagation.
247 NOTE: It doesn't seem to go through torches regardlessly of this
248 NOTE: Don't use, use "content_features(m).whatever" instead
250 inline bool sunlight_propagates_content(content_t m)
252 return content_features(m).sunlight_propagates;
255 On a node-node surface, the material of the node with higher solidness
260 NOTE: Don't use, use "content_features(m).whatever" instead
262 inline u8 content_solidness(content_t m)
264 return content_features(m).solidness;
266 // Objects collide with walkable contents
267 // NOTE: Don't use, use "content_features(m).whatever" instead
268 inline bool content_walkable(content_t m)
270 return content_features(m).walkable;
272 // NOTE: Don't use, use "content_features(m).whatever" instead
273 inline bool content_liquid(content_t m)
275 return content_features(m).liquid_type != LIQUID_NONE;
277 // NOTE: Don't use, use "content_features(m).whatever" instead
278 inline bool content_flowing_liquid(content_t m)
280 return content_features(m).liquid_type == LIQUID_FLOWING;
282 // NOTE: Don't use, use "content_features(m).whatever" instead
283 inline bool content_liquid_source(content_t m)
285 return content_features(m).liquid_type == LIQUID_SOURCE;
287 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
288 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
289 // NOTE: Don't use, use "content_features(m).whatever" instead
290 inline content_t make_liquid_flowing(content_t m)
292 u8 c = content_features(m).liquid_alternative_flowing;
293 assert(c != CONTENT_IGNORE);
296 // Pointable contents can be pointed to in the map
297 // NOTE: Don't use, use "content_features(m).whatever" instead
298 inline bool content_pointable(content_t m)
300 return content_features(m).pointable;
302 // NOTE: Don't use, use "content_features(m).whatever" instead
303 inline bool content_diggable(content_t m)
305 return content_features(m).diggable;
307 // NOTE: Don't use, use "content_features(m).whatever" instead
308 inline bool content_buildable_to(content_t m)
310 return content_features(m).buildable_to;
314 Nodes make a face if contents differ and solidness differs.
317 1: Face uses m1's content
318 2: Face uses m2's content
320 inline u8 face_contents(content_t m1, content_t m2)
322 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
325 bool contents_differ = (m1 != m2);
327 // Contents don't differ for different forms of same liquid
328 if(content_liquid(m1) && content_liquid(m2)
329 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
330 contents_differ = false;
332 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
333 bool makes_face = contents_differ && solidness_differs;
335 if(makes_face == false)
338 if(content_solidness(m1) > content_solidness(m2))
345 Packs directions like (1,0,0), (1,-1,0)
347 inline u8 packDir(v3s16 dir)
368 inline v3s16 unpackDir(u8 b)
391 facedir: CPT_FACEDIR_SIMPLE param1 value
392 dir: The face for which stuff is wanted
393 return value: The face from which the stuff is actually found
395 NOTE: Currently this uses 2 bits for Z-,X-,Z+,X+, should there be Y+
398 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
407 This is the stuff what the whole world consists of.
414 0x00-0x7f: Short content type
415 0x80-0xff: Long content type (param2>>4 makes up low bytes)
424 Misc parameter. Initialized to 0.
425 - For light_propagates() blocks, this is light intensity,
426 stored logarithmically from 0 to LIGHT_MAX.
427 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
428 - Contains 2 values, day- and night lighting. Each takes 4 bits.
429 - Mineral content (should be removed from here)
430 - Uhh... well, most blocks have light or nothing in here.
439 The second parameter. Initialized to 0.
440 E.g. direction for torches and flowing water.
441 If param0 >= 0x80, bits 0xf0 of this is extended content type data
449 MapNode(const MapNode & n)
454 MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
459 // Set after other params because this needs to override part of param2
463 bool operator==(const MapNode &other)
465 return (param0 == other.param0
466 && param1 == other.param1
467 && param2 == other.param2);
470 // To be used everywhere
471 content_t getContent()
476 return (param0<<4) + (param2>>4);
478 void setContent(content_t c)
490 param2 |= (c&0x0f)<<4;
495 These four are DEPRECATED I guess. -c55
497 bool light_propagates()
499 return light_propagates_content(getContent());
501 bool sunlight_propagates()
503 return sunlight_propagates_content(getContent());
507 return content_solidness(getContent());
511 return content_features(*this).light_source;
514 u8 getLightBanksWithSource()
516 // Select the brightest of [light source, propagated light]
519 if(content_features(*this).param_type == CPT_LIGHT)
521 lightday = param1 & 0x0f;
522 lightnight = (param1>>4)&0x0f;
524 if(light_source() > lightday)
525 lightday = light_source();
526 if(light_source() > lightnight)
527 lightnight = light_source();
528 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
531 u8 getLight(enum LightBank bank)
533 // Select the brightest of [light source, propagated light]
535 if(content_features(*this).param_type == CPT_LIGHT)
537 if(bank == LIGHTBANK_DAY)
538 light = param1 & 0x0f;
539 else if(bank == LIGHTBANK_NIGHT)
540 light = (param1>>4)&0x0f;
544 if(light_source() > light)
545 light = light_source();
549 // 0 <= daylight_factor <= 1000
550 // 0 <= return value <= LIGHT_SUN
551 u8 getLightBlend(u32 daylight_factor)
553 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
554 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
557 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
563 /*// 0 <= daylight_factor <= 1000
564 // 0 <= return value <= 255
565 u8 getLightBlend(u32 daylight_factor)
567 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
568 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
569 u8 mix = ((daylight_factor * daylight
570 + (1000-daylight_factor) * nightlight)
575 void setLight(enum LightBank bank, u8 a_light)
577 // If node doesn't contain light data, ignore this
578 if(content_features(*this).param_type != CPT_LIGHT)
580 if(bank == LIGHTBANK_DAY)
583 param1 |= a_light & 0x0f;
585 else if(bank == LIGHTBANK_NIGHT)
588 param1 |= (a_light & 0x0f)<<4;
596 Get tile of a face of the node.
597 dir: direction of face
598 Returns: TileSpec. Can contain miscellaneous texture coordinates,
599 which must be obeyed so that the texture atlas can be used.
601 TileSpec getTile(v3s16 dir);
604 Gets mineral content of node, if there is any.
605 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
610 Serialization functions
613 static u32 serializedLength(u8 version);
614 void serialize(u8 *dest, u8 version);
615 void deSerialize(u8 *source, u8 version);
620 Gets lighting value at face of node
622 Parameters must consist of air and !air.
623 Order doesn't matter.
625 If either of the nodes doesn't exist, light is 0.
628 daynight_ratio: 0...1000
630 n2: getNodeParent(p + face_dir)
631 face_dir: axis oriented unit vector from p to p2
633 returns encoded light value.
635 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,