@name speeder thing
@inputs Active [Pod Cam]:wirelink
@persist [Chair E]:entity Dist
@model models/props_phx/construct/metal_plate1.mdl
if ((first()|dupefinished()|~Pod) & ->Pod) {
    E = entity()
    
    if (Pod["Entity",entity]) {
        Chair = Pod["Entity",entity]
    } else {
        findIncludePlayerProps(owner())
        findByClass("*vehicle*")
        Chair = findClosest(E:pos())
    }
    
    if (Chair) {
        print("Chair found, positioning, welding and parenting")
        Chair:setPos(E:toWorld(vec(0,0,5)))
        Chair:setAng(E:toWorld(-ang(0,90,0)))
        #ifdef E:weldTo(E,N)
            Chair:weldTo(E,1)
        #endif
        #ifdef weld(E,E)
            weld(Chair,E)
        #endif
    } else {
        print("Chair not found!")
    }
    

    E:setMass(200)

    rangerHitWater(1)
    local R = E:getConstraints()
    R:pushEntity(E)
    rangerFilter(R)
    
    if (->Cam) {        
        Cam["Position",vector] = E:toWorld(vec(0,0,50))
        Cam["Distance",number] = 75
        Cam["Parent",entity] = E
        Cam["Activated",number] = 1
    }
} elseif (~Cam & ->Cam) {
    Cam["Position",vector] = vec(0,0,50)#E:toWorld(vec(0,0,50))
    Cam["Parent",entity] = entity()
    Cam["Activated",number] = 1
} elseif (~Active) {
    if (Active) {
        interval(50)
        
        rangerHitWater(1)
        local R = E:getConstraints()
        R:pushEntity(E)
        rangerFilter(R)
        rangerPersist(1)
        
        E:applyForce(vec(0,0,E:mass()*100))
    } else {
        stoptimer("interval")
    }
} elseif (clk()) {
    local R = rangerOffsetHull(60,E:pos(),vec(0,0,-1),E:boxSize() * 0.6)
    if (R:hit()) {
        local W = Pod["W",number]
        local A = Pod["A",number]
        local S = Pod["S",number]
        local D = Pod["D",number]
        local Shift = Pod["Shift",number]
        local Space = Pod["Space",number]
        
        local Ang = ang(0,Chair:toLocal(Chair:driver():eyeAngles()):yaw(),0)
        
        
        local Force = (vec(W-S,A-D,Space)*(20+Shift*60+Space*20)):rotate(Ang)
        E:applyForce(((Force + (R:position() + vec(0,0,40) - E:pos())) * 20 - E:vel()) * E:mass())
     
        local Torque = E:toLocal(rotationVector(quat(Ang)/quat(E))+E:pos())
        E:applyTorque((Torque*200-E:angVelVector()*20)*E:inertia())
    }
    
    interval(50)
}
