TreeTemplate.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "TreeTemplate.h"
  2. #include "Game.h"
  3. #include "MultiblockTree.h"
  4. TreeTemplate::TreeTemplate(float propability,
  5. int woodBlockTypeId,
  6. int leaveBlockType,
  7. int minHeight,
  8. int maxHeight)
  9. : GenerationTemplate(propability,
  10. 0,
  11. 1,
  12. Framework::Vec3<int>(-2, -2, 0),
  13. Framework::Vec3<int>(5, 5, maxHeight)),
  14. woodBlockTypeId(woodBlockTypeId),
  15. leaveBlockType(leaveBlockType),
  16. minHeight(minHeight),
  17. maxHeight(maxHeight)
  18. {}
  19. GeneratedStructure* TreeTemplate::generateAt(
  20. Framework::Vec3<int> location, Noise* zNoise, int dimensionId)
  21. {
  22. double noise = zNoise->getNoise((double)location.x + 40,
  23. (double)location.y + 70,
  24. (double)location.z - 20);
  25. int height = (int)(minHeight + (noise * (maxHeight - minHeight)));
  26. Dimension* zDim = Game::INSTANCE->zDimension(dimensionId);
  27. MultiblockStructure* treeStructure = zDim->zStructureByPosition(location);
  28. if (!treeStructure)
  29. {
  30. treeStructure = new MultiblockTree(
  31. dimensionId, zDim->getNextStructureId(), location);
  32. zDim->addStructure(treeStructure);
  33. }
  34. Framework::Vec3<int> minAffected
  35. = Framework::Vec3<int>(-2, -2, 0) + location;
  36. GeneratedStructure* generated
  37. = new GeneratedStructure(dynamic_cast<GenerationTemplate*>(getThis()),
  38. location,
  39. Framework::Vec3<int>(5, 5, height),
  40. minAffected);
  41. for (int x = 1; x < 4; x++)
  42. {
  43. for (int y = 1; y < 4; y++)
  44. {
  45. generated->setBlockAt(
  46. leaveBlockType, Framework::Vec3<int>(x, y, height - 1));
  47. generated->setBlockAt(
  48. leaveBlockType, Framework::Vec3<int>(x, y, height - 5));
  49. }
  50. }
  51. for (int z = height - 2; z >= height - 4; z--)
  52. {
  53. for (int x = 1; x < 4; x++)
  54. {
  55. generated->setBlockAt(
  56. leaveBlockType, Framework::Vec3<int>(x, 0, z));
  57. generated->setBlockAt(
  58. leaveBlockType, Framework::Vec3<int>(0, x, z));
  59. generated->setBlockAt(
  60. leaveBlockType, Framework::Vec3<int>(x, 4, z));
  61. generated->setBlockAt(
  62. leaveBlockType, Framework::Vec3<int>(4, x, z));
  63. for (int y = 1; y < 4; y++)
  64. generated->setBlockAt(
  65. leaveBlockType, Framework::Vec3<int>(x, y, z));
  66. }
  67. }
  68. for (int i = 0; i < height - 1; i++)
  69. generated->setBlockAt(woodBlockTypeId, Framework::Vec3<int>(2, 2, i));
  70. generated->addAllBlocksToStructure(
  71. dynamic_cast<MultiblockStructure*>(treeStructure->getThis()));
  72. return generated;
  73. }
  74. const BlockType* TreeTemplate::getWoodType() const
  75. {
  76. return StaticRegistry<BlockType>::INSTANCE.zElement(woodBlockTypeId);
  77. }
  78. const BlockType* TreeTemplate::getLeavesType() const
  79. {
  80. return StaticRegistry<BlockType>::INSTANCE.zElement(leaveBlockType);
  81. }