NetPacksLib.cpp 35 KB

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