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 u8 visual_solidness; // When solidness=0, this tells how it looks like
125 // This is used for collision detection.
126 // Also for general solidness queries.
128 // Player can point to these
130 // Player can dig these
132 // Player can climb 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 // Extra dug item and its rarity
150 std::string extra_dug_item;
151 s32 extra_dug_item_rarity;
153 // Initial metadata is cloned from this
154 NodeMetadata *initial_metadata;
156 // If the content is liquid, this is the flowing version of the liquid.
157 // If content is liquid, this is the same content.
158 content_t liquid_alternative_flowing;
159 // If the content is liquid, this is the source version of the liquid.
160 content_t liquid_alternative_source;
161 // Viscosity for fluid flow, ranging from 1 to 7, with
162 // 1 giving almost instantaneous propagation and 7 being
163 // the slowest possible
165 // Used currently for flowing liquids
167 // Special irrlicht material, used sometimes
168 video::SMaterial *special_material;
169 AtlasPointer *special_atlas;
171 // Amount of light the node emits
174 // Digging properties for different tools
175 DiggingPropertiesList digging_properties;
177 u32 damage_per_second;
179 // NOTE: Move relevant properties to here from elsewhere
183 param_type = CPT_NONE;
184 inventory_texture = NULL;
185 is_ground_content = false;
186 light_propagates = false;
187 sunlight_propagates = false;
189 visual_solidness = 0;
194 buildable_to = false;
195 liquid_type = LIQUID_NONE;
196 wall_mounted = false;
197 air_equivalent = false;
199 initial_metadata = NULL;
200 liquid_alternative_flowing = CONTENT_IGNORE;
201 liquid_alternative_source = CONTENT_IGNORE;
202 liquid_viscosity = 0;
204 special_material = NULL;
205 special_atlas = NULL;
207 digging_properties.clear();
208 damage_per_second = 0;
219 Quickhands for simple materials
222 void setTexture(u16 i, std::string name, u8 alpha=255);
224 void setAllTextures(std::string name, u8 alpha=255)
226 for(u16 i=0; i<6; i++)
228 setTexture(i, name, alpha);
230 // Force inventory texture too
231 setInventoryTexture(name);
234 void setTile(u16 i, const TileSpec &tile)
238 void setAllTiles(const TileSpec &tile)
240 for(u16 i=0; i<6; i++)
246 void setInventoryTexture(std::string imgname);
248 void setInventoryTextureCube(std::string top,
249 std::string left, std::string right);
253 Call this to access the ContentFeature list
255 ContentFeatures & content_features(content_t i);
256 ContentFeatures & content_features(MapNode &n);
259 Here is a bunch of DEPRECATED functions.
263 If true, the material allows light propagation and brightness is stored
265 NOTE: Don't use, use "content_features(m).whatever" instead
267 inline bool light_propagates_content(content_t m)
269 return content_features(m).light_propagates;
272 If true, the material allows lossless sunlight propagation.
273 NOTE: It doesn't seem to go through torches regardlessly of this
274 NOTE: Don't use, use "content_features(m).whatever" instead
276 inline bool sunlight_propagates_content(content_t m)
278 return content_features(m).sunlight_propagates;
281 On a node-node surface, the material of the node with higher solidness
286 NOTE: Don't use, use "content_features(m).whatever" instead
288 inline u8 content_solidness(content_t m)
290 return content_features(m).solidness;
292 // Objects collide with walkable contents
293 // NOTE: Don't use, use "content_features(m).whatever" instead
294 inline bool content_walkable(content_t m)
296 return content_features(m).walkable;
298 // NOTE: Don't use, use "content_features(m).whatever" instead
299 inline bool content_liquid(content_t m)
301 return content_features(m).liquid_type != LIQUID_NONE;
303 // NOTE: Don't use, use "content_features(m).whatever" instead
304 inline bool content_flowing_liquid(content_t m)
306 return content_features(m).liquid_type == LIQUID_FLOWING;
308 // NOTE: Don't use, use "content_features(m).whatever" instead
309 inline bool content_liquid_source(content_t m)
311 return content_features(m).liquid_type == LIQUID_SOURCE;
313 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
314 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
315 // NOTE: Don't use, use "content_features(m).whatever" instead
316 inline content_t make_liquid_flowing(content_t m)
318 u8 c = content_features(m).liquid_alternative_flowing;
319 assert(c != CONTENT_IGNORE);
322 // Pointable contents can be pointed to in the map
323 // NOTE: Don't use, use "content_features(m).whatever" instead
324 inline bool content_pointable(content_t m)
326 return content_features(m).pointable;
328 // NOTE: Don't use, use "content_features(m).whatever" instead
329 inline bool content_diggable(content_t m)
331 return content_features(m).diggable;
333 // NOTE: Don't use, use "content_features(m).whatever" instead
334 inline bool content_buildable_to(content_t m)
336 return content_features(m).buildable_to;
340 Nodes make a face if contents differ and solidness differs.
343 1: Face uses m1's content
344 2: Face uses m2's content
346 inline u8 face_contents(content_t m1, content_t m2)
348 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
351 bool contents_differ = (m1 != m2);
353 // Contents don't differ for different forms of same liquid
354 if(content_liquid(m1) && content_liquid(m2)
355 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
356 contents_differ = false;
358 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
359 bool makes_face = contents_differ && solidness_differs;
361 if(makes_face == false)
364 if(content_solidness(m1) > content_solidness(m2))
371 Packs directions like (1,0,0), (1,-1,0)
373 inline u8 packDir(v3s16 dir)
394 inline v3s16 unpackDir(u8 b)
417 facedir: CPT_FACEDIR_SIMPLE param1 value
418 dir: The face for which stuff is wanted
419 return value: The face from which the stuff is actually found
421 NOTE: Currently this uses 2 bits for Z-,X-,Z+,X+, should there be Y+
424 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
433 Masks for MapNode.param2 of flowing liquids
435 #define LIQUID_LEVEL_MASK 0x07
436 #define LIQUID_FLOW_DOWN_MASK 0x08
438 /* maximum amount of liquid in a block */
439 #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
440 #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
443 This is the stuff what the whole world consists of.
451 0x00-0x7f: Short content type
452 0x80-0xff: Long content type (param2>>4 makes up low bytes)
461 Misc parameter. Initialized to 0.
462 - For light_propagates() blocks, this is light intensity,
463 stored logarithmically from 0 to LIGHT_MAX.
464 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
465 - Contains 2 values, day- and night lighting. Each takes 4 bits.
466 - Mineral content (should be removed from here)
467 - Uhh... well, most blocks have light or nothing in here.
476 The second parameter. Initialized to 0.
477 E.g. direction for torches and flowing water.
478 If param0 >= 0x80, bits 0xf0 of this is extended content type data
486 MapNode(const MapNode & n)
491 MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
496 // Set after other params because this needs to override part of param2
500 bool operator==(const MapNode &other)
502 return (param0 == other.param0
503 && param1 == other.param1
504 && param2 == other.param2);
507 // To be used everywhere
508 content_t getContent()
513 return (param0<<4) + (param2>>4);
515 void setContent(content_t c)
527 param2 |= (c&0x0f)<<4;
532 These four are DEPRECATED I guess. -c55
534 bool light_propagates()
536 return light_propagates_content(getContent());
538 bool sunlight_propagates()
540 return sunlight_propagates_content(getContent());
544 return content_solidness(getContent());
548 return content_features(*this).light_source;
551 u8 getLightBanksWithSource()
553 // Select the brightest of [light source, propagated light]
556 if(content_features(*this).param_type == CPT_LIGHT)
558 lightday = param1 & 0x0f;
559 lightnight = (param1>>4)&0x0f;
561 if(light_source() > lightday)
562 lightday = light_source();
563 if(light_source() > lightnight)
564 lightnight = light_source();
565 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
568 u8 getLight(enum LightBank bank)
570 // Select the brightest of [light source, propagated light]
572 if(content_features(*this).param_type == CPT_LIGHT)
574 if(bank == LIGHTBANK_DAY)
575 light = param1 & 0x0f;
576 else if(bank == LIGHTBANK_NIGHT)
577 light = (param1>>4)&0x0f;
581 if(light_source() > light)
582 light = light_source();
586 // 0 <= daylight_factor <= 1000
587 // 0 <= return value <= LIGHT_SUN
588 u8 getLightBlend(u32 daylight_factor)
590 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
591 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
594 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
600 /*// 0 <= daylight_factor <= 1000
601 // 0 <= return value <= 255
602 u8 getLightBlend(u32 daylight_factor)
604 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
605 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
606 u8 mix = ((daylight_factor * daylight
607 + (1000-daylight_factor) * nightlight)
612 void setLight(enum LightBank bank, u8 a_light)
614 // If node doesn't contain light data, ignore this
615 if(content_features(*this).param_type != CPT_LIGHT)
617 if(bank == LIGHTBANK_DAY)
620 param1 |= a_light & 0x0f;
622 else if(bank == LIGHTBANK_NIGHT)
625 param1 |= (a_light & 0x0f)<<4;
633 Get tile of a face of the node.
634 dir: direction of face
635 Returns: TileSpec. Can contain miscellaneous texture coordinates,
636 which must be obeyed so that the texture atlas can be used.
638 TileSpec getTile(v3s16 dir);
641 Gets mineral content of node, if there is any.
642 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
647 Serialization functions
650 static u32 serializedLength(u8 version);
651 void serialize(u8 *dest, u8 version);
652 void deSerialize(u8 *source, u8 version);
657 Gets lighting value at face of node
659 Parameters must consist of air and !air.
660 Order doesn't matter.
662 If either of the nodes doesn't exist, light is 0.
665 daynight_ratio: 0...1000
667 n2: getNodeParent(p + face_dir)
668 face_dir: axis oriented unit vector from p to p2
670 returns encoded light value.
672 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,