Bonus.cpp 8.3 KB

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