NetPacksLib.cpp 34 KB

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