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