MultiblockStructure.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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(
  49. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, Block* zBlock)
  50. {
  51. for (auto i = memberBlockPositions.begin(); i; ++i)
  52. {
  53. if (i.val() == zBlock->getPos())
  54. {
  55. Punkt center = Game::INSTANCE->getChunkCenter(i.val().x, i.val().y);
  56. i.remove();
  57. // check if other blocks in the same chunk exists
  58. for (Vec3<int> pos : memberBlockPositions)
  59. {
  60. if (center == Game::INSTANCE->getChunkCenter(pos.x, pos.y))
  61. return;
  62. }
  63. // remove chunk from affected chunks if no other block is in this
  64. // chunk
  65. for (auto p = affectedChunks.begin(); p; ++p)
  66. {
  67. if (p.val() == center) p.remove();
  68. }
  69. return;
  70. }
  71. }
  72. }
  73. bool MultiblockStructure::isEmpty() const
  74. {
  75. return memberBlockPositions.getEintragAnzahl() == 0 && !isLoading;
  76. }
  77. bool MultiblockStructure::isFullyLoaded() const
  78. {
  79. for (Punkt p : affectedChunks)
  80. {
  81. if (!Game::INSTANCE->isChunkLoaded(p.x, p.y, dimensionId)) return 0;
  82. }
  83. return 1;
  84. }
  85. bool MultiblockStructure::isFullyUnloaded() const
  86. {
  87. if (isLoading) return 0;
  88. for (Punkt p : affectedChunks)
  89. {
  90. if (Game::INSTANCE->isChunkLoaded(p.x, p.y, dimensionId)) return 0;
  91. }
  92. return 1;
  93. }
  94. bool MultiblockStructure::isBlockMember(Block* zBlock) const
  95. {
  96. for (const Vec3<int>& pos : memberBlockPositions)
  97. {
  98. if (pos == zBlock->getPos()) return 1;
  99. }
  100. return 0;
  101. }
  102. __int64 MultiblockStructure::getStructureId() const
  103. {
  104. return structureId;
  105. }
  106. Framework::Vec3<int> MultiblockStructure::getUniquePosition() const
  107. {
  108. return uniquePosition;
  109. }
  110. int MultiblockStructure::getStructureTypeId() const
  111. {
  112. return structureTypeId;
  113. }
  114. MultiblockStructureType::MultiblockStructureType(int id)
  115. : ReferenceCounter(),
  116. id(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. }