NetPacksLib.cpp 32 KB

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