NetPacksLib.cpp 33 KB

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