CCallback.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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/spells/CSpellHandler.h"
  25. #include "lib/CArtHandler.h"
  26. #include "lib/GameConstants.h"
  27. #include "lib/CPlayerState.h"
  28. #include "lib/UnlockGuard.h"
  29. #include "lib/battle/BattleInfo.h"
  30. bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
  31. {
  32. CastleTeleportHero pack(who->id, where->id, 1);
  33. sendRequest(&pack);
  34. return true;
  35. }
  36. bool CCallback::moveHero(const CGHeroInstance *h, int3 dst, bool transit)
  37. {
  38. MoveHero pack(dst,h->id,transit);
  39. sendRequest(&pack);
  40. return true;
  41. }
  42. int CCallback::selectionMade(int selection, QueryID queryID)
  43. {
  44. JsonNode reply(JsonNode::DATA_INTEGER);
  45. reply.Integer() = selection;
  46. return sendQueryReply(reply, queryID);
  47. }
  48. int CCallback::sendQueryReply(const JsonNode & reply, QueryID queryID)
  49. {
  50. ASSERT_IF_CALLED_WITH_PLAYER
  51. if(queryID == QueryID(-1))
  52. {
  53. logGlobal->error("Cannot answer the query -1!");
  54. return -1;
  55. }
  56. QueryReply pack(queryID, reply);
  57. pack.player = *player;
  58. return sendRequest(&pack);
  59. }
  60. void CCallback::recruitCreatures(const CGDwelling * obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level)
  61. {
  62. // TODO exception for neutral dwellings shouldn't be hardcoded
  63. if(player != obj->tempOwner && obj->ID != Obj::WAR_MACHINE_FACTORY && obj->ID != Obj::REFUGEE_CAMP)
  64. return;
  65. RecruitCreatures pack(obj->id, dst->id, ID, amount, level);
  66. sendRequest(&pack);
  67. }
  68. bool CCallback::dismissCreature(const CArmedInstance *obj, SlotID stackPos)
  69. {
  70. if((player && obj->tempOwner != player) || (obj->stacksCount()<2 && obj->needsLastStack()))
  71. return false;
  72. DisbandCreature pack(stackPos,obj->id);
  73. sendRequest(&pack);
  74. return true;
  75. }
  76. bool CCallback::upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID)
  77. {
  78. UpgradeCreature pack(stackPos,obj->id,newID);
  79. sendRequest(&pack);
  80. return false;
  81. }
  82. void CCallback::endTurn()
  83. {
  84. logGlobal->trace("Player %d ended his turn.", player.get().getNum());
  85. EndTurn pack;
  86. sendRequest(&pack); //report that we ended turn
  87. }
  88. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  89. {
  90. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  91. sendRequest(&pack);
  92. return 0;
  93. }
  94. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  95. {
  96. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  97. sendRequest(&pack);
  98. return 0;
  99. }
  100. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)
  101. {
  102. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  103. sendRequest(&pack);
  104. return 0;
  105. }
  106. bool CCallback::dismissHero(const CGHeroInstance *hero)
  107. {
  108. if(player!=hero->tempOwner) return false;
  109. DismissHero pack(hero->id);
  110. sendRequest(&pack);
  111. return true;
  112. }
  113. // int CCallback::getMySerial() const
  114. // {
  115. // boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  116. // return gs->players[player].serial;
  117. // }
  118. bool CCallback::swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)
  119. {
  120. ExchangeArtifacts ea;
  121. ea.src = l1;
  122. ea.dst = l2;
  123. sendRequest(&ea);
  124. return true;
  125. }
  126. /**
  127. * Assembles or disassembles a combination artifact.
  128. * @param hero Hero holding the artifact(s).
  129. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  130. * @param assemble True for assembly operation, false for disassembly.
  131. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  132. * artifact to assemble to. Otherwise it's not used.
  133. */
  134. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)
  135. {
  136. if (player != hero->tempOwner)
  137. return false;
  138. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  139. sendRequest(&aa);
  140. return true;
  141. }
  142. bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
  143. {
  144. if(town->tempOwner!=player)
  145. return false;
  146. if(!canBuildStructure(town, buildingID))
  147. return false;
  148. BuildStructure pack(town->id,buildingID);
  149. sendRequest(&pack);
  150. return true;
  151. }
  152. int CBattleCallback::battleMakeAction(BattleAction* action)
  153. {
  154. assert(action->actionType == Battle::HERO_SPELL);
  155. MakeCustomAction mca(*action);
  156. sendRequest(&mca);
  157. return 0;
  158. }
  159. int CBattleCallback::sendRequest(const CPack *request)
  160. {
  161. int requestID = cl->sendRequest(request, *player);
  162. if(waitTillRealize)
  163. {
  164. logGlobal->trace("We'll wait till request %d is answered.\n", requestID);
  165. auto gsUnlocker = vstd::makeUnlockSharedGuardIf(CGameState::mutex, unlockGsWhenWaiting);
  166. CClient::waitingRequest.waitWhileContains(requestID);
  167. }
  168. boost::this_thread::interruption_point();
  169. return requestID;
  170. }
  171. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  172. {
  173. if(town->tempOwner == *player
  174. || (town->garrisonHero && town->garrisonHero->tempOwner == *player ))
  175. {
  176. GarrisonHeroSwap pack(town->id);
  177. sendRequest(&pack);
  178. }
  179. }
  180. void CCallback::buyArtifact(const CGHeroInstance *hero, ArtifactID aid)
  181. {
  182. if(hero->tempOwner != player) return;
  183. BuyArtifact pack(hero->id,aid);
  184. sendRequest(&pack);
  185. }
  186. void CCallback::trade(const CGObjectInstance *market, EMarketMode::EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance *hero)
  187. {
  188. TradeOnMarketplace pack;
  189. pack.market = market;
  190. pack.hero = hero;
  191. pack.mode = mode;
  192. pack.r1 = {id1};
  193. pack.r2 = {id2};
  194. pack.val = {val1};
  195. sendRequest(&pack);
  196. }
  197. void CCallback::trade(const CGObjectInstance *market, EMarketMode::EMarketMode mode, std::vector<ui32> id1, std::vector<ui32> id2, std::vector<ui32> val1, const CGHeroInstance *hero)
  198. {
  199. TradeOnMarketplace pack;
  200. pack.market = market;
  201. pack.hero = hero;
  202. pack.mode = mode;
  203. pack.r1 = id1;
  204. pack.r2 = id2;
  205. pack.val = val1;
  206. sendRequest(&pack);
  207. }
  208. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  209. {
  210. SetFormation pack(hero->id,tight);
  211. sendRequest(&pack);
  212. }
  213. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  214. {
  215. assert(townOrTavern);
  216. assert(hero);
  217. ui8 i=0;
  218. for(; i<gs->players[*player].availableHeroes.size(); i++)
  219. {
  220. if(gs->players[*player].availableHeroes[i] == hero)
  221. {
  222. HireHero pack(i, townOrTavern->id);
  223. pack.player = *player;
  224. sendRequest(&pack);
  225. return;
  226. }
  227. }
  228. }
  229. void CCallback::save( const std::string &fname )
  230. {
  231. cl->save(fname);
  232. }
  233. void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
  234. {
  235. ASSERT_IF_CALLED_WITH_PLAYER
  236. PlayerMessage pm(*player, mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  237. sendRequest(&(CPackForClient&)pm);
  238. }
  239. void CCallback::buildBoat( const IShipyard *obj )
  240. {
  241. BuildBoat bb;
  242. bb.objid = obj->o->id;
  243. sendRequest(&bb);
  244. }
  245. CCallback::CCallback( CGameState * GS, boost::optional<PlayerColor> Player, CClient *C )
  246. :CBattleCallback(GS, Player, C)
  247. {
  248. waitTillRealize = false;
  249. unlockGsWhenWaiting = false;
  250. }
  251. CCallback::~CCallback()
  252. {
  253. //trivial, but required. Don`t remove.
  254. }
  255. bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
  256. {
  257. //bidirectional
  258. return gs->map->canMoveBetween(a, b);
  259. }
  260. const CPathsInfo * CCallback::getPathsInfo(const CGHeroInstance *h)
  261. {
  262. return cl->getPathsInfo(h);
  263. }
  264. int3 CCallback::getGuardingCreaturePosition(int3 tile)
  265. {
  266. if (!gs->map->isInTheMap(tile))
  267. return int3(-1,-1,-1);
  268. return gs->map->guardingCreaturePositions[tile.x][tile.y][tile.z];
  269. }
  270. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out)
  271. {
  272. gs->calculatePaths(hero, out);
  273. }
  274. void CCallback::dig( const CGObjectInstance *hero )
  275. {
  276. DigWithHero dwh;
  277. dwh.id = hero->id;
  278. sendRequest(&dwh);
  279. }
  280. void CCallback::castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos)
  281. {
  282. CastAdvSpell cas;
  283. cas.hid = hero->id;
  284. cas.sid = spellID;
  285. cas.pos = pos;
  286. sendRequest(&cas);
  287. }
  288. void CCallback::unregisterAllInterfaces()
  289. {
  290. for (auto& pi : cl->playerint)
  291. pi.second->finish();
  292. cl->playerint.clear();
  293. cl->battleints.clear();
  294. }
  295. int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  296. {
  297. if(s1->getCreature(p1) == s2->getCreature(p2))
  298. return mergeStacks(s1, s2, p1, p2);
  299. else
  300. return swapCreatures(s1, s2, p1, p2);
  301. }
  302. void CCallback::registerGameInterface(std::shared_ptr<IGameEventsReceiver> gameEvents)
  303. {
  304. cl->additionalPlayerInts[*player].push_back(gameEvents);
  305. }
  306. void CCallback::registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  307. {
  308. cl->additionalBattleInts[*player].push_back(battleEvents);
  309. }
  310. void CCallback::unregisterGameInterface(std::shared_ptr<IGameEventsReceiver> gameEvents)
  311. {
  312. cl->additionalPlayerInts[*player] -= gameEvents;
  313. }
  314. void CCallback::unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  315. {
  316. cl->additionalBattleInts[*player] -= battleEvents;
  317. }
  318. CBattleCallback::CBattleCallback(CGameState *GS, boost::optional<PlayerColor> Player, CClient *C )
  319. {
  320. gs = GS;
  321. player = Player;
  322. cl = C;
  323. }
  324. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  325. {
  326. assert(cl->gs->curB->tacticDistance);
  327. MakeAction ma;
  328. ma.ba = *action;
  329. sendRequest(&ma);
  330. return true;
  331. }