BasicTool.cpp 54 KB

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