CCallback.cpp 21 KB

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