CSpellHandler.cpp 9.4 KB

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