GeneratedStructure.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "GeneratedStructure.h"
  2. #include "GenerationTemplate.h"
  3. #include "MultiblockStructure.h"
  4. #include "NoBlock.h"
  5. using namespace Framework;
  6. GeneratedStructure::GeneratedStructure(GenerationTemplate* t,
  7. Framework::Vec3<int> originPos,
  8. Framework::Vec3<int> size,
  9. Framework::Vec3<int> minAffectedPos)
  10. : ReferenceCounter(),
  11. size(size),
  12. minAffectedPos(minAffectedPos),
  13. originPos(originPos),
  14. t(t),
  15. multiblock(0)
  16. {
  17. blockIds = new int[size.x * size.y * size.z];
  18. blocks = new Block*[size.x * size.y * size.z];
  19. memset(blockIds, 0, sizeof(int) * size.x * size.y * size.z);
  20. memset(blocks, 0, sizeof(Block*) * size.x * size.y * size.z);
  21. }
  22. GeneratedStructure::~GeneratedStructure()
  23. {
  24. for (int i = 0; i < size.x * size.y * size.z; i++)
  25. {
  26. if (blocks[i]) blocks[i]->release();
  27. }
  28. delete[] blockIds;
  29. delete[] blocks;
  30. t->release();
  31. if (multiblock) multiblock->release();
  32. }
  33. void GeneratedStructure::setBlockAt(
  34. Framework::Either<Block*, int> block, Framework::Vec3<int> localPos)
  35. {
  36. assert(localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0
  37. && localPos.x < size.x && localPos.y < size.y
  38. && localPos.z < size.z);
  39. int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
  40. if (block.isA())
  41. blocks[index] = block;
  42. else
  43. blockIds[index] = block;
  44. }
  45. bool GeneratedStructure::isBlockAffected(Framework::Vec3<int> location) const
  46. {
  47. Framework::Vec3<int> localPos = location - minAffectedPos;
  48. if (localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0
  49. && localPos.x < size.x && localPos.y < size.y && localPos.z < size.z)
  50. {
  51. int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
  52. return blocks[index] || blockIds[index];
  53. }
  54. return 0;
  55. }
  56. Framework::Either<Block*, int> GeneratedStructure::generateBlockAt(
  57. Framework::Vec3<int> location) const
  58. {
  59. Framework::Vec3<int> localPos = location - minAffectedPos;
  60. if (localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0
  61. && localPos.x < size.x && localPos.y < size.y && localPos.z < size.z)
  62. {
  63. int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
  64. if (multiblock)
  65. {
  66. multiblock->addMemberPosition(minAffectedPos + localPos);
  67. if (blocks[index])
  68. {
  69. blocks[index]->addToStructure(
  70. dynamic_cast<MultiblockStructure*>(multiblock->getThis()));
  71. return blocks[index];
  72. }
  73. if (blockIds[index] && blockIds[index] != BlockTypeEnum::AIR)
  74. {
  75. Block* result
  76. = StaticRegistry<BlockType>::INSTANCE
  77. .zElement(blockIds[index])
  78. ->createBlockAt(minAffectedPos + localPos, 0);
  79. if (result)
  80. {
  81. result->addToStructure(dynamic_cast<MultiblockStructure*>(
  82. multiblock->getThis()));
  83. return result;
  84. }
  85. }
  86. return blockIds[index];
  87. }
  88. else
  89. {
  90. if (blocks[index]) return blocks[index];
  91. return blockIds[index];
  92. }
  93. }
  94. return 0;
  95. }
  96. void GeneratedStructure::addAllBlocksToStructure(MultiblockStructure* structure)
  97. {
  98. if (multiblock) multiblock->release();
  99. multiblock = structure;
  100. }
  101. Framework::Vec3<int> GeneratedStructure::getOriginPos() const
  102. {
  103. return originPos;
  104. }
  105. GenerationTemplate* GeneratedStructure::zTemplate() const
  106. {
  107. return t;
  108. }