CCallback.cpp 28 KB

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