TreeTemplate.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. : SubTypeFactory<GeneratorTemplate, TreeTemplate>()
  82. {}
  83. TreeTemplate* TreeTemplateFactory::fromJson(
  84. Framework::JSON::JSONObject* zConfig) const
  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(
  95. zConfig->asObject()->zValue("minSize")->asNumber()->getNumber())),
  96. (int)(round(
  97. zConfig->asObject()->zValue("maxSize")->asNumber()->getNumber())));
  98. }
  99. Framework::JSON::JSONObject* TreeTemplateFactory::toJson(
  100. TreeTemplate* zObject) const
  101. {
  102. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  103. result->addValue(
  104. "propability", new Framework::JSON::JSONNumber((double)zObject->getPropability()));
  105. result->addValue("wood", new Framework::JSON::JSONString(BlockType::getTypeName(zObject->woodBlockTypeId)));
  106. result->addValue("leaves",
  107. new Framework::JSON::JSONString(
  108. BlockType::getTypeName(zObject->leaveBlockType)));
  109. result->addValue(
  110. "minSize", new Framework::JSON::JSONNumber((double)zObject->minHeight));
  111. result->addValue(
  112. "maxSize", new Framework::JSON::JSONNumber((double)zObject->maxHeight));
  113. return result;
  114. }
  115. Framework::JSON::Validator::JSONValidator* TreeTemplateFactory::getValidator(
  116. Framework::JSON::Validator::ObjectValidationBuilder<
  117. Framework::JSON::Validator::JSONValidator>* builder) const
  118. {
  119. Framework::RCArray<Framework::Text> blockTypeNames;
  120. for (int i = 0; i < StaticRegistry<BlockType>::INSTANCE.getCount(); i++)
  121. {
  122. if (StaticRegistry<BlockType>::INSTANCE.zElement(i))
  123. {
  124. blockTypeNames.add(new Framework::Text(
  125. StaticRegistry<BlockType>::INSTANCE.zElement(i)->getName()));
  126. }
  127. }
  128. return builder
  129. ->withRequiredString("type")
  130. ->withExactMatch("Tree")
  131. ->finishString()
  132. ->withRequiredString("wood")
  133. ->whichIsOneOf(blockTypeNames)
  134. ->finishString()
  135. ->withRequiredString("leaves")
  136. ->whichIsOneOf(blockTypeNames)
  137. ->finishString()
  138. ->withRequiredNumber("minSize")
  139. ->whichIsGreaterThen(0)
  140. ->finishNumber()
  141. ->withRequiredNumber("maxSize")
  142. ->whichIsGreaterThen(0)
  143. ->finishNumber()
  144. ->withRequiredNumber("propability")
  145. ->whichIsGreaterThen(0)
  146. ->finishNumber()
  147. ->finishObject();
  148. }
  149. Framework::Text TreeTemplateFactory::getTypeToken() const
  150. {
  151. return "Tree";
  152. }