@name TeleKinetic Physgun2
@inputs [Num]:wirelink N5
@outputs Take P R Y  [AimEnt Self]:entity
@persist Take Move Self:entity Pos:vector P R Y
@persist Flip Initial TKP Stealth
@model models/props_junk/PopCan01a.mdl
@trigger 
#By Captain Maim STEAM_0:1:8017844
N0 = Num:number("0")
N1 = Num:number("1")
N2 = Num:number("2")
N3 = Num:number("3")
N4 = Num:number("4")
#N5 = Num:number("5")
N6 = Num:number("6")
N7 = Num:number("7")
N8 = Num:number("8")
N9 = Num:number("9")
Slash = Num:number("/")
Astrix = Num:number("*")
Minus = Num:number("-")
Plus = Num:number("+")
Enter = Num:number("enter")
Del = Num:number(".")
if(first()|duped()){
    TKP=1
    print("type !help to read TKP commands.")
    entity():setAlpha(50)
    Parts=entity():getConstraints()
    for(C=1,Parts:count()){
        Parts[C,entity]:setAlpha(50)
    }
}
O=owner()
Chat=O:lastSaid()
runOnTick(1)
runOnChat(1)
if(chatClk()&Chat==lastSaid()){
    if(Chat=="!tkp"){TKP=!TKP} #this toggles Telekinetic Physgun support
    if(Chat=="!help"){
        print("!tkp to toggle TeleKinetic Physgun support")
        print("!t to pick up or drop an item you are looking at.")
        print("t to move it towards you.")
        print("y to move it away from you.")
        print("numpad 5 to pickup or drop.")
        print("numpad 7 and 9 to pitch.")
        print("numpad 4 and 6 to yaw.")
        print("numpad 1 and 3 to roll.")
        print("numpad 0 to stop all rotation.")
        print("numpad 8 and 2 to move towards and away.")
        print("plus to draw it near quickly.")
        print("del to send away quickly.")
    }
}
Use=0


AimEnt=O:aimEntity()

Eye=O:eye()






if(TKP){
    if((chatClk()&Chat=="!t")){
        Take=!Take
        if(Take&!Self){Take=0}
        print("TeleKinetic Physgun is: "+Take)
        if(Take){Self=O:aimEntity() print("Grabbing "+Self:type())}
    }   
    if(!O:isAlive()){
        
    }
    
    if(Chat=="t"){Move-=1}
    if(Chat=="y"){Move+=1}
    if(N8){Move+=1}
    if(N2){Move-=1}
    if(N5&~N5){
        Take=!Take Self=O:aimEntity() 
        if(Take&!Self){Take=0}
        print("TeleKinetic Physgun is: "+Take)
        print("Grabbing "+Self:type())
        Move=0
        Initial=O:pos():distance(Self:pos())
    }
    Rate=150
    if(N7){P=Rate}
    if(N9){P=-Rate}
    if(N4){Y=Rate}
    if(N6){Y=-Rate}
    if(N1){R=Rate}
    if(N3){R=-Rate}
    if(N0){R=Y=P=0}
    if(Del){Move+=50}
    if(Plus){Move-=50}
    
    #clamp(R,-180,180)
    #clamp(P,-90,90)
    #clamp(Y,-180,180)
    if(Take){
        
        #Self=entity():isConstrainedTo()
        #if(!Self){Self=entity()}
        #runOnTick(1)
        #PYR=vec(0,0,0)
     
        if(abs(R)==180){R=0}
        Time=1500/1000
        interval(Time*1000)
        InertiaConstant = 27.05307927383
        TargetAng=angnorm(ang(P,Y,R))
        Local=Self:toLocal(Self:angles())
        AngDist=TargetAng-Local
        #AngDist=TargetAng-Self:angles()
        Inertia = InertiaConstant*ang(Self:inertia():y(),Self:inertia():z(),Self:inertia():x())
        AngVel = AngDist/Time
        Self:applyAngForce(Inertia*(AngVel-Self:angVel()))
    
        Speed=100*(Self:mass()/4)
        
        XYZ=O:shootPos()+O:eye()*(Initial+Move)
        
        Self:applyForce(Speed*(XYZ-Self:pos())-(Self:vel()/2)*Self:mass())
    }
}
