CCallback.cpp 25 KB

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