@name Grabber Turret
@inputs Grab:wirelink
@outputs Ang:angle I CanDo
@persist [G E O F]:entity Ro:ranger
@model models/props_junk/PopCan01a.mdl
#by [ME]OmicroN
if(first()){
    runOnTick(1)
    O = owner()
    E = entity()
    G = Grab:entity()
    G:setMass(50000)
    rangerFilter(G)
    rangerPersist(1)
}

if(findCanQuery()){
    findByClass("prop_physics")
    F = findClosest(E:pos())
}

Grabit = O:keyUse()
CanDo = sign(Grab["Grabbed Entity",entity]:mass())
Grab["Grab",number] = Grabit

I = clamp(I+(Grabit-!Grabit*2)/100,0,1)*CanDo

## Pos is the global position of where the Grabber will hover at ##
Pos = E:toWorld(vec(0,0,40))
G:applyForce(((Pos-G:pos())*15-G:vel()/2)*G:mass())

## F:pos() is the global position that the grabber will aim at ##
Dir = G:pos()-(F:pos()+F:vel()/(!CanDo*10))

## slerp swaps between two quaternions with the angles built in ##
## the value I goes between 0 and 1, swapping between the directional angle and the other ##
## AltAng is the angle that the grabber will rotate to when you hold Use ##
AltAng = E:angles()
Q = slerp( quat(Dir:toAngle())*qRotation(vec(0,1,0),-90) , quat(AltAng) , I )
G:applyTorque((150*G:toLocal(rotationVector(Q/quat(G))+G:pos()) - 12*G:angVelVector())*G:inertia())
