NetPacksServer.cpp 12 KB

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