PlantConfig.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "PlantConfig.h"
  2. #include "Chunk.h"
  3. #include "Dimension.h"
  4. #include "Game.h"
  5. #include "JNoise.h"
  6. PlantConfig::PlantConfig()
  7. : Framework::ReferenceCounter(),
  8. condition(0),
  9. noise(0),
  10. noiseConfig(0),
  11. threshold(0.0),
  12. locations(0),
  13. plantBlockTypeName(""),
  14. plantblockTypeId(0),
  15. plantHeight(0),
  16. direction(PlantDirection::UP),
  17. orientationConfig(0)
  18. {}
  19. PlantConfig::~PlantConfig()
  20. {
  21. if (condition)
  22. {
  23. condition->release();
  24. }
  25. if (noise)
  26. {
  27. noise->release();
  28. }
  29. if (noiseConfig)
  30. {
  31. noiseConfig->release();
  32. }
  33. if (orientationConfig)
  34. {
  35. orientationConfig->release();
  36. }
  37. if (plantHeight)
  38. {
  39. plantHeight->release();
  40. }
  41. }
  42. void PlantConfig::initialize(JExpressionMemory* zMemory)
  43. {
  44. condition->compile(zMemory);
  45. if (noise)
  46. {
  47. noise->release();
  48. }
  49. noise = JNoise::parseNoise(noiseConfig, zMemory);
  50. plantblockTypeId = Game::INSTANCE->getBlockTypeId(plantBlockTypeName);
  51. }
  52. double PlantConfig::doesGeneratePlant(int x,
  53. int y,
  54. int z,
  55. int dimensionId,
  56. Chunk* zChunk,
  57. bool underground,
  58. bool underwater,
  59. bool surface,
  60. int seaFluidBlockTypeId)
  61. {
  62. if (underwater && !(locations & PlantLocation::UNDERWATER))
  63. {
  64. return 0.0;
  65. }
  66. if (surface && !(locations & PlantLocation::SURFACE))
  67. {
  68. return 0.0;
  69. }
  70. if (!underwater && underground && !(locations & PlantLocation::CAVE))
  71. {
  72. return 0.0;
  73. }
  74. if (!underwater && !underground && !surface
  75. && !(locations & PlantLocation::ABOVE_SURFACE))
  76. {
  77. return 0.0;
  78. }
  79. if (!condition->getValue())
  80. {
  81. return 0.0;
  82. }
  83. if (!noise)
  84. {
  85. return 0.0;
  86. }
  87. return noise->getNoise((double)x, (double)y, (double)z) - threshold;
  88. }
  89. void PlantConfig::generatePlantAt(
  90. int x, int y, int z, int dimensionId, Chunk* zChunk)
  91. {
  92. Direction frontDirection = orientationConfig
  93. ? orientationConfig->calculateOrientation(
  94. {x, y, z}, dimensionId, zChunk)
  95. : NO_DIRECTION;
  96. int height = (int)round(plantHeight->getValue(x, y, z, dimensionId, noise));
  97. for (int i = 0; i < height; i++)
  98. {
  99. Framework::Vec3<int> pos(x, y, z);
  100. if (direction == PlantDirection::UP)
  101. {
  102. pos = {x, y, z + i};
  103. }
  104. else if (direction == PlantDirection::DOWN)
  105. {
  106. pos = {x, y, z - i};
  107. }
  108. if (pos.z < 0 || pos.z >= WORLD_HEIGHT)
  109. {
  110. break;
  111. }
  112. int currentType = zChunk->getBlockTypeAtWC(pos.x, pos.y, pos.z);
  113. if (currentType != BlockTypeEnum::AIR
  114. && currentType != BlockTypeEnum::NO_BLOCK)
  115. {
  116. break;
  117. }
  118. if (orientationConfig
  119. && frontDirection
  120. != Game::INSTANCE->zBlockType(plantblockTypeId)
  121. ->getDefaultFrontDirection())
  122. {
  123. Block* block = Game::INSTANCE->zBlockType(plantblockTypeId)
  124. ->createBlockAt(pos, dimensionId, 0);
  125. block->setFrontDirection(frontDirection);
  126. zChunk->putBlockAt(Dimension::chunkCoordinates(pos), block);
  127. }
  128. else
  129. {
  130. zChunk->putBlockTypeAt(
  131. Dimension::chunkCoordinates(pos), plantblockTypeId);
  132. }
  133. }
  134. }
  135. void PlantConfig::setCondition(JBoolExpression* condition)
  136. {
  137. if (this->condition)
  138. {
  139. this->condition->release();
  140. }
  141. this->condition = condition;
  142. }
  143. JBoolExpression* PlantConfig::zCondition() const
  144. {
  145. return condition;
  146. }
  147. void PlantConfig::setNoiseConfig(Framework::JSON::JSONObject* noiseConfig)
  148. {
  149. if (this->noiseConfig)
  150. {
  151. this->noiseConfig->release();
  152. }
  153. this->noiseConfig = noiseConfig;
  154. }
  155. Framework::JSON::JSONObject* PlantConfig::zNoiseConfig() const
  156. {
  157. return noiseConfig;
  158. }
  159. void PlantConfig::setThreshold(double threshold)
  160. {
  161. this->threshold = threshold;
  162. }
  163. double PlantConfig::getThreshold() const
  164. {
  165. return threshold;
  166. }
  167. void PlantConfig::setLocations(int locations)
  168. {
  169. this->locations = locations;
  170. }
  171. int PlantConfig::getLocations() const
  172. {
  173. return locations;
  174. }
  175. void PlantConfig::setPlantBlockTypeName(Framework::Text name)
  176. {
  177. this->plantBlockTypeName = name;
  178. }
  179. Framework::Text PlantConfig::getPlantBlockTypeName() const
  180. {
  181. return plantBlockTypeName;
  182. }
  183. void PlantConfig::setPlantHeight(FloatDistribution* height)
  184. {
  185. this->plantHeight = height;
  186. }
  187. FloatDistribution* PlantConfig::getPlantHeight() const
  188. {
  189. return plantHeight;
  190. }
  191. void PlantConfig::setDirection(int direction)
  192. {
  193. this->direction = direction;
  194. }
  195. int PlantConfig::getDirection() const
  196. {
  197. return direction;
  198. }
  199. void PlantConfig::setOrientationConfig(OrientationConfig* orientationConfig)
  200. {
  201. if (this->orientationConfig)
  202. {
  203. this->orientationConfig->release();
  204. }
  205. this->orientationConfig = orientationConfig;
  206. }
  207. OrientationConfig* PlantConfig::zOrientationConfig() const
  208. {
  209. return orientationConfig;
  210. }
  211. PlantConfigFactory::PlantConfigFactory()
  212. : ObjectTypeFactory<PlantConfig>()
  213. {}
  214. PlantConfig* PlantConfigFactory::fromJson(
  215. Framework::JSON::JSONObject* zJson) const
  216. {
  217. PlantConfig* config = new PlantConfig();
  218. config->setCondition(
  219. Game::INSTANCE->zTypeRegistry()->fromJson<JBoolExpression>(
  220. zJson->zValue("condition")));
  221. config->setNoiseConfig(zJson->getValue("noise")->asObject());
  222. config->setThreshold(zJson->zValue("threshold")->asNumber()->getNumber());
  223. config->setLocations(0);
  224. Framework::JSON::JSONArray* zLocationsArray
  225. = zJson->zValue("locations")->asArray();
  226. for (int i = 0; i < zLocationsArray->getLength(); i++)
  227. {
  228. Framework::Text locationStr
  229. = zLocationsArray->zValue(i)->asString()->getString();
  230. if (locationStr.isEqual("CAVE"))
  231. {
  232. config->setLocations(config->getLocations() | PlantLocation::CAVE);
  233. }
  234. else if (locationStr.isEqual("UNDERWATER"))
  235. {
  236. config->setLocations(
  237. config->getLocations() | PlantLocation::UNDERWATER);
  238. }
  239. else if (locationStr.isEqual("SURFACE"))
  240. {
  241. config->setLocations(
  242. config->getLocations() | PlantLocation::SURFACE);
  243. }
  244. else if (locationStr.isEqual("ABOVE_SURFACE"))
  245. {
  246. config->setLocations(
  247. config->getLocations() | PlantLocation::ABOVE_SURFACE);
  248. }
  249. }
  250. config->setPlantBlockTypeName(
  251. zJson->zValue("plantBlock")->asString()->getString());
  252. config->setPlantHeight(
  253. Game::INSTANCE->zTypeRegistry()->fromJson<FloatDistribution>(
  254. zJson->zValue("plantHeight")));
  255. Framework::Text locationStr
  256. = zJson->zValue("direction")->asString()->getString();
  257. if (locationStr.isEqual("UP"))
  258. {
  259. config->setDirection(PlantDirection::UP);
  260. }
  261. else if (locationStr.isEqual("DOWN"))
  262. {
  263. config->setDirection(PlantDirection::DOWN);
  264. }
  265. if (zJson->hasValue("orientation"))
  266. {
  267. config->setOrientationConfig(
  268. Game::INSTANCE->zTypeRegistry()->fromJson<OrientationConfig>(
  269. zJson->zValue("orientation")));
  270. }
  271. return config;
  272. }
  273. Framework::JSON::JSONObject* PlantConfigFactory::toJsonObject(
  274. PlantConfig* zObject) const
  275. {
  276. Framework::JSON::JSONObject* zJson = new Framework::JSON::JSONObject();
  277. zJson->addValue("condition",
  278. Game::INSTANCE->zTypeRegistry()->toJson<JBoolExpression>(
  279. zObject->zCondition()));
  280. zJson->addValue("noise",
  281. dynamic_cast<Framework::JSON::JSONObject*>(
  282. zObject->zNoiseConfig()->getThis()));
  283. zJson->addValue(
  284. "threshold", new Framework::JSON::JSONNumber(zObject->getThreshold()));
  285. Framework::JSON::JSONArray* zLocationsArray
  286. = new Framework::JSON::JSONArray();
  287. int locations = zObject->getLocations();
  288. if (locations & PlantLocation::CAVE)
  289. {
  290. zLocationsArray->addValue(new Framework::JSON::JSONString("CAVE"));
  291. }
  292. if (locations & PlantLocation::UNDERWATER)
  293. {
  294. zLocationsArray->addValue(
  295. new Framework::JSON::JSONString("UNDERWATER"));
  296. }
  297. if (locations & PlantLocation::SURFACE)
  298. {
  299. zLocationsArray->addValue(new Framework::JSON::JSONString("SURFACE"));
  300. }
  301. if (locations & PlantLocation::ABOVE_SURFACE)
  302. {
  303. zLocationsArray->addValue(
  304. new Framework::JSON::JSONString("ABOVE_SURFACE"));
  305. }
  306. zJson->addValue("locations", zLocationsArray);
  307. zJson->addValue("plantBlock",
  308. new Framework::JSON::JSONString(zObject->getPlantBlockTypeName()));
  309. zJson->addValue("plantHeight",
  310. Game::INSTANCE->zTypeRegistry()->toJson<FloatDistribution>(
  311. zObject->getPlantHeight()));
  312. if (zObject->getDirection() == PlantDirection::UP)
  313. {
  314. zJson->addValue("direction", new Framework::JSON::JSONString("UP"));
  315. }
  316. else if (zObject->getDirection() == PlantDirection::DOWN)
  317. {
  318. zJson->addValue("direction", new Framework::JSON::JSONString("DOWN"));
  319. }
  320. if (zObject->zOrientationConfig())
  321. {
  322. zJson->addValue("orientation",
  323. Game::INSTANCE->zTypeRegistry()->toJson<OrientationConfig>(
  324. zObject->zOrientationConfig()));
  325. }
  326. return zJson;
  327. }
  328. JSONObjectValidationBuilder* PlantConfigFactory::addToValidator(
  329. JSONObjectValidationBuilder* builder) const
  330. {
  331. return builder
  332. ->withRequiredAttribute("condition",
  333. Game::INSTANCE->zTypeRegistry()->getValidator<JBoolExpression>())
  334. ->withRequiredAttribute("noise", JNoise::getValidator(false))
  335. ->withRequiredNumber("threshold")
  336. ->withDefault(0.5)
  337. ->finishNumber()
  338. ->withRequiredArray("locations")
  339. ->addAcceptedStringInArray()
  340. ->whichIsOneOf({"CAVE", "UNDERWATER", "SURFACE", "ABOVE_SURFACE"})
  341. ->finishString()
  342. ->finishArray()
  343. ->withRequiredAttribute("plantBlock",
  344. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  345. BlockTypeNameFactory::TYPE_ID))
  346. ->withRequiredAttribute("plantHeight",
  347. Game::INSTANCE->zTypeRegistry()->getValidator<FloatDistribution>())
  348. ->withRequiredString("direction")
  349. ->whichIsOneOf({"UP", "DOWN"})
  350. ->withDefault("UP")
  351. ->finishString()
  352. ->withOptionalAttribute("orientation",
  353. Game::INSTANCE->zTypeRegistry()->getValidator<OrientationConfig>());
  354. }