CBonusTypeHandler.cpp 6.2 KB

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