NetPacksServer.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "queries/QueriesProcessor.h"
  17. #include "../lib/IGameCallback.h"
  18. #include "../lib/mapObjects/CGTownInstance.h"
  19. #include "../lib/gameState/CGameState.h"
  20. #include "../lib/battle/BattleInfo.h"
  21. #include "../lib/battle/BattleAction.h"
  22. #include "../lib/battle/Unit.h"
  23. #include "../lib/serializer/Connection.h"
  24. #include "../lib/spells/CSpellHandler.h"
  25. #include "../lib/spells/ISpellMechanics.h"
  26. #include "../lib/serializer/Cast.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::visitEndTurn(EndTurn & pack)
  34. {
  35. PlayerColor currentPlayer = gs.currentPlayer;
  36. if(pack.player != currentPlayer)
  37. {
  38. if(gh.getPlayerStatus(pack.player) == EPlayerStatus::INGAME)
  39. gh.throwAndComplain(&pack, "pack.player attempted to end turn for another pack.player!");
  40. logGlobal->debug("pack.player attempted to end turn after game over. Ignoring this request.");
  41. result = true;
  42. return;
  43. }
  44. gh.throwOnWrongPlayer(&pack, pack.player);
  45. if(gh.queries->topQuery(pack.player))
  46. gh.throwAndComplain(&pack, "Cannot end turn before resolving queries!");
  47. gh.states.setFlag(gs.currentPlayer, &PlayerStatus::makingTurn, false);
  48. result = true;
  49. }
  50. void ApplyGhNetPackVisitor::visitDismissHero(DismissHero & pack)
  51. {
  52. gh.throwOnWrongOwner(&pack, pack.hid);
  53. result = gh.removeObject(gh.getObj(pack.hid));
  54. }
  55. void ApplyGhNetPackVisitor::visitMoveHero(MoveHero & pack)
  56. {
  57. gh.throwOnWrongOwner(&pack, pack.hid);
  58. result = gh.moveHero(pack.hid, pack.dest, 0, pack.transit, gh.getPlayerAt(pack.c));
  59. }
  60. void ApplyGhNetPackVisitor::visitCastleTeleportHero(CastleTeleportHero & pack)
  61. {
  62. gh.throwOnWrongOwner(&pack, pack.hid);
  63. result = gh.teleportHero(pack.hid, pack.dest, pack.source, gh.getPlayerAt(pack.c));
  64. }
  65. void ApplyGhNetPackVisitor::visitArrangeStacks(ArrangeStacks & pack)
  66. {
  67. //checks for owning in the gh func
  68. result = gh.arrangeStacks(pack.id1, pack.id2, pack.what, pack.p1, pack.p2, pack.val, gh.getPlayerAt(pack.c));
  69. }
  70. void ApplyGhNetPackVisitor::visitBulkMoveArmy(BulkMoveArmy & pack)
  71. {
  72. result = gh.bulkMoveArmy(pack.srcArmy, pack.destArmy, pack.srcSlot);
  73. }
  74. void ApplyGhNetPackVisitor::visitBulkSplitStack(BulkSplitStack & pack)
  75. {
  76. result = gh.bulkSplitStack(pack.src, pack.srcOwner, pack.amount);
  77. }
  78. void ApplyGhNetPackVisitor::visitBulkMergeStacks(BulkMergeStacks & pack)
  79. {
  80. result = gh.bulkMergeStacks(pack.src, pack.srcOwner);
  81. }
  82. void ApplyGhNetPackVisitor::visitBulkSmartSplitStack(BulkSmartSplitStack & pack)
  83. {
  84. result = gh.bulkSmartSplitStack(pack.src, pack.srcOwner);
  85. }
  86. void ApplyGhNetPackVisitor::visitDisbandCreature(DisbandCreature & pack)
  87. {
  88. gh.throwOnWrongOwner(&pack, pack.id);
  89. result = gh.disbandCreature(pack.id, pack.pos);
  90. }
  91. void ApplyGhNetPackVisitor::visitBuildStructure(BuildStructure & pack)
  92. {
  93. gh.throwOnWrongOwner(&pack, pack.tid);
  94. result = gh.buildStructure(pack.tid, pack.bid);
  95. }
  96. void ApplyGhNetPackVisitor::visitRecruitCreatures(RecruitCreatures & pack)
  97. {
  98. result = gh.recruitCreatures(pack.tid, pack.dst, pack.crid, pack.amount, pack.level);
  99. }
  100. void ApplyGhNetPackVisitor::visitUpgradeCreature(UpgradeCreature & pack)
  101. {
  102. gh.throwOnWrongOwner(&pack, pack.id);
  103. result = gh.upgradeCreature(pack.id, pack.pos, pack.cid);
  104. }
  105. void ApplyGhNetPackVisitor::visitGarrisonHeroSwap(GarrisonHeroSwap & pack)
  106. {
  107. const CGTownInstance * town = gh.getTown(pack.tid);
  108. if(!gh.isPlayerOwns(&pack, pack.tid) && !(town->garrisonHero && gh.isPlayerOwns(&pack, town->garrisonHero->id)))
  109. gh.throwNotAllowedAction(&pack); //neither town nor garrisoned hero (if present) is ours
  110. result = gh.garrisonSwap(pack.tid);
  111. }
  112. void ApplyGhNetPackVisitor::visitExchangeArtifacts(ExchangeArtifacts & pack)
  113. {
  114. gh.throwOnWrongPlayer(&pack, pack.src.owningPlayer()); //second hero can be ally
  115. result = gh.moveArtifact(pack.src, pack.dst);
  116. }
  117. void ApplyGhNetPackVisitor::visitBulkExchangeArtifacts(BulkExchangeArtifacts & pack)
  118. {
  119. const CGHeroInstance * pSrcHero = gh.getHero(pack.srcHero);
  120. gh.throwOnWrongPlayer(&pack, pSrcHero->getOwner());
  121. result = gh.bulkMoveArtifacts(pack.srcHero, pack.dstHero, pack.swap);
  122. }
  123. void ApplyGhNetPackVisitor::visitAssembleArtifacts(AssembleArtifacts & pack)
  124. {
  125. gh.throwOnWrongOwner(&pack, pack.heroID);
  126. result = gh.assembleArtifacts(pack.heroID, pack.artifactSlot, pack.assemble, pack.assembleTo);
  127. }
  128. void ApplyGhNetPackVisitor::visitEraseArtifactByClient(EraseArtifactByClient & pack)
  129. {
  130. gh.throwOnWrongPlayer(&pack, pack.al.owningPlayer());
  131. result = gh.eraseArtifactByClient(pack.al);
  132. }
  133. void ApplyGhNetPackVisitor::visitBuyArtifact(BuyArtifact & pack)
  134. {
  135. gh.throwOnWrongOwner(&pack, pack.hid);
  136. result = gh.buyArtifact(pack.hid, pack.aid);
  137. }
  138. void ApplyGhNetPackVisitor::visitTradeOnMarketplace(TradeOnMarketplace & pack)
  139. {
  140. const CGObjectInstance * market = gh.getObj(pack.marketId);
  141. if(!market)
  142. gh.throwAndComplain(&pack, "Invalid market object");
  143. const CGHeroInstance * hero = gh.getHero(pack.heroId);
  144. //market must be owned or visited
  145. const IMarket * m = IMarket::castFrom(market);
  146. if(!m)
  147. gh.throwAndComplain(&pack, "market is not-a-market! :/");
  148. PlayerColor player = market->tempOwner;
  149. if(player >= PlayerColor::PLAYER_LIMIT)
  150. player = gh.getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
  151. if(player >= PlayerColor::PLAYER_LIMIT)
  152. gh.throwAndComplain(&pack, "No player can use this market!");
  153. bool allyTownSkillTrade = (pack.mode == EMarketMode::RESOURCE_SKILL && gh.getPlayerRelations(player, hero->tempOwner) == PlayerRelations::ALLIES);
  154. if(hero && (!(player == hero->tempOwner || allyTownSkillTrade)
  155. || hero->visitablePos() != market->visitablePos()))
  156. gh.throwAndComplain(&pack, "This hero can't use this marketplace!");
  157. if(!allyTownSkillTrade)
  158. gh.throwOnWrongPlayer(&pack, player);
  159. result = true;
  160. switch(pack.mode)
  161. {
  162. case EMarketMode::RESOURCE_RESOURCE:
  163. for(int i = 0; i < pack.r1.size(); ++i)
  164. result &= gh.tradeResources(m, pack.val[i], player, pack.r1[i], pack.r2[i]);
  165. break;
  166. case EMarketMode::RESOURCE_PLAYER:
  167. for(int i = 0; i < pack.r1.size(); ++i)
  168. result &= gh.sendResources(pack.val[i], player, GameResID(pack.r1[i]), PlayerColor(pack.r2[i]));
  169. break;
  170. case EMarketMode::CREATURE_RESOURCE:
  171. for(int i = 0; i < pack.r1.size(); ++i)
  172. result &= gh.sellCreatures(pack.val[i], m, hero, SlotID(pack.r1[i]), GameResID(pack.r2[i]));
  173. break;
  174. case EMarketMode::RESOURCE_ARTIFACT:
  175. for(int i = 0; i < pack.r1.size(); ++i)
  176. result &= gh.buyArtifact(m, hero, GameResID(pack.r1[i]), ArtifactID(pack.r2[i]));
  177. break;
  178. case EMarketMode::ARTIFACT_RESOURCE:
  179. for(int i = 0; i < pack.r1.size(); ++i)
  180. result &= gh.sellArtifact(m, hero, ArtifactInstanceID(pack.r1[i]), GameResID(pack.r2[i]));
  181. break;
  182. case EMarketMode::CREATURE_UNDEAD:
  183. for(int i = 0; i < pack.r1.size(); ++i)
  184. result &= gh.transformInUndead(m, hero, SlotID(pack.r1[i]));
  185. break;
  186. case EMarketMode::RESOURCE_SKILL:
  187. for(int i = 0; i < pack.r2.size(); ++i)
  188. result &= gh.buySecSkill(m, hero, SecondarySkill(pack.r2[i]));
  189. break;
  190. case EMarketMode::CREATURE_EXP:
  191. {
  192. std::vector<SlotID> slotIDs(pack.r1.begin(), pack.r1.end());
  193. std::vector<ui32> count(pack.val.begin(), pack.val.end());
  194. result = gh.sacrificeCreatures(m, hero, slotIDs, count);
  195. return;
  196. }
  197. case EMarketMode::ARTIFACT_EXP:
  198. {
  199. std::vector<ArtifactPosition> positions(pack.r1.begin(), pack.r1.end());
  200. result = gh.sacrificeArtifact(m, hero, positions);
  201. return;
  202. }
  203. default:
  204. gh.throwAndComplain(&pack, "Unknown exchange pack.mode!");
  205. }
  206. }
  207. void ApplyGhNetPackVisitor::visitSetFormation(SetFormation & pack)
  208. {
  209. gh.throwOnWrongOwner(&pack, pack.hid);
  210. result = gh.setFormation(pack.hid, pack.formation);
  211. }
  212. void ApplyGhNetPackVisitor::visitHireHero(HireHero & pack)
  213. {
  214. if (!gh.hasPlayerAt(pack.player, pack.c))
  215. gh.throwAndComplain(&pack, "No such pack.player!");
  216. result = gh.heroPool->hireHero(pack.tid, pack.hid, pack.player);
  217. }
  218. void ApplyGhNetPackVisitor::visitBuildBoat(BuildBoat & pack)
  219. {
  220. if(gh.getPlayerRelations(gh.getOwner(pack.objid), gh.getPlayerAt(pack.c)) == PlayerRelations::ENEMIES)
  221. gh.throwAndComplain(&pack, "Can't build boat at enemy shipyard");
  222. result = gh.buildBoat(pack.objid, gh.getPlayerAt(pack.c));
  223. }
  224. void ApplyGhNetPackVisitor::visitQueryReply(QueryReply & pack)
  225. {
  226. auto playerToConnection = gh.connections.find(pack.player);
  227. if(playerToConnection == gh.connections.end())
  228. gh.throwAndComplain(&pack, "No such pack.player!");
  229. if(!vstd::contains(playerToConnection->second, pack.c))
  230. gh.throwAndComplain(&pack, "Message came from wrong connection!");
  231. if(pack.qid == QueryID(-1))
  232. gh.throwAndComplain(&pack, "Cannot answer the query with pack.id -1!");
  233. assert(vstd::contains(gh.states.players, pack.player));
  234. result = gh.queryReply(pack.qid, pack.reply, pack.player);
  235. }
  236. void ApplyGhNetPackVisitor::visitMakeAction(MakeAction & pack)
  237. {
  238. if (!gh.hasPlayerAt(pack.player, pack.c))
  239. gh.throwAndComplain(&pack, "No such pack.player!");
  240. result = gh.battles->makeBattleAction(pack.player, pack.ba);
  241. }
  242. void ApplyGhNetPackVisitor::visitDigWithHero(DigWithHero & pack)
  243. {
  244. gh.throwOnWrongOwner(&pack, pack.id);
  245. result = gh.dig(gh.getHero(pack.id));
  246. }
  247. void ApplyGhNetPackVisitor::visitCastAdvSpell(CastAdvSpell & pack)
  248. {
  249. gh.throwOnWrongOwner(&pack, pack.hid);
  250. const CSpell * s = pack.sid.toSpell();
  251. if(!s)
  252. gh.throwNotAllowedAction(&pack);
  253. const CGHeroInstance * h = gh.getHero(pack.hid);
  254. if(!h)
  255. gh.throwNotAllowedAction(&pack);
  256. AdventureSpellCastParameters p;
  257. p.caster = h;
  258. p.pos = pack.pos;
  259. result = s->adventureCast(gh.spellEnv, p);
  260. }
  261. void ApplyGhNetPackVisitor::visitPlayerMessage(PlayerMessage & pack)
  262. {
  263. if(!pack.player.isSpectator()) // TODO: clearly not a great way to verify permissions
  264. gh.throwOnWrongPlayer(&pack, pack.player);
  265. gh.playerMessages->playerMessage(pack.player, pack.text, pack.currObj);
  266. result = true;
  267. }