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