JsonBonus.cpp 24 KB

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