2
0

CCallback.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. JsonNode reply(JsonNode::JsonType::DATA_INTEGER);
  41. reply.Integer() = selection;
  42. return sendQueryReply(reply, queryID);
  43. }
  44. int CCallback::sendQueryReply(const JsonNode & 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. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)
  150. {
  151. if (player != hero->tempOwner)
  152. return false;
  153. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  154. sendRequest(&aa);
  155. return true;
  156. }
  157. void CCallback::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap)
  158. {
  159. BulkExchangeArtifacts bma(srcHero, dstHero, swap);
  160. sendRequest(&bma);
  161. }
  162. void CCallback::eraseArtifactByClient(const ArtifactLocation & al)
  163. {
  164. EraseArtifactByClient ea(al);
  165. sendRequest(&ea);
  166. }
  167. bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
  168. {
  169. if(town->tempOwner!=player)
  170. return false;
  171. if(!canBuildStructure(town, buildingID))
  172. return false;
  173. BuildStructure pack(town->id,buildingID);
  174. sendRequest(&pack);
  175. return true;
  176. }
  177. int CBattleCallback::battleMakeAction(const BattleAction * action)
  178. {
  179. assert(action->actionType == EActionType::HERO_SPELL);
  180. MakeCustomAction mca(*action);
  181. sendRequest(&mca);
  182. return 0;
  183. }
  184. int CBattleCallback::sendRequest(const CPackForServer * request)
  185. {
  186. int requestID = cl->sendRequest(request, *player);
  187. if(waitTillRealize)
  188. {
  189. logGlobal->trace("We'll wait till request %d is answered.\n", requestID);
  190. auto gsUnlocker = vstd::makeUnlockSharedGuardIf(CGameState::mutex, unlockGsWhenWaiting);
  191. CClient::waitingRequest.waitWhileContains(requestID);
  192. }
  193. boost::this_thread::interruption_point();
  194. return requestID;
  195. }
  196. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  197. {
  198. if(town->tempOwner == *player
  199. || (town->garrisonHero && town->garrisonHero->tempOwner == *player ))
  200. {
  201. GarrisonHeroSwap pack(town->id);
  202. sendRequest(&pack);
  203. }
  204. }
  205. void CCallback::buyArtifact(const CGHeroInstance *hero, ArtifactID aid)
  206. {
  207. if(hero->tempOwner != player) return;
  208. BuyArtifact pack(hero->id,aid);
  209. sendRequest(&pack);
  210. }
  211. void CCallback::trade(const IMarket * market, EMarketMode::EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero)
  212. {
  213. trade(market, mode, std::vector<ui32>(1, id1), std::vector<ui32>(1, id2), std::vector<ui32>(1, val1), hero);
  214. }
  215. void CCallback::trade(const IMarket * market, EMarketMode::EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero)
  216. {
  217. TradeOnMarketplace pack;
  218. pack.marketId = dynamic_cast<const CGObjectInstance *>(market)->id;
  219. pack.heroId = hero ? hero->id : ObjectInstanceID();
  220. pack.mode = mode;
  221. pack.r1 = id1;
  222. pack.r2 = id2;
  223. pack.val = val1;
  224. sendRequest(&pack);
  225. }
  226. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  227. {
  228. SetFormation pack(hero->id,tight);
  229. sendRequest(&pack);
  230. }
  231. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  232. {
  233. assert(townOrTavern);
  234. assert(hero);
  235. HireHero pack(HeroTypeID(hero->subID), townOrTavern->id);
  236. pack.player = *player;
  237. sendRequest(&pack);
  238. }
  239. void CCallback::save( const std::string &fname )
  240. {
  241. cl->save(fname);
  242. }
  243. void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
  244. {
  245. ASSERT_IF_CALLED_WITH_PLAYER
  246. PlayerMessage pm(mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  247. if(player)
  248. pm.player = *player;
  249. sendRequest(&pm);
  250. }
  251. void CCallback::buildBoat( const IShipyard *obj )
  252. {
  253. BuildBoat bb;
  254. bb.objid = dynamic_cast<const CGObjectInstance*>(obj)->id;
  255. sendRequest(&bb);
  256. }
  257. CCallback::CCallback(CGameState * GS, std::optional<PlayerColor> Player, CClient * C):
  258. CBattleCallback(Player, C)
  259. {
  260. gs = GS;
  261. waitTillRealize = false;
  262. unlockGsWhenWaiting = false;
  263. }
  264. CCallback::~CCallback()
  265. {
  266. //trivial, but required. Don`t remove.
  267. }
  268. bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
  269. {
  270. //bidirectional
  271. return gs->map->canMoveBetween(a, b);
  272. }
  273. std::shared_ptr<const CPathsInfo> CCallback::getPathsInfo(const CGHeroInstance * h)
  274. {
  275. return cl->getPathsInfo(h);
  276. }
  277. int3 CCallback::getGuardingCreaturePosition(int3 tile)
  278. {
  279. if (!gs->map->isInTheMap(tile))
  280. return int3(-1,-1,-1);
  281. return gs->map->guardingCreaturePositions[tile.z][tile.x][tile.y];
  282. }
  283. void CCallback::dig( const CGObjectInstance *hero )
  284. {
  285. DigWithHero dwh;
  286. dwh.id = hero->id;
  287. sendRequest(&dwh);
  288. }
  289. void CCallback::castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos)
  290. {
  291. CastAdvSpell cas;
  292. cas.hid = hero->id;
  293. cas.sid = spellID;
  294. cas.pos = pos;
  295. sendRequest(&cas);
  296. }
  297. int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  298. {
  299. if(s1->getCreature(p1) == s2->getCreature(p2))
  300. return mergeStacks(s1, s2, p1, p2);
  301. else
  302. return swapCreatures(s1, s2, p1, p2);
  303. }
  304. void CCallback::registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  305. {
  306. cl->additionalBattleInts[*player].push_back(battleEvents);
  307. }
  308. void CCallback::unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  309. {
  310. cl->additionalBattleInts[*player] -= battleEvents;
  311. }
  312. #if SCRIPTING_ENABLED
  313. scripting::Pool * CBattleCallback::getContextPool() const
  314. {
  315. return cl->getGlobalContextPool();
  316. }
  317. #endif
  318. CBattleCallback::CBattleCallback(std::optional<PlayerColor> Player, CClient * C)
  319. {
  320. player = Player;
  321. cl = C;
  322. }
  323. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  324. {
  325. assert(cl->gs->curB->tacticDistance);
  326. MakeAction ma;
  327. ma.ba = *action;
  328. sendRequest(&ma);
  329. return true;
  330. }
  331. std::optional<BattleAction> CBattleCallback::makeSurrenderRetreatDecision(const BattleStateInfoForRetreat & battleState)
  332. {
  333. return cl->playerint[getPlayerID().value()]->makeSurrenderRetreatDecision(battleState);
  334. }