]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapnode.h
Move ContentFeatures to mapnode_contentfeatures.{h,cpp} and clean stuff
[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         Ignored node.
48
49         Anything that stores MapNodes doesn't have to preserve parameters
50         associated with this material.
51         
52         Doesn't create faces with anything and is considered being
53         out-of-map in the game map.
54 */
55 #define CONTENT_IGNORE 127
56 #define CONTENT_IGNORE_DEFAULT_PARAM 0
57
58 /*
59         The common material through which the player can walk and which
60         is transparent to light
61 */
62 #define CONTENT_AIR 126
63
64 /*
65         Nodes make a face if contents differ and solidness differs.
66         Return value:
67                 0: No face
68                 1: Face uses m1's content
69                 2: Face uses m2's content
70         equivalent: Whether the blocks share the same face (eg. water and glass)
71 */
72 u8 face_contents(content_t m1, content_t m2, bool *equivalent);
73
74 /*
75         Packs directions like (1,0,0), (1,-1,0) in six bits.
76         NOTE: This wastes way too much space for most purposes.
77 */
78 u8 packDir(v3s16 dir);
79 v3s16 unpackDir(u8 b);
80
81 /*
82         facedir: CPT_FACEDIR_SIMPLE param1 value
83         dir: The face for which stuff is wanted
84         return value: The face from which the stuff is actually found
85
86         NOTE: Currently this uses 2 bits for Z-,X-,Z+,X+, should there be Y+
87               and Y- too?
88 */
89 v3s16 facedir_rotate(u8 facedir, v3s16 dir);
90
91 enum LightBank
92 {
93         LIGHTBANK_DAY,
94         LIGHTBANK_NIGHT
95 };
96
97 /*
98         Masks for MapNode.param2 of flowing liquids
99  */
100 #define LIQUID_LEVEL_MASK 0x07
101 #define LIQUID_FLOW_DOWN_MASK 0x08
102
103 /* maximum amount of liquid in a block */
104 #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
105 #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
106
107 /*
108         This is the stuff what the whole world consists of.
109 */
110
111
112 struct MapNode
113 {
114         /*
115                 Main content
116                 0x00-0x7f: Short content type
117                 0x80-0xff: Long content type (param2>>4 makes up low bytes)
118         */
119         u8 param0;
120
121         /*
122                 Misc parameter. Initialized to 0.
123                 - For light_propagates() blocks, this is light intensity,
124                   stored logarithmically from 0 to LIGHT_MAX.
125                   Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
126                   - Contains 2 values, day- and night lighting. Each takes 4 bits.
127                 - Mineral content (should be removed from here)
128                 - Uhh... well, most blocks have light or nothing in here.
129         */
130         u8 param1;
131         
132         /*
133                 The second parameter. Initialized to 0.
134                 E.g. direction for torches and flowing water.
135                 If param0 >= 0x80, bits 0xf0 of this is extended content type data
136         */
137         u8 param2;
138
139         MapNode(const MapNode & n)
140         {
141                 *this = n;
142         }
143         
144         MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
145         {
146                 param1 = a_param1;
147                 param2 = a_param2;
148                 // Set content (param0 and (param2&0xf0)) after other params
149                 // because this needs to override part of param2
150                 setContent(content);
151         }
152
153         bool operator==(const MapNode &other)
154         {
155                 return (param0 == other.param0
156                                 && param1 == other.param1
157                                 && param2 == other.param2);
158         }
159         
160         // To be used everywhere
161         content_t getContent()
162         {
163                 if(param0 < 0x80)
164                         return param0;
165                 else
166                         return (param0<<4) + (param2>>4);
167         }
168         void setContent(content_t c)
169         {
170                 if(c < 0x80)
171                 {
172                         if(param0 >= 0x80)
173                                 param2 &= ~(0xf0);
174                         param0 = c;
175                 }
176                 else
177                 {
178                         param0 = c>>4;
179                         param2 &= ~(0xf0);
180                         param2 |= (c&0x0f)<<4;
181                 }
182         }
183         
184         /*
185                 These four are DEPRECATED I guess. -c55
186         */
187         bool light_propagates();
188         bool sunlight_propagates();
189         u8 solidness();
190         u8 light_source();
191
192         void setLight(enum LightBank bank, u8 a_light);
193         u8 getLight(enum LightBank bank);
194         u8 getLightBanksWithSource();
195         
196         // 0 <= daylight_factor <= 1000
197         // 0 <= return value <= LIGHT_SUN
198         u8 getLightBlend(u32 daylight_factor)
199         {
200                 u8 l = ((daylight_factor * getLight(LIGHTBANK_DAY)
201                         + (1000-daylight_factor) * getLight(LIGHTBANK_NIGHT))
202                         )/1000;
203                 u8 max = LIGHT_MAX;
204                 if(getLight(LIGHTBANK_DAY) == LIGHT_SUN)
205                         max = LIGHT_SUN;
206                 if(l > max)
207                         l = max;
208                 return l;
209         }
210         /*// 0 <= daylight_factor <= 1000
211         // 0 <= return value <= 255
212         u8 getLightBlend(u32 daylight_factor)
213         {
214                 u8 daylight = decode_light(getLight(LIGHTBANK_DAY));
215                 u8 nightlight = decode_light(getLight(LIGHTBANK_NIGHT));
216                 u8 mix = ((daylight_factor * daylight
217                         + (1000-daylight_factor) * nightlight)
218                         )/1000;
219                 return mix;
220         }*/
221
222         // In mapnode.cpp
223 #ifndef SERVER
224         /*
225                 Get tile of a face of the node.
226                 dir: direction of face
227                 Returns: TileSpec. Can contain miscellaneous texture coordinates,
228                          which must be obeyed so that the texture atlas can be used.
229         */
230         TileSpec getTile(v3s16 dir);
231 #endif
232         
233         /*
234                 Gets mineral content of node, if there is any.
235                 MINERAL_NONE if doesn't contain or isn't able to contain mineral.
236         */
237         u8 getMineral();
238         
239         /*
240                 Serialization functions
241         */
242
243         static u32 serializedLength(u8 version);
244         void serialize(u8 *dest, u8 version);
245         void deSerialize(u8 *source, u8 version);
246         
247 };
248
249 /*
250         Gets lighting value at face of node
251         
252         Parameters must consist of air and !air.
253         Order doesn't matter.
254
255         If either of the nodes doesn't exist, light is 0.
256         
257         parameters:
258                 daynight_ratio: 0...1000
259                 n: getNode(p) (uses only the lighting value)
260                 n2: getNode(p + face_dir) (uses only the lighting value)
261                 face_dir: axis oriented unit vector from p to p2
262         
263         returns encoded light value.
264 */
265 u8 getFaceLight(u32 daynight_ratio, MapNode n, MapNode n2,
266                 v3s16 face_dir);
267
268 #endif
269