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 // True for all ground-like things like stone and mud, false for eg. trees
124 bool is_ground_content;
125 bool light_propagates;
126 bool sunlight_propagates;
127 u8 solidness; // Used when choosing which face is drawn
128 // This is used for collision detection.
129 // Also for general solidness queries.
131 // Player can point to these
133 // Player can dig these
135 // Player can build on these
137 // Whether the node has no liquid, source liquid or flowing liquid
138 enum LiquidType liquid_type;
139 // If true, param2 is set to direction when placed. Used for torches.
140 // NOTE: the direction format is quite inefficient and should be changed
142 // If true, node is equivalent to air. Torches are, air is. Water is not.
143 // Is used for example to check whether a mud block can have grass on.
146 // Inventory item string as which the node appears in inventory when dug.
147 // Mineral overrides this.
148 std::string dug_item;
150 // Initial metadata is cloned from this
151 NodeMetadata *initial_metadata;
153 // If the content is liquid, this is the flowing version of the liquid.
154 // If content is flowing liquid, this is the same content.
155 u8 liquid_alternative_flowing;
156 // If the content is liquid, this is the source version of the liquid.
157 u8 liquid_alternative_source;
159 // Amount of light the node emits
162 // Digging properties for different tools
163 DiggingPropertiesList digging_properties;
165 // NOTE: Move relevant properties to here from elsewhere
169 //translate_to = NULL;
170 param_type = CPT_NONE;
171 inventory_texture = NULL;
172 is_ground_content = false;
173 light_propagates = false;
174 sunlight_propagates = false;
179 buildable_to = false;
180 liquid_type = LIQUID_NONE;
181 wall_mounted = false;
182 air_equivalent = false;
184 initial_metadata = NULL;
185 liquid_alternative_flowing = CONTENT_IGNORE;
187 digging_properties.clear();
198 Quickhands for simple materials
201 void setTexture(u16 i, std::string name, u8 alpha=255);
203 void setAllTextures(std::string name, u8 alpha=255)
205 for(u16 i=0; i<6; i++)
207 setTexture(i, name, alpha);
209 // Force inventory texture too
210 setInventoryTexture(name);
213 void setTile(u16 i, const TileSpec &tile)
217 void setAllTiles(const TileSpec &tile)
219 for(u16 i=0; i<6; i++)
225 void setInventoryTexture(std::string imgname);
227 void setInventoryTextureCube(std::string top,
228 std::string left, std::string right);
232 Call this to access the ContentFeature list
234 ContentFeatures & content_features(u8 i);
238 Here is a bunch of DEPRECATED functions.
242 If true, the material allows light propagation and brightness is stored
244 NOTE: Don't use, use "content_features(m).whatever" instead
246 inline bool light_propagates_content(u8 m)
248 return content_features(m).light_propagates;
251 If true, the material allows lossless sunlight propagation.
252 NOTE: It doesn't seem to go through torches regardlessly of this
253 NOTE: Don't use, use "content_features(m).whatever" instead
255 inline bool sunlight_propagates_content(u8 m)
257 return content_features(m).sunlight_propagates;
260 On a node-node surface, the material of the node with higher solidness
265 NOTE: Don't use, use "content_features(m).whatever" instead
267 inline u8 content_solidness(u8 m)
269 return content_features(m).solidness;
271 // Objects collide with walkable contents
272 // NOTE: Don't use, use "content_features(m).whatever" instead
273 inline bool content_walkable(u8 m)
275 return content_features(m).walkable;
277 // NOTE: Don't use, use "content_features(m).whatever" instead
278 inline bool content_liquid(u8 m)
280 return content_features(m).liquid_type != LIQUID_NONE;
282 // NOTE: Don't use, use "content_features(m).whatever" instead
283 inline bool content_flowing_liquid(u8 m)
285 return content_features(m).liquid_type == LIQUID_FLOWING;
287 // NOTE: Don't use, use "content_features(m).whatever" instead
288 inline bool content_liquid_source(u8 m)
290 return content_features(m).liquid_type == LIQUID_SOURCE;
292 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
293 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
294 // NOTE: Don't use, use "content_features(m).whatever" instead
295 inline u8 make_liquid_flowing(u8 m)
297 u8 c = content_features(m).liquid_alternative_flowing;
298 assert(c != CONTENT_IGNORE);
301 // Pointable contents can be pointed to in the map
302 // NOTE: Don't use, use "content_features(m).whatever" instead
303 inline bool content_pointable(u8 m)
305 return content_features(m).pointable;
307 // NOTE: Don't use, use "content_features(m).whatever" instead
308 inline bool content_diggable(u8 m)
310 return content_features(m).diggable;
312 // NOTE: Don't use, use "content_features(m).whatever" instead
313 inline bool content_buildable_to(u8 m)
315 return content_features(m).buildable_to;
319 Nodes make a face if contents differ and solidness differs.
322 1: Face uses m1's content
323 2: Face uses m2's content
325 inline u8 face_contents(u8 m1, u8 m2)
327 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
330 bool contents_differ = (m1 != m2);
332 // Contents don't differ for different forms of same liquid
333 if(content_liquid(m1) && content_liquid(m2)
334 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
335 contents_differ = false;
337 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
338 bool makes_face = contents_differ && solidness_differs;
340 if(makes_face == false)
343 if(content_solidness(m1) > content_solidness(m2))
350 Packs directions like (1,0,0), (1,-1,0)
352 inline u8 packDir(v3s16 dir)
373 inline v3s16 unpackDir(u8 b)
396 facedir: CPT_FACEDIR_SIMPLE param1 value
397 dir: The face for which stuff is wanted
398 return value: The face from which the stuff is actually found
400 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
409 Masks for MapNode.param2 of flowing liquids
411 #define LIQUID_LEVEL_MASK 0x07
412 #define LIQUID_FLOW_DOWN_MASK 0x08
415 This is the stuff what the whole world consists of.
423 0x00-0x7f: Short content type
424 0x80-0xff: Long content type (param2>>4 makes up low bytes)
433 Misc parameter. Initialized to 0.
434 - For light_propagates() blocks, this is light intensity,
435 stored logarithmically from 0 to LIGHT_MAX.
436 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
437 - Contains 2 values, day- and night lighting. Each takes 4 bits.
438 - Mineral content (should be removed from here)
439 - Uhh... well, most blocks have light or nothing in here.
448 The second parameter. Initialized to 0.
449 E.g. direction for torches and flowing water.
457 MapNode(const MapNode & n)
462 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
469 bool operator==(const MapNode &other)
472 && param == other.param
473 && param2 == other.param2);
476 // To be used everywhere
477 content_t getContent()
481 void setContent(content_t c)
487 These four are DEPRECATED I guess. -c55
489 bool light_propagates()
491 return light_propagates_content(d);
493 bool sunlight_propagates()
495 return sunlight_propagates_content(d);
499 return content_solidness(d);
503 return content_features(d).light_source;
506 u8 getLightBanksWithSource()
508 // Select the brightest of [light source, propagated light]
511 if(content_features(d).param_type == CPT_LIGHT)
513 lightday = param & 0x0f;
514 lightnight = (param>>4)&0x0f;
516 if(light_source() > lightday)
517 lightday = light_source();
518 if(light_source() > lightnight)
519 lightnight = light_source();
520 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
523 u8 getLight(enum LightBank bank)
525 // Select the brightest of [light source, propagated light]
527 if(content_features(d).param_type == CPT_LIGHT)
529 if(bank == LIGHTBANK_DAY)
530 light = param & 0x0f;
531 else if(bank == LIGHTBANK_NIGHT)
532 light = (param>>4)&0x0f;
536 if(light_source() > light)
537 light = light_source();
541 // 0 <= daylight_factor <= 1000
542 // 0 <= return value <= LIGHT_SUN
543 u8 getLightBlend(u32 daylight_factor)
545 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
546 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
549 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
555 /*// 0 <= daylight_factor <= 1000
556 // 0 <= return value <= 255
557 u8 getLightBlend(u32 daylight_factor)
559 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
560 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
561 u8 mix = ((daylight_factor * daylight
562 + (1000-daylight_factor) * nightlight)
567 void setLight(enum LightBank bank, u8 a_light)
569 // If node doesn't contain light data, ignore this
570 if(content_features(d).param_type != CPT_LIGHT)
572 if(bank == LIGHTBANK_DAY)
575 param |= a_light & 0x0f;
577 else if(bank == LIGHTBANK_NIGHT)
580 param |= (a_light & 0x0f)<<4;
588 Get tile of a face of the node.
589 dir: direction of face
590 Returns: TileSpec. Can contain miscellaneous texture coordinates,
591 which must be obeyed so that the texture atlas can be used.
593 TileSpec getTile(v3s16 dir);
596 Gets mineral content of node, if there is any.
597 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
602 Serialization functions
605 static u32 serializedLength(u8 version);
606 void serialize(u8 *dest, u8 version);
607 void deSerialize(u8 *source, u8 version);
612 Gets lighting value at face of node
614 Parameters must consist of air and !air.
615 Order doesn't matter.
617 If either of the nodes doesn't exist, light is 0.
620 daynight_ratio: 0...1000
622 n2: getNodeParent(p + face_dir)
623 face_dir: axis oriented unit vector from p to p2
625 returns encoded light value.
627 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,