--[[ | This file was obtained through the combined efforts | of Madbluntz & Plymouth Antiquarian Society. | | Credits: lifestorm, Gregory Wayne Rossel JR., | Maloy, DrPepper10 @ RIP, Atle! | | Visit for more: https://plymouth.thetwilightzone.ru/ --]] PLUGIN.name = "Stamina" PLUGIN.author = "Chessnut" PLUGIN.description = "Adds a stamina system to limit running." -- luacheck: push ignore 631 ix.config.Add("staminaDrain", 1, "Quantité d'endurance à drainer par tic (tous les quarts de seconde). Ceci est calculé avant la réduction des attributs.", nil, { data = {min = 0, max = 10, decimals = 2}, category = "Personnages" }) ix.config.Add("staminaRegeneration", 1.75, "Combien d'endurance à récupérer par tic (tous les quarts de seconde).", nil, { data = {min = 0, max = 10, decimals = 2}, category = "Personnages" }) ix.config.Add("staminaCrouchRegeneration", 2, "Combien d'endurance il faut regagner par tic (tous les quarts de seconde) en étant accroupi.", nil, { data = {min = 0, max = 10, decimals = 2}, category = "Personnages" }) ix.config.Add("punchStamina", 10, "La quantité d'endurance utilisée par les coups de poing.", nil, { data = {min = 0, max = 100}, category = "Personnages" }) -- luacheck: pop local function CalcStaminaChange(client) local character = client:GetCharacter() if (!character or client:GetMoveType() == MOVETYPE_NOCLIP) then return 0 end local runSpeed if (SERVER) then runSpeed = ix.config.Get("runSpeed") + character:GetAttribute("stm", 0) if (client:WaterLevel() > 1) then runSpeed = runSpeed * 0.775 end end local walkSpeed = ix.config.Get("walkSpeed") local maxAttributes = ix.config.Get("maxAttributes", 100) local offset if (client:KeyDown(IN_SPEED) and client:GetVelocity():LengthSqr() >= (walkSpeed * walkSpeed)) then -- characters could have attribute values greater than max if the config was changed offset = -ix.config.Get("staminaDrain", 1) + math.min(character:GetAttribute("end", 0), maxAttributes) / 100 else offset = client:Crouching() and ix.config.Get("staminaCrouchRegeneration", 2) or ix.config.Get("staminaRegeneration", 1.75) end offset = hook.Run("AdjustStaminaOffset", client, offset) or offset if (CLIENT) then return offset -- for the client we need to return the estimated stamina change else local current = client:GetLocalVar("stm", 0) local maxStamina = character:GetMaxStamina() or 100 local value = math.Clamp(current + offset, 0, maxStamina) if (current != value) then client:SetLocalVar("stm", value) if (value == 0 and !client:GetNetVar("brth", false)) then client:SetRunSpeed(walkSpeed) client:SetNetVar("brth", true) character:UpdateAttrib("end", 0.1) character:UpdateAttrib("stm", 0.01) hook.Run("PlayerStaminaLost", client) elseif (value >= (maxStamina >= 50 and 50 or maxStamina) and client:GetNetVar("brth", false)) then client:SetRunSpeed(runSpeed) client:SetNetVar("brth", nil) hook.Run("PlayerStaminaGained", client) end end end end local CHAR = ix.meta.character function CHAR:GetMaxStamina() return 100 end if (SERVER) then function PLUGIN:PostPlayerLoadout(client) local uniqueID = "ixStam" .. client:SteamID() timer.Create(uniqueID, 0.25, 0, function() if (!IsValid(client)) then timer.Remove(uniqueID) return end CalcStaminaChange(client) end) end function PLUGIN:CharacterPreSave(character) local client = character:GetPlayer() if (IsValid(client)) then character:SetData("stamina", client:GetLocalVar("stm", 0)) end end function PLUGIN:PlayerLoadedCharacter(client, character) local maxStamina = character:GetMaxStamina() or 100 timer.Simple(0.25, function() client:SetLocalVar("stm", character:GetData("stamina", maxStamina)) end) end local playerMeta = FindMetaTable("Player") function playerMeta:RestoreStamina(amount) local current = self:GetLocalVar("stm", 0) local maxStamina = self:GetCharacter() and self:GetCharacter():GetMaxStamina() or 100 local value = math.Clamp(current + amount, 0, maxStamina) self:SetLocalVar("stm", value) end function playerMeta:ConsumeStamina(amount) local current = self:GetLocalVar("stm", 0) local maxStamina = self:GetCharacter() and self:GetCharacter():GetMaxStamina() or 100 local value = math.Clamp(current - amount, 0, maxStamina) self:SetLocalVar("stm", value) end else local predictedStamina = 100 function PLUGIN:Think() local client = LocalPlayer() local maxStamina = client:GetCharacter() and client:GetCharacter():GetMaxStamina() or 100 local offset = CalcStaminaChange(client) -- the server check it every 0.25 sec, here we check it every [FrameTime()] seconds offset = math.Remap(FrameTime(), 0, 0.25, 0, offset) if (offset != 0) then predictedStamina = math.Clamp(predictedStamina + offset, 0, maxStamina) end end function PLUGIN:OnLocalVarSet(key, var) if (key != "stm") then return end if (math.abs(predictedStamina - var) > 5) then predictedStamina = var end end ix.bar.Add(function() local client = LocalPlayer() local maxStamina = client:GetCharacter() and client:GetCharacter():GetMaxStamina() or 100 return predictedStamina / maxStamina end, Color(200, 200, 40), nil, "stm") end