NetPacksLib.cpp 34 KB

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