CBonusTypeHandler.cpp 6.3 KB

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