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