CBonusTypeHandler.cpp 6.3 KB

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