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 bool is_ground_content;
120 bool light_propagates;
121 bool sunlight_propagates;
122 u8 solidness; // Used when choosing which face is drawn
123 // This is used for collision detection.
124 // Also for general solidness queries.
126 // Player can point to these
128 // Player can dig these
130 // Player can build on these
132 // Whether the node has no liquid, source liquid or flowing liquid
133 enum LiquidType liquid_type;
134 // If true, param2 is set to direction when placed. Used for torches.
135 // NOTE: the direction format is quite inefficient and should be changed
137 // If true, node is equivalent to air. Torches are, air is. Water is not.
138 // Is used for example to check whether a mud block can have grass on.
141 // Inventory item string as which the node appears in inventory when dug.
142 // Mineral overrides this.
143 std::string dug_item;
145 // Initial metadata is cloned from this
146 NodeMetadata *initial_metadata;
148 // If the content is liquid, this is the flowing version of the liquid.
149 // If content is liquid, this is the same content.
150 content_t liquid_alternative_flowing;
151 // If the content is liquid, this is the source version of the liquid.
152 content_t liquid_alternative_source;
154 // Amount of light the node emits
157 // Digging properties for different tools
158 DiggingPropertiesList digging_properties;
160 // NOTE: Move relevant properties to here from elsewhere
164 param_type = CPT_NONE;
165 inventory_texture = NULL;
166 is_ground_content = false;
167 light_propagates = false;
168 sunlight_propagates = false;
173 buildable_to = false;
174 liquid_type = LIQUID_NONE;
175 wall_mounted = false;
176 air_equivalent = false;
178 initial_metadata = NULL;
179 liquid_alternative_flowing = CONTENT_IGNORE;
181 digging_properties.clear();
192 Quickhands for simple materials
195 void setTexture(u16 i, std::string name, u8 alpha=255);
197 void setAllTextures(std::string name, u8 alpha=255)
199 for(u16 i=0; i<6; i++)
201 setTexture(i, name, alpha);
203 // Force inventory texture too
204 setInventoryTexture(name);
207 void setTile(u16 i, const TileSpec &tile)
211 void setAllTiles(const TileSpec &tile)
213 for(u16 i=0; i<6; i++)
219 void setInventoryTexture(std::string imgname);
221 void setInventoryTextureCube(std::string top,
222 std::string left, std::string right);
226 Call this to access the ContentFeature list
228 ContentFeatures & content_features(content_t i);
229 ContentFeatures & content_features(MapNode &n);
232 Here is a bunch of DEPRECATED functions.
236 If true, the material allows light propagation and brightness is stored
238 NOTE: Don't use, use "content_features(m).whatever" instead
240 inline bool light_propagates_content(content_t m)
242 return content_features(m).light_propagates;
245 If true, the material allows lossless sunlight propagation.
246 NOTE: It doesn't seem to go through torches regardlessly of this
247 NOTE: Don't use, use "content_features(m).whatever" instead
249 inline bool sunlight_propagates_content(content_t m)
251 return content_features(m).sunlight_propagates;
254 On a node-node surface, the material of the node with higher solidness
259 NOTE: Don't use, use "content_features(m).whatever" instead
261 inline u8 content_solidness(content_t m)
263 return content_features(m).solidness;
265 // Objects collide with walkable contents
266 // NOTE: Don't use, use "content_features(m).whatever" instead
267 inline bool content_walkable(content_t m)
269 return content_features(m).walkable;
271 // NOTE: Don't use, use "content_features(m).whatever" instead
272 inline bool content_liquid(content_t m)
274 return content_features(m).liquid_type != LIQUID_NONE;
276 // NOTE: Don't use, use "content_features(m).whatever" instead
277 inline bool content_flowing_liquid(content_t m)
279 return content_features(m).liquid_type == LIQUID_FLOWING;
281 // NOTE: Don't use, use "content_features(m).whatever" instead
282 inline bool content_liquid_source(content_t m)
284 return content_features(m).liquid_type == LIQUID_SOURCE;
286 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
287 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
288 // NOTE: Don't use, use "content_features(m).whatever" instead
289 inline content_t make_liquid_flowing(content_t m)
291 u8 c = content_features(m).liquid_alternative_flowing;
292 assert(c != CONTENT_IGNORE);
295 // Pointable contents can be pointed to in the map
296 // NOTE: Don't use, use "content_features(m).whatever" instead
297 inline bool content_pointable(content_t m)
299 return content_features(m).pointable;
301 // NOTE: Don't use, use "content_features(m).whatever" instead
302 inline bool content_diggable(content_t m)
304 return content_features(m).diggable;
306 // NOTE: Don't use, use "content_features(m).whatever" instead
307 inline bool content_buildable_to(content_t m)
309 return content_features(m).buildable_to;
313 Nodes make a face if contents differ and solidness differs.
316 1: Face uses m1's content
317 2: Face uses m2's content
319 inline u8 face_contents(content_t m1, content_t m2)
321 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
324 bool contents_differ = (m1 != m2);
326 // Contents don't differ for different forms of same liquid
327 if(content_liquid(m1) && content_liquid(m2)
328 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
329 contents_differ = false;
331 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
332 bool makes_face = contents_differ && solidness_differs;
334 if(makes_face == false)
337 if(content_solidness(m1) > content_solidness(m2))
344 Packs directions like (1,0,0), (1,-1,0)
346 inline u8 packDir(v3s16 dir)
367 inline v3s16 unpackDir(u8 b)
390 facedir: CPT_FACEDIR_SIMPLE param1 value
391 dir: The face for which stuff is wanted
392 return value: The face from which the stuff is actually found
394 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
403 This is the stuff what the whole world consists of.
410 0x00-0x7f: Short content type
411 0x80-0xff: Long content type (param2>>4 makes up low bytes)
420 Misc parameter. Initialized to 0.
421 - For light_propagates() blocks, this is light intensity,
422 stored logarithmically from 0 to LIGHT_MAX.
423 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
424 - Contains 2 values, day- and night lighting. Each takes 4 bits.
425 - Mineral content (should be removed from here)
426 - Uhh... well, most blocks have light or nothing in here.
435 The second parameter. Initialized to 0.
436 E.g. direction for torches and flowing water.
437 If param0 >= 0x80, bits 0xf0 of this is extended content type data
445 MapNode(const MapNode & n)
450 MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
455 // Set after other params because this needs to override part of param2
459 bool operator==(const MapNode &other)
461 return (param0 == other.param0
462 && param1 == other.param1
463 && param2 == other.param2);
466 // To be used everywhere
467 content_t getContent()
472 return (param0<<4) + (param2>>4);
474 void setContent(content_t c)
486 param2 |= (c&0x0f)<<4;
491 These four are DEPRECATED I guess. -c55
493 bool light_propagates()
495 return light_propagates_content(getContent());
497 bool sunlight_propagates()
499 return sunlight_propagates_content(getContent());
503 return content_solidness(getContent());
507 return content_features(*this).light_source;
510 u8 getLightBanksWithSource()
512 // Select the brightest of [light source, propagated light]
515 if(content_features(*this).param_type == CPT_LIGHT)
517 lightday = param1 & 0x0f;
518 lightnight = (param1>>4)&0x0f;
520 if(light_source() > lightday)
521 lightday = light_source();
522 if(light_source() > lightnight)
523 lightnight = light_source();
524 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
527 u8 getLight(enum LightBank bank)
529 // Select the brightest of [light source, propagated light]
531 if(content_features(*this).param_type == CPT_LIGHT)
533 if(bank == LIGHTBANK_DAY)
534 light = param1 & 0x0f;
535 else if(bank == LIGHTBANK_NIGHT)
536 light = (param1>>4)&0x0f;
540 if(light_source() > light)
541 light = light_source();
545 // 0 <= daylight_factor <= 1000
546 // 0 <= return value <= LIGHT_SUN
547 u8 getLightBlend(u32 daylight_factor)
549 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
550 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
553 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
559 /*// 0 <= daylight_factor <= 1000
560 // 0 <= return value <= 255
561 u8 getLightBlend(u32 daylight_factor)
563 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
564 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
565 u8 mix = ((daylight_factor * daylight
566 + (1000-daylight_factor) * nightlight)
571 void setLight(enum LightBank bank, u8 a_light)
573 // If node doesn't contain light data, ignore this
574 if(content_features(*this).param_type != CPT_LIGHT)
576 if(bank == LIGHTBANK_DAY)
579 param1 |= a_light & 0x0f;
581 else if(bank == LIGHTBANK_NIGHT)
584 param1 |= (a_light & 0x0f)<<4;
592 Get tile of a face of the node.
593 dir: direction of face
594 Returns: TileSpec. Can contain miscellaneous texture coordinates,
595 which must be obeyed so that the texture atlas can be used.
597 TileSpec getTile(v3s16 dir);
600 Gets mineral content of node, if there is any.
601 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
606 Serialization functions
609 static u32 serializedLength(u8 version);
610 void serialize(u8 *dest, u8 version);
611 void deSerialize(u8 *source, u8 version);
616 Gets lighting value at face of node
618 Parameters must consist of air and !air.
619 Order doesn't matter.
621 If either of the nodes doesn't exist, light is 0.
624 daynight_ratio: 0...1000
626 n2: getNodeParent(p + face_dir)
627 face_dir: axis oriented unit vector from p to p2
629 returns encoded light value.
631 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,