FluidContainer.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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. : ObjectTypeFactory()
  207. {}
  208. FluidContainerItemSkillConfig* FluidContainerItemSkillConfigFactory::fromJson(
  209. Framework::JSON::JSONObject* zJson) const
  210. {
  211. FluidContainerItemSkillConfig* result = new FluidContainerItemSkillConfig();
  212. result->setTargetFilter(
  213. Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
  214. zJson->asObject()->zValue("targetFilter")));
  215. result->setStaminaCost((float)zJson->asObject()
  216. ->zValue("staminaCost")
  217. ->asNumber()
  218. ->getNumber());
  219. result->setStaminaCostDevider((float)zJson->asObject()
  220. ->zValue("staminaCostDevider")
  221. ->asNumber()
  222. ->getNumber());
  223. result->setStaminaCostDeviderPerLevel(
  224. (float)zJson->asObject()
  225. ->zValue("staminaCostDeviderPerLevel")
  226. ->asNumber()
  227. ->getNumber());
  228. result->setCooldownTicks((int)zJson->asObject()
  229. ->zValue("cooldownTicks")
  230. ->asNumber()
  231. ->getNumber());
  232. result->setXpGain(
  233. (float)zJson->asObject()->zValue("xpGain")->asNumber()->getNumber());
  234. return result;
  235. }
  236. Framework::JSON::JSONObject* FluidContainerItemSkillConfigFactory::toJsonObject(
  237. FluidContainerItemSkillConfig* zObject) const
  238. {
  239. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  240. result->addValue("targetFilter",
  241. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zTargetFilter()));
  242. result->addValue("staminaCost",
  243. new Framework::JSON::JSONNumber(zObject->getStaminaCost()));
  244. result->addValue("staminaCostDevider",
  245. new Framework::JSON::JSONNumber(zObject->getStaminaCostDevider()));
  246. result->addValue("staminaCostDeviderPerLevel",
  247. new Framework::JSON::JSONNumber(
  248. zObject->getStaminaCostDeviderPerLevel()));
  249. result->addValue("cooldownTicks",
  250. new Framework::JSON::JSONNumber(zObject->getCooldownTicks()));
  251. result->addValue(
  252. "xpGain", new Framework::JSON::JSONNumber(zObject->getXpGain()));
  253. return result;
  254. }
  255. JSONObjectValidationBuilder*
  256. FluidContainerItemSkillConfigFactory::addToValidator(
  257. JSONObjectValidationBuilder* builder) const
  258. {
  259. return builder
  260. ->withRequiredAttribute("targetFilter",
  261. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
  262. ->withRequiredNumber("staminaCost")
  263. ->whichIsGreaterOrEqual(0.0)
  264. ->withDefault(0.5)
  265. ->finishNumber()
  266. ->withRequiredNumber("staminaCostDevider")
  267. ->whichIsGreaterOrEqual(0.0)
  268. ->withDefault(0.9)
  269. ->finishNumber()
  270. ->withRequiredNumber("staminaCostDeviderPerLevel")
  271. ->whichIsGreaterOrEqual(0.0)
  272. ->withDefault(0.1)
  273. ->finishNumber()
  274. ->withRequiredNumber("cooldownTicks")
  275. ->whichIsGreaterOrEqual(0)
  276. ->withDefault(10)
  277. ->finishNumber()
  278. ->withRequiredNumber("xpGain")
  279. ->whichIsGreaterOrEqual(0.0)
  280. ->withDefault(0.5)
  281. ->finishNumber();
  282. }
  283. FluidContainerItemSkill::FluidContainerItemSkill()
  284. : ItemSkill(),
  285. cooldownTicks(0),
  286. invalidUseConfig(0)
  287. {}
  288. FluidContainerItemSkill::~FluidContainerItemSkill()
  289. {
  290. if (invalidUseConfig) invalidUseConfig->release();
  291. }
  292. bool FluidContainerItemSkill::use(
  293. Entity* zActor, Item* zUsedItem, Block* zTarget)
  294. {
  295. if (cooldownTicks)
  296. {
  297. cooldownTicks--;
  298. return false;
  299. }
  300. FluidContainerItem* usedItem = dynamic_cast<FluidContainerItem*>(zUsedItem);
  301. FluidBlock* fluidBlock = dynamic_cast<FluidBlock*>(zTarget);
  302. const FluidContainerItemType* usedItemType
  303. = dynamic_cast<const FluidContainerItemType*>(usedItem->zItemType());
  304. FluidContainerItemSkillConfig* usedConfig = 0;
  305. bool invalid = false;
  306. if (!zTarget->zBlockType()->isFluid() || zTarget->getHP() <= 0)
  307. {
  308. usedConfig = invalidUseConfig;
  309. invalid = true;
  310. }
  311. else if (!usedItem)
  312. {
  313. usedConfig = invalidUseConfig;
  314. invalid = true;
  315. }
  316. else if (!fluidBlock || fluidBlock->getDistanceToSource())
  317. {
  318. usedConfig = invalidUseConfig;
  319. invalid = true;
  320. }
  321. else if (!usedItemType)
  322. {
  323. usedConfig = invalidUseConfig;
  324. invalid = true;
  325. }
  326. else if (usedItem->getAmount() + 1000 > usedItemType->getMaxFluidAmount())
  327. {
  328. usedConfig = invalidUseConfig;
  329. invalid = true;
  330. }
  331. else if (usedItem->getFluidTypeId()
  332. && usedItem->getFluidTypeId() != fluidBlock->zBlockType()->getId())
  333. {
  334. usedConfig = invalidUseConfig;
  335. invalid = true;
  336. }
  337. if (!usedConfig)
  338. {
  339. for (FluidContainerItemSkillConfig* config : configs)
  340. {
  341. if (config->zTargetFilter()->test(zTarget))
  342. {
  343. usedConfig = config;
  344. break;
  345. }
  346. }
  347. }
  348. if (!usedConfig)
  349. {
  350. usedConfig = invalidUseConfig;
  351. invalid = true;
  352. }
  353. float staminaCost = usedConfig->getStaminaCost();
  354. float staminaCostDevider
  355. = usedConfig->getStaminaCostDevider()
  356. + usedConfig->getStaminaCostDeviderPerLevel() * getLevel();
  357. if (staminaCostDevider)
  358. {
  359. staminaCost /= staminaCostDevider;
  360. }
  361. if (zActor->getStamina() < staminaCost)
  362. {
  363. return false;
  364. }
  365. zActor->setStamina(zActor->getStamina() - staminaCost);
  366. if (!invalid
  367. && usedItem->getAmount() + 1000 <= usedItemType->getMaxFluidAmount())
  368. {
  369. usedItem->setFluidTypeId(fluidBlock->zBlockType()->getId());
  370. usedItem->setAmount(usedItem->getAmount() + 1000);
  371. zTarget->setHP(0);
  372. }
  373. this->cooldownTicks = usedConfig->getCooldownTicks();
  374. setXp(getXp() + usedConfig->getXpGain());
  375. return true;
  376. }
  377. bool FluidContainerItemSkill::use(
  378. Entity* zActor, Item* zUsedItem, Entity* zTarget)
  379. {
  380. // TODO: get milk from cows and something else from other mobs
  381. return false;
  382. }
  383. void FluidContainerItemSkill::setInvalidUseConfig(
  384. FluidContainerItemSkillConfig* invalidUseConfig)
  385. {
  386. if (this->invalidUseConfig) this->invalidUseConfig->release();
  387. this->invalidUseConfig = invalidUseConfig;
  388. }
  389. FluidContainerItemSkillConfig*
  390. FluidContainerItemSkill::zInvalidUseConfig() const
  391. {
  392. return invalidUseConfig;
  393. }
  394. void FluidContainerItemSkill::addConfig(FluidContainerItemSkillConfig* config)
  395. {
  396. configs.add(config);
  397. }
  398. const Framework::RCArray<FluidContainerItemSkillConfig>&
  399. FluidContainerItemSkill::zConfigs() const
  400. {
  401. return configs;
  402. }
  403. FluidContainerItemSkillFactory::FluidContainerItemSkillFactory()
  404. : ItemSkillFactoryBase()
  405. {}
  406. FluidContainerItemSkill* FluidContainerItemSkillFactory::createValue(
  407. Framework::JSON::JSONObject* zJson) const
  408. {
  409. return new FluidContainerItemSkill();
  410. }
  411. FluidContainerItemSkill* FluidContainerItemSkillFactory::fromJson(
  412. Framework::JSON::JSONObject* zJson) const
  413. {
  414. FluidContainerItemSkill* result = ItemSkillFactoryBase::fromJson(zJson);
  415. for (Framework::JSON::JSONValue* configValue :
  416. *zJson->zValue("configs")->asArray())
  417. {
  418. result->addConfig(Game::INSTANCE->zTypeRegistry()
  419. ->fromJson<FluidContainerItemSkillConfig>(
  420. configValue->asObject()));
  421. }
  422. FluidContainerItemSkillConfig* invalidConfig
  423. = new FluidContainerItemSkillConfig();
  424. invalidConfig->setStaminaCost(
  425. (float)zJson->zValue("invalidUseStaminaCost")->asNumber()->getNumber());
  426. invalidConfig->setStaminaCostDevider(
  427. (float)zJson->zValue("invalidUseStaminaCostDevider")
  428. ->asNumber()
  429. ->getNumber());
  430. invalidConfig->setStaminaCostDeviderPerLevel(
  431. (float)zJson->zValue("invalidUseStaminaCostDeviderPerLevel")
  432. ->asNumber()
  433. ->getNumber());
  434. invalidConfig->setCooldownTicks(
  435. (int)zJson->zValue("invalidUseCooldownTicks")->asNumber()->getNumber());
  436. result->setInvalidUseConfig(invalidConfig);
  437. return result;
  438. }
  439. Framework::JSON::JSONObject* FluidContainerItemSkillFactory::toJsonObject(
  440. FluidContainerItemSkill* zObject) const
  441. {
  442. Framework::JSON::JSONObject* result
  443. = ItemSkillFactoryBase::toJsonObject(zObject);
  444. result->addValue("invalidUseStaminaCost",
  445. new Framework::JSON::JSONNumber(
  446. zObject->zInvalidUseConfig()->getStaminaCost()));
  447. result->addValue("invalidUseStaminaCostDevider",
  448. new Framework::JSON::JSONNumber(
  449. zObject->zInvalidUseConfig()->getStaminaCostDevider()));
  450. result->addValue("invalidUseStaminaCostDeviderPerLevel",
  451. new Framework::JSON::JSONNumber(
  452. zObject->zInvalidUseConfig()->getStaminaCostDeviderPerLevel()));
  453. result->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. result->addValue("configs", configs);
  462. return result;
  463. }
  464. JSONObjectValidationBuilder* FluidContainerItemSkillFactory::addToValidator(
  465. JSONObjectValidationBuilder* builder) const
  466. {
  467. return ItemSkillFactoryBase::addToValidator(builder)
  468. ->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("invalidUseStaminaCostDeviderPerLevel")
  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. const char* 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::AbstractType::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::AbstractType::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. FluidContainerItemType* FluidContainerItemTypeFactory::fromJson(
  661. Framework::JSON::JSONObject* zJson) const
  662. {
  663. FluidContainerItemType* result = ItemTypeFactoryBase::fromJson(zJson);
  664. result->setItemSkillConfig(zJson->getValue("itemSkill")->asObject());
  665. result->setLevelUpRule(
  666. Game::INSTANCE->zTypeRegistry()->fromJson<ItemSkillLevelUpRule>(
  667. zJson->zValue("levelUpRule")));
  668. result->setMaxFluidAmount(
  669. (int)zJson->zValue("maxFluidAmount")->asNumber()->getNumber());
  670. return result;
  671. }
  672. Framework::JSON::JSONObject* FluidContainerItemTypeFactory::toJsonObject(
  673. FluidContainerItemType* zObject) const
  674. {
  675. Framework::JSON::JSONObject* result
  676. = ItemTypeFactoryBase::toJsonObject(zObject);
  677. result->addValue("levelUpRule",
  678. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zLevelUpRule()));
  679. result->addValue("itemSkill",
  680. dynamic_cast<Framework::JSON::JSONObject*>(
  681. zObject->zItemSkillConfig()->getThis()));
  682. result->addValue("maxFluidAmount",
  683. new Framework::JSON::JSONNumber(zObject->getMaxFluidAmount()));
  684. return result;
  685. }
  686. JSONObjectValidationBuilder* FluidContainerItemTypeFactory::addToValidator(
  687. JSONObjectValidationBuilder* builder) const
  688. {
  689. return ItemTypeFactoryBase::addToValidator(
  690. builder
  691. ->withRequiredAttribute("levelUpRule",
  692. Game::INSTANCE->zTypeRegistry()
  693. ->getValidator<ItemSkillLevelUpRule>())
  694. ->withRequiredAttribute("itemSkill",
  695. Game::INSTANCE->zTypeRegistry()->getValidator<ItemSkill>())
  696. ->withRequiredNumber("maxFluidAmount")
  697. ->whichIsGreaterThen(0)
  698. ->withDefault(1000.0)
  699. ->finishNumber());
  700. }
  701. const char* FluidContainerItemTypeFactory::getTypeToken() const
  702. {
  703. return "fluidContainer";
  704. }