CCallback.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 "../gameState/CGameState.h"
  13. #include "../mapObjects/CGHeroInstance.h"
  14. #include "../mapObjects/CGTownInstance.h"
  15. #include "../mapping/CMap.h"
  16. #include "../networkPacks/PacksForServer.h"
  17. #include "../networkPacks/SaveLocalState.h"
  18. #define ASSERT_IF_CALLED_WITH_PLAYER if(!getPlayerID()) {logGlobal->error(BOOST_CURRENT_FUNCTION); assert(0);}
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. bool CCallback::teleportHero(const CGHeroInstance *who, const CGTownInstance *where)
  21. {
  22. CastleTeleportHero pack(who->id, where->id, 1);
  23. sendRequest(pack);
  24. return true;
  25. }
  26. void CCallback::moveHero(const CGHeroInstance *h, const int3 & destination, bool transit)
  27. {
  28. MoveHero pack({destination}, h->id, transit);
  29. sendRequest(pack);
  30. }
  31. void CCallback::moveHero(const CGHeroInstance *h, const std::vector<int3> & path, bool transit)
  32. {
  33. MoveHero pack(path, h->id, transit);
  34. sendRequest(pack);
  35. }
  36. int CCallback::selectionMade(int selection, QueryID queryID)
  37. {
  38. return sendQueryReply(selection, queryID);
  39. }
  40. int CCallback::sendQueryReply(std::optional<int32_t> reply, QueryID queryID)
  41. {
  42. ASSERT_IF_CALLED_WITH_PLAYER
  43. if(queryID == QueryID(-1))
  44. {
  45. logGlobal->error("Cannot answer the query -1!");
  46. return -1;
  47. }
  48. QueryReply pack(queryID, reply);
  49. pack.player = *getPlayerID();
  50. return sendRequest(pack);
  51. }
  52. void CCallback::recruitCreatures(const CGDwelling * obj, const CArmedInstance * dst, CreatureID ID, ui32 amount, si32 level)
  53. {
  54. // TODO exception for neutral dwellings shouldn't be hardcoded
  55. if(getPlayerID() != obj->tempOwner && obj->ID != Obj::WAR_MACHINE_FACTORY && obj->ID != Obj::REFUGEE_CAMP)
  56. return;
  57. RecruitCreatures pack(obj->id, dst->id, ID, amount, level);
  58. sendRequest(pack);
  59. }
  60. bool CCallback::dismissCreature(const CArmedInstance *obj, SlotID stackPos)
  61. {
  62. if((getPlayerID() && obj->tempOwner != getPlayerID()) || (obj->stacksCount()<2 && obj->needsLastStack()))
  63. return false;
  64. DisbandCreature pack(stackPos,obj->id);
  65. sendRequest(pack);
  66. return true;
  67. }
  68. bool CCallback::upgradeCreature(const CArmedInstance *obj, SlotID stackPos, CreatureID newID)
  69. {
  70. UpgradeCreature pack(stackPos,obj->id,newID);
  71. sendRequest(pack);
  72. return false;
  73. }
  74. void CCallback::endTurn()
  75. {
  76. logGlobal->trace("Player %d ended his turn.", getPlayerID()->getNum());
  77. EndTurn pack;
  78. sendRequest(pack);
  79. }
  80. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  81. {
  82. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  83. sendRequest(pack);
  84. return 0;
  85. }
  86. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  87. {
  88. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  89. sendRequest(pack);
  90. return 0;
  91. }
  92. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2, int val)
  93. {
  94. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  95. sendRequest(pack);
  96. return 0;
  97. }
  98. int CCallback::bulkMoveArmy(ObjectInstanceID srcArmy, ObjectInstanceID destArmy, SlotID srcSlot)
  99. {
  100. BulkMoveArmy pack(srcArmy, destArmy, srcSlot);
  101. sendRequest(pack);
  102. return 0;
  103. }
  104. int CCallback::bulkSplitStack(ObjectInstanceID armyId, SlotID srcSlot, int howMany)
  105. {
  106. BulkSplitStack pack(armyId, srcSlot, howMany);
  107. sendRequest(pack);
  108. return 0;
  109. }
  110. int CCallback::bulkSplitAndRebalanceStack(ObjectInstanceID armyId, SlotID srcSlot)
  111. {
  112. BulkSplitAndRebalanceStack pack(armyId, srcSlot);
  113. sendRequest(pack);
  114. return 0;
  115. }
  116. int CCallback::bulkMergeStacks(ObjectInstanceID armyId, SlotID srcSlot)
  117. {
  118. BulkMergeStacks pack(armyId, srcSlot);
  119. sendRequest(pack);
  120. return 0;
  121. }
  122. bool CCallback::dismissHero(const CGHeroInstance *hero)
  123. {
  124. if(getPlayerID()!=hero->tempOwner) return false;
  125. DismissHero pack(hero->id);
  126. sendRequest(pack);
  127. return true;
  128. }
  129. bool CCallback::swapArtifacts(const ArtifactLocation &l1, const ArtifactLocation &l2)
  130. {
  131. ExchangeArtifacts ea;
  132. ea.src = l1;
  133. ea.dst = l2;
  134. sendRequest(ea);
  135. return true;
  136. }
  137. /**
  138. * Assembles or disassembles a combination artifact.
  139. * @param hero Hero holding the artifact(s).
  140. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  141. * @param assemble True for assembly operation, false for disassembly.
  142. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  143. * artifact to assemble to. Otherwise it's not used.
  144. */
  145. void CCallback::assembleArtifacts(const ObjectInstanceID & heroID, ArtifactPosition artifactSlot, bool assemble, ArtifactID assembleTo)
  146. {
  147. AssembleArtifacts aa(heroID, artifactSlot, assemble, assembleTo);
  148. sendRequest(aa);
  149. }
  150. void CCallback::bulkMoveArtifacts(ObjectInstanceID srcHero, ObjectInstanceID dstHero, bool swap, bool equipped, bool backpack)
  151. {
  152. BulkExchangeArtifacts bma(srcHero, dstHero, swap, equipped, backpack);
  153. sendRequest(bma);
  154. }
  155. void CCallback::scrollBackpackArtifacts(ObjectInstanceID hero, bool left)
  156. {
  157. ManageBackpackArtifacts mba(hero, ManageBackpackArtifacts::ManageCmd::SCROLL_RIGHT);
  158. if(left)
  159. mba.cmd = ManageBackpackArtifacts::ManageCmd::SCROLL_LEFT;
  160. sendRequest(mba);
  161. }
  162. void CCallback::sortBackpackArtifactsBySlot(const ObjectInstanceID hero)
  163. {
  164. ManageBackpackArtifacts mba(hero, ManageBackpackArtifacts::ManageCmd::SORT_BY_SLOT);
  165. sendRequest(mba);
  166. }
  167. void CCallback::sortBackpackArtifactsByCost(const ObjectInstanceID hero)
  168. {
  169. ManageBackpackArtifacts mba(hero, ManageBackpackArtifacts::ManageCmd::SORT_BY_COST);
  170. sendRequest(mba);
  171. }
  172. void CCallback::sortBackpackArtifactsByClass(const ObjectInstanceID hero)
  173. {
  174. ManageBackpackArtifacts mba(hero, ManageBackpackArtifacts::ManageCmd::SORT_BY_CLASS);
  175. sendRequest(mba);
  176. }
  177. void CCallback::manageHeroCostume(ObjectInstanceID hero, size_t costumeIndex, bool saveCostume)
  178. {
  179. ManageEquippedArtifacts mea(hero, costumeIndex, saveCostume);
  180. sendRequest(mea);
  181. }
  182. void CCallback::eraseArtifactByClient(const ArtifactLocation & al)
  183. {
  184. EraseArtifactByClient ea(al);
  185. sendRequest(ea);
  186. }
  187. bool CCallback::buildBuilding(const CGTownInstance *town, BuildingID buildingID)
  188. {
  189. if(town->tempOwner!=getPlayerID())
  190. return false;
  191. if(canBuildStructure(town, buildingID) != EBuildingState::ALLOWED)
  192. return false;
  193. BuildStructure pack(town->id,buildingID);
  194. sendRequest(pack);
  195. return true;
  196. }
  197. bool CCallback::visitTownBuilding(const CGTownInstance *town, BuildingID buildingID)
  198. {
  199. if(town->tempOwner!=getPlayerID())
  200. return false;
  201. VisitTownBuilding pack(town->id, buildingID);
  202. sendRequest(pack);
  203. return true;
  204. }
  205. void CCallback::spellResearch( const CGTownInstance *town, SpellID spellAtSlot, bool accepted )
  206. {
  207. SpellResearch pack(town->id, spellAtSlot, accepted);
  208. sendRequest(pack);
  209. }
  210. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  211. {
  212. if(town->tempOwner == getPlayerID() || (town->getGarrisonHero() && town->getGarrisonHero()->tempOwner == getPlayerID() ))
  213. {
  214. GarrisonHeroSwap pack(town->id);
  215. sendRequest(pack);
  216. }
  217. }
  218. void CCallback::buyArtifact(const CGHeroInstance *hero, ArtifactID aid)
  219. {
  220. if(hero->tempOwner != getPlayerID()) return;
  221. BuyArtifact pack(hero->id,aid);
  222. sendRequest(pack);
  223. }
  224. void CCallback::trade(const ObjectInstanceID marketId, EMarketMode mode, TradeItemSell id1, TradeItemBuy id2, ui32 val1, const CGHeroInstance * hero)
  225. {
  226. trade(marketId, mode, std::vector(1, id1), std::vector(1, id2), std::vector(1, val1), hero);
  227. }
  228. void CCallback::trade(const ObjectInstanceID marketId, EMarketMode mode, const std::vector<TradeItemSell> & id1, const std::vector<TradeItemBuy> & id2, const std::vector<ui32> & val1, const CGHeroInstance * hero)
  229. {
  230. TradeOnMarketplace pack;
  231. pack.marketId = marketId;
  232. pack.heroId = hero ? hero->id : ObjectInstanceID();
  233. pack.mode = mode;
  234. pack.r1 = id1;
  235. pack.r2 = id2;
  236. pack.val = val1;
  237. sendRequest(pack);
  238. }
  239. void CCallback::setFormation(const CGHeroInstance * hero, EArmyFormation mode)
  240. {
  241. SetFormation pack(hero->id, mode);
  242. sendRequest(pack);
  243. }
  244. void CCallback::recruitHero(const CGObjectInstance *townOrTavern, const CGHeroInstance *hero, const HeroTypeID & nextHero)
  245. {
  246. assert(townOrTavern);
  247. assert(hero);
  248. HireHero pack(hero->getHeroTypeID(), townOrTavern->id, nextHero);
  249. pack.player = *getPlayerID();
  250. sendRequest(pack);
  251. }
  252. void CCallback::saveLocalState(const JsonNode & data)
  253. {
  254. SaveLocalState state;
  255. state.data = data;
  256. state.player = *getPlayerID();
  257. sendRequest(state);
  258. }
  259. void CCallback::save( const std::string &fname )
  260. {
  261. SaveGame save_game(fname);
  262. sendRequest(save_game);
  263. }
  264. void CCallback::gamePause(bool pause)
  265. {
  266. if(pause)
  267. {
  268. GamePause pack;
  269. pack.player = *getPlayerID();
  270. sendRequest(pack);
  271. }
  272. else
  273. {
  274. sendQueryReply(0, QueryID::CLIENT);
  275. }
  276. }
  277. void CCallback::sendMessage(const std::string &mess, const CGObjectInstance * currentObject)
  278. {
  279. ASSERT_IF_CALLED_WITH_PLAYER
  280. PlayerMessage pm(mess, currentObject? currentObject->id : ObjectInstanceID(-1));
  281. if(getPlayerID())
  282. pm.player = *getPlayerID();
  283. sendRequest(pm);
  284. }
  285. void CCallback::buildBoat( const IShipyard *obj )
  286. {
  287. BuildBoat bb;
  288. bb.objid = dynamic_cast<const CGObjectInstance*>(obj)->id;
  289. sendRequest(bb);
  290. }
  291. CCallback::CCallback(std::shared_ptr<CGameState> gamestate, std::optional<PlayerColor> Player, IClient * C)
  292. : CBattleCallback(Player, C)
  293. , gamestate(gamestate)
  294. {
  295. waitTillRealize = false;
  296. unlockGsWhenWaiting = false;
  297. }
  298. CCallback::~CCallback() = default;
  299. bool CCallback::canMoveBetween(const int3 &a, const int3 &b)
  300. {
  301. //bidirectional
  302. return gameState().getMap().canMoveBetween(a, b);
  303. }
  304. std::optional<PlayerColor> CCallback::getPlayerID() const
  305. {
  306. return CBattleCallback::getPlayerID();
  307. }
  308. int3 CCallback::getGuardingCreaturePosition(int3 tile)
  309. {
  310. if (!gameState().getMap().isInTheMap(tile))
  311. return int3(-1,-1,-1);
  312. return gameState().getMap().guardingCreaturePositions[tile.z][tile.x][tile.y];
  313. }
  314. void CCallback::dig( const CGObjectInstance *hero )
  315. {
  316. DigWithHero dwh;
  317. dwh.id = hero->id;
  318. sendRequest(dwh);
  319. }
  320. void CCallback::castSpell(const CGHeroInstance *hero, SpellID spellID, const int3 &pos)
  321. {
  322. CastAdvSpell cas;
  323. cas.hid = hero->id;
  324. cas.sid = spellID;
  325. cas.pos = pos;
  326. sendRequest(cas);
  327. }
  328. int CCallback::mergeOrSwapStacks(const CArmedInstance *s1, const CArmedInstance *s2, SlotID p1, SlotID p2)
  329. {
  330. if(s1->getCreature(p1) == s2->getCreature(p2))
  331. return mergeStacks(s1, s2, p1, p2);
  332. else
  333. return swapCreatures(s1, s2, p1, p2);
  334. }
  335. VCMI_LIB_NAMESPACE_END