BasicTool.cpp 62 KB

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