NetPacksLib.cpp 36 KB

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