NetPacksLib.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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,CArtifact*>::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,CArtifact*>::iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  418. if(!vstd::contains(h->artifWorn,i->first) || h->artifWorn[i->first] != i->second)
  419. {
  420. equiped.push_back(i->second);
  421. }
  422. //update hero data
  423. h->artifacts = artifacts;
  424. h->artifWorn = artifWorn;
  425. }
  426. DLL_EXPORT void SetHeroArtifacts::setArtAtPos(ui16 pos, const CArtifact* art)
  427. {
  428. if(art < 0)
  429. {
  430. if(pos<19)
  431. VLC->arth->unequipArtifact(artifWorn, pos);
  432. else if (pos - 19 < artifacts.size())
  433. artifacts.erase(artifacts.begin() + (pos - 19));
  434. }
  435. else
  436. {
  437. if (pos < 19)
  438. {
  439. VLC->arth->equipArtifact(artifWorn, pos, art);
  440. }
  441. else // Goes into the backpack.
  442. {
  443. if(pos - 19 < artifacts.size())
  444. artifacts.insert(artifacts.begin() + (pos - 19), const_cast<CArtifact*>(art));
  445. else
  446. artifacts.push_back(const_cast<CArtifact*>(art));
  447. }
  448. }
  449. }
  450. DLL_EXPORT void HeroRecruited::applyGs( CGameState *gs )
  451. {
  452. assert(vstd::contains(gs->hpool.heroesPool, hid));
  453. CGHeroInstance *h = gs->hpool.heroesPool[hid];
  454. CGTownInstance *t = gs->getTown(tid);
  455. h->setOwner(player);
  456. h->pos = tile;
  457. h->movement = h->maxMovePoints(true);
  458. gs->hpool.heroesPool.erase(hid);
  459. if(h->id < 0)
  460. {
  461. h->id = gs->map->objects.size();
  462. gs->map->objects.push_back(h);
  463. }
  464. else
  465. gs->map->objects[h->id] = h;
  466. h->initHeroDefInfo();
  467. gs->map->heroes.push_back(h);
  468. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  469. h->initObj();
  470. gs->map->addBlockVisTiles(h);
  471. if(t)
  472. {
  473. t->visitingHero = h;
  474. h->visitedTown = t;
  475. }
  476. h->inTownGarrison = false;
  477. }
  478. DLL_EXPORT void GiveHero::applyGs( CGameState *gs )
  479. {
  480. CGHeroInstance *h = gs->getHero(id);
  481. gs->map->removeBlockVisTiles(h,true);
  482. h->setOwner(player);
  483. h->movement = h->maxMovePoints(true);
  484. h->initHeroDefInfo();
  485. gs->map->heroes.push_back(h);
  486. gs->getPlayer(h->getOwner())->heroes.push_back(h);
  487. gs->map->addBlockVisTiles(h);
  488. h->inTownGarrison = false;
  489. }
  490. DLL_EXPORT void NewObject::applyGs( CGameState *gs )
  491. {
  492. CGObjectInstance *o = NULL;
  493. switch(ID)
  494. {
  495. case 8:
  496. o = new CGBoat();
  497. break;
  498. case 54: //probably more options will be needed
  499. o = new CGCreature();
  500. {
  501. CStackInstance hlp;
  502. CGCreature *cre = static_cast<CGCreature*>(o);
  503. cre->slots[0] = hlp;
  504. cre->notGrowingTeam = cre->neverFlees = 0;
  505. cre->character = 2;
  506. cre->gainedArtifact = -1;
  507. }
  508. break;
  509. default:
  510. o = new CGObjectInstance();
  511. break;
  512. }
  513. o->ID = ID;
  514. o->subID = subID;
  515. o->pos = pos;
  516. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  517. id = o->id = gs->map->objects.size();
  518. o->hoverName = VLC->generaltexth->names[ID];
  519. switch(ID)
  520. {
  521. case 54: //cfreature
  522. o->defInfo = VLC->dobjinfo->gobjs[ID][subID];
  523. assert(o->defInfo);
  524. break;
  525. case 124://hole
  526. const TerrainTile &t = gs->map->getTile(pos);
  527. o->defInfo = VLC->dobjinfo->gobjs[ID][t.tertype];
  528. assert(o->defInfo);
  529. break;
  530. }
  531. gs->map->objects.push_back(o);
  532. gs->map->addBlockVisTiles(o);
  533. o->initObj();
  534. assert(o->defInfo);
  535. }
  536. DLL_EXPORT void NewArtifact::applyGs( CGameState *gs )
  537. {
  538. IModableArt * art;
  539. std::map<ui32,ui8>::iterator itr = VLC->arth->modableArtifacts.find(artid);
  540. switch (itr->second)
  541. {
  542. case 1:
  543. art = new CScroll;
  544. break;
  545. case 2:
  546. art = new CCustomizableArt;
  547. break;
  548. case 3:
  549. art = new CCommanderArt;
  550. break;
  551. default:
  552. tlog1<<"unhandled customizable artifact!\n";
  553. };
  554. *art = *static_cast<IModableArt*>(VLC->arth->artifacts[artid]); //copy properties
  555. art->ID = gs->map->artInstances.size();
  556. art->SetProperty (value); //init scroll, banner, commander art
  557. art->Init(); //set bonuses for new instance
  558. gs->map->artInstances.push_back(art);
  559. }
  560. DLL_EXPORT void SetAvailableArtifacts::applyGs( CGameState *gs )
  561. {
  562. if(id >= 0)
  563. {
  564. if(CGBlackMarket *bm = dynamic_cast<CGBlackMarket*>(gs->map->objects[id]))
  565. {
  566. bm->artifacts = arts;
  567. }
  568. else
  569. {
  570. tlog1 << "Wrong black market id!" << std::endl;
  571. }
  572. }
  573. else
  574. {
  575. CGTownInstance::merchantArtifacts = arts;
  576. }
  577. }
  578. DLL_EXPORT void NewTurn::applyGs( CGameState *gs )
  579. {
  580. gs->day = day;
  581. BOOST_FOREACH(NewTurn::Hero h, heroes) //give mana/movement point
  582. {
  583. CGHeroInstance *hero = gs->getHero(h.id);
  584. hero->movement = h.move;
  585. hero->mana = h.mana;
  586. }
  587. for(std::map<ui8, std::vector<si32> >::iterator i = res.begin(); i != res.end(); i++)
  588. {
  589. assert(i->first < PLAYER_LIMIT);
  590. std::vector<si32> &playerRes = gs->getPlayer(i->first)->resources;
  591. for(int j = 0; j < i->second.size(); j++)
  592. playerRes[j] = i->second[j];
  593. }
  594. BOOST_FOREACH(SetAvailableCreatures h, cres) //set available creatures in towns
  595. h.applyGs(gs);
  596. if (specialWeek != NO_ACTION) //first pack applied, reset all effects and aplly new
  597. {
  598. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  599. h->bonuses.remove_if(Bonus::OneDay);
  600. if(gs->getDate(1)) //new week, Monday that is
  601. {
  602. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  603. i->second.bonuses.remove_if(Bonus::OneWeek);
  604. BOOST_FOREACH(CGHeroInstance *h, gs->map->heroes)
  605. h->bonuses.remove_if(Bonus::OneWeek);
  606. gs->globalEffects.bonuses.remove_if(Bonus::OneWeek);
  607. Bonus b;
  608. b.duration = Bonus::ONE_WEEK;
  609. b.source = Bonus::SPECIAL_WEEK;
  610. b.effectRange = Bonus::NO_LIMIT;
  611. b.valType = Bonus::BASE_NUMBER; //certainly not intuitive
  612. switch (specialWeek)
  613. {
  614. case DOUBLE_GROWTH:
  615. b.val = 100;
  616. b.type = Bonus::CREATURE_GROWTH_PERCENT;
  617. b.limiter = new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false);
  618. break;
  619. case BONUS_GROWTH:
  620. b.val = 5;
  621. b.type = Bonus::CREATURE_GROWTH;
  622. b.limiter = new CCreatureTypeLimiter(*VLC->creh->creatures[creatureid], false);
  623. break;
  624. case DEITYOFFIRE:
  625. b.val = 15;
  626. b.type = Bonus::CREATURE_GROWTH;
  627. b.limiter = new CCreatureTypeLimiter(*VLC->creh->creatures[42], true);
  628. break;
  629. case PLAGUE:
  630. b.val = -100; //no basic creatures
  631. b.type = Bonus::CREATURE_GROWTH_PERCENT;
  632. break;
  633. default:
  634. b.val = 0;
  635. }
  636. if (b.val)
  637. gs->globalEffects.bonuses.push_back(b);
  638. }
  639. }
  640. else //second pack is applied
  641. {
  642. //count days without town
  643. for( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  644. {
  645. if(i->second.towns.size() || gs->day == 1)
  646. i->second.daysWithoutCastle = 0;
  647. else
  648. i->second.daysWithoutCastle++;
  649. i->second.bonuses.remove_if(Bonus::OneDay);
  650. }
  651. if(resetBuilded) //reset amount of structures set in this turn in towns
  652. {
  653. BOOST_FOREACH(CGTownInstance* t, gs->map->towns)
  654. t->builded = 0;
  655. }
  656. }
  657. }
  658. DLL_EXPORT void SetObjectProperty::applyGs( CGameState *gs )
  659. {
  660. CGObjectInstance *obj = gs->map->objects[id];
  661. if(!obj)
  662. {
  663. tlog1 << "Wrong object ID - property cannot be set!\n";
  664. return;
  665. }
  666. if(what == ObjProperty::OWNER)
  667. {
  668. if(obj->ID == TOWNI_TYPE)
  669. {
  670. CGTownInstance *t = static_cast<CGTownInstance*>(obj);
  671. if(t->tempOwner < PLAYER_LIMIT)
  672. gs->getPlayer(t->tempOwner)->towns -= t;
  673. if(val < PLAYER_LIMIT)
  674. gs->getPlayer(val)->towns.push_back(t);
  675. }
  676. }
  677. obj->setProperty(what,val);
  678. }
  679. DLL_EXPORT void SetHoverName::applyGs( CGameState *gs )
  680. {
  681. name.toString(gs->map->objects[id]->hoverName);
  682. }
  683. DLL_EXPORT void HeroLevelUp::applyGs( CGameState *gs )
  684. {
  685. CGHeroInstance* h = gs->getHero(heroid);
  686. h->level = level;
  687. //speciality
  688. h->UpdateSpeciality();
  689. }
  690. DLL_EXPORT void BattleStart::applyGs( CGameState *gs )
  691. {
  692. gs->curB = info;
  693. info->belligerents[0]->battle = info->belligerents[1]->battle = info;
  694. }
  695. DLL_EXPORT void BattleNextRound::applyGs( CGameState *gs )
  696. {
  697. gs->curB->castSpells[0] = gs->curB->castSpells[1] = 0;
  698. gs->curB->round = round;
  699. BOOST_FOREACH(CStack *s, gs->curB->stacks)
  700. {
  701. s->state -= DEFENDING;
  702. s->state -= WAITING;
  703. s->state -= MOVED;
  704. s->state -= HAD_MORALE;
  705. s->counterAttacks = 1 + s->valOfBonuses(Bonus::ADDITIONAL_RETALIATION);
  706. //regeneration
  707. if( s->hasBonusOfType(Bonus::HP_REGENERATION) && s->alive() )
  708. s->firstHPleft = std::min<ui32>( s->MaxHealth(), s->valOfBonuses(Bonus::HP_REGENERATION) );
  709. if( s->hasBonusOfType(Bonus::FULL_HP_REGENERATION) && s->alive() )
  710. s->firstHPleft = s->MaxHealth();
  711. //remove effects and restore only those with remaining turns in duration
  712. BonusList tmpEffects = s->bonuses;
  713. s->bonuses.removeSpells(Bonus::CASTED_SPELL);
  714. for (BonusList::iterator it = tmpEffects.begin(); it != tmpEffects.end(); it++)
  715. {
  716. it->turnsRemain--;
  717. if(it->turnsRemain > 0)
  718. s->bonuses.push_back(*it);
  719. }
  720. //the same as above for features
  721. BonusList tmpFeatures = s->bonuses;
  722. s->bonuses.clear();
  723. BOOST_FOREACH(Bonus &b, tmpFeatures)
  724. {
  725. if((b.duration & Bonus::N_TURNS) != 0)
  726. {
  727. b.turnsRemain--;
  728. if(b.turnsRemain > 0)
  729. s->bonuses.push_back(b);
  730. }
  731. else
  732. {
  733. s->bonuses.push_back(b);
  734. }
  735. }
  736. }
  737. }
  738. DLL_EXPORT void BattleSetActiveStack::applyGs( CGameState *gs )
  739. {
  740. gs->curB->activeStack = stack;
  741. CStack *st = gs->curB->getStack(stack);
  742. if(vstd::contains(st->state,MOVED)) //if stack is moving second time this turn it must had a high morale bonus
  743. st->state.insert(HAD_MORALE);
  744. }
  745. void BattleResult::applyGs( CGameState *gs )
  746. {
  747. for(unsigned i=0;i<gs->curB->stacks.size();i++)
  748. delete gs->curB->stacks[i];
  749. //remove any "until next battle" bonuses
  750. CGHeroInstance *h;
  751. h = gs->curB->heroes[0];
  752. if(h)
  753. h->bonuses.remove_if(Bonus::OneBattle);
  754. h = gs->curB->heroes[1];
  755. if(h)
  756. h->bonuses.remove_if(Bonus::OneBattle);
  757. gs->curB->belligerents[0]->battle = gs->curB->belligerents[1]->battle = NULL;
  758. delete gs->curB;
  759. gs->curB = NULL;
  760. }
  761. void BattleStackMoved::applyGs( CGameState *gs )
  762. {
  763. gs->curB->getStack(stack)->position = tile;
  764. }
  765. DLL_EXPORT void BattleStackAttacked::applyGs( CGameState *gs )
  766. {
  767. CStack * at = gs->curB->getStack(stackAttacked);
  768. at->count = newAmount;
  769. at->firstHPleft = newHP;
  770. if(killed())
  771. at->state -= ALIVE;
  772. //life drain handling
  773. for (int g=0; g<healedStacks.size(); ++g)
  774. {
  775. healedStacks[g].applyGs(gs);
  776. }
  777. }
  778. DLL_EXPORT void BattleAttack::applyGs( CGameState *gs )
  779. {
  780. CStack *attacker = gs->curB->getStack(stackAttacking);
  781. if(counter())
  782. attacker->counterAttacks--;
  783. if(shot())
  784. {
  785. //don't remove ammo if we have a working ammo cart
  786. bool hasAmmoCart = false;
  787. BOOST_FOREACH(const CStack * st, gs->curB->stacks)
  788. {
  789. if(st->owner == attacker->owner && st->type->idNumber == 148 && st->alive())
  790. {
  791. hasAmmoCart = true;
  792. break;
  793. }
  794. }
  795. if (!hasAmmoCart)
  796. {
  797. attacker->shots--;
  798. }
  799. }
  800. BOOST_FOREACH(BattleStackAttacked stackAttacked, bsa)
  801. stackAttacked.applyGs(gs);
  802. attacker->bonuses.remove_if(Bonus::UntilAttack);
  803. for(std::vector<BattleStackAttacked>::const_iterator it = bsa.begin(); it != bsa.end(); ++it)
  804. {
  805. CStack * stack = gs->curB->getStack(it->stackAttacked, false);
  806. stack->bonuses.remove_if(Bonus::UntilBeingAttacked);
  807. }
  808. }
  809. DLL_EXPORT void StartAction::applyGs( CGameState *gs )
  810. {
  811. CStack *st = gs->curB->getStack(ba.stackNumber);
  812. if(ba.actionType != 1) //don't check for stack if it's custom action by hero
  813. assert(st);
  814. switch(ba.actionType)
  815. {
  816. case 3:
  817. st->state.insert(DEFENDING);
  818. break;
  819. case 8:
  820. st->state.insert(WAITING);
  821. return;
  822. case 0: case 2: case 6: case 7: case 9: case 10: case 11: case 12:
  823. st->state.insert(MOVED);
  824. break;
  825. }
  826. if(st)
  827. 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)
  828. }
  829. DLL_EXPORT void BattleSpellCast::applyGs( CGameState *gs )
  830. {
  831. assert(gs->curB);
  832. CGHeroInstance *h = (side) ? gs->curB->heroes[1] : gs->curB->heroes[0];
  833. if(h && castedByHero)
  834. {
  835. int spellCost = 0;
  836. if(gs->curB)
  837. {
  838. spellCost = gs->curB->getSpellCost(&VLC->spellh->spells[id], h);
  839. }
  840. else
  841. {
  842. spellCost = VLC->spellh->spells[id].costs[skill];
  843. }
  844. h->mana -= spellCost;
  845. if(h->mana < 0) h->mana = 0;
  846. }
  847. if(side >= 0 && side < 2 && castedByHero)
  848. {
  849. gs->curB->castSpells[side]++;
  850. }
  851. if(id == 35 || id == 78) //dispel and dispel helpful spells
  852. {
  853. bool onlyHelpful = id == 78;
  854. for(std::set<ui32>::const_iterator it = affectedCres.begin(); it != affectedCres.end(); ++it)
  855. {
  856. CStack *s = gs->curB->getStack(*it);
  857. if(s && !vstd::contains(resisted, s->ID)) //if stack exists and it didn't resist
  858. {
  859. BonusList remainingEff;
  860. for (BonusList::iterator it = remainingEff.begin(); it != remainingEff.end(); it++)
  861. {
  862. if (onlyHelpful && VLC->spellh->spells[ it->id ].positiveness != 1)
  863. {
  864. remainingEff.push_back(*it);
  865. }
  866. }
  867. s->bonuses.removeSpells(Bonus::CASTED_SPELL); //removing all effects
  868. s->bonuses = remainingEff; //assigning effects that should remain
  869. //removing all features from spells
  870. BonusList tmpFeatures = s->bonuses;
  871. s->bonuses.clear();
  872. BOOST_FOREACH(Bonus &b, tmpFeatures)
  873. {
  874. const CSpell *sp = b.sourceSpell();
  875. if(sp && sp->positiveness != 1) //if(b.source != HeroBonus::SPELL_EFFECT || b.positiveness != 1)
  876. s->bonuses.push_back(b);
  877. }
  878. }
  879. }
  880. }
  881. //elemental summoning
  882. if(id >= 66 && id <= 69)
  883. {
  884. int creID;
  885. switch(id)
  886. {
  887. case 66:
  888. creID = 114; //fire elemental
  889. break;
  890. case 67:
  891. creID = 113; //earth elemental
  892. break;
  893. case 68:
  894. creID = 115; //water elemental
  895. break;
  896. case 69:
  897. creID = 112; //air elemental
  898. break;
  899. }
  900. const int3 & tile = gs->curB->tile;
  901. TerrainTile::EterrainType ter = gs->map->terrain[tile.x][tile.y][tile.z].tertype;
  902. int pos; //position of stack on the battlefield - to be calculated
  903. bool ac[BFIELD_SIZE];
  904. std::set<int> occupyable;
  905. bool twoHex = VLC->creh->creatures[creID]->isDoubleWide();
  906. bool flying = vstd::contains(VLC->creh->creatures[creID]->bonuses, Bonus::FLYING);
  907. gs->curB->getAccessibilityMap(ac, twoHex, !side, true, occupyable, flying);
  908. for(int g=0; g<BFIELD_SIZE; ++g)
  909. {
  910. if(g % BFIELD_WIDTH != 0 && g % BFIELD_WIDTH != BFIELD_WIDTH-1 && BattleInfo::isAccessible(g, ac, twoHex, !side, flying, true) )
  911. {
  912. pos = g;
  913. break;
  914. }
  915. }
  916. 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);
  917. summonedStack->state.insert(SUMMONED);
  918. //summonedStack->bonuses.push_back( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  919. gs->curB->stacks.push_back(summonedStack);
  920. }
  921. }
  922. void actualizeEffect(CStack * s, Bonus & ef)
  923. {
  924. //actualizing effects vector
  925. for (BonusList::iterator it = s->bonuses.begin(); it != s->bonuses.end(); it++)
  926. {
  927. if(it->id == ef.id)
  928. {
  929. it->turnsRemain = std::max(it->turnsRemain, ef.turnsRemain);
  930. }
  931. }
  932. //actualizing features vector
  933. BonusList sf;
  934. s->stackEffectToFeature(sf, ef);
  935. BOOST_FOREACH(const Bonus &fromEffect, sf)
  936. {
  937. BOOST_FOREACH(Bonus &stackBonus, s->bonuses) //TODO: optimize
  938. {
  939. if(stackBonus.source == Bonus::SPELL_EFFECT && stackBonus.type == fromEffect.type && stackBonus.subtype == fromEffect.subtype)
  940. {
  941. stackBonus.turnsRemain = std::max(stackBonus.turnsRemain, ef.turnsRemain);
  942. }
  943. }
  944. }
  945. }
  946. DLL_EXPORT void SetStackEffect::applyGs( CGameState *gs )
  947. {
  948. BOOST_FOREACH(ui32 id, stacks)
  949. {
  950. CStack *s = gs->curB->getStack(id);
  951. if(s)
  952. {
  953. if(effect.id == 42 || !s->hasBonus(Selector::source(Bonus::CASTED_SPELL, effect.id)))//disrupting ray or not on the list - just add
  954. {
  955. s->bonuses.push_back(effect);
  956. BonusList sf;
  957. s->stackEffectToFeature(sf, effect);
  958. BOOST_FOREACH(const Bonus &fromEffect, sf)
  959. {
  960. s->bonuses.push_back(fromEffect);
  961. }
  962. }
  963. else //just actualize
  964. {
  965. actualizeEffect(s, effect);
  966. }
  967. }
  968. else
  969. tlog1 << "Cannot find stack " << id << std::endl;
  970. }
  971. }
  972. DLL_EXPORT void StacksInjured::applyGs( CGameState *gs )
  973. {
  974. BOOST_FOREACH(BattleStackAttacked stackAttacked, stacks)
  975. stackAttacked.applyGs(gs);
  976. }
  977. DLL_EXPORT void StacksHealedOrResurrected::applyGs( CGameState *gs )
  978. {
  979. for(int g=0; g<healedStacks.size(); ++g)
  980. {
  981. CStack * changedStack = gs->curB->getStack(healedStacks[g].stackID, false);
  982. //checking if we resurrect a stack that is under a living stack
  983. std::vector<int> access = gs->curB->getAccessibility(changedStack->ID, true);
  984. bool acc[BFIELD_SIZE];
  985. for(int h=0; h<BFIELD_SIZE; ++h)
  986. acc[h] = false;
  987. for(int h=0; h<access.size(); ++h)
  988. acc[access[h]] = true;
  989. if(!changedStack->alive() && !gs->curB->isAccessible(changedStack->position, acc,
  990. changedStack->doubleWide(), changedStack->attackerOwned,
  991. changedStack->hasBonusOfType(Bonus::FLYING), true))
  992. return; //position is already occupied
  993. //applying changes
  994. bool resurrected = !changedStack->alive(); //indicates if stack is resurrected or just healed
  995. if(resurrected)
  996. {
  997. changedStack->state.insert(ALIVE);
  998. if(healedStacks[g].lowLevelResurrection)
  999. changedStack->state.insert(SUMMONED);
  1000. //changedStack->bonuses.push_back( makeFeature(HeroBonus::SUMMONED, HeroBonus::ONE_BATTLE, 0, 0, HeroBonus::BONUS_FROM_HERO) );
  1001. }
  1002. //int missingHPfirst = changedStack->MaxHealth() - changedStack->firstHPleft;
  1003. int res = std::min( healedStacks[g].healedHP / changedStack->MaxHealth() , changedStack->baseAmount - changedStack->count );
  1004. changedStack->count += res;
  1005. changedStack->firstHPleft += healedStacks[g].healedHP - res * changedStack->MaxHealth();
  1006. if(changedStack->firstHPleft > changedStack->MaxHealth())
  1007. {
  1008. changedStack->firstHPleft -= changedStack->MaxHealth();
  1009. if(changedStack->baseAmount > changedStack->count)
  1010. {
  1011. changedStack->count += 1;
  1012. }
  1013. }
  1014. amin(changedStack->firstHPleft, changedStack->MaxHealth());
  1015. //removal of negative effects
  1016. if(resurrected)
  1017. {
  1018. for (BonusList::iterator it = changedStack->bonuses.begin(); it != changedStack->bonuses.end(); it++)
  1019. {
  1020. if(VLC->spellh->spells[it->id].positiveness < 0)
  1021. {
  1022. changedStack->bonuses.erase(it);
  1023. }
  1024. }
  1025. //removing all features from negative spells
  1026. BonusList tmpFeatures = changedStack->bonuses;
  1027. changedStack->bonuses.clear();
  1028. BOOST_FOREACH(Bonus &b, tmpFeatures)
  1029. {
  1030. const CSpell *s = b.sourceSpell();
  1031. if(s && s->positiveness >= 0)
  1032. {
  1033. changedStack->bonuses.push_back(b);
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. DLL_EXPORT void ObstaclesRemoved::applyGs( CGameState *gs )
  1040. {
  1041. if(gs->curB) //if there is a battle
  1042. {
  1043. for(std::set<si32>::const_iterator it = obstacles.begin(); it != obstacles.end(); ++it)
  1044. {
  1045. for(int i=0; i<gs->curB->obstacles.size(); ++i)
  1046. {
  1047. if(gs->curB->obstacles[i].uniqueID == *it) //remove this obstacle
  1048. {
  1049. gs->curB->obstacles.erase(gs->curB->obstacles.begin() + i);
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. }
  1055. }
  1056. DLL_EXPORT void CatapultAttack::applyGs( CGameState *gs )
  1057. {
  1058. if(gs->curB && gs->curB->siege != 0) //if there is a battle and it's a siege
  1059. {
  1060. for(std::set< std::pair< std::pair< ui8, si16 >, ui8> >::const_iterator it = attackedParts.begin(); it != attackedParts.end(); ++it)
  1061. {
  1062. gs->curB->si.wallState[it->first.first] =
  1063. std::min( gs->curB->si.wallState[it->first.first] + it->second, 3 );
  1064. }
  1065. }
  1066. }
  1067. DLL_EXPORT void BattleStacksRemoved::applyGs( CGameState *gs )
  1068. {
  1069. if(!gs->curB)
  1070. return;
  1071. for(std::set<ui32>::const_iterator it = stackIDs.begin(); it != stackIDs.end(); ++it) //for each removed stack
  1072. {
  1073. for(int b=0; b<gs->curB->stacks.size(); ++b) //find it in vector of stacks
  1074. {
  1075. if(gs->curB->stacks[b]->ID == *it) //if found
  1076. {
  1077. gs->curB->stacks.erase(gs->curB->stacks.begin() + b); //remove
  1078. break;
  1079. }
  1080. }
  1081. }
  1082. }
  1083. DLL_EXPORT void YourTurn::applyGs( CGameState *gs )
  1084. {
  1085. gs->currentPlayer = player;
  1086. }
  1087. DLL_EXPORT void SetSelection::applyGs( CGameState *gs )
  1088. {
  1089. gs->getPlayer(player)->currentSelection = id;
  1090. }
  1091. DLL_EXPORT Component::Component(const CStackInstance &stack)
  1092. :id(CREATURE), subtype(stack.type->idNumber), val(stack.count), when(0)
  1093. {
  1094. }