BasicTool.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. #include "BasicTool.h"
  2. #include <numeric>
  3. #include "Block.h"
  4. #include "Dimension.h"
  5. #include "Entity.h"
  6. #include "Game.h"
  7. #undef max
  8. XPBasedLevelUpRule::XPBasedLevelUpRule()
  9. : ItemSkillLevelUpRule(),
  10. xpIncrease(0.0),
  11. xpMultiplier(2.0),
  12. levelIncrease(1.0),
  13. levelMultiplier(1.0),
  14. maxLevel(std::numeric_limits<float>::max())
  15. {}
  16. void XPBasedLevelUpRule::applyOn(ItemSkill* zSkill)
  17. {
  18. if (zSkill->getXp() >= zSkill->getMaxXp())
  19. {
  20. zSkill->setXp(zSkill->getXp() - zSkill->getMaxXp());
  21. zSkill->setLevel(MIN(
  22. (zSkill->getLevel() + levelIncrease) * levelMultiplier, maxLevel));
  23. zSkill->setMaxXp((zSkill->getMaxXp() + xpIncrease) * xpMultiplier);
  24. }
  25. }
  26. void XPBasedLevelUpRule::setXpIncrease(float xpIncrease)
  27. {
  28. this->xpIncrease = xpIncrease;
  29. }
  30. float XPBasedLevelUpRule::getXpIncrease() const
  31. {
  32. return xpIncrease;
  33. }
  34. void XPBasedLevelUpRule::setXpMultiplier(float xpMultiplier)
  35. {
  36. this->xpMultiplier = xpMultiplier;
  37. }
  38. float XPBasedLevelUpRule::getXpMultiplier() const
  39. {
  40. return xpMultiplier;
  41. }
  42. void XPBasedLevelUpRule::setLevelIncrease(float levelIncrease)
  43. {
  44. this->levelIncrease = levelIncrease;
  45. }
  46. float XPBasedLevelUpRule::getLevelIncrease() const
  47. {
  48. return levelIncrease;
  49. }
  50. void XPBasedLevelUpRule::setLevelMultiplier(float levelMultiplier)
  51. {
  52. this->levelMultiplier = levelMultiplier;
  53. }
  54. float XPBasedLevelUpRule::getLevelMultiplier() const
  55. {
  56. return levelMultiplier;
  57. }
  58. void XPBasedLevelUpRule::setMaxLevel(float maxLevel)
  59. {
  60. this->maxLevel = maxLevel;
  61. }
  62. float XPBasedLevelUpRule::getMaxLevel() const
  63. {
  64. return maxLevel;
  65. }
  66. XPBasedLevelUpRuleFactory::XPBasedLevelUpRuleFactory()
  67. : SubTypeFactory()
  68. {}
  69. XPBasedLevelUpRule* XPBasedLevelUpRuleFactory::createValue(
  70. Framework::JSON::JSONObject* zJson) const
  71. {
  72. return new XPBasedLevelUpRule();
  73. }
  74. void XPBasedLevelUpRuleFactory::fromJson(
  75. XPBasedLevelUpRule* zResult, Framework::JSON::JSONObject* zJson) const
  76. {
  77. if (zJson->hasValue("maxLevel"))
  78. {
  79. zResult->setMaxLevel(
  80. (float)zJson->zValue("maxLevel")->asNumber()->getNumber());
  81. }
  82. zResult->setXpIncrease(
  83. (float)zJson->zValue("xpIncrease")->asNumber()->getNumber());
  84. zResult->setXpMultiplier(
  85. (float)zJson->zValue("xpMultiplier")->asNumber()->getNumber());
  86. zResult->setLevelIncrease(
  87. (float)zJson->zValue("levelIncrease")->asNumber()->getNumber());
  88. zResult->setLevelMultiplier(
  89. (float)zJson->zValue("levelMultiplier")->asNumber()->getNumber());
  90. }
  91. void XPBasedLevelUpRuleFactory::toJson(
  92. XPBasedLevelUpRule* zObject, Framework::JSON::JSONObject* zResult) const
  93. {
  94. zResult->addValue("xpIncrease",
  95. new Framework::JSON::JSONNumber(zObject->getXpIncrease()));
  96. zResult->addValue("xpMultiplier",
  97. new Framework::JSON::JSONNumber(zObject->getXpMultiplier()));
  98. zResult->addValue("levelIncrease",
  99. new Framework::JSON::JSONNumber(zObject->getLevelIncrease()));
  100. zResult->addValue("levelMultiplier",
  101. new Framework::JSON::JSONNumber(zObject->getLevelMultiplier()));
  102. zResult->addValue(
  103. "maxLevel", new Framework::JSON::JSONNumber(zObject->getMaxLevel()));
  104. }
  105. JSONObjectValidationBuilder* XPBasedLevelUpRuleFactory::addToValidator(
  106. JSONObjectValidationBuilder* builder) const
  107. {
  108. return builder->withRequiredNumber("xpIncrease")
  109. ->whichIsGreaterOrEqual(0.0)
  110. ->withDefault(0.0)
  111. ->finishNumber()
  112. ->withRequiredNumber("xpMultiplier")
  113. ->whichIsGreaterThen(0.0)
  114. ->withDefault(2.0)
  115. ->finishNumber()
  116. ->withRequiredNumber("levelIncrease")
  117. ->whichIsGreaterOrEqual(0.0)
  118. ->withDefault(1.0)
  119. ->finishNumber()
  120. ->withRequiredNumber("levelMultiplier")
  121. ->whichIsGreaterThen(0.0)
  122. ->withDefault(1.0)
  123. ->finishNumber()
  124. ->withRequiredNumber("maxLevel")
  125. ->whichIsOptional()
  126. ->finishNumber();
  127. }
  128. Framework::Text XPBasedLevelUpRuleFactory::getTypeToken() const
  129. {
  130. return "xpBased";
  131. }
  132. BasicToolItem::BasicToolItem(
  133. int itemTypeId, Framework::Text name, float maxHp, float maxDurability)
  134. : Item(itemTypeId, name),
  135. headMaterialHardness(0.0),
  136. rodMaterialHardness(0.0),
  137. handleMaterialHardness(0.0)
  138. {
  139. hp = maxHp;
  140. this->maxHp = maxHp;
  141. durability = maxDurability;
  142. this->maxDurability = maxDurability;
  143. eatable = 0;
  144. placeable = 0;
  145. equippable = 0;
  146. solid = 1;
  147. usable = 1;
  148. }
  149. void BasicToolItem::setHeadMaterialHardness(float hardness)
  150. {
  151. headMaterialHardness = hardness;
  152. }
  153. void BasicToolItem::setRodMaterialHardness(float hardness)
  154. {
  155. rodMaterialHardness = hardness;
  156. }
  157. void BasicToolItem::setHandleMaterialHardness(float hardness)
  158. {
  159. handleMaterialHardness = hardness;
  160. }
  161. float BasicToolItem::getHeadMaterialHardness() const
  162. {
  163. return headMaterialHardness;
  164. }
  165. float BasicToolItem::getRodMaterialHardness() const
  166. {
  167. return rodMaterialHardness;
  168. }
  169. float BasicToolItem::getHandleMaterialHardness() const
  170. {
  171. return handleMaterialHardness;
  172. }
  173. BasicToolItemType::BasicToolItemType()
  174. : ItemType(),
  175. headMaterialHardness(1.f),
  176. rodMaterialHardness(1.f),
  177. handleMaterialHardness(1.f),
  178. baseDurability(10.f),
  179. baseDurabilityMultiplier(1.f),
  180. headMaterialDurability(10.f),
  181. headMaterialDurabilityMultiplier(0.f),
  182. rodMaterialDurability(10.f),
  183. rodMaterialDurabilityMultiplier(0.f),
  184. handleMaterialDurability(10.f),
  185. handleMaterialDurabilityMultiplier(0.0),
  186. levelUpRule(0),
  187. brokenItemTypeName(),
  188. itemSkillConfigJson(0),
  189. brokenItemTypeId(-1)
  190. {}
  191. BasicToolItemType::~BasicToolItemType()
  192. {
  193. if (levelUpRule) levelUpRule->release();
  194. if (itemSkillConfigJson) itemSkillConfigJson->release();
  195. }
  196. void BasicToolItemType::loadSuperItem(
  197. Item* zItem, Framework::StreamReader* zReader) const
  198. {
  199. BasicToolItem* item = dynamic_cast<BasicToolItem*>(zItem);
  200. float data;
  201. zReader->lese((char*)&data, 4);
  202. item->setHeadMaterialHardness(data);
  203. zReader->lese((char*)&data, 4);
  204. item->setRodMaterialHardness(data);
  205. zReader->lese((char*)&data, 4);
  206. item->setHandleMaterialHardness(data);
  207. ItemType::loadSuperItem(item, zReader);
  208. }
  209. void BasicToolItemType::saveSuperItem(
  210. const Item* zItem, Framework::StreamWriter* zWriter) const
  211. {
  212. const BasicToolItem* item = dynamic_cast<const BasicToolItem*>(zItem);
  213. float data = item->getHeadMaterialHardness();
  214. zWriter->schreibe((char*)&data, 4);
  215. data = item->getRodMaterialHardness();
  216. zWriter->schreibe((char*)&data, 4);
  217. data = item->getHandleMaterialHardness();
  218. zWriter->schreibe((char*)&data, 4);
  219. ItemType::saveSuperItem(item, zWriter);
  220. }
  221. bool BasicToolItemType::initialize(Game* zGame)
  222. {
  223. brokenItemTypeId = zGame->getItemTypeId(brokenItemTypeName);
  224. return brokenItemTypeId >= 0 && ItemType::initialize(zGame);
  225. }
  226. const ItemType* BasicToolItemType::zBrokenItemType() const
  227. {
  228. return Game::INSTANCE->zItemType(brokenItemTypeId);
  229. }
  230. Item* BasicToolItemType::createItem() const
  231. {
  232. BasicToolItem* item = new BasicToolItem(getId(),
  233. getName(),
  234. 1.f,
  235. (baseDurability + headMaterialDurability * headMaterialHardness
  236. + rodMaterialDurability * rodMaterialHardness
  237. + handleMaterialDurability * handleMaterialHardness)
  238. * (baseDurabilityMultiplier
  239. + headMaterialDurabilityMultiplier * headMaterialHardness
  240. + rodMaterialDurabilityMultiplier * rodMaterialHardness
  241. + handleMaterialDurabilityMultiplier * handleMaterialHardness));
  242. item->setHandleMaterialHardness(headMaterialHardness);
  243. item->setRodMaterialHardness(rodMaterialHardness);
  244. item->setHandleMaterialHardness(handleMaterialHardness);
  245. return item;
  246. }
  247. void BasicToolItemType::levelUpItemSkill(ItemSkill* zSkill) const
  248. {
  249. levelUpRule->applyOn(zSkill);
  250. }
  251. void BasicToolItemType::setItemAttribute(
  252. Item* zItem, Framework::Text name, Framework::JSON::JSONValue* zValue) const
  253. {
  254. BasicToolItem* item = dynamic_cast<BasicToolItem*>(zItem);
  255. if (name.istGleich("headMaterialHardness")
  256. && zValue->getType() == Framework::JSON::JSONType::NUMBER)
  257. {
  258. item->setHeadMaterialHardness((float)zValue->asNumber()->getNumber());
  259. }
  260. else if (name.istGleich("rodMaterialHardness")
  261. && zValue->getType() == Framework::JSON::JSONType::NUMBER)
  262. {
  263. item->setRodMaterialHardness((float)zValue->asNumber()->getNumber());
  264. }
  265. else if (name.istGleich("handleMaterialHardness")
  266. && zValue->getType() == Framework::JSON::JSONType::NUMBER)
  267. {
  268. item->setHandleMaterialHardness((float)zValue->asNumber()->getNumber());
  269. }
  270. else
  271. {
  272. ItemType::setItemAttribute(item, name, zValue);
  273. }
  274. item->setMaxDurability(
  275. (baseDurability
  276. + headMaterialDurability * item->getHandleMaterialHardness()
  277. + rodMaterialDurability * item->getRodMaterialHardness()
  278. + handleMaterialDurability * item->getHandleMaterialHardness())
  279. * (baseDurabilityMultiplier
  280. + headMaterialDurabilityMultiplier * item->getHeadMaterialHardness()
  281. + rodMaterialDurabilityMultiplier * item->getRodMaterialHardness()
  282. + handleMaterialDurabilityMultiplier
  283. * item->getHandleMaterialHardness()));
  284. }
  285. void BasicToolItemType::addItemAttributes(
  286. Item* zItem, Framework::JSON::JSONObject* zItemObjet) const
  287. {
  288. BasicToolItem* item = dynamic_cast<BasicToolItem*>(zItem);
  289. zItemObjet->addValue("headMaterialHardness",
  290. new Framework::JSON::JSONNumber(item->getHeadMaterialHardness()));
  291. zItemObjet->addValue("rodMaterialHardness",
  292. new Framework::JSON::JSONNumber(item->getRodMaterialHardness()));
  293. zItemObjet->addValue("handleMaterialHardness",
  294. new Framework::JSON::JSONNumber(item->getHandleMaterialHardness()));
  295. ItemType::addItemAttributes(item, zItemObjet);
  296. }
  297. ItemSkill* BasicToolItemType::createDefaultItemSkill() const
  298. {
  299. return Game::INSTANCE->zTypeRegistry()->fromJson<ItemSkill>(
  300. itemSkillConfigJson);
  301. }
  302. void BasicToolItemType::setBrokenItemTypeName(
  303. Framework::Text brokenItemTypeName)
  304. {
  305. this->brokenItemTypeName = brokenItemTypeName;
  306. }
  307. Framework::Text BasicToolItemType::getBrokenItemTypeName() const
  308. {
  309. return brokenItemTypeName;
  310. }
  311. void BasicToolItemType::setHeadMaterialHardness(float hardness)
  312. {
  313. headMaterialHardness = hardness;
  314. }
  315. float BasicToolItemType::getHeadMaterialHardness() const
  316. {
  317. return headMaterialHardness;
  318. }
  319. void BasicToolItemType::setRodMaterialHardness(float hardness)
  320. {
  321. rodMaterialHardness = hardness;
  322. }
  323. float BasicToolItemType::getRodMaterialHardness() const
  324. {
  325. return rodMaterialHardness;
  326. }
  327. void BasicToolItemType::setHandleMaterialHardness(float hardness)
  328. {
  329. handleMaterialHardness = hardness;
  330. }
  331. float BasicToolItemType::getHandleMaterialHardness() const
  332. {
  333. return handleMaterialHardness;
  334. }
  335. void BasicToolItemType::setBaseDurability(float durability)
  336. {
  337. baseDurability = durability;
  338. }
  339. float BasicToolItemType::getBaseDurablility() const
  340. {
  341. return baseDurability;
  342. }
  343. void BasicToolItemType::setBaseDurabilityMultiplier(float multiplier)
  344. {
  345. baseDurabilityMultiplier = multiplier;
  346. }
  347. float BasicToolItemType::getBaseDurabilityMultiplier() const
  348. {
  349. return baseDurabilityMultiplier;
  350. }
  351. void BasicToolItemType::setHeadMaterialDurability(float durability)
  352. {
  353. headMaterialDurability = durability;
  354. }
  355. float BasicToolItemType::getHeadMaterialDurability() const
  356. {
  357. return headMaterialDurability;
  358. }
  359. void BasicToolItemType::setHeadMaterialDurabilityMultiplier(float multiplier)
  360. {
  361. headMaterialDurabilityMultiplier = multiplier;
  362. }
  363. float BasicToolItemType::getHeadMaterialDurabilityMultiplier() const
  364. {
  365. return headMaterialDurabilityMultiplier;
  366. }
  367. void BasicToolItemType::setRodMaterialDurability(float durability)
  368. {
  369. rodMaterialDurability = durability;
  370. }
  371. float BasicToolItemType::getRodMaterialDurability() const
  372. {
  373. return rodMaterialDurability;
  374. }
  375. void BasicToolItemType::setRodMaterialDurabilityMultiplier(float multiplier)
  376. {
  377. rodMaterialDurabilityMultiplier = multiplier;
  378. }
  379. float BasicToolItemType::getRodMaterialDurabilityMultiplier() const
  380. {
  381. return rodMaterialDurabilityMultiplier;
  382. }
  383. void BasicToolItemType::setHandleMaterialDurability(float durability)
  384. {
  385. handleMaterialDurability = durability;
  386. }
  387. float BasicToolItemType::getHandleMaterialDurability() const
  388. {
  389. return handleMaterialDurability;
  390. }
  391. void BasicToolItemType::setHandleMaterialDurabilityMultiplier(float multiplier)
  392. {
  393. handleMaterialDurabilityMultiplier = multiplier;
  394. }
  395. float BasicToolItemType::getHandleMaterialDurabilityMultiplier() const
  396. {
  397. return handleMaterialDurabilityMultiplier;
  398. }
  399. void BasicToolItemType::setLevelUpRule(ItemSkillLevelUpRule* rule)
  400. {
  401. if (levelUpRule) levelUpRule->release();
  402. levelUpRule = rule;
  403. }
  404. ItemSkillLevelUpRule* BasicToolItemType::zLevelUpRule() const
  405. {
  406. return levelUpRule;
  407. }
  408. void BasicToolItemType::setItemSkillConfigJson(
  409. Framework::JSON::JSONObject* zJson)
  410. {
  411. if (itemSkillConfigJson) itemSkillConfigJson->release();
  412. itemSkillConfigJson = zJson;
  413. }
  414. Framework::JSON::JSONObject* BasicToolItemType::getItemSkillConfigJson() const
  415. {
  416. return dynamic_cast<Framework::JSON::JSONObject*>(
  417. itemSkillConfigJson->getThis());
  418. }
  419. BasicToolItemTypeFactory::BasicToolItemTypeFactory()
  420. : ItemTypeFactoryBase()
  421. {}
  422. BasicToolItemType* BasicToolItemTypeFactory::createValue(
  423. Framework::JSON::JSONObject* zJson) const
  424. {
  425. return new BasicToolItemType();
  426. }
  427. void BasicToolItemTypeFactory::fromJson(
  428. BasicToolItemType* zResult, Framework::JSON::JSONObject* zJson) const
  429. {
  430. zResult->setHandleMaterialHardness(
  431. (float)zJson->zValue("headMaterialHardness")->asNumber()->getNumber());
  432. zResult->setRodMaterialHardness(
  433. (float)zJson->zValue("rodMaterialHardness")->asNumber()->getNumber());
  434. zResult->setHandleMaterialHardness(
  435. (float)zJson->zValue("handleMaterialHardness")
  436. ->asNumber()
  437. ->getNumber());
  438. zResult->setBaseDurability(
  439. (float)zJson->zValue("baseDurability")->asNumber()->getNumber());
  440. zResult->setBaseDurabilityMultiplier(
  441. (float)zJson->zValue("baseDurabilityMultiplier")
  442. ->asNumber()
  443. ->getNumber());
  444. zResult->setHeadMaterialDurability(
  445. (float)zJson->zValue("headMaterialDurability")
  446. ->asNumber()
  447. ->getNumber());
  448. zResult->setHeadMaterialDurabilityMultiplier(
  449. (float)zJson->zValue("headMaterialDurabilityMultiplier")
  450. ->asNumber()
  451. ->getNumber());
  452. zResult->setRodMaterialDurability(
  453. (float)zJson->zValue("rodMaterialDurability")->asNumber()->getNumber());
  454. zResult->setRodMaterialDurabilityMultiplier(
  455. (float)zJson->zValue("rodMaterialDurabilityMultiplier")
  456. ->asNumber()
  457. ->getNumber());
  458. zResult->setHandleMaterialDurability(
  459. (float)zJson->zValue("handleMaterialDurability")
  460. ->asNumber()
  461. ->getNumber());
  462. zResult->setHandleMaterialDurabilityMultiplier(
  463. (float)zJson->zValue("handleMaterialDurabilityMultiplier")
  464. ->asNumber()
  465. ->getNumber());
  466. zResult->setLevelUpRule(
  467. Game::INSTANCE->zTypeRegistry()->fromJson<ItemSkillLevelUpRule>(
  468. zJson->zValue("levelUpRule")));
  469. zResult->setBrokenItemTypeName(
  470. zJson->zValue("brokenItemTypeName")->asString()->getString());
  471. zResult->setItemSkillConfigJson(zJson->getValue("itemSkill")->asObject());
  472. ItemTypeFactoryBase::fromJson(zResult, zJson);
  473. }
  474. void BasicToolItemTypeFactory::toJson(
  475. BasicToolItemType* zObject, Framework::JSON::JSONObject* zResult) const
  476. {
  477. zResult->addValue("headMaterialHardness",
  478. new Framework::JSON::JSONNumber(zObject->getHeadMaterialHardness()));
  479. zResult->addValue("rodMaterialHardness",
  480. new Framework::JSON::JSONNumber(zObject->getRodMaterialHardness()));
  481. zResult->addValue("handleMaterialHardness",
  482. new Framework::JSON::JSONNumber(zObject->getHandleMaterialHardness()));
  483. zResult->addValue("baseDurability",
  484. new Framework::JSON::JSONNumber(zObject->getBaseDurablility()));
  485. zResult->addValue("baseDurabilityMultiplier",
  486. new Framework::JSON::JSONNumber(
  487. zObject->getBaseDurabilityMultiplier()));
  488. zResult->addValue("headMaterialDurability",
  489. new Framework::JSON::JSONNumber(zObject->getHeadMaterialDurability()));
  490. zResult->addValue("headMaterialDurabilityMultiplier",
  491. new Framework::JSON::JSONNumber(
  492. zObject->getHeadMaterialDurabilityMultiplier()));
  493. zResult->addValue("rodMaterialDurability",
  494. new Framework::JSON::JSONNumber(zObject->getRodMaterialDurability()));
  495. zResult->addValue("rodMaterialDurabilityMultiplier",
  496. new Framework::JSON::JSONNumber(
  497. zObject->getRodMaterialDurabilityMultiplier()));
  498. zResult->addValue("handleMaterialDurability",
  499. new Framework::JSON::JSONNumber(
  500. zObject->getHandleMaterialDurability()));
  501. zResult->addValue("handleMaterialDurabilityMultiplier",
  502. new Framework::JSON::JSONNumber(
  503. zObject->getHandleMaterialDurabilityMultiplier()));
  504. zResult->addValue("levelUpRule",
  505. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zLevelUpRule()));
  506. zResult->addValue("brokenItemTypeName",
  507. new Framework::JSON::JSONString(zObject->zBrokenItemType()->getName()));
  508. zResult->addValue("itemSkill", zObject->getItemSkillConfigJson());
  509. ItemTypeFactoryBase::toJson(zObject, zResult);
  510. }
  511. JSONObjectValidationBuilder* BasicToolItemTypeFactory::addToValidator(
  512. JSONObjectValidationBuilder* builder) const
  513. {
  514. return ItemTypeFactoryBase::addToValidator(
  515. builder->withRequiredNumber("headMaterialHardness")
  516. ->withDefault(1.0)
  517. ->whichIsGreaterOrEqual(0.0)
  518. ->finishNumber()
  519. ->withRequiredNumber("rodMaterialHardness")
  520. ->withDefault(1.0)
  521. ->whichIsGreaterOrEqual(0.0)
  522. ->finishNumber()
  523. ->withRequiredNumber("handleMaterialHardness")
  524. ->withDefault(1.0)
  525. ->whichIsGreaterOrEqual(0.0)
  526. ->finishNumber()
  527. ->withRequiredNumber("baseDurability")
  528. ->withDefault(10.0)
  529. ->whichIsGreaterOrEqual(0.0)
  530. ->finishNumber()
  531. ->withRequiredNumber("baseDurabilityMultiplier")
  532. ->withDefault(1.0)
  533. ->whichIsGreaterOrEqual(0.0)
  534. ->finishNumber()
  535. ->withRequiredNumber("headMaterialDurability")
  536. ->withDefault(10.0)
  537. ->whichIsGreaterOrEqual(0.0)
  538. ->finishNumber()
  539. ->withRequiredNumber("headMaterialDurabilityMultiplier")
  540. ->withDefault(0.0)
  541. ->whichIsGreaterOrEqual(0.0)
  542. ->finishNumber()
  543. ->withRequiredNumber("rodMaterialDurability")
  544. ->withDefault(10.0)
  545. ->whichIsGreaterOrEqual(0.0)
  546. ->finishNumber()
  547. ->withRequiredNumber("rodMaterialDurabilityMultiplier")
  548. ->withDefault(0.0)
  549. ->whichIsGreaterOrEqual(0.0)
  550. ->finishNumber()
  551. ->withRequiredNumber("handleMaterialDurability")
  552. ->withDefault(10.0)
  553. ->whichIsGreaterOrEqual(0.0)
  554. ->finishNumber()
  555. ->withRequiredNumber("handleMaterialDurabilityMultiplier")
  556. ->withDefault(0.0)
  557. ->whichIsGreaterOrEqual(0.0)
  558. ->finishNumber()
  559. ->withRequiredString("brokenItemTypeName")
  560. ->finishString()
  561. ->withRequiredAttribute("levelUpRule",
  562. Game::INSTANCE->zTypeRegistry()
  563. ->getValidator<ItemSkillLevelUpRule>())
  564. ->withRequiredAttribute("itemSkill",
  565. Game::INSTANCE->zTypeRegistry()->getValidator<ItemSkill>()));
  566. }
  567. Framework::Text BasicToolItemTypeFactory::getTypeToken() const
  568. {
  569. return "tool";
  570. }
  571. BlockReplaceItemSkillConfig::BlockReplaceItemSkillConfig()
  572. : ReferenceCounter(),
  573. targetBlockFilter(0),
  574. replacementBlockTypeId(-1),
  575. cooldownTicks(20),
  576. staminaCost(0.5f),
  577. staminaCostDevider(0.8f),
  578. additionalStaminaCostDeviderPerLevel(0.2f),
  579. durabilityCost(0.5f),
  580. durabilityCostDevider(0.89f),
  581. additionalDurabilityCostDeviderPerLevel(0.02f),
  582. xpGain(0.5f)
  583. {}
  584. BlockReplaceItemSkillConfig::~BlockReplaceItemSkillConfig()
  585. {
  586. if (targetBlockFilter) targetBlockFilter->release();
  587. }
  588. void BlockReplaceItemSkillConfig::setTargetBlockFilter(
  589. BlockFilter* targetBlockFilter)
  590. {
  591. this->targetBlockFilter = targetBlockFilter;
  592. }
  593. BlockFilter* BlockReplaceItemSkillConfig::zTargetBlockFilter() const
  594. {
  595. return targetBlockFilter;
  596. }
  597. void BlockReplaceItemSkillConfig::setReplacementBlockTypeId(
  598. int replacementBlockTypeId)
  599. {
  600. this->replacementBlockTypeId = replacementBlockTypeId;
  601. }
  602. int BlockReplaceItemSkillConfig::getReplacementBlockTypeId() const
  603. {
  604. return replacementBlockTypeId;
  605. }
  606. void BlockReplaceItemSkillConfig::setCooldownTicks(int cooldownTicks)
  607. {
  608. this->cooldownTicks = cooldownTicks;
  609. }
  610. int BlockReplaceItemSkillConfig::getCooldownTicks() const
  611. {
  612. return cooldownTicks;
  613. }
  614. void BlockReplaceItemSkillConfig::setStaminaCost(float staminaCost)
  615. {
  616. this->staminaCost = staminaCost;
  617. }
  618. float BlockReplaceItemSkillConfig::getStaminaCost() const
  619. {
  620. return staminaCost;
  621. }
  622. void BlockReplaceItemSkillConfig::setStaminaCostDevider(
  623. float staminaCostDevider)
  624. {
  625. this->staminaCostDevider = staminaCostDevider;
  626. }
  627. float BlockReplaceItemSkillConfig::getStaminaCostDevider() const
  628. {
  629. return staminaCostDevider;
  630. }
  631. void BlockReplaceItemSkillConfig::setAdditionalStaminaCostDeviderPerLevel(
  632. float additionalStaminaCostDeviderPerLevel)
  633. {
  634. this->additionalStaminaCostDeviderPerLevel
  635. = additionalStaminaCostDeviderPerLevel;
  636. }
  637. float BlockReplaceItemSkillConfig::getAdditionalStaminaCostDeviderPerLevel()
  638. const
  639. {
  640. return additionalStaminaCostDeviderPerLevel;
  641. }
  642. void BlockReplaceItemSkillConfig::setDurabilityCost(float durabilityCost)
  643. {
  644. this->durabilityCost = durabilityCost;
  645. }
  646. float BlockReplaceItemSkillConfig::getDurabilityCost() const
  647. {
  648. return durabilityCost;
  649. }
  650. void BlockReplaceItemSkillConfig::setDurabilityCostDevider(
  651. float durabilityCostDevider)
  652. {
  653. this->durabilityCostDevider = durabilityCostDevider;
  654. }
  655. float BlockReplaceItemSkillConfig::getDurabilityCostDevider() const
  656. {
  657. return durabilityCostDevider;
  658. }
  659. void BlockReplaceItemSkillConfig::setAdditionalDurabilityCostDeviderPerLevel(
  660. float additionalDurabilityCostDeviderPerLevel)
  661. {
  662. this->additionalDurabilityCostDeviderPerLevel
  663. = additionalDurabilityCostDeviderPerLevel;
  664. }
  665. float BlockReplaceItemSkillConfig::getAdditionalDurabilityCostDeviderPerLevel()
  666. const
  667. {
  668. return additionalDurabilityCostDeviderPerLevel;
  669. }
  670. void BlockReplaceItemSkillConfig::setXpGain(float xpGain)
  671. {
  672. this->xpGain = xpGain;
  673. }
  674. float BlockReplaceItemSkillConfig::getXpGain() const
  675. {
  676. return xpGain;
  677. }
  678. BlockReplaceItemSkillConfigFactory::BlockReplaceItemSkillConfigFactory()
  679. : TypeFactory()
  680. {}
  681. BlockReplaceItemSkillConfig* BlockReplaceItemSkillConfigFactory::createValue(
  682. Framework::JSON::JSONObject* zJson) const
  683. {
  684. return new BlockReplaceItemSkillConfig();
  685. }
  686. void BlockReplaceItemSkillConfigFactory::fromJson(
  687. BlockReplaceItemSkillConfig* zResult,
  688. Framework::JSON::JSONObject* zJson) const
  689. {
  690. zResult->setTargetBlockFilter(
  691. Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
  692. zJson->asObject()->zValue("targetFilter")));
  693. zResult->setCooldownTicks((int)zJson->asObject()
  694. ->zValue("cooldownTicks")
  695. ->asNumber()
  696. ->getNumber());
  697. zResult->setReplacementBlockTypeId(
  698. Game::INSTANCE->getBlockTypeId(zJson->asObject()
  699. ->zValue("replacementBlockType")
  700. ->asString()
  701. ->getString()));
  702. zResult->setCooldownTicks((int)zJson->asObject()
  703. ->zValue("cooldownTicks")
  704. ->asNumber()
  705. ->getNumber());
  706. zResult->setStaminaCost((float)zJson->asObject()
  707. ->zValue("staminaCost")
  708. ->asNumber()
  709. ->getNumber());
  710. zResult->setStaminaCostDevider((float)zJson->asObject()
  711. ->zValue("staminaCostDevider")
  712. ->asNumber()
  713. ->getNumber());
  714. zResult->setAdditionalStaminaCostDeviderPerLevel(
  715. (float)zJson->asObject()
  716. ->zValue("additionalStaminaCostDeviderPerLevel")
  717. ->asNumber()
  718. ->getNumber());
  719. zResult->setDurabilityCost((float)zJson->asObject()
  720. ->zValue("durabilityCost")
  721. ->asNumber()
  722. ->getNumber());
  723. zResult->setDurabilityCostDevider((float)zJson->asObject()
  724. ->zValue("durabilityCostDevider")
  725. ->asNumber()
  726. ->getNumber());
  727. zResult->setAdditionalDurabilityCostDeviderPerLevel(
  728. (float)zJson->asObject()
  729. ->zValue("additionalDurabilityCostDeviderPerLevel")
  730. ->asNumber()
  731. ->getNumber());
  732. zResult->setXpGain(
  733. (float)zJson->asObject()->zValue("xpGain")->asNumber()->getNumber());
  734. }
  735. void BlockReplaceItemSkillConfigFactory::toJson(
  736. BlockReplaceItemSkillConfig* zObject,
  737. Framework::JSON::JSONObject* zResult) const
  738. {
  739. zResult->addValue("targetFilter",
  740. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zTargetBlockFilter()));
  741. zResult->addValue("replacementBlockType",
  742. new Framework::JSON::JSONString(
  743. Game::INSTANCE->zBlockType(zObject->getReplacementBlockTypeId())
  744. ->getName()));
  745. zResult->addValue("cooldownTicks",
  746. new Framework::JSON::JSONNumber(zObject->getCooldownTicks()));
  747. zResult->addValue("staminaCost",
  748. new Framework::JSON::JSONNumber(zObject->getStaminaCost()));
  749. zResult->addValue("staminaCostDevider",
  750. new Framework::JSON::JSONNumber(zObject->getStaminaCostDevider()));
  751. zResult->addValue("additionalStaminaCostDeviderPerLevel",
  752. new Framework::JSON::JSONNumber(
  753. zObject->getAdditionalStaminaCostDeviderPerLevel()));
  754. zResult->addValue("durabilityCost",
  755. new Framework::JSON::JSONNumber(zObject->getDurabilityCost()));
  756. zResult->addValue("durabilityCostDevider",
  757. new Framework::JSON::JSONNumber(zObject->getDurabilityCostDevider()));
  758. zResult->addValue("additionalDurabilityCostDeviderPerLevel",
  759. new Framework::JSON::JSONNumber(
  760. zObject->getAdditionalDurabilityCostDeviderPerLevel()));
  761. zResult->addValue(
  762. "xpGain", new Framework::JSON::JSONNumber(zObject->getXpGain()));
  763. }
  764. JSONObjectValidationBuilder* BlockReplaceItemSkillConfigFactory::addToValidator(
  765. JSONObjectValidationBuilder* builder) const
  766. {
  767. return builder
  768. ->withRequiredAttribute("targetFilter",
  769. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
  770. ->withRequiredString("replacementBlockType")
  771. ->finishString()
  772. ->withRequiredNumber("cooldownTicks")
  773. ->whichIsGreaterOrEqual(0)
  774. ->withDefault(20)
  775. ->finishNumber()
  776. ->withRequiredNumber("staminaCost")
  777. ->whichIsGreaterOrEqual(0)
  778. ->withDefault(0.5)
  779. ->finishNumber()
  780. ->withRequiredNumber("staminaCostDevider")
  781. ->whichIsGreaterOrEqual(0)
  782. ->withDefault(0.8)
  783. ->finishNumber()
  784. ->withRequiredNumber("additionalStaminaCostDeviderPerLevel")
  785. ->whichIsGreaterOrEqual(0)
  786. ->withDefault(0.2)
  787. ->finishNumber()
  788. ->withRequiredNumber("durabilityCost")
  789. ->whichIsGreaterOrEqual(0)
  790. ->withDefault(0.5)
  791. ->finishNumber()
  792. ->withRequiredNumber("durabilityCostDevider")
  793. ->whichIsGreaterOrEqual(0)
  794. ->withDefault(0.98)
  795. ->finishNumber()
  796. ->withRequiredNumber("additionalDurabilityCostDeviderPerLevel")
  797. ->whichIsGreaterOrEqual(0)
  798. ->withDefault(0.02)
  799. ->finishNumber()
  800. ->withRequiredNumber("xpGain")
  801. ->whichIsGreaterOrEqual(0)
  802. ->withDefault(0.5)
  803. ->finishNumber();
  804. }
  805. BlockReplaceItemSkill::BlockReplaceItemSkill()
  806. : ItemSkill(),
  807. invalidUseConfig(0),
  808. configs(),
  809. cooldownTicks(20)
  810. {}
  811. BlockReplaceItemSkill::~BlockReplaceItemSkill()
  812. {
  813. if (invalidUseConfig) invalidUseConfig->release();
  814. }
  815. void BlockReplaceItemSkill::load(Framework::StreamReader* zReader)
  816. {
  817. zReader->lese((char*)&cooldownTicks, 4);
  818. ItemSkill::load(zReader);
  819. }
  820. void BlockReplaceItemSkill::save(Framework::StreamWriter* zWriter)
  821. {
  822. zWriter->schreibe((char*)&cooldownTicks, 4);
  823. ItemSkill::save(zWriter);
  824. }
  825. bool BlockReplaceItemSkill::use(Entity* zActor, Item* zUsedItem, Block* zTarget)
  826. {
  827. if (cooldownTicks > 0)
  828. {
  829. cooldownTicks--;
  830. return false;
  831. }
  832. BlockReplaceItemSkillConfig* usedConfig = 0;
  833. for (BlockReplaceItemSkillConfig* config : configs)
  834. {
  835. if (config->zTargetBlockFilter()->test(zTarget))
  836. {
  837. usedConfig = config;
  838. break;
  839. }
  840. }
  841. bool invalid = 0;
  842. if (usedConfig == 0)
  843. {
  844. usedConfig = invalidUseConfig;
  845. invalid = 1;
  846. }
  847. float staminaDevider
  848. = usedConfig->getStaminaCostDevider()
  849. + usedConfig->getAdditionalStaminaCostDeviderPerLevel() * getLevel();
  850. float staminaCost = usedConfig->getStaminaCost();
  851. if (staminaDevider != 0)
  852. {
  853. staminaCost /= staminaDevider;
  854. }
  855. if (zActor->getStamina() < staminaCost)
  856. {
  857. return false;
  858. }
  859. float durabilityDevider
  860. = usedConfig->getDurabilityCostDevider()
  861. + usedConfig->getAdditionalDurabilityCostDeviderPerLevel() * getLevel();
  862. float durabilityCost = usedConfig->getDurabilityCost();
  863. if (durabilityDevider != 0)
  864. {
  865. durabilityCost /= durabilityDevider;
  866. }
  867. zUsedItem->setDurability(zUsedItem->getDurability() - durabilityCost);
  868. zActor->setStamina(zActor->getStamina() - staminaCost);
  869. cooldownTicks = usedConfig->getCooldownTicks();
  870. setXp(getXp() + usedConfig->getXpGain());
  871. if (!invalid)
  872. {
  873. Framework::Vec3<int> pos = zTarget->getPos();
  874. int dimension = zTarget->getDimensionId();
  875. Game::INSTANCE->doLater([pos, dimension, usedConfig]() {
  876. Game::INSTANCE->zDimension(dimension)->placeBlock(
  877. pos, usedConfig->getReplacementBlockTypeId());
  878. });
  879. }
  880. return true;
  881. }
  882. bool BlockReplaceItemSkill::use(
  883. Entity* zActor, Item* zUsedItem, Entity* zTarget)
  884. {
  885. return false;
  886. }
  887. void BlockReplaceItemSkill::setInvalidUseConfig(
  888. BlockReplaceItemSkillConfig* config)
  889. {
  890. if (invalidUseConfig) invalidUseConfig->release();
  891. invalidUseConfig = config;
  892. }
  893. BlockReplaceItemSkillConfig* BlockReplaceItemSkill::zInvalidUseConfig() const
  894. {
  895. return invalidUseConfig;
  896. }
  897. void BlockReplaceItemSkill::addConfig(BlockReplaceItemSkillConfig* config)
  898. {
  899. configs.add(config);
  900. }
  901. const Framework::RCArray<BlockReplaceItemSkillConfig>&
  902. BlockReplaceItemSkill::getConfigs() const
  903. {
  904. return configs;
  905. }
  906. void BlockReplaceItemSkill::setCooldownTicks(int cooldownTicks)
  907. {
  908. this->cooldownTicks = cooldownTicks;
  909. }
  910. int BlockReplaceItemSkill::getCooldownTicks() const
  911. {
  912. return cooldownTicks;
  913. }
  914. BlockReplaceItemSkillFactory::BlockReplaceItemSkillFactory()
  915. : ItemSkillFactoryBase()
  916. {}
  917. BlockReplaceItemSkill* BlockReplaceItemSkillFactory::createValue(
  918. Framework::JSON::JSONObject* zJson) const
  919. {
  920. return new BlockReplaceItemSkill();
  921. }
  922. void BlockReplaceItemSkillFactory::fromJson(
  923. BlockReplaceItemSkill* zResult, Framework::JSON::JSONObject* zJson) const
  924. {
  925. BlockReplaceItemSkillConfig* invalidUseConfig
  926. = new BlockReplaceItemSkillConfig();
  927. invalidUseConfig->setStaminaCost(
  928. (float)zJson->zValue("invalidStaminaCost")->asNumber()->getNumber());
  929. invalidUseConfig->setStaminaCostDevider(
  930. (float)zJson->zValue("invalidStaminaCostDevider")
  931. ->asNumber()
  932. ->getNumber());
  933. invalidUseConfig->setAdditionalStaminaCostDeviderPerLevel(
  934. (float)zJson->zValue("invalidAdditionalStaminaCostDeviderPerLevel")
  935. ->asNumber()
  936. ->getNumber());
  937. invalidUseConfig->setDurabilityCost(
  938. (float)zJson->zValue("invalidDurabilityCost")->asNumber()->getNumber());
  939. invalidUseConfig->setDurabilityCostDevider(
  940. (float)zJson->zValue("invalidDurabilityCostDevider")
  941. ->asNumber()
  942. ->getNumber());
  943. invalidUseConfig->setAdditionalDurabilityCostDeviderPerLevel(
  944. (float)zJson->zValue("invalidAdditionalDurabilityCostDeviderPerLevel")
  945. ->asNumber()
  946. ->getNumber());
  947. invalidUseConfig->setCooldownTicks(
  948. (int)zJson->zValue("invalidCooldownTicks")->asNumber()->getNumber());
  949. invalidUseConfig->setXpGain(0.f);
  950. zResult->setInvalidUseConfig(invalidUseConfig);
  951. Framework::RCArray<BlockReplaceItemSkillConfig> configs;
  952. for (Framework::JSON::JSONValue* value :
  953. *zJson->zValue("configs")->asArray())
  954. {
  955. zResult->addConfig(Game::INSTANCE->zTypeRegistry()
  956. ->fromJson<BlockReplaceItemSkillConfig>(value));
  957. }
  958. ItemSkillFactoryBase::fromJson(zResult, zJson);
  959. }
  960. void BlockReplaceItemSkillFactory::toJson(
  961. BlockReplaceItemSkill* zObject, Framework::JSON::JSONObject* zResult) const
  962. {
  963. zResult->addValue("invalidStaminaCost",
  964. new Framework::JSON::JSONNumber(
  965. zObject->zInvalidUseConfig()->getStaminaCost()));
  966. zResult->addValue("invalidStaminaCostDevider",
  967. new Framework::JSON::JSONNumber(
  968. zObject->zInvalidUseConfig()->getStaminaCostDevider()));
  969. zResult->addValue("invalidAdditionalStaminaCostDeviderPerLevel",
  970. new Framework::JSON::JSONNumber(
  971. zObject->zInvalidUseConfig()
  972. ->getAdditionalStaminaCostDeviderPerLevel()));
  973. zResult->addValue("invalidDurabilityCost",
  974. new Framework::JSON::JSONNumber(
  975. zObject->zInvalidUseConfig()->getDurabilityCost()));
  976. zResult->addValue("invalidDurabilityCostDevider",
  977. new Framework::JSON::JSONNumber(
  978. zObject->zInvalidUseConfig()->getDurabilityCostDevider()));
  979. zResult->addValue("invalidAdditionalDurabilityCostDeviderPerLevel",
  980. new Framework::JSON::JSONNumber(
  981. zObject->zInvalidUseConfig()
  982. ->getAdditionalDurabilityCostDeviderPerLevel()));
  983. zResult->addValue("invalidCooldownTicks",
  984. new Framework::JSON::JSONNumber(
  985. zObject->zInvalidUseConfig()->getCooldownTicks()));
  986. Framework::JSON::JSONArray* configs = new Framework::JSON::JSONArray();
  987. for (BlockReplaceItemSkillConfig* config : zObject->getConfigs())
  988. {
  989. configs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(config));
  990. }
  991. zResult->addValue("configs", configs);
  992. ItemSkillFactoryBase::toJson(zObject, zResult);
  993. }
  994. JSONObjectValidationBuilder* BlockReplaceItemSkillFactory::addToValidator(
  995. JSONObjectValidationBuilder* builder) const
  996. {
  997. return ItemSkillFactoryBase::addToValidator(
  998. builder->withRequiredNumber("invalidStaminaCost")
  999. ->whichIsGreaterOrEqual(0.0)
  1000. ->withDefault(0.5)
  1001. ->finishNumber()
  1002. ->withRequiredNumber("invalidStaminaCostDevider")
  1003. ->whichIsGreaterOrEqual(0.0)
  1004. ->withDefault(0.8)
  1005. ->finishNumber()
  1006. ->withRequiredNumber("invalidAdditionalStaminaCostDeviderPerLevel")
  1007. ->whichIsGreaterOrEqual(0.0)
  1008. ->withDefault(0.2)
  1009. ->finishNumber()
  1010. ->withRequiredNumber("invalidDurabilityCost")
  1011. ->whichIsGreaterOrEqual(0.0)
  1012. ->withDefault(0.5)
  1013. ->finishNumber()
  1014. ->withRequiredNumber("invalidDurabilityCostDevider")
  1015. ->whichIsGreaterOrEqual(0.0)
  1016. ->withDefault(0.98)
  1017. ->finishNumber()
  1018. ->withRequiredNumber(
  1019. "invalidAdditionalDurabilityCostDeviderPerLevel")
  1020. ->whichIsGreaterOrEqual(0.0)
  1021. ->withDefault(0.02)
  1022. ->finishNumber()
  1023. ->withRequiredNumber("invalidCooldownTicks")
  1024. ->withDefault(20)
  1025. ->finishNumber()
  1026. ->withRequiredArray("configs")
  1027. ->addAcceptedTypeInArray(
  1028. Game::INSTANCE->zTypeRegistry()
  1029. ->getValidator<BlockReplaceItemSkillConfig>())
  1030. ->finishArray());
  1031. }
  1032. Framework::Text BlockReplaceItemSkillFactory::getTypeToken() const
  1033. {
  1034. return "replaceBlock";
  1035. }
  1036. DamagingItemSkillConfig::DamagingItemSkillConfig()
  1037. : ReferenceCounter(),
  1038. targetBlockFilter(0),
  1039. damage(2.f),
  1040. damagePerHeadHardness(1.f),
  1041. baseDamageMultiplier(1.f),
  1042. damageMultiplierPerHeadHardness(0.f),
  1043. damagePerLevel(0.3f),
  1044. damageMultiplierPerLevel(0.0f),
  1045. damageDevider(1.0f),
  1046. damageDeviderPerHardness(1.0f),
  1047. staminaCost(0.00001f),
  1048. staminaCostPerDamage(0.00001f),
  1049. staminaCostPerHardness(0.001f),
  1050. staminaCostDevider(0.9f),
  1051. staminaCostDeviderPerLevel(0.1f),
  1052. durabilityCost(0.1f),
  1053. durabilityCostPerDamage(0.01f),
  1054. durabilityCostPerHardness(0.01f),
  1055. durabilityCostDevider(0.8f),
  1056. additionalDurabilityCostDeviderPerLevel(0.2f),
  1057. xpGainPerDamage(0.05f)
  1058. {}
  1059. DamagingItemSkillConfig::~DamagingItemSkillConfig()
  1060. {
  1061. if (targetBlockFilter) targetBlockFilter->release();
  1062. }
  1063. void DamagingItemSkillConfig::setTargetBlockFilter(
  1064. BlockFilter* targetBlockFilter)
  1065. {
  1066. if (this->targetBlockFilter) this->targetBlockFilter->release();
  1067. this->targetBlockFilter = targetBlockFilter;
  1068. }
  1069. BlockFilter* DamagingItemSkillConfig::zTargetBlockFilter() const
  1070. {
  1071. return targetBlockFilter;
  1072. }
  1073. void DamagingItemSkillConfig::setDamage(float damage)
  1074. {
  1075. this->damage = damage;
  1076. }
  1077. float DamagingItemSkillConfig::getDamage() const
  1078. {
  1079. return damage;
  1080. }
  1081. void DamagingItemSkillConfig::setDamagePerHeadHardness(
  1082. float damagePerHeadHardness)
  1083. {
  1084. this->damagePerHeadHardness = damagePerHeadHardness;
  1085. }
  1086. float DamagingItemSkillConfig::getDamagePerHeadHardness() const
  1087. {
  1088. return damageDeviderPerHardness;
  1089. }
  1090. void DamagingItemSkillConfig::setBaseDamageMultiplier(
  1091. float baseDamageMultiplier)
  1092. {
  1093. this->baseDamageMultiplier = baseDamageMultiplier;
  1094. }
  1095. float DamagingItemSkillConfig::getBaseDamageMultiplier() const
  1096. {
  1097. return baseDamageMultiplier;
  1098. }
  1099. void DamagingItemSkillConfig::setDamageMultiplierPerHeadHardness(
  1100. float damageMupliplierPerHeadHardness)
  1101. {
  1102. this->damageMultiplierPerHeadHardness = damageMupliplierPerHeadHardness;
  1103. }
  1104. float DamagingItemSkillConfig::getDamageMultiplierPerHeadHardness() const
  1105. {
  1106. return damageMultiplierPerHeadHardness;
  1107. }
  1108. void DamagingItemSkillConfig::setDamagePerLevel(float damagePerLevel)
  1109. {
  1110. this->damagePerLevel = damagePerLevel;
  1111. }
  1112. float DamagingItemSkillConfig::getDamagePerLevel() const
  1113. {
  1114. return damagePerLevel;
  1115. }
  1116. void DamagingItemSkillConfig::setDamageMultiplierPerLevel(
  1117. float damageMultiplierPerLevel)
  1118. {
  1119. this->damageMultiplierPerLevel = damageMultiplierPerLevel;
  1120. }
  1121. float DamagingItemSkillConfig::getDamageMultiplierPerLevel() const
  1122. {
  1123. return damageMultiplierPerLevel;
  1124. }
  1125. void DamagingItemSkillConfig::setDamageDevider(float damageDevider)
  1126. {
  1127. this->damageDevider = damageDevider;
  1128. }
  1129. float DamagingItemSkillConfig::getDamageDevider() const
  1130. {
  1131. return damageDevider;
  1132. }
  1133. void DamagingItemSkillConfig::setDamageDeviderPerHardness(
  1134. float damageDeviderPerHardness)
  1135. {
  1136. this->damageDeviderPerHardness = damageDeviderPerHardness;
  1137. }
  1138. float DamagingItemSkillConfig::getDamageDeviderPerHardness() const
  1139. {
  1140. return damageDeviderPerHardness;
  1141. }
  1142. void DamagingItemSkillConfig::setStaminaCost(float staminaCost)
  1143. {
  1144. this->staminaCost = staminaCost;
  1145. }
  1146. float DamagingItemSkillConfig::getStaminaCost() const
  1147. {
  1148. return staminaCost;
  1149. }
  1150. void DamagingItemSkillConfig::setStaminaCostPerDamage(
  1151. float staminaCostPerDamage)
  1152. {
  1153. this->staminaCostPerDamage = staminaCostPerDamage;
  1154. }
  1155. float DamagingItemSkillConfig::getStaminaCostPerDamage() const
  1156. {
  1157. return staminaCostPerDamage;
  1158. }
  1159. void DamagingItemSkillConfig::setStaminaCostPerHardness(
  1160. float staminaCostPerHardness)
  1161. {
  1162. this->staminaCostPerHardness = staminaCostPerHardness;
  1163. }
  1164. float DamagingItemSkillConfig::getStaminaCostPerHardness() const
  1165. {
  1166. return staminaCostPerHardness;
  1167. }
  1168. void DamagingItemSkillConfig::setStaminaCostDevider(float staminaCostDevider)
  1169. {
  1170. this->staminaCostDevider = staminaCostDevider;
  1171. }
  1172. float DamagingItemSkillConfig::getStaminaCostDevider() const
  1173. {
  1174. return staminaCostDevider;
  1175. }
  1176. void DamagingItemSkillConfig::setStaminaCostDeviderPerLevel(
  1177. float staminaCostDeviderPerLevel)
  1178. {
  1179. this->staminaCostDeviderPerLevel = staminaCostDeviderPerLevel;
  1180. }
  1181. float DamagingItemSkillConfig::getStaminaCostDeviderPerLevel() const
  1182. {
  1183. return staminaCostDeviderPerLevel;
  1184. }
  1185. void DamagingItemSkillConfig::setDurabilityCost(float durabilityCost)
  1186. {
  1187. this->durabilityCost = durabilityCost;
  1188. }
  1189. float DamagingItemSkillConfig::getDurabilityCost() const
  1190. {
  1191. return durabilityCost;
  1192. }
  1193. void DamagingItemSkillConfig::setDurabilityCostPerDamage(
  1194. float durabilityCostPerDamage)
  1195. {
  1196. this->durabilityCostPerDamage = durabilityCostPerDamage;
  1197. }
  1198. float DamagingItemSkillConfig::getDurabilityCostPerDamage() const
  1199. {
  1200. return durabilityCostPerDamage;
  1201. }
  1202. void DamagingItemSkillConfig::setDurabilityCostPerHardness(
  1203. float durabilityCostPerHardness)
  1204. {
  1205. this->durabilityCostPerHardness = durabilityCostPerHardness;
  1206. }
  1207. float DamagingItemSkillConfig::getDurabilityCostPerHardness() const
  1208. {
  1209. return durabilityCostPerHardness;
  1210. }
  1211. void DamagingItemSkillConfig::setDurabilityCostDevider(
  1212. float durabilityCostDevider)
  1213. {
  1214. this->durabilityCostDevider = durabilityCostDevider;
  1215. }
  1216. float DamagingItemSkillConfig::getDurabilityCostDevider() const
  1217. {
  1218. return durabilityCostDevider;
  1219. }
  1220. void DamagingItemSkillConfig::setAdditionalDurabilityCostDeviderPerLevel(
  1221. float additionalDurabilityCostDeviderPerLevel)
  1222. {
  1223. this->additionalDurabilityCostDeviderPerLevel
  1224. = additionalDurabilityCostDeviderPerLevel;
  1225. }
  1226. float DamagingItemSkillConfig::getAdditionalDurabilityCostDeviderPerLevel()
  1227. const
  1228. {
  1229. return additionalDurabilityCostDeviderPerLevel;
  1230. }
  1231. void DamagingItemSkillConfig::setXpGainPerDamage(float xpGainPerDamage)
  1232. {
  1233. this->xpGainPerDamage = xpGainPerDamage;
  1234. }
  1235. float DamagingItemSkillConfig::getXpGainPerDamage() const
  1236. {
  1237. return xpGainPerDamage;
  1238. }
  1239. DamagingItemSkillConfigFactory::DamagingItemSkillConfigFactory()
  1240. : TypeFactory()
  1241. {}
  1242. DamagingItemSkillConfig* DamagingItemSkillConfigFactory::createValue(
  1243. Framework::JSON::JSONObject* zJson) const
  1244. {
  1245. return new DamagingItemSkillConfig();
  1246. }
  1247. void DamagingItemSkillConfigFactory::fromJson(
  1248. DamagingItemSkillConfig* zResult, Framework::JSON::JSONObject* zJson) const
  1249. {
  1250. zResult->setTargetBlockFilter(
  1251. Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
  1252. zJson->zValue("targetFilter")));
  1253. zResult->setDamage(
  1254. (float)zJson->asObject()->zValue("damage")->asNumber()->getNumber());
  1255. zResult->setDamagePerHeadHardness((float)zJson->asObject()
  1256. ->zValue("damagePerHeadHardness")
  1257. ->asNumber()
  1258. ->getNumber());
  1259. zResult->setBaseDamageMultiplier((float)zJson->asObject()
  1260. ->zValue("baseDamageMultiplier")
  1261. ->asNumber()
  1262. ->getNumber());
  1263. zResult->setDamageMultiplierPerHeadHardness(
  1264. (float)zJson->asObject()
  1265. ->zValue("damageMultiplierPerHeadHardness")
  1266. ->asNumber()
  1267. ->getNumber());
  1268. zResult->setDamagePerLevel((float)zJson->asObject()
  1269. ->zValue("damagePerLevel")
  1270. ->asNumber()
  1271. ->getNumber());
  1272. zResult->setDamageMultiplierPerLevel(
  1273. (float)zJson->asObject()
  1274. ->zValue("damageMultiplierPerLevel")
  1275. ->asNumber()
  1276. ->getNumber());
  1277. zResult->setDamageDevider((float)zJson->asObject()
  1278. ->zValue("damageDevider")
  1279. ->asNumber()
  1280. ->getNumber());
  1281. zResult->setDamageDeviderPerHardness(
  1282. (float)zJson->asObject()
  1283. ->zValue("damageDeviderPerHardness")
  1284. ->asNumber()
  1285. ->getNumber());
  1286. zResult->setStaminaCost((float)zJson->asObject()
  1287. ->zValue("staminaCost")
  1288. ->asNumber()
  1289. ->getNumber());
  1290. zResult->setStaminaCostPerDamage((float)zJson->asObject()
  1291. ->zValue("staminaCostPerDamage")
  1292. ->asNumber()
  1293. ->getNumber());
  1294. zResult->setStaminaCostPerHardness((float)zJson->asObject()
  1295. ->zValue("staminaCostPerHardness")
  1296. ->asNumber()
  1297. ->getNumber());
  1298. zResult->setStaminaCostDevider((float)zJson->asObject()
  1299. ->zValue("staminaCostDevider")
  1300. ->asNumber()
  1301. ->getNumber());
  1302. zResult->setStaminaCostDeviderPerLevel(
  1303. (float)zJson->asObject()
  1304. ->zValue("staminaCostDeviderPerLevel")
  1305. ->asNumber()
  1306. ->getNumber());
  1307. zResult->setDurabilityCost((float)zJson->asObject()
  1308. ->zValue("durabilityCost")
  1309. ->asNumber()
  1310. ->getNumber());
  1311. zResult->setDurabilityCostPerDamage((float)zJson->asObject()
  1312. ->zValue("durabilityCostPerDamage")
  1313. ->asNumber()
  1314. ->getNumber());
  1315. zResult->setDurabilityCostPerHardness(
  1316. (float)zJson->asObject()
  1317. ->zValue("durabilityCostPerHardness")
  1318. ->asNumber()
  1319. ->getNumber());
  1320. zResult->setDurabilityCostDevider((float)zJson->asObject()
  1321. ->zValue("durabilityCostDevider")
  1322. ->asNumber()
  1323. ->getNumber());
  1324. zResult->setAdditionalDurabilityCostDeviderPerLevel(
  1325. (float)zJson->asObject()
  1326. ->zValue("additionalDurabilityCostDeviderPerLevel")
  1327. ->asNumber()
  1328. ->getNumber());
  1329. zResult->setXpGainPerDamage((float)zJson->asObject()
  1330. ->zValue("xpGainPerDamage")
  1331. ->asNumber()
  1332. ->getNumber());
  1333. }
  1334. void DamagingItemSkillConfigFactory::toJson(DamagingItemSkillConfig* zObject,
  1335. Framework::JSON::JSONObject* zResult) const
  1336. {
  1337. zResult->addValue("targetFilter",
  1338. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zTargetBlockFilter()));
  1339. zResult->addValue(
  1340. "damage", new Framework::JSON::JSONNumber(zObject->getDamage()));
  1341. zResult->addValue("damagePerHeadHardness",
  1342. new Framework::JSON::JSONNumber(zObject->getDamagePerHeadHardness()));
  1343. zResult->addValue("baseDamageMultiplier",
  1344. new Framework::JSON::JSONNumber(zObject->getBaseDamageMultiplier()));
  1345. zResult->addValue("damageMultiplierPerHeadHardness",
  1346. new Framework::JSON::JSONNumber(
  1347. zObject->getDamageMultiplierPerHeadHardness()));
  1348. zResult->addValue("damagePerLevel",
  1349. new Framework::JSON::JSONNumber(zObject->getDamagePerLevel()));
  1350. zResult->addValue("damageMultiplierPerLevel",
  1351. new Framework::JSON::JSONNumber(
  1352. zObject->getDamageMultiplierPerLevel()));
  1353. zResult->addValue("damageDevider",
  1354. new Framework::JSON::JSONNumber(zObject->getDamageDevider()));
  1355. zResult->addValue("damageDeviderPerHardness",
  1356. new Framework::JSON::JSONNumber(
  1357. zObject->getDamageDeviderPerHardness()));
  1358. zResult->addValue("staminaCost",
  1359. new Framework::JSON::JSONNumber(zObject->getStaminaCost()));
  1360. zResult->addValue("staminaCostPerDamage",
  1361. new Framework::JSON::JSONNumber(zObject->getStaminaCostPerDamage()));
  1362. zResult->addValue("staminaCostPerHardness",
  1363. new Framework::JSON::JSONNumber(zObject->getStaminaCostPerHardness()));
  1364. zResult->addValue("staminaCostDevider",
  1365. new Framework::JSON::JSONNumber(zObject->getStaminaCostDevider()));
  1366. zResult->addValue("staminaCostDeviderPerLevel",
  1367. new Framework::JSON::JSONNumber(
  1368. zObject->getStaminaCostDeviderPerLevel()));
  1369. zResult->addValue("durabilityCost",
  1370. new Framework::JSON::JSONNumber(zObject->getDurabilityCost()));
  1371. zResult->addValue("durabilityCostPerDamage",
  1372. new Framework::JSON::JSONNumber(zObject->getDurabilityCostPerDamage()));
  1373. zResult->addValue("durabilityCostPerHardness",
  1374. new Framework::JSON::JSONNumber(
  1375. zObject->getDurabilityCostPerHardness()));
  1376. zResult->addValue("durabilityCostDevider",
  1377. new Framework::JSON::JSONNumber(zObject->getDurabilityCostDevider()));
  1378. zResult->addValue("additionalDurabilityCostDeviderPerLevel",
  1379. new Framework::JSON::JSONNumber(
  1380. zObject->getAdditionalDurabilityCostDeviderPerLevel()));
  1381. zResult->addValue("xpGainPerDamage",
  1382. new Framework::JSON::JSONNumber(zObject->getXpGainPerDamage()));
  1383. }
  1384. JSONObjectValidationBuilder* DamagingItemSkillConfigFactory::addToValidator(
  1385. JSONObjectValidationBuilder* builder) const
  1386. {
  1387. return builder
  1388. ->withRequiredAttribute("targetFilter",
  1389. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
  1390. ->withRequiredNumber("damage")
  1391. ->whichIsGreaterOrEqual(0.0)
  1392. ->withDefault(2.0)
  1393. ->finishNumber()
  1394. ->withRequiredNumber("damagePerHeadHardness")
  1395. ->whichIsGreaterOrEqual(0.0)
  1396. ->withDefault(1.0)
  1397. ->finishNumber()
  1398. ->withRequiredNumber("baseDamageMultiplier")
  1399. ->whichIsGreaterOrEqual(0.0)
  1400. ->withDefault(1.0)
  1401. ->finishNumber()
  1402. ->withRequiredNumber("damageMultiplierPerHeadHardness")
  1403. ->whichIsGreaterOrEqual(0.0)
  1404. ->withDefault(0.0)
  1405. ->finishNumber()
  1406. ->withRequiredNumber("damagePerLevel")
  1407. ->whichIsGreaterOrEqual(0.0)
  1408. ->withDefault(0.3)
  1409. ->finishNumber()
  1410. ->withRequiredNumber("damageMultiplierPerLevel")
  1411. ->whichIsGreaterOrEqual(0.0)
  1412. ->withDefault(0.0)
  1413. ->finishNumber()
  1414. ->withRequiredNumber("damageDevider")
  1415. ->whichIsGreaterOrEqual(0.0)
  1416. ->withDefault(1.0)
  1417. ->finishNumber()
  1418. ->withRequiredNumber("damageDeviderPerHardness")
  1419. ->whichIsGreaterOrEqual(0.0)
  1420. ->withDefault(1.0)
  1421. ->finishNumber()
  1422. ->withRequiredNumber("staminaCost")
  1423. ->whichIsGreaterOrEqual(0.0)
  1424. ->withDefault(0.0001)
  1425. ->finishNumber()
  1426. ->withRequiredNumber("staminaCostPerDamage")
  1427. ->whichIsGreaterOrEqual(0.0)
  1428. ->withDefault(0.00001)
  1429. ->finishNumber()
  1430. ->withRequiredNumber("staminaCostPerHardness")
  1431. ->whichIsGreaterOrEqual(0.0)
  1432. ->withDefault(0.001)
  1433. ->finishNumber()
  1434. ->withRequiredNumber("staminaCostDevider")
  1435. ->whichIsGreaterOrEqual(0.0)
  1436. ->withDefault(0.9)
  1437. ->finishNumber()
  1438. ->withRequiredNumber("staminaCostDeviderPerLevel")
  1439. ->whichIsGreaterOrEqual(0.0)
  1440. ->withDefault(0.1)
  1441. ->finishNumber()
  1442. ->withRequiredNumber("durabilityCost")
  1443. ->whichIsGreaterOrEqual(0.0)
  1444. ->withDefault(0.1)
  1445. ->finishNumber()
  1446. ->withRequiredNumber("durabilityCostPerDamage")
  1447. ->whichIsGreaterOrEqual(0.0)
  1448. ->withDefault(0.01)
  1449. ->finishNumber()
  1450. ->withRequiredNumber("durabilityCostPerHardness")
  1451. ->whichIsGreaterOrEqual(0.0)
  1452. ->withDefault(0.01)
  1453. ->finishNumber()
  1454. ->withRequiredNumber("durabilityCostDevider")
  1455. ->whichIsGreaterOrEqual(0.0)
  1456. ->withDefault(0.8)
  1457. ->finishNumber()
  1458. ->withRequiredNumber("additionalDurabilityCostDeviderPerLevel")
  1459. ->whichIsGreaterOrEqual(0.0)
  1460. ->withDefault(0.2)
  1461. ->finishNumber()
  1462. ->withRequiredNumber("xpGainPerDamage")
  1463. ->whichIsGreaterOrEqual(0.0)
  1464. ->withDefault(0.05)
  1465. ->finishNumber();
  1466. }
  1467. DamagingItemSkill::DamagingItemSkill()
  1468. : ItemSkill(),
  1469. invalidUseConfig(0)
  1470. {}
  1471. DamagingItemSkill::~DamagingItemSkill()
  1472. {
  1473. if (invalidUseConfig) invalidUseConfig->release();
  1474. }
  1475. bool DamagingItemSkill::use(Entity* zActor, Item* zUsedItem, Block* zTarget)
  1476. {
  1477. BasicToolItem* tool = dynamic_cast<BasicToolItem*>(zUsedItem);
  1478. DamagingItemSkillConfig* usedConfig = 0;
  1479. for (DamagingItemSkillConfig* config : configs)
  1480. {
  1481. if (config->zTargetBlockFilter()->test(zTarget))
  1482. {
  1483. usedConfig = config;
  1484. break;
  1485. }
  1486. }
  1487. bool invalid = 0;
  1488. if (usedConfig == 0)
  1489. {
  1490. usedConfig = invalidUseConfig;
  1491. invalid = 1;
  1492. }
  1493. float damage = usedConfig->getDamage()
  1494. + usedConfig->getDamagePerHeadHardness()
  1495. * (tool ? tool->getHeadMaterialHardness() : 0)
  1496. + usedConfig->getDamagePerLevel() * getLevel();
  1497. float damageMultiplier
  1498. = usedConfig->getBaseDamageMultiplier()
  1499. + usedConfig->getDamageMultiplierPerLevel() * getLevel()
  1500. + usedConfig->getDamageMultiplierPerHeadHardness()
  1501. * (tool ? tool->getHeadMaterialHardness() : 0);
  1502. if (damageMultiplier != 0)
  1503. {
  1504. damage *= damageMultiplier;
  1505. }
  1506. float damageDevider
  1507. = usedConfig->getDamageDevider()
  1508. + usedConfig->getDamageDeviderPerHardness() * zTarget->getHardness();
  1509. if (damageDevider != 0)
  1510. {
  1511. damage /= damageDevider;
  1512. }
  1513. float staminaCost
  1514. = usedConfig->getStaminaCost()
  1515. + usedConfig->getStaminaCostPerDamage() * damage
  1516. + usedConfig->getStaminaCostPerHardness() * zTarget->getHardness();
  1517. float staminaDevider
  1518. = usedConfig->getStaminaCostDevider()
  1519. + usedConfig->getStaminaCostDeviderPerLevel() * getLevel();
  1520. if (staminaDevider != 0)
  1521. {
  1522. staminaCost /= staminaDevider;
  1523. }
  1524. if (zActor->getStamina() < staminaCost)
  1525. {
  1526. return false;
  1527. }
  1528. float durabilityCost
  1529. = usedConfig->getDurabilityCost()
  1530. + usedConfig->getDurabilityCostPerDamage() * damage
  1531. + usedConfig->getDurabilityCostPerHardness() * zTarget->getHardness();
  1532. float durabilityDevider
  1533. = usedConfig->getDurabilityCostDevider()
  1534. + usedConfig->getAdditionalDurabilityCostDeviderPerLevel() * getLevel();
  1535. if (durabilityDevider != 0)
  1536. {
  1537. durabilityCost /= durabilityDevider;
  1538. }
  1539. zUsedItem->setDurability(zUsedItem->getDurability() - durabilityCost);
  1540. zActor->setStamina(zActor->getStamina() - staminaCost);
  1541. setXp(getXp() + usedConfig->getXpGainPerDamage() * damage);
  1542. zTarget->setHP(zTarget->getHP() - damage);
  1543. return true;
  1544. }
  1545. bool DamagingItemSkill::use(Entity* zActor, Item* zUsedItem, Entity* zTarget)
  1546. {
  1547. return false;
  1548. }
  1549. void DamagingItemSkill::setInvalidUseConfig(DamagingItemSkillConfig* config)
  1550. {
  1551. if (invalidUseConfig) invalidUseConfig->release();
  1552. invalidUseConfig = config;
  1553. }
  1554. DamagingItemSkillConfig* DamagingItemSkill::zInvalidUseConfig() const
  1555. {
  1556. return invalidUseConfig;
  1557. }
  1558. void DamagingItemSkill::addConfig(DamagingItemSkillConfig* config)
  1559. {
  1560. configs.add(config);
  1561. }
  1562. const Framework::RCArray<DamagingItemSkillConfig>&
  1563. DamagingItemSkill::getConfigs() const
  1564. {
  1565. return configs;
  1566. }
  1567. DamagingItemSkillFactory::DamagingItemSkillFactory()
  1568. : ItemSkillFactoryBase()
  1569. {}
  1570. DamagingItemSkill* DamagingItemSkillFactory::createValue(
  1571. Framework::JSON::JSONObject* zJson) const
  1572. {
  1573. return new DamagingItemSkill();
  1574. }
  1575. void DamagingItemSkillFactory::fromJson(
  1576. DamagingItemSkill* zResult, Framework::JSON::JSONObject* zJson) const
  1577. {
  1578. for (Framework::JSON::JSONValue* configValue :
  1579. *zJson->zValue("configs")->asArray())
  1580. {
  1581. zResult->addConfig(
  1582. Game::INSTANCE->zTypeRegistry()->fromJson<DamagingItemSkillConfig>(
  1583. configValue));
  1584. }
  1585. DamagingItemSkillConfig* invalidUseConfig = new DamagingItemSkillConfig();
  1586. invalidUseConfig->setDamage(0.f);
  1587. invalidUseConfig->setDamageDeviderPerHardness(0.f);
  1588. invalidUseConfig->setBaseDamageMultiplier(0.f);
  1589. invalidUseConfig->setDamageMultiplierPerHeadHardness(0.f);
  1590. invalidUseConfig->setDamagePerLevel(0.f);
  1591. invalidUseConfig->setDamageMultiplierPerLevel(0.f);
  1592. invalidUseConfig->setDamageDevider(1.f);
  1593. invalidUseConfig->setDamageDeviderPerHardness(0.f);
  1594. invalidUseConfig->setStaminaCost(
  1595. (float)zJson->zValue("invalidStaminaCost")->asNumber()->getNumber());
  1596. invalidUseConfig->setStaminaCostPerDamage(0.f);
  1597. invalidUseConfig->setStaminaCostPerHardness(
  1598. (float)zJson->zValue("invalidStaminaCostPerHardness")
  1599. ->asNumber()
  1600. ->getNumber());
  1601. invalidUseConfig->setStaminaCostDevider(
  1602. (float)zJson->zValue("invalidStaminaCostDevider")
  1603. ->asNumber()
  1604. ->getNumber());
  1605. invalidUseConfig->setStaminaCostDeviderPerLevel(
  1606. (float)zJson->zValue("invalidStaminaCostDeviderPerLevel")
  1607. ->asNumber()
  1608. ->getNumber());
  1609. invalidUseConfig->setDurabilityCost(
  1610. (float)zJson->zValue("invalidDurabilityCost")->asNumber()->getNumber());
  1611. invalidUseConfig->setDurabilityCostPerHardness(
  1612. (float)zJson->zValue("invalidDurabilityCostPerHardness")
  1613. ->asNumber()
  1614. ->getNumber());
  1615. invalidUseConfig->setDurabilityCostDevider(
  1616. (float)zJson->zValue("invalidDurabilityCostDevider")
  1617. ->asNumber()
  1618. ->getNumber());
  1619. invalidUseConfig->setAdditionalDurabilityCostDeviderPerLevel(
  1620. (float)zJson->zValue("invalidDurabilityCostDeviderPerLevel")
  1621. ->asNumber()
  1622. ->getNumber());
  1623. zResult->setInvalidUseConfig(invalidUseConfig);
  1624. ItemSkillFactoryBase::fromJson(zResult, zJson);
  1625. }
  1626. void DamagingItemSkillFactory::toJson(
  1627. DamagingItemSkill* zObject, Framework::JSON::JSONObject* zResult) const
  1628. {
  1629. zResult->addValue(
  1630. "maxXp", new Framework::JSON::JSONNumber(zObject->getMaxXp()));
  1631. zResult->addValue("invalidStaminaCost",
  1632. new Framework::JSON::JSONNumber(
  1633. zObject->zInvalidUseConfig()->getStaminaCost()));
  1634. zResult->addValue("invalidStaminaCostPerHardness",
  1635. new Framework::JSON::JSONNumber(
  1636. zObject->zInvalidUseConfig()->getStaminaCostPerHardness()));
  1637. zResult->addValue("invalidStaminaCostDevider",
  1638. new Framework::JSON::JSONNumber(
  1639. zObject->zInvalidUseConfig()->getStaminaCostDevider()));
  1640. zResult->addValue("invalidStaminaCostDeviderPerLevel",
  1641. new Framework::JSON::JSONNumber(
  1642. zObject->zInvalidUseConfig()->getStaminaCostDeviderPerLevel()));
  1643. zResult->addValue("invalidDurabilityCost",
  1644. new Framework::JSON::JSONNumber(
  1645. zObject->zInvalidUseConfig()->getDurabilityCost()));
  1646. zResult->addValue("invalidDurabilityCostPerHardness",
  1647. new Framework::JSON::JSONNumber(
  1648. zObject->zInvalidUseConfig()->getDurabilityCostPerHardness()));
  1649. zResult->addValue("invalidDurabilityCostDevider",
  1650. new Framework::JSON::JSONNumber(
  1651. zObject->zInvalidUseConfig()->getDurabilityCostDevider()));
  1652. zResult->addValue("invalidDurabilityCostDeviderPerLevel",
  1653. new Framework::JSON::JSONNumber(
  1654. zObject->zInvalidUseConfig()
  1655. ->getAdditionalDurabilityCostDeviderPerLevel()));
  1656. Framework::JSON::JSONArray* configs = new Framework::JSON::JSONArray();
  1657. for (DamagingItemSkillConfig* config : zObject->getConfigs())
  1658. {
  1659. configs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(config));
  1660. }
  1661. zResult->addValue("configs", configs);
  1662. ItemSkillFactoryBase::toJson(zObject, zResult);
  1663. }
  1664. JSONObjectValidationBuilder* DamagingItemSkillFactory::addToValidator(
  1665. JSONObjectValidationBuilder* builder) const
  1666. {
  1667. return ItemSkillFactoryBase::addToValidator(
  1668. builder->withRequiredNumber("invalidStaminaCost")
  1669. ->whichIsGreaterOrEqual(0.0)
  1670. ->withDefault(0.5)
  1671. ->finishNumber()
  1672. ->withRequiredNumber("invalidStaminaCostPerHardness")
  1673. ->whichIsGreaterOrEqual(0.0)
  1674. ->withDefault(0.001)
  1675. ->finishNumber()
  1676. ->withRequiredNumber("invalidStaminaCostDevider")
  1677. ->whichIsGreaterOrEqual(0.0)
  1678. ->withDefault(0.8)
  1679. ->finishNumber()
  1680. ->withRequiredNumber("invalidAdditionalStaminaCostDeviderPerLevel")
  1681. ->whichIsGreaterOrEqual(0.0)
  1682. ->withDefault(0.2)
  1683. ->finishNumber()
  1684. ->withRequiredNumber("invalidDurabilityCost")
  1685. ->whichIsGreaterOrEqual(0.0)
  1686. ->withDefault(0.5)
  1687. ->finishNumber()
  1688. ->withRequiredNumber("invalidDurabilityCostPerHardness")
  1689. ->whichIsGreaterOrEqual(0.0)
  1690. ->withDefault(0.01)
  1691. ->finishNumber()
  1692. ->withRequiredNumber("invalidDurabilityCostDevider")
  1693. ->whichIsGreaterOrEqual(0.0)
  1694. ->withDefault(0.98)
  1695. ->finishNumber()
  1696. ->withRequiredNumber(
  1697. "invalidAdditionalDurabilityCostDeviderPerLevel")
  1698. ->whichIsGreaterOrEqual(0.0)
  1699. ->withDefault(0.02)
  1700. ->finishNumber()
  1701. ->withRequiredArray("configs")
  1702. ->addAcceptedTypeInArray(
  1703. Game::INSTANCE->zTypeRegistry()
  1704. ->getValidator<DamagingItemSkillConfig>())
  1705. ->finishArray());
  1706. }
  1707. Framework::Text DamagingItemSkillFactory::getTypeToken() const
  1708. {
  1709. return "damaging";
  1710. }