BasicTool.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  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. ->withRequiredAttribute("brokenItemTypeName",
  562. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  563. ItemTypeNameFactory::TYPE_ID))
  564. ->withRequiredAttribute("levelUpRule",
  565. Game::INSTANCE->zTypeRegistry()
  566. ->getValidator<ItemSkillLevelUpRule>())
  567. ->withRequiredAttribute("itemSkill",
  568. Game::INSTANCE->zTypeRegistry()->getValidator<ItemSkill>()));
  569. }
  570. const char* BasicToolItemTypeFactory::getTypeToken() const
  571. {
  572. return "tool";
  573. }
  574. BlockReplaceItemSkillConfig::BlockReplaceItemSkillConfig()
  575. : ReferenceCounter(),
  576. targetBlockFilter(0),
  577. replacementBlockTypeId(-1),
  578. cooldownTicks(20),
  579. staminaCost(0.5f),
  580. staminaCostDevider(0.8f),
  581. additionalStaminaCostDeviderPerLevel(0.2f),
  582. durabilityCost(0.5f),
  583. durabilityCostDevider(0.89f),
  584. additionalDurabilityCostDeviderPerLevel(0.02f),
  585. xpGain(0.5f)
  586. {}
  587. BlockReplaceItemSkillConfig::~BlockReplaceItemSkillConfig()
  588. {
  589. if (targetBlockFilter) targetBlockFilter->release();
  590. }
  591. void BlockReplaceItemSkillConfig::setTargetBlockFilter(
  592. BlockFilter* targetBlockFilter)
  593. {
  594. this->targetBlockFilter = targetBlockFilter;
  595. }
  596. BlockFilter* BlockReplaceItemSkillConfig::zTargetBlockFilter() const
  597. {
  598. return targetBlockFilter;
  599. }
  600. void BlockReplaceItemSkillConfig::setReplacementBlockTypeId(
  601. int replacementBlockTypeId)
  602. {
  603. this->replacementBlockTypeId = replacementBlockTypeId;
  604. }
  605. int BlockReplaceItemSkillConfig::getReplacementBlockTypeId() const
  606. {
  607. return replacementBlockTypeId;
  608. }
  609. void BlockReplaceItemSkillConfig::setCooldownTicks(int cooldownTicks)
  610. {
  611. this->cooldownTicks = cooldownTicks;
  612. }
  613. int BlockReplaceItemSkillConfig::getCooldownTicks() const
  614. {
  615. return cooldownTicks;
  616. }
  617. void BlockReplaceItemSkillConfig::setStaminaCost(float staminaCost)
  618. {
  619. this->staminaCost = staminaCost;
  620. }
  621. float BlockReplaceItemSkillConfig::getStaminaCost() const
  622. {
  623. return staminaCost;
  624. }
  625. void BlockReplaceItemSkillConfig::setStaminaCostDevider(
  626. float staminaCostDevider)
  627. {
  628. this->staminaCostDevider = staminaCostDevider;
  629. }
  630. float BlockReplaceItemSkillConfig::getStaminaCostDevider() const
  631. {
  632. return staminaCostDevider;
  633. }
  634. void BlockReplaceItemSkillConfig::setAdditionalStaminaCostDeviderPerLevel(
  635. float additionalStaminaCostDeviderPerLevel)
  636. {
  637. this->additionalStaminaCostDeviderPerLevel
  638. = additionalStaminaCostDeviderPerLevel;
  639. }
  640. float BlockReplaceItemSkillConfig::getAdditionalStaminaCostDeviderPerLevel()
  641. const
  642. {
  643. return additionalStaminaCostDeviderPerLevel;
  644. }
  645. void BlockReplaceItemSkillConfig::setDurabilityCost(float durabilityCost)
  646. {
  647. this->durabilityCost = durabilityCost;
  648. }
  649. float BlockReplaceItemSkillConfig::getDurabilityCost() const
  650. {
  651. return durabilityCost;
  652. }
  653. void BlockReplaceItemSkillConfig::setDurabilityCostDevider(
  654. float durabilityCostDevider)
  655. {
  656. this->durabilityCostDevider = durabilityCostDevider;
  657. }
  658. float BlockReplaceItemSkillConfig::getDurabilityCostDevider() const
  659. {
  660. return durabilityCostDevider;
  661. }
  662. void BlockReplaceItemSkillConfig::setAdditionalDurabilityCostDeviderPerLevel(
  663. float additionalDurabilityCostDeviderPerLevel)
  664. {
  665. this->additionalDurabilityCostDeviderPerLevel
  666. = additionalDurabilityCostDeviderPerLevel;
  667. }
  668. float BlockReplaceItemSkillConfig::getAdditionalDurabilityCostDeviderPerLevel()
  669. const
  670. {
  671. return additionalDurabilityCostDeviderPerLevel;
  672. }
  673. void BlockReplaceItemSkillConfig::setXpGain(float xpGain)
  674. {
  675. this->xpGain = xpGain;
  676. }
  677. float BlockReplaceItemSkillConfig::getXpGain() const
  678. {
  679. return xpGain;
  680. }
  681. BlockReplaceItemSkillConfigFactory::BlockReplaceItemSkillConfigFactory()
  682. : ObjectTypeFactory()
  683. {}
  684. BlockReplaceItemSkillConfig* BlockReplaceItemSkillConfigFactory::fromJson(
  685. Framework::JSON::JSONObject* zJson) const
  686. {
  687. BlockReplaceItemSkillConfig* result = new BlockReplaceItemSkillConfig();
  688. result->setTargetBlockFilter(
  689. Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
  690. zJson->asObject()->zValue("targetFilter")));
  691. result->setCooldownTicks((int)zJson->asObject()
  692. ->zValue("cooldownTicks")
  693. ->asNumber()
  694. ->getNumber());
  695. result->setReplacementBlockTypeId(
  696. Game::INSTANCE->getBlockTypeId(zJson->asObject()
  697. ->zValue("replacementBlockType")
  698. ->asString()
  699. ->getString()));
  700. result->setCooldownTicks((int)zJson->asObject()
  701. ->zValue("cooldownTicks")
  702. ->asNumber()
  703. ->getNumber());
  704. result->setStaminaCost((float)zJson->asObject()
  705. ->zValue("staminaCost")
  706. ->asNumber()
  707. ->getNumber());
  708. result->setStaminaCostDevider((float)zJson->asObject()
  709. ->zValue("staminaCostDevider")
  710. ->asNumber()
  711. ->getNumber());
  712. result->setAdditionalStaminaCostDeviderPerLevel(
  713. (float)zJson->asObject()
  714. ->zValue("additionalStaminaCostDeviderPerLevel")
  715. ->asNumber()
  716. ->getNumber());
  717. result->setDurabilityCost((float)zJson->asObject()
  718. ->zValue("durabilityCost")
  719. ->asNumber()
  720. ->getNumber());
  721. result->setDurabilityCostDevider((float)zJson->asObject()
  722. ->zValue("durabilityCostDevider")
  723. ->asNumber()
  724. ->getNumber());
  725. result->setAdditionalDurabilityCostDeviderPerLevel(
  726. (float)zJson->asObject()
  727. ->zValue("additionalDurabilityCostDeviderPerLevel")
  728. ->asNumber()
  729. ->getNumber());
  730. result->setXpGain(
  731. (float)zJson->asObject()->zValue("xpGain")->asNumber()->getNumber());
  732. return result;
  733. }
  734. Framework::JSON::JSONObject* BlockReplaceItemSkillConfigFactory::toJsonObject(
  735. BlockReplaceItemSkillConfig* zObject) const
  736. {
  737. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  738. result->addValue("targetFilter",
  739. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zTargetBlockFilter()));
  740. result->addValue("replacementBlockType",
  741. new Framework::JSON::JSONString(
  742. Game::INSTANCE->zBlockType(zObject->getReplacementBlockTypeId())
  743. ->getName()));
  744. result->addValue("cooldownTicks",
  745. new Framework::JSON::JSONNumber(zObject->getCooldownTicks()));
  746. result->addValue("staminaCost",
  747. new Framework::JSON::JSONNumber(zObject->getStaminaCost()));
  748. result->addValue("staminaCostDevider",
  749. new Framework::JSON::JSONNumber(zObject->getStaminaCostDevider()));
  750. result->addValue("additionalStaminaCostDeviderPerLevel",
  751. new Framework::JSON::JSONNumber(
  752. zObject->getAdditionalStaminaCostDeviderPerLevel()));
  753. result->addValue("durabilityCost",
  754. new Framework::JSON::JSONNumber(zObject->getDurabilityCost()));
  755. result->addValue("durabilityCostDevider",
  756. new Framework::JSON::JSONNumber(zObject->getDurabilityCostDevider()));
  757. result->addValue("additionalDurabilityCostDeviderPerLevel",
  758. new Framework::JSON::JSONNumber(
  759. zObject->getAdditionalDurabilityCostDeviderPerLevel()));
  760. result->addValue(
  761. "xpGain", new Framework::JSON::JSONNumber(zObject->getXpGain()));
  762. return result;
  763. }
  764. JSONObjectValidationBuilder* BlockReplaceItemSkillConfigFactory::addToValidator(
  765. JSONObjectValidationBuilder* builder) const
  766. {
  767. return builder
  768. ->withRequiredAttribute("targetFilter",
  769. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
  770. ->withRequiredAttribute("replacementBlockType",
  771. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  772. BlockTypeNameFactory::TYPE_ID))
  773. ->withRequiredNumber("cooldownTicks")
  774. ->whichIsGreaterOrEqual(0)
  775. ->withDefault(20)
  776. ->finishNumber()
  777. ->withRequiredNumber("staminaCost")
  778. ->whichIsGreaterOrEqual(0)
  779. ->withDefault(0.5)
  780. ->finishNumber()
  781. ->withRequiredNumber("staminaCostDevider")
  782. ->whichIsGreaterOrEqual(0)
  783. ->withDefault(0.8)
  784. ->finishNumber()
  785. ->withRequiredNumber("additionalStaminaCostDeviderPerLevel")
  786. ->whichIsGreaterOrEqual(0)
  787. ->withDefault(0.2)
  788. ->finishNumber()
  789. ->withRequiredNumber("durabilityCost")
  790. ->whichIsGreaterOrEqual(0)
  791. ->withDefault(0.5)
  792. ->finishNumber()
  793. ->withRequiredNumber("durabilityCostDevider")
  794. ->whichIsGreaterOrEqual(0)
  795. ->withDefault(0.98)
  796. ->finishNumber()
  797. ->withRequiredNumber("additionalDurabilityCostDeviderPerLevel")
  798. ->whichIsGreaterOrEqual(0)
  799. ->withDefault(0.02)
  800. ->finishNumber()
  801. ->withRequiredNumber("xpGain")
  802. ->whichIsGreaterOrEqual(0)
  803. ->withDefault(0.5)
  804. ->finishNumber();
  805. }
  806. BlockReplaceItemSkill::BlockReplaceItemSkill()
  807. : ItemSkill(),
  808. invalidUseConfig(0),
  809. configs(),
  810. cooldownTicks(20)
  811. {}
  812. BlockReplaceItemSkill::~BlockReplaceItemSkill()
  813. {
  814. if (invalidUseConfig) invalidUseConfig->release();
  815. }
  816. void BlockReplaceItemSkill::load(Framework::StreamReader* zReader)
  817. {
  818. zReader->lese((char*)&cooldownTicks, 4);
  819. ItemSkill::load(zReader);
  820. }
  821. void BlockReplaceItemSkill::save(Framework::StreamWriter* zWriter)
  822. {
  823. zWriter->schreibe((char*)&cooldownTicks, 4);
  824. ItemSkill::save(zWriter);
  825. }
  826. bool BlockReplaceItemSkill::use(Entity* zActor, Item* zUsedItem, Block* zTarget)
  827. {
  828. if (cooldownTicks > 0)
  829. {
  830. cooldownTicks--;
  831. return false;
  832. }
  833. BlockReplaceItemSkillConfig* usedConfig = 0;
  834. for (BlockReplaceItemSkillConfig* config : configs)
  835. {
  836. if (config->zTargetBlockFilter()->test(zTarget))
  837. {
  838. usedConfig = config;
  839. break;
  840. }
  841. }
  842. bool invalid = 0;
  843. if (usedConfig == 0)
  844. {
  845. usedConfig = invalidUseConfig;
  846. invalid = 1;
  847. }
  848. float staminaDevider
  849. = usedConfig->getStaminaCostDevider()
  850. + usedConfig->getAdditionalStaminaCostDeviderPerLevel() * getLevel();
  851. float staminaCost = usedConfig->getStaminaCost();
  852. if (staminaDevider != 0)
  853. {
  854. staminaCost /= staminaDevider;
  855. }
  856. if (zActor->getStamina() < staminaCost)
  857. {
  858. return false;
  859. }
  860. float durabilityDevider
  861. = usedConfig->getDurabilityCostDevider()
  862. + usedConfig->getAdditionalDurabilityCostDeviderPerLevel() * getLevel();
  863. float durabilityCost = usedConfig->getDurabilityCost();
  864. if (durabilityDevider != 0)
  865. {
  866. durabilityCost /= durabilityDevider;
  867. }
  868. zUsedItem->setDurability(zUsedItem->getDurability() - durabilityCost);
  869. zActor->setStamina(zActor->getStamina() - staminaCost);
  870. cooldownTicks = usedConfig->getCooldownTicks();
  871. setXp(getXp() + usedConfig->getXpGain());
  872. if (!invalid)
  873. {
  874. Framework::Vec3<int> pos = zTarget->getPos();
  875. int dimension = zTarget->getDimensionId();
  876. Game::INSTANCE->doLater([pos, dimension, usedConfig]() {
  877. Game::INSTANCE->zDimension(dimension)->placeBlock(
  878. pos, usedConfig->getReplacementBlockTypeId());
  879. });
  880. }
  881. return true;
  882. }
  883. bool BlockReplaceItemSkill::use(
  884. Entity* zActor, Item* zUsedItem, Entity* zTarget)
  885. {
  886. return false;
  887. }
  888. void BlockReplaceItemSkill::setInvalidUseConfig(
  889. BlockReplaceItemSkillConfig* config)
  890. {
  891. if (invalidUseConfig) invalidUseConfig->release();
  892. invalidUseConfig = config;
  893. }
  894. BlockReplaceItemSkillConfig* BlockReplaceItemSkill::zInvalidUseConfig() const
  895. {
  896. return invalidUseConfig;
  897. }
  898. void BlockReplaceItemSkill::addConfig(BlockReplaceItemSkillConfig* config)
  899. {
  900. configs.add(config);
  901. }
  902. const Framework::RCArray<BlockReplaceItemSkillConfig>&
  903. BlockReplaceItemSkill::getConfigs() const
  904. {
  905. return configs;
  906. }
  907. void BlockReplaceItemSkill::setCooldownTicks(int cooldownTicks)
  908. {
  909. this->cooldownTicks = cooldownTicks;
  910. }
  911. int BlockReplaceItemSkill::getCooldownTicks() const
  912. {
  913. return cooldownTicks;
  914. }
  915. BlockReplaceItemSkillFactory::BlockReplaceItemSkillFactory()
  916. : ItemSkillFactoryBase()
  917. {}
  918. BlockReplaceItemSkill* BlockReplaceItemSkillFactory::createValue(
  919. Framework::JSON::JSONObject* zJson) const
  920. {
  921. return new BlockReplaceItemSkill();
  922. }
  923. BlockReplaceItemSkill* BlockReplaceItemSkillFactory::fromJson(
  924. Framework::JSON::JSONObject* zJson) const
  925. {
  926. BlockReplaceItemSkill* result = ItemSkillFactoryBase::fromJson(zJson);
  927. BlockReplaceItemSkillConfig* invalidUseConfig
  928. = new BlockReplaceItemSkillConfig();
  929. invalidUseConfig->setStaminaCost(
  930. (float)zJson->zValue("invalidStaminaCost")->asNumber()->getNumber());
  931. invalidUseConfig->setStaminaCostDevider(
  932. (float)zJson->zValue("invalidStaminaCostDevider")
  933. ->asNumber()
  934. ->getNumber());
  935. invalidUseConfig->setAdditionalStaminaCostDeviderPerLevel(
  936. (float)zJson->zValue("invalidAdditionalStaminaCostDeviderPerLevel")
  937. ->asNumber()
  938. ->getNumber());
  939. invalidUseConfig->setDurabilityCost(
  940. (float)zJson->zValue("invalidDurabilityCost")->asNumber()->getNumber());
  941. invalidUseConfig->setDurabilityCostDevider(
  942. (float)zJson->zValue("invalidDurabilityCostDevider")
  943. ->asNumber()
  944. ->getNumber());
  945. invalidUseConfig->setAdditionalDurabilityCostDeviderPerLevel(
  946. (float)zJson->zValue("invalidAdditionalDurabilityCostDeviderPerLevel")
  947. ->asNumber()
  948. ->getNumber());
  949. invalidUseConfig->setCooldownTicks(
  950. (int)zJson->zValue("invalidCooldownTicks")->asNumber()->getNumber());
  951. invalidUseConfig->setXpGain(0.f);
  952. result->setInvalidUseConfig(invalidUseConfig);
  953. Framework::RCArray<BlockReplaceItemSkillConfig> configs;
  954. for (Framework::JSON::JSONValue* value :
  955. *zJson->zValue("configs")->asArray())
  956. {
  957. result->addConfig(Game::INSTANCE->zTypeRegistry()
  958. ->fromJson<BlockReplaceItemSkillConfig>(value));
  959. }
  960. return result;
  961. }
  962. Framework::JSON::JSONObject* BlockReplaceItemSkillFactory::toJsonObject(
  963. BlockReplaceItemSkill* zObject) const
  964. {
  965. Framework::JSON::JSONObject* result
  966. = ItemSkillFactoryBase::toJsonObject(zObject);
  967. result->addValue("invalidStaminaCost",
  968. new Framework::JSON::JSONNumber(
  969. zObject->zInvalidUseConfig()->getStaminaCost()));
  970. result->addValue("invalidStaminaCostDevider",
  971. new Framework::JSON::JSONNumber(
  972. zObject->zInvalidUseConfig()->getStaminaCostDevider()));
  973. result->addValue("invalidAdditionalStaminaCostDeviderPerLevel",
  974. new Framework::JSON::JSONNumber(
  975. zObject->zInvalidUseConfig()
  976. ->getAdditionalStaminaCostDeviderPerLevel()));
  977. result->addValue("invalidDurabilityCost",
  978. new Framework::JSON::JSONNumber(
  979. zObject->zInvalidUseConfig()->getDurabilityCost()));
  980. result->addValue("invalidDurabilityCostDevider",
  981. new Framework::JSON::JSONNumber(
  982. zObject->zInvalidUseConfig()->getDurabilityCostDevider()));
  983. result->addValue("invalidAdditionalDurabilityCostDeviderPerLevel",
  984. new Framework::JSON::JSONNumber(
  985. zObject->zInvalidUseConfig()
  986. ->getAdditionalDurabilityCostDeviderPerLevel()));
  987. result->addValue("invalidCooldownTicks",
  988. new Framework::JSON::JSONNumber(
  989. zObject->zInvalidUseConfig()->getCooldownTicks()));
  990. Framework::JSON::JSONArray* configs = new Framework::JSON::JSONArray();
  991. for (BlockReplaceItemSkillConfig* config : zObject->getConfigs())
  992. {
  993. configs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(config));
  994. }
  995. result->addValue("configs", configs);
  996. return result;
  997. }
  998. JSONObjectValidationBuilder* BlockReplaceItemSkillFactory::addToValidator(
  999. JSONObjectValidationBuilder* builder) const
  1000. {
  1001. return ItemSkillFactoryBase::addToValidator(
  1002. builder->withRequiredNumber("invalidStaminaCost")
  1003. ->whichIsGreaterOrEqual(0.0)
  1004. ->withDefault(0.5)
  1005. ->finishNumber()
  1006. ->withRequiredNumber("invalidStaminaCostDevider")
  1007. ->whichIsGreaterOrEqual(0.0)
  1008. ->withDefault(0.8)
  1009. ->finishNumber()
  1010. ->withRequiredNumber("invalidAdditionalStaminaCostDeviderPerLevel")
  1011. ->whichIsGreaterOrEqual(0.0)
  1012. ->withDefault(0.2)
  1013. ->finishNumber()
  1014. ->withRequiredNumber("invalidDurabilityCost")
  1015. ->whichIsGreaterOrEqual(0.0)
  1016. ->withDefault(0.5)
  1017. ->finishNumber()
  1018. ->withRequiredNumber("invalidDurabilityCostDevider")
  1019. ->whichIsGreaterOrEqual(0.0)
  1020. ->withDefault(0.98)
  1021. ->finishNumber()
  1022. ->withRequiredNumber(
  1023. "invalidAdditionalDurabilityCostDeviderPerLevel")
  1024. ->whichIsGreaterOrEqual(0.0)
  1025. ->withDefault(0.02)
  1026. ->finishNumber()
  1027. ->withRequiredNumber("invalidCooldownTicks")
  1028. ->withDefault(20)
  1029. ->finishNumber()
  1030. ->withRequiredArray("configs")
  1031. ->addAcceptedTypeInArray(
  1032. Game::INSTANCE->zTypeRegistry()
  1033. ->getValidator<BlockReplaceItemSkillConfig>())
  1034. ->finishArray());
  1035. }
  1036. const char* BlockReplaceItemSkillFactory::getTypeToken() const
  1037. {
  1038. return "replaceBlock";
  1039. }
  1040. DamagingItemSkillConfig::DamagingItemSkillConfig()
  1041. : ReferenceCounter(),
  1042. targetBlockFilter(0),
  1043. damage(2.f),
  1044. damagePerHeadHardness(1.f),
  1045. baseDamageMultiplier(1.f),
  1046. damageMultiplierPerHeadHardness(0.f),
  1047. damagePerLevel(0.3f),
  1048. damageMultiplierPerLevel(0.0f),
  1049. damageDevider(1.0f),
  1050. damageDeviderPerHardness(1.0f),
  1051. staminaCost(0.00001f),
  1052. staminaCostPerDamage(0.00001f),
  1053. staminaCostPerHardness(0.001f),
  1054. staminaCostDevider(0.9f),
  1055. staminaCostDeviderPerLevel(0.1f),
  1056. durabilityCost(0.1f),
  1057. durabilityCostPerDamage(0.01f),
  1058. durabilityCostPerHardness(0.01f),
  1059. durabilityCostDevider(0.8f),
  1060. additionalDurabilityCostDeviderPerLevel(0.2f),
  1061. xpGainPerDamage(0.05f)
  1062. {}
  1063. DamagingItemSkillConfig::~DamagingItemSkillConfig()
  1064. {
  1065. if (targetBlockFilter) targetBlockFilter->release();
  1066. }
  1067. void DamagingItemSkillConfig::setTargetBlockFilter(
  1068. BlockFilter* targetBlockFilter)
  1069. {
  1070. if (this->targetBlockFilter) this->targetBlockFilter->release();
  1071. this->targetBlockFilter = targetBlockFilter;
  1072. }
  1073. BlockFilter* DamagingItemSkillConfig::zTargetBlockFilter() const
  1074. {
  1075. return targetBlockFilter;
  1076. }
  1077. void DamagingItemSkillConfig::setDamage(float damage)
  1078. {
  1079. this->damage = damage;
  1080. }
  1081. float DamagingItemSkillConfig::getDamage() const
  1082. {
  1083. return damage;
  1084. }
  1085. void DamagingItemSkillConfig::setDamagePerHeadHardness(
  1086. float damagePerHeadHardness)
  1087. {
  1088. this->damagePerHeadHardness = damagePerHeadHardness;
  1089. }
  1090. float DamagingItemSkillConfig::getDamagePerHeadHardness() const
  1091. {
  1092. return damageDeviderPerHardness;
  1093. }
  1094. void DamagingItemSkillConfig::setBaseDamageMultiplier(
  1095. float baseDamageMultiplier)
  1096. {
  1097. this->baseDamageMultiplier = baseDamageMultiplier;
  1098. }
  1099. float DamagingItemSkillConfig::getBaseDamageMultiplier() const
  1100. {
  1101. return baseDamageMultiplier;
  1102. }
  1103. void DamagingItemSkillConfig::setDamageMultiplierPerHeadHardness(
  1104. float damageMupliplierPerHeadHardness)
  1105. {
  1106. this->damageMultiplierPerHeadHardness = damageMupliplierPerHeadHardness;
  1107. }
  1108. float DamagingItemSkillConfig::getDamageMultiplierPerHeadHardness() const
  1109. {
  1110. return damageMultiplierPerHeadHardness;
  1111. }
  1112. void DamagingItemSkillConfig::setDamagePerLevel(float damagePerLevel)
  1113. {
  1114. this->damagePerLevel = damagePerLevel;
  1115. }
  1116. float DamagingItemSkillConfig::getDamagePerLevel() const
  1117. {
  1118. return damagePerLevel;
  1119. }
  1120. void DamagingItemSkillConfig::setDamageMultiplierPerLevel(
  1121. float damageMultiplierPerLevel)
  1122. {
  1123. this->damageMultiplierPerLevel = damageMultiplierPerLevel;
  1124. }
  1125. float DamagingItemSkillConfig::getDamageMultiplierPerLevel() const
  1126. {
  1127. return damageMultiplierPerLevel;
  1128. }
  1129. void DamagingItemSkillConfig::setDamageDevider(float damageDevider)
  1130. {
  1131. this->damageDevider = damageDevider;
  1132. }
  1133. float DamagingItemSkillConfig::getDamageDevider() const
  1134. {
  1135. return damageDevider;
  1136. }
  1137. void DamagingItemSkillConfig::setDamageDeviderPerHardness(
  1138. float damageDeviderPerHardness)
  1139. {
  1140. this->damageDeviderPerHardness = damageDeviderPerHardness;
  1141. }
  1142. float DamagingItemSkillConfig::getDamageDeviderPerHardness() const
  1143. {
  1144. return damageDeviderPerHardness;
  1145. }
  1146. void DamagingItemSkillConfig::setStaminaCost(float staminaCost)
  1147. {
  1148. this->staminaCost = staminaCost;
  1149. }
  1150. float DamagingItemSkillConfig::getStaminaCost() const
  1151. {
  1152. return staminaCost;
  1153. }
  1154. void DamagingItemSkillConfig::setStaminaCostPerDamage(
  1155. float staminaCostPerDamage)
  1156. {
  1157. this->staminaCostPerDamage = staminaCostPerDamage;
  1158. }
  1159. float DamagingItemSkillConfig::getStaminaCostPerDamage() const
  1160. {
  1161. return staminaCostPerDamage;
  1162. }
  1163. void DamagingItemSkillConfig::setStaminaCostPerHardness(
  1164. float staminaCostPerHardness)
  1165. {
  1166. this->staminaCostPerHardness = staminaCostPerHardness;
  1167. }
  1168. float DamagingItemSkillConfig::getStaminaCostPerHardness() const
  1169. {
  1170. return staminaCostPerHardness;
  1171. }
  1172. void DamagingItemSkillConfig::setStaminaCostDevider(float staminaCostDevider)
  1173. {
  1174. this->staminaCostDevider = staminaCostDevider;
  1175. }
  1176. float DamagingItemSkillConfig::getStaminaCostDevider() const
  1177. {
  1178. return staminaCostDevider;
  1179. }
  1180. void DamagingItemSkillConfig::setStaminaCostDeviderPerLevel(
  1181. float staminaCostDeviderPerLevel)
  1182. {
  1183. this->staminaCostDeviderPerLevel = staminaCostDeviderPerLevel;
  1184. }
  1185. float DamagingItemSkillConfig::getStaminaCostDeviderPerLevel() const
  1186. {
  1187. return staminaCostDeviderPerLevel;
  1188. }
  1189. void DamagingItemSkillConfig::setDurabilityCost(float durabilityCost)
  1190. {
  1191. this->durabilityCost = durabilityCost;
  1192. }
  1193. float DamagingItemSkillConfig::getDurabilityCost() const
  1194. {
  1195. return durabilityCost;
  1196. }
  1197. void DamagingItemSkillConfig::setDurabilityCostPerDamage(
  1198. float durabilityCostPerDamage)
  1199. {
  1200. this->durabilityCostPerDamage = durabilityCostPerDamage;
  1201. }
  1202. float DamagingItemSkillConfig::getDurabilityCostPerDamage() const
  1203. {
  1204. return durabilityCostPerDamage;
  1205. }
  1206. void DamagingItemSkillConfig::setDurabilityCostPerHardness(
  1207. float durabilityCostPerHardness)
  1208. {
  1209. this->durabilityCostPerHardness = durabilityCostPerHardness;
  1210. }
  1211. float DamagingItemSkillConfig::getDurabilityCostPerHardness() const
  1212. {
  1213. return durabilityCostPerHardness;
  1214. }
  1215. void DamagingItemSkillConfig::setDurabilityCostDevider(
  1216. float durabilityCostDevider)
  1217. {
  1218. this->durabilityCostDevider = durabilityCostDevider;
  1219. }
  1220. float DamagingItemSkillConfig::getDurabilityCostDevider() const
  1221. {
  1222. return durabilityCostDevider;
  1223. }
  1224. void DamagingItemSkillConfig::setAdditionalDurabilityCostDeviderPerLevel(
  1225. float additionalDurabilityCostDeviderPerLevel)
  1226. {
  1227. this->additionalDurabilityCostDeviderPerLevel
  1228. = additionalDurabilityCostDeviderPerLevel;
  1229. }
  1230. float DamagingItemSkillConfig::getAdditionalDurabilityCostDeviderPerLevel()
  1231. const
  1232. {
  1233. return additionalDurabilityCostDeviderPerLevel;
  1234. }
  1235. void DamagingItemSkillConfig::setXpGainPerDamage(float xpGainPerDamage)
  1236. {
  1237. this->xpGainPerDamage = xpGainPerDamage;
  1238. }
  1239. float DamagingItemSkillConfig::getXpGainPerDamage() const
  1240. {
  1241. return xpGainPerDamage;
  1242. }
  1243. DamagingItemSkillConfigFactory::DamagingItemSkillConfigFactory()
  1244. : ObjectTypeFactory()
  1245. {}
  1246. DamagingItemSkillConfig* DamagingItemSkillConfigFactory::fromJson(
  1247. Framework::JSON::JSONObject* zJson) const
  1248. {
  1249. DamagingItemSkillConfig* result = new DamagingItemSkillConfig();
  1250. result->setTargetBlockFilter(
  1251. Game::INSTANCE->zTypeRegistry()->fromJson<BlockFilter>(
  1252. zJson->zValue("targetFilter")));
  1253. result->setDamage(
  1254. (float)zJson->asObject()->zValue("damage")->asNumber()->getNumber());
  1255. result->setDamagePerHeadHardness((float)zJson->asObject()
  1256. ->zValue("damagePerHeadHardness")
  1257. ->asNumber()
  1258. ->getNumber());
  1259. result->setBaseDamageMultiplier((float)zJson->asObject()
  1260. ->zValue("baseDamageMultiplier")
  1261. ->asNumber()
  1262. ->getNumber());
  1263. result->setDamageMultiplierPerHeadHardness(
  1264. (float)zJson->asObject()
  1265. ->zValue("damageMultiplierPerHeadHardness")
  1266. ->asNumber()
  1267. ->getNumber());
  1268. result->setDamagePerLevel((float)zJson->asObject()
  1269. ->zValue("damagePerLevel")
  1270. ->asNumber()
  1271. ->getNumber());
  1272. result->setDamageMultiplierPerLevel((float)zJson->asObject()
  1273. ->zValue("damageMultiplierPerLevel")
  1274. ->asNumber()
  1275. ->getNumber());
  1276. result->setDamageDevider((float)zJson->asObject()
  1277. ->zValue("damageDevider")
  1278. ->asNumber()
  1279. ->getNumber());
  1280. result->setDamageDeviderPerHardness((float)zJson->asObject()
  1281. ->zValue("damageDeviderPerHardness")
  1282. ->asNumber()
  1283. ->getNumber());
  1284. result->setStaminaCost((float)zJson->asObject()
  1285. ->zValue("staminaCost")
  1286. ->asNumber()
  1287. ->getNumber());
  1288. result->setStaminaCostPerDamage((float)zJson->asObject()
  1289. ->zValue("staminaCostPerDamage")
  1290. ->asNumber()
  1291. ->getNumber());
  1292. result->setStaminaCostPerHardness((float)zJson->asObject()
  1293. ->zValue("staminaCostPerHardness")
  1294. ->asNumber()
  1295. ->getNumber());
  1296. result->setStaminaCostDevider((float)zJson->asObject()
  1297. ->zValue("staminaCostDevider")
  1298. ->asNumber()
  1299. ->getNumber());
  1300. result->setStaminaCostDeviderPerLevel(
  1301. (float)zJson->asObject()
  1302. ->zValue("staminaCostDeviderPerLevel")
  1303. ->asNumber()
  1304. ->getNumber());
  1305. result->setDurabilityCost((float)zJson->asObject()
  1306. ->zValue("durabilityCost")
  1307. ->asNumber()
  1308. ->getNumber());
  1309. result->setDurabilityCostPerDamage((float)zJson->asObject()
  1310. ->zValue("durabilityCostPerDamage")
  1311. ->asNumber()
  1312. ->getNumber());
  1313. result->setDurabilityCostPerHardness(
  1314. (float)zJson->asObject()
  1315. ->zValue("durabilityCostPerHardness")
  1316. ->asNumber()
  1317. ->getNumber());
  1318. result->setDurabilityCostDevider((float)zJson->asObject()
  1319. ->zValue("durabilityCostDevider")
  1320. ->asNumber()
  1321. ->getNumber());
  1322. result->setAdditionalDurabilityCostDeviderPerLevel(
  1323. (float)zJson->asObject()
  1324. ->zValue("additionalDurabilityCostDeviderPerLevel")
  1325. ->asNumber()
  1326. ->getNumber());
  1327. result->setXpGainPerDamage((float)zJson->asObject()
  1328. ->zValue("xpGainPerDamage")
  1329. ->asNumber()
  1330. ->getNumber());
  1331. return result;
  1332. }
  1333. Framework::JSON::JSONObject* DamagingItemSkillConfigFactory::toJsonObject(
  1334. DamagingItemSkillConfig* zObject) const
  1335. {
  1336. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  1337. result->addValue("targetFilter",
  1338. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zTargetBlockFilter()));
  1339. result->addValue(
  1340. "damage", new Framework::JSON::JSONNumber(zObject->getDamage()));
  1341. result->addValue("damagePerHeadHardness",
  1342. new Framework::JSON::JSONNumber(zObject->getDamagePerHeadHardness()));
  1343. result->addValue("baseDamageMultiplier",
  1344. new Framework::JSON::JSONNumber(zObject->getBaseDamageMultiplier()));
  1345. result->addValue("damageMultiplierPerHeadHardness",
  1346. new Framework::JSON::JSONNumber(
  1347. zObject->getDamageMultiplierPerHeadHardness()));
  1348. result->addValue("damagePerLevel",
  1349. new Framework::JSON::JSONNumber(zObject->getDamagePerLevel()));
  1350. result->addValue("damageMultiplierPerLevel",
  1351. new Framework::JSON::JSONNumber(
  1352. zObject->getDamageMultiplierPerLevel()));
  1353. result->addValue("damageDevider",
  1354. new Framework::JSON::JSONNumber(zObject->getDamageDevider()));
  1355. result->addValue("damageDeviderPerHardness",
  1356. new Framework::JSON::JSONNumber(
  1357. zObject->getDamageDeviderPerHardness()));
  1358. result->addValue("staminaCost",
  1359. new Framework::JSON::JSONNumber(zObject->getStaminaCost()));
  1360. result->addValue("staminaCostPerDamage",
  1361. new Framework::JSON::JSONNumber(zObject->getStaminaCostPerDamage()));
  1362. result->addValue("staminaCostPerHardness",
  1363. new Framework::JSON::JSONNumber(zObject->getStaminaCostPerHardness()));
  1364. result->addValue("staminaCostDevider",
  1365. new Framework::JSON::JSONNumber(zObject->getStaminaCostDevider()));
  1366. result->addValue("staminaCostDeviderPerLevel",
  1367. new Framework::JSON::JSONNumber(
  1368. zObject->getStaminaCostDeviderPerLevel()));
  1369. result->addValue("durabilityCost",
  1370. new Framework::JSON::JSONNumber(zObject->getDurabilityCost()));
  1371. result->addValue("durabilityCostPerDamage",
  1372. new Framework::JSON::JSONNumber(zObject->getDurabilityCostPerDamage()));
  1373. result->addValue("durabilityCostPerHardness",
  1374. new Framework::JSON::JSONNumber(
  1375. zObject->getDurabilityCostPerHardness()));
  1376. result->addValue("durabilityCostDevider",
  1377. new Framework::JSON::JSONNumber(zObject->getDurabilityCostDevider()));
  1378. result->addValue("additionalDurabilityCostDeviderPerLevel",
  1379. new Framework::JSON::JSONNumber(
  1380. zObject->getAdditionalDurabilityCostDeviderPerLevel()));
  1381. result->addValue("xpGainPerDamage",
  1382. new Framework::JSON::JSONNumber(zObject->getXpGainPerDamage()));
  1383. return result;
  1384. }
  1385. JSONObjectValidationBuilder* DamagingItemSkillConfigFactory::addToValidator(
  1386. JSONObjectValidationBuilder* builder) const
  1387. {
  1388. return builder
  1389. ->withRequiredAttribute("targetFilter",
  1390. Game::INSTANCE->zTypeRegistry()->getValidator<BlockFilter>())
  1391. ->withRequiredNumber("damage")
  1392. ->whichIsGreaterOrEqual(0.0)
  1393. ->withDefault(2.0)
  1394. ->finishNumber()
  1395. ->withRequiredNumber("damagePerHeadHardness")
  1396. ->whichIsGreaterOrEqual(0.0)
  1397. ->withDefault(1.0)
  1398. ->finishNumber()
  1399. ->withRequiredNumber("baseDamageMultiplier")
  1400. ->whichIsGreaterOrEqual(0.0)
  1401. ->withDefault(1.0)
  1402. ->finishNumber()
  1403. ->withRequiredNumber("damageMultiplierPerHeadHardness")
  1404. ->whichIsGreaterOrEqual(0.0)
  1405. ->withDefault(0.0)
  1406. ->finishNumber()
  1407. ->withRequiredNumber("damagePerLevel")
  1408. ->whichIsGreaterOrEqual(0.0)
  1409. ->withDefault(0.3)
  1410. ->finishNumber()
  1411. ->withRequiredNumber("damageMultiplierPerLevel")
  1412. ->whichIsGreaterOrEqual(0.0)
  1413. ->withDefault(0.0)
  1414. ->finishNumber()
  1415. ->withRequiredNumber("damageDevider")
  1416. ->whichIsGreaterOrEqual(0.0)
  1417. ->withDefault(1.0)
  1418. ->finishNumber()
  1419. ->withRequiredNumber("damageDeviderPerHardness")
  1420. ->whichIsGreaterOrEqual(0.0)
  1421. ->withDefault(1.0)
  1422. ->finishNumber()
  1423. ->withRequiredNumber("staminaCost")
  1424. ->whichIsGreaterOrEqual(0.0)
  1425. ->withDefault(0.0001)
  1426. ->finishNumber()
  1427. ->withRequiredNumber("staminaCostPerDamage")
  1428. ->whichIsGreaterOrEqual(0.0)
  1429. ->withDefault(0.00001)
  1430. ->finishNumber()
  1431. ->withRequiredNumber("staminaCostPerHardness")
  1432. ->whichIsGreaterOrEqual(0.0)
  1433. ->withDefault(0.001)
  1434. ->finishNumber()
  1435. ->withRequiredNumber("staminaCostDevider")
  1436. ->whichIsGreaterOrEqual(0.0)
  1437. ->withDefault(0.9)
  1438. ->finishNumber()
  1439. ->withRequiredNumber("staminaCostDeviderPerLevel")
  1440. ->whichIsGreaterOrEqual(0.0)
  1441. ->withDefault(0.1)
  1442. ->finishNumber()
  1443. ->withRequiredNumber("durabilityCost")
  1444. ->whichIsGreaterOrEqual(0.0)
  1445. ->withDefault(0.1)
  1446. ->finishNumber()
  1447. ->withRequiredNumber("durabilityCostPerDamage")
  1448. ->whichIsGreaterOrEqual(0.0)
  1449. ->withDefault(0.01)
  1450. ->finishNumber()
  1451. ->withRequiredNumber("durabilityCostPerHardness")
  1452. ->whichIsGreaterOrEqual(0.0)
  1453. ->withDefault(0.01)
  1454. ->finishNumber()
  1455. ->withRequiredNumber("durabilityCostDevider")
  1456. ->whichIsGreaterOrEqual(0.0)
  1457. ->withDefault(0.8)
  1458. ->finishNumber()
  1459. ->withRequiredNumber("additionalDurabilityCostDeviderPerLevel")
  1460. ->whichIsGreaterOrEqual(0.0)
  1461. ->withDefault(0.2)
  1462. ->finishNumber()
  1463. ->withRequiredNumber("xpGainPerDamage")
  1464. ->whichIsGreaterOrEqual(0.0)
  1465. ->withDefault(0.05)
  1466. ->finishNumber();
  1467. }
  1468. DamagingItemSkill::DamagingItemSkill()
  1469. : ItemSkill(),
  1470. invalidUseConfig(0)
  1471. {}
  1472. DamagingItemSkill::~DamagingItemSkill()
  1473. {
  1474. if (invalidUseConfig) invalidUseConfig->release();
  1475. }
  1476. bool DamagingItemSkill::use(Entity* zActor, Item* zUsedItem, Block* zTarget)
  1477. {
  1478. BasicToolItem* tool = dynamic_cast<BasicToolItem*>(zUsedItem);
  1479. DamagingItemSkillConfig* usedConfig = 0;
  1480. for (DamagingItemSkillConfig* config : configs)
  1481. {
  1482. if (config->zTargetBlockFilter()->test(zTarget))
  1483. {
  1484. usedConfig = config;
  1485. break;
  1486. }
  1487. }
  1488. bool invalid = 0;
  1489. if (usedConfig == 0)
  1490. {
  1491. usedConfig = invalidUseConfig;
  1492. invalid = 1;
  1493. }
  1494. float damage = usedConfig->getDamage()
  1495. + usedConfig->getDamagePerHeadHardness()
  1496. * (tool ? tool->getHeadMaterialHardness() : 0)
  1497. + usedConfig->getDamagePerLevel() * getLevel();
  1498. float damageMultiplier
  1499. = usedConfig->getBaseDamageMultiplier()
  1500. + usedConfig->getDamageMultiplierPerLevel() * getLevel()
  1501. + usedConfig->getDamageMultiplierPerHeadHardness()
  1502. * (tool ? tool->getHeadMaterialHardness() : 0);
  1503. if (damageMultiplier != 0)
  1504. {
  1505. damage *= damageMultiplier;
  1506. }
  1507. float damageDevider
  1508. = usedConfig->getDamageDevider()
  1509. + usedConfig->getDamageDeviderPerHardness() * zTarget->getHardness();
  1510. if (damageDevider != 0)
  1511. {
  1512. damage /= damageDevider;
  1513. }
  1514. float staminaCost
  1515. = usedConfig->getStaminaCost()
  1516. + usedConfig->getStaminaCostPerDamage() * damage
  1517. + usedConfig->getStaminaCostPerHardness() * zTarget->getHardness();
  1518. float staminaDevider
  1519. = usedConfig->getStaminaCostDevider()
  1520. + usedConfig->getStaminaCostDeviderPerLevel() * getLevel();
  1521. if (staminaDevider != 0)
  1522. {
  1523. staminaCost /= staminaDevider;
  1524. }
  1525. if (zActor->getStamina() < staminaCost)
  1526. {
  1527. return false;
  1528. }
  1529. float durabilityCost
  1530. = usedConfig->getDurabilityCost()
  1531. + usedConfig->getDurabilityCostPerDamage() * damage
  1532. + usedConfig->getDurabilityCostPerHardness() * zTarget->getHardness();
  1533. float durabilityDevider
  1534. = usedConfig->getDurabilityCostDevider()
  1535. + usedConfig->getAdditionalDurabilityCostDeviderPerLevel() * getLevel();
  1536. if (durabilityDevider != 0)
  1537. {
  1538. durabilityCost /= durabilityDevider;
  1539. }
  1540. if (zUsedItem)
  1541. {
  1542. zUsedItem->setDurability(zUsedItem->getDurability() - durabilityCost);
  1543. }
  1544. zActor->setStamina(zActor->getStamina() - staminaCost);
  1545. setXp(getXp() + usedConfig->getXpGainPerDamage() * damage);
  1546. zTarget->setHP(zTarget->getHP() - damage);
  1547. return true;
  1548. }
  1549. bool DamagingItemSkill::use(Entity* zActor, Item* zUsedItem, Entity* zTarget)
  1550. {
  1551. return false;
  1552. }
  1553. void DamagingItemSkill::setInvalidUseConfig(DamagingItemSkillConfig* config)
  1554. {
  1555. if (invalidUseConfig) invalidUseConfig->release();
  1556. invalidUseConfig = config;
  1557. }
  1558. DamagingItemSkillConfig* DamagingItemSkill::zInvalidUseConfig() const
  1559. {
  1560. return invalidUseConfig;
  1561. }
  1562. void DamagingItemSkill::addConfig(DamagingItemSkillConfig* config)
  1563. {
  1564. configs.add(config);
  1565. }
  1566. const Framework::RCArray<DamagingItemSkillConfig>&
  1567. DamagingItemSkill::getConfigs() const
  1568. {
  1569. return configs;
  1570. }
  1571. DamagingItemSkillFactory::DamagingItemSkillFactory()
  1572. : ItemSkillFactoryBase()
  1573. {}
  1574. DamagingItemSkill* DamagingItemSkillFactory::createValue(
  1575. Framework::JSON::JSONObject* zJson) const
  1576. {
  1577. return new DamagingItemSkill();
  1578. }
  1579. DamagingItemSkill* DamagingItemSkillFactory::fromJson(
  1580. Framework::JSON::JSONObject* zJson) const
  1581. {
  1582. DamagingItemSkill* result = ItemSkillFactoryBase::fromJson(zJson);
  1583. for (Framework::JSON::JSONValue* configValue :
  1584. *zJson->zValue("configs")->asArray())
  1585. {
  1586. result->addConfig(
  1587. Game::INSTANCE->zTypeRegistry()->fromJson<DamagingItemSkillConfig>(
  1588. configValue));
  1589. }
  1590. DamagingItemSkillConfig* invalidUseConfig = new DamagingItemSkillConfig();
  1591. invalidUseConfig->setDamage(0.f);
  1592. invalidUseConfig->setDamageDeviderPerHardness(0.f);
  1593. invalidUseConfig->setBaseDamageMultiplier(0.f);
  1594. invalidUseConfig->setDamageMultiplierPerHeadHardness(0.f);
  1595. invalidUseConfig->setDamagePerLevel(0.f);
  1596. invalidUseConfig->setDamageMultiplierPerLevel(0.f);
  1597. invalidUseConfig->setDamageDevider(1.f);
  1598. invalidUseConfig->setDamageDeviderPerHardness(0.f);
  1599. invalidUseConfig->setStaminaCost(
  1600. (float)zJson->zValue("invalidStaminaCost")->asNumber()->getNumber());
  1601. invalidUseConfig->setStaminaCostPerDamage(0.f);
  1602. invalidUseConfig->setStaminaCostPerHardness(
  1603. (float)zJson->zValue("invalidStaminaCostPerHardness")
  1604. ->asNumber()
  1605. ->getNumber());
  1606. invalidUseConfig->setStaminaCostDevider(
  1607. (float)zJson->zValue("invalidStaminaCostDevider")
  1608. ->asNumber()
  1609. ->getNumber());
  1610. invalidUseConfig->setStaminaCostDeviderPerLevel(
  1611. (float)zJson->zValue("invalidStaminaCostDeviderPerLevel")
  1612. ->asNumber()
  1613. ->getNumber());
  1614. invalidUseConfig->setDurabilityCost(
  1615. (float)zJson->zValue("invalidDurabilityCost")->asNumber()->getNumber());
  1616. invalidUseConfig->setDurabilityCostPerHardness(
  1617. (float)zJson->zValue("invalidDurabilityCostPerHardness")
  1618. ->asNumber()
  1619. ->getNumber());
  1620. invalidUseConfig->setDurabilityCostDevider(
  1621. (float)zJson->zValue("invalidDurabilityCostDevider")
  1622. ->asNumber()
  1623. ->getNumber());
  1624. invalidUseConfig->setAdditionalDurabilityCostDeviderPerLevel(
  1625. (float)zJson->zValue("invalidDurabilityCostDeviderPerLevel")
  1626. ->asNumber()
  1627. ->getNumber());
  1628. result->setInvalidUseConfig(invalidUseConfig);
  1629. return result;
  1630. }
  1631. Framework::JSON::JSONObject* DamagingItemSkillFactory::toJsonObject(
  1632. DamagingItemSkill* zObject) const
  1633. {
  1634. Framework::JSON::JSONObject* result
  1635. = ItemSkillFactoryBase::toJsonObject(zObject);
  1636. result->addValue(
  1637. "maxXp", new Framework::JSON::JSONNumber(zObject->getMaxXp()));
  1638. result->addValue("invalidStaminaCost",
  1639. new Framework::JSON::JSONNumber(
  1640. zObject->zInvalidUseConfig()->getStaminaCost()));
  1641. result->addValue("invalidStaminaCostPerHardness",
  1642. new Framework::JSON::JSONNumber(
  1643. zObject->zInvalidUseConfig()->getStaminaCostPerHardness()));
  1644. result->addValue("invalidStaminaCostDevider",
  1645. new Framework::JSON::JSONNumber(
  1646. zObject->zInvalidUseConfig()->getStaminaCostDevider()));
  1647. result->addValue("invalidStaminaCostDeviderPerLevel",
  1648. new Framework::JSON::JSONNumber(
  1649. zObject->zInvalidUseConfig()->getStaminaCostDeviderPerLevel()));
  1650. result->addValue("invalidDurabilityCost",
  1651. new Framework::JSON::JSONNumber(
  1652. zObject->zInvalidUseConfig()->getDurabilityCost()));
  1653. result->addValue("invalidDurabilityCostPerHardness",
  1654. new Framework::JSON::JSONNumber(
  1655. zObject->zInvalidUseConfig()->getDurabilityCostPerHardness()));
  1656. result->addValue("invalidDurabilityCostDevider",
  1657. new Framework::JSON::JSONNumber(
  1658. zObject->zInvalidUseConfig()->getDurabilityCostDevider()));
  1659. result->addValue("invalidDurabilityCostDeviderPerLevel",
  1660. new Framework::JSON::JSONNumber(
  1661. zObject->zInvalidUseConfig()
  1662. ->getAdditionalDurabilityCostDeviderPerLevel()));
  1663. Framework::JSON::JSONArray* configs = new Framework::JSON::JSONArray();
  1664. for (DamagingItemSkillConfig* config : zObject->getConfigs())
  1665. {
  1666. configs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(config));
  1667. }
  1668. result->addValue("configs", configs);
  1669. return result;
  1670. }
  1671. JSONObjectValidationBuilder* DamagingItemSkillFactory::addToValidator(
  1672. JSONObjectValidationBuilder* builder) const
  1673. {
  1674. return ItemSkillFactoryBase::addToValidator(
  1675. builder->withRequiredNumber("invalidStaminaCost")
  1676. ->whichIsGreaterOrEqual(0.0)
  1677. ->withDefault(0.5)
  1678. ->finishNumber()
  1679. ->withRequiredNumber("invalidStaminaCostPerHardness")
  1680. ->whichIsGreaterOrEqual(0.0)
  1681. ->withDefault(0.001)
  1682. ->finishNumber()
  1683. ->withRequiredNumber("invalidStaminaCostDevider")
  1684. ->whichIsGreaterOrEqual(0.0)
  1685. ->withDefault(0.8)
  1686. ->finishNumber()
  1687. ->withRequiredNumber("invalidAdditionalStaminaCostDeviderPerLevel")
  1688. ->whichIsGreaterOrEqual(0.0)
  1689. ->withDefault(0.2)
  1690. ->finishNumber()
  1691. ->withRequiredNumber("invalidDurabilityCost")
  1692. ->whichIsGreaterOrEqual(0.0)
  1693. ->withDefault(0.5)
  1694. ->finishNumber()
  1695. ->withRequiredNumber("invalidDurabilityCostPerHardness")
  1696. ->whichIsGreaterOrEqual(0.0)
  1697. ->withDefault(0.01)
  1698. ->finishNumber()
  1699. ->withRequiredNumber("invalidDurabilityCostDevider")
  1700. ->whichIsGreaterOrEqual(0.0)
  1701. ->withDefault(0.98)
  1702. ->finishNumber()
  1703. ->withRequiredNumber(
  1704. "invalidAdditionalDurabilityCostDeviderPerLevel")
  1705. ->whichIsGreaterOrEqual(0.0)
  1706. ->withDefault(0.02)
  1707. ->finishNumber()
  1708. ->withRequiredArray("configs")
  1709. ->addAcceptedTypeInArray(
  1710. Game::INSTANCE->zTypeRegistry()
  1711. ->getValidator<DamagingItemSkillConfig>())
  1712. ->finishArray());
  1713. }
  1714. const char* DamagingItemSkillFactory::getTypeToken() const
  1715. {
  1716. return "damaging";
  1717. }