CCallback.cpp 10 KB

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