CCallback.cpp 11 KB

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