CBonusTypeHandler.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 "spells/CSpellHandler.h"
  18. template class std::vector<VCMI_LIB_WRAP_NAMESPACE(CBonusType)>;
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. ///MacroString
  21. MacroString::MacroString(const std::string & format)
  22. {
  23. static const std::string MACRO_START = "${";
  24. static const std::string MACRO_END = "}";
  25. static const size_t MACRO_START_L = 2;
  26. static const size_t MACRO_END_L = 1;
  27. size_t end_pos = 0;
  28. size_t start_pos = std::string::npos;
  29. do
  30. {
  31. start_pos = format.find(MACRO_START, end_pos);
  32. if(!(start_pos == std::string::npos))
  33. {
  34. //chunk before macro
  35. items.push_back(Item(Item::STRING, format.substr(end_pos, start_pos - end_pos)));
  36. start_pos += MACRO_START_L;
  37. end_pos = format.find(MACRO_END, start_pos);
  38. if(end_pos == std::string::npos)
  39. {
  40. logBonus->warn("Format error in: %s", format);
  41. end_pos = start_pos;
  42. break;
  43. }
  44. else
  45. {
  46. items.push_back(Item(Item::MACRO, format.substr(start_pos, end_pos - start_pos)));
  47. end_pos += MACRO_END_L;
  48. }
  49. }
  50. }
  51. while(start_pos != std::string::npos);
  52. //no more macros
  53. items.push_back(Item(Item::STRING, format.substr(end_pos)));
  54. }
  55. std::string MacroString::build(const GetValue & getValue) const
  56. {
  57. std::string result;
  58. for(const Item & i : items)
  59. {
  60. switch(i.type)
  61. {
  62. case Item::MACRO:
  63. {
  64. result += getValue(i.value);
  65. break;
  66. }
  67. case Item::STRING:
  68. {
  69. result += i.value;
  70. break;
  71. }
  72. }
  73. }
  74. return result;
  75. }
  76. ///CBonusType
  77. CBonusType::CBonusType()
  78. {
  79. hidden = true;
  80. icon.clear();
  81. nameTemplate.clear();
  82. descriptionTemplate.clear();
  83. }
  84. CBonusType::~CBonusType()
  85. {
  86. }
  87. void CBonusType::buildMacros()
  88. {
  89. name = MacroString(nameTemplate);
  90. description = MacroString(descriptionTemplate);
  91. }
  92. ///CBonusTypeHandler
  93. CBonusTypeHandler::CBonusTypeHandler()
  94. {
  95. //register predefined bonus types
  96. #define BONUS_NAME(x) \
  97. do { \
  98. bonusTypes.push_back(CBonusType()); \
  99. } while(0);
  100. BONUS_LIST;
  101. #undef BONUS_NAME
  102. load();
  103. }
  104. CBonusTypeHandler::~CBonusTypeHandler()
  105. {
  106. //dtor
  107. }
  108. std::string CBonusTypeHandler::bonusToString(const std::shared_ptr<Bonus> & bonus, const IBonusBearer * bearer, bool description) const
  109. {
  110. auto getValue = [=](const std::string & name) -> std::string
  111. {
  112. if(name == "val")
  113. {
  114. return boost::lexical_cast<std::string>(bearer->valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype)));
  115. }
  116. else if(name == "subtype.creature")
  117. {
  118. const CreatureID cre(bonus->subtype);
  119. return cre.toCreature()->namePl;
  120. }
  121. else if(name == "subtype.spell")
  122. {
  123. const SpellID sp(bonus->subtype);
  124. return sp.toSpell()->name;
  125. }
  126. else if(name == "MR")
  127. {
  128. return boost::lexical_cast<std::string>(bearer->magicResistance());
  129. }
  130. else
  131. {
  132. logBonus->warn("Unknown macro in bonus config: %s", name);
  133. return "[error]";
  134. }
  135. };
  136. const CBonusType & bt = bonusTypes[bonus->type];
  137. if(bt.hidden)
  138. return "";
  139. const MacroString & macro = description ? bt.description : bt.name;
  140. return macro.build(getValue);
  141. }
  142. std::string CBonusTypeHandler::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
  143. {
  144. std::string fileName;
  145. bool fullPath = false;
  146. switch(bonus->type)
  147. {
  148. case Bonus::SECONDARY_SKILL_PREMY:
  149. if(bonus->subtype == SecondarySkill::RESISTANCE)
  150. {
  151. fileName = "E_DWARF.bmp";
  152. }
  153. break;
  154. case Bonus::SPELL_IMMUNITY:
  155. {
  156. fullPath = true;
  157. const CSpell * sp = SpellID(bonus->subtype).toSpell();
  158. fileName = sp->getIconImmune();
  159. break;
  160. }
  161. case Bonus::FIRE_IMMUNITY:
  162. switch(bonus->subtype)
  163. {
  164. case 0:
  165. fileName = "E_SPFIRE.bmp";
  166. break;//all
  167. case 1:
  168. fileName = "E_SPFIRE1.bmp";
  169. break;//not positive
  170. case 2:
  171. fileName = "E_FIRE.bmp";
  172. break;//direct damage
  173. }
  174. break;
  175. case Bonus::WATER_IMMUNITY:
  176. switch(bonus->subtype)
  177. {
  178. case 0:
  179. fileName = "E_SPWATER.bmp";
  180. break;//all
  181. case 1:
  182. fileName = "E_SPWATER1.bmp";
  183. break;//not positive
  184. case 2:
  185. fileName = "E_SPCOLD.bmp";
  186. break;//direct damage
  187. }
  188. break;
  189. case Bonus::AIR_IMMUNITY:
  190. switch(bonus->subtype)
  191. {
  192. case 0:
  193. fileName = "E_SPAIR.bmp";
  194. break;//all
  195. case 1:
  196. fileName = "E_SPAIR1.bmp";
  197. break;//not positive
  198. case 2:
  199. fileName = "E_LIGHT.bmp";
  200. break;//direct damage
  201. }
  202. break;
  203. case Bonus::EARTH_IMMUNITY:
  204. switch(bonus->subtype)
  205. {
  206. case 0:
  207. fileName = "E_SPEATH.bmp";
  208. break;//all
  209. case 1:
  210. case 2://no specific icon for direct damage immunity
  211. fileName = "E_SPEATH1.bmp";
  212. break;//not positive
  213. }
  214. break;
  215. case Bonus::LEVEL_SPELL_IMMUNITY:
  216. {
  217. if(vstd::iswithin(bonus->val, 1, 5))
  218. {
  219. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  220. }
  221. break;
  222. }
  223. case Bonus::GENERAL_DAMAGE_REDUCTION:
  224. {
  225. switch(bonus->subtype)
  226. {
  227. case 0:
  228. fileName = "DamageReductionMelee.bmp";
  229. break;
  230. case 1:
  231. fileName = "DamageReductionRanged.bmp";
  232. break;
  233. }
  234. break;
  235. }
  236. default:
  237. {
  238. const CBonusType & bt = bonusTypes[bonus->type];
  239. fileName = bt.icon;
  240. fullPath = true;
  241. }
  242. break;
  243. }
  244. if(!fileName.empty() && !fullPath)
  245. fileName = "zvs/Lib1.res/" + fileName;
  246. return fileName;
  247. }
  248. void CBonusTypeHandler::load()
  249. {
  250. const JsonNode gameConf(ResourceID("config/gameConfig.json"));
  251. const JsonNode config(JsonUtils::assembleFromFiles(gameConf["bonuses"].convertTo<std::vector<std::string>>()));
  252. load(config);
  253. }
  254. void CBonusTypeHandler::load(const JsonNode & config)
  255. {
  256. for(auto & node : config.Struct())
  257. {
  258. auto it = bonusNameMap.find(node.first);
  259. if(it == bonusNameMap.end())
  260. {
  261. //TODO: new bonus
  262. // CBonusType bt;
  263. // loadItem(node.second, bt);
  264. //
  265. // auto new_id = bonusTypes.size();
  266. //
  267. // bonusTypes.push_back(bt);
  268. logBonus->warn("Adding new bonuses not implemented (%s)", node.first);
  269. }
  270. else
  271. {
  272. CBonusType & bt = bonusTypes[it->second];
  273. loadItem(node.second, bt);
  274. logBonus->trace("Loaded bonus type %s", node.first);
  275. }
  276. }
  277. }
  278. void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest)
  279. {
  280. dest.nameTemplate = source["name"].String();
  281. dest.descriptionTemplate = source["description"].String();
  282. dest.hidden = source["hidden"].Bool(); //Null -> false
  283. const JsonNode & graphics = source["graphics"];
  284. if(!graphics.isNull())
  285. {
  286. dest.icon = graphics["icon"].String();
  287. }
  288. dest.buildMacros();
  289. }
  290. VCMI_LIB_NAMESPACE_END