mapnode.h 8.5 KB

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