NetPacksLib.cpp 36 KB

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