CCallback.cpp 27 KB

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