NetPacksLib.cpp 39 KB

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