nodedef.h 24 KB

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