]> git.lizzy.rs Git - minetest.git/blob - src/nodedef.h
Liquids: Flow into and destroy 'floodable' nodes
[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         // Liquids flow into and replace node
233         bool floodable;
234         // Player cannot build to these (placement prediction disabled)
235         bool rightclickable;
236         // Flowing liquid or snow, value = default level
237         u8 leveled;
238         // Whether the node is non-liquid, source liquid or flowing liquid
239         enum LiquidType liquid_type;
240         // If the content is liquid, this is the flowing version of the liquid.
241         std::string liquid_alternative_flowing;
242         // If the content is liquid, this is the source version of the liquid.
243         std::string liquid_alternative_source;
244         // Viscosity for fluid flow, ranging from 1 to 7, with
245         // 1 giving almost instantaneous propagation and 7 being
246         // the slowest possible
247         u8 liquid_viscosity;
248         // Is liquid renewable (new liquid source will be created between 2 existing)
249         bool liquid_renewable;
250         // Number of flowing liquids surrounding source
251         u8 liquid_range;
252         u8 drowning;
253         // Amount of light the node emits
254         u8 light_source;
255         u32 damage_per_second;
256         NodeBox node_box;
257         NodeBox selection_box;
258         NodeBox collision_box;
259         // Used for waving leaves/plants
260         u8 waving;
261         // Compatibility with old maps
262         // Set to true if paramtype used to be 'facedir_simple'
263         bool legacy_facedir_simple;
264         // Set to true if wall_mounted used to be set to true
265         bool legacy_wallmounted;
266
267         // Sound properties
268         SimpleSoundSpec sound_footstep;
269         SimpleSoundSpec sound_dig;
270         SimpleSoundSpec sound_dug;
271
272         /*
273                 Methods
274         */
275
276         ContentFeatures();
277         ~ContentFeatures();
278         void reset();
279         void serialize(std::ostream &os, u16 protocol_version) const;
280         void deSerialize(std::istream &is);
281         void serializeOld(std::ostream &os, u16 protocol_version) const;
282         void deSerializeOld(std::istream &is, int version);
283
284         /*
285                 Some handy methods
286         */
287         bool isLiquid() const{
288                 return (liquid_type != LIQUID_NONE);
289         }
290         bool sameLiquid(const ContentFeatures &f) const{
291                 if(!isLiquid() || !f.isLiquid()) return false;
292                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
293         }
294 };
295
296 class INodeDefManager {
297 public:
298         INodeDefManager(){}
299         virtual ~INodeDefManager(){}
300         // Get node definition
301         virtual const ContentFeatures &get(content_t c) const=0;
302         virtual const ContentFeatures &get(const MapNode &n) const=0;
303         virtual bool getId(const std::string &name, content_t &result) const=0;
304         virtual content_t getId(const std::string &name) const=0;
305         // Allows "group:name" in addition to regular node names
306         virtual void getIds(const std::string &name, std::set<content_t> &result)
307                         const=0;
308         virtual const ContentFeatures &get(const std::string &name) const=0;
309
310         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
311
312         virtual bool getNodeRegistrationStatus() const=0;
313
314         virtual void pendNodeResolve(NodeResolver *nr)=0;
315         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
316 };
317
318 class IWritableNodeDefManager : public INodeDefManager {
319 public:
320         IWritableNodeDefManager(){}
321         virtual ~IWritableNodeDefManager(){}
322         virtual IWritableNodeDefManager* clone()=0;
323         // Get node definition
324         virtual const ContentFeatures &get(content_t c) const=0;
325         virtual const ContentFeatures &get(const MapNode &n) const=0;
326         virtual bool getId(const std::string &name, content_t &result) const=0;
327         // If not found, returns CONTENT_IGNORE
328         virtual content_t getId(const std::string &name) const=0;
329         // Allows "group:name" in addition to regular node names
330         virtual void getIds(const std::string &name, std::set<content_t> &result)
331                 const=0;
332         // If not found, returns the features of CONTENT_UNKNOWN
333         virtual const ContentFeatures &get(const std::string &name) const=0;
334
335         // Register node definition by name (allocate an id)
336         // If returns CONTENT_IGNORE, could not allocate id
337         virtual content_t set(const std::string &name,
338                         const ContentFeatures &def)=0;
339         // If returns CONTENT_IGNORE, could not allocate id
340         virtual content_t allocateDummy(const std::string &name)=0;
341
342         /*
343                 Update item alias mapping.
344                 Call after updating item definitions.
345         */
346         virtual void updateAliases(IItemDefManager *idef)=0;
347
348         /*
349                 Override textures from servers with ones specified in texturepack/override.txt
350         */
351         virtual void applyTextureOverrides(const std::string &override_filepath)=0;
352
353         /*
354                 Update tile textures to latest return values of TextueSource.
355         */
356         virtual void updateTextures(IGameDef *gamedef,
357                 void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
358                 void *progress_cbk_args)=0;
359
360         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
361         virtual void deSerialize(std::istream &is)=0;
362
363         virtual bool getNodeRegistrationStatus() const=0;
364         virtual void setNodeRegistrationStatus(bool completed)=0;
365
366         virtual void pendNodeResolve(NodeResolver *nr)=0;
367         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
368         virtual void runNodeResolveCallbacks()=0;
369         virtual void resetNodeResolveState()=0;
370 };
371
372 IWritableNodeDefManager *createNodeDefManager();
373
374 class NodeResolver {
375 public:
376         NodeResolver();
377         virtual ~NodeResolver();
378         virtual void resolveNodeNames() = 0;
379
380         bool getIdFromNrBacklog(content_t *result_out,
381                 const std::string &node_alt, content_t c_fallback);
382         bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
383                 bool all_required=false, content_t c_fallback=CONTENT_IGNORE);
384
385         void nodeResolveInternal();
386
387         u32 m_nodenames_idx;
388         u32 m_nnlistsizes_idx;
389         std::vector<std::string> m_nodenames;
390         std::vector<size_t> m_nnlistsizes;
391         INodeDefManager *m_ndef;
392         bool m_resolve_done;
393 };
394
395 #endif