CSpellHandler.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include "StdInc.h"
  2. #include "CSpellHandler.h"
  3. #include "CGeneralTextHandler.h"
  4. #include "Filesystem/CResourceLoader.h"
  5. #include "VCMI_Lib.h"
  6. #include "JsonNode.h"
  7. #include <cctype>
  8. #include "GameConstants.h"
  9. #include "BattleHex.h"
  10. /*
  11. * CSpellHandler.cpp, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. using namespace boost::assign;
  20. namespace SRSLPraserHelpers
  21. {
  22. static int XYToHex(int x, int y)
  23. {
  24. return x + 17 * y;
  25. }
  26. static int XYToHex(std::pair<int, int> xy)
  27. {
  28. return XYToHex(xy.first, xy.second);
  29. }
  30. static int hexToY(int battleFieldPosition)
  31. {
  32. return battleFieldPosition/17;
  33. }
  34. static int hexToX(int battleFieldPosition)
  35. {
  36. int pos = battleFieldPosition - hexToY(battleFieldPosition) * 17;
  37. return pos;
  38. }
  39. static std::pair<int, int> hexToPair(int battleFieldPosition)
  40. {
  41. return std::make_pair(hexToX(battleFieldPosition), hexToY(battleFieldPosition));
  42. }
  43. //moves hex by one hex in given direction
  44. //0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left
  45. static std::pair<int, int> gotoDir(int x, int y, int direction)
  46. {
  47. switch(direction)
  48. {
  49. case 0: //top left
  50. return std::make_pair(y%2 ? x-1 : x, y-1);
  51. case 1: //top right
  52. return std::make_pair(y%2 ? x : x+1, y-1);
  53. case 2: //right
  54. return std::make_pair(x+1, y);
  55. case 3: //right bottom
  56. return std::make_pair(y%2 ? x : x+1, y+1);
  57. case 4: //left bottom
  58. return std::make_pair(y%2 ? x-1 : x, y+1);
  59. case 5: //left
  60. return std::make_pair(x-1, y);
  61. default:
  62. throw std::runtime_error("Disaster: wrong direction in SRSLPraserHelpers::gotoDir!\n");
  63. }
  64. }
  65. static std::pair<int, int> gotoDir(std::pair<int, int> xy, int direction)
  66. {
  67. return gotoDir(xy.first, xy.second, direction);
  68. }
  69. static bool isGoodHex(std::pair<int, int> xy)
  70. {
  71. return xy.first >=0 && xy.first < 17 && xy.second >= 0 && xy.second < 11;
  72. }
  73. //helper fonction for std::set<ui16> CSpell::rangeInHexes(unsigned int centralHex, ui8 schoolLvl ) const
  74. static std::set<ui16> getInRange(unsigned int center, int low, int high)
  75. {
  76. std::set<ui16> ret;
  77. if(low == 0)
  78. {
  79. ret.insert(center);
  80. }
  81. std::pair<int, int> mainPointForLayer[6]; //A, B, C, D, E, F points
  82. for(int b=0; b<6; ++b)
  83. mainPointForLayer[b] = hexToPair(center);
  84. for(int it=1; it<=high; ++it) //it - distance to the center
  85. {
  86. for(int b=0; b<6; ++b)
  87. mainPointForLayer[b] = gotoDir(mainPointForLayer[b], b);
  88. if(it>=low)
  89. {
  90. std::pair<int, int> curHex;
  91. //adding lines (A-b, B-c, C-d, etc)
  92. for(int v=0; v<6; ++v)
  93. {
  94. curHex = mainPointForLayer[v];
  95. for(int h=0; h<it; ++h)
  96. {
  97. if(isGoodHex(curHex))
  98. ret.insert(XYToHex(curHex));
  99. curHex = gotoDir(curHex, (v+2)%6);
  100. }
  101. }
  102. } //if(it>=low)
  103. }
  104. return ret;
  105. }
  106. }
  107. using namespace SRSLPraserHelpers;
  108. CSpellHandler::CSpellHandler()
  109. {
  110. VLC->spellh = this;
  111. }
  112. std::vector<BattleHex> CSpell::rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool *outDroppedHexes) const
  113. {
  114. std::vector<BattleHex> ret;
  115. if(id == Spells::FIRE_WALL || id == Spells::FORCE_FIELD)
  116. {
  117. //Special case - shape of obstacle depends on caster's side
  118. //TODO make it possible through spell_info config
  119. BattleHex::EDir firstStep, secondStep;
  120. if(side)
  121. {
  122. firstStep = BattleHex::TOP_LEFT;
  123. secondStep = BattleHex::TOP_RIGHT;
  124. }
  125. else
  126. {
  127. firstStep = BattleHex::TOP_RIGHT;
  128. secondStep = BattleHex::TOP_LEFT;
  129. }
  130. //Adds hex to the ret if it's valid. Otherwise sets output arg flag if given.
  131. auto addIfValid = [&](BattleHex hex)
  132. {
  133. if(hex.isValid())
  134. ret.push_back(hex);
  135. else if(outDroppedHexes)
  136. *outDroppedHexes = true;
  137. };
  138. ret.push_back(centralHex);
  139. addIfValid(centralHex.moveInDir(firstStep, false));
  140. if(schoolLvl >= 2) //advanced versions of fire wall / force field cotnains of 3 hexes
  141. addIfValid(centralHex.moveInDir(firstStep, false).moveInDir(secondStep, false));
  142. return ret;
  143. }
  144. std::string rng = range[schoolLvl] + ','; //copy + artificial comma for easier handling
  145. if(rng.size() >= 1 && rng[0] != 'X') //there is at lest one hex in range
  146. {
  147. std::string number1, number2;
  148. int beg, end;
  149. bool readingFirst = true;
  150. for(int it=0; it<rng.size(); ++it)
  151. {
  152. if( std::isdigit(rng[it]) ) //reading numer
  153. {
  154. if(readingFirst)
  155. number1 += rng[it];
  156. else
  157. number2 += rng[it];
  158. }
  159. else if(rng[it] == ',') //comma
  160. {
  161. //calculating variables
  162. if(readingFirst)
  163. {
  164. beg = atoi(number1.c_str());
  165. number1 = "";
  166. }
  167. else
  168. {
  169. end = atoi(number2.c_str());
  170. number2 = "";
  171. }
  172. //obtaining new hexes
  173. std::set<ui16> curLayer;
  174. if(readingFirst)
  175. {
  176. curLayer = getInRange(centralHex, beg, beg);
  177. }
  178. else
  179. {
  180. curLayer = getInRange(centralHex, beg, end);
  181. readingFirst = true;
  182. }
  183. //adding abtained hexes
  184. for(std::set<ui16>::iterator it = curLayer.begin(); it != curLayer.end(); ++it)
  185. {
  186. ret.push_back(*it);
  187. }
  188. }
  189. else if(rng[it] == '-') //dash
  190. {
  191. beg = atoi(number1.c_str());
  192. number1 = "";
  193. readingFirst = false;
  194. }
  195. }
  196. }
  197. //remove duplicates (TODO check if actually needed)
  198. range::unique(ret);
  199. return ret;
  200. }
  201. CSpell::ETargetType CSpell::getTargetType() const //TODO: parse these at game launch
  202. {
  203. if(attributes.find("CREATURE_TARGET_1") != std::string::npos
  204. || attributes.find("CREATURE_TARGET_2") != std::string::npos)
  205. return CREATURE_EXPERT_MASSIVE;
  206. if(attributes.find("CREATURE_TARGET") != std::string::npos)
  207. return CREATURE;
  208. if(attributes.find("OBSTACLE_TARGET") != std::string::npos)
  209. return OBSTACLE;
  210. return NO_TARGET;
  211. }
  212. bool CSpell::isPositive() const
  213. {
  214. return positiveness == POSITIVE;
  215. }
  216. bool CSpell::isNegative() const
  217. {
  218. return positiveness == NEGATIVE;
  219. }
  220. bool CSpell::isRisingSpell() const
  221. {
  222. return vstd::contains(VLC->spellh->risingSpells, id);
  223. }
  224. bool CSpell::isDamageSpell() const
  225. {
  226. return vstd::contains(VLC->spellh->damageSpells, id);
  227. }
  228. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos)
  229. {
  230. int3 diff = pos - center;
  231. if(diff.x >= -9 && diff.x <= 9 && diff.y >= -8 && diff.y <= 8)
  232. return true;
  233. else
  234. return false;
  235. }
  236. CSpell * CSpellHandler::loadSpell(CLegacyConfigParser & parser)
  237. {
  238. CSpell * spell = new CSpell; //new currently being read spell
  239. spell->name = parser.readString();
  240. spell->abbName = parser.readString();
  241. spell->level = parser.readNumber();
  242. spell->earth = parser.readString() == "x";
  243. spell->water = parser.readString() == "x";
  244. spell->fire = parser.readString() == "x";
  245. spell->air = parser.readString() == "x";
  246. for (int i = 0; i < 4 ; i++)
  247. spell->costs.push_back(parser.readNumber());
  248. spell->power = parser.readNumber();
  249. for (int i = 0; i < 4 ; i++)
  250. spell->powers.push_back(parser.readNumber());
  251. for (int i = 0; i < 9 ; i++)
  252. spell->probabilities.push_back(parser.readNumber());
  253. for (int i = 0; i < 4 ; i++)
  254. spell->AIVals.push_back(parser.readNumber());
  255. for (int i = 0; i < 4 ; i++)
  256. spell->descriptions.push_back(parser.readString());
  257. spell->attributes = parser.readString();
  258. spell->mainEffectAnim = -1;
  259. return spell;
  260. }
  261. void CSpellHandler::loadSpells()
  262. {
  263. CLegacyConfigParser parser("DATA/SPTRAITS.TXT");
  264. for(int i=0; i<5; i++) // header
  265. parser.endLine();
  266. do //read adventure map spells
  267. {
  268. CSpell * spell = loadSpell(parser);
  269. spell->id = spells.size();
  270. spell->combatSpell = false;
  271. spell->creatureAbility = false;
  272. spells.push_back(spell);
  273. }
  274. while (parser.endLine() && !parser.isNextEntryEmpty());
  275. for(int i=0; i<3; i++)
  276. parser.endLine();
  277. do //read battle spells
  278. {
  279. CSpell * spell = loadSpell(parser);
  280. spell->id = spells.size();
  281. spell->combatSpell = true;
  282. spell->creatureAbility = false;
  283. spells.push_back(spell);
  284. }
  285. while (parser.endLine() && !parser.isNextEntryEmpty());
  286. for(int i=0; i<3; i++)
  287. parser.endLine();
  288. do //read creature abilities
  289. {
  290. CSpell * spell = loadSpell(parser);
  291. spell->id = spells.size();
  292. spell->combatSpell = true;
  293. spell->creatureAbility = true;
  294. spells.push_back(spell);
  295. }
  296. while (parser.endLine() && !parser.isNextEntryEmpty());
  297. boost::replace_first (spells[47]->attributes, "2", ""); // disrupting ray will now affect single creature
  298. //loading of additional spell traits
  299. const JsonNode config(ResourceID("config/spell_info.json"));
  300. BOOST_FOREACH(const JsonNode &spell, config["spells"].Vector())
  301. {
  302. //reading exact info
  303. int spellID = spell["id"].Float();
  304. spells[spellID]->positiveness = spell["effect"].Float();
  305. spells[spellID]->mainEffectAnim = spell["anim"].Float();
  306. spells[spellID]->range.resize(4);
  307. int idx = 0;
  308. BOOST_FOREACH(const JsonNode &range, spell["ranges"].Vector())
  309. spells[spellID]->range[idx++] = range.String();
  310. }
  311. spells.push_back(spells[80]); //clone Acid Breath attributes for Acid Breath damage effect
  312. //forgetfulness needs to get targets automatically on expert level
  313. boost::replace_first(spells[61]->attributes, "CREATURE_TARGET", "CREATURE_TARGET_2"); //TODO: use flags instead?
  314. damageSpells += 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 57, 77;
  315. risingSpells += 38, 39, 40;
  316. mindSpells += 50, 59, 60, 61, 62;
  317. }