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