NetPacksLib.cpp 39 KB

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