NetPacksLib.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  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. //retreive 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. if (specialWeek != NO_ACTION) //first pack applied, reset all effects and aplly new
  683. {
  684. //TODO? won't work for battles lasting several turns (not an issue now but...)
  685. gs->globalEffects.popBonuses(Bonus::OneDay); //works for children -> all game objs
  686. if(gs->getDate(1)) //new week, Monday that is
  687. {
  688. gs->globalEffects.popBonuses(Bonus::OneWeek); //works for children -> all game objs
  689. Bonus *b = new Bonus();
  690. b->duration = Bonus::ONE_WEEK;
  691. b->source = Bonus::SPECIAL_WEEK;
  692. b->effectRange = Bonus::NO_LIMIT;
  693. b->valType = Bonus::BASE_NUMBER; //certainly not intuitive
  694. switch (specialWeek)
  695. {
  696. case DOUBLE_GROWTH:
  697. b->val = 100;
  698. b->type = Bonus::CREATURE_GROWTH_PERCENT;
  699. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false));
  700. break;
  701. case BONUS_GROWTH:
  702. b->val = 5;
  703. b->type = Bonus::CREATURE_GROWTH;
  704. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false));
  705. break;
  706. case DEITYOFFIRE:
  707. b->val = 15;
  708. b->type = Bonus::CREATURE_GROWTH;
  709. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[42], true));
  710. break;
  711. case PLAGUE:
  712. b->val = -100; //no basic creatures
  713. b->type = Bonus::CREATURE_GROWTH_PERCENT;
  714. break;
  715. default:
  716. b->val = 0;
  717. }
  718. if (b->val)
  719. gs->globalEffects.addNewBonus(b);
  720. }
  721. }
  722. else //second pack is applied
  723. {
  724. //count days without town
  725. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  726. {
  727. if(i->second.towns.size() || gs->day == 1)
  728. i->second.daysWithoutCastle = 0;
  729. else
  730. i->second.daysWithoutCastle++;
  731. }
  732. if(resetBuilded) //reset amount of structures set in this turn in towns
  733. {
  734. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  735. t->builded = 0;
  736. }
  737. }
  738. }
  739. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  740. {
  741. CGObjectInstance *obj = gs->map->objects[id];
  742. if(!obj)
  743. {
  744. tlog1 << "Wrong object ID - property cannot be set!\n";
  745. return;
  746. }
  747. CArmedInstance *cai = dynamic_cast<CArmedInstance *>(obj);
  748. if(what == ObjProperty::OWNER && cai)
  749. {
  750. if(obj->ID == TOWNI_TYPE)
  751. {
  752. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  753. if(t->tempOwner < PLAYER_LIMIT)
  754. gs->getPlayer(t->tempOwner)->towns -= t;
  755. if(val < PLAYER_LIMIT)
  756. gs->getPlayer(val)->towns.push_back(t);
  757. }
  758. CBonusSystemNode *nodeToMove = cai->whatShouldBeAttached();
  759. nodeToMove->detachFrom(cai->whereShouldBeAttached(gs));
  760. obj->setProperty(what,val);
  761. nodeToMove->attachTo(cai->whereShouldBeAttached(gs));
  762. }
  763. else //not an armed instance
  764. {
  765. obj->setProperty(what,val);
  766. }
  767. }
  768. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  769. {
  770. name.toString(gs->map->objects[id]->hoverName);
  771. }
  772. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  773. {
  774. CGHeroInstance* h = gs->getHero(heroid);
  775. h->level = level;
  776. //speciality
  777. h->UpdateSpeciality();
  778. }
  779. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  780. {
  781. gs->curB = info;
  782. gs->curB->localInit();
  783. }
  784. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  785. {
  786. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  787. gs->curB->round = round;
  788. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  789. {
  790. s->state -= DEFENDING;
  791. s->state -= WAITING;
  792. s->state -= MOVED;
  793. s->state -= HAD_MORALE;
  794. s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
  795. // new turn effects
  796. s->battleTurnPassed();
  797. }
  798. }
  799. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  800. {
  801. gs->curB->activeStack = stack;
  802. CStack *st = gs->curB->getStack(stack);
  803. if (st->alive())
  804. {
  805. //regeneration
  806. if(st->hasBonusOfType(Bonus::HP_REGENERATION))
  807. st->firstHPleft = std::min<ui32>( st->MaxHealth(), st->valOfBonuses(Bonus::HP_REGENERATION) );
  808. if(st->hasBonusOfType(Bonus::FULL_HP_REGENERATION))
  809. st->firstHPleft = st->MaxHealth();
  810. if(st->hasBonusOfType(Bonus::POISON))
  811. {
  812. Bonus * b = st->getBonus(Selector::source(Bonus::SPELL_EFFECT, 71) && Selector::type(Bonus::STACK_HEALTH));
  813. if (b) //TODO: what if not?...
  814. {
  815. b->val -= 10;
  816. amax (b->val, -(st->valOfBonuses(Bonus::POISON)));
  817. }
  818. }
  819. if(st->hasBonusOfType(Bonus::MANA_DRAIN))
  820. {
  821. const CGHeroInstance * enemy = gs->curB->getHero(gs->curB->theOtherPlayer(st->owner));
  822. if (enemy)
  823. {
  824. ui32 manaDrained = st->valOfBonuses(Bonus::MANA_DRAIN);
  825. amin (manaDrained, gs->curB->heroes[0]->mana);
  826. gs->getHero(enemy->id)->mana -= manaDrained; //jeez, it's overcomplicate
  827. }
  828. }
  829. }
  830. //remove bonuses that last until when stack gets new turn
  831. st->getBonusList().remove_if(Bonus::UntilGetsTurn);
  832. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  833. st->state.insert(HAD_MORALE);
  834. }
  835. void BattleResult::applyGs( CGameState *gs )
  836. {
  837. //stack with SUMMONED flag but coming from garrison -> most likely resurrected, needs to be removed
  838. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  839. {
  840. if(s->base && s->base->armyObj && vstd::contains(s->state, SUMMONED))
  841. {
  842. assert(&s->base->armyObj->getStack(s->slot) == s->base);
  843. const_cast<CArmedInstance*>(s->base->armyObj)->eraseStack(s->slot);
  844. }
  845. }
  846. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  847. delete gs->curB->stacks[i];
  848. //remove any "until next battle" bonuses
  849. CGHeroInstance *h;
  850. h = gs->curB->heroes[0];
  851. if(h)
  852. h->getBonusList().remove_if(Bonus::OneBattle);
  853. h = gs->curB->heroes[1];
  854. if(h)
  855. h->getBonusList().remove_if(Bonus::OneBattle);
  856. if (STACK_EXP)
  857. {
  858. if (exp[0]) //checking local array is easier than dereferencing this crap twice
  859. gs->curB->belligerents[0]->giveStackExp(exp[0]);
  860. if (exp[1])
  861. gs->curB->belligerents[1]->giveStackExp(exp[1]);
  862. }
  863. gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
  864. gs->curB.dellNull();
  865. }
  866. void BattleStackMoved::applyGs( CGameState *gs )
  867. {
  868. gs->curB->getStack(stack)->position = tile;
  869. }
  870. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  871. {
  872. CStack * at = gs->curB->getStack(stackAttacked);
  873. at->count = newAmount;
  874. at->firstHPleft = newHP;
  875. if(killed())
  876. {
  877. at->state -= ALIVE;
  878. }
  879. //life drain handling
  880. for (int g=0; g<healedStacks.size(); ++g)
  881. {
  882. healedStacks[g].applyGs(gs);
  883. }
  884. if (willRebirth())
  885. {
  886. at->casts--;
  887. at->state.insert(ALIVE); //hmm?
  888. }
  889. }
  890. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  891. {
  892. CStack *attacker = gs->curB->getStack(stackAttacking);
  893. if(counter())
  894. attacker->counterAttacks--;
  895. if(shot())
  896. {
  897. //don't remove ammo if we have a working ammo cart
  898. bool hasAmmoCart = false;
  899. BOOST_FOREACH(const CStack * st, gs->curB->stacks)
  900. {
  901. if(st->owner == attacker->owner && st->getCreature()->idNumber == 148 && st->alive())
  902. {
  903. hasAmmoCart = true;
  904. break;
  905. }
  906. }
  907. if (!hasAmmoCart)
  908. {
  909. attacker->shots--;
  910. }
  911. }
  912. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  913. stackAttacked.applyGs(gs);
  914. attacker->getBonusList().remove_if(Bonus::UntilAttack);
  915. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  916. {
  917. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  918. stack->getBonusList().remove_if(Bonus::UntilBeingAttacked);
  919. }
  920. }
  921. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  922. {
  923. CStack *st = gs->curB->getStack(ba.stackNumber);
  924. if(ba.actionType == BattleAction::END_TACTIC_PHASE)
  925. {
  926. gs->curB->tacticDistance = 0;
  927. return;
  928. }
  929. if(ba.actionType != BattleAction::HERO_SPELL) //don't check for stack if it's custom action by hero
  930. {
  931. assert(st);
  932. }
  933. else
  934. {
  935. gs->curB->usedSpellsHistory[ba.side].push_back(VLC->spellh->spells[ba.additionalInfo]);
  936. }
  937. switch(ba.actionType)
  938. {
  939. case BattleAction::DEFEND:
  940. st->state.insert(DEFENDING);
  941. break;
  942. case BattleAction::WAIT:
  943. st->state.insert(WAITING);
  944. return;
  945. case BattleAction::NO_ACTION: case BattleAction::WALK: case BattleAction::WALK_AND_ATTACK:
  946. case BattleAction::SHOOT: case BattleAction::CATAPULT: case BattleAction::MONSTER_SPELL:
  947. case BattleAction::BAD_MORALE: case BattleAction::STACK_HEAL:
  948. st->state.insert(MOVED);
  949. break;
  950. }
  951. if(st)
  952. 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)
  953. }
  954. DLL_EXPORT void BattleSpellCast::applyGs( CGameState *gs )
  955. {
  956. assert(gs->curB);
  957. CGHeroInstance *h = (side) ? gs->curB->heroes[1] : gs->curB->heroes[0];
  958. if(h && castedByHero)
  959. {
  960. int spellCost = 0;
  961. if(gs->curB)
  962. {
  963. spellCost = gs->curB->getSpellCost(VLC->spellh->spells[id], h);
  964. }
  965. else
  966. {
  967. spellCost = VLC->spellh->spells[id]->costs[skill];
  968. }
  969. h->mana -= spellCost;
  970. if(h->mana < 0) h->mana = 0;
  971. }
  972. if(side >= 0 && side < 2 && castedByHero)
  973. {
  974. gs->curB->castSpells[side]++;
  975. }
  976. if(id == 35 || id == 78) //dispel and dispel helpful spells
  977. {
  978. bool onlyHelpful = id == 78;
  979. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  980. {
  981. CStack *s = gs->curB->getStack(*it);
  982. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  983. {
  984. if(onlyHelpful)
  985. s->popBonuses(Selector::positiveSpellEffects);
  986. else
  987. s->popBonuses(Selector::sourceType(Bonus::SPELL_EFFECT));
  988. }
  989. }
  990. }
  991. //elemental summoning
  992. if(id >= 66 && id <= 69)
  993. {
  994. int creID;
  995. switch(id)
  996. {
  997. case 66:
  998. creID = 114; //fire elemental
  999. break;
  1000. case 67:
  1001. creID = 113; //earth elemental
  1002. break;
  1003. case 68:
  1004. creID = 115; //water elemental
  1005. break;
  1006. case 69:
  1007. creID = 112; //air elemental
  1008. break;
  1009. }
  1010. // const int3 & tile = gs->curB->tile;
  1011. // TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  1012. int pos; //position of stack on the battlefield - to be calculated
  1013. bool ac[BFIELD_SIZE];
  1014. std::set<THex> occupyable;
  1015. bool twoHex = VLC->creh->creatures[creID]->isDoubleWide();
  1016. bool flying = VLC->creh->creatures[creID]->isFlying();// vstd::contains(VLC->creh->creatures[creID]->bonuses, Bonus::FLYING);
  1017. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  1018. for(int g=0; g<BFIELD_SIZE; ++g)
  1019. {
  1020. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  1021. {
  1022. pos = g;
  1023. break;
  1024. }
  1025. }
  1026. CStackInstance *csi = new CStackInstance(creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id]->powers[skill]); //deleted by d-tor of summoned stack
  1027. csi->setArmyObj(h);
  1028. CStack * summonedStack = gs->curB->generateNewStack(*csi, gs->curB->stacks.size(), !side, 255, pos);
  1029. summonedStack->state.insert(SUMMONED);
  1030. summonedStack->attachTo(csi);
  1031. summonedStack->postInit();
  1032. //summonedStack->addNewBonus( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1033. gs->curB->stacks.push_back(summonedStack);
  1034. }
  1035. }
  1036. void actualizeEffect(CStack * s, const std::vector<Bonus> & ef)
  1037. {
  1038. //actualizing features vector
  1039. BOOST_FOREACH(const Bonus &fromEffect, ef)
  1040. {
  1041. BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
  1042. {
  1043. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == fromEffect.type && stackBonus->subtype == fromEffect.subtype)
  1044. {
  1045. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, fromEffect.turnsRemain);
  1046. }
  1047. }
  1048. }
  1049. }
  1050. void actualizeEffect(CStack * s, const Bonus & ef)
  1051. {
  1052. BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
  1053. {
  1054. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == ef.type && stackBonus->subtype == ef.subtype)
  1055. {
  1056. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, ef.turnsRemain);
  1057. }
  1058. }
  1059. }
  1060. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  1061. {
  1062. int spellid = effect.begin()->sid; //effects' source ID
  1063. BOOST_FOREACH(ui32 id, stacks)
  1064. {
  1065. CStack *s = gs->curB->getStack(id);
  1066. if(s)
  1067. {
  1068. 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
  1069. {
  1070. BOOST_FOREACH(Bonus &fromEffect, effect)
  1071. {
  1072. s->addNewBonus( new Bonus(fromEffect));
  1073. }
  1074. }
  1075. else //just actualize
  1076. {
  1077. actualizeEffect(s, effect);
  1078. }
  1079. }
  1080. else
  1081. tlog1 << "Cannot find stack " << id << std::endl;
  1082. }
  1083. typedef std::pair<ui32, Bonus> p;
  1084. BOOST_FOREACH(p para, uniqueBonuses)
  1085. {
  1086. CStack *s = gs->curB->getStack(para.first);
  1087. if (s)
  1088. {
  1089. if (!s->hasBonus(Selector::source(Bonus::SPELL_EFFECT, spellid) && Selector::typeSubtype(para.second.type, para.second.subtype)))
  1090. s->addNewBonus(new Bonus(para.second));
  1091. else
  1092. actualizeEffect(s, effect);
  1093. }
  1094. else
  1095. tlog1 << "Cannot find stack " << para.first << std::endl;
  1096. }
  1097. }
  1098. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  1099. {
  1100. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  1101. stackAttacked.applyGs(gs);
  1102. }
  1103. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  1104. {
  1105. for(int g=0; g<healedStacks.size(); ++g)
  1106. {
  1107. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  1108. //checking if we resurrect a stack that is under a living stack
  1109. std::vector<THex> access = gs->curB->getAccessibility(changedStack, true);
  1110. bool acc[BFIELD_SIZE];
  1111. for(int h=0; h<BFIELD_SIZE; ++h)
  1112. acc[h] = false;
  1113. for(int h=0; h<access.size(); ++h)
  1114. acc[access[h]] = true;
  1115. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  1116. changedStack->doubleWide(), changedStack->attackerOwned,
  1117. changedStack->hasBonusOfType(Bonus::FLYING), true))
  1118. return; //position is already occupied
  1119. //applying changes
  1120. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1121. if(resurrected)
  1122. {
  1123. changedStack->state.insert(ALIVE);
  1124. if(healedStacks[g].lowLevelResurrection)
  1125. changedStack->state.insert(SUMMONED);
  1126. //changedStack->addNewBonus( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1127. }
  1128. //int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1129. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1130. changedStack->count += res;
  1131. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1132. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1133. {
  1134. changedStack->firstHPleft -= changedStack->MaxHealth();
  1135. if(changedStack->baseAmount > changedStack->count)
  1136. {
  1137. changedStack->count += 1;
  1138. }
  1139. }
  1140. amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1141. //removal of negative effects
  1142. if(resurrected)
  1143. {
  1144. // for (BonusList::iterator it = changedStack->bonuses.begin(); it != changedStack->bonuses.end(); it++)
  1145. // {
  1146. // if(VLC->spellh->spells[(*it)->sid]->positiveness < 0)
  1147. // {
  1148. // changedStack->bonuses.erase(it);
  1149. // }
  1150. // }
  1151. //removing all features from negative spells
  1152. const BonusList tmpFeatures = changedStack->getBonusList();
  1153. //changedStack->bonuses.clear();
  1154. BOOST_FOREACH(Bonus *b, tmpFeatures)
  1155. {
  1156. const CSpell *s = b->sourceSpell();
  1157. if(s && s->positiveness < 0)
  1158. {
  1159. changedStack->removeBonus(b);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. }
  1165. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  1166. {
  1167. if(gs->curB) //if there is a battle
  1168. {
  1169. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1170. {
  1171. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1172. {
  1173. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1174. {
  1175. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1176. break;
  1177. }
  1178. }
  1179. }
  1180. }
  1181. }
  1182. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  1183. {
  1184. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1185. {
  1186. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1187. {
  1188. gs->curB->si.wallState[it->first.first] =
  1189. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1190. }
  1191. }
  1192. }
  1193. DLL_EXPORT void BattleStacksRemoved::applyGs( CGameState *gs )
  1194. {
  1195. if(!gs->curB)
  1196. return;
  1197. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1198. {
  1199. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1200. {
  1201. if(gs->curB->stacks[b]->ID == *it) //if found
  1202. {
  1203. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1204. break;
  1205. }
  1206. }
  1207. }
  1208. }
  1209. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  1210. {
  1211. gs->currentPlayer = player;
  1212. }
  1213. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  1214. {
  1215. gs->getPlayer(player)->currentSelection = id;
  1216. }
  1217. DLL_EXPORT Component::Component(const CStackBasicDescriptor &stack)
  1218. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1219. {
  1220. }