CSpellHandler.cpp 8.0 KB

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