NetPacksServer.cpp 12 KB

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