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