mapnode.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Luanti
  2. // SPDX-License-Identifier: LGPL-2.1-or-later
  3. // Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. #pragma once
  5. #include "irrlichttypes_bloated.h"
  6. #include "light.h"
  7. #include "util/pointer.h"
  8. #include <string>
  9. #include <vector>
  10. class NodeDefManager;
  11. class Map;
  12. /*
  13. Naming scheme:
  14. - Material = irrlicht's Material class
  15. - Content = (content_t) content of a node
  16. - Tile = TileSpec at some side of a node of some content type
  17. */
  18. typedef u16 content_t;
  19. #define CONTENT_MAX UINT16_MAX
  20. /*
  21. The maximum node ID that can be registered by mods. This must
  22. be significantly lower than the maximum content_t value, so that
  23. there is enough room for dummy node IDs, which are created when
  24. a MapBlock containing unknown node names is loaded from disk.
  25. */
  26. #define MAX_REGISTERED_CONTENT 0x7fffU
  27. /*
  28. A solid walkable node with the texture unknown_node.png.
  29. For example, used on the client to display unregistered node IDs
  30. (instead of expanding the vector of node definitions each time
  31. such a node is received).
  32. */
  33. #define CONTENT_UNKNOWN 125
  34. /*
  35. The common material through which the player can walk and which
  36. is transparent to light
  37. */
  38. #define CONTENT_AIR 126
  39. /*
  40. Ignored node.
  41. Unloaded chunks are considered to consist of this. Several other
  42. methods return this when an error occurs. Also, during
  43. map generation this means the node has not been set yet.
  44. Doesn't create faces with anything and is considered being
  45. out-of-map in the game map.
  46. */
  47. #define CONTENT_IGNORE 127
  48. /*
  49. Content lighting information that fits into a single byte.
  50. */
  51. struct ContentLightingFlags {
  52. u8 light_source : 4;
  53. bool has_light : 1;
  54. bool light_propagates : 1;
  55. bool sunlight_propagates : 1;
  56. bool operator==(const ContentLightingFlags &other) const
  57. {
  58. return has_light == other.has_light && light_propagates == other.light_propagates &&
  59. sunlight_propagates == other.sunlight_propagates &&
  60. light_source == other.light_source;
  61. }
  62. bool operator!=(const ContentLightingFlags &other) const { return !(*this == other); }
  63. };
  64. static_assert(sizeof(ContentLightingFlags) == 1, "Unexpected ContentLightingFlags size");
  65. enum LightBank
  66. {
  67. LIGHTBANK_DAY,
  68. LIGHTBANK_NIGHT
  69. };
  70. /*
  71. Simple rotation enum.
  72. */
  73. enum Rotation {
  74. ROTATE_0,
  75. ROTATE_90,
  76. ROTATE_180,
  77. ROTATE_270,
  78. ROTATE_RAND,
  79. };
  80. /*
  81. Masks for MapNode.param2 of flowing liquids
  82. */
  83. #define LIQUID_LEVEL_MASK 0x07
  84. #define LIQUID_FLOW_DOWN_MASK 0x08
  85. //#define LIQUID_LEVEL_MASK 0x3f // better finite water
  86. //#define LIQUID_FLOW_DOWN_MASK 0x40 // not used when finite water
  87. /* maximum amount of liquid in a block */
  88. #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
  89. #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
  90. #define LIQUID_INFINITY_MASK 0x80 //0b10000000
  91. // mask for leveled nodebox param2
  92. #define LEVELED_MASK 0x7F
  93. #define LEVELED_MAX LEVELED_MASK
  94. struct ContentFeatures;
  95. /*
  96. This is the stuff what the whole world consists of.
  97. */
  98. struct alignas(u32) MapNode
  99. {
  100. /*
  101. Main content
  102. */
  103. u16 param0;
  104. /*
  105. Misc parameter. Initialized to 0.
  106. - For light_propagates() blocks, this is light intensity,
  107. stored logarithmically from 0 to LIGHT_MAX.
  108. Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
  109. - Contains 2 values, day- and night lighting. Each takes 4 bits.
  110. - Uhh... well, most blocks have light or nothing in here.
  111. */
  112. u8 param1;
  113. /*
  114. The second parameter. Initialized to 0.
  115. E.g. direction for torches and flowing water.
  116. */
  117. u8 param2;
  118. MapNode() = default;
  119. MapNode(content_t content, u8 a_param1=0, u8 a_param2=0) noexcept
  120. : param0(content),
  121. param1(a_param1),
  122. param2(a_param2)
  123. { }
  124. bool operator==(const MapNode &other) const noexcept
  125. {
  126. return (param0 == other.param0
  127. && param1 == other.param1
  128. && param2 == other.param2);
  129. }
  130. // To be used everywhere
  131. content_t getContent() const noexcept
  132. {
  133. return param0;
  134. }
  135. void setContent(content_t c) noexcept
  136. {
  137. param0 = c;
  138. }
  139. u8 getParam1() const noexcept
  140. {
  141. return param1;
  142. }
  143. void setParam1(u8 p) noexcept
  144. {
  145. param1 = p;
  146. }
  147. u8 getParam2() const noexcept
  148. {
  149. return param2;
  150. }
  151. void setParam2(u8 p) noexcept
  152. {
  153. param2 = p;
  154. }
  155. /*!
  156. * Returns the color of the node.
  157. *
  158. * \param f content features of this node
  159. * \param color output, contains the node's color.
  160. */
  161. void getColor(const ContentFeatures &f, video::SColor *color) const;
  162. inline void setLight(LightBank bank, u8 a_light, ContentLightingFlags f) noexcept
  163. {
  164. // If node doesn't contain light data, ignore this
  165. if (!f.has_light)
  166. return;
  167. if (bank == LIGHTBANK_DAY) {
  168. param1 &= 0xf0;
  169. param1 |= a_light & 0x0f;
  170. } else {
  171. assert(bank == LIGHTBANK_NIGHT);
  172. param1 &= 0x0f;
  173. param1 |= (a_light & 0x0f)<<4;
  174. }
  175. }
  176. /**
  177. * Check if the light value for night differs from the light value for day.
  178. *
  179. * @return If the light values are equal, returns true; otherwise false
  180. */
  181. inline bool isLightDayNightEq(ContentLightingFlags f) const noexcept
  182. {
  183. return !f.has_light || getLight(LIGHTBANK_DAY, f) == getLight(LIGHTBANK_NIGHT, f);
  184. }
  185. inline u8 getLight(LightBank bank, ContentLightingFlags f) const noexcept
  186. {
  187. u8 raw_light = getLightRaw(bank, f);
  188. return MYMAX(f.light_source, raw_light);
  189. }
  190. /*!
  191. * Returns the node's light level from param1.
  192. * If the node emits light, it is ignored.
  193. * \param f the ContentLightingFlags of this node.
  194. */
  195. inline u8 getLightRaw(LightBank bank, ContentLightingFlags f) const noexcept
  196. {
  197. if(f.has_light)
  198. return bank == LIGHTBANK_DAY ? param1 & 0x0f : (param1 >> 4) & 0x0f;
  199. return 0;
  200. }
  201. // 0 <= daylight_factor <= 1000
  202. // 0 <= return value <= LIGHT_SUN
  203. u8 getLightBlend(u32 daylight_factor, ContentLightingFlags f) const
  204. {
  205. u8 lightday = getLight(LIGHTBANK_DAY, f);
  206. u8 lightnight = getLight(LIGHTBANK_NIGHT, f);
  207. return blend_light(daylight_factor, lightday, lightnight);
  208. }
  209. u8 getFaceDir(const NodeDefManager *nodemgr, bool allow_wallmounted = false) const;
  210. u8 getWallMounted(const NodeDefManager *nodemgr) const;
  211. v3s16 getWallMountedDir(const NodeDefManager *nodemgr) const;
  212. /// @returns Rotation in range 0–239 (in 1.5° steps)
  213. u8 getDegRotate(const NodeDefManager *nodemgr) const;
  214. void rotateAlongYAxis(const NodeDefManager *nodemgr, Rotation rot);
  215. /*!
  216. * Checks which neighbors does this node connect to.
  217. *
  218. * \param p coordinates of the node
  219. */
  220. u8 getNeighbors(v3s16 p, Map *map) const;
  221. /*
  222. Gets list of node boxes (used for rendering (NDT_NODEBOX))
  223. */
  224. void getNodeBoxes(const NodeDefManager *nodemgr, std::vector<aabb3f> *boxes,
  225. u8 neighbors = 0) const;
  226. /*
  227. Gets list of selection boxes
  228. */
  229. void getSelectionBoxes(const NodeDefManager *nodemg,
  230. std::vector<aabb3f> *boxes, u8 neighbors = 0) const;
  231. /*
  232. Gets list of collision boxes
  233. */
  234. void getCollisionBoxes(const NodeDefManager *nodemgr,
  235. std::vector<aabb3f> *boxes, u8 neighbors = 0) const;
  236. /*
  237. Liquid/leveled helpers
  238. */
  239. u8 getMaxLevel(const NodeDefManager *nodemgr) const;
  240. u8 getLevel(const NodeDefManager *nodemgr) const;
  241. s8 setLevel(const NodeDefManager *nodemgr, s16 level = 1);
  242. s8 addLevel(const NodeDefManager *nodemgr, s16 add = 1);
  243. /*
  244. Serialization functions
  245. */
  246. static u32 serializedLength(u8 version);
  247. void serialize(u8 *dest, u8 version) const;
  248. void deSerialize(u8 *source, u8 version);
  249. // Serializes or deserializes a list of nodes in bulk format (first the
  250. // content of all nodes, then the param1 of all nodes, then the param2
  251. // of all nodes).
  252. // version = serialization version. Must be >= 22
  253. // content_width = the number of bytes of content per node
  254. // params_width = the number of bytes of params per node
  255. // compressed = true to zlib-compress output
  256. static Buffer<u8> serializeBulk(int version,
  257. const MapNode *nodes, u32 nodecount,
  258. u8 content_width, u8 params_width);
  259. static void deSerializeBulk(std::istream &is, int version,
  260. MapNode *nodes, u32 nodecount,
  261. u8 content_width, u8 params_width);
  262. private:
  263. // Deprecated serialization methods
  264. void deSerialize_pre22(const u8 *source, u8 version);
  265. };