@name Space Ship
@inputs P:entity C:wirelink
@persist E:entity
@persist [AA PA LA]:angle
@persist N Ntpl Ign Pref
@persist M:array Block
@model models/props_wasteland/cargo_container01b.mdl

if(first())
{
    Ntpl = 50 / (500 / 33)
    
    E = entity()
    E:setMass(15000)
    E:propFreeze(0)
    #E:setColor(0, 0, 0, 0)
    
    #Body Holo
    #holoCreate(1, E:toWorld(vec(0, 50, 0)), vec(4), E:toWorld(ang(0, 90, 0)))
    holoModel(1, "models/props_combine/headcrabcannister01a.mdl")
    holoParent(1, E)
    
    #Camera Holo
    holoCreate(2)
    holoAlpha(2, 0)
    holoParent(2, E)
    
    #Cam Hit - Red
    holoCreate(3)
    holoModel(3, "hq_torus")
    holoScale(3, vec(8))
    holoColor(3, vec4(255, 0, 0, 150))
    
    #Ship Hit - Green
    holoCreate(4)
    holoScale(4, vec(6))
    holoModel(4, "hq_torus")
    holoColor(4, vec4(0, 255, 0, 150))
    
    rangerFilter(array(E, P, C:entity()):merge(M))
    rangerPersist(1)
    
    timer("check for driver",1000)
    
    if(C)
    {
        timer("set the things",1)
    }
}
elseif (~P & P)
{
    rangerFilter(array(E, P, C:entity()):merge(M))
    
    P:setPos(E:toWorld(vec(0, -150, -12)))
    P:setAng(E:toWorld(ang(0, 0, 0)))
    P:propFreeze(1)
    
    timer("pparent", 50)
}
elseif (~C & C | clk("set the things"))
{
    rangerFilter(array(E, P, C:entity()):merge(M))
        
    C:entity():setPos(E:toWorld(vec(0, -150, 0)))
    C:entity():setAng(E:toWorld(ang()))
    C:entity():propFreeze(1)
    
    C["Activated", number] = 1
    C["Position", vector] = vec()
    C["Angle", angle] = ang()
    C["Parent", entity] = E
    C["Distance", number] = 400
    
    timer("cparent",50)
}
elseif (clk("check for driver"))
{
    if (P:driver()) {
        timer("fly",50)
        Dri = P:driver()
        
        #Activate the things
        E:propGravity(0)
        
        holoVisible(3, players(), 0)
        holoVisible(4, players(), 0)
        holoVisible(3, Dri, 1)
        holoVisible(4, Dri, 1)
    }
    else
    {
        timer("check for driver",1000)
    }
}
elseif(clk("fly"))
{
    Dri = P:driver()
    
    if (!Dri) {
        timer("check for driver",1000)
        E:propGravity(1)
        exit()
    }
    Act = P:driver():isValid()
    
    W = Dri:keyForward()
    A = Dri:keyLeft()
    S = Dri:keyBack()
    D = Dri:keyRight()
    Shi = Dri:keySprint()
    Spc = Dri:keyJump()
    R = Dri:keyReload()
    Alt = Dri:keyWalk()
    M1 = Dri:keyAttack1()
    M2 = Dri:keyAttack2()
    Ctrl = Dri:keyDuck()
    
    if(changed(R) & R)
    {
        Ign = !Ign
        E:propGravity(!Ign)
    }
		
    local Switch = (changed(Alt) & Alt) & M2 #Switch perspectives
		  if(changed(M2) | Switch)
    {
        if(Switch)
        {
            Pref = !Pref
        }
        
        holoAlpha(3, M2 * 150)
        holoAlpha(4, M2 * 150)
        
        C["Position", vector] = vec(-250 * (Pref ? 1 : -1), 0, 25) * M2
        C["FOV", number] = 90 - (M2 * 45)
    }
    
    LA = (P:toLocal(Dri:eyeAngles()))
    if(!M2) #main camera mode
    {
        PA = P:angles()
        if(abs($PA:forward():length() - $LA:forward():length()) < 5)
        {
            AA = LA
        }
    }
    else #third person shooter camera mode
    {
        #Rangers
        local CamTrace = rangerOffset(10000000, C["CamPos", vector], C["CamDir", vector])
        local ETrace = rangerOffset(10000000, E:pos(), -E:right())
        
        #angle for E to aim at cam's hitpos
        AA = angnorm((CamTrace:pos() - E:pos()):toAngle())
        
        holoAng(2, LA)
        
        holoAng(3, C["Trace", ranger]:hitNormal():toAngle() + ang(90, 0, 0))
        holoAng(4, ETrace:hitNormal():toAngle() + ang(90, 0, 0))
        
        holoPos(3, CamTrace:pos())
        holoPos(4, ETrace:pos())
    }
    AA = clamp(ang(0, AA[2], -AA[1]) - ang(0, 90, 0), ang(-85, -360, -85), ang(85, 360, 85))
    
    if(Ign)
    {
        if(M:count() < 5)
        {
            if(changed(M1) & M1 & !Block)
            {
                soundPlay("fire", 0.8, "weapons/medi_shield_deploy.wav")
                
                local EA = (E:angles() + ang(0, 90, 0))
                local TE = propSpawn("models/props_phx/ww2bomb.mdl", E:toWorld(vec(0, 500, 0)), ang(-EA[3], EA[2], EA[1]), 1)
                TE:setColor(0, 0, 0, 0)
                
                #Inner White
                local H1 = holoCreate(TE:id())
                holoAng(TE:id(), TE:toWorld(ang(90, 0, 0)))
                holoScale(TE:id(), vec(0.705,0.705,14.1))
                holoColor(TE:id(), vec(255, 200, 200))
                holoModel(TE:id(), "hq_rcylinder_thick")
                holoDisableShading(TE:id(), 1)
                holoParent(TE:id(), TE)
                
                H1:soundPlay(H1:id(), 0, "ambient/machines/combine_shield_touch_loop1.wav")
                
                lightCreate(1, H1:pos(), vec(255, 20, 20), 250, 255)
                lightParent(1, H1)
                
                #Outer Red
                holoCreate(H1:id())
                holoAng(H1:id(), TE:toWorld(ang(90, 0, 0)))
                holoScale(H1:id(), vec(-0.9,-0.9,-14.4))
                holoColor(H1:id(), vec(255, 0, 0))
                holoModel(H1:id(), "hq_rcylinder_thick")
                holoDisableShading(H1:id(), 1)
                holoParent(H1:id(), TE)
                
                M:pushEntity(TE)
                
                rangerFilter(array(E, P, C:entity()):merge(M))
                
                Block = 1
                timer("ub", 1000)
            }
        }
    }
    
    if(Ign)
    {
        applyAngForce((angnorm(AA - E:angles()) * Act * ang(75, 100, 100) - E:angVel() * ang(20, 35, 35)) * Ntpl * E:mass())
        applyForce((((E:toWorldAxis((1 + Shi) * vec((D - A) * 2.5, (W - S) * (3 + (3 * Shi)), (Spc - Ctrl) * 3)))) * 10 - E:toWorldAxis(E:velL() * 0.125)) * E:mass())
    }

    if(M:count() > 0)
    {
        foreach(I, TE:entity = M)
        {
            local Rd = rangerOffset(525, TE:pos(), TE:forward())
            if(Rd:hit() | !TE:isValid())
            {
                #made before removing missile from array
                local Mi = TE
                
                M:remove(I)
                
                holoDelete(holoEntity(Mi:id()):id())
                holoDelete(Mi:id())
                
                Mi:setPos(Rd:pos())
                Mi:propBreak()
            }
            else
            {
                TE:setPos(TE:toWorld(vec(500, 0, 0)))
            }
        }
    }
        
    timer("fly",50)
}
elseif(clk("pparent"))
{    
    P:parentTo(E)
}
elseif(clk("cparent"))
{    
    C:entity():parentTo(E)
}
elseif(clk("ub"))
{
    Block = 0
}
