Commented out debug statements again
[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         // Initial metadata is cloned from this
150         NodeMetadata *initial_metadata;
151         
152         // If the content is liquid, this is the flowing version of the liquid.
153         // If content is liquid, this is the same content.
154         content_t liquid_alternative_flowing;
155         // If the content is liquid, this is the source version of the liquid.
156         content_t liquid_alternative_source;
157         // Viscosity for fluid flow, ranging from 1 to 7, with
158         // 1 giving almost instantaneous propagation and 7 being
159         // the slowest possible
160         u8 liquid_viscosity;
161         // Used currently for flowing liquids
162         u8 vertex_alpha;
163         // Post effect color, drawn when the camera is inside the node.
164         video::SColor post_effect_color;
165         // Special irrlicht material, used sometimes
166         video::SMaterial *special_material;
167         AtlasPointer *special_atlas;
168         
169         // Amount of light the node emits
170         u8 light_source;
171         
172         // Digging properties for different tools
173         DiggingPropertiesList digging_properties;
174
175         u32 damage_per_second;
176         
177         // NOTE: Move relevant properties to here from elsewhere
178
179         void reset()
180         {
181                 param_type = CPT_NONE;
182                 inventory_texture = NULL;
183                 is_ground_content = false;
184                 light_propagates = false;
185                 sunlight_propagates = false;
186                 solidness = 2;
187                 visual_solidness = 0;
188                 walkable = true;
189                 pointable = true;
190                 diggable = true;
191                 climbable = false;
192                 buildable_to = false;
193                 liquid_type = LIQUID_NONE;
194                 wall_mounted = false;
195                 air_equivalent = false;
196                 dug_item = "";
197                 initial_metadata = NULL;
198                 liquid_alternative_flowing = CONTENT_IGNORE;
199                 liquid_alternative_source = CONTENT_IGNORE;
200                 liquid_viscosity = 0;
201                 vertex_alpha = 255;
202                 post_effect_color = video::SColor(0, 0, 0, 0);
203                 special_material = NULL;
204                 special_atlas = NULL;
205                 light_source = 0;
206                 digging_properties.clear();
207                 damage_per_second = 0;
208         }
209
210         ContentFeatures()
211         {
212                 reset();
213         }
214
215         ~ContentFeatures();
216         
217         /*
218                 Quickhands for simple materials
219         */
220         
221         void setTexture(u16 i, std::string name, u8 alpha=255);
222
223         void setAllTextures(std::string name, u8 alpha=255)
224         {
225                 for(u16 i=0; i<6; i++)
226                 {
227                         setTexture(i, name, alpha);
228                 }
229                 // Force inventory texture too
230                 setInventoryTexture(name);
231         }
232
233         void setTile(u16 i, const TileSpec &tile)
234         {
235                 tiles[i] = tile;
236         }
237         void setAllTiles(const TileSpec &tile)
238         {
239                 for(u16 i=0; i<6; i++)
240                 {
241                         setTile(i, tile);
242                 }
243         }
244
245         void setInventoryTexture(std::string imgname);
246         
247         void setInventoryTextureCube(std::string top,
248                         std::string left, std::string right);
249 };
250
251 /*
252         Call this to access the ContentFeature list
253 */
254 ContentFeatures & content_features(content_t i);
255 ContentFeatures & content_features(MapNode &n);
256
257 /*
258         Here is a bunch of DEPRECATED functions.
259 */
260
261 /*
262         If true, the material allows light propagation and brightness is stored
263         in param.
264         NOTE: Don't use, use "content_features(m).whatever" instead
265 */
266 inline bool light_propagates_content(content_t m)
267 {
268         return content_features(m).light_propagates;
269 }
270 /*
271         If true, the material allows lossless sunlight propagation.
272         NOTE: It doesn't seem to go through torches regardlessly of this
273         NOTE: Don't use, use "content_features(m).whatever" instead
274 */
275 inline bool sunlight_propagates_content(content_t m)
276 {
277         return content_features(m).sunlight_propagates;
278 }
279 /*
280         On a node-node surface, the material of the node with higher solidness
281         is used for drawing.
282         0: Invisible
283         1: Transparent
284         2: Opaque
285         NOTE: Don't use, use "content_features(m).whatever" instead
286 */
287 inline u8 content_solidness(content_t m)
288 {
289         return content_features(m).solidness;
290 }
291 // Objects collide with walkable contents
292 // NOTE: Don't use, use "content_features(m).whatever" instead
293 inline bool content_walkable(content_t m)
294 {
295         return content_features(m).walkable;
296 }
297 // NOTE: Don't use, use "content_features(m).whatever" instead
298 inline bool content_liquid(content_t m)
299 {
300         return content_features(m).liquid_type != LIQUID_NONE;
301 }
302 // NOTE: Don't use, use "content_features(m).whatever" instead
303 inline bool content_flowing_liquid(content_t m)
304 {
305         return content_features(m).liquid_type == LIQUID_FLOWING;
306 }
307 // NOTE: Don't use, use "content_features(m).whatever" instead
308 inline bool content_liquid_source(content_t m)
309 {
310         return content_features(m).liquid_type == LIQUID_SOURCE;
311 }
312 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
313 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
314 // NOTE: Don't use, use "content_features(m).whatever" instead
315 inline content_t make_liquid_flowing(content_t m)
316 {
317         u8 c = content_features(m).liquid_alternative_flowing;
318         assert(c != CONTENT_IGNORE);
319         return c;
320 }
321 // Pointable contents can be pointed to in the map
322 // NOTE: Don't use, use "content_features(m).whatever" instead
323 inline bool content_pointable(content_t m)
324 {
325         return content_features(m).pointable;
326 }
327 // NOTE: Don't use, use "content_features(m).whatever" instead
328 inline bool content_diggable(content_t m)
329 {
330         return content_features(m).diggable;
331 }
332 // NOTE: Don't use, use "content_features(m).whatever" instead
333 inline bool content_buildable_to(content_t m)
334 {
335         return content_features(m).buildable_to;
336 }
337
338 /*
339         Nodes make a face if contents differ and solidness differs.
340         Return value:
341                 0: No face
342                 1: Face uses m1's content
343                 2: Face uses m2's content
344 */
345 inline u8 face_contents(content_t m1, content_t m2)
346 {
347         if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
348                 return 0;
349         
350         bool contents_differ = (m1 != m2);
351         
352         // Contents don't differ for different forms of same liquid
353         if(content_liquid(m1) && content_liquid(m2)
354                         && make_liquid_flowing(m1) == make_liquid_flowing(m2))
355                 contents_differ = false;
356         
357         bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
358         bool makes_face = contents_differ && solidness_differs;
359
360         if(makes_face == false)
361                 return 0;
362
363         if(content_solidness(m1) > content_solidness(m2))
364                 return 1;
365         else
366                 return 2;
367 }
368
369 /*
370         Packs directions like (1,0,0), (1,-1,0)
371 */
372 inline u8 packDir(v3s16 dir)
373 {
374         u8 b = 0;
375
376         if(dir.X > 0)
377                 b |= (1<<0);
378         else if(dir.X < 0)
379                 b |= (1<<1);
380
381         if(dir.Y > 0)
382                 b |= (1<<2);
383         else if(dir.Y < 0)
384                 b |= (1<<3);
385
386         if(dir.Z > 0)
387                 b |= (1<<4);
388         else if(dir.Z < 0)
389                 b |= (1<<5);
390         
391         return b;
392 }
393 inline v3s16 unpackDir(u8 b)
394 {
395         v3s16 d(0,0,0);
396
397         if(b & (1<<0))
398                 d.X = 1;
399         else if(b & (1<<1))
400                 d.X = -1;
401
402         if(b & (1<<2))
403                 d.Y = 1;
404         else if(b & (1<<3))
405                 d.Y = -1;
406
407         if(b & (1<<4))
408                 d.Z = 1;
409         else if(b & (1<<5))
410                 d.Z = -1;
411         
412         return d;
413 }
414
415 /*
416         facedir: CPT_FACEDIR_SIMPLE param1 value
417         dir: The face for which stuff is wanted
418         return value: The face from which the stuff is actually found
419
420         NOTE: Currently this uses 2 bits for Z-,X-,Z+,X+, should there be Y+
421               and Y- too?
422 */
423 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
424
425 enum LightBank
426 {
427         LIGHTBANK_DAY,
428         LIGHTBANK_NIGHT
429 };
430
431 /*
432         Masks for MapNode.param2 of flowing liquids
433  */
434 #define LIQUID_LEVEL_MASK 0x07
435 #define LIQUID_FLOW_DOWN_MASK 0x08
436
437 /* maximum amount of liquid in a block */
438 #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
439 #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
440
441 /*
442         This is the stuff what the whole world consists of.
443 */
444
445
446 struct MapNode
447 {
448         /*
449                 Main content
450                 0x00-0x7f: Short content type
451                 0x80-0xff: Long content type (param2>>4 makes up low bytes)
452         */
453         union
454         {
455                 u8 param0;
456                 //u8 d;
457         };
458
459         /*
460                 Misc parameter. Initialized to 0.
461                 - For light_propagates() blocks, this is light intensity,
462                   stored logarithmically from 0 to LIGHT_MAX.
463                   Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
464                   - Contains 2 values, day- and night lighting. Each takes 4 bits.
465                 - Mineral content (should be removed from here)
466                 - Uhh... well, most blocks have light or nothing in here.
467         */
468         union
469         {
470                 u8 param1;
471                 //s8 param;
472         };
473         
474         /*
475                 The second parameter. Initialized to 0.
476                 E.g. direction for torches and flowing water.
477                 If param0 >= 0x80, bits 0xf0 of this is extended content type data
478         */
479         union
480         {
481                 u8 param2;
482                 //u8 dir;
483         };
484
485         MapNode(const MapNode & n)
486         {
487                 *this = n;
488         }
489         
490         MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
491         {
492                 //param0 = a_param0;
493                 param1 = a_param1;
494                 param2 = a_param2;
495                 // Set after other params because this needs to override part of param2
496                 setContent(content);
497         }
498
499         bool operator==(const MapNode &other)
500         {
501                 return (param0 == other.param0
502                                 && param1 == other.param1
503                                 && param2 == other.param2);
504         }
505         
506         // To be used everywhere
507         content_t getContent()
508         {
509                 if(param0 < 0x80)
510                         return param0;
511                 else
512                         return (param0<<4) + (param2>>4);
513         }
514         void setContent(content_t c)
515         {
516                 if(c < 0x80)
517                 {
518                         if(param0 >= 0x80)
519                                 param2 &= ~(0xf0);
520                         param0 = c;
521                 }
522                 else
523                 {
524                         param0 = c>>4;
525                         param2 &= ~(0xf0);
526                         param2 |= (c&0x0f)<<4;
527                 }
528         }
529         
530         /*
531                 These four are DEPRECATED I guess. -c55
532         */
533         bool light_propagates()
534         {
535                 return light_propagates_content(getContent());
536         }
537         bool sunlight_propagates()
538         {
539                 return sunlight_propagates_content(getContent());
540         }
541         u8 solidness()
542         {
543                 return content_solidness(getContent());
544         }
545         u8 light_source()
546         {
547                 return content_features(*this).light_source;
548         }
549
550         u8 getLightBanksWithSource()
551         {
552                 // Select the brightest of [light source, propagated light]
553                 u8 lightday = 0;
554                 u8 lightnight = 0;
555                 if(content_features(*this).param_type == CPT_LIGHT)
556                 {
557                         lightday = param1 & 0x0f;
558                         lightnight = (param1>>4)&0x0f;
559                 }
560                 if(light_source() > lightday)
561                         lightday = light_source();
562                 if(light_source() > lightnight)
563                         lightnight = light_source();
564                 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
565         }
566
567         u8 getLight(enum LightBank bank)
568         {
569                 // Select the brightest of [light source, propagated light]
570                 u8 light = 0;
571                 if(content_features(*this).param_type == CPT_LIGHT)
572                 {
573                         if(bank == LIGHTBANK_DAY)
574                                 light = param1 & 0x0f;
575                         else if(bank == LIGHTBANK_NIGHT)
576                                 light = (param1>>4)&0x0f;
577                         else
578                                 assert(0);
579                 }
580                 if(light_source() > light)
581                         light = light_source();
582                 return light;
583         }
584         
585         // 0 <= daylight_factor <= 1000
586         // 0 <= return value <= LIGHT_SUN
587         u8 getLightBlend(u32 daylight_factor)
588         {
589                 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
590                         + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
591                         )/1000;
592                 u8 max = LIGHT_MAX;
593                 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
594                         max = LIGHT_SUN;
595                 if(l > max)
596                         l = max;
597                 return l;
598         }
599         /*// 0 <= daylight_factor <= 1000
600         // 0 <= return value <= 255
601         u8 getLightBlend(u32 daylight_factor)
602         {
603                 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
604                 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
605                 u8 mix = ((daylight_factor * daylight
606                         + (1000-daylight_factor) * nightlight)
607                         )/1000;
608                 return mix;
609         }*/
610
611         void setLight(enum LightBank bank, u8 a_light)
612         {
613                 // If node doesn't contain light data, ignore this
614                 if(content_features(*this).param_type != CPT_LIGHT)
615                         return;
616                 if(bank == LIGHTBANK_DAY)
617                 {
618                         param1 &= 0xf0;
619                         param1 |= a_light & 0x0f;
620                 }
621                 else if(bank == LIGHTBANK_NIGHT)
622                 {
623                         param1 &= 0x0f;
624                         param1 |= (a_light & 0x0f)<<4;
625                 }
626                 else
627                         assert(0);
628         }
629         
630         // In mapnode.cpp
631         /*
632                 Get tile of a face of the node.
633                 dir: direction of face
634                 Returns: TileSpec. Can contain miscellaneous texture coordinates,
635                          which must be obeyed so that the texture atlas can be used.
636         */
637         TileSpec getTile(v3s16 dir);
638         
639         /*
640                 Gets mineral content of node, if there is any.
641                 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
642         */
643         u8 getMineral();
644         
645         /*
646                 Serialization functions
647         */
648
649         static u32 serializedLength(u8 version);
650         void serialize(u8 *dest, u8 version);
651         void deSerialize(u8 *source, u8 version);
652         
653 };
654
655 /*
656         Gets lighting value at face of node
657         
658         Parameters must consist of air and !air.
659         Order doesn't matter.
660
661         If either of the nodes doesn't exist, light is 0.
662         
663         parameters:
664                 daynight_ratio: 0...1000
665                 n: getNodeParent(p)
666                 n2: getNodeParent(p + face_dir)
667                 face_dir: axis oriented unit vector from p to p2
668         
669         returns encoded light value.
670 */
671 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
672                 v3s16 face_dir);
673
674 #endif
675