HeroBonus.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * HeroBonus.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 "HeroBonus.h"
  12. #include "CBonusSystemNode.h"
  13. #include "Limiters.h"
  14. #include "Updaters.h"
  15. #include "Propagators.h"
  16. #include "../VCMI_Lib.h"
  17. #include "../spells/CSpellHandler.h"
  18. #include "../CCreatureHandler.h"
  19. #include "../CCreatureSet.h"
  20. #include "../CHeroHandler.h"
  21. #include "../CTownHandler.h"
  22. #include "../CGeneralTextHandler.h"
  23. #include "../CSkillHandler.h"
  24. #include "../CStack.h"
  25. #include "../CArtHandler.h"
  26. #include "../CModHandler.h"
  27. #include "../TerrainHandler.h"
  28. #include "../StringConstants.h"
  29. #include "../battle/BattleInfo.h"
  30. VCMI_LIB_NAMESPACE_BEGIN
  31. #define BONUS_NAME(x) { #x, Bonus::x },
  32. const std::map<std::string, Bonus::BonusType> bonusNameMap = {
  33. BONUS_LIST
  34. };
  35. #undef BONUS_NAME
  36. #define BONUS_VALUE(x) { #x, Bonus::x },
  37. const std::map<std::string, Bonus::ValueType> bonusValueMap = { BONUS_VALUE_LIST };
  38. #undef BONUS_VALUE
  39. #define BONUS_SOURCE(x) { #x, Bonus::x },
  40. const std::map<std::string, Bonus::BonusSource> bonusSourceMap = { BONUS_SOURCE_LIST };
  41. #undef BONUS_SOURCE
  42. #define BONUS_ITEM(x) { #x, Bonus::x },
  43. const std::map<std::string, ui16> bonusDurationMap =
  44. {
  45. BONUS_ITEM(PERMANENT)
  46. BONUS_ITEM(ONE_BATTLE)
  47. BONUS_ITEM(ONE_DAY)
  48. BONUS_ITEM(ONE_WEEK)
  49. BONUS_ITEM(N_TURNS)
  50. BONUS_ITEM(N_DAYS)
  51. BONUS_ITEM(UNTIL_BEING_ATTACKED)
  52. BONUS_ITEM(UNTIL_ATTACK)
  53. BONUS_ITEM(STACK_GETS_TURN)
  54. BONUS_ITEM(COMMANDER_KILLED)
  55. { "UNITL_BEING_ATTACKED", Bonus::UNTIL_BEING_ATTACKED }//typo, but used in some mods
  56. };
  57. const std::map<std::string, Bonus::LimitEffect> bonusLimitEffect =
  58. {
  59. BONUS_ITEM(NO_LIMIT)
  60. BONUS_ITEM(ONLY_DISTANCE_FIGHT)
  61. BONUS_ITEM(ONLY_MELEE_FIGHT)
  62. };
  63. const std::set<std::string> deprecatedBonusSet = {
  64. "SECONDARY_SKILL_PREMY",
  65. "SECONDARY_SKILL_VAL2",
  66. "MAXED_SPELL",
  67. "LAND_MOVEMENT",
  68. "SEA_MOVEMENT",
  69. "SIGHT_RADIOUS",
  70. "NO_TYPE",
  71. "SPECIAL_SECONDARY_SKILL",
  72. "FULL_HP_REGENERATION",
  73. "KING1",
  74. "KING2",
  75. "KING3",
  76. "BLOCK_MORALE",
  77. "BLOCK_LUCK",
  78. "SELF_MORALE",
  79. "SELF_LUCK"
  80. };
  81. //This constructor should be placed here to avoid side effects
  82. CAddInfo::CAddInfo() = default;
  83. CAddInfo::CAddInfo(si32 value)
  84. {
  85. if(value != CAddInfo::NONE)
  86. push_back(value);
  87. }
  88. bool CAddInfo::operator==(si32 value) const
  89. {
  90. switch(size())
  91. {
  92. case 0:
  93. return value == CAddInfo::NONE;
  94. case 1:
  95. return operator[](0) == value;
  96. default:
  97. return false;
  98. }
  99. }
  100. bool CAddInfo::operator!=(si32 value) const
  101. {
  102. return !operator==(value);
  103. }
  104. si32 & CAddInfo::operator[](size_type pos)
  105. {
  106. if(pos >= size())
  107. resize(pos + 1, CAddInfo::NONE);
  108. return vector::operator[](pos);
  109. }
  110. si32 CAddInfo::operator[](size_type pos) const
  111. {
  112. return pos < size() ? vector::operator[](pos) : CAddInfo::NONE;
  113. }
  114. std::string CAddInfo::toString() const
  115. {
  116. return toJsonNode().toJson(true);
  117. }
  118. JsonNode CAddInfo::toJsonNode() const
  119. {
  120. if(size() < 2)
  121. {
  122. return JsonUtils::intNode(operator[](0));
  123. }
  124. else
  125. {
  126. JsonNode node(JsonNode::JsonType::DATA_VECTOR);
  127. for(si32 value : *this)
  128. node.Vector().push_back(JsonUtils::intNode(value));
  129. return node;
  130. }
  131. }
  132. std::string Bonus::Description(std::optional<si32> customValue) const
  133. {
  134. std::ostringstream str;
  135. if(description.empty())
  136. {
  137. if(stacking.empty() || stacking == "ALWAYS")
  138. {
  139. switch(source)
  140. {
  141. case ARTIFACT:
  142. str << ArtifactID(sid).toArtifact(VLC->artifacts())->getNameTranslated();
  143. break;
  144. case SPELL_EFFECT:
  145. str << SpellID(sid).toSpell(VLC->spells())->getNameTranslated();
  146. break;
  147. case CREATURE_ABILITY:
  148. str << VLC->creh->objects[sid]->getNamePluralTranslated();
  149. break;
  150. case SECONDARY_SKILL:
  151. str << VLC->skillh->getByIndex(sid)->getNameTranslated();
  152. break;
  153. case HERO_SPECIAL:
  154. str << VLC->heroh->objects[sid]->getNameTranslated();
  155. break;
  156. default:
  157. //todo: handle all possible sources
  158. str << "Unknown";
  159. break;
  160. }
  161. }
  162. else
  163. str << stacking;
  164. }
  165. else
  166. {
  167. str << description;
  168. }
  169. if(auto value = customValue.value_or(val))
  170. str << " " << std::showpos << value;
  171. return str.str();
  172. }
  173. JsonNode subtypeToJson(Bonus::BonusType type, int subtype)
  174. {
  175. switch(type)
  176. {
  177. case Bonus::PRIMARY_SKILL:
  178. return JsonUtils::stringNode("primSkill." + PrimarySkill::names[subtype]);
  179. case Bonus::SPECIAL_SPELL_LEV:
  180. case Bonus::SPECIFIC_SPELL_DAMAGE:
  181. case Bonus::SPELL:
  182. case Bonus::SPECIAL_PECULIAR_ENCHANT:
  183. case Bonus::SPECIAL_ADD_VALUE_ENCHANT:
  184. case Bonus::SPECIAL_FIXED_VALUE_ENCHANT:
  185. return JsonUtils::stringNode(CModHandler::makeFullIdentifier("", "spell", SpellID::encode(subtype)));
  186. case Bonus::IMPROVED_NECROMANCY:
  187. case Bonus::SPECIAL_UPGRADE:
  188. return JsonUtils::stringNode(CModHandler::makeFullIdentifier("", "creature", CreatureID::encode(subtype)));
  189. case Bonus::GENERATE_RESOURCE:
  190. return JsonUtils::stringNode("resource." + GameConstants::RESOURCE_NAMES[subtype]);
  191. default:
  192. return JsonUtils::intNode(subtype);
  193. }
  194. }
  195. JsonNode additionalInfoToJson(Bonus::BonusType type, CAddInfo addInfo)
  196. {
  197. switch(type)
  198. {
  199. case Bonus::SPECIAL_UPGRADE:
  200. return JsonUtils::stringNode(CModHandler::makeFullIdentifier("", "creature", CreatureID::encode(addInfo[0])));
  201. default:
  202. return addInfo.toJsonNode();
  203. }
  204. }
  205. JsonNode durationToJson(ui16 duration)
  206. {
  207. std::vector<std::string> durationNames;
  208. for(ui16 durBit = 1; durBit; durBit = durBit << 1)
  209. {
  210. if(duration & durBit)
  211. durationNames.push_back(vstd::findKey(bonusDurationMap, durBit));
  212. }
  213. if(durationNames.size() == 1)
  214. {
  215. return JsonUtils::stringNode(durationNames[0]);
  216. }
  217. else
  218. {
  219. JsonNode node(JsonNode::JsonType::DATA_VECTOR);
  220. for(const std::string & dur : durationNames)
  221. node.Vector().push_back(JsonUtils::stringNode(dur));
  222. return node;
  223. }
  224. }
  225. JsonNode Bonus::toJsonNode() const
  226. {
  227. JsonNode root(JsonNode::JsonType::DATA_STRUCT);
  228. // only add values that might reasonably be found in config files
  229. root["type"].String() = vstd::findKey(bonusNameMap, type);
  230. if(subtype != -1)
  231. root["subtype"] = subtypeToJson(type, subtype);
  232. if(additionalInfo != CAddInfo::NONE)
  233. root["addInfo"] = additionalInfoToJson(type, additionalInfo);
  234. if(duration != 0)
  235. {
  236. JsonNode durationVec(JsonNode::JsonType::DATA_VECTOR);
  237. for(const auto & kv : bonusDurationMap)
  238. {
  239. if(duration & kv.second)
  240. durationVec.Vector().push_back(JsonUtils::stringNode(kv.first));
  241. }
  242. root["duration"] = durationVec;
  243. }
  244. if(turnsRemain != 0)
  245. root["turns"].Integer() = turnsRemain;
  246. if(source != OTHER)
  247. root["sourceType"].String() = vstd::findKey(bonusSourceMap, source);
  248. if(targetSourceType != OTHER)
  249. root["targetSourceType"].String() = vstd::findKey(bonusSourceMap, targetSourceType);
  250. if(sid != 0)
  251. root["sourceID"].Integer() = sid;
  252. if(val != 0)
  253. root["val"].Integer() = val;
  254. if(valType != ADDITIVE_VALUE)
  255. root["valueType"].String() = vstd::findKey(bonusValueMap, valType);
  256. if(!stacking.empty())
  257. root["stacking"].String() = stacking;
  258. if(!description.empty())
  259. root["description"].String() = description;
  260. if(effectRange != NO_LIMIT)
  261. root["effectRange"].String() = vstd::findKey(bonusLimitEffect, effectRange);
  262. if(duration != PERMANENT)
  263. root["duration"] = durationToJson(duration);
  264. if(turnsRemain)
  265. root["turns"].Integer() = turnsRemain;
  266. if(limiter)
  267. root["limiters"] = limiter->toJsonNode();
  268. if(updater)
  269. root["updater"] = updater->toJsonNode();
  270. if(propagator)
  271. root["propagator"].String() = vstd::findKey(bonusPropagatorMap, propagator);
  272. return root;
  273. }
  274. std::string Bonus::nameForBonus() const
  275. {
  276. switch(type)
  277. {
  278. case Bonus::PRIMARY_SKILL:
  279. return PrimarySkill::names[subtype];
  280. case Bonus::SPECIAL_SPELL_LEV:
  281. case Bonus::SPECIFIC_SPELL_DAMAGE:
  282. case Bonus::SPELL:
  283. case Bonus::SPECIAL_PECULIAR_ENCHANT:
  284. case Bonus::SPECIAL_ADD_VALUE_ENCHANT:
  285. case Bonus::SPECIAL_FIXED_VALUE_ENCHANT:
  286. return VLC->spells()->getByIndex(subtype)->getJsonKey();
  287. case Bonus::SPECIAL_UPGRADE:
  288. return CreatureID::encode(subtype) + "2" + CreatureID::encode(additionalInfo[0]);
  289. case Bonus::GENERATE_RESOURCE:
  290. return GameConstants::RESOURCE_NAMES[subtype];
  291. case Bonus::STACKS_SPEED:
  292. return "speed";
  293. default:
  294. return vstd::findKey(bonusNameMap, type);
  295. }
  296. }
  297. Bonus::Bonus(Bonus::BonusDuration Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype):
  298. duration(static_cast<ui16>(Duration)),
  299. type(Type),
  300. subtype(Subtype),
  301. source(Src),
  302. val(Val),
  303. sid(ID),
  304. description(std::move(Desc))
  305. {
  306. boost::algorithm::trim(description);
  307. targetSourceType = OTHER;
  308. }
  309. Bonus::Bonus(Bonus::BonusDuration Duration, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype, ValueType ValType):
  310. duration(static_cast<ui16>(Duration)),
  311. type(Type),
  312. subtype(Subtype),
  313. source(Src),
  314. val(Val),
  315. sid(ID),
  316. valType(ValType)
  317. {
  318. turnsRemain = 0;
  319. effectRange = NO_LIMIT;
  320. targetSourceType = OTHER;
  321. }
  322. std::shared_ptr<Bonus> Bonus::addPropagator(const TPropagatorPtr & Propagator)
  323. {
  324. propagator = Propagator;
  325. return this->shared_from_this();
  326. }
  327. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  328. {
  329. for(const auto & i : bonusNameMap)
  330. if(i.second == bonus.type)
  331. out << "\tType: " << i.first << " \t";
  332. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  333. printField(val);
  334. printField(subtype);
  335. printField(duration);
  336. printField(source);
  337. printField(sid);
  338. if(bonus.additionalInfo != CAddInfo::NONE)
  339. out << "\taddInfo: " << bonus.additionalInfo.toString() << "\n";
  340. printField(turnsRemain);
  341. printField(valType);
  342. if(!bonus.stacking.empty())
  343. out << "\tstacking: \"" << bonus.stacking << "\"\n";
  344. printField(effectRange);
  345. #undef printField
  346. if(bonus.limiter)
  347. out << "\tLimiter: " << bonus.limiter->toString() << "\n";
  348. if(bonus.updater)
  349. out << "\tUpdater: " << bonus.updater->toString() << "\n";
  350. return out;
  351. }
  352. std::shared_ptr<Bonus> Bonus::addLimiter(const TLimiterPtr & Limiter)
  353. {
  354. if (limiter)
  355. {
  356. //If we already have limiter list, retrieve it
  357. auto limiterList = std::dynamic_pointer_cast<AllOfLimiter>(limiter);
  358. if(!limiterList)
  359. {
  360. //Create a new limiter list with old limiter and the new one will be pushed later
  361. limiterList = std::make_shared<AllOfLimiter>();
  362. limiterList->add(limiter);
  363. limiter = limiterList;
  364. }
  365. limiterList->add(Limiter);
  366. }
  367. else
  368. {
  369. limiter = Limiter;
  370. }
  371. return this->shared_from_this();
  372. }
  373. // Updaters
  374. std::shared_ptr<Bonus> Bonus::addUpdater(const TUpdaterPtr & Updater)
  375. {
  376. updater = Updater;
  377. return this->shared_from_this();
  378. }
  379. VCMI_LIB_NAMESPACE_END