-----------------------------------------
-- MEDKIT_BACKPACK (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
local mdl = "models/v92/bf2/weapons/handheld/medikit_w.mdl"
local bags = {}

-- Настраиваемые конвары для отладки
CreateClientConVar("v92_med_back_right",   "3", true, false)
CreateClientConVar("v92_med_back_forward", "3", true, false)
CreateClientConVar("v92_med_back_up",      "3", true, false)

CreateClientConVar("v92_med_rot_right",    "7", true, false)
CreateClientConVar("v92_med_rot_up",       "0", true, false)
CreateClientConVar("v92_med_rot_forward",  "90", true, false)

CreateClientConVar("v92_med_bone", "ValveBiped.Bip01_Spine2", true, false)

local function GetOffsets()
    local cRight   = GetConVar("v92_med_back_right")
    local cForward = GetConVar("v92_med_back_forward")
    local cUp      = GetConVar("v92_med_back_up")

    local right   = cRight   and cRight:GetFloat()   or 0
    local forward = cForward and cForward:GetFloat() or 0
    local up      = cUp      and cUp:GetFloat()      or 0

    return right, forward, up
end

local function GetRotations()
    local cR = GetConVar("v92_med_rot_right")
    local cU = GetConVar("v92_med_rot_up")
    local cF = GetConVar("v92_med_rot_forward")

    local r = cR and cR:GetFloat() or 0
    local u = cU and cU:GetFloat() or 0
    local f = cF and cF:GetFloat() or 0

    return r, u, f
end

local medikitRenderData = {}

hook.Add("PostPlayerDraw", "V92_Medikit_Draw", function(ply)
    if not IsValid(ply) or not ply:Alive() then return end

    -- Не рисуем на себе в первом лице
    if ply == LocalPlayer() and not ply:ShouldDrawLocalPlayer() then
        return
    end

    local boneName = GetConVar("v92_med_bone"):GetString() or "ValveBiped.Bip01_Spine2"

    -- Проверяем наличие аптечки
    local hasBag = false
    for _, ent in ipairs(ents.FindByClass("v92_bf2_medikit_ent")) do
        if ent:GetNWBool("V92_Medikit_IsAttached", false)
        and ent:GetNWEntity("V92_Medikit_AttachedPlayer") == ply then
            hasBag = true
            break
        end
    end

    if not hasBag then
        if IsValid(bags[ply]) then bags[ply]:Remove() end
        bags[ply] = nil
        return
    end

    if not IsValid(bags[ply]) then
        bags[ply] = ClientsideModel(mdl, RENDERGROUP_OPAQUE)
        bags[ply]:SetNoDraw(true)
    end

    local bag = bags[ply]

    ply:SetupBones()

    local bone = ply:LookupBone(boneName)
    if not bone then return end

    local m = ply:GetBoneMatrix(bone)
    if not m then return end

    local pos = m:GetTranslation()
    local ang = m:GetAngles()

    -- Смещения
    local rightOff, forwardOff, upOff = GetOffsets()
    local rotRight, rotUp, rotForward = GetRotations()

    ang:RotateAroundAxis(ang:Right(),   rotRight)
    ang:RotateAroundAxis(ang:Up(),      rotUp)
    ang:RotateAroundAxis(ang:Forward(), rotForward)

    pos = pos + ang:Right() * rightOff + ang:Forward() * forwardOff + ang:Up() * upOff

    bag:SetPos(pos)
    bag:SetAngles(ang)
    bag:DrawModel()
end)

hook.Add("PlayerDisconnected", "V92_Medikit_Cleanup", function(ply)
    if bags[ply] then
        bags[ply]:Remove()
        bags[ply] = nil
    end
end)

hook.Add("PlayerDeath", "V92_Medikit_CleanupDeath", function(ply)
    if bags[ply] then
        bags[ply]:Remove()
        bags[ply] = nil
    end
end)
-----------------------------------------
-- MEDKIT_BACKPACK (MILITARY RP)
-- Собственность Олега Закона и Scripty
-----------------------------------------
