Ghecco

*INC | ghecco_Stamina

Jul 5th, 2020
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* *                                                                    *   [ghecco_Stamina]    *
  2.  
  3.                                                                             - [How to use?]
  4.                                                                             : in OnPlayerConnect add this:
  5.  
  6.                                                                               SetPlayerMaxStamina(playerid, STAMINA_DEFAULT_MAX); // STAMINA_DEFAULT_MAX or the value you prefer
  7.  
  8.  
  9.  
  10.  */
  11. #include <a_samp>
  12.  
  13. #if !defined STAMINA_DEFAULT_MAX
  14.     #define STAMINA_DEFAULT_MAX             (200)
  15. #endif
  16. #if !defined STAMINA_DEFAULT_RECOVERYTIME
  17.     #define STAMINA_DEFAULT_RECOVERYTIME    (5000)
  18. #endif
  19.  
  20.  
  21.  
  22. public OnPlayerConnect(playerid)
  23. {
  24.     SetPVarInt(playerid, "MAX_Stamina", STAMINA_DEFAULT_MAX);
  25.  
  26.     #if defined MyLib_OnPlayerConnect
  27.         MyLib_OnPlayerConnect(playerid);
  28.     #endif
  29.     return 1;
  30. }
  31. #if defined _ALS_OnPlayerConnect
  32.     #undef OnPlayerConnect
  33. #else
  34.     #define _ALS_OnPlayerConnect
  35. #endif
  36. #define OnPlayerConnect MyLib_OnPlayerConnect
  37. #if defined MyLib_OnPlayerConnect
  38.     forward MyLib_OnPlayerConnect(playerid);
  39. #endif
  40.  
  41.  
  42. public OnPlayerSpawn(playerid)
  43. {
  44.     SetPVarInt(playerid, "Stamina", GetPVarInt(playerid, "MAX_Stamina"));
  45.  
  46.     #if defined MyLib_OnPlayerSpawn
  47.         MyLib_OnPlayerSpawn(playerid);
  48.     #endif
  49.     return 1;
  50. }
  51. #if defined _ALS_OnPlayerSpawn
  52.     #undef OnPlayerSpawn
  53. #else
  54.     #define _ALS_OnPlayerSpawn
  55. #endif
  56. #define OnPlayerSpawn MyLib_OnPlayerSpawn
  57. #if defined MyLib_OnPlayerSpawn
  58.     forward MyLib_OnPlayerSpawn(playerid);
  59. #endif
  60.  
  61.  
  62.  
  63.  
  64. public OnPlayerUpdate(playerid)
  65. {
  66.     if(IsPlayerRunning(playerid)) GivePlayerStamina(playerid, -1); // if the player run, it subtracts the player's stamina
  67.     else if(GetPVarInt(playerid, "Stamina") < GetPVarInt(playerid, "MAX_Stamina")) GivePlayerStamina(playerid, 1); // if the player is not running, he recovers the current stamina up to his MAX
  68.  
  69.      #if defined MyLib_OnPlayerUpdate
  70.         MyLib_OnPlayerUpdate(playerid);
  71.     #endif
  72.     return 1;
  73. }
  74. #if defined _ALS_OnPlayerUpdate
  75.     #undef OnPlayerUpdate
  76. #else
  77.     #define _ALS_OnPlayerUpdate
  78. #endif
  79. #define OnPlayerUpdate MyLib_OnPlayerUpdate
  80. #if defined MyLib_OnPlayerUpdate
  81.     forward MyLib_OnPlayerUpdate(playerid);
  82. #endif
  83.  
  84.  
  85. forward OnPlayerStaminaOver(playerid);
  86. public OnPlayerStaminaOver(playerid) // when the player is exhausted
  87. {
  88.     SetPlayerExhausted(playerid, true);
  89.     return 1;
  90. }
  91.  
  92. forward SetPlayerExhausted(playerid, bool:Exhausted);
  93. public SetPlayerExhausted(playerid, bool:Exhausted) // set if the player is exhausted (only to help IsPlayerRunning)
  94. {
  95.     if(Exhausted)
  96.     {
  97.         TogglePlayerControllable(playerid,0);
  98.         TogglePlayerControllable(playerid,1);
  99.  
  100.         ApplyAnimation(playerid, "PED", "IDLE_tired", 4.1, 0, 1, 1, 0, STAMINA_DEFAULT_RECOVERYTIME, 1);
  101.  
  102.         SetPVarInt(playerid, "Exhausted", 1);
  103.         SetTimerEx("SetPlayerExhausted", STAMINA_DEFAULT_RECOVERYTIME, false, "ib", playerid, false);
  104.     }
  105.     else SetPVarInt(playerid, "Exhausted", 0);
  106.     return 1;
  107. }
  108.  
  109. stock GivePlayerStamina(playerid, value) //Add / Subtract the player's current stamina
  110. {
  111.     new stamina = GetPVarInt(playerid, "Stamina");
  112.     if(stamina + value <= 0) return OnPlayerStaminaOver(playerid);
  113.     if(stamina + value <= GetPVarInt(playerid, "MAX_Stamina"))
  114.     {
  115.         stamina = stamina+value;
  116.         SetPVarInt(playerid, "Stamina", stamina);
  117.         return 1;
  118.     }
  119.     else return 0;
  120. }
  121.  
  122.  
  123. stock SetPlayerMaxStamina(playerid, value) //set the player's maximum stamina
  124. {
  125.     new stamina = GetPVarInt(playerid, "Stamina"), max_stamina = GetPVarInt(playerid, "MAX_Stamina");
  126.     max_stamina = value;
  127.     SetPVarInt(playerid, "MAX_Stamina", max_stamina);
  128.     if(stamina > max_stamina) stamina = max_stamina, SetPVarInt(playerid, "Stamina", stamina);
  129.     return 1;
  130. }
  131.  
  132.  
  133. stock IsPlayerExhausted(playerid) // check if the player is exhausted
  134. {
  135.     if(GetPVarInt(playerid, "Exhausted") == 1) return 1;
  136.     else return 0;
  137. }
  138.  
  139. stock IsPlayerRunning(playerid) // Thanks SA-MP forum | check if the player is running
  140. {
  141.     if(!IsPlayerConnected(playerid) || IsPlayerInAnyVehicle(playerid) || IsPlayerExhausted(playerid)) return 0;
  142.  
  143.     new keys, updown, leftright;
  144.     GetPlayerKeys(playerid, keys, updown, leftright);
  145.     if(keys & KEY_SPRINT && GetPlayerSpecialAction(playerid) != SPECIAL_ACTION_USEJETPACK) return 1;
  146.     else return 0;
  147. }
RAW Paste Data