NetPacksServer.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. ERROR_IF_NOT_OWNS(tid);
  66. return gh->recruitCreatures(tid,crid,amount);
  67. }
  68. bool UpgradeCreature::applyGh( CGameHandler *gh )
  69. {
  70. ERROR_IF_NOT_OWNS(id);
  71. return gh->upgradeCreature(id,pos,cid);
  72. }
  73. bool GarrisonHeroSwap::applyGh( CGameHandler *gh )
  74. {
  75. ERROR_IF_NOT_OWNS(tid);
  76. return gh->garrisonSwap(tid);
  77. }
  78. bool ExchangeArtifacts::applyGh( CGameHandler *gh )
  79. {
  80. ERROR_IF_NOT_OWNS(hid1);
  81. ERROR_IF_NOT_OWNS(hid2);
  82. return gh->swapArtifacts(hid1,hid2,slot1,slot2);
  83. }
  84. bool AssembleArtifacts::applyGh( CGameHandler *gh )
  85. {
  86. ERROR_IF_NOT_OWNS(heroID);
  87. return gh->assembleArtifacts(heroID, artifactSlot, assemble, assembleTo);
  88. }
  89. bool BuyArtifact::applyGh( CGameHandler *gh )
  90. {
  91. ERROR_IF_NOT_OWNS(hid);
  92. return gh->buyArtifact(hid,aid);
  93. }
  94. bool TradeOnMarketplace::applyGh( CGameHandler *gh )
  95. {
  96. if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
  97. return gh->tradeResources(val,player,r1,r2);
  98. }
  99. bool SetFormation::applyGh( CGameHandler *gh )
  100. {
  101. ERROR_IF_NOT_OWNS(hid);
  102. return gh->setFormation(hid,formation);
  103. }
  104. bool HireHero::applyGh( CGameHandler *gh )
  105. {
  106. ERROR_IF_NOT_OWNS(tid);
  107. return gh->hireHero(tid,hid);
  108. }
  109. bool BuildBoat::applyGh( CGameHandler *gh )
  110. {
  111. ERROR_IF_NOT_OWNS(objid);
  112. return gh->buildBoat(objid);
  113. }
  114. bool QueryReply::applyGh( CGameHandler *gh )
  115. {
  116. //TODO - check if player matches the query
  117. return gh->queryReply(qid,answer);
  118. }
  119. bool MakeAction::applyGh( CGameHandler *gh )
  120. {
  121. if(!GS(gh)->curB) ERROR_AND_RETURN;
  122. if(gh->connections[GS(gh)->curB->getStack(GS(gh)->curB->activeStack)->owner] != c) ERROR_AND_RETURN;
  123. return gh->makeBattleAction(ba);
  124. }
  125. bool MakeCustomAction::applyGh( CGameHandler *gh )
  126. {
  127. if(!GS(gh)->curB) ERROR_AND_RETURN;
  128. if(gh->connections[GS(gh)->curB->getStack(GS(gh)->curB->activeStack)->owner] != c) ERROR_AND_RETURN;
  129. return gh->makeCustomAction(ba);
  130. }
  131. bool DigWithHero::applyGh( CGameHandler *gh )
  132. {
  133. ERROR_IF_NOT_OWNS(id);
  134. return gh->dig(gh->getHero(id));
  135. }
  136. bool CastAdvSpell::applyGh( CGameHandler *gh )
  137. {
  138. ERROR_IF_NOT_OWNS(hid);
  139. return gh->castSpell(gh->getHero(hid), sid, pos);
  140. }
  141. bool PlayerMessage::applyGh( CGameHandler *gh )
  142. {
  143. if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
  144. gh->playerMessage(player,text);
  145. return true;
  146. }
  147. bool SetSelection::applyGh( CGameHandler *gh )
  148. {
  149. if(gh->getPlayerAt(c) != player) ERROR_AND_RETURN;
  150. if(!gh->getObj(id)) ERROR_AND_RETURN;
  151. gh->sendAndApply(this);
  152. return true;
  153. }