CBonusTypeHandler.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 "VCMI_Lib.h"
  17. #include "modding/ModScope.h"
  18. #include "spells/CSpellHandler.h"
  19. #include "texts/CGeneralTextHandler.h"
  20. #include "json/JsonUtils.h"
  21. template class std::vector<VCMI_LIB_WRAP_NAMESPACE(CBonusType)>;
  22. VCMI_LIB_NAMESPACE_BEGIN
  23. ///CBonusType
  24. CBonusType::CBonusType():
  25. hidden(true)
  26. {}
  27. std::string CBonusType::getNameTextID() const
  28. {
  29. return TextIdentifier( "core", "bonus", identifier, "name").get();
  30. }
  31. std::string CBonusType::getDescriptionTextID() const
  32. {
  33. return TextIdentifier( "core", "bonus", identifier, "description").get();
  34. }
  35. ///CBonusTypeHandler
  36. CBonusTypeHandler::CBonusTypeHandler()
  37. {
  38. //register predefined bonus types
  39. #define BONUS_NAME(x) \
  40. do { \
  41. bonusTypes.push_back(CBonusType()); \
  42. } while(0);
  43. BONUS_LIST;
  44. #undef BONUS_NAME
  45. load();
  46. }
  47. CBonusTypeHandler::~CBonusTypeHandler()
  48. {
  49. //dtor
  50. }
  51. std::string CBonusTypeHandler::bonusToString(const std::shared_ptr<Bonus> & bonus, const IBonusBearer * bearer, bool description) const
  52. {
  53. const CBonusType & bt = bonusTypes[vstd::to_underlying(bonus->type)];
  54. if(bt.hidden)
  55. return "";
  56. std::string textID = description ? bt.getDescriptionTextID() : bt.getNameTextID();
  57. std::string text = VLC->generaltexth->translate(textID);
  58. if (text.find("${val}") != std::string::npos)
  59. boost::algorithm::replace_all(text, "${val}", std::to_string(bearer->valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  60. if (text.find("${subtype.creature}") != std::string::npos && bonus->subtype.as<CreatureID>().hasValue())
  61. boost::algorithm::replace_all(text, "${subtype.creature}", bonus->subtype.as<CreatureID>().toCreature()->getNamePluralTranslated());
  62. if (text.find("${subtype.spell}") != std::string::npos && bonus->subtype.as<SpellID>().hasValue())
  63. boost::algorithm::replace_all(text, "${subtype.spell}", bonus->subtype.as<SpellID>().toSpell()->getNameTranslated());
  64. if (text.find("${subtype.spellSchool}") != std::string::npos && bonus->subtype.as<SpellSchool>().hasValue())
  65. {
  66. auto school = bonus->subtype.as<SpellSchool>();
  67. auto schoolName = VLC->generaltexth->zelp[school == SpellSchool::ANY ? 458 : 454 + school].first;
  68. boost::algorithm::replace_all(text, "${subtype.spellSchool}", schoolName);
  69. }
  70. return text;
  71. }
  72. ImagePath CBonusTypeHandler::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
  73. {
  74. std::string fileName;
  75. bool fullPath = false;
  76. switch(bonus->type)
  77. {
  78. case BonusType::SPELL_IMMUNITY:
  79. {
  80. fullPath = true;
  81. if (bonus->subtype.as<SpellID>().hasValue())
  82. {
  83. const CSpell * sp = bonus->subtype.as<SpellID>().toSpell();
  84. fileName = sp->getIconImmune();
  85. }
  86. break;
  87. }
  88. case BonusType::SPELL_DAMAGE_REDUCTION: //Spell damage reduction for all schools
  89. {
  90. if (bonus->subtype.as<SpellSchool>() == SpellSchool::ANY)
  91. fileName = "E_GOLEM.bmp";
  92. if (bonus->subtype.as<SpellSchool>() == SpellSchool::AIR)
  93. fileName = "E_LIGHT.bmp";
  94. if (bonus->subtype.as<SpellSchool>() == SpellSchool::FIRE)
  95. fileName = "E_FIRE.bmp";
  96. if (bonus->subtype.as<SpellSchool>() == SpellSchool::WATER)
  97. fileName = "E_COLD.bmp";
  98. if (bonus->subtype.as<SpellSchool>() == SpellSchool::EARTH)
  99. fileName = "E_SPEATH1.bmp"; //No separate icon for earth damage
  100. break;
  101. }
  102. case BonusType::SPELL_SCHOOL_IMMUNITY: //for all school
  103. {
  104. if (bonus->subtype.as<SpellSchool>() == SpellSchool::AIR)
  105. fileName = "E_SPAIR.bmp";
  106. if (bonus->subtype.as<SpellSchool>() == SpellSchool::FIRE)
  107. fileName = "E_SPFIRE.bmp";
  108. if (bonus->subtype.as<SpellSchool>() == SpellSchool::WATER)
  109. fileName = "E_SPWATER.bmp";
  110. if (bonus->subtype.as<SpellSchool>() == SpellSchool::EARTH)
  111. fileName = "E_SPEATH.bmp";
  112. break;
  113. }
  114. case BonusType::NEGATIVE_EFFECTS_IMMUNITY:
  115. {
  116. if (bonus->subtype.as<SpellSchool>() == SpellSchool::AIR)
  117. fileName = "E_SPAIR1.bmp";
  118. if (bonus->subtype.as<SpellSchool>() == SpellSchool::FIRE)
  119. fileName = "E_SPFIRE1.bmp";
  120. if (bonus->subtype.as<SpellSchool>() == SpellSchool::WATER)
  121. fileName = "E_SPWATER1.bmp";
  122. if (bonus->subtype.as<SpellSchool>() == SpellSchool::EARTH)
  123. fileName = "E_SPEATH1.bmp";
  124. break;
  125. }
  126. case BonusType::LEVEL_SPELL_IMMUNITY:
  127. {
  128. if(vstd::iswithin(bonus->val, 1, 5))
  129. {
  130. fileName = "E_SPLVL" + std::to_string(bonus->val) + ".bmp";
  131. }
  132. break;
  133. }
  134. case BonusType::KING:
  135. {
  136. if(vstd::iswithin(bonus->val, 0, 3))
  137. {
  138. fileName = "E_KING" + std::to_string(std::max(1, bonus->val)) + ".bmp";
  139. }
  140. break;
  141. }
  142. case BonusType::GENERAL_DAMAGE_REDUCTION:
  143. {
  144. if (bonus->subtype == BonusCustomSubtype::damageTypeMelee)
  145. fileName = "DamageReductionMelee.bmp";
  146. if (bonus->subtype == BonusCustomSubtype::damageTypeRanged)
  147. fileName = "DamageReductionRanged.bmp";
  148. if (bonus->subtype == BonusCustomSubtype::damageTypeAll)
  149. fileName = "DamageReductionAll.bmp";
  150. break;
  151. }
  152. default:
  153. {
  154. const CBonusType & bt = bonusTypes[vstd::to_underlying(bonus->type)];
  155. fileName = bt.icon;
  156. fullPath = true;
  157. }
  158. break;
  159. }
  160. if(!fileName.empty() && !fullPath)
  161. fileName = "zvs/Lib1.res/" + fileName;
  162. return ImagePath::builtinTODO(fileName);
  163. }
  164. void CBonusTypeHandler::load()
  165. {
  166. JsonNode gameConf(JsonPath::builtin("config/gameConfig.json"));
  167. gameConf.setModScope(ModScope::scopeBuiltin());
  168. JsonNode config(JsonUtils::assembleFromFiles(gameConf["bonuses"]));
  169. config.setModScope("vcmi");
  170. load(config);
  171. }
  172. void CBonusTypeHandler::load(const JsonNode & config)
  173. {
  174. for(const auto & node : config.Struct())
  175. {
  176. auto it = bonusNameMap.find(node.first);
  177. if(it == bonusNameMap.end())
  178. {
  179. //TODO: new bonus
  180. // CBonusType bt;
  181. // loadItem(node.second, bt);
  182. //
  183. // auto new_id = bonusTypes.size();
  184. //
  185. // bonusTypes.push_back(bt);
  186. logBonus->warn("Unrecognized bonus name! (%s)", node.first);
  187. }
  188. else
  189. {
  190. CBonusType & bt = bonusTypes[vstd::to_underlying(it->second)];
  191. loadItem(node.second, bt, node.first);
  192. logBonus->trace("Loaded bonus type %s", node.first);
  193. }
  194. }
  195. }
  196. void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest, const std::string & name) const
  197. {
  198. dest.identifier = name;
  199. dest.hidden = source["hidden"].Bool(); //Null -> false
  200. if (!dest.hidden)
  201. {
  202. VLC->generaltexth->registerString( "vcmi", dest.getNameTextID(), source["name"]);
  203. VLC->generaltexth->registerString( "vcmi", dest.getDescriptionTextID(), source["description"]);
  204. }
  205. const JsonNode & graphics = source["graphics"];
  206. if(!graphics.isNull())
  207. dest.icon = graphics["icon"].String();
  208. }
  209. VCMI_LIB_NAMESPACE_END