NetPacksServer.cpp 9.9 KB

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