NetPacksServer.cpp 12 KB

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