MultiblockStructure.cpp 4.6 KB

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