CBonusTypeHandler.cpp 5.8 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)
  59. boost::algorithm::replace_all(text, "${subtype.creature}", CreatureID(bonus->subtype).toCreature()->getNamePluralTranslated());
  60. if (text.find("${subtype.spell}") != std::string::npos)
  61. boost::algorithm::replace_all(text, "${subtype.spell}", SpellID(bonus->subtype).toSpell()->getNameTranslated());
  62. return text;
  63. }
  64. std::string 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 = SpellID(bonus->subtype).toSpell();
  74. fileName = sp->getIconImmune();
  75. break;
  76. }
  77. case BonusType::FIRE_IMMUNITY:
  78. switch(bonus->subtype)
  79. {
  80. case 0:
  81. fileName = "E_SPFIRE.bmp";
  82. break;//all
  83. case 1:
  84. fileName = "E_SPFIRE1.bmp";
  85. break;//not positive
  86. case 2:
  87. fileName = "E_FIRE.bmp";
  88. break;//direct damage
  89. }
  90. break;
  91. case BonusType::WATER_IMMUNITY:
  92. switch(bonus->subtype)
  93. {
  94. case 0:
  95. fileName = "E_SPWATER.bmp";
  96. break;//all
  97. case 1:
  98. fileName = "E_SPWATER1.bmp";
  99. break;//not positive
  100. case 2:
  101. fileName = "E_SPCOLD.bmp";
  102. break;//direct damage
  103. }
  104. break;
  105. case BonusType::AIR_IMMUNITY:
  106. switch(bonus->subtype)
  107. {
  108. case 0:
  109. fileName = "E_SPAIR.bmp";
  110. break;//all
  111. case 1:
  112. fileName = "E_SPAIR1.bmp";
  113. break;//not positive
  114. case 2:
  115. fileName = "E_LIGHT.bmp";
  116. break;//direct damage
  117. }
  118. break;
  119. case BonusType::EARTH_IMMUNITY:
  120. switch(bonus->subtype)
  121. {
  122. case 0:
  123. fileName = "E_SPEATH.bmp";
  124. break;//all
  125. case 1:
  126. case 2://no specific icon for direct damage immunity
  127. fileName = "E_SPEATH1.bmp";
  128. break;//not positive
  129. }
  130. break;
  131. case BonusType::LEVEL_SPELL_IMMUNITY:
  132. {
  133. if(vstd::iswithin(bonus->val, 1, 5))
  134. {
  135. fileName = "E_SPLVL" + std::to_string(bonus->val) + ".bmp";
  136. }
  137. break;
  138. }
  139. case BonusType::KING:
  140. {
  141. if(vstd::iswithin(bonus->val, 0, 3))
  142. {
  143. fileName = "E_KING" + std::to_string(std::max(1, bonus->val)) + ".bmp";
  144. }
  145. break;
  146. }
  147. case BonusType::GENERAL_DAMAGE_REDUCTION:
  148. {
  149. switch(bonus->subtype)
  150. {
  151. case 0:
  152. fileName = "DamageReductionMelee.bmp";
  153. break;
  154. case 1:
  155. fileName = "DamageReductionRanged.bmp";
  156. break;
  157. }
  158. break;
  159. }
  160. default:
  161. {
  162. const CBonusType & bt = bonusTypes[vstd::to_underlying(bonus->type)];
  163. fileName = bt.icon;
  164. fullPath = true;
  165. }
  166. break;
  167. }
  168. if(!fileName.empty() && !fullPath)
  169. fileName = "zvs/Lib1.res/" + fileName;
  170. return fileName;
  171. }
  172. void CBonusTypeHandler::load()
  173. {
  174. const JsonNode gameConf(ResourceID("config/gameConfig.json"));
  175. const JsonNode config(JsonUtils::assembleFromFiles(gameConf["bonuses"].convertTo<std::vector<std::string>>()));
  176. load(config);
  177. }
  178. void CBonusTypeHandler::load(const JsonNode & config)
  179. {
  180. for(const auto & node : config.Struct())
  181. {
  182. auto it = bonusNameMap.find(node.first);
  183. if(it == bonusNameMap.end())
  184. {
  185. //TODO: new bonus
  186. // CBonusType bt;
  187. // loadItem(node.second, bt);
  188. //
  189. // auto new_id = bonusTypes.size();
  190. //
  191. // bonusTypes.push_back(bt);
  192. logBonus->warn("Unrecognized bonus name! (%s)", node.first);
  193. }
  194. else
  195. {
  196. CBonusType & bt = bonusTypes[vstd::to_underlying(it->second)];
  197. loadItem(node.second, bt, node.first);
  198. logBonus->trace("Loaded bonus type %s", node.first);
  199. }
  200. }
  201. }
  202. void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest, const std::string & name) const
  203. {
  204. dest.identifier = name;
  205. dest.hidden = source["hidden"].Bool(); //Null -> false
  206. if (!dest.hidden)
  207. {
  208. VLC->generaltexth->registerString( "core", dest.getNameTextID(), source["name"].String());
  209. VLC->generaltexth->registerString( "core", dest.getDescriptionTextID(), source["description"].String());
  210. }
  211. const JsonNode & graphics = source["graphics"];
  212. if(!graphics.isNull())
  213. dest.icon = graphics["icon"].String();
  214. }
  215. VCMI_LIB_NAMESPACE_END