2
0

NetPacksLib.cpp 39 KB

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