NetPacksServer.cpp 14 KB

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