CBonusTypeHandler.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 = nameTemplate = descriptionTemplate = "";
  81. }
  82. CBonusType::~CBonusType()
  83. {
  84. }
  85. void CBonusType::buildMacros()
  86. {
  87. name = MacroString(nameTemplate);
  88. description = MacroString(descriptionTemplate);
  89. }
  90. ///CBonusTypeHandler
  91. CBonusTypeHandler::CBonusTypeHandler()
  92. {
  93. //register predefined bonus types
  94. #define BONUS_NAME(x) \
  95. do { \
  96. bonusTypes.push_back(CBonusType()); \
  97. } while(0);
  98. BONUS_LIST;
  99. #undef BONUS_NAME
  100. load();
  101. }
  102. CBonusTypeHandler::~CBonusTypeHandler()
  103. {
  104. //dtor
  105. }
  106. std::string CBonusTypeHandler::bonusToString(const std::shared_ptr<Bonus> & bonus, const IBonusBearer * bearer, bool description) const
  107. {
  108. auto getValue = [=](const std::string & name) -> std::string
  109. {
  110. if(name == "val")
  111. {
  112. return boost::lexical_cast<std::string>(bearer->valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype)));
  113. }
  114. else if(name == "subtype.creature")
  115. {
  116. const CreatureID cre(bonus->subtype);
  117. return cre.toCreature()->namePl;
  118. }
  119. else if(name == "subtype.spell")
  120. {
  121. const SpellID sp(bonus->subtype);
  122. return sp.toSpell()->name;
  123. }
  124. else if(name == "MR")
  125. {
  126. return boost::lexical_cast<std::string>(bearer->magicResistance());
  127. }
  128. else
  129. {
  130. logBonus->warn("Unknown macro in bonus config: %s", name);
  131. return "[error]";
  132. }
  133. };
  134. const CBonusType & bt = bonusTypes[bonus->type];
  135. if(bt.hidden)
  136. return "";
  137. const MacroString & macro = description ? bt.description : bt.name;
  138. return macro.build(getValue);
  139. }
  140. std::string CBonusTypeHandler::bonusToGraphics(const std::shared_ptr<Bonus> & bonus) const
  141. {
  142. std::string fileName;
  143. bool fullPath = false;
  144. switch(bonus->type)
  145. {
  146. case Bonus::SECONDARY_SKILL_PREMY:
  147. if(bonus->subtype == SecondarySkill::RESISTANCE)
  148. {
  149. fileName = "E_DWARF.bmp";
  150. }
  151. break;
  152. case Bonus::SPELL_IMMUNITY:
  153. {
  154. fullPath = true;
  155. const CSpell * sp = SpellID(bonus->subtype).toSpell();
  156. fileName = sp->getIconImmune();
  157. break;
  158. }
  159. case Bonus::FIRE_IMMUNITY:
  160. switch(bonus->subtype)
  161. {
  162. case 0:
  163. fileName = "E_SPFIRE.bmp";
  164. break;//all
  165. case 1:
  166. fileName = "E_SPFIRE1.bmp";
  167. break;//not positive
  168. case 2:
  169. fileName = "E_FIRE.bmp";
  170. break;//direct damage
  171. }
  172. break;
  173. case Bonus::WATER_IMMUNITY:
  174. switch(bonus->subtype)
  175. {
  176. case 0:
  177. fileName = "E_SPWATER.bmp";
  178. break;//all
  179. case 1:
  180. fileName = "E_SPWATER1.bmp";
  181. break;//not positive
  182. case 2:
  183. fileName = "E_SPCOLD.bmp";
  184. break;//direct damage
  185. }
  186. break;
  187. case Bonus::AIR_IMMUNITY:
  188. switch(bonus->subtype)
  189. {
  190. case 0:
  191. fileName = "E_SPAIR.bmp";
  192. break;//all
  193. case 1:
  194. fileName = "E_SPAIR1.bmp";
  195. break;//not positive
  196. case 2:
  197. fileName = "E_LIGHT.bmp";
  198. break;//direct damage
  199. }
  200. break;
  201. case Bonus::EARTH_IMMUNITY:
  202. switch(bonus->subtype)
  203. {
  204. case 0:
  205. fileName = "E_SPEATH.bmp";
  206. break;//all
  207. case 1:
  208. case 2://no specific icon for direct damage immunity
  209. fileName = "E_SPEATH1.bmp";
  210. break;//not positive
  211. }
  212. break;
  213. case Bonus::LEVEL_SPELL_IMMUNITY:
  214. {
  215. if(vstd::iswithin(bonus->val, 1, 5))
  216. {
  217. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  218. }
  219. break;
  220. }
  221. case Bonus::GENERAL_DAMAGE_REDUCTION:
  222. {
  223. switch(bonus->subtype)
  224. {
  225. case 0:
  226. fileName = "DamageReductionMelee.bmp";
  227. break;
  228. case 1:
  229. fileName = "DamageReductionRanged.bmp";
  230. break;
  231. }
  232. break;
  233. }
  234. default:
  235. {
  236. const CBonusType & bt = bonusTypes[bonus->type];
  237. fileName = bt.icon;
  238. fullPath = true;
  239. }
  240. break;
  241. }
  242. if(!fileName.empty() && !fullPath)
  243. fileName = "zvs/Lib1.res/" + fileName;
  244. return fileName;
  245. }
  246. void CBonusTypeHandler::load()
  247. {
  248. const JsonNode gameConf(ResourceID("config/gameConfig.json"));
  249. const JsonNode config(JsonUtils::assembleFromFiles(gameConf["bonuses"].convertTo<std::vector<std::string>>()));
  250. load(config);
  251. }
  252. void CBonusTypeHandler::load(const JsonNode & config)
  253. {
  254. for(auto & node : config.Struct())
  255. {
  256. auto it = bonusNameMap.find(node.first);
  257. if(it == bonusNameMap.end())
  258. {
  259. //TODO: new bonus
  260. // CBonusType bt;
  261. // loadItem(node.second, bt);
  262. //
  263. // auto new_id = bonusTypes.size();
  264. //
  265. // bonusTypes.push_back(bt);
  266. logBonus->warn("Adding new bonuses not implemented (%s)", node.first);
  267. }
  268. else
  269. {
  270. CBonusType & bt = bonusTypes[it->second];
  271. loadItem(node.second, bt);
  272. logBonus->trace("Loaded bonus type %s", node.first);
  273. }
  274. }
  275. }
  276. void CBonusTypeHandler::loadItem(const JsonNode & source, CBonusType & dest)
  277. {
  278. dest.nameTemplate = source["name"].String();
  279. dest.descriptionTemplate = source["description"].String();
  280. dest.hidden = source["hidden"].Bool(); //Null -> false
  281. const JsonNode & graphics = source["graphics"];
  282. if(!graphics.isNull())
  283. {
  284. dest.icon = graphics["icon"].String();
  285. }
  286. dest.buildMacros();
  287. }
  288. VCMI_LIB_NAMESPACE_END