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