CCallback.cpp 12 KB

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