voxel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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 "irrlicht_changes/printing.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 the intersection of this area and `a`.
  161. */
  162. VoxelArea intersect(const VoxelArea &a) const
  163. {
  164. // This is an example of an operation that would be simpler with
  165. // non-inclusive edges, but oh well.
  166. VoxelArea ret;
  167. if (a.MaxEdge.X < MinEdge.X || a.MinEdge.X > MaxEdge.X)
  168. return VoxelArea();
  169. if (a.MaxEdge.Y < MinEdge.Y || a.MinEdge.Y > MaxEdge.Y)
  170. return VoxelArea();
  171. if (a.MaxEdge.Z < MinEdge.Z || a.MinEdge.Z > MaxEdge.Z)
  172. return VoxelArea();
  173. ret.MinEdge.X = std::max(a.MinEdge.X, MinEdge.X);
  174. ret.MaxEdge.X = std::min(a.MaxEdge.X, MaxEdge.X);
  175. ret.MinEdge.Y = std::max(a.MinEdge.Y, MinEdge.Y);
  176. ret.MaxEdge.Y = std::min(a.MaxEdge.Y, MaxEdge.Y);
  177. ret.MinEdge.Z = std::max(a.MinEdge.Z, MinEdge.Z);
  178. ret.MaxEdge.Z = std::min(a.MaxEdge.Z, MaxEdge.Z);
  179. return ret;
  180. }
  181. /*
  182. Returns 0-6 non-overlapping areas that can be added to
  183. a to make up this area.
  184. a: area inside *this
  185. */
  186. void diff(const VoxelArea &a, std::list<VoxelArea> &result)
  187. {
  188. /*
  189. This can result in a maximum of 6 areas
  190. */
  191. // If a is an empty area, return the current area as a whole
  192. if(a.getExtent() == v3s16(0,0,0))
  193. {
  194. VoxelArea b = *this;
  195. if(b.getVolume() != 0)
  196. result.push_back(b);
  197. return;
  198. }
  199. assert(contains(a)); // pre-condition
  200. // Take back area, XY inclusive
  201. {
  202. v3s16 min(MinEdge.X, MinEdge.Y, a.MaxEdge.Z+1);
  203. v3s16 max(MaxEdge.X, MaxEdge.Y, MaxEdge.Z);
  204. VoxelArea b(min, max);
  205. if(b.getVolume() != 0)
  206. result.push_back(b);
  207. }
  208. // Take front area, XY inclusive
  209. {
  210. v3s16 min(MinEdge.X, MinEdge.Y, MinEdge.Z);
  211. v3s16 max(MaxEdge.X, MaxEdge.Y, a.MinEdge.Z-1);
  212. VoxelArea b(min, max);
  213. if(b.getVolume() != 0)
  214. result.push_back(b);
  215. }
  216. // Take top area, X inclusive
  217. {
  218. v3s16 min(MinEdge.X, a.MaxEdge.Y+1, a.MinEdge.Z);
  219. v3s16 max(MaxEdge.X, MaxEdge.Y, a.MaxEdge.Z);
  220. VoxelArea b(min, max);
  221. if(b.getVolume() != 0)
  222. result.push_back(b);
  223. }
  224. // Take bottom area, X inclusive
  225. {
  226. v3s16 min(MinEdge.X, MinEdge.Y, a.MinEdge.Z);
  227. v3s16 max(MaxEdge.X, a.MinEdge.Y-1, a.MaxEdge.Z);
  228. VoxelArea b(min, max);
  229. if(b.getVolume() != 0)
  230. result.push_back(b);
  231. }
  232. // Take left area, non-inclusive
  233. {
  234. v3s16 min(MinEdge.X, a.MinEdge.Y, a.MinEdge.Z);
  235. v3s16 max(a.MinEdge.X-1, a.MaxEdge.Y, a.MaxEdge.Z);
  236. VoxelArea b(min, max);
  237. if(b.getVolume() != 0)
  238. result.push_back(b);
  239. }
  240. // Take right area, non-inclusive
  241. {
  242. v3s16 min(a.MaxEdge.X+1, a.MinEdge.Y, a.MinEdge.Z);
  243. v3s16 max(MaxEdge.X, a.MaxEdge.Y, a.MaxEdge.Z);
  244. VoxelArea b(min, max);
  245. if(b.getVolume() != 0)
  246. result.push_back(b);
  247. }
  248. }
  249. /*
  250. Translates position from virtual coordinates to array index
  251. */
  252. s32 index(s16 x, s16 y, s16 z) const
  253. {
  254. s32 i = (s32)(z - MinEdge.Z) * m_cache_extent.Y * m_cache_extent.X
  255. + (y - MinEdge.Y) * m_cache_extent.X
  256. + (x - MinEdge.X);
  257. return i;
  258. }
  259. s32 index(v3s16 p) const
  260. {
  261. return index(p.X, p.Y, p.Z);
  262. }
  263. /**
  264. * Translate index in the X coordinate
  265. */
  266. static void add_x(const v3s16 &extent, u32 &i, s16 a)
  267. {
  268. i += a;
  269. }
  270. /**
  271. * Translate index in the Y coordinate
  272. */
  273. static void add_y(const v3s16 &extent, u32 &i, s16 a)
  274. {
  275. i += a * extent.X;
  276. }
  277. /**
  278. * Translate index in the Z coordinate
  279. */
  280. static void add_z(const v3s16 &extent, u32 &i, s16 a)
  281. {
  282. i += a * extent.X * extent.Y;
  283. }
  284. /**
  285. * Translate index in space
  286. */
  287. static void add_p(const v3s16 &extent, u32 &i, v3s16 a)
  288. {
  289. i += a.Z * extent.X * extent.Y + a.Y * extent.X + a.X;
  290. }
  291. /*
  292. Print method for debugging
  293. */
  294. void print(std::ostream &o) const
  295. {
  296. o << MinEdge << MaxEdge << "="
  297. << m_cache_extent.X << "x" << m_cache_extent.Y << "x" << m_cache_extent.Z
  298. << "=" << getVolume();
  299. }
  300. // Edges are inclusive
  301. v3s16 MinEdge = v3s16(1,1,1);
  302. v3s16 MaxEdge;
  303. private:
  304. void cacheExtent()
  305. {
  306. m_cache_extent = MaxEdge - MinEdge + v3s16(1,1,1);
  307. }
  308. v3s16 m_cache_extent = v3s16(0,0,0);
  309. };
  310. // unused
  311. #define VOXELFLAG_UNUSED (1 << 0)
  312. // no data about that node
  313. #define VOXELFLAG_NO_DATA (1 << 1)
  314. // Algorithm-dependent
  315. #define VOXELFLAG_CHECKED1 (1 << 2)
  316. // Algorithm-dependent
  317. #define VOXELFLAG_CHECKED2 (1 << 3)
  318. // Algorithm-dependent
  319. #define VOXELFLAG_CHECKED3 (1 << 4)
  320. // Algorithm-dependent
  321. #define VOXELFLAG_CHECKED4 (1 << 5)
  322. enum VoxelPrintMode
  323. {
  324. VOXELPRINT_NOTHING,
  325. VOXELPRINT_MATERIAL,
  326. VOXELPRINT_WATERPRESSURE,
  327. VOXELPRINT_LIGHT_DAY,
  328. };
  329. class VoxelManipulator
  330. {
  331. public:
  332. VoxelManipulator() = default;
  333. virtual ~VoxelManipulator();
  334. /*
  335. These are a bit slow and shouldn't be used internally.
  336. Use m_data[m_area.index(p)] instead.
  337. */
  338. MapNode getNode(const v3s16 &p)
  339. {
  340. VoxelArea voxel_area(p);
  341. addArea(voxel_area);
  342. if (m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA) {
  343. /*dstream<<"EXCEPT: VoxelManipulator::getNode(): "
  344. <<"p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"
  345. <<", index="<<m_area.index(p)
  346. <<", flags="<<(int)m_flags[m_area.index(p)]
  347. <<" is inexistent"<<std::endl;*/
  348. throw InvalidPositionException
  349. ("VoxelManipulator: getNode: inexistent");
  350. }
  351. return m_data[m_area.index(p)];
  352. }
  353. MapNode getNodeNoEx(const v3s16 &p)
  354. {
  355. VoxelArea voxel_area(p);
  356. addArea(voxel_area);
  357. if (m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA) {
  358. return {CONTENT_IGNORE};
  359. }
  360. return m_data[m_area.index(p)];
  361. }
  362. MapNode getNodeNoExNoEmerge(const v3s16 &p)
  363. {
  364. if (!m_area.contains(p))
  365. return {CONTENT_IGNORE};
  366. if (m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA)
  367. return {CONTENT_IGNORE};
  368. return m_data[m_area.index(p)];
  369. }
  370. // Stuff explodes if non-emerged area is touched with this.
  371. // Emerge first, and check VOXELFLAG_NO_DATA if appropriate.
  372. MapNode & getNodeRefUnsafe(const v3s16 &p)
  373. {
  374. return m_data[m_area.index(p)];
  375. }
  376. const MapNode & getNodeRefUnsafeCheckFlags(const v3s16 &p)
  377. {
  378. s32 index = m_area.index(p);
  379. if (m_flags[index] & VOXELFLAG_NO_DATA)
  380. return ContentIgnoreNode;
  381. return m_data[index];
  382. }
  383. u8 & getFlagsRefUnsafe(const v3s16 &p)
  384. {
  385. return m_flags[m_area.index(p)];
  386. }
  387. bool exists(const v3s16 &p)
  388. {
  389. return m_area.contains(p) &&
  390. !(getFlagsRefUnsafe(p) & VOXELFLAG_NO_DATA);
  391. }
  392. void setNode(const v3s16 &p, const MapNode &n)
  393. {
  394. VoxelArea voxel_area(p);
  395. addArea(voxel_area);
  396. m_data[m_area.index(p)] = n;
  397. m_flags[m_area.index(p)] &= ~VOXELFLAG_NO_DATA;
  398. }
  399. // TODO: Should be removed and replaced with setNode
  400. void setNodeNoRef(const v3s16 &p, const MapNode &n)
  401. {
  402. setNode(p, n);
  403. }
  404. /*
  405. Set stuff if available without an emerge.
  406. Return false if failed.
  407. This is convenient but slower than playing around directly
  408. with the m_data table with indices.
  409. */
  410. bool setNodeNoEmerge(const v3s16 &p, MapNode n)
  411. {
  412. if(!m_area.contains(p))
  413. return false;
  414. m_data[m_area.index(p)] = n;
  415. return true;
  416. }
  417. /*
  418. Control
  419. */
  420. virtual void clear();
  421. void print(std::ostream &o, const NodeDefManager *nodemgr,
  422. VoxelPrintMode mode=VOXELPRINT_MATERIAL);
  423. void addArea(const VoxelArea &area);
  424. /*
  425. Copy data and set flags to 0
  426. dst_area.getExtent() <= src_area.getExtent()
  427. */
  428. void copyFrom(MapNode *src, const VoxelArea& src_area,
  429. v3s16 from_pos, v3s16 to_pos, const v3s16 &size);
  430. // Copy data
  431. void copyTo(MapNode *dst, const VoxelArea& dst_area,
  432. v3s16 dst_pos, v3s16 from_pos, const v3s16 &size);
  433. /*
  434. Algorithms
  435. */
  436. void clearFlag(u8 flag);
  437. /*
  438. Member variables
  439. */
  440. /*
  441. The area that is stored in m_data.
  442. addInternalBox should not be used if getExtent() == v3s16(0,0,0)
  443. MaxEdge is 1 higher than maximum allowed position
  444. */
  445. VoxelArea m_area;
  446. /*
  447. nullptr if data size is 0 (extent (0,0,0))
  448. Data is stored as [z*h*w + y*h + x]
  449. */
  450. MapNode *m_data = nullptr;
  451. /*
  452. Flags of all nodes
  453. */
  454. u8 *m_flags = nullptr;
  455. static const MapNode ContentIgnoreNode;
  456. };