2
0

CCallback.cpp 9.3 KB

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