mapnode.h 8.1 KB

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