NetPacksServer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "StdInc.h"
  2. #include "../lib/NetPacks.h"
  3. #include "CGameHandler.h"
  4. #include "../lib/CObjectHandler.h"
  5. #include "../lib/IGameCallback.h"
  6. #include "../lib/Mapping/CMap.h"
  7. #include "../lib/CGameState.h"
  8. #include "../lib/BattleState.h"
  9. #include "../lib/BattleAction.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. tlog1<<"Player is not allowed to perform this action!\n"; \
  18. return false;} while(0)
  19. #define WRONG_PLAYER_MSG(expectedplayer) do {std::ostringstream oss;\
  20. oss << "You were identified as player " << (int)gh->getPlayerAt(c) << " while expecting " << (int)expectedplayer;\
  21. tlog1 << oss.str() << std::endl; \
  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->sendMessageTo(*c,"Saving...");
  42. gh->save(fname);
  43. gh->sendMessageTo(*c,"Game has been saved as " + 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. int player = GS(gh)->currentPlayer;
  59. ERROR_IF_NOT(player);
  60. if(gh->states.checkFlag(player, &PlayerStatus::engagedIntoBattle))
  61. COMPLAIN_AND_RETURN("Cannot end turn when in battle!");
  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(hid);
  69. }
  70. bool MoveHero::applyGh( CGameHandler *gh )
  71. {
  72. ERROR_IF_NOT_OWNS(hid);
  73. return gh->moveHero(hid,dest,0,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,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. ui8 player = market->tempOwner;
  133. if(player >= GameConstants::PLAYER_LIMIT)
  134. player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
  135. if(player >= GameConstants::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, r1, r2);
  146. case EMarketMode::CREATURE_RESOURCE:
  147. if(!hero)
  148. COMPLAIN_AND_RETURN("Only hero can sell creatures!");
  149. return gh->sellCreatures(val, m, hero, r1, r2);
  150. case EMarketMode::RESOURCE_ARTIFACT:
  151. if(!hero)
  152. COMPLAIN_AND_RETURN("Only hero can buy artifacts!");
  153. return gh->buyArtifact(m, hero, r1, r2);
  154. case EMarketMode::ARTIFACT_RESOURCE:
  155. if(!hero)
  156. COMPLAIN_AND_RETURN("Only hero can sell artifacts!");
  157. return gh->sellArtifact(m, hero, r1, r2);
  158. case EMarketMode::CREATURE_UNDEAD:
  159. return gh->transformInUndead(m, hero, r1);
  160. case EMarketMode::RESOURCE_SKILL:
  161. return gh->buySecSkill(m, hero, r2);
  162. case EMarketMode::CREATURE_EXP:
  163. return gh->sacrificeCreatures(m, hero, r1, val);
  164. case EMarketMode::ARTIFACT_EXP:
  165. return gh->sacrificeArtifact(m, hero, r1);
  166. default:
  167. COMPLAIN_AND_RETURN("Unknown exchange mode!");
  168. }
  169. }
  170. bool SetFormation::applyGh( CGameHandler *gh )
  171. {
  172. ERROR_IF_NOT_OWNS(hid);
  173. return gh->setFormation(hid,formation);
  174. }
  175. bool HireHero::applyGh( CGameHandler *gh )
  176. {
  177. const CGObjectInstance *obj = gh->getObj(tid);
  178. if(obj->ID == Obj::TOWN)
  179. ERROR_IF_NOT_OWNS(tid);
  180. //TODO check for visiting hero
  181. return gh->hireHero(obj, hid,player);
  182. }
  183. bool BuildBoat::applyGh( CGameHandler *gh )
  184. {
  185. ERROR_IF_NOT_OWNS(objid);
  186. return gh->buildBoat(objid);
  187. }
  188. bool QueryReply::applyGh( CGameHandler *gh )
  189. {
  190. auto playerToConnection = gh->connections.find(player);
  191. if(playerToConnection == gh->connections.end())
  192. COMPLAIN_AND_RETURN("No such player!");
  193. if(playerToConnection->second != c)
  194. COMPLAIN_AND_RETURN("Message came from wrong connection!");
  195. if(qid == -1)
  196. COMPLAIN_AND_RETURN("Cannot answer the query with id -1!");
  197. assert(vstd::contains(gh->states.players, player));
  198. return gh->queryReply(qid, answer, player);
  199. }
  200. bool MakeAction::applyGh( CGameHandler *gh )
  201. {
  202. const BattleInfo *b = GS(gh)->curB;
  203. if(!b) ERROR_AND_RETURN;
  204. if(b->tacticDistance)
  205. {
  206. if(ba.actionType != Battle::WALK && ba.actionType != Battle::END_TACTIC_PHASE
  207. && ba.actionType != Battle::RETREAT && ba.actionType != Battle::SURRENDER)
  208. ERROR_AND_RETURN;
  209. if(gh->connections[b->sides[b->tacticsSide]] != c)
  210. ERROR_AND_RETURN;
  211. }
  212. else if(gh->connections[b->battleGetStackByID(b->activeStack)->owner] != c)
  213. ERROR_AND_RETURN;
  214. return gh->makeBattleAction(ba);
  215. }
  216. bool MakeCustomAction::applyGh( CGameHandler *gh )
  217. {
  218. const BattleInfo *b = GS(gh)->curB;
  219. if(!b) ERROR_AND_RETURN;
  220. if(b->tacticDistance) ERROR_AND_RETURN;
  221. const CStack *active = GS(gh)->curB->battleGetStackByID(GS(gh)->curB->activeStack);
  222. if(!active) ERROR_AND_RETURN;
  223. if(gh->connections[active->owner] != c) ERROR_AND_RETURN;
  224. if(ba.actionType != Battle::HERO_SPELL) ERROR_AND_RETURN;
  225. return gh->makeCustomAction(ba);
  226. }
  227. bool DigWithHero::applyGh( CGameHandler *gh )
  228. {
  229. ERROR_IF_NOT_OWNS(id);
  230. return gh->dig(gh->getHero(id));
  231. }
  232. bool CastAdvSpell::applyGh( CGameHandler *gh )
  233. {
  234. ERROR_IF_NOT_OWNS(hid);
  235. return gh->castSpell(gh->getHero(hid), sid, pos);
  236. }
  237. bool PlayerMessage::applyGh( CGameHandler *gh )
  238. {
  239. ERROR_IF_NOT(player);
  240. if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
  241. gh->playerMessage(player,text);
  242. return true;
  243. }
  244. bool SetSelection::applyGh( CGameHandler *gh )
  245. {
  246. ERROR_IF_NOT(player);
  247. if(!gh->getObj(id))
  248. {
  249. tlog1 << "No such object...\n";
  250. ERROR_AND_RETURN;
  251. }
  252. gh->sendAndApply(this);
  253. return true;
  254. }