NetPacksLib.cpp 32 KB

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