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
104 #define CONTENT_SANDSTONE 22
105 #define CONTENT_CACTUS 23
106 #define CONTENT_BRICK 24
107 #define CONTENT_CLAY 25
108 #define CONTENT_PAPYRUS 26
109 #define CONTENT_BOOKSHELF 27
110 #define CONTENT_RAIL 28
116 enum ContentParamType
121 // Direction for chests and furnaces and such
135 struct ContentFeatures
137 // If non-NULL, content is translated to this when deserialized
138 MapNode *translate_to;
140 // Type of MapNode::param
141 ContentParamType param_type;
153 video::ITexture *inventory_texture;
155 bool is_ground_content;
156 bool light_propagates;
157 bool sunlight_propagates;
158 u8 solidness; // Used when choosing which face is drawn
159 // This is used for collision detection.
160 // Also for general solidness queries.
162 // Player can point to these
164 // Player can dig these
166 // Player can build on these
168 // Whether the node has no liquid, source liquid or flowing liquid
169 enum LiquidType liquid_type;
170 // If true, param2 is set to direction when placed. Used for torches.
171 // NOTE: the direction format is quite inefficient and should be changed
173 // If true, node is equivalent to air. Torches are, air is. Water is not.
174 // Is used for example to check whether a mud block can have grass on.
177 // Inventory item string as which the node appears in inventory when dug.
178 // Mineral overrides this.
179 std::string dug_item;
181 // Initial metadata is cloned from this
182 NodeMetadata *initial_metadata;
184 //TODO: Move more properties here
189 param_type = CPT_NONE;
190 inventory_texture = NULL;
191 is_ground_content = false;
192 light_propagates = false;
193 sunlight_propagates = false;
198 buildable_to = false;
199 liquid_type = LIQUID_NONE;
200 wall_mounted = false;
201 air_equivalent = false;
203 initial_metadata = NULL;
209 Quickhands for simple materials
212 void setTexture(u16 i, std::string name, u8 alpha=255);
214 void setAllTextures(std::string name, u8 alpha=255)
216 for(u16 i=0; i<6; i++)
218 setTexture(i, name, alpha);
222 void setTile(u16 i, const TileSpec &tile)
226 void setAllTiles(const TileSpec &tile)
228 for(u16 i=0; i<6; i++)
234 void setInventoryTexture(std::string imgname);
236 void setInventoryTextureCube(std::string top,
237 std::string left, std::string right);
241 Call this to access the ContentFeature list
243 ContentFeatures & content_features(u8 i);
246 If true, the material allows light propagation and brightness is stored
248 NOTE: Don't use, use "content_features(m).whatever" instead
250 inline bool light_propagates_content(u8 m)
252 return content_features(m).light_propagates;
253 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
257 If true, the material allows lossless sunlight propagation.
258 NOTE: It doesn't seem to go through torches regardlessly of this
259 NOTE: Don't use, use "content_features(m).whatever" instead
261 inline bool sunlight_propagates_content(u8 m)
263 return content_features(m).sunlight_propagates;
264 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
268 On a node-node surface, the material of the node with higher solidness
273 NOTE: Don't use, use "content_features(m).whatever" instead
275 inline u8 content_solidness(u8 m)
277 return content_features(m).solidness;
278 /*// As of now, every pseudo node like torches are added to this
279 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
281 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
286 // Objects collide with walkable contents
287 // NOTE: Don't use, use "content_features(m).whatever" instead
288 inline bool content_walkable(u8 m)
290 return content_features(m).walkable;
291 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
294 // NOTE: Don't use, use "content_features(m).whatever" instead
295 inline bool content_liquid(u8 m)
297 return content_features(m).liquid_type != LIQUID_NONE;
298 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
301 // NOTE: Don't use, use "content_features(m).whatever" instead
302 inline bool content_flowing_liquid(u8 m)
304 return content_features(m).liquid_type == LIQUID_FLOWING;
305 //return (m == CONTENT_WATER);
308 // NOTE: Don't use, use "content_features(m).whatever" instead
309 inline bool content_liquid_source(u8 m)
311 return content_features(m).liquid_type == LIQUID_SOURCE;
312 //return (m == CONTENT_WATERSOURCE);
315 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
316 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
317 inline u8 make_liquid_flowing(u8 m)
319 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
320 return CONTENT_WATER;
324 // Pointable contents can be pointed to in the map
325 // NOTE: Don't use, use "content_features(m).whatever" instead
326 inline bool content_pointable(u8 m)
328 return content_features(m).pointable;
329 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
332 // NOTE: Don't use, use "content_features(m).whatever" instead
333 inline bool content_diggable(u8 m)
335 return content_features(m).diggable;
336 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
339 // NOTE: Don't use, use "content_features(m).whatever" instead
340 inline bool content_buildable_to(u8 m)
342 return content_features(m).buildable_to;
343 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
347 Returns true for contents that form the base ground that
348 follows the main heightmap
350 /*inline bool is_ground_content(u8 m)
352 return content_features(m).is_ground_content;
356 Nodes make a face if contents differ and solidness differs.
359 1: Face uses m1's content
360 2: Face uses m2's content
362 inline u8 face_contents(u8 m1, u8 m2)
364 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
367 bool contents_differ = (m1 != m2);
369 // Contents don't differ for different forms of same liquid
370 if(content_liquid(m1) && content_liquid(m2)
371 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
372 contents_differ = false;
374 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
375 bool makes_face = contents_differ && solidness_differs;
377 if(makes_face == false)
380 if(content_solidness(m1) > content_solidness(m2))
387 Packs directions like (1,0,0), (1,-1,0)
389 inline u8 packDir(v3s16 dir)
410 inline v3s16 unpackDir(u8 b)
433 facedir: CPT_FACEDIR_SIMPLE param1 value
434 dir: The face for which stuff is wanted
435 return value: The face from which the stuff is actually found
437 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
446 This is the stuff what the whole world consists of.
455 Misc parameter. Initialized to 0.
456 - For light_propagates() blocks, this is light intensity,
457 stored logarithmically from 0 to LIGHT_MAX.
458 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
459 - Contains 2 values, day- and night lighting. Each takes 4 bits.
468 The second parameter. Initialized to 0.
469 E.g. direction for torches and flowing water.
477 MapNode(const MapNode & n)
482 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
489 /*MapNode & operator=(const MapNode &other)
493 param2 = other.param2;
497 bool operator==(const MapNode &other)
500 && param == other.param
501 && param2 == other.param2);
504 bool light_propagates()
506 return light_propagates_content(d);
509 bool sunlight_propagates()
511 return sunlight_propagates_content(d);
516 return content_solidness(d);
522 Note that a block that isn't light_propagates() can be a light source.
524 if(d == CONTENT_TORCH)
530 u8 getLightBanksWithSource()
532 // Select the brightest of [light source, propagated light]
535 if(content_features(d).param_type == CPT_LIGHT)
537 lightday = param & 0x0f;
538 lightnight = (param>>4)&0x0f;
540 if(light_source() > lightday)
541 lightday = light_source();
542 if(light_source() > lightnight)
543 lightnight = light_source();
544 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
547 void setLightBanks(u8 a_light)
552 u8 getLight(enum LightBank bank)
554 // Select the brightest of [light source, propagated light]
556 if(content_features(d).param_type == CPT_LIGHT)
558 if(bank == LIGHTBANK_DAY)
559 light = param & 0x0f;
560 else if(bank == LIGHTBANK_NIGHT)
561 light = (param>>4)&0x0f;
565 if(light_source() > light)
566 light = light_source();
570 // 0 <= daylight_factor <= 1000
571 // 0 <= return value <= LIGHT_SUN
572 u8 getLightBlend(u32 daylight_factor)
574 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
575 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
578 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
584 /*// 0 <= daylight_factor <= 1000
585 // 0 <= return value <= 255
586 u8 getLightBlend(u32 daylight_factor)
588 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
589 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
590 u8 mix = ((daylight_factor * daylight
591 + (1000-daylight_factor) * nightlight)
596 void setLight(enum LightBank bank, u8 a_light)
598 // If node doesn't contain light data, ignore this
599 if(content_features(d).param_type != CPT_LIGHT)
601 if(bank == LIGHTBANK_DAY)
604 param |= a_light & 0x0f;
606 else if(bank == LIGHTBANK_NIGHT)
609 param |= (a_light & 0x0f)<<4;
616 TileSpec getTile(v3s16 dir);
621 These serialization functions are used when informing client
625 static u32 serializedLength(u8 version)
627 if(!ser_ver_supported(version))
628 throw VersionMismatchException("ERROR: MapNode format not supported");
632 else if(version <= 9)
637 void serialize(u8 *dest, u8 version)
639 if(!ser_ver_supported(version))
640 throw VersionMismatchException("ERROR: MapNode format not supported");
646 else if(version <= 9)
658 void deSerialize(u8 *source, u8 version)
660 if(!ser_ver_supported(version))
661 throw VersionMismatchException("ERROR: MapNode format not supported");
667 else if(version == 1)
670 // This version doesn't support saved lighting
671 if(light_propagates() || light_source() > 0)
676 else if(version <= 9)
688 // Translate deprecated stuff
689 // NOTE: This doesn't get used because MapBlock handles node
690 // parameters directly
691 MapNode *translate_to = content_features(d).translate_to;
694 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
695 <<translate_to->d<<std::endl;
696 *this = *translate_to;