nodedef.h 25 KB

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