GeneratedStructure.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "GeneratedStructure.h"
  2. #include "GeneratorTemplate.h"
  3. #include "MultiblockStructure.h"
  4. #include "NoBlock.h"
  5. #include "Game.h"
  6. using namespace Framework;
  7. GeneratedStructure::GeneratedStructure(GeneratorTemplate* t,
  8. Framework::Vec3<int> originPos,
  9. Framework::Vec3<int> size,
  10. Framework::Vec3<int> minAffectedPos)
  11. : ReferenceCounter(),
  12. size(size),
  13. minAffectedPos(minAffectedPos),
  14. originPos(originPos),
  15. t(t),
  16. multiblock(0)
  17. {
  18. blockIds = new int[size.x * size.y * size.z];
  19. blocks = new Block*[size.x * size.y * size.z];
  20. memset(blockIds, 0, sizeof(int) * size.x * size.y * size.z);
  21. memset(blocks, 0, sizeof(Block*) * size.x * size.y * size.z);
  22. }
  23. GeneratedStructure::~GeneratedStructure()
  24. {
  25. for (int i = 0; i < size.x * size.y * size.z; i++)
  26. {
  27. if (blocks[i]) blocks[i]->release();
  28. }
  29. delete[] blockIds;
  30. delete[] blocks;
  31. t->release();
  32. if (multiblock) multiblock->release();
  33. }
  34. void GeneratedStructure::setBlockAt(
  35. Framework::Either<Block*, int> block, Framework::Vec3<int> localPos)
  36. {
  37. assert(localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0
  38. && localPos.x < size.x && localPos.y < size.y
  39. && localPos.z < size.z);
  40. int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
  41. if (block.isA())
  42. blocks[index] = block;
  43. else
  44. blockIds[index] = block;
  45. }
  46. bool GeneratedStructure::isBlockAffected(Framework::Vec3<int> location) const
  47. {
  48. Framework::Vec3<int> localPos = location - minAffectedPos;
  49. if (localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0
  50. && localPos.x < size.x && localPos.y < size.y && localPos.z < size.z)
  51. {
  52. int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
  53. return blocks[index] || blockIds[index];
  54. }
  55. return 0;
  56. }
  57. Framework::Either<Block*, int> GeneratedStructure::generateBlockAt(
  58. Framework::Vec3<int> location, int dimensionId) const
  59. {
  60. Framework::Vec3<int> localPos = location - minAffectedPos;
  61. if (localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0
  62. && localPos.x < size.x && localPos.y < size.y && localPos.z < size.z)
  63. {
  64. int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
  65. if (multiblock)
  66. {
  67. multiblock->addMemberPosition(minAffectedPos + localPos);
  68. if (blocks[index])
  69. {
  70. blocks[index]->addToStructure(
  71. dynamic_cast<MultiblockStructure*>(multiblock->getThis()));
  72. return blocks[index];
  73. }
  74. if (blockIds[index] && blockIds[index] != BlockTypeEnum::AIR)
  75. {
  76. Block* result
  77. = Game::INSTANCE->zBlockType(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. }