CCallback.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #include "StdInc.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/CMap.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 "lib/CSpellHandler.h"
  19. #include "lib/CArtHandler.h"
  20. #include "lib/GameConstants.h"
  21. #ifdef min
  22. #undef min
  23. #endif
  24. #ifdef max
  25. #undef max
  26. #endif
  27. #include "lib/UnlockGuard.h"
  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. int CCallback::selectionMade(int selection, int queryID)
  54. {
  55. if(queryID == -1)
  56. {
  57. tlog1 << "Cannot answer the query -1!\n";
  58. return false;
  59. }
  60. QueryReply pack(queryID,selection);
  61. pack.player = player;
  62. return sendRequest(&pack);
  63. }
  64. void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount, si32 level/*=-1*/)
  65. {
  66. if(player!=obj->tempOwner && obj->ID != Obj::WAR_MACHINE_FACTORY)
  67. return;
  68. RecruitCreatures pack(obj->id,ID,amount,level);
  69. sendRequest(&pack);
  70. }
  71. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  72. {
  73. if(((player>=0) && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
  74. return false;
  75. DisbandCreature pack(stackPos,obj->id);
  76. sendRequest(&pack);
  77. return true;
  78. }
  79. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  80. {
  81. UpgradeCreature pack(stackPos,obj->id,newID);
  82. sendRequest(&pack);
  83. return false;
  84. }
  85. void CCallback::endTurn()
  86. {
  87. tlog5 << "Player " << (unsigned)player << " ended his turn." << std::endl;
  88. EndTurn pack;
  89. sendRequest(&pack); //report that we ended turn
  90. }
  91. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  92. {
  93. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  94. sendRequest(&pack);
  95. return 0;
  96. }
  97. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  98. {
  99. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  100. sendRequest(&pack);
  101. return 0;
  102. }
  103. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)
  104. {
  105. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  106. sendRequest(&pack);
  107. return 0;
  108. }
  109. bool CCallback::dismissHero(const CGHeroInstance *hero)
  110. {
  111. if(player!=hero->tempOwner) return false;
  112. DismissHero pack(hero->id);
  113. sendRequest(&pack);
  114. return true;
  115. }
  116. // int CCallback::getMySerial() const
  117. // {
  118. // boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  119. // return gs->players[player].serial;
  120. // }
  121. bool CCallback::swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)
  122. {
  123. ExchangeArtifacts ea;
  124. ea.src = l1;
  125. ea.dst = l2;
  126. sendRequest(&ea);
  127. return true;
  128. }
  129. /**
  130. * Assembles or disassembles a combination artifact.
  131. * @param hero Hero holding the artifact(s).
  132. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  133. * @param assemble True for assembly operation, false for disassembly.
  134. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  135. * artifact to assemble to. Otherwise it's not used.
  136. */
  137. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)
  138. {
  139. if (player != hero->tempOwner)
  140. return false;
  141. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  142. sendRequest(&aa);
  143. return true;
  144. }
  145. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  146. {
  147. if(town->tempOwner!=player)
  148. return false;
  149. if(!canBuildStructure(town, buildingID))
  150. return false;
  151. BuildStructure pack(town->id,buildingID);
  152. sendRequest(&pack);
  153. return true;
  154. }
  155. int CBattleCallback::battleMakeAction(BattleAction* action)
  156. {
  157. assert(action->actionType == BattleAction::HERO_SPELL);
  158. MakeCustomAction mca(*action);
  159. sendRequest(&mca);
  160. return 0;
  161. }
  162. int CBattleCallback::sendRequest(const CPack *request)
  163. {
  164. int requestID = cl->sendRequest(request, player);
  165. if(waitTillRealize)
  166. {
  167. tlog5 << boost::format("We'll wait till request %d is answered.\n") % requestID;
  168. auto gsUnlocker = vstd::makeUnlockSharedGuardIf(getGsMutex(), unlockGsWhenWaiting);
  169. cl->waitingRequest.waitWhileContains(requestID);
  170. }
  171. return requestID;
  172. }
  173. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  174. {
  175. if(town->tempOwner != player) return;
  176. GarrisonHeroSwap pack(town->id);
  177. sendRequest(&pack);
  178. }
  179. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  180. {
  181. if(hero->tempOwner != player) return;
  182. BuyArtifact pack(hero->id,aid);
  183. sendRequest(&pack);
  184. }
  185. void CCallback::trade(const CGObjectInstance *market, int mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = NULL*/)
  186. {
  187. TradeOnMarketplace pack;
  188. pack.market = market;
  189. pack.hero = hero;
  190. pack.mode = mode;
  191. pack.r1 = id1;
  192. pack.r2 = id2;
  193. pack.val = val1;
  194. sendRequest(&pack);
  195. }
  196. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  197. {
  198. const_cast<CGHeroInstance*>(hero)-> formation = tight;
  199. SetFormation pack(hero->id,tight);
  200. sendRequest(&pack);
  201. }
  202. void CCallback::setSelection(const CArmedInstance * obj)
  203. {
  204. SetSelection ss;
  205. ss.player = player;
  206. ss.id = obj->id;
  207. sendRequest(&(CPackForClient&)ss);
  208. if(obj->ID == Obj::HERO)
  209. {
  210. if(cl->pathInfo->hero != obj) //calculate new paths only if we selected a different hero
  211. cl->calculatePaths(static_cast<const CGHeroInstance *>(obj));
  212. //nasty workaround. TODO: nice workaround
  213. cl->gs->getPlayer(player)->currentSelection = obj->id;
  214. }
  215. }
  216. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  217. {
  218. ui8 i=0;
  219. for(; i<gs->players[player].availableHeroes.size(); i++)
  220. {
  221. if(gs->players[player].availableHeroes[i] == hero)
  222. {
  223. HireHero pack(i,townOrTavern->id);
  224. pack.player = player;
  225. sendRequest(&pack);
  226. return;
  227. }
  228. }
  229. }
  230. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  231. {
  232. return gs->getPath(src,dest,hero, ret);
  233. }
  234. void CCallback::save( const std::string &fname )
  235. {
  236. cl->save(fname);
  237. }
  238. void CCallback::sendMessage(const std::string &mess)
  239. {
  240. PlayerMessage pm(player, mess);
  241. sendRequest(&(CPackForClient&)pm);
  242. }
  243. void CCallback::buildBoat( const IShipyard *obj )
  244. {
  245. BuildBoat bb;
  246. bb.objid = obj->o->id;
  247. sendRequest(&bb);
  248. }
  249. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  250. :CBattleCallback(GS, Player, C)
  251. {
  252. waitTillRealize = false;
  253. unlockGsWhenWaiting = false;
  254. }
  255. const CGPathNode * CCallback::getPathInfo( int3 tile )
  256. {
  257. if (!gs->map->isInTheMap(tile))
  258. return nullptr;
  259. validatePaths();
  260. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  261. }
  262. bool CCallback::getPath2( int3 dest, CGPath &ret )
  263. {
  264. if (!gs->map->isInTheMap(dest))
  265. return false;
  266. validatePaths();
  267. boost::unique_lock<boost::mutex> pathLock(cl->pathMx);
  268. return cl->pathInfo->getPath(dest, ret);
  269. }
  270. void CCallback::recalculatePaths()
  271. {
  272. cl->calculatePaths(cl->IGameCallback::getSelectedHero(player));
  273. }
  274. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  275. {
  276. gs->calculatePaths(hero, out, src, movement);
  277. }
  278. void CCallback::dig( const CGObjectInstance *hero )
  279. {
  280. DigWithHero dwh;
  281. dwh.id = hero->id;
  282. sendRequest(&dwh);
  283. }
  284. void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
  285. {
  286. CastAdvSpell cas;
  287. cas.hid = hero->id;
  288. cas.sid = spellID;
  289. cas.pos = pos;
  290. sendRequest(&cas);
  291. }
  292. void CCallback::unregisterMyInterface()
  293. {
  294. assert(player >= 0); //works only for player callback
  295. cl->playerint.erase(player);
  296. cl->battleints.erase(player);
  297. //TODO? should callback be disabled as well?
  298. }
  299. void CCallback::validatePaths()
  300. {
  301. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  302. if(h && ( cl->pathInfo->hero != h //wrong hero
  303. || cl->pathInfo->hpos != h->getPosition(false) //wrong hero positoin
  304. || !cl->pathInfo->isValid)) //paths invalidated by game event
  305. {
  306. recalculatePaths();
  307. }
  308. }
  309. int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  310. {
  311. if(s1->getCreature(p1) == s2->getCreature(p2))
  312. return mergeStacks(s1, s2, p1, p2);
  313. else
  314. return swapCreatures(s1, s2, p1, p2);
  315. }
  316. CBattleCallback::CBattleCallback(CGameState *GS, int Player, CClient *C )
  317. {
  318. gs = GS;
  319. player = Player;
  320. cl = C;
  321. }
  322. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  323. {
  324. assert(cl->gs->curB->tacticDistance);
  325. MakeAction ma;
  326. ma.ba = *action;
  327. sendRequest(&ma);
  328. return true;
  329. }