NetPacksLib.cpp 39 KB

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