CCallback.cpp 9.3 KB

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