FluidContainer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include "FluidContainer.h"
  2. #include <TextFeld.h>
  3. #include "FluidBlock.h"
  4. #include "Game.h"
  5. FluidContainerItem::FluidContainerItem(int itemTypeId, const char* name)
  6. : Item(itemTypeId, name),
  7. fluidTypeId(0),
  8. fluidAmount(0)
  9. {
  10. placeable = 1;
  11. usable = 1;
  12. eatable = 1;
  13. }
  14. const BlockType* FluidContainerItem::zPlacedBlockType() const
  15. {
  16. return fluidTypeId && fluidAmount >= 1000 ? StaticRegistry<BlockType>::INSTANCE.zElement(fluidTypeId) : 0;
  17. }
  18. bool FluidContainerItem::canBeStackedWith(const Item* zItem) const
  19. {
  20. const FluidContainerItem* other
  21. = dynamic_cast<const FluidContainerItem*>(zItem);
  22. if (!other) return false;
  23. return Item::canBeStackedWith(zItem) && other->fluidTypeId == fluidTypeId
  24. && other->fluidAmount == fluidAmount;
  25. }
  26. bool FluidContainerItem::canBePlacedAt(
  27. const int dimensionId, Framework::Vec3<int> worldPos) const
  28. {
  29. if (fluidAmount >= 1000)
  30. {
  31. Dimension* dim = Game::INSTANCE->zDimension(dimensionId);
  32. if (dim)
  33. {
  34. const Block* block = dim->zBlockOrDefault(worldPos);
  35. if (block)
  36. {
  37. if (block->zBlockType()->getId() == BlockTypeEnum::AIR)
  38. return true;
  39. if (block->zBlockType()->getId() == fluidTypeId)
  40. {
  41. const FluidBlock* fluidBlock
  42. = dynamic_cast<const FluidBlock*>(block);
  43. return fluidBlock && fluidBlock->getDistanceToSource() > 0;
  44. }
  45. }
  46. }
  47. }
  48. return false;
  49. }
  50. void FluidContainerItem::onPlaced()
  51. {
  52. setAmount(fluidAmount - 1000);
  53. }
  54. Framework::Text FluidContainerItem::getTooltipUIML() const
  55. {
  56. Framework::Text uiml = "<tip><text width=\"auto\" height=\"auto\">";
  57. uiml.append() << getName();
  58. if (fluidTypeId != 0)
  59. {
  60. uiml.append() << "\nFluid: "
  61. << StaticRegistry<BlockType>::INSTANCE
  62. .zElement(fluidTypeId)
  63. ->getName()
  64. << "\nAmount: " << fluidAmount << " L";
  65. }
  66. else
  67. {
  68. uiml.append() << "\nEmpty";
  69. }
  70. uiml.append() << "</text></tip>";
  71. return uiml;
  72. }
  73. bool FluidContainerItem::applyFoodEffects(Entity* zTarget)
  74. {
  75. if (fluidTypeId)
  76. {
  77. const FluidBlockType* fluidType = dynamic_cast<const FluidBlockType*>(
  78. StaticRegistry<BlockType>::INSTANCE.zElement(fluidTypeId));
  79. if (fluidType && fluidType->getFoodEffect())
  80. {
  81. return fluidType->getFoodEffect()(this, zTarget);
  82. }
  83. }
  84. return false;
  85. }
  86. bool FluidContainerItem::canApplyFoodEffectsFully(Entity* zTarget) const
  87. {
  88. return false;
  89. }
  90. int FluidContainerItem::getAmount() const
  91. {
  92. return fluidAmount;
  93. }
  94. void FluidContainerItem::setAmount(int amount)
  95. {
  96. fluidAmount = amount;
  97. if (!fluidAmount)
  98. {
  99. fluidTypeId = 0;
  100. }
  101. }
  102. int FluidContainerItem::getFluidTypeId() const
  103. {
  104. return fluidTypeId;
  105. }
  106. void FluidContainerItem::setFluidTypeId(int fluidTypeId)
  107. {
  108. this->fluidTypeId = fluidTypeId;
  109. if (!fluidTypeId)
  110. {
  111. fluidAmount = 0;
  112. }
  113. }
  114. FluidContainerItemSkill::FluidContainerItemSkill(int itemTypeId)
  115. : ItemSkill(itemTypeId),
  116. level(1),
  117. xp(0.f),
  118. maxXP(10.f)
  119. {}
  120. bool FluidContainerItemSkill::use(
  121. Entity* zActor, Item* zUsedItem, Block* zTarget)
  122. {
  123. FluidContainerItem* usedItem = dynamic_cast<FluidContainerItem*>(zUsedItem);
  124. if (usedItem)
  125. {
  126. if (zTarget->zBlockType()->isFluid() && zTarget->getHP() > 0)
  127. {
  128. FluidBlock* fluidBlock = dynamic_cast<FluidBlock*>(zTarget);
  129. if (fluidBlock)
  130. {
  131. if (!usedItem->getFluidTypeId()
  132. || usedItem->getFluidTypeId()
  133. == fluidBlock->zBlockType()->getId())
  134. {
  135. const FluidContainerItemType* usedItemType
  136. = dynamic_cast<const FluidContainerItemType*>(
  137. usedItem->zItemType());
  138. if (usedItemType)
  139. {
  140. if (usedItem->getAmount() + 1000
  141. <= usedItemType->getMaxFluidAmount())
  142. {
  143. usedItem->setFluidTypeId(
  144. fluidBlock->zBlockType()->getId());
  145. usedItem->setAmount(usedItem->getAmount() + 1000);
  146. zTarget->setHP(0);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. return false;
  154. }
  155. bool FluidContainerItemSkill::use(
  156. Entity* zActor, Item* zUsedItem, Entity* zTarget)
  157. {
  158. // TODO: get milk from cows and something else from other mobs
  159. return false;
  160. }
  161. FluidContainerItemSkillLevelUpRule::FluidContainerItemSkillLevelUpRule()
  162. : ItemSkillLevelUpRule()
  163. {}
  164. void FluidContainerItemSkillLevelUpRule::applyOn(ItemSkill* zSkill)
  165. {
  166. FluidContainerItemSkill* skill
  167. = dynamic_cast<FluidContainerItemSkill*>(zSkill);
  168. if (skill->xp >= skill->maxXP)
  169. {
  170. skill->level++;
  171. skill->xp = 0;
  172. skill->maxXP = skill->maxXP * 2;
  173. }
  174. }
  175. void FluidContainerItemType::loadSuperItem(
  176. Item* zItem, Framework::StreamReader* zReader) const
  177. {
  178. ItemType::loadSuperItem(zItem, zReader);
  179. FluidContainerItem* item = dynamic_cast<FluidContainerItem*>(zItem);
  180. if (item)
  181. {
  182. zReader->lese((char*)&item->fluidTypeId, 4);
  183. zReader->lese((char*)&item->fluidAmount, 4);
  184. }
  185. else
  186. {
  187. std::cout << "ERROR: FluidContainerItemType::loadSuperItem: "
  188. "zItem is not a FluidContainerItem\n";
  189. }
  190. }
  191. void FluidContainerItemType::saveSuperItem(
  192. const Item* zItem, Framework::StreamWriter* zWriter) const
  193. {
  194. ItemType::saveSuperItem(zItem, zWriter);
  195. const FluidContainerItem* item
  196. = dynamic_cast<const FluidContainerItem*>(zItem);
  197. if (item)
  198. {
  199. zWriter->schreibe((char*)&item->fluidTypeId, 4);
  200. zWriter->schreibe((char*)&item->fluidAmount, 4);
  201. }
  202. else
  203. {
  204. std::cout << "ERROR: FluidContainerItemType::saveSuperItem: "
  205. "zItem is not a FluidContainerItem\n";
  206. }
  207. }
  208. void FluidContainerItemType::loadSuperItemSkill(
  209. ItemSkill* zSkill, Framework::StreamReader* zReader) const
  210. {
  211. ItemType::loadSuperItemSkill(zSkill, zReader);
  212. FluidContainerItemSkill* skill
  213. = dynamic_cast<FluidContainerItemSkill*>(zSkill);
  214. if (skill)
  215. {
  216. zReader->lese((char*)&skill->level, 4);
  217. zReader->lese((char*)&skill->xp, 4);
  218. zReader->lese((char*)&skill->maxXP, 4);
  219. }
  220. else
  221. {
  222. std::cout << "ERROR: FluidContainerItemType::loadSuperItemSkill: "
  223. "zSkill is not a FluidContainerItemSkill\n";
  224. }
  225. }
  226. void FluidContainerItemType::saveSuperItemSkill(
  227. const ItemSkill* zSkill, Framework::StreamWriter* zWriter) const
  228. {
  229. ItemType::saveSuperItemSkill(zSkill, zWriter);
  230. const FluidContainerItemSkill* skill
  231. = dynamic_cast<const FluidContainerItemSkill*>(zSkill);
  232. if (skill)
  233. {
  234. zWriter->schreibe((char*)&skill->level, 4);
  235. zWriter->schreibe((char*)&skill->xp, 4);
  236. zWriter->schreibe((char*)&skill->maxXP, 4);
  237. }
  238. else
  239. {
  240. std::cout << "ERROR: FluidContainerItemType::saveSuperItemSkill: "
  241. "zSkill is not a FluidContainerItemSkill\n";
  242. }
  243. }
  244. FluidContainerItemType::FluidContainerItemType(
  245. int typeId, const char* name, ModelInfo model)
  246. : ItemType(
  247. typeId, name, new FluidContainerItemSkillLevelUpRule(), 0, model),
  248. maxFluidAmount(1000)
  249. {}
  250. Item* FluidContainerItemType::createItem() const
  251. {
  252. Item* result = new FluidContainerItem(getId(), getName());
  253. return result;
  254. }
  255. ItemSkill* FluidContainerItemType::createDefaultItemSkill() const
  256. {
  257. return new FluidContainerItemSkill(getId());
  258. }
  259. void FluidContainerItemType::setItemAttribute(
  260. Item* zItem, Framework::Text name, Framework::JSON::JSONValue* zValue) const
  261. {
  262. FluidContainerItem* item = dynamic_cast<FluidContainerItem*>(zItem);
  263. if (!item)
  264. {
  265. std::cout << "ERROR: FluidContainerItemType::setItemAttribute: "
  266. "zItem is not a FluidContainerItem\n";
  267. return;
  268. }
  269. if (name.istGleich("fluidType"))
  270. {
  271. if (zValue->getType() == Framework::JSON::JSONType::STRING)
  272. {
  273. int id = ItemType::getTypeId(zValue->asString()->getString());
  274. if (id)
  275. {
  276. item->fluidTypeId = id;
  277. }
  278. else
  279. {
  280. std::cout << "ERROR: FluidContainerItemType::setItemAttribute: "
  281. "'fluidType' is not a valid type name\n";
  282. }
  283. }
  284. else
  285. {
  286. std::cout << "ERROR: FluidContainerItemType::setItemAttribute: "
  287. "'fluidType' is not a string or string\n";
  288. }
  289. }
  290. else if (name.istGleich("fluidAmount"))
  291. {
  292. if (zValue->getType() == Framework::JSON::JSONType::NUMBER)
  293. {
  294. item->fluidAmount = (int)zValue->asNumber()->getNumber();
  295. }
  296. else
  297. {
  298. std::cout << "ERROR: FluidContainerItemType::setItemAttribute: "
  299. "'fluidAmount' is not a number\n";
  300. }
  301. }
  302. else
  303. {
  304. ItemType::setItemAttribute(zItem, name, zValue);
  305. }
  306. }
  307. void FluidContainerItemType::addItemAttributes(
  308. Item* zItem, Framework::JSON::JSONObject* zItemObjet) const
  309. {
  310. FluidContainerItem* item = dynamic_cast<FluidContainerItem*>(zItem);
  311. if (!item)
  312. {
  313. std::cout << "ERROR: FluidContainerItemType::addItemAttributes: "
  314. "zItem is not a FluidContainerItem\n";
  315. return;
  316. }
  317. ItemType::addItemAttributes(zItem, zItemObjet);
  318. if (item->fluidTypeId)
  319. {
  320. zItemObjet->addValue("fluidType",
  321. new Framework::JSON::JSONString(
  322. StaticRegistry<ItemType>::INSTANCE.zElement(item->fluidTypeId)
  323. ->getName()));
  324. zItemObjet->addValue(
  325. "fluidAmount", new Framework::JSON::JSONNumber(item->fluidAmount));
  326. }
  327. }
  328. int FluidContainerItemType::getMaxFluidAmount() const
  329. {
  330. return maxFluidAmount;
  331. }
  332. FluidContainerItemType* FluidContainerItemType::setMaxFluidAmount(
  333. int maxFluidAmount)
  334. {
  335. this->maxFluidAmount = maxFluidAmount;
  336. return this;
  337. }