NetPacksLib.cpp 36 KB

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