CCallback.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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::JsonType::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);
  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. bool CCallback::swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)
  114. {
  115. ExchangeArtifacts ea;
  116. ea.src = l1;
  117. ea.dst = l2;
  118. sendRequest(&ea);
  119. return true;
  120. }
  121. /**
  122. * Assembles or disassembles a combination artifact.
  123. * @param hero Hero holding the artifact(s).
  124. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  125. * @param assemble True for assembly operation, false for disassembly.
  126. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  127. * artifact to assemble to. Otherwise it's not used.
  128. */
  129. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)
  130. {
  131. if (player != hero->tempOwner)
  132. return false;
  133. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  134. sendRequest(&aa);
  135. return true;
  136. }
  137. bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
  138. {
  139. if(town->tempOwner!=player)
  140. return false;
  141. if(!canBuildStructure(town, buildingID))
  142. return false;
  143. BuildStructure pack(town->id,buildingID);
  144. sendRequest(&pack);
  145. return true;
  146. }
  147. int CBattleCallback::battleMakeAction(BattleAction* action)
  148. {
  149. assert(action->actionType == EActionType::HERO_SPELL);
  150. MakeCustomAction mca(*action);
  151. sendRequest(&mca);
  152. return 0;
  153. }
  154. int CBattleCallback::sendRequest(const CPackForServer * request)
  155. {
  156. int requestID = cl->sendRequest(request, *player);
  157. if(waitTillRealize)
  158. {
  159. logGlobal->trace("We'll wait till request %d is answered.\n", requestID);
  160. auto gsUnlocker = vstd::makeUnlockSharedGuardIf(CGameState::mutex, unlockGsWhenWaiting);
  161. CClient::waitingRequest.waitWhileContains(requestID);
  162. }
  163. boost::this_thread::interruption_point();
  164. return requestID;
  165. }
  166. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  167. {
  168. if(town->tempOwner == *player
  169. || (town->garrisonHero && town->garrisonHero->tempOwner == *player ))
  170. {
  171. GarrisonHeroSwap pack(town->id);
  172. sendRequest(&pack);
  173. }
  174. }
  175. void CCallback::buyArtifact(const CGHeroInstance *hero, ArtifactID aid)
  176. {
  177. if(hero->tempOwner != player) return;
  178. BuyArtifact pack(hero->id,aid);
  179. sendRequest(&pack);
  180. }
  181. void CCallback::trade(const CGObjectInstance * market, EMarketMode::EMarketMode mode, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero)
  182. {
  183. TradeOnMarketplace pack;
  184. pack.marketId = market->id;
  185. pack.heroId = hero->id;
  186. pack.mode = mode;
  187. pack.r1 = {id1};
  188. pack.r2 = {id2};
  189. pack.val = {val1};
  190. sendRequest(&pack);
  191. }
  192. void CCallback::trade(const CGObjectInstance * market, EMarketMode::EMarketMode mode, const std::vector<ui32> & id1, const std::vector<ui32> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero)
  193. {
  194. TradeOnMarketplace pack;
  195. pack.marketId = market->id;
  196. pack.heroId = hero->id;
  197. pack.mode = mode;
  198. pack.r1 = id1;
  199. pack.r2 = id2;
  200. pack.val = val1;
  201. sendRequest(&pack);
  202. }
  203. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  204. {
  205. SetFormation pack(hero->id,tight);
  206. sendRequest(&pack);
  207. }
  208. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  209. {
  210. assert(townOrTavern);
  211. assert(hero);
  212. ui8 i=0;
  213. for(; i<gs->players[*player].availableHeroes.size(); i++)
  214. {
  215. if(gs->players[*player].availableHeroes[i] == hero)
  216. {
  217. HireHero pack(i, townOrTavern->id);
  218. pack.player = *player;
  219. sendRequest(&pack);
  220. return;
  221. }
  222. }
  223. }
  224. void CCallback::save( const std::string &fname )
  225. {
  226. cl->save(fname);
  227. }
  228. void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
  229. {
  230. ASSERT_IF_CALLED_WITH_PLAYER
  231. PlayerMessage pm(mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  232. sendRequest(&pm);
  233. }
  234. void CCallback::buildBoat( const IShipyard *obj )
  235. {
  236. BuildBoat bb;
  237. bb.objid = obj->o->id;
  238. sendRequest(&bb);
  239. }
  240. CCallback::CCallback(CGameState * GS, boost::optional<PlayerColor> Player, CClient * C)
  241. : CBattleCallback(Player, C)
  242. {
  243. gs = GS;
  244. waitTillRealize = false;
  245. unlockGsWhenWaiting = false;
  246. }
  247. CCallback::~CCallback()
  248. {
  249. //trivial, but required. Don`t remove.
  250. }
  251. bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
  252. {
  253. //bidirectional
  254. return gs->map->canMoveBetween(a, b);
  255. }
  256. const CPathsInfo * CCallback::getPathsInfo(const CGHeroInstance *h)
  257. {
  258. return cl->getPathsInfo(h);
  259. }
  260. int3 CCallback::getGuardingCreaturePosition(int3 tile)
  261. {
  262. if (!gs->map->isInTheMap(tile))
  263. return int3(-1,-1,-1);
  264. return gs->map->guardingCreaturePositions[tile.x][tile.y][tile.z];
  265. }
  266. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out)
  267. {
  268. gs->calculatePaths(hero, out);
  269. }
  270. void CCallback::dig( const CGObjectInstance *hero )
  271. {
  272. DigWithHero dwh;
  273. dwh.id = hero->id;
  274. sendRequest(&dwh);
  275. }
  276. void CCallback::castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos)
  277. {
  278. CastAdvSpell cas;
  279. cas.hid = hero->id;
  280. cas.sid = spellID;
  281. cas.pos = pos;
  282. sendRequest(&cas);
  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(boost::optional<PlayerColor> Player, CClient *C )
  308. {
  309. player = Player;
  310. cl = C;
  311. }
  312. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  313. {
  314. assert(cl->gs->curB->tacticDistance);
  315. MakeAction ma;
  316. ma.ba = *action;
  317. sendRequest(&ma);
  318. return true;
  319. }