]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodedef.h
23100a2ed6ceb2a6cbaac981cac8c0ce2fda90e3
[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 #pragma once
21
22 #include "irrlichttypes_bloated.h"
23 #include <string>
24 #include <iostream>
25 #include <map>
26 #include "mapnode.h"
27 #ifndef SERVER
28 #include "client/tile.h"
29 #include <IMeshManipulator.h>
30 class Client;
31 #endif
32 #include "itemgroup.h"
33 #include "sound.h" // SimpleSoundSpec
34 #include "constants.h" // BS
35 #include "tileanimation.h"
36
37 class INodeDefManager;
38 class IItemDefManager;
39 class ITextureSource;
40 class IShaderSource;
41 class IGameDef;
42 class NodeResolver;
43
44 enum ContentParamType
45 {
46         CPT_NONE,
47         CPT_LIGHT,
48 };
49
50 enum ContentParamType2
51 {
52         CPT2_NONE,
53         // Need 8-bit param2
54         CPT2_FULL,
55         // Flowing liquid properties
56         CPT2_FLOWINGLIQUID,
57         // Direction for chests and furnaces and such
58         CPT2_FACEDIR,
59         // Direction for signs, torches and such
60         CPT2_WALLMOUNTED,
61         // Block level like FLOWINGLIQUID
62         CPT2_LEVELED,
63         // 2D rotation for things like plants
64         CPT2_DEGROTATE,
65         // Mesh options for plants
66         CPT2_MESHOPTIONS,
67         // Index for palette
68         CPT2_COLOR,
69         // 3 bits of palette index, then facedir
70         CPT2_COLORED_FACEDIR,
71         // 5 bits of palette index, then wallmounted
72         CPT2_COLORED_WALLMOUNTED,
73         // Glasslike framed drawtype internal liquid level, param2 values 0 to 63
74         CPT2_GLASSLIKE_LIQUID_LEVEL,
75 };
76
77 enum LiquidType
78 {
79         LIQUID_NONE,
80         LIQUID_FLOWING,
81         LIQUID_SOURCE,
82 };
83
84 enum NodeBoxType
85 {
86         NODEBOX_REGULAR, // Regular block; allows buildable_to
87         NODEBOX_FIXED, // Static separately defined box(es)
88         NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
89         NODEBOX_LEVELED, // Same as fixed, but with dynamic height from param2. for snow, ...
90         NODEBOX_CONNECTED, // optionally draws nodeboxes if a neighbor node attaches
91 };
92
93 struct NodeBox
94 {
95         enum NodeBoxType type;
96         // NODEBOX_REGULAR (no parameters)
97         // NODEBOX_FIXED
98         std::vector<aabb3f> fixed;
99         // NODEBOX_WALLMOUNTED
100         aabb3f wall_top;
101         aabb3f wall_bottom;
102         aabb3f wall_side; // being at the -X side
103         // NODEBOX_CONNECTED
104         std::vector<aabb3f> connect_top;
105         std::vector<aabb3f> connect_bottom;
106         std::vector<aabb3f> connect_front;
107         std::vector<aabb3f> connect_left;
108         std::vector<aabb3f> connect_back;
109         std::vector<aabb3f> connect_right;
110
111         NodeBox()
112         { reset(); }
113
114         void reset();
115         void serialize(std::ostream &os, u16 protocol_version) const;
116         void deSerialize(std::istream &is);
117 };
118
119 struct MapNode;
120 class NodeMetadata;
121
122 enum LeavesStyle {
123         LEAVES_FANCY,
124         LEAVES_SIMPLE,
125         LEAVES_OPAQUE,
126 };
127
128 class TextureSettings {
129 public:
130         LeavesStyle leaves_style;
131         bool opaque_water;
132         bool connected_glass;
133         bool use_normal_texture;
134         bool enable_mesh_cache;
135         bool enable_minimap;
136
137         TextureSettings() = default;
138
139         void readSettings();
140 };
141
142 enum NodeDrawType
143 {
144         // A basic solid block
145         NDT_NORMAL,
146         // Nothing is drawn
147         NDT_AIRLIKE,
148         // Do not draw face towards same kind of flowing/source liquid
149         NDT_LIQUID,
150         // A very special kind of thing
151         NDT_FLOWINGLIQUID,
152         // Glass-like, don't draw faces towards other glass
153         NDT_GLASSLIKE,
154         // Leaves-like, draw all faces no matter what
155         NDT_ALLFACES,
156         // Enabled -> ndt_allfaces, disabled -> ndt_normal
157         NDT_ALLFACES_OPTIONAL,
158         // Single plane perpendicular to a surface
159         NDT_TORCHLIKE,
160         // Single plane parallel to a surface
161         NDT_SIGNLIKE,
162         // 2 vertical planes in a 'X' shape diagonal to XZ axes.
163         // paramtype2 = "meshoptions" allows various forms, sizes and
164         // vertical and horizontal random offsets.
165         NDT_PLANTLIKE,
166         // Fenceposts that connect to neighbouring fenceposts with horizontal bars
167         NDT_FENCELIKE,
168         // Selects appropriate junction texture to connect like rails to
169         // neighbouring raillikes.
170         NDT_RAILLIKE,
171         // Custom Lua-definable structure of multiple cuboids
172         NDT_NODEBOX,
173         // Glass-like, draw connected frames and all visible faces.
174         // param2 > 0 defines 64 levels of internal liquid
175         // Uses 3 textures, one for frames, second for faces,
176         // optional third is a 'special tile' for the liquid.
177         NDT_GLASSLIKE_FRAMED,
178         // Draw faces slightly rotated and only on neighbouring nodes
179         NDT_FIRELIKE,
180         // Enabled -> ndt_glasslike_framed, disabled -> ndt_glasslike
181         NDT_GLASSLIKE_FRAMED_OPTIONAL,
182         // Uses static meshes
183         NDT_MESH,
184         // Combined plantlike-on-solid
185         NDT_PLANTLIKE_ROOTED,
186 };
187
188 // Mesh options for NDT_PLANTLIKE with CPT2_MESHOPTIONS
189 static const u8 MO_MASK_STYLE          = 0x07;
190 static const u8 MO_BIT_RANDOM_OFFSET   = 0x08;
191 static const u8 MO_BIT_SCALE_SQRT2     = 0x10;
192 static const u8 MO_BIT_RANDOM_OFFSET_Y = 0x20;
193 enum PlantlikeStyle {
194         PLANT_STYLE_CROSS,
195         PLANT_STYLE_CROSS2,
196         PLANT_STYLE_STAR,
197         PLANT_STYLE_HASH,
198         PLANT_STYLE_HASH2,
199 };
200
201 /*
202         Stand-alone definition of a TileSpec (basically a server-side TileSpec)
203 */
204
205 struct TileDef
206 {
207         std::string name = "";
208         bool backface_culling = true; // Takes effect only in special cases
209         bool tileable_horizontal = true;
210         bool tileable_vertical = true;
211         //! If true, the tile has its own color.
212         bool has_color = false;
213         //! The color of the tile.
214         video::SColor color = video::SColor(0xFFFFFFFF);
215
216         struct TileAnimationParams animation;
217
218         TileDef()
219         {
220                 animation.type = TAT_NONE;
221         }
222
223         void serialize(std::ostream &os, u16 protocol_version) const;
224         void deSerialize(std::istream &is, u8 contentfeatures_version,
225                 NodeDrawType drawtype);
226 };
227
228 #define CF_SPECIAL_COUNT 6
229
230 struct ContentFeatures
231 {
232         /*
233                 Cached stuff
234          */
235 #ifndef SERVER
236         // 0     1     2     3     4     5
237         // up    down  right left  back  front
238         TileSpec tiles[6];
239         // Special tiles
240         // - Currently used for flowing liquids
241         TileSpec special_tiles[CF_SPECIAL_COUNT];
242         u8 solidness; // Used when choosing which face is drawn
243         u8 visual_solidness; // When solidness=0, this tells how it looks like
244         bool backface_culling;
245 #endif
246
247         // Server-side cached callback existence for fast skipping
248         bool has_on_construct;
249         bool has_on_destruct;
250         bool has_after_destruct;
251
252         /*
253                 Actual data
254          */
255
256         // --- GENERAL PROPERTIES ---
257
258         std::string name; // "" = undefined node
259         ItemGroupList groups; // Same as in itemdef
260         // Type of MapNode::param1
261         ContentParamType param_type;
262         // Type of MapNode::param2
263         ContentParamType2 param_type_2;
264
265         // --- VISUAL PROPERTIES ---
266
267         enum NodeDrawType drawtype;
268         std::string mesh;
269 #ifndef SERVER
270         scene::IMesh *mesh_ptr[24];
271         video::SColor minimap_color;
272 #endif
273         float visual_scale; // Misc. scale parameter
274         TileDef tiledef[6];
275         // These will be drawn over the base tiles.
276         TileDef tiledef_overlay[6];
277         TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
278         // If 255, the node is opaque.
279         // Otherwise it uses texture alpha.
280         u8 alpha;
281         // The color of the node.
282         video::SColor color;
283         std::string palette_name;
284         std::vector<video::SColor> *palette;
285         // Used for waving leaves/plants
286         u8 waving;
287         // for NDT_CONNECTED pairing
288         u8 connect_sides;
289         std::vector<std::string> connects_to;
290         std::vector<content_t> connects_to_ids;
291         // Post effect color, drawn when the camera is inside the node.
292         video::SColor post_effect_color;
293         // Flowing liquid or snow, value = default level
294         u8 leveled;
295
296         // --- LIGHTING-RELATED ---
297
298         bool light_propagates;
299         bool sunlight_propagates;
300         // Amount of light the node emits
301         u8 light_source;
302
303         // --- MAP GENERATION ---
304
305         // True for all ground-like things like stone and mud, false for eg. trees
306         bool is_ground_content;
307
308         // --- INTERACTION PROPERTIES ---
309
310         // This is used for collision detection.
311         // Also for general solidness queries.
312         bool walkable;
313         // Player can point to these
314         bool pointable;
315         // Player can dig these
316         bool diggable;
317         // Player can climb these
318         bool climbable;
319         // Player can build on these
320         bool buildable_to;
321         // Player cannot build to these (placement prediction disabled)
322         bool rightclickable;
323         u32 damage_per_second;
324         // client dig prediction
325         std::string node_dig_prediction;
326
327         // --- LIQUID PROPERTIES ---
328
329         // Whether the node is non-liquid, source liquid or flowing liquid
330         enum LiquidType liquid_type;
331         // If the content is liquid, this is the flowing version of the liquid.
332         std::string liquid_alternative_flowing;
333         // If the content is liquid, this is the source version of the liquid.
334         std::string liquid_alternative_source;
335         // Viscosity for fluid flow, ranging from 1 to 7, with
336         // 1 giving almost instantaneous propagation and 7 being
337         // the slowest possible
338         u8 liquid_viscosity;
339         // Is liquid renewable (new liquid source will be created between 2 existing)
340         bool liquid_renewable;
341         // Number of flowing liquids surrounding source
342         u8 liquid_range;
343         u8 drowning;
344         // Liquids flow into and replace node
345         bool floodable;
346
347         // --- NODEBOXES ---
348
349         NodeBox node_box;
350         NodeBox selection_box;
351         NodeBox collision_box;
352
353         // --- SOUND PROPERTIES ---
354
355         SimpleSoundSpec sound_footstep;
356         SimpleSoundSpec sound_dig;
357         SimpleSoundSpec sound_dug;
358
359         // --- LEGACY ---
360
361         // Compatibility with old maps
362         // Set to true if paramtype used to be 'facedir_simple'
363         bool legacy_facedir_simple;
364         // Set to true if wall_mounted used to be set to true
365         bool legacy_wallmounted;
366
367         /*
368                 Methods
369         */
370
371         ContentFeatures();
372         ~ContentFeatures() = default;
373         void reset();
374         void serialize(std::ostream &os, u16 protocol_version) const;
375         void deSerialize(std::istream &is);
376         void serializeOld(std::ostream &os, u16 protocol_version) const;
377         void deSerializeOld(std::istream &is, int version);
378         /*!
379          * Since vertex alpha is no longer supported, this method
380          * adds opacity directly to the texture pixels.
381          *
382          * \param tiles array of the tile definitions.
383          * \param length length of tiles
384          */
385         void correctAlpha(TileDef *tiles, int length);
386
387         /*
388                 Some handy methods
389         */
390         bool isLiquid() const{
391                 return (liquid_type != LIQUID_NONE);
392         }
393         bool sameLiquid(const ContentFeatures &f) const{
394                 if(!isLiquid() || !f.isLiquid()) return false;
395                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
396         }
397
398         int getGroup(const std::string &group) const
399         {
400                 return itemgroup_get(groups, group);
401         }
402
403 #ifndef SERVER
404         void fillTileAttribs(ITextureSource *tsrc, TileLayer *tile, TileDef *tiledef,
405                 u32 shader_id, bool use_normal_texture, bool backface_culling,
406                 u8 material_type);
407         void updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
408                 scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings);
409 #endif
410 };
411
412 class INodeDefManager {
413 public:
414         INodeDefManager() = default;
415         virtual ~INodeDefManager() = default;
416
417         // Get node definition
418         virtual const ContentFeatures &get(content_t c) const=0;
419         virtual const ContentFeatures &get(const MapNode &n) const=0;
420         virtual bool getId(const std::string &name, content_t &result) const=0;
421         virtual content_t getId(const std::string &name) const=0;
422         // Allows "group:name" in addition to regular node names
423         // returns false if node name not found, true otherwise
424         virtual bool getIds(const std::string &name, std::vector<content_t> &result)
425                         const=0;
426         virtual const ContentFeatures &get(const std::string &name) const=0;
427
428         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
429
430         virtual void pendNodeResolve(NodeResolver *nr)=0;
431         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
432         virtual bool nodeboxConnects(const MapNode from, const MapNode to, u8 connect_face)=0;
433         /*!
434          * Returns the smallest box in node coordinates that
435          * contains all nodes' selection boxes.
436          */
437         virtual core::aabbox3d<s16> getSelectionBoxIntUnion() const=0;
438 };
439
440 class IWritableNodeDefManager : public INodeDefManager {
441 public:
442         IWritableNodeDefManager() = default;
443         virtual ~IWritableNodeDefManager() = default;
444
445         // Get node definition
446         virtual const ContentFeatures &get(content_t c) const=0;
447         virtual const ContentFeatures &get(const MapNode &n) const=0;
448         virtual bool getId(const std::string &name, content_t &result) const=0;
449         // If not found, returns CONTENT_IGNORE
450         virtual content_t getId(const std::string &name) const=0;
451         // Allows "group:name" in addition to regular node names
452         virtual bool getIds(const std::string &name, std::vector<content_t> &result)
453                 const=0;
454         // If not found, returns the features of CONTENT_UNKNOWN
455         virtual const ContentFeatures &get(const std::string &name) const=0;
456
457         // Register node definition by name (allocate an id)
458         // If returns CONTENT_IGNORE, could not allocate id
459         virtual content_t set(const std::string &name,
460                         const ContentFeatures &def)=0;
461         // If returns CONTENT_IGNORE, could not allocate id
462         virtual content_t allocateDummy(const std::string &name)=0;
463         // Remove a node
464         virtual void removeNode(const std::string &name)=0;
465
466         /*
467                 Update item alias mapping.
468                 Call after updating item definitions.
469         */
470         virtual void updateAliases(IItemDefManager *idef)=0;
471
472         /*
473                 Override textures from servers with ones specified in texturepack/override.txt
474         */
475         virtual void applyTextureOverrides(const std::string &override_filepath)=0;
476
477         /*
478                 Update tile textures to latest return values of TextueSource.
479         */
480         virtual void updateTextures(IGameDef *gamedef,
481                 void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
482                 void *progress_cbk_args)=0;
483
484         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
485         virtual void deSerialize(std::istream &is)=0;
486
487         virtual void setNodeRegistrationStatus(bool completed)=0;
488
489         virtual void pendNodeResolve(NodeResolver *nr)=0;
490         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
491         virtual void runNodeResolveCallbacks()=0;
492         virtual void resetNodeResolveState()=0;
493         virtual void mapNodeboxConnections()=0;
494         virtual core::aabbox3d<s16> getSelectionBoxIntUnion() const=0;
495 };
496
497 IWritableNodeDefManager *createNodeDefManager();
498
499 class NodeResolver {
500 public:
501         NodeResolver();
502         virtual ~NodeResolver();
503         virtual void resolveNodeNames() = 0;
504
505         bool getIdFromNrBacklog(content_t *result_out,
506                 const std::string &node_alt, content_t c_fallback);
507         bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
508                 bool all_required=false, content_t c_fallback=CONTENT_IGNORE);
509
510         void nodeResolveInternal();
511
512         u32 m_nodenames_idx = 0;
513         u32 m_nnlistsizes_idx = 0;
514         std::vector<std::string> m_nodenames;
515         std::vector<size_t> m_nnlistsizes;
516         INodeDefManager *m_ndef = nullptr;
517         bool m_resolve_done = false;
518 };