CBonusTypeHandler.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * CBonusTypeHandler.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. #define INSTANTIATE_CBonusTypeHandler_HERE
  12. #include "CBonusTypeHandler.h"
  13. #include "filesystem/Filesystem.h"
  14. #include "CCreatureHandler.h"
  15. #include "GameConstants.h"
  16. #include "GameLibrary.h"
  17. #include "modding/ModScope.h"
  18. #include "modding/IdentifierStorage.h"
  19. #include "spells/CSpellHandler.h"
  20. #include "texts/CGeneralTextHandler.h"
  21. #include "json/JsonUtils.h"
  22. template class std::vector<VCMI_LIB_WRAP_NAMESPACE(CBonusType)>;
  23. VCMI_LIB_NAMESPACE_BEGIN
  24. ///CBonusType
  25. CBonusType::CBonusType():
  26. hidden(true)
  27. {}
  28. std::string CBonusType::getDescriptionTextID() const
  29. {
  30. return TextIdentifier( "core", "bonus", identifier, "description").get();
  31. }
  32. ///CBonusTypeHandler
  33. CBonusTypeHandler::CBonusTypeHandler()
  34. {
  35. //register predefined bonus types
  36. #define BONUS_NAME(x) \
  37. do { \
  38. bonusTypes.push_back(CBonusType()); \
  39. } while(0);
  40. BONUS_LIST;
  41. #undef BONUS_NAME
  42. }
  43. CBonusTypeHandler::~CBonusTypeHandler() = default;
  44. std::string CBonusTypeHandler::bonusToString(const std::shared_ptr<Bonus> & bonus, const IBonusBearer * bearer) const
  45. {
  46. const CBonusType & bt = bonusTypes[vstd::to_underlying(bonus->type)];
  47. if(bt.hidden)
  48. return "";
  49. std::string textID = bt.getDescriptionTextID();
  50. std::string text = LIBRARY->generaltexth->translate(textID);
  51. auto school = bonus->subtype.as<SpellSchool>();
  52. if (school.hasValue() && school != SpellSchool::ANY)
  53. {
  54. std::string schoolName = school.encode(school.getNum());
  55. std::string baseTextID = bt.getDescriptionTextID();
  56. std::string fullTextID = baseTextID + '.' + schoolName;
  57. text = LIBRARY->generaltexth->translate(fullTextID);
  58. }
  59. if (text.find("${val}") != std::string::npos)
  60. boost::algorithm::replace_all(text, "${val}", std::to_string(bearer->valOfBonuses(bonus->type, bonus->subtype)));
  61. if (text.find("${subtype.creature}") != std::string::npos && bonus->subtype.as<CreatureID>().hasValue())
  62. boost::algorithm::replace_all(text, "${subtype.creature}", bonus->subtype.as<CreatureID>().toCreature()->getNamePluralTranslated());
  63. if (text.find("${subtype.spell}") != std::string::npos && bonus->subtype.as<SpellID>().hasValue())
  64. boost::algorithm::replace_all(text, "${subtype.spell}", bonus->subtype.as<SpellID>().toSpell()->getNameTranslated());
  65. return text;
  66. }
  67. ImagePath CBonusTypeHandler::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
  68. {
  69. const CBonusType & bt = bonusTypes[vstd::to_underlying(bonus->type)];
  70. if (bonus->type == BonusType::SPELL_IMMUNITY && bonus->subtype.as<SpellID>().hasValue())
  71. {
  72. const CSpell * sp = bonus->subtype.as<SpellID>().toSpell();
  73. return sp->getIconImmune();
  74. }
  75. if (bt.subtypeIcons.count(bonus->subtype.getNum()))
  76. return bt.subtypeIcons.at(bonus->subtype.getNum());
  77. if (bt.valueIcons.count(bonus->val))
  78. return bt.valueIcons.at(bonus->val);
  79. return bt.icon;
  80. }
  81. std::vector<JsonNode> CBonusTypeHandler::loadLegacyData()
  82. {
  83. return {};
  84. }
  85. void CBonusTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  86. {
  87. auto it = bonusNameMap.find(name);
  88. if(it == bonusNameMap.end())
  89. {
  90. logBonus->warn("Unrecognized bonus name! (%s)", name);
  91. }
  92. else
  93. {
  94. CBonusType & bt = bonusTypes[vstd::to_underlying(it->second)];
  95. loadItem(data, bt, name);
  96. logBonus->trace("Loaded bonus type %s", name);
  97. }
  98. }
  99. void CBonusTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  100. {
  101. assert(0);
  102. }
  103. void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest, const std::string & name) const
  104. {
  105. dest.identifier = name;
  106. dest.hidden = source["hidden"].Bool(); //Null -> false
  107. if (!dest.hidden)
  108. LIBRARY->generaltexth->registerString( "vcmi", dest.getDescriptionTextID(), source["description"]);
  109. const JsonNode & graphics = source["graphics"];
  110. if(!graphics.isNull())
  111. dest.icon = ImagePath::fromJson(graphics["icon"]);
  112. for (const auto & additionalIcon : graphics["subtypeIcons"].Struct())
  113. {
  114. auto path = ImagePath::fromJson(additionalIcon.second);
  115. LIBRARY->identifiers()->requestIdentifier(additionalIcon.second.getModScope(), additionalIcon.first, [&dest, path](int32_t index)
  116. {
  117. dest.subtypeIcons[index] = path;
  118. });
  119. }
  120. for (const auto & additionalIcon : graphics["valueIcons"].Struct())
  121. {
  122. auto path = ImagePath::fromJson(additionalIcon.second);
  123. int value = std::stoi(additionalIcon.first);
  124. dest.valueIcons[value] = path;
  125. }
  126. }
  127. VCMI_LIB_NAMESPACE_END