NetPacksServer.cpp 13 KB

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