CBonusTypeHandler.cpp 6.3 KB

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