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