CCallback.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/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/mapObjects/CObjectClassesHandler.h"
  19. #include "lib/CGeneralTextHandler.h"
  20. #include "lib/CHeroHandler.h"
  21. #include "lib/NetPacks.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. 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. JsonNode reply(JsonNode::JsonType::DATA_INTEGER);
  42. reply.Integer() = selection;
  43. return sendQueryReply(reply, queryID);
  44. }
  45. int CCallback::sendQueryReply(const JsonNode & reply, QueryID queryID)
  46. {
  47. ASSERT_IF_CALLED_WITH_PLAYER
  48. if(queryID == QueryID(-1))
  49. {
  50. logGlobal->error("Cannot answer the query -1!");
  51. return -1;
  52. }
  53. QueryReply pack(queryID, reply);
  54. pack.player = *player;
  55. return sendRequest(&pack);
  56. }
  57. void CCallback::recruitCreatures(const CGDwelling * obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level)
  58. {
  59. // TODO exception for neutral dwellings shouldn't be hardcoded
  60. if(player != obj->tempOwner && obj->ID != Obj::WAR_MACHINE_FACTORY && obj->ID != Obj::REFUGEE_CAMP)
  61. return;
  62. RecruitCreatures pack(obj->id, dst->id, ID, amount, level);
  63. sendRequest(&pack);
  64. }
  65. bool CCallback::dismissCreature(const CArmedInstance *obj, SlotID stackPos)
  66. {
  67. if((player && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
  68. return false;
  69. DisbandCreature pack(stackPos,obj->id);
  70. sendRequest(&pack);
  71. return true;
  72. }
  73. bool CCallback::upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID)
  74. {
  75. UpgradeCreature pack(stackPos,obj->id,newID);
  76. sendRequest(&pack);
  77. return false;
  78. }
  79. void CCallback::endTurn()
  80. {
  81. logGlobal->trace("Player %d ended his turn.", player->getNum());
  82. EndTurn pack;
  83. sendRequest(&pack);
  84. }
  85. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  86. {
  87. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  88. sendRequest(&pack);
  89. return 0;
  90. }
  91. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  92. {
  93. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  94. sendRequest(&pack);
  95. return 0;
  96. }
  97. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)
  98. {
  99. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  100. sendRequest(&pack);
  101. return 0;
  102. }
  103. int CCallback::bulkMoveArmy(ObjectInstanceID srcArmy, ObjectInstanceID destArmy, SlotID srcSlot)
  104. {
  105. BulkMoveArmy pack(srcArmy, destArmy, srcSlot);
  106. sendRequest(&pack);
  107. return 0;
  108. }
  109. int CCallback::bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany)
  110. {
  111. BulkSplitStack pack(armyId, srcSlot, howMany);
  112. sendRequest(&pack);
  113. return 0;
  114. }
  115. int CCallback::bulkSmartSplitStack(ObjectInstanceID armyId, SlotID srcSlot)
  116. {
  117. BulkSmartSplitStack pack(armyId, srcSlot);
  118. sendRequest(&pack);
  119. return 0;
  120. }
  121. int CCallback::bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot)
  122. {
  123. BulkMergeStacks pack(armyId, srcSlot);
  124. sendRequest(&pack);
  125. return 0;
  126. }
  127. bool CCallback::dismissHero(const CGHeroInstance *hero)
  128. {
  129. if(player!=hero->tempOwner) return false;
  130. DismissHero pack(hero->id);
  131. sendRequest(&pack);
  132. return true;
  133. }
  134. bool CCallback::swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)
  135. {
  136. ExchangeArtifacts ea;
  137. ea.src = l1;
  138. ea.dst = l2;
  139. sendRequest(&ea);
  140. return true;
  141. }
  142. /**
  143. * Assembles or disassembles a combination artifact.
  144. * @param hero Hero holding the artifact(s).
  145. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  146. * @param assemble True for assembly operation, false for disassembly.
  147. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  148. * artifact to assemble to. Otherwise it's not used.
  149. */
  150. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)
  151. {
  152. if (player != hero->tempOwner)
  153. return false;
  154. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  155. sendRequest(&aa);
  156. return true;
  157. }
  158. void CCallback::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap)
  159. {
  160. BulkExchangeArtifacts bma(srcHero, dstHero, swap);
  161. sendRequest(&bma);
  162. }
  163. void CCallback::eraseArtifactByClient(const ArtifactLocation & al)
  164. {
  165. EraseArtifactByClient ea(al);
  166. sendRequest(&ea);
  167. }
  168. bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
  169. {
  170. if(town->tempOwner!=player)
  171. return false;
  172. if(!canBuildStructure(town, buildingID))
  173. return false;
  174. BuildStructure pack(town->id,buildingID);
  175. sendRequest(&pack);
  176. return true;
  177. }
  178. int CBattleCallback::battleMakeAction(const BattleAction * action)
  179. {
  180. assert(action->actionType == EActionType::HERO_SPELL);
  181. MakeCustomAction mca(*action);
  182. sendRequest(&mca);
  183. return 0;
  184. }
  185. int CBattleCallback::sendRequest(const CPackForServer * request)
  186. {
  187. int requestID = cl->sendRequest(request, *player);
  188. if(waitTillRealize)
  189. {
  190. logGlobal->trace("We'll wait till request %d is answered.\n", requestID);
  191. auto gsUnlocker = vstd::makeUnlockSharedGuardIf(CGameState::mutex, unlockGsWhenWaiting);
  192. CClient::waitingRequest.waitWhileContains(requestID);
  193. }
  194. boost::this_thread::interruption_point();
  195. return requestID;
  196. }
  197. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  198. {
  199. if(town->tempOwner == *player
  200. || (town->garrisonHero && town->garrisonHero->tempOwner == *player ))
  201. {
  202. GarrisonHeroSwap pack(town->id);
  203. sendRequest(&pack);
  204. }
  205. }
  206. void CCallback::buyArtifact(const CGHeroInstance *hero, ArtifactID aid)
  207. {
  208. if(hero->tempOwner != player) return;
  209. BuyArtifact pack(hero->id,aid);
  210. sendRequest(&pack);
  211. }
  212. void CCallback::trade(const IMarket * market, EMarketMode::EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero)
  213. {
  214. trade(market, mode, std::vector<ui32>(1, id1), std::vector<ui32>(1, id2), std::vector<ui32>(1, val1), hero);
  215. }
  216. 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)
  217. {
  218. TradeOnMarketplace pack;
  219. pack.marketId = dynamic_cast<const CGObjectInstance *>(market)->id;
  220. pack.heroId = hero ? hero->id : ObjectInstanceID();
  221. pack.mode = mode;
  222. pack.r1 = id1;
  223. pack.r2 = id2;
  224. pack.val = val1;
  225. sendRequest(&pack);
  226. }
  227. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  228. {
  229. SetFormation pack(hero->id,tight);
  230. sendRequest(&pack);
  231. }
  232. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  233. {
  234. assert(townOrTavern);
  235. assert(hero);
  236. ui8 i=0;
  237. for(; i<gs->players[*player].availableHeroes.size(); i++)
  238. {
  239. if(gs->players[*player].availableHeroes[i] == hero)
  240. {
  241. HireHero pack(i, townOrTavern->id);
  242. pack.player = *player;
  243. sendRequest(&pack);
  244. return;
  245. }
  246. }
  247. }
  248. void CCallback::save( const std::string &fname )
  249. {
  250. cl->save(fname);
  251. }
  252. void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
  253. {
  254. ASSERT_IF_CALLED_WITH_PLAYER
  255. PlayerMessage pm(mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  256. if(player)
  257. pm.player = *player;
  258. sendRequest(&pm);
  259. }
  260. void CCallback::buildBoat( const IShipyard *obj )
  261. {
  262. BuildBoat bb;
  263. bb.objid = obj->o->id;
  264. sendRequest(&bb);
  265. }
  266. CCallback::CCallback(CGameState * GS, std::optional<PlayerColor> Player, CClient * C):
  267. CBattleCallback(Player, C)
  268. {
  269. gs = GS;
  270. waitTillRealize = false;
  271. unlockGsWhenWaiting = false;
  272. }
  273. CCallback::~CCallback()
  274. {
  275. //trivial, but required. Don`t remove.
  276. }
  277. bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
  278. {
  279. //bidirectional
  280. return gs->map->canMoveBetween(a, b);
  281. }
  282. std::shared_ptr<const CPathsInfo> CCallback::getPathsInfo(const CGHeroInstance * h)
  283. {
  284. return cl->getPathsInfo(h);
  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. #if SCRIPTING_ENABLED
  322. scripting::Pool * CBattleCallback::getContextPool() const
  323. {
  324. return cl->getGlobalContextPool();
  325. }
  326. #endif
  327. CBattleCallback::CBattleCallback(std::optional<PlayerColor> Player, CClient * C)
  328. {
  329. player = Player;
  330. cl = C;
  331. }
  332. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  333. {
  334. assert(cl->gs->curB->tacticDistance);
  335. MakeAction ma;
  336. ma.ba = *action;
  337. sendRequest(&ma);
  338. return true;
  339. }
  340. std::optional<BattleAction> CBattleCallback::makeSurrenderRetreatDecision(const BattleStateInfoForRetreat & battleState)
  341. {
  342. return cl->playerint[getPlayerID().value()]->makeSurrenderRetreatDecision(battleState);
  343. }