NetPacksServer.cpp 12 KB

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