@name Quadroped
@persist [E Pod]:entity [P]:array I Diff N N1 L Ang V:vector RH
@persist Dri:entity Act W A S D Spc Alt Shift M1 M2 #Pod shiet
@persist SP
@model models/hunter/plates/plate05x05.mdl
interval(10)
if(first())
{
    function number entity:cm()
    {
    local Mass = 0
    foreach(I,K:entity = This:getConstraints())
    {
    Mass+=K:mass()
    }
    return This:mass()+Mass
    }
    function entity:turnTo(Tarang:angle)
    {
    local V = This:toLocal(rotationVector(quat(Tarang)/quat(This))+This:pos())
    This:applyTorque((100*V - 30*This:angVelVector())*This:inertia()/3)
    }
    function entity:goto(Vector:vector)
    {
    This:applyForce(((Vector-This:pos())*10-This:vel())*This:mass())   
    }
    function entity:applySlow(V:vector)
    {
    This:applyForce((V-This:pos())*0.175*This:mass())
    }
    function ikS(Dist,I1,I2,Src:vector,End:vector)
    {
    local D = Src-End
    local L = min(D:length(),Dist)
    local A1 = D:toAngle()
    local A2 = ang(-asin(L/Dist),0,0)
    holoAng(I1,A1+A2)
    holoAng(I2,A1-A2)
    }
    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)))
    }
    
    soundPlay("idl",0,"ambient/machines/diesel_engine_idle1.wav")
    soundPitch("idl",SP)
    
    E = entity()
    E:propFreeze(0)
    E:setMass(1000)
    E:setMaterial("models/props_wasteland/tugboat02")
    E:setMass(1000)
    
    Diff = 150
    
    holoCreate(1,E:toWorld(vec(0,0,10)))
    holoAng(1,E:toWorld(ang(0,90,0)))
    holoModel(1,"models/nova/airboat_seat.mdl")
    holoAlpha(1,100)
    holoParent(1,E)
    
    holoCreate(10,E:toWorld(vec(E:boxSize():x()/2,E:boxSize():y()/2,0)))
    holoModel(10,"models/Combine_Helicopter/helicopter_bomb01.mdl")
    holoScale(10,vec(0.5))
    holoParent(10,E)
    holoCreate(11,holoEntity(10):toWorld(vec(0,0,Diff/2)))
    holoParent(11,10)
    holoModel(11,"hq_sphere")
    holoScale(11,vec(0.5))
    holoCreate(12,holoEntity(10):toWorld(vec()))
    holoParent(12,11)
    holoModel(12,"models/Combine_Helicopter/helicopter_bomb01.mdl")
    holoScale(12,vec(0.5))
    holoCreate(13,holoEntity(10):toWorld(vec(0,0,Diff/4)))
    holoAng(13,holoEntity(10):toWorld(ang(0,90,90)))
    holoModel(13,"models/props_c17/utilityconnecter005.mdl")
    holoScale(13,vec(2,Diff/32,2))
    holoParent(13,10)
    holoCreate(14,holoEntity(10):toWorld(vec(0,0,Diff/20)))
    holoModel(14,"models/props_wasteland/lighthouse_fresnel_light_base.mdl")
    holoScale(14,vec(0.1,0.1,0.0625*Diff/32))
    holoParent(14,11)
    
    holoMaterial(10,"models/props_wasteland/tugboat02")
    holoMaterial(11,"models/props_wasteland/tugboat02")
    holoMaterial(12,"models/props_wasteland/tugboat02")
    holoMaterial(13,"models/props_wasteland/tugboat02")
    holoMaterial(14,"models/props_wasteland/tugboat02")
    
    holoCreate(20,E:toWorld(vec(E:boxSize():x()/2,-E:boxSize():y()/2,0)))
    holoModel(20,"models/Combine_Helicopter/helicopter_bomb01.mdl")
    holoScale(20,vec(0.5))
    holoParent(20,E)
    holoCreate(21,holoEntity(20):toWorld(vec(0,0,Diff/2)))
    holoParent(21,20)
    holoModel(21,"hq_sphere")
    holoScale(21,vec(0.5))
    holoCreate(22,holoEntity(20):toWorld(vec()))
    holoParent(22,21)
    holoModel(22,"models/Combine_Helicopter/helicopter_bomb01.mdl")
    holoScale(22,vec(0.5))
    holoCreate(23,holoEntity(20):toWorld(vec(0,0,Diff/4)))
    holoAng(23,holoEntity(20):toWorld(ang(0,90,90)))
    holoModel(23,"models/props_c17/utilityconnecter005.mdl")
    holoScale(23,vec(2,Diff/32,2))
    holoParent(23,20)
    holoCreate(24,holoEntity(20):toWorld(vec(0,0,Diff/20)))
    holoModel(24,"models/props_wasteland/lighthouse_fresnel_light_base.mdl")
    holoScale(24,vec(0.1,0.1,0.0625*Diff/32))
    holoParent(24,21)
    
    holoMaterial(20,"models/props_wasteland/tugboat02")
    holoMaterial(21,"models/props_wasteland/tugboat02")
    holoMaterial(22,"models/props_wasteland/tugboat02")
    holoMaterial(23,"models/props_wasteland/tugboat02")
    holoMaterial(24,"models/props_wasteland/tugboat02")
    
    holoCreate(30,E:toWorld(vec(-E:boxSize():x()/2,E:boxSize():y()/2,0)))
    holoModel(30,"models/Combine_Helicopter/helicopter_bomb01.mdl")
    holoScale(30,vec(0.5))
    holoParent(30,E)
    holoCreate(31,holoEntity(30):toWorld(vec(0,0,Diff/2)))
    holoParent(31,30)
    holoModel(31,"hq_sphere")
    holoScale(31,vec(0.5))
    holoCreate(32,holoEntity(30):toWorld(vec()))
    holoParent(32,31)
    holoModel(32,"models/Combine_Helicopter/helicopter_bomb01.mdl")
    holoScale(32,vec(0.5))
    holoCreate(33,holoEntity(30):toWorld(vec(0,0,Diff/4)))
    holoAng(33,holoEntity(30):toWorld(ang(0,90,90)))
    holoModel(33,"models/props_c17/utilityconnecter005.mdl")
    holoScale(33,vec(2,Diff/32,2))
    holoParent(33,30)
    holoCreate(34,holoEntity(30):toWorld(vec(0,0,Diff/20)))
    holoModel(34,"models/props_wasteland/lighthouse_fresnel_light_base.mdl")
    holoScale(34,vec(0.1,0.1,0.0625*Diff/32))
    holoParent(34,31)
    
    holoMaterial(30,"models/props_wasteland/tugboat02")
    holoMaterial(31,"models/props_wasteland/tugboat02")
    holoMaterial(32,"models/props_wasteland/tugboat02")
    holoMaterial(33,"models/props_wasteland/tugboat02")
    holoMaterial(34,"models/props_wasteland/tugboat02")
    
    holoCreate(40,E:toWorld(vec(-E:boxSize():x()/2,-E:boxSize():y()/2,0)))
    holoModel(40,"models/Combine_Helicopter/helicopter_bomb01.mdl")
    holoScale(40,vec(0.5))
    holoParent(40,E)
    holoCreate(41,holoEntity(40):toWorld(vec(0,0,Diff/2)))
    holoParent(41,40)
    holoModel(41,"hq_sphere")
    holoScale(41,vec(0.5))
    holoCreate(42,holoEntity(40):toWorld(vec()))
    holoParent(42,41)
    holoModel(42,"models/Combine_Helicopter/helicopter_bomb01.mdl")
    holoScale(42,vec(0.5))
    holoCreate(43,holoEntity(40):toWorld(vec(0,0,Diff/4)))
    holoAng(43,holoEntity(40):toWorld(ang(0,90,90)))
    holoModel(43,"models/props_c17/utilityconnecter005.mdl")
    holoScale(43,vec(2,Diff/32,2))
    holoParent(43,40)
    holoCreate(44,holoEntity(40):toWorld(vec(0,0,Diff/20)))
    holoModel(44,"models/props_wasteland/lighthouse_fresnel_light_base.mdl")
    holoScale(44,vec(0.1,0.1,0.0625*Diff/32))
    holoParent(44,41)
    
    holoMaterial(40,"models/props_wasteland/tugboat02")
    holoMaterial(41,"models/props_wasteland/tugboat02")
    holoMaterial(42,"models/props_wasteland/tugboat02")
    holoMaterial(43,"models/props_wasteland/tugboat02")
    holoMaterial(44,"models/props_wasteland/tugboat02")
    
    I = 1
    P[I,entity] = propSpawn("models/hunter/blocks/cube025x025x025.mdl",holoEntity(I*10):pos()+vec(0,0,25),0)
    P[I,entity]:setAlpha(0)
    P[I,entity]:propNotSolid(1)
    I++
    timer("r",750)
}

if(L)
{
    Dri = Pod:driver()
    Act = Pod:driver():isValid()
    W = Dri:keyForward()
    A = Dri:keyLeft()
    S = Dri:keyBack()
    D = Dri:keyRight()
    Spc = Dri:keyJump()
    Alt = Dri:keyWalk()
    Shift = Dri:keySprint()
    M1 = Dri:keyAttack1()
    M2 = Dri:keyAttack2()
    
    if((A||D)&&(W||S)&&RH)
    {
        Ang += (A-D)*0.5*(1+Shift*1.5)/(1+Alt)
    }
    
    V = E:forward()*(S-W)*12.5*(1+Shift*1.5)/(1+Alt)
    N += (W-S)*5*(1+Shift*1.5)/(1+Alt)
    N1 = N-180
    
    if(Act)
    {
        if(SP<1)
        {
            SP+=0.025
        }
    }else
    {
        if(SP>0.025)
        {
            SP-=0.1
        }
    }
    SP = clamp(SP,0.025,1)
    soundPitch("idl",SP*100)
}else
{
    findByClass("prop_vehicle_prisoner_pod")
    findSortByDistance(E:pos())
    if(find():pos():distance(E:pos())<175&&find():owner()==owner()&&find():model()=="models/nova/airboat_seat.mdl")
    {
        Pod = find()
        Pod:setPos(holoEntity(1):pos())
        Pod:setAng(holoEntity(1):angles())
        Pod:propFreeze(1)
        timer("parent",100)
    }
    if(clk("parent"))
    {
        stoptimer("parent")
        Pod:parentTo(E)
        holoDelete(1)
        L = 1
    }
}

if(clk("r"))
{
    stoptimer("r")
    if(I<=4)
    {
        timer("r",750)
        P[I,entity] = propSpawn("models/hunter/blocks/cube025x025x025.mdl",holoEntity(I*10):pos()+vec(0,0,25),0)
        P[I,entity]:setAlpha(0)
        P[I,entity]:propNotSolid(1)
        I++
    }
}

for(I=1,4)
{
    local PE = P[I,entity]
    ikS(Diff,I*10,1+I*10,holoEntity(I*10):pos(),PE:pos())
}

rangerFilter(array(E):merge(players()):merge(P))
rangerPersist(1)
R = rangerOffset(50/(1+Alt),E:pos(),vec(0,0,-1))
RH = R:hit()
R1 = rangerOffset(Diff-5,E:toWorld(vec(50,50,0)),vec(0,0,-1))
R2 = rangerOffset(Diff-5,E:toWorld(vec(50,-50,0)),vec(0,0,-1))
R3 = rangerOffset(Diff-5,E:toWorld(vec(-50,50,0)),vec(0,0,-1))
R4 = rangerOffset(Diff-5,E:toWorld(vec(-50,-50,0)),vec(0,0,-1))

if(changed(Spc)&Spc&RH)
{
    applyForce(vec(0,0,500)*E:mass())
}

if(R:hit()&!(changed(Spc)&Spc))
{
    E:goto(R:pos()+vec(0,0,50/(1+Alt)+E:vel():z()/15)+V)
}
E:turnTo(ang(0,Ang,0))

if(R1:hit())
{
    if(W||S)
    {
        P[1,entity]:goto(E:toWorld(vec(50+cos(N)*30,50,clamp(sin(N)*17.5,0,20)+8-R1:distance())))
    }else
    {
        P[1,entity]:goto(R1:pos()+R1:hitNormal()*8)
    }
}else
{
    P[1,entity]:goto(E:toWorld(vec(75,75,50)))
}
if(R2:hit())
{
    if(W||S)
    {
        P[2,entity]:goto(E:toWorld(vec(50+cos(N1)*30,-50,clamp(sin(N1)*17.5,0,20)+8-R2:distance())))
    }else
    {
        P[2,entity]:goto(R2:pos()+R2:hitNormal()*8)
    }
}else
{
    P[2,entity]:goto(E:toWorld(vec(75,-75,50)))
}
if(R3:hit())
{
    if(W||S)
    {
        P[3,entity]:goto(E:toWorld(vec(-50+cos(N1)*30,50,clamp(sin(N1)*17.5,0,20)+8-R3:distance())))
    }else
    {
        P[3,entity]:goto(R3:pos()+R3:hitNormal()*8)
    }
}else
{
    P[3,entity]:goto(E:toWorld(vec(-75,75,50)))
}
if(R4:hit())
{
    if(W||S)
    {
        P[4,entity]:goto(E:toWorld(vec(-50+cos(N)*30,-50,clamp(sin(N)*17.5,0,20)+8-R4:distance())))
    }else
    {
        P[4,entity]:goto(R4:pos()+R4:hitNormal()*8)
    }
}else
{
    P[4,entity]:goto(E:toWorld(vec(-75,-75,50)))
}
