nodedef.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes_bloated.h"
  18. #include <string>
  19. #include <iostream>
  20. #include <map>
  21. #include "mapnode.h"
  22. #include "nameidmapping.h"
  23. #ifndef SERVER
  24. #include "client/tile.h"
  25. #include <IMeshManipulator.h>
  26. class Client;
  27. #endif
  28. #include "itemgroup.h"
  29. #include "sound.h" // SimpleSoundSpec
  30. #include "constants.h" // BS
  31. #include "tileanimation.h"
  32. // PROTOCOL_VERSION >= 37
  33. static const u8 CONTENTFEATURES_VERSION = 13;
  34. class IItemDefManager;
  35. class ITextureSource;
  36. class IShaderSource;
  37. class IGameDef;
  38. class NodeResolver;
  39. enum ContentParamType
  40. {
  41. CPT_NONE,
  42. CPT_LIGHT,
  43. };
  44. enum ContentParamType2
  45. {
  46. CPT2_NONE,
  47. // Need 8-bit param2
  48. CPT2_FULL,
  49. // Flowing liquid properties
  50. CPT2_FLOWINGLIQUID,
  51. // Direction for chests and furnaces and such
  52. CPT2_FACEDIR,
  53. // Direction for signs, torches and such
  54. CPT2_WALLMOUNTED,
  55. // Block level like FLOWINGLIQUID
  56. CPT2_LEVELED,
  57. // 2D rotation for things like plants
  58. CPT2_DEGROTATE,
  59. // Mesh options for plants
  60. CPT2_MESHOPTIONS,
  61. // Index for palette
  62. CPT2_COLOR,
  63. // 3 bits of palette index, then facedir
  64. CPT2_COLORED_FACEDIR,
  65. // 5 bits of palette index, then wallmounted
  66. CPT2_COLORED_WALLMOUNTED,
  67. // Glasslike framed drawtype internal liquid level, param2 values 0 to 63
  68. CPT2_GLASSLIKE_LIQUID_LEVEL,
  69. };
  70. enum LiquidType
  71. {
  72. LIQUID_NONE,
  73. LIQUID_FLOWING,
  74. LIQUID_SOURCE,
  75. };
  76. enum NodeBoxType
  77. {
  78. NODEBOX_REGULAR, // Regular block; allows buildable_to
  79. NODEBOX_FIXED, // Static separately defined box(es)
  80. NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
  81. NODEBOX_LEVELED, // Same as fixed, but with dynamic height from param2. for snow, ...
  82. NODEBOX_CONNECTED, // optionally draws nodeboxes if a neighbor node attaches
  83. };
  84. struct NodeBox
  85. {
  86. enum NodeBoxType type;
  87. // NODEBOX_REGULAR (no parameters)
  88. // NODEBOX_FIXED
  89. std::vector<aabb3f> fixed;
  90. // NODEBOX_WALLMOUNTED
  91. aabb3f wall_top;
  92. aabb3f wall_bottom;
  93. aabb3f wall_side; // being at the -X side
  94. // NODEBOX_CONNECTED
  95. std::vector<aabb3f> connect_top;
  96. std::vector<aabb3f> connect_bottom;
  97. std::vector<aabb3f> connect_front;
  98. std::vector<aabb3f> connect_left;
  99. std::vector<aabb3f> connect_back;
  100. std::vector<aabb3f> connect_right;
  101. std::vector<aabb3f> disconnected_top;
  102. std::vector<aabb3f> disconnected_bottom;
  103. std::vector<aabb3f> disconnected_front;
  104. std::vector<aabb3f> disconnected_left;
  105. std::vector<aabb3f> disconnected_back;
  106. std::vector<aabb3f> disconnected_right;
  107. std::vector<aabb3f> disconnected;
  108. std::vector<aabb3f> disconnected_sides;
  109. NodeBox()
  110. { reset(); }
  111. void reset();
  112. void serialize(std::ostream &os, u16 protocol_version) const;
  113. void deSerialize(std::istream &is);
  114. };
  115. struct MapNode;
  116. class NodeMetadata;
  117. enum LeavesStyle {
  118. LEAVES_FANCY,
  119. LEAVES_SIMPLE,
  120. LEAVES_OPAQUE,
  121. };
  122. enum AutoScale : u8 {
  123. AUTOSCALE_DISABLE,
  124. AUTOSCALE_ENABLE,
  125. AUTOSCALE_FORCE,
  126. };
  127. enum WorldAlignMode : u8 {
  128. WORLDALIGN_DISABLE,
  129. WORLDALIGN_ENABLE,
  130. WORLDALIGN_FORCE,
  131. WORLDALIGN_FORCE_NODEBOX,
  132. };
  133. class TextureSettings {
  134. public:
  135. LeavesStyle leaves_style;
  136. WorldAlignMode world_aligned_mode;
  137. AutoScale autoscale_mode;
  138. int node_texture_size;
  139. bool opaque_water;
  140. bool connected_glass;
  141. bool use_normal_texture;
  142. bool enable_mesh_cache;
  143. bool enable_minimap;
  144. TextureSettings() = default;
  145. void readSettings();
  146. };
  147. enum NodeDrawType
  148. {
  149. // A basic solid block
  150. NDT_NORMAL,
  151. // Nothing is drawn
  152. NDT_AIRLIKE,
  153. // Do not draw face towards same kind of flowing/source liquid
  154. NDT_LIQUID,
  155. // A very special kind of thing
  156. NDT_FLOWINGLIQUID,
  157. // Glass-like, don't draw faces towards other glass
  158. NDT_GLASSLIKE,
  159. // Leaves-like, draw all faces no matter what
  160. NDT_ALLFACES,
  161. // Enabled -> ndt_allfaces, disabled -> ndt_normal
  162. NDT_ALLFACES_OPTIONAL,
  163. // Single plane perpendicular to a surface
  164. NDT_TORCHLIKE,
  165. // Single plane parallel to a surface
  166. NDT_SIGNLIKE,
  167. // 2 vertical planes in a 'X' shape diagonal to XZ axes.
  168. // paramtype2 = "meshoptions" allows various forms, sizes and
  169. // vertical and horizontal random offsets.
  170. NDT_PLANTLIKE,
  171. // Fenceposts that connect to neighbouring fenceposts with horizontal bars
  172. NDT_FENCELIKE,
  173. // Selects appropriate junction texture to connect like rails to
  174. // neighbouring raillikes.
  175. NDT_RAILLIKE,
  176. // Custom Lua-definable structure of multiple cuboids
  177. NDT_NODEBOX,
  178. // Glass-like, draw connected frames and all visible faces.
  179. // param2 > 0 defines 64 levels of internal liquid
  180. // Uses 3 textures, one for frames, second for faces,
  181. // optional third is a 'special tile' for the liquid.
  182. NDT_GLASSLIKE_FRAMED,
  183. // Draw faces slightly rotated and only on neighbouring nodes
  184. NDT_FIRELIKE,
  185. // Enabled -> ndt_glasslike_framed, disabled -> ndt_glasslike
  186. NDT_GLASSLIKE_FRAMED_OPTIONAL,
  187. // Uses static meshes
  188. NDT_MESH,
  189. // Combined plantlike-on-solid
  190. NDT_PLANTLIKE_ROOTED,
  191. };
  192. // Mesh options for NDT_PLANTLIKE with CPT2_MESHOPTIONS
  193. static const u8 MO_MASK_STYLE = 0x07;
  194. static const u8 MO_BIT_RANDOM_OFFSET = 0x08;
  195. static const u8 MO_BIT_SCALE_SQRT2 = 0x10;
  196. static const u8 MO_BIT_RANDOM_OFFSET_Y = 0x20;
  197. enum PlantlikeStyle {
  198. PLANT_STYLE_CROSS,
  199. PLANT_STYLE_CROSS2,
  200. PLANT_STYLE_STAR,
  201. PLANT_STYLE_HASH,
  202. PLANT_STYLE_HASH2,
  203. };
  204. enum AlignStyle : u8 {
  205. ALIGN_STYLE_NODE,
  206. ALIGN_STYLE_WORLD,
  207. ALIGN_STYLE_USER_DEFINED,
  208. };
  209. /*
  210. Stand-alone definition of a TileSpec (basically a server-side TileSpec)
  211. */
  212. struct TileDef
  213. {
  214. std::string name = "";
  215. bool backface_culling = true; // Takes effect only in special cases
  216. bool tileable_horizontal = true;
  217. bool tileable_vertical = true;
  218. //! If true, the tile has its own color.
  219. bool has_color = false;
  220. //! The color of the tile.
  221. video::SColor color = video::SColor(0xFFFFFFFF);
  222. AlignStyle align_style = ALIGN_STYLE_NODE;
  223. u8 scale = 0;
  224. struct TileAnimationParams animation;
  225. TileDef()
  226. {
  227. animation.type = TAT_NONE;
  228. }
  229. void serialize(std::ostream &os, u16 protocol_version) const;
  230. void deSerialize(std::istream &is, u8 contentfeatures_version,
  231. NodeDrawType drawtype);
  232. };
  233. #define CF_SPECIAL_COUNT 6
  234. struct ContentFeatures
  235. {
  236. /*
  237. Cached stuff
  238. */
  239. #ifndef SERVER
  240. // 0 1 2 3 4 5
  241. // up down right left back front
  242. TileSpec tiles[6];
  243. // Special tiles
  244. // - Currently used for flowing liquids
  245. TileSpec special_tiles[CF_SPECIAL_COUNT];
  246. u8 solidness; // Used when choosing which face is drawn
  247. u8 visual_solidness; // When solidness=0, this tells how it looks like
  248. bool backface_culling;
  249. #endif
  250. // Server-side cached callback existence for fast skipping
  251. bool has_on_construct;
  252. bool has_on_destruct;
  253. bool has_after_destruct;
  254. /*
  255. Actual data
  256. */
  257. // --- GENERAL PROPERTIES ---
  258. std::string name; // "" = undefined node
  259. ItemGroupList groups; // Same as in itemdef
  260. // Type of MapNode::param1
  261. ContentParamType param_type;
  262. // Type of MapNode::param2
  263. ContentParamType2 param_type_2;
  264. // --- VISUAL PROPERTIES ---
  265. enum NodeDrawType drawtype;
  266. std::string mesh;
  267. #ifndef SERVER
  268. scene::IMesh *mesh_ptr[24];
  269. video::SColor minimap_color;
  270. #endif
  271. float visual_scale; // Misc. scale parameter
  272. TileDef tiledef[6];
  273. // These will be drawn over the base tiles.
  274. TileDef tiledef_overlay[6];
  275. TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
  276. // If 255, the node is opaque.
  277. // Otherwise it uses texture alpha.
  278. u8 alpha;
  279. // The color of the node.
  280. video::SColor color;
  281. std::string palette_name;
  282. std::vector<video::SColor> *palette;
  283. // Used for waving leaves/plants
  284. u8 waving;
  285. // for NDT_CONNECTED pairing
  286. u8 connect_sides;
  287. std::vector<std::string> connects_to;
  288. std::vector<content_t> connects_to_ids;
  289. // Post effect color, drawn when the camera is inside the node.
  290. video::SColor post_effect_color;
  291. // Flowing liquid or snow, value = default level
  292. u8 leveled;
  293. // --- LIGHTING-RELATED ---
  294. bool light_propagates;
  295. bool sunlight_propagates;
  296. // Amount of light the node emits
  297. u8 light_source;
  298. // --- MAP GENERATION ---
  299. // True for all ground-like things like stone and mud, false for eg. trees
  300. bool is_ground_content;
  301. // --- INTERACTION PROPERTIES ---
  302. // This is used for collision detection.
  303. // Also for general solidness queries.
  304. bool walkable;
  305. // Player can point to these
  306. bool pointable;
  307. // Player can dig these
  308. bool diggable;
  309. // Player can climb these
  310. bool climbable;
  311. // Player can build on these
  312. bool buildable_to;
  313. // Player cannot build to these (placement prediction disabled)
  314. bool rightclickable;
  315. u32 damage_per_second;
  316. // client dig prediction
  317. std::string node_dig_prediction;
  318. // --- LIQUID PROPERTIES ---
  319. // Whether the node is non-liquid, source liquid or flowing liquid
  320. enum LiquidType liquid_type;
  321. // If the content is liquid, this is the flowing version of the liquid.
  322. std::string liquid_alternative_flowing;
  323. // If the content is liquid, this is the source version of the liquid.
  324. std::string liquid_alternative_source;
  325. // Viscosity for fluid flow, ranging from 1 to 7, with
  326. // 1 giving almost instantaneous propagation and 7 being
  327. // the slowest possible
  328. u8 liquid_viscosity;
  329. // Is liquid renewable (new liquid source will be created between 2 existing)
  330. bool liquid_renewable;
  331. // Number of flowing liquids surrounding source
  332. u8 liquid_range;
  333. u8 drowning;
  334. // Liquids flow into and replace node
  335. bool floodable;
  336. // --- NODEBOXES ---
  337. NodeBox node_box;
  338. NodeBox selection_box;
  339. NodeBox collision_box;
  340. // --- SOUND PROPERTIES ---
  341. SimpleSoundSpec sound_footstep;
  342. SimpleSoundSpec sound_dig;
  343. SimpleSoundSpec sound_dug;
  344. // --- LEGACY ---
  345. // Compatibility with old maps
  346. // Set to true if paramtype used to be 'facedir_simple'
  347. bool legacy_facedir_simple;
  348. // Set to true if wall_mounted used to be set to true
  349. bool legacy_wallmounted;
  350. /*
  351. Methods
  352. */
  353. ContentFeatures();
  354. ~ContentFeatures() = default;
  355. void reset();
  356. void serialize(std::ostream &os, u16 protocol_version) const;
  357. void deSerialize(std::istream &is);
  358. /*!
  359. * Since vertex alpha is no longer supported, this method
  360. * adds opacity directly to the texture pixels.
  361. *
  362. * \param tiles array of the tile definitions.
  363. * \param length length of tiles
  364. */
  365. void correctAlpha(TileDef *tiles, int length);
  366. /*
  367. Some handy methods
  368. */
  369. bool isLiquid() const{
  370. return (liquid_type != LIQUID_NONE);
  371. }
  372. bool sameLiquid(const ContentFeatures &f) const{
  373. if(!isLiquid() || !f.isLiquid()) return false;
  374. return (liquid_alternative_flowing == f.liquid_alternative_flowing);
  375. }
  376. int getGroup(const std::string &group) const
  377. {
  378. return itemgroup_get(groups, group);
  379. }
  380. #ifndef SERVER
  381. void updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
  382. scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings);
  383. #endif
  384. };
  385. /*!
  386. * @brief This class is for getting the actual properties of nodes from their
  387. * content ID.
  388. *
  389. * @details The nodes on the map are represented by three numbers (see MapNode).
  390. * The first number (param0) is the type of a node. All node types have own
  391. * properties (see ContentFeatures). This class is for storing and getting the
  392. * properties of nodes.
  393. * The manager is first filled with registered nodes, then as the game begins,
  394. * functions only get `const` pointers to it, to prevent modification of
  395. * registered nodes.
  396. */
  397. class NodeDefManager {
  398. public:
  399. /*!
  400. * Creates a NodeDefManager, and registers three ContentFeatures:
  401. * \ref CONTENT_AIR, \ref CONTENT_UNKNOWN and \ref CONTENT_IGNORE.
  402. */
  403. NodeDefManager();
  404. ~NodeDefManager();
  405. /*!
  406. * Returns the properties for the given content type.
  407. * @param c content type of a node
  408. * @return properties of the given content type, or \ref CONTENT_UNKNOWN
  409. * if the given content type is not registered.
  410. */
  411. inline const ContentFeatures& get(content_t c) const {
  412. return
  413. c < m_content_features.size() ?
  414. m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
  415. }
  416. /*!
  417. * Returns the properties of the given node.
  418. * @param n a map node
  419. * @return properties of the given node or @ref CONTENT_UNKNOWN if the
  420. * given content type is not registered.
  421. */
  422. inline const ContentFeatures& get(const MapNode &n) const {
  423. return get(n.getContent());
  424. }
  425. /*!
  426. * Returns the node properties for a node name.
  427. * @param name name of a node
  428. * @return properties of the given node or @ref CONTENT_UNKNOWN if
  429. * not found
  430. */
  431. const ContentFeatures& get(const std::string &name) const;
  432. /*!
  433. * Returns the content ID for the given name.
  434. * @param name a node name
  435. * @param[out] result will contain the content ID if found, otherwise
  436. * remains unchanged
  437. * @return true if the ID was found, false otherwise
  438. */
  439. bool getId(const std::string &name, content_t &result) const;
  440. /*!
  441. * Returns the content ID for the given name.
  442. * @param name a node name
  443. * @return ID of the node or @ref CONTENT_IGNORE if not found
  444. */
  445. content_t getId(const std::string &name) const;
  446. /*!
  447. * Returns the content IDs of the given node name or node group name.
  448. * Group names start with "group:".
  449. * @param name a node name or node group name
  450. * @param[out] result will be appended with matching IDs
  451. * @return true if `name` is a valid node name or a (not necessarily
  452. * valid) group name
  453. */
  454. bool getIds(const std::string &name, std::vector<content_t> &result) const;
  455. /*!
  456. * Returns the smallest box in integer node coordinates that
  457. * contains all nodes' selection boxes. The returned box might be larger
  458. * than the minimal size if the largest node is removed from the manager.
  459. */
  460. inline core::aabbox3d<s16> getSelectionBoxIntUnion() const {
  461. return m_selection_box_int_union;
  462. }
  463. /*!
  464. * Checks whether a node connects to an adjacent node.
  465. * @param from the node to be checked
  466. * @param to the adjacent node
  467. * @param connect_face a bit field indicating which face of the node is
  468. * adjacent to the other node.
  469. * Bits: +y (least significant), -y, -z, -x, +z, +x (most significant).
  470. * @return true if the node connects, false otherwise
  471. */
  472. bool nodeboxConnects(MapNode from, MapNode to,
  473. u8 connect_face) const;
  474. /*!
  475. * Registers a NodeResolver to wait for the registration of
  476. * ContentFeatures. Once the node registration finishes, all
  477. * listeners are notified.
  478. */
  479. void pendNodeResolve(NodeResolver *nr) const;
  480. /*!
  481. * Stops listening to the NodeDefManager.
  482. * @return true if the listener was registered before, false otherwise
  483. */
  484. bool cancelNodeResolveCallback(NodeResolver *nr) const;
  485. /*!
  486. * Registers a new node type with the given name and allocates a new
  487. * content ID.
  488. * Should not be called with an already existing name.
  489. * @param name name of the node, must match with `def.name`.
  490. * @param def definition of the registered node type.
  491. * @return ID of the registered node or @ref CONTENT_IGNORE if
  492. * the function could not allocate an ID.
  493. */
  494. content_t set(const std::string &name, const ContentFeatures &def);
  495. /*!
  496. * Allocates a blank node ID for the given name.
  497. * @param name name of a node
  498. * @return allocated ID or @ref CONTENT_IGNORE if could not allocate
  499. * an ID.
  500. */
  501. content_t allocateDummy(const std::string &name);
  502. /*!
  503. * Removes the given node name from the manager.
  504. * The node ID will remain in the manager, but won't be linked to any name.
  505. * @param name name to be removed
  506. */
  507. void removeNode(const std::string &name);
  508. /*!
  509. * Regenerates the alias list (a map from names to node IDs).
  510. * @param idef the item definition manager containing alias information
  511. */
  512. void updateAliases(IItemDefManager *idef);
  513. /*!
  514. * Reads the used texture pack's override.txt, and replaces the textures
  515. * of registered nodes with the ones specified there.
  516. *
  517. * Format of the input file: in each line
  518. * `node_name top|bottom|right|left|front|back|all|*|sides texture_name.png`
  519. *
  520. * @param override_filepath path to 'texturepack/override.txt'
  521. */
  522. void applyTextureOverrides(const std::string &override_filepath);
  523. /*!
  524. * Only the client uses this. Loads textures and shaders required for
  525. * rendering the nodes.
  526. * @param gamedef must be a Client.
  527. * @param progress_cbk called each time a node is loaded. Arguments:
  528. * `progress_cbk_args`, number of loaded ContentFeatures, number of
  529. * total ContentFeatures.
  530. * @param progress_cbk_args passed to the callback function
  531. */
  532. void updateTextures(IGameDef *gamedef,
  533. void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
  534. void *progress_cbk_args);
  535. /*!
  536. * Writes the content of this manager to the given output stream.
  537. * @param protocol_version serialization version of ContentFeatures
  538. */
  539. void serialize(std::ostream &os, u16 protocol_version) const;
  540. /*!
  541. * Restores the manager from a serialized stream.
  542. * This clears the previous state.
  543. * @param is input stream containing a serialized NodeDefManager
  544. */
  545. void deSerialize(std::istream &is);
  546. /*!
  547. * Used to indicate that node registration has finished.
  548. * @param completed tells whether registration is complete
  549. */
  550. inline void setNodeRegistrationStatus(bool completed) {
  551. m_node_registration_complete = completed;
  552. }
  553. /*!
  554. * Notifies the registered NodeResolver instances that node registration
  555. * has finished, then unregisters all listeners.
  556. * Must be called after node registration has finished!
  557. */
  558. void runNodeResolveCallbacks();
  559. /*!
  560. * Sets the registration completion flag to false and unregisters all
  561. * NodeResolver instances listening to the manager.
  562. */
  563. void resetNodeResolveState();
  564. /*!
  565. * Resolves the IDs to which connecting nodes connect from names.
  566. * Must be called after node registration has finished!
  567. */
  568. void mapNodeboxConnections();
  569. private:
  570. /*!
  571. * Resets the manager to its initial state.
  572. * See the documentation of the constructor.
  573. */
  574. void clear();
  575. /*!
  576. * Allocates a new content ID, and returns it.
  577. * @return the allocated ID or \ref CONTENT_IGNORE if could not allocate
  578. */
  579. content_t allocateId();
  580. /*!
  581. * Binds the given content ID and node name.
  582. * Registers them in \ref m_name_id_mapping and
  583. * \ref m_name_id_mapping_with_aliases.
  584. * @param i a content ID
  585. * @param name a node name
  586. */
  587. void addNameIdMapping(content_t i, std::string name);
  588. /*!
  589. * Recalculates m_selection_box_int_union based on
  590. * m_selection_box_union.
  591. */
  592. void fixSelectionBoxIntUnion();
  593. //! Features indexed by ID.
  594. std::vector<ContentFeatures> m_content_features;
  595. //! A mapping for fast conversion between names and IDs
  596. NameIdMapping m_name_id_mapping;
  597. /*!
  598. * Like @ref m_name_id_mapping, but maps only from names to IDs, and
  599. * includes aliases too. Updated by \ref updateAliases().
  600. * Note: Not serialized.
  601. */
  602. std::unordered_map<std::string, content_t> m_name_id_mapping_with_aliases;
  603. /*!
  604. * A mapping from group names to a vector of content types that belong
  605. * to it. Necessary for a direct lookup in \ref getIds().
  606. * Note: Not serialized.
  607. */
  608. std::unordered_map<std::string, std::vector<content_t>> m_group_to_items;
  609. /*!
  610. * The next ID that might be free to allocate.
  611. * It can be allocated already, because \ref CONTENT_AIR,
  612. * \ref CONTENT_UNKNOWN and \ref CONTENT_IGNORE are registered when the
  613. * manager is initialized, and new IDs are allocated from 0.
  614. */
  615. content_t m_next_id;
  616. //! True if all nodes have been registered.
  617. bool m_node_registration_complete;
  618. /*!
  619. * The union of all nodes' selection boxes.
  620. * Might be larger if big nodes are removed from the manager.
  621. */
  622. aabb3f m_selection_box_union;
  623. /*!
  624. * The smallest box in integer node coordinates that
  625. * contains all nodes' selection boxes.
  626. * Might be larger if big nodes are removed from the manager.
  627. */
  628. core::aabbox3d<s16> m_selection_box_int_union;
  629. /*!
  630. * NodeResolver instances to notify once node registration has finished.
  631. * Even constant NodeDefManager instances can register listeners.
  632. */
  633. mutable std::vector<NodeResolver *> m_pending_resolve_callbacks;
  634. };
  635. NodeDefManager *createNodeDefManager();
  636. class NodeResolver {
  637. public:
  638. NodeResolver();
  639. virtual ~NodeResolver();
  640. virtual void resolveNodeNames() = 0;
  641. bool getIdFromNrBacklog(content_t *result_out,
  642. const std::string &node_alt, content_t c_fallback,
  643. bool error_on_fallback = true);
  644. bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
  645. bool all_required = false, content_t c_fallback = CONTENT_IGNORE);
  646. void nodeResolveInternal();
  647. u32 m_nodenames_idx = 0;
  648. u32 m_nnlistsizes_idx = 0;
  649. std::vector<std::string> m_nodenames;
  650. std::vector<size_t> m_nnlistsizes;
  651. const NodeDefManager *m_ndef = nullptr;
  652. bool m_resolve_done = false;
  653. };