@name tinycopter
@inputs
@outputs
@persist [HologramName HologramModel HologramPosition HologramPositionWorld HologramSkin HologramAngle HologramScale HologramColor HologramAlpha HologramMaterial HologramParent]:array
@persist [HoloCounter Loaded Power Gravity ArcMode MovableHoloIndex_PitchYaw MovableHoloIndex_Pitch MovableHoloIndex_Yaw TargetCanHit]:number [Origin]:vector [Me Chip]:entity
@persist Timerr
@trigger

interval(25)

if( first() | dupefinished() )
{
    Me = owner()
    Chip = entity()
    Chip:setAlpha(0)
    Origin = Chip:pos()
    Loaded = 0
    Power = fromUnit("m/s", 12)
    Gravity = gravity()
    ArcMode = 1
    Timerr = 0
    MovableHoloIndex_PitchYaw = 0
    MovableHoloIndex_Pitch = 0
    MovableHoloIndex_Yaw = 0
    
    # DO NOT EDIT THIS SECTION UNLESS YOU KNOW WHAT YOU ARE DOING, OR IT MAY NOT PARSE IN MODELER CORRECTLY!
    HoloCounter = 0
    HologramModel = array("models/combine_helicopter.mdl","models/holograms/cube.mdl","models/sprops/trans/air/prop_5b_s.mdl")
    HologramPosition = array(vec(0,0,50),vec(0,0,51.3997),vec(0,0,51.7996))
    HologramAngle = array(ang(0,0,0),ang(0,0,0),ang(90,150.5,0))
    HologramScale = array(vec(0.02,0.02,0.02),vec(0.1,0.1,0.1),vec(0.2,0.2,0.2))
    HologramColor = array(vec(255,255,255),vec(255,255,255),vec(255,255,255))
    HologramAlpha = array(255,0,100)
    HologramMaterial = array("","","")
    HologramSkin = array(0,0,0)
    HologramParent = array(0,1,2)
    timer("createHolos", 1)
    # DO NOT EDIT THIS SECTION UNLESS YOU KNOW WHAT YOU ARE DOING, OR IT MAY NOT PARSE IN MODELER CORRECTLY!
    
    function angle incrementedAngle(O:angle, T:angle, S:number)
    {
        CV=O:forward()
        DV=T:forward()
        Ang=acos(CV:dot(DV))
        if(Ang>S)
        {
            TargetCanHit = 0
            return slerp(quat(O),quat(T),S/Ang):toAngle()
        }
        else
        {
            TargetCanHit = 1
            return T
        }
    }
    
    function angle getAngleOfAttack(O:vector, T:vector)
    {
        local VTar = T #+ ( Target:vel() * ( T:setZ(0):distance(O:setZ(0)) / ( Power * cos(holoEntity(8):angles():pitch()))))
        local V = VTar - O
        local Range = V:setZ(0):length()
        local Height = V:z()
        local Angle = atan(((Power^2) + ArcMode*sqrt((Power^4) - Gravity * ((Gravity*(Range^2)) + (2*Height*(Power^2)))))/(Gravity*Range))
        local Time = (Power * sin(Angle) + sqrt((Power * sin(Angle))^2 + (2 * Gravity * Height))) / Gravity
        Timer = Time
        if( Time > 0 & abs(O:z() - T:z()) < 100) { return V:toAngle():setPitch(90-Angle) }
        else { return ang(0,0,0) }
    }
}
elseif( clk("createHolos") )
{
    while(perf(90) & HoloCounter < HologramModel:count() & holoCanCreate())
    {
        HoloCounter++
        holoCreate( HoloCounter )
        holoPos(HoloCounter, Origin + HologramPosition[HoloCounter, vector])
        holoScale(HoloCounter, HologramScale[HoloCounter, vector])
        holoAng(HoloCounter, HologramAngle[HoloCounter, angle])
        holoColor(HoloCounter, HologramColor[HoloCounter, vector])
        holoModel( HoloCounter, HologramModel[HoloCounter, string] )
        holoMaterial( HoloCounter, HologramMaterial[HoloCounter, string] )
        holoSkin( HoloCounter, HologramSkin[HoloCounter, number] )
        holoAlpha( HoloCounter, HologramAlpha[HoloCounter, number] )
    }
    if(HoloCounter < HologramModel:count()) { timer("createHolos", 1) }
    else { timer("parentHolos", 1) HoloCounter = 0 }
}
elseif( clk("parentHolos") )
{
    while(perf(90) & HoloCounter < HologramModel:count())
    {
        HoloCounter++
        if(HologramParent[HoloCounter, number] > 0)
        {
            holoParent(HoloCounter, HologramParent[HoloCounter, number])
        }
    }
    if(HoloCounter == HologramModel:count()) { Loaded = 1 }
    else { timer("parentHolos", 1) }
}
elseif( clk() & Loaded == 1)
{
    Timerr+=25
    holoAng( 2, ang(15,Timerr,0))
    
    TargetV = owner():pos()
    AimV = TargetV - holoEntity(MovableHoloIndex_PitchYaw):pos()
    AimA = incrementedAngle( holoEntity(MovableHoloIndex_PitchYaw):angles(), AimV:toAngle(), 2 )
    
    if( MovableHoloIndex_PitchYaw > 0 )
    {
        holoAng( MovableHoloIndex_PitchYaw, (AimA * ang(1,1,0)))
    }
    if( MovableHoloIndex_Pitch > 0)
    {
        holoAng( MovableHoloIndex_Pitch, (AimA * ang(1,0,0)))
    }
    if( MovableHoloIndex_Yaw > 0)
    {
        holoAng( MovableHoloIndex_Yaw, (AimA * ang(0,1,0)))
    }
}
