@name RoboticArm
@inputs [Base G]:entity 
@outputs Target:vector Yaw Q1 Q2 Q3 Angle1 Angle2 Grab 
@persist Pos0:vector Ship1 Ship2 Ship3 Ship4 Ship5 Ship6 
@persist B L A C Slide Back 
interval(10)

if(first()) {
    holoCreate(1)
    holoModel(1,"models/Mechanics/robotics/f1.mdl")
    holoPos(1,Base:toWorld(vec(20,-40,14)))
    holoAng(1,ang(0,0,0))
    
    holoCreate(2)
    holoModel(2,"models/Mechanics/robotics/d3.mdl")
    
    holoCreate(3)
    holoModel(3,"hq_sphere")
    holoMaterial(3,"models/gibs/metalgibs/metal_gibs")
    
    holoCreate(4)
    holoModel(4,"models/Mechanics/robotics/d3.mdl")
    
    holoCreate(5)
    holoModel(5,"hq_cylinder")
    holoMaterial(5,"models/gibs/metalgibs/metal_gibs")
    holoPos(5,entity():toWorld(vec(0,0,0)))
    holoAng(5,entity():toWorld(ang(0,90,0)))
    holoScale(5,vec(2,2,0.2))
    
    holoCreate(6)
    holoModel(6,"hq_sphere")
    holoMaterial(6,"models/gibs/metalgibs/metal_gibs")
    holoPos(6,entity():toWorld(vec(0,0,0)))
    holoAng(6,entity():toWorld(ang(0,90,90)))
    holoScale(6,vec(1,1,1))
    holoParent(6,5)
    
    G:setAlpha(100)
    
    #Ship1 = 1
    Ship2 = 1
    #Ship3 = 1
    #Ship4 = 1
    #Ship5 = 1
    #Ship6 = 1
}

E = entity():isWeldedTo()
L = 60
Pos = holoEntity(5):pos()
Size1 = vec(1/2,1/2,1/2)
Size2 = vec(L,1,1)

Target = (Pos - holoEntity(1):pos())
Yaw = Target:toAngle():yaw() + 180

B = Target:length()
Q1 = -atan(Target:z()/vec2(Target):length())
Q2 = acos((L^2 - L^2 - B^2)/(2*L*B))
Q3 = acos((B^2 - L^2 - L^2)/(2*L*L))
Angle1 = Q1 + Q2
Angle2 = Angle1 + Q3

ArmPos1 = holoEntity(1):pos() + vec(L,0,0):rotate(ang(-Angle1,Yaw,0))
ArmPos2 = ArmPos1 + vec(L,0,0):rotate(ang(-Angle2,Yaw,0))

holoPos(1,holoEntity(1):pos())
holoScale(1,vec(0.3,0.3,0.3))

holoPos(2,(holoEntity(1):pos() + ArmPos1)/2)
holoAng(2,ang(-Angle1,Yaw,90))
holoScale(2,vec(0.4,0.4,0.4))

holoPos(3,ArmPos1)
holoScale(3,vec(0.7,0.7,0.7))
holoAng(3,ang(0,0,0))

holoPos(4,(ArmPos1 + ArmPos2)/2)
holoAng(4,ang(-Angle2,Yaw,90))
holoScale(4,vec(0.4,0.4,0.4))

G:setPos(holoEntity(1):pos())
G:setAng(holoEntity(1):toWorld(ang(0,0,90)))

Alt = owner():keyWalk()
Shift = owner():keySprint()

if (changed(Ship1 == 1) & Ship1 == 1) { holoPos(1,Base:toWorld(vec(20,-40,14))) }

if (Ship1 == 1) {
    if (changed(Alt) & Alt) {if(Slide == 0) {Slide = 1} else {Slide = 0} Grab = 1}

    if (Slide == 1) {
        if(A < 355) {
            A += 1
            holoAng(1,holoEntity(1):angles() + ang(0,0.5,0))
            holoPos(1,holoEntity(1):pos() + vec(0,0.3,0))
            holoPos(1,holoEntity(1):pos() - vec(0.2,0,0))
            
            timer("1",9500)
        }
    }

    if (clk("1")) { Back = 1 }
    
    if (Back == 1) {
        if(C < 355) {
            C += 1
            holoAng(1,holoEntity(1):angles() - ang(0,0.5,0))
            holoPos(1,holoEntity(1):pos() - vec(0,0.3,0))
            holoPos(1,holoEntity(1):pos() + vec(0.2,0,0))
        
            timer("letgo",5500)
        }
    }

    if (clk("letgo")) {
        Grab = 0
        Ship1 = 0
    }
}

if (changed(Ship2 == 1) & Ship2 == 1) { holoPos(1,Base:toWorld(vec(20,34,14))) }

if (Ship2 == 1) {
    if (changed(Alt) & Alt) {if(Slide == 0) {Slide = 1} else {Slide = 0} Grab = 1}

    if (Slide == 1) {
        if(A < 355) {
            A += 1
            holoPos(1,holoEntity(1):pos() + vec(0,0.29,0))
            holoAng(1,holoEntity(1):angles() - ang(0,0.5,0))
            
            timer("1",9500)
        }
    }

    if (clk("1")) { Back = 1 }
    
    if (Back == 1) {
        if(C < 355) {
            C += 1
            holoPos(1,holoEntity(1):pos() - vec(0,0.29,0))
            holoAng(1,holoEntity(1):angles() + ang(0,0.5,0))
        
            timer("letgo",5500)
        }
    }

    if (clk("letgo")) {
        Grab = 0
        Ship1 = 0
    }
}
