NetPacksLib.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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. CBonusSystemNode::treeHasChanged();
  586. }
  587. DLL_LINKAGE void PutArtifact::applyGs( CGameState *gs )
  588. {
  589. assert(art->canBePutAt(al));
  590. al.hero->putArtifact(al.slot, art);
  591. }
  592. DLL_LINKAGE void EraseArtifact::applyGs( CGameState *gs )
  593. {
  594. CArtifactInstance *a = al.getArt();
  595. assert(a);
  596. a->removeFrom(al.hero, al.slot);
  597. }
  598. DLL_LINKAGE void MoveArtifact::applyGs( CGameState *gs )
  599. {
  600. CArtifactInstance *a = src.getArt();
  601. if(dst.slot < GameConstants::BACKPACK_START)
  602. assert(!dst.getArt());
  603. a->move(src, dst);
  604. //TODO what'll happen if Titan's thunder is equiped by pickin git up or the start of game?
  605. if (a->artType->id == 135 && dst.hero && dst.slot == ArtifactPosition::RIGHT_HAND && !dst.hero->hasSpellbook()) //Titan's Thunder creates new spellbook on equip
  606. gs->giveHeroArtifact(dst.hero, 0);
  607. }
  608. DLL_LINKAGE void AssembledArtifact::applyGs( CGameState *gs )
  609. {
  610. CGHeroInstance *h = al.hero;
  611. const CArtifactInstance *transformedArt = al.getArt();
  612. assert(transformedArt);
  613. assert(vstd::contains(transformedArt->assemblyPossibilities(al.hero), builtArt));
  614. CCombinedArtifactInstance *combinedArt = new CCombinedArtifactInstance(builtArt);
  615. gs->map->addNewArtifactInstance(combinedArt);
  616. //retrieve all constituents
  617. BOOST_FOREACH(si32 constituentID, *builtArt->constituents)
  618. {
  619. int pos = h->getArtPos(constituentID);
  620. assert(pos >= 0);
  621. CArtifactInstance *constituentInstance = h->getArt(pos);
  622. //move constituent from hero to be part of new, combined artifact
  623. constituentInstance->removeFrom(h, pos);
  624. combinedArt->addAsConstituent(constituentInstance, pos);
  625. if(!vstd::contains(combinedArt->artType->possibleSlots, al.slot) && vstd::contains(combinedArt->artType->possibleSlots, pos))
  626. al.slot = pos;
  627. }
  628. //put new combined artifacts
  629. combinedArt->putAt(h, al.slot);
  630. }
  631. DLL_LINKAGE void DisassembledArtifact::applyGs( CGameState *gs )
  632. {
  633. CGHeroInstance *h = al.hero;
  634. CCombinedArtifactInstance *disassembled = dynamic_cast<CCombinedArtifactInstance*>(al.getArt());
  635. assert(disassembled);
  636. std::vector<CCombinedArtifactInstance::ConstituentInfo> constituents = disassembled->constituentsInfo;
  637. disassembled->removeFrom(h, al.slot);
  638. BOOST_FOREACH(CCombinedArtifactInstance::ConstituentInfo &ci, constituents)
  639. {
  640. disassembled->detachFrom(ci.art);
  641. 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
  642. }
  643. gs->map->eraseArtifactInstance(disassembled);
  644. }
  645. DLL_LINKAGE void HeroVisit::applyGs( CGameState *gs )
  646. {
  647. if(starting)
  648. gs->ongoingVisits[hero] = obj;
  649. else
  650. gs->ongoingVisits.erase(hero);
  651. }
  652. DLL_LINKAGE void SetAvailableArtifacts::applyGs( CGameState *gs )
  653. {
  654. if(id >= 0)
  655. {
  656. if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id].get()))
  657. {
  658. bm->artifacts = arts;
  659. }
  660. else
  661. {
  662. tlog1 << "Wrong black market id!" << std::endl;
  663. }
  664. }
  665. else
  666. {
  667. CGTownInstance::merchantArtifacts = arts;
  668. }
  669. }
  670. DLL_LINKAGE void NewTurn::applyGs( CGameState *gs )
  671. {
  672. gs->day = day;
  673. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  674. {
  675. CGHeroInstance *hero = gs->getHero(h.id);
  676. hero->movement = h.move;
  677. hero->mana = h.mana;
  678. }
  679. for(std::map<ui8, TResources>::iterator i = res.begin(); i != res.end(); i++)
  680. {
  681. assert(i->first < GameConstants::PLAYER_LIMIT);
  682. gs->getPlayer(i->first)->resources = i->second;
  683. }
  684. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  685. h.applyGs(gs);
  686. gs->globalEffects.popBonuses(Bonus::OneDay); //works for children -> all game objs
  687. if(gs->getDate(1)) //new week
  688. gs->globalEffects.popBonuses(Bonus::OneWeek); //works for children -> all game objs
  689. //TODO not really a single root hierarchy, what about bonuses placed elsewhere? [not an issue with H3 mechanics but in the future...]
  690. //count days without town
  691. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  692. {
  693. if(i->second.towns.size() || gs->day == 1)
  694. i->second.daysWithoutCastle = 0;
  695. else
  696. i->second.daysWithoutCastle++;
  697. }
  698. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  699. t->builded = 0;
  700. }
  701. DLL_LINKAGE void SetObjectProperty::applyGs( CGameState *gs )
  702. {
  703. CGObjectInstance *obj = gs->map->objects[id];
  704. if(!obj)
  705. {
  706. tlog1 << "Wrong object ID - property cannot be set!\n";
  707. return;
  708. }
  709. CArmedInstance *cai = dynamic_cast<CArmedInstance *>(obj);
  710. if(what == ObjProperty::OWNER && cai)
  711. {
  712. if(obj->ID == GameConstants::TOWNI_TYPE)
  713. {
  714. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  715. if(t->tempOwner < GameConstants::PLAYER_LIMIT)
  716. gs->getPlayer(t->tempOwner)->towns -= t;
  717. if(val < GameConstants::PLAYER_LIMIT)
  718. gs->getPlayer(val)->towns.push_back(t);
  719. }
  720. CBonusSystemNode *nodeToMove = cai->whatShouldBeAttached();
  721. nodeToMove->detachFrom(cai->whereShouldBeAttached(gs));
  722. obj->setProperty(what,val);
  723. nodeToMove->attachTo(cai->whereShouldBeAttached(gs));
  724. }
  725. else //not an armed instance
  726. {
  727. obj->setProperty(what,val);
  728. }
  729. }
  730. DLL_LINKAGE void SetHoverName::applyGs( CGameState *gs )
  731. {
  732. name.toString(gs->map->objects[id]->hoverName);
  733. }
  734. DLL_LINKAGE void HeroLevelUp::applyGs( CGameState *gs )
  735. {
  736. CGHeroInstance* h = gs->getHero(heroid);
  737. h->level = level;
  738. //speciality
  739. h->UpdateSpeciality();
  740. }
  741. DLL_LINKAGE void BattleStart::applyGs( CGameState *gs )
  742. {
  743. gs->curB = info;
  744. gs->curB->localInit();
  745. }
  746. DLL_LINKAGE void BattleNextRound::applyGs( CGameState *gs )
  747. {
  748. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  749. for (int i = 0; i < 2; ++i)
  750. {
  751. vstd::amax(--gs->curB->enchanterCounter[i], 0);
  752. }
  753. gs->curB->round = round;
  754. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  755. {
  756. s->state -= EBattleStackState::DEFENDING;
  757. s->state -= EBattleStackState::WAITING;
  758. s->state -= EBattleStackState::MOVED;
  759. s->state -= EBattleStackState::HAD_MORALE;
  760. s->state -= EBattleStackState::FEAR;
  761. s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
  762. // new turn effects
  763. s->battleTurnPassed();
  764. }
  765. }
  766. DLL_LINKAGE void BattleSetActiveStack::applyGs( CGameState *gs )
  767. {
  768. gs->curB->activeStack = stack;
  769. CStack *st = gs->curB->getStack(stack);
  770. //remove bonuses that last until when stack gets new turn
  771. st->getBonusList().remove_if(Bonus::UntilGetsTurn);
  772. if(vstd::contains(st->state,EBattleStackState::MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  773. st->state.insert(EBattleStackState::HAD_MORALE);
  774. }
  775. DLL_LINKAGE void BattleTriggerEffect::applyGs( CGameState *gs )
  776. {
  777. CStack *st = gs->curB->getStack(stackID);
  778. switch (effect)
  779. {
  780. case Bonus::HP_REGENERATION:
  781. st->firstHPleft += val;
  782. vstd::amin (st->firstHPleft, (ui32)st->MaxHealth());
  783. break;
  784. case Bonus::MANA_DRAIN:
  785. {
  786. CGHeroInstance * h = gs->getHero(additionalInfo);
  787. h->mana -= val;
  788. vstd::amax(h->mana, 0);
  789. break;
  790. }
  791. case Bonus::POISON:
  792. {
  793. Bonus * b = st->getBonus(Selector::source(Bonus::SPELL_EFFECT, 71) && Selector::type(Bonus::STACK_HEALTH));
  794. if (b)
  795. b->val = val;
  796. break;
  797. }
  798. case Bonus::ENCHANTER:
  799. break;
  800. case Bonus::FEAR:
  801. st->state.insert(EBattleStackState::FEAR);
  802. break;
  803. default:
  804. tlog2 << "Unrecognized trigger effect type "<< type <<"\n";
  805. }
  806. }
  807. void BattleResult::applyGs( CGameState *gs )
  808. {
  809. //stack with SUMMONED flag but coming from garrison -> most likely resurrected, needs to be removed
  810. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  811. {
  812. if(s->base && s->base->armyObj && vstd::contains(s->state, EBattleStackState::SUMMONED))
  813. {
  814. assert(&s->base->armyObj->getStack(s->slot) == s->base);
  815. const_cast<CArmedInstance*>(s->base->armyObj)->eraseStack(s->slot);
  816. }
  817. }
  818. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  819. delete gs->curB->stacks[i];
  820. //remove any "until next battle" bonuses
  821. CGHeroInstance *h;
  822. h = gs->curB->heroes[0];
  823. if(h)
  824. h->getBonusList().remove_if(Bonus::OneBattle);
  825. h = gs->curB->heroes[1];
  826. if(h)
  827. h->getBonusList().remove_if(Bonus::OneBattle);
  828. if (GameConstants::STACK_EXP)
  829. {
  830. if (exp[0]) //checking local array is easier than dereferencing this crap twice
  831. gs->curB->belligerents[0]->giveStackExp(exp[0]);
  832. if (exp[1])
  833. gs->curB->belligerents[1]->giveStackExp(exp[1]);
  834. CBonusSystemNode::treeHasChanged();
  835. }
  836. gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
  837. gs->curB.dellNull();
  838. }
  839. void BattleStackMoved::applyGs( CGameState *gs )
  840. {
  841. gs->curB->getStack(stack)->position = tilesToMove.back();
  842. }
  843. DLL_LINKAGE void BattleStackAttacked::applyGs( CGameState *gs )
  844. {
  845. CStack * at = gs->curB->getStack(stackAttacked);
  846. at->count = newAmount;
  847. at->firstHPleft = newHP;
  848. if(killed())
  849. {
  850. at->state -= EBattleStackState::ALIVE;
  851. }
  852. //life drain handling
  853. for (int g=0; g<healedStacks.size(); ++g)
  854. {
  855. healedStacks[g].applyGs(gs);
  856. }
  857. if (willRebirth())
  858. {
  859. at->casts--;
  860. at->state.insert(EBattleStackState::ALIVE); //hmm?
  861. }
  862. if (cloneKilled())
  863. {
  864. BattleStacksRemoved bsr; //remove body
  865. bsr.stackIDs.insert(at->ID);
  866. bsr.applyGs(gs);
  867. }
  868. }
  869. DLL_LINKAGE void BattleAttack::applyGs( CGameState *gs )
  870. {
  871. CStack *attacker = gs->curB->getStack(stackAttacking);
  872. if(counter())
  873. attacker->counterAttacks--;
  874. if(shot())
  875. {
  876. //don't remove ammo if we have a working ammo cart
  877. bool hasAmmoCart = false;
  878. BOOST_FOREACH(const CStack * st, gs->curB->stacks)
  879. {
  880. if(st->owner == attacker->owner && st->getCreature()->idNumber == 148 && st->alive())
  881. {
  882. hasAmmoCart = true;
  883. break;
  884. }
  885. }
  886. if (!hasAmmoCart)
  887. {
  888. attacker->shots--;
  889. }
  890. }
  891. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  892. stackAttacked.applyGs(gs);
  893. attacker->getBonusList().remove_if(Bonus::UntilAttack);
  894. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  895. {
  896. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  897. if (stack) //cloned stack is already gone
  898. stack->getBonusList().remove_if(Bonus::UntilBeingAttacked);
  899. }
  900. }
  901. DLL_LINKAGE void StartAction::applyGs( CGameState *gs )
  902. {
  903. CStack *st = gs->curB->getStack(ba.stackNumber);
  904. if(ba.actionType == BattleAction::END_TACTIC_PHASE)
  905. {
  906. gs->curB->tacticDistance = 0;
  907. return;
  908. }
  909. if(gs->curB->tacticDistance)
  910. {
  911. // moves in tactics phase do not affect creature status
  912. // (tactics stack queue is managed by client)
  913. return;
  914. }
  915. if(ba.actionType != BattleAction::HERO_SPELL) //don't check for stack if it's custom action by hero
  916. {
  917. assert(st);
  918. }
  919. else
  920. {
  921. gs->curB->usedSpellsHistory[ba.side].push_back(VLC->spellh->spells[ba.additionalInfo]);
  922. }
  923. switch(ba.actionType)
  924. {
  925. case BattleAction::DEFEND:
  926. st->state.insert(EBattleStackState::DEFENDING);
  927. break;
  928. case BattleAction::WAIT:
  929. st->state.insert(EBattleStackState::WAITING);
  930. return;
  931. case BattleAction::HERO_SPELL: //no change in current stack state
  932. return;
  933. default: //any active stack action - attack, catapult, heal, spell...
  934. st->state.insert(EBattleStackState::MOVED);
  935. break;
  936. }
  937. if(st)
  938. 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)
  939. }
  940. DLL_LINKAGE void BattleSpellCast::applyGs( CGameState *gs )
  941. {
  942. assert(gs->curB);
  943. if (castedByHero)
  944. {
  945. CGHeroInstance * h = gs->curB->heroes[side];
  946. CGHeroInstance * enemy = gs->curB->heroes[1-side];
  947. h->mana -= spellCost;
  948. vstd::amax(h->mana, 0);
  949. if (enemy && manaGained)
  950. enemy->mana += manaGained;
  951. if (side >= 0 && side < 2)
  952. {
  953. gs->curB->castSpells[side]++;
  954. }
  955. }
  956. if(id == 35 || id == 78) //dispel and dispel helpful spells
  957. {
  958. bool onlyHelpful = id == 78;
  959. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  960. {
  961. CStack *s = gs->curB->getStack(*it);
  962. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  963. {
  964. if(onlyHelpful)
  965. s->popBonuses(Selector::positiveSpellEffects);
  966. else
  967. s->popBonuses(Selector::sourceType(Bonus::SPELL_EFFECT));
  968. }
  969. }
  970. }
  971. }
  972. void actualizeEffect(CStack * s, const std::vector<Bonus> & ef)
  973. {
  974. //actualizing features vector
  975. BOOST_FOREACH(const Bonus &fromEffect, ef)
  976. {
  977. BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
  978. {
  979. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == fromEffect.type && stackBonus->subtype == fromEffect.subtype)
  980. {
  981. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, fromEffect.turnsRemain);
  982. }
  983. }
  984. }
  985. }
  986. void actualizeEffect(CStack * s, const Bonus & ef)
  987. {
  988. BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
  989. {
  990. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == ef.type && stackBonus->subtype == ef.subtype)
  991. {
  992. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, ef.turnsRemain);
  993. }
  994. }
  995. }
  996. DLL_LINKAGE void SetStackEffect::applyGs( CGameState *gs )
  997. {
  998. int spellid = effect.begin()->sid; //effects' source ID
  999. BOOST_FOREACH(ui32 id, stacks)
  1000. {
  1001. CStack *s = gs->curB->getStack(id);
  1002. if(s)
  1003. {
  1004. 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
  1005. {
  1006. BOOST_FOREACH(Bonus &fromEffect, effect)
  1007. {
  1008. s->addNewBonus( new Bonus(fromEffect));
  1009. }
  1010. }
  1011. else //just actualize
  1012. {
  1013. actualizeEffect(s, effect);
  1014. }
  1015. }
  1016. else
  1017. tlog1 << "Cannot find stack " << id << std::endl;
  1018. }
  1019. typedef std::pair<ui32, Bonus> p;
  1020. BOOST_FOREACH(p para, uniqueBonuses)
  1021. {
  1022. CStack *s = gs->curB->getStack(para.first);
  1023. if (s)
  1024. {
  1025. if (!s->hasBonus(Selector::source(Bonus::SPELL_EFFECT, spellid) && Selector::typeSubtype(para.second.type, para.second.subtype)))
  1026. s->addNewBonus(new Bonus(para.second));
  1027. else
  1028. actualizeEffect(s, effect);
  1029. }
  1030. else
  1031. tlog1 << "Cannot find stack " << para.first << std::endl;
  1032. }
  1033. }
  1034. DLL_LINKAGE void StacksInjured::applyGs( CGameState *gs )
  1035. {
  1036. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  1037. stackAttacked.applyGs(gs);
  1038. }
  1039. DLL_LINKAGE void StacksHealedOrResurrected::applyGs( CGameState *gs )
  1040. {
  1041. for(int g=0; g<healedStacks.size(); ++g)
  1042. {
  1043. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  1044. //checking if we resurrect a stack that is under a living stack
  1045. std::vector<BattleHex> access = gs->curB->getAccessibility(changedStack, true);
  1046. bool acc[GameConstants::BFIELD_SIZE];
  1047. for(int h=0; h<GameConstants::BFIELD_SIZE; ++h)
  1048. acc[h] = false;
  1049. for(int h=0; h<access.size(); ++h)
  1050. acc[access[h]] = true;
  1051. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  1052. changedStack->doubleWide(), changedStack->attackerOwned,
  1053. changedStack->hasBonusOfType(Bonus::FLYING), true))
  1054. return; //position is already occupied
  1055. //applying changes
  1056. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1057. if(resurrected)
  1058. {
  1059. changedStack->state.insert(EBattleStackState::ALIVE);
  1060. if(healedStacks[g].lowLevelResurrection)
  1061. changedStack->state.insert(EBattleStackState::SUMMONED); //TODO: different counter for rised units
  1062. }
  1063. //int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1064. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1065. changedStack->count += res;
  1066. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1067. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1068. {
  1069. changedStack->firstHPleft -= changedStack->MaxHealth();
  1070. if(changedStack->baseAmount > changedStack->count)
  1071. {
  1072. changedStack->count += 1;
  1073. }
  1074. }
  1075. vstd::amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1076. //removal of negative effects
  1077. if(resurrected)
  1078. {
  1079. // for (BonusList::iterator it = changedStack->bonuses.begin(); it != changedStack->bonuses.end(); it++)
  1080. // {
  1081. // if(VLC->spellh->spells[(*it)->sid]->positiveness < 0)
  1082. // {
  1083. // changedStack->bonuses.erase(it);
  1084. // }
  1085. // }
  1086. //removing all features from negative spells
  1087. const BonusList tmpFeatures = changedStack->getBonusList();
  1088. //changedStack->bonuses.clear();
  1089. BOOST_FOREACH(Bonus *b, tmpFeatures)
  1090. {
  1091. const CSpell *s = b->sourceSpell();
  1092. if(s && s->isNegative())
  1093. {
  1094. changedStack->removeBonus(b);
  1095. }
  1096. }
  1097. }
  1098. }
  1099. }
  1100. DLL_LINKAGE void ObstaclesRemoved::applyGs( CGameState *gs )
  1101. {
  1102. if(gs->curB) //if there is a battle
  1103. {
  1104. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1105. {
  1106. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1107. {
  1108. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1109. {
  1110. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1111. break;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. }
  1117. DLL_LINKAGE void CatapultAttack::applyGs( CGameState *gs )
  1118. {
  1119. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1120. {
  1121. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1122. {
  1123. gs->curB->si.wallState[it->first.first] =
  1124. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1125. }
  1126. }
  1127. }
  1128. DLL_LINKAGE void BattleStacksRemoved::applyGs( CGameState *gs )
  1129. {
  1130. if(!gs->curB)
  1131. return;
  1132. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1133. {
  1134. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1135. {
  1136. if(gs->curB->stacks[b]->ID == *it) //if found
  1137. {
  1138. CStack *toRemove = gs->curB->stacks[b];
  1139. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1140. toRemove->detachFromAll();
  1141. delete toRemove;
  1142. break;
  1143. }
  1144. }
  1145. }
  1146. }
  1147. DLL_LINKAGE void BattleStackAdded::applyGs(CGameState *gs)
  1148. {
  1149. if (!BattleHex(pos).isValid())
  1150. {
  1151. tlog2 << "No place found for new stack!\n";
  1152. return;
  1153. }
  1154. CStackBasicDescriptor csbd(creID, amount);
  1155. CStack * addedStack = gs->curB->generateNewStack(csbd, attacker, 255, pos); //TODO: netpacks?
  1156. if (summoned)
  1157. addedStack->state.insert(EBattleStackState::SUMMONED);
  1158. gs->curB->localInitStack(addedStack);
  1159. gs->curB->stacks.push_back(addedStack); //the stack is not "SUMMONED", it is permanent
  1160. }
  1161. DLL_LINKAGE void BattleSetStackProperty::applyGs(CGameState *gs)
  1162. {
  1163. CStack * stack = gs->curB->getStack(stackID);
  1164. switch (which)
  1165. {
  1166. case CASTS:
  1167. {
  1168. if (absolute)
  1169. stack->casts = val;
  1170. else
  1171. stack->casts += val;
  1172. vstd::amax(stack->casts, 0);
  1173. break;
  1174. }
  1175. case ENCHANTER_COUNTER:
  1176. {
  1177. int side = gs->curB->whatSide(stack->owner);
  1178. if (absolute)
  1179. gs->curB->enchanterCounter[side] = val;
  1180. else
  1181. gs->curB->enchanterCounter[side] += val;
  1182. vstd::amax(gs->curB->enchanterCounter[side], 0);
  1183. break;
  1184. }
  1185. case UNBIND:
  1186. {
  1187. stack->popBonuses(Selector::type(Bonus::BIND_EFFECT));
  1188. break;
  1189. }
  1190. case CLONED:
  1191. {
  1192. stack->state.insert(EBattleStackState::CLONED);
  1193. break;
  1194. }
  1195. }
  1196. }
  1197. DLL_LINKAGE void YourTurn::applyGs( CGameState *gs )
  1198. {
  1199. gs->currentPlayer = player;
  1200. }
  1201. DLL_LINKAGE void SetSelection::applyGs( CGameState *gs )
  1202. {
  1203. gs->getPlayer(player)->currentSelection = id;
  1204. }
  1205. DLL_LINKAGE Component::Component(const CStackBasicDescriptor &stack)
  1206. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1207. {
  1208. }