MultiblockStructure.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "MultiblockStructure.h"
  2. #include "Game.h"
  3. using namespace Framework;
  4. MultiblockStructure::MultiblockStructure(int dimensionId, __int64 structureId, Framework::Vec3<int> uniquePosition)
  5. : ReferenceCounter(),
  6. uniquePosition(uniquePosition),
  7. dimensionId(dimensionId),
  8. structureId(structureId)
  9. {}
  10. MultiblockStructure::~MultiblockStructure()
  11. {}
  12. void MultiblockStructure::onBlockLoaded(Block* block)
  13. {
  14. if (isBlockMember(block))
  15. loadedMembers.add(block);
  16. else
  17. block->release();
  18. }
  19. void MultiblockStructure::onBlockUnloaded(Block* zBlock)
  20. {
  21. for (auto i = loadedMembers.begin(); i; ++i)
  22. {
  23. if ((Block*)i == zBlock)
  24. {
  25. i.remove();
  26. return;
  27. }
  28. }
  29. }
  30. void MultiblockStructure::addMemberPosition(Framework::Vec3<int > blockPos)
  31. {
  32. memberBlockPositions.add(blockPos);
  33. Punkt center = Game::INSTANCE->getChunkCenter(blockPos.x, blockPos.y);
  34. for (const Punkt& p : affectedChunks)
  35. {
  36. if (p == center)
  37. return;
  38. }
  39. affectedChunks.add(center);
  40. }
  41. void MultiblockStructure::onBlockRemoved(Block* zBlock)
  42. {
  43. for (auto i = memberBlockPositions.begin(); i; ++i)
  44. {
  45. if (i.val() == zBlock->getPos())
  46. {
  47. Punkt center = Game::INSTANCE->getChunkCenter(i.val().x, i.val().y);
  48. i.remove();
  49. // check if other blocks in the same chunk exists
  50. for (Vec3<int> pos : memberBlockPositions)
  51. {
  52. if (center == Game::INSTANCE->getChunkCenter(pos.x, pos.y))
  53. return;
  54. }
  55. // remove chunk from affected chunks if no other block is in this chunk
  56. for (auto p = affectedChunks.begin(); p; ++p)
  57. {
  58. if (p.val() == center)
  59. p.remove();
  60. }
  61. return;
  62. }
  63. }
  64. }
  65. bool MultiblockStructure::isEmpty() const
  66. {
  67. return memberBlockPositions.getEintragAnzahl() > 0;
  68. }
  69. bool MultiblockStructure::isFullyLoaded() const
  70. {
  71. for (Punkt p : affectedChunks)
  72. {
  73. if (!Game::INSTANCE->isChunkLoaded(p.x, p.y, dimensionId))
  74. return 0;
  75. }
  76. return 1;
  77. }
  78. bool MultiblockStructure::isFullyUnloaded() const
  79. {
  80. for (Punkt p : affectedChunks)
  81. {
  82. if (Game::INSTANCE->isChunkLoaded(p.x, p.y, dimensionId))
  83. return 0;
  84. }
  85. return 1;
  86. }
  87. bool MultiblockStructure::isBlockMember(Block* zBlock) const
  88. {
  89. for (const Vec3<int>& pos : memberBlockPositions)
  90. {
  91. if (pos == zBlock->getPos())
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. __int64 MultiblockStructure::getStructureId() const
  97. {
  98. return structureId;
  99. }
  100. Framework::Vec3<int> MultiblockStructure::getUniquePosition() const
  101. {
  102. return uniquePosition;
  103. }