NetPacksServer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * NetPacksServer.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 "ServerNetPackVisitors.h"
  12. #include "CGameHandler.h"
  13. #include "battles/BattleProcessor.h"
  14. #include "processors/HeroPoolProcessor.h"
  15. #include "processors/PlayerMessageProcessor.h"
  16. #include "processors/TurnOrderProcessor.h"
  17. #include "queries/QueriesProcessor.h"
  18. #include "queries/MapQueries.h"
  19. #include "../lib/CPlayerState.h"
  20. #include "../lib/mapObjects/CGTownInstance.h"
  21. #include "../lib/mapObjects/CGHeroInstance.h"
  22. #include "../lib/gameState/CGameState.h"
  23. #include "../lib/battle/IBattleState.h"
  24. #include "../lib/battle/BattleAction.h"
  25. #include "../lib/battle/Unit.h"
  26. #include "../lib/spells/CSpellHandler.h"
  27. #include "../lib/spells/ISpellMechanics.h"
  28. void ApplyGhNetPackVisitor::visitSaveGame(SaveGame & pack)
  29. {
  30. gh.save(pack.fname);
  31. logGlobal->info("Game has been saved as %s", pack.fname);
  32. result = true;
  33. }
  34. void ApplyGhNetPackVisitor::visitGamePause(GamePause & pack)
  35. {
  36. auto turnQuery = std::make_shared<TimerPauseQuery>(&gh, pack.player);
  37. turnQuery->queryID = QueryID::CLIENT;
  38. gh.queries->addQuery(turnQuery);
  39. result = true;
  40. }
  41. void ApplyGhNetPackVisitor::visitEndTurn(EndTurn & pack)
  42. {
  43. gh.throwIfWrongPlayer(connection, &pack);
  44. gh.throwIfPlayerNotActive(connection, &pack);
  45. result = gh.turnOrder->onPlayerEndsTurn(pack.player);
  46. }
  47. void ApplyGhNetPackVisitor::visitDismissHero(DismissHero & pack)
  48. {
  49. gh.throwIfWrongOwner(connection, &pack, pack.hid);
  50. gh.throwIfPlayerNotActive(connection, &pack);
  51. result = gh.removeObject(gh.gameInfo().getObj(pack.hid), pack.player);
  52. }
  53. void ApplyGhNetPackVisitor::visitMoveHero(MoveHero & pack)
  54. {
  55. gh.throwIfWrongOwner(connection, &pack, pack.hid);
  56. gh.throwIfPlayerNotActive(connection, &pack);
  57. for (auto const & dest : pack.path)
  58. {
  59. if (!gh.moveHero(pack.hid, dest, EMovementMode::STANDARD, pack.transit, pack.player))
  60. {
  61. result = false;
  62. return;
  63. }
  64. // player got some query he has to reply to first for example, from triggered event
  65. // ignore remaining path (if any), but handle this as success - since at least part of path was legal & was applied
  66. auto query = gh.queries->topQuery(pack.player);
  67. if (query && query->blocksPack(&pack))
  68. {
  69. result = true;
  70. return;
  71. }
  72. }
  73. result = true;
  74. }
  75. void ApplyGhNetPackVisitor::visitCastleTeleportHero(CastleTeleportHero & pack)
  76. {
  77. gh.throwIfWrongOwner(connection, &pack, pack.hid);
  78. gh.throwIfPlayerNotActive(connection, &pack);
  79. result = gh.teleportHero(pack.hid, pack.dest, pack.source, pack.player);
  80. }
  81. void ApplyGhNetPackVisitor::visitArrangeStacks(ArrangeStacks & pack)
  82. {
  83. gh.throwIfWrongPlayer(connection, &pack);
  84. gh.throwIfPlayerNotActive(connection, &pack);
  85. result = gh.arrangeStacks(pack.id1, pack.id2, pack.what, pack.p1, pack.p2, pack.val, pack.player);
  86. }
  87. void ApplyGhNetPackVisitor::visitBulkMoveArmy(BulkMoveArmy & pack)
  88. {
  89. gh.throwIfWrongOwner(connection, &pack, pack.srcArmy);
  90. gh.throwIfPlayerNotActive(connection, &pack);
  91. result = gh.bulkMoveArmy(pack.srcArmy, pack.destArmy, pack.srcSlot);
  92. }
  93. void ApplyGhNetPackVisitor::visitBulkSplitStack(BulkSplitStack & pack)
  94. {
  95. gh.throwIfWrongPlayer(connection, &pack);
  96. gh.throwIfPlayerNotActive(connection, &pack);
  97. result = gh.bulkSplitStack(pack.src, pack.srcOwner, pack.amount);
  98. }
  99. void ApplyGhNetPackVisitor::visitBulkMergeStacks(BulkMergeStacks & pack)
  100. {
  101. gh.throwIfWrongPlayer(connection, &pack);
  102. gh.throwIfPlayerNotActive(connection, &pack);
  103. result = gh.bulkMergeStacks(pack.src, pack.srcOwner);
  104. }
  105. void ApplyGhNetPackVisitor::visitBulkSplitAndRebalanceStack(BulkSplitAndRebalanceStack & pack)
  106. {
  107. gh.throwIfWrongPlayer(connection, &pack);
  108. gh.throwIfPlayerNotActive(connection, &pack);
  109. result = gh.bulkSplitAndRebalanceStack(pack.src, pack.srcOwner);
  110. }
  111. void ApplyGhNetPackVisitor::visitDisbandCreature(DisbandCreature & pack)
  112. {
  113. gh.throwIfWrongOwner(connection, &pack, pack.id);
  114. gh.throwIfPlayerNotActive(connection, &pack);
  115. result = gh.disbandCreature(pack.id, pack.pos);
  116. }
  117. void ApplyGhNetPackVisitor::visitBuildStructure(BuildStructure & pack)
  118. {
  119. gh.throwIfWrongOwner(connection, &pack, pack.tid);
  120. gh.throwIfPlayerNotActive(connection, &pack);
  121. result = gh.buildStructure(pack.tid, pack.bid);
  122. }
  123. void ApplyGhNetPackVisitor::visitSpellResearch(SpellResearch & pack)
  124. {
  125. gh.throwIfWrongOwner(connection, &pack, pack.tid);
  126. gh.throwIfPlayerNotActive(connection, &pack);
  127. result = gh.spellResearch(pack.tid, pack.spellAtSlot, pack.accepted);
  128. }
  129. void ApplyGhNetPackVisitor::visitVisitTownBuilding(VisitTownBuilding & pack)
  130. {
  131. gh.throwIfWrongOwner(connection, &pack, pack.tid);
  132. gh.throwIfPlayerNotActive(connection, &pack);
  133. result = gh.visitTownBuilding(pack.tid, pack.bid);
  134. }
  135. void ApplyGhNetPackVisitor::visitRecruitCreatures(RecruitCreatures & pack)
  136. {
  137. gh.throwIfWrongPlayer(connection, &pack);
  138. gh.throwIfPlayerNotActive(connection, &pack);
  139. // ownership checks are inside recruitCreatures
  140. result = gh.recruitCreatures(pack.tid, pack.dst, pack.crid, pack.amount, pack.level, pack.player);
  141. }
  142. void ApplyGhNetPackVisitor::visitUpgradeCreature(UpgradeCreature & pack)
  143. {
  144. gh.throwIfWrongOwner(connection, &pack, pack.id);
  145. gh.throwIfPlayerNotActive(connection, &pack);
  146. result = gh.upgradeCreature(pack.id, pack.pos, pack.cid);
  147. }
  148. void ApplyGhNetPackVisitor::visitGarrisonHeroSwap(GarrisonHeroSwap & pack)
  149. {
  150. const CGTownInstance * town = gh.gameInfo().getTown(pack.tid);
  151. if(!gh.isPlayerOwns(connection, &pack, pack.tid) && !(town->getGarrisonHero() && gh.isPlayerOwns(connection, &pack, town->getGarrisonHero()->id)))
  152. gh.throwNotAllowedAction(connection); //neither town nor garrisoned hero (if present) is ours
  153. gh.throwIfPlayerNotActive(connection, &pack);
  154. result = gh.garrisonSwap(pack.tid);
  155. }
  156. void ApplyGhNetPackVisitor::visitExchangeArtifacts(ExchangeArtifacts & pack)
  157. {
  158. if(gh.gameInfo().getHero(pack.src.artHolder))
  159. gh.throwIfWrongPlayer(connection, &pack, gh.gameState().getOwner(pack.src.artHolder)); //second hero can be ally
  160. gh.throwIfPlayerNotActive(connection, &pack);
  161. result = gh.moveArtifact(pack.player, pack.src, pack.dst);
  162. }
  163. void ApplyGhNetPackVisitor::visitBulkExchangeArtifacts(BulkExchangeArtifacts & pack)
  164. {
  165. if(gh.gameState().getMarket(pack.srcHero) == nullptr)
  166. gh.throwIfWrongOwner(connection, &pack, pack.srcHero);
  167. if(pack.swap)
  168. gh.throwIfWrongOwner(connection, &pack, pack.dstHero);
  169. gh.throwIfPlayerNotActive(connection, &pack);
  170. result = gh.bulkMoveArtifacts(pack.player, pack.srcHero, pack.dstHero, pack.swap, pack.equipped, pack.backpack);
  171. }
  172. void ApplyGhNetPackVisitor::visitManageBackpackArtifacts(ManageBackpackArtifacts & pack)
  173. {
  174. gh.throwIfPlayerNotActive(connection, &pack);
  175. if(gh.gameInfo().getPlayerRelations(pack.player, gh.gameState().getOwner(pack.artHolder)) != PlayerRelations::ENEMIES)
  176. result = gh.manageBackpackArtifacts(pack.player, pack.artHolder, pack.cmd);
  177. }
  178. void ApplyGhNetPackVisitor::visitManageEquippedArtifacts(ManageEquippedArtifacts & pack)
  179. {
  180. gh.throwIfWrongOwner(connection, &pack, pack.artHolder);
  181. if(pack.saveCostume)
  182. result = gh.saveArtifactsCostume(pack.player, pack.artHolder, pack.costumeIdx);
  183. else
  184. result = gh.switchArtifactsCostume(pack.player, pack.artHolder, pack.costumeIdx);
  185. }
  186. void ApplyGhNetPackVisitor::visitAssembleArtifacts(AssembleArtifacts & pack)
  187. {
  188. gh.throwIfWrongOwner(connection, &pack, pack.heroID);
  189. // gh.throwIfPlayerNotActive(&pack); // Might happen when player captures artifacts in battle?
  190. result = gh.assembleArtifacts(pack.heroID, pack.artifactSlot, pack.assemble, pack.assembleTo);
  191. }
  192. void ApplyGhNetPackVisitor::visitEraseArtifactByClient(EraseArtifactByClient & pack)
  193. {
  194. gh.throwIfWrongPlayer(connection, &pack, gh.gameState().getOwner(pack.al.artHolder));
  195. gh.throwIfPlayerNotActive(connection, &pack);
  196. result = gh.eraseArtifactByClient(pack.al);
  197. }
  198. void ApplyGhNetPackVisitor::visitBuyArtifact(BuyArtifact & pack)
  199. {
  200. gh.throwIfWrongOwner(connection, &pack, pack.hid);
  201. gh.throwIfPlayerNotActive(connection, &pack);
  202. result = gh.buyArtifact(pack.hid, pack.aid);
  203. }
  204. void ApplyGhNetPackVisitor::visitTradeOnMarketplace(TradeOnMarketplace & pack)
  205. {
  206. const CGObjectInstance * object = gh.gameInfo().getObj(pack.marketId);
  207. const CGHeroInstance * hero = gh.gameInfo().getHero(pack.heroId);
  208. const auto * market = gh.gameState().getMarket(pack.marketId);
  209. gh.throwIfWrongPlayer(connection, &pack);
  210. gh.throwIfPlayerNotActive(connection, &pack);
  211. if(!object)
  212. gh.throwAndComplain(connection, "Invalid market object");
  213. if(!market)
  214. gh.throwAndComplain(connection, "market is not-a-market! :/");
  215. bool heroCanBeInvalid = false;
  216. if (pack.mode == EMarketMode::RESOURCE_RESOURCE || pack.mode == EMarketMode::RESOURCE_PLAYER)
  217. {
  218. // For resource exchange we must use our own market or visit neutral market
  219. if (object->getOwner().isValidPlayer())
  220. {
  221. gh.throwIfWrongOwner(connection, &pack, pack.marketId);
  222. heroCanBeInvalid = true;
  223. }
  224. }
  225. if (pack.mode == EMarketMode::CREATURE_UNDEAD)
  226. {
  227. // For skeleton transformer, if hero is null then object must be owned
  228. if (!hero)
  229. {
  230. gh.throwIfWrongOwner(connection, &pack, pack.marketId);
  231. heroCanBeInvalid = true;
  232. }
  233. }
  234. if (!heroCanBeInvalid)
  235. {
  236. gh.throwIfWrongOwner(connection, &pack, pack.heroId);
  237. if (!hero)
  238. gh.throwAndComplain(connection, "Can not trade - no hero!");
  239. // TODO: check that object is actually being visited (e.g. Query exists)
  240. if (!object->visitableAt(hero->visitablePos()))
  241. gh.throwAndComplain(connection, "Can not trade - object not visited!");
  242. if (object->getOwner().isValidPlayer() && gh.gameInfo().getPlayerRelations(object->getOwner(), hero->getOwner()) == PlayerRelations::ENEMIES)
  243. gh.throwAndComplain(connection, "Can not trade - market not owned!");
  244. }
  245. result = true;
  246. switch(pack.mode)
  247. {
  248. case EMarketMode::RESOURCE_RESOURCE:
  249. for(int i = 0; i < pack.r1.size(); ++i)
  250. result &= gh.tradeResources(market, pack.val[i], pack.player, pack.r1[i].as<GameResID>(), pack.r2[i].as<GameResID>());
  251. break;
  252. case EMarketMode::RESOURCE_PLAYER:
  253. for(int i = 0; i < pack.r1.size(); ++i)
  254. result &= gh.sendResources(pack.val[i], pack.player, pack.r1[i].as<GameResID>(), pack.r2[i].as<PlayerColor>());
  255. break;
  256. case EMarketMode::CREATURE_RESOURCE:
  257. for(int i = 0; i < pack.r1.size(); ++i)
  258. result &= gh.sellCreatures(pack.val[i], market, hero, pack.r1[i].as<SlotID>(), pack.r2[i].as<GameResID>());
  259. break;
  260. case EMarketMode::RESOURCE_ARTIFACT:
  261. for(int i = 0; i < pack.r1.size(); ++i)
  262. result &= gh.buyArtifact(market, hero, pack.r1[i].as<GameResID>(), pack.r2[i].as<ArtifactID>());
  263. break;
  264. case EMarketMode::ARTIFACT_RESOURCE:
  265. for(int i = 0; i < pack.r1.size(); ++i)
  266. result &= gh.sellArtifact(market, hero, pack.r1[i].as<ArtifactInstanceID>(), pack.r2[i].as<GameResID>());
  267. break;
  268. case EMarketMode::CREATURE_UNDEAD:
  269. for(int i = 0; i < pack.r1.size(); ++i)
  270. result &= gh.transformInUndead(market, hero, pack.r1[i].as<SlotID>());
  271. break;
  272. case EMarketMode::RESOURCE_SKILL:
  273. for(int i = 0; i < pack.r2.size(); ++i)
  274. result &= gh.buySecSkill(market, hero, pack.r2[i].as<SecondarySkill>());
  275. break;
  276. case EMarketMode::CREATURE_EXP:
  277. {
  278. std::vector<SlotID> slotIDs;
  279. std::vector<ui32> count(pack.val.begin(), pack.val.end());
  280. for(auto const & slot : pack.r1)
  281. slotIDs.push_back(slot.as<SlotID>());
  282. result = gh.sacrificeCreatures(market, hero, slotIDs, count);
  283. return;
  284. }
  285. case EMarketMode::ARTIFACT_EXP:
  286. {
  287. std::vector<ArtifactInstanceID> positions;
  288. for(auto const & artInstId : pack.r1)
  289. positions.push_back(artInstId.as<ArtifactInstanceID>());
  290. result = gh.sacrificeArtifact(market, hero, positions);
  291. return;
  292. }
  293. default:
  294. gh.throwAndComplain(connection, "Unknown exchange pack.mode!");
  295. }
  296. }
  297. void ApplyGhNetPackVisitor::visitSetFormation(SetFormation & pack)
  298. {
  299. gh.throwIfWrongOwner(connection, &pack, pack.hid);
  300. gh.throwIfPlayerNotActive(connection, &pack);
  301. result = gh.setFormation(pack.hid, pack.formation);
  302. }
  303. void ApplyGhNetPackVisitor::visitHireHero(HireHero & pack)
  304. {
  305. gh.throwIfWrongPlayer(connection, &pack);
  306. gh.throwIfPlayerNotActive(connection, &pack);
  307. result = gh.heroPool->hireHero(pack.tid, pack.hid, pack.player, pack.nhid);
  308. }
  309. void ApplyGhNetPackVisitor::visitBuildBoat(BuildBoat & pack)
  310. {
  311. gh.throwIfWrongPlayer(connection, &pack);
  312. gh.throwIfPlayerNotActive(connection, &pack);
  313. if(gh.gameInfo().getPlayerRelations(gh.gameState().getOwner(pack.objid), pack.player) == PlayerRelations::ENEMIES)
  314. gh.throwAndComplain(connection, "Can't build boat at enemy shipyard");
  315. result = gh.buildBoat(pack.objid, pack.player);
  316. }
  317. void ApplyGhNetPackVisitor::visitQueryReply(QueryReply & pack)
  318. {
  319. gh.throwIfWrongPlayer(connection, &pack);
  320. if(pack.qid == QueryID(-1))
  321. gh.throwAndComplain(connection, "Cannot answer the query with pack.id -1!");
  322. result = gh.queryReply(pack.qid, pack.reply, pack.player);
  323. }
  324. void ApplyGhNetPackVisitor::visitSaveLocalState(SaveLocalState & pack)
  325. {
  326. gh.throwIfWrongPlayer(connection, &pack);
  327. *gh.gameState().getPlayerState(pack.player)->playerLocalSettings = pack.data;
  328. result = true;
  329. }
  330. void ApplyGhNetPackVisitor::visitMakeAction(MakeAction & pack)
  331. {
  332. gh.throwIfWrongPlayer(connection, &pack);
  333. // allowed even if it is not our turn - will be filtered by battle sides
  334. result = gh.battles->makePlayerBattleAction(pack.battleID, pack.player, pack.ba);
  335. }
  336. void ApplyGhNetPackVisitor::visitDigWithHero(DigWithHero & pack)
  337. {
  338. gh.throwIfWrongOwner(connection, &pack, pack.id);
  339. gh.throwIfPlayerNotActive(connection, &pack);
  340. result = gh.dig(gh.gameInfo().getHero(pack.id));
  341. }
  342. void ApplyGhNetPackVisitor::visitCastAdvSpell(CastAdvSpell & pack)
  343. {
  344. gh.throwIfWrongOwner(connection, &pack, pack.hid);
  345. gh.throwIfPlayerNotActive(connection, &pack);
  346. if (!pack.sid.hasValue())
  347. gh.throwNotAllowedAction(connection);
  348. const CGHeroInstance * h = gh.gameInfo().getHero(pack.hid);
  349. if(!h)
  350. gh.throwNotAllowedAction(connection);
  351. AdventureSpellCastParameters p;
  352. p.caster = h;
  353. p.pos = pack.pos;
  354. const CSpell * s = pack.sid.toSpell();
  355. result = s->adventureCast(gh.spellEnv, p);
  356. }
  357. void ApplyGhNetPackVisitor::visitPlayerMessage(PlayerMessage & pack)
  358. {
  359. if(!pack.player.isSpectator()) // TODO: clearly not a great way to verify permissions
  360. gh.throwIfWrongPlayer(connection, &pack, pack.player);
  361. gh.playerMessages->playerMessage(pack.player, pack.text, pack.currObj);
  362. result = true;
  363. }