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