@name Mouse Aiming
@inputs [Pod Cam]:wirelink 
@outputs 
@persist 
@trigger 
runOnTick(1)

E = entity():isWeldedTo()

if (first()) {
    E:setMass(1000)
    
    holoCreate(1)
    holoPos(1,E:toWorld(vec(0,0,0)))
    holoAlpha(1,0)
    holoParent(1,E)
    
    holoCreate(2)
    holoPos(2,holoEntity(1):toWorld(vec(0,-30,40)))
    holoAlpha(2,100)
    holoParent(2,1)
    
    Offset = holoEntity(2):pos()
    Self = holoEntity(2)
    Cam["Parent", entity] = Self
    Cam["Position", vector] = Offset
}

local Seat = Pod["Entity",entity]
local Driver = Seat:driver()
local Active = Driver:isPlayer()
local W = Driver:keyForward()
local A = Driver:keyLeft()
local S = Driver:keyBack()
local D = Driver:keyRight()
local Space = Driver:keyJump()
local Shift = Driver:keySprint()
local M1 = Driver:keyAttack1()
local M2 = Driver:keyAttack2()
Cam["Activated",number] = Active

if (Space) {Height = 75} else {Height = 30}

RF = array(E)

rangerFilter(RF) L1R = rangerOffset(70,E:pos()+E:right()*30-E:forward()*50,-E:up())
rangerFilter(RF) L2R = rangerOffset(70,E:pos()-E:right()*30-E:forward()*50,-E:up())

rangerFilter(RF) R1R = rangerOffset(70,E:pos()+E:right()*30+E:forward()*50,-E:up())
rangerFilter(RF) R2R = rangerOffset(70,E:pos()-E:right()*30+E:forward()*50,-E:up())

L1RD = L1R:distance()
L2RD = L2R:distance()
R1RD = R1R:distance()
R2RD = R2R:distance()

Added = (L1RD+L2RD+R1RD+R2RD)/4

# If Seat Is Attached
if (Active) {
    rangerFilter(E)
    rangerFilter(Seat)
    Ranger = rangerOffset(32768,Seat:pos(),Driver:eye())
    Yaw = (Ranger:pos() - E:pos()):toAngle():yaw()
    Angle = ang((R1RD+R2RD)-(L1RD+L2RD),Yaw - 180,(L1RD+R1RD)-(R2RD+L2RD))
}

if (Added < Height) {
    Diff = Height - Added
    E:applyForce((vec(0,0,Diff * 25) - E:vel()) * E:mass() + E:forward() * (S-W) * E:mass() * 150 * (3 + Shift))
    E:applyAngForce((E:toLocal(Angle)*200 - E:angVel()*20) * shiftL(ang(E:inertia())))
}
#

#[ Is Seat Isn't Attached
if (Active) {
    rangerFilter(E)
    rangerFilter(Seat)
    Ranger = rangerOffset(32768,Seat:pos(),Driver:eye())
    Yaw = (Ranger:pos() - entity():pos()):toAngle():yaw()
    Angle = ang((R1RD+R2RD)-(L1RD+L2RD),Yaw - 180,(L1RD+R1RD)-(R2RD+L2RD))
}

if (Added < Height) {
    Diff = Height - Added
    E:applyForce((vec(0,0,Diff * 25) - E:vel()) * E:mass() + E:forward() * (S-W) * E:mass() * 150 * (3 + Shift))
    #E:applyAngForce((E:toLocal(Angle)*200 - E:angVel()*20) * shiftL(ang(E:inertia())))
    E:applyAngForce((E:toLocal(ang((R1RD+R2RD)-(L1RD+L2RD),Yaw - 180,(L1RD+R1RD)-(R2RD+L2RD)))*200 - E:angVel()*20) * shiftL(ang(E:inertia())))
}
