]> git.lizzy.rs Git - minetest.git/blob - src/nodedef.h
Add paramtype2s for 4 horizontal rotations and 64 colors (#11431)
[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 #include "nameidmapping.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 "texture_override.h" // TextureOverride
37 #include "tileanimation.h"
38
39 class IItemDefManager;
40 class ITextureSource;
41 class IShaderSource;
42 class IGameDef;
43 class NodeResolver;
44 #if BUILD_UNITTESTS
45 class TestSchematic;
46 #endif
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 (with axis rotation)
62         CPT2_FACEDIR,
63         // Direction for signs, torches and such
64         CPT2_WALLMOUNTED,
65         // Block level like FLOWINGLIQUID
66         CPT2_LEVELED,
67         // 2D rotation
68         CPT2_DEGROTATE,
69         // Mesh options for plants
70         CPT2_MESHOPTIONS,
71         // Index for palette
72         CPT2_COLOR,
73         // 3 bits of palette index, then facedir
74         CPT2_COLORED_FACEDIR,
75         // 5 bits of palette index, then wallmounted
76         CPT2_COLORED_WALLMOUNTED,
77         // Glasslike framed drawtype internal liquid level, param2 values 0 to 63
78         CPT2_GLASSLIKE_LIQUID_LEVEL,
79         // 3 bits of palette index, then degrotate
80         CPT2_COLORED_DEGROTATE,
81         // Simplified direction for chests and furnaces and such (4 directions)
82         CPT2_4DIR,
83         // 6 bits of palette index, then 4dir
84         CPT2_COLORED_4DIR,
85 };
86
87 enum LiquidType
88 {
89         LIQUID_NONE,
90         LIQUID_FLOWING,
91         LIQUID_SOURCE,
92 };
93
94 enum NodeBoxType
95 {
96         NODEBOX_REGULAR, // Regular block; allows buildable_to
97         NODEBOX_FIXED, // Static separately defined box(es)
98         NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
99         NODEBOX_LEVELED, // Same as fixed, but with dynamic height from param2. for snow, ...
100         NODEBOX_CONNECTED, // optionally draws nodeboxes if a neighbor node attaches
101 };
102
103 struct NodeBoxConnected
104 {
105         std::vector<aabb3f> connect_top;
106         std::vector<aabb3f> connect_bottom;
107         std::vector<aabb3f> connect_front;
108         std::vector<aabb3f> connect_left;
109         std::vector<aabb3f> connect_back;
110         std::vector<aabb3f> connect_right;
111         std::vector<aabb3f> disconnected_top;
112         std::vector<aabb3f> disconnected_bottom;
113         std::vector<aabb3f> disconnected_front;
114         std::vector<aabb3f> disconnected_left;
115         std::vector<aabb3f> disconnected_back;
116         std::vector<aabb3f> disconnected_right;
117         std::vector<aabb3f> disconnected;
118         std::vector<aabb3f> disconnected_sides;
119 };
120
121 struct NodeBox
122 {
123         enum NodeBoxType type;
124         // NODEBOX_REGULAR (no parameters)
125         // NODEBOX_FIXED
126         std::vector<aabb3f> fixed;
127         // NODEBOX_WALLMOUNTED
128         aabb3f wall_top;
129         aabb3f wall_bottom;
130         aabb3f wall_side; // being at the -X side
131         // NODEBOX_CONNECTED
132         // (kept externally to not bloat the structure)
133         std::shared_ptr<NodeBoxConnected> connected;
134
135         NodeBox()
136         { reset(); }
137         ~NodeBox() = default;
138
139         inline NodeBoxConnected &getConnected() {
140                 if (!connected)
141                         connected = std::make_shared<NodeBoxConnected>();
142                 return *connected;
143         }
144         inline const NodeBoxConnected &getConnected() const {
145                 assert(connected);
146                 return *connected;
147         }
148
149         void reset();
150         void serialize(std::ostream &os, u16 protocol_version) const;
151         void deSerialize(std::istream &is);
152 };
153
154 struct MapNode;
155 class NodeMetadata;
156
157 enum LeavesStyle {
158         LEAVES_FANCY,
159         LEAVES_SIMPLE,
160         LEAVES_OPAQUE,
161 };
162
163 enum AutoScale : u8 {
164         AUTOSCALE_DISABLE,
165         AUTOSCALE_ENABLE,
166         AUTOSCALE_FORCE,
167 };
168
169 enum WorldAlignMode : u8 {
170         WORLDALIGN_DISABLE,
171         WORLDALIGN_ENABLE,
172         WORLDALIGN_FORCE,
173         WORLDALIGN_FORCE_NODEBOX,
174 };
175
176 class TextureSettings {
177 public:
178         LeavesStyle leaves_style;
179         WorldAlignMode world_aligned_mode;
180         AutoScale autoscale_mode;
181         int node_texture_size;
182         bool opaque_water;
183         bool connected_glass;
184         bool enable_mesh_cache;
185         bool enable_minimap;
186
187         TextureSettings() = default;
188
189         void readSettings();
190 };
191
192 enum NodeDrawType
193 {
194         // A basic solid block
195         NDT_NORMAL,
196         // Nothing is drawn
197         NDT_AIRLIKE,
198         // Do not draw face towards same kind of flowing/source liquid
199         NDT_LIQUID,
200         // A very special kind of thing
201         NDT_FLOWINGLIQUID,
202         // Glass-like, don't draw faces towards other glass
203         NDT_GLASSLIKE,
204         // Leaves-like, draw all faces no matter what
205         NDT_ALLFACES,
206         // Enabled -> ndt_allfaces, disabled -> ndt_normal
207         NDT_ALLFACES_OPTIONAL,
208         // Single plane perpendicular to a surface
209         NDT_TORCHLIKE,
210         // Single plane parallel to a surface
211         NDT_SIGNLIKE,
212         // 2 vertical planes in a 'X' shape diagonal to XZ axes.
213         // paramtype2 = "meshoptions" allows various forms, sizes and
214         // vertical and horizontal random offsets.
215         NDT_PLANTLIKE,
216         // Fenceposts that connect to neighbouring fenceposts with horizontal bars
217         NDT_FENCELIKE,
218         // Selects appropriate junction texture to connect like rails to
219         // neighbouring raillikes.
220         NDT_RAILLIKE,
221         // Custom Lua-definable structure of multiple cuboids
222         NDT_NODEBOX,
223         // Glass-like, draw connected frames and all visible faces.
224         // param2 > 0 defines 64 levels of internal liquid
225         // Uses 3 textures, one for frames, second for faces,
226         // optional third is a 'special tile' for the liquid.
227         NDT_GLASSLIKE_FRAMED,
228         // Draw faces slightly rotated and only on neighbouring nodes
229         NDT_FIRELIKE,
230         // Enabled -> ndt_glasslike_framed, disabled -> ndt_glasslike
231         NDT_GLASSLIKE_FRAMED_OPTIONAL,
232         // Uses static meshes
233         NDT_MESH,
234         // Combined plantlike-on-solid
235         NDT_PLANTLIKE_ROOTED,
236 };
237
238 // Mesh options for NDT_PLANTLIKE with CPT2_MESHOPTIONS
239 static const u8 MO_MASK_STYLE          = 0x07;
240 static const u8 MO_BIT_RANDOM_OFFSET   = 0x08;
241 static const u8 MO_BIT_SCALE_SQRT2     = 0x10;
242 static const u8 MO_BIT_RANDOM_OFFSET_Y = 0x20;
243 enum PlantlikeStyle {
244         PLANT_STYLE_CROSS,
245         PLANT_STYLE_CROSS2,
246         PLANT_STYLE_STAR,
247         PLANT_STYLE_HASH,
248         PLANT_STYLE_HASH2,
249 };
250
251 enum AlignStyle : u8 {
252         ALIGN_STYLE_NODE,
253         ALIGN_STYLE_WORLD,
254         ALIGN_STYLE_USER_DEFINED,
255 };
256
257 enum AlphaMode : u8 {
258         ALPHAMODE_BLEND,
259         ALPHAMODE_CLIP,
260         ALPHAMODE_OPAQUE,
261         ALPHAMODE_LEGACY_COMPAT, /* means either opaque or clip */
262 };
263
264
265 /*
266         Stand-alone definition of a TileSpec (basically a server-side TileSpec)
267 */
268
269 struct TileDef
270 {
271         std::string name = "";
272         bool backface_culling = true; // Takes effect only in special cases
273         bool tileable_horizontal = true;
274         bool tileable_vertical = true;
275         //! If true, the tile has its own color.
276         bool has_color = false;
277         //! The color of the tile.
278         video::SColor color = video::SColor(0xFFFFFFFF);
279         AlignStyle align_style = ALIGN_STYLE_NODE;
280         u8 scale = 0;
281
282         struct TileAnimationParams animation;
283
284         TileDef()
285         {
286                 animation.type = TAT_NONE;
287         }
288
289         void serialize(std::ostream &os, u16 protocol_version) const;
290         void deSerialize(std::istream &is, NodeDrawType drawtype, u16 protocol_version);
291 };
292
293 // Defines the number of special tiles per nodedef
294 //
295 // NOTE: When changing this value, the enum entries of OverrideTarget and
296 //       parser in TextureOverrideSource must be updated so that all special
297 //       tiles can be overridden.
298 #define CF_SPECIAL_COUNT 6
299
300 struct ContentFeatures
301 {
302         // PROTOCOL_VERSION >= 37. This is legacy and should not be increased anymore, 
303         // write checks that depend directly on the protocol version instead.
304         static const u8 CONTENTFEATURES_VERSION = 13;
305
306         /*
307                 Cached stuff
308          */
309 #ifndef SERVER
310         // 0     1     2     3     4     5
311         // up    down  right left  back  front
312         TileSpec tiles[6];
313         // Special tiles
314         TileSpec special_tiles[CF_SPECIAL_COUNT];
315         u8 solidness; // Used when choosing which face is drawn
316         u8 visual_solidness; // When solidness=0, this tells how it looks like
317         bool backface_culling;
318 #endif
319
320         // Server-side cached callback existence for fast skipping
321         bool has_on_construct;
322         bool has_on_destruct;
323         bool has_after_destruct;
324
325         /*
326                 Actual data
327          */
328
329         // --- GENERAL PROPERTIES ---
330
331         std::string name; // "" = undefined node
332         ItemGroupList groups; // Same as in itemdef
333         // Type of MapNode::param1
334         ContentParamType param_type;
335         // Type of MapNode::param2
336         ContentParamType2 param_type_2;
337
338         // --- VISUAL PROPERTIES ---
339
340         enum NodeDrawType drawtype;
341         std::string mesh;
342 #ifndef SERVER
343         scene::IMesh *mesh_ptr[24];
344         video::SColor minimap_color;
345 #endif
346         float visual_scale; // Misc. scale parameter
347         TileDef tiledef[6];
348         // These will be drawn over the base tiles.
349         TileDef tiledef_overlay[6];
350         TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
351         AlphaMode alpha;
352         // The color of the node.
353         video::SColor color;
354         std::string palette_name;
355         std::vector<video::SColor> *palette;
356         // Used for waving leaves/plants
357         u8 waving;
358         // for NDT_CONNECTED pairing
359         u8 connect_sides;
360         std::vector<std::string> connects_to;
361         std::vector<content_t> connects_to_ids;
362         // Post effect color, drawn when the camera is inside the node.
363         video::SColor post_effect_color;
364         // Flowing liquid or leveled nodebox, value = default level
365         u8 leveled;
366         // Maximum value for leveled nodes
367         u8 leveled_max;
368
369         // --- LIGHTING-RELATED ---
370
371         bool light_propagates;
372         bool sunlight_propagates;
373         // Amount of light the node emits
374         u8 light_source;
375
376         // --- MAP GENERATION ---
377
378         // True for all ground-like things like stone and mud, false for eg. trees
379         bool is_ground_content;
380
381         // --- INTERACTION PROPERTIES ---
382
383         // This is used for collision detection.
384         // Also for general solidness queries.
385         bool walkable;
386         // Player can point to these
387         bool pointable;
388         // Player can dig these
389         bool diggable;
390         // Player can climb these
391         bool climbable;
392         // Player can build on these
393         bool buildable_to;
394         // Player cannot build to these (placement prediction disabled)
395         bool rightclickable;
396         u32 damage_per_second;
397         // client dig prediction
398         std::string node_dig_prediction;
399         // how slow players move through
400         u8 move_resistance = 0;
401
402         // --- LIQUID PROPERTIES ---
403
404         // Whether the node is non-liquid, source liquid or flowing liquid
405         enum LiquidType liquid_type;
406         // If true, movement (e.g. of players) inside this node is liquid-like.
407         bool liquid_move_physics;
408         // If the content is liquid, this is the flowing version of the liquid.
409         std::string liquid_alternative_flowing;
410         content_t liquid_alternative_flowing_id;
411         // If the content is liquid, this is the source version of the liquid.
412         std::string liquid_alternative_source;
413         content_t liquid_alternative_source_id;
414         // Viscosity for fluid flow, ranging from 1 to 7, with
415         // 1 giving almost instantaneous propagation and 7 being
416         // the slowest possible
417         u8 liquid_viscosity;
418         // Is liquid renewable (new liquid source will be created between 2 existing)
419         bool liquid_renewable;
420         // Number of flowing liquids surrounding source
421         u8 liquid_range;
422         u8 drowning;
423         // Liquids flow into and replace node
424         bool floodable;
425
426         // --- NODEBOXES ---
427
428         NodeBox node_box;
429         NodeBox selection_box;
430         NodeBox collision_box;
431
432         // --- SOUND PROPERTIES ---
433
434         SimpleSoundSpec sound_footstep;
435         SimpleSoundSpec sound_dig;
436         SimpleSoundSpec sound_dug;
437
438         // --- LEGACY ---
439
440         // Compatibility with old maps
441         // Set to true if paramtype used to be 'facedir_simple'
442         bool legacy_facedir_simple;
443         // Set to true if wall_mounted used to be set to true
444         bool legacy_wallmounted;
445
446         /*
447                 Methods
448         */
449
450         ContentFeatures();
451         ~ContentFeatures();
452         void reset();
453         void serialize(std::ostream &os, u16 protocol_version) const;
454         void deSerialize(std::istream &is, u16 protocol_version);
455
456         /*
457                 Some handy methods
458         */
459         void setDefaultAlphaMode()
460         {
461                 switch (drawtype) {
462                 case NDT_NORMAL:
463                 case NDT_LIQUID:
464                 case NDT_FLOWINGLIQUID:
465                         alpha = ALPHAMODE_OPAQUE;
466                         break;
467                 case NDT_NODEBOX:
468                 case NDT_MESH:
469                         alpha = ALPHAMODE_LEGACY_COMPAT; // this should eventually be OPAQUE
470                         break;
471                 default:
472                         alpha = ALPHAMODE_CLIP;
473                         break;
474                 }
475         }
476
477         bool needsBackfaceCulling() const
478         {
479                 switch (drawtype) {
480                 case NDT_TORCHLIKE:
481                 case NDT_SIGNLIKE:
482                 case NDT_FIRELIKE:
483                 case NDT_RAILLIKE:
484                 case NDT_PLANTLIKE:
485                 case NDT_PLANTLIKE_ROOTED:
486                 case NDT_MESH:
487                         return false;
488                 default:
489                         return true;
490                 }
491         }
492
493         bool isLiquid() const{
494                 return (liquid_type != LIQUID_NONE);
495         }
496         bool sameLiquid(const ContentFeatures &f) const{
497                 if(!isLiquid() || !f.isLiquid()) return false;
498                 return (liquid_alternative_flowing_id == f.liquid_alternative_flowing_id);
499         }
500
501         bool lightingEquivalent(const ContentFeatures &other) const {
502                 return light_propagates == other.light_propagates
503                                 && sunlight_propagates == other.sunlight_propagates
504                                 && light_source == other.light_source;
505         }
506
507         int getGroup(const std::string &group) const
508         {
509                 return itemgroup_get(groups, group);
510         }
511
512 #ifndef SERVER
513         void updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
514                 scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings);
515 #endif
516
517 private:
518 #ifndef SERVER
519         /*
520          * Checks if any tile texture has any transparent pixels.
521          * Prints a warning and returns true if that is the case, false otherwise.
522          * This is supposed to be used for use_texture_alpha backwards compatibility.
523          */
524         bool textureAlphaCheck(ITextureSource *tsrc, const TileDef *tiles,
525                 int length);
526 #endif
527
528         void setAlphaFromLegacy(u8 legacy_alpha);
529
530         u8 getAlphaForLegacy() const;
531 };
532
533 /*!
534  * @brief This class is for getting the actual properties of nodes from their
535  * content ID.
536  *
537  * @details The nodes on the map are represented by three numbers (see MapNode).
538  * The first number (param0) is the type of a node. All node types have own
539  * properties (see ContentFeatures). This class is for storing and getting the
540  * properties of nodes.
541  * The manager is first filled with registered nodes, then as the game begins,
542  * functions only get `const` pointers to it, to prevent modification of
543  * registered nodes.
544  */
545 class NodeDefManager {
546 public:
547         /*!
548          * Creates a NodeDefManager, and registers three ContentFeatures:
549          * \ref CONTENT_AIR, \ref CONTENT_UNKNOWN and \ref CONTENT_IGNORE.
550          */
551         NodeDefManager();
552         ~NodeDefManager();
553
554         /*!
555          * Returns the properties for the given content type.
556          * @param c content type of a node
557          * @return properties of the given content type, or \ref CONTENT_UNKNOWN
558          * if the given content type is not registered.
559          */
560         inline const ContentFeatures& get(content_t c) const {
561                 return
562                         (c < m_content_features.size() && !m_content_features[c].name.empty()) ?
563                                 m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
564         }
565
566         /*!
567          * Returns the properties of the given node.
568          * @param n a map node
569          * @return properties of the given node or @ref CONTENT_UNKNOWN if the
570          * given content type is not registered.
571          */
572         inline const ContentFeatures& get(const MapNode &n) const {
573                 return get(n.getContent());
574         }
575
576         /*!
577          * Returns the node properties for a node name.
578          * @param name name of a node
579          * @return properties of the given node or @ref CONTENT_UNKNOWN if
580          * not found
581          */
582         const ContentFeatures& get(const std::string &name) const;
583
584         /*!
585          * Returns the content ID for the given name.
586          * @param name a node name
587          * @param[out] result will contain the content ID if found, otherwise
588          * remains unchanged
589          * @return true if the ID was found, false otherwise
590          */
591         bool getId(const std::string &name, content_t &result) const;
592
593         /*!
594          * Returns the content ID for the given name.
595          * @param name a node name
596          * @return ID of the node or @ref CONTENT_IGNORE if not found
597          */
598         content_t getId(const std::string &name) const;
599
600         /*!
601          * Returns the content IDs of the given node name or node group name.
602          * Group names start with "group:".
603          * @param name a node name or node group name
604          * @param[out] result will be appended with matching IDs
605          * @return true if `name` is a valid node name or a (not necessarily
606          * valid) group name
607          */
608         bool getIds(const std::string &name, std::vector<content_t> &result) const;
609
610         /*!
611          * Returns the smallest box in integer node coordinates that
612          * contains all nodes' selection boxes. The returned box might be larger
613          * than the minimal size if the largest node is removed from the manager.
614          */
615         inline core::aabbox3d<s16> getSelectionBoxIntUnion() const {
616                 return m_selection_box_int_union;
617         }
618
619         /*!
620          * Checks whether a node connects to an adjacent node.
621          * @param from the node to be checked
622          * @param to the adjacent node
623          * @param connect_face a bit field indicating which face of the node is
624          * adjacent to the other node.
625          * Bits: +y (least significant), -y, -z, -x, +z, +x (most significant).
626          * @return true if the node connects, false otherwise
627          */
628         bool nodeboxConnects(MapNode from, MapNode to,
629                         u8 connect_face) const;
630
631         /*!
632          * Registers a NodeResolver to wait for the registration of
633          * ContentFeatures. Once the node registration finishes, all
634          * listeners are notified.
635          */
636         void pendNodeResolve(NodeResolver *nr) const;
637
638         /*!
639          * Stops listening to the NodeDefManager.
640          * @return true if the listener was registered before, false otherwise
641          */
642         bool cancelNodeResolveCallback(NodeResolver *nr) const;
643
644         /*!
645          * Registers a new node type with the given name and allocates a new
646          * content ID.
647          * Should not be called with an already existing name.
648          * @param name name of the node, must match with `def.name`.
649          * @param def definition of the registered node type.
650          * @return ID of the registered node or @ref CONTENT_IGNORE if
651          * the function could not allocate an ID.
652          */
653         content_t set(const std::string &name, const ContentFeatures &def);
654
655         /*!
656          * Allocates a blank node ID for the given name.
657          * @param name name of a node
658          * @return allocated ID or @ref CONTENT_IGNORE if could not allocate
659          * an ID.
660          */
661         content_t allocateDummy(const std::string &name);
662
663         /*!
664          * Removes the given node name from the manager.
665          * The node ID will remain in the manager, but won't be linked to any name.
666          * @param name name to be removed
667          */
668         void removeNode(const std::string &name);
669
670         /*!
671          * Regenerates the alias list (a map from names to node IDs).
672          * @param idef the item definition manager containing alias information
673          */
674         void updateAliases(IItemDefManager *idef);
675
676         /*!
677          * Replaces the textures of registered nodes with the ones specified in
678          * the texturepack's override.txt file
679          *
680          * @param overrides the texture overrides
681          */
682         void applyTextureOverrides(const std::vector<TextureOverride> &overrides);
683
684         /*!
685          * Only the client uses this. Loads textures and shaders required for
686          * rendering the nodes.
687          * @param gamedef must be a Client.
688          * @param progress_cbk called each time a node is loaded. Arguments:
689          * `progress_cbk_args`, number of loaded ContentFeatures, number of
690          * total ContentFeatures.
691          * @param progress_cbk_args passed to the callback function
692          */
693         void updateTextures(IGameDef *gamedef, void *progress_cbk_args);
694
695         /*!
696          * Writes the content of this manager to the given output stream.
697          * @param protocol_version Active network protocol version
698          */
699         void serialize(std::ostream &os, u16 protocol_version) const;
700
701         /*!
702          * Restores the manager from a serialized stream.
703          * This clears the previous state.
704          * @param is input stream containing a serialized NodeDefManager
705          * @param protocol_version Active network protocol version
706          */
707         void deSerialize(std::istream &is, u16 protocol_version);
708
709         /*!
710          * Used to indicate that node registration has finished.
711          * @param completed tells whether registration is complete
712          */
713         inline void setNodeRegistrationStatus(bool completed) {
714                 m_node_registration_complete = completed;
715         }
716
717         /*!
718          * Notifies the registered NodeResolver instances that node registration
719          * has finished, then unregisters all listeners.
720          * Must be called after node registration has finished!
721          */
722         void runNodeResolveCallbacks();
723
724         /*!
725          * Sets the registration completion flag to false and unregisters all
726          * NodeResolver instances listening to the manager.
727          */
728         void resetNodeResolveState();
729
730         /*!
731          * Resolves (caches the IDs) cross-references between nodes,
732          * like liquid alternatives.
733          * Must be called after node registration has finished!
734          */
735         void resolveCrossrefs();
736
737 private:
738         /*!
739          * Resets the manager to its initial state.
740          * See the documentation of the constructor.
741          */
742         void clear();
743
744         /*!
745          * Allocates a new content ID, and returns it.
746          * @return the allocated ID or \ref CONTENT_IGNORE if could not allocate
747          */
748         content_t allocateId();
749
750         /*!
751          * Binds the given content ID and node name.
752          * Registers them in \ref m_name_id_mapping and
753          * \ref m_name_id_mapping_with_aliases.
754          * @param i a content ID
755          * @param name a node name
756          */
757         void addNameIdMapping(content_t i, const std::string &name);
758
759         /*!
760          * Removes a content ID from all groups.
761          * Erases content IDs from vectors in \ref m_group_to_items and
762          * removes empty vectors.
763          * @param id Content ID
764          */
765         void eraseIdFromGroups(content_t id);
766
767         /*!
768          * Recalculates m_selection_box_int_union based on
769          * m_selection_box_union.
770          */
771         void fixSelectionBoxIntUnion();
772
773         //! Features indexed by ID.
774         std::vector<ContentFeatures> m_content_features;
775
776         //! A mapping for fast conversion between names and IDs
777         NameIdMapping m_name_id_mapping;
778
779         /*!
780          * Like @ref m_name_id_mapping, but maps only from names to IDs, and
781          * includes aliases too. Updated by \ref updateAliases().
782          * Note: Not serialized.
783          */
784         std::unordered_map<std::string, content_t> m_name_id_mapping_with_aliases;
785
786         /*!
787          * A mapping from group names to a vector of content types that belong
788          * to it. Necessary for a direct lookup in \ref getIds().
789          * Note: Not serialized.
790          */
791         std::unordered_map<std::string, std::vector<content_t>> m_group_to_items;
792
793         /*!
794          * The next ID that might be free to allocate.
795          * It can be allocated already, because \ref CONTENT_AIR,
796          * \ref CONTENT_UNKNOWN and \ref CONTENT_IGNORE are registered when the
797          * manager is initialized, and new IDs are allocated from 0.
798          */
799         content_t m_next_id;
800
801         //! True if all nodes have been registered.
802         bool m_node_registration_complete;
803
804         /*!
805          * The union of all nodes' selection boxes.
806          * Might be larger if big nodes are removed from the manager.
807          */
808         aabb3f m_selection_box_union;
809
810         /*!
811          * The smallest box in integer node coordinates that
812          * contains all nodes' selection boxes.
813          * Might be larger if big nodes are removed from the manager.
814          */
815         core::aabbox3d<s16> m_selection_box_int_union;
816
817         /*!
818          * NodeResolver instances to notify once node registration has finished.
819          * Even constant NodeDefManager instances can register listeners.
820          */
821         mutable std::vector<NodeResolver *> m_pending_resolve_callbacks;
822 };
823
824 NodeDefManager *createNodeDefManager();
825
826 // NodeResolver: Queue for node names which are then translated
827 // to content_t after the NodeDefManager was initialized
828 class NodeResolver {
829 public:
830         NodeResolver();
831         virtual ~NodeResolver();
832         // Callback which is run as soon NodeDefManager is ready
833         virtual void resolveNodeNames() = 0;
834
835         // required because this class is used as mixin for ObjDef
836         void cloneTo(NodeResolver *res) const;
837
838         bool getIdFromNrBacklog(content_t *result_out,
839                 const std::string &node_alt, content_t c_fallback,
840                 bool error_on_fallback = true);
841         bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
842                 bool all_required = false, content_t c_fallback = CONTENT_IGNORE);
843
844         inline bool isResolveDone() const { return m_resolve_done; }
845         void reset(bool resolve_done = false);
846
847         // Vector containing all node names in the resolve "queue"
848         std::vector<std::string> m_nodenames;
849         // Specifies the "set size" of node names which are to be processed
850         // this is used for getIdsFromNrBacklog
851         // TODO: replace or remove
852         std::vector<size_t> m_nnlistsizes;
853
854 protected:
855         friend class NodeDefManager; // m_ndef
856
857         const NodeDefManager *m_ndef = nullptr;
858         // Index of the next "m_nodenames" entry to resolve
859         u32 m_nodenames_idx = 0;
860
861 private:
862 #if BUILD_UNITTESTS
863         // Unittest requires access to m_resolve_done
864         friend class TestSchematic;
865 #endif
866         void nodeResolveInternal();
867
868         // Index of the next "m_nnlistsizes" entry to process
869         u32 m_nnlistsizes_idx = 0;
870         bool m_resolve_done = false;
871 };