NetPacksLib.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  576. st->state.insert(HAD_MORALE);
  577. }
  578. void BattleResult::applyGs( CGameState *gs )
  579. {
  580. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  581. delete gs->curB->stacks[i];
  582. //remove any "until next battle" bonuses
  583. CGHeroInstance *h;
  584. h = gs->getHero(gs->curB->hero1);
  585. if(h)
  586. h->bonuses.remove_if(HeroBonus::OneBattle);
  587. h = gs->getHero(gs->curB->hero2);
  588. if(h)
  589. h->bonuses.remove_if(HeroBonus::OneBattle);
  590. delete gs->curB;
  591. gs->curB = NULL;
  592. }
  593. void BattleStackMoved::applyGs( CGameState *gs )
  594. {
  595. gs->curB->getStack(stack)->position = tile;
  596. }
  597. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  598. {
  599. CStack * at = gs->curB->getStack(stackAttacked);
  600. at->amount = newAmount;
  601. at->firstHPleft = newHP;
  602. if(killed())
  603. at->state -= ALIVE;
  604. }
  605. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  606. {
  607. CStack *attacker = gs->curB->getStack(stackAttacking);
  608. if(counter())
  609. attacker->counterAttacks--;
  610. if(shot())
  611. attacker->shots--;
  612. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  613. stackAttacked.applyGs(gs);
  614. for(int g=0; g<attacker->features.size(); ++g)
  615. {
  616. if((attacker->features[g].duration & StackFeature::UNTIL_ATTACK) != 0)
  617. {
  618. attacker->features.erase(attacker->features.begin() + g);
  619. g = 0;
  620. }
  621. }
  622. for(std::set<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  623. {
  624. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  625. for(int g=0; g<stack->features.size(); ++g)
  626. {
  627. if((stack->features[g].duration & StackFeature::UNITL_BEING_ATTACKED) != 0)
  628. {
  629. stack->features.erase(stack->features.begin() + g);
  630. g = 0;
  631. }
  632. }
  633. }
  634. }
  635. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  636. {
  637. CStack *st = gs->curB->getStack(ba.stackNumber);
  638. switch(ba.actionType)
  639. {
  640. case 3:
  641. st->state.insert(DEFENDING);
  642. break;
  643. case 8:
  644. st->state.insert(WAITING);
  645. break;
  646. case 2: case 6: case 7: case 9: case 10: case 11:
  647. st->state.insert(MOVED);
  648. break;
  649. }
  650. }
  651. DLL_EXPORT void SpellCast::applyGs( CGameState *gs )
  652. {
  653. CGHeroInstance *h = (side) ? gs->getHero(gs->curB->hero2) : gs->getHero(gs->curB->hero1);
  654. if(h)
  655. {
  656. int spellCost = 0;
  657. if(gs->curB)
  658. {
  659. spellCost = gs->curB->getSpellCost(&VLC->spellh->spells[id], h);
  660. }
  661. else
  662. {
  663. spellCost = VLC->spellh->spells[id].costs[skill];
  664. }
  665. h->mana -= spellCost;
  666. if(h->mana < 0) h->mana = 0;
  667. }
  668. if(side >= 0 && side < 2)
  669. {
  670. gs->curB->castSpells[side]++;
  671. }
  672. if(gs->curB && id == 35) //dispel
  673. {
  674. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  675. {
  676. CStack *s = gs->curB->getStack(*it);
  677. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  678. {
  679. s->effects.clear(); //removing all effects
  680. //removing all features from spells
  681. std::vector<StackFeature> tmpFeatures = s->features;
  682. s->features.clear();
  683. for(int i=0; i < tmpFeatures.size(); i++)
  684. {
  685. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT)
  686. {
  687. s->features.push_back(tmpFeatures[i]);
  688. }
  689. }
  690. }
  691. }
  692. }
  693. //elemental summoning
  694. if(id >= 66 && id <= 69)
  695. {
  696. int creID;
  697. switch(id)
  698. {
  699. case 66:
  700. creID = 114; //fire elemental
  701. break;
  702. case 67:
  703. creID = 113; //earth elemental
  704. break;
  705. case 68:
  706. creID = 115; //water elemental
  707. break;
  708. case 69:
  709. creID = 112; //air elemental
  710. break;
  711. }
  712. const int3 & tile = gs->curB->tile;
  713. TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  714. int pos; //position of stack on the battlefield - to be calculated
  715. bool ac[BFIELD_SIZE];
  716. std::set<int> occupyable;
  717. bool twoHex = vstd::contains(VLC->creh->creatures[creID].abilities, StackFeature::DOUBLE_WIDE);
  718. bool flying = vstd::contains(VLC->creh->creatures[creID].abilities, StackFeature::FLYING);
  719. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  720. for(int g=0; g<BFIELD_SIZE; ++g)
  721. {
  722. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  723. {
  724. pos = g;
  725. break;
  726. }
  727. }
  728. CStack * summonedStack = gs->curB->generateNewStack(h, creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id].powers[skill], gs->curB->stacks.size(), !side, 255, ter, pos);
  729. summonedStack->features.push_back( makeFeature(StackFeature::SUMMONED, StackFeature::WHOLE_BATTLE, 0, 0, StackFeature::BONUS_FROM_HERO) );
  730. gs->curB->stacks.push_back(summonedStack);
  731. }
  732. }
  733. static inline StackFeature featureGenerator(StackFeature::ECombatFeatures type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0)
  734. {
  735. return makeFeature(type, StackFeature::N_TURNS, subtype, value, StackFeature::SPELL_EFFECT, turnsRemain, additionalInfo);
  736. }
  737. static std::vector<StackFeature> stackEffectToFeature(const CStack::StackEffect & sse)
  738. {
  739. std::vector<StackFeature> sf;
  740. switch(sse.id)
  741. {
  742. case 27: //shield
  743. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  744. break;
  745. case 28: //air shield
  746. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  747. break;
  748. case 30: //protection from air
  749. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  750. break;
  751. case 31: //protection from fire
  752. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  753. break;
  754. case 32: //protection from water
  755. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 2, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  756. break;
  757. case 33: //protection from earth
  758. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 3, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  759. break;
  760. case 34: //anti-magic
  761. sf.push_back(featureGenerator(StackFeature::LEVEL_SPELL_IMMUNITY, 0, VLC->spellh->spells[sse.id].powers[sse.level] - 1, sse.turnsRemain));
  762. break;
  763. case 41: //bless
  764. sf.push_back(featureGenerator(StackFeature::ALWAYS_MAXIMUM_DAMAGE, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  765. break;
  766. case 42: //curse
  767. 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));
  768. break;
  769. case 43: //bloodlust
  770. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  771. break;
  772. case 44: //precision
  773. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  774. break;
  775. case 45: //weakness
  776. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  777. break;
  778. case 46: //stone skin
  779. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  780. break;
  781. case 47: //disrupting ray
  782. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  783. break;
  784. case 48: //prayer
  785. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  786. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  787. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  788. break;
  789. case 49: //mirth
  790. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  791. break;
  792. case 50: //sorrow
  793. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  794. break;
  795. case 51: //fortune
  796. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  797. break;
  798. case 52: //misfortune
  799. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  800. break;
  801. case 53: //haste
  802. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  803. break;
  804. case 54: //slow
  805. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, 0, sse.turnsRemain, -1 * VLC->spellh->spells[sse.id].powers[sse.level]));
  806. break;
  807. case 55: //slayer
  808. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  809. break;
  810. case 56: //frenzy
  811. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  812. break;
  813. case 58: //counterstrike
  814. sf.push_back(featureGenerator(StackFeature::ADDITIONAL_RETALIATION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  815. break;
  816. case 59: //bersek
  817. sf.push_back(featureGenerator(StackFeature::ATTACKS_NEAREST_CREATURE, 0, sse.level, sse.turnsRemain));
  818. break;
  819. case 60: //hypnotize
  820. sf.push_back(featureGenerator(StackFeature::HYPNOTIZED, 0, sse.level, sse.turnsRemain));
  821. break;
  822. case 61: //forgetfulness
  823. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  824. break;
  825. case 62: //blind
  826. sf.push_back(makeFeature(StackFeature::NOT_ACTIVE, StackFeature::UNITL_BEING_ATTACKED | StackFeature::N_TURNS, 0, 0, StackFeature::SPELL_EFFECT, sse.turnsRemain, 0));
  827. 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));
  828. break;
  829. }
  830. //setting positiveness
  831. for(int g=0; g<sf.size(); ++g)
  832. {
  833. sf[g].positiveness = VLC->spellh->spells[sse.id].positiveness;
  834. }
  835. return sf;
  836. }
  837. void actualizeEffect(CStack * s, CStack::StackEffect & ef)
  838. {
  839. //actualizing effects vector
  840. for(int g=0; g<s->effects.size(); ++g)
  841. {
  842. if(s->effects[g].id == ef.id)
  843. {
  844. s->effects[g].turnsRemain = std::max(s->effects[g].turnsRemain, ef.turnsRemain);
  845. }
  846. }
  847. //actualizing features vector
  848. std::vector<StackFeature> sf = stackEffectToFeature(ef);
  849. for(int b=0; b<sf.size(); ++b)
  850. {
  851. for(int g=0; g<s->features.size(); ++g)
  852. {
  853. if(s->features[g].source == StackFeature::SPELL_EFFECT && s->features[g].type == sf[b].type && s->features[g].subtype == sf[b].subtype)
  854. {
  855. s->features[g].turnsRemain = std::max(s->features[g].turnsRemain, ef.turnsRemain);
  856. }
  857. }
  858. }
  859. }
  860. bool containsEff(const std::vector<CStack::StackEffect> & vec, int effectId)
  861. {
  862. for(int g=0; g<vec.size(); ++g)
  863. {
  864. if(vec[g].id == effectId)
  865. return true;
  866. }
  867. return false;
  868. }
  869. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  870. {
  871. BOOST_FOREACH(ui32 id, stacks)
  872. {
  873. CStack *s = gs->curB->getStack(id);
  874. if(s)
  875. {
  876. if(effect.id == 42 || !containsEff(s->effects, effect.id))//disrupting ray or not on the list - just add
  877. {
  878. s->effects.push_back(effect);
  879. std::vector<StackFeature> sf = stackEffectToFeature(effect);
  880. for(int n=0; n<sf.size(); ++n)
  881. {
  882. s->features.push_back(sf[n]);
  883. }
  884. }
  885. else //just actualize
  886. {
  887. actualizeEffect(s, effect);
  888. }
  889. }
  890. else
  891. tlog1 << "Cannot find stack " << id << std::endl;
  892. }
  893. }
  894. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  895. {
  896. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  897. stackAttacked.applyGs(gs);
  898. }
  899. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  900. {
  901. for(int g=0; g<healedStacks.size(); ++g)
  902. {
  903. CStack * changedStack = gs->curB->stacks[healedStacks[g].stackID];
  904. //checking if we resurrect a stack that is under a living stack
  905. std::vector<int> access = gs->curB->getAccessibility(changedStack->ID, true);
  906. bool acc[BFIELD_SIZE];
  907. for(int h=0; h<BFIELD_SIZE; ++h)
  908. acc[h] = false;
  909. for(int h=0; h<access.size(); ++h)
  910. acc[access[h]] = true;
  911. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  912. changedStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE), changedStack->attackerOwned,
  913. changedStack->hasFeatureOfType(StackFeature::FLYING), true))
  914. return; //position is already occupied
  915. //applying changes
  916. if(!changedStack->alive())
  917. {
  918. changedStack->state.insert(ALIVE);
  919. }
  920. int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  921. changedStack->amount += healedStacks[g].healedHP / changedStack->MaxHealth();
  922. changedStack->firstHPleft += healedStacks[g].healedHP - changedStack->amount * changedStack->MaxHealth();
  923. if(changedStack->firstHPleft > changedStack->MaxHealth())
  924. {
  925. changedStack->firstHPleft -= changedStack->MaxHealth();
  926. changedStack->amount += 1;
  927. }
  928. //removal of negative effects
  929. {
  930. for(int h=0; h<changedStack->effects.size(); ++h)
  931. {
  932. if(VLC->spellh->spells[changedStack->effects[h].id].positiveness < 0)
  933. {
  934. changedStack->effects.erase(changedStack->effects.begin() + h);
  935. }
  936. }
  937. //removing all features from negative spells
  938. std::vector<StackFeature> tmpFeatures = changedStack->features;
  939. changedStack->features.clear();
  940. for(int i=0; i < tmpFeatures.size(); i++)
  941. {
  942. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT || tmpFeatures[i].positiveness >= 0)
  943. {
  944. changedStack->features.push_back(tmpFeatures[i]);
  945. }
  946. }
  947. }
  948. }
  949. }
  950. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  951. {
  952. if(gs->curB) //if there is a battle
  953. {
  954. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  955. {
  956. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  957. {
  958. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  959. {
  960. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  961. break;
  962. }
  963. }
  964. }
  965. }
  966. }
  967. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  968. {
  969. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  970. {
  971. gs->curB->si.wallState[attackedPartOfWall] =
  972. std::min( gs->curB->si.wallState[attackedPartOfWall] + damageDealt, 3 );
  973. }
  974. }
  975. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  976. {
  977. gs->currentPlayer = player;
  978. }
  979. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  980. {
  981. gs->getPlayer(player)->currentSelection = id;
  982. }