NetPacksLib.cpp 36 KB

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