CCallback.cpp 27 KB

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