NetPacksLib.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  1. #define VCMI_DLL
  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 <boost/bind.hpp>
  13. #include <boost/foreach.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #include <boost/algorithm/string/replace.hpp>
  16. #include <boost/thread.hpp>
  17. #include <boost/thread/shared_mutex.hpp>
  18. #include "CGameState.h"
  19. #include "BattleState.h"
  20. #undef min
  21. #undef max
  22. /*
  23. * NetPacksLib.cpp, part of VCMI engine
  24. *
  25. * Authors: listed in file AUTHORS in main folder
  26. *
  27. * License: GNU General Public License v2.0 or later
  28. * Full text of license available in license.txt file, in main folder
  29. *
  30. */
  31. #ifdef min
  32. #undef min
  33. #endif
  34. #ifdef max
  35. #undef max
  36. #endif
  37. DLL_EXPORT void SetResource::applyGs( CGameState *gs )
  38. {
  39. assert(player < PLAYER_LIMIT);
  40. amax(val, 0); //new value must be >= 0
  41. gs->getPlayer(player)->resources[resid] = val;
  42. }
  43. DLL_EXPORT void SetResources::applyGs( CGameState *gs )
  44. {
  45. assert(player < PLAYER_LIMIT);
  46. for(int i=0;i<res.size();i++)
  47. gs->getPlayer(player)->resources[i] = res[i];
  48. }
  49. DLL_EXPORT void SetPrimSkill::applyGs( CGameState *gs )
  50. {
  51. CGHeroInstance *hero = gs->getHero(id);
  52. assert(hero);
  53. if(which <4)
  54. {
  55. Bonus *skill = hero->getBonus(Selector::type(Bonus::PRIMARY_SKILL) && Selector::subtype(which) && Selector::sourceType(Bonus::HERO_BASE_SKILL));
  56. assert(skill);
  57. if(abs)
  58. skill->val = val;
  59. else
  60. skill->val += val;
  61. }
  62. else if(which == 4) //XP
  63. {
  64. if(abs)
  65. hero->exp = val;
  66. else
  67. hero->exp += val;
  68. }
  69. }
  70. DLL_EXPORT void SetSecSkill::applyGs( CGameState *gs )
  71. {
  72. CGHeroInstance *hero = gs->getHero(id);
  73. hero->setSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(which), val, abs);
  74. }
  75. DLL_EXPORT void HeroVisitCastle::applyGs( CGameState *gs )
  76. {
  77. CGHeroInstance *h = gs->getHero(hid);
  78. CGTownInstance *t = gs->getTown(tid);
  79. if(start())
  80. {
  81. if(garrison())
  82. {
  83. t->garrisonHero = h;
  84. h->visitedTown = t;
  85. h->inTownGarrison = true;
  86. }
  87. else
  88. {
  89. t->visitingHero = h;
  90. h->visitedTown = t;
  91. h->inTownGarrison = false;
  92. }
  93. }
  94. else
  95. {
  96. if(garrison())
  97. {
  98. t->garrisonHero = NULL;
  99. h->visitedTown = NULL;
  100. h->inTownGarrison = false;
  101. }
  102. else
  103. {
  104. t->visitingHero = NULL;
  105. h->visitedTown = NULL;
  106. h->inTownGarrison = false;
  107. }
  108. }
  109. }
  110. DLL_EXPORT void ChangeSpells::applyGs( CGameState *gs )
  111. {
  112. CGHeroInstance *hero = gs->getHero(hid);
  113. if(learn)
  114. BOOST_FOREACH(ui32 sid, spells)
  115. hero->spells.insert(sid);
  116. else
  117. BOOST_FOREACH(ui32 sid, spells)
  118. hero->spells.erase(sid);
  119. }
  120. DLL_EXPORT void SetMana::applyGs( CGameState *gs )
  121. {
  122. CGHeroInstance *hero = gs->getHero(hid);
  123. amax(val, 0); //not less than 0
  124. hero->mana = val;
  125. }
  126. DLL_EXPORT void SetMovePoints::applyGs( CGameState *gs )
  127. {
  128. CGHeroInstance *hero = gs->getHero(hid);
  129. hero->movement = val;
  130. }
  131. DLL_EXPORT void FoWChange::applyGs( CGameState *gs )
  132. {
  133. TeamState * team = gs->getPlayerTeam(player);
  134. BOOST_FOREACH(int3 t, tiles)
  135. team->fogOfWarMap[t.x][t.y][t.z] = mode;
  136. if (mode == 0) //do not hide too much
  137. {
  138. std::set<int3> tilesRevealed;
  139. for (size_t i = 0; i < gs->map->objects.size(); i++)
  140. {
  141. if (gs->map->objects[i])
  142. {
  143. switch(gs->map->objects[i]->ID)
  144. {
  145. case 34://hero
  146. case 53://mine
  147. case 98://town
  148. case 220:
  149. if(vstd::contains(team->players, gs->map->objects[i]->tempOwner)) //check owned observators
  150. gs->map->objects[i]->getSightTiles(tilesRevealed);
  151. break;
  152. }
  153. }
  154. }
  155. BOOST_FOREACH(int3 t, tilesRevealed) //probably not the most optimal solution ever
  156. team->fogOfWarMap[t.x][t.y][t.z] = 1;
  157. }
  158. }
  159. DLL_EXPORT void SetAvailableHeroes::applyGs( CGameState *gs )
  160. {
  161. PlayerState *p = gs->getPlayer(player);
  162. p->availableHeroes.clear();
  163. for (int i = 0; i < AVAILABLE_HEROES_PER_PLAYER; i++)
  164. {
  165. CGHeroInstance *h = (hid[i]>=0 ? (CGHeroInstance*)gs->hpool.heroesPool[hid[i]] : NULL);
  166. if(h && army[i])
  167. h->setToArmy(*army[i]);
  168. p->availableHeroes.push_back(h);
  169. }
  170. }
  171. DLL_EXPORT void GiveBonus::applyGs( CGameState *gs )
  172. {
  173. CBonusSystemNode *cbsn = NULL;
  174. switch(who)
  175. {
  176. case HERO:
  177. cbsn = gs->getHero(id);
  178. break;
  179. case PLAYER:
  180. cbsn = gs->getPlayer(id);
  181. break;
  182. case TOWN:
  183. cbsn = gs->getTown(id);
  184. break;
  185. }
  186. assert(cbsn);
  187. Bonus *b = new Bonus(bonus);
  188. cbsn->addNewBonus(b);
  189. std::string &descr = b->description;
  190. if(!bdescr.message.size()
  191. && bonus.source == Bonus::OBJECT
  192. && (bonus.type == Bonus::LUCK || bonus.type == Bonus::MORALE)
  193. && gs->map->objects[bonus.id]->ID == EVENTI_TYPE) //it's morale/luck bonus from an event without description
  194. {
  195. descr = VLC->generaltexth->arraytxt[bonus.val > 0 ? 110 : 109]; //+/-%d Temporary until next battle"
  196. boost::replace_first(descr,"%d",boost::lexical_cast<std::string>(std::abs(bonus.val)));
  197. }
  198. else
  199. {
  200. bdescr.toString(descr);
  201. }
  202. }
  203. DLL_EXPORT void ChangeObjPos::applyGs( CGameState *gs )
  204. {
  205. CGObjectInstance *obj = gs->map->objects[objid];
  206. if(!obj)
  207. {
  208. tlog1 << "Wrong ChangeObjPos: object " << objid << " doesn't exist!\n";
  209. return;
  210. }
  211. gs->map->removeBlockVisTiles(obj);
  212. obj->pos = nPos;
  213. gs->map->addBlockVisTiles(obj);
  214. }
  215. DLL_EXPORT void PlayerEndsGame::applyGs( CGameState *gs )
  216. {
  217. PlayerState *p = gs->getPlayer(player);
  218. p->status = victory ? 2 : 1;
  219. }
  220. DLL_EXPORT void RemoveBonus::applyGs( CGameState *gs )
  221. {
  222. BonusList &bonuses = (who == HERO ? gs->getHero(whoID)->bonuses : gs->getPlayer(whoID)->bonuses);
  223. for(BonusList::iterator i = bonuses.begin(); i != bonuses.end(); i++)
  224. {
  225. if((*i)->source == source && (*i)->id == id)
  226. {
  227. bonus = **i; //backup bonus (to show to interfaces later)
  228. bonuses.erase(i);
  229. break;
  230. }
  231. }
  232. }
  233. DLL_EXPORT 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==HEROI_TYPE)
  242. {
  243. CGHeroInstance *h = static_cast<CGHeroInstance*>(obj);
  244. std::vector<ConstTransitivePtr<CGHeroInstance> >::iterator nitr = std::find(gs->map->heroes.begin(), gs->map->heroes.end(),h);
  245. gs->map->heroes.erase(nitr);
  246. int player = h->tempOwner;
  247. nitr = std::find(gs->getPlayer(player)->heroes.begin(), gs->getPlayer(player)->heroes.end(), h);
  248. gs->getPlayer(player)->heroes.erase(nitr);
  249. h->tempOwner = 255; //no one owns beaten hero
  250. if(CGTownInstance *t = const_cast<CGTownInstance *>(h->visitedTown))
  251. {
  252. if(h->inTownGarrison)
  253. t->garrisonHero = NULL;
  254. else
  255. t->visitingHero = NULL;
  256. h->visitedTown = NULL;
  257. }
  258. //return hero to the pool, so he may reappear in tavern
  259. gs->hpool.heroesPool[h->subID] = h;
  260. if(!vstd::contains(gs->hpool.pavailable, h->subID))
  261. gs->hpool.pavailable[h->subID] = 0xff;
  262. }
  263. else if (obj->ID==CREI_TYPE && gs->map->version > CMapHeader::RoE) //only fixed monsters can be a part of quest
  264. {
  265. CGCreature *cre = static_cast<CGCreature*>(obj);
  266. gs->map->monsters[cre->identifier]->pos = int3 (-1,-1,-1); //use nonexistent monster for quest :>
  267. }
  268. gs->map->objects[id].dellNull();
  269. }
  270. static int getDir(int3 src, int3 dst)
  271. {
  272. int ret = -1;
  273. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  274. {
  275. ret = 1;
  276. }
  277. else if(dst.x == src.x && dst.y+1 == src.y) //t
  278. {
  279. ret = 2;
  280. }
  281. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  282. {
  283. ret = 3;
  284. }
  285. else if(dst.x-1 == src.x && dst.y == src.y) //r
  286. {
  287. ret = 4;
  288. }
  289. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  290. {
  291. ret = 5;
  292. }
  293. else if(dst.x == src.x && dst.y-1 == src.y) //b
  294. {
  295. ret = 6;
  296. }
  297. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  298. {
  299. ret = 7;
  300. }
  301. else if(dst.x+1 == src.x && dst.y == src.y) //l
  302. {
  303. ret = 8;
  304. }
  305. return ret;
  306. }
  307. void TryMoveHero::applyGs( CGameState *gs )
  308. {
  309. CGHeroInstance *h = gs->getHero(id);
  310. h->movement = movePoints;
  311. if((result == SUCCESS || result == BLOCKING_VISIT || result == EMBARK || result == DISEMBARK) && start != end) {
  312. h->moveDir = getDir(start,end);
  313. }
  314. if(result == EMBARK) //hero enters boat at dest tile
  315. {
  316. const TerrainTile &tt = gs->map->getTile(CGHeroInstance::convertPosition(end, false));
  317. assert(tt.visitableObjects.size() == 1 && tt.visitableObjects.front()->ID == 8); //the only vis obj at dest is Boat
  318. CGBoat *boat = static_cast<CGBoat*>(tt.visitableObjects.front());
  319. gs->map->removeBlockVisTiles(boat); //hero blockvis mask will be used, we don't need to duplicate it with boat
  320. h->boat = boat;
  321. boat->hero = h;
  322. }
  323. else if(result == DISEMBARK) //hero leaves boat to dest tile
  324. {
  325. CGBoat *b = const_cast<CGBoat *>(h->boat);
  326. b->direction = h->moveDir;
  327. b->pos = start;
  328. b->hero = NULL;
  329. gs->map->addBlockVisTiles(b);
  330. h->boat = NULL;
  331. }
  332. if(start!=end && (result == SUCCESS || result == TELEPORTATION || result == EMBARK || result == DISEMBARK))
  333. {
  334. gs->map->removeBlockVisTiles(h);
  335. h->pos = end;
  336. if(CGBoat *b = const_cast<CGBoat *>(h->boat))
  337. b->pos = end;
  338. gs->map->addBlockVisTiles(h);
  339. }
  340. BOOST_FOREACH(int3 t, fowRevealed)
  341. gs->getPlayerTeam(h->getOwner())->fogOfWarMap[t.x][t.y][t.z] = 1;
  342. }
  343. // DLL_EXPORT void SetGarrisons::applyGs( CGameState *gs )
  344. // {
  345. // for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  346. // {
  347. // CArmedInstance *ai = static_cast<CArmedInstance*>(gs->map->objects[i->first]);
  348. // ai->setToArmy(i->second);
  349. // if(ai->ID==TOWNI_TYPE && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  350. // const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->setToArmy(i->second);
  351. // else if(ai->ID==HEROI_TYPE)
  352. // {
  353. // CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  354. // CGTownInstance *t = const_cast<CGTownInstance *>(h->visitedTown);
  355. // if(t && h->inTownGarrison)
  356. // t->setToArmy(i->second);
  357. // }
  358. // }
  359. // }
  360. DLL_EXPORT void NewStructures::applyGs( CGameState *gs )
  361. {
  362. CGTownInstance *t = gs->getTown(tid);
  363. BOOST_FOREACH(si32 id,bid)
  364. {
  365. t->builtBuildings.insert(id);
  366. }
  367. t->builded = builded;
  368. }
  369. DLL_EXPORT void RazeStructures::applyGs( CGameState *gs )
  370. {
  371. CGTownInstance *t = gs->getTown(tid);
  372. BOOST_FOREACH(si32 id,bid)
  373. {
  374. t->builtBuildings.erase(id);
  375. }
  376. t->destroyed = destroyed; //yeaha
  377. }
  378. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  379. {
  380. CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->map->objects[tid].get());
  381. assert(dw);
  382. dw->creatures = creatures;
  383. }
  384. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  385. {
  386. CGTownInstance *t = gs->getTown(tid);
  387. CGHeroInstance *v = gs->getHero(visiting),
  388. *g = gs->getHero(garrison);
  389. t->visitingHero = v;
  390. t->garrisonHero = g;
  391. if(v)
  392. {
  393. v->visitedTown = t;
  394. v->inTownGarrison = false;
  395. gs->map->addBlockVisTiles(v);
  396. }
  397. if(g)
  398. {
  399. g->visitedTown = t;
  400. g->inTownGarrison = true;
  401. gs->map->removeBlockVisTiles(g);
  402. }
  403. }
  404. DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  405. {
  406. CGHeroInstance *h = gs->getHero(hid);
  407. for(std::map<ui16, const CArtifact*>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
  408. if(!vstd::contains(artifWorn,i->first) || artifWorn[i->first] != i->second)
  409. unequiped.push_back(i->second);
  410. for(std::map<ui16, const CArtifact*>::iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  411. if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  412. {
  413. equiped.push_back(i->second);
  414. }
  415. //update hero data
  416. h->artifacts = artifacts;
  417. h->artifWorn = artifWorn;
  418. }
  419. DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, const CArtifact* art)
  420. {
  421. if(!art)
  422. {
  423. if(pos<19)
  424. VLC->arth->unequipArtifact(artifWorn, pos);
  425. else if (pos - 19 < artifacts.size())
  426. artifacts.erase(artifacts.begin() + (pos - 19));
  427. }
  428. else
  429. {
  430. if (pos < 19)
  431. {
  432. VLC->arth->equipArtifact(artifWorn, pos, art);
  433. }
  434. else // Goes into the backpack.
  435. {
  436. if(pos - 19 < artifacts.size())
  437. artifacts.insert(artifacts.begin() + (pos - 19), art);
  438. else
  439. artifacts.push_back(art);
  440. }
  441. }
  442. }
  443. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  444. {
  445. assert(vstd::contains(gs->hpool.heroesPool, hid));
  446. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  447. CGTownInstance *t = gs->getTown(tid);
  448. h->setOwner(player);
  449. h->pos = tile;
  450. h->movement = h->maxMovePoints(true);
  451. gs->hpool.heroesPool.erase(hid);
  452. if(h->id < 0)
  453. {
  454. h->id = gs->map->objects.size();
  455. gs->map->objects.push_back(h);
  456. }
  457. else
  458. gs->map->objects[h->id] = h;
  459. h->initHeroDefInfo();
  460. gs->map->heroes.push_back(h);
  461. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  462. h->initObj();
  463. gs->map->addBlockVisTiles(h);
  464. if(t)
  465. {
  466. t->visitingHero = h;
  467. h->visitedTown = t;
  468. }
  469. h->inTownGarrison = false;
  470. }
  471. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  472. {
  473. CGHeroInstance *h = gs->getHero(id);
  474. gs->map->removeBlockVisTiles(h,true);
  475. h->setOwner(player);
  476. h->movement = h->maxMovePoints(true);
  477. h->initHeroDefInfo();
  478. gs->map->heroes.push_back(h);
  479. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  480. gs->map->addBlockVisTiles(h);
  481. h->inTownGarrison = false;
  482. }
  483. DLL_EXPORT void NewObject::applyGs( CGameState *gs )
  484. {
  485. CGObjectInstance *o = NULL;
  486. switch(ID)
  487. {
  488. case 8:
  489. o = new CGBoat();
  490. break;
  491. case 54: //probably more options will be needed
  492. o = new CGCreature();
  493. {
  494. //CStackInstance hlp;
  495. CGCreature *cre = static_cast<CGCreature*>(o);
  496. //cre->slots[0] = hlp;
  497. cre->notGrowingTeam = cre->neverFlees = 0;
  498. cre->character = 2;
  499. cre->gainedArtifact = -1;
  500. }
  501. break;
  502. default:
  503. o = new CGObjectInstance();
  504. break;
  505. }
  506. o->ID = ID;
  507. o->subID = subID;
  508. o->pos = pos;
  509. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  510. id = o->id = gs->map->objects.size();
  511. o->hoverName = VLC->generaltexth->names[ID];
  512. switch(ID)
  513. {
  514. case 54: //cfreature
  515. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  516. assert(o->defInfo);
  517. break;
  518. case 124://hole
  519. const TerrainTile &t = gs->map->getTile(pos);
  520. o->defInfo = VLC->dobjinfo->gobjs[ID][t.tertype];
  521. assert(o->defInfo);
  522. break;
  523. }
  524. gs->map->objects.push_back(o);
  525. gs->map->addBlockVisTiles(o);
  526. o->initObj();
  527. assert(o->defInfo);
  528. }
  529. DLL_EXPORT void NewArtifact::applyGs( CGameState *gs )
  530. {
  531. assert(!vstd::contains(gs->map->artInstances, art));
  532. art->id = gs->map->artInstances.size();
  533. gs->map->artInstances.push_back(art);
  534. assert(!art->parents.size());
  535. art->attachTo(art->artType);
  536. }
  537. DLL_EXPORT const CStackInstance * StackLocation::getStack()
  538. {
  539. if(!army->hasStackAtSlot(slot))
  540. {
  541. tlog2 << "Warning: " << army->nodeName() << " dont have a stack at slot " << slot << std::endl;
  542. return NULL;
  543. }
  544. return &army->getStack(slot);
  545. }
  546. DLL_EXPORT const CArtifactInstance *ArtifactLocation::getArt() const
  547. {
  548. const ArtSlotInfo *s = getSlot();
  549. if(s && s->artifact)
  550. {
  551. if(!s->locked)
  552. return s->artifact;
  553. else
  554. {
  555. tlog3 << "ArtifactLocation::getArt: That location is locked!\n";
  556. return NULL;
  557. }
  558. }
  559. return NULL;
  560. }
  561. DLL_EXPORT CArtifactInstance *ArtifactLocation::getArt()
  562. {
  563. const ArtifactLocation *t = this;
  564. return const_cast<CArtifactInstance*>(t->getArt());
  565. }
  566. DLL_EXPORT const ArtSlotInfo *ArtifactLocation::getSlot() const
  567. {
  568. return hero->getSlot(slot);
  569. }
  570. DLL_EXPORT void ChangeStackCount::applyGs( CGameState *gs )
  571. {
  572. if(absoluteValue)
  573. sl.army->setStackCount(sl.slot, count);
  574. else
  575. sl.army->changeStackCount(sl.slot, count);
  576. }
  577. DLL_EXPORT void SetStackType::applyGs( CGameState *gs )
  578. {
  579. sl.army->setStackType(sl.slot, type);
  580. }
  581. DLL_EXPORT void EraseStack::applyGs( CGameState *gs )
  582. {
  583. sl.army->eraseStack(sl.slot);
  584. }
  585. DLL_EXPORT void SwapStacks::applyGs( CGameState *gs )
  586. {
  587. CStackInstance *s1 = sl1.army->detachStack(sl1.slot),
  588. *s2 = sl2.army->detachStack(sl2.slot);
  589. sl2.army->putStack(sl2.slot, s1);
  590. sl1.army->putStack(sl1.slot, s2);
  591. }
  592. DLL_EXPORT void InsertNewStack::applyGs( CGameState *gs )
  593. {
  594. CStackInstance *s = new CStackInstance(stack.type, stack.count);
  595. sl.army->putStack(sl.slot, s);
  596. }
  597. DLL_EXPORT void RebalanceStacks::applyGs( CGameState *gs )
  598. {
  599. const CCreature *srcType = src.army->getCreature(src.slot);
  600. TQuantity srcCount = src.army->getStackCount(src.slot);
  601. if(srcCount == count) //moving whole stack
  602. {
  603. if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> merge
  604. {
  605. assert(c == srcType);
  606. src.army->eraseStack(src.slot);
  607. dst.army->changeStackCount(dst.slot, count);
  608. }
  609. else //move stack to an empty slot
  610. {
  611. CStackInstance *stackDetached = src.army->detachStack(src.slot);
  612. dst.army->putStack(dst.slot, stackDetached);
  613. }
  614. }
  615. else
  616. {
  617. if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> rebalance
  618. {
  619. assert(c == srcType);
  620. src.army->changeStackCount(src.slot, -count);
  621. dst.army->changeStackCount(dst.slot, count);
  622. }
  623. else //split stack to an empty slot
  624. {
  625. src.army->changeStackCount(src.slot, -count);
  626. dst.army->addToSlot(dst.slot, srcType->idNumber, count, false);
  627. }
  628. }
  629. }
  630. DLL_EXPORT void PutArtifact::applyGs( CGameState *gs )
  631. {
  632. assert(art->canBePutAt(al));
  633. al.hero->putArtifact(al.slot, art);
  634. //art->putAt(al.hero, al.slot);
  635. }
  636. DLL_EXPORT void EraseArtifact::applyGs( CGameState *gs )
  637. {
  638. CArtifactInstance *a = al.getArt();
  639. assert(a);
  640. a->removeFrom(al.hero, al.slot);
  641. }
  642. DLL_EXPORT void MoveArtifact::applyGs( CGameState *gs )
  643. {
  644. }
  645. DLL_EXPORT void SetAvailableArtifacts::applyGs( CGameState *gs )
  646. {
  647. if(id >= 0)
  648. {
  649. if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id].get()))
  650. {
  651. bm->artifacts = arts;
  652. }
  653. else
  654. {
  655. tlog1 << "Wrong black market id!" << std::endl;
  656. }
  657. }
  658. else
  659. {
  660. CGTownInstance::merchantArtifacts = arts;
  661. }
  662. }
  663. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  664. {
  665. gs->day = day;
  666. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  667. {
  668. CGHeroInstance *hero = gs->getHero(h.id);
  669. hero->movement = h.move;
  670. hero->mana = h.mana;
  671. }
  672. for(std::map<ui8, std::vector<si32> >::iterator i = res.begin(); i != res.end(); i++)
  673. {
  674. assert(i->first < PLAYER_LIMIT);
  675. std::vector<si32> &playerRes = gs->getPlayer(i->first)->resources;
  676. for(int j = 0; j < i->second.size(); j++)
  677. playerRes[j] = i->second[j];
  678. }
  679. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  680. h.applyGs(gs);
  681. if (specialWeek != NO_ACTION) //first pack applied, reset all effects and aplly new
  682. {
  683. //TODO? won't work for battles lasting several turns (not an issue now but...)
  684. gs->globalEffects.popBonuses(Bonus::OneDay); //works for children -> all game objs
  685. if(gs->getDate(1)) //new week, Monday that is
  686. {
  687. gs->globalEffects.popBonuses(Bonus::OneWeek); //works for children -> all game objs
  688. Bonus *b = new Bonus();
  689. b->duration = Bonus::ONE_WEEK;
  690. b->source = Bonus::SPECIAL_WEEK;
  691. b->effectRange = Bonus::NO_LIMIT;
  692. b->valType = Bonus::BASE_NUMBER; //certainly not intuitive
  693. switch (specialWeek)
  694. {
  695. case DOUBLE_GROWTH:
  696. b->val = 100;
  697. b->type = Bonus::CREATURE_GROWTH_PERCENT;
  698. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false));
  699. break;
  700. case BONUS_GROWTH:
  701. b->val = 5;
  702. b->type = Bonus::CREATURE_GROWTH;
  703. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false));
  704. break;
  705. case DEITYOFFIRE:
  706. b->val = 15;
  707. b->type = Bonus::CREATURE_GROWTH;
  708. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[42], true));
  709. break;
  710. case PLAGUE:
  711. b->val = -100; //no basic creatures
  712. b->type = Bonus::CREATURE_GROWTH_PERCENT;
  713. break;
  714. default:
  715. b->val = 0;
  716. }
  717. if (b->val)
  718. gs->globalEffects.addNewBonus(b);
  719. }
  720. }
  721. else //second pack is applied
  722. {
  723. //count days without town
  724. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  725. {
  726. if(i->second.towns.size() || gs->day == 1)
  727. i->second.daysWithoutCastle = 0;
  728. else
  729. i->second.daysWithoutCastle++;
  730. }
  731. if(resetBuilded) //reset amount of structures set in this turn in towns
  732. {
  733. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  734. t->builded = 0;
  735. }
  736. }
  737. }
  738. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  739. {
  740. CGObjectInstance *obj = gs->map->objects[id];
  741. if(!obj)
  742. {
  743. tlog1 << "Wrong object ID - property cannot be set!\n";
  744. return;
  745. }
  746. if(what == ObjProperty::OWNER)
  747. {
  748. if(obj->ID == TOWNI_TYPE)
  749. {
  750. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  751. if(t->tempOwner < PLAYER_LIMIT)
  752. gs->getPlayer(t->tempOwner)->towns -= t;
  753. if(val < PLAYER_LIMIT)
  754. gs->getPlayer(val)->towns.push_back(t);
  755. }
  756. }
  757. obj->setProperty(what,val);
  758. }
  759. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  760. {
  761. name.toString(gs->map->objects[id]->hoverName);
  762. }
  763. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  764. {
  765. CGHeroInstance* h = gs->getHero(heroid);
  766. h->level = level;
  767. //speciality
  768. h->UpdateSpeciality();
  769. }
  770. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  771. {
  772. gs->curB = info;
  773. gs->curB->localInit();
  774. }
  775. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  776. {
  777. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  778. gs->curB->round = round;
  779. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  780. {
  781. s->state -= DEFENDING;
  782. s->state -= WAITING;
  783. s->state -= MOVED;
  784. s->state -= HAD_MORALE;
  785. s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
  786. //regeneration
  787. if( s->hasBonusOfType(Bonus::HP_REGENERATION) && s->alive() )
  788. s->firstHPleft = std::min<ui32>( s->MaxHealth(), s->valOfBonuses(Bonus::HP_REGENERATION) );
  789. if( s->hasBonusOfType(Bonus::FULL_HP_REGENERATION) && s->alive() )
  790. s->firstHPleft = s->MaxHealth();
  791. //remove effects and restore only those with remaining turns in duration
  792. BonusList tmpEffects = s->bonuses;
  793. s->bonuses.removeSpells(Bonus::CASTED_SPELL);
  794. BOOST_FOREACH(Bonus *it, tmpEffects)
  795. {
  796. it->turnsRemain--;
  797. if(it->turnsRemain > 0)
  798. s->addNewBonus(it);
  799. }
  800. //the same as above for features
  801. BonusList tmpFeatures = s->bonuses;
  802. s->bonuses.clear();
  803. BOOST_FOREACH(Bonus *b, tmpFeatures)
  804. {
  805. if((b->duration & Bonus::N_TURNS) != 0)
  806. {
  807. b->turnsRemain--;
  808. if(b->turnsRemain > 0)
  809. s->addNewBonus(b);
  810. }
  811. else
  812. {
  813. s->addNewBonus(b);
  814. }
  815. }
  816. }
  817. }
  818. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  819. {
  820. gs->curB->activeStack = stack;
  821. CStack *st = gs->curB->getStack(stack);
  822. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  823. st->state.insert(HAD_MORALE);
  824. }
  825. void BattleResult::applyGs( CGameState *gs )
  826. {
  827. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  828. delete gs->curB->stacks[i];
  829. //remove any "until next battle" bonuses
  830. CGHeroInstance *h;
  831. h = gs->curB->heroes[0];
  832. if(h)
  833. h->bonuses.remove_if(Bonus::OneBattle);
  834. h = gs->curB->heroes[1];
  835. if(h)
  836. h->bonuses.remove_if(Bonus::OneBattle);
  837. gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
  838. gs->curB.dellNull();
  839. }
  840. void BattleStackMoved::applyGs( CGameState *gs )
  841. {
  842. gs->curB->getStack(stack)->position = tile;
  843. }
  844. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  845. {
  846. CStack * at = gs->curB->getStack(stackAttacked);
  847. at->count = newAmount;
  848. at->firstHPleft = newHP;
  849. if(killed())
  850. at->state -= ALIVE;
  851. //life drain handling
  852. for (int g=0; g<healedStacks.size(); ++g)
  853. {
  854. healedStacks[g].applyGs(gs);
  855. }
  856. }
  857. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  858. {
  859. CStack *attacker = gs->curB->getStack(stackAttacking);
  860. if(counter())
  861. attacker->counterAttacks--;
  862. if(shot())
  863. {
  864. //don't remove ammo if we have a working ammo cart
  865. bool hasAmmoCart = false;
  866. BOOST_FOREACH(const CStack * st, gs->curB->stacks)
  867. {
  868. if(st->owner == attacker->owner && st->getCreature()->idNumber == 148 && st->alive())
  869. {
  870. hasAmmoCart = true;
  871. break;
  872. }
  873. }
  874. if (!hasAmmoCart)
  875. {
  876. attacker->shots--;
  877. }
  878. }
  879. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  880. stackAttacked.applyGs(gs);
  881. attacker->bonuses.remove_if(Bonus::UntilAttack);
  882. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  883. {
  884. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  885. stack->bonuses.remove_if(Bonus::UntilBeingAttacked);
  886. }
  887. }
  888. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  889. {
  890. CStack *st = gs->curB->getStack(ba.stackNumber);
  891. if(ba.actionType != BattleAction::HERO_SPELL) //don't check for stack if it's custom action by hero
  892. assert(st);
  893. switch(ba.actionType)
  894. {
  895. case BattleAction::DEFEND:
  896. st->state.insert(DEFENDING);
  897. break;
  898. case BattleAction::WAIT:
  899. st->state.insert(WAITING);
  900. return;
  901. case BattleAction::NO_ACTION: case BattleAction::WALK: case BattleAction::WALK_AND_ATTACK:
  902. case BattleAction::SHOOT: case BattleAction::CATAPULT: case BattleAction::MONSTER_SPELL:
  903. case BattleAction::BAD_MORALE: case BattleAction::STACK_HEAL:
  904. st->state.insert(MOVED);
  905. break;
  906. }
  907. if(st)
  908. st->state -= 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)
  909. }
  910. DLL_EXPORT void BattleSpellCast::applyGs( CGameState *gs )
  911. {
  912. assert(gs->curB);
  913. CGHeroInstance *h = (side) ? gs->curB->heroes[1] : gs->curB->heroes[0];
  914. if(h && castedByHero)
  915. {
  916. int spellCost = 0;
  917. if(gs->curB)
  918. {
  919. spellCost = gs->curB->getSpellCost(VLC->spellh->spells[id], h);
  920. }
  921. else
  922. {
  923. spellCost = VLC->spellh->spells[id]->costs[skill];
  924. }
  925. h->mana -= spellCost;
  926. if(h->mana < 0) h->mana = 0;
  927. }
  928. if(side >= 0 && side < 2 && castedByHero)
  929. {
  930. gs->curB->castSpells[side]++;
  931. }
  932. if(id == 35 || id == 78) //dispel and dispel helpful spells
  933. {
  934. bool onlyHelpful = id == 78;
  935. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  936. {
  937. CStack *s = gs->curB->getStack(*it);
  938. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  939. {
  940. BonusList remainingEff;
  941. //WTF?
  942. for (BonusList::iterator it = remainingEff.begin(); it != remainingEff.end(); it++)
  943. {
  944. if (onlyHelpful && VLC->spellh->spells[ (*it)->id ]->positiveness != 1)
  945. {
  946. remainingEff.push_back(*it);
  947. }
  948. }
  949. s->bonuses.removeSpells(Bonus::CASTED_SPELL); //removing all effects
  950. s->bonuses = remainingEff; //assigning effects that should remain
  951. //removing all features from spells
  952. BonusList tmpFeatures = s->bonuses;
  953. s->bonuses.clear();
  954. BOOST_FOREACH(Bonus *b, tmpFeatures)
  955. {
  956. const CSpell *sp = b->sourceSpell();
  957. if(sp && sp->positiveness != 1) //if(b->source != HeroBonus::SPELL_EFFECT || b.positiveness != 1)
  958. s->addNewBonus(b);
  959. }
  960. }
  961. }
  962. }
  963. //elemental summoning
  964. if(id >= 66 && id <= 69)
  965. {
  966. int creID;
  967. switch(id)
  968. {
  969. case 66:
  970. creID = 114; //fire elemental
  971. break;
  972. case 67:
  973. creID = 113; //earth elemental
  974. break;
  975. case 68:
  976. creID = 115; //water elemental
  977. break;
  978. case 69:
  979. creID = 112; //air elemental
  980. break;
  981. }
  982. // const int3 & tile = gs->curB->tile;
  983. // TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  984. int pos; //position of stack on the battlefield - to be calculated
  985. bool ac[BFIELD_SIZE];
  986. std::set<int> occupyable;
  987. bool twoHex = VLC->creh->creatures[creID]->isDoubleWide();
  988. bool flying = VLC->creh->creatures[creID]->isFlying();// vstd::contains(VLC->creh->creatures[creID]->bonuses, Bonus::FLYING);
  989. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  990. for(int g=0; g<BFIELD_SIZE; ++g)
  991. {
  992. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  993. {
  994. pos = g;
  995. break;
  996. }
  997. }
  998. CStackInstance *csi = new CStackInstance(creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id]->powers[skill]); //deleted by d-tor of summoned stack
  999. csi->setArmyObj(h);
  1000. CStack * summonedStack = gs->curB->generateNewStack(*csi, gs->curB->stacks.size(), !side, 255, pos);
  1001. summonedStack->state.insert(SUMMONED);
  1002. //summonedStack->addNewBonus( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1003. gs->curB->stacks.push_back(summonedStack);
  1004. }
  1005. }
  1006. void actualizeEffect(CStack * s, Bonus & ef)
  1007. {
  1008. //actualizing effects vector
  1009. BOOST_FOREACH(Bonus *it, s->bonuses)
  1010. {
  1011. if(it->id == ef.id)
  1012. {
  1013. it->turnsRemain = std::max(it->turnsRemain, ef.turnsRemain);
  1014. }
  1015. }
  1016. //actualizing features vector
  1017. BonusList sf;
  1018. s->stackEffectToFeature(sf, ef);
  1019. BOOST_FOREACH(const Bonus *fromEffect, sf)
  1020. {
  1021. BOOST_FOREACH(Bonus *stackBonus, s->bonuses) //TODO: optimize
  1022. {
  1023. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == fromEffect->type && stackBonus->subtype == fromEffect->subtype)
  1024. {
  1025. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, ef.turnsRemain);
  1026. }
  1027. }
  1028. }
  1029. }
  1030. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  1031. {
  1032. BOOST_FOREACH(ui32 id, stacks)
  1033. {
  1034. CStack *s = gs->curB->getStack(id);
  1035. if(s)
  1036. {
  1037. if(effect.id == 42 || !s->hasBonus(Selector::source(Bonus::CASTED_SPELL, effect.id)))//disrupting ray or not on the list - just add
  1038. {
  1039. s->addNewBonus(new Bonus(effect));
  1040. BonusList sf;
  1041. s->stackEffectToFeature(sf, effect);
  1042. BOOST_FOREACH(Bonus *fromEffect, sf)
  1043. {
  1044. s->addNewBonus(fromEffect);
  1045. }
  1046. }
  1047. else //just actualize
  1048. {
  1049. actualizeEffect(s, effect);
  1050. }
  1051. }
  1052. else
  1053. tlog1 << "Cannot find stack " << id << std::endl;
  1054. }
  1055. }
  1056. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  1057. {
  1058. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  1059. stackAttacked.applyGs(gs);
  1060. }
  1061. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  1062. {
  1063. for(int g=0; g<healedStacks.size(); ++g)
  1064. {
  1065. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  1066. //checking if we resurrect a stack that is under a living stack
  1067. std::vector<int> access = gs->curB->getAccessibility(changedStack->ID, true);
  1068. bool acc[BFIELD_SIZE];
  1069. for(int h=0; h<BFIELD_SIZE; ++h)
  1070. acc[h] = false;
  1071. for(int h=0; h<access.size(); ++h)
  1072. acc[access[h]] = true;
  1073. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  1074. changedStack->doubleWide(), changedStack->attackerOwned,
  1075. changedStack->hasBonusOfType(Bonus::FLYING), true))
  1076. return; //position is already occupied
  1077. //applying changes
  1078. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1079. if(resurrected)
  1080. {
  1081. changedStack->state.insert(ALIVE);
  1082. if(healedStacks[g].lowLevelResurrection)
  1083. changedStack->state.insert(SUMMONED);
  1084. //changedStack->addNewBonus( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1085. }
  1086. //int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1087. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1088. changedStack->count += res;
  1089. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1090. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1091. {
  1092. changedStack->firstHPleft -= changedStack->MaxHealth();
  1093. if(changedStack->baseAmount > changedStack->count)
  1094. {
  1095. changedStack->count += 1;
  1096. }
  1097. }
  1098. amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1099. //removal of negative effects
  1100. if(resurrected)
  1101. {
  1102. for (BonusList::iterator it = changedStack->bonuses.begin(); it != changedStack->bonuses.end(); it++)
  1103. {
  1104. if(VLC->spellh->spells[(*it)->id]->positiveness < 0)
  1105. {
  1106. changedStack->bonuses.erase(it);
  1107. }
  1108. }
  1109. //removing all features from negative spells
  1110. BonusList tmpFeatures = changedStack->bonuses;
  1111. changedStack->bonuses.clear();
  1112. BOOST_FOREACH(Bonus *b, tmpFeatures)
  1113. {
  1114. const CSpell *s = b->sourceSpell();
  1115. if(s && s->positiveness >= 0)
  1116. {
  1117. changedStack->addNewBonus(b);
  1118. }
  1119. }
  1120. }
  1121. }
  1122. }
  1123. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  1124. {
  1125. if(gs->curB) //if there is a battle
  1126. {
  1127. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1128. {
  1129. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1130. {
  1131. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1132. {
  1133. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1134. break;
  1135. }
  1136. }
  1137. }
  1138. }
  1139. }
  1140. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  1141. {
  1142. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1143. {
  1144. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1145. {
  1146. gs->curB->si.wallState[it->first.first] =
  1147. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1148. }
  1149. }
  1150. }
  1151. DLL_EXPORT void BattleStacksRemoved::applyGs( CGameState *gs )
  1152. {
  1153. if(!gs->curB)
  1154. return;
  1155. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1156. {
  1157. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1158. {
  1159. if(gs->curB->stacks[b]->ID == *it) //if found
  1160. {
  1161. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1162. break;
  1163. }
  1164. }
  1165. }
  1166. }
  1167. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  1168. {
  1169. gs->currentPlayer = player;
  1170. }
  1171. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  1172. {
  1173. gs->getPlayer(player)->currentSelection = id;
  1174. }
  1175. DLL_EXPORT Component::Component(const CStackBasicDescriptor &stack)
  1176. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1177. {
  1178. }