NetPacksLib.cpp 35 KB

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