NetPacksLib.cpp 28 KB

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