CBonusTypeHandler.cpp 6.7 KB

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