NetPacksLib.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  412. CGTownInstance *t = gs->getTown(tid);
  413. h->setOwner(player);
  414. h->pos = tile;
  415. h->movement = h->maxMovePoints(true);
  416. gs->hpool.heroesPool.erase(hid);
  417. if(h->id < 0)
  418. {
  419. h->id = gs->map->objects.size();
  420. gs->map->objects.push_back(h);
  421. }
  422. else
  423. gs->map->objects[h->id] = h;
  424. h->initHeroDefInfo();
  425. gs->map->heroes.push_back(h);
  426. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  427. h->initObj();
  428. gs->map->addBlockVisTiles(h);
  429. t->visitingHero = h;
  430. h->visitedTown = t;
  431. h->inTownGarrison = false;
  432. }
  433. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  434. {
  435. CGHeroInstance *h = gs->getHero(id);
  436. gs->map->removeBlockVisTiles(h,true);
  437. h->setOwner(player);
  438. h->movement = h->maxMovePoints(true);
  439. h->initHeroDefInfo();
  440. gs->map->heroes.push_back(h);
  441. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  442. gs->map->addBlockVisTiles(h);
  443. h->inTownGarrison = false;
  444. }
  445. DLL_EXPORT void NewObject::applyGs( CGameState *gs )
  446. {
  447. CGObjectInstance *o = NULL;
  448. switch(ID)
  449. {
  450. case 8:
  451. o = new CGBoat();
  452. break;
  453. default:
  454. o = new CGObjectInstance();
  455. break;
  456. }
  457. o->ID = ID;
  458. o->subID = subID;
  459. o->pos = pos;
  460. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  461. id = o->id = gs->map->objects.size();
  462. o->hoverName = VLC->generaltexth->names[ID];
  463. gs->map->objects.push_back(o);
  464. gs->map->addBlockVisTiles(o);
  465. o->initObj();
  466. assert(o->defInfo);
  467. }
  468. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  469. {
  470. gs->day = day;
  471. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  472. {
  473. CGHeroInstance *hero = gs->getHero(h.id);
  474. hero->movement = h.move;
  475. hero->mana = h.mana;
  476. }
  477. BOOST_FOREACH(SetResources h, res) //give resources
  478. h.applyGs(gs);
  479. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  480. h.applyGs(gs);
  481. if(resetBuilded) //reset amount of structures set in this turn in towns
  482. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  483. t->builded = 0;
  484. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  485. h->bonuses.remove_if(HeroBonus::OneDay);
  486. if(gs->getDate(1) == 7) //new week
  487. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  488. h->bonuses.remove_if(HeroBonus::OneWeek);
  489. }
  490. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  491. {
  492. CGObjectInstance *obj = gs->map->objects[id];
  493. if(!obj)
  494. tlog1 << "Wrong object ID - property cannot be set!\n";
  495. else
  496. obj->setProperty(what,val);
  497. }
  498. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  499. {
  500. name.toString(gs->map->objects[id]->hoverName);
  501. }
  502. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  503. {
  504. gs->getHero(heroid)->level = level;
  505. }
  506. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  507. {
  508. gs->curB = info;
  509. }
  510. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  511. {
  512. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  513. gs->curB->round = round;
  514. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  515. {
  516. s->state -= DEFENDING;
  517. s->state -= WAITING;
  518. s->state -= MOVED;
  519. s->state -= HAD_MORALE;
  520. s->counterAttacks = 1;
  521. //remove effects and restore only those with remaining turns in duration
  522. std::vector<CStack::StackEffect> tmpEffects = s->effects;
  523. s->effects.clear();
  524. for(int i=0; i < tmpEffects.size(); i++)
  525. {
  526. tmpEffects[i].turnsRemain--;
  527. if(tmpEffects[i].turnsRemain > 0)
  528. s->effects.push_back(tmpEffects[i]);
  529. }
  530. //the same as above for features
  531. std::vector<StackFeature> tmpFeatures = s->features;
  532. s->features.clear();
  533. for(int i=0; i < tmpFeatures.size(); i++)
  534. {
  535. if(tmpFeatures[i].duration == StackFeature::N_TURNS)
  536. {
  537. tmpFeatures[i].turnsRemain--;
  538. if(tmpFeatures[i].turnsRemain > 0)
  539. s->features.push_back(tmpFeatures[i]);
  540. }
  541. else
  542. {
  543. s->features.push_back(tmpFeatures[i]);
  544. }
  545. }
  546. }
  547. }
  548. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  549. {
  550. gs->curB->activeStack = stack;
  551. CStack *st = gs->curB->getStack(stack);
  552. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  553. st->state.insert(HAD_MORALE);
  554. }
  555. void BattleResult::applyGs( CGameState *gs )
  556. {
  557. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  558. delete gs->curB->stacks[i];
  559. //remove any "until next battle" bonuses
  560. CGHeroInstance *h;
  561. h = gs->getHero(gs->curB->hero1);
  562. if(h)
  563. h->bonuses.remove_if(HeroBonus::OneBattle);
  564. h = gs->getHero(gs->curB->hero2);
  565. if(h)
  566. h->bonuses.remove_if(HeroBonus::OneBattle);
  567. delete gs->curB;
  568. gs->curB = NULL;
  569. }
  570. void BattleStackMoved::applyGs( CGameState *gs )
  571. {
  572. gs->curB->getStack(stack)->position = tile;
  573. }
  574. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  575. {
  576. CStack * at = gs->curB->getStack(stackAttacked);
  577. at->amount = newAmount;
  578. at->firstHPleft = newHP;
  579. if(killed())
  580. at->state -= ALIVE;
  581. }
  582. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  583. {
  584. CStack *attacker = gs->curB->getStack(stackAttacking);
  585. if(counter())
  586. attacker->counterAttacks--;
  587. if(shot())
  588. attacker->shots--;
  589. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  590. stackAttacked.applyGs(gs);
  591. }
  592. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  593. {
  594. CStack *st = gs->curB->getStack(ba.stackNumber);
  595. switch(ba.actionType)
  596. {
  597. case 3:
  598. st->state.insert(DEFENDING);
  599. break;
  600. case 8:
  601. st->state.insert(WAITING);
  602. break;
  603. case 2: case 6: case 7: case 9: case 10: case 11:
  604. st->state.insert(MOVED);
  605. break;
  606. }
  607. }
  608. DLL_EXPORT void SpellCast::applyGs( CGameState *gs )
  609. {
  610. CGHeroInstance *h = (side) ? gs->getHero(gs->curB->hero2) : gs->getHero(gs->curB->hero1);
  611. if(h)
  612. {
  613. h->mana -= VLC->spellh->spells[id].costs[skill];
  614. if(h->mana < 0) h->mana = 0;
  615. }
  616. if(side >= 0 && side < 2)
  617. {
  618. gs->curB->castSpells[side]++;
  619. }
  620. if(gs->curB && id == 35) //dispel
  621. {
  622. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  623. {
  624. CStack *s = gs->curB->getStack(*it);
  625. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  626. {
  627. s->effects.clear(); //removing all effects
  628. //removing all features from spells
  629. std::vector<StackFeature> tmpFeatures = s->features;
  630. s->features.clear();
  631. for(int i=0; i < tmpFeatures.size(); i++)
  632. {
  633. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT)
  634. {
  635. s->features.push_back(tmpFeatures[i]);
  636. }
  637. }
  638. }
  639. }
  640. }
  641. }
  642. static inline StackFeature featureGenerator(StackFeature::ECombatFeatures type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0)
  643. {
  644. return makeFeature(type, StackFeature::N_TURNS, subtype, value, StackFeature::SPELL_EFFECT, turnsRemain, additionalInfo);
  645. }
  646. static std::vector<StackFeature> stackEffectToFeature(const CStack::StackEffect & sse)
  647. {
  648. std::vector<StackFeature> sf;
  649. switch(sse.id)
  650. {
  651. case 27: //shield
  652. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  653. break;
  654. case 28: //air shield
  655. sf.push_back(featureGenerator(StackFeature::GENERAL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  656. break;
  657. case 30: //protection from air
  658. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  659. break;
  660. case 31: //protection from fire
  661. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  662. break;
  663. case 32: //protection from water
  664. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 2, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  665. break;
  666. case 33: //protection from earth
  667. sf.push_back(featureGenerator(StackFeature::SPELL_DAMAGE_REDUCTION, 3, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  668. break;
  669. case 41: //bless
  670. sf.push_back(featureGenerator(StackFeature::ALWAYS_MAXIMUM_DAMAGE, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  671. break;
  672. case 42: //curse
  673. 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));
  674. break;
  675. case 43: //bloodlust
  676. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  677. break;
  678. case 44: //precision
  679. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  680. break;
  681. case 45: //weakness
  682. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  683. break;
  684. case 46: //stone skin
  685. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  686. break;
  687. case 47: //disrupting ray
  688. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, -1, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  689. break;
  690. case 48: //prayer
  691. sf.push_back(featureGenerator(StackFeature::ATTACK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  692. sf.push_back(featureGenerator(StackFeature::DEFENCE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  693. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  694. break;
  695. case 49: //mirth
  696. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  697. break;
  698. case 50: //sorrow
  699. sf.push_back(featureGenerator(StackFeature::MORALE_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  700. break;
  701. case 51: //fortune
  702. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  703. break;
  704. case 52: //misfortune
  705. sf.push_back(featureGenerator(StackFeature::LUCK_BONUS, 0, -1 * VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  706. break;
  707. case 53: //haste
  708. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, VLC->spellh->spells[sse.id].powers[sse.level], sse.turnsRemain));
  709. break;
  710. case 54: //slow
  711. sf.push_back(featureGenerator(StackFeature::SPEED_BONUS, 0, 0, sse.turnsRemain, -1 * VLC->spellh->spells[sse.id].powers[sse.level]));
  712. break;
  713. case 55: //slayer
  714. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  715. break;
  716. case 61: //forgetfulness
  717. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  718. break;
  719. case 56: //frenzy
  720. sf.push_back(featureGenerator(StackFeature::SLAYER, 0, sse.level, sse.turnsRemain));
  721. break;
  722. }
  723. //setting positiveness
  724. for(int g=0; g<sf.size(); ++g)
  725. {
  726. sf[g].positiveness = VLC->spellh->spells[sse.id].positiveness;
  727. }
  728. return sf;
  729. }
  730. void actualizeEffect(CStack * s, CStack::StackEffect & ef)
  731. {
  732. //actualizing effects vector
  733. for(int g=0; g<s->effects.size(); ++g)
  734. {
  735. if(s->effects[g].id == ef.id)
  736. {
  737. s->effects[g].turnsRemain = std::max(s->effects[g].turnsRemain, ef.turnsRemain);
  738. }
  739. }
  740. //actualizing features vector
  741. std::vector<StackFeature> sf = stackEffectToFeature(ef);
  742. for(int b=0; b<sf.size(); ++b)
  743. {
  744. for(int g=0; g<s->features.size(); ++g)
  745. {
  746. if(s->features[g].source == StackFeature::SPELL_EFFECT && s->features[g].type == sf[b].type && s->features[g].subtype == sf[b].subtype)
  747. {
  748. s->features[g].turnsRemain = std::max(s->features[g].turnsRemain, ef.turnsRemain);
  749. }
  750. }
  751. }
  752. }
  753. bool containsEff(const std::vector<CStack::StackEffect> & vec, int effectId)
  754. {
  755. for(int g=0; g<vec.size(); ++g)
  756. {
  757. if(vec[g].id == effectId)
  758. return true;
  759. }
  760. return false;
  761. }
  762. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  763. {
  764. BOOST_FOREACH(ui32 id, stacks)
  765. {
  766. CStack *s = gs->curB->getStack(id);
  767. if(s)
  768. {
  769. if(effect.id == 42 || !containsEff(s->effects, effect.id))//disrupting ray or not on the list - just add
  770. {
  771. s->effects.push_back(effect);
  772. std::vector<StackFeature> sf = stackEffectToFeature(effect);
  773. for(int n=0; n<sf.size(); ++n)
  774. {
  775. s->features.push_back(sf[n]);
  776. }
  777. }
  778. else //just actualize
  779. {
  780. actualizeEffect(s, effect);
  781. }
  782. }
  783. else
  784. tlog1 << "Cannot find stack " << id << std::endl;
  785. }
  786. }
  787. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  788. {
  789. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  790. stackAttacked.applyGs(gs);
  791. }
  792. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  793. {
  794. for(int g=0; g<healedStacks.size(); ++g)
  795. {
  796. CStack * changedStack = gs->curB->stacks[healedStacks[g].stackID];
  797. changedStack->firstHPleft += healedStacks[g].healForFirstStack;
  798. changedStack->amount += healedStacks[g].resurrectedCres;
  799. //removal of negative effects
  800. {
  801. for(int h=0; h<changedStack->effects.size(); ++h)
  802. {
  803. if(VLC->spellh->spells[changedStack->effects[h].id].positiveness < 0)
  804. {
  805. changedStack->effects.erase(changedStack->effects.begin() + h);
  806. }
  807. }
  808. //removing all features from negative spells
  809. std::vector<StackFeature> tmpFeatures = changedStack->features;
  810. changedStack->features.clear();
  811. for(int i=0; i < tmpFeatures.size(); i++)
  812. {
  813. if(tmpFeatures[i].source != StackFeature::SPELL_EFFECT || tmpFeatures[i].positiveness >= 0)
  814. {
  815. changedStack->features.push_back(tmpFeatures[i]);
  816. }
  817. }
  818. }
  819. }
  820. }
  821. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  822. {
  823. gs->currentPlayer = player;
  824. }
  825. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  826. {
  827. gs->getPlayer(player)->currentSelection = id;
  828. }