NetPacksServer.cpp 4.4 KB

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