2
0

NetPacksLib.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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. //unblock tiles
  238. if(obj->defInfo)
  239. {
  240. gs->map->removeBlockVisTiles(obj);
  241. }
  242. if(obj->ID==HEROI_TYPE)
  243. {
  244. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  245. std::vector<CGHeroInstance*>::iterator nitr = std::find(gs->map->heroes.begin(), gs->map->heroes.end(),h);
  246. gs->map->heroes.erase(nitr);
  247. int player = h->tempOwner;
  248. nitr = std::find(gs->getPlayer(player)->heroes.begin(), gs->getPlayer(player)->heroes.end(), h);
  249. gs->getPlayer(player)->heroes.erase(nitr);
  250. h->tempOwner = 255; //no one owns beaten hero
  251. if(h->visitedTown)
  252. {
  253. if(h->inTownGarrison)
  254. h->visitedTown->garrisonHero = NULL;
  255. else
  256. h->visitedTown->visitingHero = NULL;
  257. h->visitedTown = NULL;
  258. }
  259. //TODO: add to the pool?
  260. }
  261. else if (obj->ID==CREI_TYPE && gs->map->version > CMapHeader::RoE) //only fixed monsters can be a part of quest
  262. {
  263. CGCreature *cre = static_cast<CGCreature*>(obj);
  264. gs->map->monsters[cre->identifier]->pos = int3 (-1,-1,-1); //use nonexistent monster for quest :>
  265. }
  266. gs->map->objects[id] = NULL;
  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. if(ID == 124) // hole
  522. {
  523. const TerrainTile &t = gs->map->getTile(pos);
  524. o->defInfo = VLC->dobjinfo->gobjs[ID][t.tertype];
  525. assert(o->defInfo);
  526. }
  527. gs->map->objects.push_back(o);
  528. gs->map->addBlockVisTiles(o);
  529. o->initObj();
  530. assert(o->defInfo);
  531. }
  532. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  533. {
  534. gs->day = day;
  535. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  536. {
  537. CGHeroInstance *hero = gs->getHero(h.id);
  538. hero->movement = h.move;
  539. hero->mana = h.mana;
  540. }
  541. for(std::map<ui8, std::vector<si32> >::iterator i = res.begin(); i != res.end(); i++)
  542. {
  543. assert(i->first < PLAYER_LIMIT);
  544. std::vector<si32> &playerRes = gs->getPlayer(i->first)->resources;
  545. for(int j = 0; j < i->second.size(); j++)
  546. playerRes[j] = i->second[j];
  547. }
  548. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  549. h.applyGs(gs);
  550. if(resetBuilded) //reset amount of structures set in this turn in towns
  551. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  552. t->builded = 0;
  553. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  554. h->bonuses.remove_if(HeroBonus::OneDay);
  555. if(gs->getDate(1) == 7) //new week
  556. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  557. h->bonuses.remove_if(HeroBonus::OneWeek);
  558. //count days without town
  559. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  560. {
  561. if(i->second.towns.size() || gs->day == 1)
  562. i->second.daysWithoutCastle = 0;
  563. else
  564. i->second.daysWithoutCastle++;
  565. i->second.bonuses.remove_if(HeroBonus::OneDay);
  566. if(gs->getDate(1) == 7) //new week
  567. i->second.bonuses.remove_if(HeroBonus::OneWeek);
  568. }
  569. }
  570. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  571. {
  572. CGObjectInstance *obj = gs->map->objects[id];
  573. if(!obj)
  574. {
  575. tlog1 << "Wrong object ID - property cannot be set!\n";
  576. return;
  577. }
  578. if(what == 1)
  579. {
  580. if(obj->ID == TOWNI_TYPE)
  581. {
  582. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  583. if(t->tempOwner < PLAYER_LIMIT)
  584. gs->getPlayer(t->tempOwner)->towns -= t;
  585. if(val < PLAYER_LIMIT)
  586. gs->getPlayer(val)->towns.push_back(t);
  587. }
  588. }
  589. obj->setProperty(what,val);
  590. }
  591. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  592. {
  593. name.toString(gs->map->objects[id]->hoverName);
  594. }
  595. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  596. {
  597. gs->getHero(heroid)->level = level;
  598. }
  599. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  600. {
  601. gs->curB = info;
  602. }
  603. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  604. {
  605. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  606. gs->curB->round = round;
  607. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  608. {
  609. s->state -= DEFENDING;
  610. s->state -= WAITING;
  611. s->state -= MOVED;
  612. s->state -= HAD_MORALE;
  613. s->counterAttacks = 1 + s->valOfFeatures(StackFeature::ADDITIONAL_RETALIATION);
  614. //regeneration
  615. if( s->hasFeatureOfType(StackFeature::HP_REGENERATION) && s->alive() )
  616. s->firstHPleft = std::min<ui32>( s->MaxHealth(), s->valOfFeatures(StackFeature::HP_REGENERATION) );
  617. if( s->hasFeatureOfType(StackFeature::FULL_HP_REGENERATION) && s->alive() )
  618. s->firstHPleft = s->MaxHealth();
  619. //remove effects and restore only those with remaining turns in duration
  620. std::vector<CStack::StackEffect> tmpEffects = s->effects;
  621. s->effects.clear();
  622. for(int i=0; i < tmpEffects.size(); i++)
  623. {
  624. tmpEffects[i].turnsRemain--;
  625. if(tmpEffects[i].turnsRemain > 0)
  626. s->effects.push_back(tmpEffects[i]);
  627. }
  628. //the same as above for features
  629. std::vector<StackFeature> tmpFeatures = s->features;
  630. s->features.clear();
  631. for(int i=0; i < tmpFeatures.size(); i++)
  632. {
  633. if((tmpFeatures[i].duration & StackFeature::N_TURNS) != 0)
  634. {
  635. tmpFeatures[i].turnsRemain--;
  636. if(tmpFeatures[i].turnsRemain > 0)
  637. s->features.push_back(tmpFeatures[i]);
  638. }
  639. else
  640. {
  641. s->features.push_back(tmpFeatures[i]);
  642. }
  643. }
  644. }
  645. }
  646. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  647. {
  648. gs->curB->activeStack = stack;
  649. CStack *st = gs->curB->getStack(stack);
  650. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  651. st->state.insert(HAD_MORALE);
  652. }
  653. void BattleResult::applyGs( CGameState *gs )
  654. {
  655. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  656. delete gs->curB->stacks[i];
  657. //remove any "until next battle" bonuses
  658. CGHeroInstance *h;
  659. h = gs->curB->heroes[0];
  660. if(h)
  661. h->bonuses.remove_if(HeroBonus::OneBattle);
  662. h = gs->curB->heroes[1];
  663. if(h)
  664. h->bonuses.remove_if(HeroBonus::OneBattle);
  665. delete gs->curB;
  666. gs->curB = NULL;
  667. }
  668. void BattleStackMoved::applyGs( CGameState *gs )
  669. {
  670. gs->curB->getStack(stack)->position = tile;
  671. }
  672. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  673. {
  674. CStack * at = gs->curB->getStack(stackAttacked);
  675. at->amount = newAmount;
  676. at->firstHPleft = newHP;
  677. if(killed())
  678. at->state -= ALIVE;
  679. }
  680. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  681. {
  682. CStack *attacker = gs->curB->getStack(stackAttacking);
  683. if(counter())
  684. attacker->counterAttacks--;
  685. if(shot())
  686. attacker->shots--;
  687. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  688. stackAttacked.applyGs(gs);
  689. for(int g=0; g<attacker->features.size(); ++g)
  690. {
  691. if((attacker->features[g].duration & StackFeature::UNTIL_ATTACK) != 0)
  692. {
  693. attacker->features.erase(attacker->features.begin() + g);
  694. g = 0;
  695. }
  696. }
  697. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  698. {
  699. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  700. for(int g=0; g<stack->features.size(); ++g)
  701. {
  702. if((stack->features[g].duration & StackFeature::UNITL_BEING_ATTACKED) != 0)
  703. {
  704. stack->features.erase(stack->features.begin() + g);
  705. g = 0;
  706. }
  707. }
  708. }
  709. }
  710. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  711. {
  712. CStack *st = gs->curB->getStack(ba.stackNumber);
  713. if(ba.actionType != 1) //don't check for stack if it's custom action by hero
  714. assert(st);
  715. switch(ba.actionType)
  716. {
  717. case 3:
  718. st->state.insert(DEFENDING);
  719. break;
  720. case 8:
  721. st->state.insert(WAITING);
  722. return;
  723. case 2: case 6: case 7: case 9: case 10: case 11:
  724. st->state.insert(MOVED);
  725. break;
  726. }
  727. if(st)
  728. 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)
  729. }
  730. DLL_EXPORT void SpellCast::applyGs( CGameState *gs )
  731. {
  732. CGHeroInstance *h = (side) ? gs->curB->heroes[1] : gs->curB->heroes[0];
  733. if(h)
  734. {
  735. int spellCost = 0;
  736. if(gs->curB)
  737. {
  738. spellCost = gs->curB->getSpellCost(&VLC->spellh->spells[id], h);
  739. }
  740. else
  741. {
  742. spellCost = VLC->spellh->spells[id].costs[skill];
  743. }
  744. h->mana -= spellCost;
  745. if(h->mana < 0) h->mana = 0;
  746. }
  747. if(side >= 0 && side < 2)
  748. {
  749. gs->curB->castSpells[side]++;
  750. }
  751. if(gs->curB && id == 35) //dispel
  752. {
  753. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  754. {
  755. CStack *s = gs->curB->getStack(*it);
  756. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  757. {
  758. s->effects.clear(); //removing all effects
  759. //removing all features from spells
  760. std::vector<StackFeature> tmpFeatures = s->features;
  761. s->features.clear();
  762. for(int i=0; i < tmpFeatures.size(); i++)
  763. {
  764. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT)
  765. {
  766. s->features.push_back(tmpFeatures[i]);
  767. }
  768. }
  769. }
  770. }
  771. }
  772. //elemental summoning
  773. if(id >= 66 && id <= 69)
  774. {
  775. int creID;
  776. switch(id)
  777. {
  778. case 66:
  779. creID = 114; //fire elemental
  780. break;
  781. case 67:
  782. creID = 113; //earth elemental
  783. break;
  784. case 68:
  785. creID = 115; //water elemental
  786. break;
  787. case 69:
  788. creID = 112; //air elemental
  789. break;
  790. }
  791. const int3 & tile = gs->curB->tile;
  792. TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  793. int pos; //position of stack on the battlefield - to be calculated
  794. bool ac[BFIELD_SIZE];
  795. std::set<int> occupyable;
  796. bool twoHex = vstd::contains(VLC->creh->creatures[creID].abilities, StackFeature::DOUBLE_WIDE);
  797. bool flying = vstd::contains(VLC->creh->creatures[creID].abilities, StackFeature::FLYING);
  798. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  799. for(int g=0; g<BFIELD_SIZE; ++g)
  800. {
  801. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  802. {
  803. pos = g;
  804. break;
  805. }
  806. }
  807. CStack * summonedStack = gs->curB->generateNewStack(h, creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id].powers[skill], gs->curB->stacks.size(), !side, 255, ter, pos);
  808. summonedStack->features.push_back( makeFeature(StackFeature::SUMMONED, StackFeature::WHOLE_BATTLE, 0, 0, StackFeature::BONUS_FROM_HERO) );
  809. gs->curB->stacks.push_back(summonedStack);
  810. }
  811. }
  812. static inline StackFeature featureGenerator(StackFeature::ECombatFeatures type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0)
  813. {
  814. return makeFeature(type, StackFeature::N_TURNS, subtype, value, StackFeature::SPELL_EFFECT, turnsRemain, additionalInfo);
  815. }
  816. static std::vector<StackFeature> stackEffectToFeature(const CStack::StackEffect & sse)
  817. {
  818. std::vector<StackFeature> sf;
  819. switch(sse.id)
  820. {
  821. case 27: //shield
  822. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  823. break;
  824. case 28: //air shield
  825. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  826. break;
  827. case 30: //protection from air
  828. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  829. break;
  830. case 31: //protection from fire
  831. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  832. break;
  833. case 32: //protection from water
  834. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 2, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  835. break;
  836. case 33: //protection from earth
  837. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 3, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  838. break;
  839. case 34: //anti-magic
  840. sf.push_back(featureGenerator(StackFeature::LEVEL_SPELL_IMMUNITY, 0, VLC->spellh->spells[sse.id].powers[sse.level] - 1, sse.turnsRemain));
  841. break;
  842. case 41: //bless
  843. sf.push_back(featureGenerator(StackFeature::ALWAYS_MAXIMUM_DAMAGE, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  844. break;
  845. case 42: //curse
  846. 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));
  847. break;
  848. case 43: //bloodlust
  849. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  850. break;
  851. case 44: //precision
  852. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  853. break;
  854. case 45: //weakness
  855. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  856. break;
  857. case 46: //stone skin
  858. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  859. break;
  860. case 47: //disrupting ray
  861. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  862. break;
  863. case 48: //prayer
  864. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  865. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  866. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  867. break;
  868. case 49: //mirth
  869. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  870. break;
  871. case 50: //sorrow
  872. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  873. break;
  874. case 51: //fortune
  875. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  876. break;
  877. case 52: //misfortune
  878. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  879. break;
  880. case 53: //haste
  881. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  882. break;
  883. case 54: //slow
  884. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, 0, sse.turnsRemain, -1 * ( 100 - VLC->spellh->spells[sse.id].powers[sse.level] ) ));
  885. break;
  886. case 55: //slayer
  887. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  888. break;
  889. case 56: //frenzy
  890. sf.push_back(featureGenerator(StackFeature::IN_FRENZY, 0, sse.level, sse.turnsRemain));
  891. break;
  892. case 58: //counterstrike
  893. sf.push_back(featureGenerator(StackFeature::ADDITIONAL_RETALIATION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  894. break;
  895. case 59: //bersek
  896. sf.push_back(featureGenerator(StackFeature::ATTACKS_NEAREST_CREATURE, 0, sse.level, sse.turnsRemain));
  897. break;
  898. case 60: //hypnotize
  899. sf.push_back(featureGenerator(StackFeature::HYPNOTIZED, 0, sse.level, sse.turnsRemain));
  900. break;
  901. case 61: //forgetfulness
  902. sf.push_back(featureGenerator(StackFeature::FORGETFULL, 0, sse.level, sse.turnsRemain));
  903. break;
  904. case 62: //blind
  905. sf.push_back(makeFeature(StackFeature::NOT_ACTIVE, StackFeature::UNITL_BEING_ATTACKED | StackFeature::N_TURNS, 0, 0, StackFeature::SPELL_EFFECT, sse.turnsRemain, 0));
  906. 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));
  907. break;
  908. }
  909. //setting positiveness
  910. for(int g=0; g<sf.size(); ++g)
  911. {
  912. sf[g].positiveness = VLC->spellh->spells[sse.id].positiveness;
  913. }
  914. return sf;
  915. }
  916. void actualizeEffect(CStack * s, CStack::StackEffect & ef)
  917. {
  918. //actualizing effects vector
  919. for(int g=0; g<s->effects.size(); ++g)
  920. {
  921. if(s->effects[g].id == ef.id)
  922. {
  923. s->effects[g].turnsRemain = std::max(s->effects[g].turnsRemain, ef.turnsRemain);
  924. }
  925. }
  926. //actualizing features vector
  927. std::vector<StackFeature> sf = stackEffectToFeature(ef);
  928. for(int b=0; b<sf.size(); ++b)
  929. {
  930. for(int g=0; g<s->features.size(); ++g)
  931. {
  932. if(s->features[g].source == StackFeature::SPELL_EFFECT && s->features[g].type == sf[b].type && s->features[g].subtype == sf[b].subtype)
  933. {
  934. s->features[g].turnsRemain = std::max(s->features[g].turnsRemain, ef.turnsRemain);
  935. }
  936. }
  937. }
  938. }
  939. bool containsEff(const std::vector<CStack::StackEffect> & vec, int effectId)
  940. {
  941. for(int g=0; g<vec.size(); ++g)
  942. {
  943. if(vec[g].id == effectId)
  944. return true;
  945. }
  946. return false;
  947. }
  948. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  949. {
  950. BOOST_FOREACH(ui32 id, stacks)
  951. {
  952. CStack *s = gs->curB->getStack(id);
  953. if(s)
  954. {
  955. if(effect.id == 42 || !containsEff(s->effects, effect.id))//disrupting ray or not on the list - just add
  956. {
  957. s->effects.push_back(effect);
  958. std::vector<StackFeature> sf = stackEffectToFeature(effect);
  959. for(int n=0; n<sf.size(); ++n)
  960. {
  961. s->features.push_back(sf[n]);
  962. }
  963. }
  964. else //just actualize
  965. {
  966. actualizeEffect(s, effect);
  967. }
  968. }
  969. else
  970. tlog1 << "Cannot find stack " << id << std::endl;
  971. }
  972. }
  973. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  974. {
  975. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  976. stackAttacked.applyGs(gs);
  977. }
  978. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  979. {
  980. for(int g=0; g<healedStacks.size(); ++g)
  981. {
  982. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  983. //checking if we resurrect a stack that is under a living stack
  984. std::vector<int> access = gs->curB->getAccessibility(changedStack->ID, true);
  985. bool acc[BFIELD_SIZE];
  986. for(int h=0; h<BFIELD_SIZE; ++h)
  987. acc[h] = false;
  988. for(int h=0; h<access.size(); ++h)
  989. acc[access[h]] = true;
  990. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  991. changedStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE), changedStack->attackerOwned,
  992. changedStack->hasFeatureOfType(StackFeature::FLYING), true))
  993. return; //position is already occupied
  994. //applying changes
  995. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  996. if(!changedStack->alive())
  997. {
  998. changedStack->state.insert(ALIVE);
  999. if(healedStacks[g].lowLevelResurrection)
  1000. changedStack->features.push_back( makeFeature(StackFeature::SUMMONED, StackFeature::WHOLE_BATTLE, 0, 0, StackFeature::BONUS_FROM_HERO) );
  1001. }
  1002. int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1003. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->amount );
  1004. changedStack->amount += res;
  1005. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1006. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1007. {
  1008. changedStack->firstHPleft -= changedStack->MaxHealth();
  1009. if(changedStack->baseAmount > changedStack->amount)
  1010. {
  1011. changedStack->amount += 1;
  1012. }
  1013. }
  1014. amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1015. //removal of negative effects
  1016. if(resurrected)
  1017. {
  1018. for(int h=0; h<changedStack->effects.size(); ++h)
  1019. {
  1020. if(VLC->spellh->spells[changedStack->effects[h].id].positiveness < 0)
  1021. {
  1022. changedStack->effects.erase(changedStack->effects.begin() + h);
  1023. }
  1024. }
  1025. //removing all features from negative spells
  1026. std::vector<StackFeature> tmpFeatures = changedStack->features;
  1027. changedStack->features.clear();
  1028. for(int i=0; i < tmpFeatures.size(); i++)
  1029. {
  1030. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT || tmpFeatures[i].positiveness >= 0)
  1031. {
  1032. changedStack->features.push_back(tmpFeatures[i]);
  1033. }
  1034. }
  1035. }
  1036. }
  1037. }
  1038. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  1039. {
  1040. if(gs->curB) //if there is a battle
  1041. {
  1042. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1043. {
  1044. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1045. {
  1046. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1047. {
  1048. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. }
  1054. }
  1055. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  1056. {
  1057. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1058. {
  1059. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1060. {
  1061. gs->curB->si.wallState[it->first.first] =
  1062. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1063. }
  1064. }
  1065. }
  1066. DLL_EXPORT void BattleStacksRemoved::applyGs( CGameState *gs )
  1067. {
  1068. if(!gs->curB)
  1069. return;
  1070. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1071. {
  1072. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1073. {
  1074. if(gs->curB->stacks[b]->ID == *it) //if found
  1075. {
  1076. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1077. break;
  1078. }
  1079. }
  1080. }
  1081. }
  1082. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  1083. {
  1084. gs->currentPlayer = player;
  1085. }
  1086. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  1087. {
  1088. gs->getPlayer(player)->currentSelection = id;
  1089. }