CSpellHandler.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CSpellHandler.h"
  4. #include "CLodHandler.h"
  5. #include "../lib/VCMI_Lib.h"
  6. #include <boost/algorithm/string/replace.hpp>
  7. #include <cctype>
  8. extern CLodHandler *bitmaph;
  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. namespace SRSLPraserHelpers
  19. {
  20. static int XYToHex(int x, int y)
  21. {
  22. return x + 17 * y;
  23. }
  24. static int XYToHex(std::pair<int, int> xy)
  25. {
  26. return XYToHex(xy.first, xy.second);
  27. }
  28. static int hexToY(int battleFieldPosition)
  29. {
  30. return battleFieldPosition/17;
  31. }
  32. static int hexToX(int battleFieldPosition)
  33. {
  34. int pos = battleFieldPosition - hexToY(battleFieldPosition) * 17;
  35. return pos;
  36. }
  37. static std::pair<int, int> hexToPair(int battleFieldPosition)
  38. {
  39. return std::make_pair(hexToX(battleFieldPosition), hexToY(battleFieldPosition));
  40. }
  41. //moves hex by one hex in given direction
  42. //0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left
  43. static std::pair<int, int> gotoDir(int x, int y, int direction)
  44. {
  45. switch(direction)
  46. {
  47. case 0: //top left
  48. return std::make_pair(y%2 ? x-1 : x, y-1);
  49. case 1: //top right
  50. return std::make_pair(y%2 ? x : x+1, y-1);
  51. case 2: //right
  52. return std::make_pair(x+1, y);
  53. case 3: //right bottom
  54. return std::make_pair(y%2 ? x : x+1, y+1);
  55. case 4: //left bottom
  56. return std::make_pair(y%2 ? x-1 : x, y+1);
  57. case 5: //left
  58. return std::make_pair(x-1, y);
  59. default:
  60. throw std::string("Disaster: wrong direction in SRSLPraserHelpers::gotoDir!\n");
  61. }
  62. }
  63. static std::pair<int, int> gotoDir(std::pair<int, int> xy, int direction)
  64. {
  65. return gotoDir(xy.first, xy.second, direction);
  66. }
  67. static bool isGoodHex(std::pair<int, int> xy)
  68. {
  69. return xy.first >=0 && xy.first < 17 && xy.second >= 0 && xy.second < 11;
  70. }
  71. //helper fonction for std::set<ui16> CSpell::rangeInHexes(unsigned int centralHex, ui8 schoolLvl ) const
  72. static std::set<ui16> getInRange(unsigned int center, int low, int high)
  73. {
  74. std::set<ui16> ret;
  75. if(low == 0)
  76. {
  77. ret.insert(center);
  78. }
  79. std::pair<int, int> mainPointForLayer[6]; //A, B, C, D, E, F points
  80. for(int b=0; b<6; ++b)
  81. mainPointForLayer[b] = hexToPair(center);
  82. for(int it=1; it<=high; ++it) //it - distance to the center
  83. {
  84. for(int b=0; b<6; ++b)
  85. mainPointForLayer[b] = gotoDir(mainPointForLayer[b], b);
  86. if(it>=low)
  87. {
  88. std::pair<int, int> curHex;
  89. //adding lines (A-b, B-c, C-d, etc)
  90. for(int v=0; v<6; ++v)
  91. {
  92. curHex = mainPointForLayer[v];
  93. for(int h=0; h<it; ++h)
  94. {
  95. if(isGoodHex(curHex))
  96. ret.insert(XYToHex(curHex));
  97. curHex = gotoDir(curHex, (v+2)%6);
  98. }
  99. }
  100. } //if(it>=low)
  101. }
  102. return ret;
  103. }
  104. }
  105. using namespace SRSLPraserHelpers;
  106. CSpellHandler::CSpellHandler()
  107. {
  108. VLC->spellh = this;
  109. }
  110. std::set<ui16> CSpell::rangeInHexes(unsigned int centralHex, ui8 schoolLvl ) const
  111. {
  112. std::set<ui16> ret;
  113. std::string rng = range[schoolLvl] + ','; //copy + artificial comma for easier handling
  114. if(rng.size() >= 1 && rng[0] != 'X') //there is at lest one hex in range
  115. {
  116. std::string number1, number2;
  117. int beg, end;
  118. bool readingFirst = true;
  119. for(int it=0; it<rng.size(); ++it)
  120. {
  121. if( std::isdigit(rng[it]) ) //reading numer
  122. {
  123. if(readingFirst)
  124. number1 += rng[it];
  125. else
  126. number2 += rng[it];
  127. }
  128. else if(rng[it] == ',') //comma
  129. {
  130. //calculating variables
  131. if(readingFirst)
  132. {
  133. beg = atoi(number1.c_str());
  134. number1 = "";
  135. }
  136. else
  137. {
  138. end = atoi(number2.c_str());
  139. number2 = "";
  140. }
  141. //obtaining new hexes
  142. std::set<ui16> curLayer;
  143. if(readingFirst)
  144. {
  145. curLayer = getInRange(centralHex, beg, beg);
  146. }
  147. else
  148. {
  149. curLayer = getInRange(centralHex, beg, end);
  150. readingFirst = true;
  151. }
  152. //adding abtained hexes
  153. for(std::set<ui16>::iterator it = curLayer.begin(); it != curLayer.end(); ++it)
  154. {
  155. ret.insert(*it);
  156. }
  157. }
  158. else if(rng[it] == '-') //dash
  159. {
  160. beg = atoi(number1.c_str());
  161. number1 = "";
  162. readingFirst = false;
  163. }
  164. }
  165. }
  166. return ret;
  167. }
  168. static bool startsWithX(const std::string &s)
  169. {
  170. return s.size() && s[0] == 'x';
  171. }
  172. bool DLL_EXPORT isInScreenRange(const int3 &center, const int3 &pos)
  173. {
  174. int3 diff = pos - center;
  175. if(diff.x >= -9 && diff.x <= 9 && diff.y >= -8 && diff.y <= 8)
  176. return true;
  177. else
  178. return false;
  179. }
  180. void CSpellHandler::loadSpells()
  181. {
  182. std::string buf = bitmaph->getTextFile("SPTRAITS.TXT"), pom;
  183. int andame = buf.size(), i=0; //buf iterator
  184. for(int z=0; z<5; ++z)
  185. loadToIt(pom,buf,i,3);
  186. bool combSpells=false; //true, if we are reading combat spells
  187. bool creatureAbility=false; //if true, only creature can use this spell
  188. int ifHit = 0;
  189. while(i<andame)
  190. {
  191. if(spells.size()==81)
  192. break;
  193. CSpell * nsp = new CSpell; //new currently being read spell
  194. loadToIt(nsp->name,buf,i,4);
  195. if(nsp->name == std::string(""))
  196. {
  197. if(ifHit == 0)
  198. {
  199. combSpells = true;
  200. }
  201. if(ifHit == 1)
  202. {
  203. creatureAbility = true;
  204. }
  205. for(int z=0; z<3; ++z)
  206. loadToIt(pom,buf,i,3);
  207. loadToIt(nsp->name,buf,i,4);
  208. ++ifHit;
  209. }
  210. loadToIt(nsp->abbName,buf,i,4);
  211. loadToIt(nsp->level,buf,i,4);
  212. loadToIt(pom,buf,i,4);
  213. nsp->earth = startsWithX(pom);
  214. loadToIt(pom,buf,i,4);
  215. nsp->water = startsWithX(pom);
  216. loadToIt(pom,buf,i,4);
  217. nsp->fire = startsWithX(pom);
  218. loadToIt(pom,buf,i,4);
  219. nsp->air = startsWithX(pom);
  220. nsp->costs.resize(4);
  221. for (int z = 0; z < 4 ; z++)
  222. loadToIt(nsp->costs[z],buf,i,4);
  223. loadToIt(nsp->power,buf,i,4);
  224. nsp->powers.resize(4);
  225. for (int z = 0; z < 4 ; z++)
  226. loadToIt(nsp->powers[z],buf,i,4);
  227. nsp->probabilities.resize(9);
  228. for (int z = 0; z < 9 ; z++)
  229. loadToIt(nsp->probabilities[z],buf,i,4);
  230. nsp->AIVals.resize(4);
  231. for (int z = 0; z < 4 ; z++)
  232. loadToIt(nsp->AIVals[z],buf,i,4);
  233. nsp->descriptions.resize(4);
  234. for (int z = 0; z < 4 ; z++)
  235. {
  236. loadToIt(nsp->descriptions[z],buf,i,4);
  237. boost::algorithm::replace_all(nsp->descriptions[z],"\"","");
  238. }
  239. loadToIt(nsp->attributes,buf,i,3);
  240. nsp->id = spells.size();
  241. nsp->combatSpell = combSpells;
  242. nsp->creatureAbility = creatureAbility;
  243. nsp->mainEffectAnim = -1;
  244. spells.push_back(nsp);
  245. }
  246. //loading of additional spell traits
  247. std::ifstream ast;
  248. ast.open(DATA_DIR "/config/spell_info.txt", std::ios::binary);
  249. if(!ast.is_open())
  250. {
  251. tlog1<<"lack of config/spell_info.txt file!"<<std::endl;
  252. }
  253. else
  254. {
  255. //reading header
  256. std::string dump;
  257. for(int i=0; i<60; ++i) ast>>dump;
  258. //reading exact info
  259. int spellID;
  260. ast>>spellID;
  261. while(spellID != -1)
  262. {
  263. int buf;
  264. ast >> buf;
  265. spells[spellID]->positiveness = buf;
  266. ast >> buf;
  267. spells[spellID]->mainEffectAnim = buf;
  268. spells[spellID]->range.resize(4);
  269. for(int g=0; g<4; ++g)
  270. ast>>spells[spellID]->range[g];
  271. ast>>spellID;
  272. }
  273. }
  274. }