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 video::SMaterial *special_material2;
125 AtlasPointer *special_atlas;
128 // List of all block textures that have been used (value is dummy)
129 // Exists on server too for cleaner code in content_mapnode.cpp
130 core::map<std::string, bool> used_texturenames;
132 // Type of MapNode::param1
133 ContentParamType param_type;
134 // True for all ground-like things like stone and mud, false for eg. trees
135 bool is_ground_content;
136 bool light_propagates;
137 bool sunlight_propagates;
138 u8 solidness; // Used when choosing which face is drawn
139 u8 visual_solidness; // When solidness=0, this tells how it looks like
140 // This is used for collision detection.
141 // Also for general solidness queries.
143 // Player can point to these
145 // Player can dig these
147 // Player can climb these
149 // Player can build on these
151 // Whether the node has no liquid, source liquid or flowing liquid
152 enum LiquidType liquid_type;
153 // If true, param2 is set to direction when placed. Used for torches.
154 // NOTE: the direction format is quite inefficient and should be changed
156 // If true, node is equivalent to air. Torches are, air is. Water is not.
157 // Is used for example to check whether a mud block can have grass on.
159 // Whether this content type often contains mineral.
160 // Used for texture atlas creation.
161 // Currently only enabled for CONTENT_STONE.
162 bool often_contains_mineral;
164 // Inventory item string as which the node appears in inventory when dug.
165 // Mineral overrides this.
166 std::string dug_item;
168 // Extra dug item and its rarity
169 std::string extra_dug_item;
170 s32 extra_dug_item_rarity;
172 // Initial metadata is cloned from this
173 NodeMetadata *initial_metadata;
175 // If the content is liquid, this is the flowing version of the liquid.
176 // If content is liquid, this is the same content.
177 content_t liquid_alternative_flowing;
178 // If the content is liquid, this is the source version of the liquid.
179 content_t liquid_alternative_source;
180 // Viscosity for fluid flow, ranging from 1 to 7, with
181 // 1 giving almost instantaneous propagation and 7 being
182 // the slowest possible
185 // Amount of light the node emits
188 // Digging properties for different tools
189 DiggingPropertiesList digging_properties;
191 u32 damage_per_second;
193 // NOTE: Move relevant properties to here from elsewhere
198 inventory_texture = NULL;
201 post_effect_color = video::SColor(0, 0, 0, 0);
202 special_material = NULL;
203 special_material2 = NULL;
204 special_atlas = NULL;
206 param_type = CPT_NONE;
207 is_ground_content = false;
208 light_propagates = false;
209 sunlight_propagates = false;
211 visual_solidness = 0;
216 buildable_to = false;
217 liquid_type = LIQUID_NONE;
218 wall_mounted = false;
219 air_equivalent = false;
220 often_contains_mineral = false;
222 initial_metadata = NULL;
223 liquid_alternative_flowing = CONTENT_IGNORE;
224 liquid_alternative_source = CONTENT_IGNORE;
225 liquid_viscosity = 0;
227 digging_properties.clear();
228 damage_per_second = 0;
239 Quickhands for simple materials
243 void setTexture(u16 i, std::string name, u8 alpha=255)
245 void setAllTextures(std::string name, u8 alpha=255)
248 void setTexture(u16 i, std::string name, u8 alpha=255);
250 void setAllTextures(std::string name, u8 alpha=255)
252 for(u16 i=0; i<6; i++)
254 setTexture(i, name, alpha);
256 // Force inventory texture too
257 setInventoryTexture(name);
262 void setTile(u16 i, const TileSpec &tile)
266 void setAllTiles(const TileSpec &tile)
268 for(u16 i=0; i<6; i++)
276 void setInventoryTexture(std::string imgname)
278 void setInventoryTextureCube(std::string top,
279 std::string left, std::string right)
282 void setInventoryTexture(std::string imgname);
284 void setInventoryTextureCube(std::string top,
285 std::string left, std::string right);
290 Call this to access the ContentFeature list
292 ContentFeatures & content_features(content_t i);
293 ContentFeatures & content_features(MapNode &n);
296 Here is a bunch of DEPRECATED functions.
300 If true, the material allows light propagation and brightness is stored
302 NOTE: Don't use, use "content_features(m).whatever" instead
304 inline bool light_propagates_content(content_t m)
306 return content_features(m).light_propagates;
309 If true, the material allows lossless sunlight propagation.
310 NOTE: It doesn't seem to go through torches regardlessly of this
311 NOTE: Don't use, use "content_features(m).whatever" instead
313 inline bool sunlight_propagates_content(content_t m)
315 return content_features(m).sunlight_propagates;
318 On a node-node surface, the material of the node with higher solidness
323 NOTE: Don't use, use "content_features(m).whatever" instead
325 inline u8 content_solidness(content_t m)
327 return content_features(m).solidness;
329 // Objects collide with walkable contents
330 // NOTE: Don't use, use "content_features(m).whatever" instead
331 inline bool content_walkable(content_t m)
333 return content_features(m).walkable;
335 // NOTE: Don't use, use "content_features(m).whatever" instead
336 inline bool content_liquid(content_t m)
338 return content_features(m).liquid_type != LIQUID_NONE;
340 // NOTE: Don't use, use "content_features(m).whatever" instead
341 inline bool content_flowing_liquid(content_t m)
343 return content_features(m).liquid_type == LIQUID_FLOWING;
345 // NOTE: Don't use, use "content_features(m).whatever" instead
346 inline bool content_liquid_source(content_t m)
348 return content_features(m).liquid_type == LIQUID_SOURCE;
350 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
351 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
352 // NOTE: Don't use, use "content_features(m).whatever" instead
353 inline content_t make_liquid_flowing(content_t m)
355 u8 c = content_features(m).liquid_alternative_flowing;
356 assert(c != CONTENT_IGNORE);
359 // Pointable contents can be pointed to in the map
360 // NOTE: Don't use, use "content_features(m).whatever" instead
361 inline bool content_pointable(content_t m)
363 return content_features(m).pointable;
365 // NOTE: Don't use, use "content_features(m).whatever" instead
366 inline bool content_diggable(content_t m)
368 return content_features(m).diggable;
370 // NOTE: Don't use, use "content_features(m).whatever" instead
371 inline bool content_buildable_to(content_t m)
373 return content_features(m).buildable_to;
377 Nodes make a face if contents differ and solidness differs.
380 1: Face uses m1's content
381 2: Face uses m2's content
382 equivalent: Whether the blocks share the same face (eg. water and glass)
384 u8 face_contents(content_t m1, content_t m2, bool *equivalent);
387 Packs directions like (1,0,0), (1,-1,0)
389 inline u8 packDir(v3s16 dir)
410 inline v3s16 unpackDir(u8 b)
433 facedir: CPT_FACEDIR_SIMPLE param1 value
434 dir: The face for which stuff is wanted
435 return value: The face from which the stuff is actually found
437 NOTE: Currently this uses 2 bits for Z-,X-,Z+,X+, should there be Y+
440 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
449 Masks for MapNode.param2 of flowing liquids
451 #define LIQUID_LEVEL_MASK 0x07
452 #define LIQUID_FLOW_DOWN_MASK 0x08
454 /* maximum amount of liquid in a block */
455 #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
456 #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
459 This is the stuff what the whole world consists of.
467 0x00-0x7f: Short content type
468 0x80-0xff: Long content type (param2>>4 makes up low bytes)
477 Misc parameter. Initialized to 0.
478 - For light_propagates() blocks, this is light intensity,
479 stored logarithmically from 0 to LIGHT_MAX.
480 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
481 - Contains 2 values, day- and night lighting. Each takes 4 bits.
482 - Mineral content (should be removed from here)
483 - Uhh... well, most blocks have light or nothing in here.
492 The second parameter. Initialized to 0.
493 E.g. direction for torches and flowing water.
494 If param0 >= 0x80, bits 0xf0 of this is extended content type data
502 MapNode(const MapNode & n)
507 MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
512 // Set after other params because this needs to override part of param2
516 bool operator==(const MapNode &other)
518 return (param0 == other.param0
519 && param1 == other.param1
520 && param2 == other.param2);
523 // To be used everywhere
524 content_t getContent()
529 return (param0<<4) + (param2>>4);
531 void setContent(content_t c)
543 param2 |= (c&0x0f)<<4;
548 These four are DEPRECATED I guess. -c55
550 bool light_propagates()
552 return light_propagates_content(getContent());
554 bool sunlight_propagates()
556 return sunlight_propagates_content(getContent());
560 return content_solidness(getContent());
564 return content_features(*this).light_source;
567 u8 getLightBanksWithSource()
569 // Select the brightest of [light source, propagated light]
572 if(content_features(*this).param_type == CPT_LIGHT)
574 lightday = param1 & 0x0f;
575 lightnight = (param1>>4)&0x0f;
577 if(light_source() > lightday)
578 lightday = light_source();
579 if(light_source() > lightnight)
580 lightnight = light_source();
581 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
584 u8 getLight(enum LightBank bank)
586 // Select the brightest of [light source, propagated light]
588 if(content_features(*this).param_type == CPT_LIGHT)
590 if(bank == LIGHTBANK_DAY)
591 light = param1 & 0x0f;
592 else if(bank == LIGHTBANK_NIGHT)
593 light = (param1>>4)&0x0f;
597 if(light_source() > light)
598 light = light_source();
602 // 0 <= daylight_factor <= 1000
603 // 0 <= return value <= LIGHT_SUN
604 u8 getLightBlend(u32 daylight_factor)
606 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
607 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
610 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
616 /*// 0 <= daylight_factor <= 1000
617 // 0 <= return value <= 255
618 u8 getLightBlend(u32 daylight_factor)
620 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
621 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
622 u8 mix = ((daylight_factor * daylight
623 + (1000-daylight_factor) * nightlight)
628 void setLight(enum LightBank bank, u8 a_light)
630 // If node doesn't contain light data, ignore this
631 if(content_features(*this).param_type != CPT_LIGHT)
633 if(bank == LIGHTBANK_DAY)
636 param1 |= a_light & 0x0f;
638 else if(bank == LIGHTBANK_NIGHT)
641 param1 |= (a_light & 0x0f)<<4;
650 Get tile of a face of the node.
651 dir: direction of face
652 Returns: TileSpec. Can contain miscellaneous texture coordinates,
653 which must be obeyed so that the texture atlas can be used.
655 TileSpec getTile(v3s16 dir);
659 Gets mineral content of node, if there is any.
660 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
665 Serialization functions
668 static u32 serializedLength(u8 version);
669 void serialize(u8 *dest, u8 version);
670 void deSerialize(u8 *source, u8 version);
675 Gets lighting value at face of node
677 Parameters must consist of air and !air.
678 Order doesn't matter.
680 If either of the nodes doesn't exist, light is 0.
683 daynight_ratio: 0...1000
685 n2: getNodeParent(p + face_dir)
686 face_dir: axis oriented unit vector from p to p2
688 returns encoded light value.
690 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,