CCallback.cpp 25 KB

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