MultiblockStructure.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. StaticRegistry<MultiblockStructureType>::INSTANCE.registerT(this, id);
  117. }
  118. MultiblockStructureType::~MultiblockStructureType() {}
  119. void MultiblockStructureType::loadSuperStructure(
  120. MultiblockStructure* zStructure, Framework::StreamReader* zReader) const
  121. {
  122. zStructure->affectedChunks.leeren();
  123. zStructure->memberBlockPositions.leeren();
  124. int blockCount;
  125. zReader->lese((char*)&blockCount, 4);
  126. for (int i = 0; i < blockCount; i++)
  127. {
  128. Framework::Vec3<int> pos;
  129. zReader->lese((char*)&pos.x, 4);
  130. zReader->lese((char*)&pos.y, 4);
  131. zReader->lese((char*)&pos.z, 4);
  132. zStructure->addMemberPosition(pos);
  133. }
  134. }
  135. void MultiblockStructureType::saveSuperStructure(
  136. MultiblockStructure* zStructure, Framework::StreamWriter* zWriter) const
  137. {
  138. int blockCount = zStructure->memberBlockPositions.getEintragAnzahl();
  139. zWriter->schreibe((char*)&blockCount, 4);
  140. for (Framework::Vec3<int> pos : zStructure->memberBlockPositions)
  141. {
  142. zWriter->schreibe((char*)&pos.x, 4);
  143. zWriter->schreibe((char*)&pos.y, 4);
  144. zWriter->schreibe((char*)&pos.z, 4);
  145. }
  146. }
  147. MultiblockStructure* MultiblockStructureType::loadStructure(int dimensionId,
  148. __int64 structureId,
  149. Framework::Vec3<int> uniquePosition,
  150. Framework::StreamReader* zReader) const
  151. {
  152. MultiblockStructure* str
  153. = createStructure(dimensionId, structureId, uniquePosition);
  154. loadSuperStructure(str, zReader);
  155. return str;
  156. }
  157. void MultiblockStructureType::saveStructure(
  158. MultiblockStructure* zStructure, Framework::StreamWriter* zWriter) const
  159. {
  160. saveSuperStructure(zStructure, zWriter);
  161. }
  162. int MultiblockStructureType::getId() const
  163. {
  164. return id;
  165. }