2
0

CBonusTypeHandler.cpp 6.8 KB

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