NetPacksServer.cpp 8.3 KB

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