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