@name continuum4
@inputs Camera:wirelink Vehicle:entity
@outputs
@persist Segments:array Num Length Count DT CT WorldDown:vector OverVec:vector Player:entity Vels:array
@trigger


if (!Num) {
    Count = 40
    Length = 50
    WorldDown = vec(0, 0, -1)
    OverVec = vec(0, 0, Length - 5)

    while (Num < Count) {
        Num++

        Segments[Num, vector] = entity():toWorld(vec(-Length*Num, 0, 0))

        holoCreate(Num, Segments[Num, vector])
        holoColor(Num, vec4(255, 125, 0, 15))
        holoDisableShading(Num, 1)
        holoModel(Num, "sphere")

        if (Num > 1) {
            local SegThick = sqrt(Length)
            local ID = Num + 500
            local SC = -cos((360/Count)*Num )
            holoCreate(ID, holoEntity(Num):toWorld(vec(Length/2, 0, 12 + SC*SegThick/2)), vec(24 + SC*SegThick, 24 + SC*SegThick, Length)/12, holoEntity(Num):toWorld(ang(90, 0, 0)))
            holoParent(ID, Num)
            holoModel(ID, "hexagon")
            holoColor(ID, Num % 2 == 0 ? vec() : vec(255))
            #holoColor(ID, hsv2rgb((10 / Count)*Num + 80, 1/3, 1/3))

            #holoParent(N, N - 1)
        }
    }

    function setup() {
        Camera["Parent", entity]   = holoEntity(2)
        Camera["Position", vector] = vec(8, 0, 100)
        Camera["Distance", number] = 0

        rangerHitEntities(0)
        rangerFilter(Vehicle)
        rangerPersist(1)
    }

    setup()

    CT = curtime()

    interval(100)
    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()
    }


    DT = curtime() - CT
    CT = curtime()

    if (Player) {
        Camera["Activated", number] = 1
        if (Player:keyReload()) { Vehicle:ejectPod() }
    } else {
        Camera["Activated", number] = 0
    }

        local Dir = rangerOffset(99999, holoEntity(2):toWorld(vec(0, 0, 100)), Vehicle:toLocalAxis(Player:eye())):position() - Segments[1, vector]
        local Yaw = Dir:toAngle():yaw()
        local Vel = vec(Player:isValid(), 0, 0):rotate(0, Yaw + sin(curtime()*180)*45, 0) * min(800, Dir:length())

        local Ranger = rangerOffset(Length, Segments[1, vector] + Vel*DT + OverVec, WorldDown)
        if (Ranger:hit()) {
            Segments[1, vector] = Ranger:pos()
            Vels[1, vector] = vec()
        }
        else {
            Vels[1, vector] = Vels[1, vector] + vec(0, 0, Length)
            Segments[1, vector] = Segments[1, vector] + Vel*DT - Vels[1, vector]*DT  - Vels[1, vector]*DT
        }

        holoPos(1, Segments[1, vector])

        for (N = 2, Num) {
            if (N % 2 == 0) {
                local Ranger = rangerOffset(Length, Segments[N - 1, vector] - (Segments[N - 1, vector] - Segments[N, vector]):normalized()*Length + OverVec, WorldDown)

                if (Ranger:hit()) {
                    Segments[N, vector] = Ranger:pos()
                    Vels[N - 1, vector] = vec()
                }
                else {
                    Segments[N, vector] = Segments[N - 1, vector] - (Segments[N - 1, vector] - Segments[N, vector]):normalized()*Length
                    #Segments[N, vector] = Segments[N, vector]:rotateAroundAxis(holoEntity(N):right(), Length*DT)
                    #Vels[N - 1, vector] = holoEntity(N):up() * -Length
                    #Vels[N - 1, vector] + vec(0, 0, Length*2)
                    #Segments[N, vector] = Segments[N - 1, vector] - (Segments[N - 1, vector] - Segments[N, vector]):normalized()*Length - Vels[N - 1, vector]*DT
                   # print(N)
                }

                #holoColor(N+500, vec(255, 0, 0))
            }
            else {
                Segments[N, vector] = Segments[N - 1, vector] - (Segments[N - 1, vector] - Segments[N, vector]):normalized()*Length
            }

            holoPos(N, Segments[N, vector])
            holoAng(N, (Segments[N - 1, vector] - Segments[N, vector]):toAngle())
        }
        
        
        #Mat = convar("material_override")
       # if (changed(Mat)) {
        #    for (N = 1, Num) { holoMaterial(N+500, Mat) }
        #}
        #for (N = Num, 2, -1) {
            #Segments[N, vector] = Segments[N, vector]:rotateAroundAxis(holoEntity(N):forward(), Length*DT)
            #:rotateAroundAxis((Segments[N - 1, vector] - Segments[N, vector]):normalized():cross(vec(0, 1, 0)), Length)
        #}

}


#[        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())
        }]#
