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 "iirrlichtwrapper.h"
33 Initializes all kind of stuff in here.
34 Many things depend on this.
36 This accesses g_texturesource; if it is non-NULL, textures are set.
38 Client first calls this with g_texturesource=NULL to run some
39 unit tests and stuff, then it runs this again with g_texturesource
40 defined to get the textures.
42 Server only calls this once with g_texturesource=NULL.
46 // Initializes g_content_inventory_texture_paths
47 void init_content_inventory_texture_paths();
50 // NOTE: This is not used appropriately everywhere.
51 #define MATERIALS_COUNT 256
56 Anything that stores MapNodes doesn't have to preserve parameters
57 associated with this material.
59 Doesn't create faces with anything and is considered being
60 out-of-map in the game map.
62 #define CONTENT_IGNORE 255
63 #define CONTENT_IGNORE_DEFAULT_PARAM 0
66 The common material through which the player can walk and which
67 is transparent to light
69 #define CONTENT_AIR 254
77 - Material = irrlicht's Material class
78 - Content = (u8) content of a node
79 - Tile = (u16) Material ID at some side of a node
82 #define CONTENT_STONE 0
83 #define CONTENT_GRASS 1
84 #define CONTENT_WATER 2
85 #define CONTENT_TORCH 3
86 #define CONTENT_TREE 4
87 #define CONTENT_LEAVES 5
88 #define CONTENT_GRASS_FOOTSTEPS 6
89 #define CONTENT_MESE 7
91 #define CONTENT_WATERSOURCE 9
92 // Pretty much useless, clouds won't be drawn this way
93 #define CONTENT_CLOUD 10
94 #define CONTENT_COALSTONE 11
95 #define CONTENT_WOOD 12
96 #define CONTENT_SAND 13
97 #define CONTENT_SIGN_WALL 14
98 #define CONTENT_CHEST 15
99 #define CONTENT_FURNACE 16
100 //#define CONTENT_WORKBENCH 17
101 #define CONTENT_COBBLE 18
102 #define CONTENT_STEEL 19
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
155 enum LiquidType liquid_type;
156 // If true, param2 is set to direction when placed
157 // NOTE: the direction format is quite inefficient and should be changed
160 // Inventory item string as which the node appears in inventory when dug.
161 // Mineral overrides this.
162 std::string dug_item;
164 // Initial metadata is cloned from this
165 NodeMetadata *initial_metadata;
167 //TODO: Move more properties here
172 param_type = CPT_NONE;
173 inventory_texture = NULL;
174 is_ground_content = false;
175 light_propagates = false;
176 sunlight_propagates = false;
181 buildable_to = false;
182 liquid_type = LIQUID_NONE;
183 wall_mounted = false;
185 initial_metadata = NULL;
191 Quickhands for simple materials
194 void setTexture(u16 i, std::string name, u8 alpha=255);
196 void setAllTextures(std::string name, u8 alpha=255)
198 for(u16 i=0; i<6; i++)
200 setTexture(i, name, alpha);
204 void setTile(u16 i, const TileSpec &tile)
208 void setAllTiles(const TileSpec &tile)
210 for(u16 i=0; i<6; i++)
216 void setInventoryTexture(std::string imgname);
218 void setInventoryTextureCube(std::string top,
219 std::string left, std::string right);
223 Call this to access the ContentFeature list
225 ContentFeatures & content_features(u8 i);
228 If true, the material allows light propagation and brightness is stored
230 NOTE: Don't use, use "content_features(m).whatever" instead
232 inline bool light_propagates_content(u8 m)
234 return content_features(m).light_propagates;
235 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
239 If true, the material allows lossless sunlight propagation.
240 NOTE: It doesn't seem to go through torches regardlessly of this
241 NOTE: Don't use, use "content_features(m).whatever" instead
243 inline bool sunlight_propagates_content(u8 m)
245 return content_features(m).sunlight_propagates;
246 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
250 On a node-node surface, the material of the node with higher solidness
255 NOTE: Don't use, use "content_features(m).whatever" instead
257 inline u8 content_solidness(u8 m)
259 return content_features(m).solidness;
260 /*// As of now, every pseudo node like torches are added to this
261 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
263 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
268 // Objects collide with walkable contents
269 // NOTE: Don't use, use "content_features(m).whatever" instead
270 inline bool content_walkable(u8 m)
272 return content_features(m).walkable;
273 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
276 // NOTE: Don't use, use "content_features(m).whatever" instead
277 inline bool content_liquid(u8 m)
279 return content_features(m).liquid_type != LIQUID_NONE;
280 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
283 // NOTE: Don't use, use "content_features(m).whatever" instead
284 inline bool content_flowing_liquid(u8 m)
286 return content_features(m).liquid_type == LIQUID_FLOWING;
287 //return (m == CONTENT_WATER);
290 // NOTE: Don't use, use "content_features(m).whatever" instead
291 inline bool content_liquid_source(u8 m)
293 return content_features(m).liquid_type == LIQUID_SOURCE;
294 //return (m == CONTENT_WATERSOURCE);
297 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
298 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
299 inline u8 make_liquid_flowing(u8 m)
301 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
302 return CONTENT_WATER;
306 // Pointable contents can be pointed to in the map
307 // NOTE: Don't use, use "content_features(m).whatever" instead
308 inline bool content_pointable(u8 m)
310 return content_features(m).pointable;
311 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
314 // NOTE: Don't use, use "content_features(m).whatever" instead
315 inline bool content_diggable(u8 m)
317 return content_features(m).diggable;
318 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
321 // NOTE: Don't use, use "content_features(m).whatever" instead
322 inline bool content_buildable_to(u8 m)
324 return content_features(m).buildable_to;
325 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
329 Returns true for contents that form the base ground that
330 follows the main heightmap
332 /*inline bool is_ground_content(u8 m)
334 return content_features(m).is_ground_content;
338 Nodes make a face if contents differ and solidness differs.
341 1: Face uses m1's content
342 2: Face uses m2's content
344 inline u8 face_contents(u8 m1, u8 m2)
346 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
349 bool contents_differ = (m1 != m2);
351 // Contents don't differ for different forms of same liquid
352 if(content_liquid(m1) && content_liquid(m2)
353 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
354 contents_differ = false;
356 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
357 bool makes_face = contents_differ && solidness_differs;
359 if(makes_face == false)
362 if(content_solidness(m1) > content_solidness(m2))
369 Packs directions like (1,0,0), (1,-1,0)
371 inline u8 packDir(v3s16 dir)
392 inline v3s16 unpackDir(u8 b)
415 facedir: CPT_FACEDIR_SIMPLE param1 value
416 dir: The face for which stuff is wanted
417 return value: The face from which the stuff is actually found
419 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
428 This is the stuff what the whole world consists of.
437 Misc parameter. Initialized to 0.
438 - For light_propagates() blocks, this is light intensity,
439 stored logarithmically from 0 to LIGHT_MAX.
440 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
441 - Contains 2 values, day- and night lighting. Each takes 4 bits.
450 The second parameter. Initialized to 0.
451 E.g. direction for torches and flowing water.
459 MapNode(const MapNode & n)
464 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
471 /*MapNode & operator=(const MapNode &other)
475 param2 = other.param2;
479 bool operator==(const MapNode &other)
482 && param == other.param
483 && param2 == other.param2);
486 bool light_propagates()
488 return light_propagates_content(d);
491 bool sunlight_propagates()
493 return sunlight_propagates_content(d);
498 return content_solidness(d);
504 Note that a block that isn't light_propagates() can be a light source.
506 if(d == CONTENT_TORCH)
512 u8 getLightBanksWithSource()
514 // Select the brightest of [light source, propagated light]
517 if(light_propagates())
519 lightday = param & 0x0f;
520 lightnight = (param>>4)&0x0f;
522 if(light_source() > lightday)
523 lightday = light_source();
524 if(light_source() > lightnight)
525 lightnight = light_source();
526 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
529 void setLightBanks(u8 a_light)
534 u8 getLight(enum LightBank bank)
536 // Select the brightest of [light source, propagated light]
538 if(light_propagates())
540 if(bank == LIGHTBANK_DAY)
541 light = param & 0x0f;
542 else if(bank == LIGHTBANK_NIGHT)
543 light = (param>>4)&0x0f;
547 if(light_source() > light)
548 light = light_source();
552 // 0 <= daylight_factor <= 1000
553 // 0 <= return value <= LIGHT_SUN
554 u8 getLightBlend(u32 daylight_factor)
556 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
557 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
560 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
566 /*// 0 <= daylight_factor <= 1000
567 // 0 <= return value <= 255
568 u8 getLightBlend(u32 daylight_factor)
570 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
571 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
572 u8 mix = ((daylight_factor * daylight
573 + (1000-daylight_factor) * nightlight)
578 void setLight(enum LightBank bank, u8 a_light)
580 // If not transparent, can't set light
581 if(light_propagates() == false)
583 if(bank == LIGHTBANK_DAY)
586 param |= a_light & 0x0f;
588 else if(bank == LIGHTBANK_NIGHT)
591 param |= (a_light & 0x0f)<<4;
598 TileSpec getTile(v3s16 dir);
603 These serialization functions are used when informing client
607 static u32 serializedLength(u8 version)
609 if(!ser_ver_supported(version))
610 throw VersionMismatchException("ERROR: MapNode format not supported");
614 else if(version <= 9)
619 void serialize(u8 *dest, u8 version)
621 if(!ser_ver_supported(version))
622 throw VersionMismatchException("ERROR: MapNode format not supported");
628 else if(version <= 9)
640 void deSerialize(u8 *source, u8 version)
642 if(!ser_ver_supported(version))
643 throw VersionMismatchException("ERROR: MapNode format not supported");
649 else if(version == 1)
652 // This version doesn't support saved lighting
653 if(light_propagates() || light_source() > 0)
658 else if(version <= 9)
670 // Translate deprecated stuff
671 // NOTE: This doesn't get used because MapBlock handles node
672 // parameters directly
673 MapNode *translate_to = content_features(d).translate_to;
676 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
677 <<translate_to->d<<std::endl;
678 *this = *translate_to;