NetPacksLib.cpp 33 KB

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