NetPacksLib.cpp 21 KB

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