@name Tail
@inputs 
@persist BoneCount BoneSize:vector Mode 
@persist BonesSpawned [Speed Size]:number [ EE O ]:entity 
@outputs AngDif:angle Dot Acos 
#runOnTick(1)
interval(50)

if (first()) {    
    BoneCount = 6
    BoneSize = vec(1.5,0.5,0.5)
    Mode = 0
    EE = entity()
    O = owner()
    Speed = 64
    Size = 2
    
    holoCreate(100)
    holoColor(100,vec(255,255,255),0)
    
    holoCreate(10)
    holoModel(10,"models/sprops/misc/domes/size_2/dome_24x30.mdl")
    holoAng(10,EE:toWorld(ang(-90,0,0)))
    
    holoCreate(11)
    holoModel(11,"models/sprops/misc/domes/size_2/dome_24x30.mdl")
    holoAng(11,EE:toWorld(ang(0,0,90)))
    holoScale(11,vec(0.8,0.2,1))
    
    holoCreate(12)
    holoModel(12,"models/sprops/misc/domes/size_2/dome_24x30.mdl")
    holoAng(12,EE:toWorld(ang(0,0,-90)))
    holoScale(12,vec(0.8,0.2,1))
    
    holoCreate(13)
    holoModel(13,"models/sprops/misc/domes/size_2/dome_24x30.mdl")
    holoAng(13,EE:toWorld(ang(10,0,0)))
    holoScale(13,vec(0.7,0.14,0.8))
    
    holoCreate(14)
    holoModel(14,"models/sprops/misc/domes/size_2/dome_24x30.mdl")
    holoAng(14,EE:toWorld(ang(8,0,0)))
    holoScale(14,vec(0.5,0.14,0.6))
    
    holoCreate(15)
    holoModel(15,"models/sprops/misc/domes/size_2/dome_24x30.mdl")
    holoAng(15,EE:toWorld(ang(-13,0,180)))
    holoScale(15,vec(0.5,0.14,0.4))
    
    holoCreate(16)
    holoModel(16,"models/sprops/misc/tubes/size_2/tube_24x24.mdl")
    holoAng(16,EE:toWorld(ang(0,90,0)))
    
    holoCreate(17)
    holoModel(17,"models/sprops/misc/tubes/size_2/tube_24x24.mdl")
    holoAng(17,EE:toWorld(ang(0,90,0)))
    
    holoCreate(18)
    holoModel(18,"models/sprops/misc/tubes/size_2/tube_24x24.mdl")
    holoAng(18,EE:toWorld(ang(0,90,0)))
    
    holoCreate(19)
    holoModel(19,"models/sprops/misc/domes/size_2/dome_24x30.mdl")
    holoAng(19,EE:toWorld(ang(90,0,0)))
    holoScale(19,vec(1,1,1.4))
    
    for (I = 10,16) {holoDisableShading(I,0)}
}

holoPos(100,holoEntity(1):toWorld(vec(0,sin(curtime() * Speed) * Size,0)))

if (Mode == 0) {
    local Flip = BonesSpawned % 2
    local NewHolo = holoCreate( BonesSpawned )  
    
    if (NewHolo) {
        holoPos(BonesSpawned,EE:toWorld(vec(BoneSize:x() * BonesSpawned * 12,0,0)))
        holoScale(BonesSpawned,BoneSize)
        holoColor(BonesSpawned,vec(255,255,255),0)
        holoDisableShading(BonesSpawned,0)
        
        holoPos(10,holoEntity(0):toWorld(vec(7,0,0)))
        holoParent(10,0)
        
        holoPos(11,holoEntity(1):toWorld(vec(6,-12,0)))
        holoParent(11,1)
        
        holoPos(12,holoEntity(1):toWorld(vec(6,12,0)))
        holoParent(12,1)
        
        holoPos(13,holoEntity(1):toWorld(vec(10,0,2)))
        holoParent(13,1)
        
        holoPos(14,holoEntity(5):toWorld(vec(8,0,0)))
        holoParent(14,5)
        
        holoPos(15,holoEntity(5):toWorld(vec(8,0,0)))
        holoParent(15,5)
        
        holoPos(16,holoEntity(1):toWorld(vec(0.2,0,0)))
        holoParent(16,1)
        
        holoPos(17,holoEntity(2):toWorld(vec(0.2,0,0)))
        holoParent(17,2)
        
        holoPos(18,holoEntity(3):toWorld(vec(0.2,0,0)))
        holoParent(18,3)
        
        holoPos(19,holoEntity(4):toWorld(vec(-6,0,0)))
        holoParent(19,4)
        
        for (I = 10,19) {holoMaterial(I,"models/props_combine/metal_combinebridge001")}
        
        holoParent(0,EE)
        
        BonesSpawned++
    }
    if (BonesSpawned >= BoneCount) {Mode = 1}
} elseif (Mode == 1) {
    AngDif = ( holoEntity(100):pos() - EE:pos() ):toAngle()
    AngDif = angnorm( AngDif ) / ( BoneCount - BoneCount / 3 )
    
    for (I = 0,BoneCount - 1) {
        holoAng( I, AngDif )
        
        if (I != 0) {
            holoPos( I, holoEntity( I - 1 ):toWorld( vec( BoneSize:x() * 12, 0, 0 ):rotate( ( AngDif * 1 ) / 2 )  ) )
            holoAng( I, holoEntity( I - 1 ):toWorld( AngDif * 1 ) )
        }
    }
}
