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 liquid, this is the same content.
154 u8 liquid_alternative_flowing;
156 // Amount of light the node emits
159 // Digging properties for different tools
160 DiggingPropertiesList digging_properties;
162 // NOTE: Move relevant properties to here from elsewhere
166 //translate_to = NULL;
167 param_type = CPT_NONE;
168 inventory_texture = NULL;
169 is_ground_content = false;
170 light_propagates = false;
171 sunlight_propagates = false;
176 buildable_to = false;
177 liquid_type = LIQUID_NONE;
178 wall_mounted = false;
179 air_equivalent = false;
181 initial_metadata = NULL;
182 liquid_alternative_flowing = CONTENT_IGNORE;
184 digging_properties.clear();
195 Quickhands for simple materials
198 void setTexture(u16 i, std::string name, u8 alpha=255);
200 void setAllTextures(std::string name, u8 alpha=255)
202 for(u16 i=0; i<6; i++)
204 setTexture(i, name, alpha);
206 // Force inventory texture too
207 setInventoryTexture(name);
210 void setTile(u16 i, const TileSpec &tile)
214 void setAllTiles(const TileSpec &tile)
216 for(u16 i=0; i<6; i++)
222 void setInventoryTexture(std::string imgname);
224 void setInventoryTextureCube(std::string top,
225 std::string left, std::string right);
229 Call this to access the ContentFeature list
231 ContentFeatures & content_features(u8 i);
235 Here is a bunch of DEPRECATED functions.
239 If true, the material allows light propagation and brightness is stored
241 NOTE: Don't use, use "content_features(m).whatever" instead
243 inline bool light_propagates_content(u8 m)
245 return content_features(m).light_propagates;
248 If true, the material allows lossless sunlight propagation.
249 NOTE: It doesn't seem to go through torches regardlessly of this
250 NOTE: Don't use, use "content_features(m).whatever" instead
252 inline bool sunlight_propagates_content(u8 m)
254 return content_features(m).sunlight_propagates;
257 On a node-node surface, the material of the node with higher solidness
262 NOTE: Don't use, use "content_features(m).whatever" instead
264 inline u8 content_solidness(u8 m)
266 return content_features(m).solidness;
268 // Objects collide with walkable contents
269 // NOTE: Don't use, use "content_features(m).whatever" instead
270 inline bool content_walkable(u8 m)
272 return content_features(m).walkable;
274 // NOTE: Don't use, use "content_features(m).whatever" instead
275 inline bool content_liquid(u8 m)
277 return content_features(m).liquid_type != LIQUID_NONE;
279 // NOTE: Don't use, use "content_features(m).whatever" instead
280 inline bool content_flowing_liquid(u8 m)
282 return content_features(m).liquid_type == LIQUID_FLOWING;
284 // NOTE: Don't use, use "content_features(m).whatever" instead
285 inline bool content_liquid_source(u8 m)
287 return content_features(m).liquid_type == LIQUID_SOURCE;
289 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
290 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
291 // NOTE: Don't use, use "content_features(m).whatever" instead
292 inline u8 make_liquid_flowing(u8 m)
294 u8 c = content_features(m).liquid_alternative_flowing;
295 assert(c != CONTENT_IGNORE);
298 // Pointable contents can be pointed to in the map
299 // NOTE: Don't use, use "content_features(m).whatever" instead
300 inline bool content_pointable(u8 m)
302 return content_features(m).pointable;
304 // NOTE: Don't use, use "content_features(m).whatever" instead
305 inline bool content_diggable(u8 m)
307 return content_features(m).diggable;
309 // NOTE: Don't use, use "content_features(m).whatever" instead
310 inline bool content_buildable_to(u8 m)
312 return content_features(m).buildable_to;
316 Nodes make a face if contents differ and solidness differs.
319 1: Face uses m1's content
320 2: Face uses m2's content
322 inline u8 face_contents(u8 m1, u8 m2)
324 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
327 bool contents_differ = (m1 != m2);
329 // Contents don't differ for different forms of same liquid
330 if(content_liquid(m1) && content_liquid(m2)
331 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
332 contents_differ = false;
334 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
335 bool makes_face = contents_differ && solidness_differs;
337 if(makes_face == false)
340 if(content_solidness(m1) > content_solidness(m2))
347 Packs directions like (1,0,0), (1,-1,0)
349 inline u8 packDir(v3s16 dir)
370 inline v3s16 unpackDir(u8 b)
393 facedir: CPT_FACEDIR_SIMPLE param1 value
394 dir: The face for which stuff is wanted
395 return value: The face from which the stuff is actually found
397 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
406 This is the stuff what the whole world consists of.
413 0x00-0x7f: Short content type
414 0x80-0xff: Long content type (param2>>4 makes up low bytes)
423 Misc parameter. Initialized to 0.
424 - For light_propagates() blocks, this is light intensity,
425 stored logarithmically from 0 to LIGHT_MAX.
426 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
427 - Contains 2 values, day- and night lighting. Each takes 4 bits.
428 - Mineral content (should be removed from here)
429 - Uhh... well, most blocks have light or nothing in here.
438 The second parameter. Initialized to 0.
439 E.g. direction for torches and flowing water.
447 MapNode(const MapNode & n)
452 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
459 bool operator==(const MapNode &other)
462 && param == other.param
463 && param2 == other.param2);
466 // To be used everywhere
467 content_t getContent()
471 void setContent(content_t c)
477 These four are DEPRECATED I guess. -c55
479 bool light_propagates()
481 return light_propagates_content(d);
483 bool sunlight_propagates()
485 return sunlight_propagates_content(d);
489 return content_solidness(d);
493 return content_features(d).light_source;
496 u8 getLightBanksWithSource()
498 // Select the brightest of [light source, propagated light]
501 if(content_features(d).param_type == CPT_LIGHT)
503 lightday = param & 0x0f;
504 lightnight = (param>>4)&0x0f;
506 if(light_source() > lightday)
507 lightday = light_source();
508 if(light_source() > lightnight)
509 lightnight = light_source();
510 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
513 u8 getLight(enum LightBank bank)
515 // Select the brightest of [light source, propagated light]
517 if(content_features(d).param_type == CPT_LIGHT)
519 if(bank == LIGHTBANK_DAY)
520 light = param & 0x0f;
521 else if(bank == LIGHTBANK_NIGHT)
522 light = (param>>4)&0x0f;
526 if(light_source() > light)
527 light = light_source();
531 // 0 <= daylight_factor <= 1000
532 // 0 <= return value <= LIGHT_SUN
533 u8 getLightBlend(u32 daylight_factor)
535 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
536 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
539 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
545 /*// 0 <= daylight_factor <= 1000
546 // 0 <= return value <= 255
547 u8 getLightBlend(u32 daylight_factor)
549 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
550 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
551 u8 mix = ((daylight_factor * daylight
552 + (1000-daylight_factor) * nightlight)
557 void setLight(enum LightBank bank, u8 a_light)
559 // If node doesn't contain light data, ignore this
560 if(content_features(d).param_type != CPT_LIGHT)
562 if(bank == LIGHTBANK_DAY)
565 param |= a_light & 0x0f;
567 else if(bank == LIGHTBANK_NIGHT)
570 param |= (a_light & 0x0f)<<4;
578 Get tile of a face of the node.
579 dir: direction of face
580 Returns: TileSpec. Can contain miscellaneous texture coordinates,
581 which must be obeyed so that the texture atlas can be used.
583 TileSpec getTile(v3s16 dir);
586 Gets mineral content of node, if there is any.
587 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
592 Serialization functions
595 static u32 serializedLength(u8 version);
596 void serialize(u8 *dest, u8 version);
597 void deSerialize(u8 *source, u8 version);
602 Gets lighting value at face of node
604 Parameters must consist of air and !air.
605 Order doesn't matter.
607 If either of the nodes doesn't exist, light is 0.
610 daynight_ratio: 0...1000
612 n2: getNodeParent(p + face_dir)
613 face_dir: axis oriented unit vector from p to p2
615 returns encoded light value.
617 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,