NetPacksLib.cpp 36 KB

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