BasicTool.cpp 53 KB

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