@name Ejection System
@inputs Pod:wirelink Seat:entity 
@outputs 
@persist Allow Fall R:ranger 
@trigger 
interval(50)

local E = entity():isWeldedTo()
local Seat = Pod["Entity",entity]
local Driver = Seat:driver()
local Alt = Driver:keyWalk()

if (first()) {
    Seat:propFreeze(1)
    Seat:setPos(E:pos() + vec(0,0,8))
    Seat:setAng(E:angles())
    timer("parent",400)
    
    Allow = 0
}

if (clk("parent")) { Seat:parentTo(E) }

if (changed(Alt) & Alt) {
    Allow = 1
}

if (changed(Allow == 1) & Allow == 1) {    
    Seat:deparent()
    Seat:propFreeze(0)
    
    Seat:applyForce(E:up()*100*E:mass()*50)
    
    luaCheat("
        ply = Entity(" + owner():id() + ")
        local prop = Entity(" + entity():id() + ")
        local explode = ents.Create( 'env_explosion' )
	       explode:SetPos( prop:GetPos() )
	       explode:SetOwner( ply )
	       explode:Spawn()
	       explode:SetKeyValue( 'iMagnitude', '0' )
	       explode:Fire( 'Explode', 0, 0 )
        timer.Create( 'Remove', 1, 1, function() explode:Remove() end )
    ")
    
    timer("Parachute",2500)
}

rangerFilter(Seat)
R = rangerOffset(10,Seat:pos(),-Seat:up())

if (clk("Parachute")) {
    holoCreate(1)
    holoModel(1,"models/hunter/misc/shell2x2a.mdl")
    holoPos(1,Seat:toWorld(vec(0,0,80)))
    holoAng(1,Seat:toWorld(ang(0,0,0)))
    holoParent(1,Seat)
    
    Fall = 1
}

if (Fall == 1) { Seat:applyForce(E:up()*E:mass()*46) }

if (R:hit()) {
    holoDelete(1)
    Seat:applyForce(-E:up()*E:mass()*47)
}
