NetPacksLib.cpp 32 KB

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