voxel.h 13 KB

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