CCallback.cpp 9.5 KB

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