nodedef.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. #ifndef SERVER
  23. #include "client/tile.h"
  24. #include <IMeshManipulator.h>
  25. class Client;
  26. #endif
  27. #include "itemgroup.h"
  28. #include "sound.h" // SimpleSoundSpec
  29. #include "constants.h" // BS
  30. #include "tileanimation.h"
  31. class INodeDefManager;
  32. class IItemDefManager;
  33. class ITextureSource;
  34. class IShaderSource;
  35. class IGameDef;
  36. class NodeResolver;
  37. enum ContentParamType
  38. {
  39. CPT_NONE,
  40. CPT_LIGHT,
  41. };
  42. enum ContentParamType2
  43. {
  44. CPT2_NONE,
  45. // Need 8-bit param2
  46. CPT2_FULL,
  47. // Flowing liquid properties
  48. CPT2_FLOWINGLIQUID,
  49. // Direction for chests and furnaces and such
  50. CPT2_FACEDIR,
  51. // Direction for signs, torches and such
  52. CPT2_WALLMOUNTED,
  53. // Block level like FLOWINGLIQUID
  54. CPT2_LEVELED,
  55. // 2D rotation for things like plants
  56. CPT2_DEGROTATE,
  57. // Mesh options for plants
  58. CPT2_MESHOPTIONS,
  59. // Index for palette
  60. CPT2_COLOR,
  61. // 3 bits of palette index, then facedir
  62. CPT2_COLORED_FACEDIR,
  63. // 5 bits of palette index, then wallmounted
  64. CPT2_COLORED_WALLMOUNTED,
  65. // Glasslike framed drawtype internal liquid level, param2 values 0 to 63
  66. CPT2_GLASSLIKE_LIQUID_LEVEL,
  67. };
  68. enum LiquidType
  69. {
  70. LIQUID_NONE,
  71. LIQUID_FLOWING,
  72. LIQUID_SOURCE,
  73. };
  74. enum NodeBoxType
  75. {
  76. NODEBOX_REGULAR, // Regular block; allows buildable_to
  77. NODEBOX_FIXED, // Static separately defined box(es)
  78. NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
  79. NODEBOX_LEVELED, // Same as fixed, but with dynamic height from param2. for snow, ...
  80. NODEBOX_CONNECTED, // optionally draws nodeboxes if a neighbor node attaches
  81. };
  82. struct NodeBox
  83. {
  84. enum NodeBoxType type;
  85. // NODEBOX_REGULAR (no parameters)
  86. // NODEBOX_FIXED
  87. std::vector<aabb3f> fixed;
  88. // NODEBOX_WALLMOUNTED
  89. aabb3f wall_top;
  90. aabb3f wall_bottom;
  91. aabb3f wall_side; // being at the -X side
  92. // NODEBOX_CONNECTED
  93. std::vector<aabb3f> connect_top;
  94. std::vector<aabb3f> connect_bottom;
  95. std::vector<aabb3f> connect_front;
  96. std::vector<aabb3f> connect_left;
  97. std::vector<aabb3f> connect_back;
  98. std::vector<aabb3f> connect_right;
  99. NodeBox()
  100. { reset(); }
  101. void reset();
  102. void serialize(std::ostream &os, u16 protocol_version) const;
  103. void deSerialize(std::istream &is);
  104. };
  105. struct MapNode;
  106. class NodeMetadata;
  107. enum LeavesStyle {
  108. LEAVES_FANCY,
  109. LEAVES_SIMPLE,
  110. LEAVES_OPAQUE,
  111. };
  112. class TextureSettings {
  113. public:
  114. LeavesStyle leaves_style;
  115. bool opaque_water;
  116. bool connected_glass;
  117. bool use_normal_texture;
  118. bool enable_mesh_cache;
  119. bool enable_minimap;
  120. TextureSettings() = default;
  121. void readSettings();
  122. };
  123. enum NodeDrawType
  124. {
  125. // A basic solid block
  126. NDT_NORMAL,
  127. // Nothing is drawn
  128. NDT_AIRLIKE,
  129. // Do not draw face towards same kind of flowing/source liquid
  130. NDT_LIQUID,
  131. // A very special kind of thing
  132. NDT_FLOWINGLIQUID,
  133. // Glass-like, don't draw faces towards other glass
  134. NDT_GLASSLIKE,
  135. // Leaves-like, draw all faces no matter what
  136. NDT_ALLFACES,
  137. // Enabled -> ndt_allfaces, disabled -> ndt_normal
  138. NDT_ALLFACES_OPTIONAL,
  139. // Single plane perpendicular to a surface
  140. NDT_TORCHLIKE,
  141. // Single plane parallel to a surface
  142. NDT_SIGNLIKE,
  143. // 2 vertical planes in a 'X' shape diagonal to XZ axes.
  144. // paramtype2 = "meshoptions" allows various forms, sizes and
  145. // vertical and horizontal random offsets.
  146. NDT_PLANTLIKE,
  147. // Fenceposts that connect to neighbouring fenceposts with horizontal bars
  148. NDT_FENCELIKE,
  149. // Selects appropriate junction texture to connect like rails to
  150. // neighbouring raillikes.
  151. NDT_RAILLIKE,
  152. // Custom Lua-definable structure of multiple cuboids
  153. NDT_NODEBOX,
  154. // Glass-like, draw connected frames and all visible faces.
  155. // param2 > 0 defines 64 levels of internal liquid
  156. // Uses 3 textures, one for frames, second for faces,
  157. // optional third is a 'special tile' for the liquid.
  158. NDT_GLASSLIKE_FRAMED,
  159. // Draw faces slightly rotated and only on neighbouring nodes
  160. NDT_FIRELIKE,
  161. // Enabled -> ndt_glasslike_framed, disabled -> ndt_glasslike
  162. NDT_GLASSLIKE_FRAMED_OPTIONAL,
  163. // Uses static meshes
  164. NDT_MESH,
  165. // Combined plantlike-on-solid
  166. NDT_PLANTLIKE_ROOTED,
  167. };
  168. // Mesh options for NDT_PLANTLIKE with CPT2_MESHOPTIONS
  169. static const u8 MO_MASK_STYLE = 0x07;
  170. static const u8 MO_BIT_RANDOM_OFFSET = 0x08;
  171. static const u8 MO_BIT_SCALE_SQRT2 = 0x10;
  172. static const u8 MO_BIT_RANDOM_OFFSET_Y = 0x20;
  173. enum PlantlikeStyle {
  174. PLANT_STYLE_CROSS,
  175. PLANT_STYLE_CROSS2,
  176. PLANT_STYLE_STAR,
  177. PLANT_STYLE_HASH,
  178. PLANT_STYLE_HASH2,
  179. };
  180. /*
  181. Stand-alone definition of a TileSpec (basically a server-side TileSpec)
  182. */
  183. struct TileDef
  184. {
  185. std::string name = "";
  186. bool backface_culling = true; // Takes effect only in special cases
  187. bool tileable_horizontal = true;
  188. bool tileable_vertical = true;
  189. //! If true, the tile has its own color.
  190. bool has_color = false;
  191. //! The color of the tile.
  192. video::SColor color = video::SColor(0xFFFFFFFF);
  193. struct TileAnimationParams animation;
  194. TileDef()
  195. {
  196. animation.type = TAT_NONE;
  197. }
  198. void serialize(std::ostream &os, u16 protocol_version) const;
  199. void deSerialize(std::istream &is, u8 contentfeatures_version,
  200. NodeDrawType drawtype);
  201. };
  202. #define CF_SPECIAL_COUNT 6
  203. struct ContentFeatures
  204. {
  205. /*
  206. Cached stuff
  207. */
  208. #ifndef SERVER
  209. // 0 1 2 3 4 5
  210. // up down right left back front
  211. TileSpec tiles[6];
  212. // Special tiles
  213. // - Currently used for flowing liquids
  214. TileSpec special_tiles[CF_SPECIAL_COUNT];
  215. u8 solidness; // Used when choosing which face is drawn
  216. u8 visual_solidness; // When solidness=0, this tells how it looks like
  217. bool backface_culling;
  218. #endif
  219. // Server-side cached callback existence for fast skipping
  220. bool has_on_construct;
  221. bool has_on_destruct;
  222. bool has_after_destruct;
  223. /*
  224. Actual data
  225. */
  226. // --- GENERAL PROPERTIES ---
  227. std::string name; // "" = undefined node
  228. ItemGroupList groups; // Same as in itemdef
  229. // Type of MapNode::param1
  230. ContentParamType param_type;
  231. // Type of MapNode::param2
  232. ContentParamType2 param_type_2;
  233. // --- VISUAL PROPERTIES ---
  234. enum NodeDrawType drawtype;
  235. std::string mesh;
  236. #ifndef SERVER
  237. scene::IMesh *mesh_ptr[24];
  238. video::SColor minimap_color;
  239. #endif
  240. float visual_scale; // Misc. scale parameter
  241. TileDef tiledef[6];
  242. // These will be drawn over the base tiles.
  243. TileDef tiledef_overlay[6];
  244. TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
  245. // If 255, the node is opaque.
  246. // Otherwise it uses texture alpha.
  247. u8 alpha;
  248. // The color of the node.
  249. video::SColor color;
  250. std::string palette_name;
  251. std::vector<video::SColor> *palette;
  252. // Used for waving leaves/plants
  253. u8 waving;
  254. // for NDT_CONNECTED pairing
  255. u8 connect_sides;
  256. std::vector<std::string> connects_to;
  257. std::vector<content_t> connects_to_ids;
  258. // Post effect color, drawn when the camera is inside the node.
  259. video::SColor post_effect_color;
  260. // Flowing liquid or snow, value = default level
  261. u8 leveled;
  262. // --- LIGHTING-RELATED ---
  263. bool light_propagates;
  264. bool sunlight_propagates;
  265. // Amount of light the node emits
  266. u8 light_source;
  267. // --- MAP GENERATION ---
  268. // True for all ground-like things like stone and mud, false for eg. trees
  269. bool is_ground_content;
  270. // --- INTERACTION PROPERTIES ---
  271. // This is used for collision detection.
  272. // Also for general solidness queries.
  273. bool walkable;
  274. // Player can point to these
  275. bool pointable;
  276. // Player can dig these
  277. bool diggable;
  278. // Player can climb these
  279. bool climbable;
  280. // Player can build on these
  281. bool buildable_to;
  282. // Player cannot build to these (placement prediction disabled)
  283. bool rightclickable;
  284. u32 damage_per_second;
  285. // client dig prediction
  286. std::string node_dig_prediction;
  287. // --- LIQUID PROPERTIES ---
  288. // Whether the node is non-liquid, source liquid or flowing liquid
  289. enum LiquidType liquid_type;
  290. // If the content is liquid, this is the flowing version of the liquid.
  291. std::string liquid_alternative_flowing;
  292. // If the content is liquid, this is the source version of the liquid.
  293. std::string liquid_alternative_source;
  294. // Viscosity for fluid flow, ranging from 1 to 7, with
  295. // 1 giving almost instantaneous propagation and 7 being
  296. // the slowest possible
  297. u8 liquid_viscosity;
  298. // Is liquid renewable (new liquid source will be created between 2 existing)
  299. bool liquid_renewable;
  300. // Number of flowing liquids surrounding source
  301. u8 liquid_range;
  302. u8 drowning;
  303. // Liquids flow into and replace node
  304. bool floodable;
  305. // --- NODEBOXES ---
  306. NodeBox node_box;
  307. NodeBox selection_box;
  308. NodeBox collision_box;
  309. // --- SOUND PROPERTIES ---
  310. SimpleSoundSpec sound_footstep;
  311. SimpleSoundSpec sound_dig;
  312. SimpleSoundSpec sound_dug;
  313. // --- LEGACY ---
  314. // Compatibility with old maps
  315. // Set to true if paramtype used to be 'facedir_simple'
  316. bool legacy_facedir_simple;
  317. // Set to true if wall_mounted used to be set to true
  318. bool legacy_wallmounted;
  319. /*
  320. Methods
  321. */
  322. ContentFeatures();
  323. ~ContentFeatures() = default;
  324. void reset();
  325. void serialize(std::ostream &os, u16 protocol_version) const;
  326. void deSerialize(std::istream &is);
  327. void serializeOld(std::ostream &os, u16 protocol_version) const;
  328. void deSerializeOld(std::istream &is, int version);
  329. /*!
  330. * Since vertex alpha is no longer supported, this method
  331. * adds opacity directly to the texture pixels.
  332. *
  333. * \param tiles array of the tile definitions.
  334. * \param length length of tiles
  335. */
  336. void correctAlpha(TileDef *tiles, int length);
  337. /*
  338. Some handy methods
  339. */
  340. bool isLiquid() const{
  341. return (liquid_type != LIQUID_NONE);
  342. }
  343. bool sameLiquid(const ContentFeatures &f) const{
  344. if(!isLiquid() || !f.isLiquid()) return false;
  345. return (liquid_alternative_flowing == f.liquid_alternative_flowing);
  346. }
  347. int getGroup(const std::string &group) const
  348. {
  349. return itemgroup_get(groups, group);
  350. }
  351. #ifndef SERVER
  352. void fillTileAttribs(ITextureSource *tsrc, TileLayer *tile, TileDef *tiledef,
  353. u32 shader_id, bool use_normal_texture, bool backface_culling,
  354. u8 material_type);
  355. void updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
  356. scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings);
  357. #endif
  358. };
  359. class INodeDefManager {
  360. public:
  361. INodeDefManager() = default;
  362. virtual ~INodeDefManager() = default;
  363. // Get node definition
  364. virtual const ContentFeatures &get(content_t c) const=0;
  365. virtual const ContentFeatures &get(const MapNode &n) const=0;
  366. virtual bool getId(const std::string &name, content_t &result) const=0;
  367. virtual content_t getId(const std::string &name) const=0;
  368. // Allows "group:name" in addition to regular node names
  369. // returns false if node name not found, true otherwise
  370. virtual bool getIds(const std::string &name, std::vector<content_t> &result)
  371. const=0;
  372. virtual const ContentFeatures &get(const std::string &name) const=0;
  373. virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
  374. virtual void pendNodeResolve(NodeResolver *nr)=0;
  375. virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
  376. virtual bool nodeboxConnects(const MapNode from, const MapNode to, u8 connect_face)=0;
  377. /*!
  378. * Returns the smallest box in node coordinates that
  379. * contains all nodes' selection boxes.
  380. */
  381. virtual core::aabbox3d<s16> getSelectionBoxIntUnion() const=0;
  382. };
  383. class IWritableNodeDefManager : public INodeDefManager {
  384. public:
  385. IWritableNodeDefManager() = default;
  386. virtual ~IWritableNodeDefManager() = default;
  387. // Get node definition
  388. virtual const ContentFeatures &get(content_t c) const=0;
  389. virtual const ContentFeatures &get(const MapNode &n) const=0;
  390. virtual bool getId(const std::string &name, content_t &result) const=0;
  391. // If not found, returns CONTENT_IGNORE
  392. virtual content_t getId(const std::string &name) const=0;
  393. // Allows "group:name" in addition to regular node names
  394. virtual bool getIds(const std::string &name, std::vector<content_t> &result)
  395. const=0;
  396. // If not found, returns the features of CONTENT_UNKNOWN
  397. virtual const ContentFeatures &get(const std::string &name) const=0;
  398. // Register node definition by name (allocate an id)
  399. // If returns CONTENT_IGNORE, could not allocate id
  400. virtual content_t set(const std::string &name,
  401. const ContentFeatures &def)=0;
  402. // If returns CONTENT_IGNORE, could not allocate id
  403. virtual content_t allocateDummy(const std::string &name)=0;
  404. // Remove a node
  405. virtual void removeNode(const std::string &name)=0;
  406. /*
  407. Update item alias mapping.
  408. Call after updating item definitions.
  409. */
  410. virtual void updateAliases(IItemDefManager *idef)=0;
  411. /*
  412. Override textures from servers with ones specified in texturepack/override.txt
  413. */
  414. virtual void applyTextureOverrides(const std::string &override_filepath)=0;
  415. /*
  416. Update tile textures to latest return values of TextueSource.
  417. */
  418. virtual void updateTextures(IGameDef *gamedef,
  419. void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
  420. void *progress_cbk_args)=0;
  421. virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
  422. virtual void deSerialize(std::istream &is)=0;
  423. virtual void setNodeRegistrationStatus(bool completed)=0;
  424. virtual void pendNodeResolve(NodeResolver *nr)=0;
  425. virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
  426. virtual void runNodeResolveCallbacks()=0;
  427. virtual void resetNodeResolveState()=0;
  428. virtual void mapNodeboxConnections()=0;
  429. virtual core::aabbox3d<s16> getSelectionBoxIntUnion() const=0;
  430. };
  431. IWritableNodeDefManager *createNodeDefManager();
  432. class NodeResolver {
  433. public:
  434. NodeResolver();
  435. virtual ~NodeResolver();
  436. virtual void resolveNodeNames() = 0;
  437. bool getIdFromNrBacklog(content_t *result_out,
  438. const std::string &node_alt, content_t c_fallback);
  439. bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
  440. bool all_required=false, content_t c_fallback=CONTENT_IGNORE);
  441. void nodeResolveInternal();
  442. u32 m_nodenames_idx = 0;
  443. u32 m_nnlistsizes_idx = 0;
  444. std::vector<std::string> m_nodenames;
  445. std::vector<size_t> m_nnlistsizes;
  446. INodeDefManager *m_ndef = nullptr;
  447. bool m_resolve_done = false;
  448. };