CBonusTypeHandler.cpp 7.1 KB

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