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