nodedef.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 "texture_override.h" // TextureOverride
  32. #include "tileanimation.h"
  33. // PROTOCOL_VERSION >= 37
  34. static const u8 CONTENTFEATURES_VERSION = 13;
  35. class IItemDefManager;
  36. class ITextureSource;
  37. class IShaderSource;
  38. class IGameDef;
  39. class NodeResolver;
  40. enum ContentParamType
  41. {
  42. CPT_NONE,
  43. CPT_LIGHT,
  44. };
  45. enum ContentParamType2
  46. {
  47. CPT2_NONE,
  48. // Need 8-bit param2
  49. CPT2_FULL,
  50. // Flowing liquid properties
  51. CPT2_FLOWINGLIQUID,
  52. // Direction for chests and furnaces and such
  53. CPT2_FACEDIR,
  54. // Direction for signs, torches and such
  55. CPT2_WALLMOUNTED,
  56. // Block level like FLOWINGLIQUID
  57. CPT2_LEVELED,
  58. // 2D rotation for things like plants
  59. CPT2_DEGROTATE,
  60. // Mesh options for plants
  61. CPT2_MESHOPTIONS,
  62. // Index for palette
  63. CPT2_COLOR,
  64. // 3 bits of palette index, then facedir
  65. CPT2_COLORED_FACEDIR,
  66. // 5 bits of palette index, then wallmounted
  67. CPT2_COLORED_WALLMOUNTED,
  68. // Glasslike framed drawtype internal liquid level, param2 values 0 to 63
  69. CPT2_GLASSLIKE_LIQUID_LEVEL,
  70. };
  71. enum LiquidType
  72. {
  73. LIQUID_NONE,
  74. LIQUID_FLOWING,
  75. LIQUID_SOURCE,
  76. };
  77. enum NodeBoxType
  78. {
  79. NODEBOX_REGULAR, // Regular block; allows buildable_to
  80. NODEBOX_FIXED, // Static separately defined box(es)
  81. NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
  82. NODEBOX_LEVELED, // Same as fixed, but with dynamic height from param2. for snow, ...
  83. NODEBOX_CONNECTED, // optionally draws nodeboxes if a neighbor node attaches
  84. };
  85. struct NodeBox
  86. {
  87. enum NodeBoxType type;
  88. // NODEBOX_REGULAR (no parameters)
  89. // NODEBOX_FIXED
  90. std::vector<aabb3f> fixed;
  91. // NODEBOX_WALLMOUNTED
  92. aabb3f wall_top;
  93. aabb3f wall_bottom;
  94. aabb3f wall_side; // being at the -X side
  95. // NODEBOX_CONNECTED
  96. std::vector<aabb3f> connect_top;
  97. std::vector<aabb3f> connect_bottom;
  98. std::vector<aabb3f> connect_front;
  99. std::vector<aabb3f> connect_left;
  100. std::vector<aabb3f> connect_back;
  101. std::vector<aabb3f> connect_right;
  102. std::vector<aabb3f> disconnected_top;
  103. std::vector<aabb3f> disconnected_bottom;
  104. std::vector<aabb3f> disconnected_front;
  105. std::vector<aabb3f> disconnected_left;
  106. std::vector<aabb3f> disconnected_back;
  107. std::vector<aabb3f> disconnected_right;
  108. std::vector<aabb3f> disconnected;
  109. std::vector<aabb3f> disconnected_sides;
  110. NodeBox()
  111. { reset(); }
  112. void reset();
  113. void serialize(std::ostream &os, u16 protocol_version) const;
  114. void deSerialize(std::istream &is);
  115. };
  116. struct MapNode;
  117. class NodeMetadata;
  118. enum LeavesStyle {
  119. LEAVES_FANCY,
  120. LEAVES_SIMPLE,
  121. LEAVES_OPAQUE,
  122. };
  123. enum AutoScale : u8 {
  124. AUTOSCALE_DISABLE,
  125. AUTOSCALE_ENABLE,
  126. AUTOSCALE_FORCE,
  127. };
  128. enum WorldAlignMode : u8 {
  129. WORLDALIGN_DISABLE,
  130. WORLDALIGN_ENABLE,
  131. WORLDALIGN_FORCE,
  132. WORLDALIGN_FORCE_NODEBOX,
  133. };
  134. class TextureSettings {
  135. public:
  136. LeavesStyle leaves_style;
  137. WorldAlignMode world_aligned_mode;
  138. AutoScale autoscale_mode;
  139. int node_texture_size;
  140. bool opaque_water;
  141. bool connected_glass;
  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. enum AlphaMode : u8 {
  210. ALPHAMODE_BLEND,
  211. ALPHAMODE_CLIP,
  212. ALPHAMODE_OPAQUE,
  213. ALPHAMODE_LEGACY_COMPAT, /* means either opaque or clip */
  214. };
  215. /*
  216. Stand-alone definition of a TileSpec (basically a server-side TileSpec)
  217. */
  218. struct TileDef
  219. {
  220. std::string name = "";
  221. bool backface_culling = true; // Takes effect only in special cases
  222. bool tileable_horizontal = true;
  223. bool tileable_vertical = true;
  224. //! If true, the tile has its own color.
  225. bool has_color = false;
  226. //! The color of the tile.
  227. video::SColor color = video::SColor(0xFFFFFFFF);
  228. AlignStyle align_style = ALIGN_STYLE_NODE;
  229. u8 scale = 0;
  230. struct TileAnimationParams animation;
  231. TileDef()
  232. {
  233. animation.type = TAT_NONE;
  234. }
  235. void serialize(std::ostream &os, u16 protocol_version) const;
  236. void deSerialize(std::istream &is, u8 contentfeatures_version,
  237. NodeDrawType drawtype);
  238. };
  239. // Defines the number of special tiles per nodedef
  240. //
  241. // NOTE: When changing this value, the enum entries of OverrideTarget and
  242. // parser in TextureOverrideSource must be updated so that all special
  243. // tiles can be overridden.
  244. #define CF_SPECIAL_COUNT 6
  245. struct ContentFeatures
  246. {
  247. /*
  248. Cached stuff
  249. */
  250. #ifndef SERVER
  251. // 0 1 2 3 4 5
  252. // up down right left back front
  253. TileSpec tiles[6];
  254. // Special tiles
  255. // - Currently used for flowing liquids
  256. TileSpec special_tiles[CF_SPECIAL_COUNT];
  257. u8 solidness; // Used when choosing which face is drawn
  258. u8 visual_solidness; // When solidness=0, this tells how it looks like
  259. bool backface_culling;
  260. #endif
  261. // Server-side cached callback existence for fast skipping
  262. bool has_on_construct;
  263. bool has_on_destruct;
  264. bool has_after_destruct;
  265. /*
  266. Actual data
  267. */
  268. // --- GENERAL PROPERTIES ---
  269. std::string name; // "" = undefined node
  270. ItemGroupList groups; // Same as in itemdef
  271. // Type of MapNode::param1
  272. ContentParamType param_type;
  273. // Type of MapNode::param2
  274. ContentParamType2 param_type_2;
  275. // --- VISUAL PROPERTIES ---
  276. enum NodeDrawType drawtype;
  277. std::string mesh;
  278. #ifndef SERVER
  279. scene::IMesh *mesh_ptr[24];
  280. video::SColor minimap_color;
  281. #endif
  282. float visual_scale; // Misc. scale parameter
  283. TileDef tiledef[6];
  284. // These will be drawn over the base tiles.
  285. TileDef tiledef_overlay[6];
  286. TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
  287. AlphaMode alpha;
  288. // The color of the node.
  289. video::SColor color;
  290. std::string palette_name;
  291. std::vector<video::SColor> *palette;
  292. // Used for waving leaves/plants
  293. u8 waving;
  294. // for NDT_CONNECTED pairing
  295. u8 connect_sides;
  296. std::vector<std::string> connects_to;
  297. std::vector<content_t> connects_to_ids;
  298. // Post effect color, drawn when the camera is inside the node.
  299. video::SColor post_effect_color;
  300. // Flowing liquid or leveled nodebox, value = default level
  301. u8 leveled;
  302. // Maximum value for leveled nodes
  303. u8 leveled_max;
  304. // --- LIGHTING-RELATED ---
  305. bool light_propagates;
  306. bool sunlight_propagates;
  307. // Amount of light the node emits
  308. u8 light_source;
  309. // --- MAP GENERATION ---
  310. // True for all ground-like things like stone and mud, false for eg. trees
  311. bool is_ground_content;
  312. // --- INTERACTION PROPERTIES ---
  313. // This is used for collision detection.
  314. // Also for general solidness queries.
  315. bool walkable;
  316. // Player can point to these
  317. bool pointable;
  318. // Player can dig these
  319. bool diggable;
  320. // Player can climb these
  321. bool climbable;
  322. // Player can build on these
  323. bool buildable_to;
  324. // Player cannot build to these (placement prediction disabled)
  325. bool rightclickable;
  326. u32 damage_per_second;
  327. // client dig prediction
  328. std::string node_dig_prediction;
  329. // --- LIQUID PROPERTIES ---
  330. // Whether the node is non-liquid, source liquid or flowing liquid
  331. enum LiquidType liquid_type;
  332. // If the content is liquid, this is the flowing version of the liquid.
  333. std::string liquid_alternative_flowing;
  334. content_t liquid_alternative_flowing_id;
  335. // If the content is liquid, this is the source version of the liquid.
  336. std::string liquid_alternative_source;
  337. content_t liquid_alternative_source_id;
  338. // Viscosity for fluid flow, ranging from 1 to 7, with
  339. // 1 giving almost instantaneous propagation and 7 being
  340. // the slowest possible
  341. u8 liquid_viscosity;
  342. // Is liquid renewable (new liquid source will be created between 2 existing)
  343. bool liquid_renewable;
  344. // Number of flowing liquids surrounding source
  345. u8 liquid_range;
  346. u8 drowning;
  347. // Liquids flow into and replace node
  348. bool floodable;
  349. // --- NODEBOXES ---
  350. NodeBox node_box;
  351. NodeBox selection_box;
  352. NodeBox collision_box;
  353. // --- SOUND PROPERTIES ---
  354. SimpleSoundSpec sound_footstep;
  355. SimpleSoundSpec sound_dig;
  356. SimpleSoundSpec sound_dug;
  357. // --- LEGACY ---
  358. // Compatibility with old maps
  359. // Set to true if paramtype used to be 'facedir_simple'
  360. bool legacy_facedir_simple;
  361. // Set to true if wall_mounted used to be set to true
  362. bool legacy_wallmounted;
  363. /*
  364. Methods
  365. */
  366. ContentFeatures();
  367. ~ContentFeatures();
  368. void reset();
  369. void serialize(std::ostream &os, u16 protocol_version) const;
  370. void deSerialize(std::istream &is);
  371. /*
  372. Some handy methods
  373. */
  374. void setDefaultAlphaMode()
  375. {
  376. switch (drawtype) {
  377. case NDT_NORMAL:
  378. case NDT_LIQUID:
  379. case NDT_FLOWINGLIQUID:
  380. alpha = ALPHAMODE_OPAQUE;
  381. break;
  382. case NDT_NODEBOX:
  383. case NDT_MESH:
  384. alpha = ALPHAMODE_LEGACY_COMPAT; // this should eventually be OPAQUE
  385. break;
  386. default:
  387. alpha = ALPHAMODE_CLIP;
  388. break;
  389. }
  390. }
  391. bool needsBackfaceCulling() const
  392. {
  393. switch (drawtype) {
  394. case NDT_TORCHLIKE:
  395. case NDT_SIGNLIKE:
  396. case NDT_FIRELIKE:
  397. case NDT_RAILLIKE:
  398. case NDT_PLANTLIKE:
  399. case NDT_PLANTLIKE_ROOTED:
  400. case NDT_MESH:
  401. return false;
  402. default:
  403. return true;
  404. }
  405. }
  406. bool isLiquid() const{
  407. return (liquid_type != LIQUID_NONE);
  408. }
  409. bool sameLiquid(const ContentFeatures &f) const{
  410. if(!isLiquid() || !f.isLiquid()) return false;
  411. return (liquid_alternative_flowing_id == f.liquid_alternative_flowing_id);
  412. }
  413. int getGroup(const std::string &group) const
  414. {
  415. return itemgroup_get(groups, group);
  416. }
  417. #ifndef SERVER
  418. void updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
  419. scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings);
  420. #endif
  421. private:
  422. #ifndef SERVER
  423. /*
  424. * Checks if any tile texture has any transparent pixels.
  425. * Prints a warning and returns true if that is the case, false otherwise.
  426. * This is supposed to be used for use_texture_alpha backwards compatibility.
  427. */
  428. bool textureAlphaCheck(ITextureSource *tsrc, const TileDef *tiles,
  429. int length);
  430. #endif
  431. void setAlphaFromLegacy(u8 legacy_alpha);
  432. u8 getAlphaForLegacy() const;
  433. };
  434. /*!
  435. * @brief This class is for getting the actual properties of nodes from their
  436. * content ID.
  437. *
  438. * @details The nodes on the map are represented by three numbers (see MapNode).
  439. * The first number (param0) is the type of a node. All node types have own
  440. * properties (see ContentFeatures). This class is for storing and getting the
  441. * properties of nodes.
  442. * The manager is first filled with registered nodes, then as the game begins,
  443. * functions only get `const` pointers to it, to prevent modification of
  444. * registered nodes.
  445. */
  446. class NodeDefManager {
  447. public:
  448. /*!
  449. * Creates a NodeDefManager, and registers three ContentFeatures:
  450. * \ref CONTENT_AIR, \ref CONTENT_UNKNOWN and \ref CONTENT_IGNORE.
  451. */
  452. NodeDefManager();
  453. ~NodeDefManager();
  454. /*!
  455. * Returns the properties for the given content type.
  456. * @param c content type of a node
  457. * @return properties of the given content type, or \ref CONTENT_UNKNOWN
  458. * if the given content type is not registered.
  459. */
  460. inline const ContentFeatures& get(content_t c) const {
  461. return
  462. c < m_content_features.size() ?
  463. m_content_features[c] : m_content_features[CONTENT_UNKNOWN];
  464. }
  465. /*!
  466. * Returns the properties of the given node.
  467. * @param n a map node
  468. * @return properties of the given node or @ref CONTENT_UNKNOWN if the
  469. * given content type is not registered.
  470. */
  471. inline const ContentFeatures& get(const MapNode &n) const {
  472. return get(n.getContent());
  473. }
  474. /*!
  475. * Returns the node properties for a node name.
  476. * @param name name of a node
  477. * @return properties of the given node or @ref CONTENT_UNKNOWN if
  478. * not found
  479. */
  480. const ContentFeatures& get(const std::string &name) const;
  481. /*!
  482. * Returns the content ID for the given name.
  483. * @param name a node name
  484. * @param[out] result will contain the content ID if found, otherwise
  485. * remains unchanged
  486. * @return true if the ID was found, false otherwise
  487. */
  488. bool getId(const std::string &name, content_t &result) const;
  489. /*!
  490. * Returns the content ID for the given name.
  491. * @param name a node name
  492. * @return ID of the node or @ref CONTENT_IGNORE if not found
  493. */
  494. content_t getId(const std::string &name) const;
  495. /*!
  496. * Returns the content IDs of the given node name or node group name.
  497. * Group names start with "group:".
  498. * @param name a node name or node group name
  499. * @param[out] result will be appended with matching IDs
  500. * @return true if `name` is a valid node name or a (not necessarily
  501. * valid) group name
  502. */
  503. bool getIds(const std::string &name, std::vector<content_t> &result) const;
  504. /*!
  505. * Returns the smallest box in integer node coordinates that
  506. * contains all nodes' selection boxes. The returned box might be larger
  507. * than the minimal size if the largest node is removed from the manager.
  508. */
  509. inline core::aabbox3d<s16> getSelectionBoxIntUnion() const {
  510. return m_selection_box_int_union;
  511. }
  512. /*!
  513. * Checks whether a node connects to an adjacent node.
  514. * @param from the node to be checked
  515. * @param to the adjacent node
  516. * @param connect_face a bit field indicating which face of the node is
  517. * adjacent to the other node.
  518. * Bits: +y (least significant), -y, -z, -x, +z, +x (most significant).
  519. * @return true if the node connects, false otherwise
  520. */
  521. bool nodeboxConnects(MapNode from, MapNode to,
  522. u8 connect_face) const;
  523. /*!
  524. * Registers a NodeResolver to wait for the registration of
  525. * ContentFeatures. Once the node registration finishes, all
  526. * listeners are notified.
  527. */
  528. void pendNodeResolve(NodeResolver *nr) const;
  529. /*!
  530. * Stops listening to the NodeDefManager.
  531. * @return true if the listener was registered before, false otherwise
  532. */
  533. bool cancelNodeResolveCallback(NodeResolver *nr) const;
  534. /*!
  535. * Registers a new node type with the given name and allocates a new
  536. * content ID.
  537. * Should not be called with an already existing name.
  538. * @param name name of the node, must match with `def.name`.
  539. * @param def definition of the registered node type.
  540. * @return ID of the registered node or @ref CONTENT_IGNORE if
  541. * the function could not allocate an ID.
  542. */
  543. content_t set(const std::string &name, const ContentFeatures &def);
  544. /*!
  545. * Allocates a blank node ID for the given name.
  546. * @param name name of a node
  547. * @return allocated ID or @ref CONTENT_IGNORE if could not allocate
  548. * an ID.
  549. */
  550. content_t allocateDummy(const std::string &name);
  551. /*!
  552. * Removes the given node name from the manager.
  553. * The node ID will remain in the manager, but won't be linked to any name.
  554. * @param name name to be removed
  555. */
  556. void removeNode(const std::string &name);
  557. /*!
  558. * Regenerates the alias list (a map from names to node IDs).
  559. * @param idef the item definition manager containing alias information
  560. */
  561. void updateAliases(IItemDefManager *idef);
  562. /*!
  563. * Replaces the textures of registered nodes with the ones specified in
  564. * the texturepack's override.txt file
  565. *
  566. * @param overrides the texture overrides
  567. */
  568. void applyTextureOverrides(const std::vector<TextureOverride> &overrides);
  569. /*!
  570. * Only the client uses this. Loads textures and shaders required for
  571. * rendering the nodes.
  572. * @param gamedef must be a Client.
  573. * @param progress_cbk called each time a node is loaded. Arguments:
  574. * `progress_cbk_args`, number of loaded ContentFeatures, number of
  575. * total ContentFeatures.
  576. * @param progress_cbk_args passed to the callback function
  577. */
  578. void updateTextures(IGameDef *gamedef,
  579. void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
  580. void *progress_cbk_args);
  581. /*!
  582. * Writes the content of this manager to the given output stream.
  583. * @param protocol_version serialization version of ContentFeatures
  584. */
  585. void serialize(std::ostream &os, u16 protocol_version) const;
  586. /*!
  587. * Restores the manager from a serialized stream.
  588. * This clears the previous state.
  589. * @param is input stream containing a serialized NodeDefManager
  590. */
  591. void deSerialize(std::istream &is);
  592. /*!
  593. * Used to indicate that node registration has finished.
  594. * @param completed tells whether registration is complete
  595. */
  596. inline void setNodeRegistrationStatus(bool completed) {
  597. m_node_registration_complete = completed;
  598. }
  599. /*!
  600. * Notifies the registered NodeResolver instances that node registration
  601. * has finished, then unregisters all listeners.
  602. * Must be called after node registration has finished!
  603. */
  604. void runNodeResolveCallbacks();
  605. /*!
  606. * Sets the registration completion flag to false and unregisters all
  607. * NodeResolver instances listening to the manager.
  608. */
  609. void resetNodeResolveState();
  610. /*!
  611. * Resolves (caches the IDs) cross-references between nodes,
  612. * like liquid alternatives.
  613. * Must be called after node registration has finished!
  614. */
  615. void resolveCrossrefs();
  616. private:
  617. /*!
  618. * Resets the manager to its initial state.
  619. * See the documentation of the constructor.
  620. */
  621. void clear();
  622. /*!
  623. * Allocates a new content ID, and returns it.
  624. * @return the allocated ID or \ref CONTENT_IGNORE if could not allocate
  625. */
  626. content_t allocateId();
  627. /*!
  628. * Binds the given content ID and node name.
  629. * Registers them in \ref m_name_id_mapping and
  630. * \ref m_name_id_mapping_with_aliases.
  631. * @param i a content ID
  632. * @param name a node name
  633. */
  634. void addNameIdMapping(content_t i, std::string name);
  635. /*!
  636. * Removes a content ID from all groups.
  637. * Erases content IDs from vectors in \ref m_group_to_items and
  638. * removes empty vectors.
  639. * @param id Content ID
  640. */
  641. void eraseIdFromGroups(content_t id);
  642. /*!
  643. * Recalculates m_selection_box_int_union based on
  644. * m_selection_box_union.
  645. */
  646. void fixSelectionBoxIntUnion();
  647. //! Features indexed by ID.
  648. std::vector<ContentFeatures> m_content_features;
  649. //! A mapping for fast conversion between names and IDs
  650. NameIdMapping m_name_id_mapping;
  651. /*!
  652. * Like @ref m_name_id_mapping, but maps only from names to IDs, and
  653. * includes aliases too. Updated by \ref updateAliases().
  654. * Note: Not serialized.
  655. */
  656. std::unordered_map<std::string, content_t> m_name_id_mapping_with_aliases;
  657. /*!
  658. * A mapping from group names to a vector of content types that belong
  659. * to it. Necessary for a direct lookup in \ref getIds().
  660. * Note: Not serialized.
  661. */
  662. std::unordered_map<std::string, std::vector<content_t>> m_group_to_items;
  663. /*!
  664. * The next ID that might be free to allocate.
  665. * It can be allocated already, because \ref CONTENT_AIR,
  666. * \ref CONTENT_UNKNOWN and \ref CONTENT_IGNORE are registered when the
  667. * manager is initialized, and new IDs are allocated from 0.
  668. */
  669. content_t m_next_id;
  670. //! True if all nodes have been registered.
  671. bool m_node_registration_complete;
  672. /*!
  673. * The union of all nodes' selection boxes.
  674. * Might be larger if big nodes are removed from the manager.
  675. */
  676. aabb3f m_selection_box_union;
  677. /*!
  678. * The smallest box in integer node coordinates that
  679. * contains all nodes' selection boxes.
  680. * Might be larger if big nodes are removed from the manager.
  681. */
  682. core::aabbox3d<s16> m_selection_box_int_union;
  683. /*!
  684. * NodeResolver instances to notify once node registration has finished.
  685. * Even constant NodeDefManager instances can register listeners.
  686. */
  687. mutable std::vector<NodeResolver *> m_pending_resolve_callbacks;
  688. };
  689. NodeDefManager *createNodeDefManager();
  690. class NodeResolver {
  691. public:
  692. NodeResolver();
  693. virtual ~NodeResolver();
  694. virtual void resolveNodeNames() = 0;
  695. // required because this class is used as mixin for ObjDef
  696. void cloneTo(NodeResolver *res) const;
  697. bool getIdFromNrBacklog(content_t *result_out,
  698. const std::string &node_alt, content_t c_fallback,
  699. bool error_on_fallback = true);
  700. bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
  701. bool all_required = false, content_t c_fallback = CONTENT_IGNORE);
  702. void nodeResolveInternal();
  703. u32 m_nodenames_idx = 0;
  704. u32 m_nnlistsizes_idx = 0;
  705. std::vector<std::string> m_nodenames;
  706. std::vector<size_t> m_nnlistsizes;
  707. const NodeDefManager *m_ndef = nullptr;
  708. bool m_resolve_done = false;
  709. };