CSpellHandler.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CSpellHandler.h"
  4. #include "CLodHandler.h"
  5. #include "CSoundBase.h"
  6. #include "../lib/VCMI_Lib.h"
  7. #include <boost/algorithm/string/replace.hpp>
  8. #include <cctype>
  9. extern CLodHandler *bitmaph;
  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. 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::string("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. std::set<ui16> CSpell::rangeInHexes(unsigned int centralHex, ui8 schoolLvl ) const
  108. {
  109. std::set<ui16> ret;
  110. std::string rng = range[schoolLvl] + ','; //copy + artificial comma for easier handling
  111. if(rng.size() >= 1 && rng[0] != 'X') //there is at lest one hex in range
  112. {
  113. std::string number1, number2;
  114. int beg, end;
  115. bool readingFirst = true;
  116. for(int it=0; it<rng.size(); ++it)
  117. {
  118. if( std::isdigit(rng[it]) ) //reading numer
  119. {
  120. if(readingFirst)
  121. number1 += rng[it];
  122. else
  123. number2 += rng[it];
  124. }
  125. else if(rng[it] == ',') //comma
  126. {
  127. //calculating variables
  128. if(readingFirst)
  129. {
  130. beg = atoi(number1.c_str());
  131. number1 = "";
  132. }
  133. else
  134. {
  135. end = atoi(number2.c_str());
  136. number2 = "";
  137. }
  138. //obtaining new hexes
  139. std::set<ui16> curLayer;
  140. if(readingFirst)
  141. {
  142. curLayer = getInRange(centralHex, beg, beg);
  143. }
  144. else
  145. {
  146. curLayer = getInRange(centralHex, beg, end);
  147. readingFirst = true;
  148. }
  149. //adding abtained hexes
  150. for(std::set<ui16>::iterator it = curLayer.begin(); it != curLayer.end(); ++it)
  151. {
  152. ret.insert(*it);
  153. }
  154. }
  155. else if(rng[it] == '-') //dash
  156. {
  157. beg = atoi(number1.c_str());
  158. number1 = "";
  159. readingFirst = false;
  160. }
  161. }
  162. }
  163. return ret;
  164. }
  165. void CSpellHandler::loadSpells()
  166. {
  167. std::string buf = bitmaph->getTextFile("SPTRAITS.TXT"), pom;
  168. int andame = buf.size(), i=0; //buf iterator
  169. for(int z=0; z<5; ++z)
  170. loadToIt(pom,buf,i,3);
  171. bool combSpells=false; //true, if we are reading combat spells
  172. bool creatureAbility=false; //if true, only creature can use this spell
  173. int ifHit = 0;
  174. while(i<andame)
  175. {
  176. if(spells.size()==81)
  177. break;
  178. CSpell nsp; //new currently being read spell
  179. loadToIt(nsp.name,buf,i,4);
  180. if(nsp.name == std::string(""))
  181. {
  182. if(ifHit == 0)
  183. {
  184. combSpells = true;
  185. }
  186. if(ifHit == 1)
  187. {
  188. creatureAbility = true;
  189. }
  190. for(int z=0; z<3; ++z)
  191. loadToIt(pom,buf,i,3);
  192. loadToIt(nsp.name,buf,i,4);
  193. ++ifHit;
  194. }
  195. loadToIt(nsp.abbName,buf,i,4);
  196. loadToIt(nsp.level,buf,i,4);
  197. loadToIt(pom,buf,i,4);
  198. nsp.earth = pom[0]=='x' ? true : false;
  199. loadToIt(pom,buf,i,4);
  200. nsp.water = pom[0]=='x' ? true : false;
  201. loadToIt(pom,buf,i,4);
  202. nsp.fire = pom[0]=='x' ? true : false;
  203. loadToIt(pom,buf,i,4);
  204. nsp.air = pom[0]=='x' ? true : false;
  205. nsp.costs.resize(4);
  206. for (int z = 0; z < 4 ; z++)
  207. loadToIt(nsp.costs[z],buf,i,4);
  208. loadToIt(nsp.power,buf,i,4);
  209. nsp.powers.resize(4);
  210. for (int z = 0; z < 4 ; z++)
  211. loadToIt(nsp.powers[z],buf,i,4);
  212. nsp.probabilities.resize(9);
  213. for (int z = 0; z < 9 ; z++)
  214. loadToIt(nsp.probabilities[z],buf,i,4);
  215. nsp.AIVals.resize(4);
  216. for (int z = 0; z < 4 ; z++)
  217. loadToIt(nsp.AIVals[z],buf,i,4);
  218. nsp.descriptions.resize(4);
  219. for (int z = 0; z < 4 ; z++)
  220. {
  221. loadToIt(nsp.descriptions[z],buf,i,4);
  222. boost::algorithm::replace_all(nsp.descriptions[z],"\"","");
  223. }
  224. loadToIt(nsp.attributes,buf,i,3);
  225. nsp.id = spells.size();
  226. nsp.combatSpell = combSpells;
  227. nsp.creatureAbility = creatureAbility;
  228. nsp.mainEffectAnim = -1;
  229. nsp.soundID = soundBase::invalid;
  230. spells.push_back(nsp);
  231. }
  232. //loading of additional spell traits
  233. std::ifstream ast;
  234. ast.open("config/spell_info.txt", std::ios::binary);
  235. if(!ast.is_open())
  236. {
  237. tlog1<<"lack of config/spell_info.txt file!"<<std::endl;
  238. }
  239. else
  240. {
  241. //reading header
  242. std::string dump;
  243. for(int i=0; i<60; ++i) ast>>dump;
  244. //reading exact info
  245. int spellID;
  246. ast>>spellID;
  247. while(spellID != -1)
  248. {
  249. int buf;
  250. ast >> buf;
  251. spells[spellID].positiveness = buf;
  252. ast >> buf;
  253. spells[spellID].mainEffectAnim = buf;
  254. spells[spellID].range.resize(4);
  255. for(int g=0; g<4; ++g)
  256. ast>>spells[spellID].range[g];
  257. ast>>spellID;
  258. }
  259. }
  260. }