TreeTemplate.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. : GeneratorTemplate(propability,
  10. Framework::Vec3<int>(-2, -2, 0),
  11. Framework::Vec3<int>(5, 5, maxHeight)),
  12. woodBlockTypeId(woodBlockTypeId),
  13. leaveBlockType(leaveBlockType),
  14. minHeight(minHeight),
  15. maxHeight(maxHeight)
  16. {}
  17. GeneratedStructure* TreeTemplate::generateAt(
  18. Framework::Vec3<int> location, Noise* zNoise, int dimensionId)
  19. {
  20. double noise = zNoise->getNoise((double)location.x + 40,
  21. (double)location.y + 70,
  22. (double)location.z - 20);
  23. int height = (int)(minHeight + (noise * (maxHeight - minHeight)));
  24. Dimension* zDim = Game::INSTANCE->zDimension(dimensionId);
  25. MultiblockStructure* treeStructure = zDim->zStructureByPosition(location);
  26. if (!treeStructure)
  27. {
  28. treeStructure = new MultiblockTree(
  29. dimensionId, zDim->getNextStructureId(), location);
  30. zDim->addStructure(treeStructure);
  31. }
  32. Framework::Vec3<int> minAffected
  33. = Framework::Vec3<int>(-2, -2, 0) + location;
  34. GeneratedStructure* generated
  35. = new GeneratedStructure(dynamic_cast<GeneratorTemplate*>(getThis()),
  36. location,
  37. Framework::Vec3<int>(5, 5, height),
  38. minAffected);
  39. for (int x = 1; x < 4; x++)
  40. {
  41. for (int y = 1; y < 4; y++)
  42. {
  43. generated->setBlockAt(
  44. leaveBlockType, Framework::Vec3<int>(x, y, height - 1));
  45. generated->setBlockAt(
  46. leaveBlockType, Framework::Vec3<int>(x, y, height - 5));
  47. }
  48. }
  49. for (int z = height - 2; z >= height - 4; z--)
  50. {
  51. for (int x = 1; x < 4; x++)
  52. {
  53. generated->setBlockAt(
  54. leaveBlockType, Framework::Vec3<int>(x, 0, z));
  55. generated->setBlockAt(
  56. leaveBlockType, Framework::Vec3<int>(0, x, z));
  57. generated->setBlockAt(
  58. leaveBlockType, Framework::Vec3<int>(x, 4, z));
  59. generated->setBlockAt(
  60. leaveBlockType, Framework::Vec3<int>(4, x, z));
  61. for (int y = 1; y < 4; y++)
  62. generated->setBlockAt(
  63. leaveBlockType, Framework::Vec3<int>(x, y, z));
  64. }
  65. }
  66. for (int i = 0; i < height - 1; i++)
  67. generated->setBlockAt(woodBlockTypeId, Framework::Vec3<int>(2, 2, i));
  68. generated->addAllBlocksToStructure(
  69. dynamic_cast<MultiblockStructure*>(treeStructure->getThis()));
  70. return generated;
  71. }
  72. const BlockType* TreeTemplate::getWoodType() const
  73. {
  74. return StaticRegistry<BlockType>::INSTANCE.zElement(woodBlockTypeId);
  75. }
  76. const BlockType* TreeTemplate::getLeavesType() const
  77. {
  78. return StaticRegistry<BlockType>::INSTANCE.zElement(leaveBlockType);
  79. }
  80. TreeTemplateFactory::TreeTemplateFactory()
  81. : GeneratorTemplateFactory()
  82. {}
  83. GeneratorTemplate* TreeTemplateFactory::createTemplate(
  84. Framework::JSON::JSONValue* zConfig)
  85. {
  86. return new TreeTemplate((float)zConfig->asObject()
  87. ->zValue("propability")
  88. ->asNumber()
  89. ->getNumber(),
  90. BlockType::getTypeId(
  91. zConfig->asObject()->zValue("wood")->asString()->getString()),
  92. BlockType::getTypeId(
  93. zConfig->asObject()->zValue("leaves")->asString()->getString()),
  94. (int)(round(zConfig->asObject()
  95. ->zValue("minSize")
  96. ->asNumber()
  97. ->getNumber())),
  98. (int)(round(zConfig->asObject()->zValue("maxSize")->asNumber()->getNumber())));
  99. }
  100. Framework::JSON::Validator::JSONValidator* TreeTemplateFactory::getValidator()
  101. {
  102. Framework::RCArray<Framework::Text> blockTypeNames;
  103. for (int i = 0; i < StaticRegistry<BlockType>::INSTANCE.getCount(); i++)
  104. {
  105. if (StaticRegistry<BlockType>::INSTANCE.zElement(i))
  106. {
  107. blockTypeNames.add(new Framework::Text(
  108. StaticRegistry<BlockType>::INSTANCE.zElement(i)->getName()));
  109. }
  110. }
  111. return Framework::JSON::Validator::JSONValidator::buildForObject()
  112. ->withRequiredString("type")
  113. ->withExactMatch("Tree")
  114. ->finishString()
  115. ->withRequiredString("wood")
  116. ->whichIsOneOf(blockTypeNames)
  117. ->finishString()
  118. ->withRequiredString("leaves")
  119. ->whichIsOneOf(blockTypeNames)
  120. ->finishString()
  121. ->withRequiredNumber("minSize")
  122. ->whichIsGreaterThen(0)
  123. ->finishNumber()
  124. ->withRequiredNumber("maxSize")
  125. ->whichIsGreaterThen(0)
  126. ->finishNumber()
  127. ->withRequiredNumber("propability")
  128. ->whichIsGreaterThen(0)
  129. ->finishNumber()
  130. ->finishObject();
  131. }