NetPacksLib.cpp 33 KB

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