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