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
108 enum ContentParamType
113 // Direction for chests and furnaces and such
127 struct ContentFeatures
129 // If non-NULL, content is translated to this when deserialized
130 MapNode *translate_to;
132 // Type of MapNode::param
133 ContentParamType param_type;
145 video::ITexture *inventory_texture;
147 bool is_ground_content;
148 bool light_propagates;
149 bool sunlight_propagates;
150 u8 solidness; // Used when choosing which face is drawn
151 // This is used for collision detection.
152 // Also for general solidness queries.
157 enum LiquidType liquid_type;
158 // If true, param2 is set to direction when placed
159 // NOTE: the direction format is quite inefficient and should be changed
162 // Inventory item string as which the node appears in inventory when dug.
163 // Mineral overrides this.
164 std::string dug_item;
166 // Initial metadata is cloned from this
167 NodeMetadata *initial_metadata;
169 //TODO: Move more properties here
174 param_type = CPT_NONE;
175 inventory_texture = NULL;
176 is_ground_content = false;
177 light_propagates = false;
178 sunlight_propagates = false;
183 buildable_to = false;
184 liquid_type = LIQUID_NONE;
185 wall_mounted = false;
187 initial_metadata = NULL;
193 Quickhands for simple materials
196 void setTexture(u16 i, std::string name, u8 alpha=255);
198 void setAllTextures(std::string name, u8 alpha=255)
200 for(u16 i=0; i<6; i++)
202 setTexture(i, name, alpha);
206 void setTile(u16 i, const TileSpec &tile)
210 void setAllTiles(const TileSpec &tile)
212 for(u16 i=0; i<6; i++)
218 void setInventoryTexture(std::string imgname);
220 void setInventoryTextureCube(std::string top,
221 std::string left, std::string right);
225 Call this to access the ContentFeature list
227 ContentFeatures & content_features(u8 i);
230 If true, the material allows light propagation and brightness is stored
232 NOTE: Don't use, use "content_features(m).whatever" instead
234 inline bool light_propagates_content(u8 m)
236 return content_features(m).light_propagates;
237 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
241 If true, the material allows lossless sunlight propagation.
242 NOTE: It doesn't seem to go through torches regardlessly of this
243 NOTE: Don't use, use "content_features(m).whatever" instead
245 inline bool sunlight_propagates_content(u8 m)
247 return content_features(m).sunlight_propagates;
248 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
252 On a node-node surface, the material of the node with higher solidness
257 NOTE: Don't use, use "content_features(m).whatever" instead
259 inline u8 content_solidness(u8 m)
261 return content_features(m).solidness;
262 /*// As of now, every pseudo node like torches are added to this
263 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
265 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
270 // Objects collide with walkable contents
271 // NOTE: Don't use, use "content_features(m).whatever" instead
272 inline bool content_walkable(u8 m)
274 return content_features(m).walkable;
275 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
278 // NOTE: Don't use, use "content_features(m).whatever" instead
279 inline bool content_liquid(u8 m)
281 return content_features(m).liquid_type != LIQUID_NONE;
282 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
285 // NOTE: Don't use, use "content_features(m).whatever" instead
286 inline bool content_flowing_liquid(u8 m)
288 return content_features(m).liquid_type == LIQUID_FLOWING;
289 //return (m == CONTENT_WATER);
292 // NOTE: Don't use, use "content_features(m).whatever" instead
293 inline bool content_liquid_source(u8 m)
295 return content_features(m).liquid_type == LIQUID_SOURCE;
296 //return (m == CONTENT_WATERSOURCE);
299 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
300 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
301 inline u8 make_liquid_flowing(u8 m)
303 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
304 return CONTENT_WATER;
308 // Pointable contents can be pointed to in the map
309 // NOTE: Don't use, use "content_features(m).whatever" instead
310 inline bool content_pointable(u8 m)
312 return content_features(m).pointable;
313 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
316 // NOTE: Don't use, use "content_features(m).whatever" instead
317 inline bool content_diggable(u8 m)
319 return content_features(m).diggable;
320 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
323 // NOTE: Don't use, use "content_features(m).whatever" instead
324 inline bool content_buildable_to(u8 m)
326 return content_features(m).buildable_to;
327 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
331 Returns true for contents that form the base ground that
332 follows the main heightmap
334 /*inline bool is_ground_content(u8 m)
336 return content_features(m).is_ground_content;
340 Nodes make a face if contents differ and solidness differs.
343 1: Face uses m1's content
344 2: Face uses m2's content
346 inline u8 face_contents(u8 m1, u8 m2)
348 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
351 bool contents_differ = (m1 != m2);
353 // Contents don't differ for different forms of same liquid
354 if(content_liquid(m1) && content_liquid(m2)
355 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
356 contents_differ = false;
358 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
359 bool makes_face = contents_differ && solidness_differs;
361 if(makes_face == false)
364 if(content_solidness(m1) > content_solidness(m2))
371 Packs directions like (1,0,0), (1,-1,0)
373 inline u8 packDir(v3s16 dir)
394 inline v3s16 unpackDir(u8 b)
417 facedir: CPT_FACEDIR_SIMPLE param1 value
418 dir: The face for which stuff is wanted
419 return value: The face from which the stuff is actually found
421 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
430 This is the stuff what the whole world consists of.
439 Misc parameter. Initialized to 0.
440 - For light_propagates() blocks, this is light intensity,
441 stored logarithmically from 0 to LIGHT_MAX.
442 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
443 - Contains 2 values, day- and night lighting. Each takes 4 bits.
452 The second parameter. Initialized to 0.
453 E.g. direction for torches and flowing water.
461 MapNode(const MapNode & n)
466 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
473 /*MapNode & operator=(const MapNode &other)
477 param2 = other.param2;
481 bool operator==(const MapNode &other)
484 && param == other.param
485 && param2 == other.param2);
488 bool light_propagates()
490 return light_propagates_content(d);
493 bool sunlight_propagates()
495 return sunlight_propagates_content(d);
500 return content_solidness(d);
506 Note that a block that isn't light_propagates() can be a light source.
508 if(d == CONTENT_TORCH)
514 u8 getLightBanksWithSource()
516 // Select the brightest of [light source, propagated light]
519 if(content_features(d).param_type == CPT_LIGHT)
521 lightday = param & 0x0f;
522 lightnight = (param>>4)&0x0f;
524 if(light_source() > lightday)
525 lightday = light_source();
526 if(light_source() > lightnight)
527 lightnight = light_source();
528 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
531 void setLightBanks(u8 a_light)
536 u8 getLight(enum LightBank bank)
538 // Select the brightest of [light source, propagated light]
540 if(content_features(d).param_type == CPT_LIGHT)
542 if(bank == LIGHTBANK_DAY)
543 light = param & 0x0f;
544 else if(bank == LIGHTBANK_NIGHT)
545 light = (param>>4)&0x0f;
549 if(light_source() > light)
550 light = light_source();
554 // 0 <= daylight_factor <= 1000
555 // 0 <= return value <= LIGHT_SUN
556 u8 getLightBlend(u32 daylight_factor)
558 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
559 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
562 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
568 /*// 0 <= daylight_factor <= 1000
569 // 0 <= return value <= 255
570 u8 getLightBlend(u32 daylight_factor)
572 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
573 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
574 u8 mix = ((daylight_factor * daylight
575 + (1000-daylight_factor) * nightlight)
580 void setLight(enum LightBank bank, u8 a_light)
582 // If node doesn't contain light data, ignore this
583 if(content_features(d).param_type != CPT_LIGHT)
585 if(bank == LIGHTBANK_DAY)
588 param |= a_light & 0x0f;
590 else if(bank == LIGHTBANK_NIGHT)
593 param |= (a_light & 0x0f)<<4;
600 TileSpec getTile(v3s16 dir);
605 These serialization functions are used when informing client
609 static u32 serializedLength(u8 version)
611 if(!ser_ver_supported(version))
612 throw VersionMismatchException("ERROR: MapNode format not supported");
616 else if(version <= 9)
621 void serialize(u8 *dest, u8 version)
623 if(!ser_ver_supported(version))
624 throw VersionMismatchException("ERROR: MapNode format not supported");
630 else if(version <= 9)
642 void deSerialize(u8 *source, u8 version)
644 if(!ser_ver_supported(version))
645 throw VersionMismatchException("ERROR: MapNode format not supported");
651 else if(version == 1)
654 // This version doesn't support saved lighting
655 if(light_propagates() || light_source() > 0)
660 else if(version <= 9)
672 // Translate deprecated stuff
673 // NOTE: This doesn't get used because MapBlock handles node
674 // parameters directly
675 MapNode *translate_to = content_features(d).translate_to;
678 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
679 <<translate_to->d<<std::endl;
680 *this = *translate_to;