@name Punching Arm
@inputs T Dr
@persist [E H1 Chassis]:entity
@persist [V1]:vector
@persist OX OY OZ L1 L2 Y
@model models/hunter/plates/plate.mdl
if(first())
{
    runOnTick(1)
    
    E = propSpawn("models/props_junk/PropaneCanister001a.mdl",entity():toWorld(vec(0,0,25)),0)
    E:setMass(50000)
    E:setAlpha(0)
    
    OX = -2.5
    OY = 25
    OZ = 45
    L1 = 200
    L2 = 200
    
    Chassis = entity()
    Chassis:setAlpha(0)
    
    H1 = holoCreate(1,Chassis:toWorld(vec(-OY,OX,OZ)))
    holoAng(1,Chassis:toWorld(ang(0,90,0)))
    holoParent(1,entity())
    holoModel(1,"hq_sphere")
    holoScale(1,vec(1.25))
    holoCreate(2,H1:toWorld(vec(0,0,L1)))
    holoAng(2,H1:toWorld(ang()))
    holoParent(2,1)
    holoModel(2,"hq_sphere")
    holoScale(2,vec(1.25))
    holoCreate(3,holoEntity(2):toWorld(vec(-14,0,L2)))
    holoAng(3,holoEntity(2):toWorld(ang(-63,-124,30)))
    holoParent(3,2)
    holoClipEnabled(3,1)
    holoModel(3,"models/weapons/c_models/c_boxing_gloves/c_boxing_gloves.mdl")
    
    holoCreate(4,H1:toWorld(vec(0,0,L1/2)))
    holoAng(4,H1:toWorld(ang()))
    holoScale(4,vec(0.75,0.75,L1/12))
    holoParent(4,1)
    holoModel(4,"hq_cylinder")
    holoCreate(5,holoEntity(2):toWorld(vec(0,0,L2/2)))
    holoAng(5,holoEntity(2):toWorld(ang()))
    holoScale(5,vec(0.75,0.75,L2/12))
    holoParent(5,2)
    holoModel(5,"hq_cylinder")
    
    for(I=1,5)
    {
        holoColor(I,vec(150))
        holoMaterial(I,"models/shiny")
    }
    
    rangerFilter(E)
    rangerPersist(1)
    
    function entity:goto(Vector:vector)
    {
        This:applyForce(((Vector-This:pos())*25-This:vel())*This:mass())   
    }
    function number cosine(A, B, C)
    {
        return acos((A^2 + B^2 - C^2) / (2*A*B))
    }
    function ik(L1,L2,Hip:entity,Foot:entity,Chassis:entity,I1,I2)
    {    
        local Axis = Chassis:toLocalAxis(Foot:pos() - Hip:pos())
        local Angle = Axis:toAngle():setRoll(-bearing(Hip:pos(), Chassis:angles(), Foot:pos()))
        local Dist = min(Axis:length(), L1 + L2)
        local Quat = quat(Angle) * qRotation(vec(0,90 + cosine(Dist, L1, L2),0))
        
        holoAng(I1, Chassis:toWorld(Quat:toAngle()))
        holoAng(I2, Hip:toWorld(ang(cosine(L2, L1, Dist) + 180, 0, 0)))
    }
}

Chassis:setPos(owner():pos()+vec(0,0,125))
Chassis:setAng(owner():eyeAngles():setPitch(0)-ang(0,90,0))

T = owner():keyReload()

if(T&!Dr)
{
    local Trace = (owner():eyeTrace():pos()-H1:pos()):normalized()
    TP = rangerOffset(L1+L2-10,H1:pos(),Trace)
    V1 = TP:pos()+Trace*10
}else
{
    V1 =  Chassis:toWorld(vec(-OY+12,OX+17.5,OZ-5))
}

holoClip(3,holoEntity(3):pos(),-holoEntity(3):right(),1)

E:goto(V1)

ik(L1,L2,H1,E,Chassis,1,2)
