Advertisement
eyal282

Health Station VScript L4D2 ( .nut )

May 11th, 2023 (edited)
1,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function tick()
  2. {  
  3.     // Change this in the other function(s) too.
  4.     local healthPerSecond = 10;
  5.     local genericMax = 256;
  6.    
  7.     local ent = null;
  8.  
  9.     while((ent = Entities.FindByClassname(ent, "func_button_timed")))
  10.     {
  11.         // Target name should contain "health" and "station" ( health_station or health station )
  12.         if(NetProps.GetPropString(ent, "m_iName").find("health") == null || NetProps.GetPropString(ent, "m_iName").find("station") == null)
  13.             continue;
  14.            
  15.         local player = NetProps.GetPropEntity(ent, "m_hActivator")
  16.        
  17.         if(player == null || player.GetClassname() != "player" || player.IsDead() || !player.IsSurvivor() || player.IsIncapacitated())
  18.         {
  19.             NetProps.SetPropInt(ent, "m_nUseTime", genericMax);
  20.             continue;
  21.         }
  22.        
  23.         // Ignore health stations that started this instant.
  24.         else if(NetProps.GetPropFloat(player, "m_flProgressBarStartTime") == Time())
  25.             continue;
  26.        
  27.         // Ignore situations before we set the progress bar.
  28.         else if(NetProps.GetPropFloat(player, "m_flProgressBarDuration") == 0 || NetProps.GetPropInt(ent, "m_nUseTime") >= genericMax)
  29.             continue;
  30.            
  31.         local timeLeft = NetProps.GetPropFloat(player, "m_flProgressBarStartTime") + NetProps.GetPropFloat(player, "m_flProgressBarDuration") - Time()
  32.        
  33.         // If the remainder of the timeLeft is zero, it's a whole number, apply the healing now.
  34.         if(timeLeft % 1 == 0)
  35.         {
  36.             player.SetHealth(player.GetHealth() + healthPerSecond);
  37.            
  38.             // Reached max health? Reduce health to max health.
  39.             if(player.GetHealth() > player.GetMaxHealth())
  40.             {
  41.                 player.SetHealth(player.GetMaxHealth());
  42.             }
  43.            
  44.             // Temporary health pushes your health beyond the max health? Account for that as well!
  45.             if(player.GetHealth() + player.GetHealthBuffer() > player.GetMaxHealth())
  46.             {
  47.                 player.SetHealthBuffer(player.GetMaxHealth() - player.GetHealth())
  48.             }
  49.         }
  50.         // Took damage while using? Healed somehow while using? Force restart it.
  51.         else if(abs(player.GetHealth() + ceil(timeLeft).tointeger() * healthPerSecond - player.GetMaxHealth()) > healthPerSecond)
  52.         {
  53.             EntFire("!activator", "Disable", null, 0, ent)
  54.             EntFire("!activator", "Enable", null, 0.01, ent)
  55.         }
  56.        
  57.        
  58.         if(timeLeft <= 0)
  59.         {
  60.             EntFire("!activator", "Disable", null, 0, ent)
  61.             EntFire("!activator", "Enable", null, 1.0, ent)
  62.         }
  63.     }
  64.  
  65.     return 0.001; // can't actually rethink this fast, but returning lower than max think speed will cap to the fastest think possible.
  66. }
  67.  
  68. function OnHealthStationUsed()
  69. {
  70.     // Change this in the other function(s) too.
  71.     local healthPerSecond = 10;
  72.     local genericMax = 256;
  73.    
  74.     local ent = caller;
  75.    
  76.    
  77.     // Fetch player instead of using activator as the player may quickly release E which m_hActivator notices.
  78.     // There are also less sinister things about this like getting incapped at the split second of trying to heal.
  79.     local player = NetProps.GetPropEntity(ent, "m_hActivator")
  80.    
  81.     if(player == null || player.GetClassname() != "player" || player.IsDead() || !player.IsSurvivor() || player.IsIncapacitated())
  82.         return;
  83.    
  84.     // We don't care about temporary health just yet, as to calculate duration we pretend the player has 0 temporary health.
  85.     local duration = ceil((player.GetMaxHealth() - player.GetHealth()).tofloat() / healthPerSecond.tofloat()).tofloat();
  86.    
  87.  
  88.     NetProps.SetPropInt(ent, "m_nUseTime", duration.tointeger());  
  89.    
  90.     NetProps.SetPropFloat(player, "m_flProgressBarDuration", duration)
  91.    
  92.     NetProps.SetPropFloat(player, "m_flProgressBarStartTime", Time())
  93.  
  94.     if(duration < 1)
  95.     {
  96.         player.SetHealth(player.GetMaxHealth() - 1);
  97.        
  98.         duration = 1;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement