TreeTemplate.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include "TreeTemplate.h"
  2. #include "MultiblockTree.h"
  3. #include "Game.h"
  4. TreeTemplate::TreeTemplate(float propability, const BlockType* zWoodType, const BlockType* zLeaveType, int minHeight, int maxHeight)
  5. : GenerationTemplate(propability, 0, 1, Framework::Vec3<int>(-2, -2, 0), Framework::Vec3<int>(5, 5, maxHeight)),
  6. zWoodType(zWoodType),
  7. zLeaveType(zLeaveType),
  8. minHeight(minHeight),
  9. maxHeight(maxHeight)
  10. {}
  11. GeneratedStructure* TreeTemplate::generateAt(Framework::Vec3<int> location, Noise* zNoise, int dimensionId)
  12. {
  13. double noise = zNoise->getNoise((double)location.x + 40, (double)location.y + 70, (double)location.z - 20);
  14. int height = (int)(minHeight + (noise * (maxHeight - minHeight)));
  15. Dimension* zDim = Game::INSTANCE->zDimension(dimensionId);
  16. MultiblockStructure* treeStructure = zDim->zStructureByPosition(location);
  17. if (!treeStructure)
  18. {
  19. treeStructure = new MultiblockTree(dimensionId, zDim->getNextStructureId(), location);
  20. zDim->addStructure(treeStructure);
  21. }
  22. Framework::Vec3<int> minAffected = Framework::Vec3<int>(-2, -2, 0) + location;
  23. GeneratedStructure* generated = new GeneratedStructure(dynamic_cast<GenerationTemplate*>(getThis()), location, Framework::Vec3<int>(5, 5, height), minAffected);
  24. for (int x = 1; x < 4; x++)
  25. {
  26. for (int y = 1; y < 4; y++)
  27. {
  28. generated->setBlockAt(zLeaveType->getId(), Framework::Vec3<int>(x, y, height - 1));
  29. generated->setBlockAt(zLeaveType->getId(), Framework::Vec3<int>(x, y, height - 5));
  30. }
  31. }
  32. for (int z = height - 2; z >= height - 4; z--)
  33. {
  34. for (int x = 1; x < 4; x++)
  35. {
  36. generated->setBlockAt(zLeaveType->getId(), Framework::Vec3<int>(x, 0, z));
  37. generated->setBlockAt(zLeaveType->getId(), Framework::Vec3<int>(0, x, z));
  38. generated->setBlockAt(zLeaveType->getId(), Framework::Vec3<int>(x, 4, z));
  39. generated->setBlockAt(zLeaveType->getId(), Framework::Vec3<int>(4, x, z));
  40. for (int y = 1; y < 4; y++)
  41. generated->setBlockAt(zLeaveType->getId(), Framework::Vec3<int>(x, y, z));
  42. }
  43. }
  44. for (int i = 0; i < height - 1; i++)
  45. generated->setBlockAt(zWoodType->getId(), Framework::Vec3<int>(2, 2, i));
  46. generated->addAllBlocksToStructure(dynamic_cast<MultiblockStructure*>(treeStructure->getThis()));
  47. return generated;
  48. }
  49. const BlockType* TreeTemplate::getWoodType() const
  50. {
  51. return zWoodType;
  52. }
  53. const BlockType* TreeTemplate::getLeavesType() const
  54. {
  55. return zLeaveType;
  56. }