3 Copyright (C) 2010 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"
31 // Initializes all kind of stuff in here.
32 // Doesn't depend on anything else.
33 // Many things depend on this.
36 // Initializes g_content_inventory_texture_paths
37 void init_content_inventory_texture_paths();
40 // NOTE: This is not used appropriately everywhere.
41 #define MATERIALS_COUNT 256
46 Anything that stores MapNodes doesn't have to preserve parameters
47 associated with this material.
49 Doesn't create faces with anything and is considered being
50 out-of-map in the game map.
52 #define CONTENT_IGNORE 255
53 #define CONTENT_IGNORE_DEFAULT_PARAM 0
56 The common material through which the player can walk and which
57 is transparent to light
59 #define CONTENT_AIR 254
67 - Material = irrlicht's Material class
68 - Content = (u8) content of a node
69 - Tile = (u16) Material ID at some side of a node
72 #define CONTENT_STONE 0
73 #define CONTENT_GRASS 1
74 #define CONTENT_WATER 2
75 #define CONTENT_TORCH 3
76 #define CONTENT_TREE 4
77 #define CONTENT_LEAVES 5
78 #define CONTENT_GRASS_FOOTSTEPS 6
79 #define CONTENT_MESE 7
81 #define CONTENT_WATERSOURCE 9
82 // Pretty much useless, clouds won't be drawn this way
83 #define CONTENT_CLOUD 10
84 #define CONTENT_COALSTONE 11
85 #define CONTENT_WOOD 12
86 #define CONTENT_SAND 13
89 This is used by all kinds of things to allocate memory for all
90 contents except CONTENT_AIR and CONTENT_IGNORE
92 #define USEFUL_CONTENT_COUNT 14
114 struct ContentFeatures
116 // If non-NULL, content is translated to this when deserialized
117 MapNode *translate_to;
119 // Type of MapNode::param
120 ContentParamType param_type;
132 std::string inventory_image_path;
134 bool is_ground_content; //TODO: Remove, use walkable instead
135 bool light_propagates;
136 bool sunlight_propagates;
137 u8 solidness; // Used when choosing which face is drawn
142 enum LiquidType liquid_type;
143 bool wall_mounted; // If true, param2 is set to direction when placed
145 //TODO: Move more properties here
150 param_type = CPT_NONE;
151 is_ground_content = false;
152 light_propagates = false;
153 sunlight_propagates = false;
158 buildable_to = false;
159 liquid_type = LIQUID_NONE;
160 wall_mounted = false;
165 void setAllTextures(std::string imgname, u8 alpha=255)
167 for(u16 i=0; i<6; i++)
169 tiles[i].name = porting::getDataPath(imgname.c_str());
170 tiles[i].alpha = alpha;
173 // Set this too so it can be left as is most times
174 if(inventory_image_path == "")
175 inventory_image_path = porting::getDataPath(imgname.c_str());
177 void setTexture(u16 i, std::string imgname, u8 alpha=255)
179 tiles[i].name = porting::getDataPath(imgname.c_str());
180 tiles[i].alpha = alpha;
183 void setInventoryImage(std::string imgname)
185 inventory_image_path = porting::getDataPath(imgname.c_str());
189 // Initialized by init_mapnode()
190 extern struct ContentFeatures g_content_features[256];
192 inline ContentFeatures & content_features(u8 i)
194 return g_content_features[i];
197 extern const char * g_content_inventory_texture_paths[USEFUL_CONTENT_COUNT];
200 If true, the material allows light propagation and brightness is stored
202 NOTE: Don't use, use "content_features(m).whatever" instead
204 inline bool light_propagates_content(u8 m)
206 return g_content_features[m].light_propagates;
207 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
211 If true, the material allows lossless sunlight propagation.
212 NOTE: It doesn't seem to go through torches regardlessly of this
213 NOTE: Don't use, use "content_features(m).whatever" instead
215 inline bool sunlight_propagates_content(u8 m)
217 return g_content_features[m].sunlight_propagates;
218 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
222 On a node-node surface, the material of the node with higher solidness
227 NOTE: Don't use, use "content_features(m).whatever" instead
229 inline u8 content_solidness(u8 m)
231 return g_content_features[m].solidness;
232 /*// As of now, every pseudo node like torches are added to this
233 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
235 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
240 // Objects collide with walkable contents
241 // NOTE: Don't use, use "content_features(m).whatever" instead
242 inline bool content_walkable(u8 m)
244 return g_content_features[m].walkable;
245 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
248 // NOTE: Don't use, use "content_features(m).whatever" instead
249 inline bool content_liquid(u8 m)
251 return g_content_features[m].liquid_type != LIQUID_NONE;
252 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
255 // NOTE: Don't use, use "content_features(m).whatever" instead
256 inline bool content_flowing_liquid(u8 m)
258 return g_content_features[m].liquid_type == LIQUID_FLOWING;
259 //return (m == CONTENT_WATER);
262 // NOTE: Don't use, use "content_features(m).whatever" instead
263 inline bool content_liquid_source(u8 m)
265 return g_content_features[m].liquid_type == LIQUID_SOURCE;
266 //return (m == CONTENT_WATERSOURCE);
269 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
270 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
271 inline u8 make_liquid_flowing(u8 m)
273 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
274 return CONTENT_WATER;
278 // Pointable contents can be pointed to in the map
279 // NOTE: Don't use, use "content_features(m).whatever" instead
280 inline bool content_pointable(u8 m)
282 return g_content_features[m].pointable;
283 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
286 // NOTE: Don't use, use "content_features(m).whatever" instead
287 inline bool content_diggable(u8 m)
289 return g_content_features[m].diggable;
290 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
293 // NOTE: Don't use, use "content_features(m).whatever" instead
294 inline bool content_buildable_to(u8 m)
296 return g_content_features[m].buildable_to;
297 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
301 Returns true for contents that form the base ground that
302 follows the main heightmap
304 /*inline bool is_ground_content(u8 m)
306 return g_content_features[m].is_ground_content;
310 Nodes make a face if contents differ and solidness differs.
313 1: Face uses m1's content
314 2: Face uses m2's content
316 inline u8 face_contents(u8 m1, u8 m2)
318 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
321 bool contents_differ = (m1 != m2);
323 // Contents don't differ for different forms of same liquid
324 if(content_liquid(m1) && content_liquid(m2)
325 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
326 contents_differ = false;
328 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
329 bool makes_face = contents_differ && solidness_differs;
331 if(makes_face == false)
334 if(content_solidness(m1) > content_solidness(m2))
341 Packs directions like (1,0,0), (1,-1,0)
343 inline u8 packDir(v3s16 dir)
364 inline v3s16 unpackDir(u8 b)
393 This is the stuff what the whole world consists of.
402 Misc parameter. Initialized to 0.
403 - For light_propagates() blocks, this is light intensity,
404 stored logarithmically from 0 to LIGHT_MAX.
405 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
406 - Contains 2 values, day- and night lighting. Each takes 4 bits.
415 Direction for torches and other stuff.
416 Format is freeform. e.g. packDir or encode_dirs can be used.
421 MapNode(const MapNode & n)
426 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
433 bool operator==(const MapNode &other)
436 && param == other.param
437 && param2 == other.param2);
440 bool light_propagates()
442 return light_propagates_content(d);
445 bool sunlight_propagates()
447 return sunlight_propagates_content(d);
452 return content_solidness(d);
458 Note that a block that isn't light_propagates() can be a light source.
460 if(d == CONTENT_TORCH)
466 u8 getLightBanksWithSource()
468 // Select the brightest of [light source, propagated light]
471 if(light_propagates())
473 lightday = param & 0x0f;
474 lightnight = (param>>4)&0x0f;
476 if(light_source() > lightday)
477 lightday = light_source();
478 if(light_source() > lightnight)
479 lightnight = light_source();
480 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
483 void setLightBanks(u8 a_light)
488 u8 getLight(enum LightBank bank)
490 // Select the brightest of [light source, propagated light]
492 if(light_propagates())
494 if(bank == LIGHTBANK_DAY)
495 light = param & 0x0f;
496 else if(bank == LIGHTBANK_NIGHT)
497 light = (param>>4)&0x0f;
501 if(light_source() > light)
502 light = light_source();
506 // 0 <= daylight_factor <= 1000
507 // 0 <= return value <= LIGHT_SUN
508 u8 getLightBlend(u32 daylight_factor)
510 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
511 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
514 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
520 /*// 0 <= daylight_factor <= 1000
521 // 0 <= return value <= 255
522 u8 getLightBlend(u32 daylight_factor)
524 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
525 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
526 u8 mix = ((daylight_factor * daylight
527 + (1000-daylight_factor) * nightlight)
532 void setLight(enum LightBank bank, u8 a_light)
534 // If not transparent, can't set light
535 if(light_propagates() == false)
537 if(bank == LIGHTBANK_DAY)
540 param |= a_light & 0x0f;
542 else if(bank == LIGHTBANK_NIGHT)
545 param |= (a_light & 0x0f)<<4;
552 TileSpec getTile(v3s16 dir);
557 These serialization functions are used when informing client
561 static u32 serializedLength(u8 version)
563 if(!ser_ver_supported(version))
564 throw VersionMismatchException("ERROR: MapNode format not supported");
568 else if(version <= 9)
573 void serialize(u8 *dest, u8 version)
575 if(!ser_ver_supported(version))
576 throw VersionMismatchException("ERROR: MapNode format not supported");
582 else if(version <= 9)
594 void deSerialize(u8 *source, u8 version)
596 if(!ser_ver_supported(version))
597 throw VersionMismatchException("ERROR: MapNode format not supported");
603 else if(version == 1)
606 // This version doesn't support saved lighting
607 if(light_propagates() || light_source() > 0)
612 else if(version <= 9)
624 // Translate deprecated stuff
625 MapNode *translate_to = g_content_features[d].translate_to;
628 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
629 <<translate_to->d<<std::endl;
630 *this = *translate_to;
636 Returns integer position of the node in given
637 floating point position.
639 inline v3s16 floatToInt(v3f p)
642 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
643 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
644 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
649 The same thing backwards
651 inline v3f intToFloat(v3s16 p)