Bonus.cpp 8.2 KB

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