GeneratedStructure.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "GeneratedStructure.h"
  2. #include "GeneratorTemplate.h"
  3. #include "MultiblockStructure.h"
  4. #include "NoBlock.h"
  5. using namespace Framework;
  6. GeneratedStructure::GeneratedStructure(GeneratorTemplate* 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, int dimensionId) 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(
  79. minAffectedPos + localPos, dimensionId, 0);
  80. if (result)
  81. {
  82. result->addToStructure(dynamic_cast<MultiblockStructure*>(
  83. multiblock->getThis()));
  84. return result;
  85. }
  86. }
  87. return blockIds[index];
  88. }
  89. else
  90. {
  91. if (blocks[index]) return blocks[index];
  92. return blockIds[index];
  93. }
  94. }
  95. return 0;
  96. }
  97. void GeneratedStructure::addAllBlocksToStructure(MultiblockStructure* structure)
  98. {
  99. if (multiblock) multiblock->release();
  100. multiblock = structure;
  101. }
  102. Framework::Vec3<int> GeneratedStructure::getOriginPos() const
  103. {
  104. return originPos;
  105. }
  106. GeneratorTemplate* GeneratedStructure::zTemplate() const
  107. {
  108. return t;
  109. }