GeneratedStructure.cpp 3.0 KB

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