Mauzen

GetPlayerO2 V1

Aug 19th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.20 KB | None | 0 0
  1. /*
  2.  *  GetPlayerO2 Include
  3.  *  Written by Mauzen, 19.8.2012
  4.  *  Version 1.0
  5.  */
  6.  
  7. // O2 used up/regenerated per second
  8. #define O2_CONSUMPTION                      (2.7)       // 100.0% / 40-3 seconds
  9.  
  10. // Checks the last updated animations to get if the player is swimming/diving
  11. #define IsPlayerWaterDiving(%1)             (1540 <= O2_lastAnim[%1] && O2_lastAnim[%1] <= 1544)
  12. #define IsPlayerSwimming(%1)                (O2_lastAnim[%1] == 1538 || O2_lastAnim[%1] == 1539)
  13.  
  14.  
  15. // Stores the animation id of each player on the last update
  16. new O2_lastAnim[MAX_PLAYERS];
  17. // Stores the ms time when a player started, or finished to dive for O2 calculation
  18. new O2_diveStart[MAX_PLAYERS];
  19. // Stores the O2 level at certain syncing points
  20. // This does NOT contain the correct and actual O2 value all the time, use GetPlayerO2
  21. new Float:O2_level[MAX_PLAYERS];
  22.  
  23.  
  24.  
  25. forward Float:GetPlayerO2(playerid);
  26.  
  27.  
  28. stock Float:GetPlayerO2(playerid) {
  29.     new Float:curO2;
  30.     // If O2 wasnt touched yet, return 100%
  31.     if (O2_diveStart[playerid] == 0) return 100.0;
  32.    
  33.     // Calculation when player is currently diving
  34.     if (IsPlayerWaterDiving(playerid)) {
  35.         // Player can dive 3 seconds without losing O2
  36.         if (tickcount() - O2_diveStart[playerid] < 3000) return O2_level[playerid];
  37.         // Use O2 level at the point of starting diving - o2 consumption * total diving time
  38.         curO2 = O2_level[playerid] - ((tickcount() - O2_diveStart[playerid] - 3000) / 1000.0) * O2_CONSUMPTION;
  39.         // limit at 0.0
  40.         if (curO2 < 0.0) curO2 = 0.0;
  41.     } else {
  42.         // Calculation when player is currently breathing
  43.         // Use O2 level at the point of ending the last dive + o2 consumption * total breathing time
  44.         curO2 = O2_level[playerid] + ((tickcount() - O2_diveStart[playerid]) / 1000.0) * O2_CONSUMPTION;
  45.         // limit at 100.0
  46.         if (curO2 > 100.0) curO2 = 100.0;
  47.     }
  48.     return curO2;
  49. }
  50.  
  51. public OnPlayerUpdate(playerid) {
  52.     static O2_newAnim;
  53.     O2_newAnim = GetPlayerAnimationIndex(playerid);
  54.    
  55.     // Debug output for testing
  56.     new txt[64];
  57.     format(txt, 64, "Anim: %f", GetPlayerO2(playerid));
  58.     SendClientMessage(playerid, -1, txt);
  59.    
  60.     // Animation changed
  61.     if (O2_lastAnim[playerid] != O2_newAnim) {     
  62.         // Player was not diving, now dives
  63.         if (!IsPlayerWaterDiving(playerid) && (1540 <= O2_newAnim && O2_newAnim <= 1544)) {
  64.             // Set the current O2 level as base for the dive calculation
  65.             O2_level[playerid] = GetPlayerO2(playerid);
  66.             // Save the time when diving started
  67.             O2_diveStart[playerid] = tickcount();
  68.             SendClientMessage(playerid, -1, "Now diving");
  69.         } else
  70.         // Player was diving, now comes upgrade
  71.         if (IsPlayerWaterDiving(playerid) && !(1540 <= O2_newAnim && O2_newAnim <= 1544)) {
  72.             // Set the current O2 level as base for the breathing calculation
  73.             O2_level[playerid] = GetPlayerO2(playerid);
  74.             // Save the time when regeneration started
  75.             O2_diveStart[playerid] = tickcount();
  76.             SendClientMessage(playerid, -1, "Now coming up");
  77.         }      
  78.        
  79.         // Update the new/last animation variable
  80.         O2_lastAnim[playerid] = O2_newAnim;
  81.     }
  82.     return CallLocalFunction("O2_OnPlayerUpdate", "i", playerid);
  83. }
  84. #if defined _ALS_OnPlayerUpdate
  85.     #undef OnPlayerUpdate
  86. #else
  87.     #define _ALS_OnPlayerUpdate
  88. #endif
  89. #define OnPlayerUpdate O2_OnPlayerUpdate
  90. forward O2_OnPlayerUpdate(playerid);
Advertisement
Add Comment
Please, Sign In to add comment