JsonBonus.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. /*
  2. * JsonUtils.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "JsonBonus.h"
  12. #include "JsonValidator.h"
  13. #include "../ScopeGuard.h"
  14. #include "../bonuses/BonusParams.h"
  15. #include "../bonuses/Bonus.h"
  16. #include "../bonuses/Limiters.h"
  17. #include "../bonuses/Propagators.h"
  18. #include "../bonuses/Updaters.h"
  19. #include "../filesystem/Filesystem.h"
  20. #include "../modding/IdentifierStorage.h"
  21. #include "../VCMI_Lib.h" //for identifier resolution
  22. #include "../CGeneralTextHandler.h"
  23. #include "../constants/StringConstants.h"
  24. #include "../battle/BattleHex.h"
  25. VCMI_LIB_NAMESPACE_BEGIN
  26. static const JsonNode nullNode;
  27. static void loadBonusSubtype(BonusSubtypeID & subtype, BonusType type, const JsonNode & node)
  28. {
  29. if (node.isNull())
  30. {
  31. subtype = BonusSubtypeID();
  32. return;
  33. }
  34. if (node.isNumber()) // Compatibility code for 1.3 or older
  35. {
  36. logMod->warn("Bonus subtype must be string! (%s)", node.meta);
  37. subtype = BonusCustomSubtype(node.Integer());
  38. return;
  39. }
  40. if (!node.isString())
  41. {
  42. logMod->warn("Bonus subtype must be string! (%s)", node.meta);
  43. subtype = BonusSubtypeID();
  44. return;
  45. }
  46. switch (type)
  47. {
  48. case BonusType::MAGIC_SCHOOL_SKILL:
  49. case BonusType::SPELL_DAMAGE:
  50. case BonusType::SPELLS_OF_SCHOOL:
  51. case BonusType::SPELL_DAMAGE_REDUCTION:
  52. case BonusType::SPELL_SCHOOL_IMMUNITY:
  53. case BonusType::NEGATIVE_EFFECTS_IMMUNITY:
  54. {
  55. VLC->identifiers()->requestIdentifier( "spellSchool", node, [&subtype](int32_t identifier)
  56. {
  57. subtype = SpellSchool(identifier);
  58. });
  59. break;
  60. }
  61. case BonusType::NO_TERRAIN_PENALTY:
  62. {
  63. VLC->identifiers()->requestIdentifier( "terrain", node, [&subtype](int32_t identifier)
  64. {
  65. subtype = TerrainId(identifier);
  66. });
  67. break;
  68. }
  69. case BonusType::PRIMARY_SKILL:
  70. {
  71. VLC->identifiers()->requestIdentifier( "primarySkill", node, [&subtype](int32_t identifier)
  72. {
  73. subtype = PrimarySkill(identifier);
  74. });
  75. break;
  76. }
  77. case BonusType::IMPROVED_NECROMANCY:
  78. case BonusType::HERO_GRANTS_ATTACKS:
  79. case BonusType::BONUS_DAMAGE_CHANCE:
  80. case BonusType::BONUS_DAMAGE_PERCENTAGE:
  81. case BonusType::SPECIAL_UPGRADE:
  82. case BonusType::HATE:
  83. case BonusType::SUMMON_GUARDIANS:
  84. case BonusType::MANUAL_CONTROL:
  85. {
  86. VLC->identifiers()->requestIdentifier( "creature", node, [&subtype](int32_t identifier)
  87. {
  88. subtype = CreatureID(identifier);
  89. });
  90. break;
  91. }
  92. case BonusType::SPELL_IMMUNITY:
  93. case BonusType::SPELL_DURATION:
  94. case BonusType::SPECIAL_ADD_VALUE_ENCHANT:
  95. case BonusType::SPECIAL_FIXED_VALUE_ENCHANT:
  96. case BonusType::SPECIAL_PECULIAR_ENCHANT:
  97. case BonusType::SPECIAL_SPELL_LEV:
  98. case BonusType::SPECIFIC_SPELL_DAMAGE:
  99. case BonusType::SPELL:
  100. case BonusType::OPENING_BATTLE_SPELL:
  101. case BonusType::SPELL_LIKE_ATTACK:
  102. case BonusType::CATAPULT:
  103. case BonusType::CATAPULT_EXTRA_SHOTS:
  104. case BonusType::HEALER:
  105. case BonusType::SPELLCASTER:
  106. case BonusType::ENCHANTER:
  107. case BonusType::SPELL_AFTER_ATTACK:
  108. case BonusType::SPELL_BEFORE_ATTACK:
  109. case BonusType::SPECIFIC_SPELL_POWER:
  110. case BonusType::ENCHANTED:
  111. case BonusType::MORE_DAMAGE_FROM_SPELL:
  112. case BonusType::NOT_ACTIVE:
  113. {
  114. VLC->identifiers()->requestIdentifier( "spell", node, [&subtype](int32_t identifier)
  115. {
  116. subtype = SpellID(identifier);
  117. });
  118. break;
  119. }
  120. case BonusType::GENERATE_RESOURCE:
  121. {
  122. VLC->identifiers()->requestIdentifier( "resource", node, [&subtype](int32_t identifier)
  123. {
  124. subtype = GameResID(identifier);
  125. });
  126. break;
  127. }
  128. case BonusType::MOVEMENT:
  129. case BonusType::WATER_WALKING:
  130. case BonusType::FLYING_MOVEMENT:
  131. case BonusType::NEGATE_ALL_NATURAL_IMMUNITIES:
  132. case BonusType::CREATURE_DAMAGE:
  133. case BonusType::FLYING:
  134. case BonusType::FIRST_STRIKE:
  135. case BonusType::GENERAL_DAMAGE_REDUCTION:
  136. case BonusType::PERCENTAGE_DAMAGE_BOOST:
  137. case BonusType::SOUL_STEAL:
  138. case BonusType::TRANSMUTATION:
  139. case BonusType::DESTRUCTION:
  140. case BonusType::DEATH_STARE:
  141. case BonusType::REBIRTH:
  142. case BonusType::VISIONS:
  143. case BonusType::SPELLS_OF_LEVEL: // spell level
  144. case BonusType::CREATURE_GROWTH: // creature level
  145. {
  146. VLC->identifiers()->requestIdentifier( "bonusSubtype", node, [&subtype](int32_t identifier)
  147. {
  148. subtype = BonusCustomSubtype(identifier);
  149. });
  150. break;
  151. }
  152. default:
  153. for(const auto & i : bonusNameMap)
  154. if(i.second == type)
  155. logMod->warn("Bonus type %s does not supports subtypes!", i.first );
  156. subtype = BonusSubtypeID();
  157. }
  158. }
  159. static void loadBonusSourceInstance(BonusSourceID & sourceInstance, BonusSource sourceType, const JsonNode & node)
  160. {
  161. if (node.isNull())
  162. {
  163. sourceInstance = BonusCustomSource();
  164. return;
  165. }
  166. if (node.isNumber()) // Compatibility code for 1.3 or older
  167. {
  168. logMod->warn("Bonus source must be string!");
  169. sourceInstance = BonusCustomSource(node.Integer());
  170. return;
  171. }
  172. if (!node.isString())
  173. {
  174. logMod->warn("Bonus source must be string!");
  175. sourceInstance = BonusCustomSource();
  176. return;
  177. }
  178. switch (sourceType)
  179. {
  180. case BonusSource::ARTIFACT:
  181. case BonusSource::ARTIFACT_INSTANCE:
  182. {
  183. VLC->identifiers()->requestIdentifier( "artifact", node, [&sourceInstance](int32_t identifier)
  184. {
  185. sourceInstance = ArtifactID(identifier);
  186. });
  187. break;
  188. }
  189. case BonusSource::OBJECT_TYPE:
  190. {
  191. VLC->identifiers()->requestIdentifier( "object", node, [&sourceInstance](int32_t identifier)
  192. {
  193. sourceInstance = Obj(identifier);
  194. });
  195. break;
  196. }
  197. case BonusSource::OBJECT_INSTANCE:
  198. case BonusSource::HERO_BASE_SKILL:
  199. sourceInstance = ObjectInstanceID(ObjectInstanceID::decode(node.String()));
  200. break;
  201. case BonusSource::CREATURE_ABILITY:
  202. {
  203. VLC->identifiers()->requestIdentifier( "creature", node, [&sourceInstance](int32_t identifier)
  204. {
  205. sourceInstance = CreatureID(identifier);
  206. });
  207. break;
  208. }
  209. case BonusSource::TERRAIN_OVERLAY:
  210. {
  211. VLC->identifiers()->requestIdentifier( "spell", node, [&sourceInstance](int32_t identifier)
  212. {
  213. sourceInstance = BattleField(identifier);
  214. });
  215. break;
  216. }
  217. case BonusSource::SPELL_EFFECT:
  218. {
  219. VLC->identifiers()->requestIdentifier( "spell", node, [&sourceInstance](int32_t identifier)
  220. {
  221. sourceInstance = SpellID(identifier);
  222. });
  223. break;
  224. }
  225. case BonusSource::TOWN_STRUCTURE:
  226. assert(0); // TODO
  227. sourceInstance = BuildingTypeUniqueID();
  228. break;
  229. case BonusSource::SECONDARY_SKILL:
  230. {
  231. VLC->identifiers()->requestIdentifier( "secondarySkill", node, [&sourceInstance](int32_t identifier)
  232. {
  233. sourceInstance = SecondarySkill(identifier);
  234. });
  235. break;
  236. }
  237. case BonusSource::HERO_SPECIAL:
  238. {
  239. VLC->identifiers()->requestIdentifier( "hero", node, [&sourceInstance](int32_t identifier)
  240. {
  241. sourceInstance = HeroTypeID(identifier);
  242. });
  243. break;
  244. }
  245. case BonusSource::CAMPAIGN_BONUS:
  246. sourceInstance = CampaignScenarioID(CampaignScenarioID::decode(node.String()));
  247. break;
  248. case BonusSource::ARMY:
  249. case BonusSource::STACK_EXPERIENCE:
  250. case BonusSource::COMMANDER:
  251. case BonusSource::GLOBAL:
  252. case BonusSource::TERRAIN_NATIVE:
  253. case BonusSource::OTHER:
  254. default:
  255. sourceInstance = BonusSourceID();
  256. break;
  257. }
  258. }
  259. std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonVector & ability_vec)
  260. {
  261. auto b = std::make_shared<Bonus>();
  262. std::string type = ability_vec[0].String();
  263. auto it = bonusNameMap.find(type);
  264. if (it == bonusNameMap.end())
  265. {
  266. logMod->error("Error: invalid ability type %s.", type);
  267. return b;
  268. }
  269. b->type = it->second;
  270. b->val = static_cast<si32>(ability_vec[1].Float());
  271. loadBonusSubtype(b->subtype, b->type, ability_vec[2]);
  272. b->additionalInfo = static_cast<si32>(ability_vec[3].Float());
  273. b->duration = BonusDuration::PERMANENT; //TODO: handle flags (as integer)
  274. b->turnsRemain = 0;
  275. return b;
  276. }
  277. template <typename T>
  278. const T parseByMap(const std::map<std::string, T> & map, const JsonNode * val, const std::string & err)
  279. {
  280. if (!val->isNull())
  281. {
  282. const std::string & type = val->String();
  283. auto it = map.find(type);
  284. if (it == map.end())
  285. {
  286. logMod->error("Error: invalid %s%s.", err, type);
  287. return {};
  288. }
  289. else
  290. {
  291. return it->second;
  292. }
  293. }
  294. else
  295. return {};
  296. }
  297. template <typename T>
  298. const T parseByMapN(const std::map<std::string, T> & map, const JsonNode * val, const std::string & err)
  299. {
  300. if(val->isNumber())
  301. return static_cast<T>(val->Integer());
  302. else
  303. return parseByMap<T>(map, val, err);
  304. }
  305. void JsonUtils::resolveAddInfo(CAddInfo & var, const JsonNode & node)
  306. {
  307. const JsonNode & value = node["addInfo"];
  308. if (!value.isNull())
  309. {
  310. switch (value.getType())
  311. {
  312. case JsonNode::JsonType::DATA_INTEGER:
  313. var = static_cast<si32>(value.Integer());
  314. break;
  315. case JsonNode::JsonType::DATA_FLOAT:
  316. var = static_cast<si32>(value.Float());
  317. break;
  318. case JsonNode::JsonType::DATA_STRING:
  319. VLC->identifiers()->requestIdentifier(value, [&](si32 identifier)
  320. {
  321. var = identifier;
  322. });
  323. break;
  324. case JsonNode::JsonType::DATA_VECTOR:
  325. {
  326. const JsonVector & vec = value.Vector();
  327. var.resize(vec.size());
  328. for(int i = 0; i < vec.size(); i++)
  329. {
  330. switch(vec[i].getType())
  331. {
  332. case JsonNode::JsonType::DATA_INTEGER:
  333. var[i] = static_cast<si32>(vec[i].Integer());
  334. break;
  335. case JsonNode::JsonType::DATA_FLOAT:
  336. var[i] = static_cast<si32>(vec[i].Float());
  337. break;
  338. case JsonNode::JsonType::DATA_STRING:
  339. VLC->identifiers()->requestIdentifier(vec[i], [&var,i](si32 identifier)
  340. {
  341. var[i] = identifier;
  342. });
  343. break;
  344. default:
  345. logMod->error("Error! Wrong identifier used for value of addInfo[%d].", i);
  346. }
  347. }
  348. break;
  349. }
  350. default:
  351. logMod->error("Error! Wrong identifier used for value of addInfo.");
  352. }
  353. }
  354. }
  355. std::shared_ptr<ILimiter> JsonUtils::parseLimiter(const JsonNode & limiter)
  356. {
  357. switch(limiter.getType())
  358. {
  359. case JsonNode::JsonType::DATA_VECTOR:
  360. {
  361. const JsonVector & subLimiters = limiter.Vector();
  362. if(subLimiters.empty())
  363. {
  364. logMod->warn("Warning: empty limiter list");
  365. return std::make_shared<AllOfLimiter>();
  366. }
  367. std::shared_ptr<AggregateLimiter> result;
  368. int offset = 1;
  369. // determine limiter type and offset for sub-limiters
  370. if(subLimiters[0].getType() == JsonNode::JsonType::DATA_STRING)
  371. {
  372. const std::string & aggregator = subLimiters[0].String();
  373. if(aggregator == AllOfLimiter::aggregator)
  374. result = std::make_shared<AllOfLimiter>();
  375. else if(aggregator == AnyOfLimiter::aggregator)
  376. result = std::make_shared<AnyOfLimiter>();
  377. else if(aggregator == NoneOfLimiter::aggregator)
  378. result = std::make_shared<NoneOfLimiter>();
  379. }
  380. if(!result)
  381. {
  382. // collapse for single limiter without explicit aggregate operator
  383. if(subLimiters.size() == 1)
  384. return parseLimiter(subLimiters[0]);
  385. // implicit aggregator must be allOf
  386. result = std::make_shared<AllOfLimiter>();
  387. offset = 0;
  388. }
  389. if(subLimiters.size() == offset)
  390. logMod->warn("Warning: empty sub-limiter list");
  391. for(int sl = offset; sl < subLimiters.size(); ++sl)
  392. result->add(parseLimiter(subLimiters[sl]));
  393. return result;
  394. }
  395. break;
  396. case JsonNode::JsonType::DATA_STRING: //pre-defined limiters
  397. return parseByMap(bonusLimiterMap, &limiter, "limiter type ");
  398. break;
  399. case JsonNode::JsonType::DATA_STRUCT: //customizable limiters
  400. {
  401. std::string limiterType = limiter["type"].String();
  402. const JsonVector & parameters = limiter["parameters"].Vector();
  403. if(limiterType == "CREATURE_TYPE_LIMITER")
  404. {
  405. auto creatureLimiter = std::make_shared<CCreatureTypeLimiter>();
  406. VLC->identifiers()->requestIdentifier("creature", parameters[0], [=](si32 creature)
  407. {
  408. creatureLimiter->setCreature(CreatureID(creature));
  409. });
  410. auto includeUpgrades = false;
  411. if(parameters.size() > 1)
  412. {
  413. bool success = true;
  414. includeUpgrades = parameters[1].TryBoolFromString(success);
  415. if(!success)
  416. logMod->error("Second parameter of '%s' limiter should be Bool", limiterType);
  417. }
  418. creatureLimiter->includeUpgrades = includeUpgrades;
  419. return creatureLimiter;
  420. }
  421. else if(limiterType == "HAS_ANOTHER_BONUS_LIMITER")
  422. {
  423. std::string anotherBonusType = parameters[0].String();
  424. auto it = bonusNameMap.find(anotherBonusType);
  425. if(it == bonusNameMap.end())
  426. {
  427. logMod->error("Error: invalid ability type %s.", anotherBonusType);
  428. }
  429. else
  430. {
  431. auto bonusLimiter = std::make_shared<HasAnotherBonusLimiter>();
  432. bonusLimiter->type = it->second;
  433. auto findSource = [&](const JsonNode & parameter)
  434. {
  435. if(parameter.getType() == JsonNode::JsonType::DATA_STRUCT)
  436. {
  437. auto sourceIt = bonusSourceMap.find(parameter["type"].String());
  438. if(sourceIt != bonusSourceMap.end())
  439. {
  440. bonusLimiter->source = sourceIt->second;
  441. bonusLimiter->isSourceRelevant = true;
  442. if(!parameter["id"].isNull()) {
  443. loadBonusSourceInstance(bonusLimiter->sid, bonusLimiter->source, parameter["id"]);
  444. bonusLimiter->isSourceIDRelevant = true;
  445. }
  446. }
  447. }
  448. return false;
  449. };
  450. if(parameters.size() > 1)
  451. {
  452. if(findSource(parameters[1]) && parameters.size() == 2)
  453. return bonusLimiter;
  454. else
  455. {
  456. loadBonusSubtype(bonusLimiter->subtype, bonusLimiter->type, parameters[1]);
  457. bonusLimiter->isSubtypeRelevant = true;
  458. if(parameters.size() > 2)
  459. findSource(parameters[2]);
  460. }
  461. }
  462. return bonusLimiter;
  463. }
  464. }
  465. else if(limiterType == "CREATURE_ALIGNMENT_LIMITER")
  466. {
  467. int alignment = vstd::find_pos(GameConstants::ALIGNMENT_NAMES, parameters[0].String());
  468. if(alignment == -1)
  469. logMod->error("Error: invalid alignment %s.", parameters[0].String());
  470. else
  471. return std::make_shared<CreatureAlignmentLimiter>(static_cast<EAlignment>(alignment));
  472. }
  473. else if(limiterType == "FACTION_LIMITER" || limiterType == "CREATURE_FACTION_LIMITER") //Second name is deprecated, 1.2 compat
  474. {
  475. auto factionLimiter = std::make_shared<FactionLimiter>();
  476. VLC->identifiers()->requestIdentifier("faction", parameters[0], [=](si32 faction)
  477. {
  478. factionLimiter->faction = FactionID(faction);
  479. });
  480. return factionLimiter;
  481. }
  482. else if(limiterType == "CREATURE_LEVEL_LIMITER")
  483. {
  484. auto levelLimiter = std::make_shared<CreatureLevelLimiter>();
  485. if(!parameters.empty()) //If parameters is empty, level limiter works as CREATURES_ONLY limiter
  486. {
  487. levelLimiter->minLevel = parameters[0].Integer();
  488. if(parameters[1].isNumber())
  489. levelLimiter->maxLevel = parameters[1].Integer();
  490. }
  491. return levelLimiter;
  492. }
  493. else if(limiterType == "CREATURE_TERRAIN_LIMITER")
  494. {
  495. auto terrainLimiter = std::make_shared<CreatureTerrainLimiter>();
  496. if(!parameters.empty())
  497. {
  498. VLC->identifiers()->requestIdentifier("terrain", parameters[0], [=](si32 terrain)
  499. {
  500. //TODO: support limiters
  501. //terrainLimiter->terrainType = terrain;
  502. });
  503. }
  504. return terrainLimiter;
  505. }
  506. else if(limiterType == "UNIT_ON_HEXES") {
  507. auto hexLimiter = std::make_shared<UnitOnHexLimiter>();
  508. if(!parameters.empty())
  509. {
  510. for (const auto & parameter: parameters){
  511. if(parameter.isNumber())
  512. hexLimiter->applicableHexes.insert(BattleHex(parameter.Integer()));
  513. }
  514. }
  515. return hexLimiter;
  516. }
  517. else
  518. {
  519. logMod->error("Error: invalid customizable limiter type %s.", limiterType);
  520. }
  521. }
  522. break;
  523. default:
  524. break;
  525. }
  526. return nullptr;
  527. }
  528. std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonNode &ability)
  529. {
  530. auto b = std::make_shared<Bonus>();
  531. if (!parseBonus(ability, b.get()))
  532. {
  533. // caller code can not handle this case and presumes that returned bonus is always valid
  534. logGlobal->error("Failed to parse bonus! Json config was %S ", ability.toString());
  535. b->type = BonusType::NONE;
  536. return b;
  537. }
  538. return b;
  539. }
  540. std::shared_ptr<Bonus> JsonUtils::parseBuildingBonus(const JsonNode & ability, const FactionID & faction, const BuildingID & building, const std::string & description)
  541. {
  542. /* duration = BonusDuration::PERMANENT
  543. source = BonusSource::TOWN_STRUCTURE
  544. bonusType, val, subtype - get from json
  545. */
  546. auto b = std::make_shared<Bonus>(BonusDuration::PERMANENT, BonusType::NONE, BonusSource::TOWN_STRUCTURE, 0, BuildingTypeUniqueID(faction, building), description);
  547. if(!parseBonus(ability, b.get()))
  548. return nullptr;
  549. return b;
  550. }
  551. static BonusParams convertDeprecatedBonus(const JsonNode &ability)
  552. {
  553. if(vstd::contains(deprecatedBonusSet, ability["type"].String()))
  554. {
  555. logMod->warn("There is deprecated bonus found:\n%s\nTrying to convert...", ability.toString());
  556. auto params = BonusParams(ability["type"].String(),
  557. ability["subtype"].isString() ? ability["subtype"].String() : "",
  558. ability["subtype"].isNumber() ? ability["subtype"].Integer() : -1);
  559. if(params.isConverted)
  560. {
  561. if(ability["type"].String() == "SECONDARY_SKILL_PREMY" && bonusValueMap.find(ability["valueType"].String())->second == BonusValueType::PERCENT_TO_BASE) //assume secondary skill special
  562. {
  563. params.valueType = BonusValueType::PERCENT_TO_TARGET_TYPE;
  564. params.targetType = BonusSource::SECONDARY_SKILL;
  565. }
  566. logMod->warn("Please, use this bonus:\n%s\nConverted successfully!", params.toJson().toString());
  567. return params;
  568. }
  569. else
  570. logMod->error("Cannot convert bonus!\n%s", ability.toString());
  571. }
  572. BonusParams ret;
  573. ret.isConverted = false;
  574. return ret;
  575. }
  576. static TUpdaterPtr parseUpdater(const JsonNode & updaterJson)
  577. {
  578. switch(updaterJson.getType())
  579. {
  580. case JsonNode::JsonType::DATA_STRING:
  581. return parseByMap(bonusUpdaterMap, &updaterJson, "updater type ");
  582. break;
  583. case JsonNode::JsonType::DATA_STRUCT:
  584. if(updaterJson["type"].String() == "GROWS_WITH_LEVEL")
  585. {
  586. auto updater = std::make_shared<GrowsWithLevelUpdater>();
  587. const JsonVector param = updaterJson["parameters"].Vector();
  588. updater->valPer20 = static_cast<int>(param[0].Integer());
  589. if(param.size() > 1)
  590. updater->stepSize = static_cast<int>(param[1].Integer());
  591. return updater;
  592. }
  593. else if (updaterJson["type"].String() == "ARMY_MOVEMENT")
  594. {
  595. auto updater = std::make_shared<ArmyMovementUpdater>();
  596. if(updaterJson["parameters"].isVector())
  597. {
  598. const auto & param = updaterJson["parameters"].Vector();
  599. if(param.size() < 4)
  600. logMod->warn("Invalid ARMY_MOVEMENT parameters, using default!");
  601. else
  602. {
  603. updater->base = static_cast<si32>(param.at(0).Integer());
  604. updater->divider = static_cast<si32>(param.at(1).Integer());
  605. updater->multiplier = static_cast<si32>(param.at(2).Integer());
  606. updater->max = static_cast<si32>(param.at(3).Integer());
  607. }
  608. return updater;
  609. }
  610. }
  611. else
  612. logMod->warn("Unknown updater type \"%s\"", updaterJson["type"].String());
  613. break;
  614. }
  615. return nullptr;
  616. }
  617. bool JsonUtils::parseBonus(const JsonNode &ability, Bonus *b)
  618. {
  619. const JsonNode * value = nullptr;
  620. std::string type = ability["type"].String();
  621. auto it = bonusNameMap.find(type);
  622. auto params = std::make_unique<BonusParams>(false);
  623. if (it == bonusNameMap.end())
  624. {
  625. params = std::make_unique<BonusParams>(convertDeprecatedBonus(ability));
  626. if(!params->isConverted)
  627. {
  628. logMod->error("Error: invalid ability type %s.", type);
  629. return false;
  630. }
  631. b->type = params->type;
  632. b->val = params->val.value_or(0);
  633. b->valType = params->valueType.value_or(BonusValueType::ADDITIVE_VALUE);
  634. if(params->targetType)
  635. b->targetSourceType = params->targetType.value();
  636. }
  637. else
  638. b->type = it->second;
  639. loadBonusSubtype(b->subtype, b->type, params->isConverted ? params->toJson()["subtype"] : ability["subtype"]);
  640. if(!params->isConverted)
  641. {
  642. b->val = static_cast<si32>(ability["val"].Float());
  643. value = &ability["valueType"];
  644. if (!value->isNull())
  645. b->valType = static_cast<BonusValueType>(parseByMapN(bonusValueMap, value, "value type "));
  646. }
  647. b->stacking = ability["stacking"].String();
  648. resolveAddInfo(b->additionalInfo, ability);
  649. b->turnsRemain = static_cast<si32>(ability["turns"].Float());
  650. if(!ability["description"].isNull())
  651. {
  652. if (ability["description"].isString())
  653. b->description = ability["description"].String();
  654. if (ability["description"].isNumber())
  655. b->description = VLC->generaltexth->translate("core.arraytxt", ability["description"].Integer());
  656. }
  657. value = &ability["effectRange"];
  658. if (!value->isNull())
  659. b->effectRange = static_cast<BonusLimitEffect>(parseByMapN(bonusLimitEffect, value, "effect range "));
  660. value = &ability["duration"];
  661. if (!value->isNull())
  662. {
  663. switch (value->getType())
  664. {
  665. case JsonNode::JsonType::DATA_STRING:
  666. b->duration = parseByMap(bonusDurationMap, value, "duration type ");
  667. break;
  668. case JsonNode::JsonType::DATA_VECTOR:
  669. {
  670. BonusDuration::Type dur = 0;
  671. for (const JsonNode & d : value->Vector())
  672. dur |= parseByMapN(bonusDurationMap, &d, "duration type ");
  673. b->duration = dur;
  674. }
  675. break;
  676. default:
  677. logMod->error("Error! Wrong bonus duration format.");
  678. }
  679. }
  680. value = &ability["sourceType"];
  681. if (!value->isNull())
  682. b->source = static_cast<BonusSource>(parseByMap(bonusSourceMap, value, "source type "));
  683. if (!ability["sourceID"].isNull())
  684. loadBonusSourceInstance(b->sid, b->source, ability["sourceID"]);
  685. value = &ability["targetSourceType"];
  686. if (!value->isNull())
  687. b->targetSourceType = static_cast<BonusSource>(parseByMap(bonusSourceMap, value, "target type "));
  688. value = &ability["limiters"];
  689. if (!value->isNull())
  690. b->limiter = parseLimiter(*value);
  691. value = &ability["propagator"];
  692. if (!value->isNull())
  693. {
  694. //ALL_CREATURES old propagator compatibility
  695. if(value->String() == "ALL_CREATURES")
  696. {
  697. logMod->warn("ALL_CREATURES propagator is deprecated. Use GLOBAL_EFFECT propagator with CREATURES_ONLY limiter");
  698. b->addLimiter(std::make_shared<CreatureLevelLimiter>());
  699. b->propagator = bonusPropagatorMap.at("GLOBAL_EFFECT");
  700. }
  701. else
  702. b->propagator = parseByMap(bonusPropagatorMap, value, "propagator type ");
  703. }
  704. value = &ability["updater"];
  705. if(!value->isNull())
  706. b->addUpdater(parseUpdater(*value));
  707. value = &ability["propagationUpdater"];
  708. if(!value->isNull())
  709. b->propagationUpdater = parseUpdater(*value);
  710. return true;
  711. }
  712. CSelector JsonUtils::parseSelector(const JsonNode & ability)
  713. {
  714. CSelector ret = Selector::all;
  715. // Recursive parsers for anyOf, allOf, noneOf
  716. const auto * value = &ability["allOf"];
  717. if(value->isVector())
  718. {
  719. for(const auto & andN : value->Vector())
  720. ret = ret.And(parseSelector(andN));
  721. }
  722. value = &ability["anyOf"];
  723. if(value->isVector())
  724. {
  725. CSelector base = Selector::none;
  726. for(const auto & andN : value->Vector())
  727. base = base.Or(parseSelector(andN));
  728. ret = ret.And(base);
  729. }
  730. value = &ability["noneOf"];
  731. if(value->isVector())
  732. {
  733. CSelector base = Selector::none;
  734. for(const auto & andN : value->Vector())
  735. base = base.Or(parseSelector(andN));
  736. ret = ret.And(base.Not());
  737. }
  738. BonusType type = BonusType::NONE;
  739. // Actual selector parser
  740. value = &ability["type"];
  741. if(value->isString())
  742. {
  743. auto it = bonusNameMap.find(value->String());
  744. if(it != bonusNameMap.end())
  745. {
  746. type = it->second;
  747. ret = ret.And(Selector::type()(it->second));
  748. }
  749. }
  750. value = &ability["subtype"];
  751. if(!value->isNull() && type != BonusType::NONE)
  752. {
  753. BonusSubtypeID subtype;
  754. loadBonusSubtype(subtype, type, ability);
  755. ret = ret.And(Selector::subtype()(subtype));
  756. }
  757. value = &ability["sourceType"];
  758. std::optional<BonusSource> src = std::nullopt; //Fixes for GCC false maybe-uninitialized
  759. std::optional<BonusSourceID> id = std::nullopt;
  760. if(value->isString())
  761. {
  762. auto it = bonusSourceMap.find(value->String());
  763. if(it != bonusSourceMap.end())
  764. src = it->second;
  765. }
  766. value = &ability["sourceID"];
  767. if(!value->isNull() && src.has_value())
  768. {
  769. loadBonusSourceInstance(*id, *src, ability);
  770. }
  771. if(src && id)
  772. ret = ret.And(Selector::source(*src, *id));
  773. else if(src)
  774. ret = ret.And(Selector::sourceTypeSel(*src));
  775. value = &ability["targetSourceType"];
  776. if(value->isString())
  777. {
  778. auto it = bonusSourceMap.find(value->String());
  779. if(it != bonusSourceMap.end())
  780. ret = ret.And(Selector::targetSourceType()(it->second));
  781. }
  782. value = &ability["valueType"];
  783. if(value->isString())
  784. {
  785. auto it = bonusValueMap.find(value->String());
  786. if(it != bonusValueMap.end())
  787. ret = ret.And(Selector::valueType(it->second));
  788. }
  789. CAddInfo info;
  790. value = &ability["addInfo"];
  791. if(!value->isNull())
  792. {
  793. resolveAddInfo(info, ability["addInfo"]);
  794. ret = ret.And(Selector::info()(info));
  795. }
  796. value = &ability["effectRange"];
  797. if(value->isString())
  798. {
  799. auto it = bonusLimitEffect.find(value->String());
  800. if(it != bonusLimitEffect.end())
  801. ret = ret.And(Selector::effectRange()(it->second));
  802. }
  803. value = &ability["lastsTurns"];
  804. if(value->isNumber())
  805. ret = ret.And(Selector::turns(value->Integer()));
  806. value = &ability["lastsDays"];
  807. if(value->isNumber())
  808. ret = ret.And(Selector::days(value->Integer()));
  809. return ret;
  810. }
  811. VCMI_LIB_NAMESPACE_END