CBonusTypeHandler.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. logBonus->warnStream() << "Format error in: " << format;
  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. load();
  99. }
  100. CBonusTypeHandler::~CBonusTypeHandler()
  101. {
  102. //dtor
  103. }
  104. std::string CBonusTypeHandler::bonusToString(Bonus *bonus, const IBonusBearer *bearer, bool description) const
  105. {
  106. auto getValue = [=](const std::string &name) -> std::string
  107. {
  108. if (name == "val")
  109. {
  110. return boost::lexical_cast<std::string>(bearer->valOfBonuses(Selector::typeSubtype(bonus->type, bonus->subtype)));
  111. }
  112. else if (name == "subtype.creature")
  113. {
  114. return VLC->creh->creatures[bonus->subtype]->namePl;
  115. }
  116. else if (name == "subtype.spell")
  117. {
  118. return VLC->spellh->spells[bonus->subtype]->name;
  119. }
  120. else if (name == "MR")
  121. {
  122. return boost::lexical_cast<std::string>(bearer->magicResistance());
  123. }
  124. else
  125. {
  126. logBonus->warnStream() << "Unknown macro in bonus config: " << name;
  127. return "[error]";
  128. }
  129. };
  130. const CBonusType& bt = bonusTypes[bonus->type];
  131. std::string text;
  132. if (description)
  133. {
  134. text = bt.description.build(getValue);
  135. }
  136. else
  137. {
  138. text = bt.name.build(getValue);
  139. }
  140. return text;
  141. }
  142. std::string CBonusTypeHandler::bonusToGraphics(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"; break; //all
  166. case 1:
  167. fileName = "E_SPFIRE1.bmp"; break; //not positive
  168. case 2:
  169. fileName = "E_FIRE.bmp"; break; //direct damage
  170. }
  171. break;
  172. case Bonus::WATER_IMMUNITY:
  173. switch (bonus->subtype)
  174. {
  175. case 0:
  176. fileName = "E_SPWATER.bmp"; break; //all
  177. case 1:
  178. fileName = "E_SPWATER1.bmp"; break; //not positive
  179. case 2:
  180. fileName = "E_SPCOLD.bmp"; break; //direct damage
  181. }
  182. break;
  183. case Bonus::AIR_IMMUNITY:
  184. switch (bonus->subtype)
  185. {
  186. case 0:
  187. fileName = "E_SPAIR.bmp"; break; //all
  188. case 1:
  189. fileName = "E_SPAIR1.bmp"; break; //not positive
  190. case 2:
  191. fileName = "E_LIGHT.bmp"; break;//direct damage
  192. }
  193. break;
  194. case Bonus::EARTH_IMMUNITY:
  195. switch (bonus->subtype)
  196. {
  197. case 0:
  198. fileName = "E_SPEATH.bmp"; break; //all
  199. case 1:
  200. case 2: //no specific icon for direct damage immunity
  201. fileName = "E_SPEATH1.bmp"; break; //not positive
  202. }
  203. break;
  204. case Bonus::LEVEL_SPELL_IMMUNITY:
  205. {
  206. if (vstd::iswithin(bonus->val, 1 , 5))
  207. {
  208. fileName = "E_SPLVL" + boost::lexical_cast<std::string>(bonus->val) + ".bmp";
  209. }
  210. break;
  211. }
  212. default:
  213. {
  214. const CBonusType& bt = bonusTypes[bonus->type];
  215. fileName = bt.icon;
  216. fullPath = true;
  217. break;
  218. }
  219. }
  220. if(!fileName.empty() && !fullPath)
  221. fileName = "zvs/Lib1.res/" + fileName;
  222. return fileName;
  223. }
  224. void CBonusTypeHandler::load()
  225. {
  226. const JsonNode gameConf(ResourceID("config/gameConfig.json"));
  227. const JsonNode config(JsonUtils::assembleFromFiles(gameConf["bonuses"].convertTo<std::vector<std::string> >()));
  228. load(config);
  229. }
  230. void CBonusTypeHandler::load(const JsonNode& config)
  231. {
  232. BOOST_FOREACH(auto & node, config.Struct())
  233. {
  234. auto it = bonusNameMap.find(node.first);
  235. if(it == bonusNameMap.end())
  236. {
  237. //TODO: new bonus
  238. // CBonusType bt;
  239. // loadItem(node.second, bt);
  240. //
  241. // auto new_id = bonusTypes.size();
  242. //
  243. // bonusTypes.push_back(bt);
  244. logBonus->warnStream() << "Adding new bonuses not implemented (" << node.first << ")";
  245. }
  246. else
  247. {
  248. CBonusType& bt = bonusTypes[it->second];
  249. loadItem(node.second, bt);
  250. logBonus->traceStream() << "Loaded bonus type " << node.first;
  251. }
  252. }
  253. }
  254. void CBonusTypeHandler::loadItem(const JsonNode& source, CBonusType& dest)
  255. {
  256. dest.nameTemplate = source["name"].String();
  257. dest.descriptionTemplate = source["description"].String();
  258. dest.hidden = source["hidden"].Bool(); //Null -> false
  259. const JsonNode& graphics = source["graphics"];
  260. if(!graphics.isNull())
  261. {
  262. dest.icon = graphics["icon"].String();
  263. }
  264. dest.buildMacros();
  265. }