NetPacksLib.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. #define VCMI_DLL
  2. #include "NetPacks.h"
  3. #include "../hch/CGeneralTextHandler.h"
  4. #include "../hch/CDefObjInfoHandler.h"
  5. #include "../hch/CArtHandler.h"
  6. #include "../hch/CHeroHandler.h"
  7. #include "../hch/CObjectHandler.h"
  8. #include "VCMI_Lib.h"
  9. #include "map.h"
  10. #include "../hch/CSpellHandler.h"
  11. #include <boost/bind.hpp>
  12. #include <boost/foreach.hpp>
  13. #include <boost/lexical_cast.hpp>
  14. #include <boost/algorithm/string/replace.hpp>
  15. #include <boost/thread.hpp>
  16. #include <boost/thread/shared_mutex.hpp>
  17. /*
  18. * NetPacksLib.cpp, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. #ifdef min
  27. #undef min
  28. #endif
  29. #ifdef max
  30. #undef max
  31. #endif
  32. DLL_EXPORT void SetResource::applyGs( CGameState *gs )
  33. {
  34. gs->getPlayer(player)->resources[resid] = val;
  35. }
  36. DLL_EXPORT void SetResources::applyGs( CGameState *gs )
  37. {
  38. for(int i=0;i<res.size();i++)
  39. gs->getPlayer(player)->resources[i] = res[i];
  40. }
  41. DLL_EXPORT void SetPrimSkill::applyGs( CGameState *gs )
  42. {
  43. CGHeroInstance *hero = gs->getHero(id);
  44. if(which <4)
  45. {
  46. if(abs)
  47. hero->primSkills[which] = val;
  48. else
  49. hero->primSkills[which] += val;
  50. }
  51. else if(which == 4) //XP
  52. {
  53. if(abs)
  54. hero->exp = val;
  55. else
  56. hero->exp += val;
  57. }
  58. }
  59. DLL_EXPORT void SetSecSkill::applyGs( CGameState *gs )
  60. {
  61. CGHeroInstance *hero = gs->getHero(id);
  62. if(hero->getSecSkillLevel(which) == 0)
  63. {
  64. hero->secSkills.push_back(std::pair<int,int>(which, val));
  65. }
  66. else
  67. {
  68. for(unsigned i=0;i<hero->secSkills.size();i++)
  69. {
  70. if(hero->secSkills[i].first == which)
  71. {
  72. if(abs)
  73. hero->secSkills[i].second = val;
  74. else
  75. hero->secSkills[i].second += val;
  76. }
  77. }
  78. }
  79. }
  80. DLL_EXPORT void HeroVisitCastle::applyGs( CGameState *gs )
  81. {
  82. CGHeroInstance *h = gs->getHero(hid);
  83. CGTownInstance *t = gs->getTown(tid);
  84. if(start())
  85. {
  86. if(garrison())
  87. {
  88. t->garrisonHero = h;
  89. h->visitedTown = t;
  90. h->inTownGarrison = true;
  91. }
  92. else
  93. {
  94. t->visitingHero = h;
  95. h->visitedTown = t;
  96. h->inTownGarrison = false;
  97. }
  98. }
  99. else
  100. {
  101. if(garrison())
  102. {
  103. t->garrisonHero = NULL;
  104. h->visitedTown = NULL;
  105. h->inTownGarrison = false;
  106. }
  107. else
  108. {
  109. t->visitingHero = NULL;
  110. h->visitedTown = NULL;
  111. h->inTownGarrison = false;
  112. }
  113. }
  114. }
  115. DLL_EXPORT void ChangeSpells::applyGs( CGameState *gs )
  116. {
  117. CGHeroInstance *hero = gs->getHero(hid);
  118. if(learn)
  119. BOOST_FOREACH(ui32 sid, spells)
  120. hero->spells.insert(sid);
  121. else
  122. BOOST_FOREACH(ui32 sid, spells)
  123. hero->spells.erase(sid);
  124. }
  125. DLL_EXPORT void SetMana::applyGs( CGameState *gs )
  126. {
  127. CGHeroInstance *hero = gs->getHero(hid);
  128. hero->mana = val;
  129. }
  130. DLL_EXPORT void SetMovePoints::applyGs( CGameState *gs )
  131. {
  132. CGHeroInstance *hero = gs->getHero(hid);
  133. hero->movement = val;
  134. }
  135. DLL_EXPORT void FoWChange::applyGs( CGameState *gs )
  136. {
  137. BOOST_FOREACH(int3 t, tiles)
  138. gs->getPlayer(player)->fogOfWarMap[t.x][t.y][t.z] = mode;
  139. }
  140. DLL_EXPORT void SetAvailableHeroes::applyGs( CGameState *gs )
  141. {
  142. gs->getPlayer(player)->availableHeroes.clear();
  143. CGHeroInstance *h = (hid1>=0 ? gs->hpool.heroesPool[hid1] : NULL);
  144. gs->getPlayer(player)->availableHeroes.push_back(h);
  145. if(h && flags & 1)
  146. {
  147. h->army.slots.clear();
  148. h->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[h->type->refTypeStack[0]],1);
  149. }
  150. h = (hid2>=0 ? gs->hpool.heroesPool[hid2] : NULL);
  151. gs->getPlayer(player)->availableHeroes.push_back(h);
  152. if(flags & 2)
  153. {
  154. h->army.slots.clear();
  155. h->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[h->type->refTypeStack[0]],1);
  156. }
  157. }
  158. DLL_EXPORT void GiveBonus::applyGs( CGameState *gs )
  159. {
  160. CGHeroInstance *h = gs->getHero(hid);
  161. h->bonuses.push_back(bonus);
  162. std::string &descr = h->bonuses.back().description;
  163. if(!bdescr.texts.size()
  164. && bonus.source == HeroBonus::OBJECT
  165. && (bonus.type == HeroBonus::LUCK || bonus.type == HeroBonus::MORALE || bonus.type == HeroBonus::MORALE_AND_LUCK)
  166. && gs->map->objects[bonus.id]->ID == 26) //it's morale/luck bonus from an event without description
  167. {
  168. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  169. boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val)));
  170. }
  171. else
  172. {
  173. descr = toString(bdescr);
  174. }
  175. }
  176. DLL_EXPORT void ChangeObjPos::applyGs( CGameState *gs )
  177. {
  178. CGObjectInstance *obj = gs->map->objects[objid];
  179. if(!obj)
  180. {
  181. tlog1 << "Wrong ChangeObjPos: object " << objid << " doesn't exist!\n";
  182. return;
  183. }
  184. gs->map->removeBlockVisTiles(obj);
  185. obj->pos = nPos;
  186. gs->map->addBlockVisTiles(obj);
  187. }
  188. DLL_EXPORT void RemoveObject::applyGs( CGameState *gs )
  189. {
  190. CGObjectInstance *obj = gs->map->objects[id];
  191. if(obj->ID==HEROI_TYPE)
  192. {
  193. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  194. std::vector<CGHeroInstance*>::iterator nitr = std::find(gs->map->heroes.begin(), gs->map->heroes.end(),h);
  195. gs->map->heroes.erase(nitr);
  196. int player = h->tempOwner;
  197. nitr = std::find(gs->getPlayer(player)->heroes.begin(), gs->getPlayer(player)->heroes.end(), h);
  198. gs->getPlayer(player)->heroes.erase(nitr);
  199. if(h->visitedTown)
  200. {
  201. if(h->inTownGarrison)
  202. h->visitedTown->garrisonHero = NULL;
  203. else
  204. h->visitedTown->visitingHero = NULL;
  205. h->visitedTown = NULL;
  206. }
  207. //TODO: add to the pool?
  208. }
  209. gs->map->objects[id] = NULL;
  210. //unblock tiles
  211. if(obj->defInfo)
  212. {
  213. gs->map->removeBlockVisTiles(obj);
  214. }
  215. }
  216. void TryMoveHero::applyGs( CGameState *gs )
  217. {
  218. CGHeroInstance *h = gs->getHero(id);
  219. h->movement = movePoints;
  220. if(start!=end && (result == SUCCESS || result == TELEPORTATION))
  221. {
  222. gs->map->removeBlockVisTiles(h);
  223. h->pos = end;
  224. gs->map->addBlockVisTiles(h);
  225. }
  226. BOOST_FOREACH(int3 t, fowRevealed)
  227. gs->getPlayer(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
  228. }
  229. DLL_EXPORT void SetGarrisons::applyGs( CGameState *gs )
  230. {
  231. for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  232. {
  233. CArmedInstance *ai = static_cast<CArmedInstance*>(gs->map->objects[i->first]);
  234. ai->army = i->second;
  235. if(ai->ID==TOWNI_TYPE && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  236. const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->army = i->second;
  237. else if(ai->ID==HEROI_TYPE)
  238. {
  239. CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  240. if(h->visitedTown && h->inTownGarrison)
  241. h->visitedTown->army = i->second;
  242. }
  243. }
  244. }
  245. DLL_EXPORT void NewStructures::applyGs( CGameState *gs )
  246. {
  247. CGTownInstance*t = gs->getTown(tid);
  248. BOOST_FOREACH(si32 id,bid)
  249. t->builtBuildings.insert(id);
  250. t->builded = builded;
  251. }
  252. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  253. {
  254. gs->getTown(tid)->strInfo.creatures = creatures;
  255. }
  256. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  257. {
  258. CGTownInstance *t = gs->getTown(tid);
  259. CGHeroInstance *v = gs->getHero(visiting),
  260. *g = gs->getHero(garrison);
  261. t->visitingHero = v;
  262. t->garrisonHero = g;
  263. if(v)
  264. {
  265. v->visitedTown = t;
  266. v->inTownGarrison = false;
  267. gs->map->addBlockVisTiles(v);
  268. }
  269. if(g)
  270. {
  271. g->visitedTown = t;
  272. g->inTownGarrison = true;
  273. gs->map->removeBlockVisTiles(g);
  274. }
  275. }
  276. DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  277. {
  278. CGHeroInstance *h = gs->getHero(hid);
  279. std::vector<ui32> equiped, unequiped;
  280. for(std::map<ui16,ui32>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
  281. if(!vstd::contains(artifWorn,i->first) || artifWorn[i->first] != i->second)
  282. unequiped.push_back(i->second);
  283. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  284. if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  285. equiped.push_back(i->second);
  286. BOOST_FOREACH(ui32 id, equiped)
  287. {
  288. //if hero already had equipped at least one artifact of that type, don't give any new bonuses
  289. if(h->getArtPos(id) >= 0)
  290. continue;
  291. CArtifact &art = VLC->arth->artifacts[id];
  292. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  293. {
  294. gained.push_back(&*i);
  295. h->bonuses.push_back(*i);
  296. }
  297. }
  298. //update hero data
  299. h->artifacts = artifacts;
  300. h->artifWorn = artifWorn;
  301. //remove bonus from unequipped artifact
  302. BOOST_FOREACH(ui32 id, unequiped)
  303. {
  304. //if hero still has equipped at least one artifact of that type, don't remove bonuses
  305. if(h->getArtPos(id) >= 0)
  306. continue;
  307. while(1)
  308. {
  309. std::list<HeroBonus>::iterator hlp = std::find_if(h->bonuses.begin(),h->bonuses.end(),boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,id));
  310. if(hlp != h->bonuses.end())
  311. {
  312. lost.push_back(&*hlp);
  313. h->bonuses.erase(hlp);
  314. }
  315. else
  316. {
  317. break;
  318. }
  319. }
  320. }
  321. }
  322. DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, int art)
  323. {
  324. if(art<0)
  325. {
  326. if(pos<19)
  327. artifWorn.erase(pos);
  328. else
  329. artifacts -= artifacts[pos-19];
  330. }
  331. else
  332. {
  333. if(pos<19)
  334. artifWorn[pos] = art;
  335. else
  336. if(pos-19 < artifacts.size())
  337. artifacts[pos-19] = art;
  338. else
  339. artifacts.push_back(art);
  340. }
  341. }
  342. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  343. {
  344. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  345. CGTownInstance *t = gs->getTown(tid);
  346. h->setOwner(player);
  347. h->pos = tile;
  348. h->movement = h->maxMovePoints(true);
  349. gs->hpool.heroesPool.erase(hid);
  350. if(h->id < 0)
  351. {
  352. h->id = gs->map->objects.size();
  353. gs->map->objects.push_back(h);
  354. }
  355. else
  356. gs->map->objects[h->id] = h;
  357. h->initHeroDefInfo();
  358. gs->map->heroes.push_back(h);
  359. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  360. gs->map->addBlockVisTiles(h);
  361. t->visitingHero = h;
  362. h->visitedTown = t;
  363. h->inTownGarrison = false;
  364. }
  365. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  366. {
  367. CGHeroInstance *h = gs->getHero(id);
  368. gs->map->removeBlockVisTiles(h,true);
  369. h->setOwner(player);
  370. h->movement = h->maxMovePoints(true);
  371. h->initHeroDefInfo();
  372. gs->map->heroes.push_back(h);
  373. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  374. gs->map->addBlockVisTiles(h);
  375. h->inTownGarrison = false;
  376. }
  377. DLL_EXPORT void HeroExchange::applyGs(CGameState *gs)
  378. {
  379. }
  380. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  381. {
  382. gs->day = day;
  383. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  384. {
  385. CGHeroInstance *hero = gs->getHero(h.id);
  386. hero->movement = h.move;
  387. hero->mana = h.mana;
  388. }
  389. BOOST_FOREACH(SetResources h, res) //give resources
  390. h.applyGs(gs);
  391. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  392. h.applyGs(gs);
  393. if(resetBuilded) //reset amount of structures set in this turn in towns
  394. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  395. t->builded = 0;
  396. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  397. h->bonuses.remove_if(HeroBonus::OneDay);
  398. if(gs->getDate(1) == 7) //new week
  399. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  400. h->bonuses.remove_if(HeroBonus::OneWeek);
  401. }
  402. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  403. {
  404. CGObjectInstance *obj = gs->map->objects[id];
  405. if(!obj)
  406. tlog1 << "Wrong object ID - property cannot be set!\n";
  407. else
  408. obj->setProperty(what,val);
  409. }
  410. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  411. {
  412. gs->map->objects[id]->hoverName = toString(name);
  413. }
  414. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  415. {
  416. gs->getHero(heroid)->level = level;
  417. }
  418. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  419. {
  420. gs->curB = info;
  421. }
  422. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  423. {
  424. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  425. gs->curB->round = round;
  426. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  427. {
  428. s->state -= DEFENDING;
  429. s->state -= WAITING;
  430. s->state -= MOVED;
  431. s->state -= HAD_MORALE;
  432. s->counterAttacks = 1;
  433. //remove effects and restore only those with remaining turns in duration
  434. std::vector<CStack::StackEffect> tmpEffects = s->effects;
  435. s->effects.clear();
  436. for(int i=0; i < tmpEffects.size(); i++)
  437. {
  438. tmpEffects[i].turnsRemain--;
  439. if(tmpEffects[i].turnsRemain > 0)
  440. s->effects.push_back(tmpEffects[i]);
  441. }
  442. //the same as above for features
  443. std::vector<StackFeature> tmpFeatures = s->features;
  444. s->features.clear();
  445. for(int i=0; i < tmpFeatures.size(); i++)
  446. {
  447. if(tmpFeatures[i].duration == StackFeature::N_TURNS)
  448. {
  449. tmpFeatures[i].turnsRemain--;
  450. if(tmpFeatures[i].turnsRemain > 0)
  451. s->features.push_back(tmpFeatures[i]);
  452. }
  453. else
  454. {
  455. s->features.push_back(tmpFeatures[i]);
  456. }
  457. }
  458. }
  459. }
  460. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  461. {
  462. gs->curB->activeStack = stack;
  463. CStack *st = gs->curB->getStack(stack);
  464. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  465. st->state.insert(HAD_MORALE);
  466. }
  467. void BattleResult::applyGs( CGameState *gs )
  468. {
  469. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  470. delete gs->curB->stacks[i];
  471. //remove any "until next battle" bonuses
  472. CGHeroInstance *h;
  473. h = gs->getHero(gs->curB->hero1);
  474. if(h)
  475. h->bonuses.remove_if(HeroBonus::OneBattle);
  476. h = gs->getHero(gs->curB->hero2);
  477. if(h)
  478. h->bonuses.remove_if(HeroBonus::OneBattle);
  479. delete gs->curB;
  480. gs->curB = NULL;
  481. }
  482. void BattleStackMoved::applyGs( CGameState *gs )
  483. {
  484. gs->curB->getStack(stack)->position = tile;
  485. }
  486. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  487. {
  488. CStack * at = gs->curB->getStack(stackAttacked);
  489. at->amount = newAmount;
  490. at->firstHPleft = newHP;
  491. if(killed())
  492. at->state -= ALIVE;
  493. }
  494. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  495. {
  496. CStack *attacker = gs->curB->getStack(stackAttacking);
  497. if(counter())
  498. attacker->counterAttacks--;
  499. if(shot())
  500. attacker->shots--;
  501. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  502. stackAttacked.applyGs(gs);
  503. }
  504. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  505. {
  506. CStack *st = gs->curB->getStack(ba.stackNumber);
  507. switch(ba.actionType)
  508. {
  509. case 3:
  510. st->state.insert(DEFENDING);
  511. break;
  512. case 8:
  513. st->state.insert(WAITING);
  514. break;
  515. case 2: case 6: case 7: case 9: case 10: case 11:
  516. st->state.insert(MOVED);
  517. break;
  518. }
  519. }
  520. DLL_EXPORT void SpellCast::applyGs( CGameState *gs )
  521. {
  522. CGHeroInstance *h = (side) ? gs->getHero(gs->curB->hero2) : gs->getHero(gs->curB->hero1);
  523. if(h)
  524. {
  525. h->mana -= VLC->spellh->spells[id].costs[skill];
  526. if(h->mana < 0) h->mana = 0;
  527. }
  528. if(side >= 0 && side < 2)
  529. {
  530. gs->curB->castSpells[side]++;
  531. }
  532. if(gs->curB && id == 35) //dispel
  533. {
  534. CStack *s = gs->curB->getStackT(tile);
  535. if(s)
  536. {
  537. s->effects.clear(); //removing all effects
  538. //removing all features from spells
  539. std::vector<StackFeature> tmpFeatures = s->features;
  540. s->features.clear();
  541. for(int i=0; i < tmpFeatures.size(); i++)
  542. {
  543. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT)
  544. {
  545. s->features.push_back(tmpFeatures[i]);
  546. }
  547. }
  548. }
  549. }
  550. }
  551. static inline StackFeature featureGenerator(StackFeature::ECombatFeatures type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0)
  552. {
  553. return makeFeature(type, StackFeature::N_TURNS, subtype, value, StackFeature::SPELL_EFFECT, turnsRemain, additionalInfo);
  554. }
  555. static std::vector<StackFeature> stackEffectToFeature(const CStack::StackEffect & sse)
  556. {
  557. std::vector<StackFeature> sf;
  558. switch(sse.id)
  559. {
  560. case 27: //shield
  561. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  562. break;
  563. case 28: //air shield
  564. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  565. break;
  566. case 30: //protection from air
  567. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  568. break;
  569. case 31: //protection from fire
  570. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  571. break;
  572. case 32: //protection from water
  573. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 2, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  574. break;
  575. case 33: //protection from earth
  576. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 3, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  577. break;
  578. case 41: //bless
  579. sf.push_back(featureGenerator(StackFeature::ALWAYS_MAXIMUM_DAMAGE, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  580. break;
  581. case 42: //curse
  582. sf.push_back(featureGenerator(StackFeature::ALWAYS_MINUMUM_DAMAGE, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain, sse.level >= 2 ? 20 : 0));
  583. break;
  584. case 43: //bloodlust
  585. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  586. break;
  587. case 44: //precision
  588. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  589. break;
  590. case 45: //weakness
  591. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  592. break;
  593. case 46: //stone skin
  594. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  595. break;
  596. case 47: //disrupting ray
  597. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  598. break;
  599. case 48: //prayer
  600. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  601. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  602. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  603. break;
  604. case 49: //mirth
  605. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  606. break;
  607. case 50: //sorrow
  608. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  609. break;
  610. case 51: //fortune
  611. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  612. break;
  613. case 52: //misfortune
  614. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  615. break;
  616. case 53: //haste
  617. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  618. break;
  619. case 54: //slow
  620. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, 0, sse.turnsRemain, -1 * VLC->spellh->spells[sse.id].powers[sse.level]));
  621. break;
  622. case 55: //slayer
  623. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  624. break;
  625. case 61: //forgetfulness
  626. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  627. break;
  628. case 56: //frenzy
  629. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  630. break;
  631. }
  632. return sf;
  633. }
  634. void actualizeEffect(CStack * s, CStack::StackEffect & ef)
  635. {
  636. //actualizing effects vector
  637. for(int g=0; g<s->effects.size(); ++g)
  638. {
  639. if(s->effects[g].id == ef.id)
  640. {
  641. s->effects[g].turnsRemain = std::max(s->effects[g].turnsRemain, ef.turnsRemain);
  642. }
  643. }
  644. //actualizing features vector
  645. std::vector<StackFeature> sf = stackEffectToFeature(ef);
  646. for(int b=0; b<sf.size(); ++b)
  647. {
  648. for(int g=0; g<s->features.size(); ++g)
  649. {
  650. if(s->features[g].source == StackFeature::SPELL_EFFECT && s->features[g].type == sf[b].type && s->features[g].subtype == sf[b].subtype)
  651. {
  652. s->features[g].turnsRemain = std::max(s->features[g].turnsRemain, ef.turnsRemain);
  653. }
  654. }
  655. }
  656. }
  657. bool containsEff(const std::vector<CStack::StackEffect> & vec, int effectId)
  658. {
  659. for(int g=0; g<vec.size(); ++g)
  660. {
  661. if(vec[g].id == effectId)
  662. return true;
  663. }
  664. return false;
  665. }
  666. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  667. {
  668. BOOST_FOREACH(ui32 id, stacks)
  669. {
  670. CStack *s = gs->curB->getStack(id);
  671. if(s)
  672. {
  673. if(effect.id == 42 || !containsEff(s->effects, effect.id))//disrupting ray or not on the list - just add
  674. {
  675. s->effects.push_back(effect);
  676. std::vector<StackFeature> sf = stackEffectToFeature(effect);
  677. for(int n=0; n<sf.size(); ++n)
  678. {
  679. s->features.push_back(sf[n]);
  680. }
  681. }
  682. else //just actualize
  683. {
  684. actualizeEffect(s, effect);
  685. }
  686. }
  687. else
  688. tlog1 << "Cannot find stack " << id << std::endl;
  689. }
  690. }
  691. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  692. {
  693. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  694. stackAttacked.applyGs(gs);
  695. }
  696. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  697. {
  698. gs->currentPlayer = player;
  699. }
  700. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  701. {
  702. gs->getPlayer(player)->currentSelection = id;
  703. }