e99407f860fcbbb38ee3612a0b7fda687b1a46f8
[oweals/minetest.git] / src / mapnode.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef MAPNODE_HEADER
21 #define MAPNODE_HEADER
22
23 #include <iostream>
24 #include "common_irrlicht.h"
25 #include "light.h"
26 #include "utility.h"
27 #include "exceptions.h"
28 #include "serialization.h"
29 #include "tile.h"
30 #include "materials.h"
31
32 /*
33         Naming scheme:
34         - Material = irrlicht's Material class
35         - Content = (content_t) content of a node
36         - Tile = TileSpec at some side of a node of some content type
37
38         Content ranges:
39                 0x000...0x07f: param2 is fully usable
40                 0x800...0xfff: param2 lower 4 bytes are free
41 */
42 typedef u16 content_t;
43 #define MAX_CONTENT 0xfff
44
45 /*
46         Initializes all kind of stuff in here.
47         Many things depend on this.
48
49         This accesses g_texturesource; if it is non-NULL, textures are set.
50
51         Client first calls this with g_texturesource=NULL to run some
52         unit tests and stuff, then it runs this again with g_texturesource
53         defined to get the textures.
54
55         Server only calls this once with g_texturesource=NULL.
56 */
57 void init_mapnode();
58
59 /*
60         Ignored node.
61
62         Anything that stores MapNodes doesn't have to preserve parameters
63         associated with this material.
64         
65         Doesn't create faces with anything and is considered being
66         out-of-map in the game map.
67 */
68 //#define CONTENT_IGNORE 255
69 #define CONTENT_IGNORE 127
70 #define CONTENT_IGNORE_DEFAULT_PARAM 0
71
72 /*
73         The common material through which the player can walk and which
74         is transparent to light
75 */
76 //#define CONTENT_AIR 254
77 #define CONTENT_AIR 126
78
79 /*
80         Content feature list
81 */
82
83 enum ContentParamType
84 {
85         CPT_NONE,
86         CPT_LIGHT,
87         CPT_MINERAL,
88         // Direction for chests and furnaces and such
89         CPT_FACEDIR_SIMPLE
90 };
91
92 enum LiquidType
93 {
94         LIQUID_NONE,
95         LIQUID_FLOWING,
96         LIQUID_SOURCE
97 };
98
99 struct MapNode;
100 class NodeMetadata;
101
102 struct ContentFeatures
103 {
104         // Type of MapNode::param1
105         ContentParamType param_type;
106
107         /*
108                 0: up
109                 1: down
110                 2: right
111                 3: left
112                 4: back
113                 5: front
114         */
115         TileSpec tiles[6];
116         
117         video::ITexture *inventory_texture;
118         
119         // True for all ground-like things like stone and mud, false for eg. trees
120         bool is_ground_content;
121         bool light_propagates;
122         bool sunlight_propagates;
123         u8 solidness; // Used when choosing which face is drawn
124         u8 visual_solidness; // When solidness=0, this tells how it looks like
125         // This is used for collision detection.
126         // Also for general solidness queries.
127         bool walkable;
128         // Player can point to these
129         bool pointable;
130         // Player can dig these
131         bool diggable;
132         // Player can climb these
133         bool climbable;
134         // Player can build on these
135         bool buildable_to;
136         // Whether the node has no liquid, source liquid or flowing liquid
137         enum LiquidType liquid_type;
138         // If true, param2 is set to direction when placed. Used for torches.
139         // NOTE: the direction format is quite inefficient and should be changed
140         bool wall_mounted;
141         // If true, node is equivalent to air. Torches are, air is. Water is not.
142         // Is used for example to check whether a mud block can have grass on.
143         bool air_equivalent;
144         
145         // Inventory item string as which the node appears in inventory when dug.
146         // Mineral overrides this.
147         std::string dug_item;
148
149         // Extra dug item and its rarity
150         std::string extra_dug_item;
151         s32 extra_dug_item_rarity;
152         
153         // Initial metadata is cloned from this
154         NodeMetadata *initial_metadata;
155         
156         // If the content is liquid, this is the flowing version of the liquid.
157         // If content is liquid, this is the same content.
158         content_t liquid_alternative_flowing;
159         // If the content is liquid, this is the source version of the liquid.
160         content_t liquid_alternative_source;
161         // Viscosity for fluid flow, ranging from 1 to 7, with
162         // 1 giving almost instantaneous propagation and 7 being
163         // the slowest possible
164         u8 liquid_viscosity;
165         // Used currently for flowing liquids
166         u8 vertex_alpha;
167         // Post effect color, drawn when the camera is inside the node.
168         video::SColor post_effect_color;
169         // Special irrlicht material, used sometimes
170         video::SMaterial *special_material;
171         AtlasPointer *special_atlas;
172         
173         // Amount of light the node emits
174         u8 light_source;
175         
176         // Digging properties for different tools
177         DiggingPropertiesList digging_properties;
178
179         u32 damage_per_second;
180         
181         // NOTE: Move relevant properties to here from elsewhere
182
183         void reset()
184         {
185                 param_type = CPT_NONE;
186                 inventory_texture = NULL;
187                 is_ground_content = false;
188                 light_propagates = false;
189                 sunlight_propagates = false;
190                 solidness = 2;
191                 visual_solidness = 0;
192                 walkable = true;
193                 pointable = true;
194                 diggable = true;
195                 climbable = false;
196                 buildable_to = false;
197                 liquid_type = LIQUID_NONE;
198                 wall_mounted = false;
199                 air_equivalent = false;
200                 dug_item = "";
201                 initial_metadata = NULL;
202                 liquid_alternative_flowing = CONTENT_IGNORE;
203                 liquid_alternative_source = CONTENT_IGNORE;
204                 liquid_viscosity = 0;
205                 vertex_alpha = 255;
206                 post_effect_color = video::SColor(0, 0, 0, 0);
207                 special_material = NULL;
208                 special_atlas = NULL;
209                 light_source = 0;
210                 digging_properties.clear();
211                 damage_per_second = 0;
212         }
213
214         ContentFeatures()
215         {
216                 reset();
217         }
218
219         ~ContentFeatures();
220         
221         /*
222                 Quickhands for simple materials
223         */
224         
225         void setTexture(u16 i, std::string name, u8 alpha=255);
226
227         void setAllTextures(std::string name, u8 alpha=255)
228         {
229                 for(u16 i=0; i<6; i++)
230                 {
231                         setTexture(i, name, alpha);
232                 }
233                 // Force inventory texture too
234                 setInventoryTexture(name);
235         }
236
237         void setTile(u16 i, const TileSpec &tile)
238         {
239                 tiles[i] = tile;
240         }
241         void setAllTiles(const TileSpec &tile)
242         {
243                 for(u16 i=0; i<6; i++)
244                 {
245                         setTile(i, tile);
246                 }
247         }
248
249         void setInventoryTexture(std::string imgname);
250         
251         void setInventoryTextureCube(std::string top,
252                         std::string left, std::string right);
253 };
254
255 /*
256         Call this to access the ContentFeature list
257 */
258 ContentFeatures & content_features(content_t i);
259 ContentFeatures & content_features(MapNode &n);
260
261 /*
262         Here is a bunch of DEPRECATED functions.
263 */
264
265 /*
266         If true, the material allows light propagation and brightness is stored
267         in param.
268         NOTE: Don't use, use "content_features(m).whatever" instead
269 */
270 inline bool light_propagates_content(content_t m)
271 {
272         return content_features(m).light_propagates;
273 }
274 /*
275         If true, the material allows lossless sunlight propagation.
276         NOTE: It doesn't seem to go through torches regardlessly of this
277         NOTE: Don't use, use "content_features(m).whatever" instead
278 */
279 inline bool sunlight_propagates_content(content_t m)
280 {
281         return content_features(m).sunlight_propagates;
282 }
283 /*
284         On a node-node surface, the material of the node with higher solidness
285         is used for drawing.
286         0: Invisible
287         1: Transparent
288         2: Opaque
289         NOTE: Don't use, use "content_features(m).whatever" instead
290 */
291 inline u8 content_solidness(content_t m)
292 {
293         return content_features(m).solidness;
294 }
295 // Objects collide with walkable contents
296 // NOTE: Don't use, use "content_features(m).whatever" instead
297 inline bool content_walkable(content_t m)
298 {
299         return content_features(m).walkable;
300 }
301 // NOTE: Don't use, use "content_features(m).whatever" instead
302 inline bool content_liquid(content_t m)
303 {
304         return content_features(m).liquid_type != LIQUID_NONE;
305 }
306 // NOTE: Don't use, use "content_features(m).whatever" instead
307 inline bool content_flowing_liquid(content_t m)
308 {
309         return content_features(m).liquid_type == LIQUID_FLOWING;
310 }
311 // NOTE: Don't use, use "content_features(m).whatever" instead
312 inline bool content_liquid_source(content_t m)
313 {
314         return content_features(m).liquid_type == LIQUID_SOURCE;
315 }
316 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
317 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
318 // NOTE: Don't use, use "content_features(m).whatever" instead
319 inline content_t make_liquid_flowing(content_t m)
320 {
321         u8 c = content_features(m).liquid_alternative_flowing;
322         assert(c != CONTENT_IGNORE);
323         return c;
324 }
325 // Pointable contents can be pointed to in the map
326 // NOTE: Don't use, use "content_features(m).whatever" instead
327 inline bool content_pointable(content_t m)
328 {
329         return content_features(m).pointable;
330 }
331 // NOTE: Don't use, use "content_features(m).whatever" instead
332 inline bool content_diggable(content_t m)
333 {
334         return content_features(m).diggable;
335 }
336 // NOTE: Don't use, use "content_features(m).whatever" instead
337 inline bool content_buildable_to(content_t m)
338 {
339         return content_features(m).buildable_to;
340 }
341
342 /*
343         Nodes make a face if contents differ and solidness differs.
344         Return value:
345                 0: No face
346                 1: Face uses m1's content
347                 2: Face uses m2's content
348 */
349 inline u8 face_contents(content_t m1, content_t m2)
350 {
351         if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
352                 return 0;
353         
354         bool contents_differ = (m1 != m2);
355         
356         // Contents don't differ for different forms of same liquid
357         if(content_liquid(m1) && content_liquid(m2)
358                         && make_liquid_flowing(m1) == make_liquid_flowing(m2))
359                 contents_differ = false;
360         
361         bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
362         bool makes_face = contents_differ && solidness_differs;
363
364         if(makes_face == false)
365                 return 0;
366
367         if(content_solidness(m1) > content_solidness(m2))
368                 return 1;
369         else
370                 return 2;
371 }
372
373 /*
374         Packs directions like (1,0,0), (1,-1,0)
375 */
376 inline u8 packDir(v3s16 dir)
377 {
378         u8 b = 0;
379
380         if(dir.X > 0)
381                 b |= (1<<0);
382         else if(dir.X < 0)
383                 b |= (1<<1);
384
385         if(dir.Y > 0)
386                 b |= (1<<2);
387         else if(dir.Y < 0)
388                 b |= (1<<3);
389
390         if(dir.Z > 0)
391                 b |= (1<<4);
392         else if(dir.Z < 0)
393                 b |= (1<<5);
394         
395         return b;
396 }
397 inline v3s16 unpackDir(u8 b)
398 {
399         v3s16 d(0,0,0);
400
401         if(b & (1<<0))
402                 d.X = 1;
403         else if(b & (1<<1))
404                 d.X = -1;
405
406         if(b & (1<<2))
407                 d.Y = 1;
408         else if(b & (1<<3))
409                 d.Y = -1;
410
411         if(b & (1<<4))
412                 d.Z = 1;
413         else if(b & (1<<5))
414                 d.Z = -1;
415         
416         return d;
417 }
418
419 /*
420         facedir: CPT_FACEDIR_SIMPLE param1 value
421         dir: The face for which stuff is wanted
422         return value: The face from which the stuff is actually found
423
424         NOTE: Currently this uses 2 bits for Z-,X-,Z+,X+, should there be Y+
425               and Y- too?
426 */
427 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
428
429 enum LightBank
430 {
431         LIGHTBANK_DAY,
432         LIGHTBANK_NIGHT
433 };
434
435 /*
436         Masks for MapNode.param2 of flowing liquids
437  */
438 #define LIQUID_LEVEL_MASK 0x07
439 #define LIQUID_FLOW_DOWN_MASK 0x08
440
441 /* maximum amount of liquid in a block */
442 #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
443 #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
444
445 /*
446         This is the stuff what the whole world consists of.
447 */
448
449
450 struct MapNode
451 {
452         /*
453                 Main content
454                 0x00-0x7f: Short content type
455                 0x80-0xff: Long content type (param2>>4 makes up low bytes)
456         */
457         union
458         {
459                 u8 param0;
460                 //u8 d;
461         };
462
463         /*
464                 Misc parameter. Initialized to 0.
465                 - For light_propagates() blocks, this is light intensity,
466                   stored logarithmically from 0 to LIGHT_MAX.
467                   Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
468                   - Contains 2 values, day- and night lighting. Each takes 4 bits.
469                 - Mineral content (should be removed from here)
470                 - Uhh... well, most blocks have light or nothing in here.
471         */
472         union
473         {
474                 u8 param1;
475                 //s8 param;
476         };
477         
478         /*
479                 The second parameter. Initialized to 0.
480                 E.g. direction for torches and flowing water.
481                 If param0 >= 0x80, bits 0xf0 of this is extended content type data
482         */
483         union
484         {
485                 u8 param2;
486                 //u8 dir;
487         };
488
489         MapNode(const MapNode & n)
490         {
491                 *this = n;
492         }
493         
494         MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
495         {
496                 //param0 = a_param0;
497                 param1 = a_param1;
498                 param2 = a_param2;
499                 // Set after other params because this needs to override part of param2
500                 setContent(content);
501         }
502
503         bool operator==(const MapNode &other)
504         {
505                 return (param0 == other.param0
506                                 && param1 == other.param1
507                                 && param2 == other.param2);
508         }
509         
510         // To be used everywhere
511         content_t getContent()
512         {
513                 if(param0 < 0x80)
514                         return param0;
515                 else
516                         return (param0<<4) + (param2>>4);
517         }
518         void setContent(content_t c)
519         {
520                 if(c < 0x80)
521                 {
522                         if(param0 >= 0x80)
523                                 param2 &= ~(0xf0);
524                         param0 = c;
525                 }
526                 else
527                 {
528                         param0 = c>>4;
529                         param2 &= ~(0xf0);
530                         param2 |= (c&0x0f)<<4;
531                 }
532         }
533         
534         /*
535                 These four are DEPRECATED I guess. -c55
536         */
537         bool light_propagates()
538         {
539                 return light_propagates_content(getContent());
540         }
541         bool sunlight_propagates()
542         {
543                 return sunlight_propagates_content(getContent());
544         }
545         u8 solidness()
546         {
547                 return content_solidness(getContent());
548         }
549         u8 light_source()
550         {
551                 return content_features(*this).light_source;
552         }
553
554         u8 getLightBanksWithSource()
555         {
556                 // Select the brightest of [light source, propagated light]
557                 u8 lightday = 0;
558                 u8 lightnight = 0;
559                 if(content_features(*this).param_type == CPT_LIGHT)
560                 {
561                         lightday = param1 & 0x0f;
562                         lightnight = (param1>>4)&0x0f;
563                 }
564                 if(light_source() > lightday)
565                         lightday = light_source();
566                 if(light_source() > lightnight)
567                         lightnight = light_source();
568                 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
569         }
570
571         u8 getLight(enum LightBank bank)
572         {
573                 // Select the brightest of [light source, propagated light]
574                 u8 light = 0;
575                 if(content_features(*this).param_type == CPT_LIGHT)
576                 {
577                         if(bank == LIGHTBANK_DAY)
578                                 light = param1 & 0x0f;
579                         else if(bank == LIGHTBANK_NIGHT)
580                                 light = (param1>>4)&0x0f;
581                         else
582                                 assert(0);
583                 }
584                 if(light_source() > light)
585                         light = light_source();
586                 return light;
587         }
588         
589         // 0 <= daylight_factor <= 1000
590         // 0 <= return value <= LIGHT_SUN
591         u8 getLightBlend(u32 daylight_factor)
592         {
593                 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
594                         + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
595                         )/1000;
596                 u8 max = LIGHT_MAX;
597                 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
598                         max = LIGHT_SUN;
599                 if(l > max)
600                         l = max;
601                 return l;
602         }
603         /*// 0 <= daylight_factor <= 1000
604         // 0 <= return value <= 255
605         u8 getLightBlend(u32 daylight_factor)
606         {
607                 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
608                 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
609                 u8 mix = ((daylight_factor * daylight
610                         + (1000-daylight_factor) * nightlight)
611                         )/1000;
612                 return mix;
613         }*/
614
615         void setLight(enum LightBank bank, u8 a_light)
616         {
617                 // If node doesn't contain light data, ignore this
618                 if(content_features(*this).param_type != CPT_LIGHT)
619                         return;
620                 if(bank == LIGHTBANK_DAY)
621                 {
622                         param1 &= 0xf0;
623                         param1 |= a_light & 0x0f;
624                 }
625                 else if(bank == LIGHTBANK_NIGHT)
626                 {
627                         param1 &= 0x0f;
628                         param1 |= (a_light & 0x0f)<<4;
629                 }
630                 else
631                         assert(0);
632         }
633         
634         // In mapnode.cpp
635         /*
636                 Get tile of a face of the node.
637                 dir: direction of face
638                 Returns: TileSpec. Can contain miscellaneous texture coordinates,
639                          which must be obeyed so that the texture atlas can be used.
640         */
641         TileSpec getTile(v3s16 dir);
642         
643         /*
644                 Gets mineral content of node, if there is any.
645                 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
646         */
647         u8 getMineral();
648         
649         /*
650                 Serialization functions
651         */
652
653         static u32 serializedLength(u8 version);
654         void serialize(u8 *dest, u8 version);
655         void deSerialize(u8 *source, u8 version);
656         
657 };
658
659 /*
660         Gets lighting value at face of node
661         
662         Parameters must consist of air and !air.
663         Order doesn't matter.
664
665         If either of the nodes doesn't exist, light is 0.
666         
667         parameters:
668                 daynight_ratio: 0...1000
669                 n: getNodeParent(p)
670                 n2: getNodeParent(p + face_dir)
671                 face_dir: axis oriented unit vector from p to p2
672         
673         returns encoded light value.
674 */
675 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
676                 v3s16 face_dir);
677
678 #endif
679