CSpellHandler.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 <boost/assign/std/set.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. 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::string("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::set<ui16> CSpell::rangeInHexes(unsigned int centralHex, ui8 schoolLvl ) const
  113. {
  114. std::set<ui16> ret;
  115. std::string rng = range[schoolLvl] + ','; //copy + artificial comma for easier handling
  116. if(rng.size() >= 1 && rng[0] != 'X') //there is at lest one hex in range
  117. {
  118. std::string number1, number2;
  119. int beg, end;
  120. bool readingFirst = true;
  121. for(int it=0; it<rng.size(); ++it)
  122. {
  123. if( std::isdigit(rng[it]) ) //reading numer
  124. {
  125. if(readingFirst)
  126. number1 += rng[it];
  127. else
  128. number2 += rng[it];
  129. }
  130. else if(rng[it] == ',') //comma
  131. {
  132. //calculating variables
  133. if(readingFirst)
  134. {
  135. beg = atoi(number1.c_str());
  136. number1 = "";
  137. }
  138. else
  139. {
  140. end = atoi(number2.c_str());
  141. number2 = "";
  142. }
  143. //obtaining new hexes
  144. std::set<ui16> curLayer;
  145. if(readingFirst)
  146. {
  147. curLayer = getInRange(centralHex, beg, beg);
  148. }
  149. else
  150. {
  151. curLayer = getInRange(centralHex, beg, end);
  152. readingFirst = true;
  153. }
  154. //adding abtained hexes
  155. for(std::set<ui16>::iterator it = curLayer.begin(); it != curLayer.end(); ++it)
  156. {
  157. ret.insert(*it);
  158. }
  159. }
  160. else if(rng[it] == '-') //dash
  161. {
  162. beg = atoi(number1.c_str());
  163. number1 = "";
  164. readingFirst = false;
  165. }
  166. }
  167. }
  168. return ret;
  169. }
  170. CSpell::ETargetType CSpell::getTargetType() const
  171. {
  172. if(attributes.find("CREATURE_TARGET_1") != std::string::npos
  173. || attributes.find("CREATURE_TARGET_2") != std::string::npos)
  174. return CREATURE_EXPERT_MASSIVE;
  175. if(attributes.find("CREATURE_TARGET") != std::string::npos)
  176. return CREATURE;
  177. if(attributes.find("OBSTACLE_TARGET") != std::string::npos)
  178. return OBSTACLE;
  179. return NO_TARGET;
  180. }
  181. static bool startsWithX(const std::string &s)
  182. {
  183. return s.size() && s[0] == 'x';
  184. }
  185. bool DLL_EXPORT isInScreenRange(const int3 &center, const int3 &pos)
  186. {
  187. int3 diff = pos - center;
  188. if(diff.x >= -9 && diff.x <= 9 && diff.y >= -8 && diff.y <= 8)
  189. return true;
  190. else
  191. return false;
  192. }
  193. void CSpellHandler::loadSpells()
  194. {
  195. std::string buf = bitmaph->getTextFile("SPTRAITS.TXT"), pom;
  196. int andame = buf.size(), i=0; //buf iterator
  197. for(int z=0; z<5; ++z)
  198. loadToIt(pom,buf,i,3);
  199. bool combSpells=false; //true, if we are reading combat spells
  200. bool creatureAbility=false; //if true, only creature can use this spell
  201. int ifHit = 0;
  202. while(i<andame)
  203. {
  204. if(spells.size()==81)
  205. break;
  206. CSpell * nsp = new CSpell; //new currently being read spell
  207. loadToIt(nsp->name,buf,i,4);
  208. if(nsp->name == std::string(""))
  209. {
  210. if(ifHit == 0)
  211. {
  212. combSpells = true;
  213. }
  214. if(ifHit == 1)
  215. {
  216. creatureAbility = true;
  217. }
  218. for(int z=0; z<3; ++z)
  219. loadToIt(pom,buf,i,3);
  220. loadToIt(nsp->name,buf,i,4);
  221. ++ifHit;
  222. }
  223. loadToIt(nsp->abbName,buf,i,4);
  224. loadToIt(nsp->level,buf,i,4);
  225. loadToIt(pom,buf,i,4);
  226. nsp->earth = startsWithX(pom);
  227. loadToIt(pom,buf,i,4);
  228. nsp->water = startsWithX(pom);
  229. loadToIt(pom,buf,i,4);
  230. nsp->fire = startsWithX(pom);
  231. loadToIt(pom,buf,i,4);
  232. nsp->air = startsWithX(pom);
  233. nsp->costs.resize(4);
  234. for (int z = 0; z < 4 ; z++)
  235. loadToIt(nsp->costs[z],buf,i,4);
  236. loadToIt(nsp->power,buf,i,4);
  237. nsp->powers.resize(4);
  238. for (int z = 0; z < 4 ; z++)
  239. loadToIt(nsp->powers[z],buf,i,4);
  240. nsp->probabilities.resize(9);
  241. for (int z = 0; z < 9 ; z++)
  242. loadToIt(nsp->probabilities[z],buf,i,4);
  243. nsp->AIVals.resize(4);
  244. for (int z = 0; z < 4 ; z++)
  245. loadToIt(nsp->AIVals[z],buf,i,4);
  246. nsp->descriptions.resize(4);
  247. for (int z = 0; z < 4 ; z++)
  248. {
  249. loadToIt(nsp->descriptions[z],buf,i,4);
  250. boost::algorithm::replace_all(nsp->descriptions[z],"\"","");
  251. }
  252. loadToIt(nsp->attributes,buf,i,3);
  253. nsp->id = spells.size();
  254. nsp->combatSpell = combSpells;
  255. nsp->creatureAbility = creatureAbility;
  256. nsp->mainEffectAnim = -1;
  257. spells.push_back(nsp);
  258. }
  259. boost::replace_first (spells[47]->attributes, "2", ""); // disrupting ray will now affect single creature
  260. //loading of additional spell traits
  261. std::ifstream ast;
  262. ast.open(DATA_DIR "/config/spell_info.txt", std::ios::binary);
  263. if(!ast.is_open())
  264. {
  265. tlog1<<"lack of config/spell_info.txt file!"<<std::endl;
  266. }
  267. else
  268. {
  269. //reading header
  270. std::string dump;
  271. for(int i=0; i<60; ++i) ast>>dump;
  272. //reading exact info
  273. int spellID;
  274. ast>>spellID;
  275. while(spellID != -1)
  276. {
  277. int buf;
  278. ast >> buf;
  279. spells[spellID]->positiveness = buf;
  280. ast >> buf;
  281. spells[spellID]->mainEffectAnim = buf;
  282. spells[spellID]->range.resize(4);
  283. for(int g=0; g<4; ++g)
  284. ast>>spells[spellID]->range[g];
  285. ast>>spellID;
  286. }
  287. }
  288. ast.close();
  289. spells.push_back(spells[80]); //clone Acid Breath attributes for Acid Breath damage effect
  290. damageSpells += 11, 13, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 57, 77;
  291. }