NetPacksLib.cpp 35 KB

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