CBonusTypeHandler.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 (bt.subtypeIcons.count(bonus->subtype.getNum()))
  75. return bt.subtypeIcons.at(bonus->subtype.getNum());
  76. if (bt.valueIcons.count(bonus->val))
  77. return bt.valueIcons.at(bonus->val);
  78. return bt.icon;
  79. }
  80. std::vector<JsonNode> CBonusTypeHandler::loadLegacyData()
  81. {
  82. return {};
  83. }
  84. void CBonusTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data)
  85. {
  86. auto it = bonusNameMap.find(name);
  87. if(it == bonusNameMap.end())
  88. {
  89. logBonus->warn("Unrecognized bonus name! (%s)", name);
  90. }
  91. else
  92. {
  93. CBonusType & bt = bonusTypes[vstd::to_underlying(it->second)];
  94. loadItem(data, bt, name);
  95. logBonus->trace("Loaded bonus type %s", name);
  96. }
  97. }
  98. void CBonusTypeHandler::loadObject(std::string scope, std::string name, const JsonNode & data, size_t index)
  99. {
  100. assert(0);
  101. }
  102. void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest, const std::string & name) const
  103. {
  104. dest.identifier = name;
  105. dest.hidden = source["hidden"].Bool(); //Null -> false
  106. if (!dest.hidden)
  107. {
  108. LIBRARY->generaltexth->registerString( "vcmi", dest.getNameTextID(), source["name"]);
  109. LIBRARY->generaltexth->registerString( "vcmi", dest.getDescriptionTextID(), source["description"]);
  110. }
  111. const JsonNode & graphics = source["graphics"];
  112. if(!graphics.isNull())
  113. dest.icon = ImagePath::fromJson(graphics["icon"]);
  114. for (const auto & additionalIcon : graphics["subtypeIcons"].Struct())
  115. {
  116. auto path = ImagePath::fromJson(additionalIcon.second);
  117. VLC->identifiers()->requestIdentifier(additionalIcon.second.getModScope(), additionalIcon.first, [&dest, path](int32_t index)
  118. {
  119. dest.subtypeIcons[index] = path;
  120. });
  121. }
  122. for (const auto & additionalIcon : graphics["valueIcons"].Struct())
  123. {
  124. auto path = ImagePath::fromJson(additionalIcon.second);
  125. int value = std::stoi(additionalIcon.first);
  126. dest.valueIcons[value] = path;
  127. }
  128. }
  129. VCMI_LIB_NAMESPACE_END