CCallback.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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, int id1, int id2, int 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::setFormation(const CGHeroInstance * hero, bool tight)
  198. {
  199. SetFormation pack(hero->id,tight);
  200. sendRequest(&pack);
  201. }
  202. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  203. {
  204. assert(townOrTavern);
  205. assert(hero);
  206. ui8 i=0;
  207. for(; i<gs->players[*player].availableHeroes.size(); i++)
  208. {
  209. if(gs->players[*player].availableHeroes[i] == hero)
  210. {
  211. HireHero pack(i, townOrTavern->id);
  212. pack.player = *player;
  213. sendRequest(&pack);
  214. return;
  215. }
  216. }
  217. }
  218. void CCallback::save( const std::string &fname )
  219. {
  220. cl->save(fname);
  221. }
  222. void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
  223. {
  224. ASSERT_IF_CALLED_WITH_PLAYER
  225. PlayerMessage pm(*player, mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  226. sendRequest(&(CPackForClient&)pm);
  227. }
  228. void CCallback::buildBoat( const IShipyard *obj )
  229. {
  230. BuildBoat bb;
  231. bb.objid = obj->o->id;
  232. sendRequest(&bb);
  233. }
  234. CCallback::CCallback( CGameState * GS, boost::optional<PlayerColor> Player, CClient *C )
  235. :CBattleCallback(GS, Player, C)
  236. {
  237. waitTillRealize = false;
  238. unlockGsWhenWaiting = false;
  239. }
  240. CCallback::~CCallback()
  241. {
  242. //trivial, but required. Don`t remove.
  243. }
  244. bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
  245. {
  246. //bidirectional
  247. return gs->map->canMoveBetween(a, b);
  248. }
  249. const CPathsInfo * CCallback::getPathsInfo(const CGHeroInstance *h)
  250. {
  251. return cl->getPathsInfo(h);
  252. }
  253. int3 CCallback::getGuardingCreaturePosition(int3 tile)
  254. {
  255. if (!gs->map->isInTheMap(tile))
  256. return int3(-1,-1,-1);
  257. return gs->map->guardingCreaturePositions[tile.x][tile.y][tile.z];
  258. }
  259. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out)
  260. {
  261. gs->calculatePaths(hero, out);
  262. }
  263. void CCallback::dig( const CGObjectInstance *hero )
  264. {
  265. DigWithHero dwh;
  266. dwh.id = hero->id;
  267. sendRequest(&dwh);
  268. }
  269. void CCallback::castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos)
  270. {
  271. CastAdvSpell cas;
  272. cas.hid = hero->id;
  273. cas.sid = spellID;
  274. cas.pos = pos;
  275. sendRequest(&cas);
  276. }
  277. void CCallback::unregisterAllInterfaces()
  278. {
  279. for (auto& pi : cl->playerint)
  280. pi.second->finish();
  281. cl->playerint.clear();
  282. cl->battleints.clear();
  283. }
  284. int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  285. {
  286. if(s1->getCreature(p1) == s2->getCreature(p2))
  287. return mergeStacks(s1, s2, p1, p2);
  288. else
  289. return swapCreatures(s1, s2, p1, p2);
  290. }
  291. void CCallback::registerGameInterface(std::shared_ptr<IGameEventsReceiver> gameEvents)
  292. {
  293. cl->additionalPlayerInts[*player].push_back(gameEvents);
  294. }
  295. void CCallback::registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  296. {
  297. cl->additionalBattleInts[*player].push_back(battleEvents);
  298. }
  299. void CCallback::unregisterGameInterface(std::shared_ptr<IGameEventsReceiver> gameEvents)
  300. {
  301. cl->additionalPlayerInts[*player] -= gameEvents;
  302. }
  303. void CCallback::unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  304. {
  305. cl->additionalBattleInts[*player] -= battleEvents;
  306. }
  307. CBattleCallback::CBattleCallback(CGameState *GS, boost::optional<PlayerColor> Player, CClient *C )
  308. {
  309. gs = GS;
  310. player = Player;
  311. cl = C;
  312. }
  313. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  314. {
  315. assert(cl->gs->curB->tacticDistance);
  316. MakeAction ma;
  317. ma.ba = *action;
  318. sendRequest(&ma);
  319. return true;
  320. }