NetPacksLib.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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. boost::unordered_set<int3, ShashInt3> tilesRevealed;
  139. for (size_t i = 0; i < gs->map->objects.size(); i++)
  140. {
  141. if (gs->map->objects[i])
  142. {
  143. switch(gs->map->objects[i]->ID)
  144. {
  145. case 34://hero
  146. case 53://mine
  147. case 98://town
  148. case 220:
  149. if(vstd::contains(team->players, gs->map->objects[i]->tempOwner)) //check owned observators
  150. gs->map->objects[i]->getSightTiles(tilesRevealed);
  151. break;
  152. }
  153. }
  154. }
  155. BOOST_FOREACH(int3 t, tilesRevealed) //probably not the most optimal solution ever
  156. team->fogOfWarMap[t.x][t.y][t.z] = 1;
  157. }
  158. }
  159. DLL_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. 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.front()->ID == 8); //the only vis obj at dest is Boat
  319. CGBoat *boat = static_cast<CGBoat*>(tt.visitableObjects.front());
  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_EXPORT void SetGarrisons::applyGs( CGameState *gs )
  345. // {
  346. // for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  347. // {
  348. // CArmedInstance *ai = static_cast<CArmedInstance*>(gs->map->objects[i->first]);
  349. // ai->setToArmy(i->second);
  350. // if(ai->ID==TOWNI_TYPE && (static_cast<CGTownInstance*>(ai))->garrisonHero) //if there is a hero in garrison then we must update also his army
  351. // const_cast<CGHeroInstance*>((static_cast<CGTownInstance*>(ai))->garrisonHero)->setToArmy(i->second);
  352. // else if(ai->ID==HEROI_TYPE)
  353. // {
  354. // CGHeroInstance *h = static_cast<CGHeroInstance*>(ai);
  355. // CGTownInstance *t = const_cast<CGTownInstance *>(h->visitedTown);
  356. // if(t && h->inTownGarrison)
  357. // t->setToArmy(i->second);
  358. // }
  359. // }
  360. // }
  361. DLL_EXPORT void NewStructures::applyGs( CGameState *gs )
  362. {
  363. CGTownInstance *t = gs->getTown(tid);
  364. BOOST_FOREACH(si32 id,bid)
  365. {
  366. t->builtBuildings.insert(id);
  367. }
  368. t->builded = builded;
  369. }
  370. DLL_EXPORT void RazeStructures::applyGs( CGameState *gs )
  371. {
  372. CGTownInstance *t = gs->getTown(tid);
  373. BOOST_FOREACH(si32 id,bid)
  374. {
  375. t->builtBuildings.erase(id);
  376. }
  377. t->destroyed = destroyed; //yeaha
  378. }
  379. DLL_EXPORT void SetAvailableCreatures::applyGs( CGameState *gs )
  380. {
  381. CGDwelling *dw = dynamic_cast<CGDwelling*>(gs->map->objects[tid].get());
  382. assert(dw);
  383. dw->creatures = creatures;
  384. }
  385. DLL_EXPORT void SetHeroesInTown::applyGs( CGameState *gs )
  386. {
  387. CGTownInstance *t = gs->getTown(tid);
  388. CGHeroInstance *v = gs->getHero(visiting),
  389. *g = gs->getHero(garrison);
  390. t->visitingHero = v;
  391. t->garrisonHero = g;
  392. if(v)
  393. {
  394. v->visitedTown = t;
  395. v->inTownGarrison = false;
  396. gs->map->addBlockVisTiles(v);
  397. }
  398. if(g)
  399. {
  400. g->visitedTown = t;
  401. g->inTownGarrison = true;
  402. gs->map->removeBlockVisTiles(g);
  403. }
  404. }
  405. // DLL_EXPORT void SetHeroArtifacts::applyGs( CGameState *gs )
  406. // {
  407. // CGHeroInstance *h = gs->getHero(hid);
  408. // for(std::map<ui16, const CArtifact*>::const_iterator i = h->artifWorn.begin(); i != h->artifWorn.end(); i++)
  409. // if(!vstd::contains(artifWorn,i->first) || artifWorn[i->first] != i->second)
  410. // unequiped.push_back(i->second);
  411. //
  412. // for(std::map<ui16, const CArtifact*>::iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  413. // if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  414. // {
  415. // equiped.push_back(i->second);
  416. // }
  417. //
  418. // //update hero data
  419. // h->artifacts = artifacts;
  420. // h->artifWorn = artifWorn;
  421. // }
  422. //
  423. // DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, const CArtifact* art)
  424. // {
  425. // if(!art)
  426. // {
  427. // if(pos<19)
  428. // VLC->arth->unequipArtifact(artifWorn, pos);
  429. // else if (pos - 19 < artifacts.size())
  430. // artifacts.erase(artifacts.begin() + (pos - 19));
  431. // }
  432. // else
  433. // {
  434. // if (pos < 19)
  435. // {
  436. // VLC->arth->equipArtifact(artifWorn, pos, art);
  437. // }
  438. // else // Goes into the backpack.
  439. // {
  440. // if(pos - 19 < artifacts.size())
  441. // artifacts.insert(artifacts.begin() + (pos - 19), art);
  442. // else
  443. // artifacts.push_back(art);
  444. // }
  445. // }
  446. // }
  447. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  448. {
  449. assert(vstd::contains(gs->hpool.heroesPool, hid));
  450. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  451. CGTownInstance *t = gs->getTown(tid);
  452. h->setOwner(player);
  453. h->pos = tile;
  454. h->movement = h->maxMovePoints(true);
  455. gs->hpool.heroesPool.erase(hid);
  456. if(h->id < 0)
  457. {
  458. h->id = gs->map->objects.size();
  459. gs->map->objects.push_back(h);
  460. }
  461. else
  462. gs->map->objects[h->id] = h;
  463. h->initHeroDefInfo();
  464. gs->map->heroes.push_back(h);
  465. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  466. h->initObj();
  467. gs->map->addBlockVisTiles(h);
  468. if(t)
  469. {
  470. t->visitingHero = h;
  471. h->visitedTown = t;
  472. }
  473. h->inTownGarrison = false;
  474. }
  475. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  476. {
  477. CGHeroInstance *h = gs->getHero(id);
  478. gs->map->removeBlockVisTiles(h,true);
  479. h->setOwner(player);
  480. h->movement = h->maxMovePoints(true);
  481. h->initHeroDefInfo();
  482. gs->map->heroes.push_back(h);
  483. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  484. gs->map->addBlockVisTiles(h);
  485. h->inTownGarrison = false;
  486. }
  487. DLL_EXPORT void NewObject::applyGs( CGameState *gs )
  488. {
  489. CGObjectInstance *o = NULL;
  490. switch(ID)
  491. {
  492. case 8:
  493. o = new CGBoat();
  494. break;
  495. case 54: //probably more options will be needed
  496. o = new CGCreature();
  497. {
  498. //CStackInstance hlp;
  499. CGCreature *cre = static_cast<CGCreature*>(o);
  500. //cre->slots[0] = hlp;
  501. cre->notGrowingTeam = cre->neverFlees = 0;
  502. cre->character = 2;
  503. cre->gainedArtifact = -1;
  504. }
  505. break;
  506. default:
  507. o = new CGObjectInstance();
  508. break;
  509. }
  510. o->ID = ID;
  511. o->subID = subID;
  512. o->pos = pos;
  513. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  514. id = o->id = gs->map->objects.size();
  515. o->hoverName = VLC->generaltexth->names[ID];
  516. switch(ID)
  517. {
  518. case 54: //cfreature
  519. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  520. assert(o->defInfo);
  521. break;
  522. case 124://hole
  523. const TerrainTile &t = gs->map->getTile(pos);
  524. o->defInfo = VLC->dobjinfo->gobjs[ID][t.tertype];
  525. assert(o->defInfo);
  526. break;
  527. }
  528. gs->map->objects.push_back(o);
  529. gs->map->addBlockVisTiles(o);
  530. o->initObj();
  531. assert(o->defInfo);
  532. }
  533. DLL_EXPORT void NewArtifact::applyGs( CGameState *gs )
  534. {
  535. assert(!vstd::contains(gs->map->artInstances, art));
  536. gs->map->addNewArtifactInstance(art);
  537. assert(!art->parents.size());
  538. art->setType(art->artType);
  539. }
  540. DLL_EXPORT const CStackInstance * StackLocation::getStack()
  541. {
  542. if(!army->hasStackAtSlot(slot))
  543. {
  544. tlog2 << "Warning: " << army->nodeName() << " dont have a stack at slot " << slot << std::endl;
  545. return NULL;
  546. }
  547. return &army->getStack(slot);
  548. }
  549. DLL_EXPORT const CArtifactInstance *ArtifactLocation::getArt() const
  550. {
  551. const ArtSlotInfo *s = getSlot();
  552. if(s && s->artifact)
  553. {
  554. if(!s->locked)
  555. return s->artifact;
  556. else
  557. {
  558. tlog3 << "ArtifactLocation::getArt: That location is locked!\n";
  559. return NULL;
  560. }
  561. }
  562. return NULL;
  563. return NULL;
  564. }
  565. DLL_EXPORT CArtifactInstance *ArtifactLocation::getArt()
  566. {
  567. const ArtifactLocation *t = this;
  568. return const_cast<CArtifactInstance*>(t->getArt());
  569. }
  570. DLL_EXPORT const ArtSlotInfo *ArtifactLocation::getSlot() const
  571. {
  572. return hero->getSlot(slot);
  573. }
  574. DLL_EXPORT 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_EXPORT void SetStackType::applyGs( CGameState *gs )
  582. {
  583. sl.army->setStackType(sl.slot, type);
  584. }
  585. DLL_EXPORT void EraseStack::applyGs( CGameState *gs )
  586. {
  587. sl.army->eraseStack(sl.slot);
  588. }
  589. DLL_EXPORT 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_EXPORT 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_EXPORT 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. src.army->eraseStack(src.slot);
  611. dst.army->changeStackCount(dst.slot, count);
  612. }
  613. else //move stack to an empty slot
  614. {
  615. CStackInstance *stackDetached = src.army->detachStack(src.slot);
  616. dst.army->putStack(dst.slot, stackDetached);
  617. }
  618. }
  619. else
  620. {
  621. if(const CCreature *c = dst.army->getCreature(dst.slot)) //stack at dest -> rebalance
  622. {
  623. assert(c == srcType);
  624. src.army->changeStackCount(src.slot, -count);
  625. dst.army->changeStackCount(dst.slot, count);
  626. }
  627. else //split stack to an empty slot
  628. {
  629. src.army->changeStackCount(src.slot, -count);
  630. dst.army->addToSlot(dst.slot, srcType->idNumber, count, false);
  631. }
  632. }
  633. }
  634. DLL_EXPORT void PutArtifact::applyGs( CGameState *gs )
  635. {
  636. assert(art->canBePutAt(al));
  637. al.hero->putArtifact(al.slot, art);
  638. //art->putAt(al.hero, al.slot);
  639. }
  640. DLL_EXPORT void EraseArtifact::applyGs( CGameState *gs )
  641. {
  642. CArtifactInstance *a = al.getArt();
  643. assert(a);
  644. a->removeFrom(al.hero, al.slot);
  645. }
  646. DLL_EXPORT void MoveArtifact::applyGs( CGameState *gs )
  647. {
  648. CArtifactInstance *a = src.getArt();
  649. if(dst.slot < Arts::BACKPACK_START)
  650. assert(!dst.getArt());
  651. a->move(src, dst);
  652. }
  653. DLL_EXPORT void AssembledArtifact::applyGs( CGameState *gs )
  654. {
  655. CGHeroInstance *h = al.hero;
  656. const CArtifactInstance *transformedArt = al.getArt();
  657. assert(transformedArt);
  658. assert(vstd::contains(transformedArt->assemblyPossibilities(al.hero), builtArt));
  659. CCombinedArtifactInstance *combinedArt = new CCombinedArtifactInstance(builtArt);
  660. //retreive all constituents
  661. BOOST_FOREACH(si32 constituentID, *builtArt->constituents)
  662. {
  663. int pos = h->getArtPos(constituentID);
  664. assert(pos >= 0);
  665. CArtifactInstance *constituentInstance = h->getArt(pos);
  666. //move constituent from hero to be part of new, combined artifact
  667. constituentInstance->removeFrom(h, pos);
  668. combinedArt->addAsConstituent(constituentInstance, pos);
  669. }
  670. //put new combined artifacts
  671. combinedArt->putAt(h, al.slot);
  672. }
  673. DLL_EXPORT void DisassembledArtifact::applyGs( CGameState *gs )
  674. {
  675. CGHeroInstance *h = al.hero;
  676. CCombinedArtifactInstance *disassembled = dynamic_cast<CCombinedArtifactInstance*>(al.getArt());
  677. assert(disassembled);
  678. std::vector<CCombinedArtifactInstance::ConstituentInfo> constituents = disassembled->constituentsInfo;
  679. disassembled->removeFrom(h, al.slot);
  680. BOOST_FOREACH(CCombinedArtifactInstance::ConstituentInfo &ci, constituents)
  681. {
  682. ci.art->detachFrom(disassembled);
  683. ci.art->putAt(h, ci.slot >= 0 ? ci.slot : al.slot); //-1 is slot of main constituent -> it'll replace combined artifact in its pos
  684. }
  685. delNull(disassembled);
  686. }
  687. DLL_EXPORT void SetAvailableArtifacts::applyGs( CGameState *gs )
  688. {
  689. if(id >= 0)
  690. {
  691. if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id].get()))
  692. {
  693. bm->artifacts = arts;
  694. }
  695. else
  696. {
  697. tlog1 << "Wrong black market id!" << std::endl;
  698. }
  699. }
  700. else
  701. {
  702. CGTownInstance::merchantArtifacts = arts;
  703. }
  704. }
  705. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  706. {
  707. gs->day = day;
  708. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  709. {
  710. CGHeroInstance *hero = gs->getHero(h.id);
  711. hero->movement = h.move;
  712. hero->mana = h.mana;
  713. }
  714. for(std::map<ui8, std::vector<si32> >::iterator i = res.begin(); i != res.end(); i++)
  715. {
  716. assert(i->first < PLAYER_LIMIT);
  717. std::vector<si32> &playerRes = gs->getPlayer(i->first)->resources;
  718. for(int j = 0; j < i->second.size(); j++)
  719. playerRes[j] = i->second[j];
  720. }
  721. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  722. h.applyGs(gs);
  723. if (specialWeek != NO_ACTION) //first pack applied, reset all effects and aplly new
  724. {
  725. //TODO? won't work for battles lasting several turns (not an issue now but...)
  726. gs->globalEffects.popBonuses(Bonus::OneDay); //works for children -> all game objs
  727. if(gs->getDate(1)) //new week, Monday that is
  728. {
  729. gs->globalEffects.popBonuses(Bonus::OneWeek); //works for children -> all game objs
  730. Bonus *b = new Bonus();
  731. b->duration = Bonus::ONE_WEEK;
  732. b->source = Bonus::SPECIAL_WEEK;
  733. b->effectRange = Bonus::NO_LIMIT;
  734. b->valType = Bonus::BASE_NUMBER; //certainly not intuitive
  735. switch (specialWeek)
  736. {
  737. case DOUBLE_GROWTH:
  738. b->val = 100;
  739. b->type = Bonus::CREATURE_GROWTH_PERCENT;
  740. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false));
  741. break;
  742. case BONUS_GROWTH:
  743. b->val = 5;
  744. b->type = Bonus::CREATURE_GROWTH;
  745. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false));
  746. break;
  747. case DEITYOFFIRE:
  748. b->val = 15;
  749. b->type = Bonus::CREATURE_GROWTH;
  750. b->limiter.reset(new CCreatureTypeLimiter(*VLC->creh->creatures[42], true));
  751. break;
  752. case PLAGUE:
  753. b->val = -100; //no basic creatures
  754. b->type = Bonus::CREATURE_GROWTH_PERCENT;
  755. break;
  756. default:
  757. b->val = 0;
  758. }
  759. if (b->val)
  760. gs->globalEffects.addNewBonus(b);
  761. }
  762. }
  763. else //second pack is applied
  764. {
  765. //count days without town
  766. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  767. {
  768. if(i->second.towns.size() || gs->day == 1)
  769. i->second.daysWithoutCastle = 0;
  770. else
  771. i->second.daysWithoutCastle++;
  772. }
  773. if(resetBuilded) //reset amount of structures set in this turn in towns
  774. {
  775. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  776. t->builded = 0;
  777. }
  778. }
  779. }
  780. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  781. {
  782. CGObjectInstance *obj = gs->map->objects[id];
  783. if(!obj)
  784. {
  785. tlog1 << "Wrong object ID - property cannot be set!\n";
  786. return;
  787. }
  788. if(what == ObjProperty::OWNER)
  789. {
  790. if(obj->ID == TOWNI_TYPE)
  791. {
  792. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  793. if(t->tempOwner < PLAYER_LIMIT)
  794. gs->getPlayer(t->tempOwner)->towns -= t;
  795. if(val < PLAYER_LIMIT)
  796. gs->getPlayer(val)->towns.push_back(t);
  797. }
  798. }
  799. obj->setProperty(what,val);
  800. }
  801. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  802. {
  803. name.toString(gs->map->objects[id]->hoverName);
  804. }
  805. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  806. {
  807. CGHeroInstance* h = gs->getHero(heroid);
  808. h->level = level;
  809. //speciality
  810. h->UpdateSpeciality();
  811. }
  812. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  813. {
  814. gs->curB = info;
  815. gs->curB->localInit();
  816. }
  817. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  818. {
  819. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  820. gs->curB->round = round;
  821. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  822. {
  823. s->state -= DEFENDING;
  824. s->state -= WAITING;
  825. s->state -= MOVED;
  826. s->state -= HAD_MORALE;
  827. s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
  828. //regeneration
  829. if( s->hasBonusOfType(Bonus::HP_REGENERATION) && s->alive() )
  830. s->firstHPleft = std::min<ui32>( s->MaxHealth(), s->valOfBonuses(Bonus::HP_REGENERATION) );
  831. if( s->hasBonusOfType(Bonus::FULL_HP_REGENERATION) && s->alive() )
  832. s->firstHPleft = s->MaxHealth();
  833. //remove effects and restore only those with remaining turns in duration
  834. BonusList tmpEffects = s->bonuses;
  835. s->bonuses.removeSpells(Bonus::SPELL_EFFECT);
  836. BOOST_FOREACH(Bonus *it, tmpEffects)
  837. {
  838. it->turnsRemain--;
  839. if(it->turnsRemain > 0)
  840. s->addNewBonus(it);
  841. }
  842. //the same as above for features
  843. BonusList tmpFeatures = s->bonuses;
  844. s->bonuses.clear();
  845. BOOST_FOREACH(Bonus *b, tmpFeatures)
  846. {
  847. if((b->duration & Bonus::N_TURNS) != 0)
  848. {
  849. b->turnsRemain--;
  850. if(b->turnsRemain > 0)
  851. s->addNewBonus(b);
  852. }
  853. else
  854. {
  855. s->addNewBonus(b);
  856. }
  857. }
  858. }
  859. }
  860. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  861. {
  862. gs->curB->activeStack = stack;
  863. CStack *st = gs->curB->getStack(stack);
  864. //remove bonuses that last until when stack gets new turn
  865. st->bonuses.remove_if(Bonus::UntilGetsTurn);
  866. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  867. st->state.insert(HAD_MORALE);
  868. }
  869. void BattleResult::applyGs( CGameState *gs )
  870. {
  871. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  872. delete gs->curB->stacks[i];
  873. //remove any "until next battle" bonuses
  874. CGHeroInstance *h;
  875. h = gs->curB->heroes[0];
  876. if(h)
  877. h->bonuses.remove_if(Bonus::OneBattle);
  878. h = gs->curB->heroes[1];
  879. if(h)
  880. h->bonuses.remove_if(Bonus::OneBattle);
  881. gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
  882. gs->curB.dellNull();
  883. }
  884. void BattleStackMoved::applyGs( CGameState *gs )
  885. {
  886. gs->curB->getStack(stack)->position = tile;
  887. }
  888. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  889. {
  890. CStack * at = gs->curB->getStack(stackAttacked);
  891. at->count = newAmount;
  892. at->firstHPleft = newHP;
  893. if(killed())
  894. at->state -= ALIVE;
  895. //life drain handling
  896. for (int g=0; g<healedStacks.size(); ++g)
  897. {
  898. healedStacks[g].applyGs(gs);
  899. }
  900. }
  901. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  902. {
  903. CStack *attacker = gs->curB->getStack(stackAttacking);
  904. if(counter())
  905. attacker->counterAttacks--;
  906. if(shot())
  907. {
  908. //don't remove ammo if we have a working ammo cart
  909. bool hasAmmoCart = false;
  910. BOOST_FOREACH(const CStack * st, gs->curB->stacks)
  911. {
  912. if(st->owner == attacker->owner && st->getCreature()->idNumber == 148 && st->alive())
  913. {
  914. hasAmmoCart = true;
  915. break;
  916. }
  917. }
  918. if (!hasAmmoCart)
  919. {
  920. attacker->shots--;
  921. }
  922. }
  923. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  924. stackAttacked.applyGs(gs);
  925. attacker->bonuses.remove_if(Bonus::UntilAttack);
  926. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  927. {
  928. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  929. stack->bonuses.remove_if(Bonus::UntilBeingAttacked);
  930. }
  931. }
  932. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  933. {
  934. CStack *st = gs->curB->getStack(ba.stackNumber);
  935. if(ba.actionType != BattleAction::HERO_SPELL) //don't check for stack if it's custom action by hero
  936. assert(st);
  937. switch(ba.actionType)
  938. {
  939. case BattleAction::DEFEND:
  940. st->state.insert(DEFENDING);
  941. break;
  942. case BattleAction::WAIT:
  943. st->state.insert(WAITING);
  944. return;
  945. case BattleAction::NO_ACTION: case BattleAction::WALK: case BattleAction::WALK_AND_ATTACK:
  946. case BattleAction::SHOOT: case BattleAction::CATAPULT: case BattleAction::MONSTER_SPELL:
  947. case BattleAction::BAD_MORALE: case BattleAction::STACK_HEAL:
  948. st->state.insert(MOVED);
  949. break;
  950. }
  951. if(st)
  952. 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)
  953. }
  954. DLL_EXPORT void BattleSpellCast::applyGs( CGameState *gs )
  955. {
  956. assert(gs->curB);
  957. CGHeroInstance *h = (side) ? gs->curB->heroes[1] : gs->curB->heroes[0];
  958. if(h && castedByHero)
  959. {
  960. int spellCost = 0;
  961. if(gs->curB)
  962. {
  963. spellCost = gs->curB->getSpellCost(VLC->spellh->spells[id], h);
  964. }
  965. else
  966. {
  967. spellCost = VLC->spellh->spells[id]->costs[skill];
  968. }
  969. h->mana -= spellCost;
  970. if(h->mana < 0) h->mana = 0;
  971. }
  972. if(side >= 0 && side < 2 && castedByHero)
  973. {
  974. gs->curB->castSpells[side]++;
  975. }
  976. if(id == 35 || id == 78) //dispel and dispel helpful spells
  977. {
  978. bool onlyHelpful = id == 78;
  979. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  980. {
  981. CStack *s = gs->curB->getStack(*it);
  982. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  983. {
  984. BonusList remainingEff;
  985. //WTF?
  986. for (BonusList::iterator it = remainingEff.begin(); it != remainingEff.end(); it++)
  987. {
  988. if (onlyHelpful && VLC->spellh->spells[ (*it)->id ]->positiveness != 1)
  989. {
  990. remainingEff.push_back(*it);
  991. }
  992. }
  993. s->bonuses.removeSpells(Bonus::SPELL_EFFECT); //removing all effects
  994. s->bonuses = remainingEff; //assigning effects that should remain
  995. //removing all features from spells
  996. BonusList tmpFeatures = s->bonuses;
  997. s->bonuses.clear();
  998. BOOST_FOREACH(Bonus *b, tmpFeatures)
  999. {
  1000. const CSpell *sp = b->sourceSpell();
  1001. if(sp && sp->positiveness != 1) //if(b->source != HeroBonus::SPELL_EFFECT || b.positiveness != 1)
  1002. s->addNewBonus(b);
  1003. }
  1004. }
  1005. }
  1006. }
  1007. //elemental summoning
  1008. if(id >= 66 && id <= 69)
  1009. {
  1010. int creID;
  1011. switch(id)
  1012. {
  1013. case 66:
  1014. creID = 114; //fire elemental
  1015. break;
  1016. case 67:
  1017. creID = 113; //earth elemental
  1018. break;
  1019. case 68:
  1020. creID = 115; //water elemental
  1021. break;
  1022. case 69:
  1023. creID = 112; //air elemental
  1024. break;
  1025. }
  1026. // const int3 & tile = gs->curB->tile;
  1027. // TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  1028. int pos; //position of stack on the battlefield - to be calculated
  1029. bool ac[BFIELD_SIZE];
  1030. std::set<THex> occupyable;
  1031. bool twoHex = VLC->creh->creatures[creID]->isDoubleWide();
  1032. bool flying = VLC->creh->creatures[creID]->isFlying();// vstd::contains(VLC->creh->creatures[creID]->bonuses, Bonus::FLYING);
  1033. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  1034. for(int g=0; g<BFIELD_SIZE; ++g)
  1035. {
  1036. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  1037. {
  1038. pos = g;
  1039. break;
  1040. }
  1041. }
  1042. CStackInstance *csi = new CStackInstance(creID, h->getPrimSkillLevel(2) * VLC->spellh->spells[id]->powers[skill]); //deleted by d-tor of summoned stack
  1043. csi->setArmyObj(h);
  1044. CStack * summonedStack = gs->curB->generateNewStack(*csi, gs->curB->stacks.size(), !side, 255, pos);
  1045. summonedStack->state.insert(SUMMONED);
  1046. //summonedStack->addNewBonus( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1047. gs->curB->stacks.push_back(summonedStack);
  1048. }
  1049. }
  1050. void actualizeEffect(CStack * s, const std::vector<Bonus> & ef)
  1051. {
  1052. //actualizing features vector
  1053. BOOST_FOREACH(const Bonus &fromEffect, ef)
  1054. {
  1055. BOOST_FOREACH(Bonus *stackBonus, s->bonuses) //TODO: optimize
  1056. {
  1057. if(stackBonus->source == Bonus::SPELL_EFFECT && stackBonus->type == fromEffect.type && stackBonus->subtype == fromEffect.subtype)
  1058. {
  1059. stackBonus->turnsRemain = std::max(stackBonus->turnsRemain, fromEffect.turnsRemain);
  1060. }
  1061. }
  1062. }
  1063. }
  1064. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  1065. {
  1066. BOOST_FOREACH(ui32 id, stacks)
  1067. {
  1068. CStack *s = gs->curB->getStack(id);
  1069. if(s)
  1070. {
  1071. int id = effect.begin()->id; //effects' source ID
  1072. if(id == 47 || !s->hasBonus(Selector::source(Bonus::SPELL_EFFECT, id)))//disrupting ray or not on the list - just add
  1073. {
  1074. BOOST_FOREACH(Bonus &fromEffect, effect)
  1075. {
  1076. s->addNewBonus( new Bonus(fromEffect));
  1077. }
  1078. }
  1079. else //just actualize
  1080. {
  1081. actualizeEffect(s, effect);
  1082. }
  1083. }
  1084. else
  1085. tlog1 << "Cannot find stack " << id << std::endl;
  1086. }
  1087. }
  1088. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  1089. {
  1090. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  1091. stackAttacked.applyGs(gs);
  1092. }
  1093. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  1094. {
  1095. for(int g=0; g<healedStacks.size(); ++g)
  1096. {
  1097. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  1098. //checking if we resurrect a stack that is under a living stack
  1099. std::vector<THex> access = gs->curB->getAccessibility(changedStack, true);
  1100. bool acc[BFIELD_SIZE];
  1101. for(int h=0; h<BFIELD_SIZE; ++h)
  1102. acc[h] = false;
  1103. for(int h=0; h<access.size(); ++h)
  1104. acc[access[h]] = true;
  1105. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  1106. changedStack->doubleWide(), changedStack->attackerOwned,
  1107. changedStack->hasBonusOfType(Bonus::FLYING), true))
  1108. return; //position is already occupied
  1109. //applying changes
  1110. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  1111. if(resurrected)
  1112. {
  1113. changedStack->state.insert(ALIVE);
  1114. if(healedStacks[g].lowLevelResurrection)
  1115. changedStack->state.insert(SUMMONED);
  1116. //changedStack->addNewBonus( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1117. }
  1118. //int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1119. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1120. changedStack->count += res;
  1121. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1122. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1123. {
  1124. changedStack->firstHPleft -= changedStack->MaxHealth();
  1125. if(changedStack->baseAmount > changedStack->count)
  1126. {
  1127. changedStack->count += 1;
  1128. }
  1129. }
  1130. amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1131. //removal of negative effects
  1132. if(resurrected)
  1133. {
  1134. for (BonusList::iterator it = changedStack->bonuses.begin(); it != changedStack->bonuses.end(); it++)
  1135. {
  1136. if(VLC->spellh->spells[(*it)->id]->positiveness < 0)
  1137. {
  1138. changedStack->bonuses.erase(it);
  1139. }
  1140. }
  1141. //removing all features from negative spells
  1142. BonusList tmpFeatures = changedStack->bonuses;
  1143. changedStack->bonuses.clear();
  1144. BOOST_FOREACH(Bonus *b, tmpFeatures)
  1145. {
  1146. const CSpell *s = b->sourceSpell();
  1147. if(s && s->positiveness >= 0)
  1148. {
  1149. changedStack->addNewBonus(b);
  1150. }
  1151. }
  1152. }
  1153. }
  1154. }
  1155. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  1156. {
  1157. if(gs->curB) //if there is a battle
  1158. {
  1159. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1160. {
  1161. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1162. {
  1163. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1164. {
  1165. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1166. break;
  1167. }
  1168. }
  1169. }
  1170. }
  1171. }
  1172. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  1173. {
  1174. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1175. {
  1176. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1177. {
  1178. gs->curB->si.wallState[it->first.first] =
  1179. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1180. }
  1181. }
  1182. }
  1183. DLL_EXPORT void BattleStacksRemoved::applyGs( CGameState *gs )
  1184. {
  1185. if(!gs->curB)
  1186. return;
  1187. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1188. {
  1189. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1190. {
  1191. if(gs->curB->stacks[b]->ID == *it) //if found
  1192. {
  1193. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. }
  1199. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  1200. {
  1201. gs->currentPlayer = player;
  1202. }
  1203. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  1204. {
  1205. gs->getPlayer(player)->currentSelection = id;
  1206. }
  1207. DLL_EXPORT Component::Component(const CStackBasicDescriptor &stack)
  1208. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1209. {
  1210. }