CCallback.cpp 28 KB

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