CCallback.cpp 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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(int pos, bool onlyAlive)
  460. {
  461. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  462. return battleGetStackByID(gs->battleGetStack(pos, onlyAlive), onlyAlive);
  463. }
  464. int 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 -1;
  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 -1;
  478. }
  479. std::vector<const CStack*> CBattleCallback::battleGetStacks()
  480. {
  481. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  482. std::vector<const CStack*> 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. ret.push_back(s);
  490. return ret;
  491. }
  492. void CBattleCallback::getStackQueue( std::vector<const CStack *> &out, int howMany )
  493. {
  494. if(!gs->curB)
  495. {
  496. tlog2 << "battleGetStackQueue called when there is not battle!" << std::endl;
  497. return;
  498. }
  499. gs->curB->getStackQueue(out, howMany);
  500. }
  501. std::vector<int> CBattleCallback::battleGetAvailableHexes(int ID, bool addOccupiable)
  502. {
  503. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  504. if(!gs->curB)
  505. {
  506. tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
  507. return std::vector<int>();
  508. }
  509. return gs->curB->getAccessibility(ID, addOccupiable);
  510. //return gs->battleGetRange(ID);
  511. }
  512. bool CBattleCallback::battleCanShoot(int ID, int dest)
  513. {
  514. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  515. if(!gs->curB) return false;
  516. return gs->battleCanShoot(ID, dest);
  517. }
  518. bool CBattleCallback::battleCanCastSpell()
  519. {
  520. if(!gs->curB) //there is no battle
  521. return false;
  522. if(gs->curB->side1 == player)
  523. return gs->curB->castSpells[0] == 0 && gs->curB->heroes[0] && gs->curB->heroes[0]->getArt(17);
  524. else
  525. return gs->curB->castSpells[1] == 0 && gs->curB->heroes[1] && gs->curB->heroes[1]->getArt(17);
  526. }
  527. bool CBattleCallback::battleCanFlee()
  528. {
  529. return gs->battleCanFlee(player);
  530. }
  531. const CGTownInstance *CBattleCallback::battleGetDefendedTown()
  532. {
  533. if(!gs->curB || gs->curB->tid == -1)
  534. return NULL;
  535. return static_cast<const CGTownInstance *>(gs->map->objects[gs->curB->tid].get());
  536. }
  537. ui8 CBattleCallback::battleGetWallState(int partOfWall)
  538. {
  539. if(!gs->curB || gs->curB->siege == 0)
  540. {
  541. return 0;
  542. }
  543. return gs->curB->si.wallState[partOfWall];
  544. }
  545. int CBattleCallback::battleGetWallUnderHex(int hex)
  546. {
  547. if(!gs->curB || gs->curB->siege == 0)
  548. {
  549. return -1;
  550. }
  551. return gs->curB->hexToWallPart(hex);
  552. }
  553. std::pair<ui32, ui32> CBattleCallback::battleEstimateDamage(int attackerID, int defenderID)
  554. {
  555. if(!gs->curB)
  556. return std::make_pair(0, 0);
  557. const CGHeroInstance * attackerHero, * defenderHero;
  558. if(gs->curB->side1 == player)
  559. {
  560. attackerHero = gs->curB->heroes[0];
  561. defenderHero = gs->curB->heroes[1];
  562. }
  563. else
  564. {
  565. attackerHero = gs->curB->heroes[1];
  566. defenderHero = gs->curB->heroes[0];
  567. }
  568. const CStack * attacker = gs->curB->getStack(attackerID, false),
  569. * defender = gs->curB->getStack(defenderID);
  570. return gs->curB->calculateDmgRange(attacker, defender, attackerHero, defenderHero, battleCanShoot(attacker->ID, defender->position), 0, false);
  571. }
  572. ui8 CBattleCallback::battleGetSiegeLevel()
  573. {
  574. if(!gs->curB)
  575. return 0;
  576. return gs->curB->siege;
  577. }
  578. const CGHeroInstance * CBattleCallback::battleGetFightingHero(ui8 side) const
  579. {
  580. if(!gs->curB)
  581. return 0;
  582. return gs->curB->heroes[side];
  583. }
  584. template <typename T>
  585. void CBattleCallback::sendRequest(const T* request)
  586. {
  587. //TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
  588. if(waitTillRealize)
  589. cl->waitingRequest.set(true);
  590. *cl->serv << request;
  591. if(waitTillRealize)
  592. cl->waitingRequest.waitWhileTrue();
  593. }
  594. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  595. {
  596. if(town->tempOwner != player) return;
  597. GarrisonHeroSwap pack(town->id);
  598. sendRequest(&pack);
  599. }
  600. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  601. {
  602. if(hero->tempOwner != player) return;
  603. BuyArtifact pack(hero->id,aid);
  604. sendRequest(&pack);
  605. }
  606. std::vector < const CGObjectInstance * > CCallback::getBlockingObjs( int3 pos ) const
  607. {
  608. std::vector<const CGObjectInstance *> ret;
  609. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  610. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  611. return ret;
  612. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  613. ret.push_back(obj);
  614. return ret;
  615. }
  616. std::vector < const CGObjectInstance * > CCallback::getVisitableObjs( int3 pos ) const
  617. {
  618. std::vector<const CGObjectInstance *> ret;
  619. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  620. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  621. return ret;
  622. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].visitableObjects)
  623. ret.push_back(obj);
  624. return ret;
  625. }
  626. std::vector < const CGObjectInstance * > CCallback::getFlaggableObjects(int3 pos) const
  627. {
  628. if(!isVisible(pos))
  629. return std::vector < const CGObjectInstance * >();
  630. std::vector < const CGObjectInstance * > ret;
  631. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  632. for(size_t b=0; b<objs.size(); ++b)
  633. {
  634. 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))
  635. ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  636. }
  637. return ret;
  638. }
  639. int3 CCallback::getMapSize() const
  640. {
  641. return CGI->mh->sizes;
  642. }
  643. void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
  644. {
  645. TradeOnMarketplace pack;
  646. pack.market = market;
  647. pack.hero = hero;
  648. pack.mode = mode;
  649. pack.r1 = id1;
  650. pack.r2 = id2;
  651. pack.val = val1;
  652. sendRequest(&pack);
  653. }
  654. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  655. {
  656. const_cast<CGHeroInstance*>(hero)-> formation = tight;
  657. SetFormation pack(hero->id,tight);
  658. sendRequest(&pack);
  659. }
  660. void CCallback::setSelection(const CArmedInstance * obj)
  661. {
  662. SetSelection ss;
  663. ss.player = player;
  664. ss.id = obj->id;
  665. sendRequest(&ss);
  666. if(obj->ID == HEROI_TYPE)
  667. {
  668. cl->gs->calculatePaths(static_cast<const CGHeroInstance *>(obj), *cl->pathInfo);
  669. //nasty workaround. TODO: nice workaround
  670. cl->gs->getPlayer(player)->currentSelection = obj->id;
  671. }
  672. }
  673. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  674. {
  675. ui8 i=0;
  676. for(; i<gs->players[player].availableHeroes.size(); i++)
  677. {
  678. if(gs->players[player].availableHeroes[i] == hero)
  679. {
  680. HireHero pack(i,townOrTavern->id);
  681. pack.player = player;
  682. sendRequest(&pack);
  683. return;
  684. }
  685. }
  686. }
  687. std::vector<const CGHeroInstance *> CCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  688. {
  689. std::vector<const CGHeroInstance *> ret(gs->players[player].availableHeroes.size());
  690. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  691. return ret;
  692. }
  693. const TerrainTile * CCallback::getTileInfo( int3 tile ) const
  694. {
  695. if(!gs->map->isInTheMap(tile))
  696. {
  697. tlog1 << tile << "is outside the map! (call to getTileInfo)\n";
  698. return NULL;
  699. }
  700. if(!isVisible(tile, player)) return NULL;
  701. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  702. return &gs->map->getTile(tile);
  703. }
  704. int CCallback::canBuildStructure( const CGTownInstance *t, int ID )
  705. {
  706. return gs->canBuildStructure(t,ID);
  707. }
  708. std::set<int> CCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
  709. {
  710. return gs->getBuildingRequiments(t,ID);
  711. }
  712. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  713. {
  714. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  715. return gs->getPath(src,dest,hero, ret);
  716. }
  717. void CCallback::save( const std::string &fname )
  718. {
  719. cl->save(fname);
  720. }
  721. void CCallback::sendMessage(const std::string &mess)
  722. {
  723. PlayerMessage pm(player, mess);
  724. sendRequest(&pm);
  725. }
  726. void CCallback::buildBoat( const IShipyard *obj )
  727. {
  728. BuildBoat bb;
  729. bb.objid = obj->o->id;
  730. sendRequest(&bb);
  731. }
  732. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  733. :CBattleCallback(GS, Player, C)
  734. {
  735. waitTillRealize = false;
  736. }
  737. const CMapHeader * CCallback::getMapHeader() const
  738. {
  739. return gs->map;
  740. }
  741. const CGPathNode * CCallback::getPathInfo( int3 tile )
  742. {
  743. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  744. }
  745. bool CCallback::getPath2( int3 dest, CGPath &ret )
  746. {
  747. if (!gs->map->isInTheMap(dest))
  748. return false;
  749. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  750. assert(cl->pathInfo->hero == h);
  751. if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
  752. {
  753. recalculatePaths();
  754. }
  755. return cl->pathInfo->getPath(dest, ret);
  756. }
  757. void CCallback::recalculatePaths()
  758. {
  759. gs->calculatePaths(cl->IGameCallback::getSelectedHero(player), *cl->pathInfo);
  760. }
  761. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  762. {
  763. gs->calculatePaths(hero, out, src, movement);
  764. }
  765. int3 CCallback::getGrailPos( float &outKnownRatio )
  766. {
  767. if (CGObelisk::obeliskCount == 0)
  768. {
  769. outKnownRatio = 0.0f;
  770. }
  771. else
  772. {
  773. outKnownRatio = (float)CGObelisk::visited[gs->getPlayerTeam(player)->id] / CGObelisk::obeliskCount;
  774. }
  775. return gs->map->grailPos;
  776. }
  777. void CCallback::dig( const CGObjectInstance *hero )
  778. {
  779. DigWithHero dwh;
  780. dwh.id = hero->id;
  781. sendRequest(&dwh);
  782. }
  783. void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
  784. {
  785. CastAdvSpell cas;
  786. cas.hid = hero->id;
  787. cas.sid = spellID;
  788. cas.pos = pos;
  789. sendRequest(&cas);
  790. }
  791. bool CCallback::hasAccess(int playerId) const
  792. {
  793. return gs->getPlayerRelations( playerId, player ) || player < 0;
  794. }
  795. si8 CBattleCallback::battleHasDistancePenalty( int stackID, int destHex )
  796. {
  797. return gs->curB->hasDistancePenalty(stackID, destHex);
  798. }
  799. si8 CBattleCallback::battleHasWallPenalty( int stackID, int destHex )
  800. {
  801. return gs->curB->hasWallPenalty(stackID, destHex);
  802. }
  803. si8 CBattleCallback::battleCanTeleportTo(int stackID, int destHex, int telportLevel)
  804. {
  805. return gs->curB->canTeleportTo(stackID, destHex, telportLevel);
  806. }
  807. int CCallback::getPlayerStatus(int player) const
  808. {
  809. const PlayerState *ps = gs->getPlayer(player, false);
  810. if(!ps)
  811. return -1;
  812. return ps->status;
  813. }
  814. std::string CCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  815. {
  816. return "GOSSIP TEST";
  817. }
  818. std::vector < const CGObjectInstance * > CCallback::getMyObjects() const
  819. {
  820. std::vector < const CGObjectInstance * > ret;
  821. for (int g=0; g<gs->map->objects.size(); ++g)
  822. {
  823. if (gs->map->objects[g]->tempOwner == LOCPLINT->playerID)
  824. {
  825. ret.push_back(gs->map->objects[g]);
  826. }
  827. }
  828. return ret;
  829. }
  830. std::vector < const CGDwelling * > CCallback::getMyDwellings() const
  831. {
  832. std::vector < const CGDwelling * > ret;
  833. BOOST_FOREACH(CGDwelling * dw, gs->getPlayer(player)->dwellings)
  834. {
  835. ret.push_back(dw);
  836. }
  837. return ret;
  838. }
  839. int CCallback::getPlayerRelations( ui8 color1, ui8 color2 ) const
  840. {
  841. return gs->getPlayerRelations(color1, color2);
  842. }
  843. InfoAboutTown::InfoAboutTown()
  844. {
  845. tType = NULL;
  846. details = NULL;
  847. fortLevel = 0;
  848. owner = -1;
  849. }
  850. InfoAboutTown::~InfoAboutTown()
  851. {
  852. delete details;
  853. }
  854. void InfoAboutTown::initFromTown( const CGTownInstance *t, bool detailed )
  855. {
  856. obj = t;
  857. army = ArmyDescriptor(t, detailed);
  858. built = t->builded;
  859. fortLevel = t->fortLevel();
  860. name = t->name;
  861. tType = t->town;
  862. owner = t->tempOwner;
  863. if(detailed)
  864. {
  865. //include details about hero
  866. details = new Details;
  867. details->goldIncome = t->dailyIncome();
  868. details->customRes = vstd::contains(t->builtBuildings, 15);
  869. details->hallLevel = t->hallLevel();
  870. details->garrisonedHero = t->garrisonHero;
  871. }
  872. //TODO: adjust undetailed info about army to our count of thieves guilds
  873. }
  874. void InfoAboutTown::initFromGarrison(const CGGarrison *garr, bool detailed)
  875. {
  876. obj = garr;
  877. fortLevel = 0;
  878. army = ArmyDescriptor(garr, detailed);
  879. name = CGI->generaltexth->names[33]; // "Garrison"
  880. owner = garr->tempOwner;
  881. built = false;
  882. tType = NULL;
  883. // Show detailed info only to owning player.
  884. if(detailed)
  885. {
  886. details = new InfoAboutTown::Details;
  887. details->customRes = false;
  888. details->garrisonedHero = false;
  889. details->goldIncome = -1;
  890. details->hallLevel = -1;
  891. }
  892. }
  893. bool CBattleCallback::hasAccess( int playerId ) const
  894. {
  895. return playerId == player || player < 0;
  896. }
  897. CBattleCallback::CBattleCallback(CGameState *GS, int Player, CClient *C )
  898. {
  899. gs = GS;
  900. player = Player;
  901. cl = C;
  902. }