GeneratedStructure.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "GeneratedStructure.h"
  2. #include "GenerationTemplate.h"
  3. GeneratedStructure::GeneratedStructure( GenerationTemplate* t, Framework::Vec3<int> originPos, Framework::Vec3<int> size, Framework::Vec3<int> minAffectedPos )
  4. : ReferenceCounter(),
  5. size( size ),
  6. minAffectedPos( minAffectedPos ),
  7. originPos( originPos ),
  8. t( t )
  9. {
  10. blockIds = new int[ size.x * size.y * size.z ];
  11. blocks = new Block * [ size.x * size.y * size.z ];
  12. memset( blockIds, 0, sizeof( int ) * size.x * size.y * size.z );
  13. memset( blocks, 0, sizeof( Block* ) * size.x * size.y * size.z );
  14. }
  15. GeneratedStructure::~GeneratedStructure()
  16. {
  17. for( int i = 0; i < size.x * size.y * size.z; i++ )
  18. {
  19. if( blocks[ i ] )
  20. blocks[ i ]->release();
  21. }
  22. delete[] blockIds;
  23. delete[] blocks;
  24. t->release();
  25. }
  26. void GeneratedStructure::setBlockAt( Framework::Either<Block*, int> block, Framework::Vec3<int> localPos )
  27. {
  28. assert( localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0 && localPos.x < size.x&& localPos.y < size.y&& localPos.z < size.z );
  29. int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
  30. if( block.isA() )
  31. blocks[ index ] = block;
  32. else
  33. blockIds[ index ] = block;
  34. }
  35. bool GeneratedStructure::isBlockAffected( Framework::Vec3<int> location ) const
  36. {
  37. Framework::Vec3<int> localPos = location - minAffectedPos;
  38. if( localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0 && localPos.x < size.x && localPos.y < size.y && localPos.z < size.z )
  39. {
  40. int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
  41. return blocks[ index ] || blockIds[ index ];
  42. }
  43. return 0;
  44. }
  45. Framework::Either<Block*, int> GeneratedStructure::generateBlockAt( Framework::Vec3<int> location ) const
  46. {
  47. Framework::Vec3<int> localPos = location - minAffectedPos;
  48. if( localPos.x >= 0 && localPos.y >= 0 && localPos.z >= 0 && localPos.x < size.x && localPos.y < size.y && localPos.z < size.z )
  49. {
  50. int index = ((localPos.x * size.y) + localPos.y) * size.z + localPos.z;
  51. if( blocks[ index ] )
  52. return blocks[ index ];
  53. return blockIds[ index ];
  54. }
  55. return 0;
  56. }
  57. Framework::Vec3<int> GeneratedStructure::getOriginPos() const
  58. {
  59. return originPos;
  60. }
  61. GenerationTemplate* GeneratedStructure::zTemplate() const
  62. {
  63. return t;
  64. }