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