NetPacksServer.cpp 11 KB

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