NetPacksLib.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. #include "StdInc.h"
  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 "CGameState.h"
  13. #include "BattleState.h"
  14. #undef min
  15. #undef max
  16. /*
  17. * NetPacksLib.cpp, part of VCMI engine
  18. *
  19. * Authors: listed in file AUTHORS in main folder
  20. *
  21. * License: GNU General Public License v2.0 or later
  22. * Full text of license available in license.txt file, in main folder
  23. *
  24. */
  25. #ifdef min
  26. #undef min
  27. #endif
  28. #ifdef max
  29. #undef max
  30. #endif
  31. DLL_LINKAGE void SetResource::applyGs( CGameState *gs )
  32. {
  33. assert(player < GameConstants::PLAYER_LIMIT);
  34. vstd::amax(val, 0); //new value must be >= 0
  35. gs->getPlayer(player)->resources[resid] = val;
  36. }
  37. DLL_LINKAGE void SetResources::applyGs( CGameState *gs )
  38. {
  39. assert(player < GameConstants::PLAYER_LIMIT);
  40. gs->getPlayer(player)->resources = res;
  41. }
  42. DLL_LINKAGE void SetPrimSkill::applyGs( CGameState *gs )
  43. {
  44. CGHeroInstance *hero = gs->getHero(id);
  45. assert(hero);
  46. if(which <4)
  47. {
  48. Bonus *skill = hero->getBonus(Selector::type(Bonus::PRIMARY_SKILL) && Selector::subtype(which) && Selector::sourceType(Bonus::HERO_BASE_SKILL));
  49. assert(skill);
  50. if(abs)
  51. skill->val = val;
  52. else
  53. skill->val += val;
  54. }
  55. else if(which == 4) //XP
  56. {
  57. if(abs)
  58. hero->exp = val;
  59. else
  60. hero->exp += val;
  61. }
  62. }
  63. DLL_LINKAGE void SetSecSkill::applyGs( CGameState *gs )
  64. {
  65. CGHeroInstance *hero = gs->getHero(id);
  66. hero->setSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(which), val, abs);
  67. }
  68. DLL_LINKAGE void HeroVisitCastle::applyGs( CGameState *gs )
  69. {
  70. CGHeroInstance *h = gs->getHero(hid);
  71. CGTownInstance *t = gs->getTown(tid);
  72. if(start())
  73. t->setVisitingHero(h);
  74. else
  75. t->setVisitingHero(NULL);
  76. }
  77. DLL_LINKAGE void ChangeSpells::applyGs( CGameState *gs )
  78. {
  79. CGHeroInstance *hero = gs->getHero(hid);
  80. if(learn)
  81. BOOST_FOREACH(ui32 sid, spells)
  82. hero->spells.insert(sid);
  83. else
  84. BOOST_FOREACH(ui32 sid, spells)
  85. hero->spells.erase(sid);
  86. }
  87. DLL_LINKAGE void SetMana::applyGs( CGameState *gs )
  88. {
  89. CGHeroInstance *hero = gs->getHero(hid);
  90. vstd::amax(val, 0); //not less than 0
  91. hero->mana = val;
  92. }
  93. DLL_LINKAGE void SetMovePoints::applyGs( CGameState *gs )
  94. {
  95. CGHeroInstance *hero = gs->getHero(hid);
  96. hero->movement = val;
  97. }
  98. DLL_LINKAGE void FoWChange::applyGs( CGameState *gs )
  99. {
  100. TeamState * team = gs->getPlayerTeam(player);
  101. BOOST_FOREACH(int3 t, tiles)
  102. team->fogOfWarMap[t.x][t.y][t.z] = mode;
  103. if (mode == 0) //do not hide too much
  104. {
  105. boost::unordered_set<int3, ShashInt3> tilesRevealed;
  106. for (size_t i = 0; i < gs->map->objects.size(); i++)
  107. {
  108. if (gs->map->objects[i])
  109. {
  110. switch(gs->map->objects[i]->ID)
  111. {
  112. case 34://hero
  113. case 53://mine
  114. case 98://town
  115. case 220:
  116. if(vstd::contains(team->players, gs->map->objects[i]->tempOwner)) //check owned observators
  117. gs->map->objects[i]->getSightTiles(tilesRevealed);
  118. break;
  119. }
  120. }
  121. }
  122. BOOST_FOREACH(int3 t, tilesRevealed) //probably not the most optimal solution ever
  123. team->fogOfWarMap[t.x][t.y][t.z] = 1;
  124. }
  125. }
  126. DLL_LINKAGE void SetAvailableHeroes::applyGs( CGameState *gs )
  127. {
  128. PlayerState *p = gs->getPlayer(player);
  129. p->availableHeroes.clear();
  130. for (int i = 0; i < GameConstants::AVAILABLE_HEROES_PER_PLAYER; i++)
  131. {
  132. CGHeroInstance *h = (hid[i]>=0 ? (CGHeroInstance*)gs->hpool.heroesPool[hid[i]] : NULL);
  133. if(h && army[i])
  134. h->setToArmy(army[i]);
  135. p->availableHeroes.push_back(h);
  136. }
  137. }
  138. DLL_LINKAGE void GiveBonus::applyGs( CGameState *gs )
  139. {
  140. CBonusSystemNode *cbsn = NULL;
  141. switch(who)
  142. {
  143. case HERO:
  144. cbsn = gs->getHero(id);
  145. break;
  146. case PLAYER:
  147. cbsn = gs->getPlayer(id);
  148. break;
  149. case TOWN:
  150. cbsn = gs->getTown(id);
  151. break;
  152. }
  153. assert(cbsn);
  154. Bonus *b = new Bonus(bonus);
  155. cbsn->addNewBonus(b);
  156. std::string &descr = b->description;
  157. if(!bdescr.message.size()
  158. && bonus.source == Bonus::OBJECT
  159. && (bonus.type == Bonus::LUCK || bonus.type == Bonus::MORALE)
  160. && gs->map->objects[bonus.sid]->ID == GameConstants::EVENTI_TYPE) //it's morale/luck bonus from an event without description
  161. {
  162. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  163. boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val)));
  164. }
  165. else
  166. {
  167. bdescr.toString(descr);
  168. }
  169. }
  170. DLL_LINKAGE void ChangeObjPos::applyGs( CGameState *gs )
  171. {
  172. CGObjectInstance *obj = gs->map->objects[objid];
  173. if(!obj)
  174. {
  175. tlog1 << "Wrong ChangeObjPos: object " << objid << " doesn't exist!\n";
  176. return;
  177. }
  178. gs->map->removeBlockVisTiles(obj);
  179. obj->pos = nPos;
  180. gs->map->addBlockVisTiles(obj);
  181. }
  182. DLL_LINKAGE void PlayerEndsGame::applyGs( CGameState *gs )
  183. {
  184. PlayerState *p = gs->getPlayer(player);
  185. p->status = victory ? 2 : 1;
  186. }
  187. DLL_LINKAGE void RemoveBonus::applyGs( CGameState *gs )
  188. {
  189. CBonusSystemNode *node;
  190. if (who == HERO)
  191. node = gs->getHero(whoID);
  192. else
  193. node = gs->getPlayer(whoID);
  194. BonusList &bonuses = node->getBonusList();
  195. for (int i = 0; i < bonuses.size(); i++)
  196. {
  197. Bonus *b = bonuses[i];
  198. if(b->source == source && b->sid == id)
  199. {
  200. bonus = *b; //backup bonus (to show to interfaces later)
  201. bonuses.erase(i);
  202. break;
  203. }
  204. }
  205. }
  206. DLL_LINKAGE 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==GameConstants::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_LINKAGE 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_LINKAGE 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_LINKAGE 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_LINKAGE 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_LINKAGE 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_LINKAGE void GiveHero::applyGs( CGameState *gs )
  396. {
  397. CGHeroInstance *h = gs->getHero(id);
  398. //bonus system
  399. h->detachFrom(&gs->globalEffects);
  400. h->attachTo(gs->getPlayer(player));
  401. gs->map->removeBlockVisTiles(h,true);
  402. h->setOwner(player);
  403. h->movement = h->maxMovePoints(true);
  404. h->initHeroDefInfo();
  405. gs->map->heroes.push_back(h);
  406. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  407. gs->map->addBlockVisTiles(h);
  408. h->inTownGarrison = false;
  409. }
  410. DLL_LINKAGE void NewObject::applyGs( CGameState *gs )
  411. {
  412. CGObjectInstance *o = NULL;
  413. switch(ID)
  414. {
  415. case 8:
  416. o = new CGBoat();
  417. break;
  418. case 54: //probably more options will be needed
  419. o = new CGCreature();
  420. {
  421. //CStackInstance hlp;
  422. CGCreature *cre = static_cast<CGCreature*>(o);
  423. //cre->slots[0] = hlp;
  424. cre->notGrowingTeam = cre->neverFlees = 0;
  425. cre->character = 2;
  426. cre->gainedArtifact = -1;
  427. cre->identifier = -1;
  428. cre->addToSlot(0, new CStackInstance(subID, -1)); //add placeholder stack
  429. }
  430. break;
  431. default:
  432. o = new CGObjectInstance();
  433. break;
  434. }
  435. o->ID = ID;
  436. o->subID = subID;
  437. o->pos = pos;
  438. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  439. id = o->id = gs->map->objects.size();
  440. o->hoverName = VLC->generaltexth->names[ID];
  441. switch(ID)
  442. {
  443. case 54: //cfreature
  444. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  445. assert(o->defInfo);
  446. break;
  447. case 124://hole
  448. const TerrainTile &t = gs->map->getTile(pos);
  449. o->defInfo = VLC->dobjinfo->gobjs[ID][t.tertype];
  450. assert(o->defInfo);
  451. break;
  452. }
  453. gs->map->objects.push_back(o);
  454. gs->map->addBlockVisTiles(o);
  455. o->initObj();
  456. assert(o->defInfo);
  457. }
  458. DLL_LINKAGE void NewArtifact::applyGs( CGameState *gs )
  459. {
  460. assert(!vstd::contains(gs->map->artInstances, art));
  461. gs->map->addNewArtifactInstance(art);
  462. assert(!art->getParentNodes().size());
  463. art->setType(art->artType);
  464. if (CCombinedArtifactInstance* cart = dynamic_cast<CCombinedArtifactInstance*>(art.get()))
  465. cart->createConstituents();
  466. }
  467. DLL_LINKAGE const CStackInstance * StackLocation::getStack()
  468. {
  469. if(!army->hasStackAtSlot(slot))
  470. {
  471. tlog2 << "Warning: " << army->nodeName() << " dont have a stack at slot " << slot << std::endl;
  472. return NULL;
  473. }
  474. return &army->getStack(slot);
  475. }
  476. struct ObjectRetriever : boost::static_visitor<const CArmedInstance *>
  477. {
  478. const CArmedInstance * operator()(const ConstTransitivePtr<CGHeroInstance> &h) const
  479. {
  480. return h;
  481. }
  482. const CArmedInstance * operator()(const ConstTransitivePtr<CStackInstance> &s) const
  483. {
  484. return s->armyObj;
  485. }
  486. };
  487. template <typename T>
  488. struct GetBase : boost::static_visitor<T*>
  489. {
  490. template <typename TArg>
  491. T * operator()(TArg &arg) const
  492. {
  493. return arg;
  494. }
  495. };
  496. DLL_LINKAGE const CArmedInstance * ArtifactLocation::relatedObj() const
  497. {
  498. return boost::apply_visitor(ObjectRetriever(), artHolder);
  499. }
  500. DLL_LINKAGE int ArtifactLocation::owningPlayer() const
  501. {
  502. auto obj = relatedObj();
  503. return obj ? obj->tempOwner : GameConstants::NEUTRAL_PLAYER;
  504. }
  505. DLL_LINKAGE CArtifactSet *ArtifactLocation::getHolderArtSet()
  506. {
  507. return boost::apply_visitor(GetBase<CArtifactSet>(), artHolder);
  508. }
  509. DLL_LINKAGE CBonusSystemNode *ArtifactLocation::getHolderNode()
  510. {
  511. return boost::apply_visitor(GetBase<CBonusSystemNode>(), artHolder);
  512. }
  513. DLL_LINKAGE const CArtifactInstance *ArtifactLocation::getArt() const
  514. {
  515. const ArtSlotInfo *s = getSlot();
  516. if(s && s->artifact)
  517. {
  518. if(!s->locked)
  519. return s->artifact;
  520. else
  521. {
  522. tlog3 << "ArtifactLocation::getArt: That location is locked!\n";
  523. return NULL;
  524. }
  525. }
  526. return NULL;
  527. }
  528. DLL_LINKAGE const CArtifactSet * ArtifactLocation::getHolderArtSet() const
  529. {
  530. ArtifactLocation *t = const_cast<ArtifactLocation*>(this);
  531. return t->getHolderArtSet();
  532. }
  533. DLL_LINKAGE const CBonusSystemNode * ArtifactLocation::getHolderNode() const
  534. {
  535. ArtifactLocation *t = const_cast<ArtifactLocation*>(this);
  536. return t->getHolderNode();
  537. }
  538. DLL_LINKAGE CArtifactInstance *ArtifactLocation::getArt()
  539. {
  540. const ArtifactLocation *t = this;
  541. return const_cast<CArtifactInstance*>(t->getArt());
  542. }
  543. DLL_LINKAGE const ArtSlotInfo *ArtifactLocation::getSlot() const
  544. {
  545. return getHolderArtSet()->getSlot(slot);
  546. }
  547. DLL_LINKAGE void ChangeStackCount::applyGs( CGameState *gs )
  548. {
  549. if(absoluteValue)
  550. sl.army->setStackCount(sl.slot, count);
  551. else
  552. sl.army->changeStackCount(sl.slot, count);
  553. }
  554. DLL_LINKAGE void SetStackType::applyGs( CGameState *gs )
  555. {
  556. sl.army->setStackType(sl.slot, type);
  557. }
  558. DLL_LINKAGE void EraseStack::applyGs( CGameState *gs )
  559. {
  560. sl.army->eraseStack(sl.slot);
  561. }
  562. DLL_LINKAGE void SwapStacks::applyGs( CGameState *gs )
  563. {
  564. CStackInstance *s1 = sl1.army->detachStack(sl1.slot),
  565. *s2 = sl2.army->detachStack(sl2.slot);
  566. sl2.army->putStack(sl2.slot, s1);
  567. sl1.army->putStack(sl1.slot, s2);
  568. }
  569. DLL_LINKAGE void InsertNewStack::applyGs( CGameState *gs )
  570. {
  571. CStackInstance *s = new CStackInstance(stack.type, stack.count);
  572. sl.army->putStack(sl.slot, s);
  573. }
  574. DLL_LINKAGE void RebalanceStacks::applyGs( CGameState *gs )
  575. {
  576. const CCreature *srcType = src.army->getCreature(src.slot);
  577. TQuantity srcCount = src.army->getStackCount(src.slot);
  578. if(srcCount == count) //moving whole stack
  579. {
  580. if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> merge
  581. {
  582. assert(c == srcType);
  583. if (GameConstants::STACK_EXP)
  584. {
  585. ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
  586. src.army->eraseStack(src.slot);
  587. dst.army->changeStackCount(dst.slot, count);
  588. dst.army->setStackExp(dst.slot, totalExp /(dst.army->getStackCount(dst.slot))); //mean
  589. }
  590. else
  591. {
  592. src.army->eraseStack(src.slot);
  593. dst.army->changeStackCount(dst.slot, count);
  594. }
  595. }
  596. else //move stack to an empty slot, no exp change needed
  597. {
  598. CStackInstance *stackDetached = src.army->detachStack(src.slot);
  599. dst.army->putStack(dst.slot, stackDetached);
  600. }
  601. }
  602. else
  603. {
  604. if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> rebalance
  605. {
  606. assert(c == srcType);
  607. if (GameConstants::STACK_EXP)
  608. {
  609. ui64 totalExp = srcCount * src.army->getStackExperience(src.slot) + dst.army->getStackCount(dst.slot) * dst.army->getStackExperience(dst.slot);
  610. src.army->changeStackCount(src.slot, -count);
  611. dst.army->changeStackCount(dst.slot, count);
  612. dst.army->setStackExp(dst.slot, totalExp /(src.army->getStackCount(src.slot) + dst.army->getStackCount(dst.slot))); //mean
  613. }
  614. else
  615. {
  616. src.army->changeStackCount(src.slot, -count);
  617. dst.army->changeStackCount(dst.slot, count);
  618. }
  619. }
  620. else //split stack to an empty slot
  621. {
  622. src.army->changeStackCount(src.slot, -count);
  623. dst.army->addToSlot(dst.slot, srcType->idNumber, count, false);
  624. if (GameConstants::STACK_EXP)
  625. dst.army->setStackExp(dst.slot, src.army->getStackExperience(src.slot));
  626. }
  627. }
  628. CBonusSystemNode::treeHasChanged();
  629. }
  630. DLL_LINKAGE void PutArtifact::applyGs( CGameState *gs )
  631. {
  632. assert(art->canBePutAt(al));
  633. art->putAt(al);
  634. //al.hero->putArtifact(al.slot, art);
  635. }
  636. DLL_LINKAGE void EraseArtifact::applyGs( CGameState *gs )
  637. {
  638. CArtifactInstance *a = al.getArt();
  639. assert(a);
  640. a->removeFrom(al);
  641. }
  642. DLL_LINKAGE void MoveArtifact::applyGs( CGameState *gs )
  643. {
  644. CArtifactInstance *a = src.getArt();
  645. if(dst.slot < GameConstants::BACKPACK_START)
  646. assert(!dst.getArt());
  647. a->move(src, dst);
  648. //TODO what'll happen if Titan's thunder is equiped by pickin git up or the start of game?
  649. if (a->artType->id == 135 && dst.slot == ArtifactPosition::RIGHT_HAND) //Titan's Thunder creates new spellbook on equip
  650. {
  651. auto hPtr = boost::get<ConstTransitivePtr<CGHeroInstance> >(&dst.artHolder);
  652. if(hPtr)
  653. {
  654. CGHeroInstance *h = *hPtr;
  655. if(h && !h->hasSpellbook())
  656. gs->giveHeroArtifact(h, 0);
  657. }
  658. }
  659. }
  660. DLL_LINKAGE void AssembledArtifact::applyGs( CGameState *gs )
  661. {
  662. CArtifactSet *artSet = al.getHolderArtSet();
  663. const CArtifactInstance *transformedArt = al.getArt();
  664. assert(transformedArt);
  665. assert(vstd::contains(transformedArt->assemblyPossibilities(artSet), builtArt));
  666. CCombinedArtifactInstance *combinedArt = new CCombinedArtifactInstance(builtArt);
  667. gs->map->addNewArtifactInstance(combinedArt);
  668. //retrieve all constituents
  669. BOOST_FOREACH(si32 constituentID, *builtArt->constituents)
  670. {
  671. int pos = artSet->getArtPos(constituentID);
  672. assert(pos >= 0);
  673. CArtifactInstance *constituentInstance = artSet->getArt(pos);
  674. //move constituent from hero to be part of new, combined artifact
  675. constituentInstance->removeFrom(al);
  676. combinedArt->addAsConstituent(constituentInstance, pos);
  677. if(!vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], al.slot) && vstd::contains(combinedArt->artType->possibleSlots[artSet->bearerType()], pos))
  678. al.slot = pos;
  679. }
  680. //put new combined artifacts
  681. combinedArt->putAt(al);
  682. }
  683. DLL_LINKAGE void DisassembledArtifact::applyGs( CGameState *gs )
  684. {
  685. CCombinedArtifactInstance *disassembled = dynamic_cast<CCombinedArtifactInstance*>(al.getArt());
  686. assert(disassembled);
  687. std::vector<CCombinedArtifactInstance::ConstituentInfo> constituents = disassembled->constituentsInfo;
  688. disassembled->removeFrom(al);
  689. BOOST_FOREACH(CCombinedArtifactInstance::ConstituentInfo &ci, constituents)
  690. {
  691. ArtifactLocation constituentLoc = al;
  692. constituentLoc.slot = (ci.slot >= 0 ? ci.slot : al.slot); //-1 is slot of main constituent -> it'll replace combined artifact in its pos
  693. disassembled->detachFrom(ci.art);
  694. ci.art->putAt(constituentLoc);
  695. }
  696. gs->map->eraseArtifactInstance(disassembled);
  697. }
  698. DLL_LINKAGE void HeroVisit::applyGs( CGameState *gs )
  699. {
  700. if(starting)
  701. gs->ongoingVisits[hero] = obj;
  702. else
  703. gs->ongoingVisits.erase(hero);
  704. }
  705. DLL_LINKAGE void SetAvailableArtifacts::applyGs( CGameState *gs )
  706. {
  707. if(id >= 0)
  708. {
  709. if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id].get()))
  710. {
  711. bm->artifacts = arts;
  712. }
  713. else
  714. {
  715. tlog1 << "Wrong black market id!" << std::endl;
  716. }
  717. }
  718. else
  719. {
  720. CGTownInstance::merchantArtifacts = arts;
  721. }
  722. }
  723. DLL_LINKAGE void NewTurn::applyGs( CGameState *gs )
  724. {
  725. gs->day = day;
  726. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  727. {
  728. CGHeroInstance *hero = gs->getHero(h.id);
  729. hero->movement = h.move;
  730. hero->mana = h.mana;
  731. }
  732. for(std::map<ui8, TResources>::iterator i = res.begin(); i != res.end(); i++)
  733. {
  734. assert(i->first < GameConstants::PLAYER_LIMIT);
  735. gs->getPlayer(i->first)->resources = i->second;
  736. }
  737. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  738. h.applyGs(gs);
  739. gs->globalEffects.popBonuses(Bonus::OneDay); //works for children -> all game objs
  740. if(gs->getDate(1)) //new week
  741. gs->globalEffects.popBonuses(Bonus::OneWeek); //works for children -> all game objs
  742. //TODO not really a single root hierarchy, what about bonuses placed elsewhere? [not an issue with H3 mechanics but in the future...]
  743. //count days without town
  744. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  745. {
  746. if(i->second.towns.size() || gs->day == 1)
  747. i->second.daysWithoutCastle = 0;
  748. else
  749. i->second.daysWithoutCastle++;
  750. }
  751. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  752. t->builded = 0;
  753. }
  754. DLL_LINKAGE void SetObjectProperty::applyGs( CGameState *gs )
  755. {
  756. CGObjectInstance *obj = gs->map->objects[id];
  757. if(!obj)
  758. {
  759. tlog1 << "Wrong object ID - property cannot be set!\n";
  760. return;
  761. }
  762. CArmedInstance *cai = dynamic_cast<CArmedInstance *>(obj);
  763. if(what == ObjProperty::OWNER && cai)
  764. {
  765. if(obj->ID == GameConstants::TOWNI_TYPE)
  766. {
  767. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  768. if(t->tempOwner < GameConstants::PLAYER_LIMIT)
  769. gs->getPlayer(t->tempOwner)->towns -= t;
  770. if(val < GameConstants::PLAYER_LIMIT)
  771. gs->getPlayer(val)->towns.push_back(t);
  772. }
  773. CBonusSystemNode *nodeToMove = cai->whatShouldBeAttached();
  774. nodeToMove->detachFrom(cai->whereShouldBeAttached(gs));
  775. obj->setProperty(what,val);
  776. nodeToMove->attachTo(cai->whereShouldBeAttached(gs));
  777. }
  778. else //not an armed instance
  779. {
  780. obj->setProperty(what,val);
  781. }
  782. }
  783. DLL_LINKAGE void SetHoverName::applyGs( CGameState *gs )
  784. {
  785. name.toString(gs->map->objects[id]->hoverName);
  786. }
  787. DLL_LINKAGE void HeroLevelUp::applyGs( CGameState *gs )
  788. {
  789. CGHeroInstance* h = gs->getHero(heroid);
  790. h->level = level;
  791. //speciality
  792. h->UpdateSpeciality();
  793. }
  794. DLL_LINKAGE void BattleStart::applyGs( CGameState *gs )
  795. {
  796. gs->curB = info;
  797. gs->curB->localInit();
  798. }
  799. DLL_LINKAGE void BattleNextRound::applyGs( CGameState *gs )
  800. {
  801. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  802. for (int i = 0; i < 2; ++i)
  803. {
  804. vstd::amax(--gs->curB->enchanterCounter[i], 0);
  805. }
  806. gs->curB->round = round;
  807. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  808. {
  809. s->state -= EBattleStackState::DEFENDING;
  810. s->state -= EBattleStackState::WAITING;
  811. s->state -= EBattleStackState::MOVED;
  812. s->state -= EBattleStackState::HAD_MORALE;
  813. s->state -= EBattleStackState::FEAR;
  814. s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
  815. // new turn effects
  816. s->battleTurnPassed();
  817. }
  818. }
  819. DLL_LINKAGE void BattleSetActiveStack::applyGs( CGameState *gs )
  820. {
  821. gs->curB->activeStack = stack;
  822. CStack *st = gs->curB->getStack(stack);
  823. //remove bonuses that last until when stack gets new turn
  824. st->getBonusList().remove_if(Bonus::UntilGetsTurn);
  825. if(vstd::contains(st->state,EBattleStackState::MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  826. st->state.insert(EBattleStackState::HAD_MORALE);
  827. }
  828. DLL_LINKAGE void BattleTriggerEffect::applyGs( CGameState *gs )
  829. {
  830. CStack *st = gs->curB->getStack(stackID);
  831. switch (effect)
  832. {
  833. case Bonus::HP_REGENERATION:
  834. st->firstHPleft += val;
  835. vstd::amin (st->firstHPleft, (ui32)st->MaxHealth());
  836. break;
  837. case Bonus::MANA_DRAIN:
  838. {
  839. CGHeroInstance * h = gs->getHero(additionalInfo);
  840. h->mana -= val;
  841. vstd::amax(h->mana, 0);
  842. break;
  843. }
  844. case Bonus::POISON:
  845. {
  846. Bonus * b = st->getBonus(Selector::source(Bonus::SPELL_EFFECT, 71) && Selector::type(Bonus::STACK_HEALTH));
  847. if (b)
  848. b->val = val;
  849. break;
  850. }
  851. case Bonus::ENCHANTER:
  852. break;
  853. case Bonus::FEAR:
  854. st->state.insert(EBattleStackState::FEAR);
  855. break;
  856. default:
  857. tlog2 << "Unrecognized trigger effect type "<< type <<"\n";
  858. }
  859. }
  860. void BattleResult::applyGs( CGameState *gs )
  861. {
  862. //stack with SUMMONED flag but coming from garrison -> most likely resurrected, needs to be removed
  863. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  864. {
  865. if(s->base && s->base->armyObj && vstd::contains(s->state, EBattleStackState::SUMMONED))
  866. {
  867. assert(&s->base->armyObj->getStack(s->slot) == s->base);
  868. const_cast<CArmedInstance*>(s->base->armyObj)->eraseStack(s->slot);
  869. }
  870. }
  871. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  872. delete gs->curB->stacks[i];
  873. //remove any "until next battle" bonuses
  874. CGHeroInstance *h;
  875. h = gs->curB->heroes[0];
  876. if(h)
  877. h->getBonusList().remove_if(Bonus::OneBattle);
  878. h = gs->curB->heroes[1];
  879. if(h)
  880. h->getBonusList().remove_if(Bonus::OneBattle);
  881. if (GameConstants::STACK_EXP)
  882. {
  883. if (exp[0]) //checking local array is easier than dereferencing this crap twice
  884. gs->curB->belligerents[0]->giveStackExp(exp[0]);
  885. if (exp[1])
  886. gs->curB->belligerents[1]->giveStackExp(exp[1]);
  887. CBonusSystemNode::treeHasChanged();
  888. }
  889. gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
  890. gs->curB.dellNull();
  891. }
  892. void BattleStackMoved::applyGs( CGameState *gs )
  893. {
  894. gs->curB->getStack(stack)->position = tilesToMove.back();
  895. }
  896. DLL_LINKAGE void BattleStackAttacked::applyGs( CGameState *gs )
  897. {
  898. CStack * at = gs->curB->getStack(stackAttacked);
  899. at->count = newAmount;
  900. at->firstHPleft = newHP;
  901. if(killed())
  902. {
  903. at->state -= EBattleStackState::ALIVE;
  904. }
  905. //life drain handling
  906. for (int g=0; g<healedStacks.size(); ++g)
  907. {
  908. healedStacks[g].applyGs(gs);
  909. }
  910. if (willRebirth())
  911. {
  912. at->casts--;
  913. at->state.insert(EBattleStackState::ALIVE); //hmm?
  914. }
  915. if (cloneKilled())
  916. {
  917. BattleStacksRemoved bsr; //remove body
  918. bsr.stackIDs.insert(at->ID);
  919. bsr.applyGs(gs);
  920. }
  921. }
  922. DLL_LINKAGE void BattleAttack::applyGs( CGameState *gs )
  923. {
  924. CStack *attacker = gs->curB->getStack(stackAttacking);
  925. if(counter())
  926. attacker->counterAttacks--;
  927. if(shot())
  928. {
  929. //don't remove ammo if we have a working ammo cart
  930. bool hasAmmoCart = false;
  931. BOOST_FOREACH(const CStack * st, gs->curB->stacks)
  932. {
  933. if(st->owner == attacker->owner && st->getCreature()->idNumber == 148 && st->alive())
  934. {
  935. hasAmmoCart = true;
  936. break;
  937. }
  938. }
  939. if (!hasAmmoCart)
  940. {
  941. attacker->shots--;
  942. }
  943. }
  944. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  945. stackAttacked.applyGs(gs);
  946. attacker->getBonusList().remove_if(Bonus::UntilAttack);
  947. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  948. {
  949. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  950. if (stack) //cloned stack is already gone
  951. stack->getBonusList().remove_if(Bonus::UntilBeingAttacked);
  952. }
  953. }
  954. DLL_LINKAGE void StartAction::applyGs( CGameState *gs )
  955. {
  956. CStack *st = gs->curB->getStack(ba.stackNumber);
  957. if(ba.actionType == BattleAction::END_TACTIC_PHASE)
  958. {
  959. gs->curB->tacticDistance = 0;
  960. return;
  961. }
  962. if(gs->curB->tacticDistance)
  963. {
  964. // moves in tactics phase do not affect creature status
  965. // (tactics stack queue is managed by client)
  966. return;
  967. }
  968. if(ba.actionType != BattleAction::HERO_SPELL) //don't check for stack if it's custom action by hero
  969. {
  970. assert(st);
  971. }
  972. else
  973. {
  974. gs->curB->usedSpellsHistory[ba.side].push_back(VLC->spellh->spells[ba.additionalInfo]);
  975. }
  976. switch(ba.actionType)
  977. {
  978. case BattleAction::DEFEND:
  979. st->state.insert(EBattleStackState::DEFENDING);
  980. break;
  981. case BattleAction::WAIT:
  982. st->state.insert(EBattleStackState::WAITING);
  983. return;
  984. case BattleAction::HERO_SPELL: //no change in current stack state
  985. return;
  986. default: //any active stack action - attack, catapult, heal, spell...
  987. st->state.insert(EBattleStackState::MOVED);
  988. break;
  989. }
  990. if(st)
  991. st->state -= EBattleStackState::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)
  992. }
  993. DLL_LINKAGE void BattleSpellCast::applyGs( CGameState *gs )
  994. {
  995. assert(gs->curB);
  996. if (castedByHero)
  997. {
  998. CGHeroInstance * h = gs->curB->heroes[side];
  999. CGHeroInstance * enemy = gs->curB->heroes[1-side];
  1000. h->mana -= spellCost;
  1001. vstd::amax(h->mana, 0);
  1002. if (enemy && manaGained)
  1003. enemy->mana += manaGained;
  1004. if (side >= 0 && side < 2)
  1005. {
  1006. gs->curB->castSpells[side]++;
  1007. }
  1008. }
  1009. if(id == 35 || id == 78) //dispel and dispel helpful spells
  1010. {
  1011. bool onlyHelpful = id == 78;
  1012. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  1013. {
  1014. CStack *s = gs->curB->getStack(*it);
  1015. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  1016. {
  1017. if(onlyHelpful)
  1018. s->popBonuses(Selector::positiveSpellEffects);
  1019. else
  1020. s->popBonuses(Selector::sourceType(Bonus::SPELL_EFFECT));
  1021. }
  1022. }
  1023. }
  1024. }
  1025. void actualizeEffect(CStack * s, const std::vector<Bonus> & ef)
  1026. {
  1027. //actualizing features vector
  1028. BOOST_FOREACH(const Bonus &fromEffect, ef)
  1029. {
  1030. BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
  1031. {
  1032. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == fromEffect.type && stackBonus->subtype == fromEffect.subtype)
  1033. {
  1034. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, fromEffect.turnsRemain);
  1035. }
  1036. }
  1037. }
  1038. }
  1039. void actualizeEffect(CStack * s, const Bonus & ef)
  1040. {
  1041. BOOST_FOREACH(Bonus *stackBonus, s->getBonusList()) //TODO: optimize
  1042. {
  1043. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == ef.type && stackBonus->subtype == ef.subtype)
  1044. {
  1045. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, ef.turnsRemain);
  1046. }
  1047. }
  1048. }
  1049. DLL_LINKAGE void SetStackEffect::applyGs( CGameState *gs )
  1050. {
  1051. int spellid = effect.begin()->sid; //effects' source ID
  1052. BOOST_FOREACH(ui32 id, stacks)
  1053. {
  1054. CStack *s = gs->curB->getStack(id);
  1055. if(s)
  1056. {
  1057. 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
  1058. {
  1059. BOOST_FOREACH(Bonus &fromEffect, effect)
  1060. {
  1061. s->addNewBonus( new Bonus(fromEffect));
  1062. }
  1063. }
  1064. else //just actualize
  1065. {
  1066. actualizeEffect(s, effect);
  1067. }
  1068. }
  1069. else
  1070. tlog1 << "Cannot find stack " << id << std::endl;
  1071. }
  1072. typedef std::pair<ui32, Bonus> p;
  1073. BOOST_FOREACH(p para, uniqueBonuses)
  1074. {
  1075. CStack *s = gs->curB->getStack(para.first);
  1076. if (s)
  1077. {
  1078. if (!s->hasBonus(Selector::source(Bonus::SPELL_EFFECT, spellid) && Selector::typeSubtype(para.second.type, para.second.subtype)))
  1079. s->addNewBonus(new Bonus(para.second));
  1080. else
  1081. actualizeEffect(s, effect);
  1082. }
  1083. else
  1084. tlog1 << "Cannot find stack " << para.first << std::endl;
  1085. }
  1086. }
  1087. DLL_LINKAGE void StacksInjured::applyGs( CGameState *gs )
  1088. {
  1089. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  1090. stackAttacked.applyGs(gs);
  1091. }
  1092. DLL_LINKAGE void StacksHealedOrResurrected::applyGs( CGameState *gs )
  1093. {
  1094. for(int g=0; g<healedStacks.size(); ++g)
  1095. {
  1096. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  1097. //checking if we resurrect a stack that is under a living stack
  1098. std::vector<BattleHex> access = gs->curB->getAccessibility(changedStack, true);
  1099. bool acc[GameConstants::BFIELD_SIZE];
  1100. for(int h=0; h<GameConstants::BFIELD_SIZE; ++h)
  1101. acc[h] = false;
  1102. for(int h=0; h<access.size(); ++h)
  1103. acc[access[h]] = true;
  1104. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  1105. changedStack->doubleWide(), changedStack->attackerOwned,
  1106. changedStack->hasBonusOfType(Bonus::FLYING), true))
  1107. return; //position is already occupied
  1108. //applying changes
  1109. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1110. if(resurrected)
  1111. {
  1112. changedStack->state.insert(EBattleStackState::ALIVE);
  1113. if(healedStacks[g].lowLevelResurrection)
  1114. changedStack->state.insert(EBattleStackState::SUMMONED); //TODO: different counter for rised units
  1115. }
  1116. //int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1117. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1118. changedStack->count += res;
  1119. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1120. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1121. {
  1122. changedStack->firstHPleft -= changedStack->MaxHealth();
  1123. if(changedStack->baseAmount > changedStack->count)
  1124. {
  1125. changedStack->count += 1;
  1126. }
  1127. }
  1128. vstd::amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1129. //removal of negative effects
  1130. if(resurrected)
  1131. {
  1132. // for (BonusList::iterator it = changedStack->bonuses.begin(); it != changedStack->bonuses.end(); it++)
  1133. // {
  1134. // if(VLC->spellh->spells[(*it)->sid]->positiveness < 0)
  1135. // {
  1136. // changedStack->bonuses.erase(it);
  1137. // }
  1138. // }
  1139. //removing all features from negative spells
  1140. const BonusList tmpFeatures = changedStack->getBonusList();
  1141. //changedStack->bonuses.clear();
  1142. BOOST_FOREACH(Bonus *b, tmpFeatures)
  1143. {
  1144. const CSpell *s = b->sourceSpell();
  1145. if(s && s->isNegative())
  1146. {
  1147. changedStack->removeBonus(b);
  1148. }
  1149. }
  1150. }
  1151. }
  1152. }
  1153. DLL_LINKAGE void ObstaclesRemoved::applyGs( CGameState *gs )
  1154. {
  1155. if(gs->curB) //if there is a battle
  1156. {
  1157. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1158. {
  1159. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1160. {
  1161. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1162. {
  1163. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1164. break;
  1165. }
  1166. }
  1167. }
  1168. }
  1169. }
  1170. DLL_LINKAGE void CatapultAttack::applyGs( CGameState *gs )
  1171. {
  1172. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1173. {
  1174. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1175. {
  1176. gs->curB->si.wallState[it->first.first] =
  1177. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1178. }
  1179. }
  1180. }
  1181. DLL_LINKAGE void BattleStacksRemoved::applyGs( CGameState *gs )
  1182. {
  1183. if(!gs->curB)
  1184. return;
  1185. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1186. {
  1187. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1188. {
  1189. if(gs->curB->stacks[b]->ID == *it) //if found
  1190. {
  1191. CStack *toRemove = gs->curB->stacks[b];
  1192. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1193. toRemove->detachFromAll();
  1194. delete toRemove;
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. }
  1200. DLL_LINKAGE void BattleStackAdded::applyGs(CGameState *gs)
  1201. {
  1202. if (!BattleHex(pos).isValid())
  1203. {
  1204. tlog2 << "No place found for new stack!\n";
  1205. return;
  1206. }
  1207. CStackBasicDescriptor csbd(creID, amount);
  1208. CStack * addedStack = gs->curB->generateNewStack(csbd, attacker, 255, pos); //TODO: netpacks?
  1209. if (summoned)
  1210. addedStack->state.insert(EBattleStackState::SUMMONED);
  1211. gs->curB->localInitStack(addedStack);
  1212. gs->curB->stacks.push_back(addedStack); //the stack is not "SUMMONED", it is permanent
  1213. }
  1214. DLL_LINKAGE void BattleSetStackProperty::applyGs(CGameState *gs)
  1215. {
  1216. CStack * stack = gs->curB->getStack(stackID);
  1217. switch (which)
  1218. {
  1219. case CASTS:
  1220. {
  1221. if (absolute)
  1222. stack->casts = val;
  1223. else
  1224. stack->casts += val;
  1225. vstd::amax(stack->casts, 0);
  1226. break;
  1227. }
  1228. case ENCHANTER_COUNTER:
  1229. {
  1230. int side = gs->curB->whatSide(stack->owner);
  1231. if (absolute)
  1232. gs->curB->enchanterCounter[side] = val;
  1233. else
  1234. gs->curB->enchanterCounter[side] += val;
  1235. vstd::amax(gs->curB->enchanterCounter[side], 0);
  1236. break;
  1237. }
  1238. case UNBIND:
  1239. {
  1240. stack->popBonuses(Selector::type(Bonus::BIND_EFFECT));
  1241. break;
  1242. }
  1243. case CLONED:
  1244. {
  1245. stack->state.insert(EBattleStackState::CLONED);
  1246. break;
  1247. }
  1248. }
  1249. }
  1250. DLL_LINKAGE void YourTurn::applyGs( CGameState *gs )
  1251. {
  1252. gs->currentPlayer = player;
  1253. }
  1254. DLL_LINKAGE void SetSelection::applyGs( CGameState *gs )
  1255. {
  1256. gs->getPlayer(player)->currentSelection = id;
  1257. }
  1258. DLL_LINKAGE Component::Component(const CStackBasicDescriptor &stack)
  1259. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1260. {
  1261. }