]> git.lizzy.rs Git - minetest.git/blob - src/nodedef.h
Properly and efficiently use split utility headers
[minetest.git] / src / nodedef.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 NODEDEF_HEADER
21 #define NODEDEF_HEADER
22
23 #include "irrlichttypes.h"
24 #include <string>
25 #include <iostream>
26 #include <map>
27 #include "mapnode.h"
28 #ifndef SERVER
29 #include "tile.h"
30 #endif
31 #include "itemgroup.h"
32 #include "sound.h" // SimpleSoundSpec
33 #include "constants.h" // BS
34
35 class IItemDefManager;
36 class ITextureSource;
37 class IGameDef;
38
39 enum ContentParamType
40 {
41         CPT_NONE,
42         CPT_LIGHT,
43 };
44
45 enum ContentParamType2
46 {
47         CPT2_NONE,
48         // Need 8-bit param2
49         CPT2_FULL,
50         // Flowing liquid properties
51         CPT2_FLOWINGLIQUID,
52         // Direction for chests and furnaces and such
53         CPT2_FACEDIR,
54         // Direction for signs, torches and such
55         CPT2_WALLMOUNTED,
56 };
57
58 enum LiquidType
59 {
60         LIQUID_NONE,
61         LIQUID_FLOWING,
62         LIQUID_SOURCE
63 };
64
65 enum NodeBoxType
66 {
67         NODEBOX_REGULAR, // Regular block; allows buildable_to
68         NODEBOX_FIXED, // Static separately defined box
69         NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
70 };
71
72 struct NodeBox
73 {
74         enum NodeBoxType type;
75         // NODEBOX_REGULAR (no parameters)
76         // NODEBOX_FIXED
77         core::aabbox3d<f32> fixed;
78         // NODEBOX_WALLMOUNTED
79         core::aabbox3d<f32> wall_top;
80         core::aabbox3d<f32> wall_bottom;
81         core::aabbox3d<f32> wall_side; // being at the -X side
82
83         NodeBox():
84                 type(NODEBOX_REGULAR),
85                 // default is rail-like
86                 fixed(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2),
87                 // default is sign/ladder-like
88                 wall_top(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2),
89                 wall_bottom(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2),
90                 wall_side(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2)
91         {}
92
93         void serialize(std::ostream &os) const;
94         void deSerialize(std::istream &is);
95 };
96
97 struct MapNode;
98 class NodeMetadata;
99
100 /*
101         Stand-alone definition of a TileSpec (basically a server-side TileSpec)
102 */
103 enum TileAnimationType{
104         TAT_NONE=0,
105         TAT_VERTICAL_FRAMES=1,
106 };
107 struct TileDef
108 {
109         std::string name;
110         bool backface_culling; // Takes effect only in special cases
111         struct{
112                 enum TileAnimationType type;
113                 int aspect_w; // width for aspect ratio
114                 int aspect_h; // height for aspect ratio
115                 float length; // seconds
116         } animation;
117
118         TileDef()
119         {
120                 name = "";
121                 backface_culling = true;
122                 animation.type = TAT_NONE;
123                 animation.aspect_w = 1;
124                 animation.aspect_h = 1;
125                 animation.length = 1.0;
126         }
127
128         void serialize(std::ostream &os) const;
129         void deSerialize(std::istream &is);
130 };
131
132 enum NodeDrawType
133 {
134         NDT_NORMAL, // A basic solid block
135         NDT_AIRLIKE, // Nothing is drawn
136         NDT_LIQUID, // Do not draw face towards same kind of flowing/source liquid
137         NDT_FLOWINGLIQUID, // A very special kind of thing
138         NDT_GLASSLIKE, // Glass-like, don't draw faces towards other glass
139         NDT_ALLFACES, // Leaves-like, draw all faces no matter what
140         NDT_ALLFACES_OPTIONAL, // Fancy -> allfaces, fast -> normal
141         NDT_TORCHLIKE,
142         NDT_SIGNLIKE,
143         NDT_PLANTLIKE,
144         NDT_FENCELIKE,
145         NDT_RAILLIKE,
146 };
147
148 #define CF_SPECIAL_COUNT 2
149
150 struct ContentFeatures
151 {
152         /*
153                 Cached stuff
154         */
155 #ifndef SERVER
156         // 0     1     2     3     4     5
157         // up    down  right left  back  front 
158         TileSpec tiles[6];
159         // Special tiles
160         // - Currently used for flowing liquids
161         TileSpec special_tiles[CF_SPECIAL_COUNT];
162         u8 solidness; // Used when choosing which face is drawn
163         u8 visual_solidness; // When solidness=0, this tells how it looks like
164         bool backface_culling;
165 #endif
166
167         // Server-side cached callback existence for fast skipping
168         bool has_on_construct;
169         bool has_on_destruct;
170         bool has_after_destruct;
171
172         /*
173                 Actual data
174         */
175
176         std::string name; // "" = undefined node
177         ItemGroupList groups; // Same as in itemdef
178
179         // Visual definition
180         enum NodeDrawType drawtype;
181         float visual_scale; // Misc. scale parameter
182         TileDef tiledef[6];
183         TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
184         u8 alpha;
185
186         // Post effect color, drawn when the camera is inside the node.
187         video::SColor post_effect_color;
188         // Type of MapNode::param1
189         ContentParamType param_type;
190         // Type of MapNode::param2
191         ContentParamType2 param_type_2;
192         // True for all ground-like things like stone and mud, false for eg. trees
193         bool is_ground_content;
194         bool light_propagates;
195         bool sunlight_propagates;
196         // This is used for collision detection.
197         // Also for general solidness queries.
198         bool walkable;
199         // Player can point to these
200         bool pointable;
201         // Player can dig these
202         bool diggable;
203         // Player can climb these
204         bool climbable;
205         // Player can build on these
206         bool buildable_to;
207         // Whether the node is non-liquid, source liquid or flowing liquid
208         enum LiquidType liquid_type;
209         // If the content is liquid, this is the flowing version of the liquid.
210         std::string liquid_alternative_flowing;
211         // If the content is liquid, this is the source version of the liquid.
212         std::string liquid_alternative_source;
213         // Viscosity for fluid flow, ranging from 1 to 7, with
214         // 1 giving almost instantaneous propagation and 7 being
215         // the slowest possible
216         u8 liquid_viscosity;
217         // Amount of light the node emits
218         u8 light_source;
219         u32 damage_per_second;
220         NodeBox selection_box;
221         // Compatibility with old maps
222         // Set to true if paramtype used to be 'facedir_simple'
223         bool legacy_facedir_simple;
224         // Set to true if wall_mounted used to be set to true
225         bool legacy_wallmounted;
226
227         // Sound properties
228         SimpleSoundSpec sound_footstep;
229         SimpleSoundSpec sound_dig;
230         SimpleSoundSpec sound_dug;
231
232         /*
233                 Methods
234         */
235         
236         ContentFeatures();
237         ~ContentFeatures();
238         void reset();
239         void serialize(std::ostream &os);
240         void deSerialize(std::istream &is);
241
242         /*
243                 Some handy methods
244         */
245         bool isLiquid() const{
246                 return (liquid_type != LIQUID_NONE);
247         }
248         bool sameLiquid(const ContentFeatures &f) const{
249                 if(!isLiquid() || !f.isLiquid()) return false;
250                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
251         }
252 };
253
254 class INodeDefManager
255 {
256 public:
257         INodeDefManager(){}
258         virtual ~INodeDefManager(){}
259         // Get node definition
260         virtual const ContentFeatures& get(content_t c) const=0;
261         virtual const ContentFeatures& get(const MapNode &n) const=0;
262         virtual bool getId(const std::string &name, content_t &result) const=0;
263         virtual content_t getId(const std::string &name) const=0;
264         // Allows "group:name" in addition to regular node names
265         virtual void getIds(const std::string &name, std::set<content_t> &result)
266                         const=0;
267         virtual const ContentFeatures& get(const std::string &name) const=0;
268         
269         virtual void serialize(std::ostream &os)=0;
270 };
271
272 class IWritableNodeDefManager : public INodeDefManager
273 {
274 public:
275         IWritableNodeDefManager(){}
276         virtual ~IWritableNodeDefManager(){}
277         virtual IWritableNodeDefManager* clone()=0;
278         // Get node definition
279         virtual const ContentFeatures& get(content_t c) const=0;
280         virtual const ContentFeatures& get(const MapNode &n) const=0;
281         virtual bool getId(const std::string &name, content_t &result) const=0;
282         virtual content_t getId(const std::string &name) const=0;
283         // Allows "group:name" in addition to regular node names
284         virtual void getIds(const std::string &name, std::set<content_t> &result)
285                         const=0;
286         // If not found, returns the features of CONTENT_IGNORE
287         virtual const ContentFeatures& get(const std::string &name) const=0;
288
289         // Register node definition
290         virtual void set(content_t c, const ContentFeatures &def)=0;
291         // Register node definition by name (allocate an id)
292         // If returns CONTENT_IGNORE, could not allocate id
293         virtual content_t set(const std::string &name,
294                         const ContentFeatures &def)=0;
295         // If returns CONTENT_IGNORE, could not allocate id
296         virtual content_t allocateDummy(const std::string &name)=0;
297
298         /*
299                 Update item alias mapping.
300                 Call after updating item definitions.
301         */
302         virtual void updateAliases(IItemDefManager *idef)=0;
303
304         /*
305                 Update tile textures to latest return values of TextueSource.
306                 Call after updating the texture atlas of a TextureSource.
307         */
308         virtual void updateTextures(ITextureSource *tsrc)=0;
309
310         virtual void serialize(std::ostream &os)=0;
311         virtual void deSerialize(std::istream &is)=0;
312 };
313
314 IWritableNodeDefManager* createNodeDefManager();
315
316 #endif
317