@name Joybot Zeta
@inputs [Joy Grabber User Igniter Hdrive Forcer Turret RTCam Num]:wirelink 
@outputs HoverFR HoverFL HoverRR HoverRL Hover Angles:angle Rot UpDn Pon
@outputs RLThruster FBThruster Turn Roll Pitch Keyboard

@persist
@trigger 
interval(10)

B1=Joy["B1",number]
B2=Joy["B2",number]
B3=Joy["B3",number]
B4=Joy["B4",number]
B5=Joy["B5",number]
B6=Joy["B6",number]
B7=Joy["B7",number]
B8=Joy["B8",number]
B9=Joy["B9",number]
B10=Joy["B10",number]
B11=Joy["B11",number]
B12=Joy["B12",number]
Yup=Joy["Yup",number]
Ydown=Joy["Ydown",number]
Xleft=Joy["Xleft",number]
Xright=Joy["Xright",number]
Zup=Joy["Zup",number]
Zdown=Joy["Zdown",number]
Rleft=Joy["Rleft",number]
Rright=Joy["Rright",number]
Hup=Joy["Hup",number]
Hdown=Joy["Hdown",number]
Hleft=Joy["Hleft",number]
Hright=Joy["Hright",number]


O=owner()
Chat=O:lastSaid()
if(Chat:find("Joybot K")){Keyboard=1}
if(Chat:find("Joybot J")){Keyboard=0}
    
if(Keyboard){
    N0 =   Num["0",number]
    N1 =   Num["1",number]
    N2 =   Num["2",number]
    N3 =   Num["3",number]
    N4 =   Num["4",number]
    N5 =   Num["5",number]
    N6 =   Num["6",number]
    N7 =   Num["7",number]
    N8 =   Num["8",number]
    N9 =   Num["9",number]
    Slash= Num["/",number]
    Astrix=Num["*",number]
    Minus= Num["-",number]
    Plus=  Num["+",number]
    Enter= Num["enter",number]
    Del=   Num[".",number]

    Yup=O:keyForward()
    Ydown=O:keyBack()
    Xleft=O:keyLeft()
    Xright=O:keyRight()
    Rright=N6
    Rleft=N4
    Zup=N8
    Zdown=N2
    B1=N1
    B2=O:keyReload()
    B3=Astrix
    B4=O:keyDuck()
    B5=Slash
    B6=Del
    B7=Enter
    B8=Plus
    
}
#Angles=entity():angles()
#A=50 #degrees at which the joystick will invert and bounce back.
#Pon=-(Angles:pitch()>A)-(Angles:pitch()<-A)+(Angles:pitch()<A)+(Angles:pitch()>-A)

R=0.5
RLThruster=(Xleft-Xright)*500
FBThruster=(Yup-Ydown)*500*(10*B6+!B6)
Turn=(-Rright+Rleft)*150*(R*!Hover+Hover)
Pitch=(Zup-Zdown)*300
Roll=(Hleft-Hright)*600


User["Fire",number]=B1
Forcer["Velocity",number]=B2*10000
Forcer["Force",number]=B2*10000
Grabber["Grab",number]=B3
Igniter["A",number]=B4
RTCam["Activate",number]=B9



Rot=(Zup-Zdown)*R
UpDn=(B8-B7)*(B6*5+!B6)


HoverFR=(UpDn+Rot-(Hright-Hleft)*(R/2))
HoverFL=(UpDn+Rot-(-Hright+Hleft)*(R/2))
HoverRR=(UpDn-Rot-(Hright-Hleft)*(R/2))
HoverRL=(UpDn-Rot-(-Hright+Hleft)*(R/2))




if(B11){Hover=!Hover}

if(B12){
    
    Laser=rangerOffset(4000,User:entity():pos(),User:entity():up())
    Hdrive["TargetPos",vector]=Laser:pos()-(User:entity():up()*100)
    Hdrive["Jump",number]=1
    Hdrive["TargetAngle",angle]=entity():isConstrainedTo():angles()
}else{Hdrive["Jump",number]=0}

