NetPacksServer.cpp 14 KB

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