NetPacksLib.cpp 31 KB

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