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