CBonusTypeHandler.cpp 4.8 KB

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