TreeTemplate.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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)(zConfig->asObject()->zValue("minSize")->asNumber()->getNumber()
  95. + 0.5),
  96. (int)(zConfig->asObject()->zValue("maxSize")->asNumber()->getNumber()
  97. + 0.5));
  98. }
  99. Framework::JSON::Validator::JSONValidator* TreeTemplateFactory::getValidator()
  100. {
  101. Framework::RCArray<Framework::Text> blockTypeNames;
  102. for (int i = 0; i < StaticRegistry<BlockType>::INSTANCE.getCount(); i++)
  103. {
  104. if (StaticRegistry<BlockType>::INSTANCE.zElement(i))
  105. {
  106. blockTypeNames.add(new Framework::Text(
  107. StaticRegistry<BlockType>::INSTANCE.zElement(i)->getName()));
  108. }
  109. }
  110. return Framework::JSON::Validator::JSONValidator::buildForObject()
  111. ->withRequiredString("type")
  112. ->withExactMatch("Tree")
  113. ->finishString()
  114. ->withRequiredString("wood")
  115. ->whichIsOneOf(blockTypeNames)
  116. ->finishString()
  117. ->withRequiredString("leaves")
  118. ->whichIsOneOf(blockTypeNames)
  119. ->finishString()
  120. ->withRequiredNumber("minSize")
  121. ->whichIsGreaterThen(0)
  122. ->finishNumber()
  123. ->withRequiredNumber("maxSize")
  124. ->whichIsGreaterThen(0)
  125. ->finishNumber()
  126. ->withRequiredNumber("propability")
  127. ->whichIsGreaterThen(0)
  128. ->finishNumber()
  129. ->finishObject();
  130. }