NetPacksLib.cpp 35 KB

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