NetPacksServer.cpp 8.4 KB

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