CBonusTypeHandler.cpp 6.8 KB

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