mapnode.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #ifndef MAPNODE_HEADER
  17. #define MAPNODE_HEADER
  18. #include "irrlichttypes.h"
  19. #include "irr_v3d.h"
  20. #include "irr_aabb3d.h"
  21. #include "light.h"
  22. #include <string>
  23. #include <vector>
  24. class INodeDefManager;
  25. /*
  26. Naming scheme:
  27. - Material = irrlicht's Material class
  28. - Content = (content_t) content of a node
  29. - Tile = TileSpec at some side of a node of some content type
  30. */
  31. typedef u16 content_t;
  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. enum LightBank
  61. {
  62. LIGHTBANK_DAY,
  63. LIGHTBANK_NIGHT
  64. };
  65. /*
  66. Simple rotation enum.
  67. */
  68. enum Rotation {
  69. ROTATE_0,
  70. ROTATE_90,
  71. ROTATE_180,
  72. ROTATE_270,
  73. ROTATE_RAND,
  74. };
  75. /*
  76. Masks for MapNode.param2 of flowing liquids
  77. */
  78. #define LIQUID_LEVEL_MASK 0x07
  79. #define LIQUID_FLOW_DOWN_MASK 0x08
  80. //#define LIQUID_LEVEL_MASK 0x3f // better finite water
  81. //#define LIQUID_FLOW_DOWN_MASK 0x40 // not used when finite water
  82. /* maximum amount of liquid in a block */
  83. #define LIQUID_LEVEL_MAX LIQUID_LEVEL_MASK
  84. #define LIQUID_LEVEL_SOURCE (LIQUID_LEVEL_MAX+1)
  85. #define LIQUID_INFINITY_MASK 0x80 //0b10000000
  86. // mask for param2, now as for liquid
  87. #define LEVELED_MASK 0x3F
  88. #define LEVELED_MAX LEVELED_MASK
  89. /*
  90. This is the stuff what the whole world consists of.
  91. */
  92. struct MapNode
  93. {
  94. /*
  95. Main content
  96. */
  97. u16 param0;
  98. /*
  99. Misc parameter. Initialized to 0.
  100. - For light_propagates() blocks, this is light intensity,
  101. stored logarithmically from 0 to LIGHT_MAX.
  102. Sunlight is LIGHT_SUN, which is LIGHT_MAX+1.
  103. - Contains 2 values, day- and night lighting. Each takes 4 bits.
  104. - Uhh... well, most blocks have light or nothing in here.
  105. */
  106. u8 param1;
  107. /*
  108. The second parameter. Initialized to 0.
  109. E.g. direction for torches and flowing water.
  110. */
  111. u8 param2;
  112. MapNode(const MapNode & n)
  113. {
  114. *this = n;
  115. }
  116. MapNode(content_t content=CONTENT_AIR, u8 a_param1=0, u8 a_param2=0)
  117. {
  118. param0 = content;
  119. param1 = a_param1;
  120. param2 = a_param2;
  121. }
  122. // Create directly from a nodename
  123. // If name is unknown, sets CONTENT_IGNORE
  124. MapNode(INodeDefManager *ndef, const std::string &name,
  125. u8 a_param1=0, u8 a_param2=0);
  126. bool operator==(const MapNode &other)
  127. {
  128. return (param0 == other.param0
  129. && param1 == other.param1
  130. && param2 == other.param2);
  131. }
  132. // To be used everywhere
  133. content_t getContent() const
  134. {
  135. return param0;
  136. }
  137. void setContent(content_t c)
  138. {
  139. param0 = c;
  140. }
  141. u8 getParam1() const
  142. {
  143. return param1;
  144. }
  145. void setParam1(u8 p)
  146. {
  147. param1 = p;
  148. }
  149. u8 getParam2() const
  150. {
  151. return param2;
  152. }
  153. void setParam2(u8 p)
  154. {
  155. param2 = p;
  156. }
  157. void setLight(enum LightBank bank, u8 a_light, INodeDefManager *nodemgr);
  158. u8 getLight(enum LightBank bank, INodeDefManager *nodemgr) const;
  159. bool getLightBanks(u8 &lightday, u8 &lightnight, INodeDefManager *nodemgr) const;
  160. // 0 <= daylight_factor <= 1000
  161. // 0 <= return value <= LIGHT_SUN
  162. u8 getLightBlend(u32 daylight_factor, INodeDefManager *nodemgr) const
  163. {
  164. u8 lightday = 0;
  165. u8 lightnight = 0;
  166. getLightBanks(lightday, lightnight, nodemgr);
  167. return blend_light(daylight_factor, lightday, lightnight);
  168. }
  169. // 0.0 <= daylight_factor <= 1.0
  170. // 0 <= return value <= LIGHT_SUN
  171. u8 getLightBlendF1(float daylight_factor, INodeDefManager *nodemgr) const
  172. {
  173. u8 lightday = 0;
  174. u8 lightnight = 0;
  175. getLightBanks(lightday, lightnight, nodemgr);
  176. return blend_light_f1(daylight_factor, lightday, lightnight);
  177. }
  178. u8 getFaceDir(INodeDefManager *nodemgr) const;
  179. u8 getWallMounted(INodeDefManager *nodemgr) const;
  180. v3s16 getWallMountedDir(INodeDefManager *nodemgr) const;
  181. void rotateAlongYAxis(INodeDefManager *nodemgr, Rotation rot);
  182. /*
  183. Gets list of node boxes (used for rendering (NDT_NODEBOX)
  184. and collision)
  185. */
  186. std::vector<aabb3f> getNodeBoxes(INodeDefManager *nodemgr) const;
  187. /*
  188. Gets list of selection boxes
  189. */
  190. std::vector<aabb3f> getSelectionBoxes(INodeDefManager *nodemgr) const;
  191. /* Liquid helpers */
  192. u8 getMaxLevel(INodeDefManager *nodemgr) const;
  193. u8 getLevel(INodeDefManager *nodemgr) const;
  194. u8 setLevel(INodeDefManager *nodemgr, s8 level = 1);
  195. u8 addLevel(INodeDefManager *nodemgr, s8 add = 1);
  196. void freezeMelt(INodeDefManager *nodemgr);
  197. /*
  198. Serialization functions
  199. */
  200. static u32 serializedLength(u8 version);
  201. void serialize(u8 *dest, u8 version);
  202. void deSerialize(u8 *source, u8 version);
  203. // Serializes or deserializes a list of nodes in bulk format (first the
  204. // content of all nodes, then the param1 of all nodes, then the param2
  205. // of all nodes).
  206. // version = serialization version. Must be >= 22
  207. // content_width = the number of bytes of content per node
  208. // params_width = the number of bytes of params per node
  209. // compressed = true to zlib-compress output
  210. static void serializeBulk(std::ostream &os, int version,
  211. const MapNode *nodes, u32 nodecount,
  212. u8 content_width, u8 params_width, bool compressed);
  213. static void deSerializeBulk(std::istream &is, int version,
  214. MapNode *nodes, u32 nodecount,
  215. u8 content_width, u8 params_width, bool compressed);
  216. private:
  217. // Deprecated serialization methods
  218. void deSerialize_pre22(u8 *source, u8 version);
  219. };
  220. #endif