CCallback.cpp 27 KB

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