@name continuum
@inputs Camera:wirelink Vehicle:entity
@outputs Speed
@persist Positions:array Segments SegmentLength RangerDir:vector RangerHeight:vector Yaw:angle Player:entity Speed
@trigger none

if (!Segments) {
    local Num = 40
    
    SegmentLength = 25
    function number lerp(A, B, X) {
        #return A + (B - A) * X
        #return N0*T + N1*(1-T)
        return (1-X)*A + X*B
    }    
    SegThick = sqrt(SegmentLength)
    for (N = 1, Num) {
        Segments++
        Positions[Segments, vector] = holoCreate(Segments):pos()
        holoAlpha(Segments, 0)
        
        if (N > 1) {
            local ID = Segments + 500
            local SC = -cos((360/Num)*N )
            holoCreate(ID, holoEntity(Segments):toWorld(vec(SegmentLength/2, 0, 12 + SC*SegThick/2)), vec(24 + SC*SegThick, 24 + SC*SegThick, SegmentLength)/12, holoEntity(Segments):toWorld(ang(90, 0, 0)))
            holoParent(ID, Segments)
            holoModel(ID, "hexagon")
            
            #holoParent(N, N - 1)
        }
        
    }
    
    function setup() {
        Camera["Parent", entity]   = holoEntity(2)
        Camera["Position", vector] = vec(8, 0, 38)
        Camera["Distance", number] = 0
        
        rangerFilter(Vehicle)
        rangerPersist(1)
    }
    
    setup() 
        
    RangerDir = vec(0, 0, -1)
    RangerHeight = vec(0, 0, 25)
    
    Positions[1, vector] = entity():pos()

    interval(60)
    runOnLast(1)
}
else {
    interval(90)

    #--------------
    #- Vehicle
    Player = Vehicle:driver()
    
    if (!Vehicle:parent()) {
        Vehicle:propFreeze(1)
        
        local Pos = holoEntity(2):toWorld(vec(0, 0, 15))
        local Ang = holoEntity(2):toWorld(ang(0, -90, 0))
    
        Vehicle:setPos(Pos)
        Vehicle:setAng(Ang)
        
        if ((Vehicle:pos() - Pos):length() < 0.1 & vec(Vehicle:angles() - Ang):length() < 0.1) {
            Vehicle:parentTo(holoEntity(2))
            
            setup()
        }
    }
    if (last()) { 
        Vehicle:deparent()
    }
    
    
    if (Player) {
        local Hull = vec(SegmentLength)/2
        Camera["Activated", number] = 1
        if (Player:keyReload()) { Vehicle:ejectPod() }
        local Aim = rangerOffset(99999, Player:shootPos(), Vehicle:toLocalAxis(Player:eye())):position()
        
        Speed = lerp(Speed, 1 + Player:keySprint(), 0.1)
        
        Yaw = slerp(quat((Aim - Positions[1, vector]):toAngle()), quat(Yaw), 0.5):toAngle()
        #MoveDir = vec(1, sin(curtime()*90) / abs(180 / Yaw[2]), 0):rotate(0, Yaw[2], 0):normalized()
        MoveDir = vec(1, sin(curtime()*90)*Speed/2, 0):rotate(0, Yaw[2], 0):normalized()
        
        Positions[1, vector] = rangerOffset(100, Positions[1, vector] + MoveDir*Speed*10 + vec(0, 0, 50), vec(0, 0, -1)):position()
        holoPos(1, Positions[1, vector])
    
        for (N = 2, Segments) {
            #Positions[N, vector] = rangerOffset(SegmentLength*2, Positions[N - 1, vector] - (Positions[N - 1, vector] - Positions[N, vector]):normalized()*SegmentLength + RangerHeight, RangerDir):position()
            local Ranger = rangerOffset(SegmentLength, Positions[N - 1, vector] - (Positions[N - 1, vector] - Positions[N, vector]):normalized()*SegmentLength + RangerHeight, RangerDir):position()
            
            Positions[N, vector] = rangerOffset(SegmentLength*2, Positions[N - 1, vector] - (Positions[N - 1, vector] - Positions[N, vector]):normalized()*SegmentLength + RangerHeight, RangerDir):position()
            
            holoPos(N, Positions[N, vector])
            holoAng(N, (Positions[N - 1, vector] - Positions[N, vector]):toAngle())
        }
        
    }
    else {
#[        
        local Aim = owner():aimPos()
        
        Yaw = slerp(quat((Aim - Positions[1, vector]):toAngle()), quat(Yaw), 0.5):toAngle()
        MoveDir = vec(1, sin(curtime()*180) / abs(180 / Yaw[2]), 0):rotate(0, Yaw[2], 0):normalized()
    
        Positions[1, vector] = rangerOffset(100, Positions[1, vector] + MoveDir*10 + vec(0, 0, 50), vec(0, 0, -1)):position()
        holoPos(1, Positions[1, vector])
    
        for (N = 2, Segments) {
            Positions[N, vector] = rangerOffset(SegmentLength*2, Positions[N - 1, vector] - (Positions[N - 1, vector] - Positions[N, vector]):normalized()*SegmentLength + RangerHeight, RangerDir):position()
          
            for (J = N + 1, Segments) {
                if (Positions[N, vector]:distance(Positions[J, vector]) < SegmentLength - 10) {
                    Positions[N - 1, vector] = Positions[N - 1, vector] + vec(0, 0, SegmentLength/2)
                    Positions[N, vector] = Positions[N, vector] + vec(0, 0, SegmentLength)
                    Positions[N + 1, vector] = Positions[N + 1, vector] + vec(0, 0, SegmentLength/2)
                    break    
                }
            }
            
            holoPos(N, Positions[N, vector])
            holoAng(N, (Positions[N - 1, vector] - Positions[N, vector]):toAngle())
        }        
    }]#
    }
}
