CCallback.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * CCallback.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CCallback.h"
  12. #include "lib/CCreatureHandler.h"
  13. #include "lib/gameState/CGameState.h"
  14. #include "client/CPlayerInterface.h"
  15. #include "client/Client.h"
  16. #include "lib/mapping/CMap.h"
  17. #include "lib/CBuildingHandler.h"
  18. #include "lib/CGeneralTextHandler.h"
  19. #include "lib/CHeroHandler.h"
  20. #include "lib/NetPacks.h"
  21. #include "lib/CArtHandler.h"
  22. #include "lib/GameConstants.h"
  23. #include "lib/CPlayerState.h"
  24. #include "lib/UnlockGuard.h"
  25. #include "lib/battle/BattleInfo.h"
  26. bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
  27. {
  28. CastleTeleportHero pack(who->id, where->id, 1);
  29. sendRequest(&pack);
  30. return true;
  31. }
  32. bool CCallback::moveHero(const CGHeroInstance *h, int3 dst, bool transit)
  33. {
  34. MoveHero pack(dst,h->id,transit);
  35. sendRequest(&pack);
  36. return true;
  37. }
  38. int CCallback::selectionMade(int selection, QueryID queryID)
  39. {
  40. return sendQueryReply(selection, queryID);
  41. }
  42. int CCallback::sendQueryReply(std::optional<int32_t> reply, QueryID queryID)
  43. {
  44. ASSERT_IF_CALLED_WITH_PLAYER
  45. if(queryID == QueryID(-1))
  46. {
  47. logGlobal->error("Cannot answer the query -1!");
  48. return -1;
  49. }
  50. QueryReply pack(queryID, reply);
  51. pack.player = *player;
  52. return sendRequest(&pack);
  53. }
  54. void CCallback::recruitCreatures(const CGDwelling * obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level)
  55. {
  56. // TODO exception for neutral dwellings shouldn't be hardcoded
  57. if(player != obj->tempOwner && obj->ID != Obj::WAR_MACHINE_FACTORY && obj->ID != Obj::REFUGEE_CAMP)
  58. return;
  59. RecruitCreatures pack(obj->id, dst->id, ID, amount, level);
  60. sendRequest(&pack);
  61. }
  62. bool CCallback::dismissCreature(const CArmedInstance *obj, SlotID stackPos)
  63. {
  64. if((player && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
  65. return false;
  66. DisbandCreature pack(stackPos,obj->id);
  67. sendRequest(&pack);
  68. return true;
  69. }
  70. bool CCallback::upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID)
  71. {
  72. UpgradeCreature pack(stackPos,obj->id,newID);
  73. sendRequest(&pack);
  74. return false;
  75. }
  76. void CCallback::endTurn()
  77. {
  78. logGlobal->trace("Player %d ended his turn.", player->getNum());
  79. EndTurn pack;
  80. sendRequest(&pack);
  81. }
  82. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  83. {
  84. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  85. sendRequest(&pack);
  86. return 0;
  87. }
  88. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  89. {
  90. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  91. sendRequest(&pack);
  92. return 0;
  93. }
  94. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)
  95. {
  96. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  97. sendRequest(&pack);
  98. return 0;
  99. }
  100. int CCallback::bulkMoveArmy(ObjectInstanceID srcArmy, ObjectInstanceID destArmy, SlotID srcSlot)
  101. {
  102. BulkMoveArmy pack(srcArmy, destArmy, srcSlot);
  103. sendRequest(&pack);
  104. return 0;
  105. }
  106. int CCallback::bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany)
  107. {
  108. BulkSplitStack pack(armyId, srcSlot, howMany);
  109. sendRequest(&pack);
  110. return 0;
  111. }
  112. int CCallback::bulkSmartSplitStack(ObjectInstanceID armyId, SlotID srcSlot)
  113. {
  114. BulkSmartSplitStack pack(armyId, srcSlot);
  115. sendRequest(&pack);
  116. return 0;
  117. }
  118. int CCallback::bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot)
  119. {
  120. BulkMergeStacks pack(armyId, srcSlot);
  121. sendRequest(&pack);
  122. return 0;
  123. }
  124. bool CCallback::dismissHero(const CGHeroInstance *hero)
  125. {
  126. if(player!=hero->tempOwner) return false;
  127. DismissHero pack(hero->id);
  128. sendRequest(&pack);
  129. return true;
  130. }
  131. bool CCallback::swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)
  132. {
  133. ExchangeArtifacts ea;
  134. ea.src = l1;
  135. ea.dst = l2;
  136. sendRequest(&ea);
  137. return true;
  138. }
  139. /**
  140. * Assembles or disassembles a combination artifact.
  141. * @param hero Hero holding the artifact(s).
  142. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  143. * @param assemble True for assembly operation, false for disassembly.
  144. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  145. * artifact to assemble to. Otherwise it's not used.
  146. */
  147. void CCallback::assembleArtifacts(const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)
  148. {
  149. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  150. sendRequest(&aa);
  151. }
  152. void CCallback::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap)
  153. {
  154. BulkExchangeArtifacts bma(srcHero, dstHero, swap);
  155. sendRequest(&bma);
  156. }
  157. void CCallback::eraseArtifactByClient(const ArtifactLocation & al)
  158. {
  159. EraseArtifactByClient ea(al);
  160. sendRequest(&ea);
  161. }
  162. bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
  163. {
  164. if(town->tempOwner!=player)
  165. return false;
  166. if(canBuildStructure(town, buildingID) != EBuildingState::ALLOWED)
  167. return false;
  168. BuildStructure pack(town->id,buildingID);
  169. sendRequest(&pack);
  170. return true;
  171. }
  172. void CBattleCallback::battleMakeSpellAction(const BattleID & battleID, const BattleAction & action)
  173. {
  174. assert(action.actionType == EActionType::HERO_SPELL);
  175. MakeAction mca(action);
  176. mca.battleID = battleID;
  177. sendRequest(&mca);
  178. }
  179. int CBattleCallback::sendRequest(const CPackForServer * request)
  180. {
  181. int requestID = cl->sendRequest(request, *getPlayerID());
  182. if(waitTillRealize)
  183. {
  184. logGlobal->trace("We'll wait till request %d is answered.\n", requestID);
  185. auto gsUnlocker = vstd::makeUnlockSharedGuardIf(CGameState::mutex, unlockGsWhenWaiting);
  186. CClient::waitingRequest.waitWhileContains(requestID);
  187. }
  188. boost::this_thread::interruption_point();
  189. return requestID;
  190. }
  191. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  192. {
  193. if(town->tempOwner == *player || (town->garrisonHero && town->garrisonHero->tempOwner == *player ))
  194. {
  195. GarrisonHeroSwap pack(town->id);
  196. sendRequest(&pack);
  197. }
  198. }
  199. void CCallback::buyArtifact(const CGHeroInstance *hero, ArtifactID aid)
  200. {
  201. if(hero->tempOwner != *player) return;
  202. BuyArtifact pack(hero->id,aid);
  203. sendRequest(&pack);
  204. }
  205. void CCallback::trade(const IMarket * market, EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero)
  206. {
  207. trade(market, mode, std::vector<ui32>(1, id1), std::vector<ui32>(1, id2), std::vector<ui32>(1, val1), hero);
  208. }
  209. void CCallback::trade(const IMarket * market, EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero)
  210. {
  211. TradeOnMarketplace pack;
  212. pack.marketId = dynamic_cast<const CGObjectInstance *>(market)->id;
  213. pack.heroId = hero ? hero->id : ObjectInstanceID();
  214. pack.mode = mode;
  215. pack.r1 = id1;
  216. pack.r2 = id2;
  217. pack.val = val1;
  218. sendRequest(&pack);
  219. }
  220. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  221. {
  222. SetFormation pack(hero->id,tight);
  223. sendRequest(&pack);
  224. }
  225. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  226. {
  227. assert(townOrTavern);
  228. assert(hero);
  229. HireHero pack(HeroTypeID(hero->subID), townOrTavern->id);
  230. pack.player = *player;
  231. sendRequest(&pack);
  232. }
  233. void CCallback::save( const std::string &fname )
  234. {
  235. cl->save(fname);
  236. }
  237. void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
  238. {
  239. ASSERT_IF_CALLED_WITH_PLAYER
  240. PlayerMessage pm(mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  241. if(player)
  242. pm.player = *player;
  243. sendRequest(&pm);
  244. }
  245. void CCallback::buildBoat( const IShipyard *obj )
  246. {
  247. BuildBoat bb;
  248. bb.objid = dynamic_cast<const CGObjectInstance*>(obj)->id;
  249. sendRequest(&bb);
  250. }
  251. CCallback::CCallback(CGameState * GS, std::optional<PlayerColor> Player, CClient * C)
  252. : CBattleCallback(Player, C)
  253. {
  254. gs = GS;
  255. waitTillRealize = false;
  256. unlockGsWhenWaiting = false;
  257. }
  258. CCallback::~CCallback() = default;
  259. bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
  260. {
  261. //bidirectional
  262. return gs->map->canMoveBetween(a, b);
  263. }
  264. std::shared_ptr<const CPathsInfo> CCallback::getPathsInfo(const CGHeroInstance * h)
  265. {
  266. return cl->getPathsInfo(h);
  267. }
  268. std::optional<PlayerColor> CCallback::getPlayerID() const
  269. {
  270. return CBattleCallback::getPlayerID();
  271. }
  272. int3 CCallback::getGuardingCreaturePosition(int3 tile)
  273. {
  274. if (!gs->map->isInTheMap(tile))
  275. return int3(-1,-1,-1);
  276. return gs->map->guardingCreaturePositions[tile.z][tile.x][tile.y];
  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, SpellID 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. int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  293. {
  294. if(s1->getCreature(p1) == s2->getCreature(p2))
  295. return mergeStacks(s1, s2, p1, p2);
  296. else
  297. return swapCreatures(s1, s2, p1, p2);
  298. }
  299. void CCallback::registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  300. {
  301. cl->additionalBattleInts[*player].push_back(battleEvents);
  302. }
  303. void CCallback::unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  304. {
  305. cl->additionalBattleInts[*player] -= battleEvents;
  306. }
  307. CBattleCallback::CBattleCallback(std::optional<PlayerColor> player, CClient * C):
  308. cl(C),
  309. player(player)
  310. {
  311. }
  312. void CBattleCallback::battleMakeUnitAction(const BattleID & battleID, const BattleAction & action)
  313. {
  314. assert(!cl->gs->getBattle(battleID)->tacticDistance);
  315. MakeAction ma;
  316. ma.ba = action;
  317. ma.battleID = battleID;
  318. sendRequest(&ma);
  319. }
  320. void CBattleCallback::battleMakeTacticAction(const BattleID & battleID, const BattleAction & action )
  321. {
  322. assert(cl->gs->getBattle(battleID)->tacticDistance);
  323. MakeAction ma;
  324. ma.ba = action;
  325. ma.battleID = battleID;
  326. sendRequest(&ma);
  327. }
  328. std::optional<BattleAction> CBattleCallback::makeSurrenderRetreatDecision(const BattleID & battleID, const BattleStateInfoForRetreat & battleState)
  329. {
  330. return cl->playerint[getPlayerID().value()]->makeSurrenderRetreatDecision(battleID, battleState);
  331. }
  332. std::shared_ptr<CPlayerBattleCallback> CBattleCallback::getBattle(const BattleID & battleID)
  333. {
  334. return activeBattles.at(battleID);
  335. }
  336. std::optional<PlayerColor> CBattleCallback::getPlayerID() const
  337. {
  338. return player;
  339. }
  340. void CBattleCallback::onBattleStarted(const IBattleInfo * info)
  341. {
  342. activeBattles[info->getBattleID()] = std::make_shared<CPlayerBattleCallback>(info, *getPlayerID());
  343. }
  344. void CBattleCallback::onBattleEnded(const BattleID & battleID)
  345. {
  346. activeBattles.erase(battleID);
  347. }