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