TreeTemplate.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 Game::INSTANCE->zBlockType(woodBlockTypeId);
  75. }
  76. const BlockType* TreeTemplate::getLeavesType() const
  77. {
  78. return Game::INSTANCE->zBlockType(leaveBlockType);
  79. }
  80. TreeTemplateFactory::TreeTemplateFactory()
  81. : SubTypeFactory()
  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("propability",
  104. new Framework::JSON::JSONNumber((double)zObject->getPropability()));
  105. result->addValue("wood",
  106. new Framework::JSON::JSONString(
  107. BlockType::getTypeName(zObject->woodBlockTypeId)));
  108. result->addValue("leaves",
  109. new Framework::JSON::JSONString(
  110. BlockType::getTypeName(zObject->leaveBlockType)));
  111. result->addValue(
  112. "minSize", new Framework::JSON::JSONNumber((double)zObject->minHeight));
  113. result->addValue(
  114. "maxSize", new Framework::JSON::JSONNumber((double)zObject->maxHeight));
  115. return result;
  116. }
  117. Framework::JSON::Validator::JSONValidator* TreeTemplateFactory::getValidator(
  118. Framework::JSON::Validator::ObjectValidationBuilder<
  119. Framework::JSON::Validator::JSONValidator>* builder) const
  120. {
  121. Framework::RCArray<Framework::Text> blockTypeNames;
  122. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  123. {
  124. if (Game::INSTANCE->zBlockType(i))
  125. {
  126. blockTypeNames.add(
  127. new Framework::Text(Game::INSTANCE->zBlockType(i)->getName()));
  128. }
  129. }
  130. return builder->withRequiredString("type")
  131. ->withExactMatch("Tree")
  132. ->finishString()
  133. ->withRequiredString("wood")
  134. ->whichIsOneOf(blockTypeNames)
  135. ->finishString()
  136. ->withRequiredString("leaves")
  137. ->whichIsOneOf(blockTypeNames)
  138. ->finishString()
  139. ->withRequiredNumber("minSize")
  140. ->whichIsGreaterThen(0)
  141. ->finishNumber()
  142. ->withRequiredNumber("maxSize")
  143. ->whichIsGreaterThen(0)
  144. ->finishNumber()
  145. ->withRequiredNumber("propability")
  146. ->whichIsGreaterThen(0)
  147. ->finishNumber()
  148. ->finishObject();
  149. }
  150. Framework::Text TreeTemplateFactory::getTypeToken() const
  151. {
  152. return "Tree";
  153. }