CCallback.cpp 11 KB

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