CCallback.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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::setArtifact(const CGHeroInstance * hero, ui16 pos, int artID)
  372. {
  373. if(player != hero->tempOwner)
  374. return false;
  375. SetArtifact sa(hero->id, pos, artID);
  376. sendRequest(&sa);
  377. return true;
  378. }
  379. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  380. {
  381. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  382. if(town->tempOwner!=player)
  383. return false;
  384. CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  385. for(int i=0;i<b->resources.size();i++)
  386. if(b->resources[i] > gs->players[player].resources[i])
  387. return false; //lack of resources
  388. BuildStructure pack(town->id,buildingID);
  389. sendRequest(&pack);
  390. return true;
  391. }
  392. int CCallback::battleGetBattlefieldType()
  393. {
  394. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  395. return gs->battleGetBattlefieldType();
  396. }
  397. int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
  398. {
  399. //TODO - write
  400. return -1;
  401. }
  402. std::vector<CObstacleInstance> CCallback::battleGetAllObstacles()
  403. {
  404. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  405. if(gs->curB)
  406. return gs->curB->obstacles;
  407. else
  408. return std::vector<CObstacleInstance>();
  409. }
  410. int CCallback::battleGetStack(int pos, bool onlyAlive)
  411. {
  412. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  413. return gs->battleGetStack(pos, onlyAlive);
  414. }
  415. const CStack* CCallback::battleGetStackByID(int ID, bool onlyAlive)
  416. {
  417. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  418. if(!gs->curB) return NULL;
  419. return gs->curB->getStack(ID, onlyAlive);
  420. }
  421. int CCallback::battleMakeAction(BattleAction* action)
  422. {
  423. MakeCustomAction mca(*action);
  424. sendRequest(&mca);
  425. return 0;
  426. }
  427. const CStack* CCallback::battleGetStackByPos(int pos, bool onlyAlive)
  428. {
  429. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  430. return battleGetStackByID(battleGetStack(pos, onlyAlive), onlyAlive);
  431. }
  432. int CCallback::battleGetPos(int stack)
  433. {
  434. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  435. if(!gs->curB)
  436. {
  437. tlog2<<"battleGetPos called when there is no battle!"<<std::endl;
  438. return -1;
  439. }
  440. for(size_t g=0; g<gs->curB->stacks.size(); ++g)
  441. {
  442. if(gs->curB->stacks[g]->ID == stack)
  443. return gs->curB->stacks[g]->position;
  444. }
  445. return -1;
  446. }
  447. std::map<int, CStack> CCallback::battleGetStacks()
  448. {
  449. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  450. std::map<int, CStack> ret;
  451. if(!gs->curB) //there is no battle
  452. {
  453. return ret;
  454. }
  455. for(size_t g=0; g<gs->curB->stacks.size(); ++g)
  456. {
  457. ret[gs->curB->stacks[g]->ID] = *(gs->curB->stacks[g]);
  458. }
  459. return ret;
  460. }
  461. void CCallback::getStackQueue( std::vector<const CStack *> &out, int howMany )
  462. {
  463. if(!gs->curB)
  464. {
  465. tlog2 << "battleGetStackQueue called when there is not battle!" << std::endl;
  466. return;
  467. }
  468. gs->curB->getStackQueue(out, howMany);
  469. }
  470. CCreature CCallback::battleGetCreature(int number)
  471. {
  472. boost::shared_lock<boost::shared_mutex> lock(*gs->mx); //TODO use me?
  473. if(!gs->curB)
  474. {
  475. tlog2<<"battleGetCreature called when there is no battle!"<<std::endl;
  476. }
  477. for(size_t h=0; h<gs->curB->stacks.size(); ++h)
  478. {
  479. if(gs->curB->stacks[h]->ID == number) //creature found
  480. return *(gs->curB->stacks[h]->creature);
  481. }
  482. #ifndef __GNUC__
  483. throw new std::exception("Cannot find the creature");
  484. #else
  485. throw new std::exception();
  486. #endif
  487. }
  488. std::vector<int> CCallback::battleGetAvailableHexes(int ID, bool addOccupiable)
  489. {
  490. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  491. if(!gs->curB)
  492. {
  493. tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
  494. return std::vector<int>();
  495. }
  496. return gs->curB->getAccessibility(ID, addOccupiable);
  497. //return gs->battleGetRange(ID);
  498. }
  499. bool CCallback::battleIsStackMine(int ID)
  500. {
  501. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  502. if(!gs->curB)
  503. {
  504. tlog2<<"battleIsStackMine called when there is no battle!"<<std::endl;
  505. return false;
  506. }
  507. for(size_t h=0; h<gs->curB->stacks.size(); ++h)
  508. {
  509. if(gs->curB->stacks[h]->ID == ID) //creature found
  510. return gs->curB->stacks[h]->owner == player;
  511. }
  512. return false;
  513. }
  514. bool CCallback::battleCanShoot(int ID, int dest)
  515. {
  516. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  517. if(!gs->curB) return false;
  518. return gs->battleCanShoot(ID, dest);
  519. }
  520. bool CCallback::battleCanCastSpell()
  521. {
  522. if(!gs->curB) //there is no battle
  523. return false;
  524. if(gs->curB->side1 == player)
  525. return gs->curB->castSpells[0] == 0 && gs->curB->heroes[0]->getArt(17);
  526. else
  527. return gs->curB->castSpells[1] == 0 && gs->curB->heroes[1]->getArt(17);
  528. }
  529. bool CCallback::battleCanFlee()
  530. {
  531. return gs->battleCanFlee(player);
  532. }
  533. const CGTownInstance *CCallback::battleGetDefendedTown()
  534. {
  535. if(!gs->curB || gs->curB->tid == -1)
  536. return NULL;
  537. return static_cast<const CGTownInstance *>(gs->map->objects[gs->curB->tid]);
  538. }
  539. ui8 CCallback::battleGetWallState(int partOfWall)
  540. {
  541. if(!gs->curB || gs->curB->siege == 0)
  542. {
  543. return 0;
  544. }
  545. return gs->curB->si.wallState[partOfWall];
  546. }
  547. int CCallback::battleGetWallUnderHex(int hex)
  548. {
  549. if(!gs->curB || gs->curB->siege == 0)
  550. {
  551. return -1;
  552. }
  553. return gs->curB->hexToWallPart(hex);
  554. }
  555. std::pair<ui32, ui32> CCallback::battleEstimateDamage(int attackerID, int defenderID)
  556. {
  557. if(!gs->curB)
  558. return std::make_pair(0, 0);
  559. const CGHeroInstance * attackerHero, * defenderHero;
  560. if(gs->curB->side1 == player)
  561. {
  562. attackerHero = gs->curB->heroes[0];
  563. defenderHero = gs->curB->heroes[1];
  564. }
  565. else
  566. {
  567. attackerHero = gs->curB->heroes[1];
  568. defenderHero = gs->curB->heroes[0];
  569. }
  570. const CStack * attacker = gs->curB->getStack(attackerID, false),
  571. * defender = gs->curB->getStack(defenderID);
  572. return BattleInfo::calculateDmgRange(attacker, defender, attackerHero, defenderHero, battleCanShoot(attacker->ID, defender->position), 0);
  573. }
  574. ui8 CCallback::battleGetSiegeLevel()
  575. {
  576. if(!gs->curB)
  577. return 0;
  578. return gs->curB->siege;
  579. }
  580. const CGHeroInstance * CCallback::battleGetFightingHero(ui8 side) const
  581. {
  582. if(!gs->curB)
  583. return 0;
  584. return gs->curB->heroes[side];
  585. }
  586. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  587. {
  588. if(town->tempOwner != player) return;
  589. GarrisonHeroSwap pack(town->id);
  590. sendRequest(&pack);
  591. }
  592. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  593. {
  594. if(hero->tempOwner != player) return;
  595. BuyArtifact pack(hero->id,aid);
  596. sendRequest(&pack);
  597. }
  598. std::vector < const CGObjectInstance * > CCallback::getBlockingObjs( int3 pos ) const
  599. {
  600. std::vector<const CGObjectInstance *> ret;
  601. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  602. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  603. return ret;
  604. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  605. ret.push_back(obj);
  606. return ret;
  607. }
  608. std::vector < const CGObjectInstance * > CCallback::getVisitableObjs( int3 pos ) const
  609. {
  610. std::vector<const CGObjectInstance *> ret;
  611. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  612. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  613. return ret;
  614. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].visitableObjects)
  615. ret.push_back(obj);
  616. return ret;
  617. }
  618. void CCallback::getMarketOffer( int t1, int t2, int &give, int &rec, int mode/*=0*/ ) const
  619. {
  620. if(mode) return; //TODO - support
  621. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  622. //if(gs->resVals[t1] >= gs->resVals[t2])
  623. float r = gs->resVals[t1], //price of given resource
  624. g = gs->resVals[t2] / gs->getMarketEfficiency(player,mode); //price of wanted resource
  625. if(r>g) //if given resource is more expensive than wanted
  626. {
  627. rec = ceil(r / g);
  628. give = 1;
  629. }
  630. else //if wanted resource is more expensive
  631. {
  632. give = ceil(g / r);
  633. rec = 1;
  634. }
  635. }
  636. std::vector < const CGObjectInstance * > CCallback::getFlaggableObjects(int3 pos) const
  637. {
  638. if(!isVisible(pos))
  639. return std::vector < const CGObjectInstance * >();
  640. std::vector < const CGObjectInstance * > ret;
  641. std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  642. for(size_t b=0; b<objs.size(); ++b)
  643. {
  644. 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))
  645. ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  646. }
  647. return ret;
  648. }
  649. int3 CCallback::getMapSize() const
  650. {
  651. return CGI->mh->sizes;
  652. }
  653. void CCallback::trade( int mode, int id1, int id2, int val1 )
  654. {
  655. int p1, p2;
  656. getMarketOffer(id1,id2,p1,p2,mode);
  657. TradeOnMarketplace pack(player,mode,id1,id2,val1);
  658. sendRequest(&pack);
  659. }
  660. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  661. {
  662. const_cast<CGHeroInstance*>(hero)->army.formation = tight;
  663. SetFormation pack(hero->id,tight);
  664. sendRequest(&pack);
  665. }
  666. void CCallback::setSelection(const CArmedInstance * obj)
  667. {
  668. SetSelection ss;
  669. ss.player = player;
  670. ss.id = obj->id;
  671. sendRequest(&ss);
  672. if(obj->ID == HEROI_TYPE)
  673. {
  674. cl->gs->calculatePaths(static_cast<const CGHeroInstance *>(obj), *cl->pathInfo);
  675. //nasty workaround. TODO: nice workaround
  676. cl->gs->getPlayer(player)->currentSelection = obj->id;
  677. }
  678. }
  679. void CCallback::recruitHero(const CGTownInstance *town, const CGHeroInstance *hero)
  680. {
  681. ui8 i=0;
  682. for(; i<gs->players[player].availableHeroes.size(); i++)
  683. {
  684. if(gs->players[player].availableHeroes[i] == hero)
  685. {
  686. HireHero pack(i,town->id);
  687. sendRequest(&pack);
  688. return;
  689. }
  690. }
  691. }
  692. std::vector<const CGHeroInstance *> CCallback::getAvailableHeroes(const CGTownInstance * town) const
  693. {
  694. std::vector<const CGHeroInstance *> ret(gs->players[player].availableHeroes.size());
  695. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  696. return ret;
  697. }
  698. const TerrainTile * CCallback::getTileInfo( int3 tile ) const
  699. {
  700. if(!gs->map->isInTheMap(tile))
  701. {
  702. tlog1 << tile << "is outside the map! (call to getTileInfo)\n";
  703. return NULL;
  704. }
  705. if(!isVisible(tile, player)) return NULL;
  706. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  707. return &gs->map->getTile(tile);
  708. }
  709. int CCallback::canBuildStructure( const CGTownInstance *t, int ID )
  710. {
  711. return gs->canBuildStructure(t,ID);
  712. }
  713. std::set<int> CCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
  714. {
  715. return gs->getBuildingRequiments(t,ID);
  716. }
  717. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  718. {
  719. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  720. return gs->getPath(src,dest,hero, ret);
  721. }
  722. void CCallback::save( const std::string &fname )
  723. {
  724. cl->save(fname);
  725. }
  726. void CCallback::sendMessage(const std::string &mess)
  727. {
  728. PlayerMessage pm(player, mess);
  729. sendRequest(&pm);
  730. }
  731. void CCallback::buildBoat( const IShipyard *obj )
  732. {
  733. BuildBoat bb;
  734. bb.objid = obj->o->id;
  735. sendRequest(&bb);
  736. }
  737. template <typename T>
  738. void CCallback::sendRequest(const T* request)
  739. {
  740. //TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
  741. if(waitTillRealize)
  742. cl->waitingRequest.set(true);
  743. *cl->serv << request;
  744. if(waitTillRealize)
  745. cl->waitingRequest.waitWhileTrue();
  746. }
  747. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  748. :gs(GS), cl(C), player(Player)
  749. {
  750. waitTillRealize = false;
  751. }
  752. const CMapHeader * CCallback::getMapHeader() const
  753. {
  754. return gs->map;
  755. }
  756. const CGPathNode * CCallback::getPathInfo( int3 tile )
  757. {
  758. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  759. }
  760. bool CCallback::getPath2( int3 dest, CGPath &ret )
  761. {
  762. if (!gs->map->isInTheMap(dest))
  763. return false;
  764. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  765. assert(cl->pathInfo->hero == h);
  766. if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
  767. {
  768. recalculatePaths();
  769. }
  770. return cl->pathInfo->getPath(dest, ret);
  771. }
  772. void CCallback::recalculatePaths()
  773. {
  774. gs->calculatePaths(cl->IGameCallback::getSelectedHero(player), *cl->pathInfo);
  775. }
  776. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  777. {
  778. gs->calculatePaths(hero, out, src, movement);
  779. }
  780. InfoAboutHero::InfoAboutHero()
  781. {
  782. details = NULL;
  783. hclass = NULL;
  784. portrait = -1;
  785. }
  786. InfoAboutHero::~InfoAboutHero()
  787. {
  788. delete details;
  789. }
  790. void InfoAboutHero::initFromHero( const CGHeroInstance *h, bool detailed )
  791. {
  792. owner = h->tempOwner;
  793. hclass = h->type->heroClass;
  794. name = h->name;
  795. portrait = h->portrait;
  796. army = h->army;
  797. if(detailed)
  798. {
  799. //include details about hero
  800. details = new Details;
  801. details->luck = h->getCurrentLuck();
  802. details->morale = h->getCurrentMorale();
  803. details->mana = h->mana;
  804. details->primskills.resize(PRIMARY_SKILLS);
  805. for (int i = 0; i < PRIMARY_SKILLS ; i++)
  806. {
  807. details->primskills[i] = h->getPrimSkillLevel(i);
  808. }
  809. }
  810. else
  811. {
  812. //hide info about hero stacks counts using descriptives names ids
  813. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army.slots.begin(); i != army.slots.end(); ++i)
  814. {
  815. i->second.second = CCreature::getQuantityID(i->second.second);
  816. }
  817. }
  818. }
  819. InfoAboutTown::InfoAboutTown()
  820. {
  821. tType = NULL;
  822. details = NULL;
  823. fortLevel = 0;
  824. owner = -1;
  825. }
  826. InfoAboutTown::~InfoAboutTown()
  827. {
  828. delete details;
  829. }
  830. void InfoAboutTown::initFromTown( const CGTownInstance *t, bool detailed )
  831. {
  832. obj = t;
  833. army = t->army;
  834. built = t->builded;
  835. fortLevel = t->fortLevel();
  836. name = t->name;
  837. tType = t->town;
  838. owner = t->tempOwner;
  839. if(detailed)
  840. {
  841. //include details about hero
  842. details = new Details;
  843. details->goldIncome = t->dailyIncome();
  844. details->customRes = vstd::contains(t->builtBuildings, 15);
  845. details->hallLevel = t->hallLevel();
  846. details->garrisonedHero = t->garrisonHero;
  847. }
  848. /*else
  849. {
  850. //hide info about hero stacks counts
  851. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army.slots.begin(); i != army.slots.end(); ++i)
  852. {
  853. i->second.second = 0;
  854. }
  855. }*/
  856. }