]> git.lizzy.rs Git - minetest.git/blob - src/mapnode.h
Decoration: Handle facedir and wallmounted param2types with schematic rotation
[minetest.git] / src / mapnode.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 typedef u16 content_t;
38 #define MAX_CONTENT 0xfff
39
40 /*
41         Ignored node.
42
43         Anything that stores MapNodes doesn't have to preserve parameters
44         associated with this material.
45         
46         Doesn't create faces with anything and is considered being
47         out-of-map in the game map.
48 */
49 #define CONTENT_IGNORE 127
50 #define CONTENT_IGNORE_DEFAULT_PARAM 0
51
52 /*
53         The common material through which the player can walk and which
54         is transparent to light
55 */
56 #define CONTENT_AIR 126
57
58 enum LightBank
59 {
60         LIGHTBANK_DAY,
61         LIGHTBANK_NIGHT
62 };
63
64 /*
65         Simple rotation enum.
66 */
67 enum Rotation {
68         ROTATE_0,
69         ROTATE_90,
70         ROTATE_180,
71         ROTATE_270,
72         ROTATE_RAND,
73 };
74
75 /*
76         Masks for MapNode.param2 of flowing liquids
77  */
78 #define LIQUID_LEVEL_MASK 0x07
79 #define LIQUID_FLOW_DOWN_MASK 0x08
80
81 //#define LIQUID_LEVEL_MASK 0x3f // better finite water
82 //#define LIQUID_FLOW_DOWN_MASK 0x40 // not used when finite water
83
84 /* maximum amount of liquid in a block */
85 #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
86 #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
87
88 #define LIQUID_INFINITY_MASK 0x80 //0b10000000
89
90 /*
91         This is the stuff what the whole world consists of.
92 */
93
94
95 struct MapNode
96 {
97         /*
98                 Main content
99         */
100         u16 param0;
101
102         /*
103                 Misc parameter. Initialized to 0.
104                 - For light_propagates() blocks, this is light intensity,
105                   stored logarithmically from 0 to LIGHT_MAX.
106                   Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
107                   - Contains 2 values, day- and night lighting. Each takes 4 bits.
108                 - Uhh... well, most blocks have light or nothing in here.
109         */
110         u8 param1;
111         
112         /*
113                 The second parameter. Initialized to 0.
114                 E.g. direction for torches and flowing water.
115         */
116         u8 param2;
117
118         MapNode(const MapNode & n)
119         {
120                 *this = n;
121         }
122         
123         MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
124         {
125                 param0 = content;
126                 param1 = a_param1;
127                 param2 = a_param2;
128         }
129         
130         // Create directly from a nodename
131         // If name is unknown, sets CONTENT_IGNORE
132         MapNode(INodeDefManager *ndef, const std::string &name,
133                         u8 a_param1=0, u8 a_param2=0);
134
135         bool operator==(const MapNode &other)
136         {
137                 return (param0 == other.param0
138                                 && param1 == other.param1
139                                 && param2 == other.param2);
140         }
141         
142         // To be used everywhere
143         content_t getContent() const
144         {
145                 return param0;
146         }
147         void setContent(content_t c)
148         {
149                 param0 = c;
150         }
151         u8 getParam1() const
152         {
153                 return param1;
154         }
155         void setParam1(u8 p)
156         {
157                 param1 = p;
158         }
159         u8 getParam2() const
160         {
161                 return param2;
162         }
163         void setParam2(u8 p)
164         {
165                 param2 = p;
166         }
167         
168         void setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr);
169         u8 getLight(enum LightBank bank, INodeDefManager *nodemgr) const;
170         bool getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const;
171         
172         // 0 <= daylight_factor <= 1000
173         // 0 <= return value <= LIGHT_SUN
174         u8 getLightBlend(u32 daylight_factor, INodeDefManager *nodemgr) const
175         {
176                 u8 lightday = 0;
177                 u8 lightnight = 0;
178                 getLightBanks(lightday, lightnight, nodemgr);
179                 return blend_light(daylight_factor, lightday, lightnight);
180         }
181
182         // 0.0 <= daylight_factor <= 1.0
183         // 0 <= return value <= LIGHT_SUN
184         u8 getLightBlendF1(float daylight_factor, INodeDefManager *nodemgr) const
185         {
186                 u8 lightday = 0;
187                 u8 lightnight = 0;
188                 getLightBanks(lightday, lightnight, nodemgr);
189                 return blend_light_f1(daylight_factor, lightday, lightnight);
190         }
191
192         u8 getFaceDir(INodeDefManager *nodemgr) const;
193         u8 getWallMounted(INodeDefManager *nodemgr) const;
194         v3s16 getWallMountedDir(INodeDefManager *nodemgr) const;
195         
196         void rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot);
197
198         /*
199                 Gets list of node boxes (used for rendering (NDT_NODEBOX)
200                 and collision)
201         */
202         std::vector<aabb3f> getNodeBoxes(INodeDefManager *nodemgr) const;
203
204         /*
205                 Gets list of selection boxes
206         */
207         std::vector<aabb3f> getSelectionBoxes(INodeDefManager *nodemgr) const;
208
209         /*
210                 Serialization functions
211         */
212
213         static u32 serializedLength(u8 version);
214         void serialize(u8 *dest, u8 version);
215         void deSerialize(u8 *source, u8 version);
216         
217         // Serializes or deserializes a list of nodes in bulk format (first the
218         // content of all nodes, then the param1 of all nodes, then the param2
219         // of all nodes).
220         //   version = serialization version. Must be >= 22
221         //   content_width = the number of bytes of content per node
222         //   params_width = the number of bytes of params per node
223         //   compressed = true to zlib-compress output
224         static void serializeBulk(std::ostream &os, int version,
225                         const MapNode *nodes, u32 nodecount,
226                         u8 content_width, u8 params_width, bool compressed);
227         static void deSerializeBulk(std::istream &is, int version,
228                         MapNode *nodes, u32 nodecount,
229                         u8 content_width, u8 params_width, bool compressed);
230
231 private:
232         // Deprecated serialization methods
233         void deSerialize_pre22(u8 *source, u8 version);
234 };
235
236 #endif
237