CCallback.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/CArtHandler.h"
  25. #include "lib/GameConstants.h"
  26. #include "lib/CPlayerState.h"
  27. #include "lib/UnlockGuard.h"
  28. #include "lib/battle/BattleInfo.h"
  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. JsonNode reply(JsonNode::JsonType::DATA_INTEGER);
  44. reply.Integer() = selection;
  45. return sendQueryReply(reply, queryID);
  46. }
  47. int CCallback::sendQueryReply(const JsonNode & reply, QueryID queryID)
  48. {
  49. ASSERT_IF_CALLED_WITH_PLAYER
  50. if(queryID == QueryID(-1))
  51. {
  52. logGlobal->error("Cannot answer the query -1!");
  53. return -1;
  54. }
  55. QueryReply pack(queryID, reply);
  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)
  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->trace("Player %d ended his turn.", player.get().getNum());
  84. EndTurn pack;
  85. sendRequest(&pack);
  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. 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(const BattleAction * action)
  147. {
  148. assert(action->actionType == EActionType::HERO_SPELL);
  149. MakeCustomAction mca(*action);
  150. sendRequest(&mca);
  151. return 0;
  152. }
  153. int CBattleCallback::sendRequest(const CPackForServer * request)
  154. {
  155. int requestID = cl->sendRequest(request, *player);
  156. if(waitTillRealize)
  157. {
  158. logGlobal->trace("We'll wait till request %d is answered.\n", requestID);
  159. auto gsUnlocker = vstd::makeUnlockSharedGuardIf(CGameState::mutex, unlockGsWhenWaiting);
  160. CClient::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, ui32 id1, ui32 id2, ui32 val1, const CGHeroInstance * hero)
  181. {
  182. trade(market, mode, std::vector<ui32>(1, id1), std::vector<ui32>(1, id2), std::vector<ui32>(1, val1), hero);
  183. }
  184. 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)
  185. {
  186. TradeOnMarketplace pack;
  187. pack.marketId = market->id;
  188. pack.heroId = hero ? hero->id : ObjectInstanceID();
  189. pack.mode = mode;
  190. pack.r1 = id1;
  191. pack.r2 = id2;
  192. pack.val = val1;
  193. sendRequest(&pack);
  194. }
  195. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  196. {
  197. SetFormation pack(hero->id,tight);
  198. sendRequest(&pack);
  199. }
  200. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero)
  201. {
  202. assert(townOrTavern);
  203. assert(hero);
  204. ui8 i=0;
  205. for(; i<gs->players[*player].availableHeroes.size(); i++)
  206. {
  207. if(gs->players[*player].availableHeroes[i] == hero)
  208. {
  209. HireHero pack(i, townOrTavern->id);
  210. pack.player = *player;
  211. sendRequest(&pack);
  212. return;
  213. }
  214. }
  215. }
  216. void CCallback::save( const std::string &fname )
  217. {
  218. cl->save(fname);
  219. }
  220. void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
  221. {
  222. ASSERT_IF_CALLED_WITH_PLAYER
  223. PlayerMessage pm(mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  224. sendRequest(&pm);
  225. }
  226. void CCallback::buildBoat( const IShipyard *obj )
  227. {
  228. BuildBoat bb;
  229. bb.objid = obj->o->id;
  230. sendRequest(&bb);
  231. }
  232. CCallback::CCallback(CGameState * GS, boost::optional<PlayerColor> Player, CClient * C)
  233. : CBattleCallback(Player, C)
  234. {
  235. gs = GS;
  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. //bidirectional
  246. return gs->map->canMoveBetween(a, b);
  247. }
  248. std::shared_ptr<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. int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  277. {
  278. if(s1->getCreature(p1) == s2->getCreature(p2))
  279. return mergeStacks(s1, s2, p1, p2);
  280. else
  281. return swapCreatures(s1, s2, p1, p2);
  282. }
  283. void CCallback::registerBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  284. {
  285. cl->additionalBattleInts[*player].push_back(battleEvents);
  286. }
  287. void CCallback::unregisterBattleInterface(std::shared_ptr<IBattleEventsReceiver> battleEvents)
  288. {
  289. cl->additionalBattleInts[*player] -= battleEvents;
  290. }
  291. scripting::Pool * CBattleCallback::getContextPool() const
  292. {
  293. return cl->getGlobalContextPool();
  294. }
  295. CBattleCallback::CBattleCallback(boost::optional<PlayerColor> Player, CClient *C )
  296. {
  297. player = Player;
  298. cl = C;
  299. }
  300. bool CBattleCallback::battleMakeTacticAction( BattleAction * action )
  301. {
  302. assert(cl->gs->curB->tacticDistance);
  303. MakeAction ma;
  304. ma.ba = *action;
  305. sendRequest(&ma);
  306. return true;
  307. }