NetPacksServer.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "../lib/NetPacks.h"
  2. #include "CGameHandler.h"
  3. #include "../hch/CObjectHandler.h"
  4. #include "../lib/IGameCallback.h"
  5. #include "../lib/map.h"
  6. #define PLAYER_OWNS(id) (gh->getPlayerAt(c)==gh->getOwner(id))
  7. #define ERROR_AND_RETURN {if(c) *c << &SystemMessage("You are not allowed to perform this action!"); \
  8. tlog1<<"Player is not allowed to perform this action!\n"; \
  9. return false;}
  10. #define ERROR_IF_NOT_OWNS(id) if(!PLAYER_OWNS(id)) ERROR_AND_RETURN
  11. #define COMPLAIN_AND_RETURN(txt) { gh->complain(txt); ERROR_AND_RETURN }
  12. /*
  13. * NetPacksServer.cpp, part of VCMI engine
  14. *
  15. * Authors: listed in file AUTHORS in main folder
  16. *
  17. * License: GNU General Public License v2.0 or later
  18. * Full text of license available in license.txt file, in main folder
  19. *
  20. */
  21. CGameState* CPackForServer::GS(CGameHandler *gh)
  22. {
  23. return gh->gs;
  24. }
  25. bool SaveGame::applyGh( CGameHandler *gh )
  26. {
  27. //gh->sendMessageTo(*c,"Saving...");
  28. gh->save(fname);
  29. gh->sendMessageTo(*c,"Game has been saved as " + fname);
  30. return true;
  31. }
  32. bool CloseServer::applyGh( CGameHandler *gh )
  33. {
  34. gh->close();
  35. return true;
  36. }
  37. bool EndTurn::applyGh( CGameHandler *gh )
  38. {
  39. gh->states.setFlag(GS(gh)->currentPlayer,&PlayerStatus::makingTurn,false);
  40. return true;
  41. }
  42. bool DismissHero::applyGh( CGameHandler *gh )
  43. {
  44. ERROR_IF_NOT_OWNS(hid);
  45. return gh->removeObject(hid);
  46. }
  47. bool MoveHero::applyGh( CGameHandler *gh )
  48. {
  49. ERROR_IF_NOT_OWNS(hid);
  50. return gh->moveHero(hid,dest,0,gh->getPlayerAt(c));
  51. }
  52. bool ArrangeStacks::applyGh( CGameHandler *gh )
  53. {
  54. //checks for owning in the gh func
  55. return gh->arrangeStacks(id1,id2,what,p1,p2,val);
  56. }
  57. bool DisbandCreature::applyGh( CGameHandler *gh )
  58. {
  59. ERROR_IF_NOT_OWNS(id);
  60. return gh->disbandCreature(id,pos);
  61. }
  62. bool BuildStructure::applyGh( CGameHandler *gh )
  63. {
  64. ERROR_IF_NOT_OWNS(tid);
  65. return gh->buildStructure(tid,bid);
  66. }
  67. bool RecruitCreatures::applyGh( CGameHandler *gh )
  68. {
  69. return gh->recruitCreatures(tid,crid,amount);
  70. }
  71. bool UpgradeCreature::applyGh( CGameHandler *gh )
  72. {
  73. ERROR_IF_NOT_OWNS(id);
  74. return gh->upgradeCreature(id,pos,cid);
  75. }
  76. bool GarrisonHeroSwap::applyGh( CGameHandler *gh )
  77. {
  78. ERROR_IF_NOT_OWNS(tid);
  79. return gh->garrisonSwap(tid);
  80. }
  81. bool ExchangeArtifacts::applyGh( CGameHandler *gh )
  82. {
  83. ERROR_IF_NOT_OWNS(hid1);
  84. ERROR_IF_NOT_OWNS(hid2);
  85. return gh->swapArtifacts(hid1,hid2,slot1,slot2);
  86. }
  87. bool AssembleArtifacts::applyGh( CGameHandler *gh )
  88. {
  89. ERROR_IF_NOT_OWNS(heroID);
  90. return gh->assembleArtifacts(heroID, artifactSlot, assemble, assembleTo);
  91. }
  92. bool BuyArtifact::applyGh( CGameHandler *gh )
  93. {
  94. ERROR_IF_NOT_OWNS(hid);
  95. return gh->buyArtifact(hid,aid);
  96. }
  97. bool TradeOnMarketplace::applyGh( CGameHandler *gh )
  98. {
  99. //market must be owned or visited
  100. const IMarket *m = IMarket::castFrom(market);
  101. if(!m)
  102. COMPLAIN_AND_RETURN("market is not-a-market! :/");
  103. ui8 player = market->tempOwner;
  104. if(player >= PLAYER_LIMIT)
  105. player = gh->getTile(market->visitablePos())->visitableObjects.back()->tempOwner;
  106. if(player >= PLAYER_LIMIT)
  107. COMPLAIN_AND_RETURN("No player can use this market!");
  108. if(hero && (player != hero->tempOwner || hero->visitablePos() != market->visitablePos()))
  109. COMPLAIN_AND_RETURN("This hero can't use this marketplace!");
  110. if(gh->getPlayerAt(c) != player)
  111. ERROR_AND_RETURN;
  112. switch(mode)
  113. {
  114. case RESOURCE_RESOURCE:
  115. return gh->tradeResources(m, val, player, r1, r2);
  116. case RESOURCE_PLAYER:
  117. return gh->sendResources(val, player, r1, r2);
  118. default:
  119. gh->complain("Unknown exchange mode!");
  120. ERROR_AND_RETURN;
  121. }
  122. }
  123. bool SetFormation::applyGh( CGameHandler *gh )
  124. {
  125. ERROR_IF_NOT_OWNS(hid);
  126. return gh->setFormation(hid,formation);
  127. }
  128. bool HireHero::applyGh( CGameHandler *gh )
  129. {
  130. ERROR_IF_NOT_OWNS(tid);
  131. return gh->hireHero(tid,hid);
  132. }
  133. bool BuildBoat::applyGh( CGameHandler *gh )
  134. {
  135. ERROR_IF_NOT_OWNS(objid);
  136. return gh->buildBoat(objid);
  137. }
  138. bool QueryReply::applyGh( CGameHandler *gh )
  139. {
  140. //TODO - check if player matches the query
  141. return gh->queryReply(qid,answer);
  142. }
  143. bool MakeAction::applyGh( CGameHandler *gh )
  144. {
  145. if(!GS(gh)->curB) ERROR_AND_RETURN;
  146. if(gh->connections[GS(gh)->curB->getStack(GS(gh)->curB->activeStack)->owner] != c) ERROR_AND_RETURN;
  147. return gh->makeBattleAction(ba);
  148. }
  149. bool MakeCustomAction::applyGh( CGameHandler *gh )
  150. {
  151. if(!GS(gh)->curB) ERROR_AND_RETURN;
  152. if(gh->connections[GS(gh)->curB->getStack(GS(gh)->curB->activeStack)->owner] != c) ERROR_AND_RETURN;
  153. return gh->makeCustomAction(ba);
  154. }
  155. bool DigWithHero::applyGh( CGameHandler *gh )
  156. {
  157. ERROR_IF_NOT_OWNS(id);
  158. return gh->dig(gh->getHero(id));
  159. }
  160. bool CastAdvSpell::applyGh( CGameHandler *gh )
  161. {
  162. ERROR_IF_NOT_OWNS(hid);
  163. return gh->castSpell(gh->getHero(hid), sid, pos);
  164. }
  165. bool PlayerMessage::applyGh( CGameHandler *gh )
  166. {
  167. if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
  168. gh->playerMessage(player,text);
  169. return true;
  170. }
  171. bool SetSelection::applyGh( CGameHandler *gh )
  172. {
  173. if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
  174. if(!gh->getObj(id)) ERROR_AND_RETURN;
  175. gh->sendAndApply(this);
  176. return true;
  177. }