NetPacksServer.cpp 10 KB

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