NetPacksLib.cpp 39 KB

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