NetPacksLib.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. boost::unordered_set<int3, ShashInt3> 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 ? (CGHeroInstance*)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. return;
  263. }
  264. else if (obj->ID==CREI_TYPE && gs->map->version > CMapHeader::RoE) //only fixed monsters can be a part of quest
  265. {
  266. CGCreature *cre = static_cast<CGCreature*>(obj);
  267. gs->map->monsters[cre->identifier]->pos = int3 (-1,-1,-1); //use nonexistent monster for quest :>
  268. }
  269. gs->map->objects[id].dellNull();
  270. }
  271. static int getDir(int3 src, int3 dst)
  272. {
  273. int ret = -1;
  274. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  275. {
  276. ret = 1;
  277. }
  278. else if(dst.x == src.x && dst.y+1 == src.y) //t
  279. {
  280. ret = 2;
  281. }
  282. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  283. {
  284. ret = 3;
  285. }
  286. else if(dst.x-1 == src.x && dst.y == src.y) //r
  287. {
  288. ret = 4;
  289. }
  290. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  291. {
  292. ret = 5;
  293. }
  294. else if(dst.x == src.x && dst.y-1 == src.y) //b
  295. {
  296. ret = 6;
  297. }
  298. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  299. {
  300. ret = 7;
  301. }
  302. else if(dst.x+1 == src.x && dst.y == src.y) //l
  303. {
  304. ret = 8;
  305. }
  306. return ret;
  307. }
  308. void TryMoveHero::applyGs( CGameState *gs )
  309. {
  310. CGHeroInstance *h = gs->getHero(id);
  311. h->movement = movePoints;
  312. if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end) {
  313. h->moveDir = getDir(start,end);
  314. }
  315. if(result == EMBARK) //hero enters boat at dest tile
  316. {
  317. const TerrainTile &tt = gs->map->getTile(CGHeroInstance::convertPosition(end, false));
  318. assert(tt.visitableObjects.size() == 1 && tt.visitableObjects.front()->ID == 8); //the only vis obj at dest is Boat
  319. CGBoat *boat = static_cast<CGBoat*>(tt.visitableObjects.front());
  320. gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
  321. h->boat = boat;
  322. boat->hero = h;
  323. }
  324. else if(result == DISEMBARK) //hero leaves boat to dest tile
  325. {
  326. CGBoat *b = const_cast<CGBoat *>(h->boat);
  327. b->direction = h->moveDir;
  328. b->pos = start;
  329. b->hero = NULL;
  330. gs->map->addBlockVisTiles(b);
  331. h->boat = NULL;
  332. }
  333. if(start!=end && (result == SUCCESS || result == TELEPORTATION || result == EMBARK || result == DISEMBARK))
  334. {
  335. gs->map->removeBlockVisTiles(h);
  336. h->pos = end;
  337. if(CGBoat *b = const_cast<CGBoat *>(h->boat))
  338. b->pos = end;
  339. gs->map->addBlockVisTiles(h);
  340. }
  341. BOOST_FOREACH(int3 t, fowRevealed)
  342. gs->getPlayerTeam(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
  343. }
  344. // DLL_EXPORT void SetGarrisons::applyGs( CGameState *gs )
  345. // {
  346. // for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  347. // {
  348. // CArmedInstance *ai = static_cast<CArmedInstance*>(gs->map->objects[i->first]);
  349. // ai->setToArmy(i->second);
  350. // if(ai->ID==TOWNI_TYPE && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  351. // const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->setToArmy(i->second);
  352. // else if(ai->ID==HEROI_TYPE)
  353. // {
  354. // CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  355. // CGTownInstance *t = const_cast<CGTownInstance *>(h->visitedTown);
  356. // if(t && h->inTownGarrison)
  357. // t->setToArmy(i->second);
  358. // }
  359. // }
  360. // }
  361. DLL_EXPORT void NewStructures::applyGs( CGameState *gs )
  362. {
  363. CGTownInstance *t = gs->getTown(tid);
  364. BOOST_FOREACH(si32 id,bid)
  365. {
  366. t->builtBuildings.insert(id);
  367. }
  368. t->builded = builded;
  369. }
  370. DLL_EXPORT void RazeStructures::applyGs( CGameState *gs )
  371. {
  372. CGTownInstance *t = gs->getTown(tid);
  373. BOOST_FOREACH(si32 id,bid)
  374. {
  375. t->builtBuildings.erase(id);
  376. }
  377. t->destroyed = destroyed; //yeaha
  378. }
  379. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  380. {
  381. CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->map->objects[tid].get());
  382. assert(dw);
  383. dw->creatures = creatures;
  384. }
  385. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  386. {
  387. CGTownInstance *t = gs->getTown(tid);
  388. CGHeroInstance *v = gs->getHero(visiting),
  389. *g = gs->getHero(garrison);
  390. t->visitingHero = v;
  391. t->garrisonHero = g;
  392. if(v)
  393. {
  394. v->visitedTown = t;
  395. v->inTownGarrison = false;
  396. gs->map->addBlockVisTiles(v);
  397. }
  398. if(g)
  399. {
  400. g->visitedTown = t;
  401. g->inTownGarrison = true;
  402. gs->map->removeBlockVisTiles(g);
  403. }
  404. }
  405. // DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  406. // {
  407. // CGHeroInstance *h = gs->getHero(hid);
  408. // for(std::map<ui16, const CArtifact*>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
  409. // if(!vstd::contains(artifWorn,i->first) || artifWorn[i->first] != i->second)
  410. // unequiped.push_back(i->second);
  411. //
  412. // for(std::map<ui16, const CArtifact*>::iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  413. // if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  414. // {
  415. // equiped.push_back(i->second);
  416. // }
  417. //
  418. // //update hero data
  419. // h->artifacts = artifacts;
  420. // h->artifWorn = artifWorn;
  421. // }
  422. //
  423. // DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, const CArtifact* art)
  424. // {
  425. // if(!art)
  426. // {
  427. // if(pos<19)
  428. // VLC->arth->unequipArtifact(artifWorn, pos);
  429. // else if (pos - 19 < artifacts.size())
  430. // artifacts.erase(artifacts.begin() + (pos - 19));
  431. // }
  432. // else
  433. // {
  434. // if (pos < 19)
  435. // {
  436. // VLC->arth->equipArtifact(artifWorn, pos, art);
  437. // }
  438. // else // Goes into the backpack.
  439. // {
  440. // if(pos - 19 < artifacts.size())
  441. // artifacts.insert(artifacts.begin() + (pos - 19), art);
  442. // else
  443. // artifacts.push_back(art);
  444. // }
  445. // }
  446. // }
  447. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  448. {
  449. assert(vstd::contains(gs->hpool.heroesPool, hid));
  450. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  451. CGTownInstance *t = gs->getTown(tid);
  452. h->setOwner(player);
  453. h->pos = tile;
  454. h->movement = h->maxMovePoints(true);
  455. gs->hpool.heroesPool.erase(hid);
  456. if(h->id < 0)
  457. {
  458. h->id = gs->map->objects.size();
  459. gs->map->objects.push_back(h);
  460. }
  461. else
  462. gs->map->objects[h->id] = h;
  463. h->initHeroDefInfo();
  464. gs->map->heroes.push_back(h);
  465. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  466. h->initObj();
  467. gs->map->addBlockVisTiles(h);
  468. if(t)
  469. {
  470. t->visitingHero = h;
  471. h->visitedTown = t;
  472. }
  473. h->inTownGarrison = false;
  474. }
  475. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  476. {
  477. CGHeroInstance *h = gs->getHero(id);
  478. gs->map->removeBlockVisTiles(h,true);
  479. h->setOwner(player);
  480. h->movement = h->maxMovePoints(true);
  481. h->initHeroDefInfo();
  482. gs->map->heroes.push_back(h);
  483. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  484. gs->map->addBlockVisTiles(h);
  485. h->inTownGarrison = false;
  486. }
  487. DLL_EXPORT void NewObject::applyGs( CGameState *gs )
  488. {
  489. CGObjectInstance *o = NULL;
  490. switch(ID)
  491. {
  492. case 8:
  493. o = new CGBoat();
  494. break;
  495. case 54: //probably more options will be needed
  496. o = new CGCreature();
  497. {
  498. //CStackInstance hlp;
  499. CGCreature *cre = static_cast<CGCreature*>(o);
  500. //cre->slots[0] = hlp;
  501. cre->notGrowingTeam = cre->neverFlees = 0;
  502. cre->character = 2;
  503. cre->gainedArtifact = -1;
  504. }
  505. break;
  506. default:
  507. o = new CGObjectInstance();
  508. break;
  509. }
  510. o->ID = ID;
  511. o->subID = subID;
  512. o->pos = pos;
  513. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  514. id = o->id = gs->map->objects.size();
  515. o->hoverName = VLC->generaltexth->names[ID];
  516. switch(ID)
  517. {
  518. case 54: //cfreature
  519. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  520. assert(o->defInfo);
  521. break;
  522. case 124://hole
  523. const TerrainTile &t = gs->map->getTile(pos);
  524. o->defInfo = VLC->dobjinfo->gobjs[ID][t.tertype];
  525. assert(o->defInfo);
  526. break;
  527. }
  528. gs->map->objects.push_back(o);
  529. gs->map->addBlockVisTiles(o);
  530. o->initObj();
  531. assert(o->defInfo);
  532. }
  533. DLL_EXPORT void NewArtifact::applyGs( CGameState *gs )
  534. {
  535. assert(!vstd::contains(gs->map->artInstances, art));
  536. art->id = gs->map->artInstances.size();
  537. gs->map->artInstances.push_back(art);
  538. assert(!art->parents.size());
  539. art->setType(art->artType);
  540. }
  541. DLL_EXPORT const CStackInstance * StackLocation::getStack()
  542. {
  543. if(!army->hasStackAtSlot(slot))
  544. {
  545. tlog2 << "Warning: " << army->nodeName() << " dont have a stack at slot " << slot << std::endl;
  546. return NULL;
  547. }
  548. return &army->getStack(slot);
  549. }
  550. DLL_EXPORT const CArtifactInstance *ArtifactLocation::getArt() const
  551. {
  552. const ArtSlotInfo *s = getSlot();
  553. if(s && s->artifact)
  554. {
  555. if(!s->locked)
  556. return s->artifact;
  557. else
  558. {
  559. tlog3 << "ArtifactLocation::getArt: That location is locked!\n";
  560. return NULL;
  561. }
  562. }
  563. return NULL;
  564. return NULL;
  565. }
  566. DLL_EXPORT CArtifactInstance *ArtifactLocation::getArt()
  567. {
  568. const ArtifactLocation *t = this;
  569. return const_cast<CArtifactInstance*>(t->getArt());
  570. }
  571. DLL_EXPORT const ArtSlotInfo *ArtifactLocation::getSlot() const
  572. {
  573. return hero->getSlot(slot);
  574. }
  575. DLL_EXPORT void ChangeStackCount::applyGs( CGameState *gs )
  576. {
  577. if(absoluteValue)
  578. sl.army->setStackCount(sl.slot, count);
  579. else
  580. sl.army->changeStackCount(sl.slot, count);
  581. }
  582. DLL_EXPORT void SetStackType::applyGs( CGameState *gs )
  583. {
  584. sl.army->setStackType(sl.slot, type);
  585. }
  586. DLL_EXPORT void EraseStack::applyGs( CGameState *gs )
  587. {
  588. sl.army->eraseStack(sl.slot);
  589. }
  590. DLL_EXPORT void SwapStacks::applyGs( CGameState *gs )
  591. {
  592. CStackInstance *s1 = sl1.army->detachStack(sl1.slot),
  593. *s2 = sl2.army->detachStack(sl2.slot);
  594. sl2.army->putStack(sl2.slot, s1);
  595. sl1.army->putStack(sl1.slot, s2);
  596. }
  597. DLL_EXPORT void InsertNewStack::applyGs( CGameState *gs )
  598. {
  599. CStackInstance *s = new CStackInstance(stack.type, stack.count);
  600. sl.army->putStack(sl.slot, s);
  601. }
  602. DLL_EXPORT void RebalanceStacks::applyGs( CGameState *gs )
  603. {
  604. const CCreature *srcType = src.army->getCreature(src.slot);
  605. TQuantity srcCount = src.army->getStackCount(src.slot);
  606. if(srcCount == count) //moving whole stack
  607. {
  608. if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> merge
  609. {
  610. assert(c == srcType);
  611. src.army->eraseStack(src.slot);
  612. dst.army->changeStackCount(dst.slot, count);
  613. }
  614. else //move stack to an empty slot
  615. {
  616. CStackInstance *stackDetached = src.army->detachStack(src.slot);
  617. dst.army->putStack(dst.slot, stackDetached);
  618. }
  619. }
  620. else
  621. {
  622. if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> rebalance
  623. {
  624. assert(c == srcType);
  625. src.army->changeStackCount(src.slot, -count);
  626. dst.army->changeStackCount(dst.slot, count);
  627. }
  628. else //split stack to an empty slot
  629. {
  630. src.army->changeStackCount(src.slot, -count);
  631. dst.army->addToSlot(dst.slot, srcType->idNumber, count, false);
  632. }
  633. }
  634. }
  635. DLL_EXPORT void PutArtifact::applyGs( CGameState *gs )
  636. {
  637. assert(art->canBePutAt(al));
  638. al.hero->putArtifact(al.slot, art);
  639. //art->putAt(al.hero, al.slot);
  640. }
  641. DLL_EXPORT void EraseArtifact::applyGs( CGameState *gs )
  642. {
  643. CArtifactInstance *a = al.getArt();
  644. assert(a);
  645. a->removeFrom(al.hero, al.slot);
  646. }
  647. DLL_EXPORT void MoveArtifact::applyGs( CGameState *gs )
  648. {
  649. CArtifactInstance *a = src.getArt();
  650. if(dst.slot < Arts::BACKPACK_START)
  651. assert(!dst.getArt());
  652. a->move(src, dst);
  653. }
  654. DLL_EXPORT void SetAvailableArtifacts::applyGs( CGameState *gs )
  655. {
  656. if(id >= 0)
  657. {
  658. if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id].get()))
  659. {
  660. bm->artifacts = arts;
  661. }
  662. else
  663. {
  664. tlog1 << "Wrong black market id!" << std::endl;
  665. }
  666. }
  667. else
  668. {
  669. CGTownInstance::merchantArtifacts = arts;
  670. }
  671. }
  672. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  673. {
  674. gs->day = day;
  675. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  676. {
  677. CGHeroInstance *hero = gs->getHero(h.id);
  678. hero->movement = h.move;
  679. hero->mana = h.mana;
  680. }
  681. for(std::map<ui8, std::vector<si32> >::iterator i = res.begin(); i != res.end(); i++)
  682. {
  683. assert(i->first < PLAYER_LIMIT);
  684. std::vector<si32> &playerRes = gs->getPlayer(i->first)->resources;
  685. for(int j = 0; j < i->second.size(); j++)
  686. playerRes[j] = i->second[j];
  687. }
  688. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  689. h.applyGs(gs);
  690. if (specialWeek != NO_ACTION) //first pack applied, reset all effects and aplly new
  691. {
  692. //TODO? won't work for battles lasting several turns (not an issue now but...)
  693. gs->globalEffects.popBonuses(Bonus::OneDay); //works for children -> all game objs
  694. if(gs->getDate(1)) //new week, Monday that is
  695. {
  696. gs->globalEffects.popBonuses(Bonus::OneWeek); //works for children -> all game objs
  697. Bonus *b = new Bonus();
  698. b->duration = Bonus::ONE_WEEK;
  699. b->source = Bonus::SPECIAL_WEEK;
  700. b->effectRange = Bonus::NO_LIMIT;
  701. b->valType = Bonus::BASE_NUMBER; //certainly not intuitive
  702. switch (specialWeek)
  703. {
  704. case DOUBLE_GROWTH:
  705. b->val = 100;
  706. b->type = Bonus::CREATURE_GROWTH_PERCENT;
  707. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false));
  708. break;
  709. case BONUS_GROWTH:
  710. b->val = 5;
  711. b->type = Bonus::CREATURE_GROWTH;
  712. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false));
  713. break;
  714. case DEITYOFFIRE:
  715. b->val = 15;
  716. b->type = Bonus::CREATURE_GROWTH;
  717. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[42], true));
  718. break;
  719. case PLAGUE:
  720. b->val = -100; //no basic creatures
  721. b->type = Bonus::CREATURE_GROWTH_PERCENT;
  722. break;
  723. default:
  724. b->val = 0;
  725. }
  726. if (b->val)
  727. gs->globalEffects.addNewBonus(b);
  728. }
  729. }
  730. else //second pack is applied
  731. {
  732. //count days without town
  733. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  734. {
  735. if(i->second.towns.size() || gs->day == 1)
  736. i->second.daysWithoutCastle = 0;
  737. else
  738. i->second.daysWithoutCastle++;
  739. }
  740. if(resetBuilded) //reset amount of structures set in this turn in towns
  741. {
  742. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  743. t->builded = 0;
  744. }
  745. }
  746. }
  747. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  748. {
  749. CGObjectInstance *obj = gs->map->objects[id];
  750. if(!obj)
  751. {
  752. tlog1 << "Wrong object ID - property cannot be set!\n";
  753. return;
  754. }
  755. if(what == ObjProperty::OWNER)
  756. {
  757. if(obj->ID == TOWNI_TYPE)
  758. {
  759. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  760. if(t->tempOwner < PLAYER_LIMIT)
  761. gs->getPlayer(t->tempOwner)->towns -= t;
  762. if(val < PLAYER_LIMIT)
  763. gs->getPlayer(val)->towns.push_back(t);
  764. }
  765. }
  766. obj->setProperty(what,val);
  767. }
  768. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  769. {
  770. name.toString(gs->map->objects[id]->hoverName);
  771. }
  772. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  773. {
  774. CGHeroInstance* h = gs->getHero(heroid);
  775. h->level = level;
  776. //speciality
  777. h->UpdateSpeciality();
  778. }
  779. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  780. {
  781. gs->curB = info;
  782. gs->curB->localInit();
  783. }
  784. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  785. {
  786. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  787. gs->curB->round = round;
  788. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  789. {
  790. s->state -= DEFENDING;
  791. s->state -= WAITING;
  792. s->state -= MOVED;
  793. s->state -= HAD_MORALE;
  794. s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
  795. //regeneration
  796. if( s->hasBonusOfType(Bonus::HP_REGENERATION) && s->alive() )
  797. s->firstHPleft = std::min<ui32>( s->MaxHealth(), s->valOfBonuses(Bonus::HP_REGENERATION) );
  798. if( s->hasBonusOfType(Bonus::FULL_HP_REGENERATION) && s->alive() )
  799. s->firstHPleft = s->MaxHealth();
  800. //remove effects and restore only those with remaining turns in duration
  801. BonusList tmpEffects = s->bonuses;
  802. s->bonuses.removeSpells(Bonus::SPELL_EFFECT);
  803. BOOST_FOREACH(Bonus *it, tmpEffects)
  804. {
  805. it->turnsRemain--;
  806. if(it->turnsRemain > 0)
  807. s->addNewBonus(it);
  808. }
  809. //the same as above for features
  810. BonusList tmpFeatures = s->bonuses;
  811. s->bonuses.clear();
  812. BOOST_FOREACH(Bonus *b, tmpFeatures)
  813. {
  814. if((b->duration & Bonus::N_TURNS) != 0)
  815. {
  816. b->turnsRemain--;
  817. if(b->turnsRemain > 0)
  818. s->addNewBonus(b);
  819. }
  820. else
  821. {
  822. s->addNewBonus(b);
  823. }
  824. }
  825. }
  826. }
  827. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  828. {
  829. gs->curB->activeStack = stack;
  830. CStack *st = gs->curB->getStack(stack);
  831. //remove bonuses that last until when stack gets new turn
  832. st->bonuses.remove_if(Bonus::UntilGetsTurn);
  833. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  834. st->state.insert(HAD_MORALE);
  835. }
  836. void BattleResult::applyGs( CGameState *gs )
  837. {
  838. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  839. delete gs->curB->stacks[i];
  840. //remove any "until next battle" bonuses
  841. CGHeroInstance *h;
  842. h = gs->curB->heroes[0];
  843. if(h)
  844. h->bonuses.remove_if(Bonus::OneBattle);
  845. h = gs->curB->heroes[1];
  846. if(h)
  847. h->bonuses.remove_if(Bonus::OneBattle);
  848. gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
  849. gs->curB.dellNull();
  850. }
  851. void BattleStackMoved::applyGs( CGameState *gs )
  852. {
  853. gs->curB->getStack(stack)->position = tile;
  854. }
  855. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  856. {
  857. CStack * at = gs->curB->getStack(stackAttacked);
  858. at->count = newAmount;
  859. at->firstHPleft = newHP;
  860. if(killed())
  861. at->state -= ALIVE;
  862. //life drain handling
  863. for (int g=0; g<healedStacks.size(); ++g)
  864. {
  865. healedStacks[g].applyGs(gs);
  866. }
  867. }
  868. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  869. {
  870. CStack *attacker = gs->curB->getStack(stackAttacking);
  871. if(counter())
  872. attacker->counterAttacks--;
  873. if(shot())
  874. {
  875. //don't remove ammo if we have a working ammo cart
  876. bool hasAmmoCart = false;
  877. BOOST_FOREACH(const CStack * st, gs->curB->stacks)
  878. {
  879. if(st->owner == attacker->owner && st->getCreature()->idNumber == 148 && st->alive())
  880. {
  881. hasAmmoCart = true;
  882. break;
  883. }
  884. }
  885. if (!hasAmmoCart)
  886. {
  887. attacker->shots--;
  888. }
  889. }
  890. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  891. stackAttacked.applyGs(gs);
  892. attacker->bonuses.remove_if(Bonus::UntilAttack);
  893. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  894. {
  895. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  896. stack->bonuses.remove_if(Bonus::UntilBeingAttacked);
  897. }
  898. }
  899. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  900. {
  901. CStack *st = gs->curB->getStack(ba.stackNumber);
  902. if(ba.actionType != BattleAction::HERO_SPELL) //don't check for stack if it's custom action by hero
  903. assert(st);
  904. switch(ba.actionType)
  905. {
  906. case BattleAction::DEFEND:
  907. st->state.insert(DEFENDING);
  908. break;
  909. case BattleAction::WAIT:
  910. st->state.insert(WAITING);
  911. return;
  912. case BattleAction::NO_ACTION: case BattleAction::WALK: case BattleAction::WALK_AND_ATTACK:
  913. case BattleAction::SHOOT: case BattleAction::CATAPULT: case BattleAction::MONSTER_SPELL:
  914. case BattleAction::BAD_MORALE: case BattleAction::STACK_HEAL:
  915. st->state.insert(MOVED);
  916. break;
  917. }
  918. if(st)
  919. 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)
  920. }
  921. DLL_EXPORT void BattleSpellCast::applyGs( CGameState *gs )
  922. {
  923. assert(gs->curB);
  924. CGHeroInstance *h = (side) ? gs->curB->heroes[1] : gs->curB->heroes[0];
  925. if(h && castedByHero)
  926. {
  927. int spellCost = 0;
  928. if(gs->curB)
  929. {
  930. spellCost = gs->curB->getSpellCost(VLC->spellh->spells[id], h);
  931. }
  932. else
  933. {
  934. spellCost = VLC->spellh->spells[id]->costs[skill];
  935. }
  936. h->mana -= spellCost;
  937. if(h->mana < 0) h->mana = 0;
  938. }
  939. if(side >= 0 && side < 2 && castedByHero)
  940. {
  941. gs->curB->castSpells[side]++;
  942. }
  943. if(id == 35 || id == 78) //dispel and dispel helpful spells
  944. {
  945. bool onlyHelpful = id == 78;
  946. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  947. {
  948. CStack *s = gs->curB->getStack(*it);
  949. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  950. {
  951. BonusList remainingEff;
  952. //WTF?
  953. for (BonusList::iterator it = remainingEff.begin(); it != remainingEff.end(); it++)
  954. {
  955. if (onlyHelpful && VLC->spellh->spells[ (*it)->id ]->positiveness != 1)
  956. {
  957. remainingEff.push_back(*it);
  958. }
  959. }
  960. s->bonuses.removeSpells(Bonus::SPELL_EFFECT); //removing all effects
  961. s->bonuses = remainingEff; //assigning effects that should remain
  962. //removing all features from spells
  963. BonusList tmpFeatures = s->bonuses;
  964. s->bonuses.clear();
  965. BOOST_FOREACH(Bonus *b, tmpFeatures)
  966. {
  967. const CSpell *sp = b->sourceSpell();
  968. if(sp && sp->positiveness != 1) //if(b->source != HeroBonus::SPELL_EFFECT || b.positiveness != 1)
  969. s->addNewBonus(b);
  970. }
  971. }
  972. }
  973. }
  974. //elemental summoning
  975. if(id >= 66 && id <= 69)
  976. {
  977. int creID;
  978. switch(id)
  979. {
  980. case 66:
  981. creID = 114; //fire elemental
  982. break;
  983. case 67:
  984. creID = 113; //earth elemental
  985. break;
  986. case 68:
  987. creID = 115; //water elemental
  988. break;
  989. case 69:
  990. creID = 112; //air elemental
  991. break;
  992. }
  993. // const int3 & tile = gs->curB->tile;
  994. // TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  995. int pos; //position of stack on the battlefield - to be calculated
  996. bool ac[BFIELD_SIZE];
  997. std::set<THex> occupyable;
  998. bool twoHex = VLC->creh->creatures[creID]->isDoubleWide();
  999. bool flying = VLC->creh->creatures[creID]->isFlying();// vstd::contains(VLC->creh->creatures[creID]->bonuses, Bonus::FLYING);
  1000. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  1001. for(int g=0; g<BFIELD_SIZE; ++g)
  1002. {
  1003. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  1004. {
  1005. pos = g;
  1006. break;
  1007. }
  1008. }
  1009. CStackInstance *csi = new CStackInstance(creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id]->powers[skill]); //deleted by d-tor of summoned stack
  1010. csi->setArmyObj(h);
  1011. CStack * summonedStack = gs->curB->generateNewStack(*csi, gs->curB->stacks.size(), !side, 255, pos);
  1012. summonedStack->state.insert(SUMMONED);
  1013. //summonedStack->addNewBonus( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1014. gs->curB->stacks.push_back(summonedStack);
  1015. }
  1016. }
  1017. void actualizeEffect(CStack * s, const std::vector<Bonus> & ef)
  1018. {
  1019. //actualizing features vector
  1020. BOOST_FOREACH(const Bonus &fromEffect, ef)
  1021. {
  1022. BOOST_FOREACH(Bonus *stackBonus, s->bonuses) //TODO: optimize
  1023. {
  1024. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == fromEffect.type && stackBonus->subtype == fromEffect.subtype)
  1025. {
  1026. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, fromEffect.turnsRemain);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  1032. {
  1033. BOOST_FOREACH(ui32 id, stacks)
  1034. {
  1035. CStack *s = gs->curB->getStack(id);
  1036. if(s)
  1037. {
  1038. int id = effect.begin()->id; //effects' source ID
  1039. if(id == 47 || !s->hasBonus(Selector::source(Bonus::SPELL_EFFECT, id)))//disrupting ray or not on the list - just add
  1040. {
  1041. BOOST_FOREACH(Bonus &fromEffect, effect)
  1042. {
  1043. s->addNewBonus( new Bonus(fromEffect));
  1044. }
  1045. }
  1046. else //just actualize
  1047. {
  1048. actualizeEffect(s, effect);
  1049. }
  1050. }
  1051. else
  1052. tlog1 << "Cannot find stack " << id << std::endl;
  1053. }
  1054. }
  1055. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  1056. {
  1057. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  1058. stackAttacked.applyGs(gs);
  1059. }
  1060. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  1061. {
  1062. for(int g=0; g<healedStacks.size(); ++g)
  1063. {
  1064. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  1065. //checking if we resurrect a stack that is under a living stack
  1066. std::vector<THex> access = gs->curB->getAccessibility(changedStack, true);
  1067. bool acc[BFIELD_SIZE];
  1068. for(int h=0; h<BFIELD_SIZE; ++h)
  1069. acc[h] = false;
  1070. for(int h=0; h<access.size(); ++h)
  1071. acc[access[h]] = true;
  1072. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  1073. changedStack->doubleWide(), changedStack->attackerOwned,
  1074. changedStack->hasBonusOfType(Bonus::FLYING), true))
  1075. return; //position is already occupied
  1076. //applying changes
  1077. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1078. if(resurrected)
  1079. {
  1080. changedStack->state.insert(ALIVE);
  1081. if(healedStacks[g].lowLevelResurrection)
  1082. changedStack->state.insert(SUMMONED);
  1083. //changedStack->addNewBonus( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1084. }
  1085. //int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1086. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1087. changedStack->count += res;
  1088. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1089. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1090. {
  1091. changedStack->firstHPleft -= changedStack->MaxHealth();
  1092. if(changedStack->baseAmount > changedStack->count)
  1093. {
  1094. changedStack->count += 1;
  1095. }
  1096. }
  1097. amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1098. //removal of negative effects
  1099. if(resurrected)
  1100. {
  1101. for (BonusList::iterator it = changedStack->bonuses.begin(); it != changedStack->bonuses.end(); it++)
  1102. {
  1103. if(VLC->spellh->spells[(*it)->id]->positiveness < 0)
  1104. {
  1105. changedStack->bonuses.erase(it);
  1106. }
  1107. }
  1108. //removing all features from negative spells
  1109. BonusList tmpFeatures = changedStack->bonuses;
  1110. changedStack->bonuses.clear();
  1111. BOOST_FOREACH(Bonus *b, tmpFeatures)
  1112. {
  1113. const CSpell *s = b->sourceSpell();
  1114. if(s && s->positiveness >= 0)
  1115. {
  1116. changedStack->addNewBonus(b);
  1117. }
  1118. }
  1119. }
  1120. }
  1121. }
  1122. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  1123. {
  1124. if(gs->curB) //if there is a battle
  1125. {
  1126. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1127. {
  1128. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1129. {
  1130. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1131. {
  1132. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. }
  1138. }
  1139. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  1140. {
  1141. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1142. {
  1143. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1144. {
  1145. gs->curB->si.wallState[it->first.first] =
  1146. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1147. }
  1148. }
  1149. }
  1150. DLL_EXPORT void BattleStacksRemoved::applyGs( CGameState *gs )
  1151. {
  1152. if(!gs->curB)
  1153. return;
  1154. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1155. {
  1156. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1157. {
  1158. if(gs->curB->stacks[b]->ID == *it) //if found
  1159. {
  1160. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1161. break;
  1162. }
  1163. }
  1164. }
  1165. }
  1166. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  1167. {
  1168. gs->currentPlayer = player;
  1169. }
  1170. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  1171. {
  1172. gs->getPlayer(player)->currentSelection = id;
  1173. }
  1174. DLL_EXPORT Component::Component(const CStackBasicDescriptor &stack)
  1175. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1176. {
  1177. }