TreeTemplate.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "TreeTemplate.h"
  2. #include "Dimension.h"
  3. #include "Game.h"
  4. #include "MultiblockTree.h"
  5. TreeTemplate::TreeTemplate()
  6. : GeneratorTemplate(),
  7. woodBlockTypeId(0),
  8. leaveBlockType(0),
  9. minHeight(0),
  10. maxHeight(0)
  11. {
  12. setMinPosOffset(Framework::Vec3<int>(-2, -2, 0));
  13. }
  14. GeneratedStructure* TreeTemplate::generateAt(
  15. Framework::Vec3<int> location, Noise* zNoise, int dimensionId)
  16. {
  17. double noise = zNoise->getNoise((double)location.x + 40,
  18. (double)location.y + 70,
  19. (double)location.z - 20);
  20. int height = (int)(minHeight + (noise * (maxHeight - minHeight)));
  21. Dimension* zDim = Game::INSTANCE->zDimension(dimensionId);
  22. MultiblockStructure* treeStructure = zDim->zStructureByPosition(location);
  23. if (!treeStructure)
  24. {
  25. treeStructure = new MultiblockTree(
  26. dimensionId, zDim->getNextStructureId(), location);
  27. zDim->addStructure(treeStructure);
  28. }
  29. Framework::Vec3<int> minAffected
  30. = Framework::Vec3<int>(-2, -2, 0) + location;
  31. GeneratedStructure* generated
  32. = new GeneratedStructure(dynamic_cast<GeneratorTemplate*>(getThis()),
  33. location,
  34. Framework::Vec3<int>(5, 5, height),
  35. minAffected);
  36. for (int x = 1; x < 4; x++)
  37. {
  38. for (int y = 1; y < 4; y++)
  39. {
  40. generated->setBlockAt(
  41. leaveBlockType, Framework::Vec3<int>(x, y, height - 1));
  42. generated->setBlockAt(
  43. leaveBlockType, Framework::Vec3<int>(x, y, height - 5));
  44. }
  45. }
  46. for (int z = height - 2; z >= height - 4; z--)
  47. {
  48. for (int x = 1; x < 4; x++)
  49. {
  50. generated->setBlockAt(
  51. leaveBlockType, Framework::Vec3<int>(x, 0, z));
  52. generated->setBlockAt(
  53. leaveBlockType, Framework::Vec3<int>(0, x, z));
  54. generated->setBlockAt(
  55. leaveBlockType, Framework::Vec3<int>(x, 4, z));
  56. generated->setBlockAt(
  57. leaveBlockType, Framework::Vec3<int>(4, x, z));
  58. for (int y = 1; y < 4; y++)
  59. generated->setBlockAt(
  60. leaveBlockType, Framework::Vec3<int>(x, y, z));
  61. }
  62. }
  63. for (int i = 0; i < height - 1; i++)
  64. generated->setBlockAt(woodBlockTypeId, Framework::Vec3<int>(2, 2, i));
  65. generated->addAllBlocksToStructure(
  66. dynamic_cast<MultiblockStructure*>(treeStructure->getThis()));
  67. return generated;
  68. }
  69. void TreeTemplate::setWoodTypeId(int id)
  70. {
  71. woodBlockTypeId = id;
  72. }
  73. void TreeTemplate::setLeavesTypeId(int id)
  74. {
  75. leaveBlockType = id;
  76. }
  77. const BlockType* TreeTemplate::zWoodType() const
  78. {
  79. return Game::INSTANCE->zBlockType(woodBlockTypeId);
  80. }
  81. const BlockType* TreeTemplate::zLeavesType() const
  82. {
  83. return Game::INSTANCE->zBlockType(leaveBlockType);
  84. }
  85. void TreeTemplate::setMinHeight(int height)
  86. {
  87. minHeight = height;
  88. }
  89. void TreeTemplate::setMaxHeight(int height)
  90. {
  91. maxHeight = height;
  92. setMaxSize(Framework::Vec3<int>(5, 5, maxHeight));
  93. }
  94. int TreeTemplate::getMinHeight() const
  95. {
  96. return minHeight;
  97. }
  98. int TreeTemplate::getMaxHeight() const
  99. {
  100. return maxHeight;
  101. }
  102. TreeTemplateFactory::TreeTemplateFactory()
  103. : GeneratorTemplateFactory()
  104. {}
  105. TreeTemplate* TreeTemplateFactory::createValue(
  106. Framework::JSON::JSONObject* zJson) const
  107. {
  108. return new TreeTemplate();
  109. }
  110. TreeTemplate* TreeTemplateFactory::fromJson(
  111. Framework::JSON::JSONObject* zConfig) const
  112. {
  113. TreeTemplate* result = GeneratorTemplateFactory::fromJson(zConfig);
  114. result->setWoodTypeId(BlockType::getTypeId(
  115. zConfig->asObject()->zValue("wood")->asString()->getString()));
  116. result->setLeavesTypeId(BlockType::getTypeId(
  117. zConfig->asObject()->zValue("leaves")->asString()->getString()));
  118. result->setMinHeight(
  119. (int)(zConfig->asObject()->zValue("minSize")->asNumber()->getNumber()));
  120. result->setMaxHeight(
  121. (int)(zConfig->asObject()->zValue("maxSize")->asNumber()->getNumber()));
  122. return result;
  123. }
  124. Framework::JSON::JSONObject* TreeTemplateFactory::toJsonObject(
  125. TreeTemplate* zObject) const
  126. {
  127. Framework::JSON::JSONObject* result
  128. = GeneratorTemplateFactory::toJsonObject(zObject);
  129. result->addValue("propability",
  130. new Framework::JSON::JSONNumber((double)zObject->getPropability()));
  131. result->addValue("wood",
  132. new Framework::JSON::JSONString(zObject->zWoodType()->getName()));
  133. result->addValue("leaves",
  134. new Framework::JSON::JSONString(zObject->zLeavesType()->getName()));
  135. result->addValue("minSize",
  136. new Framework::JSON::JSONNumber((double)zObject->getMinHeight()));
  137. result->addValue("maxSize",
  138. new Framework::JSON::JSONNumber((double)zObject->getMaxHeight()));
  139. return result;
  140. }
  141. JSONObjectValidationBuilder* TreeTemplateFactory::addToValidator(
  142. JSONObjectValidationBuilder* builder) const
  143. {
  144. return GeneratorTemplateFactory::addToValidator(
  145. builder
  146. ->withRequiredAttribute("wood",
  147. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  148. BlockTypeNameFactory::TYPE_ID))
  149. ->withRequiredAttribute("leaves",
  150. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  151. BlockTypeNameFactory::TYPE_ID))
  152. ->withRequiredNumber("minSize")
  153. ->whichIsGreaterThen(0)
  154. ->finishNumber()
  155. ->withRequiredNumber("maxSize")
  156. ->whichIsGreaterThen(0)
  157. ->finishNumber()
  158. ->withRequiredNumber("propability")
  159. ->whichIsGreaterThen(0)
  160. ->finishNumber());
  161. }
  162. const char* TreeTemplateFactory::getTypeToken() const
  163. {
  164. return "Tree";
  165. }