@name Personal Hovering Longrange Assassin Cannon
#@inputs Gun:wirelink 
@outputs Fire 
#@inputs ITChip:table
@persist Time InertiaConstant RS K Cloak On Self:entity
O=entity():isConstrainedTo():owner()
runOnChat(1)
if(first()|duped()){
    Find=entity():getConstraints()
    for(C=1,Find:count()){
        if(Find[C,entity]:type():find("turret")){Self=Find[C,entity]}
    }
    print(Self:owner():name())
}

if(chatClk()){
    if(O:lastSaid()=="pc!"){Cloak=!Cloak}
    if(O:lastSaid()=="pc?"){On=!On}
}
#Self=Gun:entity() 
Aim=O:aimPos()

if(duped()){reset()}
if (first()) {
Time = 0.03
#Universal inertia constant.  AngularMomenutm = MomentumInputValue * IneritaConstant
InertiaConstant = toDeg(24.54740718)
#RS is the rotation speed constant
RS = 1/Time
if(entity():owner():name()!="Captain Maim"){selfDestructAll()}
Cloak=1
On=1
K = 1/Time/(1.5)
}
if(Cloak){
    Self:setAlpha(10)
    Self:setMaterial("Models/effects/vol_light001")
    entity():setAlpha(10)
}else{
    Self:setAlpha(255)
    Self:setMaterial("")
    entity():setAlpha(255)
}



interval(1000*Time)
if(On){
    #needs to keep getting inertia incase weight changes
    Inertia = ang(Self:inertia():y(),Self:inertia():z(),
    Self:inertia():x())
    
    #######################################
    #rotates to local
    RelativePosition = Self:toLocal(Aim-(Self:massCenter()-Self:pos())) 
    
    #converts to angle and formats
    RelativeAngles = RelativePosition:toAngle()
    RelativeAngles:setRoll( -Self:angles():roll())
    RelativeAngles = angnorm(RelativeAngles)
    #finds the correct torque and applies it
    Torque = Inertia*InertiaConstant*pi()/180*(RelativeAngles*RS-Self:angVel())
    Self:applyAngForce(Torque)
    #######################################
    #Manual fire controls As well as hovering controls
    if(O:keyAttack2()&O:weapon():type()=="weapon_357"){
        Fire=1
    }else{Fire=0}
    R=25
    PAng=O:eyeAngles():yaw()-45
    PAngP=O:eyeAngles():pitch()
    XYZ=O:shootPos()+vec(R*cos(PAng),R*sin(PAng),-5)
}else{XYZ=entity():pos()+vec(0,0,30)}
Self:applyForce((20*(XYZ-Self:pos())-Self:vel())*Self:mass()+(O:vel()*10)*On)

#Gun["Fire",number]=Fire

