NetPacksServer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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/mapping/CMap.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::visitBuyArtifact(BuyArtifact & pack)
  125. {
  126. gh.throwOnWrongOwner(&pack, pack.hid);
  127. result = gh.buyArtifact(pack.hid, pack.aid);
  128. }
  129. void ApplyGhNetPackVisitor::visitTradeOnMarketplace(TradeOnMarketplace & pack)
  130. {
  131. const CGObjectInstance * market = gh.getObj(pack.marketId);
  132. if(!market)
  133. gh.throwAndComplain(&pack, "Invalid market object");
  134. const CGHeroInstance * hero = gh.getHero(pack.heroId);
  135. //market must be owned or visited
  136. const IMarket * m = IMarket::castFrom(market);
  137. if(!m)
  138. gh.throwAndComplain(&pack, "market is not-a-market! :/");
  139. PlayerColor player = market->tempOwner;
  140. if(player >= PlayerColor::PLAYER_LIMIT)
  141. player = gh.getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
  142. if(player >= PlayerColor::PLAYER_LIMIT)
  143. gh.throwAndComplain(&pack, "No player can use this market!");
  144. bool allyTownSkillTrade = (pack.mode == EMarketMode::RESOURCE_SKILL && gh.getPlayerRelations(player, hero->tempOwner) == PlayerRelations::ALLIES);
  145. if(hero && (!(player == hero->tempOwner || allyTownSkillTrade)
  146. || hero->visitablePos() != market->visitablePos()))
  147. gh.throwAndComplain(&pack, "This hero can't use this marketplace!");
  148. if(!allyTownSkillTrade)
  149. gh.throwOnWrongPlayer(&pack, player);
  150. result = true;
  151. switch(pack.mode)
  152. {
  153. case EMarketMode::RESOURCE_RESOURCE:
  154. for(int i = 0; i < pack.r1.size(); ++i)
  155. result &= gh.tradeResources(m, pack.val[i], player, pack.r1[i], pack.r2[i]);
  156. break;
  157. case EMarketMode::RESOURCE_PLAYER:
  158. for(int i = 0; i < pack.r1.size(); ++i)
  159. result &= gh.sendResources(pack.val[i], player, static_cast<Res::ERes>(pack.r1[i]), PlayerColor(pack.r2[i]));
  160. break;
  161. case EMarketMode::CREATURE_RESOURCE:
  162. for(int i = 0; i < pack.r1.size(); ++i)
  163. result &= gh.sellCreatures(pack.val[i], m, hero, SlotID(pack.r1[i]), static_cast<Res::ERes>(pack.r2[i]));
  164. break;
  165. case EMarketMode::RESOURCE_ARTIFACT:
  166. for(int i = 0; i < pack.r1.size(); ++i)
  167. result &= gh.buyArtifact(m, hero, static_cast<Res::ERes>(pack.r1[i]), ArtifactID(pack.r2[i]));
  168. break;
  169. case EMarketMode::ARTIFACT_RESOURCE:
  170. for(int i = 0; i < pack.r1.size(); ++i)
  171. result &= gh.sellArtifact(m, hero, ArtifactInstanceID(pack.r1[i]), static_cast<Res::ERes>(pack.r2[i]));
  172. break;
  173. case EMarketMode::CREATURE_UNDEAD:
  174. for(int i = 0; i < pack.r1.size(); ++i)
  175. result &= gh.transformInUndead(m, hero, SlotID(pack.r1[i]));
  176. break;
  177. case EMarketMode::RESOURCE_SKILL:
  178. for(int i = 0; i < pack.r2.size(); ++i)
  179. result &= gh.buySecSkill(m, hero, SecondarySkill(pack.r2[i]));
  180. break;
  181. case EMarketMode::CREATURE_EXP:
  182. {
  183. std::vector<SlotID> slotIDs(pack.r1.begin(), pack.r1.end());
  184. std::vector<ui32> count(pack.val.begin(), pack.val.end());
  185. result = gh.sacrificeCreatures(m, hero, slotIDs, count);
  186. return;
  187. }
  188. case EMarketMode::ARTIFACT_EXP:
  189. {
  190. std::vector<ArtifactPosition> positions(pack.r1.begin(), pack.r1.end());
  191. result = gh.sacrificeArtifact(m, hero, positions);
  192. return;
  193. }
  194. default:
  195. gh.throwAndComplain(&pack, "Unknown exchange pack.mode!");
  196. }
  197. }
  198. void ApplyGhNetPackVisitor::visitSetFormation(SetFormation & pack)
  199. {
  200. gh.throwOnWrongOwner(&pack, pack.hid);
  201. result = gh.setFormation(pack.hid, pack.formation);
  202. }
  203. void ApplyGhNetPackVisitor::visitHireHero(HireHero & pack)
  204. {
  205. const CGObjectInstance * obj = gh.getObj(pack.tid);
  206. const CGTownInstance * town = dynamic_ptr_cast<CGTownInstance>(obj);
  207. if(town && PlayerRelations::ENEMIES == gh.getPlayerRelations(obj->tempOwner, gh.getPlayerAt(pack.c)))
  208. gh.throwAndComplain(&pack, "Can't buy hero in enemy town!");
  209. result = gh.hireHero(obj, pack.hid, pack.player);
  210. }
  211. void ApplyGhNetPackVisitor::visitBuildBoat(BuildBoat & pack)
  212. {
  213. if(gh.getPlayerRelations(gh.getOwner(pack.objid), gh.getPlayerAt(pack.c)) == PlayerRelations::ENEMIES)
  214. gh.throwAndComplain(&pack, "Can't build boat at enemy shipyard");
  215. result = gh.buildBoat(pack.objid, gh.getPlayerAt(pack.c));
  216. }
  217. void ApplyGhNetPackVisitor::visitQueryReply(QueryReply & pack)
  218. {
  219. auto playerToConnection = gh.connections.find(pack.player);
  220. if(playerToConnection == gh.connections.end())
  221. gh.throwAndComplain(&pack, "No such pack.player!");
  222. if(!vstd::contains(playerToConnection->second, pack.c))
  223. gh.throwAndComplain(&pack, "Message came from wrong connection!");
  224. if(pack.qid == QueryID(-1))
  225. gh.throwAndComplain(&pack, "Cannot answer the query with pack.id -1!");
  226. assert(vstd::contains(gh.states.players, pack.player));
  227. result = gh.queryReply(pack.qid, pack.reply, pack.player);
  228. }
  229. void ApplyGhNetPackVisitor::visitMakeAction(MakeAction & pack)
  230. {
  231. const BattleInfo * b = gs.curB;
  232. if(!b)
  233. gh.throwNotAllowedAction(&pack);
  234. if(b->tacticDistance)
  235. {
  236. if(pack.ba.actionType != EActionType::WALK && pack.ba.actionType != EActionType::END_TACTIC_PHASE
  237. && pack.ba.actionType != EActionType::RETREAT && pack.ba.actionType != EActionType::SURRENDER)
  238. gh.throwNotAllowedAction(&pack);
  239. if(!vstd::contains(gh.connections[b->sides[b->tacticsSide].color], pack.c))
  240. gh.throwNotAllowedAction(&pack);
  241. }
  242. else
  243. {
  244. auto active = b->battleActiveUnit();
  245. if(!active)
  246. gh.throwNotAllowedAction(&pack);
  247. auto unitOwner = b->battleGetOwner(active);
  248. if(!vstd::contains(gh.connections[unitOwner], pack.c))
  249. gh.throwNotAllowedAction(&pack);
  250. }
  251. result = gh.makeBattleAction(pack.ba);
  252. }
  253. void ApplyGhNetPackVisitor::visitMakeCustomAction(MakeCustomAction & pack)
  254. {
  255. const BattleInfo * b = gs.curB;
  256. if(!b)
  257. gh.throwNotAllowedAction(&pack);
  258. if(b->tacticDistance)
  259. gh.throwNotAllowedAction(&pack);
  260. auto active = b->battleActiveUnit();
  261. if(!active)
  262. gh.throwNotAllowedAction(&pack);
  263. auto unitOwner = b->battleGetOwner(active);
  264. if(!vstd::contains(gh.connections[unitOwner], pack.c))
  265. gh.throwNotAllowedAction(&pack);
  266. if(pack.ba.actionType != EActionType::HERO_SPELL)
  267. gh.throwNotAllowedAction(&pack);
  268. result = gh.makeCustomAction(pack.ba);
  269. }
  270. void ApplyGhNetPackVisitor::visitDigWithHero(DigWithHero & pack)
  271. {
  272. gh.throwOnWrongOwner(&pack, pack.id);
  273. result = gh.dig(gh.getHero(pack.id));
  274. }
  275. void ApplyGhNetPackVisitor::visitCastAdvSpell(CastAdvSpell & pack)
  276. {
  277. gh.throwOnWrongOwner(&pack, pack.hid);
  278. const CSpell * s = pack.sid.toSpell();
  279. if(!s)
  280. gh.throwNotAllowedAction(&pack);
  281. const CGHeroInstance * h = gh.getHero(pack.hid);
  282. if(!h)
  283. gh.throwNotAllowedAction(&pack);
  284. AdventureSpellCastParameters p;
  285. p.caster = h;
  286. p.pos = pack.pos;
  287. result = s->adventureCast(gh.spellEnv, p);
  288. }
  289. void ApplyGhNetPackVisitor::visitPlayerMessage(PlayerMessage & pack)
  290. {
  291. if(!pack.player.isSpectator()) // TODO: clearly not a great way to verify permissions
  292. gh.throwOnWrongPlayer(&pack, pack.player);
  293. gh.playerMessage(pack.player, pack.text, pack.currObj);
  294. result = true;
  295. }