CCallback.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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(const 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. trade(market, mode, std::vector<ui32>(1, id1), std::vector<ui32>(1, id2), std::vector<ui32>(1, val1), hero);
  184. }
  185. 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)
  186. {
  187. TradeOnMarketplace pack;
  188. pack.marketId = market->id;
  189. pack.heroId = hero ? hero->id : ObjectInstanceID();
  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(mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  225. sendRequest(&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(Player, C)
  235. {
  236. gs = GS;
  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. std::shared_ptr<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. 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(boost::optional<PlayerColor> Player, CClient *C )
  301. {
  302. player = Player;
  303. cl = C;
  304. }
  305. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  306. {
  307. assert(cl->gs->curB->tacticDistance);
  308. MakeAction ma;
  309. ma.ba = *action;
  310. sendRequest(&ma);
  311. return true;
  312. }