CSpellHandler.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. }
  112. CSpell::CSpell()
  113. {
  114. isDamage = false;
  115. isMind = false;
  116. isRising = false;
  117. isOffensive = false;
  118. }
  119. std::vector<BattleHex> CSpell::rangeInHexes(BattleHex centralHex, ui8 schoolLvl, ui8 side, bool *outDroppedHexes) const
  120. {
  121. std::vector<BattleHex> ret;
  122. if(id == Spells::FIRE_WALL || id == Spells::FORCE_FIELD)
  123. {
  124. //Special case - shape of obstacle depends on caster's side
  125. //TODO make it possible through spell_info config
  126. BattleHex::EDir firstStep, secondStep;
  127. if(side)
  128. {
  129. firstStep = BattleHex::TOP_LEFT;
  130. secondStep = BattleHex::TOP_RIGHT;
  131. }
  132. else
  133. {
  134. firstStep = BattleHex::TOP_RIGHT;
  135. secondStep = BattleHex::TOP_LEFT;
  136. }
  137. //Adds hex to the ret if it's valid. Otherwise sets output arg flag if given.
  138. auto addIfValid = [&](BattleHex hex)
  139. {
  140. if(hex.isValid())
  141. ret.push_back(hex);
  142. else if(outDroppedHexes)
  143. *outDroppedHexes = true;
  144. };
  145. ret.push_back(centralHex);
  146. addIfValid(centralHex.moveInDir(firstStep, false));
  147. if(schoolLvl >= 2) //advanced versions of fire wall / force field cotnains of 3 hexes
  148. addIfValid(centralHex.moveInDir(firstStep, false).moveInDir(secondStep, false));
  149. return ret;
  150. }
  151. std::string rng = range[schoolLvl] + ','; //copy + artificial comma for easier handling
  152. if(rng.size() >= 1 && rng[0] != 'X') //there is at lest one hex in range
  153. {
  154. std::string number1, number2;
  155. int beg, end;
  156. bool readingFirst = true;
  157. for(int it=0; it<rng.size(); ++it)
  158. {
  159. if( std::isdigit(rng[it]) ) //reading numer
  160. {
  161. if(readingFirst)
  162. number1 += rng[it];
  163. else
  164. number2 += rng[it];
  165. }
  166. else if(rng[it] == ',') //comma
  167. {
  168. //calculating variables
  169. if(readingFirst)
  170. {
  171. beg = atoi(number1.c_str());
  172. number1 = "";
  173. }
  174. else
  175. {
  176. end = atoi(number2.c_str());
  177. number2 = "";
  178. }
  179. //obtaining new hexes
  180. std::set<ui16> curLayer;
  181. if(readingFirst)
  182. {
  183. curLayer = getInRange(centralHex, beg, beg);
  184. }
  185. else
  186. {
  187. curLayer = getInRange(centralHex, beg, end);
  188. readingFirst = true;
  189. }
  190. //adding abtained hexes
  191. for(std::set<ui16>::iterator it = curLayer.begin(); it != curLayer.end(); ++it)
  192. {
  193. ret.push_back(*it);
  194. }
  195. }
  196. else if(rng[it] == '-') //dash
  197. {
  198. beg = atoi(number1.c_str());
  199. number1 = "";
  200. readingFirst = false;
  201. }
  202. }
  203. }
  204. //remove duplicates (TODO check if actually needed)
  205. range::unique(ret);
  206. return ret;
  207. }
  208. CSpell::ETargetType CSpell::getTargetType() const //TODO: parse these at game launch
  209. {
  210. if(attributes.find("CREATURE_TARGET_1") != std::string::npos
  211. || attributes.find("CREATURE_TARGET_2") != std::string::npos)
  212. return CREATURE_EXPERT_MASSIVE;
  213. if(attributes.find("CREATURE_TARGET") != std::string::npos)
  214. return CREATURE;
  215. if(attributes.find("OBSTACLE_TARGET") != std::string::npos)
  216. return OBSTACLE;
  217. return NO_TARGET;
  218. }
  219. bool CSpell::isPositive() const
  220. {
  221. return positiveness == POSITIVE;
  222. }
  223. bool CSpell::isNegative() const
  224. {
  225. return positiveness == NEGATIVE;
  226. }
  227. bool CSpell::isRisingSpell() const
  228. {
  229. return isRising;
  230. }
  231. bool CSpell::isDamageSpell() const
  232. {
  233. return isDamage;
  234. }
  235. bool CSpell::isMindSpell() const
  236. {
  237. return isMind;
  238. }
  239. bool CSpell::isOffensiveSpell() const
  240. {
  241. return isOffensive;
  242. }
  243. bool CSpell::hasEffects() const
  244. {
  245. return !effects[0].empty();
  246. }
  247. void CSpell::getEffects(std::vector<Bonus>& lst, const int level) const
  248. {
  249. if (level < 0 || level>3)
  250. {
  251. tlog1 << __FUNCTION__ << " invalid school level " << level;
  252. return;
  253. }
  254. lst.reserve(lst.size() + effects[level].size());
  255. BOOST_FOREACH (Bonus b, effects[level])
  256. {
  257. //TODO: value, add value
  258. lst.push_back(b);
  259. }
  260. }
  261. bool CSpell::isImmuneBy(const IBonusBearer* obj) const
  262. {
  263. BOOST_FOREACH(auto b, limiters)
  264. {
  265. if (!obj->hasBonusOfType(b))
  266. return true;
  267. }
  268. BOOST_FOREACH(auto b, immunities)
  269. {
  270. if (obj->hasBonusOfType(b))
  271. return true;
  272. }
  273. if (isMindSpell() && obj->hasBonusOfType(Bonus::MIND_IMMUNITY))
  274. return true;
  275. if (isDamageSpell() && obj->hasBonusOfType(Bonus::DIRECT_DAMAGE_IMMUNITY))
  276. return true;
  277. auto battleTestElementalImmunity = [&,this](Bonus::BonusType element) -> bool
  278. {
  279. if (!isPositive()) //negative or indifferent
  280. {
  281. if ((isDamageSpell() && obj->hasBonusOfType(element, 2)) || obj->hasBonusOfType(element, 1))
  282. return true;
  283. }
  284. else if (isPositive()) //positive
  285. {
  286. if (obj->hasBonusOfType(element, 0)) //must be immune to all spells
  287. return true;
  288. }
  289. return false;
  290. };
  291. if (fire)
  292. {
  293. if (battleTestElementalImmunity(Bonus::FIRE_IMMUNITY))
  294. return true;
  295. }
  296. if (water)
  297. {
  298. if (battleTestElementalImmunity(Bonus::WATER_IMMUNITY))
  299. return true;
  300. }
  301. if (earth)
  302. {
  303. if (battleTestElementalImmunity(Bonus::EARTH_IMMUNITY))
  304. return true;
  305. }
  306. if (air)
  307. {
  308. if (battleTestElementalImmunity(Bonus::AIR_IMMUNITY))
  309. return true;
  310. }
  311. TBonusListPtr immunities = obj->getBonuses(Selector::type(Bonus::LEVEL_SPELL_IMMUNITY));
  312. if(obj->hasBonusOfType(Bonus::NEGATE_ALL_NATURAL_IMMUNITIES))
  313. {
  314. immunities->remove_if([](const Bonus* b){ return b->source == Bonus::CREATURE_ABILITY; });
  315. }
  316. if(obj->hasBonusOfType(Bonus::SPELL_IMMUNITY, id)
  317. || ( immunities->size() > 0 && immunities->totalValue() >= level && level))
  318. {
  319. return true;
  320. }
  321. return false;
  322. }
  323. bool DLL_LINKAGE isInScreenRange(const int3 &center, const int3 &pos)
  324. {
  325. int3 diff = pos - center;
  326. if(diff.x >= -9 && diff.x <= 9 && diff.y >= -8 && diff.y <= 8)
  327. return true;
  328. else
  329. return false;
  330. }
  331. CSpell * CSpellHandler::loadSpell(CLegacyConfigParser & parser)
  332. {
  333. CSpell * spell = new CSpell; //new currently being read spell
  334. spell->name = parser.readString();
  335. spell->abbName = parser.readString();
  336. spell->level = parser.readNumber();
  337. spell->earth = parser.readString() == "x";
  338. spell->water = parser.readString() == "x";
  339. spell->fire = parser.readString() == "x";
  340. spell->air = parser.readString() == "x";
  341. spell->costs = parser.readNumArray<si32>(4);
  342. spell->power = parser.readNumber();
  343. spell->powers = parser.readNumArray<si32>(4);
  344. for (int i = 0; i < 9 ; i++)
  345. spell->probabilities[i] = parser.readNumber();
  346. spell->AIVals = parser.readNumArray<si32>(4);
  347. for (int i = 0; i < 4 ; i++)
  348. spell->descriptions.push_back(parser.readString());
  349. spell->attributes = parser.readString();
  350. spell->mainEffectAnim = -1;
  351. return spell;
  352. }
  353. void CSpellHandler::loadSpells()
  354. {
  355. CLegacyConfigParser parser("DATA/SPTRAITS.TXT");
  356. auto read = [&,this](bool combat, bool alility)
  357. {
  358. do
  359. {
  360. CSpell * spell = loadSpell(parser);
  361. spell->id = spells.size();
  362. spell->combatSpell = combat;
  363. spell->creatureAbility = alility;
  364. spells.push_back(spell);
  365. }
  366. while (parser.endLine() && !parser.isNextEntryEmpty());
  367. };
  368. auto skip = [&](int cnt)
  369. {
  370. for(int i=0; i<cnt; i++)
  371. parser.endLine();
  372. };
  373. skip(5);// header
  374. read(false,false); //read adventure map spells
  375. skip(3);
  376. read(true,false); //read battle spells
  377. skip(3);
  378. read(true,true);//read creature abilities
  379. boost::replace_first (spells[Spells::DISRUPTING_RAY]->attributes, "2", ""); // disrupting ray will now affect single creature
  380. spells.push_back(spells[Spells::ACID_BREATH_DEFENSE]); //clone Acid Breath attributes for Acid Breath damage effect
  381. //loading of additional spell traits
  382. const JsonNode config(ResourceID("config/spell_info.json"));
  383. BOOST_FOREACH(auto &spell, config["spells"].Struct())
  384. {
  385. //reading exact info
  386. int spellID = spell.second["id"].Float();
  387. CSpell *s = spells[spellID];
  388. s->positiveness = spell.second["effect"].Float();
  389. s->mainEffectAnim = spell.second["anim"].Float();
  390. s->range.resize(4);
  391. int idx = 0;
  392. BOOST_FOREACH(const JsonNode &range, spell.second["ranges"].Vector())
  393. s->range[idx++] = range.String();
  394. s->counteredSpells = spell.second["counters"].convertTo<std::vector<TSpell> >();
  395. s->identifier = spell.first;
  396. VLC->modh->identifiers.registerObject("spell." + spell.first, spellID);
  397. const JsonNode & flags_node = spell.second["flags"];
  398. if (!flags_node.isNull())
  399. {
  400. auto flags = flags_node.convertTo<std::vector<std::string> >();
  401. BOOST_FOREACH (const auto & flag, flags)
  402. {
  403. if (flag == "damage")
  404. {
  405. s->isDamage = true;
  406. }
  407. else if (flag == "rising")
  408. {
  409. s->isRising = true;
  410. }
  411. else if (flag == "mind")
  412. {
  413. s->isMind = true;
  414. }
  415. else if (flag == "offensive")
  416. {
  417. s->isOffensive = true;
  418. }
  419. }
  420. }
  421. const JsonNode & effects_node = spell.second["effects"];
  422. BOOST_FOREACH (const JsonNode & bonus_node, effects_node.Vector())
  423. {
  424. auto &v_node = bonus_node["values"];
  425. auto &a_node = bonus_node["ainfos"];
  426. auto v = v_node.convertTo<std::vector<int> >();
  427. auto a = a_node.convertTo<std::vector<int> >();
  428. for (int i=0; i<4 ; i++)
  429. {
  430. Bonus * b = JsonUtils::parseBonus(bonus_node);
  431. b->sid = s->id; //for all
  432. b->source = Bonus::SPELL_EFFECT;//for all
  433. b->val = s->powers[i];
  434. if (!v.empty())
  435. b->val = v[i];
  436. if (!a.empty())
  437. b->additionalInfo = a[i];
  438. s->effects[i].push_back(*b);
  439. }
  440. }
  441. auto find_in_map = [](std::string name, std::vector<Bonus::BonusType> &vec)
  442. {
  443. auto it = bonusNameMap.find(name);
  444. if (it == bonusNameMap.end())
  445. {
  446. tlog1 << "Error: invalid bonus name" << name << std::endl;
  447. }
  448. else
  449. {
  450. vec.push_back((Bonus::BonusType)it->second);
  451. }
  452. };
  453. auto read_node = [&](std::string name, std::vector<Bonus::BonusType> &vec)
  454. {
  455. const JsonNode & node = spell.second[name];
  456. if (!node.isNull())
  457. {
  458. auto names = node.convertTo<std::vector<std::string> >();
  459. BOOST_FOREACH(auto name, names)
  460. find_in_map(name, vec);
  461. }
  462. };
  463. read_node("immunity",s->immunities);
  464. read_node("limit",s->limiters);
  465. }
  466. //spell fixes
  467. //forgetfulness needs to get targets automatically on expert level
  468. boost::replace_first(spells[Spells::FORGETFULNESS]->attributes, "CREATURE_TARGET", "CREATURE_TARGET_2"); //TODO: use flags instead?
  469. }
  470. std::vector<ui8> CSpellHandler::getDefaultAllowedSpells() const
  471. {
  472. std::vector<ui8> allowedSpells;
  473. allowedSpells.resize(GameConstants::SPELLS_QUANTITY, 1);
  474. return allowedSpells;
  475. }