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