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