CCallback.cpp 24 KB

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