nodedef.h 24 KB

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