JsonBonus.cpp 24 KB

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