NetPacksServer.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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/map.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. *c << &temp_message; \
  15. } \
  16. tlog1<<"Player is not allowed to perform this action!\n"; \
  17. return false;} while(0)
  18. #define WRONG_PLAYER_MSG(expectedplayer) do {std::ostringstream oss;\
  19. oss << "You were identified as player " << (int)gh->getPlayerAt(c) << " while expecting " << (int)expectedplayer;\
  20. tlog1 << oss.str() << std::endl; \
  21. if(c) { SystemMessage temp_message(oss.str()); *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->sendMessageTo(*c,"Saving...");
  41. gh->save(fname);
  42. gh->sendMessageTo(*c,"Game has been saved as " + 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. int player = GS(gh)->currentPlayer;
  58. ERROR_IF_NOT(player);
  59. if(gh->states.checkFlag(player, &PlayerStatus::engagedIntoBattle))
  60. COMPLAIN_AND_RETURN("Cannot end turn when in battle!");
  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(hid);
  68. }
  69. bool MoveHero::applyGh( CGameHandler *gh )
  70. {
  71. ERROR_IF_NOT_OWNS(hid);
  72. return gh->moveHero(hid,dest,0,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,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_OWNS(hid1);//second hero can be ally
  113. if (hid1)
  114. { //TODO: polymorph
  115. if (hid2)
  116. return gh->moveArtifact(hid1,hid2,slot1,slot2);
  117. else
  118. return gh->moveArtifact(hid1,s2,slot1,slot2);
  119. }
  120. else
  121. {
  122. if (hid2)
  123. return gh->moveArtifact(s1,hid2,slot1,slot2);
  124. else
  125. return gh->moveArtifact(s1,s2,slot1,slot2);
  126. }
  127. }
  128. bool AssembleArtifacts::applyGh( CGameHandler *gh )
  129. {
  130. ERROR_IF_NOT_OWNS(heroID);
  131. return gh->assembleArtifacts(heroID, artifactSlot, assemble, assembleTo);
  132. }
  133. bool BuyArtifact::applyGh( CGameHandler *gh )
  134. {
  135. ERROR_IF_NOT_OWNS(hid);
  136. return gh->buyArtifact(hid,aid);
  137. }
  138. bool TradeOnMarketplace::applyGh( CGameHandler *gh )
  139. {
  140. //market must be owned or visited
  141. const IMarket *m = IMarket::castFrom(market);
  142. if(!m)
  143. COMPLAIN_AND_RETURN("market is not-a-market! :/");
  144. ui8 player = market->tempOwner;
  145. if(player >= GameConstants::PLAYER_LIMIT)
  146. player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
  147. if(player >= GameConstants::PLAYER_LIMIT)
  148. COMPLAIN_AND_RETURN("No player can use this market!");
  149. if(hero && (player != hero->tempOwner || hero->visitablePos() != market->visitablePos()))
  150. COMPLAIN_AND_RETURN("This hero can't use this marketplace!");
  151. ERROR_IF_NOT(player);
  152. switch(mode)
  153. {
  154. case EMarketMode::RESOURCE_RESOURCE:
  155. return gh->tradeResources(m, val, player, r1, r2);
  156. case EMarketMode::RESOURCE_PLAYER:
  157. return gh->sendResources(val, player, r1, r2);
  158. case EMarketMode::CREATURE_RESOURCE:
  159. if(!hero)
  160. COMPLAIN_AND_RETURN("Only hero can sell creatures!");
  161. return gh->sellCreatures(val, m, hero, r1, r2);
  162. case EMarketMode::RESOURCE_ARTIFACT:
  163. if(!hero)
  164. COMPLAIN_AND_RETURN("Only hero can buy artifacts!");
  165. return gh->buyArtifact(m, hero, r1, r2);
  166. case EMarketMode::ARTIFACT_RESOURCE:
  167. if(!hero)
  168. COMPLAIN_AND_RETURN("Only hero can sell artifacts!");
  169. return gh->sellArtifact(m, hero, r1, r2);
  170. case EMarketMode::CREATURE_UNDEAD:
  171. return gh->transformInUndead(m, hero, r1);
  172. case EMarketMode::RESOURCE_SKILL:
  173. return gh->buySecSkill(m, hero, r2);
  174. case EMarketMode::CREATURE_EXP:
  175. return gh->sacrificeCreatures(m, hero, r1, val);
  176. case EMarketMode::ARTIFACT_EXP:
  177. return gh->sacrificeArtifact(m, hero, r1);
  178. default:
  179. COMPLAIN_AND_RETURN("Unknown exchange mode!");
  180. }
  181. }
  182. bool SetFormation::applyGh( CGameHandler *gh )
  183. {
  184. ERROR_IF_NOT_OWNS(hid);
  185. return gh->setFormation(hid,formation);
  186. }
  187. bool HireHero::applyGh( CGameHandler *gh )
  188. {
  189. const CGObjectInstance *obj = gh->getObj(tid);
  190. if(obj->ID == GameConstants::TOWNI_TYPE)
  191. ERROR_IF_NOT_OWNS(tid);
  192. //TODO check for visiting hero
  193. return gh->hireHero(obj, hid,player);
  194. }
  195. bool BuildBoat::applyGh( CGameHandler *gh )
  196. {
  197. ERROR_IF_NOT_OWNS(objid);
  198. return gh->buildBoat(objid);
  199. }
  200. bool QueryReply::applyGh( CGameHandler *gh )
  201. {
  202. ERROR_IF_NOT(player);
  203. assert(vstd::contains(gh->states.players, player));
  204. return gh->queryReply(qid, answer, player);
  205. }
  206. bool MakeAction::applyGh( CGameHandler *gh )
  207. {
  208. const BattleInfo *b = GS(gh)->curB;
  209. if(!b) ERROR_AND_RETURN;
  210. if(b->tacticDistance)
  211. {
  212. if(ba.actionType != BattleAction::WALK && ba.actionType != BattleAction::END_TACTIC_PHASE
  213. && ba.actionType != BattleAction::RETREAT && ba.actionType != BattleAction::SURRENDER)
  214. ERROR_AND_RETURN;
  215. if(gh->connections[b->sides[b->tacticsSide]] != c)
  216. ERROR_AND_RETURN;
  217. }
  218. else if(gh->connections[b->getStack(b->activeStack)->owner] != c)
  219. ERROR_AND_RETURN;
  220. return gh->makeBattleAction(ba);
  221. }
  222. bool MakeCustomAction::applyGh( CGameHandler *gh )
  223. {
  224. const BattleInfo *b = GS(gh)->curB;
  225. if(!b) ERROR_AND_RETURN;
  226. if(b->tacticDistance) ERROR_AND_RETURN;
  227. const CStack *active = GS(gh)->curB->getStack(GS(gh)->curB->activeStack);
  228. if(!active) ERROR_AND_RETURN;
  229. if(gh->connections[active->owner] != c) ERROR_AND_RETURN;
  230. return gh->makeCustomAction(ba);
  231. }
  232. bool DigWithHero::applyGh( CGameHandler *gh )
  233. {
  234. ERROR_IF_NOT_OWNS(id);
  235. return gh->dig(gh->getHero(id));
  236. }
  237. bool CastAdvSpell::applyGh( CGameHandler *gh )
  238. {
  239. ERROR_IF_NOT_OWNS(hid);
  240. return gh->castSpell(gh->getHero(hid), sid, pos);
  241. }
  242. bool PlayerMessage::applyGh( CGameHandler *gh )
  243. {
  244. ERROR_IF_NOT(player);
  245. if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
  246. gh->playerMessage(player,text);
  247. return true;
  248. }
  249. bool SetSelection::applyGh( CGameHandler *gh )
  250. {
  251. ERROR_IF_NOT(player);
  252. if(!gh->getObj(id))
  253. {
  254. tlog1 << "No such object...\n";
  255. ERROR_AND_RETURN;
  256. }
  257. gh->sendAndApply(this);
  258. return true;
  259. }