NetPacksLib.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  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. #undef min
  18. #undef max
  19. /*
  20. * NetPacksLib.cpp, part of VCMI engine
  21. *
  22. * Authors: listed in file AUTHORS in main folder
  23. *
  24. * License: GNU General Public License v2.0 or later
  25. * Full text of license available in license.txt file, in main folder
  26. *
  27. */
  28. #ifdef min
  29. #undef min
  30. #endif
  31. #ifdef max
  32. #undef max
  33. #endif
  34. DLL_EXPORT void SetResource::applyGs( CGameState *gs )
  35. {
  36. assert(player < PLAYER_LIMIT);
  37. amax(val, 0); //new value must be >= 0
  38. gs->getPlayer(player)->resources[resid] = val;
  39. }
  40. DLL_EXPORT void SetResources::applyGs( CGameState *gs )
  41. {
  42. assert(player < PLAYER_LIMIT);
  43. for(int i=0;i<res.size();i++)
  44. gs->getPlayer(player)->resources[i] = res[i];
  45. }
  46. DLL_EXPORT void SetPrimSkill::applyGs( CGameState *gs )
  47. {
  48. CGHeroInstance *hero = gs->getHero(id);
  49. assert(hero);
  50. if(which <4)
  51. {
  52. Bonus *skill = hero->getBonus(Selector::type(Bonus::PRIMARY_SKILL) && Selector::subtype(which) && Selector::sourceType(Bonus::HERO_BASE_SKILL));
  53. assert(skill);
  54. if(abs)
  55. skill->val = val;
  56. else
  57. skill->val += val;
  58. }
  59. else if(which == 4) //XP
  60. {
  61. if(abs)
  62. hero->exp = val;
  63. else
  64. hero->exp += val;
  65. }
  66. }
  67. DLL_EXPORT void SetSecSkill::applyGs( CGameState *gs )
  68. {
  69. CGHeroInstance *hero = gs->getHero(id);
  70. if(hero->getSecSkillLevel(which) == 0)
  71. {
  72. hero->secSkills.push_back(std::pair<int,int>(which, val));
  73. }
  74. else
  75. {
  76. for(unsigned i=0;i<hero->secSkills.size();i++)
  77. {
  78. if(hero->secSkills[i].first == which)
  79. {
  80. if(abs)
  81. hero->secSkills[i].second = val;
  82. else
  83. hero->secSkills[i].second += val;
  84. if(hero->secSkills[i].second > 3) //workaround to avoid crashes when same sec skill is given more than once
  85. {
  86. tlog1 << "Warning: Skill " << which << " increased over limit! Decreasing to Expert.\n";
  87. hero->secSkills[i].second = 3;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. DLL_EXPORT void HeroVisitCastle::applyGs( CGameState *gs )
  94. {
  95. CGHeroInstance *h = gs->getHero(hid);
  96. CGTownInstance *t = gs->getTown(tid);
  97. if(start())
  98. {
  99. if(garrison())
  100. {
  101. t->garrisonHero = h;
  102. h->visitedTown = t;
  103. h->inTownGarrison = true;
  104. }
  105. else
  106. {
  107. t->visitingHero = h;
  108. h->visitedTown = t;
  109. h->inTownGarrison = false;
  110. }
  111. }
  112. else
  113. {
  114. if(garrison())
  115. {
  116. t->garrisonHero = NULL;
  117. h->visitedTown = NULL;
  118. h->inTownGarrison = false;
  119. }
  120. else
  121. {
  122. t->visitingHero = NULL;
  123. h->visitedTown = NULL;
  124. h->inTownGarrison = false;
  125. }
  126. }
  127. }
  128. DLL_EXPORT void ChangeSpells::applyGs( CGameState *gs )
  129. {
  130. CGHeroInstance *hero = gs->getHero(hid);
  131. if(learn)
  132. BOOST_FOREACH(ui32 sid, spells)
  133. hero->spells.insert(sid);
  134. else
  135. BOOST_FOREACH(ui32 sid, spells)
  136. hero->spells.erase(sid);
  137. }
  138. DLL_EXPORT void SetMana::applyGs( CGameState *gs )
  139. {
  140. CGHeroInstance *hero = gs->getHero(hid);
  141. amax(val, 0); //not less than 0
  142. hero->mana = val;
  143. }
  144. DLL_EXPORT void SetMovePoints::applyGs( CGameState *gs )
  145. {
  146. CGHeroInstance *hero = gs->getHero(hid);
  147. hero->movement = val;
  148. }
  149. DLL_EXPORT void FoWChange::applyGs( CGameState *gs )
  150. {
  151. BOOST_FOREACH(int3 t, tiles)
  152. gs->getPlayer(player)->fogOfWarMap[t.x][t.y][t.z] = mode;
  153. }
  154. DLL_EXPORT void SetAvailableHeroes::applyGs( CGameState *gs )
  155. {
  156. gs->getPlayer(player)->availableHeroes.clear();
  157. CGHeroInstance *h = (hid1>=0 ? gs->hpool.heroesPool[hid1] : NULL);
  158. gs->getPlayer(player)->availableHeroes.push_back(h);
  159. if(h && flags & 1)
  160. {
  161. h->clear();
  162. h->addStack(0, CStackInstance(VLC->creh->nameToID[h->type->refTypeStack[0]],1));
  163. }
  164. h = (hid2>=0 ? gs->hpool.heroesPool[hid2] : NULL);
  165. gs->getPlayer(player)->availableHeroes.push_back(h);
  166. if(flags & 2)
  167. {
  168. h->clear();
  169. h->addStack(0, CStackInstance(VLC->creh->nameToID[h->type->refTypeStack[0]],1));
  170. }
  171. }
  172. DLL_EXPORT void GiveBonus::applyGs( CGameState *gs )
  173. {
  174. BonusList *bonuses = NULL;
  175. switch(who)
  176. {
  177. case HERO:
  178. {
  179. CGHeroInstance *h = gs->getHero(id);
  180. assert(h);
  181. bonuses = &h->bonuses;
  182. }
  183. break;
  184. case PLAYER:
  185. {
  186. PlayerState *p = gs->getPlayer(id);
  187. assert(p);
  188. bonuses = &p->bonuses;
  189. }
  190. break;
  191. }
  192. bonuses->push_back(bonus);
  193. std::string &descr = bonuses->back().description;
  194. if(!bdescr.message.size()
  195. && bonus.source == Bonus::OBJECT
  196. && (bonus.type == Bonus::LUCK || bonus.type == Bonus::MORALE)
  197. && gs->map->objects[bonus.id]->ID == EVENTI_TYPE) //it's morale/luck bonus from an event without description
  198. {
  199. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  200. boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val)));
  201. }
  202. else
  203. {
  204. bdescr.toString(descr);
  205. }
  206. }
  207. DLL_EXPORT void ChangeObjPos::applyGs( CGameState *gs )
  208. {
  209. CGObjectInstance *obj = gs->map->objects[objid];
  210. if(!obj)
  211. {
  212. tlog1 << "Wrong ChangeObjPos: object " << objid << " doesn't exist!\n";
  213. return;
  214. }
  215. gs->map->removeBlockVisTiles(obj);
  216. obj->pos = nPos;
  217. gs->map->addBlockVisTiles(obj);
  218. }
  219. DLL_EXPORT void PlayerEndsGame::applyGs( CGameState *gs )
  220. {
  221. PlayerState *p = gs->getPlayer(player);
  222. p->status = victory ? 2 : 1;
  223. }
  224. DLL_EXPORT void RemoveBonus::applyGs( CGameState *gs )
  225. {
  226. std::list<Bonus> &bonuses = (who == HERO ? gs->getHero(whoID)->bonuses : gs->getPlayer(whoID)->bonuses);
  227. for(std::list<Bonus>::iterator i = bonuses.begin(); i != bonuses.end(); i++)
  228. {
  229. if(i->source == source && i->id == id)
  230. {
  231. bonus = *i; //backup bonus (to show to interfaces later)
  232. bonuses.erase(i);
  233. break;
  234. }
  235. }
  236. }
  237. DLL_EXPORT void RemoveObject::applyGs( CGameState *gs )
  238. {
  239. CGObjectInstance *obj = gs->map->objects[id];
  240. //unblock tiles
  241. if(obj->defInfo)
  242. {
  243. gs->map->removeBlockVisTiles(obj);
  244. }
  245. if(obj->ID==HEROI_TYPE)
  246. {
  247. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  248. std::vector<CGHeroInstance*>::iterator nitr = std::find(gs->map->heroes.begin(), gs->map->heroes.end(),h);
  249. gs->map->heroes.erase(nitr);
  250. int player = h->tempOwner;
  251. nitr = std::find(gs->getPlayer(player)->heroes.begin(), gs->getPlayer(player)->heroes.end(), h);
  252. gs->getPlayer(player)->heroes.erase(nitr);
  253. h->tempOwner = 255; //no one owns beaten hero
  254. if(h->visitedTown)
  255. {
  256. if(h->inTownGarrison)
  257. h->visitedTown->garrisonHero = NULL;
  258. else
  259. h->visitedTown->visitingHero = NULL;
  260. h->visitedTown = NULL;
  261. }
  262. //TODO: add to the pool?
  263. }
  264. else if (obj->ID==CREI_TYPE && gs->map->version > CMapHeader::RoE) //only fixed monsters can be a part of quest
  265. {
  266. CGCreature *cre = static_cast<CGCreature*>(obj);
  267. gs->map->monsters[cre->identifier]->pos = int3 (-1,-1,-1); //use nonexistent monster for quest :>
  268. }
  269. gs->map->objects[id] = NULL;
  270. }
  271. static int getDir(int3 src, int3 dst)
  272. {
  273. int ret = -1;
  274. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  275. {
  276. ret = 1;
  277. }
  278. else if(dst.x == src.x && dst.y+1 == src.y) //t
  279. {
  280. ret = 2;
  281. }
  282. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  283. {
  284. ret = 3;
  285. }
  286. else if(dst.x-1 == src.x && dst.y == src.y) //r
  287. {
  288. ret = 4;
  289. }
  290. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  291. {
  292. ret = 5;
  293. }
  294. else if(dst.x == src.x && dst.y-1 == src.y) //b
  295. {
  296. ret = 6;
  297. }
  298. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  299. {
  300. ret = 7;
  301. }
  302. else if(dst.x+1 == src.x && dst.y == src.y) //l
  303. {
  304. ret = 8;
  305. }
  306. return ret;
  307. }
  308. void TryMoveHero::applyGs( CGameState *gs )
  309. {
  310. CGHeroInstance *h = gs->getHero(id);
  311. h->movement = movePoints;
  312. if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end) {
  313. h->moveDir = getDir(start,end);
  314. }
  315. if(result == EMBARK) //hero enters boat at dest tile
  316. {
  317. const TerrainTile &tt = gs->map->getTile(CGHeroInstance::convertPosition(end, false));
  318. assert(tt.visitableObjects.size() == 1 && tt.visitableObjects.front()->ID == 8); //the only vis obj at dest is Boat
  319. CGBoat *boat = static_cast<CGBoat*>(tt.visitableObjects.front());
  320. gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
  321. h->boat = boat;
  322. boat->hero = h;
  323. }
  324. else if(result == DISEMBARK) //hero leaves boat to dest tile
  325. {
  326. h->boat->direction = h->moveDir;
  327. h->boat->pos = start;
  328. h->boat->hero = NULL;
  329. gs->map->addBlockVisTiles(h->boat);
  330. h->boat = NULL;
  331. }
  332. if(start!=end && (result == SUCCESS || result == TELEPORTATION || result == EMBARK || result == DISEMBARK))
  333. {
  334. gs->map->removeBlockVisTiles(h);
  335. h->pos = end;
  336. if(h->boat)
  337. h->boat->pos = end;
  338. gs->map->addBlockVisTiles(h);
  339. }
  340. BOOST_FOREACH(int3 t, fowRevealed)
  341. gs->getPlayer(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
  342. }
  343. DLL_EXPORT void SetGarrisons::applyGs( CGameState *gs )
  344. {
  345. for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  346. {
  347. CArmedInstance *ai = static_cast<CArmedInstance*>(gs->map->objects[i->first]);
  348. ai->setArmy(i->second);
  349. if(ai->ID==TOWNI_TYPE && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  350. const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->setArmy(i->second);
  351. else if(ai->ID==HEROI_TYPE)
  352. {
  353. CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  354. if(h->visitedTown && h->inTownGarrison)
  355. h->visitedTown->setArmy(i->second);
  356. }
  357. }
  358. }
  359. DLL_EXPORT void NewStructures::applyGs( CGameState *gs )
  360. {
  361. CGTownInstance *t = gs->getTown(tid);
  362. BOOST_FOREACH(si32 id,bid)
  363. {
  364. t->builtBuildings.insert(id);
  365. }
  366. t->builded = builded;
  367. }
  368. DLL_EXPORT void RazeStructures::applyGs( CGameState *gs )
  369. {
  370. CGTownInstance *t = gs->getTown(tid);
  371. BOOST_FOREACH(si32 id,bid)
  372. {
  373. t->builtBuildings.erase(id);
  374. }
  375. t->destroyed = destroyed; //yeaha
  376. }
  377. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  378. {
  379. CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->map->objects[tid]);
  380. assert(dw);
  381. dw->creatures = creatures;
  382. }
  383. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  384. {
  385. CGTownInstance *t = gs->getTown(tid);
  386. CGHeroInstance *v = gs->getHero(visiting),
  387. *g = gs->getHero(garrison);
  388. t->visitingHero = v;
  389. t->garrisonHero = g;
  390. if(v)
  391. {
  392. v->visitedTown = t;
  393. v->inTownGarrison = false;
  394. gs->map->addBlockVisTiles(v);
  395. }
  396. if(g)
  397. {
  398. g->visitedTown = t;
  399. g->inTownGarrison = true;
  400. gs->map->removeBlockVisTiles(g);
  401. }
  402. }
  403. DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  404. {
  405. CGHeroInstance *h = gs->getHero(hid);
  406. std::vector<ui32> equiped, unequiped;
  407. for(std::map<ui16,ui32>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
  408. if(!vstd::contains(artifWorn,i->first) || artifWorn[i->first] != i->second)
  409. unequiped.push_back(i->second);
  410. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  411. if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  412. equiped.push_back(i->second);
  413. BOOST_FOREACH(ui32 id, equiped)
  414. {
  415. //if hero already had equipped at least one artifact of that type, don't give any new bonuses
  416. if(h->getArtPos(id) >= 0)
  417. continue;
  418. CArtifact &art = VLC->arth->artifacts[id];
  419. art.addBonusesTo(&h->bonuses);
  420. art.addBonusesTo(&gained);
  421. }
  422. //update hero data
  423. h->artifacts = artifacts;
  424. h->artifWorn = artifWorn;
  425. //remove bonus from unequipped artifact
  426. BOOST_FOREACH(ui32 id, unequiped)
  427. {
  428. //if hero still has equipped at least one artifact of that type, don't remove bonuses
  429. if(h->getArtPos(id) >= 0)
  430. continue;
  431. CArtifact &art = VLC->arth->artifacts[id];
  432. art.removeBonusesFrom(&h->bonuses);
  433. art.addBonusesTo(&lost);
  434. }
  435. }
  436. DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, int art)
  437. {
  438. if(art<0)
  439. {
  440. if(pos<19)
  441. VLC->arth->unequipArtifact(artifWorn, pos);
  442. else if (pos - 19 < artifacts.size())
  443. artifacts.erase(artifacts.begin() + (pos - 19));
  444. }
  445. else
  446. {
  447. if (pos < 19) {
  448. VLC->arth->equipArtifact(artifWorn, pos, (ui32) art);
  449. } else { // Goes into the backpack.
  450. if(pos - 19 < artifacts.size())
  451. artifacts.insert(artifacts.begin() + (pos - 19), art);
  452. else
  453. artifacts.push_back(art);
  454. }
  455. }
  456. }
  457. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  458. {
  459. assert(vstd::contains(gs->hpool.heroesPool, hid));
  460. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  461. CGTownInstance *t = gs->getTown(tid);
  462. h->setOwner(player);
  463. h->pos = tile;
  464. h->movement = h->maxMovePoints(true);
  465. gs->hpool.heroesPool.erase(hid);
  466. if(h->id < 0)
  467. {
  468. h->id = gs->map->objects.size();
  469. gs->map->objects.push_back(h);
  470. }
  471. else
  472. gs->map->objects[h->id] = h;
  473. h->initHeroDefInfo();
  474. gs->map->heroes.push_back(h);
  475. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  476. h->initObj();
  477. gs->map->addBlockVisTiles(h);
  478. t->visitingHero = h;
  479. h->visitedTown = t;
  480. h->inTownGarrison = false;
  481. }
  482. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  483. {
  484. CGHeroInstance *h = gs->getHero(id);
  485. gs->map->removeBlockVisTiles(h,true);
  486. h->setOwner(player);
  487. h->movement = h->maxMovePoints(true);
  488. h->initHeroDefInfo();
  489. gs->map->heroes.push_back(h);
  490. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  491. gs->map->addBlockVisTiles(h);
  492. h->inTownGarrison = false;
  493. }
  494. DLL_EXPORT void NewObject::applyGs( CGameState *gs )
  495. {
  496. CGObjectInstance *o = NULL;
  497. switch(ID)
  498. {
  499. case 8:
  500. o = new CGBoat();
  501. break;
  502. default:
  503. o = new CGObjectInstance();
  504. break;
  505. }
  506. o->ID = ID;
  507. o->subID = subID;
  508. o->pos = pos;
  509. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  510. id = o->id = gs->map->objects.size();
  511. o->hoverName = VLC->generaltexth->names[ID];
  512. if(ID == 124) // hole
  513. {
  514. const TerrainTile &t = gs->map->getTile(pos);
  515. o->defInfo = VLC->dobjinfo->gobjs[ID][t.tertype];
  516. assert(o->defInfo);
  517. }
  518. gs->map->objects.push_back(o);
  519. gs->map->addBlockVisTiles(o);
  520. o->initObj();
  521. assert(o->defInfo);
  522. }
  523. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  524. {
  525. gs->day = day;
  526. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  527. {
  528. CGHeroInstance *hero = gs->getHero(h.id);
  529. hero->movement = h.move;
  530. hero->mana = h.mana;
  531. }
  532. for(std::map<ui8, std::vector<si32> >::iterator i = res.begin(); i != res.end(); i++)
  533. {
  534. assert(i->first < PLAYER_LIMIT);
  535. std::vector<si32> &playerRes = gs->getPlayer(i->first)->resources;
  536. for(int j = 0; j < i->second.size(); j++)
  537. playerRes[j] = i->second[j];
  538. }
  539. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  540. h.applyGs(gs);
  541. if(resetBuilded) //reset amount of structures set in this turn in towns
  542. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  543. t->builded = 0;
  544. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  545. h->bonuses.remove_if(Bonus::OneDay);
  546. if(gs->getDate(1) == 7) //new week
  547. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  548. h->bonuses.remove_if(Bonus::OneWeek);
  549. //count days without town
  550. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  551. {
  552. if(i->second.towns.size() || gs->day == 1)
  553. i->second.daysWithoutCastle = 0;
  554. else
  555. i->second.daysWithoutCastle++;
  556. i->second.bonuses.remove_if(Bonus::OneDay);
  557. if(gs->getDate(1) == 7) //new week
  558. i->second.bonuses.remove_if(Bonus::OneWeek);
  559. }
  560. }
  561. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  562. {
  563. CGObjectInstance *obj = gs->map->objects[id];
  564. if(!obj)
  565. {
  566. tlog1 << "Wrong object ID - property cannot be set!\n";
  567. return;
  568. }
  569. if(what == ObjProperty::OWNER)
  570. {
  571. if(obj->ID == TOWNI_TYPE)
  572. {
  573. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  574. if(t->tempOwner < PLAYER_LIMIT)
  575. gs->getPlayer(t->tempOwner)->towns -= t;
  576. if(val < PLAYER_LIMIT)
  577. gs->getPlayer(val)->towns.push_back(t);
  578. }
  579. }
  580. obj->setProperty(what,val);
  581. }
  582. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  583. {
  584. name.toString(gs->map->objects[id]->hoverName);
  585. }
  586. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  587. {
  588. gs->getHero(heroid)->level = level;
  589. }
  590. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  591. {
  592. gs->curB = info;
  593. info->belligerents[0]->battle = info->belligerents[1]->battle = info;
  594. }
  595. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  596. {
  597. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  598. gs->curB->round = round;
  599. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  600. {
  601. s->state -= DEFENDING;
  602. s->state -= WAITING;
  603. s->state -= MOVED;
  604. s->state -= HAD_MORALE;
  605. s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
  606. //regeneration
  607. if( s->hasBonusOfType(Bonus::HP_REGENERATION) && s->alive() )
  608. s->firstHPleft = std::min<ui32>( s->MaxHealth(), s->valOfBonuses(Bonus::HP_REGENERATION) );
  609. if( s->hasBonusOfType(Bonus::FULL_HP_REGENERATION) && s->alive() )
  610. s->firstHPleft = s->MaxHealth();
  611. //remove effects and restore only those with remaining turns in duration
  612. std::vector<CStack::StackEffect> tmpEffects = s->effects;
  613. s->effects.clear();
  614. for(int i=0; i < tmpEffects.size(); i++)
  615. {
  616. tmpEffects[i].turnsRemain--;
  617. if(tmpEffects[i].turnsRemain > 0)
  618. s->effects.push_back(tmpEffects[i]);
  619. }
  620. //the same as above for features
  621. BonusList tmpFeatures = s->bonuses;
  622. s->bonuses.clear();
  623. BOOST_FOREACH(Bonus &b, tmpFeatures)
  624. {
  625. if((b.duration & Bonus::N_TURNS) != 0)
  626. {
  627. b.turnsRemain--;
  628. if(b.turnsRemain > 0)
  629. s->bonuses.push_back(b);
  630. }
  631. else
  632. {
  633. s->bonuses.push_back(b);
  634. }
  635. }
  636. }
  637. }
  638. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  639. {
  640. gs->curB->activeStack = stack;
  641. CStack *st = gs->curB->getStack(stack);
  642. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  643. st->state.insert(HAD_MORALE);
  644. }
  645. void BattleResult::applyGs( CGameState *gs )
  646. {
  647. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  648. delete gs->curB->stacks[i];
  649. //remove any "until next battle" bonuses
  650. CGHeroInstance *h;
  651. h = gs->curB->heroes[0];
  652. if(h)
  653. h->bonuses.remove_if(Bonus::OneBattle);
  654. h = gs->curB->heroes[1];
  655. if(h)
  656. h->bonuses.remove_if(Bonus::OneBattle);
  657. gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
  658. delete gs->curB;
  659. gs->curB = NULL;
  660. }
  661. void BattleStackMoved::applyGs( CGameState *gs )
  662. {
  663. gs->curB->getStack(stack)->position = tile;
  664. }
  665. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  666. {
  667. CStack * at = gs->curB->getStack(stackAttacked);
  668. at->count = newAmount;
  669. at->firstHPleft = newHP;
  670. if(killed())
  671. at->state -= ALIVE;
  672. //life drain handling
  673. for (int g=0; g<healedStacks.size(); ++g)
  674. {
  675. healedStacks[g].applyGs(gs);
  676. }
  677. }
  678. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  679. {
  680. CStack *attacker = gs->curB->getStack(stackAttacking);
  681. if(counter())
  682. attacker->counterAttacks--;
  683. if(shot())
  684. attacker->shots--;
  685. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  686. stackAttacked.applyGs(gs);
  687. attacker->bonuses.remove_if(Bonus::UntilAttack);
  688. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  689. {
  690. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  691. stack->bonuses.remove_if(Bonus::UntilBeingAttacked);
  692. }
  693. }
  694. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  695. {
  696. CStack *st = gs->curB->getStack(ba.stackNumber);
  697. if(ba.actionType != 1) //don't check for stack if it's custom action by hero
  698. assert(st);
  699. switch(ba.actionType)
  700. {
  701. case 3:
  702. st->state.insert(DEFENDING);
  703. break;
  704. case 8:
  705. st->state.insert(WAITING);
  706. return;
  707. case 0: case 2: case 6: case 7: case 9: case 10: case 11: case 12:
  708. st->state.insert(MOVED);
  709. break;
  710. }
  711. if(st)
  712. st->state -= WAITING; //if stack was waiting it has made move, so it won't be "waiting" anymore (if the action was WAIT, then we have returned)
  713. }
  714. DLL_EXPORT void BattleSpellCast::applyGs( CGameState *gs )
  715. {
  716. assert(gs->curB);
  717. CGHeroInstance *h = (side) ? gs->curB->heroes[1] : gs->curB->heroes[0];
  718. if(h && castedByHero)
  719. {
  720. int spellCost = 0;
  721. if(gs->curB)
  722. {
  723. spellCost = gs->curB->getSpellCost(&VLC->spellh->spells[id], h);
  724. }
  725. else
  726. {
  727. spellCost = VLC->spellh->spells[id].costs[skill];
  728. }
  729. h->mana -= spellCost;
  730. if(h->mana < 0) h->mana = 0;
  731. }
  732. if(side >= 0 && side < 2 && castedByHero)
  733. {
  734. gs->curB->castSpells[side]++;
  735. }
  736. if(id == 35 || id == 78) //dispel and dispel helpful spells
  737. {
  738. bool onlyHelpful = id == 78;
  739. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  740. {
  741. CStack *s = gs->curB->getStack(*it);
  742. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  743. {
  744. std::vector<CStack::StackEffect> remainingEff;
  745. for(int g=0; g< s->effects.size(); ++g)
  746. {
  747. if (onlyHelpful && VLC->spellh->spells[ s->effects[g].id ].positiveness != 1)
  748. {
  749. remainingEff.push_back(s->effects[g]);
  750. }
  751. }
  752. s->effects.clear(); //removing all effects
  753. s->effects = remainingEff; //assigning effects that should remain
  754. //removing all features from spells
  755. BonusList tmpFeatures = s->bonuses;
  756. s->bonuses.clear();
  757. BOOST_FOREACH(Bonus &b, tmpFeatures)
  758. {
  759. const CSpell *sp = b.sourceSpell();
  760. if(sp && sp->positiveness != 1) //if(b.source != HeroBonus::SPELL_EFFECT || b.positiveness != 1)
  761. s->bonuses.push_back(b);
  762. }
  763. }
  764. }
  765. }
  766. //elemental summoning
  767. if(id >= 66 && id <= 69)
  768. {
  769. int creID;
  770. switch(id)
  771. {
  772. case 66:
  773. creID = 114; //fire elemental
  774. break;
  775. case 67:
  776. creID = 113; //earth elemental
  777. break;
  778. case 68:
  779. creID = 115; //water elemental
  780. break;
  781. case 69:
  782. creID = 112; //air elemental
  783. break;
  784. }
  785. const int3 & tile = gs->curB->tile;
  786. TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  787. int pos; //position of stack on the battlefield - to be calculated
  788. bool ac[BFIELD_SIZE];
  789. std::set<int> occupyable;
  790. bool twoHex = VLC->creh->creatures[creID]->isDoubleWide();
  791. bool flying = vstd::contains(VLC->creh->creatures[creID]->bonuses, Bonus::FLYING);
  792. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  793. for(int g=0; g<BFIELD_SIZE; ++g)
  794. {
  795. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  796. {
  797. pos = g;
  798. break;
  799. }
  800. }
  801. CStack * summonedStack = gs->curB->generateNewStack(CStackInstance(creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id].powers[skill], h), gs->curB->stacks.size(), !side, 255, ter, pos);
  802. summonedStack->state.insert(SUMMONED);
  803. //summonedStack->bonuses.push_back( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  804. gs->curB->stacks.push_back(summonedStack);
  805. }
  806. }
  807. static inline Bonus featureGenerator(Bonus::BonusType type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0, si32 limit = Bonus::NO_LIMIT)
  808. {
  809. Bonus hb(makeFeature(type, Bonus::N_TURNS, subtype, value, Bonus::SPELL_EFFECT, turnsRemain, additionalInfo));
  810. hb.effectRange = limit;
  811. return hb;
  812. }
  813. static inline Bonus featureGeneratorVT(Bonus::BonusType type, si16 subtype, si32 value, ui16 turnsRemain, ui8 valType)
  814. {
  815. Bonus ret(makeFeature(type, Bonus::N_TURNS, subtype, value, Bonus::SPELL_EFFECT, turnsRemain));
  816. ret.valType = valType;
  817. return ret;
  818. }
  819. static BonusList stackEffectToFeature(const CStack::StackEffect & sse)
  820. {
  821. BonusList sf;
  822. si32 power = VLC->spellh->spells[sse.id].powers[sse.level];
  823. switch(sse.id)
  824. {
  825. case 27: //shield
  826. sf.push_back(featureGenerator(Bonus::GENERAL_DAMAGE_REDUCTION, 0, power, sse.turnsRemain));
  827. sf.back().id = sse.id;
  828. break;
  829. case 28: //air shield
  830. sf.push_back(featureGenerator(Bonus::GENERAL_DAMAGE_REDUCTION, 1, power, sse.turnsRemain));
  831. sf.back().id = sse.id;
  832. break;
  833. case 29: //fire shield
  834. sf.push_back(featureGenerator(Bonus::FIRE_SHIELD, 0, power, sse.turnsRemain));
  835. sf.back().id = sse.id;
  836. break;
  837. case 30: //protection from air
  838. sf.push_back(featureGenerator(Bonus::SPELL_DAMAGE_REDUCTION, 0, power, sse.turnsRemain));
  839. sf.back().id = sse.id;
  840. break;
  841. case 31: //protection from fire
  842. sf.push_back(featureGenerator(Bonus::SPELL_DAMAGE_REDUCTION, 1, power, sse.turnsRemain));
  843. sf.back().id = sse.id;
  844. break;
  845. case 32: //protection from water
  846. sf.push_back(featureGenerator(Bonus::SPELL_DAMAGE_REDUCTION, 2, power, sse.turnsRemain));
  847. sf.back().id = sse.id;
  848. break;
  849. case 33: //protection from earth
  850. sf.push_back(featureGenerator(Bonus::SPELL_DAMAGE_REDUCTION, 3, power, sse.turnsRemain));
  851. sf.back().id = sse.id;
  852. break;
  853. case 34: //anti-magic
  854. sf.push_back(featureGenerator(Bonus::LEVEL_SPELL_IMMUNITY, 0, power - 1, sse.turnsRemain));
  855. sf.back().id = sse.id;
  856. break;
  857. case 41: //bless
  858. sf.push_back(featureGenerator(Bonus::ALWAYS_MAXIMUM_DAMAGE, -1, power, sse.turnsRemain));
  859. sf.back().id = sse.id;
  860. break;
  861. case 42: //curse
  862. sf.push_back(featureGenerator(Bonus::ALWAYS_MINIMUM_DAMAGE, -1, -1 * power, sse.turnsRemain, sse.level >= 2 ? 20 : 0));
  863. sf.back().id = sse.id;
  864. break;
  865. case 43: //bloodlust
  866. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK, power, sse.turnsRemain, 0, Bonus::ONLY_MELEE_FIGHT));
  867. sf.back().id = sse.id;
  868. break;
  869. case 44: //precision
  870. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK, power, sse.turnsRemain, 0, Bonus::ONLY_DISTANCE_FIGHT));
  871. sf.back().id = sse.id;
  872. break;
  873. case 45: //weakness
  874. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK, -1 * power, sse.turnsRemain));
  875. sf.back().id = sse.id;
  876. break;
  877. case 46: //stone skin
  878. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE, power, sse.turnsRemain));
  879. sf.back().id = sse.id;
  880. break;
  881. case 47: //disrupting ray
  882. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE, -1 * power, sse.turnsRemain));
  883. sf.back().id = sse.id;
  884. break;
  885. case 48: //prayer
  886. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK, power, sse.turnsRemain));
  887. sf.back().id = sse.id;
  888. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE, power, sse.turnsRemain));
  889. sf.back().id = sse.id;
  890. sf.push_back(featureGenerator(Bonus::STACKS_SPEED, 0, power, sse.turnsRemain));
  891. sf.back().id = sse.id;
  892. break;
  893. case 49: //mirth
  894. sf.push_back(featureGenerator(Bonus::MORALE, 0, power, sse.turnsRemain));
  895. sf.back().id = sse.id;
  896. break;
  897. case 50: //sorrow
  898. sf.push_back(featureGenerator(Bonus::MORALE, 0, -1 * power, sse.turnsRemain));
  899. sf.back().id = sse.id;
  900. break;
  901. case 51: //fortune
  902. sf.push_back(featureGenerator(Bonus::LUCK, 0, power, sse.turnsRemain));
  903. sf.back().id = sse.id;
  904. break;
  905. case 52: //misfortune
  906. sf.push_back(featureGenerator(Bonus::LUCK, 0, -1 * power, sse.turnsRemain));
  907. sf.back().id = sse.id;
  908. break;
  909. case 53: //haste
  910. sf.push_back(featureGenerator(Bonus::STACKS_SPEED, 0, power, sse.turnsRemain));
  911. sf.back().id = sse.id;
  912. break;
  913. case 54: //slow
  914. sf.push_back(featureGeneratorVT(Bonus::STACKS_SPEED, 0, -1 * ( 100 - power ), sse.turnsRemain, Bonus::PERCENT_TO_ALL));
  915. sf.back().id = sse.id;
  916. break;
  917. case 55: //slayer
  918. sf.push_back(featureGenerator(Bonus::SLAYER, 0, sse.level, sse.turnsRemain));
  919. sf.back().id = sse.id;
  920. break;
  921. case 56: //frenzy
  922. sf.push_back(featureGenerator(Bonus::IN_FRENZY, 0, VLC->spellh->spells[56].powers[sse.level]/100.0, sse.turnsRemain));
  923. sf.back().id = sse.id;
  924. break;
  925. case 58: //counterstrike
  926. sf.push_back(featureGenerator(Bonus::ADDITIONAL_RETALIATION, 0, power, sse.turnsRemain));
  927. sf.back().id = sse.id;
  928. break;
  929. case 59: //bersek
  930. sf.push_back(featureGenerator(Bonus::ATTACKS_NEAREST_CREATURE, 0, sse.level, sse.turnsRemain));
  931. sf.back().id = sse.id;
  932. break;
  933. case 60: //hypnotize
  934. sf.push_back(featureGenerator(Bonus::HYPNOTIZED, 0, sse.level, sse.turnsRemain));
  935. sf.back().id = sse.id;
  936. break;
  937. case 61: //forgetfulness
  938. sf.push_back(featureGenerator(Bonus::FORGETFULL, 0, sse.level, sse.turnsRemain));
  939. sf.back().id = sse.id;
  940. break;
  941. case 62: //blind
  942. sf.push_back(makeFeature(Bonus::NOT_ACTIVE, Bonus::UNITL_BEING_ATTACKED | Bonus::N_TURNS, 0, 0, Bonus::SPELL_EFFECT, sse.turnsRemain));
  943. sf.back().id = sse.id;
  944. sf.push_back(makeFeature(Bonus::GENERAL_ATTACK_REDUCTION, Bonus::UNTIL_ATTACK | Bonus::N_TURNS, 0, power, Bonus::SPELL_EFFECT, sse.turnsRemain));
  945. sf.back().id = sse.id;
  946. break;
  947. }
  948. return sf;
  949. }
  950. void actualizeEffect(CStack * s, CStack::StackEffect & ef)
  951. {
  952. //actualizing effects vector
  953. for(int g=0; g<s->effects.size(); ++g)
  954. {
  955. if(s->effects[g].id == ef.id)
  956. {
  957. s->effects[g].turnsRemain = std::max(s->effects[g].turnsRemain, ef.turnsRemain);
  958. }
  959. }
  960. //actualizing features vector
  961. BonusList sf = stackEffectToFeature(ef);
  962. BOOST_FOREACH(const Bonus &fromEffect, sf)
  963. {
  964. BOOST_FOREACH(Bonus &stackBonus, s->bonuses)
  965. {
  966. if(stackBonus.source == Bonus::SPELL_EFFECT && stackBonus.type == fromEffect.type && stackBonus.subtype == fromEffect.subtype)
  967. {
  968. stackBonus.turnsRemain = std::max(stackBonus.turnsRemain, ef.turnsRemain);
  969. }
  970. }
  971. }
  972. }
  973. bool containsEff(const std::vector<CStack::StackEffect> & vec, int effectId)
  974. {
  975. for(int g=0; g<vec.size(); ++g)
  976. {
  977. if(vec[g].id == effectId)
  978. return true;
  979. }
  980. return false;
  981. }
  982. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  983. {
  984. BOOST_FOREACH(ui32 id, stacks)
  985. {
  986. CStack *s = gs->curB->getStack(id);
  987. if(s)
  988. {
  989. if(effect.id == 42 || !containsEff(s->effects, effect.id))//disrupting ray or not on the list - just add
  990. {
  991. s->effects.push_back(effect);
  992. BonusList sf = stackEffectToFeature(effect);
  993. BOOST_FOREACH(const Bonus &fromEffect, sf)
  994. {
  995. s->bonuses.push_back(fromEffect);
  996. }
  997. }
  998. else //just actualize
  999. {
  1000. actualizeEffect(s, effect);
  1001. }
  1002. }
  1003. else
  1004. tlog1 << "Cannot find stack " << id << std::endl;
  1005. }
  1006. }
  1007. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  1008. {
  1009. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  1010. stackAttacked.applyGs(gs);
  1011. }
  1012. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  1013. {
  1014. for(int g=0; g<healedStacks.size(); ++g)
  1015. {
  1016. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  1017. //checking if we resurrect a stack that is under a living stack
  1018. std::vector<int> access = gs->curB->getAccessibility(changedStack->ID, true);
  1019. bool acc[BFIELD_SIZE];
  1020. for(int h=0; h<BFIELD_SIZE; ++h)
  1021. acc[h] = false;
  1022. for(int h=0; h<access.size(); ++h)
  1023. acc[access[h]] = true;
  1024. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  1025. changedStack->doubleWide(), changedStack->attackerOwned,
  1026. changedStack->hasBonusOfType(Bonus::FLYING), true))
  1027. return; //position is already occupied
  1028. //applying changes
  1029. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1030. if(resurrected)
  1031. {
  1032. changedStack->state.insert(ALIVE);
  1033. if(healedStacks[g].lowLevelResurrection)
  1034. changedStack->state.insert(SUMMONED);
  1035. //changedStack->bonuses.push_back( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1036. }
  1037. int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1038. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1039. changedStack->count += res;
  1040. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1041. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1042. {
  1043. changedStack->firstHPleft -= changedStack->MaxHealth();
  1044. if(changedStack->baseAmount > changedStack->count)
  1045. {
  1046. changedStack->count += 1;
  1047. }
  1048. }
  1049. amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1050. //removal of negative effects
  1051. if(resurrected)
  1052. {
  1053. for(int h=0; h<changedStack->effects.size(); ++h)
  1054. {
  1055. if(VLC->spellh->spells[changedStack->effects[h].id].positiveness < 0)
  1056. {
  1057. changedStack->effects.erase(changedStack->effects.begin() + h);
  1058. }
  1059. }
  1060. //removing all features from negative spells
  1061. BonusList tmpFeatures = changedStack->bonuses;
  1062. changedStack->bonuses.clear();
  1063. BOOST_FOREACH(Bonus &b, tmpFeatures)
  1064. {
  1065. const CSpell *s = b.sourceSpell();
  1066. if(s && s->positiveness >= 0)
  1067. {
  1068. changedStack->bonuses.push_back(b);
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  1075. {
  1076. if(gs->curB) //if there is a battle
  1077. {
  1078. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1079. {
  1080. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1081. {
  1082. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1083. {
  1084. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1085. break;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. }
  1091. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  1092. {
  1093. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1094. {
  1095. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1096. {
  1097. gs->curB->si.wallState[it->first.first] =
  1098. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1099. }
  1100. }
  1101. }
  1102. DLL_EXPORT void BattleStacksRemoved::applyGs( CGameState *gs )
  1103. {
  1104. if(!gs->curB)
  1105. return;
  1106. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1107. {
  1108. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1109. {
  1110. if(gs->curB->stacks[b]->ID == *it) //if found
  1111. {
  1112. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1113. break;
  1114. }
  1115. }
  1116. }
  1117. }
  1118. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  1119. {
  1120. gs->currentPlayer = player;
  1121. }
  1122. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  1123. {
  1124. gs->getPlayer(player)->currentSelection = id;
  1125. }
  1126. DLL_EXPORT Component::Component(const CStackInstance &stack)
  1127. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1128. {
  1129. }