voxel.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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(VoxelArea &a)
  70. {
  71. if(getExtent() == v3s16(0,0,0))
  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(v3s16 p)
  84. {
  85. if(getExtent() == v3s16(0,0,0))
  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(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. s32 getVolume() const
  122. {
  123. v3s16 e = getExtent();
  124. return (s32)e.X * (s32)e.Y * (s32)e.Z;
  125. }
  126. bool contains(const VoxelArea &a) const
  127. {
  128. // No area contains an empty area
  129. // NOTE: Algorithms depend on this, so do not change.
  130. if(a.getExtent() == v3s16(0,0,0))
  131. return false;
  132. return(
  133. a.MinEdge.X >= MinEdge.X && a.MaxEdge.X <= MaxEdge.X &&
  134. a.MinEdge.Y >= MinEdge.Y && a.MaxEdge.Y <= MaxEdge.Y &&
  135. a.MinEdge.Z >= MinEdge.Z && a.MaxEdge.Z <= MaxEdge.Z
  136. );
  137. }
  138. bool contains(v3s16 p) const
  139. {
  140. return(
  141. p.X >= MinEdge.X && p.X <= MaxEdge.X &&
  142. p.Y >= MinEdge.Y && p.Y <= MaxEdge.Y &&
  143. p.Z >= MinEdge.Z && p.Z <= MaxEdge.Z
  144. );
  145. }
  146. bool contains(s32 i) const
  147. {
  148. return (i >= 0 && i < getVolume());
  149. }
  150. bool operator==(const VoxelArea &other) const
  151. {
  152. return (MinEdge == other.MinEdge
  153. && MaxEdge == other.MaxEdge);
  154. }
  155. VoxelArea operator+(v3s16 off) const
  156. {
  157. return VoxelArea(MinEdge+off, MaxEdge+off);
  158. }
  159. VoxelArea operator-(v3s16 off) const
  160. {
  161. return VoxelArea(MinEdge-off, MaxEdge-off);
  162. }
  163. /*
  164. Returns 0-6 non-overlapping areas that can be added to
  165. a to make up this area.
  166. a: area inside *this
  167. */
  168. void diff(const VoxelArea &a, std::list<VoxelArea> &result)
  169. {
  170. /*
  171. This can result in a maximum of 6 areas
  172. */
  173. // If a is an empty area, return the current area as a whole
  174. if(a.getExtent() == v3s16(0,0,0))
  175. {
  176. VoxelArea b = *this;
  177. if(b.getVolume() != 0)
  178. result.push_back(b);
  179. return;
  180. }
  181. assert(contains(a));
  182. // Take back area, XY inclusive
  183. {
  184. v3s16 min(MinEdge.X, MinEdge.Y, a.MaxEdge.Z+1);
  185. v3s16 max(MaxEdge.X, MaxEdge.Y, MaxEdge.Z);
  186. VoxelArea b(min, max);
  187. if(b.getVolume() != 0)
  188. result.push_back(b);
  189. }
  190. // Take front area, XY inclusive
  191. {
  192. v3s16 min(MinEdge.X, MinEdge.Y, MinEdge.Z);
  193. v3s16 max(MaxEdge.X, MaxEdge.Y, a.MinEdge.Z-1);
  194. VoxelArea b(min, max);
  195. if(b.getVolume() != 0)
  196. result.push_back(b);
  197. }
  198. // Take top area, X inclusive
  199. {
  200. v3s16 min(MinEdge.X, a.MaxEdge.Y+1, a.MinEdge.Z);
  201. v3s16 max(MaxEdge.X, MaxEdge.Y, a.MaxEdge.Z);
  202. VoxelArea b(min, max);
  203. if(b.getVolume() != 0)
  204. result.push_back(b);
  205. }
  206. // Take bottom area, X inclusive
  207. {
  208. v3s16 min(MinEdge.X, MinEdge.Y, a.MinEdge.Z);
  209. v3s16 max(MaxEdge.X, a.MinEdge.Y-1, a.MaxEdge.Z);
  210. VoxelArea b(min, max);
  211. if(b.getVolume() != 0)
  212. result.push_back(b);
  213. }
  214. // Take left area, non-inclusive
  215. {
  216. v3s16 min(MinEdge.X, a.MinEdge.Y, a.MinEdge.Z);
  217. v3s16 max(a.MinEdge.X-1, a.MaxEdge.Y, a.MaxEdge.Z);
  218. VoxelArea b(min, max);
  219. if(b.getVolume() != 0)
  220. result.push_back(b);
  221. }
  222. // Take right area, non-inclusive
  223. {
  224. v3s16 min(a.MaxEdge.X+1, a.MinEdge.Y, a.MinEdge.Z);
  225. v3s16 max(MaxEdge.X, a.MaxEdge.Y, a.MaxEdge.Z);
  226. VoxelArea b(min, max);
  227. if(b.getVolume() != 0)
  228. result.push_back(b);
  229. }
  230. }
  231. /*
  232. Translates position from virtual coordinates to array index
  233. */
  234. s32 index(s16 x, s16 y, s16 z) const
  235. {
  236. v3s16 em = getExtent();
  237. v3s16 off = MinEdge;
  238. s32 i = (s32)(z-off.Z)*em.Y*em.X + (y-off.Y)*em.X + (x-off.X);
  239. //dstream<<" i("<<x<<","<<y<<","<<z<<")="<<i<<" ";
  240. return i;
  241. }
  242. s32 index(v3s16 p) const
  243. {
  244. return index(p.X, p.Y, p.Z);
  245. }
  246. // Translate index in the X coordinate
  247. void add_x(const v3s16 &extent, u32 &i, s16 a)
  248. {
  249. i += a;
  250. }
  251. // Translate index in the Y coordinate
  252. void add_y(const v3s16 &extent, u32 &i, s16 a)
  253. {
  254. i += a * extent.X;
  255. }
  256. // Translate index in the Z coordinate
  257. void add_z(const v3s16 &extent, u32 &i, s16 a)
  258. {
  259. i += a * extent.X*extent.Y;
  260. }
  261. // Translate index in space
  262. void add_p(const v3s16 &extent, u32 &i, v3s16 a)
  263. {
  264. i += a.Z*extent.X*extent.Y + a.Y*extent.X + a.X;
  265. }
  266. /*
  267. Print method for debugging
  268. */
  269. void print(std::ostream &o) const
  270. {
  271. v3s16 e = getExtent();
  272. o<<"("<<MinEdge.X
  273. <<","<<MinEdge.Y
  274. <<","<<MinEdge.Z
  275. <<")("<<MaxEdge.X
  276. <<","<<MaxEdge.Y
  277. <<","<<MaxEdge.Z
  278. <<")"
  279. <<"="<<e.X<<"x"<<e.Y<<"x"<<e.Z<<"="<<getVolume();
  280. }
  281. // Edges are inclusive
  282. v3s16 MinEdge;
  283. v3s16 MaxEdge;
  284. };
  285. // unused
  286. #define VOXELFLAG_UNUSED (1<<0)
  287. // no data about that node
  288. #define VOXELFLAG_NO_DATA (1<<1)
  289. // Algorithm-dependent
  290. #define VOXELFLAG_CHECKED1 (1<<2)
  291. // Algorithm-dependent
  292. #define VOXELFLAG_CHECKED2 (1<<3)
  293. // Algorithm-dependent
  294. #define VOXELFLAG_CHECKED3 (1<<4)
  295. // Algorithm-dependent
  296. #define VOXELFLAG_CHECKED4 (1<<5)
  297. enum VoxelPrintMode
  298. {
  299. VOXELPRINT_NOTHING,
  300. VOXELPRINT_MATERIAL,
  301. VOXELPRINT_WATERPRESSURE,
  302. VOXELPRINT_LIGHT_DAY,
  303. };
  304. class VoxelManipulator /*: public NodeContainer*/
  305. {
  306. public:
  307. VoxelManipulator();
  308. virtual ~VoxelManipulator();
  309. /*
  310. Virtuals from NodeContainer
  311. */
  312. /*virtual u16 nodeContainerId() const
  313. {
  314. return NODECONTAINER_ID_VOXELMANIPULATOR;
  315. }
  316. bool isValidPosition(v3s16 p)
  317. {
  318. addArea(p);
  319. return !(m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA);
  320. }*/
  321. /*
  322. These are a bit slow and shouldn't be used internally.
  323. Use m_data[m_area.index(p)] instead.
  324. */
  325. MapNode getNode(v3s16 p)
  326. {
  327. addArea(p);
  328. if(m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA)
  329. {
  330. /*dstream<<"EXCEPT: VoxelManipulator::getNode(): "
  331. <<"p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"
  332. <<", index="<<m_area.index(p)
  333. <<", flags="<<(int)m_flags[m_area.index(p)]
  334. <<" is inexistent"<<std::endl;*/
  335. throw InvalidPositionException
  336. ("VoxelManipulator: getNode: inexistent");
  337. }
  338. return m_data[m_area.index(p)];
  339. }
  340. MapNode getNodeNoEx(v3s16 p)
  341. {
  342. addArea(p);
  343. if(m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA)
  344. {
  345. return MapNode(CONTENT_IGNORE);
  346. }
  347. return m_data[m_area.index(p)];
  348. }
  349. MapNode getNodeNoExNoEmerge(v3s16 p)
  350. {
  351. if(m_area.contains(p) == false)
  352. return MapNode(CONTENT_IGNORE);
  353. if(m_flags[m_area.index(p)] & VOXELFLAG_NO_DATA)
  354. return MapNode(CONTENT_IGNORE);
  355. return m_data[m_area.index(p)];
  356. }
  357. // Stuff explodes if non-emerged area is touched with this.
  358. // Emerge first, and check VOXELFLAG_NO_DATA if appropriate.
  359. MapNode & getNodeRefUnsafe(v3s16 p)
  360. {
  361. return m_data[m_area.index(p)];
  362. }
  363. u8 & getFlagsRefUnsafe(v3s16 p)
  364. {
  365. return m_flags[m_area.index(p)];
  366. }
  367. bool exists(v3s16 p)
  368. {
  369. return m_area.contains(p) &&
  370. !(getFlagsRefUnsafe(p) & VOXELFLAG_NO_DATA);
  371. }
  372. MapNode & getNodeRef(v3s16 p)
  373. {
  374. addArea(p);
  375. if(getFlagsRefUnsafe(p) & VOXELFLAG_NO_DATA)
  376. {
  377. /*dstream<<"EXCEPT: VoxelManipulator::getNode(): "
  378. <<"p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"
  379. <<", index="<<m_area.index(p)
  380. <<", flags="<<(int)getFlagsRefUnsafe(p)
  381. <<" is inexistent"<<std::endl;*/
  382. throw InvalidPositionException
  383. ("VoxelManipulator: getNode: inexistent");
  384. }
  385. return getNodeRefUnsafe(p);
  386. }
  387. void setNode(v3s16 p, const MapNode &n)
  388. {
  389. addArea(p);
  390. m_data[m_area.index(p)] = n;
  391. m_flags[m_area.index(p)] &= ~VOXELFLAG_NO_DATA;
  392. }
  393. // TODO: Should be removed and replaced with setNode
  394. void setNodeNoRef(v3s16 p, const MapNode &n)
  395. {
  396. setNode(p, n);
  397. }
  398. /*void setExists(VoxelArea a)
  399. {
  400. addArea(a);
  401. for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
  402. for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
  403. for(s32 x=a.MinEdge.X; x<=a.MaxEdge.X; x++)
  404. {
  405. m_flags[m_area.index(x,y,z)] &= ~VOXELFLAG_NO_DATA;
  406. }
  407. }*/
  408. /*MapNode & operator[](v3s16 p)
  409. {
  410. //dstream<<"operator[] p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;
  411. if(isValidPosition(p) == false)
  412. addArea(VoxelArea(p));
  413. return m_data[m_area.index(p)];
  414. }*/
  415. /*
  416. Set stuff if available without an emerge.
  417. Return false if failed.
  418. This is convenient but slower than playing around directly
  419. with the m_data table with indices.
  420. */
  421. bool setNodeNoEmerge(v3s16 p, MapNode n)
  422. {
  423. if(m_area.contains(p) == false)
  424. return false;
  425. m_data[m_area.index(p)] = n;
  426. return true;
  427. }
  428. bool setNodeNoEmerge(s32 i, MapNode n)
  429. {
  430. if(m_area.contains(i) == false)
  431. return false;
  432. m_data[i] = n;
  433. return true;
  434. }
  435. /*bool setContentNoEmerge(v3s16 p, u8 c)
  436. {
  437. if(isValidPosition(p) == false)
  438. return false;
  439. m_data[m_area.index(p)].d = c;
  440. }*/
  441. /*
  442. Control
  443. */
  444. virtual void clear();
  445. void print(std::ostream &o, INodeDefManager *nodemgr,
  446. VoxelPrintMode mode=VOXELPRINT_MATERIAL);
  447. void addArea(VoxelArea area);
  448. /*
  449. Copy data and set flags to 0
  450. dst_area.getExtent() <= src_area.getExtent()
  451. */
  452. void copyFrom(MapNode *src, const VoxelArea& src_area,
  453. v3s16 from_pos, v3s16 to_pos, v3s16 size);
  454. // Copy data
  455. void copyTo(MapNode *dst, const VoxelArea& dst_area,
  456. v3s16 dst_pos, v3s16 from_pos, v3s16 size);
  457. /*
  458. Algorithms
  459. */
  460. void clearFlag(u8 flag);
  461. // TODO: Move to voxelalgorithms.h
  462. void unspreadLight(enum LightBank bank, v3s16 p, u8 oldlight,
  463. std::set<v3s16> & light_sources, INodeDefManager *nodemgr);
  464. void unspreadLight(enum LightBank bank,
  465. std::map<v3s16, u8> & from_nodes,
  466. std::set<v3s16> & light_sources, INodeDefManager *nodemgr);
  467. void spreadLight(enum LightBank bank, v3s16 p, INodeDefManager *nodemgr);
  468. void spreadLight(enum LightBank bank,
  469. std::set<v3s16> & from_nodes, INodeDefManager *nodemgr);
  470. /*
  471. Virtual functions
  472. */
  473. /*
  474. Member variables
  475. */
  476. /*
  477. The area that is stored in m_data.
  478. addInternalBox should not be used if getExtent() == v3s16(0,0,0)
  479. MaxEdge is 1 higher than maximum allowed position
  480. */
  481. VoxelArea m_area;
  482. /*
  483. NULL if data size is 0 (extent (0,0,0))
  484. Data is stored as [z*h*w + y*h + x]
  485. */
  486. MapNode *m_data;
  487. /*
  488. Flags of all nodes
  489. */
  490. u8 *m_flags;
  491. //TODO: Use these or remove them
  492. //TODO: Would these make any speed improvement?
  493. //bool m_pressure_route_valid;
  494. //v3s16 m_pressure_route_surface;
  495. /*
  496. Some settings
  497. */
  498. //bool m_disable_water_climb;
  499. private:
  500. };
  501. #endif