mapblock.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 MAPBLOCK_HEADER
  17. #define MAPBLOCK_HEADER
  18. #include <set>
  19. #include "debug.h"
  20. #include "irr_v3d.h"
  21. #include "mapnode.h"
  22. #include "exceptions.h"
  23. #include "constants.h"
  24. #include "staticobject.h"
  25. #include "nodemetadata.h"
  26. #include "nodetimer.h"
  27. #include "modifiedstate.h"
  28. #include "util/numeric.h" // getContainerPos
  29. #include "settings.h"
  30. #include "mapgen.h"
  31. class Map;
  32. class NodeMetadataList;
  33. class IGameDef;
  34. class MapBlockMesh;
  35. class VoxelManipulator;
  36. #define BLOCK_TIMESTAMP_UNDEFINED 0xffffffff
  37. ////
  38. //// MapBlock modified reason flags
  39. ////
  40. #define MOD_REASON_INITIAL (1 << 0)
  41. #define MOD_REASON_REALLOCATE (1 << 1)
  42. #define MOD_REASON_SET_IS_UNDERGROUND (1 << 2)
  43. #define MOD_REASON_SET_LIGHTING_COMPLETE (1 << 3)
  44. #define MOD_REASON_SET_GENERATED (1 << 4)
  45. #define MOD_REASON_SET_NODE (1 << 5)
  46. #define MOD_REASON_SET_NODE_NO_CHECK (1 << 6)
  47. #define MOD_REASON_SET_TIMESTAMP (1 << 7)
  48. #define MOD_REASON_REPORT_META_CHANGE (1 << 8)
  49. #define MOD_REASON_CLEAR_ALL_OBJECTS (1 << 9)
  50. #define MOD_REASON_BLOCK_EXPIRED (1 << 10)
  51. #define MOD_REASON_ADD_ACTIVE_OBJECT_RAW (1 << 11)
  52. #define MOD_REASON_REMOVE_OBJECTS_REMOVE (1 << 12)
  53. #define MOD_REASON_REMOVE_OBJECTS_DEACTIVATE (1 << 13)
  54. #define MOD_REASON_TOO_MANY_OBJECTS (1 << 14)
  55. #define MOD_REASON_STATIC_DATA_ADDED (1 << 15)
  56. #define MOD_REASON_STATIC_DATA_REMOVED (1 << 16)
  57. #define MOD_REASON_STATIC_DATA_CHANGED (1 << 17)
  58. #define MOD_REASON_EXPIRE_DAYNIGHTDIFF (1 << 18)
  59. #define MOD_REASON_UNKNOWN (1 << 19)
  60. ////
  61. //// MapBlock itself
  62. ////
  63. class MapBlock
  64. {
  65. public:
  66. MapBlock(Map *parent, v3s16 pos, IGameDef *gamedef, bool dummy=false);
  67. ~MapBlock();
  68. /*virtual u16 nodeContainerId() const
  69. {
  70. return NODECONTAINER_ID_MAPBLOCK;
  71. }*/
  72. Map * getParent()
  73. {
  74. return m_parent;
  75. }
  76. void reallocate()
  77. {
  78. delete[] data;
  79. data = new MapNode[nodecount];
  80. for (u32 i = 0; i < nodecount; i++)
  81. data[i] = MapNode(CONTENT_IGNORE);
  82. raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_REALLOCATE);
  83. }
  84. MapNode* getData()
  85. {
  86. return data;
  87. }
  88. ////
  89. //// Modification tracking methods
  90. ////
  91. void raiseModified(u32 mod, u32 reason=MOD_REASON_UNKNOWN)
  92. {
  93. if (mod > m_modified) {
  94. m_modified = mod;
  95. m_modified_reason = reason;
  96. if (m_modified >= MOD_STATE_WRITE_AT_UNLOAD)
  97. m_disk_timestamp = m_timestamp;
  98. } else if (mod == m_modified) {
  99. m_modified_reason |= reason;
  100. }
  101. }
  102. inline u32 getModified()
  103. {
  104. return m_modified;
  105. }
  106. inline u32 getModifiedReason()
  107. {
  108. return m_modified_reason;
  109. }
  110. std::string getModifiedReasonString();
  111. inline void resetModified()
  112. {
  113. m_modified = MOD_STATE_CLEAN;
  114. m_modified_reason = 0;
  115. }
  116. ////
  117. //// Flags
  118. ////
  119. inline bool isDummy()
  120. {
  121. return !data;
  122. }
  123. inline void unDummify()
  124. {
  125. assert(isDummy()); // Pre-condition
  126. reallocate();
  127. }
  128. // is_underground getter/setter
  129. inline bool getIsUnderground()
  130. {
  131. return is_underground;
  132. }
  133. inline void setIsUnderground(bool a_is_underground)
  134. {
  135. is_underground = a_is_underground;
  136. raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_IS_UNDERGROUND);
  137. }
  138. inline void setLightingComplete(u16 newflags)
  139. {
  140. if (newflags != m_lighting_complete) {
  141. m_lighting_complete = newflags;
  142. raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_LIGHTING_COMPLETE);
  143. }
  144. }
  145. inline u16 getLightingComplete()
  146. {
  147. return m_lighting_complete;
  148. }
  149. inline void setLightingComplete(LightBank bank, u8 direction,
  150. bool is_complete)
  151. {
  152. assert(direction >= 0 && direction <= 5);
  153. if (bank == LIGHTBANK_NIGHT) {
  154. direction += 6;
  155. }
  156. u16 newflags = m_lighting_complete;
  157. if (is_complete) {
  158. newflags |= 1 << direction;
  159. } else {
  160. newflags &= ~(1 << direction);
  161. }
  162. setLightingComplete(newflags);
  163. }
  164. inline bool isLightingComplete(LightBank bank, u8 direction)
  165. {
  166. assert(direction >= 0 && direction <= 5);
  167. if (bank == LIGHTBANK_NIGHT) {
  168. direction += 6;
  169. }
  170. return (m_lighting_complete & (1 << direction)) != 0;
  171. }
  172. inline bool isGenerated()
  173. {
  174. return m_generated;
  175. }
  176. inline void setGenerated(bool b)
  177. {
  178. if (b != m_generated) {
  179. raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_GENERATED);
  180. m_generated = b;
  181. }
  182. }
  183. ////
  184. //// Position stuff
  185. ////
  186. inline v3s16 getPos()
  187. {
  188. return m_pos;
  189. }
  190. inline v3s16 getPosRelative()
  191. {
  192. return m_pos_relative;
  193. }
  194. inline core::aabbox3d<s16> getBox()
  195. {
  196. return core::aabbox3d<s16>(getPosRelative(),
  197. getPosRelative()
  198. + v3s16(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE)
  199. - v3s16(1,1,1));
  200. }
  201. ////
  202. //// Regular MapNode get-setters
  203. ////
  204. inline bool isValidPosition(s16 x, s16 y, s16 z)
  205. {
  206. return data
  207. && x >= 0 && x < MAP_BLOCKSIZE
  208. && y >= 0 && y < MAP_BLOCKSIZE
  209. && z >= 0 && z < MAP_BLOCKSIZE;
  210. }
  211. inline bool isValidPosition(v3s16 p)
  212. {
  213. return isValidPosition(p.X, p.Y, p.Z);
  214. }
  215. inline MapNode getNode(s16 x, s16 y, s16 z, bool *valid_position)
  216. {
  217. *valid_position = isValidPosition(x, y, z);
  218. if (!*valid_position)
  219. return MapNode(CONTENT_IGNORE);
  220. return data[z * zstride + y * ystride + x];
  221. }
  222. inline MapNode getNode(v3s16 p, bool *valid_position)
  223. {
  224. return getNode(p.X, p.Y, p.Z, valid_position);
  225. }
  226. inline MapNode getNodeNoEx(v3s16 p)
  227. {
  228. bool is_valid;
  229. return getNode(p.X, p.Y, p.Z, &is_valid);
  230. }
  231. inline void setNode(s16 x, s16 y, s16 z, MapNode & n)
  232. {
  233. if (!isValidPosition(x, y, z))
  234. throw InvalidPositionException();
  235. data[z * zstride + y * ystride + x] = n;
  236. raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_NODE);
  237. }
  238. inline void setNode(v3s16 p, MapNode & n)
  239. {
  240. setNode(p.X, p.Y, p.Z, n);
  241. }
  242. ////
  243. //// Non-checking variants of the above
  244. ////
  245. inline MapNode getNodeNoCheck(s16 x, s16 y, s16 z, bool *valid_position)
  246. {
  247. *valid_position = data != nullptr;
  248. if (!valid_position)
  249. return MapNode(CONTENT_IGNORE);
  250. return data[z * zstride + y * ystride + x];
  251. }
  252. inline MapNode getNodeNoCheck(v3s16 p, bool *valid_position)
  253. {
  254. return getNodeNoCheck(p.X, p.Y, p.Z, valid_position);
  255. }
  256. ////
  257. //// Non-checking, unsafe variants of the above
  258. //// MapBlock must be loaded by another function in the same scope/function
  259. //// Caller must ensure that this is not a dummy block (by calling isDummy())
  260. ////
  261. inline const MapNode &getNodeUnsafe(s16 x, s16 y, s16 z)
  262. {
  263. return data[z * zstride + y * ystride + x];
  264. }
  265. inline const MapNode &getNodeUnsafe(v3s16 &p)
  266. {
  267. return getNodeUnsafe(p.X, p.Y, p.Z);
  268. }
  269. inline void setNodeNoCheck(s16 x, s16 y, s16 z, MapNode & n)
  270. {
  271. if (!data)
  272. throw InvalidPositionException();
  273. data[z * zstride + y * ystride + x] = n;
  274. raiseModified(MOD_STATE_WRITE_NEEDED, MOD_REASON_SET_NODE_NO_CHECK);
  275. }
  276. inline void setNodeNoCheck(v3s16 p, MapNode & n)
  277. {
  278. setNodeNoCheck(p.X, p.Y, p.Z, n);
  279. }
  280. // These functions consult the parent container if the position
  281. // is not valid on this MapBlock.
  282. bool isValidPositionParent(v3s16 p);
  283. MapNode getNodeParent(v3s16 p, bool *is_valid_position = NULL);
  284. void setNodeParent(v3s16 p, MapNode & n);
  285. inline void drawbox(s16 x0, s16 y0, s16 z0, s16 w, s16 h, s16 d, MapNode node)
  286. {
  287. for (u16 z = 0; z < d; z++)
  288. for (u16 y = 0; y < h; y++)
  289. for (u16 x = 0; x < w; x++)
  290. setNode(x0 + x, y0 + y, z0 + z, node);
  291. }
  292. // See comments in mapblock.cpp
  293. bool propagateSunlight(std::set<v3s16> &light_sources,
  294. bool remove_light=false, bool *black_air_left=NULL);
  295. // Copies data to VoxelManipulator to getPosRelative()
  296. void copyTo(VoxelManipulator &dst);
  297. // Copies data from VoxelManipulator getPosRelative()
  298. void copyFrom(VoxelManipulator &dst);
  299. // Update day-night lighting difference flag.
  300. // Sets m_day_night_differs to appropriate value.
  301. // These methods don't care about neighboring blocks.
  302. void actuallyUpdateDayNightDiff();
  303. // Call this to schedule what the previous function does to be done
  304. // when the value is actually needed.
  305. void expireDayNightDiff();
  306. inline bool getDayNightDiff()
  307. {
  308. if (m_day_night_differs_expired)
  309. actuallyUpdateDayNightDiff();
  310. return m_day_night_differs;
  311. }
  312. ////
  313. //// Miscellaneous stuff
  314. ////
  315. /*
  316. Tries to measure ground level.
  317. Return value:
  318. -1 = only air
  319. -2 = only ground
  320. -3 = random fail
  321. 0...MAP_BLOCKSIZE-1 = ground level
  322. */
  323. s16 getGroundLevel(v2s16 p2d);
  324. ////
  325. //// Timestamp (see m_timestamp)
  326. ////
  327. // NOTE: BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
  328. inline void setTimestamp(u32 time)
  329. {
  330. m_timestamp = time;
  331. raiseModified(MOD_STATE_WRITE_AT_UNLOAD, MOD_REASON_SET_TIMESTAMP);
  332. }
  333. inline void setTimestampNoChangedFlag(u32 time)
  334. {
  335. m_timestamp = time;
  336. }
  337. inline u32 getTimestamp()
  338. {
  339. return m_timestamp;
  340. }
  341. inline u32 getDiskTimestamp()
  342. {
  343. return m_disk_timestamp;
  344. }
  345. ////
  346. //// Usage timer (see m_usage_timer)
  347. ////
  348. inline void resetUsageTimer()
  349. {
  350. m_usage_timer = 0;
  351. }
  352. inline void incrementUsageTimer(float dtime)
  353. {
  354. m_usage_timer += dtime;
  355. }
  356. inline float getUsageTimer()
  357. {
  358. return m_usage_timer;
  359. }
  360. ////
  361. //// Reference counting (see m_refcount)
  362. ////
  363. inline void refGrab()
  364. {
  365. m_refcount++;
  366. }
  367. inline void refDrop()
  368. {
  369. m_refcount--;
  370. }
  371. inline int refGet()
  372. {
  373. return m_refcount;
  374. }
  375. ////
  376. //// Node Timers
  377. ////
  378. inline NodeTimer getNodeTimer(v3s16 p)
  379. {
  380. return m_node_timers.get(p);
  381. }
  382. inline void removeNodeTimer(v3s16 p)
  383. {
  384. m_node_timers.remove(p);
  385. }
  386. inline void setNodeTimer(const NodeTimer &t)
  387. {
  388. m_node_timers.set(t);
  389. }
  390. inline void clearNodeTimers()
  391. {
  392. m_node_timers.clear();
  393. }
  394. ////
  395. //// Serialization
  396. ///
  397. // These don't write or read version by itself
  398. // Set disk to true for on-disk format, false for over-the-network format
  399. // Precondition: version >= SER_FMT_VER_LOWEST_WRITE
  400. void serialize(std::ostream &os, u8 version, bool disk);
  401. // If disk == true: In addition to doing other things, will add
  402. // unknown blocks from id-name mapping to wndef
  403. void deSerialize(std::istream &is, u8 version, bool disk);
  404. void serializeNetworkSpecific(std::ostream &os);
  405. void deSerializeNetworkSpecific(std::istream &is);
  406. private:
  407. /*
  408. Private methods
  409. */
  410. void deSerialize_pre22(std::istream &is, u8 version, bool disk);
  411. /*
  412. Used only internally, because changes can't be tracked
  413. */
  414. inline MapNode &getNodeRef(s16 x, s16 y, s16 z)
  415. {
  416. if (!isValidPosition(x, y, z))
  417. throw InvalidPositionException();
  418. return data[z * zstride + y * ystride + x];
  419. }
  420. inline MapNode &getNodeRef(v3s16 &p)
  421. {
  422. return getNodeRef(p.X, p.Y, p.Z);
  423. }
  424. public:
  425. /*
  426. Public member variables
  427. */
  428. #ifndef SERVER // Only on client
  429. MapBlockMesh *mesh = nullptr;
  430. #endif
  431. NodeMetadataList m_node_metadata;
  432. NodeTimerList m_node_timers;
  433. StaticObjectList m_static_objects;
  434. static const u32 ystride = MAP_BLOCKSIZE;
  435. static const u32 zstride = MAP_BLOCKSIZE * MAP_BLOCKSIZE;
  436. static const u32 nodecount = MAP_BLOCKSIZE * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
  437. private:
  438. /*
  439. Private member variables
  440. */
  441. // NOTE: Lots of things rely on this being the Map
  442. Map *m_parent;
  443. // Position in blocks on parent
  444. v3s16 m_pos;
  445. /* This is the precalculated m_pos_relative value
  446. * This caches the value, improving performance by removing 3 s16 multiplications
  447. * at runtime on each getPosRelative call
  448. * For a 5 minutes runtime with valgrind this removes 3 * 19M s16 multiplications
  449. * The gain can be estimated in Release Build to 3 * 100M multiply operations for 5 mins
  450. */
  451. v3s16 m_pos_relative;
  452. IGameDef *m_gamedef;
  453. /*
  454. If NULL, block is a dummy block.
  455. Dummy blocks are used for caching not-found-on-disk blocks.
  456. */
  457. MapNode *data = nullptr;
  458. /*
  459. - On the server, this is used for telling whether the
  460. block has been modified from the one on disk.
  461. - On the client, this is used for nothing.
  462. */
  463. u32 m_modified = MOD_STATE_WRITE_NEEDED;
  464. u32 m_modified_reason = MOD_REASON_INITIAL;
  465. /*
  466. When propagating sunlight and the above block doesn't exist,
  467. sunlight is assumed if this is false.
  468. In practice this is set to true if the block is completely
  469. undeground with nothing visible above the ground except
  470. caves.
  471. */
  472. bool is_underground = false;
  473. /*!
  474. * Each bit indicates if light spreading was finished
  475. * in a direction. (Because the neighbor could also be unloaded.)
  476. * Bits (most significant first):
  477. * nothing, nothing, nothing, nothing,
  478. * night X-, night Y-, night Z-, night Z+, night Y+, night X+,
  479. * day X-, day Y-, day Z-, day Z+, day Y+, day X+.
  480. */
  481. u16 m_lighting_complete = 0xFFFF;
  482. // Whether day and night lighting differs
  483. bool m_day_night_differs = false;
  484. bool m_day_night_differs_expired = true;
  485. bool m_generated = false;
  486. /*
  487. When block is removed from active blocks, this is set to gametime.
  488. Value BLOCK_TIMESTAMP_UNDEFINED=0xffffffff means there is no timestamp.
  489. */
  490. u32 m_timestamp = BLOCK_TIMESTAMP_UNDEFINED;
  491. // The on-disk (or to-be on-disk) timestamp value
  492. u32 m_disk_timestamp = BLOCK_TIMESTAMP_UNDEFINED;
  493. /*
  494. When the block is accessed, this is set to 0.
  495. Map will unload the block when this reaches a timeout.
  496. */
  497. float m_usage_timer = 0;
  498. /*
  499. Reference count; currently used for determining if this block is in
  500. the list of blocks to be drawn.
  501. */
  502. int m_refcount = 0;
  503. };
  504. typedef std::vector<MapBlock*> MapBlockVect;
  505. inline bool objectpos_over_limit(v3f p)
  506. {
  507. const float max_limit_bs = MAX_MAP_GENERATION_LIMIT * BS;
  508. return p.X < -max_limit_bs ||
  509. p.X > max_limit_bs ||
  510. p.Y < -max_limit_bs ||
  511. p.Y > max_limit_bs ||
  512. p.Z < -max_limit_bs ||
  513. p.Z > max_limit_bs;
  514. }
  515. inline bool blockpos_over_max_limit(v3s16 p)
  516. {
  517. const s16 max_limit_bp = MAX_MAP_GENERATION_LIMIT / MAP_BLOCKSIZE;
  518. return p.X < -max_limit_bp ||
  519. p.X > max_limit_bp ||
  520. p.Y < -max_limit_bp ||
  521. p.Y > max_limit_bp ||
  522. p.Z < -max_limit_bp ||
  523. p.Z > max_limit_bp;
  524. }
  525. /*
  526. Returns the position of the block where the node is located
  527. */
  528. inline v3s16 getNodeBlockPos(v3s16 p)
  529. {
  530. return getContainerPos(p, MAP_BLOCKSIZE);
  531. }
  532. inline v2s16 getNodeSectorPos(v2s16 p)
  533. {
  534. return getContainerPos(p, MAP_BLOCKSIZE);
  535. }
  536. inline s16 getNodeBlockY(s16 y)
  537. {
  538. return getContainerPos(y, MAP_BLOCKSIZE);
  539. }
  540. inline void getNodeBlockPosWithOffset(const v3s16 &p, v3s16 &block, v3s16 &offset)
  541. {
  542. getContainerPosWithOffset(p, MAP_BLOCKSIZE, block, offset);
  543. }
  544. inline void getNodeSectorPosWithOffset(const v2s16 &p, v2s16 &block, v2s16 &offset)
  545. {
  546. getContainerPosWithOffset(p, MAP_BLOCKSIZE, block, offset);
  547. }
  548. /*
  549. Get a quick string to describe what a block actually contains
  550. */
  551. std::string analyze_block(MapBlock *block);
  552. #endif