NetPacksLib.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  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 "../hch/CCreatureHandler.h"
  12. #include <boost/bind.hpp>
  13. #include <boost/foreach.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #include <boost/algorithm/string/replace.hpp>
  16. #include <boost/thread.hpp>
  17. #include <boost/thread/shared_mutex.hpp>
  18. #undef min
  19. #undef max
  20. /*
  21. * NetPacksLib.cpp, part of VCMI engine
  22. *
  23. * Authors: listed in file AUTHORS in main folder
  24. *
  25. * License: GNU General Public License v2.0 or later
  26. * Full text of license available in license.txt file, in main folder
  27. *
  28. */
  29. #ifdef min
  30. #undef min
  31. #endif
  32. #ifdef max
  33. #undef max
  34. #endif
  35. DLL_EXPORT void SetResource::applyGs( CGameState *gs )
  36. {
  37. assert(player < PLAYER_LIMIT);
  38. amax(val, 0); //new value must be >= 0
  39. gs->getPlayer(player)->resources[resid] = val;
  40. }
  41. DLL_EXPORT void SetResources::applyGs( CGameState *gs )
  42. {
  43. assert(player < PLAYER_LIMIT);
  44. for(int i=0;i<res.size();i++)
  45. gs->getPlayer(player)->resources[i] = res[i];
  46. }
  47. DLL_EXPORT void SetPrimSkill::applyGs( CGameState *gs )
  48. {
  49. CGHeroInstance *hero = gs->getHero(id);
  50. assert(hero);
  51. if(which <4)
  52. {
  53. Bonus *skill = hero->getBonus(Selector::type(Bonus::PRIMARY_SKILL) && Selector::subtype(which) && Selector::sourceType(Bonus::HERO_BASE_SKILL));
  54. assert(skill);
  55. if(abs)
  56. skill->val = val;
  57. else
  58. skill->val += val;
  59. }
  60. else if(which == 4) //XP
  61. {
  62. if(abs)
  63. hero->exp = val;
  64. else
  65. hero->exp += val;
  66. }
  67. }
  68. DLL_EXPORT void SetSecSkill::applyGs( CGameState *gs )
  69. {
  70. CGHeroInstance *hero = gs->getHero(id);
  71. hero->setSecSkillLevel(which, val, abs);
  72. }
  73. DLL_EXPORT void HeroVisitCastle::applyGs( CGameState *gs )
  74. {
  75. CGHeroInstance *h = gs->getHero(hid);
  76. CGTownInstance *t = gs->getTown(tid);
  77. if(start())
  78. {
  79. if(garrison())
  80. {
  81. t->garrisonHero = h;
  82. h->visitedTown = t;
  83. h->inTownGarrison = true;
  84. }
  85. else
  86. {
  87. t->visitingHero = h;
  88. h->visitedTown = t;
  89. h->inTownGarrison = false;
  90. }
  91. }
  92. else
  93. {
  94. if(garrison())
  95. {
  96. t->garrisonHero = NULL;
  97. h->visitedTown = NULL;
  98. h->inTownGarrison = false;
  99. }
  100. else
  101. {
  102. t->visitingHero = NULL;
  103. h->visitedTown = NULL;
  104. h->inTownGarrison = false;
  105. }
  106. }
  107. }
  108. DLL_EXPORT void ChangeSpells::applyGs( CGameState *gs )
  109. {
  110. CGHeroInstance *hero = gs->getHero(hid);
  111. if(learn)
  112. BOOST_FOREACH(ui32 sid, spells)
  113. hero->spells.insert(sid);
  114. else
  115. BOOST_FOREACH(ui32 sid, spells)
  116. hero->spells.erase(sid);
  117. }
  118. DLL_EXPORT void SetMana::applyGs( CGameState *gs )
  119. {
  120. CGHeroInstance *hero = gs->getHero(hid);
  121. amax(val, 0); //not less than 0
  122. hero->mana = val;
  123. }
  124. DLL_EXPORT void SetMovePoints::applyGs( CGameState *gs )
  125. {
  126. CGHeroInstance *hero = gs->getHero(hid);
  127. hero->movement = val;
  128. }
  129. DLL_EXPORT void FoWChange::applyGs( CGameState *gs )
  130. {
  131. TeamState * team = gs->getPlayerTeam(player);
  132. BOOST_FOREACH(int3 t, tiles)
  133. team->fogOfWarMap[t.x][t.y][t.z] = mode;
  134. if (mode == 0) //do not hide too much
  135. {
  136. std::set<int3> tilesRevealed;
  137. for (size_t i = 0; i < gs->map->objects.size(); i++)
  138. {
  139. if (gs->map->objects[i])
  140. {
  141. switch(gs->map->objects[i]->ID)
  142. {
  143. case 34://hero
  144. case 53://mine
  145. case 98://town
  146. case 220:
  147. if(vstd::contains(team->players, gs->map->objects[i]->tempOwner)) //check owned observators
  148. gs->map->objects[i]->getSightTiles(tilesRevealed);
  149. break;
  150. }
  151. }
  152. }
  153. BOOST_FOREACH(int3 t, tilesRevealed) //probably not the most optimal solution ever
  154. team->fogOfWarMap[t.x][t.y][t.z] = 1;
  155. }
  156. }
  157. DLL_EXPORT void SetAvailableHeroes::applyGs( CGameState *gs )
  158. {
  159. PlayerState *p = gs->getPlayer(player);
  160. p->availableHeroes.clear();
  161. for (int i = 0; i < AVAILABLE_HEROES_PER_PLAYER; i++)
  162. {
  163. CGHeroInstance *h = (hid[i]>=0 ? gs->hpool.heroesPool[hid[i]] : NULL);
  164. if(h && army[i])
  165. h->setArmy(*army[i]);
  166. p->availableHeroes.push_back(h);
  167. }
  168. }
  169. DLL_EXPORT void GiveBonus::applyGs( CGameState *gs )
  170. {
  171. BonusList *bonuses = NULL;
  172. switch(who)
  173. {
  174. case HERO:
  175. {
  176. CGHeroInstance *h = gs->getHero(id);
  177. assert(h);
  178. bonuses = &h->bonuses;
  179. }
  180. break;
  181. case PLAYER:
  182. {
  183. PlayerState *p = gs->getPlayer(id);
  184. assert(p);
  185. bonuses = &p->bonuses;
  186. }
  187. break;
  188. case TOWN:
  189. {
  190. CGTownInstance *t = gs->getTown(id);
  191. assert(t);
  192. bonuses = &t->bonuses;
  193. }
  194. }
  195. bonuses->push_back(bonus);
  196. std::string &descr = bonuses->back().description;
  197. if(!bdescr.message.size()
  198. && bonus.source == Bonus::OBJECT
  199. && (bonus.type == Bonus::LUCK || bonus.type == Bonus::MORALE)
  200. && gs->map->objects[bonus.id]->ID == EVENTI_TYPE) //it's morale/luck bonus from an event without description
  201. {
  202. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  203. boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val)));
  204. }
  205. else
  206. {
  207. bdescr.toString(descr);
  208. }
  209. }
  210. DLL_EXPORT void ChangeObjPos::applyGs( CGameState *gs )
  211. {
  212. CGObjectInstance *obj = gs->map->objects[objid];
  213. if(!obj)
  214. {
  215. tlog1 << "Wrong ChangeObjPos: object " << objid << " doesn't exist!\n";
  216. return;
  217. }
  218. gs->map->removeBlockVisTiles(obj);
  219. obj->pos = nPos;
  220. gs->map->addBlockVisTiles(obj);
  221. }
  222. DLL_EXPORT void PlayerEndsGame::applyGs( CGameState *gs )
  223. {
  224. PlayerState *p = gs->getPlayer(player);
  225. p->status = victory ? 2 : 1;
  226. }
  227. DLL_EXPORT void RemoveBonus::applyGs( CGameState *gs )
  228. {
  229. std::list<Bonus> &bonuses = (who == HERO ? gs->getHero(whoID)->bonuses : gs->getPlayer(whoID)->bonuses);
  230. for(std::list<Bonus>::iterator i = bonuses.begin(); i != bonuses.end(); i++)
  231. {
  232. if(i->source == source && i->id == id)
  233. {
  234. bonus = *i; //backup bonus (to show to interfaces later)
  235. bonuses.erase(i);
  236. break;
  237. }
  238. }
  239. }
  240. DLL_EXPORT void RemoveObject::applyGs( CGameState *gs )
  241. {
  242. CGObjectInstance *obj = gs->map->objects[id];
  243. //unblock tiles
  244. if(obj->defInfo)
  245. {
  246. gs->map->removeBlockVisTiles(obj);
  247. }
  248. if(obj->ID==HEROI_TYPE)
  249. {
  250. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  251. std::vector<CGHeroInstance*>::iterator nitr = std::find(gs->map->heroes.begin(), gs->map->heroes.end(),h);
  252. gs->map->heroes.erase(nitr);
  253. int player = h->tempOwner;
  254. nitr = std::find(gs->getPlayer(player)->heroes.begin(), gs->getPlayer(player)->heroes.end(), h);
  255. gs->getPlayer(player)->heroes.erase(nitr);
  256. h->tempOwner = 255; //no one owns beaten hero
  257. if(CGTownInstance *t = const_cast<CGTownInstance *>(h->visitedTown))
  258. {
  259. if(h->inTownGarrison)
  260. t->garrisonHero = NULL;
  261. else
  262. t->visitingHero = NULL;
  263. h->visitedTown = NULL;
  264. }
  265. //return hero to the pool, so he may reappear in tavern
  266. gs->hpool.heroesPool[h->subID] = h;
  267. if(!vstd::contains(gs->hpool.pavailable, h->subID))
  268. gs->hpool.pavailable[h->subID] = 0xff;
  269. }
  270. else if (obj->ID==CREI_TYPE && gs->map->version > CMapHeader::RoE) //only fixed monsters can be a part of quest
  271. {
  272. CGCreature *cre = static_cast<CGCreature*>(obj);
  273. gs->map->monsters[cre->identifier]->pos = int3 (-1,-1,-1); //use nonexistent monster for quest :>
  274. }
  275. gs->map->objects[id] = NULL;
  276. }
  277. static int getDir(int3 src, int3 dst)
  278. {
  279. int ret = -1;
  280. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  281. {
  282. ret = 1;
  283. }
  284. else if(dst.x == src.x && dst.y+1 == src.y) //t
  285. {
  286. ret = 2;
  287. }
  288. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  289. {
  290. ret = 3;
  291. }
  292. else if(dst.x-1 == src.x && dst.y == src.y) //r
  293. {
  294. ret = 4;
  295. }
  296. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  297. {
  298. ret = 5;
  299. }
  300. else if(dst.x == src.x && dst.y-1 == src.y) //b
  301. {
  302. ret = 6;
  303. }
  304. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  305. {
  306. ret = 7;
  307. }
  308. else if(dst.x+1 == src.x && dst.y == src.y) //l
  309. {
  310. ret = 8;
  311. }
  312. return ret;
  313. }
  314. void TryMoveHero::applyGs( CGameState *gs )
  315. {
  316. CGHeroInstance *h = gs->getHero(id);
  317. h->movement = movePoints;
  318. if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end) {
  319. h->moveDir = getDir(start,end);
  320. }
  321. if(result == EMBARK) //hero enters boat at dest tile
  322. {
  323. const TerrainTile &tt = gs->map->getTile(CGHeroInstance::convertPosition(end, false));
  324. assert(tt.visitableObjects.size() == 1 && tt.visitableObjects.front()->ID == 8); //the only vis obj at dest is Boat
  325. CGBoat *boat = static_cast<CGBoat*>(tt.visitableObjects.front());
  326. gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
  327. h->boat = boat;
  328. boat->hero = h;
  329. }
  330. else if(result == DISEMBARK) //hero leaves boat to dest tile
  331. {
  332. CGBoat *b = const_cast<CGBoat *>(h->boat);
  333. b->direction = h->moveDir;
  334. b->pos = start;
  335. b->hero = NULL;
  336. gs->map->addBlockVisTiles(b);
  337. h->boat = NULL;
  338. }
  339. if(start!=end && (result == SUCCESS || result == TELEPORTATION || result == EMBARK || result == DISEMBARK))
  340. {
  341. gs->map->removeBlockVisTiles(h);
  342. h->pos = end;
  343. if(CGBoat *b = const_cast<CGBoat *>(h->boat))
  344. b->pos = end;
  345. gs->map->addBlockVisTiles(h);
  346. }
  347. BOOST_FOREACH(int3 t, fowRevealed)
  348. gs->getPlayerTeam(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
  349. }
  350. DLL_EXPORT void SetGarrisons::applyGs( CGameState *gs )
  351. {
  352. for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  353. {
  354. CArmedInstance *ai = static_cast<CArmedInstance*>(gs->map->objects[i->first]);
  355. ai->setArmy(i->second);
  356. if(ai->ID==TOWNI_TYPE && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  357. const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->setArmy(i->second);
  358. else if(ai->ID==HEROI_TYPE)
  359. {
  360. CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  361. CGTownInstance *t = const_cast<CGTownInstance *>(h->visitedTown);
  362. if(t && h->inTownGarrison)
  363. t->setArmy(i->second);
  364. }
  365. }
  366. }
  367. DLL_EXPORT void NewStructures::applyGs( CGameState *gs )
  368. {
  369. CGTownInstance *t = gs->getTown(tid);
  370. BOOST_FOREACH(si32 id,bid)
  371. {
  372. t->builtBuildings.insert(id);
  373. }
  374. t->builded = builded;
  375. }
  376. DLL_EXPORT void RazeStructures::applyGs( CGameState *gs )
  377. {
  378. CGTownInstance *t = gs->getTown(tid);
  379. BOOST_FOREACH(si32 id,bid)
  380. {
  381. t->builtBuildings.erase(id);
  382. }
  383. t->destroyed = destroyed; //yeaha
  384. }
  385. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  386. {
  387. CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->map->objects[tid]);
  388. assert(dw);
  389. dw->creatures = creatures;
  390. }
  391. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  392. {
  393. CGTownInstance *t = gs->getTown(tid);
  394. CGHeroInstance *v = gs->getHero(visiting),
  395. *g = gs->getHero(garrison);
  396. t->visitingHero = v;
  397. t->garrisonHero = g;
  398. if(v)
  399. {
  400. v->visitedTown = t;
  401. v->inTownGarrison = false;
  402. gs->map->addBlockVisTiles(v);
  403. }
  404. if(g)
  405. {
  406. g->visitedTown = t;
  407. g->inTownGarrison = true;
  408. gs->map->removeBlockVisTiles(g);
  409. }
  410. }
  411. DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  412. {
  413. CGHeroInstance *h = gs->getHero(hid);
  414. for(std::map<ui16,ui32>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
  415. if(!vstd::contains(artifWorn,i->first) || artifWorn[i->first] != i->second)
  416. unequiped.push_back(i->second);
  417. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  418. if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  419. equiped.push_back(i->second);
  420. //update hero data
  421. h->artifacts = artifacts;
  422. h->artifWorn = artifWorn;
  423. }
  424. DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, int art)
  425. {
  426. if(art < 0)
  427. {
  428. if(pos<19)
  429. VLC->arth->unequipArtifact(artifWorn, pos);
  430. else if (pos - 19 < artifacts.size())
  431. artifacts.erase(artifacts.begin() + (pos - 19));
  432. }
  433. else
  434. {
  435. if (pos < 19)
  436. {
  437. VLC->arth->equipArtifact(artifWorn, pos, (ui32) art);
  438. }
  439. else // Goes into the backpack.
  440. {
  441. if(pos - 19 < artifacts.size())
  442. artifacts.insert(artifacts.begin() + (pos - 19), art);
  443. else
  444. artifacts.push_back(art);
  445. }
  446. }
  447. }
  448. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  449. {
  450. assert(vstd::contains(gs->hpool.heroesPool, hid));
  451. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  452. CGTownInstance *t = gs->getTown(tid);
  453. h->setOwner(player);
  454. h->pos = tile;
  455. h->movement = h->maxMovePoints(true);
  456. gs->hpool.heroesPool.erase(hid);
  457. if(h->id < 0)
  458. {
  459. h->id = gs->map->objects.size();
  460. gs->map->objects.push_back(h);
  461. }
  462. else
  463. gs->map->objects[h->id] = h;
  464. h->initHeroDefInfo();
  465. gs->map->heroes.push_back(h);
  466. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  467. h->initObj();
  468. gs->map->addBlockVisTiles(h);
  469. if(t)
  470. {
  471. t->visitingHero = h;
  472. h->visitedTown = t;
  473. }
  474. h->inTownGarrison = false;
  475. }
  476. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  477. {
  478. CGHeroInstance *h = gs->getHero(id);
  479. gs->map->removeBlockVisTiles(h,true);
  480. h->setOwner(player);
  481. h->movement = h->maxMovePoints(true);
  482. h->initHeroDefInfo();
  483. gs->map->heroes.push_back(h);
  484. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  485. gs->map->addBlockVisTiles(h);
  486. h->inTownGarrison = false;
  487. }
  488. DLL_EXPORT void NewObject::applyGs( CGameState *gs )
  489. {
  490. CGObjectInstance *o = NULL;
  491. switch(ID)
  492. {
  493. case 8:
  494. o = new CGBoat();
  495. break;
  496. case 54: //probably more options will be needed
  497. o = new CGCreature();
  498. {
  499. CStackInstance hlp;
  500. CGCreature *cre = static_cast<CGCreature*>(o);
  501. cre->slots[0] = hlp;
  502. cre->notGrowingTeam = cre->neverFlees = 0;
  503. cre->character = 2;
  504. cre->gainedArtifact = -1;
  505. }
  506. break;
  507. default:
  508. o = new CGObjectInstance();
  509. break;
  510. }
  511. o->ID = ID;
  512. o->subID = subID;
  513. o->pos = pos;
  514. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  515. id = o->id = gs->map->objects.size();
  516. o->hoverName = VLC->generaltexth->names[ID];
  517. switch(ID)
  518. {
  519. case 54: //cfreature
  520. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  521. assert(o->defInfo);
  522. break;
  523. case 124://hole
  524. const TerrainTile &t = gs->map->getTile(pos);
  525. o->defInfo = VLC->dobjinfo->gobjs[ID][t.tertype];
  526. assert(o->defInfo);
  527. break;
  528. }
  529. gs->map->objects.push_back(o);
  530. gs->map->addBlockVisTiles(o);
  531. o->initObj();
  532. assert(o->defInfo);
  533. }
  534. DLL_EXPORT void SetAvailableArtifacts::applyGs( CGameState *gs )
  535. {
  536. if(id >= 0)
  537. {
  538. if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id]))
  539. {
  540. bm->artifacts = arts;
  541. }
  542. else
  543. {
  544. tlog1 << "Wrong black market id!" << std::endl;
  545. }
  546. }
  547. else
  548. {
  549. CGTownInstance::merchantArtifacts = arts;
  550. }
  551. }
  552. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  553. {
  554. gs->day = day;
  555. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  556. {
  557. CGHeroInstance *hero = gs->getHero(h.id);
  558. hero->movement = h.move;
  559. hero->mana = h.mana;
  560. }
  561. for(std::map<ui8, std::vector<si32> >::iterator i = res.begin(); i != res.end(); i++)
  562. {
  563. assert(i->first < PLAYER_LIMIT);
  564. std::vector<si32> &playerRes = gs->getPlayer(i->first)->resources;
  565. for(int j = 0; j < i->second.size(); j++)
  566. playerRes[j] = i->second[j];
  567. }
  568. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  569. h.applyGs(gs);
  570. if(resetBuilded) //reset amount of structures set in this turn in towns
  571. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  572. t->builded = 0;
  573. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  574. h->bonuses.remove_if(Bonus::OneDay);
  575. if(gs->getDate(1) == 1) //new week, Monday that is
  576. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  577. h->bonuses.remove_if(Bonus::OneWeek);
  578. //count days without town
  579. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  580. {
  581. if(i->second.towns.size() || gs->day == 1)
  582. i->second.daysWithoutCastle = 0;
  583. else
  584. i->second.daysWithoutCastle++;
  585. i->second.bonuses.remove_if(Bonus::OneDay);
  586. if(gs->getDate(1) == 1) //new week
  587. i->second.bonuses.remove_if(Bonus::OneWeek);
  588. }
  589. }
  590. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  591. {
  592. CGObjectInstance *obj = gs->map->objects[id];
  593. if(!obj)
  594. {
  595. tlog1 << "Wrong object ID - property cannot be set!\n";
  596. return;
  597. }
  598. if(what == ObjProperty::OWNER)
  599. {
  600. if(obj->ID == TOWNI_TYPE)
  601. {
  602. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  603. if(t->tempOwner < PLAYER_LIMIT)
  604. gs->getPlayer(t->tempOwner)->towns -= t;
  605. if(val < PLAYER_LIMIT)
  606. gs->getPlayer(val)->towns.push_back(t);
  607. }
  608. }
  609. obj->setProperty(what,val);
  610. }
  611. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  612. {
  613. name.toString(gs->map->objects[id]->hoverName);
  614. }
  615. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  616. {
  617. CGHeroInstance* h = gs->getHero(heroid);
  618. h->level = level;
  619. //speciality
  620. h->UpdateSpeciality();
  621. }
  622. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  623. {
  624. gs->curB = info;
  625. info->belligerents[0]->battle = info->belligerents[1]->battle = info;
  626. }
  627. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  628. {
  629. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  630. gs->curB->round = round;
  631. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  632. {
  633. s->state -= DEFENDING;
  634. s->state -= WAITING;
  635. s->state -= MOVED;
  636. s->state -= HAD_MORALE;
  637. s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
  638. //regeneration
  639. if( s->hasBonusOfType(Bonus::HP_REGENERATION) && s->alive() )
  640. s->firstHPleft = std::min<ui32>( s->MaxHealth(), s->valOfBonuses(Bonus::HP_REGENERATION) );
  641. if( s->hasBonusOfType(Bonus::FULL_HP_REGENERATION) && s->alive() )
  642. s->firstHPleft = s->MaxHealth();
  643. //remove effects and restore only those with remaining turns in duration
  644. std::vector<CStack::StackEffect> tmpEffects = s->effects;
  645. s->effects.clear();
  646. for(int i=0; i < tmpEffects.size(); i++)
  647. {
  648. tmpEffects[i].turnsRemain--;
  649. if(tmpEffects[i].turnsRemain > 0)
  650. s->effects.push_back(tmpEffects[i]);
  651. }
  652. //the same as above for features
  653. BonusList tmpFeatures = s->bonuses;
  654. s->bonuses.clear();
  655. BOOST_FOREACH(Bonus &b, tmpFeatures)
  656. {
  657. if((b.duration & Bonus::N_TURNS) != 0)
  658. {
  659. b.turnsRemain--;
  660. if(b.turnsRemain > 0)
  661. s->bonuses.push_back(b);
  662. }
  663. else
  664. {
  665. s->bonuses.push_back(b);
  666. }
  667. }
  668. }
  669. }
  670. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  671. {
  672. gs->curB->activeStack = stack;
  673. CStack *st = gs->curB->getStack(stack);
  674. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  675. st->state.insert(HAD_MORALE);
  676. }
  677. void BattleResult::applyGs( CGameState *gs )
  678. {
  679. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  680. delete gs->curB->stacks[i];
  681. //remove any "until next battle" bonuses
  682. CGHeroInstance *h;
  683. h = gs->curB->heroes[0];
  684. if(h)
  685. h->bonuses.remove_if(Bonus::OneBattle);
  686. h = gs->curB->heroes[1];
  687. if(h)
  688. h->bonuses.remove_if(Bonus::OneBattle);
  689. gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
  690. delete gs->curB;
  691. gs->curB = NULL;
  692. }
  693. void BattleStackMoved::applyGs( CGameState *gs )
  694. {
  695. gs->curB->getStack(stack)->position = tile;
  696. }
  697. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  698. {
  699. CStack * at = gs->curB->getStack(stackAttacked);
  700. at->count = newAmount;
  701. at->firstHPleft = newHP;
  702. if(killed())
  703. at->state -= ALIVE;
  704. //life drain handling
  705. for (int g=0; g<healedStacks.size(); ++g)
  706. {
  707. healedStacks[g].applyGs(gs);
  708. }
  709. }
  710. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  711. {
  712. CStack *attacker = gs->curB->getStack(stackAttacking);
  713. if(counter())
  714. attacker->counterAttacks--;
  715. if(shot())
  716. {
  717. //don't remove ammo if we have a working ammo cart
  718. bool hasAmmoCart = false;
  719. BOOST_FOREACH(const CStack * st, gs->curB->stacks)
  720. {
  721. if(st->owner == attacker->owner && st->type->idNumber == 148 && st->alive())
  722. {
  723. hasAmmoCart = true;
  724. break;
  725. }
  726. }
  727. if (!hasAmmoCart)
  728. {
  729. attacker->shots--;
  730. }
  731. }
  732. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  733. stackAttacked.applyGs(gs);
  734. attacker->bonuses.remove_if(Bonus::UntilAttack);
  735. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  736. {
  737. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  738. stack->bonuses.remove_if(Bonus::UntilBeingAttacked);
  739. }
  740. }
  741. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  742. {
  743. CStack *st = gs->curB->getStack(ba.stackNumber);
  744. if(ba.actionType != 1) //don't check for stack if it's custom action by hero
  745. assert(st);
  746. switch(ba.actionType)
  747. {
  748. case 3:
  749. st->state.insert(DEFENDING);
  750. break;
  751. case 8:
  752. st->state.insert(WAITING);
  753. return;
  754. case 0: case 2: case 6: case 7: case 9: case 10: case 11: case 12:
  755. st->state.insert(MOVED);
  756. break;
  757. }
  758. if(st)
  759. 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)
  760. }
  761. DLL_EXPORT void BattleSpellCast::applyGs( CGameState *gs )
  762. {
  763. assert(gs->curB);
  764. CGHeroInstance *h = (side) ? gs->curB->heroes[1] : gs->curB->heroes[0];
  765. if(h && castedByHero)
  766. {
  767. int spellCost = 0;
  768. if(gs->curB)
  769. {
  770. spellCost = gs->curB->getSpellCost(&VLC->spellh->spells[id], h);
  771. }
  772. else
  773. {
  774. spellCost = VLC->spellh->spells[id].costs[skill];
  775. }
  776. h->mana -= spellCost;
  777. if(h->mana < 0) h->mana = 0;
  778. }
  779. if(side >= 0 && side < 2 && castedByHero)
  780. {
  781. gs->curB->castSpells[side]++;
  782. }
  783. if(id == 35 || id == 78) //dispel and dispel helpful spells
  784. {
  785. bool onlyHelpful = id == 78;
  786. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  787. {
  788. CStack *s = gs->curB->getStack(*it);
  789. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  790. {
  791. std::vector<CStack::StackEffect> remainingEff;
  792. for(int g=0; g< s->effects.size(); ++g)
  793. {
  794. if (onlyHelpful && VLC->spellh->spells[ s->effects[g].id ].positiveness != 1)
  795. {
  796. remainingEff.push_back(s->effects[g]);
  797. }
  798. }
  799. s->effects.clear(); //removing all effects
  800. s->effects = remainingEff; //assigning effects that should remain
  801. //removing all features from spells
  802. BonusList tmpFeatures = s->bonuses;
  803. s->bonuses.clear();
  804. BOOST_FOREACH(Bonus &b, tmpFeatures)
  805. {
  806. const CSpell *sp = b.sourceSpell();
  807. if(sp && sp->positiveness != 1) //if(b.source != HeroBonus::SPELL_EFFECT || b.positiveness != 1)
  808. s->bonuses.push_back(b);
  809. }
  810. }
  811. }
  812. }
  813. //elemental summoning
  814. if(id >= 66 && id <= 69)
  815. {
  816. int creID;
  817. switch(id)
  818. {
  819. case 66:
  820. creID = 114; //fire elemental
  821. break;
  822. case 67:
  823. creID = 113; //earth elemental
  824. break;
  825. case 68:
  826. creID = 115; //water elemental
  827. break;
  828. case 69:
  829. creID = 112; //air elemental
  830. break;
  831. }
  832. const int3 & tile = gs->curB->tile;
  833. TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  834. int pos; //position of stack on the battlefield - to be calculated
  835. bool ac[BFIELD_SIZE];
  836. std::set<int> occupyable;
  837. bool twoHex = VLC->creh->creatures[creID]->isDoubleWide();
  838. bool flying = vstd::contains(VLC->creh->creatures[creID]->bonuses, Bonus::FLYING);
  839. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  840. for(int g=0; g<BFIELD_SIZE; ++g)
  841. {
  842. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  843. {
  844. pos = g;
  845. break;
  846. }
  847. }
  848. CStack * summonedStack = gs->curB->generateNewStack(CStackInstance(creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id].powers[skill], h), gs->curB->stacks.size(), !side, 255, ter, pos);
  849. summonedStack->state.insert(SUMMONED);
  850. //summonedStack->bonuses.push_back( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  851. gs->curB->stacks.push_back(summonedStack);
  852. }
  853. }
  854. static inline Bonus featureGenerator(Bonus::BonusType type, si16 subtype, si32 value, ui16 turnsRemain, si32 additionalInfo = 0, si32 limit = Bonus::NO_LIMIT)
  855. {
  856. Bonus hb(makeFeature(type, Bonus::N_TURNS, subtype, value, Bonus::SPELL_EFFECT, turnsRemain, additionalInfo));
  857. hb.effectRange = limit;
  858. return hb;
  859. }
  860. static inline Bonus featureGeneratorVT(Bonus::BonusType type, si16 subtype, si32 value, ui16 turnsRemain, ui8 valType)
  861. {
  862. Bonus ret(makeFeature(type, Bonus::N_TURNS, subtype, value, Bonus::SPELL_EFFECT, turnsRemain));
  863. ret.valType = valType;
  864. return ret;
  865. }
  866. static BonusList stackEffectToFeature(const CStack::StackEffect & sse)
  867. {
  868. BonusList sf;
  869. si32 power = VLC->spellh->spells[sse.id].powers[sse.level];
  870. switch(sse.id)
  871. {
  872. case 27: //shield
  873. sf.push_back(featureGenerator(Bonus::GENERAL_DAMAGE_REDUCTION, 0, power, sse.turnsRemain));
  874. sf.back().id = sse.id;
  875. break;
  876. case 28: //air shield
  877. sf.push_back(featureGenerator(Bonus::GENERAL_DAMAGE_REDUCTION, 1, power, sse.turnsRemain));
  878. sf.back().id = sse.id;
  879. break;
  880. case 29: //fire shield
  881. sf.push_back(featureGenerator(Bonus::FIRE_SHIELD, 0, power, sse.turnsRemain));
  882. sf.back().id = sse.id;
  883. break;
  884. case 30: //protection from air
  885. sf.push_back(featureGenerator(Bonus::SPELL_DAMAGE_REDUCTION, 0, power, sse.turnsRemain));
  886. sf.back().id = sse.id;
  887. break;
  888. case 31: //protection from fire
  889. sf.push_back(featureGenerator(Bonus::SPELL_DAMAGE_REDUCTION, 1, power, sse.turnsRemain));
  890. sf.back().id = sse.id;
  891. break;
  892. case 32: //protection from water
  893. sf.push_back(featureGenerator(Bonus::SPELL_DAMAGE_REDUCTION, 2, power, sse.turnsRemain));
  894. sf.back().id = sse.id;
  895. break;
  896. case 33: //protection from earth
  897. sf.push_back(featureGenerator(Bonus::SPELL_DAMAGE_REDUCTION, 3, power, sse.turnsRemain));
  898. sf.back().id = sse.id;
  899. break;
  900. case 34: //anti-magic
  901. sf.push_back(featureGenerator(Bonus::LEVEL_SPELL_IMMUNITY, 0, power - 1, sse.turnsRemain));
  902. sf.back().id = sse.id;
  903. break;
  904. case 41: //bless
  905. sf.push_back(featureGenerator(Bonus::ALWAYS_MAXIMUM_DAMAGE, -1, power, sse.turnsRemain));
  906. sf.back().id = sse.id;
  907. break;
  908. case 42: //curse
  909. sf.push_back(featureGenerator(Bonus::ALWAYS_MINIMUM_DAMAGE, -1, -1 * power, sse.turnsRemain, sse.level >= 2 ? 20 : 0));
  910. sf.back().id = sse.id;
  911. break;
  912. case 43: //bloodlust
  913. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK, power, sse.turnsRemain, 0, Bonus::ONLY_MELEE_FIGHT));
  914. sf.back().id = sse.id;
  915. break;
  916. case 44: //precision
  917. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK, power, sse.turnsRemain, 0, Bonus::ONLY_DISTANCE_FIGHT));
  918. sf.back().id = sse.id;
  919. break;
  920. case 45: //weakness
  921. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK, -1 * power, sse.turnsRemain));
  922. sf.back().id = sse.id;
  923. break;
  924. case 46: //stone skin
  925. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE, power, sse.turnsRemain));
  926. sf.back().id = sse.id;
  927. break;
  928. case 47: //disrupting ray
  929. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE, -1 * power, sse.turnsRemain));
  930. sf.back().id = sse.id;
  931. break;
  932. case 48: //prayer
  933. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::ATTACK, power, sse.turnsRemain));
  934. sf.back().id = sse.id;
  935. sf.push_back(featureGenerator(Bonus::PRIMARY_SKILL, PrimarySkill::DEFENSE, power, sse.turnsRemain));
  936. sf.back().id = sse.id;
  937. sf.push_back(featureGenerator(Bonus::STACKS_SPEED, 0, power, sse.turnsRemain));
  938. sf.back().id = sse.id;
  939. break;
  940. case 49: //mirth
  941. sf.push_back(featureGenerator(Bonus::MORALE, 0, power, sse.turnsRemain));
  942. sf.back().id = sse.id;
  943. break;
  944. case 50: //sorrow
  945. sf.push_back(featureGenerator(Bonus::MORALE, 0, -1 * power, sse.turnsRemain));
  946. sf.back().id = sse.id;
  947. break;
  948. case 51: //fortune
  949. sf.push_back(featureGenerator(Bonus::LUCK, 0, power, sse.turnsRemain));
  950. sf.back().id = sse.id;
  951. break;
  952. case 52: //misfortune
  953. sf.push_back(featureGenerator(Bonus::LUCK, 0, -1 * power, sse.turnsRemain));
  954. sf.back().id = sse.id;
  955. break;
  956. case 53: //haste
  957. sf.push_back(featureGenerator(Bonus::STACKS_SPEED, 0, power, sse.turnsRemain));
  958. sf.back().id = sse.id;
  959. break;
  960. case 54: //slow
  961. sf.push_back(featureGeneratorVT(Bonus::STACKS_SPEED, 0, -1 * ( 100 - power ), sse.turnsRemain, Bonus::PERCENT_TO_ALL));
  962. sf.back().id = sse.id;
  963. break;
  964. case 55: //slayer
  965. sf.push_back(featureGenerator(Bonus::SLAYER, 0, sse.level, sse.turnsRemain));
  966. sf.back().id = sse.id;
  967. break;
  968. case 56: //frenzy
  969. sf.push_back(featureGenerator(Bonus::IN_FRENZY, 0, VLC->spellh->spells[56].powers[sse.level]/100.0, sse.turnsRemain));
  970. sf.back().id = sse.id;
  971. break;
  972. case 58: //counterstrike
  973. sf.push_back(featureGenerator(Bonus::ADDITIONAL_RETALIATION, 0, power, sse.turnsRemain));
  974. sf.back().id = sse.id;
  975. break;
  976. case 59: //bersek
  977. sf.push_back(featureGenerator(Bonus::ATTACKS_NEAREST_CREATURE, 0, sse.level, sse.turnsRemain));
  978. sf.back().id = sse.id;
  979. break;
  980. case 60: //hypnotize
  981. sf.push_back(featureGenerator(Bonus::HYPNOTIZED, 0, sse.level, sse.turnsRemain));
  982. sf.back().id = sse.id;
  983. break;
  984. case 61: //forgetfulness
  985. sf.push_back(featureGenerator(Bonus::FORGETFULL, 0, sse.level, sse.turnsRemain));
  986. sf.back().id = sse.id;
  987. break;
  988. case 62: //blind
  989. sf.push_back(makeFeature(Bonus::NOT_ACTIVE, Bonus::UNITL_BEING_ATTACKED | Bonus::N_TURNS, 0, 0, Bonus::SPELL_EFFECT, sse.turnsRemain));
  990. sf.back().id = sse.id;
  991. sf.push_back(makeFeature(Bonus::GENERAL_ATTACK_REDUCTION, Bonus::UNTIL_ATTACK | Bonus::N_TURNS, 0, power, Bonus::SPELL_EFFECT, sse.turnsRemain));
  992. sf.back().id = sse.id;
  993. break;
  994. }
  995. return sf;
  996. }
  997. void actualizeEffect(CStack * s, CStack::StackEffect & ef)
  998. {
  999. //actualizing effects vector
  1000. for(int g=0; g<s->effects.size(); ++g)
  1001. {
  1002. if(s->effects[g].id == ef.id)
  1003. {
  1004. s->effects[g].turnsRemain = std::max(s->effects[g].turnsRemain, ef.turnsRemain);
  1005. }
  1006. }
  1007. //actualizing features vector
  1008. BonusList sf = stackEffectToFeature(ef);
  1009. BOOST_FOREACH(const Bonus &fromEffect, sf)
  1010. {
  1011. BOOST_FOREACH(Bonus &stackBonus, s->bonuses)
  1012. {
  1013. if(stackBonus.source == Bonus::SPELL_EFFECT && stackBonus.type == fromEffect.type && stackBonus.subtype == fromEffect.subtype)
  1014. {
  1015. stackBonus.turnsRemain = std::max(stackBonus.turnsRemain, ef.turnsRemain);
  1016. }
  1017. }
  1018. }
  1019. }
  1020. bool containsEff(const std::vector<CStack::StackEffect> & vec, int effectId)
  1021. {
  1022. for(int g=0; g<vec.size(); ++g)
  1023. {
  1024. if(vec[g].id == effectId)
  1025. return true;
  1026. }
  1027. return false;
  1028. }
  1029. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  1030. {
  1031. BOOST_FOREACH(ui32 id, stacks)
  1032. {
  1033. CStack *s = gs->curB->getStack(id);
  1034. if(s)
  1035. {
  1036. if(effect.id == 42 || !containsEff(s->effects, effect.id))//disrupting ray or not on the list - just add
  1037. {
  1038. s->effects.push_back(effect);
  1039. BonusList sf = stackEffectToFeature(effect);
  1040. BOOST_FOREACH(const Bonus &fromEffect, sf)
  1041. {
  1042. s->bonuses.push_back(fromEffect);
  1043. }
  1044. }
  1045. else //just actualize
  1046. {
  1047. actualizeEffect(s, effect);
  1048. }
  1049. }
  1050. else
  1051. tlog1 << "Cannot find stack " << id << std::endl;
  1052. }
  1053. }
  1054. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  1055. {
  1056. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  1057. stackAttacked.applyGs(gs);
  1058. }
  1059. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  1060. {
  1061. for(int g=0; g<healedStacks.size(); ++g)
  1062. {
  1063. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  1064. //checking if we resurrect a stack that is under a living stack
  1065. std::vector<int> access = gs->curB->getAccessibility(changedStack->ID, true);
  1066. bool acc[BFIELD_SIZE];
  1067. for(int h=0; h<BFIELD_SIZE; ++h)
  1068. acc[h] = false;
  1069. for(int h=0; h<access.size(); ++h)
  1070. acc[access[h]] = true;
  1071. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  1072. changedStack->doubleWide(), changedStack->attackerOwned,
  1073. changedStack->hasBonusOfType(Bonus::FLYING), true))
  1074. return; //position is already occupied
  1075. //applying changes
  1076. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1077. if(resurrected)
  1078. {
  1079. changedStack->state.insert(ALIVE);
  1080. if(healedStacks[g].lowLevelResurrection)
  1081. changedStack->state.insert(SUMMONED);
  1082. //changedStack->bonuses.push_back( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1083. }
  1084. int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1085. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1086. changedStack->count += res;
  1087. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1088. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1089. {
  1090. changedStack->firstHPleft -= changedStack->MaxHealth();
  1091. if(changedStack->baseAmount > changedStack->count)
  1092. {
  1093. changedStack->count += 1;
  1094. }
  1095. }
  1096. amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1097. //removal of negative effects
  1098. if(resurrected)
  1099. {
  1100. for(int h=0; h<changedStack->effects.size(); ++h)
  1101. {
  1102. if(VLC->spellh->spells[changedStack->effects[h].id].positiveness < 0)
  1103. {
  1104. changedStack->effects.erase(changedStack->effects.begin() + h);
  1105. }
  1106. }
  1107. //removing all features from negative spells
  1108. BonusList tmpFeatures = changedStack->bonuses;
  1109. changedStack->bonuses.clear();
  1110. BOOST_FOREACH(Bonus &b, tmpFeatures)
  1111. {
  1112. const CSpell *s = b.sourceSpell();
  1113. if(s && s->positiveness >= 0)
  1114. {
  1115. changedStack->bonuses.push_back(b);
  1116. }
  1117. }
  1118. }
  1119. }
  1120. }
  1121. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  1122. {
  1123. if(gs->curB) //if there is a battle
  1124. {
  1125. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1126. {
  1127. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1128. {
  1129. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1130. {
  1131. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1132. break;
  1133. }
  1134. }
  1135. }
  1136. }
  1137. }
  1138. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  1139. {
  1140. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1141. {
  1142. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1143. {
  1144. gs->curB->si.wallState[it->first.first] =
  1145. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1146. }
  1147. }
  1148. }
  1149. DLL_EXPORT void BattleStacksRemoved::applyGs( CGameState *gs )
  1150. {
  1151. if(!gs->curB)
  1152. return;
  1153. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1154. {
  1155. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1156. {
  1157. if(gs->curB->stacks[b]->ID == *it) //if found
  1158. {
  1159. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  1166. {
  1167. gs->currentPlayer = player;
  1168. }
  1169. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  1170. {
  1171. gs->getPlayer(player)->currentSelection = id;
  1172. }
  1173. DLL_EXPORT Component::Component(const CStackInstance &stack)
  1174. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1175. {
  1176. }