@name Dynamic Telescoping Piston
@inputs [Point1 Point2]:entity
@persist ExtraPistons
if(first()|dupefinished())
{
    #Created by Mouldy_Taco
    #You are allowed to distribute and/or modify this code
    #However, please do NOT distribute modified code
    
    #INSTRUCTIONS##########
    #Spawn a Wire Entity Marker, link it to itself
    #Do this twice then wire Point1 and Point2 to the different entity markers
    #Refresh the e2 to create the pistons correctly
    #----
    #If you are using this as a visual effect on machinery,
    #be sure to refresh the e2 when both points are at maximum distance
    #for it to appear as the correct size
    #----
    #If the piston overlaps with the housing when fully contracted,
    #increase the amount of extra pistons until there is no overlap
    #######################
    
    #Editable materials and colors for housing/pistons
    HousingColor=vec(55,55,366)    
    HousingMaterial="phoenix_storms/fender_white"
    PistonColor=vec(150,150,150)
    PistonMaterial="phoenix_storms/fender_chrome"
    
    Size=10   #The diameter, in GMOD units, of the piston housing
    ExtraPistons = 3    #The number of extra pistons between the housing and the final piston
    
    #Maximum and minimum piston width as a RATIO of 'Size'
    MaxWidth = 0.75   #Don't touch this ratio
    MinWidth = 0.4    #Keep this number less than 'MaxWidth' and greater than 0, 0.4 usually works good
    
    MaxDistance=Point1:pos():distance(Point2:pos())
    Angle=(Point1:pos()-Point2:pos()):toAngle()
    Distance=Point1:pos():distance(Point2:pos())*1.1
    
    CylinderScale = vec(Size,Size,Distance/(2+ExtraPistons))/12
    
    H=holoCreate(1,Point1:pos(),vec(Size/10,Size/10,Size/10)/12,Point1:toWorld(Angle+ang(0,0,0)),HousingColor,"hqcylinder")
    holoParent(1,Point1)
    holoMaterial(1,HousingMaterial)
    
    holoCreate(2,H:toWorld(vec(0,0,-Distance/(2*(2+ExtraPistons)))),CylinderScale,H:angles(),HousingColor,"hqcylinder")
    holoParent(2,H)
    holoMaterial(2,HousingMaterial)
    
    holoCreate(3,H:toWorld(vec(0,0,-(Distance+Size/(2*(2+ExtraPistons)))/(2+ExtraPistons))),vec(Size,Size,Size/(2+ExtraPistons))/(12*1.2),H:toWorld(ang(0,0,0)),HousingColor/1.25,"hq_rcylinder_thin")
    holoParent(3,H)
    holoMaterial(3,HousingMaterial)
    
    holoCreate(4,H:toWorld(vec(0,0,0)),vec(Size,Size,Size)/12,H:toWorld(ang(0,0,90)),HousingColor,"hqcylinder")
    holoParent(4,H)
    holoMaterial(4,HousingMaterial)
    
    H=holoCreate(5,Point2:pos(),vec(Size/10,Size/10,Size/10)/12,Angle,PistonColor,"hqsphere")
    holoParent(5,Point2)
    holoMaterial(5,PistonMaterial)
    
    holoCreate(6,H:toWorld(vec(0,0,Distance/(2*(2+ExtraPistons)))),CylinderScale*vec(MinWidth,MinWidth,1),H:angles(),PistonColor,"hqcylinder")
    holoParent(6,H)
    holoMaterial(6,PistonMaterial)
    
    holoCreate(7,H:toWorld(vec(0,0,0)),vec(Size,Size,Size)/(12*1.25),H:toWorld(ang(0,0,90)),PistonColor,"hqcylinder")
    holoParent(7,H)
    holoMaterial(7,PistonMaterial)
    
    SizeDifference = (MaxWidth - MinWidth)/(ExtraPistons+1)
    for(I=1,ExtraPistons)
    {
        NewSize = vec(MinWidth+SizeDifference*(I),MinWidth+SizeDifference*(I),1)
        holoCreate(7+I,mix(Point1:pos(),Point2:pos(),(I+1)/(ExtraPistons+1)),CylinderScale*NewSize,H:angles(),PistonColor,"hqcylinder")
        holoParent(7+I,H)
        holoMaterial(7+I,PistonMaterial)
    }
}
interval(50)
Angle=(Point1:pos()-Point2:pos() + (Point1:vel()-Point2:vel())/100):toAngle()
holoAng(1,Angle+ang(90,0,0))
holoAng(5,Angle+ang(90,0,0))
for(I=1,ExtraPistons)
{
    holoPos(7+I,mix(holoEntity(2):pos(),holoEntity(6):pos(),(I)/(ExtraPistons+1)))
}
