CSpellHandler.cpp 13 KB

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