NetPacksLib.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. amax(val, 0); //new value must be >= 0
  37. gs->getPlayer(player)->resources[resid] = val;
  38. }
  39. DLL_EXPORT void SetResources::applyGs( CGameState *gs )
  40. {
  41. for(int i=0;i<res.size();i++)
  42. gs->getPlayer(player)->resources[i] = res[i];
  43. }
  44. DLL_EXPORT void SetPrimSkill::applyGs( CGameState *gs )
  45. {
  46. CGHeroInstance *hero = gs->getHero(id);
  47. if(which <4)
  48. {
  49. if(abs)
  50. hero->primSkills[which] = val;
  51. else
  52. hero->primSkills[which] += val;
  53. }
  54. else if(which == 4) //XP
  55. {
  56. if(abs)
  57. hero->exp = val;
  58. else
  59. hero->exp += val;
  60. }
  61. }
  62. DLL_EXPORT void SetSecSkill::applyGs( CGameState *gs )
  63. {
  64. CGHeroInstance *hero = gs->getHero(id);
  65. if(hero->getSecSkillLevel(which) == 0)
  66. {
  67. hero->secSkills.push_back(std::pair<int,int>(which, val));
  68. }
  69. else
  70. {
  71. for(unsigned i=0;i<hero->secSkills.size();i++)
  72. {
  73. if(hero->secSkills[i].first == which)
  74. {
  75. if(abs)
  76. hero->secSkills[i].second = val;
  77. else
  78. hero->secSkills[i].second += val;
  79. if(hero->secSkills[i].second > 3) //workaround to avoid crashes when same sec skill is given more than once
  80. {
  81. tlog1 << "Warning: Skill " << which << " increased over limit! Decreasing to Expert.\n";
  82. hero->secSkills[i].second = 3;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. DLL_EXPORT void HeroVisitCastle::applyGs( CGameState *gs )
  89. {
  90. CGHeroInstance *h = gs->getHero(hid);
  91. CGTownInstance *t = gs->getTown(tid);
  92. if(start())
  93. {
  94. if(garrison())
  95. {
  96. t->garrisonHero = h;
  97. h->visitedTown = t;
  98. h->inTownGarrison = true;
  99. }
  100. else
  101. {
  102. t->visitingHero = h;
  103. h->visitedTown = t;
  104. h->inTownGarrison = false;
  105. }
  106. }
  107. else
  108. {
  109. if(garrison())
  110. {
  111. t->garrisonHero = NULL;
  112. h->visitedTown = NULL;
  113. h->inTownGarrison = false;
  114. }
  115. else
  116. {
  117. t->visitingHero = NULL;
  118. h->visitedTown = NULL;
  119. h->inTownGarrison = false;
  120. }
  121. }
  122. }
  123. DLL_EXPORT void ChangeSpells::applyGs( CGameState *gs )
  124. {
  125. CGHeroInstance *hero = gs->getHero(hid);
  126. if(learn)
  127. BOOST_FOREACH(ui32 sid, spells)
  128. hero->spells.insert(sid);
  129. else
  130. BOOST_FOREACH(ui32 sid, spells)
  131. hero->spells.erase(sid);
  132. }
  133. DLL_EXPORT void SetMana::applyGs( CGameState *gs )
  134. {
  135. CGHeroInstance *hero = gs->getHero(hid);
  136. amax(val, 0); //not less than 0
  137. hero->mana = val;
  138. }
  139. DLL_EXPORT void SetMovePoints::applyGs( CGameState *gs )
  140. {
  141. CGHeroInstance *hero = gs->getHero(hid);
  142. hero->movement = val;
  143. }
  144. DLL_EXPORT void FoWChange::applyGs( CGameState *gs )
  145. {
  146. BOOST_FOREACH(int3 t, tiles)
  147. gs->getPlayer(player)->fogOfWarMap[t.x][t.y][t.z] = mode;
  148. }
  149. DLL_EXPORT void SetAvailableHeroes::applyGs( CGameState *gs )
  150. {
  151. gs->getPlayer(player)->availableHeroes.clear();
  152. CGHeroInstance *h = (hid1>=0 ? gs->hpool.heroesPool[hid1] : NULL);
  153. gs->getPlayer(player)->availableHeroes.push_back(h);
  154. if(h && flags & 1)
  155. {
  156. h->army.slots.clear();
  157. h->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[h->type->refTypeStack[0]],1);
  158. }
  159. h = (hid2>=0 ? gs->hpool.heroesPool[hid2] : NULL);
  160. gs->getPlayer(player)->availableHeroes.push_back(h);
  161. if(flags & 2)
  162. {
  163. h->army.slots.clear();
  164. h->army.slots[0] = std::pair<ui32,si32>(VLC->creh->nameToID[h->type->refTypeStack[0]],1);
  165. }
  166. }
  167. DLL_EXPORT void GiveBonus::applyGs( CGameState *gs )
  168. {
  169. CGHeroInstance *h = gs->getHero(hid);
  170. h->bonuses.push_back(bonus);
  171. std::string &descr = h->bonuses.back().description;
  172. if(!bdescr.message.size()
  173. && bonus.source == HeroBonus::OBJECT
  174. && (bonus.type == HeroBonus::LUCK || bonus.type == HeroBonus::MORALE || bonus.type == HeroBonus::MORALE_AND_LUCK)
  175. && gs->map->objects[bonus.id]->ID == 26) //it's morale/luck bonus from an event without description
  176. {
  177. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  178. boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val)));
  179. }
  180. else
  181. {
  182. bdescr.toString(descr);
  183. }
  184. }
  185. DLL_EXPORT void ChangeObjPos::applyGs( CGameState *gs )
  186. {
  187. CGObjectInstance *obj = gs->map->objects[objid];
  188. if(!obj)
  189. {
  190. tlog1 << "Wrong ChangeObjPos: object " << objid << " doesn't exist!\n";
  191. return;
  192. }
  193. gs->map->removeBlockVisTiles(obj);
  194. obj->pos = nPos;
  195. gs->map->addBlockVisTiles(obj);
  196. }
  197. DLL_EXPORT void RemoveObject::applyGs( CGameState *gs )
  198. {
  199. CGObjectInstance *obj = gs->map->objects[id];
  200. if(obj->ID==HEROI_TYPE)
  201. {
  202. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  203. std::vector<CGHeroInstance*>::iterator nitr = std::find(gs->map->heroes.begin(), gs->map->heroes.end(),h);
  204. gs->map->heroes.erase(nitr);
  205. int player = h->tempOwner;
  206. nitr = std::find(gs->getPlayer(player)->heroes.begin(), gs->getPlayer(player)->heroes.end(), h);
  207. gs->getPlayer(player)->heroes.erase(nitr);
  208. if(h->visitedTown)
  209. {
  210. if(h->inTownGarrison)
  211. h->visitedTown->garrisonHero = NULL;
  212. else
  213. h->visitedTown->visitingHero = NULL;
  214. h->visitedTown = NULL;
  215. }
  216. //TODO: add to the pool?
  217. }
  218. gs->map->objects[id] = NULL;
  219. //unblock tiles
  220. if(obj->defInfo)
  221. {
  222. gs->map->removeBlockVisTiles(obj);
  223. }
  224. }
  225. static int getDir(int3 src, int3 dst)
  226. {
  227. int ret = -1;
  228. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  229. {
  230. ret = 1;
  231. }
  232. else if(dst.x == src.x && dst.y+1 == src.y) //t
  233. {
  234. ret = 2;
  235. }
  236. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  237. {
  238. ret = 3;
  239. }
  240. else if(dst.x-1 == src.x && dst.y == src.y) //r
  241. {
  242. ret = 4;
  243. }
  244. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  245. {
  246. ret = 5;
  247. }
  248. else if(dst.x == src.x && dst.y-1 == src.y) //b
  249. {
  250. ret = 6;
  251. }
  252. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  253. {
  254. ret = 7;
  255. }
  256. else if(dst.x+1 == src.x && dst.y == src.y) //l
  257. {
  258. ret = 8;
  259. }
  260. return ret;
  261. }
  262. void TryMoveHero::applyGs( CGameState *gs )
  263. {
  264. CGHeroInstance *h = gs->getHero(id);
  265. h->movement = movePoints;
  266. if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end)
  267. h->moveDir = getDir(start,end);
  268. if(result == EMBARK) //hero enters boat at dest tile
  269. {
  270. const TerrainTile &tt = gs->map->getTile(CGHeroInstance::convertPosition(end, false));
  271. assert(tt.visitableObjects.size() == 1 && tt.visitableObjects.front()->ID == 8); //the only vis obj at dest is Boat
  272. CGBoat *boat = static_cast<CGBoat*>(tt.visitableObjects.front());
  273. gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
  274. h->boat = boat;
  275. boat->hero = h;
  276. }
  277. else if(result == DISEMBARK) //hero leaves boat to dest tile
  278. {
  279. h->boat->direction = h->moveDir;
  280. h->boat->pos = start;
  281. h->boat->hero = NULL;
  282. gs->map->addBlockVisTiles(h->boat);
  283. h->boat = NULL;
  284. }
  285. if(start!=end && (result == SUCCESS || result == TELEPORTATION || result == EMBARK || result == DISEMBARK))
  286. {
  287. gs->map->removeBlockVisTiles(h);
  288. h->pos = end;
  289. if(h->boat)
  290. h->boat->pos = end;
  291. gs->map->addBlockVisTiles(h);
  292. }
  293. BOOST_FOREACH(int3 t, fowRevealed)
  294. gs->getPlayer(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
  295. }
  296. DLL_EXPORT void SetGarrisons::applyGs( CGameState *gs )
  297. {
  298. for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  299. {
  300. CArmedInstance *ai = static_cast<CArmedInstance*>(gs->map->objects[i->first]);
  301. ai->army = i->second;
  302. if(ai->ID==TOWNI_TYPE && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  303. const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->army = i->second;
  304. else if(ai->ID==HEROI_TYPE)
  305. {
  306. CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  307. if(h->visitedTown && h->inTownGarrison)
  308. h->visitedTown->army = i->second;
  309. }
  310. }
  311. }
  312. DLL_EXPORT void NewStructures::applyGs( CGameState *gs )
  313. {
  314. CGTownInstance *t = gs->getTown(tid);
  315. BOOST_FOREACH(si32 id,bid)
  316. {
  317. t->builtBuildings.insert(id);
  318. }
  319. t->builded = builded;
  320. }
  321. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  322. {
  323. CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->map->objects[tid]);
  324. assert(dw);
  325. dw->creatures = creatures;
  326. }
  327. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  328. {
  329. CGTownInstance *t = gs->getTown(tid);
  330. CGHeroInstance *v = gs->getHero(visiting),
  331. *g = gs->getHero(garrison);
  332. t->visitingHero = v;
  333. t->garrisonHero = g;
  334. if(v)
  335. {
  336. v->visitedTown = t;
  337. v->inTownGarrison = false;
  338. gs->map->addBlockVisTiles(v);
  339. }
  340. if(g)
  341. {
  342. g->visitedTown = t;
  343. g->inTownGarrison = true;
  344. gs->map->removeBlockVisTiles(g);
  345. }
  346. }
  347. DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  348. {
  349. CGHeroInstance *h = gs->getHero(hid);
  350. std::vector<ui32> equiped, unequiped;
  351. for(std::map<ui16,ui32>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
  352. if(!vstd::contains(artifWorn,i->first) || artifWorn[i->first] != i->second)
  353. unequiped.push_back(i->second);
  354. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  355. if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  356. equiped.push_back(i->second);
  357. BOOST_FOREACH(ui32 id, equiped)
  358. {
  359. //if hero already had equipped at least one artifact of that type, don't give any new bonuses
  360. if(h->getArtPos(id) >= 0)
  361. continue;
  362. CArtifact &art = VLC->arth->artifacts[id];
  363. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  364. {
  365. gained.push_back(&*i);
  366. h->bonuses.push_back(*i);
  367. }
  368. }
  369. //update hero data
  370. h->artifacts = artifacts;
  371. h->artifWorn = artifWorn;
  372. //remove bonus from unequipped artifact
  373. BOOST_FOREACH(ui32 id, unequiped)
  374. {
  375. //if hero still has equipped at least one artifact of that type, don't remove bonuses
  376. if(h->getArtPos(id) >= 0)
  377. continue;
  378. while(1)
  379. {
  380. std::list<HeroBonus>::iterator hlp = std::find_if(h->bonuses.begin(),h->bonuses.end(),boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,id));
  381. if(hlp != h->bonuses.end())
  382. {
  383. lost.push_back(&*hlp);
  384. h->bonuses.erase(hlp);
  385. }
  386. else
  387. {
  388. break;
  389. }
  390. }
  391. }
  392. }
  393. DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, int art)
  394. {
  395. if(art<0)
  396. {
  397. if(pos<19)
  398. artifWorn.erase(pos);
  399. else
  400. artifacts -= artifacts[pos-19];
  401. }
  402. else
  403. {
  404. if(pos<19)
  405. artifWorn[pos] = art;
  406. else
  407. if(pos-19 < artifacts.size())
  408. artifacts[pos-19] = art;
  409. else
  410. artifacts.push_back(art);
  411. }
  412. }
  413. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  414. {
  415. assert(vstd::contains(gs->hpool.heroesPool, hid));
  416. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  417. CGTownInstance *t = gs->getTown(tid);
  418. h->setOwner(player);
  419. h->pos = tile;
  420. h->movement = h->maxMovePoints(true);
  421. gs->hpool.heroesPool.erase(hid);
  422. if(h->id < 0)
  423. {
  424. h->id = gs->map->objects.size();
  425. gs->map->objects.push_back(h);
  426. }
  427. else
  428. gs->map->objects[h->id] = h;
  429. h->initHeroDefInfo();
  430. gs->map->heroes.push_back(h);
  431. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  432. h->initObj();
  433. gs->map->addBlockVisTiles(h);
  434. t->visitingHero = h;
  435. h->visitedTown = t;
  436. h->inTownGarrison = false;
  437. }
  438. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  439. {
  440. CGHeroInstance *h = gs->getHero(id);
  441. gs->map->removeBlockVisTiles(h,true);
  442. h->setOwner(player);
  443. h->movement = h->maxMovePoints(true);
  444. h->initHeroDefInfo();
  445. gs->map->heroes.push_back(h);
  446. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  447. gs->map->addBlockVisTiles(h);
  448. h->inTownGarrison = false;
  449. }
  450. DLL_EXPORT void NewObject::applyGs( CGameState *gs )
  451. {
  452. CGObjectInstance *o = NULL;
  453. switch(ID)
  454. {
  455. case 8:
  456. o = new CGBoat();
  457. break;
  458. default:
  459. o = new CGObjectInstance();
  460. break;
  461. }
  462. o->ID = ID;
  463. o->subID = subID;
  464. o->pos = pos;
  465. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  466. id = o->id = gs->map->objects.size();
  467. o->hoverName = VLC->generaltexth->names[ID];
  468. gs->map->objects.push_back(o);
  469. gs->map->addBlockVisTiles(o);
  470. o->initObj();
  471. assert(o->defInfo);
  472. }
  473. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  474. {
  475. gs->day = day;
  476. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  477. {
  478. CGHeroInstance *hero = gs->getHero(h.id);
  479. hero->movement = h.move;
  480. hero->mana = h.mana;
  481. }
  482. BOOST_FOREACH(SetResources h, res) //give resources
  483. h.applyGs(gs);
  484. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  485. h.applyGs(gs);
  486. if(resetBuilded) //reset amount of structures set in this turn in towns
  487. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  488. t->builded = 0;
  489. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  490. h->bonuses.remove_if(HeroBonus::OneDay);
  491. if(gs->getDate(1) == 7) //new week
  492. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  493. h->bonuses.remove_if(HeroBonus::OneWeek);
  494. }
  495. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  496. {
  497. CGObjectInstance *obj = gs->map->objects[id];
  498. if(!obj)
  499. {
  500. tlog1 << "Wrong object ID - property cannot be set!\n";
  501. return;
  502. }
  503. if(what == 1)
  504. {
  505. if(obj->ID == TOWNI_TYPE)
  506. {
  507. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  508. if(t->tempOwner < PLAYER_LIMIT)
  509. gs->getPlayer(t->tempOwner)->towns -= t;
  510. if(val < PLAYER_LIMIT)
  511. gs->getPlayer(val)->towns.push_back(t);
  512. }
  513. }
  514. obj->setProperty(what,val);
  515. }
  516. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  517. {
  518. name.toString(gs->map->objects[id]->hoverName);
  519. }
  520. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  521. {
  522. gs->getHero(heroid)->level = level;
  523. }
  524. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  525. {
  526. gs->curB = info;
  527. }
  528. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  529. {
  530. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  531. gs->curB->round = round;
  532. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  533. {
  534. s->state -= DEFENDING;
  535. s->state -= WAITING;
  536. s->state -= MOVED;
  537. s->state -= HAD_MORALE;
  538. s->counterAttacks = 1 + s->valOfFeatures(StackFeature::ADDITIONAL_RETALIATION);
  539. //regeneration
  540. if( s->hasFeatureOfType(StackFeature::HP_REGENERATION) )
  541. s->firstHPleft = std::min<ui32>( s->MaxHealth(), s->valOfFeatures(StackFeature::HP_REGENERATION) );
  542. if( s->hasFeatureOfType(StackFeature::FULL_HP_REGENERATION) )
  543. s->firstHPleft = s->MaxHealth();
  544. //remove effects and restore only those with remaining turns in duration
  545. std::vector<CStack::StackEffect> tmpEffects = s->effects;
  546. s->effects.clear();
  547. for(int i=0; i < tmpEffects.size(); i++)
  548. {
  549. tmpEffects[i].turnsRemain--;
  550. if(tmpEffects[i].turnsRemain > 0)
  551. s->effects.push_back(tmpEffects[i]);
  552. }
  553. //the same as above for features
  554. std::vector<StackFeature> tmpFeatures = s->features;
  555. s->features.clear();
  556. for(int i=0; i < tmpFeatures.size(); i++)
  557. {
  558. if((tmpFeatures[i].duration & StackFeature::N_TURNS) != 0)
  559. {
  560. tmpFeatures[i].turnsRemain--;
  561. if(tmpFeatures[i].turnsRemain > 0)
  562. s->features.push_back(tmpFeatures[i]);
  563. }
  564. else
  565. {
  566. s->features.push_back(tmpFeatures[i]);
  567. }
  568. }
  569. }
  570. }
  571. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  572. {
  573. gs->curB->activeStack = stack;
  574. CStack *st = gs->curB->getStack(stack);
  575. st->state -= WAITING; //if stack was waiting it'll now make move, so it won't be "waiting" anymore
  576. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  577. st->state.insert(HAD_MORALE);
  578. }
  579. void BattleResult::applyGs( CGameState *gs )
  580. {
  581. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  582. delete gs->curB->stacks[i];
  583. //remove any "until next battle" bonuses
  584. CGHeroInstance *h;
  585. h = gs->getHero(gs->curB->hero1);
  586. if(h)
  587. h->bonuses.remove_if(HeroBonus::OneBattle);
  588. h = gs->getHero(gs->curB->hero2);
  589. if(h)
  590. h->bonuses.remove_if(HeroBonus::OneBattle);
  591. delete gs->curB;
  592. gs->curB = NULL;
  593. }
  594. void BattleStackMoved::applyGs( CGameState *gs )
  595. {
  596. gs->curB->getStack(stack)->position = tile;
  597. }
  598. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  599. {
  600. CStack * at = gs->curB->getStack(stackAttacked);
  601. at->amount = newAmount;
  602. at->firstHPleft = newHP;
  603. if(killed())
  604. at->state -= ALIVE;
  605. }
  606. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  607. {
  608. CStack *attacker = gs->curB->getStack(stackAttacking);
  609. if(counter())
  610. attacker->counterAttacks--;
  611. if(shot())
  612. attacker->shots--;
  613. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  614. stackAttacked.applyGs(gs);
  615. for(int g=0; g<attacker->features.size(); ++g)
  616. {
  617. if((attacker->features[g].duration & StackFeature::UNTIL_ATTACK) != 0)
  618. {
  619. attacker->features.erase(attacker->features.begin() + g);
  620. g = 0;
  621. }
  622. }
  623. for(std::set<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  624. {
  625. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  626. for(int g=0; g<stack->features.size(); ++g)
  627. {
  628. if((stack->features[g].duration & StackFeature::UNITL_BEING_ATTACKED) != 0)
  629. {
  630. stack->features.erase(stack->features.begin() + g);
  631. g = 0;
  632. }
  633. }
  634. }
  635. }
  636. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  637. {
  638. CStack *st = gs->curB->getStack(ba.stackNumber);
  639. assert(st);
  640. switch(ba.actionType)
  641. {
  642. case 3:
  643. st->state.insert(DEFENDING);
  644. break;
  645. case 8:
  646. st->state.insert(WAITING);
  647. break;
  648. case 2: case 6: case 7: case 9: case 10: case 11:
  649. st->state.insert(MOVED);
  650. break;
  651. }
  652. }
  653. DLL_EXPORT void SpellCast::applyGs( CGameState *gs )
  654. {
  655. CGHeroInstance *h = (side) ? gs->getHero(gs->curB->hero2) : gs->getHero(gs->curB->hero1);
  656. if(h)
  657. {
  658. int spellCost = 0;
  659. if(gs->curB)
  660. {
  661. spellCost = gs->curB->getSpellCost(&VLC->spellh->spells[id], h);
  662. }
  663. else
  664. {
  665. spellCost = VLC->spellh->spells[id].costs[skill];
  666. }
  667. h->mana -= spellCost;
  668. if(h->mana < 0) h->mana = 0;
  669. }
  670. if(side >= 0 && side < 2)
  671. {
  672. gs->curB->castSpells[side]++;
  673. }
  674. if(gs->curB && id == 35) //dispel
  675. {
  676. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  677. {
  678. CStack *s = gs->curB->getStack(*it);
  679. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  680. {
  681. s->effects.clear(); //removing all effects
  682. //removing all features from spells
  683. std::vector<StackFeature> tmpFeatures = s->features;
  684. s->features.clear();
  685. for(int i=0; i < tmpFeatures.size(); i++)
  686. {
  687. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT)
  688. {
  689. s->features.push_back(tmpFeatures[i]);
  690. }
  691. }
  692. }
  693. }
  694. }
  695. //elemental summoning
  696. if(id >= 66 && id <= 69)
  697. {
  698. int creID;
  699. switch(id)
  700. {
  701. case 66:
  702. creID = 114; //fire elemental
  703. break;
  704. case 67:
  705. creID = 113; //earth elemental
  706. break;
  707. case 68:
  708. creID = 115; //water elemental
  709. break;
  710. case 69:
  711. creID = 112; //air elemental
  712. break;
  713. }
  714. const int3 & tile = gs->curB->tile;
  715. TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  716. int pos; //position of stack on the battlefield - to be calculated
  717. bool ac[BFIELD_SIZE];
  718. std::set<int> occupyable;
  719. bool twoHex = vstd::contains(VLC->creh->creatures[creID].abilities, StackFeature::DOUBLE_WIDE);
  720. bool flying = vstd::contains(VLC->creh->creatures[creID].abilities, StackFeature::FLYING);
  721. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  722. for(int g=0; g<BFIELD_SIZE; ++g)
  723. {
  724. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  725. {
  726. pos = g;
  727. break;
  728. }
  729. }
  730. CStack * summonedStack = gs->curB->generateNewStack(h, creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id].powers[skill], gs->curB->stacks.size(), !side, 255, ter, pos);
  731. summonedStack->features.push_back( makeFeature(StackFeature::SUMMONED, StackFeature::WHOLE_BATTLE, 0, 0, StackFeature::BONUS_FROM_HERO) );
  732. gs->curB->stacks.push_back(summonedStack);
  733. }
  734. }
  735. static inline StackFeature featureGenerator(StackFeature::ECombatFeatures type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0)
  736. {
  737. return makeFeature(type, StackFeature::N_TURNS, subtype, value, StackFeature::SPELL_EFFECT, turnsRemain, additionalInfo);
  738. }
  739. static std::vector<StackFeature> stackEffectToFeature(const CStack::StackEffect & sse)
  740. {
  741. std::vector<StackFeature> sf;
  742. switch(sse.id)
  743. {
  744. case 27: //shield
  745. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  746. break;
  747. case 28: //air shield
  748. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  749. break;
  750. case 30: //protection from air
  751. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  752. break;
  753. case 31: //protection from fire
  754. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  755. break;
  756. case 32: //protection from water
  757. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 2, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  758. break;
  759. case 33: //protection from earth
  760. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 3, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  761. break;
  762. case 34: //anti-magic
  763. sf.push_back(featureGenerator(StackFeature::LEVEL_SPELL_IMMUNITY, 0, VLC->spellh->spells[sse.id].powers[sse.level] - 1, sse.turnsRemain));
  764. break;
  765. case 41: //bless
  766. sf.push_back(featureGenerator(StackFeature::ALWAYS_MAXIMUM_DAMAGE, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  767. break;
  768. case 42: //curse
  769. sf.push_back(featureGenerator(StackFeature::ALWAYS_MINIMUM_DAMAGE, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain, sse.level >= 2 ? 20 : 0));
  770. break;
  771. case 43: //bloodlust
  772. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  773. break;
  774. case 44: //precision
  775. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  776. break;
  777. case 45: //weakness
  778. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  779. break;
  780. case 46: //stone skin
  781. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  782. break;
  783. case 47: //disrupting ray
  784. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  785. break;
  786. case 48: //prayer
  787. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  788. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  789. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  790. break;
  791. case 49: //mirth
  792. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  793. break;
  794. case 50: //sorrow
  795. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  796. break;
  797. case 51: //fortune
  798. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  799. break;
  800. case 52: //misfortune
  801. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  802. break;
  803. case 53: //haste
  804. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  805. break;
  806. case 54: //slow
  807. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, 0, sse.turnsRemain, -1 * VLC->spellh->spells[sse.id].powers[sse.level]));
  808. break;
  809. case 55: //slayer
  810. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  811. break;
  812. case 56: //frenzy
  813. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  814. break;
  815. case 58: //counterstrike
  816. sf.push_back(featureGenerator(StackFeature::ADDITIONAL_RETALIATION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  817. break;
  818. case 59: //bersek
  819. sf.push_back(featureGenerator(StackFeature::ATTACKS_NEAREST_CREATURE, 0, sse.level, sse.turnsRemain));
  820. break;
  821. case 60: //hypnotize
  822. sf.push_back(featureGenerator(StackFeature::HYPNOTIZED, 0, sse.level, sse.turnsRemain));
  823. break;
  824. case 61: //forgetfulness
  825. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  826. break;
  827. case 62: //blind
  828. sf.push_back(makeFeature(StackFeature::NOT_ACTIVE, StackFeature::UNITL_BEING_ATTACKED | StackFeature::N_TURNS, 0, 0, StackFeature::SPELL_EFFECT, sse.turnsRemain, 0));
  829. sf.push_back(makeFeature(StackFeature::GENERAL_ATTACK_REDUCTION, StackFeature::UNTIL_ATTACK | StackFeature::N_TURNS, 0, VLC->spellh->spells[sse.id].powers[sse.level], StackFeature::SPELL_EFFECT, sse.turnsRemain, 0));
  830. break;
  831. }
  832. //setting positiveness
  833. for(int g=0; g<sf.size(); ++g)
  834. {
  835. sf[g].positiveness = VLC->spellh->spells[sse.id].positiveness;
  836. }
  837. return sf;
  838. }
  839. void actualizeEffect(CStack * s, CStack::StackEffect & ef)
  840. {
  841. //actualizing effects vector
  842. for(int g=0; g<s->effects.size(); ++g)
  843. {
  844. if(s->effects[g].id == ef.id)
  845. {
  846. s->effects[g].turnsRemain = std::max(s->effects[g].turnsRemain, ef.turnsRemain);
  847. }
  848. }
  849. //actualizing features vector
  850. std::vector<StackFeature> sf = stackEffectToFeature(ef);
  851. for(int b=0; b<sf.size(); ++b)
  852. {
  853. for(int g=0; g<s->features.size(); ++g)
  854. {
  855. if(s->features[g].source == StackFeature::SPELL_EFFECT && s->features[g].type == sf[b].type && s->features[g].subtype == sf[b].subtype)
  856. {
  857. s->features[g].turnsRemain = std::max(s->features[g].turnsRemain, ef.turnsRemain);
  858. }
  859. }
  860. }
  861. }
  862. bool containsEff(const std::vector<CStack::StackEffect> & vec, int effectId)
  863. {
  864. for(int g=0; g<vec.size(); ++g)
  865. {
  866. if(vec[g].id == effectId)
  867. return true;
  868. }
  869. return false;
  870. }
  871. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  872. {
  873. BOOST_FOREACH(ui32 id, stacks)
  874. {
  875. CStack *s = gs->curB->getStack(id);
  876. if(s)
  877. {
  878. if(effect.id == 42 || !containsEff(s->effects, effect.id))//disrupting ray or not on the list - just add
  879. {
  880. s->effects.push_back(effect);
  881. std::vector<StackFeature> sf = stackEffectToFeature(effect);
  882. for(int n=0; n<sf.size(); ++n)
  883. {
  884. s->features.push_back(sf[n]);
  885. }
  886. }
  887. else //just actualize
  888. {
  889. actualizeEffect(s, effect);
  890. }
  891. }
  892. else
  893. tlog1 << "Cannot find stack " << id << std::endl;
  894. }
  895. }
  896. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  897. {
  898. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  899. stackAttacked.applyGs(gs);
  900. }
  901. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  902. {
  903. for(int g=0; g<healedStacks.size(); ++g)
  904. {
  905. CStack * changedStack = gs->curB->stacks[healedStacks[g].stackID];
  906. //checking if we resurrect a stack that is under a living stack
  907. std::vector<int> access = gs->curB->getAccessibility(changedStack->ID, true);
  908. bool acc[BFIELD_SIZE];
  909. for(int h=0; h<BFIELD_SIZE; ++h)
  910. acc[h] = false;
  911. for(int h=0; h<access.size(); ++h)
  912. acc[access[h]] = true;
  913. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  914. changedStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE), changedStack->attackerOwned,
  915. changedStack->hasFeatureOfType(StackFeature::FLYING), true))
  916. return; //position is already occupied
  917. //applying changes
  918. if(!changedStack->alive())
  919. {
  920. changedStack->state.insert(ALIVE);
  921. }
  922. int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  923. changedStack->amount += healedStacks[g].healedHP / changedStack->MaxHealth();
  924. changedStack->firstHPleft += healedStacks[g].healedHP - changedStack->amount * changedStack->MaxHealth();
  925. if(changedStack->firstHPleft > changedStack->MaxHealth())
  926. {
  927. changedStack->firstHPleft -= changedStack->MaxHealth();
  928. changedStack->amount += 1;
  929. }
  930. //removal of negative effects
  931. {
  932. for(int h=0; h<changedStack->effects.size(); ++h)
  933. {
  934. if(VLC->spellh->spells[changedStack->effects[h].id].positiveness < 0)
  935. {
  936. changedStack->effects.erase(changedStack->effects.begin() + h);
  937. }
  938. }
  939. //removing all features from negative spells
  940. std::vector<StackFeature> tmpFeatures = changedStack->features;
  941. changedStack->features.clear();
  942. for(int i=0; i < tmpFeatures.size(); i++)
  943. {
  944. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT || tmpFeatures[i].positiveness >= 0)
  945. {
  946. changedStack->features.push_back(tmpFeatures[i]);
  947. }
  948. }
  949. }
  950. }
  951. }
  952. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  953. {
  954. if(gs->curB) //if there is a battle
  955. {
  956. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  957. {
  958. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  959. {
  960. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  961. {
  962. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  963. break;
  964. }
  965. }
  966. }
  967. }
  968. }
  969. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  970. {
  971. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  972. {
  973. gs->curB->si.wallState[attackedPartOfWall] =
  974. std::min( gs->curB->si.wallState[attackedPartOfWall] + damageDealt, 3 );
  975. }
  976. }
  977. DLL_EXPORT void BattleStacksRemoved::applyGs( CGameState *gs )
  978. {
  979. if(!gs->curB)
  980. return;
  981. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  982. {
  983. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  984. {
  985. if(gs->curB->stacks[b]->ID == *it) //if found
  986. {
  987. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  988. break;
  989. }
  990. }
  991. }
  992. }
  993. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  994. {
  995. gs->currentPlayer = player;
  996. }
  997. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  998. {
  999. gs->getPlayer(player)->currentSelection = id;
  1000. }