2
0

CSpellHandler.cpp 9.8 KB

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