Bonus.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Bonus.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 "Bonus.h"
  12. #include "Limiters.h"
  13. #include "Updaters.h"
  14. #include "Propagators.h"
  15. #include "../VCMI_Lib.h"
  16. #include "../spells/CSpellHandler.h"
  17. #include "../CCreatureHandler.h"
  18. #include "../CCreatureSet.h"
  19. #include "../CHeroHandler.h"
  20. #include "../CTownHandler.h"
  21. #include "../CGeneralTextHandler.h"
  22. #include "../CSkillHandler.h"
  23. #include "../CArtHandler.h"
  24. #include "../TerrainHandler.h"
  25. #include "../constants/StringConstants.h"
  26. #include "../battle/BattleInfo.h"
  27. #include "../modding/ModUtility.h"
  28. VCMI_LIB_NAMESPACE_BEGIN
  29. //This constructor should be placed here to avoid side effects
  30. CAddInfo::CAddInfo() = default;
  31. CAddInfo::CAddInfo(si32 value)
  32. {
  33. if(value != CAddInfo::NONE)
  34. push_back(value);
  35. }
  36. bool CAddInfo::operator==(si32 value) const
  37. {
  38. switch(size())
  39. {
  40. case 0:
  41. return value == CAddInfo::NONE;
  42. case 1:
  43. return operator[](0) == value;
  44. default:
  45. return false;
  46. }
  47. }
  48. bool CAddInfo::operator!=(si32 value) const
  49. {
  50. return !operator==(value);
  51. }
  52. si32 & CAddInfo::operator[](size_type pos)
  53. {
  54. if(pos >= size())
  55. resize(pos + 1, CAddInfo::NONE);
  56. return vector::operator[](pos);
  57. }
  58. si32 CAddInfo::operator[](size_type pos) const
  59. {
  60. return pos < size() ? vector::operator[](pos) : CAddInfo::NONE;
  61. }
  62. std::string CAddInfo::toString() const
  63. {
  64. return toJsonNode().toCompactString();
  65. }
  66. JsonNode CAddInfo::toJsonNode() const
  67. {
  68. if(size() < 2)
  69. {
  70. return JsonNode(operator[](0));
  71. }
  72. else
  73. {
  74. JsonNode node;
  75. for(si32 value : *this)
  76. node.Vector().emplace_back(value);
  77. return node;
  78. }
  79. }
  80. std::string Bonus::Description(std::optional<si32> customValue) const
  81. {
  82. MetaString descriptionHelper = description;
  83. auto valueToShow = customValue.value_or(val);
  84. if(descriptionHelper.empty())
  85. {
  86. // no custom description - try to generate one based on bonus source
  87. switch(source)
  88. {
  89. case BonusSource::ARTIFACT:
  90. descriptionHelper.appendName(sid.as<ArtifactID>());
  91. break;
  92. case BonusSource::SPELL_EFFECT:
  93. descriptionHelper.appendName(sid.as<SpellID>());
  94. break;
  95. case BonusSource::CREATURE_ABILITY:
  96. descriptionHelper.appendNamePlural(sid.as<CreatureID>());
  97. break;
  98. case BonusSource::SECONDARY_SKILL:
  99. descriptionHelper.appendTextID(sid.as<SecondarySkill>().toEntity(VLC)->getNameTextID());
  100. break;
  101. case BonusSource::HERO_SPECIAL:
  102. descriptionHelper.appendTextID(sid.as<HeroTypeID>().toEntity(VLC)->getNameTextID());
  103. break;
  104. }
  105. }
  106. if(descriptionHelper.empty())
  107. {
  108. // still no description - try to generate one based on duration
  109. if ((duration & BonusDuration::ONE_BATTLE).any())
  110. {
  111. if (val > 0)
  112. descriptionHelper.appendTextID("core.arraytxt.110"); //+%d Temporary until next battle"
  113. else
  114. descriptionHelper.appendTextID("core.arraytxt.109"); //-%d Temporary until next battle"
  115. // erase sign - already present in description string
  116. valueToShow = std::abs(valueToShow);
  117. }
  118. }
  119. if(descriptionHelper.empty())
  120. {
  121. // still no description - generate placeholder one
  122. descriptionHelper.appendRawString("Unknown");
  123. }
  124. if(valueToShow != 0)
  125. {
  126. descriptionHelper.replacePositiveNumber(valueToShow);
  127. // there is one known string that uses '%s' placeholder for bonus value:
  128. // "core.arraytxt.69" : "\nFountain of Fortune Visited %s",
  129. // So also add string replacement to handle this case
  130. if (valueToShow > 0)
  131. descriptionHelper.replaceRawString(std::to_string(valueToShow));
  132. else
  133. descriptionHelper.replaceRawString("-" + std::to_string(valueToShow));
  134. }
  135. return descriptionHelper.toString();
  136. }
  137. static JsonNode additionalInfoToJson(BonusType type, CAddInfo addInfo)
  138. {
  139. switch(type)
  140. {
  141. case BonusType::SPECIAL_UPGRADE:
  142. return JsonNode(ModUtility::makeFullIdentifier("", "creature", CreatureID::encode(addInfo[0])));
  143. default:
  144. return addInfo.toJsonNode();
  145. }
  146. }
  147. JsonNode Bonus::toJsonNode() const
  148. {
  149. JsonNode root;
  150. // only add values that might reasonably be found in config files
  151. root["type"].String() = vstd::findKey(bonusNameMap, type);
  152. if(subtype != BonusSubtypeID())
  153. root["subtype"].String() = subtype.toString();
  154. if(additionalInfo != CAddInfo::NONE)
  155. root["addInfo"] = additionalInfoToJson(type, additionalInfo);
  156. if(source != BonusSource::OTHER)
  157. root["sourceType"].String() = vstd::findKey(bonusSourceMap, source);
  158. if(targetSourceType != BonusSource::OTHER)
  159. root["targetSourceType"].String() = vstd::findKey(bonusSourceMap, targetSourceType);
  160. if(sid != BonusSourceID())
  161. root["sourceID"].String() = sid.toString();
  162. if(val != 0)
  163. root["val"].Integer() = val;
  164. if(valType != BonusValueType::ADDITIVE_VALUE)
  165. root["valueType"].String() = vstd::findKey(bonusValueMap, valType);
  166. if(!stacking.empty())
  167. root["stacking"].String() = stacking;
  168. if(!description.empty())
  169. root["description"].String() = description.toString();
  170. if(effectRange != BonusLimitEffect::NO_LIMIT)
  171. root["effectRange"].String() = vstd::findKey(bonusLimitEffect, effectRange);
  172. if(duration != BonusDuration::PERMANENT)
  173. root["duration"] = BonusDuration::toJson(duration);
  174. if(turnsRemain)
  175. root["turns"].Integer() = turnsRemain;
  176. if(limiter)
  177. root["limiters"] = limiter->toJsonNode();
  178. if(updater)
  179. root["updater"] = updater->toJsonNode();
  180. if(propagator)
  181. root["propagator"].String() = vstd::findKey(bonusPropagatorMap, propagator);
  182. return root;
  183. }
  184. Bonus::Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID ID)
  185. : Bonus(Duration, Type, Src, Val, ID, BonusSubtypeID())
  186. {}
  187. Bonus::Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID ID, BonusSubtypeID Subtype):
  188. duration(Duration),
  189. type(Type),
  190. subtype(Subtype),
  191. source(Src),
  192. val(Val),
  193. sid(ID)
  194. {
  195. targetSourceType = BonusSource::OTHER;
  196. }
  197. Bonus::Bonus(BonusDuration::Type Duration, BonusType Type, BonusSource Src, si32 Val, BonusSourceID ID, BonusSubtypeID Subtype, BonusValueType ValType):
  198. duration(Duration),
  199. type(Type),
  200. subtype(Subtype),
  201. source(Src),
  202. val(Val),
  203. sid(ID),
  204. valType(ValType)
  205. {
  206. turnsRemain = 0;
  207. effectRange = BonusLimitEffect::NO_LIMIT;
  208. targetSourceType = BonusSource::OTHER;
  209. }
  210. std::shared_ptr<Bonus> Bonus::addPropagator(const TPropagatorPtr & Propagator)
  211. {
  212. propagator = Propagator;
  213. return this->shared_from_this();
  214. }
  215. DLL_LINKAGE std::ostream & operator<<(std::ostream &out, const Bonus &bonus)
  216. {
  217. for(const auto & i : bonusNameMap)
  218. if(i.second == bonus.type)
  219. out << "\tType: " << i.first << " \t";
  220. #define printField(field) out << "\t" #field ": " << (int)bonus.field << "\n"
  221. printField(val);
  222. out << "\tSubtype: " << bonus.subtype.toString() << "\n";
  223. printField(duration.to_ulong());
  224. printField(source);
  225. out << "\tSource ID: " << bonus.sid.toString() << "\n";
  226. if(bonus.additionalInfo != CAddInfo::NONE)
  227. out << "\taddInfo: " << bonus.additionalInfo.toString() << "\n";
  228. printField(turnsRemain);
  229. printField(valType);
  230. if(!bonus.stacking.empty())
  231. out << "\tstacking: \"" << bonus.stacking << "\"\n";
  232. printField(effectRange);
  233. #undef printField
  234. if(bonus.limiter)
  235. out << "\tLimiter: " << bonus.limiter->toString() << "\n";
  236. if(bonus.updater)
  237. out << "\tUpdater: " << bonus.updater->toString() << "\n";
  238. return out;
  239. }
  240. std::shared_ptr<Bonus> Bonus::addLimiter(const TLimiterPtr & Limiter)
  241. {
  242. if (limiter)
  243. {
  244. //If we already have limiter list, retrieve it
  245. auto limiterList = std::dynamic_pointer_cast<AllOfLimiter>(limiter);
  246. if(!limiterList)
  247. {
  248. //Create a new limiter list with old limiter and the new one will be pushed later
  249. limiterList = std::make_shared<AllOfLimiter>();
  250. limiterList->add(limiter);
  251. limiter = limiterList;
  252. }
  253. limiterList->add(Limiter);
  254. }
  255. else
  256. {
  257. limiter = Limiter;
  258. }
  259. return this->shared_from_this();
  260. }
  261. // Updaters
  262. std::shared_ptr<Bonus> Bonus::addUpdater(const TUpdaterPtr & Updater)
  263. {
  264. updater = Updater;
  265. return this->shared_from_this();
  266. }
  267. VCMI_LIB_NAMESPACE_END