#@name continuum 2
@inputs 
@outputs CPU OPS
@persist Index SegNum SegLength Entity:entity RangerH:vector RangerD:vector Rotate90:angle BezNum BezStart
@persist [A B C D E F]:vector Pitch Roll Mass Inertia:angle Owner:entity
@trigger none
@model models/maxofs2d/cube_tool.mdl

if (first()) {
    Entity = entity()
    Owner = owner()
    
    SegNum = 5
    SegLength = 48
    
    for (I = 1, 5) { 
        Index++, 
        holoCreate(Index) 
        holoModel(Index, "sphere")
        holoDisableShading(Index, 1)
        holoColor(Index, vec(255, 125, 0))
    }

    BezStart = Index
    BezNum = 20

    for (I = 1, BezNum) {
        Index++
        holoCreate(Index)
        holoModel(Index, "models/props_c17/oildrum001_explosive.mdl")
        holoScaleUnits(Index, 12 + vec(12 - I * 0.75, 12 - I * 0.75, (SegLength * SegNum) / BezNum))
        holoAlpha(Index, 50)
    }

    
    Rotate90 = ang(90, 0, 0)
    RangerH = vec(0, 0, 50)
    RangerD = vec(0, 0, -1)
    
    Mass = Entity:mass()
    Inertia = ang(shiftL(Entity:inertia()))
    
    rangerHitEntities(0)
    rangerPersist(1)
}

interval(60)

A = Entity:pos()
B = rangerOffset(60, A - (A - B):normalized()*SegLength + RangerH, RangerD):position()
C = rangerOffset(60, B - (B - C):normalized()*SegLength + RangerH, RangerD):position()
D = rangerOffset(60, C - (C - D):normalized()*SegLength + RangerH, RangerD):position()
E = rangerOffset(60, D - (D - E):normalized()*SegLength + RangerH, RangerD):position()
F = rangerOffset(60, E - (E - F):normalized()*SegLength + RangerH, RangerD):position()

holoPos(1, B)
holoPos(2, C)
holoPos(3, D)
holoPos(4, E)
holoPos(5, F)

for (I = 1, BezNum) {
    local T = I / BezNum
    #holoPos(I + BezStart, A*(1 - T)^5 + T*B*5*(1 - T)^4 + T^2*C*10*(1 - T)^3 + T^3*D*10*(1 - T)^2 + T^4*E*5*(1 - T) + T^5*F)
    holoPos(I + BezStart, T*(5*B*(T-1)^4+T*(T*(10*D*(T-1)^2+T*(-5*E*T+5*E+F*T))-10*C*(T-1)^3))-A*(T-1)^5)
    holoAng(I + BezStart, ((I > 1 ? holoEntity(I - 1 + BezStart):pos() : A) - holoEntity(I + BezStart):pos()):toAngle() + Rotate90)
}

Dist = rangerOffset(150, Entity:pos(), vec(0,0,-1))
if (Dist:hit()) {
    Angles = Entity:angles()
    Forward = Entity:forward():setZ(0) * (abs(Angles[1]) >= 1 ? Angles[1] : 0) * 2
    Right = Entity:right():setZ(0) * (abs(Angles[3]) >= 1 ? Angles[3] : 0) * 2
    Pitch += clamp((Owner:keyPressed("Up") - Owner:keyPressed("Down"))*25 - Pitch, -10, 10)
    Roll += clamp((Owner:keyPressed("Right") - Owner:keyPressed("Left"))*25 - Roll, -10, 10)

    Entity:applyForce((Forward + Right + vec(0, 0, abs(Pitch) + abs(Roll) + sin(curtime()*50)*5 + 80 - Dist:distance()) - Entity:vel() * vec(0.1, 0.1, 1)) * Mass)
    Entity:applyAngForce((Entity:toLocal(ang(Pitch, Angles:yaw(), Roll))*150 - Entity:angVel()*30)*Inertia)
    Entity:propGravity(0)
} else {
    Entity:propGravity(1)
}


