NetPacksLib.cpp 21 KB

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