voxel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. Minetest
  3. Copyright (C) 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.h"
  18. #include "irr_v3d.h"
  19. #include <iostream>
  20. #include <cassert>
  21. #include "exceptions.h"
  22. #include "mapnode.h"
  23. #include <set>
  24. #include <list>
  25. #include "util/basic_macros.h"
  26. class NodeDefManager;
  27. // For VC++
  28. #undef min
  29. #undef max
  30. /*
  31. A fast voxel manipulator class.
  32. In normal operation, it fetches more map when it is requested.
  33. It can also be used so that all allowed area is fetched at the
  34. start, using ManualMapVoxelManipulator.
  35. Not thread-safe.
  36. */
  37. /*
  38. Debug stuff
  39. */
  40. extern u64 emerge_time;
  41. extern u64 emerge_load_time;
  42. /*
  43. This class resembles aabbox3d<s16> a lot, but has inclusive
  44. edges for saner handling of integer sizes
  45. */
  46. class VoxelArea
  47. {
  48. public:
  49. // Starts as zero sized
  50. VoxelArea() = default;
  51. VoxelArea(const v3s16 &min_edge, const v3s16 &max_edge):
  52. MinEdge(min_edge),
  53. MaxEdge(max_edge)
  54. {
  55. cacheExtent();
  56. }
  57. VoxelArea(const v3s16 &p):
  58. MinEdge(p),
  59. MaxEdge(p)
  60. {
  61. cacheExtent();
  62. }
  63. /*
  64. Modifying methods
  65. */
  66. void addArea(const VoxelArea &a)
  67. {
  68. if (hasEmptyExtent())
  69. {
  70. *this = a;
  71. return;
  72. }
  73. if(a.MinEdge.X < MinEdge.X) MinEdge.X = a.MinEdge.X;
  74. if(a.MinEdge.Y < MinEdge.Y) MinEdge.Y = a.MinEdge.Y;
  75. if(a.MinEdge.Z < MinEdge.Z) MinEdge.Z = a.MinEdge.Z;
  76. if(a.MaxEdge.X > MaxEdge.X) MaxEdge.X = a.MaxEdge.X;
  77. if(a.MaxEdge.Y > MaxEdge.Y) MaxEdge.Y = a.MaxEdge.Y;
  78. if(a.MaxEdge.Z > MaxEdge.Z) MaxEdge.Z = a.MaxEdge.Z;
  79. cacheExtent();
  80. }
  81. void addPoint(const v3s16 &p)
  82. {
  83. if(hasEmptyExtent())
  84. {
  85. MinEdge = p;
  86. MaxEdge = p;
  87. cacheExtent();
  88. return;
  89. }
  90. if(p.X < MinEdge.X) MinEdge.X = p.X;
  91. if(p.Y < MinEdge.Y) MinEdge.Y = p.Y;
  92. if(p.Z < MinEdge.Z) MinEdge.Z = p.Z;
  93. if(p.X > MaxEdge.X) MaxEdge.X = p.X;
  94. if(p.Y > MaxEdge.Y) MaxEdge.Y = p.Y;
  95. if(p.Z > MaxEdge.Z) MaxEdge.Z = p.Z;
  96. cacheExtent();
  97. }
  98. // Pad with d nodes
  99. void pad(const v3s16 &d)
  100. {
  101. MinEdge -= d;
  102. MaxEdge += d;
  103. }
  104. /*
  105. const methods
  106. */
  107. const v3s16 &getExtent() const
  108. {
  109. return m_cache_extent;
  110. }
  111. /* Because MaxEdge and MinEdge are included in the voxel area an empty extent
  112. * is not represented by (0, 0, 0), but instead (-1, -1, -1)
  113. */
  114. bool hasEmptyExtent() const
  115. {
  116. return MaxEdge - MinEdge == v3s16(-1, -1, -1);
  117. }
  118. s32 getVolume() const
  119. {
  120. return (s32)m_cache_extent.X * (s32)m_cache_extent.Y * (s32)m_cache_extent.Z;
  121. }
  122. bool contains(const VoxelArea &a) const
  123. {
  124. // No area contains an empty area
  125. // NOTE: Algorithms depend on this, so do not change.
  126. if(a.hasEmptyExtent())
  127. return false;
  128. return(
  129. a.MinEdge.X >= MinEdge.X && a.MaxEdge.X <= MaxEdge.X &&
  130. a.MinEdge.Y >= MinEdge.Y && a.MaxEdge.Y <= MaxEdge.Y &&
  131. a.MinEdge.Z >= MinEdge.Z && a.MaxEdge.Z <= MaxEdge.Z
  132. );
  133. }
  134. bool contains(v3s16 p) const
  135. {
  136. return(
  137. p.X >= MinEdge.X && p.X <= MaxEdge.X &&
  138. p.Y >= MinEdge.Y && p.Y <= MaxEdge.Y &&
  139. p.Z >= MinEdge.Z && p.Z <= MaxEdge.Z
  140. );
  141. }
  142. bool contains(s32 i) const
  143. {
  144. return (i >= 0 && i < getVolume());
  145. }
  146. bool operator==(const VoxelArea &other) const
  147. {
  148. return (MinEdge == other.MinEdge
  149. && MaxEdge == other.MaxEdge);
  150. }
  151. VoxelArea operator+(const v3s16 &off) const
  152. {
  153. return {MinEdge+off, MaxEdge+off};
  154. }
  155. VoxelArea operator-(const v3s16 &off) const
  156. {
  157. return {MinEdge-off, MaxEdge-off};
  158. }
  159. /*
  160. Returns 0-6 non-overlapping areas that can be added to
  161. a to make up this area.
  162. a: area inside *this
  163. */
  164. void diff(const VoxelArea &a, std::list<VoxelArea> &result)
  165. {
  166. /*
  167. This can result in a maximum of 6 areas
  168. */
  169. // If a is an empty area, return the current area as a whole
  170. if(a.getExtent() == v3s16(0,0,0))
  171. {
  172. VoxelArea b = *this;
  173. if(b.getVolume() != 0)
  174. result.push_back(b);
  175. return;
  176. }
  177. assert(contains(a)); // pre-condition
  178. // Take back area, XY inclusive
  179. {
  180. v3s16 min(MinEdge.X, MinEdge.Y, a.MaxEdge.Z+1);
  181. v3s16 max(MaxEdge.X, MaxEdge.Y, MaxEdge.Z);
  182. VoxelArea b(min, max);
  183. if(b.getVolume() != 0)
  184. result.push_back(b);
  185. }
  186. // Take front area, XY inclusive
  187. {
  188. v3s16 min(MinEdge.X, MinEdge.Y, MinEdge.Z);
  189. v3s16 max(MaxEdge.X, MaxEdge.Y, a.MinEdge.Z-1);
  190. VoxelArea b(min, max);
  191. if(b.getVolume() != 0)
  192. result.push_back(b);
  193. }
  194. // Take top area, X inclusive
  195. {
  196. v3s16 min(MinEdge.X, a.MaxEdge.Y+1, a.MinEdge.Z);
  197. v3s16 max(MaxEdge.X, MaxEdge.Y, a.MaxEdge.Z);
  198. VoxelArea b(min, max);
  199. if(b.getVolume() != 0)
  200. result.push_back(b);
  201. }
  202. // Take bottom area, X inclusive
  203. {
  204. v3s16 min(MinEdge.X, MinEdge.Y, a.MinEdge.Z);
  205. v3s16 max(MaxEdge.X, a.MinEdge.Y-1, a.MaxEdge.Z);
  206. VoxelArea b(min, max);
  207. if(b.getVolume() != 0)
  208. result.push_back(b);
  209. }
  210. // Take left area, non-inclusive
  211. {
  212. v3s16 min(MinEdge.X, a.MinEdge.Y, a.MinEdge.Z);
  213. v3s16 max(a.MinEdge.X-1, a.MaxEdge.Y, a.MaxEdge.Z);
  214. VoxelArea b(min, max);
  215. if(b.getVolume() != 0)
  216. result.push_back(b);
  217. }
  218. // Take right area, non-inclusive
  219. {
  220. v3s16 min(a.MaxEdge.X+1, a.MinEdge.Y, a.MinEdge.Z);
  221. v3s16 max(MaxEdge.X, a.MaxEdge.Y, a.MaxEdge.Z);
  222. VoxelArea b(min, max);
  223. if(b.getVolume() != 0)
  224. result.push_back(b);
  225. }
  226. }
  227. /*
  228. Translates position from virtual coordinates to array index
  229. */
  230. s32 index(s16 x, s16 y, s16 z) const
  231. {
  232. s32 i = (s32)(z - MinEdge.Z) * m_cache_extent.Y * m_cache_extent.X
  233. + (y - MinEdge.Y) * m_cache_extent.X
  234. + (x - MinEdge.X);
  235. return i;
  236. }
  237. s32 index(v3s16 p) const
  238. {
  239. return index(p.X, p.Y, p.Z);
  240. }
  241. /**
  242. * Translate index in the X coordinate
  243. */
  244. static void add_x(const v3s16 &extent, u32 &i, s16 a)
  245. {
  246. i += a;
  247. }
  248. /**
  249. * Translate index in the Y coordinate
  250. */
  251. static void add_y(const v3s16 &extent, u32 &i, s16 a)
  252. {
  253. i += a * extent.X;
  254. }
  255. /**
  256. * Translate index in the Z coordinate
  257. */
  258. static void add_z(const v3s16 &extent, u32 &i, s16 a)
  259. {
  260. i += a * extent.X * extent.Y;
  261. }
  262. /**
  263. * Translate index in space
  264. */
  265. static void add_p(const v3s16 &extent, u32 &i, v3s16 a)
  266. {
  267. i += a.Z * extent.X * extent.Y + a.Y * extent.X + a.X;
  268. }
  269. /*
  270. Print method for debugging
  271. */
  272. void print(std::ostream &o) const
  273. {
  274. o << PP(MinEdge) << PP(MaxEdge) << "="
  275. << m_cache_extent.X << "x" << m_cache_extent.Y << "x" << m_cache_extent.Z
  276. << "=" << getVolume();
  277. }
  278. // Edges are inclusive
  279. v3s16 MinEdge = v3s16(1,1,1);
  280. v3s16 MaxEdge;
  281. private:
  282. void cacheExtent()
  283. {
  284. m_cache_extent = MaxEdge - MinEdge + v3s16(1,1,1);
  285. }
  286. v3s16 m_cache_extent = v3s16(0,0,0);
  287. };
  288. // unused
  289. #define VOXELFLAG_UNUSED (1 << 0)
  290. // no data about that node
  291. #define VOXELFLAG_NO_DATA (1 << 1)
  292. // Algorithm-dependent
  293. #define VOXELFLAG_CHECKED1 (1 << 2)
  294. // Algorithm-dependent
  295. #define VOXELFLAG_CHECKED2 (1 << 3)
  296. // Algorithm-dependent
  297. #define VOXELFLAG_CHECKED3 (1 << 4)
  298. // Algorithm-dependent
  299. #define VOXELFLAG_CHECKED4 (1 << 5)
  300. enum VoxelPrintMode
  301. {
  302. VOXELPRINT_NOTHING,
  303. VOXELPRINT_MATERIAL,
  304. VOXELPRINT_WATERPRESSURE,
  305. VOXELPRINT_LIGHT_DAY,
  306. };
  307. class VoxelManipulator
  308. {
  309. public:
  310. VoxelManipulator() = default;
  311. virtual ~VoxelManipulator();
  312. /*
  313. These are a bit slow and shouldn't be used internally.
  314. Use m_data[m_area.index(p)] instead.
  315. */
  316. MapNode getNode(const v3s16 &p)
  317. {
  318. VoxelArea voxel_area(p);
  319. addArea(voxel_area);
  320. if (m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA) {
  321. /*dstream<<"EXCEPT: VoxelManipulator::getNode(): "
  322. <<"p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"
  323. <<", index="<<m_area.index(p)
  324. <<", flags="<<(int)m_flags[m_area.index(p)]
  325. <<" is inexistent"<<std::endl;*/
  326. throw InvalidPositionException
  327. ("VoxelManipulator: getNode: inexistent");
  328. }
  329. return m_data[m_area.index(p)];
  330. }
  331. MapNode getNodeNoEx(const v3s16 &p)
  332. {
  333. VoxelArea voxel_area(p);
  334. addArea(voxel_area);
  335. if (m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA) {
  336. return {CONTENT_IGNORE};
  337. }
  338. return m_data[m_area.index(p)];
  339. }
  340. MapNode getNodeNoExNoEmerge(const v3s16 &p)
  341. {
  342. if (!m_area.contains(p))
  343. return {CONTENT_IGNORE};
  344. if (m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA)
  345. return {CONTENT_IGNORE};
  346. return m_data[m_area.index(p)];
  347. }
  348. // Stuff explodes if non-emerged area is touched with this.
  349. // Emerge first, and check VOXELFLAG_NO_DATA if appropriate.
  350. MapNode & getNodeRefUnsafe(const v3s16 &p)
  351. {
  352. return m_data[m_area.index(p)];
  353. }
  354. const MapNode & getNodeRefUnsafeCheckFlags(const v3s16 &p)
  355. {
  356. s32 index = m_area.index(p);
  357. if (m_flags[index] & VOXELFLAG_NO_DATA)
  358. return ContentIgnoreNode;
  359. return m_data[index];
  360. }
  361. u8 & getFlagsRefUnsafe(const v3s16 &p)
  362. {
  363. return m_flags[m_area.index(p)];
  364. }
  365. bool exists(const v3s16 &p)
  366. {
  367. return m_area.contains(p) &&
  368. !(getFlagsRefUnsafe(p) & VOXELFLAG_NO_DATA);
  369. }
  370. void setNode(const v3s16 &p, const MapNode &n)
  371. {
  372. VoxelArea voxel_area(p);
  373. addArea(voxel_area);
  374. m_data[m_area.index(p)] = n;
  375. m_flags[m_area.index(p)] &= ~VOXELFLAG_NO_DATA;
  376. }
  377. // TODO: Should be removed and replaced with setNode
  378. void setNodeNoRef(const v3s16 &p, const MapNode &n)
  379. {
  380. setNode(p, n);
  381. }
  382. /*
  383. Set stuff if available without an emerge.
  384. Return false if failed.
  385. This is convenient but slower than playing around directly
  386. with the m_data table with indices.
  387. */
  388. bool setNodeNoEmerge(const v3s16 &p, MapNode n)
  389. {
  390. if(!m_area.contains(p))
  391. return false;
  392. m_data[m_area.index(p)] = n;
  393. return true;
  394. }
  395. /*
  396. Control
  397. */
  398. virtual void clear();
  399. void print(std::ostream &o, const NodeDefManager *nodemgr,
  400. VoxelPrintMode mode=VOXELPRINT_MATERIAL);
  401. void addArea(const VoxelArea &area);
  402. /*
  403. Copy data and set flags to 0
  404. dst_area.getExtent() <= src_area.getExtent()
  405. */
  406. void copyFrom(MapNode *src, const VoxelArea& src_area,
  407. v3s16 from_pos, v3s16 to_pos, const v3s16 &size);
  408. // Copy data
  409. void copyTo(MapNode *dst, const VoxelArea& dst_area,
  410. v3s16 dst_pos, v3s16 from_pos, const v3s16 &size);
  411. /*
  412. Algorithms
  413. */
  414. void clearFlag(u8 flag);
  415. /*
  416. Member variables
  417. */
  418. /*
  419. The area that is stored in m_data.
  420. addInternalBox should not be used if getExtent() == v3s16(0,0,0)
  421. MaxEdge is 1 higher than maximum allowed position
  422. */
  423. VoxelArea m_area;
  424. /*
  425. nullptr if data size is 0 (extent (0,0,0))
  426. Data is stored as [z*h*w + y*h + x]
  427. */
  428. MapNode *m_data = nullptr;
  429. /*
  430. Flags of all nodes
  431. */
  432. u8 *m_flags = nullptr;
  433. static const MapNode ContentIgnoreNode;
  434. };