@name Chaqua Car Base
@persist Pln Act Ign 
@persist W A S D Spa Alt Shi
@persist OX OY H Sp Radius N Y
@persist [Pod E]:entity
@persist [Data]:array
@model models/hunter/plates/plate2x3.mdl
if(first())
{
    runOnTick(1)
    
    E = entity()
    E:setColor(0,0,0,0)
    E:setMass(500)
    E:propFreeze(0)
    E:propNotSolid(0)
    
    OX = 55
    OY = 35
    H = 35
    Sp = 4
    
    holoCreate(1)
    holoCreate(2)
    holoCreate(3)
    holoCreate(4)
    
    local MDL = "models/sprops/trans/wheel_b/t_wheel30.mdl"
    holoModel(1,MDL)
    holoModel(2,MDL)
    holoModel(3,MDL)
    holoModel(4,MDL)
    
    holoParent(1,E)
    holoParent(2,E)
    holoParent(3,E)
    holoParent(4,E)
    Radius = holoEntity(1):radius()
    
    holoCreate(5,E:toWorld(vec(0,7.5,0)),vec(1),E:toWorld(ang(0,90,0)))
    holoModel(5,"models/props_vehicles/car005a_physics.mdl")
    holoParent(5,E)
    
    holoCreate(2500,E:toWorld(vec(-12,3,-4)))
    holoModel(2500,"models/nova/airboat_seat.mdl")
    holoAng(2500,E:toWorld(ang(0,0,0)))
    holoAlpha(2500,150)
    holoParent(2500,E)
    
    #include "lib/functions"
    
    rangerHitWater(1)
    rangerPersist(1)
}

if(Pln)
{
    Act = Pod:driver():isValid()
    Dri = Pod:driver()
    W = Dri:keyForward()
    A = Dri:keyLeft()
    S = Dri:keyBack()
    D = Dri:keyRight()
    R = Dri:keyReload()
    F = Dri:keyPressed("F")
    Spa = Dri:keyJump()
    Alt = Dri:keyWalk()
    Shi = Dri:keySprint()
    M1 = Dri:keyAttack1()
    M2 = Dri:keyAttack2()
    
    if(changed(Act))
    {
        if(Act)
        {
            soundStop("sto")
            soundPlay("sta",0,"vehicles/tractor/tractor_start_loop.wav")
            soundVolume("sta",0.25)
        }else
        {
            soundStop("sta")
            soundPlay("sto",2.25,"vehicles/tractor/tractor_stop.wav")
            soundVolume("sto",0.25)
            Fly = 0
        }
    }

    Up = E:up()
    Mass = E:mass()
    Vel = E:velL()
    YN = Vel:setZ(0):setX(0):length()*clamp(Vel:y(),-1,1)
    RPT=YN/66/2/pi()/Radius*360
    
    Gas = !Fly*(W-S)*(5+(Shi*10)-(Alt*4))*!Spa
    
    N+=RPT/(1+Spa*5)
    
    local Div = 1.5
    if(A||D)
    {
        Y+=(D-A)/Div
    }else
    {
        if(inrange(Y,-0.5,0.5))
        {
            Y = 0
        }else
        {
            Y += Y > 0 ? -1/Div : 1/Div
        }
    }
    Y  = clamp(Y,-25,25)
    
    rangerFilter(E:getConstraints():merge(players()):merge(array(Pod,Dri)))
    FL = rangerOffset(H,E:toWorld(vec(OY,OX,-1.5)),-Up)
    FR = rangerOffset(H,E:toWorld(vec(-OY,OX,-1.5)),-Up)
    BL = rangerOffset(H,E:toWorld(vec(OY,-OX,-1.5)),-Up)
    BR = rangerOffset(H,E:toWorld(vec(-OY,-OX,-1.5)),-Up)
    
    holoPos(1,FL:position()+Up*Radius/1.5)
    holoPos(2,FR:position()+Up*Radius/1.5)
    holoPos(3,BL:position()+Up*Radius/1.5)
    holoPos(4,BR:position()+Up*Radius/1.5)
    
    FLD = max(1,H-FL:distance())
    if(FL:hit())
    {
        E:applyOffsetForce((FLD*Sp - Data[1,number]*(Sp-0.5))*Up*Mass,FL:position())
        E:applyForce(E:toWorldAxis(-Vel*vec(0.1,0.0025+Spa/250,0)*Mass))
        
        Data[1,number] = FLD
    }
    FRD = max(1,H-FR:distance())
    if(FR:hit())
    {
        E:applyOffsetForce((FRD*Sp - Data[2,number]*(Sp-0.5))*Up*Mass,FR:position())
        E:applyForce(E:toWorldAxis(-Vel*vec(0.1,0.0025+Spa/250,0)*Mass))
        
        Data[2,number] = FRD
    }
    BLD = max(1,H-BL:distance())
    if(BL:hit())
    {
        E:applyOffsetForce((BLD*Sp - Data[3,number]*(Sp-0.5))*Up*Mass,BL:position())
        E:applyForce(E:toWorldAxis(-Vel*vec(0.0040625,0.0025+Spa/250,0)*Mass))
        E:applyForce(E:toWorldAxis(vec(0,Gas,0)*Mass))
        
        Data[3,number] = BLD
    }
    BRD = max(1,H-BR:distance())
    if(BR:hit())
    {
        E:applyOffsetForce((BRD*Sp - Data[4,number]*(Sp-0.5))*Up*Mass,BR:position())
        E:applyForce(E:toWorldAxis(-Vel*vec(0.0040625,0.0025+Spa/250,0)*Mass))
        E:applyForce(E:toWorldAxis(vec(0,Gas,0)*Mass))
        
        Data[4,number] = BRD
    }
    
    E:applyAngForce(ang(clamp(E:angVel():yaw()*Vel:y(),-110000,110000)*2,(-Y*YN*Mass)/2,0)*(FL:hit()|FR:hit()|BL:hit()|BR:hit())-E:angVel()*ang(0,50,0)*shiftL(ang(E:inertia())))
    
    if(changed(M2))
    {
        if(M2)
        {
            soundPlay("honk",0.25,"synth/saw_440.wav")
        }
    }
    
    holoAng(1,E:toWorld(ang(N*BR:hit(),90-Y,0)))
    holoAng(2,E:toWorld(ang(N*BR:hit(),90-Y,0)))
    holoAng(3,E:toWorld(ang(N*BR:hit(),90,0)))
    holoAng(4,E:toWorld(ang(N*BR:hit(),90,0)))
    
}else
{
    if(Pod:isValid())
    {
        if(clk("pln"))
        {
            stoptimer("pln")
            Pod:parentTo(E)
            Pln = 1
        }
    }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(2500):pos())
            Pod:setAng(holoEntity(2500):angles())
            Pod:propFreeze(1)
            holoDelete(2500)
            timer("pln",100)
        }
    }
}
