MultiblockStructure.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "MultiblockStructure.h"
  2. #include "Block.h"
  3. #include "Game.h"
  4. using namespace Framework;
  5. MultiblockStructure::MultiblockStructure(int dimensionId,
  6. __int64 structureId,
  7. Framework::Vec3<int> uniquePosition,
  8. int structureTypeId)
  9. : ReferenceCounter(),
  10. uniquePosition(uniquePosition),
  11. dimensionId(dimensionId),
  12. structureId(structureId),
  13. structureTypeId(structureTypeId),
  14. isLoading(1)
  15. {}
  16. MultiblockStructure::~MultiblockStructure() {}
  17. void MultiblockStructure::onBlockLoaded(Block* block)
  18. {
  19. if (isBlockMember(block))
  20. {
  21. loadedMembers.add(block);
  22. isLoading = 0;
  23. }
  24. else
  25. block->release();
  26. }
  27. void MultiblockStructure::onBlockUnloaded(Block* zBlock)
  28. {
  29. for (auto i = loadedMembers.begin(); i; ++i)
  30. {
  31. if ((Block*)i == zBlock)
  32. {
  33. i.remove();
  34. return;
  35. }
  36. }
  37. }
  38. void MultiblockStructure::addMemberPosition(Framework::Vec3<int> blockPos)
  39. {
  40. memberBlockPositions.add(blockPos);
  41. Punkt center = Game::INSTANCE->getChunkCenter(blockPos.x, blockPos.y);
  42. for (const Punkt& p : affectedChunks)
  43. {
  44. if (p == center) return;
  45. }
  46. affectedChunks.add(center);
  47. }
  48. void MultiblockStructure::onBlockRemoved(Block* zBlock)
  49. {
  50. for (auto i = memberBlockPositions.begin(); i; ++i)
  51. {
  52. if (i.val() == zBlock->getPos())
  53. {
  54. Punkt center = Game::INSTANCE->getChunkCenter(i.val().x, i.val().y);
  55. i.remove();
  56. // check if other blocks in the same chunk exists
  57. for (Vec3<int> pos : memberBlockPositions)
  58. {
  59. if (center == Game::INSTANCE->getChunkCenter(pos.x, pos.y))
  60. return;
  61. }
  62. // remove chunk from affected chunks if no other block is in this
  63. // chunk
  64. for (auto p = affectedChunks.begin(); p; ++p)
  65. {
  66. if (p.val() == center) p.remove();
  67. }
  68. return;
  69. }
  70. }
  71. }
  72. bool MultiblockStructure::isEmpty() const
  73. {
  74. return memberBlockPositions.getEintragAnzahl() == 0 && !isLoading;
  75. }
  76. bool MultiblockStructure::isFullyLoaded() const
  77. {
  78. for (Punkt p : affectedChunks)
  79. {
  80. if (!Game::INSTANCE->isChunkLoaded(p.x, p.y, dimensionId)) return 0;
  81. }
  82. return 1;
  83. }
  84. bool MultiblockStructure::isFullyUnloaded() const
  85. {
  86. if (isLoading) return 0;
  87. for (Punkt p : affectedChunks)
  88. {
  89. if (Game::INSTANCE->isChunkLoaded(p.x, p.y, dimensionId)) return 0;
  90. }
  91. return 1;
  92. }
  93. bool MultiblockStructure::isBlockMember(Block* zBlock) const
  94. {
  95. for (const Vec3<int>& pos : memberBlockPositions)
  96. {
  97. if (pos == zBlock->getPos()) return 1;
  98. }
  99. return 0;
  100. }
  101. __int64 MultiblockStructure::getStructureId() const
  102. {
  103. return structureId;
  104. }
  105. Framework::Vec3<int> MultiblockStructure::getUniquePosition() const
  106. {
  107. return uniquePosition;
  108. }
  109. int MultiblockStructure::getStructureTypeId() const
  110. {
  111. return structureTypeId;
  112. }
  113. MultiblockStructureType::MultiblockStructureType(int id)
  114. : ReferenceCounter(),
  115. id(id)
  116. {}
  117. MultiblockStructureType::~MultiblockStructureType() {}
  118. void MultiblockStructureType::loadSuperStructure(
  119. MultiblockStructure* zStructure, Framework::StreamReader* zReader) const
  120. {
  121. zStructure->affectedChunks.leeren();
  122. zStructure->memberBlockPositions.leeren();
  123. int blockCount;
  124. zReader->lese((char*)&blockCount, 4);
  125. for (int i = 0; i < blockCount; i++)
  126. {
  127. Framework::Vec3<int> pos;
  128. zReader->lese((char*)&pos.x, 4);
  129. zReader->lese((char*)&pos.y, 4);
  130. zReader->lese((char*)&pos.z, 4);
  131. zStructure->addMemberPosition(pos);
  132. }
  133. }
  134. void MultiblockStructureType::saveSuperStructure(
  135. MultiblockStructure* zStructure, Framework::StreamWriter* zWriter) const
  136. {
  137. int blockCount = zStructure->memberBlockPositions.getEintragAnzahl();
  138. zWriter->schreibe((char*)&blockCount, 4);
  139. for (Framework::Vec3<int> pos : zStructure->memberBlockPositions)
  140. {
  141. zWriter->schreibe((char*)&pos.x, 4);
  142. zWriter->schreibe((char*)&pos.y, 4);
  143. zWriter->schreibe((char*)&pos.z, 4);
  144. }
  145. }
  146. MultiblockStructure* MultiblockStructureType::loadStructure(int dimensionId,
  147. __int64 structureId,
  148. Framework::Vec3<int> uniquePosition,
  149. Framework::StreamReader* zReader) const
  150. {
  151. MultiblockStructure* str
  152. = createStructure(dimensionId, structureId, uniquePosition);
  153. loadSuperStructure(str, zReader);
  154. return str;
  155. }
  156. void MultiblockStructureType::saveStructure(
  157. MultiblockStructure* zStructure, Framework::StreamWriter* zWriter) const
  158. {
  159. saveSuperStructure(zStructure, zWriter);
  160. }
  161. int MultiblockStructureType::getId() const
  162. {
  163. return id;
  164. }