CCallback.cpp 24 KB

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