NetPacksLib.cpp 31 KB

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