NetPacksLib.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. #define VCMI_DLL
  2. #include "../lib/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 "../lib/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)
  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. h->artifacts = artifacts;
  287. h->artifWorn = artifWorn;
  288. BOOST_FOREACH(ui32 id, unequiped)
  289. {
  290. while(1)
  291. {
  292. std::list<HeroBonus>::iterator hlp = std::find_if(h->bonuses.begin(),h->bonuses.end(),boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,id));
  293. if(hlp != h->bonuses.end())
  294. {
  295. lost.push_back(&*hlp);
  296. h->bonuses.erase(hlp);
  297. }
  298. else
  299. {
  300. break;
  301. }
  302. }
  303. }
  304. BOOST_FOREACH(ui32 id, equiped)
  305. {
  306. CArtifact &art = VLC->arth->artifacts[id];
  307. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  308. {
  309. gained.push_back(&*i);
  310. h->bonuses.push_back(*i);
  311. }
  312. }
  313. }
  314. DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, int art)
  315. {
  316. if(art<0)
  317. {
  318. if(pos<19)
  319. artifWorn.erase(pos);
  320. else
  321. artifacts -= artifacts[pos-19];
  322. }
  323. else
  324. {
  325. if(pos<19)
  326. artifWorn[pos] = art;
  327. else
  328. if(pos-19 < artifacts.size())
  329. artifacts[pos-19] = art;
  330. else
  331. artifacts.push_back(art);
  332. }
  333. }
  334. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  335. {
  336. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  337. CGTownInstance *t = gs->getTown(tid);
  338. h->setOwner(player);
  339. h->pos = tile;
  340. h->movement = h->maxMovePoints(true);
  341. gs->hpool.heroesPool.erase(hid);
  342. if(h->id < 0)
  343. {
  344. h->id = gs->map->objects.size();
  345. gs->map->objects.push_back(h);
  346. }
  347. else
  348. gs->map->objects[h->id] = h;
  349. h->initHeroDefInfo();
  350. gs->map->heroes.push_back(h);
  351. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  352. gs->map->addBlockVisTiles(h);
  353. t->visitingHero = h;
  354. h->visitedTown = t;
  355. h->inTownGarrison = false;
  356. }
  357. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  358. {
  359. CGHeroInstance *h = gs->getHero(id);
  360. gs->map->removeBlockVisTiles(h,true);
  361. h->setOwner(player);
  362. h->movement = h->maxMovePoints(true);
  363. h->initHeroDefInfo();
  364. gs->map->heroes.push_back(h);
  365. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  366. gs->map->addBlockVisTiles(h);
  367. h->inTownGarrison = false;
  368. }
  369. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  370. {
  371. gs->day = day;
  372. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  373. {
  374. CGHeroInstance *hero = gs->getHero(h.id);
  375. hero->movement = h.move;
  376. hero->mana = h.mana;
  377. }
  378. BOOST_FOREACH(SetResources h, res) //give resources
  379. h.applyGs(gs);
  380. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  381. h.applyGs(gs);
  382. if(resetBuilded) //reset amount of structures set in this turn in towns
  383. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  384. t->builded = 0;
  385. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  386. h->bonuses.remove_if(HeroBonus::OneDay);
  387. if(gs->getDate(1) == 7) //new week
  388. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  389. h->bonuses.remove_if(HeroBonus::OneWeek);
  390. }
  391. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  392. {
  393. CGObjectInstance *obj = gs->map->objects[id];
  394. if(!obj)
  395. tlog1 << "Wrong object ID - property cannot be set!\n";
  396. else
  397. obj->setProperty(what,val);
  398. }
  399. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  400. {
  401. gs->map->objects[id]->hoverName = toString(name);
  402. }
  403. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  404. {
  405. gs->getHero(heroid)->level = level;
  406. }
  407. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  408. {
  409. gs->curB = info;
  410. }
  411. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  412. {
  413. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  414. gs->curB->round = round;
  415. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  416. {
  417. s->state -= DEFENDING;
  418. s->state -= WAITING;
  419. s->state -= MOVED;
  420. s->state -= HAD_MORALE;
  421. s->counterAttacks = 1;
  422. //remove effects and restore only those with remaining turns in duration
  423. std::vector<CStack::StackEffect> tmpEffects = s->effects;
  424. s->effects.clear();
  425. for(int i=0; i < tmpEffects.size(); i++)
  426. {
  427. tmpEffects[i].turnsRemain--;
  428. if(tmpEffects[i].turnsRemain > 0)
  429. s->effects.push_back(tmpEffects[i]);
  430. }
  431. //the same as above for features
  432. std::vector<StackFeature> tmpFeatures = s->features;
  433. s->features.clear();
  434. for(int i=0; i < tmpFeatures.size(); i++)
  435. {
  436. if(tmpFeatures[i].duration == StackFeature::N_TURNS)
  437. {
  438. tmpFeatures[i].turnsRemain--;
  439. if(tmpFeatures[i].turnsRemain > 0)
  440. s->features.push_back(tmpFeatures[i]);
  441. }
  442. else
  443. {
  444. s->features.push_back(tmpFeatures[i]);
  445. }
  446. }
  447. }
  448. }
  449. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  450. {
  451. gs->curB->activeStack = stack;
  452. CStack *st = gs->curB->getStack(stack);
  453. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  454. st->state.insert(HAD_MORALE);
  455. }
  456. void BattleResult::applyGs( CGameState *gs )
  457. {
  458. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  459. delete gs->curB->stacks[i];
  460. //remove any "until next battle" bonuses
  461. CGHeroInstance *h;
  462. h = gs->getHero(gs->curB->hero1);
  463. if(h)
  464. h->bonuses.remove_if(HeroBonus::OneBattle);
  465. h = gs->getHero(gs->curB->hero2);
  466. if(h)
  467. h->bonuses.remove_if(HeroBonus::OneBattle);
  468. delete gs->curB;
  469. gs->curB = NULL;
  470. }
  471. void BattleStackMoved::applyGs( CGameState *gs )
  472. {
  473. gs->curB->getStack(stack)->position = tile;
  474. }
  475. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  476. {
  477. CStack * at = gs->curB->getStack(stackAttacked);
  478. at->amount = newAmount;
  479. at->firstHPleft = newHP;
  480. if(killed())
  481. at->state -= ALIVE;
  482. }
  483. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  484. {
  485. CStack *attacker = gs->curB->getStack(stackAttacking);
  486. if(counter())
  487. attacker->counterAttacks--;
  488. if(shot())
  489. attacker->shots--;
  490. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  491. stackAttacked.applyGs(gs);
  492. }
  493. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  494. {
  495. CStack *st = gs->curB->getStack(ba.stackNumber);
  496. switch(ba.actionType)
  497. {
  498. case 3:
  499. st->state.insert(DEFENDING);
  500. break;
  501. case 8:
  502. st->state.insert(WAITING);
  503. break;
  504. case 2: case 6: case 7: case 9: case 10: case 11:
  505. st->state.insert(MOVED);
  506. break;
  507. }
  508. }
  509. DLL_EXPORT void SpellCast::applyGs( CGameState *gs )
  510. {
  511. CGHeroInstance *h = (side) ? gs->getHero(gs->curB->hero2) : gs->getHero(gs->curB->hero1);
  512. if(h)
  513. {
  514. h->mana -= VLC->spellh->spells[id].costs[skill];
  515. if(h->mana < 0) h->mana = 0;
  516. }
  517. if(side >= 0 && side < 2)
  518. {
  519. gs->curB->castSpells[side]++;
  520. }
  521. if(gs->curB && id == 35) //dispel
  522. {
  523. CStack *s = gs->curB->getStackT(tile);
  524. if(s)
  525. {
  526. s->effects.clear(); //removing all effects
  527. //removing all features from spells
  528. std::vector<StackFeature> tmpFeatures = s->features;
  529. s->features.clear();
  530. for(int i=0; i < tmpFeatures.size(); i++)
  531. {
  532. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT)
  533. {
  534. s->features.push_back(tmpFeatures[i]);
  535. }
  536. }
  537. }
  538. }
  539. }
  540. StackFeature featureGenerator(StackFeature::ECombatFeatures type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0)
  541. {
  542. return makeFeature(type, StackFeature::N_TURNS, subtype, value, StackFeature::SPELL_EFFECT, turnsRemain, additionalInfo);
  543. }
  544. std::vector<StackFeature> stackEffectToFeature(const CStack::StackEffect & sse)
  545. {
  546. std::vector<StackFeature> sf;
  547. switch(sse.id)
  548. {
  549. case 27: //shield
  550. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  551. break;
  552. case 28: //air shield
  553. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  554. break;
  555. case 30: //protection from air
  556. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  557. break;
  558. case 31: //protection from fire
  559. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  560. break;
  561. case 32: //protection from water
  562. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 2, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  563. break;
  564. case 33: //protection from earth
  565. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 3, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  566. break;
  567. case 41: //bless
  568. sf.push_back(featureGenerator(StackFeature::ALWAYS_MAXIMUM_DAMAGE, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  569. break;
  570. case 42: //curse
  571. 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));
  572. break;
  573. case 43: //bloodlust
  574. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  575. break;
  576. case 44: //precision
  577. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  578. break;
  579. case 45: //weakness
  580. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  581. break;
  582. case 46: //stone skin
  583. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  584. break;
  585. case 47: //disrupting ray
  586. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  587. break;
  588. case 48: //prayer
  589. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  590. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  591. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  592. break;
  593. case 49: //mirth
  594. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  595. break;
  596. case 50: //sorrow
  597. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  598. break;
  599. case 51: //fortune
  600. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  601. break;
  602. case 52: //misfortune
  603. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  604. break;
  605. case 53: //haste
  606. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  607. break;
  608. case 54: //slow
  609. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, 0, sse.turnsRemain, -1 * VLC->spellh->spells[sse.id].powers[sse.level]));
  610. break;
  611. case 55: //slayer
  612. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  613. break;
  614. case 61: //forgetfulness
  615. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  616. break;
  617. case 56: //frenzy
  618. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  619. break;
  620. }
  621. return sf;
  622. }
  623. void actualizeEffect(CStack * s, CStack::StackEffect & ef)
  624. {
  625. //actualizing effects vector
  626. for(int g=0; g<s->effects.size(); ++g)
  627. {
  628. if(s->effects[g].id == ef.id)
  629. {
  630. s->effects[g].turnsRemain = std::max(s->effects[g].turnsRemain, ef.turnsRemain);
  631. }
  632. }
  633. //actualizing features vector
  634. std::vector<StackFeature> sf = stackEffectToFeature(ef);
  635. for(int b=0; b<sf.size(); ++b)
  636. {
  637. for(int g=0; g<s->features.size(); ++g)
  638. {
  639. if(s->features[g].source == StackFeature::SPELL_EFFECT && s->features[g].type == sf[b].type && s->features[g].subtype == sf[b].subtype)
  640. {
  641. s->features[g].turnsRemain = std::max(s->features[g].turnsRemain, ef.turnsRemain);
  642. }
  643. }
  644. }
  645. }
  646. bool containsEff(const std::vector<CStack::StackEffect> & vec, int effectId)
  647. {
  648. for(int g=0; g<vec.size(); ++g)
  649. {
  650. if(vec[g].id == effectId)
  651. return true;
  652. }
  653. return false;
  654. }
  655. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  656. {
  657. BOOST_FOREACH(ui32 id, stacks)
  658. {
  659. CStack *s = gs->curB->getStack(id);
  660. if(s)
  661. {
  662. if(effect.id == 42 || !containsEff(s->effects, effect.id))//disrupting ray or not on the list - just add
  663. {
  664. s->effects.push_back(effect);
  665. std::vector<StackFeature> sf = stackEffectToFeature(effect);
  666. for(int n=0; n<sf.size(); ++n)
  667. {
  668. s->features.push_back(sf[n]);
  669. }
  670. }
  671. else //just actualize
  672. {
  673. actualizeEffect(s, effect);
  674. }
  675. }
  676. else
  677. tlog1 << "Cannot find stack " << id << std::endl;
  678. }
  679. }
  680. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  681. {
  682. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  683. stackAttacked.applyGs(gs);
  684. }
  685. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  686. {
  687. gs->currentPlayer = player;
  688. }
  689. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  690. {
  691. gs->getPlayer(player)->currentSelection = id;
  692. }