NetPacksServer.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "StdInc.h"
  2. #include "../lib/NetPacks.h"
  3. #include "CGameHandler.h"
  4. #include "../lib/IGameCallback.h"
  5. #include "../lib/mapping/CMap.h"
  6. #include "../lib/CGameState.h"
  7. #include "../lib/BattleState.h"
  8. #include "../lib/BattleAction.h"
  9. #include "../lib/serializer/Connection.h"
  10. #define PLAYER_OWNS(id) (gh->getPlayerAt(c)==gh->getOwner(id))
  11. #define ERROR_AND_RETURN \
  12. do { if(c) { \
  13. SystemMessage temp_message("You are not allowed to perform this action!"); \
  14. boost::unique_lock<boost::mutex> lock(*c->wmx); \
  15. *c << &temp_message; \
  16. } \
  17. logNetwork->error("Player is not allowed to perform this action!"); \
  18. return false;} while(0)
  19. #define WRONG_PLAYER_MSG(expectedplayer) do {std::ostringstream oss;\
  20. oss << "You were identified as player " << gh->getPlayerAt(c) << " while expecting " << expectedplayer;\
  21. logNetwork->error(oss.str()); \
  22. if(c) { SystemMessage temp_message(oss.str()); boost::unique_lock<boost::mutex> lock(*c->wmx); *c << &temp_message; } } while(0)
  23. #define ERROR_IF_NOT_OWNS(id) do{if(!PLAYER_OWNS(id)){WRONG_PLAYER_MSG(gh->getOwner(id)); ERROR_AND_RETURN; }}while(0)
  24. #define ERROR_IF_NOT(player) do{if(player != gh->getPlayerAt(c)){WRONG_PLAYER_MSG(player); ERROR_AND_RETURN; }}while(0)
  25. #define COMPLAIN_AND_RETURN(txt) { gh->complain(txt); ERROR_AND_RETURN; }
  26. /*
  27. * NetPacksServer.cpp, part of VCMI engine
  28. *
  29. * Authors: listed in file AUTHORS in main folder
  30. *
  31. * License: GNU General Public License v2.0 or later
  32. * Full text of license available in license.txt file, in main folder
  33. *
  34. */
  35. CGameState* CPackForServer::GS(CGameHandler *gh)
  36. {
  37. return gh->gs;
  38. }
  39. bool SaveGame::applyGh( CGameHandler *gh )
  40. {
  41. gh->save(fname);
  42. logGlobal->info("Game has been saved as %s", fname);
  43. return true;
  44. }
  45. bool CommitPackage::applyGh( CGameHandler *gh )
  46. {
  47. gh->sendAndApply(packToCommit);
  48. return true;
  49. }
  50. bool CloseServer::applyGh( CGameHandler *gh )
  51. {
  52. gh->close();
  53. return true;
  54. }
  55. bool EndTurn::applyGh( CGameHandler *gh )
  56. {
  57. PlayerColor player = GS(gh)->currentPlayer;
  58. ERROR_IF_NOT(player);
  59. if(gh->queries.topQuery(player))
  60. COMPLAIN_AND_RETURN("Cannot end turn before resolving queries!");
  61. gh->states.setFlag(GS(gh)->currentPlayer,&PlayerStatus::makingTurn,false);
  62. return true;
  63. }
  64. bool DismissHero::applyGh( CGameHandler *gh )
  65. {
  66. ERROR_IF_NOT_OWNS(hid);
  67. return gh->removeObject(gh->getObj(hid));
  68. }
  69. bool MoveHero::applyGh( CGameHandler *gh )
  70. {
  71. ERROR_IF_NOT_OWNS(hid);
  72. return gh->moveHero(hid,dest,0,transit,gh->getPlayerAt(c));
  73. }
  74. bool CastleTeleportHero::applyGh( CGameHandler *gh )
  75. {
  76. ERROR_IF_NOT_OWNS(hid);
  77. return gh->teleportHero(hid,dest,source,gh->getPlayerAt(c));
  78. }
  79. bool ArrangeStacks::applyGh( CGameHandler *gh )
  80. {
  81. //checks for owning in the gh func
  82. return gh->arrangeStacks(id1,id2,what,p1,p2,val,gh->getPlayerAt(c));
  83. }
  84. bool DisbandCreature::applyGh( CGameHandler *gh )
  85. {
  86. ERROR_IF_NOT_OWNS(id);
  87. return gh->disbandCreature(id,pos);
  88. }
  89. bool BuildStructure::applyGh( CGameHandler *gh )
  90. {
  91. ERROR_IF_NOT_OWNS(tid);
  92. return gh->buildStructure(tid,bid);
  93. }
  94. bool RecruitCreatures::applyGh( CGameHandler *gh )
  95. {
  96. return gh->recruitCreatures(tid,dst,crid,amount,level);
  97. }
  98. bool UpgradeCreature::applyGh( CGameHandler *gh )
  99. {
  100. ERROR_IF_NOT_OWNS(id);
  101. return gh->upgradeCreature(id,pos,cid);
  102. }
  103. bool GarrisonHeroSwap::applyGh( CGameHandler *gh )
  104. {
  105. const CGTownInstance * town = gh->getTown(tid);
  106. if (!PLAYER_OWNS(tid) && !( town->garrisonHero && PLAYER_OWNS(town->garrisonHero->id) ) )
  107. ERROR_AND_RETURN;//neither town nor garrisoned hero (if present) is ours
  108. return gh->garrisonSwap(tid);
  109. }
  110. bool ExchangeArtifacts::applyGh( CGameHandler *gh )
  111. {
  112. ERROR_IF_NOT(src.owningPlayer());//second hero can be ally
  113. return gh->moveArtifact(src, dst);
  114. }
  115. bool AssembleArtifacts::applyGh( CGameHandler *gh )
  116. {
  117. ERROR_IF_NOT_OWNS(heroID);
  118. return gh->assembleArtifacts(heroID, artifactSlot, assemble, assembleTo);
  119. }
  120. bool BuyArtifact::applyGh( CGameHandler *gh )
  121. {
  122. ERROR_IF_NOT_OWNS(hid);
  123. return gh->buyArtifact(hid,aid);
  124. }
  125. bool TradeOnMarketplace::applyGh( CGameHandler *gh )
  126. {
  127. //market must be owned or visited
  128. const IMarket *m = IMarket::castFrom(market);
  129. if(!m)
  130. COMPLAIN_AND_RETURN("market is not-a-market! :/");
  131. PlayerColor player = market->tempOwner;
  132. if(player >= PlayerColor::PLAYER_LIMIT)
  133. player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
  134. if(player >= PlayerColor::PLAYER_LIMIT)
  135. COMPLAIN_AND_RETURN("No player can use this market!");
  136. if(hero && (player != hero->tempOwner || hero->visitablePos() != market->visitablePos()))
  137. COMPLAIN_AND_RETURN("This hero can't use this marketplace!");
  138. ERROR_IF_NOT(player);
  139. switch(mode)
  140. {
  141. case EMarketMode::RESOURCE_RESOURCE:
  142. return gh->tradeResources(m, val, player, r1, r2);
  143. case EMarketMode::RESOURCE_PLAYER:
  144. return gh->sendResources(val, player, static_cast<Res::ERes>(r1), PlayerColor(r2));
  145. case EMarketMode::CREATURE_RESOURCE:
  146. return gh->sellCreatures(val, m, hero, SlotID(r1), static_cast<Res::ERes>(r2));
  147. case EMarketMode::RESOURCE_ARTIFACT:
  148. return gh->buyArtifact(m, hero, static_cast<Res::ERes>(r1), ArtifactID(r2));
  149. case EMarketMode::ARTIFACT_RESOURCE:
  150. return gh->sellArtifact(m, hero, ArtifactInstanceID(r1), static_cast<Res::ERes>(r2));
  151. case EMarketMode::CREATURE_UNDEAD:
  152. return gh->transformInUndead(m, hero, SlotID(r1));
  153. case EMarketMode::RESOURCE_SKILL:
  154. return gh->buySecSkill(m, hero, SecondarySkill(r2));
  155. case EMarketMode::CREATURE_EXP:
  156. return gh->sacrificeCreatures(m, hero, SlotID(r1), val);
  157. case EMarketMode::ARTIFACT_EXP:
  158. return gh->sacrificeArtifact(m, hero, ArtifactPosition(r1));
  159. default:
  160. COMPLAIN_AND_RETURN("Unknown exchange mode!");
  161. }
  162. }
  163. bool SetFormation::applyGh( CGameHandler *gh )
  164. {
  165. ERROR_IF_NOT_OWNS(hid);
  166. return gh->setFormation(hid,formation);
  167. }
  168. bool HireHero::applyGh( CGameHandler *gh )
  169. {
  170. const CGObjectInstance *obj = gh->getObj(tid);
  171. const CGTownInstance *town = dynamic_ptr_cast<CGTownInstance>(obj);
  172. if(town && PlayerRelations::ENEMIES == gh->getPlayerRelations(obj->tempOwner, gh->getPlayerAt(c)))
  173. COMPLAIN_AND_RETURN("Can't buy hero in enemy town!");
  174. return gh->hireHero(obj, hid,player);
  175. }
  176. bool BuildBoat::applyGh( CGameHandler *gh )
  177. {
  178. ERROR_IF_NOT_OWNS(objid);
  179. return gh->buildBoat(objid);
  180. }
  181. bool QueryReply::applyGh( CGameHandler *gh )
  182. {
  183. auto playerToConnection = gh->connections.find(player);
  184. if(playerToConnection == gh->connections.end())
  185. COMPLAIN_AND_RETURN("No such player!");
  186. if(playerToConnection->second != c)
  187. COMPLAIN_AND_RETURN("Message came from wrong connection!");
  188. if(qid == QueryID(-1))
  189. COMPLAIN_AND_RETURN("Cannot answer the query with id -1!");
  190. assert(vstd::contains(gh->states.players, player));
  191. return gh->queryReply(qid, answer, player);
  192. }
  193. bool MakeAction::applyGh( CGameHandler *gh )
  194. {
  195. const BattleInfo *b = GS(gh)->curB;
  196. if(!b) ERROR_AND_RETURN;
  197. if(b->tacticDistance)
  198. {
  199. if(ba.actionType != Battle::WALK && ba.actionType != Battle::END_TACTIC_PHASE
  200. && ba.actionType != Battle::RETREAT && ba.actionType != Battle::SURRENDER)
  201. ERROR_AND_RETURN;
  202. if(gh->connections[b->sides[b->tacticsSide].color] != c)
  203. ERROR_AND_RETURN;
  204. }
  205. else if(gh->connections[b->battleGetStackByID(b->activeStack)->owner] != c)
  206. ERROR_AND_RETURN;
  207. return gh->makeBattleAction(ba);
  208. }
  209. bool MakeCustomAction::applyGh( CGameHandler *gh )
  210. {
  211. const BattleInfo *b = GS(gh)->curB;
  212. if(!b) ERROR_AND_RETURN;
  213. if(b->tacticDistance) ERROR_AND_RETURN;
  214. const CStack *active = GS(gh)->curB->battleGetStackByID(GS(gh)->curB->activeStack);
  215. if(!active) ERROR_AND_RETURN;
  216. if(gh->connections[active->owner] != c) ERROR_AND_RETURN;
  217. if(ba.actionType != Battle::HERO_SPELL) ERROR_AND_RETURN;
  218. return gh->makeCustomAction(ba);
  219. }
  220. bool DigWithHero::applyGh( CGameHandler *gh )
  221. {
  222. ERROR_IF_NOT_OWNS(id);
  223. return gh->dig(gh->getHero(id));
  224. }
  225. bool CastAdvSpell::applyGh( CGameHandler *gh )
  226. {
  227. ERROR_IF_NOT_OWNS(hid);
  228. return gh->castSpell(gh->getHero(hid), sid, pos);
  229. }
  230. bool PlayerMessage::applyGh( CGameHandler *gh )
  231. {
  232. ERROR_IF_NOT(player);
  233. if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
  234. gh->playerMessage(player,text, currObj);
  235. return true;
  236. }