BasicBlocks.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. #include "BasicBlocks.h"
  2. #include "AddEntityUpdate.h"
  3. #include "Game.h"
  4. #include "ItemEntity.h"
  5. #include "ModelInfo.h"
  6. #include "TreeSeblingBlock.h"
  7. BasicBlock::BasicBlock(int typeId, Framework::Vec3<int> pos, int dimensionId)
  8. : BasicBlock(typeId, pos, dimensionId, false)
  9. {}
  10. BasicBlock::BasicBlock(
  11. int typeId, Framework::Vec3<int> pos, int dimensionId, bool hasInventory)
  12. : Block(typeId, pos, dimensionId, hasInventory)
  13. {}
  14. bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  15. {
  16. return 0;
  17. }
  18. void BasicBlock::onPostTick() {}
  19. BasicBlockType::BasicBlockType(Framework::Text itemTypeName,
  20. ModelInfo* model,
  21. Framework::Text name,
  22. int mapColor,
  23. bool modelSubscription,
  24. float hardness,
  25. Framework::RCArray<Framework::Text> groupNames)
  26. : BlockType(0,
  27. model,
  28. 1,
  29. 100,
  30. 0,
  31. name,
  32. modelSubscription,
  33. mapColor,
  34. groupNames,
  35. hardness),
  36. itemTypeName(itemTypeName),
  37. transparent(0),
  38. passable(0),
  39. speedModifier(1.f),
  40. interactable(1)
  41. {}
  42. bool BasicBlockType::initialize(Game* zGame)
  43. {
  44. if (itemTypeName.getLength())
  45. {
  46. itemTypeId = zGame->getItemTypeId(itemTypeName);
  47. }
  48. else
  49. {
  50. itemTypeId = 0;
  51. }
  52. return itemTypeId >= 0 && BlockType::initialize(zGame);
  53. }
  54. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  55. {
  56. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  57. block->transparent = transparent;
  58. block->passable = passable;
  59. block->hp = (float)getInitialMaxHP();
  60. block->maxHP = (float)getInitialMaxHP();
  61. block->hardness = getHardness();
  62. block->speedModifier = speedModifier;
  63. block->interactable = interactable;
  64. BlockType::createSuperBlock(zBlock, zItem);
  65. }
  66. Block* BasicBlockType::createBlock(
  67. Framework::Vec3<int> position, int dimensionId) const
  68. {
  69. return new BasicBlock(getId(), position, dimensionId);
  70. }
  71. Item* BasicBlockType::createItem() const
  72. {
  73. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  74. }
  75. Framework::Text BasicBlockType::getItemTypeName() const
  76. {
  77. return itemTypeName;
  78. }
  79. ItemType* BasicBlockType::createItemType() const
  80. {
  81. return new BasicBlockItemType(getItemTypeName(),
  82. new ModelInfo(zModel()->getModelPath(), zModel()->getTexturePaths(), zModel()->isTransparent(), zModel()->getSize() / 2.f),
  83. transparent,
  84. passable,
  85. getHardness(),
  86. speedModifier,
  87. getItemTypeName(),
  88. 0, 50);
  89. }
  90. int BasicBlockType::getItemTypeId() const
  91. {
  92. return itemTypeId;
  93. }
  94. void BasicBlockType::setTransparent(bool transparent) {
  95. this->transparent = transparent;
  96. }
  97. bool BasicBlockType::isTransparent() const
  98. {
  99. return transparent;
  100. }
  101. void BasicBlockType::setPassable(bool passable) {
  102. this->passable = passable;
  103. }
  104. bool BasicBlockType::isPassable() const
  105. {
  106. return passable;
  107. }
  108. void BasicBlockType::setSpeedModifier(float speedModifier) {
  109. this->speedModifier = speedModifier;
  110. }
  111. float BasicBlockType::getSpeedModifier() const
  112. {
  113. return speedModifier;
  114. }
  115. void BasicBlockType::setInteractable(bool interactable) {
  116. this->interactable = interactable;
  117. }
  118. BasicBlockTypeFactory::BasicBlockTypeFactory()
  119. : SubTypeFactory()
  120. {}
  121. BasicBlockType* BasicBlockTypeFactory::fromJson(
  122. Framework::JSON::JSONObject* zJson) const
  123. {
  124. Framework::RCArray<Framework::Text> groupNames;
  125. for (Framework::JSON::JSONValue* value :
  126. *zJson->zValue("groupNames")->asArray())
  127. {
  128. groupNames.add(new Framework::Text(value->asString()->getString()));
  129. }
  130. return new BasicBlockType(
  131. zJson->zValue("itemType")->asString()->getString(),
  132. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  133. zJson->zValue("model")),
  134. zJson->zValue("name")->asString()->getString(),
  135. (int)zJson->zValue("mapColor")->asString()->getString(),
  136. zJson->zValue("modelSubscription")->asBool()->getBool(),
  137. (float)zJson->zValue("hardness")->asNumber()->getNumber(),
  138. groupNames);
  139. }
  140. Framework::JSON::JSONObject* BasicBlockTypeFactory::toJson(
  141. BasicBlockType* zObject) const
  142. {
  143. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  144. result->addValue("itemType",
  145. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  146. result->addValue(
  147. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  148. result->addValue(
  149. "name", new Framework::JSON::JSONString(zObject->getName()));
  150. result->addValue(
  151. "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
  152. result->addValue("modelSubscription",
  153. new Framework::JSON::JSONBool(zObject->doesNeedModelSubscription()));
  154. result->addValue(
  155. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  156. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  157. for (Framework::Text* groupName : zObject->getGroupNames())
  158. {
  159. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  160. }
  161. result->addValue("groupNames", groupNames);
  162. return result;
  163. }
  164. Framework::JSON::Validator::JSONValidator* BasicBlockTypeFactory::getValidator(
  165. Framework::JSON::Validator::ObjectValidationBuilder<
  166. Framework::JSON::Validator::JSONValidator>* builder) const
  167. {
  168. return builder->withRequiredString("itemType")
  169. ->finishString()
  170. ->withRequiredAttribute(
  171. "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  172. ->withRequiredString("name")
  173. ->finishString()
  174. ->withRequiredString("mapColor")
  175. ->finishString()
  176. ->withRequiredBool("modelSubscription")
  177. ->withDefault(false)
  178. ->finishBool()
  179. ->withRequiredNumber("hardness")
  180. ->withDefault(1.0)
  181. ->finishNumber()
  182. ->withRequiredArray("groupNames")
  183. ->withDefault(new Framework::JSON::JSONArray())
  184. ->addAcceptedStringInArray()
  185. ->finishString()
  186. ->finishArray()
  187. ->finishObject();
  188. }
  189. Framework::Text BasicBlockTypeFactory::getTypeToken() const
  190. {
  191. return "basicBlock";
  192. }
  193. AdditionalItemSpawningBlock::AdditionalItemSpawningBlock(
  194. int typeId, Framework::Vec3<int> pos, int dimensionId)
  195. : BasicBlock(typeId, pos, dimensionId)
  196. {}
  197. void AdditionalItemSpawningBlock::addSpawn(SpawnConfig config)
  198. {
  199. spawns.add(config);
  200. }
  201. void AdditionalItemSpawningBlock::onDestroy()
  202. {
  203. for (const SpawnConfig& config : spawns)
  204. {
  205. if ((double)rand() / RAND_MAX < config.chance)
  206. {
  207. int amount = config.min
  208. + (int)((config.max - config.min)
  209. * ((double)rand() / RAND_MAX));
  210. if (amount > 0)
  211. {
  212. ItemStack* spawnedItems
  213. = Game::INSTANCE->zItemType(config.typeId)
  214. ->createItemStack(amount);
  215. if (spawnedItems)
  216. {
  217. ItemEntity* itemEntity
  218. = (ItemEntity*)Game::INSTANCE
  219. ->zEntityType(EntityTypeEnum::ITEM)
  220. ->createEntity(location
  221. + Framework::Vec3<float>(
  222. 0.5f, 0.5f, 0.5f),
  223. getDimensionId(),
  224. Game::INSTANCE->getNextEntityId());
  225. itemEntity->unsaveAddItem(spawnedItems, NO_DIRECTION, 0);
  226. spawnedItems->release();
  227. Game::INSTANCE->requestWorldUpdate(
  228. new AddEntityUpdate(itemEntity, getDimensionId()));
  229. }
  230. }
  231. }
  232. }
  233. BasicBlock::onDestroy();
  234. }
  235. void AdditionalItemSpawningBlockType::createSuperBlock(
  236. Block* zBlock, Item* zItem) const
  237. {
  238. AdditionalItemSpawningBlock* block
  239. = dynamic_cast<AdditionalItemSpawningBlock*>(zBlock);
  240. if (block)
  241. {
  242. for (const SpawnConfig& config : spawns)
  243. {
  244. block->addSpawn(config);
  245. }
  246. }
  247. BasicBlockType::createSuperBlock(zBlock, zItem);
  248. }
  249. AdditionalItemSpawningBlockType::AdditionalItemSpawningBlockType(
  250. Framework::Text itemTypeName,
  251. ModelInfo* model,
  252. Framework::Text name,
  253. int mapColor,
  254. bool modelSubscription,
  255. float hardness,
  256. Framework::Array<SpawnConfig> spawns,
  257. Framework::RCArray<Framework::Text> groupNames)
  258. : BasicBlockType(itemTypeName,
  259. model,
  260. name,
  261. mapColor,
  262. modelSubscription,
  263. hardness,
  264. groupNames),
  265. spawns(spawns)
  266. {}
  267. bool AdditionalItemSpawningBlockType::initialize(Game* zGame)
  268. {
  269. for (auto iterator = spawns.begin(); iterator; iterator++)
  270. {
  271. int itemTypeId
  272. = Game::INSTANCE->getItemTypeId(iterator.val().itemTypeName);
  273. if (itemTypeId < 0)
  274. {
  275. return false;
  276. }
  277. iterator.set({iterator.val().min,
  278. iterator.val().max,
  279. iterator.val().chance,
  280. iterator.val().itemTypeName,
  281. itemTypeId});
  282. }
  283. return BasicBlockType::initialize(zGame);
  284. }
  285. Block* AdditionalItemSpawningBlockType::createBlock(
  286. Framework::Vec3<int> position, int dimensionId) const
  287. {
  288. AdditionalItemSpawningBlock* block
  289. = new AdditionalItemSpawningBlock(getId(), position, dimensionId);
  290. return block;
  291. }
  292. const Framework::Array<SpawnConfig>&
  293. AdditionalItemSpawningBlockType::getSpawns() const
  294. {
  295. return spawns;
  296. }
  297. AdditionalItemSpawningBlockTypeFactory::AdditionalItemSpawningBlockTypeFactory()
  298. : SubTypeFactory()
  299. {}
  300. AdditionalItemSpawningBlockType*
  301. AdditionalItemSpawningBlockTypeFactory::fromJson(
  302. Framework::JSON::JSONObject* zJson) const
  303. {
  304. Framework::RCArray<Framework::Text> groupNames;
  305. for (Framework::JSON::JSONValue* value :
  306. *zJson->zValue("groupNames")->asArray())
  307. {
  308. groupNames.add(new Framework::Text(value->asString()->getString()));
  309. }
  310. Framework::Array<SpawnConfig> spawns;
  311. Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
  312. for (int i = 0; i < spawnsJson->getLength(); i++)
  313. {
  314. Framework::JSON::JSONObject* spawnJson
  315. = spawnsJson->zValue(i)->asObject();
  316. spawns.add(SpawnConfig{
  317. (int)spawnJson->zValue("min")->asNumber()->getNumber(),
  318. (int)spawnJson->zValue("max")->asNumber()->getNumber(),
  319. (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
  320. spawnJson->zValue("itemType")->asString()->getString(),
  321. 0,
  322. });
  323. }
  324. return new AdditionalItemSpawningBlockType(
  325. zJson->zValue("itemType")->asString()->getString(),
  326. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  327. zJson->zValue("model")),
  328. zJson->zValue("name")->asString()->getString(),
  329. (int)zJson->zValue("mapColor")->asString()->getString(),
  330. zJson->zValue("modelSubscription")->asBool()->getBool(),
  331. (float)zJson->zValue("hardness")->asNumber()->getNumber(),
  332. spawns,
  333. groupNames);
  334. }
  335. Framework::JSON::JSONObject* AdditionalItemSpawningBlockTypeFactory::toJson(
  336. AdditionalItemSpawningBlockType* zObject) const
  337. {
  338. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  339. result->addValue("itemType",
  340. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  341. result->addValue(
  342. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  343. result->addValue(
  344. "name", new Framework::JSON::JSONString(zObject->getName()));
  345. result->addValue(
  346. "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
  347. result->addValue("modelSubscription",
  348. new Framework::JSON::JSONBool(zObject->doesNeedModelSubscription()));
  349. result->addValue(
  350. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  351. Framework::JSON::JSONArray* spawns = new Framework::JSON::JSONArray();
  352. for (const SpawnConfig& config : zObject->getSpawns())
  353. {
  354. Framework::JSON::JSONObject* spawn = new Framework::JSON::JSONObject();
  355. spawn->addValue(
  356. "itemType", new Framework::JSON::JSONString(config.itemTypeName));
  357. spawn->addValue(
  358. "chance", new Framework::JSON::JSONNumber(config.chance));
  359. spawn->addValue("min", new Framework::JSON::JSONNumber(config.min));
  360. spawn->addValue("max", new Framework::JSON::JSONNumber(config.max));
  361. spawns->addValue(spawn);
  362. }
  363. result->addValue("spawns", spawns);
  364. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  365. for (Framework::Text* groupName : zObject->getGroupNames())
  366. {
  367. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  368. }
  369. result->addValue("groupNames", groupNames);
  370. return result;
  371. }
  372. Framework::JSON::Validator::JSONValidator*
  373. AdditionalItemSpawningBlockTypeFactory::getValidator(
  374. Framework::JSON::Validator::ObjectValidationBuilder<
  375. Framework::JSON::Validator::JSONValidator>* builder) const
  376. {
  377. return builder->withRequiredString("itemType")
  378. ->finishString()
  379. ->withRequiredAttribute(
  380. "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  381. ->withRequiredString("name")
  382. ->finishString()
  383. ->withRequiredString("mapColor")
  384. ->finishString()
  385. ->withRequiredBool("modelSubscription")
  386. ->withDefault(false)
  387. ->finishBool()
  388. ->withRequiredNumber("hardness")
  389. ->withDefault(1.0)
  390. ->finishNumber()
  391. ->withRequiredAttribute("spawns",
  392. JSON::Validator::JSONValidator::buildForArray()
  393. ->addAcceptedObjectInArray()
  394. ->withRequiredString("itemType")
  395. ->finishString()
  396. ->withRequiredNumber("chance")
  397. ->finishNumber()
  398. ->withRequiredNumber("min")
  399. ->finishNumber()
  400. ->withRequiredNumber("max")
  401. ->finishNumber()
  402. ->finishObject()
  403. ->finishArray())
  404. ->withRequiredArray("groupNames")
  405. ->withDefault(new Framework::JSON::JSONArray())
  406. ->addAcceptedStringInArray()
  407. ->finishString()
  408. ->finishArray()
  409. ->finishObject();
  410. }
  411. Framework::Text AdditionalItemSpawningBlockTypeFactory::getTypeToken() const
  412. {
  413. return "additionalItemsBlockType";
  414. }