@name Flying lawnmower
@persist Pln Act Ign 
@persist W A S D Spa Alt Shi Thr
@persist OX OY H Sp Radius N Y
@persist Fly SpV
@persist [Pod E]:entity
@persist [Data]:array
@persist [P2H_Mod P2H_Color P2H_Mat P2H_Pos P2H_Ang]:array P2H_Int P2H_Offset P2H_Ready P2H_Base:entity
@model models/hunter/plates/plate1x1.mdl
if(first())
{
    runOnTick(1)
    
    Thr = 3
    
    E = entity()
    E:setMass(500)
    E:propFreeze(0)
    E:propNotSolid(0)
    E:setColor(0, 0, 0, 0)
    
    OX = E:boxSize():x()/3
    OY = E:boxSize():x()/2.45
    H = 12.5
    Sp = 3.5
    
    holoCreate(1)
    holoCreate(2)
    holoCreate(3)
    holoCreate(4)
    
    local MDL = "models/sprops/trans/wheel_a/t_wheel15.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)
    
    holoCreate(5, E:toWorld(vec(0, -E:boxSize():y()*0.7, 7.5)))
    holoAng(5, E:toWorld(ang(0, 0, 90)))
    holoModel(5, "models/xqm/afterburner1.mdl")
    holoParent(5, E)
    
    Radius = holoEntity(1):radius()
    
    holoCreate(2500, E:toWorld(vec(0, 0, 20)))
    holoModel(2500, "models/nova/airboat_seat.mdl")
    holoAng(2500, E:toWorld(ang(0, 0, 0)))
    holoAlpha(2500, 150)
    holoParent(2500, E)
    
    #include "lib/functions"
    
    P2H_Base = holoCreate(6, E:toWorld(vec(0, 0, 2.5)), vec(0), E:toWorld(ang(0, 90, 0)))
    holoParent(6, E)
    
    P2H_Int = 0
    P2H_Offset = 6
    P2H_Mod = array("models/sprops/rectangles/size_2_5/rect_18x18x3.mdl", "models/sprops/trans/wheel_a/t_wheel45.mdl", "models/sprops/rectangles/size_2_5/rect_18x18x3.mdl", "models/sprops/rectangles/size_2_5/rect_18x18x3.mdl", "models/sprops/rectangles/size_2_5/rect_18x18x3.mdl", "models/props_c17/trappropeller_engine.mdl", "models/sprops/cylinders/size_2/cylinder_3x12.mdl", "models/sprops/cylinders/size_2/cylinder_3x12.mdl", "models/sprops/cylinders/size_2/cylinder_3x66.mdl", "models/sprops/cylinders/size_2/cylinder_3x66.mdl", "models/sprops/cylinders/size_2/cylinder_3x6.mdl", "models/sprops/cylinders/size_2/cylinder_3x12.mdl", "models/sprops/geometry/sphere_3.mdl", "models/sprops/cylinders/size_2/cylinder_3x12.mdl", "models/sprops/geometry/sphere_3.mdl", "models/sprops/cylinders/size_2/cylinder_3x6.mdl", "models/props_junk/sawblade001a.mdl")
    P2H_Color = array(vec4(0, 140, 0, 255), vec4(0, 140, 0, 255), vec4(0, 140, 0, 255), vec4(0, 140, 0, 255), vec4(0, 140, 0, 255), vec4(127, 127, 127, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255), vec4(113, 113, 113, 255))
    P2H_Mat = array("sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1", "sprops/trans/wheels/wheel_d_rim1")
    P2H_Pos = array(vec(9, -9, -0.2021), vec(0, 0, 4.2979), vec(9, 9, -0.2022), vec(-9, 9, -0.2022), vec(-9, -9, -0.2021), vec(0, 2.5, 12.5), vec(-42.1782, -5.7332, 35.9981), vec(-42.1782, 5.7338, 35.998), vec(-36.8123, 12.4824, 30.6327), vec(-36.8123, -12.4819, 30.6329), vec(-61.0557, -10.0235, 55.4678), vec(-62.5051, -2.3573, 57.5938), vec(-62.4905, -8.461, 57.5889), vec(-62.5051, 2.3582, 57.5937), vec(-62.4905, 8.4619, 57.5888), vec(-61.0557, 10.0244, 55.4687), vec(0, 0, -2.5))
    P2H_Ang = array(ang(0, -0.0001, -0.0004), ang(0, 0, -90.0004), ang(0, 0, -0.0005), ang(0, 180, 0.0005), ang(0, -179.9999, 0.0004), ang(-90, -90, 90), ang(-45, 179.9995, 90.0006), ang(-45, 179.9995, -89.9994), ang(-45, 2.5004, -0.0006), ang(-45, -2.4996, -0.0006), ang(-45.0003, -47.4389, -0.0005), ang(-89.9995, -89.9998, 0), ang(0, 0, -0.0004), ang(-89.9995, 89.9998, 0), ang(0, 0, -0.0004), ang(-44.9997, 47.4395, -0.0003), ang())
}

if(!P2H_Ready)
{
    while(holoCanCreate() & perf() & P2H_Int < P2H_Mod:count())
    {
        P2H_Int++
        local P2H_TempInt = P2H_Int+P2H_Offset
        holoCreate(P2H_TempInt, P2H_Base:toWorld(P2H_Pos[P2H_Int, vector]), vec(1), P2H_Base:toWorld(P2H_Ang[P2H_Int, angle]))
        holoColor(P2H_TempInt, P2H_Color[P2H_Int, vector4])
        holoModel(P2H_TempInt, P2H_Mod[P2H_Int, string])
        holoMaterial(P2H_TempInt, P2H_Mat[P2H_Int, string])
        holoParent(P2H_TempInt, E)
    }
    P2H_Ready = P2H_Int >= P2H_Mod:count()
}elseif(changed(P2H_Ready))
{
    holoScale(23, vec(1.25))
}

if(Pln & P2H_Ready)
{
    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
        }
    }
    
    SpV += Act ? 0.0125 : -0.0125
    SpV = clamp(SpV, 0, 1)
    holoAng(23, holoEntity(23):toWorld(ang(0, SpV*25, 0)))
    
    if(changed(Shi)&Shi)
    {
        Fly = !Fly
        Pod:hintDriver("Fly toggled to "+Fly, 3)
    }

    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)*2*!Spa
    
    N+=RPT/(1+Spa*5)
    
    local Div = 4
    if(A||D)
    {
        Y+=(D-A)/Div
    }else
    {
        if(inrange(Y, -0.25, 0.25))
        {
            Y = 0
        }else
        {
            Y += Y > 0 ? -1/Div : 1/Div
        }
    }
    Y  = clamp(Y, -5, 5)
    
    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/150, 0)*Mass))
        E:applyForce(E:toWorldAxis(vec(0, Gas, 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/150, 0)*Mass))
        E:applyForce(E:toWorldAxis(vec(0, Gas, 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.06125, 0.0025+Spa/150, 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.06125, 0.0025+Spa/150, 0)*Mass))
        
        Data[4, number] = BRD
    }
    
    if(changed(Fly))
    {
        if(Fly)
        {
            Pod:soundPlay("mus", 92, "chaq/DuckTalesMoon.mp3")
            timer("mus_rep", 92000)
            soundPlay("jet", 0, "vehicles/airboat/fan_motor_fullthrottle_loop1.wav")
            soundPitch("jet", 255)
            soundVolume("jet", 0.25)
            holoCreate(104, holoEntity(5):pos())
            holoParent(104, E)
            holoAlpha(104, 0)
            holoEntity(104):setTrails(12, 1000, 7, "trails/smoke", vec(200), 100)
        }else
        {
            soundStop("mus")
            stoptimer("mus_rep")
            soundStop("jet")
            holoUnparent(104)
        }
    }
    if(Fly)
    {
        if(clk("mus_rep"))
        {
            stoptimer("mus_rep")
            soundStop("mus")
            Pod:soundPlay("mus", 0, "chaq/DuckTalesMoon.mp3")
            timer("mus_rep", 92000)
        }
        E:applyForce(-E:right()*Thr*2*E:mass())
        E:applyForce(E:toWorldAxis(-Vel*vec(1, 0.0025, 1)*Mass))
        
        if(changed(R)&R)
        {
            Thr++
            Pod:hintDriver("Throttle: "+Thr, 3)
        }elseif(changed(F)&F)
        {
            Thr--
            Pod:hintDriver("Throttle: "+Thr, 3)
        }
        
        FWN = clamp(Vel:y(), -1, 1)
        
        FA = ang((D-A)*3, (M1-M2)*1, (S-W)*2)*12.5*FWN
        
        E:applyAngForce((FA-E:angVel()/2.75)*E:mass())
    }
    else
    {
        E:applyAngForce(ang(clamp(E:angVel():yaw()*1.25*Vel:y(), -110000, 110000), (-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+Y, 0)))
    holoAng(4, E:toWorld(ang(N*BR:hit(), 90+Y, 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:setColor(0, 0, 0, 0)
            Pod:setPos(holoEntity(2500):pos())
            Pod:setAng(holoEntity(2500):angles())
            Pod:propFreeze(1)
            holoDelete(2500)
            timer("pln", 100)
        }
    }
}
