@name Portal Radio 2 Above Head
@inputs V:vector
@persist [O E]:entity
@model models/props/radio_reference.mdl

if(first()){
    O=owner()
    E=entity()
    Holosoundplay1=holoCreate(1)
    holoAlpha(1,(0))
    holoParent(1,entity())
    Holosoundplay2=holoCreate(2)
    holoAlpha(2,(0))
    holoParent(2,entity())
    Holosoundplay2=holoCreate(3)
    holoAlpha(3,(0))
    holoParent(3,entity())
    timer("finishedspawning",100)
}

if(clk("finishedspawning")){
    entity():soundPlay(1,108627,"ambient/music/looping_radio_mix.wav")
    Holosoundplay1:soundPlay(2,108627,"ambient/music/looping_radio_mix.wav")
    Holosoundplay2:soundPlay(3,108627,"ambient/music/looping_radio_mix.wav")
    Holosoundplay2:soundPlay(4,108627,"ambient/music/looping_radio_mix.wav")
}

if(duped()){
    concmd("say Made by Tomo742 and you cannot haz naughty duper")
    timer("selfdestruct",100)
}

if(clk("selfdestruct")){
    selfDestruct()
}

if(tickClk()){
    ##############################
    if(->V){
        Target=V
    }else{
        Target=O:shootPos()+vec(0,0,100) # Above the owner's head.
        #Target=O:aimPos()+vec(0,0,50) # Above where the owner is looking.
    }
    E:applyForce(((Target-E:pos())*10-E:vel())*E:mass()) #<-- makes E go to Target
    ##############################
}elseif(first()){
    E=entity()
    O=owner()
    runOnTick(1)
}

                #STABILISE
                TarQ = quat(O:eyeAngles()) #Calculate quaternion for target orientation
                CurQ = quat(E) #Calculate current orientation quaternion
                
                #TarQ/CurQ is a quaternion representing the rotation that will rotate the object from current orientation to the target one.
                Q = TarQ/CurQ
                
                #applyTorque() works on intrinsic vectors! Get the rotation vector and transform it.
                EP=E:pos()
                TTV = E:toLocal(rotationVector(Q)+EP)
                #Alternatively, can use "V = transpose(matrix(E))*rotationVector(Q)"
                
                #Apply torque. angVelVector() works like a delta term.
                #Factors 150 and 12 can be adjusted to achieve best effect.
                E:applyTorque((150*TTV - 12*E:angVelVector())*E:inertia())
