TreeTemplate.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. }
  93. int TreeTemplate::getMinHeight() const
  94. {
  95. return minHeight;
  96. }
  97. int TreeTemplate::getMaxHeight() const
  98. {
  99. return maxHeight;
  100. }
  101. TreeTemplateFactory::TreeTemplateFactory()
  102. : GeneratorTemplateFactory()
  103. {}
  104. TreeTemplate* TreeTemplateFactory::createValue(
  105. Framework::JSON::JSONObject* zJson) const
  106. {
  107. return new TreeTemplate();
  108. }
  109. void TreeTemplateFactory::fromJson(
  110. TreeTemplate* zResult, Framework::JSON::JSONObject* zConfig) const
  111. {
  112. zResult->setWoodTypeId(BlockType::getTypeId(
  113. zConfig->asObject()->zValue("wood")->asString()->getString()));
  114. zResult->setLeavesTypeId(BlockType::getTypeId(
  115. zConfig->asObject()->zValue("leaves")->asString()->getString()));
  116. zResult->setMinHeight(
  117. (int)(zConfig->asObject()->zValue("minSize")->asNumber()->getNumber()));
  118. zResult->setMaxHeight(
  119. (int)(zConfig->asObject()->zValue("maxSize")->asNumber()->getNumber()));
  120. GeneratorTemplateFactory::fromJson(zResult, zConfig);
  121. }
  122. void TreeTemplateFactory::toJson(
  123. TreeTemplate* zObject, Framework::JSON::JSONObject* zResult) const
  124. {
  125. zResult->addValue("propability",
  126. new Framework::JSON::JSONNumber((double)zObject->getPropability()));
  127. zResult->addValue("wood",
  128. new Framework::JSON::JSONString(zObject->zWoodType()->getName()));
  129. zResult->addValue("leaves",
  130. new Framework::JSON::JSONString(zObject->zLeavesType()->getName()));
  131. zResult->addValue("minSize",
  132. new Framework::JSON::JSONNumber((double)zObject->getMinHeight()));
  133. zResult->addValue("maxSize",
  134. new Framework::JSON::JSONNumber((double)zObject->getMaxHeight()));
  135. }
  136. JSONObjectValidationBuilder* TreeTemplateFactory::addToValidator(
  137. JSONObjectValidationBuilder* builder) const
  138. {
  139. Framework::RCArray<Framework::Text> blockTypeNames;
  140. for (int i = 0; i < Game::INSTANCE->getBlockTypeCount(); i++)
  141. {
  142. if (Game::INSTANCE->zBlockType(i))
  143. {
  144. blockTypeNames.add(
  145. new Framework::Text(Game::INSTANCE->zBlockType(i)->getName()));
  146. }
  147. }
  148. return GeneratorTemplateFactory::addToValidator(
  149. builder->withRequiredString("wood")
  150. ->whichIsOneOf(blockTypeNames)
  151. ->finishString()
  152. ->withRequiredString("leaves")
  153. ->whichIsOneOf(blockTypeNames)
  154. ->finishString()
  155. ->withRequiredNumber("minSize")
  156. ->whichIsGreaterThen(0)
  157. ->finishNumber()
  158. ->withRequiredNumber("maxSize")
  159. ->whichIsGreaterThen(0)
  160. ->finishNumber()
  161. ->withRequiredNumber("propability")
  162. ->whichIsGreaterThen(0)
  163. ->finishNumber());
  164. }
  165. Framework::Text TreeTemplateFactory::getTypeToken() const
  166. {
  167. return "Tree";
  168. }