]> git.lizzy.rs Git - minetest.git/blob - src/mapnode.h
Fix wctomb use
[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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 "irrlichttypes.h"
24 #include "irr_v3d.h"
25 #include "irr_aabb3d.h"
26 #include "light.h"
27 #include <vector>
28
29 class INodeDefManager;
30
31 /*
32         Naming scheme:
33         - Material = irrlicht's Material class
34         - Content = (content_t) content of a node
35         - Tile = TileSpec at some side of a node of some content type
36
37         Content ranges:
38           0x000...0x07f: param2 is fully usable
39           0x800...0xfff: param2 lower 4 bits are free
40 */
41 typedef u16 content_t;
42 #define MAX_CONTENT 0xfff
43
44 /*
45         Ignored node.
46
47         Anything that stores MapNodes doesn't have to preserve parameters
48         associated with this material.
49         
50         Doesn't create faces with anything and is considered being
51         out-of-map in the game map.
52 */
53 #define CONTENT_IGNORE 127
54 #define CONTENT_IGNORE_DEFAULT_PARAM 0
55
56 /*
57         The common material through which the player can walk and which
58         is transparent to light
59 */
60 #define CONTENT_AIR 126
61
62 enum LightBank
63 {
64         LIGHTBANK_DAY,
65         LIGHTBANK_NIGHT
66 };
67
68 /*
69         Masks for MapNode.param2 of flowing liquids
70  */
71 #define LIQUID_LEVEL_MASK 0x07
72 #define LIQUID_FLOW_DOWN_MASK 0x08
73
74 /* maximum amount of liquid in a block */
75 #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
76 #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
77
78 /*
79         This is the stuff what the whole world consists of.
80 */
81
82
83 struct MapNode
84 {
85         /*
86                 Main content
87                 0x00-0x7f: Short content type
88                 0x80-0xff: Long content type (param2>>4 makes up low bytes)
89         */
90         u8 param0;
91
92         /*
93                 Misc parameter. Initialized to 0.
94                 - For light_propagates() blocks, this is light intensity,
95                   stored logarithmically from 0 to LIGHT_MAX.
96                   Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
97                   - Contains 2 values, day- and night lighting. Each takes 4 bits.
98                 - Uhh... well, most blocks have light or nothing in here.
99         */
100         u8 param1;
101         
102         /*
103                 The second parameter. Initialized to 0.
104                 E.g. direction for torches and flowing water.
105                 If param0 >= 0x80, bits 0xf0 of this is extended content type data
106         */
107         u8 param2;
108
109         MapNode(const MapNode & n)
110         {
111                 *this = n;
112         }
113         
114         MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
115         {
116                 param1 = a_param1;
117                 param2 = a_param2;
118                 // Set content (param0 and (param2&0xf0)) after other params
119                 // because this needs to override part of param2
120                 setContent(content);
121         }
122         
123         // Create directly from a nodename
124         // If name is unknown, sets CONTENT_IGNORE
125         MapNode(INodeDefManager *ndef, const std::string &name,
126                         u8 a_param1=0, u8 a_param2=0);
127
128         bool operator==(const MapNode &other)
129         {
130                 return (param0 == other.param0
131                                 && param1 == other.param1
132                                 && param2 == other.param2);
133         }
134         
135         // To be used everywhere
136         content_t getContent() const
137         {
138                 if(param0 < 0x80)
139                         return param0;
140                 else
141                         return (param0<<4) + (param2>>4);
142         }
143         void setContent(content_t c)
144         {
145                 if(c < 0x80)
146                 {
147                         if(param0 >= 0x80)
148                                 param2 &= ~(0xf0);
149                         param0 = c;
150                 }
151                 else
152                 {
153                         param0 = c>>4;
154                         param2 &= ~(0xf0);
155                         param2 |= (c&0x0f)<<4;
156                 }
157         }
158         u8 getParam1() const
159         {
160                 return param1;
161         }
162         void setParam1(u8 p)
163         {
164                 param1 = p;
165         }
166         u8 getParam2() const
167         {
168                 if(param0 < 0x80)
169                         return param2;
170                 else
171                         return param2 & 0x0f;
172         }
173         void setParam2(u8 p)
174         {
175                 if(param0 < 0x80)
176                         param2 = p;
177                 else{
178                         param2 &= 0xf0;
179                         param2 |= (p&0x0f);
180                 }
181         }
182         
183         void setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr);
184         u8 getLight(enum LightBank bank, INodeDefManager *nodemgr) const;
185         bool getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const;
186         
187         // 0 <= daylight_factor <= 1000
188         // 0 <= return value <= LIGHT_SUN
189         u8 getLightBlend(u32 daylight_factor, INodeDefManager *nodemgr) const
190         {
191                 u8 lightday = 0;
192                 u8 lightnight = 0;
193                 getLightBanks(lightday, lightnight, nodemgr);
194                 return blend_light(daylight_factor, lightday, lightnight);
195         }
196
197         u8 getFaceDir(INodeDefManager *nodemgr) const;
198         u8 getWallMounted(INodeDefManager *nodemgr) const;
199         v3s16 getWallMountedDir(INodeDefManager *nodemgr) const;
200
201         /*
202                 Gets list of node boxes (used for rendering (NDT_NODEBOX)
203                 and collision)
204         */
205         std::vector<aabb3f> getNodeBoxes(INodeDefManager *nodemgr) const;
206
207         /*
208                 Gets list of selection boxes
209         */
210         std::vector<aabb3f> getSelectionBoxes(INodeDefManager *nodemgr) const;
211
212         /*
213                 Serialization functions
214         */
215
216         static u32 serializedLength(u8 version);
217         void serialize(u8 *dest, u8 version);
218         void deSerialize(u8 *source, u8 version);
219         
220         // Serializes or deserializes a list of nodes in bulk format (first the
221         // content of all nodes, then the param1 of all nodes, then the param2
222         // of all nodes).
223         //   version = serialization version. Must be >= 22
224         //   content_width = the number of bytes of content per node
225         //   params_width = the number of bytes of params per node
226         //   compressed = true to zlib-compress output
227         static void serializeBulk(std::ostream &os, int version,
228                         const MapNode *nodes, u32 nodecount,
229                         u8 content_width, u8 params_width, bool compressed);
230         static void deSerializeBulk(std::istream &is, int version,
231                         MapNode *nodes, u32 nodecount,
232                         u8 content_width, u8 params_width, bool compressed);
233
234 private:
235         // Deprecated serialization methods
236         void serialize_pre22(u8 *dest, u8 version);
237         void deSerialize_pre22(u8 *source, u8 version);
238 };
239
240 #endif
241