FluidContainer.cpp 23 KB

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