CCallback.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. #include "stdafx.h"
  2. #include "CCallback.h"
  3. #include "lib/CCreatureHandler.h"
  4. #include "client/CGameInfo.h"
  5. #include "lib/CGameState.h"
  6. #include "lib/BattleState.h"
  7. #include "client/CPlayerInterface.h"
  8. #include "client/Client.h"
  9. #include "lib/map.h"
  10. #include "lib/CBuildingHandler.h"
  11. #include "lib/CDefObjInfoHandler.h"
  12. #include "lib/CGeneralTextHandler.h"
  13. #include "lib/CHeroHandler.h"
  14. #include "lib/CObjectHandler.h"
  15. #include "lib/Connection.h"
  16. #include "lib/NetPacks.h"
  17. #include "client/mapHandler.h"
  18. #include <boost/foreach.hpp>
  19. #include <boost/thread.hpp>
  20. #include <boost/thread/shared_mutex.hpp>
  21. #include "lib/CSpellHandler.h"
  22. #include "lib/CArtHandler.h"
  23. #ifdef min
  24. #undef min
  25. #endif
  26. #ifdef max
  27. #undef max
  28. #endif
  29. /*
  30. * CCallback.cpp, part of VCMI engine
  31. *
  32. * Authors: listed in file AUTHORS in main folder
  33. *
  34. * License: GNU General Public License v2.0 or later
  35. * Full text of license available in license.txt file, in main folder
  36. *
  37. */
  38. template <ui16 N> bool isType(CPack *pack)
  39. {
  40. return pack->getType() == N;
  41. }
  42. bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
  43. {
  44. CastleTeleportHero pack(who->id, where->id, 1);
  45. sendRequest(&pack);
  46. return true;
  47. }
  48. bool CCallback::moveHero(const CGHeroInstance *h, int3 dst)
  49. {
  50. MoveHero pack(dst,h->id);
  51. sendRequest(&pack);
  52. return true;
  53. }
  54. void CCallback::selectionMade(int selection, int asker)
  55. {
  56. QueryReply pack(asker,selection);
  57. *cl->serv << &pack;
  58. }
  59. void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level/*=-1*/)
  60. {
  61. if(player!=obj->tempOwner && obj->ID != 106)
  62. return;
  63. RecruitCreatures pack(obj->id,ID,amount,level);
  64. sendRequest(&pack);
  65. }
  66. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  67. {
  68. if(((player>=0) && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
  69. return false;
  70. DisbandCreature pack(stackPos,obj->id);
  71. sendRequest(&pack);
  72. return true;
  73. }
  74. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  75. {
  76. UpgradeCreature pack(stackPos,obj->id,newID);
  77. sendRequest(&pack);
  78. return false;
  79. }
  80. void CCallback::endTurn()
  81. {
  82. tlog5 << "Player " << (unsigned)player << " ended his turn." << std::endl;
  83. EndTurn pack;
  84. sendRequest(&pack); //report that we ended turn
  85. }
  86. UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos) const
  87. {
  88. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  89. return gs->getUpgradeInfo(obj->getStack(stackPos));
  90. }
  91. const StartInfo * CCallback::getStartInfo() const
  92. {
  93. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  94. return gs->scenarioOps;
  95. }
  96. int CCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
  97. {
  98. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  99. //if there is a battle
  100. if(gs->curB)
  101. return gs->curB->getSpellCost(sp, caster);
  102. //if there is no battle
  103. return caster->getSpellCost(sp);
  104. }
  105. int CCallback::estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const
  106. {
  107. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  108. if(!gs->curB) //no battle
  109. {
  110. if (hero) //but we see hero's spellbook
  111. return gs->curB->calculateSpellDmg(sp, hero, NULL, hero->getSpellSchoolLevel(sp), hero->getPrimSkillLevel(2));
  112. else
  113. return 0; //mage guild
  114. }
  115. //gs->getHero(gs->currentPlayer)
  116. const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1];
  117. return gs->curB->calculateSpellDmg(sp, ourHero, NULL, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(2));
  118. }
  119. void CCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj)
  120. {
  121. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  122. if(obj == NULL)
  123. return;
  124. if(obj->ID == TOWNI_TYPE || obj->ID == 95) //it is a town or adv map tavern
  125. {
  126. gs->obtainPlayersStats(thi, gs->players[player].towns.size());
  127. }
  128. else if(obj->ID == 97) //Den of Thieves
  129. {
  130. gs->obtainPlayersStats(thi, 20);
  131. }
  132. }
  133. int CCallback::howManyTowns() const
  134. {
  135. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  136. return gs->players[player].towns.size();
  137. }
  138. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
  139. {
  140. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  141. if (!mode)
  142. {
  143. const std::vector<ConstTransitivePtr<CGTownInstance> > &towns = gs->players[gs->currentPlayer].towns;
  144. if(val < towns.size())
  145. return towns[val];
  146. else
  147. return NULL;
  148. }
  149. else if(mode == 1)
  150. {
  151. const CGObjectInstance *obj = getObjectInfo(val);
  152. if(!obj)
  153. return NULL;
  154. if(obj->ID != TOWNI_TYPE)
  155. return NULL;
  156. else
  157. return static_cast<const CGTownInstance *>(obj);
  158. }
  159. return NULL;
  160. }
  161. bool CCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown &dest ) const
  162. {
  163. if(!isVisible(town, player)) //it's not a town or it's not visible for layer
  164. return false;
  165. bool detailed = hasAccess(town->tempOwner);
  166. //TODO vision support
  167. if(town->ID == TOWNI_TYPE)
  168. dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
  169. else if(town->ID == 33 || town->ID == 219)
  170. dest.initFromGarrison(static_cast<const CGGarrison *>(town), detailed);
  171. else
  172. return false;
  173. return true;
  174. }
  175. int3 CCallback::guardingCreaturePosition (int3 pos) const
  176. {
  177. return gs->guardingCreaturePosition(pos);
  178. }
  179. int CCallback::howManyHeroes(bool includeGarrisoned) const
  180. {
  181. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  182. return cl->getHeroCount(player,includeGarrisoned);
  183. }
  184. const CGHeroInstance * CCallback::getHeroInfo(int val, int mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
  185. {
  186. boost::shared_lock<boost::shared_mutex> lock(*gs->mx); //TODO use me?
  187. //if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  188. // return NULL;
  189. if (!mode) //esrial id
  190. {
  191. if(val<gs->players[player].heroes.size())
  192. {
  193. return gs->players[player].heroes[val];
  194. }
  195. else
  196. {
  197. return NULL;
  198. }
  199. }
  200. else if(mode==1) //it's hero type id
  201. {
  202. for (size_t i=0; i < gs->players[player].heroes.size(); ++i)
  203. {
  204. if (gs->players[player].heroes[i]->type->ID==val)
  205. {
  206. return gs->players[player].heroes[i];
  207. }
  208. }
  209. }
  210. else //object id
  211. {
  212. return static_cast<const CGHeroInstance*>(gs->map->objects[val].get());
  213. }
  214. return NULL;
  215. }
  216. const CGObjectInstance * CCallback::getObjectInfo(int ID) const
  217. {
  218. //TODO: check for visibility
  219. return gs->map->objects[ID];
  220. }
  221. bool CCallback::getHeroInfo( const CGObjectInstance *hero, InfoAboutHero &dest ) const
  222. {
  223. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
  224. if(!h || !isVisible(h->getPosition(false))) //it's not a hero or it's not visible for layer
  225. return false;
  226. //TODO vision support
  227. dest.initFromHero(h, hasAccess(h->tempOwner));
  228. return true;
  229. }
  230. int CCallback::getResourceAmount(int type) const
  231. {
  232. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  233. return gs->players[player].resources[type];
  234. }
  235. std::vector<si32> CCallback::getResourceAmount() const
  236. {
  237. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  238. return gs->players[player].resources;
  239. }
  240. int CCallback::getDate(int mode) const
  241. {
  242. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  243. return gs->getDate(mode);
  244. }
  245. std::vector < std::string > CCallback::getObjDescriptions(int3 pos) const
  246. {
  247. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  248. std::vector<std::string> ret;
  249. if(!isVisible(pos,player))
  250. return ret;
  251. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  252. ret.push_back(obj->getHoverText());
  253. return ret;
  254. }
  255. bool CCallback::verifyPath(CPath * path, bool blockSea) const
  256. {
  257. for (size_t i=0; i < path->nodes.size(); ++i)
  258. {
  259. if ( CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  260. && (! (CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
  261. return false; //path is wrong - one of the tiles is blocked
  262. if (blockSea)
  263. {
  264. if (i==0)
  265. continue;
  266. if (
  267. ((CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tertype==TerrainTile::water)
  268. &&
  269. (CGI->mh->map->terrain[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tertype!=TerrainTile::water))
  270. ||
  271. ((CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tertype!=TerrainTile::water)
  272. &&
  273. (CGI->mh->map->terrain[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tertype==TerrainTile::water))
  274. ||
  275. (CGI->mh->map->terrain[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tertype==TerrainTile::rock)
  276. )
  277. return false;
  278. }
  279. }
  280. return true;
  281. }
  282. std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap() const
  283. {
  284. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  285. return gs->getPlayerTeam(player)->fogOfWarMap;
  286. }
  287. bool CCallback::isVisible(int3 pos, int Player) const
  288. {
  289. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  290. return gs->map->isInTheMap(pos) && gs->isVisible(pos, Player);
  291. }
  292. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur) const
  293. {
  294. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  295. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  296. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  297. {
  298. for (size_t j=0; j < (*i).second.towns.size(); ++j)
  299. {
  300. if ((*i).first==player
  301. || (isVisible((*i).second.towns[j],player) && !onlyOur))
  302. {
  303. ret.push_back((*i).second.towns[j]);
  304. }
  305. }
  306. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  307. return ret;
  308. }
  309. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur) const
  310. {
  311. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  312. std::vector < const CGHeroInstance *> ret;
  313. for(size_t i=0;i<gs->map->heroes.size();i++)
  314. {
  315. if( (gs->map->heroes[i]->tempOwner==player) ||
  316. (isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
  317. {
  318. ret.push_back(gs->map->heroes[i]);
  319. }
  320. }
  321. return ret;
  322. }
  323. bool CCallback::isVisible(int3 pos) const
  324. {
  325. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  326. return isVisible(pos,player);
  327. }
  328. bool CCallback::isVisible( const CGObjectInstance *obj, int Player ) const
  329. {
  330. return gs->isVisible(obj, Player);
  331. }
  332. int CCallback::getMyColor() const
  333. {
  334. return player;
  335. }
  336. int CCallback::getHeroSerial(const CGHeroInstance * hero) const
  337. {
  338. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  339. for (size_t i=0; i<gs->players[player].heroes.size();i++)
  340. {
  341. if (gs->players[player].heroes[i]==hero)
  342. return i;
  343. }
  344. return -1;
  345. }
  346. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj) const
  347. {
  348. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  349. const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
  350. if(!armi)
  351. return NULL;
  352. else
  353. return armi;
  354. }
  355. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  356. {
  357. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  358. sendRequest(&pack);
  359. return 0;
  360. }
  361. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  362. {
  363. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  364. sendRequest(&pack);
  365. return 0;
  366. }
  367. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)
  368. {
  369. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  370. sendRequest(&pack);
  371. return 0;
  372. }
  373. bool CCallback::dismissHero(const CGHeroInstance *hero)
  374. {
  375. if(player!=hero->tempOwner) return false;
  376. DismissHero pack(hero->id);
  377. sendRequest(&pack);
  378. return true;
  379. }
  380. // int CCallback::getMySerial() const
  381. // {
  382. // boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  383. // return gs->players[player].serial;
  384. // }
  385. bool CCallback::swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)
  386. {
  387. if(player!=hero1->tempOwner && player!=hero2->tempOwner)
  388. return false;
  389. ExchangeArtifacts ea(hero1->id, hero2->id, pos1, pos2);
  390. sendRequest(&ea);
  391. return true;
  392. }
  393. /**
  394. * Assembles or disassembles a combination artifact.
  395. * @param hero Hero holding the artifact(s).
  396. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  397. * @param assemble True for assembly operation, false for disassembly.
  398. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  399. * artifact to assemble to. Otherwise it's not used.
  400. */
  401. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)
  402. {
  403. if (player != hero->tempOwner)
  404. return false;
  405. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  406. sendRequest(&aa);
  407. return true;
  408. }
  409. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  410. {
  411. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  412. if(town->tempOwner!=player)
  413. return false;
  414. const CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  415. for(int i=0;i<b->resources.size();i++)
  416. if(b->resources[i] > gs->players[player].resources[i])
  417. return false; //lack of resources
  418. BuildStructure pack(town->id,buildingID);
  419. sendRequest(&pack);
  420. return true;
  421. }
  422. int CBattleCallback::battleGetBattlefieldType()
  423. {
  424. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  425. //return gs->battleGetBattlefieldType();
  426. if(!gs->curB)
  427. {
  428. tlog2<<"battleGetBattlefieldType called when there is no battle!"<<std::endl;
  429. return -1;
  430. }
  431. return gs->curB->battlefieldType;
  432. }
  433. int CBattleCallback::battleGetObstaclesAtTile(THex tile) //returns bitfield
  434. {
  435. //TODO - write
  436. return -1;
  437. }
  438. std::vector<CObstacleInstance> CBattleCallback::battleGetAllObstacles()
  439. {
  440. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  441. if(gs->curB)
  442. return gs->curB->obstacles;
  443. else
  444. return std::vector<CObstacleInstance>();
  445. }
  446. const CStack* CBattleCallback::battleGetStackByID(int ID, bool onlyAlive)
  447. {
  448. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  449. if(!gs->curB) return NULL;
  450. return gs->curB->getStack(ID, onlyAlive);
  451. }
  452. int CBattleCallback::battleMakeAction(BattleAction* action)
  453. {
  454. assert(action->actionType == BattleAction::HERO_SPELL);
  455. MakeCustomAction mca(*action);
  456. sendRequest(&mca);
  457. return 0;
  458. }
  459. const CStack* CBattleCallback::battleGetStackByPos(THex pos, bool onlyAlive)
  460. {
  461. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  462. return gs->curB->battleGetStack(pos, onlyAlive);
  463. }
  464. THex CBattleCallback::battleGetPos(int stack)
  465. {
  466. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  467. if(!gs->curB)
  468. {
  469. tlog2<<"battleGetPos called when there is no battle!"<<std::endl;
  470. return THex::INVALID;
  471. }
  472. for(size_t g=0; g<gs->curB->stacks.size(); ++g)
  473. {
  474. if(gs->curB->stacks[g]->ID == stack)
  475. return gs->curB->stacks[g]->position;
  476. }
  477. return THex::INVALID;
  478. }
  479. std::vector<const CStack*> CBattleCallback::battleGetStacks(bool onlyAlive /*= true*/)
  480. {
  481. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  482. std::vector<const CStack*> ret;
  483. if(!gs->curB) //there is no battle
  484. {
  485. tlog2<<"battleGetStacks called when there is no battle!"<<std::endl;
  486. return ret;
  487. }
  488. BOOST_FOREACH(const CStack *s, gs->curB->stacks)
  489. if(s->alive() || !onlyAlive)
  490. ret.push_back(s);
  491. return ret;
  492. }
  493. void CBattleCallback::getStackQueue( std::vector<const CStack *> &out, int howMany )
  494. {
  495. if(!gs->curB)
  496. {
  497. tlog2 << "battleGetStackQueue called when there is not battle!" << std::endl;
  498. return;
  499. }
  500. gs->curB->getStackQueue(out, howMany);
  501. }
  502. std::vector<THex> CBattleCallback::battleGetAvailableHexes(const CStack * stack, bool addOccupiable)
  503. {
  504. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  505. if(!gs->curB)
  506. {
  507. tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
  508. return std::vector<THex>();
  509. }
  510. return gs->curB->getAccessibility(stack, addOccupiable);
  511. //return gs->battleGetRange(ID);
  512. }
  513. bool CBattleCallback::battleCanShoot(const CStack * stack, THex dest)
  514. {
  515. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  516. if(!gs->curB) return false;
  517. return gs->curB->battleCanShoot(stack, dest);
  518. }
  519. bool CBattleCallback::battleCanCastSpell()
  520. {
  521. if(!gs->curB) //there is no battle
  522. return false;
  523. if(gs->curB->sides[0] == player)
  524. return gs->curB->castSpells[0] == 0 && gs->curB->heroes[0] && gs->curB->heroes[0]->getArt(17);
  525. else
  526. return gs->curB->castSpells[1] == 0 && gs->curB->heroes[1] && gs->curB->heroes[1]->getArt(17);
  527. }
  528. bool CBattleCallback::battleCanFlee()
  529. {
  530. return gs->curB->battleCanFlee(player);
  531. }
  532. const CGTownInstance *CBattleCallback::battleGetDefendedTown()
  533. {
  534. if(!gs->curB || gs->curB->town == NULL)
  535. return NULL;
  536. return gs->curB->town;
  537. }
  538. ui8 CBattleCallback::battleGetWallState(int partOfWall)
  539. {
  540. if(!gs->curB || gs->curB->siege == 0)
  541. {
  542. return 0;
  543. }
  544. return gs->curB->si.wallState[partOfWall];
  545. }
  546. int CBattleCallback::battleGetWallUnderHex(THex hex)
  547. {
  548. if(!gs->curB || gs->curB->siege == 0)
  549. {
  550. return -1;
  551. }
  552. return gs->curB->hexToWallPart(hex);
  553. }
  554. TDmgRange CBattleCallback::battleEstimateDamage(const CStack * attacker, const CStack * defender, TDmgRange * retaliationDmg)
  555. {
  556. if(!gs->curB)
  557. return std::make_pair(0, 0);
  558. const CGHeroInstance * attackerHero, * defenderHero;
  559. bool shooting = battleCanShoot(attacker, defender->position);
  560. if(gs->curB->sides[0] == player)
  561. {
  562. attackerHero = gs->curB->heroes[0];
  563. defenderHero = gs->curB->heroes[1];
  564. }
  565. else
  566. {
  567. attackerHero = gs->curB->heroes[1];
  568. defenderHero = gs->curB->heroes[0];
  569. }
  570. TDmgRange ret = gs->curB->calculateDmgRange(attacker, defender, attackerHero, defenderHero, shooting, 0, false);
  571. if(retaliationDmg)
  572. {
  573. if(shooting)
  574. {
  575. retaliationDmg->first = retaliationDmg->second = 0;
  576. }
  577. else
  578. {
  579. ui32 TDmgRange::* pairElems[] = {&TDmgRange::first, &TDmgRange::second};
  580. for (int i=0; i<2; ++i)
  581. {
  582. BattleStackAttacked bsa;
  583. bsa.damageAmount = ret.*pairElems[i];
  584. retaliationDmg->*pairElems[!i] = gs->curB->calculateDmgRange(defender, attacker, bsa.newAmount, attacker->count, attackerHero, defenderHero, false, false, false).*pairElems[!i];
  585. }
  586. }
  587. }
  588. return ret;
  589. }
  590. ui8 CBattleCallback::battleGetSiegeLevel()
  591. {
  592. if(!gs->curB)
  593. return 0;
  594. return gs->curB->siege;
  595. }
  596. const CGHeroInstance * CBattleCallback::battleGetFightingHero(ui8 side) const
  597. {
  598. if(!gs->curB)
  599. return 0;
  600. return gs->curB->heroes[side];
  601. }
  602. template <typename T>
  603. void CBattleCallback::sendRequest(const T* request)
  604. {
  605. //TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
  606. if(waitTillRealize)
  607. cl->waitingRequest.set(true);
  608. *cl->serv << request;
  609. if(waitTillRealize)
  610. cl->waitingRequest.waitWhileTrue();
  611. }
  612. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  613. {
  614. if(town->tempOwner != player) return;
  615. GarrisonHeroSwap pack(town->id);
  616. sendRequest(&pack);
  617. }
  618. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  619. {
  620. if(hero->tempOwner != player) return;
  621. BuyArtifact pack(hero->id,aid);
  622. sendRequest(&pack);
  623. }
  624. std::vector < const CGObjectInstance * > CCallback::getBlockingObjs( int3 pos ) const
  625. {
  626. std::vector<const CGObjectInstance *> ret;
  627. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  628. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  629. return ret;
  630. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  631. ret.push_back(obj);
  632. return ret;
  633. }
  634. std::vector < const CGObjectInstance * > CCallback::getVisitableObjs( int3 pos ) const
  635. {
  636. std::vector<const CGObjectInstance *> ret;
  637. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  638. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  639. return ret;
  640. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].visitableObjects)
  641. ret.push_back(obj);
  642. return ret;
  643. }
  644. std::vector < const CGObjectInstance * > CCallback::getFlaggableObjects(int3 pos) const
  645. {
  646. if(!isVisible(pos))
  647. return std::vector < const CGObjectInstance * >();
  648. std::vector < const CGObjectInstance * > ret;
  649. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  650. for(size_t b=0; b<objs.size(); ++b)
  651. {
  652. 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))
  653. ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  654. }
  655. return ret;
  656. }
  657. int3 CCallback::getMapSize() const
  658. {
  659. return CGI->mh->sizes;
  660. }
  661. void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
  662. {
  663. TradeOnMarketplace pack;
  664. pack.market = market;
  665. pack.hero = hero;
  666. pack.mode = mode;
  667. pack.r1 = id1;
  668. pack.r2 = id2;
  669. pack.val = val1;
  670. sendRequest(&pack);
  671. }
  672. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  673. {
  674. const_cast<CGHeroInstance*>(hero)-> formation = tight;
  675. SetFormation pack(hero->id,tight);
  676. sendRequest(&pack);
  677. }
  678. void CCallback::setSelection(const CArmedInstance * obj)
  679. {
  680. SetSelection ss;
  681. ss.player = player;
  682. ss.id = obj->id;
  683. sendRequest(&ss);
  684. if(obj->ID == HEROI_TYPE)
  685. {
  686. cl->gs->calculatePaths(static_cast<const CGHeroInstance *>(obj), *cl->pathInfo);
  687. //nasty workaround. TODO: nice workaround
  688. cl->gs->getPlayer(player)->currentSelection = obj->id;
  689. }
  690. }
  691. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  692. {
  693. ui8 i=0;
  694. for(; i<gs->players[player].availableHeroes.size(); i++)
  695. {
  696. if(gs->players[player].availableHeroes[i] == hero)
  697. {
  698. HireHero pack(i,townOrTavern->id);
  699. pack.player = player;
  700. sendRequest(&pack);
  701. return;
  702. }
  703. }
  704. }
  705. std::vector<const CGHeroInstance *> CCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  706. {
  707. std::vector<const CGHeroInstance *> ret(gs->players[player].availableHeroes.size());
  708. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  709. return ret;
  710. }
  711. const TerrainTile * CCallback::getTileInfo( int3 tile ) const
  712. {
  713. if(!gs->map->isInTheMap(tile))
  714. {
  715. tlog1 << tile << "is outside the map! (call to getTileInfo)\n";
  716. return NULL;
  717. }
  718. if(!isVisible(tile, player)) return NULL;
  719. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  720. return &gs->map->getTile(tile);
  721. }
  722. int CCallback::canBuildStructure( const CGTownInstance *t, int ID )
  723. {
  724. return gs->canBuildStructure(t,ID);
  725. }
  726. std::set<int> CCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
  727. {
  728. return gs->getBuildingRequiments(t,ID);
  729. }
  730. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  731. {
  732. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  733. return gs->getPath(src,dest,hero, ret);
  734. }
  735. void CCallback::save( const std::string &fname )
  736. {
  737. cl->save(fname);
  738. }
  739. void CCallback::sendMessage(const std::string &mess)
  740. {
  741. PlayerMessage pm(player, mess);
  742. sendRequest(&pm);
  743. }
  744. void CCallback::buildBoat( const IShipyard *obj )
  745. {
  746. BuildBoat bb;
  747. bb.objid = obj->o->id;
  748. sendRequest(&bb);
  749. }
  750. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  751. :CBattleCallback(GS, Player, C)
  752. {
  753. waitTillRealize = false;
  754. }
  755. const CMapHeader * CCallback::getMapHeader() const
  756. {
  757. return gs->map;
  758. }
  759. const CGPathNode * CCallback::getPathInfo( int3 tile )
  760. {
  761. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  762. }
  763. bool CCallback::getPath2( int3 dest, CGPath &ret )
  764. {
  765. if (!gs->map->isInTheMap(dest))
  766. return false;
  767. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  768. assert(cl->pathInfo->hero == h);
  769. if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
  770. {
  771. recalculatePaths();
  772. }
  773. return cl->pathInfo->getPath(dest, ret);
  774. }
  775. void CCallback::recalculatePaths()
  776. {
  777. gs->calculatePaths(cl->IGameCallback::getSelectedHero(player), *cl->pathInfo);
  778. }
  779. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  780. {
  781. gs->calculatePaths(hero, out, src, movement);
  782. }
  783. int3 CCallback::getGrailPos( float &outKnownRatio )
  784. {
  785. if (CGObelisk::obeliskCount == 0)
  786. {
  787. outKnownRatio = 0.0f;
  788. }
  789. else
  790. {
  791. outKnownRatio = (float)CGObelisk::visited[gs->getPlayerTeam(player)->id] / CGObelisk::obeliskCount;
  792. }
  793. return gs->map->grailPos;
  794. }
  795. void CCallback::dig( const CGObjectInstance *hero )
  796. {
  797. DigWithHero dwh;
  798. dwh.id = hero->id;
  799. sendRequest(&dwh);
  800. }
  801. void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
  802. {
  803. CastAdvSpell cas;
  804. cas.hid = hero->id;
  805. cas.sid = spellID;
  806. cas.pos = pos;
  807. sendRequest(&cas);
  808. }
  809. bool CCallback::hasAccess(int playerId) const
  810. {
  811. return gs->getPlayerRelations( playerId, player ) || player < 0;
  812. }
  813. si8 CBattleCallback::battleHasDistancePenalty( const CStack * stack, THex destHex )
  814. {
  815. return gs->curB->hasDistancePenalty(stack, destHex);
  816. }
  817. si8 CBattleCallback::battleHasWallPenalty( const CStack * stack, THex destHex )
  818. {
  819. return gs->curB->hasWallPenalty(stack, destHex);
  820. }
  821. si8 CBattleCallback::battleCanTeleportTo(const CStack * stack, THex destHex, int telportLevel)
  822. {
  823. return gs->curB->canTeleportTo(stack, destHex, telportLevel);
  824. }
  825. int CCallback::getPlayerStatus(int player) const
  826. {
  827. const PlayerState *ps = gs->getPlayer(player, false);
  828. if(!ps)
  829. return -1;
  830. return ps->status;
  831. }
  832. std::string CCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  833. {
  834. return "GOSSIP TEST";
  835. }
  836. std::vector < const CGObjectInstance * > CCallback::getMyObjects() const
  837. {
  838. std::vector < const CGObjectInstance * > ret;
  839. for (int g=0; g<gs->map->objects.size(); ++g)
  840. {
  841. if (gs->map->objects[g]->tempOwner == LOCPLINT->playerID)
  842. {
  843. ret.push_back(gs->map->objects[g]);
  844. }
  845. }
  846. return ret;
  847. }
  848. std::vector < const CGDwelling * > CCallback::getMyDwellings() const
  849. {
  850. std::vector < const CGDwelling * > ret;
  851. BOOST_FOREACH(CGDwelling * dw, gs->getPlayer(player)->dwellings)
  852. {
  853. ret.push_back(dw);
  854. }
  855. return ret;
  856. }
  857. int CCallback::getPlayerRelations( ui8 color1, ui8 color2 ) const
  858. {
  859. return gs->getPlayerRelations(color1, color2);
  860. }
  861. InfoAboutTown::InfoAboutTown()
  862. {
  863. tType = NULL;
  864. details = NULL;
  865. fortLevel = 0;
  866. owner = -1;
  867. }
  868. InfoAboutTown::~InfoAboutTown()
  869. {
  870. delete details;
  871. }
  872. void InfoAboutTown::initFromTown( const CGTownInstance *t, bool detailed )
  873. {
  874. obj = t;
  875. army = ArmyDescriptor(t, detailed);
  876. built = t->builded;
  877. fortLevel = t->fortLevel();
  878. name = t->name;
  879. tType = t->town;
  880. owner = t->tempOwner;
  881. if(detailed)
  882. {
  883. //include details about hero
  884. details = new Details;
  885. details->goldIncome = t->dailyIncome();
  886. details->customRes = vstd::contains(t->builtBuildings, 15);
  887. details->hallLevel = t->hallLevel();
  888. details->garrisonedHero = t->garrisonHero;
  889. }
  890. //TODO: adjust undetailed info about army to our count of thieves guilds
  891. }
  892. void InfoAboutTown::initFromGarrison(const CGGarrison *garr, bool detailed)
  893. {
  894. obj = garr;
  895. fortLevel = 0;
  896. army = ArmyDescriptor(garr, detailed);
  897. name = CGI->generaltexth->names[33]; // "Garrison"
  898. owner = garr->tempOwner;
  899. built = false;
  900. tType = NULL;
  901. // Show detailed info only to owning player.
  902. if(detailed)
  903. {
  904. details = new InfoAboutTown::Details;
  905. details->customRes = false;
  906. details->garrisonedHero = false;
  907. details->goldIncome = -1;
  908. details->hallLevel = -1;
  909. }
  910. }
  911. bool CBattleCallback::hasAccess( int playerId ) const
  912. {
  913. return playerId == player || player < 0;
  914. }
  915. CBattleCallback::CBattleCallback(CGameState *GS, int Player, CClient *C )
  916. {
  917. gs = GS;
  918. player = Player;
  919. cl = C;
  920. }
  921. std::vector<int> CBattleCallback::battleGetDistances(const CStack * stack, THex hex /*= THex::INVALID*/, THex * predecessors /*= NULL*/)
  922. {
  923. if(!hex.isValid())
  924. hex = stack->position;
  925. std::vector<int> ret;
  926. bool ac[BFIELD_SIZE];
  927. THex pr[BFIELD_SIZE];
  928. int dist[BFIELD_SIZE];
  929. gs->curB->makeBFS(stack->position, ac, pr, dist, stack->doubleWide(), stack->attackerOwned, stack->hasBonusOfType(Bonus::FLYING), false);
  930. for(int i=0; i<BFIELD_SIZE; ++i)
  931. {
  932. if(pr[i] == -1)
  933. ret.push_back(-1);
  934. else
  935. ret.push_back(dist[i]);
  936. }
  937. if(predecessors)
  938. {
  939. memcpy(predecessors, pr, BFIELD_SIZE * sizeof(THex));
  940. }
  941. return ret;
  942. }