@name Hover Go Kart
@outputs T Dr
@persist [E GB TE]:entity
@persist [V]:vector
@persist [Pod]:entity
@persist Toggle Speed SpeedMul W A S D Shift Alt Active TM ChairOn
@model models/hunter/plates/plate1x1.mdl
if(first())
{
    E = entity()
    
    #include "lib/functions"
    
    soundPlay("active",0,"ambient/machines/turbine_loop_1.wav")
    soundPitch("active",0)
    
    soundPlay("move",0,"weapons/airboat/airboat_gun_loop2.wav")
    soundPitch("move",0)
    
    runOnChat(1)
    runOnTick(1)
    E:propFreeze(0)
    setMass(200)
    
    holoCreate(1)
    holoModel(1,"models/props_canal/boat002b.mdl")
    holoAng(1,E:toWorld(ang(0,-90,0)))
    holoParent(1,E)
    
    holoCreate(3,E:toWorld(vec(0,0,5)))
    holoAng(3,E:toWorld(ang(0,180,0)))
    holoModel(3,"models/nova/airboat_seat.mdl")
    holoAlpha(3,150)
    holoParent(3,E)
    
    GB = holoCreate(10,E:toWorld(vec(0,-24,5)))
    holoModel(10,"hq_sphere")
    holoParent(10,E)
    holoMaterial(10,"models/shiny")
    holoColor(10,vec(150))
    
    holoCreate(11,GB:toWorld(vec(24,0,0)))
    holoAng(11,GB:toWorld(ang(90,0,0)))
    holoModel(11,"hq_tube")
    holoScale(11,vec(0.8,0.8,4))
    holoParent(11,10)
    holoMaterial(11,"models/shiny")
    holoColor(11,vec(150))
    
    holoCreate(100,E:toWorld(vec(E:boxSize():x(),E:boxSize():y(),0)/2))
    holoCreate(101,E:toWorld(vec(-E:boxSize():x(),E:boxSize():y(),0)/2))
    holoAlpha(100,0)
    holoAlpha(101,0)
    holoParent(100,E)
    holoParent(101,E)
    holoEntity(100):setTrails(50,0,2,"trails/laser",vec(0,255,0),255)
    holoEntity(101):setTrails(50,0,2,"trails/laser",vec(0,255,0),255)
    
    function number entity:cm()
    {
        local Mass = 0
        for(I=1,This:getConstraints():count())
        {
            Mass+=This:getConstraints()[I,entity]:mass()
        }
        return E:mass()+Mass
    }
    function entity:makeDrive(L,R,TV,RV,TUI,TUV)
    {
        local P = (TUI*TUV)
        local Y = (L-R)*TV
        local R = (L-R)*RV
        local FA = ((ang(R,0,(-abs(Y)/6)-P)))-This:angles()
        This:applyAngForce((FA:setYaw(Y)*300*This:cm()/50)-This:angVel()*2*This:cm())
    }
    
    timer("tp",50)
}

if(clk("tp")){ stoptimer("tp") entity():setPos(owner():aimPos()) }

if(ChairOn)
{
    if(chatClk(owner()))
    {
        LS = owner():lastSaid():lower()
        if(LS=="/k")
        {
            hideChat(1)
            Pod:ejectPod()
        }
    }
    
    if(Pod:isValid())
    {
        Dri = Pod:driver()
        Active = Dri:isValid()
        W = Dri:keyForward()
        A = Dri:keyLeft()
        S = Dri:keyBack()
        D = Dri:keyRight()
        Rk = Dri:keyReload()
        Shift = Dri:keySprint()
        Space = Dri:keyJump()
        Alt = Dri:keyWalk()
    }
    
    if(changed(Rk))
    {
        if(Rk)
        {
            Toggle = !Toggle
        }
    }
    
    if(changed(Active))
    {
        if(Active)
        {
            
        }else
        {
            Toggle = 0
        }
    }
    if(changed(Toggle))
    {
        if(Toggle)
        {
            soundPlay("activate",2,"ambient/machines/thumper_startup1.wav")
            soundStop("shutdown")
            holoPos(100,E:toWorld(vec(E:boxSize():x(),E:boxSize():y(),0)/2))
            holoPos(101,E:toWorld(vec(-E:boxSize():x(),E:boxSize():y(),0)/2))
            holoParent(100,E)
            holoParent(101,E)
            holoEntity(100):setTrails(50,0,2,"trails/laser",vec(0,255,0),255)
            holoEntity(101):setTrails(50,0,2,"trails/laser",vec(0,255,0),255)
        }else
        {
            soundPlay("shutdown",5,"ambient/machines/thumper_shutdown1.wav")
            soundStop("activate")
            holoUnparent(100)
            holoUnparent(101)
        }
    }
    
    if(Toggle)
    {
        TM+=0.05
    }else
    {
        TM-=0.05
    }
    TM = clamp(TM,0,1)
    soundPitch("active",TM*60+(E:vel():setZ(0):length()/12)*TM)
    soundPitch("move",(E:vel():setZ(0):length()/55)*TM)
    
    if(W||S)
    {
        SpeedMul+=0.075
    }else
    {
        SpeedMul-=0.1
    }
    SpeedMul = clamp(SpeedMul,0,1)
    
    if(Active&Toggle)
    {
        VelL = E:velL()
        Speed = (25-(Alt*15)+(Space*35))*(W-S)*SpeedMul
        rangerHitWater(1)
        R = rangerOffset(70,E:pos(),-E:up())
        V = vec(0,0,(R:hit()*((R:pos():z()+70)-E:pos():z()))*E:cm())
        AV = E:toWorldAxis(-VelL*vec(0.125/2,0.015+(!(W||S)*0.025),0.12-(0.0775*!Shift))*E:cm())
        MV = E:right()*Speed*(E:cm())
        E:makeDrive(A,D,25,10,Shift*(W-S),6)
        E:applyForce(V+AV+MV)
        T = owner():keyAttack1()
        Dr = W||S
        
        local A = Dri:eyeAngles()
        holoAng(10,A)
        
        if(changed(Dri:keyAttack1())&&Dri:keyAttack1()&&Dri:aimEntity()!=E&!TE:isValid())
        {
            soundPlay("fire",3,"weapons/rocket_blackbox_shoot.wav")
            TE = propSpawn("models/props_phx/amraam.mdl",GB:toWorld(vec(100,0,0)),A,0)
            TE:propGravity(0)
            TE:setTrails(100,0,2,"trails/laser",vec(0,255,0),255)
        }
    }
    
    if(duped()|dupefinished())
    {
        reset()
    }
    
    if(TE:isValid())
    {
        TE:applyForce(TE:toWorldAxis(-TE:velL()*vec(0,1,1)*TE:mass()))
        TE:applyForce(TE:forward()*100*TE:mass())
        TE:turnTo((GB:toWorld(vec(2000+GB:pos():distance(TE:pos()),0,0))-TE:pos()):toAngle())
    }
}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(3):pos())
        Pod:setAng(holoEntity(3):angles())
        Pod:propFreeze(1)
        timer("parent",100)
    }
    if(clk("parent"))
    {
        stoptimer("parent")
        Pod:parentTo(E)
        holoDelete(3)
        ChairOn = 1
    }
}
