NetPacksLib.cpp 36 KB

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