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 // Type of MapNode::param1
128 ContentParamType param_type;
129 // True for all ground-like things like stone and mud, false for eg. trees
130 bool is_ground_content;
131 bool light_propagates;
132 bool sunlight_propagates;
133 u8 solidness; // Used when choosing which face is drawn
134 u8 visual_solidness; // When solidness=0, this tells how it looks like
135 // This is used for collision detection.
136 // Also for general solidness queries.
138 // Player can point to these
140 // Player can dig these
142 // Player can climb these
144 // Player can build on these
146 // Whether the node has no liquid, source liquid or flowing liquid
147 enum LiquidType liquid_type;
148 // If true, param2 is set to direction when placed. Used for torches.
149 // NOTE: the direction format is quite inefficient and should be changed
151 // If true, node is equivalent to air. Torches are, air is. Water is not.
152 // Is used for example to check whether a mud block can have grass on.
155 // Inventory item string as which the node appears in inventory when dug.
156 // Mineral overrides this.
157 std::string dug_item;
159 // Extra dug item and its rarity
160 std::string extra_dug_item;
161 s32 extra_dug_item_rarity;
163 // Initial metadata is cloned from this
164 NodeMetadata *initial_metadata;
166 // If the content is liquid, this is the flowing version of the liquid.
167 // If content is liquid, this is the same content.
168 content_t liquid_alternative_flowing;
169 // If the content is liquid, this is the source version of the liquid.
170 content_t liquid_alternative_source;
171 // Viscosity for fluid flow, ranging from 1 to 7, with
172 // 1 giving almost instantaneous propagation and 7 being
173 // the slowest possible
176 // Amount of light the node emits
179 // Digging properties for different tools
180 DiggingPropertiesList digging_properties;
182 u32 damage_per_second;
184 // NOTE: Move relevant properties to here from elsewhere
189 inventory_texture = NULL;
192 post_effect_color = video::SColor(0, 0, 0, 0);
193 special_material = NULL;
194 special_atlas = NULL;
196 param_type = CPT_NONE;
197 is_ground_content = false;
198 light_propagates = false;
199 sunlight_propagates = false;
201 visual_solidness = 0;
206 buildable_to = false;
207 liquid_type = LIQUID_NONE;
208 wall_mounted = false;
209 air_equivalent = false;
211 initial_metadata = NULL;
212 liquid_alternative_flowing = CONTENT_IGNORE;
213 liquid_alternative_source = CONTENT_IGNORE;
214 liquid_viscosity = 0;
216 digging_properties.clear();
217 damage_per_second = 0;
228 Quickhands for simple materials
232 void setTexture(u16 i, std::string name, u8 alpha=255)
234 void setAllTextures(std::string name, u8 alpha=255)
237 void setTexture(u16 i, std::string name, u8 alpha=255);
239 void setAllTextures(std::string name, u8 alpha=255)
241 for(u16 i=0; i<6; i++)
243 setTexture(i, name, alpha);
245 // Force inventory texture too
246 setInventoryTexture(name);
251 void setTile(u16 i, const TileSpec &tile)
255 void setAllTiles(const TileSpec &tile)
257 for(u16 i=0; i<6; i++)
265 void setInventoryTexture(std::string imgname)
267 void setInventoryTextureCube(std::string top,
268 std::string left, std::string right)
271 void setInventoryTexture(std::string imgname);
273 void setInventoryTextureCube(std::string top,
274 std::string left, std::string right);
279 Call this to access the ContentFeature list
281 ContentFeatures & content_features(content_t i);
282 ContentFeatures & content_features(MapNode &n);
285 Here is a bunch of DEPRECATED functions.
289 If true, the material allows light propagation and brightness is stored
291 NOTE: Don't use, use "content_features(m).whatever" instead
293 inline bool light_propagates_content(content_t m)
295 return content_features(m).light_propagates;
298 If true, the material allows lossless sunlight propagation.
299 NOTE: It doesn't seem to go through torches regardlessly of this
300 NOTE: Don't use, use "content_features(m).whatever" instead
302 inline bool sunlight_propagates_content(content_t m)
304 return content_features(m).sunlight_propagates;
307 On a node-node surface, the material of the node with higher solidness
312 NOTE: Don't use, use "content_features(m).whatever" instead
314 inline u8 content_solidness(content_t m)
316 return content_features(m).solidness;
318 // Objects collide with walkable contents
319 // NOTE: Don't use, use "content_features(m).whatever" instead
320 inline bool content_walkable(content_t m)
322 return content_features(m).walkable;
324 // NOTE: Don't use, use "content_features(m).whatever" instead
325 inline bool content_liquid(content_t m)
327 return content_features(m).liquid_type != LIQUID_NONE;
329 // NOTE: Don't use, use "content_features(m).whatever" instead
330 inline bool content_flowing_liquid(content_t m)
332 return content_features(m).liquid_type == LIQUID_FLOWING;
334 // NOTE: Don't use, use "content_features(m).whatever" instead
335 inline bool content_liquid_source(content_t m)
337 return content_features(m).liquid_type == LIQUID_SOURCE;
339 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
340 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
341 // NOTE: Don't use, use "content_features(m).whatever" instead
342 inline content_t make_liquid_flowing(content_t m)
344 u8 c = content_features(m).liquid_alternative_flowing;
345 assert(c != CONTENT_IGNORE);
348 // Pointable contents can be pointed to in the map
349 // NOTE: Don't use, use "content_features(m).whatever" instead
350 inline bool content_pointable(content_t m)
352 return content_features(m).pointable;
354 // NOTE: Don't use, use "content_features(m).whatever" instead
355 inline bool content_diggable(content_t m)
357 return content_features(m).diggable;
359 // NOTE: Don't use, use "content_features(m).whatever" instead
360 inline bool content_buildable_to(content_t m)
362 return content_features(m).buildable_to;
366 Nodes make a face if contents differ and solidness differs.
369 1: Face uses m1's content
370 2: Face uses m2's content
372 inline u8 face_contents(content_t m1, content_t m2)
374 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
377 bool contents_differ = (m1 != m2);
379 // Contents don't differ for different forms of same liquid
380 if(content_liquid(m1) && content_liquid(m2)
381 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
382 contents_differ = false;
384 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
385 bool makes_face = contents_differ && solidness_differs;
387 if(makes_face == false)
390 if(content_solidness(m1) > content_solidness(m2))
397 Packs directions like (1,0,0), (1,-1,0)
399 inline u8 packDir(v3s16 dir)
420 inline v3s16 unpackDir(u8 b)
443 facedir: CPT_FACEDIR_SIMPLE param1 value
444 dir: The face for which stuff is wanted
445 return value: The face from which the stuff is actually found
447 NOTE: Currently this uses 2 bits for Z-,X-,Z+,X+, should there be Y+
450 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
459 Masks for MapNode.param2 of flowing liquids
461 #define LIQUID_LEVEL_MASK 0x07
462 #define LIQUID_FLOW_DOWN_MASK 0x08
464 /* maximum amount of liquid in a block */
465 #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
466 #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
469 This is the stuff what the whole world consists of.
477 0x00-0x7f: Short content type
478 0x80-0xff: Long content type (param2>>4 makes up low bytes)
487 Misc parameter. Initialized to 0.
488 - For light_propagates() blocks, this is light intensity,
489 stored logarithmically from 0 to LIGHT_MAX.
490 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
491 - Contains 2 values, day- and night lighting. Each takes 4 bits.
492 - Mineral content (should be removed from here)
493 - Uhh... well, most blocks have light or nothing in here.
502 The second parameter. Initialized to 0.
503 E.g. direction for torches and flowing water.
504 If param0 >= 0x80, bits 0xf0 of this is extended content type data
512 MapNode(const MapNode & n)
517 MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
522 // Set after other params because this needs to override part of param2
526 bool operator==(const MapNode &other)
528 return (param0 == other.param0
529 && param1 == other.param1
530 && param2 == other.param2);
533 // To be used everywhere
534 content_t getContent()
539 return (param0<<4) + (param2>>4);
541 void setContent(content_t c)
553 param2 |= (c&0x0f)<<4;
558 These four are DEPRECATED I guess. -c55
560 bool light_propagates()
562 return light_propagates_content(getContent());
564 bool sunlight_propagates()
566 return sunlight_propagates_content(getContent());
570 return content_solidness(getContent());
574 return content_features(*this).light_source;
577 u8 getLightBanksWithSource()
579 // Select the brightest of [light source, propagated light]
582 if(content_features(*this).param_type == CPT_LIGHT)
584 lightday = param1 & 0x0f;
585 lightnight = (param1>>4)&0x0f;
587 if(light_source() > lightday)
588 lightday = light_source();
589 if(light_source() > lightnight)
590 lightnight = light_source();
591 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
594 u8 getLight(enum LightBank bank)
596 // Select the brightest of [light source, propagated light]
598 if(content_features(*this).param_type == CPT_LIGHT)
600 if(bank == LIGHTBANK_DAY)
601 light = param1 & 0x0f;
602 else if(bank == LIGHTBANK_NIGHT)
603 light = (param1>>4)&0x0f;
607 if(light_source() > light)
608 light = light_source();
612 // 0 <= daylight_factor <= 1000
613 // 0 <= return value <= LIGHT_SUN
614 u8 getLightBlend(u32 daylight_factor)
616 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
617 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
620 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
626 /*// 0 <= daylight_factor <= 1000
627 // 0 <= return value <= 255
628 u8 getLightBlend(u32 daylight_factor)
630 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
631 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
632 u8 mix = ((daylight_factor * daylight
633 + (1000-daylight_factor) * nightlight)
638 void setLight(enum LightBank bank, u8 a_light)
640 // If node doesn't contain light data, ignore this
641 if(content_features(*this).param_type != CPT_LIGHT)
643 if(bank == LIGHTBANK_DAY)
646 param1 |= a_light & 0x0f;
648 else if(bank == LIGHTBANK_NIGHT)
651 param1 |= (a_light & 0x0f)<<4;
660 Get tile of a face of the node.
661 dir: direction of face
662 Returns: TileSpec. Can contain miscellaneous texture coordinates,
663 which must be obeyed so that the texture atlas can be used.
665 TileSpec getTile(v3s16 dir);
669 Gets mineral content of node, if there is any.
670 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
675 Serialization functions
678 static u32 serializedLength(u8 version);
679 void serialize(u8 *dest, u8 version);
680 void deSerialize(u8 *source, u8 version);
685 Gets lighting value at face of node
687 Parameters must consist of air and !air.
688 Order doesn't matter.
690 If either of the nodes doesn't exist, light is 0.
693 daynight_ratio: 0...1000
695 n2: getNodeParent(p + face_dir)
696 face_dir: axis oriented unit vector from p to p2
698 returns encoded light value.
700 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,