PlantConfig.cpp 8.7 KB

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