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