2
0

NetPacksLib.cpp 32 KB

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