CCallback.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. #include "stdafx.h"
  2. #include "CAdvmapInterface.h"
  3. #include "CCallback.h"
  4. #include "CGameInfo.h"
  5. #include "CGameState.h"
  6. #include "CPathfinder.h"
  7. #include "CPlayerInterface.h"
  8. #include "CPlayerInterface.h"
  9. #include "client/Client.h"
  10. #include "hch/CAmbarCendamo.h"
  11. #include "hch/CBuildingHandler.h"
  12. #include "hch/CDefObjInfoHandler.h"
  13. #include "hch/CGeneralTextHandler.h"
  14. #include "hch/CHeroHandler.h"
  15. #include "hch/CObjectHandler.h"
  16. #include "hch/CTownHandler.h"
  17. #include "lib/Connection.h"
  18. #include "lib/NetPacks.h"
  19. #include "mapHandler.h"
  20. #include <boost/foreach.hpp>
  21. #include <boost/thread.hpp>
  22. #include <boost/thread/shared_mutex.hpp>
  23. #ifdef min
  24. #undef min
  25. #endif
  26. #ifdef max
  27. #undef max
  28. #endif
  29. extern CSharedCond<std::set<IPack*> > mess;
  30. int gcd(int x, int y)
  31. {
  32. int temp;
  33. if (y > x)
  34. std::swap(x,y);
  35. while (y != 0)
  36. {
  37. temp = y;
  38. y = x-y;
  39. x = temp;
  40. if (y > x)
  41. std::swap(x,y);
  42. }
  43. return x;
  44. }
  45. HeroMoveDetails::HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho)
  46. :src(Src),dst(Dst),ho(Ho)
  47. {
  48. owner = ho->getOwner();
  49. };
  50. bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
  51. {
  52. CGHeroInstance * hero = NULL;
  53. if (idtype==0)
  54. {
  55. if (player==-1)
  56. hero=gs->players[player+1].heroes[ID];
  57. else
  58. hero=gs->players[player].heroes[ID];
  59. }
  60. else if (idtype==1 && player>=0) //looking for it in local area
  61. {
  62. for (size_t i=0; i < gs->players[player].heroes.size(); ++i)
  63. {
  64. if (gs->players[player].heroes[i]->type->ID == ID)
  65. hero = gs->players[player].heroes[i];
  66. }
  67. }
  68. else //idtype==1; player<0
  69. {
  70. for(std::map<ui8, PlayerState>::iterator j=gs->players.begin(); j!=gs->players.end(); ++j)
  71. {
  72. for (size_t i=0; i < (*j).second.heroes.size(); ++i)
  73. {
  74. if ((*j).second.heroes[i]->type->ID == ID)
  75. {
  76. hero = (*j).second.heroes[i];
  77. }
  78. }
  79. }
  80. }
  81. if (!hero)
  82. return false; //can't find hero
  83. if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
  84. return false; //invalid path
  85. //check path format
  86. if (pathType==0)
  87. CPathfinder::convertPath(path,pathType);
  88. if (pathType>1)
  89. #ifndef __GNUC__
  90. throw std::exception("Unknown path format");
  91. #else
  92. throw std::exception();
  93. #endif
  94. CPath * ourPath = path;
  95. if(!ourPath)
  96. return false;
  97. for(int i=ourPath->nodes.size()-1; i>0; i--)
  98. {
  99. int3 stpos(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z),
  100. endpos(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
  101. HeroMoveDetails curd(stpos,endpos,hero);
  102. *cl->serv << ui16(501) << hero->id << stpos << endpos;
  103. {//wait till there is server answer
  104. boost::unique_lock<boost::mutex> lock(*mess.mx);
  105. while(std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>) == mess.res->end())
  106. mess.cv->wait(lock);
  107. std::set<IPack*>::iterator itr = std::find_if(mess.res->begin(),mess.res->end(),IPack::isType<501>);
  108. TryMoveHero tmh = *static_cast<TryMoveHero*>(*itr);
  109. mess.res->erase(itr);
  110. if(!tmh.result)
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. void CCallback::selectionMade(int selection, int asker)
  117. {
  118. *cl->serv << ui16(2001) << ui32(asker) << ui32(selection);
  119. }
  120. void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount)
  121. {
  122. if(player!=obj->tempOwner) return;
  123. *cl->serv << ui16(506) << obj->id << ID << amount;
  124. }
  125. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  126. {
  127. if(((player>=0) && obj->tempOwner != player) || obj->army.slots.size()<2)
  128. return false;
  129. *cl->serv << ui16(503) << obj->id << ui8(stackPos);
  130. return true;
  131. }
  132. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  133. {
  134. *cl->serv << ui16(507) << obj->id << ui8(stackPos) << ui32(newID);
  135. return false;
  136. }
  137. void CCallback::endTurn()
  138. {
  139. tlog5 << "Player "<<(unsigned)player<<" end his turn."<<std::endl;
  140. cl->serv->wmx->lock();
  141. *cl->serv << ui16(100); //report that we ended turn
  142. cl->serv->wmx->unlock();
  143. }
  144. UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos) const
  145. {
  146. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  147. return gs->getUpgradeInfo(const_cast<CArmedInstance*>(obj),stackPos);
  148. }
  149. const StartInfo * CCallback::getStartInfo() const
  150. {
  151. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  152. return gs->scenarioOps;
  153. }
  154. int CCallback::howManyTowns() const
  155. {
  156. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  157. return gs->players[player].towns.size();
  158. }
  159. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
  160. {
  161. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  162. if (!mode)
  163. return gs->players[gs->currentPlayer].towns[val];
  164. else
  165. {
  166. //TODO: add some smart ID to the CTownInstance
  167. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  168. //{
  169. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  170. // return gs->players[gs->currentPlayer].towns[i];
  171. //}
  172. return NULL;
  173. }
  174. return NULL;
  175. }
  176. int CCallback::howManyHeroes() const
  177. {
  178. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  179. return gs->players[player].heroes.size();
  180. }
  181. const CGHeroInstance * CCallback::getHeroInfo(int val, int mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
  182. {
  183. boost::shared_lock<boost::shared_mutex> lock(*gs->mx); //TODO use me?
  184. //if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  185. // return NULL;
  186. if (!mode) //esrial id
  187. {
  188. if(val<gs->players[player].heroes.size())
  189. {
  190. return gs->players[player].heroes[val];
  191. }
  192. else
  193. {
  194. return NULL;
  195. }
  196. }
  197. else if(mode==1) //it's hero type id
  198. {
  199. for (size_t i=0; i < gs->players[player].heroes.size(); ++i)
  200. {
  201. if (gs->players[player].heroes[i]->type->ID==val)
  202. {
  203. return gs->players[player].heroes[i];
  204. }
  205. }
  206. }
  207. else //object id
  208. {
  209. return static_cast<CGHeroInstance*>(gs->map->objects[val]);
  210. }
  211. return NULL;
  212. }
  213. int CCallback::getResourceAmount(int type) const
  214. {
  215. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  216. return gs->players[player].resources[type];
  217. }
  218. std::vector<si32> CCallback::getResourceAmount() const
  219. {
  220. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  221. return gs->players[player].resources;
  222. }
  223. int CCallback::getDate(int mode) const
  224. {
  225. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  226. return gs->getDate(mode);
  227. }
  228. std::vector < std::string > CCallback::getObjDescriptions(int3 pos) const
  229. {
  230. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  231. std::vector<std::string> ret;
  232. if(!isVisible(pos,player))
  233. return ret;
  234. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  235. ret.push_back(obj->getHoverText());
  236. return ret;
  237. }
  238. bool CCallback::verifyPath(CPath * path, bool blockSea) const
  239. {
  240. for (size_t i=0; i < path->nodes.size(); ++i)
  241. {
  242. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->blocked
  243. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->visitable)))
  244. return false; //path is wrong - one of the tiles is blocked
  245. if (blockSea)
  246. {
  247. if (i==0)
  248. continue;
  249. if (
  250. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype==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!=water))
  253. ||
  254. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype!=water)
  255. &&
  256. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==water))
  257. ||
  258. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==rock)
  259. )
  260. return false;
  261. }
  262. }
  263. return true;
  264. }
  265. std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap() const
  266. {
  267. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  268. return gs->players[player].fogOfWarMap;
  269. }
  270. bool CCallback::isVisible(int3 pos, int Player) const
  271. {
  272. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  273. return gs->players[Player].fogOfWarMap[pos.x][pos.y][pos.z];
  274. }
  275. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur) const
  276. {
  277. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  278. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  279. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  280. {
  281. for (size_t j=0; j < (*i).second.towns.size(); ++j)
  282. {
  283. if ( ( isVisible((*i).second.towns[j],player) ) || (*i).first==player)
  284. {
  285. ret.push_back((*i).second.towns[j]);
  286. }
  287. }
  288. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  289. return ret;
  290. }
  291. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur) const
  292. {
  293. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  294. std::vector < const CGHeroInstance *> ret;
  295. for(size_t i=0;i<gs->map->heroes.size();i++)
  296. {
  297. if( (gs->map->heroes[i]->tempOwner==player) ||
  298. (isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
  299. {
  300. ret.push_back(gs->map->heroes[i]);
  301. }
  302. }
  303. return ret;
  304. }
  305. bool CCallback::isVisible(int3 pos) const
  306. {
  307. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  308. return isVisible(pos,player);
  309. }
  310. bool CCallback::isVisible( CGObjectInstance *obj, int Player ) const
  311. {
  312. //object is visible when at least one blocked tile is visible
  313. for(int fx=0; fx<8; ++fx)
  314. {
  315. for(int fy=0; fy<6; ++fy)
  316. {
  317. int3 pos = obj->pos + int3(fx-7,fy-5,0);
  318. if(gs->map->isInTheMap(pos)
  319. && !((obj->defInfo->blockMap[fy] >> (7 - fx)) & 1)
  320. && isVisible(pos,Player) )
  321. return true;
  322. }
  323. }
  324. return false;
  325. }
  326. int CCallback::getMyColor() const
  327. {
  328. return player;
  329. }
  330. int CCallback::getHeroSerial(const CGHeroInstance * hero) const
  331. {
  332. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  333. for (size_t i=0; i<gs->players[player].heroes.size();i++)
  334. {
  335. if (gs->players[player].heroes[i]==hero)
  336. return i;
  337. }
  338. return -1;
  339. }
  340. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj) const
  341. {
  342. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  343. if(!obj)
  344. return NULL;
  345. if(obj->ID == HEROI_TYPE)
  346. return &(dynamic_cast<const CGHeroInstance*>(obj))->army;
  347. else if(obj->ID == TOWNI_TYPE)
  348. return &(dynamic_cast<const CGTownInstance*>(obj)->army);
  349. else return NULL;
  350. }
  351. int CCallback::swapCreatures(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  352. {
  353. if(s1->tempOwner != player || s2->tempOwner != player)
  354. return -1;
  355. *cl->serv << ui16(502) << ui8(1) << s1->id << ui8(p1) << s2->id << ui8(p2);
  356. return 0;
  357. }
  358. int CCallback::mergeStacks(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2)
  359. {
  360. if ((s1->tempOwner!= player || s2->tempOwner!=player))
  361. {
  362. return -1;
  363. }
  364. *cl->serv << ui16(502) << ui8(2) << s1->id << ui8(p1) << s2->id << ui8(p2);
  365. return 0;
  366. }
  367. int CCallback::splitStack(const CGObjectInstance *s1, const CGObjectInstance *s2, int p1, int p2, int val)
  368. {
  369. if (s1->tempOwner!= player || s2->tempOwner!=player || (!val))
  370. {
  371. return -1;
  372. }
  373. *cl->serv << ui16(502) << ui8(3) << s1->id << ui8(p1) << s2->id << ui8(p2) << si32(val);
  374. return 0;
  375. }
  376. bool CCallback::dismissHero(const CGHeroInstance *hero)
  377. {
  378. if(player!=hero->tempOwner) return false;
  379. *cl->serv << ui16(500) << hero->id;
  380. return true;
  381. }
  382. int CCallback::getMySerial() const
  383. {
  384. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  385. return gs->players[player].serial;
  386. }
  387. bool CCallback::swapArifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)
  388. {
  389. if(player!=hero1->tempOwner || player!=hero2->tempOwner)
  390. return false;
  391. *cl->serv << ui16(509) << hero1->id << pos1 << hero2->id << pos2;
  392. return true;
  393. }
  394. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  395. {
  396. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  397. if(town->tempOwner!=player)
  398. return false;
  399. CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  400. for(int i=0;i<7;i++)
  401. if(b->resources[i] > gs->players[player].resources[i])
  402. return false; //lack of resources
  403. *cl->serv << ui16(504) << town->id << buildingID;
  404. return true;
  405. }
  406. int CCallback::battleGetBattlefieldType()
  407. {
  408. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  409. return gs->battleGetBattlefieldType();
  410. }
  411. int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
  412. {
  413. //TODO - write
  414. return -1;
  415. }
  416. std::vector<CObstacleInstance> CCallback::battleGetAllObstacles()
  417. {
  418. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  419. if(gs->curB)
  420. return gs->curB->obstacles;
  421. else
  422. return std::vector<CObstacleInstance>();
  423. }
  424. int CCallback::battleGetStack(int pos)
  425. {
  426. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  427. return gs->battleGetStack(pos);
  428. }
  429. CStack* CCallback::battleGetStackByID(int ID)
  430. {
  431. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  432. if(!gs->curB) return NULL;
  433. return gs->curB->getStack(ID);
  434. }
  435. int CCallback::battleMakeAction(BattleAction* action)
  436. {
  437. *cl->serv << ui16(3003) << *action;
  438. return 0;
  439. }
  440. CStack* CCallback::battleGetStackByPos(int pos)
  441. {
  442. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  443. return battleGetStackByID(battleGetStack(pos));
  444. }
  445. int CCallback::battleGetPos(int stack)
  446. {
  447. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  448. if(!gs->curB)
  449. {
  450. tlog2<<"battleGetPos called when there is no battle!"<<std::endl;
  451. return -1;
  452. }
  453. for(size_t g=0; g<gs->curB->stacks.size(); ++g)
  454. {
  455. if(gs->curB->stacks[g]->ID == stack)
  456. return gs->curB->stacks[g]->position;
  457. }
  458. return -1;
  459. }
  460. std::map<int, CStack> CCallback::battleGetStacks()
  461. {
  462. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  463. std::map<int, CStack> ret;
  464. if(!gs->curB) //there is no battle
  465. {
  466. return ret;
  467. }
  468. for(size_t g=0; g<gs->curB->stacks.size(); ++g)
  469. {
  470. ret[gs->curB->stacks[g]->ID] = *(gs->curB->stacks[g]);
  471. }
  472. return ret;
  473. }
  474. std::vector<CStack> CCallback::battleGetStackQueue()
  475. {
  476. if(!gs->curB)
  477. {
  478. tlog2<<"battleGetStackQueue called when there is not battle!"<<std::endl;
  479. return std::vector<CStack>();
  480. }
  481. return gs->curB->getStackQueue();
  482. }
  483. CCreature CCallback::battleGetCreature(int number)
  484. {
  485. boost::shared_lock<boost::shared_mutex> lock(*gs->mx); //TODO use me?
  486. if(!gs->curB)
  487. {
  488. tlog2<<"battleGetCreature called when there is no battle!"<<std::endl;
  489. }
  490. for(size_t h=0; h<gs->curB->stacks.size(); ++h)
  491. {
  492. if(gs->curB->stacks[h]->ID == number) //creature found
  493. return *(gs->curB->stacks[h]->creature);
  494. }
  495. #ifndef __GNUC__
  496. throw new std::exception("Cannot find the creature");
  497. #else
  498. throw new std::exception();
  499. #endif
  500. }
  501. std::vector<int> CCallback::battleGetAvailableHexes(int ID, bool addOccupiable)
  502. {
  503. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  504. if(!gs->curB)
  505. {
  506. tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
  507. return std::vector<int>();
  508. }
  509. return gs->curB->getAccessibility(ID, addOccupiable);
  510. //return gs->battleGetRange(ID);
  511. }
  512. bool CCallback::battleIsStackMine(int ID)
  513. {
  514. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  515. if(!gs->curB)
  516. {
  517. tlog2<<"battleIsStackMine called when there is no battle!"<<std::endl;
  518. return false;
  519. }
  520. for(size_t h=0; h<gs->curB->stacks.size(); ++h)
  521. {
  522. if(gs->curB->stacks[h]->ID == ID) //creature found
  523. return gs->curB->stacks[h]->owner == player;
  524. }
  525. return false;
  526. }
  527. bool CCallback::battleCanShoot(int ID, int dest)
  528. {
  529. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  530. CStack *our = battleGetStackByID(ID), *dst = battleGetStackByPos(dest);
  531. if(!our || !dst || !gs->curB) return false;
  532. for(size_t g=0; g<our->effects.size(); ++g)
  533. {
  534. if(61 == our->effects[g].id) //forgetfulness
  535. return false;
  536. }
  537. if(vstd::contains(our->abilities,SHOOTER)//it's shooter
  538. && our->owner != dst->owner
  539. && dst->alive()
  540. && !gs->curB->isStackBlocked(ID)
  541. && our->shots
  542. )
  543. return true;
  544. return false;
  545. }
  546. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  547. {
  548. if(town->tempOwner != player) return;
  549. *cl->serv << ui16(508) << si32(town->id);
  550. }
  551. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  552. {
  553. if(hero->tempOwner != player) return;
  554. *cl->serv << ui16(510) << hero->id << ui32(aid);
  555. }
  556. std::vector < const CGObjectInstance * > CCallback::getBlockingObjs( int3 pos ) const
  557. {
  558. std::vector<const CGObjectInstance *> ret;
  559. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  560. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  561. return ret;
  562. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  563. ret.push_back(obj);
  564. return ret;
  565. }
  566. std::vector < const CGObjectInstance * > CCallback::getVisitableObjs( int3 pos ) const
  567. {
  568. std::vector<const CGObjectInstance *> ret;
  569. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  570. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  571. return ret;
  572. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].visitableObjects)
  573. ret.push_back(obj);
  574. return ret;
  575. }
  576. void CCallback::getMarketOffer( int t1, int t2, int &give, int &rec, int mode/*=0*/ ) const
  577. {
  578. if(mode) return; //TODO - support
  579. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  580. //if(gs->resVals[t1] >= gs->resVals[t2])
  581. float r = gs->resVals[t1],
  582. g = gs->resVals[t2] / gs->getMarketEfficiency(player,mode);
  583. if(r>g)
  584. {
  585. rec = ceil(r / g);
  586. give = 1;
  587. }
  588. else
  589. {
  590. give = ceil(g / r);
  591. rec = 1;
  592. }
  593. }
  594. std::vector < const CGObjectInstance * > CCallback::getFlaggableObjects(int3 pos) const
  595. {
  596. if(!isVisible(pos))
  597. return std::vector < const CGObjectInstance * >();
  598. std::vector < const CGObjectInstance * > ret;
  599. std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  600. for(size_t b=0; b<objs.size(); ++b)
  601. {
  602. 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))
  603. ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  604. }
  605. return ret;
  606. }
  607. int3 CCallback::getMapSize() const
  608. {
  609. return CGI->mh->sizes;
  610. }
  611. void CCallback::trade( int mode, int id1, int id2, int val1 )
  612. {
  613. int p1, p2;
  614. getMarketOffer(id1,id2,p1,p2,mode);
  615. *cl->serv << ui16(511) << ui8(player) << ui32(mode) << ui32(id1) << ui32(id2) << ui32(val1);
  616. }
  617. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  618. {
  619. const_cast<CGHeroInstance*>(hero)->army.formation = tight;
  620. *cl->serv << ui16(512) << hero->id << ui8(tight);
  621. }
  622. void CCallback::setSelection(const CArmedInstance * obj)
  623. {
  624. SetSelection ss;
  625. ss.player = player;
  626. ss.id = obj->id;
  627. *cl->serv << ui16(514) << ss;
  628. }
  629. void CCallback::recruitHero(const CGTownInstance *town, const CGHeroInstance *hero)
  630. {
  631. ui8 i=0;
  632. for(;i<gs->players[player].availableHeroes.size();i++)
  633. if(gs->players[player].availableHeroes[i] == hero)
  634. break;
  635. if(i>1) return;
  636. *cl->serv << ui16(515) << town->id << i;
  637. }
  638. std::vector<const CGHeroInstance *> CCallback::getAvailableHeroes(const CGTownInstance * town) const
  639. {
  640. std::vector<const CGHeroInstance *> ret(gs->players[player].availableHeroes.size());
  641. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  642. return ret;
  643. }