NetPacksLib.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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(ba.actionType != BattleAction::HERO_SPELL) //don't check for stack if it's custom action by hero
  910. {
  911. assert(st);
  912. }
  913. else
  914. {
  915. gs->curB->usedSpellsHistory[ba.side].push_back(VLC->spellh->spells[ba.additionalInfo]);
  916. }
  917. switch(ba.actionType)
  918. {
  919. case BattleAction::DEFEND:
  920. st->state.insert(EBattleStackState::DEFENDING);
  921. break;
  922. case BattleAction::WAIT:
  923. st->state.insert(EBattleStackState::WAITING);
  924. return;
  925. case BattleAction::HERO_SPELL: //no change in current stack state
  926. return;
  927. default: //any active stack action - attack, catapult, heal, spell...
  928. st->state.insert(EBattleStackState::MOVED);
  929. break;
  930. }
  931. if(st)
  932. 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)
  933. }
  934. DLL_LINKAGE void BattleSpellCast::applyGs( CGameState *gs )
  935. {
  936. assert(gs->curB);
  937. if (castedByHero)
  938. {
  939. CGHeroInstance * h = gs->curB->heroes[side];
  940. CGHeroInstance * enemy = gs->curB->heroes[1-side];
  941. h->mana -= spellCost;
  942. vstd::amax(h->mana, 0);
  943. if (enemy && manaGained)
  944. enemy->mana += manaGained;
  945. if (side >= 0 && side < 2)
  946. {
  947. gs->curB->castSpells[side]++;
  948. }
  949. }
  950. if(id == 35 || id == 78) //dispel and dispel helpful spells
  951. {
  952. bool onlyHelpful = id == 78;
  953. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  954. {
  955. CStack *s = gs->curB->getStack(*it);
  956. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  957. {
  958. if(onlyHelpful)
  959. s->popBonuses(Selector::positiveSpellEffects);
  960. else
  961. s->popBonuses(Selector::sourceType(Bonus::SPELL_EFFECT));
  962. }
  963. }
  964. }
  965. }
  966. void actualizeEffect(CStack * s, const std::vector<Bonus> & ef)
  967. {
  968. //actualizing features vector
  969. BOOST_FOREACH(const Bonus &fromEffect, ef)
  970. {
  971. BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
  972. {
  973. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == fromEffect.type && stackBonus->subtype == fromEffect.subtype)
  974. {
  975. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, fromEffect.turnsRemain);
  976. }
  977. }
  978. }
  979. }
  980. void actualizeEffect(CStack * s, const Bonus & ef)
  981. {
  982. BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
  983. {
  984. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == ef.type && stackBonus->subtype == ef.subtype)
  985. {
  986. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, ef.turnsRemain);
  987. }
  988. }
  989. }
  990. DLL_LINKAGE void SetStackEffect::applyGs( CGameState *gs )
  991. {
  992. int spellid = effect.begin()->sid; //effects' source ID
  993. BOOST_FOREACH(ui32 id, stacks)
  994. {
  995. CStack *s = gs->curB->getStack(id);
  996. if(s)
  997. {
  998. 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
  999. {
  1000. BOOST_FOREACH(Bonus &fromEffect, effect)
  1001. {
  1002. s->addNewBonus( new Bonus(fromEffect));
  1003. }
  1004. }
  1005. else //just actualize
  1006. {
  1007. actualizeEffect(s, effect);
  1008. }
  1009. }
  1010. else
  1011. tlog1 << "Cannot find stack " << id << std::endl;
  1012. }
  1013. typedef std::pair<ui32, Bonus> p;
  1014. BOOST_FOREACH(p para, uniqueBonuses)
  1015. {
  1016. CStack *s = gs->curB->getStack(para.first);
  1017. if (s)
  1018. {
  1019. if (!s->hasBonus(Selector::source(Bonus::SPELL_EFFECT, spellid) && Selector::typeSubtype(para.second.type, para.second.subtype)))
  1020. s->addNewBonus(new Bonus(para.second));
  1021. else
  1022. actualizeEffect(s, effect);
  1023. }
  1024. else
  1025. tlog1 << "Cannot find stack " << para.first << std::endl;
  1026. }
  1027. }
  1028. DLL_LINKAGE void StacksInjured::applyGs( CGameState *gs )
  1029. {
  1030. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  1031. stackAttacked.applyGs(gs);
  1032. }
  1033. DLL_LINKAGE void StacksHealedOrResurrected::applyGs( CGameState *gs )
  1034. {
  1035. for(int g=0; g<healedStacks.size(); ++g)
  1036. {
  1037. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  1038. //checking if we resurrect a stack that is under a living stack
  1039. std::vector<BattleHex> access = gs->curB->getAccessibility(changedStack, true);
  1040. bool acc[GameConstants::BFIELD_SIZE];
  1041. for(int h=0; h<GameConstants::BFIELD_SIZE; ++h)
  1042. acc[h] = false;
  1043. for(int h=0; h<access.size(); ++h)
  1044. acc[access[h]] = true;
  1045. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  1046. changedStack->doubleWide(), changedStack->attackerOwned,
  1047. changedStack->hasBonusOfType(Bonus::FLYING), true))
  1048. return; //position is already occupied
  1049. //applying changes
  1050. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1051. if(resurrected)
  1052. {
  1053. changedStack->state.insert(EBattleStackState::ALIVE);
  1054. if(healedStacks[g].lowLevelResurrection)
  1055. changedStack->state.insert(EBattleStackState::SUMMONED); //TODO: different counter for rised units
  1056. }
  1057. //int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1058. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1059. changedStack->count += res;
  1060. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1061. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1062. {
  1063. changedStack->firstHPleft -= changedStack->MaxHealth();
  1064. if(changedStack->baseAmount > changedStack->count)
  1065. {
  1066. changedStack->count += 1;
  1067. }
  1068. }
  1069. vstd::amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1070. //removal of negative effects
  1071. if(resurrected)
  1072. {
  1073. // for (BonusList::iterator it = changedStack->bonuses.begin(); it != changedStack->bonuses.end(); it++)
  1074. // {
  1075. // if(VLC->spellh->spells[(*it)->sid]->positiveness < 0)
  1076. // {
  1077. // changedStack->bonuses.erase(it);
  1078. // }
  1079. // }
  1080. //removing all features from negative spells
  1081. const BonusList tmpFeatures = changedStack->getBonusList();
  1082. //changedStack->bonuses.clear();
  1083. BOOST_FOREACH(Bonus *b, tmpFeatures)
  1084. {
  1085. const CSpell *s = b->sourceSpell();
  1086. if(s && s->isNegative())
  1087. {
  1088. changedStack->removeBonus(b);
  1089. }
  1090. }
  1091. }
  1092. }
  1093. }
  1094. DLL_LINKAGE void ObstaclesRemoved::applyGs( CGameState *gs )
  1095. {
  1096. if(gs->curB) //if there is a battle
  1097. {
  1098. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1099. {
  1100. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1101. {
  1102. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1103. {
  1104. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1105. break;
  1106. }
  1107. }
  1108. }
  1109. }
  1110. }
  1111. DLL_LINKAGE void CatapultAttack::applyGs( CGameState *gs )
  1112. {
  1113. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1114. {
  1115. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1116. {
  1117. gs->curB->si.wallState[it->first.first] =
  1118. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1119. }
  1120. }
  1121. }
  1122. DLL_LINKAGE void BattleStacksRemoved::applyGs( CGameState *gs )
  1123. {
  1124. if(!gs->curB)
  1125. return;
  1126. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1127. {
  1128. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1129. {
  1130. if(gs->curB->stacks[b]->ID == *it) //if found
  1131. {
  1132. CStack *toRemove = gs->curB->stacks[b];
  1133. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1134. toRemove->detachFromAll();
  1135. delete toRemove;
  1136. break;
  1137. }
  1138. }
  1139. }
  1140. }
  1141. DLL_LINKAGE void BattleStackAdded::applyGs(CGameState *gs)
  1142. {
  1143. if (!BattleHex(pos).isValid())
  1144. {
  1145. tlog2 << "No place found for new stack!\n";
  1146. return;
  1147. }
  1148. CStackBasicDescriptor csbd(creID, amount);
  1149. CStack * addedStack = gs->curB->generateNewStack(csbd, attacker, 255, pos); //TODO: netpacks?
  1150. if (summoned)
  1151. addedStack->state.insert(EBattleStackState::SUMMONED);
  1152. gs->curB->localInitStack(addedStack);
  1153. gs->curB->stacks.push_back(addedStack); //the stack is not "SUMMONED", it is permanent
  1154. }
  1155. DLL_LINKAGE void BattleSetStackProperty::applyGs(CGameState *gs)
  1156. {
  1157. CStack * stack = gs->curB->getStack(stackID);
  1158. switch (which)
  1159. {
  1160. case CASTS:
  1161. {
  1162. if (absolute)
  1163. stack->casts = val;
  1164. else
  1165. stack->casts += val;
  1166. vstd::amax(stack->casts, 0);
  1167. break;
  1168. }
  1169. case ENCHANTER_COUNTER:
  1170. {
  1171. int side = gs->curB->whatSide(stack->owner);
  1172. if (absolute)
  1173. gs->curB->enchanterCounter[side] = val;
  1174. else
  1175. gs->curB->enchanterCounter[side] += val;
  1176. vstd::amax(gs->curB->enchanterCounter[side], 0);
  1177. break;
  1178. }
  1179. case UNBIND:
  1180. {
  1181. stack->popBonuses(Selector::type(Bonus::BIND_EFFECT));
  1182. break;
  1183. }
  1184. case CLONED:
  1185. {
  1186. stack->state.insert(EBattleStackState::CLONED);
  1187. break;
  1188. }
  1189. }
  1190. }
  1191. DLL_LINKAGE void YourTurn::applyGs( CGameState *gs )
  1192. {
  1193. gs->currentPlayer = player;
  1194. }
  1195. DLL_LINKAGE void SetSelection::applyGs( CGameState *gs )
  1196. {
  1197. gs->getPlayer(player)->currentSelection = id;
  1198. }
  1199. DLL_LINKAGE Component::Component(const CStackBasicDescriptor &stack)
  1200. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1201. {
  1202. }