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