NetPacksLib.cpp 35 KB

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