TreeTemplate.cpp 5.7 KB

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