@name Grapple Suit Chip
@inputs RWinch:wirelink LWinch:wirelink 
@inputs RWeld:wirelink LWeld:wirelink
@inputs AdvPod:wirelink Mouse1 Mouse2
@inputs LArm:wirelink RArm:wirelink
@outputs LLength RLength Left Right Rweld Lweld RDist LDist
@outputs RPos1L LPos1L 
@persist Left Right LPos:vector RPos:vector Mouse1 Mouse2
@persist RLength LLength Space Alt
@trigger
interval(30)
#outputs
RLength1=RWinch:number("Length")
LLength1=LWinch:number("Length")



W=AdvPod:number("W")
A=AdvPod:number("A")
S=AdvPod:number("S")
D=AdvPod:number("D")
#Mouse1=AdvPod:number("Mouse1")
#Mouse2=AdvPod:number("Mouse2")
R=AdvPod:number("R")
Space=AdvPod:number("Space")
Shift=AdvPod:number("Shift")
Zoom=AdvPod:number("Zoom")
Alt=AdvPod:number("Alt")
TurnLeftKey=AdvPod:number("TurnLeftKey")
TurnRightKey=AdvPod:number("TurnRightKey")
X=AdvPod:number("X")
Y=AdvPod:number("Y")
Y=AdvPod:number("Z")
PrevWeapon=AdvPod:number("PrevWeapon")
NextWeapon=AdvPod:number("NextWeapon")
AimPos=AdvPod:vector("AimPos")
ThirdPerson=AdvPod:number("ThirdPerson")
Distance=AdvPod:number("Distance")
Light=AdvPod:number("Light")
Active=AdvPod:number("Active")
Team=AdvPod:number("Team")
Health=AdvPod:number("Health")
Armor=AdvPod:number("Armor")
Bearing=AdvPod:number("Bearing")
Elevation=AdvPod:number("Elevation")
Entity=AdvPod:entity("Entity")

Pod=entity():isConstrainedTo()
if(Mouse1&~Mouse1){
    Left=!Left
    Pod:printDriver("Left: "+Left)
    LPos=AimPos
    if(changed(Mouse1)){LLength=round(LPos:distance(Pod:pos()))}
}
if(Mouse2&~Mouse2){
    Right=!Right
    Pod:printDriver("Right: "+Right)
    RPos=AimPos
    if(changed(Mouse2)){RLength=round(RPos:distance(Pod:pos()))}
}
#constants, distances, entities and masses
Mass1=1
Mass2=50000
RA=RArm:entity()    
LA=LArm:entity()
Pod:setMass(40)
PodYaw=Pod:angles():yaw()-90
Rad=40
Height=60

#Distance to where you looked when you clicked
RPos2=Pod:pos():distance(RPos)
LPos2=Pod:pos():distance(LPos)
#rest locations
RPos1=Pod:pos()+vec(Rad*cos(PodYaw),Rad*sin(PodYaw),Height)
LPos1=Pod:pos()+vec(Rad*cos(PodYaw),Rad*sin(-PodYaw),Height)
#if(first()){
#    holoCreate(1,RPos1) 
#    holoCreate(2,LPos1)
#    holoParent(1,Pod)
#    holoParent(2,Pod)
#    holoColor(1,vec(255,0,0))
#    holoColor(2,vec(0,0,255))
#}
LPos1L=LPos1:distance(Pod:pos())
RPos1L=RPos1:distance(Pod:pos())
#distance to arms
RDist=round(RA:pos():distance(RPos1))
LDist=round(LA:pos():distance(LPos1))
#distance from arms to target points
RDist1=round(RA:pos():distance(RPos1))
LDist1=round(LA:pos():distance(LPos1))
Min=40
Fast=10
Slow=4
Off=10
RDist2=RDist1<Min
LDist2=LDist1<Min

#checks to shut down the apply force code
RDisable=RDist>Off
LDisable=LDist>Off
#to prevent welded + apply forced = Spasms
if(!RDisable){RDist=Off-5}else{Rweld=0}
if(!LDisable){LDist=Off-5}else{Lweld=0}


#This controls the arms
if(Right){ 
    if(RDist2){RA:setMass(Mass2)
    #RGo=1 
    if(changed(RDist2)){Pod:printDriver("Right Locked")}}
        else{
            RArm:entity():setMass(Mass1) 
           # RGo=0
        }
    RA:applyForce((Fast*(RPos-RA:pos())-RA:vel())*RA:mass())
    Rweld=0
    #if(!RGo){RLength=round(RPos2)}
}

if(!Right){
if(RDisable){
    RLength=round(Pod:pos():distance(RA:pos()))
    RA:applyForce((Slow*(RPos1-RA:pos())-RA:vel())*RA:mass())
}else{
Rweld=1 
RLength=Off
}}



if(Left){ 
    if(LDist2){LA:setMass(Mass2) 
    #LGo=1 
    if(changed(LDist2)){Pod:printDriver("Left Locked")}}
        else{
        LArm:entity():setMass(Mass1) 
        #LGo=0
    }
    LA:applyForce((Fast*((LPos-LA:pos())+vec(0,0,0))-LA:vel())*LA:mass())
    Lweld=0
    #if(!LGo){LLength=round(LPos2)}
}

if(!Left){
if(LDisable){
    LLength=round(Pod:pos():distance(LA:pos()))
    LA:applyForce((Slow*((LPos1-LA:pos())+vec(0,0,0))-LA:vel())*LA:mass())
}else{
Lweld=1
LLength=Off
}
}





#RNoCollide=0
#LNoCollide=0
Buttons=1
Crosshairs=1
Mul=100
if(!R){
RIn=Space*Mul
LIn=Space*Mul
ROut=Alt*Mul
LOut=Alt*Mul
}else{
RIn=Alt*Mul
LIn=Space*Mul
ROut=Space*Mul
LOut=Alt*Mul
}

In=Out=50

if(RIn){RLength-=In if(RLength<0){RLength=Min}}
if(LIn){LLength-=In if(LLength<0){LLength=Min}}
if(ROut){RLength+=Out}
if(LOut){LLength+=Out}

#Inputs

AdvPod:setNumber("Crosshairs",Crosshairs)
AdvPod:setNumber("Allow Buttons",Buttons)
#AdvPod:setNumber("Relative",Relative)


#Right Arm
RWinch:setNumber("In",RIn)
RWinch:setNumber("Out",ROut)
RWinch:setNumber("Length",RLength)
RWeld:setNumber("Activate",Rweld)
#RWeld:setNumber("NoCollide",RNoCollide)
#Left Arm
LWinch:setNumber("In",LIn)
LWinch:setNumber("Out",LOut)
LWinch:setNumber("Length",LLength)
LWeld:setNumber("Activate",Lweld)
#LWeld:setNumber("NoCollide",LNoCollide)
