NetPacksLib.cpp 31 KB

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