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"
26 #include "exceptions.h"
27 #include "serialization.h"
28 #include "materials.h"
35 - Material = irrlicht's Material class
36 - Content = (content_t) content of a node
37 - Tile = TileSpec at some side of a node of some content type
40 0x000...0x07f: param2 is fully usable
41 0x800...0xfff: param2 lower 4 bytes are free
43 typedef u16 content_t;
44 #define MAX_CONTENT 0xfff
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
116 video::ITexture *inventory_texture;
118 // Used currently for flowing liquids
120 // Post effect color, drawn when the camera is inside the node.
121 video::SColor post_effect_color;
122 // Special irrlicht material, used sometimes
123 video::SMaterial *special_material;
124 AtlasPointer *special_atlas;
127 // List of all block textures that have been used (value is dummy)
128 // Exists on server too for cleaner code in content_mapnode.cpp
129 core::map<std::string, bool> used_texturenames;
131 // Type of MapNode::param1
132 ContentParamType param_type;
133 // True for all ground-like things like stone and mud, false for eg. trees
134 bool is_ground_content;
135 bool light_propagates;
136 bool sunlight_propagates;
137 u8 solidness; // Used when choosing which face is drawn
138 u8 visual_solidness; // When solidness=0, this tells how it looks like
139 // This is used for collision detection.
140 // Also for general solidness queries.
142 // Player can point to these
144 // Player can dig these
146 // Player can climb these
148 // Player can build on these
150 // Whether the node has no liquid, source liquid or flowing liquid
151 enum LiquidType liquid_type;
152 // If true, param2 is set to direction when placed. Used for torches.
153 // NOTE: the direction format is quite inefficient and should be changed
155 // If true, node is equivalent to air. Torches are, air is. Water is not.
156 // Is used for example to check whether a mud block can have grass on.
158 // Whether this content type often contains mineral.
159 // Used for texture atlas creation.
160 // Currently only enabled for CONTENT_STONE.
161 bool often_contains_mineral;
163 // Inventory item string as which the node appears in inventory when dug.
164 // Mineral overrides this.
165 std::string dug_item;
167 // Extra dug item and its rarity
168 std::string extra_dug_item;
169 s32 extra_dug_item_rarity;
171 // Initial metadata is cloned from this
172 NodeMetadata *initial_metadata;
174 // If the content is liquid, this is the flowing version of the liquid.
175 // If content is liquid, this is the same content.
176 content_t liquid_alternative_flowing;
177 // If the content is liquid, this is the source version of the liquid.
178 content_t liquid_alternative_source;
179 // Viscosity for fluid flow, ranging from 1 to 7, with
180 // 1 giving almost instantaneous propagation and 7 being
181 // the slowest possible
184 // Amount of light the node emits
187 // Digging properties for different tools
188 DiggingPropertiesList digging_properties;
190 u32 damage_per_second;
192 // NOTE: Move relevant properties to here from elsewhere
197 inventory_texture = NULL;
200 post_effect_color = video::SColor(0, 0, 0, 0);
201 special_material = NULL;
202 special_atlas = NULL;
204 param_type = CPT_NONE;
205 is_ground_content = false;
206 light_propagates = false;
207 sunlight_propagates = false;
209 visual_solidness = 0;
214 buildable_to = false;
215 liquid_type = LIQUID_NONE;
216 wall_mounted = false;
217 air_equivalent = false;
218 often_contains_mineral = false;
220 initial_metadata = NULL;
221 liquid_alternative_flowing = CONTENT_IGNORE;
222 liquid_alternative_source = CONTENT_IGNORE;
223 liquid_viscosity = 0;
225 digging_properties.clear();
226 damage_per_second = 0;
237 Quickhands for simple materials
241 void setTexture(u16 i, std::string name, u8 alpha=255)
243 void setAllTextures(std::string name, u8 alpha=255)
246 void setTexture(u16 i, std::string name, u8 alpha=255);
248 void setAllTextures(std::string name, u8 alpha=255)
250 for(u16 i=0; i<6; i++)
252 setTexture(i, name, alpha);
254 // Force inventory texture too
255 setInventoryTexture(name);
260 void setTile(u16 i, const TileSpec &tile)
264 void setAllTiles(const TileSpec &tile)
266 for(u16 i=0; i<6; i++)
274 void setInventoryTexture(std::string imgname)
276 void setInventoryTextureCube(std::string top,
277 std::string left, std::string right)
280 void setInventoryTexture(std::string imgname);
282 void setInventoryTextureCube(std::string top,
283 std::string left, std::string right);
288 Call this to access the ContentFeature list
290 ContentFeatures & content_features(content_t i);
291 ContentFeatures & content_features(MapNode &n);
294 Here is a bunch of DEPRECATED functions.
298 If true, the material allows light propagation and brightness is stored
300 NOTE: Don't use, use "content_features(m).whatever" instead
302 inline bool light_propagates_content(content_t m)
304 return content_features(m).light_propagates;
307 If true, the material allows lossless sunlight propagation.
308 NOTE: It doesn't seem to go through torches regardlessly of this
309 NOTE: Don't use, use "content_features(m).whatever" instead
311 inline bool sunlight_propagates_content(content_t m)
313 return content_features(m).sunlight_propagates;
316 On a node-node surface, the material of the node with higher solidness
321 NOTE: Don't use, use "content_features(m).whatever" instead
323 inline u8 content_solidness(content_t m)
325 return content_features(m).solidness;
327 // Objects collide with walkable contents
328 // NOTE: Don't use, use "content_features(m).whatever" instead
329 inline bool content_walkable(content_t m)
331 return content_features(m).walkable;
333 // NOTE: Don't use, use "content_features(m).whatever" instead
334 inline bool content_liquid(content_t m)
336 return content_features(m).liquid_type != LIQUID_NONE;
338 // NOTE: Don't use, use "content_features(m).whatever" instead
339 inline bool content_flowing_liquid(content_t m)
341 return content_features(m).liquid_type == LIQUID_FLOWING;
343 // NOTE: Don't use, use "content_features(m).whatever" instead
344 inline bool content_liquid_source(content_t m)
346 return content_features(m).liquid_type == LIQUID_SOURCE;
348 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
349 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
350 // NOTE: Don't use, use "content_features(m).whatever" instead
351 inline content_t make_liquid_flowing(content_t m)
353 u8 c = content_features(m).liquid_alternative_flowing;
354 assert(c != CONTENT_IGNORE);
357 // Pointable contents can be pointed to in the map
358 // NOTE: Don't use, use "content_features(m).whatever" instead
359 inline bool content_pointable(content_t m)
361 return content_features(m).pointable;
363 // NOTE: Don't use, use "content_features(m).whatever" instead
364 inline bool content_diggable(content_t m)
366 return content_features(m).diggable;
368 // NOTE: Don't use, use "content_features(m).whatever" instead
369 inline bool content_buildable_to(content_t m)
371 return content_features(m).buildable_to;
375 Nodes make a face if contents differ and solidness differs.
378 1: Face uses m1's content
379 2: Face uses m2's content
381 inline u8 face_contents(content_t m1, content_t m2)
383 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
386 bool contents_differ = (m1 != m2);
388 // Contents don't differ for different forms of same liquid
389 if(content_liquid(m1) && content_liquid(m2)
390 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
391 contents_differ = false;
393 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
394 bool makes_face = contents_differ && solidness_differs;
396 if(makes_face == false)
399 u8 c1 = content_solidness(m1);
400 u8 c2 = content_solidness(m2);
403 Special case for half-transparent content.
405 This makes eg. the water (solidness=1) surrounding an underwater
406 glass block (solidness=0, visual_solidness=1) not get drawn.
408 if(c1 == 1 && c2 == 0 && content_features(m2).visual_solidness != 0)
410 if(c2 == 1 && c1 == 0 && content_features(m1).visual_solidness != 0)
420 Packs directions like (1,0,0), (1,-1,0)
422 inline u8 packDir(v3s16 dir)
443 inline v3s16 unpackDir(u8 b)
466 facedir: CPT_FACEDIR_SIMPLE param1 value
467 dir: The face for which stuff is wanted
468 return value: The face from which the stuff is actually found
470 NOTE: Currently this uses 2 bits for Z-,X-,Z+,X+, should there be Y+
473 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
482 Masks for MapNode.param2 of flowing liquids
484 #define LIQUID_LEVEL_MASK 0x07
485 #define LIQUID_FLOW_DOWN_MASK 0x08
487 /* maximum amount of liquid in a block */
488 #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
489 #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
492 This is the stuff what the whole world consists of.
500 0x00-0x7f: Short content type
501 0x80-0xff: Long content type (param2>>4 makes up low bytes)
510 Misc parameter. Initialized to 0.
511 - For light_propagates() blocks, this is light intensity,
512 stored logarithmically from 0 to LIGHT_MAX.
513 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
514 - Contains 2 values, day- and night lighting. Each takes 4 bits.
515 - Mineral content (should be removed from here)
516 - Uhh... well, most blocks have light or nothing in here.
525 The second parameter. Initialized to 0.
526 E.g. direction for torches and flowing water.
527 If param0 >= 0x80, bits 0xf0 of this is extended content type data
535 MapNode(const MapNode & n)
540 MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
545 // Set after other params because this needs to override part of param2
549 bool operator==(const MapNode &other)
551 return (param0 == other.param0
552 && param1 == other.param1
553 && param2 == other.param2);
556 // To be used everywhere
557 content_t getContent()
562 return (param0<<4) + (param2>>4);
564 void setContent(content_t c)
576 param2 |= (c&0x0f)<<4;
581 These four are DEPRECATED I guess. -c55
583 bool light_propagates()
585 return light_propagates_content(getContent());
587 bool sunlight_propagates()
589 return sunlight_propagates_content(getContent());
593 return content_solidness(getContent());
597 return content_features(*this).light_source;
600 u8 getLightBanksWithSource()
602 // Select the brightest of [light source, propagated light]
605 if(content_features(*this).param_type == CPT_LIGHT)
607 lightday = param1 & 0x0f;
608 lightnight = (param1>>4)&0x0f;
610 if(light_source() > lightday)
611 lightday = light_source();
612 if(light_source() > lightnight)
613 lightnight = light_source();
614 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
617 u8 getLight(enum LightBank bank)
619 // Select the brightest of [light source, propagated light]
621 if(content_features(*this).param_type == CPT_LIGHT)
623 if(bank == LIGHTBANK_DAY)
624 light = param1 & 0x0f;
625 else if(bank == LIGHTBANK_NIGHT)
626 light = (param1>>4)&0x0f;
630 if(light_source() > light)
631 light = light_source();
635 // 0 <= daylight_factor <= 1000
636 // 0 <= return value <= LIGHT_SUN
637 u8 getLightBlend(u32 daylight_factor)
639 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
640 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
643 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
649 /*// 0 <= daylight_factor <= 1000
650 // 0 <= return value <= 255
651 u8 getLightBlend(u32 daylight_factor)
653 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
654 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
655 u8 mix = ((daylight_factor * daylight
656 + (1000-daylight_factor) * nightlight)
661 void setLight(enum LightBank bank, u8 a_light)
663 // If node doesn't contain light data, ignore this
664 if(content_features(*this).param_type != CPT_LIGHT)
666 if(bank == LIGHTBANK_DAY)
669 param1 |= a_light & 0x0f;
671 else if(bank == LIGHTBANK_NIGHT)
674 param1 |= (a_light & 0x0f)<<4;
683 Get tile of a face of the node.
684 dir: direction of face
685 Returns: TileSpec. Can contain miscellaneous texture coordinates,
686 which must be obeyed so that the texture atlas can be used.
688 TileSpec getTile(v3s16 dir);
692 Gets mineral content of node, if there is any.
693 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
698 Serialization functions
701 static u32 serializedLength(u8 version);
702 void serialize(u8 *dest, u8 version);
703 void deSerialize(u8 *source, u8 version);
708 Gets lighting value at face of node
710 Parameters must consist of air and !air.
711 Order doesn't matter.
713 If either of the nodes doesn't exist, light is 0.
716 daynight_ratio: 0...1000
718 n2: getNodeParent(p + face_dir)
719 face_dir: axis oriented unit vector from p to p2
721 returns encoded light value.
723 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,