2
0

CBonusTypeHandler.cpp 6.6 KB

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