CCallback.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. #include "stdafx.h"
  2. #include "CCallback.h"
  3. #include "lib/CCreatureHandler.h"
  4. #include "client/CGameInfo.h"
  5. #include "lib/CGameState.h"
  6. #include "lib/BattleState.h"
  7. #include "client/CPlayerInterface.h"
  8. #include "client/Client.h"
  9. #include "lib/map.h"
  10. #include "lib/CBuildingHandler.h"
  11. #include "lib/CDefObjInfoHandler.h"
  12. #include "lib/CGeneralTextHandler.h"
  13. #include "lib/CHeroHandler.h"
  14. #include "lib/CObjectHandler.h"
  15. #include "lib/Connection.h"
  16. #include "lib/NetPacks.h"
  17. #include "client/mapHandler.h"
  18. #include <boost/foreach.hpp>
  19. #include <boost/thread.hpp>
  20. #include <boost/thread/shared_mutex.hpp>
  21. #include "lib/CSpellHandler.h"
  22. #include "lib/CArtHandler.h"
  23. #ifdef min
  24. #undef min
  25. #endif
  26. #ifdef max
  27. #undef max
  28. #endif
  29. /*
  30. * CCallback.cpp, part of VCMI engine
  31. *
  32. * Authors: listed in file AUTHORS in main folder
  33. *
  34. * License: GNU General Public License v2.0 or later
  35. * Full text of license available in license.txt file, in main folder
  36. *
  37. */
  38. template <ui16 N> bool isType(CPack *pack)
  39. {
  40. return pack->getType() == N;
  41. }
  42. bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
  43. {
  44. CastleTeleportHero pack(who->id, where->id, 1);
  45. sendRequest(&pack);
  46. return true;
  47. }
  48. bool CCallback::moveHero(const CGHeroInstance *h, int3 dst)
  49. {
  50. MoveHero pack(dst,h->id);
  51. sendRequest(&pack);
  52. return true;
  53. }
  54. void CCallback::selectionMade(int selection, int asker)
  55. {
  56. QueryReply pack(asker,selection);
  57. *cl->serv << &pack;
  58. }
  59. void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level/*=-1*/)
  60. {
  61. if(player!=obj->tempOwner && obj->ID != 106)
  62. return;
  63. RecruitCreatures pack(obj->id,ID,amount,level);
  64. sendRequest(&pack);
  65. }
  66. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  67. {
  68. if(((player>=0) && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
  69. return false;
  70. DisbandCreature pack(stackPos,obj->id);
  71. sendRequest(&pack);
  72. return true;
  73. }
  74. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  75. {
  76. UpgradeCreature pack(stackPos,obj->id,newID);
  77. sendRequest(&pack);
  78. return false;
  79. }
  80. void CCallback::endTurn()
  81. {
  82. tlog5 << "Player " << (unsigned)player << " ended his turn." << std::endl;
  83. EndTurn pack;
  84. sendRequest(&pack); //report that we ended turn
  85. }
  86. UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos) const
  87. {
  88. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  89. return gs->getUpgradeInfo(obj->getStack(stackPos));
  90. }
  91. const StartInfo * CCallback::getStartInfo() const
  92. {
  93. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  94. return gs->scenarioOps;
  95. }
  96. int CCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
  97. {
  98. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  99. //if there is a battle
  100. if(gs->curB)
  101. return gs->curB->getSpellCost(sp, caster);
  102. //if there is no battle
  103. return caster->getSpellCost(sp);
  104. }
  105. int CCallback::estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const
  106. {
  107. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  108. if(!gs->curB) //no battle
  109. {
  110. if (hero) //but we see hero's spellbook
  111. return gs->curB->calculateSpellDmg(sp, hero, NULL, hero->getSpellSchoolLevel(sp), hero->getPrimSkillLevel(2));
  112. else
  113. return 0; //mage guild
  114. }
  115. //gs->getHero(gs->currentPlayer)
  116. const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1];
  117. return gs->curB->calculateSpellDmg(sp, ourHero, NULL, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(2));
  118. }
  119. void CCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj)
  120. {
  121. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  122. if(obj == NULL)
  123. return;
  124. if(obj->ID == TOWNI_TYPE || obj->ID == 95) //it is a town or adv map tavern
  125. {
  126. gs->obtainPlayersStats(thi, gs->players[player].towns.size());
  127. }
  128. else if(obj->ID == 97) //Den of Thieves
  129. {
  130. gs->obtainPlayersStats(thi, 20);
  131. }
  132. }
  133. int CCallback::howManyTowns() const
  134. {
  135. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  136. return gs->players[player].towns.size();
  137. }
  138. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
  139. {
  140. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  141. if (!mode)
  142. {
  143. const std::vector<ConstTransitivePtr<CGTownInstance> > &towns = gs->players[gs->currentPlayer].towns;
  144. if(val < towns.size())
  145. return towns[val];
  146. else
  147. return NULL;
  148. }
  149. else if(mode == 1)
  150. {
  151. const CGObjectInstance *obj = getObjectInfo(val);
  152. if(!obj)
  153. return NULL;
  154. if(obj->ID != TOWNI_TYPE)
  155. return NULL;
  156. else
  157. return static_cast<const CGTownInstance *>(obj);
  158. }
  159. return NULL;
  160. }
  161. bool CCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown &dest ) const
  162. {
  163. if(!isVisible(town, player)) //it's not a town or it's not visible for layer
  164. return false;
  165. bool detailed = hasAccess(town->tempOwner);
  166. //TODO vision support
  167. if(town->ID == TOWNI_TYPE)
  168. dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
  169. else if(town->ID == 33 || town->ID == 219)
  170. dest.initFromGarrison(static_cast<const CGGarrison *>(town), detailed);
  171. else
  172. return false;
  173. return true;
  174. }
  175. int3 CCallback::guardingCreaturePosition (int3 pos) const
  176. {
  177. return gs->guardingCreaturePosition(pos);
  178. }
  179. int CCallback::howManyHeroes(bool includeGarrisoned) const
  180. {
  181. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  182. return cl->getHeroCount(player,includeGarrisoned);
  183. }
  184. const CGHeroInstance * CCallback::getHeroInfo(int val, int mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
  185. {
  186. boost::shared_lock<boost::shared_mutex> lock(*gs->mx); //TODO use me?
  187. //if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  188. // return NULL;
  189. if (!mode) //esrial id
  190. {
  191. if(val<gs->players[player].heroes.size())
  192. {
  193. return gs->players[player].heroes[val];
  194. }
  195. else
  196. {
  197. return NULL;
  198. }
  199. }
  200. else if(mode==1) //it's hero type id
  201. {
  202. for (size_t i=0; i < gs->players[player].heroes.size(); ++i)
  203. {
  204. if (gs->players[player].heroes[i]->type->ID==val)
  205. {
  206. return gs->players[player].heroes[i];
  207. }
  208. }
  209. }
  210. else //object id
  211. {
  212. return static_cast<const CGHeroInstance*>(gs->map->objects[val].get());
  213. }
  214. return NULL;
  215. }
  216. const CGObjectInstance * CCallback::getObjectInfo(int ID) const
  217. {
  218. //TODO: check for visibility
  219. return gs->map->objects[ID];
  220. }
  221. bool CCallback::getHeroInfo( const CGObjectInstance *hero, InfoAboutHero &dest ) const
  222. {
  223. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
  224. if(!h || !isVisible(h->getPosition(false))) //it's not a hero or it's not visible for layer
  225. return false;
  226. //TODO vision support
  227. dest.initFromHero(h, hasAccess(h->tempOwner));
  228. return true;
  229. }
  230. int CCallback::getResourceAmount(int type) const
  231. {
  232. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  233. return gs->players[player].resources[type];
  234. }
  235. std::vector<si32> CCallback::getResourceAmount() const
  236. {
  237. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  238. return gs->players[player].resources;
  239. }
  240. int CCallback::getDate(int mode) const
  241. {
  242. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  243. return gs->getDate(mode);
  244. }
  245. std::vector < std::string > CCallback::getObjDescriptions(int3 pos) const
  246. {
  247. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  248. std::vector<std::string> ret;
  249. if(!isVisible(pos,player))
  250. return ret;
  251. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  252. ret.push_back(obj->getHoverText());
  253. return ret;
  254. }
  255. bool CCallback::verifyPath(CPath * path, bool blockSea) const
  256. {
  257. for (size_t i=0; i < path->nodes.size(); ++i)
  258. {
  259. if ( CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  260. && (! (CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
  261. return false; //path is wrong - one of the tiles is blocked
  262. if (blockSea)
  263. {
  264. if (i==0)
  265. continue;
  266. if (
  267. ((CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tertype==TerrainTile::water)
  268. &&
  269. (CGI->mh->map->terrain[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tertype!=TerrainTile::water))
  270. ||
  271. ((CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tertype!=TerrainTile::water)
  272. &&
  273. (CGI->mh->map->terrain[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tertype==TerrainTile::water))
  274. ||
  275. (CGI->mh->map->terrain[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tertype==TerrainTile::rock)
  276. )
  277. return false;
  278. }
  279. }
  280. return true;
  281. }
  282. std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap() const
  283. {
  284. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  285. return gs->getPlayerTeam(player)->fogOfWarMap;
  286. }
  287. bool CCallback::isVisible(int3 pos, int Player) const
  288. {
  289. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  290. return gs->map->isInTheMap(pos) && gs->isVisible(pos, Player);
  291. }
  292. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur) const
  293. {
  294. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  295. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  296. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  297. {
  298. for (size_t j=0; j < (*i).second.towns.size(); ++j)
  299. {
  300. if ((*i).first==player
  301. || (isVisible((*i).second.towns[j],player) && !onlyOur))
  302. {
  303. ret.push_back((*i).second.towns[j]);
  304. }
  305. }
  306. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  307. return ret;
  308. }
  309. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur) const
  310. {
  311. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  312. std::vector < const CGHeroInstance *> ret;
  313. for(size_t i=0;i<gs->map->heroes.size();i++)
  314. {
  315. if( (gs->map->heroes[i]->tempOwner==player) ||
  316. (isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
  317. {
  318. ret.push_back(gs->map->heroes[i]);
  319. }
  320. }
  321. return ret;
  322. }
  323. bool CCallback::isVisible(int3 pos) const
  324. {
  325. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  326. return isVisible(pos,player);
  327. }
  328. bool CCallback::isVisible( const CGObjectInstance *obj, int Player ) const
  329. {
  330. return gs->isVisible(obj, Player);
  331. }
  332. int CCallback::getMyColor() const
  333. {
  334. return player;
  335. }
  336. int CCallback::getHeroSerial(const CGHeroInstance * hero) const
  337. {
  338. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  339. for (size_t i=0; i<gs->players[player].heroes.size();i++)
  340. {
  341. if (gs->players[player].heroes[i]==hero)
  342. return i;
  343. }
  344. return -1;
  345. }
  346. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj) const
  347. {
  348. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  349. const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
  350. if(!armi)
  351. return NULL;
  352. else
  353. return armi;
  354. }
  355. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  356. {
  357. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  358. sendRequest(&pack);
  359. return 0;
  360. }
  361. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  362. {
  363. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  364. sendRequest(&pack);
  365. return 0;
  366. }
  367. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)
  368. {
  369. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  370. sendRequest(&pack);
  371. return 0;
  372. }
  373. bool CCallback::dismissHero(const CGHeroInstance *hero)
  374. {
  375. if(player!=hero->tempOwner) return false;
  376. DismissHero pack(hero->id);
  377. sendRequest(&pack);
  378. return true;
  379. }
  380. // int CCallback::getMySerial() const
  381. // {
  382. // boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  383. // return gs->players[player].serial;
  384. // }
  385. bool CCallback::swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)
  386. {
  387. if(player!=hero1->tempOwner && player!=hero2->tempOwner)
  388. return false;
  389. ExchangeArtifacts ea(hero1->id, hero2->id, pos1, pos2);
  390. sendRequest(&ea);
  391. return true;
  392. }
  393. /**
  394. * Assembles or disassembles a combination artifact.
  395. * @param hero Hero holding the artifact(s).
  396. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  397. * @param assemble True for assembly operation, false for disassembly.
  398. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  399. * artifact to assemble to. Otherwise it's not used.
  400. */
  401. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)
  402. {
  403. if (player != hero->tempOwner)
  404. return false;
  405. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  406. sendRequest(&aa);
  407. return true;
  408. }
  409. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  410. {
  411. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  412. if(town->tempOwner!=player)
  413. return false;
  414. const CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  415. for(int i=0;i<b->resources.size();i++)
  416. if(b->resources[i] > gs->players[player].resources[i])
  417. return false; //lack of resources
  418. BuildStructure pack(town->id,buildingID);
  419. sendRequest(&pack);
  420. return true;
  421. }
  422. int CBattleCallback::battleGetBattlefieldType()
  423. {
  424. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  425. //return gs->battleGetBattlefieldType();
  426. if(!gs->curB)
  427. {
  428. tlog2<<"battleGetBattlefieldType called when there is no battle!"<<std::endl;
  429. return -1;
  430. }
  431. return gs->curB->battlefieldType;
  432. }
  433. int CBattleCallback::battleGetObstaclesAtTile(THex tile) //returns bitfield
  434. {
  435. //TODO - write
  436. return -1;
  437. }
  438. std::vector<CObstacleInstance> CBattleCallback::battleGetAllObstacles()
  439. {
  440. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  441. if(gs->curB)
  442. return gs->curB->obstacles;
  443. else
  444. return std::vector<CObstacleInstance>();
  445. }
  446. const CStack* CBattleCallback::battleGetStackByID(int ID, bool onlyAlive)
  447. {
  448. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  449. if(!gs->curB) return NULL;
  450. return gs->curB->getStack(ID, onlyAlive);
  451. }
  452. int CBattleCallback::battleMakeAction(BattleAction* action)
  453. {
  454. assert(action->actionType == BattleAction::HERO_SPELL);
  455. MakeCustomAction mca(*action);
  456. sendRequest(&mca);
  457. return 0;
  458. }
  459. const CStack* CBattleCallback::battleGetStackByPos(THex pos, bool onlyAlive)
  460. {
  461. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  462. return gs->curB->battleGetStack(pos, onlyAlive);
  463. }
  464. THex CBattleCallback::battleGetPos(int stack)
  465. {
  466. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  467. if(!gs->curB)
  468. {
  469. tlog2<<"battleGetPos called when there is no battle!"<<std::endl;
  470. return THex::INVALID;
  471. }
  472. for(size_t g=0; g<gs->curB->stacks.size(); ++g)
  473. {
  474. if(gs->curB->stacks[g]->ID == stack)
  475. return gs->curB->stacks[g]->position;
  476. }
  477. return THex::INVALID;
  478. }
  479. TStacks CBattleCallback::battleGetStacks(EStackOwnership whose /*= MINE_AND_ENEMY*/, bool onlyAlive /*= true*/)
  480. {
  481. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  482. TStacks ret;
  483. if(!gs->curB) //there is no battle
  484. {
  485. tlog2<<"battleGetStacks called when there is no battle!"<<std::endl;
  486. return ret;
  487. }
  488. BOOST_FOREACH(const CStack *s, gs->curB->stacks)
  489. {
  490. bool ownerMatches = whose == MINE_AND_ENEMY || whose == ONLY_MINE && s->owner == player || whose == ONLY_ENEMY && s->owner != player;
  491. bool alivenessMatches = s->alive() || !onlyAlive;
  492. if(ownerMatches && alivenessMatches)
  493. ret.push_back(s);
  494. }
  495. return ret;
  496. }
  497. void CBattleCallback::getStackQueue( std::vector<const CStack *> &out, int howMany )
  498. {
  499. if(!gs->curB)
  500. {
  501. tlog2 << "battleGetStackQueue called when there is not battle!" << std::endl;
  502. return;
  503. }
  504. gs->curB->getStackQueue(out, howMany);
  505. }
  506. std::vector<THex> CBattleCallback::battleGetAvailableHexes(const CStack * stack, bool addOccupiable, std::vector<THex> * attackable)
  507. {
  508. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  509. if(!gs->curB)
  510. {
  511. tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
  512. return std::vector<THex>();
  513. }
  514. return gs->curB->getAccessibility(stack, addOccupiable, attackable);
  515. //return gs->battleGetRange(ID);
  516. }
  517. bool CBattleCallback::battleCanShoot(const CStack * stack, THex dest)
  518. {
  519. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  520. if(!gs->curB) return false;
  521. return gs->curB->battleCanShoot(stack, dest);
  522. }
  523. bool CBattleCallback::battleCanCastSpell()
  524. {
  525. if(!gs->curB) //there is no battle
  526. return false;
  527. return gs->curB->battleCanCastSpell(player, SpellCasting::HERO_CASTING) == SpellCasting::OK;
  528. }
  529. bool CBattleCallback::battleCanFlee()
  530. {
  531. return gs->curB->battleCanFlee(player);
  532. }
  533. const CGTownInstance *CBattleCallback::battleGetDefendedTown()
  534. {
  535. if(!gs->curB || gs->curB->town == NULL)
  536. return NULL;
  537. return gs->curB->town;
  538. }
  539. ui8 CBattleCallback::battleGetWallState(int partOfWall)
  540. {
  541. if(!gs->curB || gs->curB->siege == 0)
  542. {
  543. return 0;
  544. }
  545. return gs->curB->si.wallState[partOfWall];
  546. }
  547. int CBattleCallback::battleGetWallUnderHex(THex hex)
  548. {
  549. if(!gs->curB || gs->curB->siege == 0)
  550. {
  551. return -1;
  552. }
  553. return gs->curB->hexToWallPart(hex);
  554. }
  555. TDmgRange CBattleCallback::battleEstimateDamage(const CStack * attacker, const CStack * defender, TDmgRange * retaliationDmg)
  556. {
  557. if(!gs->curB)
  558. return std::make_pair(0, 0);
  559. const CGHeroInstance * attackerHero, * defenderHero;
  560. bool shooting = battleCanShoot(attacker, defender->position);
  561. if(gs->curB->sides[0] == player)
  562. {
  563. attackerHero = gs->curB->heroes[0];
  564. defenderHero = gs->curB->heroes[1];
  565. }
  566. else
  567. {
  568. attackerHero = gs->curB->heroes[1];
  569. defenderHero = gs->curB->heroes[0];
  570. }
  571. TDmgRange ret = gs->curB->calculateDmgRange(attacker, defender, attackerHero, defenderHero, shooting, 0, false, false);
  572. if(retaliationDmg)
  573. {
  574. if(shooting)
  575. {
  576. retaliationDmg->first = retaliationDmg->second = 0;
  577. }
  578. else
  579. {
  580. ui32 TDmgRange::* pairElems[] = {&TDmgRange::first, &TDmgRange::second};
  581. for (int i=0; i<2; ++i)
  582. {
  583. BattleStackAttacked bsa;
  584. bsa.damageAmount = ret.*pairElems[i];
  585. retaliationDmg->*pairElems[!i] = gs->curB->calculateDmgRange(defender, attacker, bsa.newAmount, attacker->count, attackerHero, defenderHero, false, false, false, false).*pairElems[!i];
  586. }
  587. }
  588. }
  589. return ret;
  590. }
  591. ui8 CBattleCallback::battleGetSiegeLevel()
  592. {
  593. if(!gs->curB)
  594. return 0;
  595. return gs->curB->siege;
  596. }
  597. const CGHeroInstance * CBattleCallback::battleGetFightingHero(ui8 side) const
  598. {
  599. if(!gs->curB)
  600. return 0;
  601. return gs->curB->heroes[side];
  602. }
  603. template <typename T>
  604. void CBattleCallback::sendRequest(const T* request)
  605. {
  606. //TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
  607. if(waitTillRealize)
  608. cl->waitingRequest.set(true);
  609. *cl->serv << request;
  610. if(waitTillRealize)
  611. cl->waitingRequest.waitWhileTrue();
  612. }
  613. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  614. {
  615. if(town->tempOwner != player) return;
  616. GarrisonHeroSwap pack(town->id);
  617. sendRequest(&pack);
  618. }
  619. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  620. {
  621. if(hero->tempOwner != player) return;
  622. BuyArtifact pack(hero->id,aid);
  623. sendRequest(&pack);
  624. }
  625. std::vector < const CGObjectInstance * > CCallback::getBlockingObjs( int3 pos ) const
  626. {
  627. std::vector<const CGObjectInstance *> ret;
  628. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  629. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  630. return ret;
  631. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  632. ret.push_back(obj);
  633. return ret;
  634. }
  635. std::vector < const CGObjectInstance * > CCallback::getVisitableObjs( int3 pos ) const
  636. {
  637. std::vector<const CGObjectInstance *> ret;
  638. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  639. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  640. return ret;
  641. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].visitableObjects)
  642. ret.push_back(obj);
  643. return ret;
  644. }
  645. std::vector < const CGObjectInstance * > CCallback::getFlaggableObjects(int3 pos) const
  646. {
  647. if(!isVisible(pos))
  648. return std::vector < const CGObjectInstance * >();
  649. std::vector < const CGObjectInstance * > ret;
  650. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  651. for(size_t b=0; b<objs.size(); ++b)
  652. {
  653. if(objs[b].first->tempOwner!=254 && !((objs[b].first->defInfo->blockMap[pos.y - objs[b].first->pos.y + 5] >> (objs[b].first->pos.x - pos.x)) & 1))
  654. ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  655. }
  656. return ret;
  657. }
  658. int3 CCallback::getMapSize() const
  659. {
  660. return CGI->mh->sizes;
  661. }
  662. void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
  663. {
  664. TradeOnMarketplace pack;
  665. pack.market = market;
  666. pack.hero = hero;
  667. pack.mode = mode;
  668. pack.r1 = id1;
  669. pack.r2 = id2;
  670. pack.val = val1;
  671. sendRequest(&pack);
  672. }
  673. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  674. {
  675. const_cast<CGHeroInstance*>(hero)-> formation = tight;
  676. SetFormation pack(hero->id,tight);
  677. sendRequest(&pack);
  678. }
  679. void CCallback::setSelection(const CArmedInstance * obj)
  680. {
  681. SetSelection ss;
  682. ss.player = player;
  683. ss.id = obj->id;
  684. sendRequest(&ss);
  685. if(obj->ID == HEROI_TYPE)
  686. {
  687. cl->gs->calculatePaths(static_cast<const CGHeroInstance *>(obj), *cl->pathInfo);
  688. //nasty workaround. TODO: nice workaround
  689. cl->gs->getPlayer(player)->currentSelection = obj->id;
  690. }
  691. }
  692. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  693. {
  694. ui8 i=0;
  695. for(; i<gs->players[player].availableHeroes.size(); i++)
  696. {
  697. if(gs->players[player].availableHeroes[i] == hero)
  698. {
  699. HireHero pack(i,townOrTavern->id);
  700. pack.player = player;
  701. sendRequest(&pack);
  702. return;
  703. }
  704. }
  705. }
  706. std::vector<const CGHeroInstance *> CCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  707. {
  708. std::vector<const CGHeroInstance *> ret(gs->players[player].availableHeroes.size());
  709. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  710. return ret;
  711. }
  712. const TerrainTile * CCallback::getTileInfo( int3 tile ) const
  713. {
  714. if(!gs->map->isInTheMap(tile))
  715. {
  716. tlog1 << tile << "is outside the map! (call to getTileInfo)\n";
  717. return NULL;
  718. }
  719. if(!isVisible(tile, player)) return NULL;
  720. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  721. return &gs->map->getTile(tile);
  722. }
  723. int CCallback::canBuildStructure( const CGTownInstance *t, int ID )
  724. {
  725. return gs->canBuildStructure(t,ID);
  726. }
  727. std::set<int> CCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
  728. {
  729. return gs->getBuildingRequiments(t,ID);
  730. }
  731. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  732. {
  733. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  734. return gs->getPath(src,dest,hero, ret);
  735. }
  736. void CCallback::save( const std::string &fname )
  737. {
  738. cl->save(fname);
  739. }
  740. void CCallback::sendMessage(const std::string &mess)
  741. {
  742. PlayerMessage pm(player, mess);
  743. sendRequest(&pm);
  744. }
  745. void CCallback::buildBoat( const IShipyard *obj )
  746. {
  747. BuildBoat bb;
  748. bb.objid = obj->o->id;
  749. sendRequest(&bb);
  750. }
  751. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  752. :CBattleCallback(GS, Player, C)
  753. {
  754. waitTillRealize = false;
  755. }
  756. const CMapHeader * CCallback::getMapHeader() const
  757. {
  758. return gs->map;
  759. }
  760. const CGPathNode * CCallback::getPathInfo( int3 tile )
  761. {
  762. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  763. }
  764. bool CCallback::getPath2( int3 dest, CGPath &ret )
  765. {
  766. if (!gs->map->isInTheMap(dest))
  767. return false;
  768. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  769. assert(cl->pathInfo->hero == h);
  770. if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
  771. {
  772. recalculatePaths();
  773. }
  774. return cl->pathInfo->getPath(dest, ret);
  775. }
  776. void CCallback::recalculatePaths()
  777. {
  778. gs->calculatePaths(cl->IGameCallback::getSelectedHero(player), *cl->pathInfo);
  779. }
  780. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  781. {
  782. gs->calculatePaths(hero, out, src, movement);
  783. }
  784. int3 CCallback::getGrailPos( float &outKnownRatio )
  785. {
  786. if (CGObelisk::obeliskCount == 0)
  787. {
  788. outKnownRatio = 0.0f;
  789. }
  790. else
  791. {
  792. outKnownRatio = (float)CGObelisk::visited[gs->getPlayerTeam(player)->id] / CGObelisk::obeliskCount;
  793. }
  794. return gs->map->grailPos;
  795. }
  796. void CCallback::dig( const CGObjectInstance *hero )
  797. {
  798. DigWithHero dwh;
  799. dwh.id = hero->id;
  800. sendRequest(&dwh);
  801. }
  802. void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
  803. {
  804. CastAdvSpell cas;
  805. cas.hid = hero->id;
  806. cas.sid = spellID;
  807. cas.pos = pos;
  808. sendRequest(&cas);
  809. }
  810. bool CCallback::hasAccess(int playerId) const
  811. {
  812. return gs->getPlayerRelations( playerId, player ) || player < 0;
  813. }
  814. si8 CBattleCallback::battleHasDistancePenalty( const CStack * stack, THex destHex )
  815. {
  816. return gs->curB->hasDistancePenalty(stack, destHex);
  817. }
  818. si8 CBattleCallback::battleHasWallPenalty( const CStack * stack, THex destHex )
  819. {
  820. return gs->curB->hasWallPenalty(stack, destHex);
  821. }
  822. si8 CBattleCallback::battleCanTeleportTo(const CStack * stack, THex destHex, int telportLevel)
  823. {
  824. return gs->curB->canTeleportTo(stack, destHex, telportLevel);
  825. }
  826. int CCallback::getPlayerStatus(int player) const
  827. {
  828. const PlayerState *ps = gs->getPlayer(player, false);
  829. if(!ps)
  830. return -1;
  831. return ps->status;
  832. }
  833. std::string CCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  834. {
  835. return "GOSSIP TEST";
  836. }
  837. std::vector < const CGObjectInstance * > CCallback::getMyObjects() const
  838. {
  839. std::vector < const CGObjectInstance * > ret;
  840. for (int g=0; g<gs->map->objects.size(); ++g)
  841. {
  842. if (gs->map->objects[g] && gs->map->objects[g]->tempOwner == LOCPLINT->playerID)
  843. {
  844. ret.push_back(gs->map->objects[g]);
  845. }
  846. }
  847. return ret;
  848. }
  849. std::vector < const CGDwelling * > CCallback::getMyDwellings() const
  850. {
  851. std::vector < const CGDwelling * > ret;
  852. BOOST_FOREACH(CGDwelling * dw, gs->getPlayer(player)->dwellings)
  853. {
  854. ret.push_back(dw);
  855. }
  856. return ret;
  857. }
  858. int CCallback::getPlayerRelations( ui8 color1, ui8 color2 ) const
  859. {
  860. return gs->getPlayerRelations(color1, color2);
  861. }
  862. InfoAboutTown::InfoAboutTown()
  863. {
  864. tType = NULL;
  865. details = NULL;
  866. fortLevel = 0;
  867. owner = -1;
  868. }
  869. InfoAboutTown::~InfoAboutTown()
  870. {
  871. delete details;
  872. }
  873. void InfoAboutTown::initFromTown( const CGTownInstance *t, bool detailed )
  874. {
  875. obj = t;
  876. army = ArmyDescriptor(t->getUpperArmy(), detailed);
  877. built = t->builded;
  878. fortLevel = t->fortLevel();
  879. name = t->name;
  880. tType = t->town;
  881. owner = t->tempOwner;
  882. if(detailed)
  883. {
  884. //include details about hero
  885. details = new Details;
  886. details->goldIncome = t->dailyIncome();
  887. details->customRes = vstd::contains(t->builtBuildings, 15);
  888. details->hallLevel = t->hallLevel();
  889. details->garrisonedHero = t->garrisonHero;
  890. }
  891. //TODO: adjust undetailed info about army to our count of thieves guilds
  892. }
  893. void InfoAboutTown::initFromGarrison(const CGGarrison *garr, bool detailed)
  894. {
  895. obj = garr;
  896. fortLevel = 0;
  897. army = ArmyDescriptor(garr, detailed);
  898. name = CGI->generaltexth->names[33]; // "Garrison"
  899. owner = garr->tempOwner;
  900. built = false;
  901. tType = NULL;
  902. // Show detailed info only to owning player.
  903. if(detailed)
  904. {
  905. details = new InfoAboutTown::Details;
  906. details->customRes = false;
  907. details->garrisonedHero = false;
  908. details->goldIncome = -1;
  909. details->hallLevel = -1;
  910. }
  911. }
  912. bool CBattleCallback::hasAccess( int playerId ) const
  913. {
  914. return playerId == player || player < 0;
  915. }
  916. CBattleCallback::CBattleCallback(CGameState *GS, int Player, CClient *C )
  917. {
  918. gs = GS;
  919. player = Player;
  920. cl = C;
  921. }
  922. std::vector<int> CBattleCallback::battleGetDistances(const CStack * stack, THex hex /*= THex::INVALID*/, THex * predecessors /*= NULL*/)
  923. {
  924. if(!hex.isValid())
  925. hex = stack->position;
  926. std::vector<int> ret;
  927. bool ac[BFIELD_SIZE] = {0};
  928. std::set<THex> occupyable;
  929. gs->curB->getAccessibilityMap(ac, stack->doubleWide(), stack->attackerOwned, false, occupyable, stack->hasBonusOfType(Bonus::FLYING), stack);
  930. THex pr[BFIELD_SIZE];
  931. int dist[BFIELD_SIZE];
  932. gs->curB->makeBFS(stack->position, ac, pr, dist, stack->doubleWide(), stack->attackerOwned, stack->hasBonusOfType(Bonus::FLYING), false);
  933. for(int i=0; i<BFIELD_SIZE; ++i)
  934. {
  935. if(pr[i] == -1)
  936. ret.push_back(-1);
  937. else
  938. ret.push_back(dist[i]);
  939. }
  940. if(predecessors)
  941. {
  942. memcpy(predecessors, pr, BFIELD_SIZE * sizeof(THex));
  943. }
  944. return ret;
  945. }
  946. SpellCasting::ESpellCastProblem CBattleCallback::battleCanCastThisSpell( const CSpell * spell )
  947. {
  948. if(!gs->curB)
  949. {
  950. tlog1 << "battleCanCastThisSpell called when there is no battle!\n";
  951. return SpellCasting::NO_HERO_TO_CAST_SPELL;
  952. }
  953. return gs->curB->battleCanCastThisSpell(player, spell, SpellCasting::HERO_CASTING);
  954. }
  955. si8 CBattleCallback::battleGetTacticDist()
  956. {
  957. if (!gs->curB)
  958. {
  959. tlog1 << "battleGetTacticDist called when no battle!\n";
  960. return 0;
  961. }
  962. if (gs->curB->sides[gs->curB->tacticsSide] == player)
  963. {
  964. return gs->curB->tacticDistance;
  965. }
  966. return 0;
  967. }
  968. ui8 CBattleCallback::battleGetMySide()
  969. {
  970. if (!gs->curB)
  971. {
  972. tlog1 << "battleGetMySide called when no battle!\n";
  973. return 0;
  974. }
  975. return gs->curB->sides[1] == player;
  976. }
  977. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  978. {
  979. MakeAction ma;
  980. ma.ba = *action;
  981. sendRequest(&ma);
  982. return true;
  983. }
  984. int CBattleCallback::battleGetSurrenderCost()
  985. {
  986. if (!gs->curB)
  987. {
  988. tlog1 << "battleGetSurrenderCost called when no battle!\n";
  989. return -1;
  990. }
  991. return gs->curB->getSurrenderingCost(player);
  992. }