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 irrlicht: Used for getting texture ids.
38 void init_mapnode(IIrrlichtWrapper *irrlicht);
40 // Initializes g_content_inventory_texture_paths
41 void init_content_inventory_texture_paths();
44 // NOTE: This is not used appropriately everywhere.
45 #define MATERIALS_COUNT 256
50 Anything that stores MapNodes doesn't have to preserve parameters
51 associated with this material.
53 Doesn't create faces with anything and is considered being
54 out-of-map in the game map.
56 #define CONTENT_IGNORE 255
57 #define CONTENT_IGNORE_DEFAULT_PARAM 0
60 The common material through which the player can walk and which
61 is transparent to light
63 #define CONTENT_AIR 254
71 - Material = irrlicht's Material class
72 - Content = (u8) content of a node
73 - Tile = (u16) Material ID at some side of a node
76 #define CONTENT_STONE 0
77 #define CONTENT_GRASS 1
78 #define CONTENT_WATER 2
79 #define CONTENT_TORCH 3
80 #define CONTENT_TREE 4
81 #define CONTENT_LEAVES 5
82 #define CONTENT_GRASS_FOOTSTEPS 6
83 #define CONTENT_MESE 7
85 #define CONTENT_WATERSOURCE 9
86 // Pretty much useless, clouds won't be drawn this way
87 #define CONTENT_CLOUD 10
88 #define CONTENT_COALSTONE 11
89 #define CONTENT_WOOD 12
90 #define CONTENT_SAND 13
93 This is used by all kinds of things to allocate memory for all
94 contents except CONTENT_AIR and CONTENT_IGNORE
96 #define USEFUL_CONTENT_COUNT 14
102 enum ContentParamType
118 struct ContentFeatures
120 // If non-NULL, content is translated to this when deserialized
121 MapNode *translate_to;
123 // Type of MapNode::param
124 ContentParamType param_type;
136 // TODO: Somehow specify inventory image
137 //std::string inventory_image_path;
138 //TextureSpec inventory_texture;
139 //u32 inventory_texture_id;
141 bool is_ground_content; //TODO: Remove, use walkable instead
142 bool light_propagates;
143 bool sunlight_propagates;
144 u8 solidness; // Used when choosing which face is drawn
149 enum LiquidType liquid_type;
150 bool wall_mounted; // If true, param2 is set to direction when placed
152 //TODO: Move more properties here
157 param_type = CPT_NONE;
158 is_ground_content = false;
159 light_propagates = false;
160 sunlight_propagates = false;
165 buildable_to = false;
166 liquid_type = LIQUID_NONE;
167 wall_mounted = false;
173 Quickhands for simple materials
176 void setTexture(u16 i, std::string name, u8 alpha=255);
178 void setAllTextures(std::string name, u8 alpha=255)
180 for(u16 i=0; i<6; i++)
182 setTexture(i, name, alpha);
186 /*void setTexture(u16 i, AtlasPointer p, u8 alpha=255)
188 tiles[i].texture = p;
191 tiles[i].alpha = alpha;
192 tiles[i].material_type = MATERIAL_ALPHA_VERTEX;
195 void setAllTextures(AtlasPointer p, u8 alpha=255)
197 for(u16 i=0; i<6; i++)
199 setTexture(i, p, alpha);
203 void setTile(u16 i, const TileSpec &tile)
207 void setAllTiles(const TileSpec &tile)
209 for(u16 i=0; i<6; i++)
215 /*void setInventoryTexture(const TextureSpec &spec)
217 inventory_texture = spec;
220 /*void setInventoryImage(std::string imgname)
222 inventory_image_path = porting::getDataPath(imgname.c_str());
227 Call this to access the ContentFeature list
229 ContentFeatures & content_features(u8 i);
232 If true, the material allows light propagation and brightness is stored
234 NOTE: Don't use, use "content_features(m).whatever" instead
236 inline bool light_propagates_content(u8 m)
238 return content_features(m).light_propagates;
239 //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
243 If true, the material allows lossless sunlight propagation.
244 NOTE: It doesn't seem to go through torches regardlessly of this
245 NOTE: Don't use, use "content_features(m).whatever" instead
247 inline bool sunlight_propagates_content(u8 m)
249 return content_features(m).sunlight_propagates;
250 //return (m == CONTENT_AIR || m == CONTENT_TORCH);
254 On a node-node surface, the material of the node with higher solidness
259 NOTE: Don't use, use "content_features(m).whatever" instead
261 inline u8 content_solidness(u8 m)
263 return content_features(m).solidness;
264 /*// As of now, every pseudo node like torches are added to this
265 if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
267 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
272 // Objects collide with walkable contents
273 // NOTE: Don't use, use "content_features(m).whatever" instead
274 inline bool content_walkable(u8 m)
276 return content_features(m).walkable;
277 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
280 // NOTE: Don't use, use "content_features(m).whatever" instead
281 inline bool content_liquid(u8 m)
283 return content_features(m).liquid_type != LIQUID_NONE;
284 //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
287 // NOTE: Don't use, use "content_features(m).whatever" instead
288 inline bool content_flowing_liquid(u8 m)
290 return content_features(m).liquid_type == LIQUID_FLOWING;
291 //return (m == CONTENT_WATER);
294 // NOTE: Don't use, use "content_features(m).whatever" instead
295 inline bool content_liquid_source(u8 m)
297 return content_features(m).liquid_type == LIQUID_SOURCE;
298 //return (m == CONTENT_WATERSOURCE);
301 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
302 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
303 inline u8 make_liquid_flowing(u8 m)
305 if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
306 return CONTENT_WATER;
310 // Pointable contents can be pointed to in the map
311 // NOTE: Don't use, use "content_features(m).whatever" instead
312 inline bool content_pointable(u8 m)
314 return content_features(m).pointable;
315 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
318 // NOTE: Don't use, use "content_features(m).whatever" instead
319 inline bool content_diggable(u8 m)
321 return content_features(m).diggable;
322 //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
325 // NOTE: Don't use, use "content_features(m).whatever" instead
326 inline bool content_buildable_to(u8 m)
328 return content_features(m).buildable_to;
329 //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
333 Returns true for contents that form the base ground that
334 follows the main heightmap
336 /*inline bool is_ground_content(u8 m)
338 return content_features(m).is_ground_content;
342 Nodes make a face if contents differ and solidness differs.
345 1: Face uses m1's content
346 2: Face uses m2's content
348 inline u8 face_contents(u8 m1, u8 m2)
350 if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
353 bool contents_differ = (m1 != m2);
355 // Contents don't differ for different forms of same liquid
356 if(content_liquid(m1) && content_liquid(m2)
357 && make_liquid_flowing(m1) == make_liquid_flowing(m2))
358 contents_differ = false;
360 bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
361 bool makes_face = contents_differ && solidness_differs;
363 if(makes_face == false)
366 if(content_solidness(m1) > content_solidness(m2))
373 Packs directions like (1,0,0), (1,-1,0)
375 inline u8 packDir(v3s16 dir)
396 inline v3s16 unpackDir(u8 b)
425 This is the stuff what the whole world consists of.
434 Misc parameter. Initialized to 0.
435 - For light_propagates() blocks, this is light intensity,
436 stored logarithmically from 0 to LIGHT_MAX.
437 Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
438 - Contains 2 values, day- and night lighting. Each takes 4 bits.
445 The second parameter. Initialized to 0.
446 Direction for torches and flowing water.
452 MapNode(const MapNode & n)
457 MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
464 /*MapNode & operator=(const MapNode &other)
468 param2 = other.param2;
472 bool operator==(const MapNode &other)
475 && param == other.param
476 && param2 == other.param2);
479 bool light_propagates()
481 return light_propagates_content(d);
484 bool sunlight_propagates()
486 return sunlight_propagates_content(d);
491 return content_solidness(d);
497 Note that a block that isn't light_propagates() can be a light source.
499 if(d == CONTENT_TORCH)
505 u8 getLightBanksWithSource()
507 // Select the brightest of [light source, propagated light]
510 if(light_propagates())
512 lightday = param & 0x0f;
513 lightnight = (param>>4)&0x0f;
515 if(light_source() > lightday)
516 lightday = light_source();
517 if(light_source() > lightnight)
518 lightnight = light_source();
519 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
522 void setLightBanks(u8 a_light)
527 u8 getLight(enum LightBank bank)
529 // Select the brightest of [light source, propagated light]
531 if(light_propagates())
533 if(bank == LIGHTBANK_DAY)
534 light = param & 0x0f;
535 else if(bank == LIGHTBANK_NIGHT)
536 light = (param>>4)&0x0f;
540 if(light_source() > light)
541 light = light_source();
545 // 0 <= daylight_factor <= 1000
546 // 0 <= return value <= LIGHT_SUN
547 u8 getLightBlend(u32 daylight_factor)
549 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
550 + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
553 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
559 /*// 0 <= daylight_factor <= 1000
560 // 0 <= return value <= 255
561 u8 getLightBlend(u32 daylight_factor)
563 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
564 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
565 u8 mix = ((daylight_factor * daylight
566 + (1000-daylight_factor) * nightlight)
571 void setLight(enum LightBank bank, u8 a_light)
573 // If not transparent, can't set light
574 if(light_propagates() == false)
576 if(bank == LIGHTBANK_DAY)
579 param |= a_light & 0x0f;
581 else if(bank == LIGHTBANK_NIGHT)
584 param |= (a_light & 0x0f)<<4;
591 TileSpec getTile(v3s16 dir);
596 These serialization functions are used when informing client
600 static u32 serializedLength(u8 version)
602 if(!ser_ver_supported(version))
603 throw VersionMismatchException("ERROR: MapNode format not supported");
607 else if(version <= 9)
612 void serialize(u8 *dest, u8 version)
614 if(!ser_ver_supported(version))
615 throw VersionMismatchException("ERROR: MapNode format not supported");
621 else if(version <= 9)
633 void deSerialize(u8 *source, u8 version)
635 if(!ser_ver_supported(version))
636 throw VersionMismatchException("ERROR: MapNode format not supported");
642 else if(version == 1)
645 // This version doesn't support saved lighting
646 if(light_propagates() || light_source() > 0)
651 else if(version <= 9)
663 // Translate deprecated stuff
664 // NOTE: This doesn't get used because MapBlock handles node
665 // parameters directly
666 MapNode *translate_to = content_features(d).translate_to;
669 dstream<<"MapNode: WARNING: Translating "<<d<<" to "
670 <<translate_to->d<<std::endl;
671 *this = *translate_to;
677 Returns integer position of the node in given
678 floating point position.
680 inline v3s16 floatToInt(v3f p)
683 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
684 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
685 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
690 The same thing backwards
692 inline v3f intToFloat(v3s16 p)