CCallback.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/mapping/CMap.h"
  10. #include "lib/CBuildingHandler.h"
  11. #include "lib/mapObjects/CObjectClassesHandler.h"
  12. #include "lib/CGeneralTextHandler.h"
  13. #include "lib/CHeroHandler.h"
  14. #include "lib/NetPacks.h"
  15. #include "client/mapHandler.h"
  16. #include "lib/spells/CSpellHandler.h"
  17. #include "lib/CArtHandler.h"
  18. #include "lib/GameConstants.h"
  19. #include "lib/CPlayerState.h"
  20. #include "lib/UnlockGuard.h"
  21. /*
  22. * CCallback.cpp, part of VCMI engine
  23. *
  24. * Authors: listed in file AUTHORS in main folder
  25. *
  26. * License: GNU General Public License v2.0 or later
  27. * Full text of license available in license.txt file, in main folder
  28. *
  29. */
  30. template <ui16 N> bool isType(CPack *pack)
  31. {
  32. return pack->getType() == N;
  33. }
  34. bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
  35. {
  36. CastleTeleportHero pack(who->id, where->id, 1);
  37. sendRequest(&pack);
  38. return true;
  39. }
  40. bool CCallback::moveHero(const CGHeroInstance *h, int3 dst, bool transit)
  41. {
  42. MoveHero pack(dst,h->id,transit);
  43. sendRequest(&pack);
  44. return true;
  45. }
  46. int CCallback::selectionMade(int selection, QueryID queryID)
  47. {
  48. ASSERT_IF_CALLED_WITH_PLAYER
  49. if(queryID == QueryID(-1))
  50. {
  51. logGlobal->errorStream() << "Cannot answer the query -1!";
  52. return false;
  53. }
  54. QueryReply pack(queryID,selection);
  55. pack.player = *player;
  56. return sendRequest(&pack);
  57. }
  58. void CCallback::recruitCreatures(const CGDwelling *obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level/*=-1*/)
  59. {
  60. // TODO exception for neutral dwellings shouldn't be hardcoded
  61. if(player != obj->tempOwner && obj->ID != Obj::WAR_MACHINE_FACTORY && obj->ID != Obj::REFUGEE_CAMP)
  62. return;
  63. RecruitCreatures pack(obj->id, dst->id, ID, amount, level);
  64. sendRequest(&pack);
  65. }
  66. bool CCallback::dismissCreature(const CArmedInstance *obj, SlotID stackPos)
  67. {
  68. if((player && 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, SlotID stackPos, CreatureID newID)
  75. {
  76. UpgradeCreature pack(stackPos,obj->id,newID);
  77. sendRequest(&pack);
  78. return false;
  79. }
  80. void CCallback::endTurn()
  81. {
  82. logGlobal->traceStream() << "Player " << *player << " ended his turn.";
  83. EndTurn pack;
  84. sendRequest(&pack); //report that we ended turn
  85. }
  86. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  87. {
  88. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  89. sendRequest(&pack);
  90. return 0;
  91. }
  92. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  93. {
  94. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  95. sendRequest(&pack);
  96. return 0;
  97. }
  98. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)
  99. {
  100. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  101. sendRequest(&pack);
  102. return 0;
  103. }
  104. bool CCallback::dismissHero(const CGHeroInstance *hero)
  105. {
  106. if(player!=hero->tempOwner) return false;
  107. DismissHero pack(hero->id);
  108. sendRequest(&pack);
  109. return true;
  110. }
  111. // int CCallback::getMySerial() const
  112. // {
  113. // boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  114. // return gs->players[player].serial;
  115. // }
  116. bool CCallback::swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)
  117. {
  118. ExchangeArtifacts ea;
  119. ea.src = l1;
  120. ea.dst = l2;
  121. sendRequest(&ea);
  122. return true;
  123. }
  124. /**
  125. * Assembles or disassembles a combination artifact.
  126. * @param hero Hero holding the artifact(s).
  127. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  128. * @param assemble True for assembly operation, false for disassembly.
  129. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  130. * artifact to assemble to. Otherwise it's not used.
  131. */
  132. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)
  133. {
  134. if (player != hero->tempOwner)
  135. return false;
  136. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  137. sendRequest(&aa);
  138. return true;
  139. }
  140. bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
  141. {
  142. if(town->tempOwner!=player)
  143. return false;
  144. if(!canBuildStructure(town, buildingID))
  145. return false;
  146. BuildStructure pack(town->id,buildingID);
  147. sendRequest(&pack);
  148. return true;
  149. }
  150. int CBattleCallback::battleMakeAction(BattleAction* action)
  151. {
  152. assert(action->actionType == Battle::HERO_SPELL);
  153. MakeCustomAction mca(*action);
  154. sendRequest(&mca);
  155. return 0;
  156. }
  157. int CBattleCallback::sendRequest(const CPack *request)
  158. {
  159. int requestID = cl->sendRequest(request, *player);
  160. if(waitTillRealize)
  161. {
  162. logGlobal->traceStream() << boost::format("We'll wait till request %d is answered.\n") % requestID;
  163. auto gsUnlocker = vstd::makeUnlockSharedGuardIf(getGsMutex(), unlockGsWhenWaiting);
  164. cl->waitingRequest.waitWhileContains(requestID);
  165. }
  166. boost::this_thread::interruption_point();
  167. return requestID;
  168. }
  169. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  170. {
  171. if(town->tempOwner == *player
  172. || (town->garrisonHero && town->garrisonHero->tempOwner == *player ))
  173. {
  174. GarrisonHeroSwap pack(town->id);
  175. sendRequest(&pack);
  176. }
  177. }
  178. void CCallback::buyArtifact(const CGHeroInstance *hero, ArtifactID aid)
  179. {
  180. if(hero->tempOwner != player) return;
  181. BuyArtifact pack(hero->id,aid);
  182. sendRequest(&pack);
  183. }
  184. void CCallback::trade(const CGObjectInstance *market, EMarketMode::EMarketMode mode, int id1, int id2, int val1, const CGHeroInstance *hero/* = nullptr*/)
  185. {
  186. TradeOnMarketplace pack;
  187. pack.market = market;
  188. pack.hero = hero;
  189. pack.mode = mode;
  190. pack.r1 = id1;
  191. pack.r2 = id2;
  192. pack.val = val1;
  193. sendRequest(&pack);
  194. }
  195. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  196. {
  197. SetFormation pack(hero->id,tight);
  198. sendRequest(&pack);
  199. }
  200. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  201. {
  202. assert(townOrTavern);
  203. assert(hero);
  204. ui8 i=0;
  205. for(; i<gs->players[*player].availableHeroes.size(); i++)
  206. {
  207. if(gs->players[*player].availableHeroes[i] == hero)
  208. {
  209. HireHero pack(i, townOrTavern->id);
  210. pack.player = *player;
  211. sendRequest(&pack);
  212. return;
  213. }
  214. }
  215. }
  216. void CCallback::save( const std::string &fname )
  217. {
  218. cl->save(fname);
  219. }
  220. void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
  221. {
  222. ASSERT_IF_CALLED_WITH_PLAYER
  223. PlayerMessage pm(*player, mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  224. sendRequest(&(CPackForClient&)pm);
  225. }
  226. void CCallback::buildBoat( const IShipyard *obj )
  227. {
  228. BuildBoat bb;
  229. bb.objid = obj->o->id;
  230. sendRequest(&bb);
  231. }
  232. CCallback::CCallback( CGameState * GS, boost::optional<PlayerColor> Player, CClient *C )
  233. :CBattleCallback(GS, Player, C)
  234. {
  235. waitTillRealize = false;
  236. unlockGsWhenWaiting = false;
  237. }
  238. CCallback::~CCallback()
  239. {
  240. //trivial, but required. Don`t remove.
  241. }
  242. bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
  243. {
  244. //TODO: merge with Pathfinder::canMoveBetween
  245. return gs->checkForVisitableDir(a, b) && gs->checkForVisitableDir(b, a);
  246. }
  247. const CPathsInfo * CCallback::getPathsInfo(const CGHeroInstance *h)
  248. {
  249. return cl->getPathsInfo(h);
  250. }
  251. int3 CCallback::getGuardingCreaturePosition(int3 tile)
  252. {
  253. if (!gs->map->isInTheMap(tile))
  254. return int3(-1,-1,-1);
  255. return gs->map->guardingCreaturePositions[tile.x][tile.y][tile.z];
  256. }
  257. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out)
  258. {
  259. gs->calculatePaths(hero, out);
  260. }
  261. void CCallback::dig( const CGObjectInstance *hero )
  262. {
  263. DigWithHero dwh;
  264. dwh.id = hero->id;
  265. sendRequest(&dwh);
  266. }
  267. void CCallback::castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos)
  268. {
  269. CastAdvSpell cas;
  270. cas.hid = hero->id;
  271. cas.sid = spellID;
  272. cas.pos = pos;
  273. sendRequest(&cas);
  274. }
  275. void CCallback::unregisterAllInterfaces()
  276. {
  277. for (auto& pi : cl->playerint)
  278. pi.second->finish();
  279. cl->playerint.clear();
  280. cl->battleints.clear();
  281. }
  282. int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  283. {
  284. if(s1->getCreature(p1) == s2->getCreature(p2))
  285. return mergeStacks(s1, s2, p1, p2);
  286. else
  287. return swapCreatures(s1, s2, p1, p2);
  288. }
  289. void CCallback::registerGameInterface(std::shared_ptr<IGameEventsReceiver> gameEvents)
  290. {
  291. cl->additionalPlayerInts[*player].push_back(gameEvents);
  292. }
  293. void CCallback::registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  294. {
  295. cl->additionalBattleInts[*player].push_back(battleEvents);
  296. }
  297. void CCallback::unregisterGameInterface(std::shared_ptr<IGameEventsReceiver> gameEvents)
  298. {
  299. cl->additionalPlayerInts[*player] -= gameEvents;
  300. }
  301. void CCallback::unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  302. {
  303. cl->additionalBattleInts[*player] -= battleEvents;
  304. }
  305. CBattleCallback::CBattleCallback(CGameState *GS, boost::optional<PlayerColor> Player, CClient *C )
  306. {
  307. gs = GS;
  308. player = Player;
  309. cl = C;
  310. }
  311. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  312. {
  313. assert(cl->gs->curB->tacticDistance);
  314. MakeAction ma;
  315. ma.ba = *action;
  316. sendRequest(&ma);
  317. return true;
  318. }