CCallback.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  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. TStacks CBattleCallback::battleGetStacks(EStackOwnership whose /*= MINE_AND_ENEMY*/, bool onlyAlive /*= true*/)
  480. {
  481. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  482. TStacks 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. {
  490. bool ownerMatches = whose == MINE_AND_ENEMY || whose == ONLY_MINE && s->owner == player;
  491. bool alivenessMatches = s->alive() || !onlyAlive;
  492. if(ownerMatches && alivenessMatches)
  493. ret.push_back(s);
  494. }
  495. return ret;
  496. }
  497. void CBattleCallback::getStackQueue( std::vector<const CStack *> &out, int howMany )
  498. {
  499. if(!gs->curB)
  500. {
  501. tlog2 << "battleGetStackQueue called when there is not battle!" << std::endl;
  502. return;
  503. }
  504. gs->curB->getStackQueue(out, howMany);
  505. }
  506. std::vector<THex> CBattleCallback::battleGetAvailableHexes(const CStack * stack, bool addOccupiable)
  507. {
  508. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  509. if(!gs->curB)
  510. {
  511. tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
  512. return std::vector<THex>();
  513. }
  514. return gs->curB->getAccessibility(stack, addOccupiable);
  515. //return gs->battleGetRange(ID);
  516. }
  517. bool CBattleCallback::battleCanShoot(const CStack * stack, THex dest)
  518. {
  519. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  520. if(!gs->curB) return false;
  521. return gs->curB->battleCanShoot(stack, dest);
  522. }
  523. bool CBattleCallback::battleCanCastSpell()
  524. {
  525. if(!gs->curB) //there is no battle
  526. return false;
  527. if(gs->curB->sides[0] == player)
  528. return gs->curB->castSpells[0] == 0 && gs->curB->heroes[0] && gs->curB->heroes[0]->getArt(17);
  529. else
  530. return gs->curB->castSpells[1] == 0 && gs->curB->heroes[1] && gs->curB->heroes[1]->getArt(17);
  531. }
  532. bool CBattleCallback::battleCanFlee()
  533. {
  534. return gs->curB->battleCanFlee(player);
  535. }
  536. const CGTownInstance *CBattleCallback::battleGetDefendedTown()
  537. {
  538. if(!gs->curB || gs->curB->town == NULL)
  539. return NULL;
  540. return gs->curB->town;
  541. }
  542. ui8 CBattleCallback::battleGetWallState(int partOfWall)
  543. {
  544. if(!gs->curB || gs->curB->siege == 0)
  545. {
  546. return 0;
  547. }
  548. return gs->curB->si.wallState[partOfWall];
  549. }
  550. int CBattleCallback::battleGetWallUnderHex(THex hex)
  551. {
  552. if(!gs->curB || gs->curB->siege == 0)
  553. {
  554. return -1;
  555. }
  556. return gs->curB->hexToWallPart(hex);
  557. }
  558. TDmgRange CBattleCallback::battleEstimateDamage(const CStack * attacker, const CStack * defender, TDmgRange * retaliationDmg)
  559. {
  560. if(!gs->curB)
  561. return std::make_pair(0, 0);
  562. const CGHeroInstance * attackerHero, * defenderHero;
  563. bool shooting = battleCanShoot(attacker, defender->position);
  564. if(gs->curB->sides[0] == player)
  565. {
  566. attackerHero = gs->curB->heroes[0];
  567. defenderHero = gs->curB->heroes[1];
  568. }
  569. else
  570. {
  571. attackerHero = gs->curB->heroes[1];
  572. defenderHero = gs->curB->heroes[0];
  573. }
  574. TDmgRange ret = gs->curB->calculateDmgRange(attacker, defender, attackerHero, defenderHero, shooting, 0, false);
  575. if(retaliationDmg)
  576. {
  577. if(shooting)
  578. {
  579. retaliationDmg->first = retaliationDmg->second = 0;
  580. }
  581. else
  582. {
  583. ui32 TDmgRange::* pairElems[] = {&TDmgRange::first, &TDmgRange::second};
  584. for (int i=0; i<2; ++i)
  585. {
  586. BattleStackAttacked bsa;
  587. bsa.damageAmount = ret.*pairElems[i];
  588. retaliationDmg->*pairElems[!i] = gs->curB->calculateDmgRange(defender, attacker, bsa.newAmount, attacker->count, attackerHero, defenderHero, false, false, false).*pairElems[!i];
  589. }
  590. }
  591. }
  592. return ret;
  593. }
  594. ui8 CBattleCallback::battleGetSiegeLevel()
  595. {
  596. if(!gs->curB)
  597. return 0;
  598. return gs->curB->siege;
  599. }
  600. const CGHeroInstance * CBattleCallback::battleGetFightingHero(ui8 side) const
  601. {
  602. if(!gs->curB)
  603. return 0;
  604. return gs->curB->heroes[side];
  605. }
  606. template <typename T>
  607. void CBattleCallback::sendRequest(const T* request)
  608. {
  609. //TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
  610. if(waitTillRealize)
  611. cl->waitingRequest.set(true);
  612. *cl->serv << request;
  613. if(waitTillRealize)
  614. cl->waitingRequest.waitWhileTrue();
  615. }
  616. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  617. {
  618. if(town->tempOwner != player) return;
  619. GarrisonHeroSwap pack(town->id);
  620. sendRequest(&pack);
  621. }
  622. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  623. {
  624. if(hero->tempOwner != player) return;
  625. BuyArtifact pack(hero->id,aid);
  626. sendRequest(&pack);
  627. }
  628. std::vector < const CGObjectInstance * > CCallback::getBlockingObjs( int3 pos ) const
  629. {
  630. std::vector<const CGObjectInstance *> ret;
  631. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  632. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  633. return ret;
  634. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  635. ret.push_back(obj);
  636. return ret;
  637. }
  638. std::vector < const CGObjectInstance * > CCallback::getVisitableObjs( int3 pos ) const
  639. {
  640. std::vector<const CGObjectInstance *> ret;
  641. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  642. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  643. return ret;
  644. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].visitableObjects)
  645. ret.push_back(obj);
  646. return ret;
  647. }
  648. std::vector < const CGObjectInstance * > CCallback::getFlaggableObjects(int3 pos) const
  649. {
  650. if(!isVisible(pos))
  651. return std::vector < const CGObjectInstance * >();
  652. std::vector < const CGObjectInstance * > ret;
  653. const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  654. for(size_t b=0; b<objs.size(); ++b)
  655. {
  656. 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))
  657. ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  658. }
  659. return ret;
  660. }
  661. int3 CCallback::getMapSize() const
  662. {
  663. return CGI->mh->sizes;
  664. }
  665. void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
  666. {
  667. TradeOnMarketplace pack;
  668. pack.market = market;
  669. pack.hero = hero;
  670. pack.mode = mode;
  671. pack.r1 = id1;
  672. pack.r2 = id2;
  673. pack.val = val1;
  674. sendRequest(&pack);
  675. }
  676. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  677. {
  678. const_cast<CGHeroInstance*>(hero)-> formation = tight;
  679. SetFormation pack(hero->id,tight);
  680. sendRequest(&pack);
  681. }
  682. void CCallback::setSelection(const CArmedInstance * obj)
  683. {
  684. SetSelection ss;
  685. ss.player = player;
  686. ss.id = obj->id;
  687. sendRequest(&ss);
  688. if(obj->ID == HEROI_TYPE)
  689. {
  690. cl->gs->calculatePaths(static_cast<const CGHeroInstance *>(obj), *cl->pathInfo);
  691. //nasty workaround. TODO: nice workaround
  692. cl->gs->getPlayer(player)->currentSelection = obj->id;
  693. }
  694. }
  695. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  696. {
  697. ui8 i=0;
  698. for(; i<gs->players[player].availableHeroes.size(); i++)
  699. {
  700. if(gs->players[player].availableHeroes[i] == hero)
  701. {
  702. HireHero pack(i,townOrTavern->id);
  703. pack.player = player;
  704. sendRequest(&pack);
  705. return;
  706. }
  707. }
  708. }
  709. std::vector<const CGHeroInstance *> CCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  710. {
  711. std::vector<const CGHeroInstance *> ret(gs->players[player].availableHeroes.size());
  712. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  713. return ret;
  714. }
  715. const TerrainTile * CCallback::getTileInfo( int3 tile ) const
  716. {
  717. if(!gs->map->isInTheMap(tile))
  718. {
  719. tlog1 << tile << "is outside the map! (call to getTileInfo)\n";
  720. return NULL;
  721. }
  722. if(!isVisible(tile, player)) return NULL;
  723. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  724. return &gs->map->getTile(tile);
  725. }
  726. int CCallback::canBuildStructure( const CGTownInstance *t, int ID )
  727. {
  728. return gs->canBuildStructure(t,ID);
  729. }
  730. std::set<int> CCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
  731. {
  732. return gs->getBuildingRequiments(t,ID);
  733. }
  734. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  735. {
  736. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  737. return gs->getPath(src,dest,hero, ret);
  738. }
  739. void CCallback::save( const std::string &fname )
  740. {
  741. cl->save(fname);
  742. }
  743. void CCallback::sendMessage(const std::string &mess)
  744. {
  745. PlayerMessage pm(player, mess);
  746. sendRequest(&pm);
  747. }
  748. void CCallback::buildBoat( const IShipyard *obj )
  749. {
  750. BuildBoat bb;
  751. bb.objid = obj->o->id;
  752. sendRequest(&bb);
  753. }
  754. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  755. :CBattleCallback(GS, Player, C)
  756. {
  757. waitTillRealize = false;
  758. }
  759. const CMapHeader * CCallback::getMapHeader() const
  760. {
  761. return gs->map;
  762. }
  763. const CGPathNode * CCallback::getPathInfo( int3 tile )
  764. {
  765. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  766. }
  767. bool CCallback::getPath2( int3 dest, CGPath &ret )
  768. {
  769. if (!gs->map->isInTheMap(dest))
  770. return false;
  771. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  772. assert(cl->pathInfo->hero == h);
  773. if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
  774. {
  775. recalculatePaths();
  776. }
  777. return cl->pathInfo->getPath(dest, ret);
  778. }
  779. void CCallback::recalculatePaths()
  780. {
  781. gs->calculatePaths(cl->IGameCallback::getSelectedHero(player), *cl->pathInfo);
  782. }
  783. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  784. {
  785. gs->calculatePaths(hero, out, src, movement);
  786. }
  787. int3 CCallback::getGrailPos( float &outKnownRatio )
  788. {
  789. if (CGObelisk::obeliskCount == 0)
  790. {
  791. outKnownRatio = 0.0f;
  792. }
  793. else
  794. {
  795. outKnownRatio = (float)CGObelisk::visited[gs->getPlayerTeam(player)->id] / CGObelisk::obeliskCount;
  796. }
  797. return gs->map->grailPos;
  798. }
  799. void CCallback::dig( const CGObjectInstance *hero )
  800. {
  801. DigWithHero dwh;
  802. dwh.id = hero->id;
  803. sendRequest(&dwh);
  804. }
  805. void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
  806. {
  807. CastAdvSpell cas;
  808. cas.hid = hero->id;
  809. cas.sid = spellID;
  810. cas.pos = pos;
  811. sendRequest(&cas);
  812. }
  813. bool CCallback::hasAccess(int playerId) const
  814. {
  815. return gs->getPlayerRelations( playerId, player ) || player < 0;
  816. }
  817. si8 CBattleCallback::battleHasDistancePenalty( const CStack * stack, THex destHex )
  818. {
  819. return gs->curB->hasDistancePenalty(stack, destHex);
  820. }
  821. si8 CBattleCallback::battleHasWallPenalty( const CStack * stack, THex destHex )
  822. {
  823. return gs->curB->hasWallPenalty(stack, destHex);
  824. }
  825. si8 CBattleCallback::battleCanTeleportTo(const CStack * stack, THex destHex, int telportLevel)
  826. {
  827. return gs->curB->canTeleportTo(stack, destHex, telportLevel);
  828. }
  829. int CCallback::getPlayerStatus(int player) const
  830. {
  831. const PlayerState *ps = gs->getPlayer(player, false);
  832. if(!ps)
  833. return -1;
  834. return ps->status;
  835. }
  836. std::string CCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  837. {
  838. return "GOSSIP TEST";
  839. }
  840. std::vector < const CGObjectInstance * > CCallback::getMyObjects() const
  841. {
  842. std::vector < const CGObjectInstance * > ret;
  843. for (int g=0; g<gs->map->objects.size(); ++g)
  844. {
  845. if (gs->map->objects[g]->tempOwner == LOCPLINT->playerID)
  846. {
  847. ret.push_back(gs->map->objects[g]);
  848. }
  849. }
  850. return ret;
  851. }
  852. std::vector < const CGDwelling * > CCallback::getMyDwellings() const
  853. {
  854. std::vector < const CGDwelling * > ret;
  855. BOOST_FOREACH(CGDwelling * dw, gs->getPlayer(player)->dwellings)
  856. {
  857. ret.push_back(dw);
  858. }
  859. return ret;
  860. }
  861. int CCallback::getPlayerRelations( ui8 color1, ui8 color2 ) const
  862. {
  863. return gs->getPlayerRelations(color1, color2);
  864. }
  865. InfoAboutTown::InfoAboutTown()
  866. {
  867. tType = NULL;
  868. details = NULL;
  869. fortLevel = 0;
  870. owner = -1;
  871. }
  872. InfoAboutTown::~InfoAboutTown()
  873. {
  874. delete details;
  875. }
  876. void InfoAboutTown::initFromTown( const CGTownInstance *t, bool detailed )
  877. {
  878. obj = t;
  879. army = ArmyDescriptor(t, detailed);
  880. built = t->builded;
  881. fortLevel = t->fortLevel();
  882. name = t->name;
  883. tType = t->town;
  884. owner = t->tempOwner;
  885. if(detailed)
  886. {
  887. //include details about hero
  888. details = new Details;
  889. details->goldIncome = t->dailyIncome();
  890. details->customRes = vstd::contains(t->builtBuildings, 15);
  891. details->hallLevel = t->hallLevel();
  892. details->garrisonedHero = t->garrisonHero;
  893. }
  894. //TODO: adjust undetailed info about army to our count of thieves guilds
  895. }
  896. void InfoAboutTown::initFromGarrison(const CGGarrison *garr, bool detailed)
  897. {
  898. obj = garr;
  899. fortLevel = 0;
  900. army = ArmyDescriptor(garr, detailed);
  901. name = CGI->generaltexth->names[33]; // "Garrison"
  902. owner = garr->tempOwner;
  903. built = false;
  904. tType = NULL;
  905. // Show detailed info only to owning player.
  906. if(detailed)
  907. {
  908. details = new InfoAboutTown::Details;
  909. details->customRes = false;
  910. details->garrisonedHero = false;
  911. details->goldIncome = -1;
  912. details->hallLevel = -1;
  913. }
  914. }
  915. bool CBattleCallback::hasAccess( int playerId ) const
  916. {
  917. return playerId == player || player < 0;
  918. }
  919. CBattleCallback::CBattleCallback(CGameState *GS, int Player, CClient *C )
  920. {
  921. gs = GS;
  922. player = Player;
  923. cl = C;
  924. }
  925. std::vector<int> CBattleCallback::battleGetDistances(const CStack * stack, THex hex /*= THex::INVALID*/, THex * predecessors /*= NULL*/)
  926. {
  927. if(!hex.isValid())
  928. hex = stack->position;
  929. std::vector<int> ret;
  930. bool ac[BFIELD_SIZE];
  931. THex pr[BFIELD_SIZE];
  932. int dist[BFIELD_SIZE];
  933. gs->curB->makeBFS(stack->position, ac, pr, dist, stack->doubleWide(), stack->attackerOwned, stack->hasBonusOfType(Bonus::FLYING), false);
  934. for(int i=0; i<BFIELD_SIZE; ++i)
  935. {
  936. if(pr[i] == -1)
  937. ret.push_back(-1);
  938. else
  939. ret.push_back(dist[i]);
  940. }
  941. if(predecessors)
  942. {
  943. memcpy(predecessors, pr, BFIELD_SIZE * sizeof(THex));
  944. }
  945. return ret;
  946. }
  947. CBattleCallback::ESpellCastProblem CBattleCallback::battleCanCastThisSpell( const CSpell * spell )
  948. {
  949. if(!battleCanCastSpell())
  950. return GENERAL_CASTING_PROBLEM;
  951. int spellIDs[] = {66, 67, 68, 69}; //IDs of summon elemental spells (fire, earth, water, air)
  952. int creIDs[] = {114, 113, 115, 112}; //(fire, earth, water, air)
  953. int * idp = std::find(spellIDs, spellIDs + ARRAY_COUNT(spellIDs), spell->id);
  954. int arpos = idp - spellIDs;
  955. if(arpos < ARRAY_COUNT(spellIDs))
  956. {
  957. //check if there are summoned elementals of other type
  958. BOOST_FOREACH ( const CStack * st, gs->curB->stacks)
  959. {
  960. if (vstd::contains(st->state, SUMMONED) && st->getCreature()->idNumber != creIDs[arpos])
  961. {
  962. return ANOTHER_ELEMENTAL_SUMMONED;
  963. }
  964. }
  965. }
  966. return OK;
  967. }
  968. si8 CBattleCallback::battleGetTacticDist()
  969. {
  970. if (!gs->curB)
  971. {
  972. tlog1 << "battleGetTacticDist called when no battle!\n";
  973. return 0;
  974. }
  975. if (gs->curB->sides[gs->curB->tacticsSide] == player)
  976. {
  977. return gs->curB->tacticDistance;
  978. }
  979. return 0;
  980. }
  981. ui8 CBattleCallback::battleGetMySide()
  982. {
  983. if (!gs->curB)
  984. {
  985. tlog1 << "battleGetMySide called when no battle!\n";
  986. return 0;
  987. }
  988. return gs->curB->sides[1] == player;
  989. }
  990. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  991. {
  992. MakeAction ma;
  993. ma.ba = *action;
  994. sendRequest(&ma);
  995. return true;
  996. }