]> git.lizzy.rs Git - minetest.git/blob - src/mapnode.h
new texture stuff quite working
[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 "iirrlichtwrapper.h"
31
32 /*
33         Initializes all kind of stuff in here.
34         Many things depend on this.
35
36         This accesses g_texturesource; if it is non-NULL, textures are set.
37
38         Client first calls this with g_texturesource=NULL to run some
39         unit tests and stuff, then it runs this again with g_texturesource
40         defined to get the textures.
41
42         Server only calls this once with g_texturesource=NULL.
43 */
44 void init_mapnode();
45
46 // Initializes g_content_inventory_texture_paths
47 void init_content_inventory_texture_paths();
48
49
50 // NOTE: This is not used appropriately everywhere.
51 #define MATERIALS_COUNT 256
52
53 /*
54         Ignored node.
55
56         Anything that stores MapNodes doesn't have to preserve parameters
57         associated with this material.
58         
59         Doesn't create faces with anything and is considered being
60         out-of-map in the game map.
61 */
62 #define CONTENT_IGNORE 255
63 #define CONTENT_IGNORE_DEFAULT_PARAM 0
64
65 /*
66         The common material through which the player can walk and which
67         is transparent to light
68 */
69 #define CONTENT_AIR 254
70
71 /*
72         Suggested materials:
73         - Gravel
74         - Sand
75         
76         New naming scheme:
77         - Material = irrlicht's Material class
78         - Content = (u8) content of a node
79         - Tile = (u16) Material ID at some side of a node
80 */
81
82 #define CONTENT_STONE 0
83 #define CONTENT_GRASS 1
84 #define CONTENT_WATER 2
85 #define CONTENT_TORCH 3
86 #define CONTENT_TREE 4
87 #define CONTENT_LEAVES 5
88 #define CONTENT_GRASS_FOOTSTEPS 6
89 #define CONTENT_MESE 7
90 #define CONTENT_MUD 8
91 #define CONTENT_WATERSOURCE 9
92 // Pretty much useless, clouds won't be drawn this way
93 #define CONTENT_CLOUD 10
94 #define CONTENT_COALSTONE 11
95 #define CONTENT_WOOD 12
96 #define CONTENT_SAND 13
97
98 /*
99         This is used by all kinds of things to allocate memory for all
100         contents except CONTENT_AIR and CONTENT_IGNORE
101 */
102 #define USEFUL_CONTENT_COUNT 14
103
104 /*
105         Content feature list
106 */
107
108 enum ContentParamType
109 {
110         CPT_NONE,
111         CPT_LIGHT,
112         CPT_MINERAL
113 };
114
115 enum LiquidType
116 {
117         LIQUID_NONE,
118         LIQUID_FLOWING,
119         LIQUID_SOURCE
120 };
121
122 class MapNode;
123
124 struct ContentFeatures
125 {
126         // If non-NULL, content is translated to this when deserialized
127         MapNode *translate_to;
128
129         // Type of MapNode::param
130         ContentParamType param_type;
131
132         /*
133                 0: up
134                 1: down
135                 2: right
136                 3: left
137                 4: back
138                 5: front
139         */
140         TileSpec tiles[6];
141         
142         // TODO: Somehow specify inventory image
143         //std::string inventory_image_path;
144         //TextureSpec inventory_texture;
145         //u32 inventory_texture_id;
146         video::ITexture *inventory_texture;
147
148         bool is_ground_content; //TODO: Remove, use walkable instead
149         bool light_propagates;
150         bool sunlight_propagates;
151         u8 solidness; // Used when choosing which face is drawn
152         bool walkable;
153         bool pointable;
154         bool diggable;
155         bool buildable_to;
156         enum LiquidType liquid_type;
157         bool wall_mounted; // If true, param2 is set to direction when placed
158
159         //TODO: Move more properties here
160
161         ContentFeatures()
162         {
163                 translate_to = NULL;
164                 param_type = CPT_NONE;
165                 inventory_texture = NULL;
166                 is_ground_content = false;
167                 light_propagates = false;
168                 sunlight_propagates = false;
169                 solidness = 2;
170                 walkable = true;
171                 pointable = true;
172                 diggable = true;
173                 buildable_to = false;
174                 liquid_type = LIQUID_NONE;
175                 wall_mounted = false;
176         }
177
178         ~ContentFeatures();
179         
180         /*
181                 Quickhands for simple materials
182         */
183         
184         void setTexture(u16 i, std::string name, u8 alpha=255);
185
186         void setAllTextures(std::string name, u8 alpha=255)
187         {
188                 for(u16 i=0; i<6; i++)
189                 {
190                         setTexture(i, name, alpha);
191                 }
192         }
193
194         /*void setTexture(u16 i, AtlasPointer p, u8 alpha=255)
195         {
196                 tiles[i].texture = p;
197                 if(alpha != 255)
198                 {
199                         tiles[i].alpha = alpha;
200                         tiles[i].material_type = MATERIAL_ALPHA_VERTEX;
201                 }
202         }
203         void setAllTextures(AtlasPointer p, u8 alpha=255)
204         {
205                 for(u16 i=0; i<6; i++)
206                 {
207                         setTexture(i, p, alpha);
208                 }
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(u8 i);
233
234 /*
235         If true, the material allows light propagation and brightness is stored
236         in param.
237         NOTE: Don't use, use "content_features(m).whatever" instead
238 */
239 inline bool light_propagates_content(u8 m)
240 {
241         return content_features(m).light_propagates;
242         //return (m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
243 }
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(u8 m)
251 {
252         return content_features(m).sunlight_propagates;
253         //return (m == CONTENT_AIR || m == CONTENT_TORCH);
254 }
255
256 /*
257         On a node-node surface, the material of the node with higher solidness
258         is used for drawing.
259         0: Invisible
260         1: Transparent
261         2: Opaque
262         NOTE: Don't use, use "content_features(m).whatever" instead
263 */
264 inline u8 content_solidness(u8 m)
265 {
266         return content_features(m).solidness;
267         /*// As of now, every pseudo node like torches are added to this
268         if(m == CONTENT_AIR || m == CONTENT_TORCH || m == CONTENT_WATER)
269                 return 0;
270         if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
271                 return 1;
272         return 2;*/
273 }
274
275 // Objects collide with walkable contents
276 // NOTE: Don't use, use "content_features(m).whatever" instead
277 inline bool content_walkable(u8 m)
278 {
279         return content_features(m).walkable;
280         //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE && m != CONTENT_TORCH);
281 }
282
283 // NOTE: Don't use, use "content_features(m).whatever" instead
284 inline bool content_liquid(u8 m)
285 {
286         return content_features(m).liquid_type != LIQUID_NONE;
287         //return (m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
288 }
289
290 // NOTE: Don't use, use "content_features(m).whatever" instead
291 inline bool content_flowing_liquid(u8 m)
292 {
293         return content_features(m).liquid_type == LIQUID_FLOWING;
294         //return (m == CONTENT_WATER);
295 }
296
297 // NOTE: Don't use, use "content_features(m).whatever" instead
298 inline bool content_liquid_source(u8 m)
299 {
300         return content_features(m).liquid_type == LIQUID_SOURCE;
301         //return (m == CONTENT_WATERSOURCE);
302 }
303
304 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
305 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
306 inline u8 make_liquid_flowing(u8 m)
307 {
308         if(m == CONTENT_WATER || m == CONTENT_WATERSOURCE)
309                 return CONTENT_WATER;
310         assert(0);
311 }
312
313 // Pointable contents can be pointed to in the map
314 // NOTE: Don't use, use "content_features(m).whatever" instead
315 inline bool content_pointable(u8 m)
316 {
317         return content_features(m).pointable;
318         //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
319 }
320
321 // NOTE: Don't use, use "content_features(m).whatever" instead
322 inline bool content_diggable(u8 m)
323 {
324         return content_features(m).diggable;
325         //return (m != CONTENT_AIR && m != CONTENT_WATER && m != CONTENT_WATERSOURCE);
326 }
327
328 // NOTE: Don't use, use "content_features(m).whatever" instead
329 inline bool content_buildable_to(u8 m)
330 {
331         return content_features(m).buildable_to;
332         //return (m == CONTENT_AIR || m == CONTENT_WATER || m == CONTENT_WATERSOURCE);
333 }
334
335 /*
336         Returns true for contents that form the base ground that
337         follows the main heightmap
338 */
339 /*inline bool is_ground_content(u8 m)
340 {
341         return content_features(m).is_ground_content;
342 }*/
343
344 /*
345         Nodes make a face if contents differ and solidness differs.
346         Return value:
347                 0: No face
348                 1: Face uses m1's content
349                 2: Face uses m2's content
350 */
351 inline u8 face_contents(u8 m1, u8 m2)
352 {
353         if(m1 == CONTENT_IGNORE || m2 == CONTENT_IGNORE)
354                 return 0;
355         
356         bool contents_differ = (m1 != m2);
357         
358         // Contents don't differ for different forms of same liquid
359         if(content_liquid(m1) && content_liquid(m2)
360                         && make_liquid_flowing(m1) == make_liquid_flowing(m2))
361                 contents_differ = false;
362         
363         bool solidness_differs = (content_solidness(m1) != content_solidness(m2));
364         bool makes_face = contents_differ && solidness_differs;
365
366         if(makes_face == false)
367                 return 0;
368
369         if(content_solidness(m1) > content_solidness(m2))
370                 return 1;
371         else
372                 return 2;
373 }
374
375 /*
376         Packs directions like (1,0,0), (1,-1,0)
377 */
378 inline u8 packDir(v3s16 dir)
379 {
380         u8 b = 0;
381
382         if(dir.X > 0)
383                 b |= (1<<0);
384         else if(dir.X < 0)
385                 b |= (1<<1);
386
387         if(dir.Y > 0)
388                 b |= (1<<2);
389         else if(dir.Y < 0)
390                 b |= (1<<3);
391
392         if(dir.Z > 0)
393                 b |= (1<<4);
394         else if(dir.Z < 0)
395                 b |= (1<<5);
396         
397         return b;
398 }
399 inline v3s16 unpackDir(u8 b)
400 {
401         v3s16 d(0,0,0);
402
403         if(b & (1<<0))
404                 d.X = 1;
405         else if(b & (1<<1))
406                 d.X = -1;
407
408         if(b & (1<<2))
409                 d.Y = 1;
410         else if(b & (1<<3))
411                 d.Y = -1;
412
413         if(b & (1<<4))
414                 d.Z = 1;
415         else if(b & (1<<5))
416                 d.Z = -1;
417         
418         return d;
419 }
420
421 enum LightBank
422 {
423         LIGHTBANK_DAY,
424         LIGHTBANK_NIGHT
425 };
426
427 /*
428         This is the stuff what the whole world consists of.
429 */
430
431 struct MapNode
432 {
433         // Content
434         u8 d;
435
436         /*
437                 Misc parameter. Initialized to 0.
438                 - For light_propagates() blocks, this is light intensity,
439                   stored logarithmically from 0 to LIGHT_MAX.
440                   Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
441                 - Contains 2 values, day- and night lighting. Each takes 4 bits.
442         */
443         s8 param;
444         
445         union
446         {
447                 /*
448                         The second parameter. Initialized to 0.
449                         Direction for torches and flowing water.
450                 */
451                 u8 param2;
452                 u8 dir;
453         };
454
455         MapNode(const MapNode & n)
456         {
457                 *this = n;
458         }
459         
460         MapNode(u8 data=CONTENT_AIR, u8 a_param=0, u8 a_param2=0)
461         {
462                 d = data;
463                 param = a_param;
464                 param2 = a_param2;
465         }
466
467         /*MapNode & operator=(const MapNode &other)
468         {
469                 d = other.d;
470                 param = other.param;
471                 param2 = other.param2;
472                 return *this;
473         }*/
474
475         bool operator==(const MapNode &other)
476         {
477                 return (d == other.d
478                                 && param == other.param
479                                 && param2 == other.param2);
480         }
481
482         bool light_propagates()
483         {
484                 return light_propagates_content(d);
485         }
486         
487         bool sunlight_propagates()
488         {
489                 return sunlight_propagates_content(d);
490         }
491         
492         u8 solidness()
493         {
494                 return content_solidness(d);
495         }
496
497         u8 light_source()
498         {
499                 /*
500                         Note that a block that isn't light_propagates() can be a light source.
501                 */
502                 if(d == CONTENT_TORCH)
503                         return LIGHT_MAX;
504                 
505                 return 0;
506         }
507
508         u8 getLightBanksWithSource()
509         {
510                 // Select the brightest of [light source, propagated light]
511                 u8 lightday = 0;
512                 u8 lightnight = 0;
513                 if(light_propagates())
514                 {
515                         lightday = param & 0x0f;
516                         lightnight = (param>>4)&0x0f;
517                 }
518                 if(light_source() > lightday)
519                         lightday = light_source();
520                 if(light_source() > lightnight)
521                         lightnight = light_source();
522                 return (lightday&0x0f) | ((lightnight<<4)&0xf0);
523         }
524
525         void setLightBanks(u8 a_light)
526         {
527                 param = a_light;
528         }
529
530         u8 getLight(enum LightBank bank)
531         {
532                 // Select the brightest of [light source, propagated light]
533                 u8 light = 0;
534                 if(light_propagates())
535                 {
536                         if(bank == LIGHTBANK_DAY)
537                                 light = param & 0x0f;
538                         else if(bank == LIGHTBANK_NIGHT)
539                                 light = (param>>4)&0x0f;
540                         else
541                                 assert(0);
542                 }
543                 if(light_source() > light)
544                         light = light_source();
545                 return light;
546         }
547         
548         // 0 <= daylight_factor <= 1000
549         // 0 <= return value <= LIGHT_SUN
550         u8 getLightBlend(u32 daylight_factor)
551         {
552                 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
553                         + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
554                         )/1000;
555                 u8 max = LIGHT_MAX;
556                 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
557                         max = LIGHT_SUN;
558                 if(l > max)
559                         l = max;
560                 return l;
561         }
562         /*// 0 <= daylight_factor <= 1000
563         // 0 <= return value <= 255
564         u8 getLightBlend(u32 daylight_factor)
565         {
566                 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
567                 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
568                 u8 mix = ((daylight_factor * daylight
569                         + (1000-daylight_factor) * nightlight)
570                         )/1000;
571                 return mix;
572         }*/
573
574         void setLight(enum LightBank bank, u8 a_light)
575         {
576                 // If not transparent, can't set light
577                 if(light_propagates() == false)
578                         return;
579                 if(bank == LIGHTBANK_DAY)
580                 {
581                         param &= 0xf0;
582                         param |= a_light & 0x0f;
583                 }
584                 else if(bank == LIGHTBANK_NIGHT)
585                 {
586                         param &= 0x0f;
587                         param |= (a_light & 0x0f)<<4;
588                 }
589                 else
590                         assert(0);
591         }
592         
593         // In mapnode.cpp
594         TileSpec getTile(v3s16 dir);
595
596         u8 getMineral();
597
598         /*
599                 These serialization functions are used when informing client
600                 of a single node add
601         */
602
603         static u32 serializedLength(u8 version)
604         {
605                 if(!ser_ver_supported(version))
606                         throw VersionMismatchException("ERROR: MapNode format not supported");
607                         
608                 if(version == 0)
609                         return 1;
610                 else if(version <= 9)
611                         return 2;
612                 else
613                         return 3;
614         }
615         void serialize(u8 *dest, u8 version)
616         {
617                 if(!ser_ver_supported(version))
618                         throw VersionMismatchException("ERROR: MapNode format not supported");
619                         
620                 if(version == 0)
621                 {
622                         dest[0] = d;
623                 }
624                 else if(version <= 9)
625                 {
626                         dest[0] = d;
627                         dest[1] = param;
628                 }
629                 else
630                 {
631                         dest[0] = d;
632                         dest[1] = param;
633                         dest[2] = param2;
634                 }
635         }
636         void deSerialize(u8 *source, u8 version)
637         {
638                 if(!ser_ver_supported(version))
639                         throw VersionMismatchException("ERROR: MapNode format not supported");
640                         
641                 if(version == 0)
642                 {
643                         d = source[0];
644                 }
645                 else if(version == 1)
646                 {
647                         d = source[0];
648                         // This version doesn't support saved lighting
649                         if(light_propagates() || light_source() > 0)
650                                 param = 0;
651                         else
652                                 param = source[1];
653                 }
654                 else if(version <= 9)
655                 {
656                         d = source[0];
657                         param = source[1];
658                 }
659                 else
660                 {
661                         d = source[0];
662                         param = source[1];
663                         param2 = source[2];
664                 }
665
666                 // Translate deprecated stuff
667                 // NOTE: This doesn't get used because MapBlock handles node
668                 // parameters directly
669                 MapNode *translate_to = content_features(d).translate_to;
670                 if(translate_to)
671                 {
672                         dstream<<"MapNode: WARNING: Translating "<<d<<" to "
673                                         <<translate_to->d<<std::endl;
674                         *this = *translate_to;
675                 }
676         }
677 };
678
679 /*
680         Returns integer position of the node in given
681         floating point position.
682 */
683 inline v3s16 floatToInt(v3f p)
684 {
685         v3s16 p2(
686                 (p.X + (p.X>0 ? BS/2 : -BS/2))/BS,
687                 (p.Y + (p.Y>0 ? BS/2 : -BS/2))/BS,
688                 (p.Z + (p.Z>0 ? BS/2 : -BS/2))/BS);
689         return p2;
690 }
691
692 /*
693         The same thing backwards
694 */
695 inline v3f intToFloat(v3s16 p)
696 {
697         v3f p2(
698                 p.X * BS,
699                 p.Y * BS,
700                 p.Z * BS
701         );
702         return p2;
703 }
704
705
706
707 #endif
708