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"
32 Initializes all kind of stuff in here.
33 Many things depend on this.
35 This accesses g_texturesource; if it is non-NULL, textures are set.
37 Client first calls this with g_texturesource=NULL to run some
38 unit tests and stuff, then it runs this again with g_texturesource
39 defined to get the textures.
41 Server only calls this once with g_texturesource=NULL.
45 // Initializes g_content_inventory_texture_paths
46 void init_content_inventory_texture_paths();
49 // NOTE: This is not used appropriately everywhere.
50 #define MATERIALS_COUNT 256
55 Anything that stores MapNodes doesn't have to preserve parameters
56 associated with this material.
58 Doesn't create faces with anything and is considered being
59 out-of-map in the game map.
61 #define CONTENT_IGNORE 255
62 #define CONTENT_IGNORE_DEFAULT_PARAM 0
65 The common material through which the player can walk and which
66 is transparent to light
68 #define CONTENT_AIR 254
76 - Material = irrlicht's Material class
77 - Content = (u8) content of a node
78 - Tile = (u16) Material ID at some side of a node
81 #define CONTENT_STONE 0
82 #define CONTENT_GRASS 1
83 #define CONTENT_WATER 2
84 #define CONTENT_TORCH 3
85 #define CONTENT_TREE 4
86 #define CONTENT_LEAVES 5
87 #define CONTENT_GRASS_FOOTSTEPS 6
88 #define CONTENT_MESE 7
90 #define CONTENT_WATERSOURCE 9
91 // Pretty much useless, clouds won't be drawn this way
92 #define CONTENT_CLOUD 10
93 #define CONTENT_COALSTONE 11
94 #define CONTENT_WOOD 12
95 #define CONTENT_SAND 13
96 #define CONTENT_SIGN_WALL 14
97 #define CONTENT_CHEST 15
98 #define CONTENT_FURNACE 16
99 //#define CONTENT_WORKBENCH 17
100 #define CONTENT_COBBLE 18
101 #define CONTENT_STEEL 19
102 #define CONTENT_GLASS 20
103 #define CONTENT_FENCE 21
109 enum ContentParamType
114 // Direction for chests and furnaces and such
128 struct ContentFeatures
130 // If non-NULL, content is translated to this when deserialized
131 MapNode *translate_to;
133 // Type of MapNode::param
134 ContentParamType param_type;
146 video::ITexture *inventory_texture;
148 bool is_ground_content;
149 bool light_propagates;
150 bool sunlight_propagates;
151 u8 solidness; // Used when choosing which face is drawn
152 // This is used for collision detection.
153 // Also for general solidness queries.
158 enum LiquidType liquid_type;
159 // If true, param2 is set to direction when placed
160 // NOTE: the direction format is quite inefficient and should be changed
163 // Inventory item string as which the node appears in inventory when dug.
164 // Mineral overrides this.
165 std::string dug_item;
167 // Initial metadata is cloned from this
168 NodeMetadata *initial_metadata;
170 //TODO: Move more properties here
175 param_type = CPT_NONE;
176 inventory_texture = NULL;
177 is_ground_content = false;
178 light_propagates = false;
179 sunlight_propagates = false;
184 buildable_to = false;
185 liquid_type = LIQUID_NONE;
186 wall_mounted = false;
188 initial_metadata = NULL;
194 Quickhands for simple materials
197 void setTexture(u16 i, std::string name, u8 alpha=255);
199 void setAllTextures(std::string name, u8 alpha=255)
201 for(u16 i=0; i<6; i++)
203 setTexture(i, name, alpha);
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(u8 i);
231 If true, the material allows light propagation and brightness is stored
233 NOTE: Don't use, use "content_features(m).whatever" instead
235 inline bool light_propagates_content(u8 m)
237 return content_features(m).light_propagates;
238 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
242 If true, the material allows lossless sunlight propagation.
243 NOTE: It doesn't seem to go through torches regardlessly of this
244 NOTE: Don't use, use "content_features(m).whatever" instead
246 inline bool sunlight_propagates_content(u8 m)
248 return content_features(m).sunlight_propagates;
249 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
253 On a node-node surface, the material of the node with higher solidness
258 NOTE: Don't use, use "content_features(m).whatever" instead
260 inline u8 content_solidness(u8 m)
262 return content_features(m).solidness;
263 /*// As of now, every pseudo node like torches are added to this
264 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
266 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
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;
276 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
279 // NOTE: Don't use, use "content_features(m).whatever" instead
280 inline bool content_liquid(u8 m)
282 return content_features(m).liquid_type != LIQUID_NONE;
283 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
286 // NOTE: Don't use, use "content_features(m).whatever" instead
287 inline bool content_flowing_liquid(u8 m)
289 return content_features(m).liquid_type == LIQUID_FLOWING;
290 //return (m == CONTENT_WATER);
293 // NOTE: Don't use, use "content_features(m).whatever" instead
294 inline bool content_liquid_source(u8 m)
296 return content_features(m).liquid_type == LIQUID_SOURCE;
297 //return (m == CONTENT_WATERSOURCE);
300 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
301 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
302 inline u8 make_liquid_flowing(u8 m)
304 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
305 return CONTENT_WATER;
309 // Pointable contents can be pointed to in the map
310 // NOTE: Don't use, use "content_features(m).whatever" instead
311 inline bool content_pointable(u8 m)
313 return content_features(m).pointable;
314 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
317 // NOTE: Don't use, use "content_features(m).whatever" instead
318 inline bool content_diggable(u8 m)
320 return content_features(m).diggable;
321 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
324 // NOTE: Don't use, use "content_features(m).whatever" instead
325 inline bool content_buildable_to(u8 m)
327 return content_features(m).buildable_to;
328 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
332 Returns true for contents that form the base ground that
333 follows the main heightmap
335 /*inline bool is_ground_content(u8 m)
337 return content_features(m).is_ground_content;
341 Nodes make a face if contents differ and solidness differs.
344 1: Face uses m1's content
345 2: Face uses m2's content
347 inline u8 face_contents(u8 m1, u8 m2)
349 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
352 bool contents_differ = (m1 != m2);
354 // Contents don't differ for different forms of same liquid
355 if(content_liquid(m1) && content_liquid(m2)
356 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
357 contents_differ = false;
359 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
360 bool makes_face = contents_differ && solidness_differs;
362 if(makes_face == false)
365 if(content_solidness(m1) > content_solidness(m2))
372 Packs directions like (1,0,0), (1,-1,0)
374 inline u8 packDir(v3s16 dir)
395 inline v3s16 unpackDir(u8 b)
418 facedir: CPT_FACEDIR_SIMPLE param1 value
419 dir: The face for which stuff is wanted
420 return value: The face from which the stuff is actually found
422 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
431 This is the stuff what the whole world consists of.
440 Misc parameter. Initialized to 0.
441 - For light_propagates() blocks, this is light intensity,
442 stored logarithmically from 0 to LIGHT_MAX.
443 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
444 - Contains 2 values, day- and night lighting. Each takes 4 bits.
453 The second parameter. Initialized to 0.
454 E.g. direction for torches and flowing water.
462 MapNode(const MapNode & n)
467 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
474 /*MapNode & operator=(const MapNode &other)
478 param2 = other.param2;
482 bool operator==(const MapNode &other)
485 && param == other.param
486 && param2 == other.param2);
489 bool light_propagates()
491 return light_propagates_content(d);
494 bool sunlight_propagates()
496 return sunlight_propagates_content(d);
501 return content_solidness(d);
507 Note that a block that isn't light_propagates() can be a light source.
509 if(d == CONTENT_TORCH)
515 u8 getLightBanksWithSource()
517 // Select the brightest of [light source, propagated light]
520 if(content_features(d).param_type == CPT_LIGHT)
522 lightday = param & 0x0f;
523 lightnight = (param>>4)&0x0f;
525 if(light_source() > lightday)
526 lightday = light_source();
527 if(light_source() > lightnight)
528 lightnight = light_source();
529 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
532 void setLightBanks(u8 a_light)
537 u8 getLight(enum LightBank bank)
539 // Select the brightest of [light source, propagated light]
541 if(content_features(d).param_type == CPT_LIGHT)
543 if(bank == LIGHTBANK_DAY)
544 light = param & 0x0f;
545 else if(bank == LIGHTBANK_NIGHT)
546 light = (param>>4)&0x0f;
550 if(light_source() > light)
551 light = light_source();
555 // 0 <= daylight_factor <= 1000
556 // 0 <= return value <= LIGHT_SUN
557 u8 getLightBlend(u32 daylight_factor)
559 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
560 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
563 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
569 /*// 0 <= daylight_factor <= 1000
570 // 0 <= return value <= 255
571 u8 getLightBlend(u32 daylight_factor)
573 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
574 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
575 u8 mix = ((daylight_factor * daylight
576 + (1000-daylight_factor) * nightlight)
581 void setLight(enum LightBank bank, u8 a_light)
583 // If node doesn't contain light data, ignore this
584 if(content_features(d).param_type != CPT_LIGHT)
586 if(bank == LIGHTBANK_DAY)
589 param |= a_light & 0x0f;
591 else if(bank == LIGHTBANK_NIGHT)
594 param |= (a_light & 0x0f)<<4;
601 TileSpec getTile(v3s16 dir);
606 These serialization functions are used when informing client
610 static u32 serializedLength(u8 version)
612 if(!ser_ver_supported(version))
613 throw VersionMismatchException("ERROR: MapNode format not supported");
617 else if(version <= 9)
622 void serialize(u8 *dest, u8 version)
624 if(!ser_ver_supported(version))
625 throw VersionMismatchException("ERROR: MapNode format not supported");
631 else if(version <= 9)
643 void deSerialize(u8 *source, u8 version)
645 if(!ser_ver_supported(version))
646 throw VersionMismatchException("ERROR: MapNode format not supported");
652 else if(version == 1)
655 // This version doesn't support saved lighting
656 if(light_propagates() || light_source() > 0)
661 else if(version <= 9)
673 // Translate deprecated stuff
674 // NOTE: This doesn't get used because MapBlock handles node
675 // parameters directly
676 MapNode *translate_to = content_features(d).translate_to;
679 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
680 <<translate_to->d<<std::endl;
681 *this = *translate_to;