@name antenna_by_shadowscion
@persist Antennae:table
@trigger none

if ( first() ) {
    
    #---------------------------------------------------------------------------
    #---------------------------------------------------------------------------
    function build_antenna( Data:table, Index ) {
        local SegmentLength = Data["Length", number]/Data["Segments", number]
    
        holoCreate( Index )
        holoModel( Index, "sphere" )
        holoAlpha( Index, 75 )
        holoDisableShading( Index, 1 )
        
        for ( I = 1, Data["Segments", number] ) {
            Index++ 
            
            holoCreate( Index )
            holoModel( Index, "models/props_c17/oildrum001.mdl" )
            holoScaleUnits( Index, vec( 1.5 - I*0.05 ):setZ( SegmentLength ) )
            
            if ( I > 1 ) {
                holoParent( Index, Index - 1 )
                holoPos( Index, holoEntity( Index ):parent():toWorld( vec( 0, 0, SegmentLength - 0.5 ) ) )
                continue     
            }
    
            holoParent( Index, Data["Entity", entity] )
        }
    }
    
    function add_antenna( Entity:entity, Length, Segments, Constant, Damping ) {
        local Index = Antennae:count() + 1
        local Hash  = hash( Index + "" )
        
        local Table = table( 
            "Entity"   = Entity, 
            "Length"   = Length, 
            "Segments" = Segments, 
            "Constant" = Constant, 
            "Damping"  = Damping, 
            "Index"    = Hash, 
            "Position" = Entity:pos(), 
            "Velocity" = vec(), 
            "Offset"   = vec( 0, 0, Length )
        )
    
        build_antenna( Table, Hash )

        Antennae[Index + "", table] = Table 
    }
    #---------------------------------------------------------------------------
    #---------------------------------------------------------------------------
    
    
    add_antenna( entity(), 100, 5, 0.25, 0.65 )

}

interval( 60 )

foreach ( K, Antenna:table = Antennae ) {
    local Effector = Antenna["Entity", entity]:toWorld( Antenna["Offset", vector] )

    Antenna["Velocity", vector] = Antenna["Velocity", vector] - Antenna["Constant", number]*( Antenna["Position", vector] - Effector )
    Antenna["Velocity", vector] = Antenna["Velocity", vector]*Antenna["Damping", number]
    Antenna["Position", vector] = Antenna["Position", vector] + Antenna["Velocity", vector]
    
    Antenna["Position", vector] = Antenna["Position", vector] - vec(0, 0, 9.8*1)
    
    local Index = Antenna["Index", number]
    local Count = Antenna["Segments", number]
    
    local LocalVel = Antenna["Entity", entity]:toLocalAxis( Antenna["Velocity", vector] )
    local LocalAng = ang( -LocalVel:x(), 0, LocalVel:y() )/Antenna["Segments", number]

    for ( I = Index + 1, Index + Count ) {
        holoAng( I, holoEntity( I ):parent():toWorld( LocalAng ) )
    }

    holoPos( Index, Antenna["Position", vector] )
}

