CCallback.cpp 10 KB

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