FluidContainer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. #include "FluidContainer.h"
  2. #include <TextFeld.h>
  3. #include "Dimension.h"
  4. #include "Entity.h"
  5. #include "FluidBlock.h"
  6. #include "Game.h"
  7. FluidContainerItem::FluidContainerItem(int itemTypeId, Framework::Text name)
  8. : Item(itemTypeId, name),
  9. fluidTypeId(0),
  10. fluidAmount(0)
  11. {
  12. placeable = 1;
  13. usable = 1;
  14. eatable = 1;
  15. }
  16. const BlockType* FluidContainerItem::zPlacedBlockType() const
  17. {
  18. return fluidTypeId && fluidAmount >= 1000
  19. ? Game::INSTANCE->zBlockType(fluidTypeId)
  20. : 0;
  21. }
  22. bool FluidContainerItem::canBeStackedWith(const Item* zItem) const
  23. {
  24. const FluidContainerItem* other
  25. = dynamic_cast<const FluidContainerItem*>(zItem);
  26. if (!other) return false;
  27. return Item::canBeStackedWith(zItem) && other->fluidTypeId == fluidTypeId
  28. && other->fluidAmount == fluidAmount;
  29. }
  30. bool FluidContainerItem::canBePlacedAt(
  31. const int dimensionId, Framework::Vec3<int> worldPos) const
  32. {
  33. if (fluidAmount >= 1000)
  34. {
  35. Dimension* dim = Game::INSTANCE->zDimension(dimensionId);
  36. if (dim)
  37. {
  38. const Block* block = dim->zBlockOrDefault(worldPos);
  39. if (block)
  40. {
  41. if (block->zBlockType()->getId() == BlockTypeEnum::AIR)
  42. return true;
  43. if (block->zBlockType()->getId() == fluidTypeId)
  44. {
  45. const FluidBlock* fluidBlock
  46. = dynamic_cast<const FluidBlock*>(block);
  47. return fluidBlock && fluidBlock->getDistanceToSource() > 0;
  48. }
  49. }
  50. }
  51. }
  52. return false;
  53. }
  54. void FluidContainerItem::onPlaced()
  55. {
  56. setAmount(fluidAmount - 1000);
  57. }
  58. Framework::Text FluidContainerItem::getTooltipUIML() const
  59. {
  60. Framework::Text uiml = "<tip><text width=\"auto\" height=\"auto\">";
  61. uiml.append() << getName();
  62. if (fluidTypeId != 0)
  63. {
  64. uiml.append() << "\nFluid: "
  65. << Game::INSTANCE->zBlockType(fluidTypeId)->getName()
  66. << "\nAmount: " << fluidAmount << " L";
  67. }
  68. else
  69. {
  70. uiml.append() << "\nEmpty";
  71. }
  72. uiml.append() << "</text></tip>";
  73. return uiml;
  74. }
  75. bool FluidContainerItem::applyFoodEffects(Entity* zTarget)
  76. {
  77. if (fluidTypeId)
  78. {
  79. const FluidBlockType* fluidType = dynamic_cast<const FluidBlockType*>(
  80. Game::INSTANCE->zBlockType(fluidTypeId));
  81. if (fluidType
  82. && (fluidType->getHungerRecoveryPerL()
  83. || fluidType->getThirstRecoveryPerL()))
  84. {
  85. int drinkable
  86. = fluidType->getThirstRecoveryPerL() > 0
  87. ? (int)((zTarget->getMaxThirst() - zTarget->getThirst())
  88. / fluidType->getThirstRecoveryPerL())
  89. : (int)((zTarget->getMaxHunger() - zTarget->getHunger())
  90. / fluidType->getHungerRecoveryPerL());
  91. if (fluidType->getHungerRecoveryPerL() > 0
  92. && fluidType->getThirstRecoveryPerL() > 0)
  93. {
  94. int drinkable2
  95. = (int)((zTarget->getMaxHunger() - zTarget->getHunger())
  96. / fluidType->getHungerRecoveryPerL());
  97. if (drinkable2 < drinkable) drinkable = drinkable2;
  98. }
  99. if (getAmount() < drinkable) drinkable = getAmount();
  100. if (!drinkable) return false;
  101. setAmount(getAmount() - drinkable);
  102. zTarget->setThirst(
  103. zTarget->getThirst()
  104. + drinkable * fluidType->getThirstRecoveryPerL());
  105. zTarget->setHunger(
  106. zTarget->getHunger()
  107. + drinkable * fluidType->getHungerRecoveryPerL());
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. bool FluidContainerItem::canApplyFoodEffectsFully(Entity* zTarget) const
  114. {
  115. return false;
  116. }
  117. int FluidContainerItem::getAmount() const
  118. {
  119. return fluidAmount;
  120. }
  121. void FluidContainerItem::setAmount(int amount)
  122. {
  123. fluidAmount = amount;
  124. if (!fluidAmount)
  125. {
  126. fluidTypeId = 0;
  127. }
  128. }
  129. int FluidContainerItem::getFluidTypeId() const
  130. {
  131. return fluidTypeId;
  132. }
  133. void FluidContainerItem::setFluidTypeId(int fluidTypeId)
  134. {
  135. this->fluidTypeId = fluidTypeId;
  136. if (!fluidTypeId)
  137. {
  138. fluidAmount = 0;
  139. }
  140. }
  141. FluidContainerItemSkillConfig::FluidContainerItemSkillConfig()
  142. : ReferenceCounter(),
  143. targetFilter(0),
  144. staminaCost(0.5f),
  145. staminaCostDevider(0.9f),
  146. staminaCostDeviderPerLevel(1.f),
  147. cooldownTicks(10),
  148. xpGain(0.5f)
  149. {}
  150. FluidContainerItemSkillConfig::~FluidContainerItemSkillConfig()
  151. {
  152. if (targetFilter) targetFilter->release();
  153. }
  154. void FluidContainerItemSkillConfig::setTargetFilter(BlockFilter* targetFilter)
  155. {
  156. this->targetFilter = targetFilter;
  157. }
  158. BlockFilter* FluidContainerItemSkillConfig::zTargetFilter() const
  159. {
  160. return targetFilter;
  161. }
  162. void FluidContainerItemSkillConfig::setStaminaCost(float staminaCost)
  163. {
  164. this->staminaCost = staminaCost;
  165. }
  166. float FluidContainerItemSkillConfig::getStaminaCost() const
  167. {
  168. return staminaCost;
  169. }
  170. void FluidContainerItemSkillConfig::setStaminaCostDevider(
  171. float staminaCostDevider)
  172. {
  173. this->staminaCostDevider = staminaCostDevider;
  174. }
  175. float FluidContainerItemSkillConfig::getStaminaCostDevider() const
  176. {
  177. return staminaCostDevider;
  178. }
  179. void FluidContainerItemSkillConfig::setStaminaCostDeviderPerLevel(
  180. float staminaCostDeviderPerLevel)
  181. {
  182. this->staminaCostDeviderPerLevel = staminaCostDeviderPerLevel;
  183. }
  184. float FluidContainerItemSkillConfig::getStaminaCostDeviderPerLevel() const
  185. {
  186. return staminaCostDeviderPerLevel;
  187. }
  188. void FluidContainerItemSkillConfig::setCooldownTicks(int cooldownTicks)
  189. {
  190. this->cooldownTicks = cooldownTicks;
  191. }
  192. int FluidContainerItemSkillConfig::getCooldownTicks() const
  193. {
  194. return cooldownTicks;
  195. }
  196. void FluidContainerItemSkillConfig::setXpGain(float xpGain)
  197. {
  198. this->xpGain = xpGain;
  199. }
  200. float FluidContainerItemSkillConfig::getXpGain() const
  201. {
  202. return xpGain;
  203. }
  204. FluidContainerItemSkillConfigFactory::FluidContainerItemSkillConfigFactory()
  205. : TypeFactory()
  206. {}
  207. FluidContainerItemSkillConfig*
  208. FluidContainerItemSkillConfigFactory::createValue(
  209. Framework::JSON::JSONObject* zJson) const
  210. {
  211. return new FluidContainerItemSkillConfig();
  212. }
  213. void FluidContainerItemSkillConfigFactory::fromJson(
  214. FluidContainerItemSkillConfig* zResult,
  215. Framework::JSON::JSONObject* zJson) const
  216. {
  217. zResult->setTargetFilter(
  218. Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
  219. zJson->asObject()->zValue("targetFilter")));
  220. zResult->setStaminaCost((float)zJson->asObject()
  221. ->zValue("staminaCost")
  222. ->asNumber()
  223. ->getNumber());
  224. zResult->setStaminaCostDevider((float)zJson->asObject()
  225. ->zValue("staminaCostDevider")
  226. ->asNumber()
  227. ->getNumber());
  228. zResult->setStaminaCostDeviderPerLevel(
  229. (float)zJson->asObject()
  230. ->zValue("staminaCostDeviderPerLevel")
  231. ->asNumber()
  232. ->getNumber());
  233. zResult->setCooldownTicks((int)zJson->asObject()
  234. ->zValue("cooldownTicks")
  235. ->asNumber()
  236. ->getNumber());
  237. zResult->setXpGain(
  238. (float)zJson->asObject()->zValue("xpGain")->asNumber()->getNumber());
  239. }
  240. void FluidContainerItemSkillConfigFactory::toJson(
  241. FluidContainerItemSkillConfig* zObject,
  242. Framework::JSON::JSONObject* zResult) const
  243. {
  244. zResult->addValue("targetFilter",
  245. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zTargetFilter()));
  246. zResult->addValue("staminaCost",
  247. new Framework::JSON::JSONNumber(zObject->getStaminaCost()));
  248. zResult->addValue("staminaCostDevider",
  249. new Framework::JSON::JSONNumber(zObject->getStaminaCostDevider()));
  250. zResult->addValue("staminaCostDeviderPerLevel",
  251. new Framework::JSON::JSONNumber(
  252. zObject->getStaminaCostDeviderPerLevel()));
  253. zResult->addValue("cooldownTicks",
  254. new Framework::JSON::JSONNumber(zObject->getCooldownTicks()));
  255. zResult->addValue(
  256. "xpGain", new Framework::JSON::JSONNumber(zObject->getXpGain()));
  257. }
  258. JSONObjectValidationBuilder*
  259. FluidContainerItemSkillConfigFactory::addToValidator(
  260. JSONObjectValidationBuilder* builder) const
  261. {
  262. return builder
  263. ->withRequiredAttribute("targetFilter",
  264. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
  265. ->withRequiredNumber("staminaCost")
  266. ->whichIsGreaterOrEqual(0.0)
  267. ->withDefault(0.5)
  268. ->finishNumber()
  269. ->withRequiredNumber("staminaCostDevider")
  270. ->whichIsGreaterOrEqual(0.0)
  271. ->withDefault(0.9)
  272. ->finishNumber()
  273. ->withRequiredNumber("staminaCostDeviderPerLevel")
  274. ->whichIsGreaterOrEqual(0.0)
  275. ->withDefault(0.1)
  276. ->finishNumber()
  277. ->withRequiredNumber("cooldownTicks")
  278. ->whichIsGreaterOrEqual(0)
  279. ->withDefault(10)
  280. ->finishNumber()
  281. ->withRequiredNumber("xpGain")
  282. ->whichIsGreaterOrEqual(0.0)
  283. ->withDefault(0.5)
  284. ->finishNumber();
  285. }
  286. FluidContainerItemSkill::FluidContainerItemSkill()
  287. : ItemSkill(),
  288. cooldownTicks(0),
  289. invalidUseConfig(0)
  290. {}
  291. FluidContainerItemSkill::~FluidContainerItemSkill()
  292. {
  293. if (invalidUseConfig) invalidUseConfig->release();
  294. }
  295. bool FluidContainerItemSkill::use(
  296. Entity* zActor, Item* zUsedItem, Block* zTarget)
  297. {
  298. if (cooldownTicks)
  299. {
  300. cooldownTicks--;
  301. return false;
  302. }
  303. FluidContainerItem* usedItem = dynamic_cast<FluidContainerItem*>(zUsedItem);
  304. FluidBlock* fluidBlock = dynamic_cast<FluidBlock*>(zTarget);
  305. const FluidContainerItemType* usedItemType
  306. = dynamic_cast<const FluidContainerItemType*>(usedItem->zItemType());
  307. FluidContainerItemSkillConfig* usedConfig = 0;
  308. bool invalid = false;
  309. if (!zTarget->zBlockType()->isFluid() || zTarget->getHP() <= 0)
  310. {
  311. usedConfig = invalidUseConfig;
  312. invalid = true;
  313. }
  314. else if (!usedItem)
  315. {
  316. usedConfig = invalidUseConfig;
  317. invalid = true;
  318. }
  319. else if (!fluidBlock || fluidBlock->getDistanceToSource())
  320. {
  321. usedConfig = invalidUseConfig;
  322. invalid = true;
  323. }
  324. else if (!usedItemType)
  325. {
  326. usedConfig = invalidUseConfig;
  327. invalid = true;
  328. }
  329. else if (usedItem->getAmount() + 1000 <= usedItemType->getMaxFluidAmount())
  330. {
  331. usedConfig = invalidUseConfig;
  332. invalid = true;
  333. }
  334. else if (usedItem->getFluidTypeId()
  335. && usedItem->getFluidTypeId() != fluidBlock->zBlockType()->getId())
  336. {
  337. usedConfig = invalidUseConfig;
  338. invalid = true;
  339. }
  340. if (!usedConfig)
  341. {
  342. for (FluidContainerItemSkillConfig* config : configs)
  343. {
  344. if (config->zTargetFilter()->test(zTarget))
  345. {
  346. usedConfig = config;
  347. break;
  348. }
  349. }
  350. }
  351. if (!usedConfig)
  352. {
  353. usedConfig = invalidUseConfig;
  354. invalid = true;
  355. }
  356. float staminaCost = usedConfig->getStaminaCost();
  357. float staminaCostDevider
  358. = usedConfig->getStaminaCostDevider()
  359. + usedConfig->getStaminaCostDeviderPerLevel() * getLevel();
  360. if (staminaCostDevider)
  361. {
  362. staminaCost /= staminaCostDevider;
  363. }
  364. if (zActor->getStamina() < staminaCost)
  365. {
  366. return false;
  367. }
  368. zActor->setStamina(zActor->getStamina() - staminaCost);
  369. if (!invalid
  370. && usedItem->getAmount() + 1000 <= usedItemType->getMaxFluidAmount())
  371. {
  372. usedItem->setFluidTypeId(fluidBlock->zBlockType()->getId());
  373. usedItem->setAmount(usedItem->getAmount() + 1000);
  374. zTarget->setHP(0);
  375. }
  376. this->cooldownTicks = usedConfig->getCooldownTicks();
  377. setXp(getXp() + usedConfig->getXpGain());
  378. return true;
  379. }
  380. bool FluidContainerItemSkill::use(
  381. Entity* zActor, Item* zUsedItem, Entity* zTarget)
  382. {
  383. // TODO: get milk from cows and something else from other mobs
  384. return false;
  385. }
  386. void FluidContainerItemSkill::setInvalidUseConfig(
  387. FluidContainerItemSkillConfig* invalidUseConfig)
  388. {
  389. if (this->invalidUseConfig) this->invalidUseConfig->release();
  390. this->invalidUseConfig = invalidUseConfig;
  391. }
  392. FluidContainerItemSkillConfig*
  393. FluidContainerItemSkill::zInvalidUseConfig() const
  394. {
  395. return invalidUseConfig;
  396. }
  397. void FluidContainerItemSkill::addConfig(FluidContainerItemSkillConfig* config)
  398. {
  399. configs.add(config);
  400. }
  401. const Framework::RCArray<FluidContainerItemSkillConfig>&
  402. FluidContainerItemSkill::zConfigs() const
  403. {
  404. return configs;
  405. }
  406. FluidContainerItemSkillFactory::FluidContainerItemSkillFactory()
  407. : ItemSkillFactoryBase()
  408. {}
  409. FluidContainerItemSkill* FluidContainerItemSkillFactory::createValue(
  410. Framework::JSON::JSONObject* zJson) const
  411. {
  412. return new FluidContainerItemSkill();
  413. }
  414. void FluidContainerItemSkillFactory::fromJson(
  415. FluidContainerItemSkill* zResult, Framework::JSON::JSONObject* zJson) const
  416. {
  417. for (Framework::JSON::JSONValue* configValue :
  418. *zJson->zValue("configs")->asArray())
  419. {
  420. zResult->addConfig(Game::INSTANCE->zTypeRegistry()
  421. ->fromJson<FluidContainerItemSkillConfig>(
  422. configValue->asObject()));
  423. }
  424. FluidContainerItemSkillConfig* invalidConfig
  425. = new FluidContainerItemSkillConfig();
  426. invalidConfig->setStaminaCost(
  427. (float)zJson->zValue("invalidUseStaminaCost")->asNumber()->getNumber());
  428. invalidConfig->setStaminaCostDevider(
  429. (float)zJson->zValue("invalidUseStaminaCostDevider")
  430. ->asNumber()
  431. ->getNumber());
  432. invalidConfig->setStaminaCostDeviderPerLevel(
  433. (float)zJson->zValue("invalidUseStaminaCostDeviderPerLevel")
  434. ->asNumber()
  435. ->getNumber());
  436. invalidConfig->setCooldownTicks(
  437. (int)zJson->zValue("invalidUseCooldownTicks")->asNumber()->getNumber());
  438. zResult->setInvalidUseConfig(invalidConfig);
  439. ItemSkillFactoryBase::fromJson(zResult, zJson);
  440. }
  441. void FluidContainerItemSkillFactory::toJson(FluidContainerItemSkill* zObject,
  442. Framework::JSON::JSONObject* zResult) const
  443. {
  444. zResult->addValue("invalidUseStaminaCost",
  445. new Framework::JSON::JSONNumber(
  446. zObject->zInvalidUseConfig()->getStaminaCost()));
  447. zResult->addValue("invalidUseStaminaCostDevider",
  448. new Framework::JSON::JSONNumber(
  449. zObject->zInvalidUseConfig()->getStaminaCostDevider()));
  450. zResult->addValue("invalidUseStaminaCostDeviderPerLevel",
  451. new Framework::JSON::JSONNumber(
  452. zObject->zInvalidUseConfig()->getStaminaCostDeviderPerLevel()));
  453. zResult->addValue("invalidUseCooldownTicks",
  454. new Framework::JSON::JSONNumber(
  455. zObject->zInvalidUseConfig()->getCooldownTicks()));
  456. Framework::JSON::JSONArray* configs = new Framework::JSON::JSONArray();
  457. for (FluidContainerItemSkillConfig* config : zObject->zConfigs())
  458. {
  459. configs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(config));
  460. }
  461. zResult->addValue("configs", configs);
  462. ItemSkillFactoryBase::toJson(zObject, zResult);
  463. }
  464. JSONObjectValidationBuilder* FluidContainerItemSkillFactory::addToValidator(
  465. JSONObjectValidationBuilder* builder) const
  466. {
  467. return builder->withRequiredNumber("invalidUseStaminaCost")
  468. ->whichIsGreaterOrEqual(0.0)
  469. ->withDefault(0.5)
  470. ->finishNumber()
  471. ->withRequiredNumber("invalidUseStaminaCostDevider")
  472. ->whichIsGreaterOrEqual(0.0)
  473. ->withDefault(0.9)
  474. ->finishNumber()
  475. ->withRequiredNumber("sinvalidUseStaminaCostDeviderPerLevel")
  476. ->whichIsGreaterOrEqual(0.0)
  477. ->withDefault(0.1)
  478. ->finishNumber()
  479. ->withRequiredNumber("invalidUseCooldownTicks")
  480. ->whichIsGreaterOrEqual(0)
  481. ->withDefault(10)
  482. ->finishNumber()
  483. ->withRequiredArray("configs")
  484. ->addAcceptedTypeInArray(
  485. Game::INSTANCE->zTypeRegistry()
  486. ->getValidator<FluidContainerItemSkillConfig>())
  487. ->finishArray();
  488. }
  489. Framework::Text FluidContainerItemSkillFactory::getTypeToken() const
  490. {
  491. return "fluidGathering";
  492. }
  493. FluidContainerItemType::FluidContainerItemType()
  494. : ItemType(),
  495. maxFluidAmount(1000),
  496. itemSkillConfig(0),
  497. levelUpRule(0)
  498. {}
  499. FluidContainerItemType::~FluidContainerItemType()
  500. {
  501. if (itemSkillConfig) itemSkillConfig->release();
  502. if (levelUpRule) levelUpRule->release();
  503. }
  504. void FluidContainerItemType::loadSuperItem(
  505. Item* zItem, Framework::StreamReader* zReader) const
  506. {
  507. ItemType::loadSuperItem(zItem, zReader);
  508. FluidContainerItem* item = dynamic_cast<FluidContainerItem*>(zItem);
  509. if (item)
  510. {
  511. zReader->lese((char*)&item->fluidTypeId, 4);
  512. zReader->lese((char*)&item->fluidAmount, 4);
  513. }
  514. else
  515. {
  516. std::cout << "ERROR: FluidContainerItemType::loadSuperItem: "
  517. "zItem is not a FluidContainerItem\n";
  518. }
  519. }
  520. void FluidContainerItemType::saveSuperItem(
  521. const Item* zItem, Framework::StreamWriter* zWriter) const
  522. {
  523. ItemType::saveSuperItem(zItem, zWriter);
  524. const FluidContainerItem* item
  525. = dynamic_cast<const FluidContainerItem*>(zItem);
  526. if (item)
  527. {
  528. zWriter->schreibe((char*)&item->fluidTypeId, 4);
  529. zWriter->schreibe((char*)&item->fluidAmount, 4);
  530. }
  531. else
  532. {
  533. std::cout << "ERROR: FluidContainerItemType::saveSuperItem: "
  534. "zItem is not a FluidContainerItem\n";
  535. }
  536. }
  537. Item* FluidContainerItemType::createItem() const
  538. {
  539. Item* result = new FluidContainerItem(getId(), getName());
  540. return result;
  541. }
  542. ItemSkill* FluidContainerItemType::createDefaultItemSkill() const
  543. {
  544. return Game::INSTANCE->zTypeRegistry()->fromJson<ItemSkill>(
  545. itemSkillConfig);
  546. }
  547. void FluidContainerItemType::levelUpItemSkill(ItemSkill* zSkill) const
  548. {
  549. levelUpRule->applyOn(zSkill);
  550. }
  551. void FluidContainerItemType::setItemAttribute(
  552. Item* zItem, Framework::Text name, Framework::JSON::JSONValue* zValue) const
  553. {
  554. FluidContainerItem* item = dynamic_cast<FluidContainerItem*>(zItem);
  555. if (!item)
  556. {
  557. std::cout << "ERROR: FluidContainerItemType::setItemAttribute: "
  558. "zItem is not a FluidContainerItem\n";
  559. return;
  560. }
  561. if (name.istGleich("fluidType"))
  562. {
  563. if (zValue->getType() == Framework::JSON::JSONType::STRING)
  564. {
  565. int id = ItemType::getTypeId(zValue->asString()->getString());
  566. if (id)
  567. {
  568. item->fluidTypeId = id;
  569. }
  570. else
  571. {
  572. std::cout << "ERROR: FluidContainerItemType::setItemAttribute: "
  573. "'fluidType' is not a valid type name\n";
  574. }
  575. }
  576. else
  577. {
  578. std::cout << "ERROR: FluidContainerItemType::setItemAttribute: "
  579. "'fluidType' is not a string or string\n";
  580. }
  581. }
  582. else if (name.istGleich("fluidAmount"))
  583. {
  584. if (zValue->getType() == Framework::JSON::JSONType::NUMBER)
  585. {
  586. item->fluidAmount = (int)zValue->asNumber()->getNumber();
  587. }
  588. else
  589. {
  590. std::cout << "ERROR: FluidContainerItemType::setItemAttribute: "
  591. "'fluidAmount' is not a number\n";
  592. }
  593. }
  594. else
  595. {
  596. ItemType::setItemAttribute(zItem, name, zValue);
  597. }
  598. }
  599. void FluidContainerItemType::addItemAttributes(
  600. Item* zItem, Framework::JSON::JSONObject* zItemObjet) const
  601. {
  602. FluidContainerItem* item = dynamic_cast<FluidContainerItem*>(zItem);
  603. if (!item)
  604. {
  605. std::cout << "ERROR: FluidContainerItemType::addItemAttributes: "
  606. "zItem is not a FluidContainerItem\n";
  607. return;
  608. }
  609. ItemType::addItemAttributes(zItem, zItemObjet);
  610. if (item->fluidTypeId)
  611. {
  612. zItemObjet->addValue("fluidType",
  613. new Framework::JSON::JSONString(
  614. Game::INSTANCE->zItemType(item->fluidTypeId)->getName()));
  615. zItemObjet->addValue(
  616. "fluidAmount", new Framework::JSON::JSONNumber(item->fluidAmount));
  617. }
  618. }
  619. void FluidContainerItemType::setItemSkillConfig(
  620. Framework::JSON::JSONObject* itemSkillConfig)
  621. {
  622. if (this->itemSkillConfig) this->itemSkillConfig->release();
  623. this->itemSkillConfig = itemSkillConfig;
  624. }
  625. Framework::JSON::JSONObject* FluidContainerItemType::zItemSkillConfig() const
  626. {
  627. return itemSkillConfig;
  628. }
  629. void FluidContainerItemType::setLevelUpRule(ItemSkillLevelUpRule* levelUpRule)
  630. {
  631. if (this->levelUpRule) this->levelUpRule->release();
  632. this->levelUpRule = levelUpRule;
  633. }
  634. ItemSkillLevelUpRule* FluidContainerItemType::zLevelUpRule() const
  635. {
  636. return levelUpRule;
  637. }
  638. void FluidContainerItemType::setMaxFluidAmount(int maxFluidAmount)
  639. {
  640. this->maxFluidAmount = maxFluidAmount;
  641. }
  642. int FluidContainerItemType::getMaxFluidAmount() const
  643. {
  644. return maxFluidAmount;
  645. }
  646. FluidContainerItemTypeFactory::FluidContainerItemTypeFactory()
  647. : ItemTypeFactoryBase()
  648. {}
  649. FluidContainerItemType* FluidContainerItemTypeFactory::createValue(
  650. Framework::JSON::JSONObject* zJson) const
  651. {
  652. return new FluidContainerItemType();
  653. }
  654. void FluidContainerItemTypeFactory::fromJson(
  655. FluidContainerItemType* zResult, Framework::JSON::JSONObject* zJson) const
  656. {
  657. zResult->setItemSkillConfig(zJson->getValue("itemSkill")->asObject());
  658. zResult->setLevelUpRule(
  659. Game::INSTANCE->zTypeRegistry()->fromJson<ItemSkillLevelUpRule>(
  660. zJson->zValue("levelUpRule")));
  661. zResult->setMaxFluidAmount(
  662. (int)zJson->zValue("maxFluidAmount")->asNumber()->getNumber());
  663. ItemTypeFactoryBase::fromJson(zResult, zJson);
  664. }
  665. void FluidContainerItemTypeFactory::toJson(
  666. FluidContainerItemType* zObject, Framework::JSON::JSONObject* zResult) const
  667. {
  668. zResult->addValue("levelUpRule",
  669. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zLevelUpRule()));
  670. zResult->addValue("itemSkill",
  671. dynamic_cast<Framework::JSON::JSONObject*>(
  672. zObject->zItemSkillConfig()->getThis()));
  673. zResult->addValue("maxFluidAmount",
  674. new Framework::JSON::JSONNumber(zObject->getMaxFluidAmount()));
  675. ItemTypeFactoryBase::toJson(zObject, zResult);
  676. }
  677. JSONObjectValidationBuilder* FluidContainerItemTypeFactory::addToValidator(
  678. JSONObjectValidationBuilder* builder) const
  679. {
  680. return ItemTypeFactoryBase::addToValidator(
  681. builder
  682. ->withRequiredAttribute("levelUpRule",
  683. Game::INSTANCE->zTypeRegistry()
  684. ->getValidator<ItemSkillLevelUpRule>())
  685. ->withRequiredAttribute("itemSkill",
  686. Game::INSTANCE->zTypeRegistry()->getValidator<ItemSkill>())
  687. ->withRequiredNumber("maxFluidAmount")
  688. ->whichIsGreaterThen(0)
  689. ->withDefault(1000.0)
  690. ->finishNumber());
  691. }
  692. Framework::Text FluidContainerItemTypeFactory::getTypeToken() const
  693. {
  694. return "fluidContainer";
  695. }