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