CBonusTypeHandler.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 (bonus->subtype.as<SpellSchool>().hasValue())
  59. {
  60. auto school = bonus->subtype.as<SpellSchool>();
  61. if(school != SpellSchool::ANY)
  62. {
  63. auto specificTextID = description ? bt.getDescriptionTextID() + ".specific" : bt.getNameTextID() + ".specific";
  64. auto specificText = VLC->generaltexth->translate(specificTextID);
  65. if(specificText.find("${subtype.spellSchool}") != std::string::npos)
  66. {
  67. auto schoolName = VLC->generaltexth->translate(TextIdentifier("core.spellSchools", school).get());
  68. boost::algorithm::replace_all(specificText, "${subtype.spellSchool}", schoolName);
  69. text = specificText;
  70. }
  71. }
  72. }
  73. if (text.find("${val}") != std::string::npos)
  74. boost::algorithm::replace_all(text, "${val}", std::to_string(bearer->valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype))));
  75. if (text.find("${subtype.creature}") != std::string::npos && bonus->subtype.as<CreatureID>().hasValue())
  76. boost::algorithm::replace_all(text, "${subtype.creature}", bonus->subtype.as<CreatureID>().toCreature()->getNamePluralTranslated());
  77. if (text.find("${subtype.spell}") != std::string::npos && bonus->subtype.as<SpellID>().hasValue())
  78. boost::algorithm::replace_all(text, "${subtype.spell}", bonus->subtype.as<SpellID>().toSpell()->getNameTranslated());
  79. return text;
  80. }
  81. ImagePath CBonusTypeHandler::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
  82. {
  83. std::string fileName;
  84. bool fullPath = false;
  85. switch(bonus->type)
  86. {
  87. case BonusType::SPELL_IMMUNITY:
  88. {
  89. fullPath = true;
  90. if (bonus->subtype.as<SpellID>().hasValue())
  91. {
  92. const CSpell * sp = bonus->subtype.as<SpellID>().toSpell();
  93. fileName = sp->getIconImmune();
  94. }
  95. break;
  96. }
  97. case BonusType::SPELL_DAMAGE_REDUCTION: //Spell damage reduction for all schools
  98. {
  99. if (bonus->subtype.as<SpellSchool>() == SpellSchool::ANY)
  100. fileName = "E_GOLEM.bmp";
  101. if (bonus->subtype.as<SpellSchool>() == SpellSchool::AIR)
  102. fileName = "E_LIGHT.bmp";
  103. if (bonus->subtype.as<SpellSchool>() == SpellSchool::FIRE)
  104. fileName = "E_FIRE.bmp";
  105. if (bonus->subtype.as<SpellSchool>() == SpellSchool::WATER)
  106. fileName = "E_COLD.bmp";
  107. if (bonus->subtype.as<SpellSchool>() == SpellSchool::EARTH)
  108. fileName = "E_SPEATH1.bmp"; //No separate icon for earth damage
  109. break;
  110. }
  111. case BonusType::SPELL_SCHOOL_IMMUNITY: //for all school
  112. {
  113. if (bonus->subtype.as<SpellSchool>() == SpellSchool::AIR)
  114. fileName = "E_SPAIR.bmp";
  115. if (bonus->subtype.as<SpellSchool>() == SpellSchool::FIRE)
  116. fileName = "E_SPFIRE.bmp";
  117. if (bonus->subtype.as<SpellSchool>() == SpellSchool::WATER)
  118. fileName = "E_SPWATER.bmp";
  119. if (bonus->subtype.as<SpellSchool>() == SpellSchool::EARTH)
  120. fileName = "E_SPEATH.bmp";
  121. break;
  122. }
  123. case BonusType::NEGATIVE_EFFECTS_IMMUNITY:
  124. {
  125. if (bonus->subtype.as<SpellSchool>() == SpellSchool::AIR)
  126. fileName = "E_SPAIR1.bmp";
  127. if (bonus->subtype.as<SpellSchool>() == SpellSchool::FIRE)
  128. fileName = "E_SPFIRE1.bmp";
  129. if (bonus->subtype.as<SpellSchool>() == SpellSchool::WATER)
  130. fileName = "E_SPWATER1.bmp";
  131. if (bonus->subtype.as<SpellSchool>() == SpellSchool::EARTH)
  132. fileName = "E_SPEATH1.bmp";
  133. break;
  134. }
  135. case BonusType::LEVEL_SPELL_IMMUNITY:
  136. {
  137. if(vstd::iswithin(bonus->val, 1, 5))
  138. {
  139. fileName = "E_SPLVL" + std::to_string(bonus->val) + ".bmp";
  140. }
  141. break;
  142. }
  143. case BonusType::KING:
  144. {
  145. if(vstd::iswithin(bonus->val, 0, 3))
  146. {
  147. fileName = "E_KING" + std::to_string(std::max(1, bonus->val)) + ".bmp";
  148. }
  149. break;
  150. }
  151. case BonusType::GENERAL_DAMAGE_REDUCTION:
  152. {
  153. if (bonus->subtype == BonusCustomSubtype::damageTypeMelee)
  154. fileName = "DamageReductionMelee.bmp";
  155. if (bonus->subtype == BonusCustomSubtype::damageTypeRanged)
  156. fileName = "DamageReductionRanged.bmp";
  157. if (bonus->subtype == BonusCustomSubtype::damageTypeAll)
  158. fileName = "DamageReductionAll.bmp";
  159. break;
  160. }
  161. default:
  162. {
  163. const CBonusType & bt = bonusTypes[vstd::to_underlying(bonus->type)];
  164. fileName = bt.icon;
  165. fullPath = true;
  166. }
  167. break;
  168. }
  169. if(!fileName.empty() && !fullPath)
  170. fileName = "zvs/Lib1.res/" + fileName;
  171. return ImagePath::builtinTODO(fileName);
  172. }
  173. void CBonusTypeHandler::load()
  174. {
  175. JsonNode gameConf(JsonPath::builtin("config/gameConfig.json"));
  176. gameConf.setModScope(ModScope::scopeBuiltin());
  177. JsonNode config(JsonUtils::assembleFromFiles(gameConf["bonuses"]));
  178. config.setModScope("vcmi");
  179. load(config);
  180. }
  181. void CBonusTypeHandler::load(const JsonNode & config)
  182. {
  183. for(const auto & node : config.Struct())
  184. {
  185. auto it = bonusNameMap.find(node.first);
  186. if(it == bonusNameMap.end())
  187. {
  188. //TODO: new bonus
  189. // CBonusType bt;
  190. // loadItem(node.second, bt);
  191. //
  192. // auto new_id = bonusTypes.size();
  193. //
  194. // bonusTypes.push_back(bt);
  195. logBonus->warn("Unrecognized bonus name! (%s)", node.first);
  196. }
  197. else
  198. {
  199. CBonusType & bt = bonusTypes[vstd::to_underlying(it->second)];
  200. loadItem(node.second, bt, node.first);
  201. logBonus->trace("Loaded bonus type %s", node.first);
  202. }
  203. }
  204. }
  205. void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest, const std::string & name) const
  206. {
  207. dest.identifier = name;
  208. dest.hidden = source["hidden"].Bool(); //Null -> false
  209. if (!dest.hidden)
  210. {
  211. VLC->generaltexth->registerString( "vcmi", dest.getNameTextID(), source["name"]);
  212. VLC->generaltexth->registerString( "vcmi", dest.getDescriptionTextID(), source["description"]);
  213. }
  214. const JsonNode & graphics = source["graphics"];
  215. if(!graphics.isNull())
  216. dest.icon = graphics["icon"].String();
  217. }
  218. VCMI_LIB_NAMESPACE_END