NetPacksLib.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. t->builtBuildings.insert(id);
  317. t->builded = builded;
  318. }
  319. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  320. {
  321. CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->map->objects[tid]);
  322. assert(dw);
  323. dw->creatures = creatures;
  324. }
  325. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  326. {
  327. CGTownInstance *t = gs->getTown(tid);
  328. CGHeroInstance *v = gs->getHero(visiting),
  329. *g = gs->getHero(garrison);
  330. t->visitingHero = v;
  331. t->garrisonHero = g;
  332. if(v)
  333. {
  334. v->visitedTown = t;
  335. v->inTownGarrison = false;
  336. gs->map->addBlockVisTiles(v);
  337. }
  338. if(g)
  339. {
  340. g->visitedTown = t;
  341. g->inTownGarrison = true;
  342. gs->map->removeBlockVisTiles(g);
  343. }
  344. }
  345. DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  346. {
  347. CGHeroInstance *h = gs->getHero(hid);
  348. std::vector<ui32> equiped, unequiped;
  349. for(std::map<ui16,ui32>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
  350. if(!vstd::contains(artifWorn,i->first) || artifWorn[i->first] != i->second)
  351. unequiped.push_back(i->second);
  352. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  353. if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  354. equiped.push_back(i->second);
  355. BOOST_FOREACH(ui32 id, equiped)
  356. {
  357. //if hero already had equipped at least one artifact of that type, don't give any new bonuses
  358. if(h->getArtPos(id) >= 0)
  359. continue;
  360. CArtifact &art = VLC->arth->artifacts[id];
  361. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  362. {
  363. gained.push_back(&*i);
  364. h->bonuses.push_back(*i);
  365. }
  366. }
  367. //update hero data
  368. h->artifacts = artifacts;
  369. h->artifWorn = artifWorn;
  370. //remove bonus from unequipped artifact
  371. BOOST_FOREACH(ui32 id, unequiped)
  372. {
  373. //if hero still has equipped at least one artifact of that type, don't remove bonuses
  374. if(h->getArtPos(id) >= 0)
  375. continue;
  376. while(1)
  377. {
  378. std::list<HeroBonus>::iterator hlp = std::find_if(h->bonuses.begin(),h->bonuses.end(),boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,id));
  379. if(hlp != h->bonuses.end())
  380. {
  381. lost.push_back(&*hlp);
  382. h->bonuses.erase(hlp);
  383. }
  384. else
  385. {
  386. break;
  387. }
  388. }
  389. }
  390. }
  391. DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, int art)
  392. {
  393. if(art<0)
  394. {
  395. if(pos<19)
  396. artifWorn.erase(pos);
  397. else
  398. artifacts -= artifacts[pos-19];
  399. }
  400. else
  401. {
  402. if(pos<19)
  403. artifWorn[pos] = art;
  404. else
  405. if(pos-19 < artifacts.size())
  406. artifacts[pos-19] = art;
  407. else
  408. artifacts.push_back(art);
  409. }
  410. }
  411. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  412. {
  413. assert(vstd::contains(gs->hpool.heroesPool, hid));
  414. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  415. CGTownInstance *t = gs->getTown(tid);
  416. h->setOwner(player);
  417. h->pos = tile;
  418. h->movement = h->maxMovePoints(true);
  419. gs->hpool.heroesPool.erase(hid);
  420. if(h->id < 0)
  421. {
  422. h->id = gs->map->objects.size();
  423. gs->map->objects.push_back(h);
  424. }
  425. else
  426. gs->map->objects[h->id] = h;
  427. h->initHeroDefInfo();
  428. gs->map->heroes.push_back(h);
  429. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  430. h->initObj();
  431. gs->map->addBlockVisTiles(h);
  432. t->visitingHero = h;
  433. h->visitedTown = t;
  434. h->inTownGarrison = false;
  435. }
  436. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  437. {
  438. CGHeroInstance *h = gs->getHero(id);
  439. gs->map->removeBlockVisTiles(h,true);
  440. h->setOwner(player);
  441. h->movement = h->maxMovePoints(true);
  442. h->initHeroDefInfo();
  443. gs->map->heroes.push_back(h);
  444. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  445. gs->map->addBlockVisTiles(h);
  446. h->inTownGarrison = false;
  447. }
  448. DLL_EXPORT void NewObject::applyGs( CGameState *gs )
  449. {
  450. CGObjectInstance *o = NULL;
  451. switch(ID)
  452. {
  453. case 8:
  454. o = new CGBoat();
  455. break;
  456. default:
  457. o = new CGObjectInstance();
  458. break;
  459. }
  460. o->ID = ID;
  461. o->subID = subID;
  462. o->pos = pos;
  463. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  464. id = o->id = gs->map->objects.size();
  465. o->hoverName = VLC->generaltexth->names[ID];
  466. gs->map->objects.push_back(o);
  467. gs->map->addBlockVisTiles(o);
  468. o->initObj();
  469. assert(o->defInfo);
  470. }
  471. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  472. {
  473. gs->day = day;
  474. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  475. {
  476. CGHeroInstance *hero = gs->getHero(h.id);
  477. hero->movement = h.move;
  478. hero->mana = h.mana;
  479. }
  480. BOOST_FOREACH(SetResources h, res) //give resources
  481. h.applyGs(gs);
  482. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  483. h.applyGs(gs);
  484. if(resetBuilded) //reset amount of structures set in this turn in towns
  485. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  486. t->builded = 0;
  487. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  488. h->bonuses.remove_if(HeroBonus::OneDay);
  489. if(gs->getDate(1) == 7) //new week
  490. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  491. h->bonuses.remove_if(HeroBonus::OneWeek);
  492. }
  493. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  494. {
  495. CGObjectInstance *obj = gs->map->objects[id];
  496. if(!obj)
  497. {
  498. tlog1 << "Wrong object ID - property cannot be set!\n";
  499. return;
  500. }
  501. if(what == 1)
  502. {
  503. if(obj->ID == TOWNI_TYPE)
  504. {
  505. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  506. if(t->tempOwner < PLAYER_LIMIT)
  507. gs->getPlayer(t->tempOwner)->towns -= t;
  508. if(val < PLAYER_LIMIT)
  509. gs->getPlayer(val)->towns.push_back(t);
  510. }
  511. }
  512. obj->setProperty(what,val);
  513. }
  514. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  515. {
  516. name.toString(gs->map->objects[id]->hoverName);
  517. }
  518. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  519. {
  520. gs->getHero(heroid)->level = level;
  521. }
  522. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  523. {
  524. gs->curB = info;
  525. }
  526. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  527. {
  528. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  529. gs->curB->round = round;
  530. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  531. {
  532. s->state -= DEFENDING;
  533. s->state -= WAITING;
  534. s->state -= MOVED;
  535. s->state -= HAD_MORALE;
  536. s->counterAttacks = 1 + s->valOfFeatures(StackFeature::ADDITIONAL_RETALIATION);
  537. //regeneration
  538. if( s->hasFeatureOfType(StackFeature::HP_REGENERATION) )
  539. s->firstHPleft = std::min<ui32>( s->MaxHealth(), s->valOfFeatures(StackFeature::HP_REGENERATION) );
  540. if( s->hasFeatureOfType(StackFeature::FULL_HP_REGENERATION) )
  541. s->firstHPleft = s->MaxHealth();
  542. //remove effects and restore only those with remaining turns in duration
  543. std::vector<CStack::StackEffect> tmpEffects = s->effects;
  544. s->effects.clear();
  545. for(int i=0; i < tmpEffects.size(); i++)
  546. {
  547. tmpEffects[i].turnsRemain--;
  548. if(tmpEffects[i].turnsRemain > 0)
  549. s->effects.push_back(tmpEffects[i]);
  550. }
  551. //the same as above for features
  552. std::vector<StackFeature> tmpFeatures = s->features;
  553. s->features.clear();
  554. for(int i=0; i < tmpFeatures.size(); i++)
  555. {
  556. if((tmpFeatures[i].duration & StackFeature::N_TURNS) != 0)
  557. {
  558. tmpFeatures[i].turnsRemain--;
  559. if(tmpFeatures[i].turnsRemain > 0)
  560. s->features.push_back(tmpFeatures[i]);
  561. }
  562. else
  563. {
  564. s->features.push_back(tmpFeatures[i]);
  565. }
  566. }
  567. }
  568. }
  569. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  570. {
  571. gs->curB->activeStack = stack;
  572. CStack *st = gs->curB->getStack(stack);
  573. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  574. st->state.insert(HAD_MORALE);
  575. }
  576. void BattleResult::applyGs( CGameState *gs )
  577. {
  578. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  579. delete gs->curB->stacks[i];
  580. //remove any "until next battle" bonuses
  581. CGHeroInstance *h;
  582. h = gs->getHero(gs->curB->hero1);
  583. if(h)
  584. h->bonuses.remove_if(HeroBonus::OneBattle);
  585. h = gs->getHero(gs->curB->hero2);
  586. if(h)
  587. h->bonuses.remove_if(HeroBonus::OneBattle);
  588. delete gs->curB;
  589. gs->curB = NULL;
  590. }
  591. void BattleStackMoved::applyGs( CGameState *gs )
  592. {
  593. gs->curB->getStack(stack)->position = tile;
  594. }
  595. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  596. {
  597. CStack * at = gs->curB->getStack(stackAttacked);
  598. at->amount = newAmount;
  599. at->firstHPleft = newHP;
  600. if(killed())
  601. at->state -= ALIVE;
  602. }
  603. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  604. {
  605. CStack *attacker = gs->curB->getStack(stackAttacking);
  606. if(counter())
  607. attacker->counterAttacks--;
  608. if(shot())
  609. attacker->shots--;
  610. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  611. stackAttacked.applyGs(gs);
  612. for(int g=0; g<attacker->features.size(); ++g)
  613. {
  614. if((attacker->features[g].duration & StackFeature::UNTIL_ATTACK) != 0)
  615. {
  616. attacker->features.erase(attacker->features.begin() + g);
  617. g = 0;
  618. }
  619. }
  620. for(std::set<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  621. {
  622. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  623. for(int g=0; g<stack->features.size(); ++g)
  624. {
  625. if((stack->features[g].duration & StackFeature::UNITL_BEING_ATTACKED) != 0)
  626. {
  627. stack->features.erase(stack->features.begin() + g);
  628. g = 0;
  629. }
  630. }
  631. }
  632. }
  633. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  634. {
  635. CStack *st = gs->curB->getStack(ba.stackNumber);
  636. switch(ba.actionType)
  637. {
  638. case 3:
  639. st->state.insert(DEFENDING);
  640. break;
  641. case 8:
  642. st->state.insert(WAITING);
  643. break;
  644. case 2: case 6: case 7: case 9: case 10: case 11:
  645. st->state.insert(MOVED);
  646. break;
  647. }
  648. }
  649. DLL_EXPORT void SpellCast::applyGs( CGameState *gs )
  650. {
  651. CGHeroInstance *h = (side) ? gs->getHero(gs->curB->hero2) : gs->getHero(gs->curB->hero1);
  652. if(h)
  653. {
  654. int spellCost = 0;
  655. if(gs->curB)
  656. {
  657. spellCost = gs->curB->getSpellCost(&VLC->spellh->spells[id], h);
  658. }
  659. else
  660. {
  661. spellCost = VLC->spellh->spells[id].costs[skill];
  662. }
  663. h->mana -= spellCost;
  664. if(h->mana < 0) h->mana = 0;
  665. }
  666. if(side >= 0 && side < 2)
  667. {
  668. gs->curB->castSpells[side]++;
  669. }
  670. if(gs->curB && id == 35) //dispel
  671. {
  672. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  673. {
  674. CStack *s = gs->curB->getStack(*it);
  675. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  676. {
  677. s->effects.clear(); //removing all effects
  678. //removing all features from spells
  679. std::vector<StackFeature> tmpFeatures = s->features;
  680. s->features.clear();
  681. for(int i=0; i < tmpFeatures.size(); i++)
  682. {
  683. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT)
  684. {
  685. s->features.push_back(tmpFeatures[i]);
  686. }
  687. }
  688. }
  689. }
  690. }
  691. //elemental summoning
  692. if(id >= 66 && id <= 69)
  693. {
  694. int creID;
  695. switch(id)
  696. {
  697. case 66:
  698. creID = 114; //fire elemental
  699. break;
  700. case 67:
  701. creID = 113; //earth elemental
  702. break;
  703. case 68:
  704. creID = 115; //water elemental
  705. break;
  706. case 69:
  707. creID = 112; //air elemental
  708. break;
  709. }
  710. const int3 & tile = gs->curB->tile;
  711. TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  712. int pos; //position of stack on the battlefield - to be calculated
  713. bool ac[BFIELD_SIZE];
  714. std::set<int> occupyable;
  715. bool twoHex = vstd::contains(VLC->creh->creatures[creID].abilities, StackFeature::DOUBLE_WIDE);
  716. bool flying = vstd::contains(VLC->creh->creatures[creID].abilities, StackFeature::FLYING);
  717. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  718. for(int g=0; g<BFIELD_SIZE; ++g)
  719. {
  720. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  721. {
  722. pos = g;
  723. break;
  724. }
  725. }
  726. CStack * summonedStack = BattleInfo::generateNewStack(h, creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id].powers[skill], gs->curB->stacks.size(), !side, 255, ter, pos);
  727. summonedStack->features.push_back( makeFeature(StackFeature::SUMMONED, StackFeature::WHOLE_BATTLE, 0, 0, StackFeature::BONUS_FROM_HERO) );
  728. gs->curB->stacks.push_back(summonedStack);
  729. }
  730. }
  731. static inline StackFeature featureGenerator(StackFeature::ECombatFeatures type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0)
  732. {
  733. return makeFeature(type, StackFeature::N_TURNS, subtype, value, StackFeature::SPELL_EFFECT, turnsRemain, additionalInfo);
  734. }
  735. static std::vector<StackFeature> stackEffectToFeature(const CStack::StackEffect & sse)
  736. {
  737. std::vector<StackFeature> sf;
  738. switch(sse.id)
  739. {
  740. case 27: //shield
  741. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  742. break;
  743. case 28: //air shield
  744. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  745. break;
  746. case 30: //protection from air
  747. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  748. break;
  749. case 31: //protection from fire
  750. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  751. break;
  752. case 32: //protection from water
  753. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 2, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  754. break;
  755. case 33: //protection from earth
  756. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 3, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  757. break;
  758. case 34: //anti-magic
  759. sf.push_back(featureGenerator(StackFeature::LEVEL_SPELL_IMMUNITY, 0, VLC->spellh->spells[sse.id].powers[sse.level] - 1, sse.turnsRemain));
  760. break;
  761. case 41: //bless
  762. sf.push_back(featureGenerator(StackFeature::ALWAYS_MAXIMUM_DAMAGE, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  763. break;
  764. case 42: //curse
  765. 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));
  766. break;
  767. case 43: //bloodlust
  768. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  769. break;
  770. case 44: //precision
  771. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  772. break;
  773. case 45: //weakness
  774. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  775. break;
  776. case 46: //stone skin
  777. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  778. break;
  779. case 47: //disrupting ray
  780. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  781. break;
  782. case 48: //prayer
  783. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  784. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  785. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  786. break;
  787. case 49: //mirth
  788. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  789. break;
  790. case 50: //sorrow
  791. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  792. break;
  793. case 51: //fortune
  794. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  795. break;
  796. case 52: //misfortune
  797. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  798. break;
  799. case 53: //haste
  800. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  801. break;
  802. case 54: //slow
  803. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, 0, sse.turnsRemain, -1 * VLC->spellh->spells[sse.id].powers[sse.level]));
  804. break;
  805. case 55: //slayer
  806. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  807. break;
  808. case 56: //frenzy
  809. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  810. break;
  811. case 58: //counterstrike
  812. sf.push_back(featureGenerator(StackFeature::ADDITIONAL_RETALIATION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  813. break;
  814. case 60: //hypnotize
  815. sf.push_back(featureGenerator(StackFeature::HYPNOTIZED, 0, sse.level, sse.turnsRemain));
  816. break;
  817. case 61: //forgetfulness
  818. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  819. break;
  820. case 62: //blind
  821. sf.push_back(makeFeature(StackFeature::NOT_ACTIVE, StackFeature::UNITL_BEING_ATTACKED | StackFeature::N_TURNS, 0, 0, StackFeature::SPELL_EFFECT, sse.turnsRemain, 0));
  822. 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));
  823. break;
  824. }
  825. //setting positiveness
  826. for(int g=0; g<sf.size(); ++g)
  827. {
  828. sf[g].positiveness = VLC->spellh->spells[sse.id].positiveness;
  829. }
  830. return sf;
  831. }
  832. void actualizeEffect(CStack * s, CStack::StackEffect & ef)
  833. {
  834. //actualizing effects vector
  835. for(int g=0; g<s->effects.size(); ++g)
  836. {
  837. if(s->effects[g].id == ef.id)
  838. {
  839. s->effects[g].turnsRemain = std::max(s->effects[g].turnsRemain, ef.turnsRemain);
  840. }
  841. }
  842. //actualizing features vector
  843. std::vector<StackFeature> sf = stackEffectToFeature(ef);
  844. for(int b=0; b<sf.size(); ++b)
  845. {
  846. for(int g=0; g<s->features.size(); ++g)
  847. {
  848. if(s->features[g].source == StackFeature::SPELL_EFFECT && s->features[g].type == sf[b].type && s->features[g].subtype == sf[b].subtype)
  849. {
  850. s->features[g].turnsRemain = std::max(s->features[g].turnsRemain, ef.turnsRemain);
  851. }
  852. }
  853. }
  854. }
  855. bool containsEff(const std::vector<CStack::StackEffect> & vec, int effectId)
  856. {
  857. for(int g=0; g<vec.size(); ++g)
  858. {
  859. if(vec[g].id == effectId)
  860. return true;
  861. }
  862. return false;
  863. }
  864. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  865. {
  866. BOOST_FOREACH(ui32 id, stacks)
  867. {
  868. CStack *s = gs->curB->getStack(id);
  869. if(s)
  870. {
  871. if(effect.id == 42 || !containsEff(s->effects, effect.id))//disrupting ray or not on the list - just add
  872. {
  873. s->effects.push_back(effect);
  874. std::vector<StackFeature> sf = stackEffectToFeature(effect);
  875. for(int n=0; n<sf.size(); ++n)
  876. {
  877. s->features.push_back(sf[n]);
  878. }
  879. }
  880. else //just actualize
  881. {
  882. actualizeEffect(s, effect);
  883. }
  884. }
  885. else
  886. tlog1 << "Cannot find stack " << id << std::endl;
  887. }
  888. }
  889. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  890. {
  891. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  892. stackAttacked.applyGs(gs);
  893. }
  894. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  895. {
  896. for(int g=0; g<healedStacks.size(); ++g)
  897. {
  898. CStack * changedStack = gs->curB->stacks[healedStacks[g].stackID];
  899. //checking if we resurrect a stack that is under a living stack
  900. std::vector<int> access = gs->curB->getAccessibility(changedStack->ID, true);
  901. bool acc[BFIELD_SIZE];
  902. for(int h=0; h<BFIELD_SIZE; ++h)
  903. acc[h] = false;
  904. for(int h=0; h<access.size(); ++h)
  905. acc[access[h]] = true;
  906. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  907. changedStack->hasFeatureOfType(StackFeature::DOUBLE_WIDE), changedStack->attackerOwned,
  908. changedStack->hasFeatureOfType(StackFeature::FLYING), true))
  909. return; //position is already occupied
  910. //applying changes
  911. if(!changedStack->alive())
  912. {
  913. changedStack->state.insert(ALIVE);
  914. }
  915. int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  916. changedStack->amount += healedStacks[g].healedHP / changedStack->MaxHealth();
  917. changedStack->firstHPleft += healedStacks[g].healedHP - changedStack->amount * changedStack->MaxHealth();
  918. if(changedStack->firstHPleft > changedStack->MaxHealth())
  919. {
  920. changedStack->firstHPleft -= changedStack->MaxHealth();
  921. changedStack->amount += 1;
  922. }
  923. //removal of negative effects
  924. {
  925. for(int h=0; h<changedStack->effects.size(); ++h)
  926. {
  927. if(VLC->spellh->spells[changedStack->effects[h].id].positiveness < 0)
  928. {
  929. changedStack->effects.erase(changedStack->effects.begin() + h);
  930. }
  931. }
  932. //removing all features from negative spells
  933. std::vector<StackFeature> tmpFeatures = changedStack->features;
  934. changedStack->features.clear();
  935. for(int i=0; i < tmpFeatures.size(); i++)
  936. {
  937. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT || tmpFeatures[i].positiveness >= 0)
  938. {
  939. changedStack->features.push_back(tmpFeatures[i]);
  940. }
  941. }
  942. }
  943. }
  944. }
  945. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  946. {
  947. if(gs->curB) //if there is a battle
  948. {
  949. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  950. {
  951. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  952. {
  953. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  954. {
  955. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  956. break;
  957. }
  958. }
  959. }
  960. }
  961. }
  962. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  963. {
  964. gs->currentPlayer = player;
  965. }
  966. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  967. {
  968. gs->getPlayer(player)->currentSelection = id;
  969. }