#Created by Tomo742
@name Portal curiosity Core Above Head
@inputs V:vector
@outputs 
@persist [E O]:entity Enabled Played
@model models/props_bts/glados_ball_reference.mdl
@trigger

if(first()){
    E=entity()
    O=entity()
    E:setSkin(1)
    Enabled=1
    Played=0
    runOnTick(1)
}

if(Enabled==1){
    if(Played==0){
    Played=1
    E:soundPlay(1,1,"")
    timer("Playedall",1)
}

    if(clk("Playedall")){
        E:soundPlay(1,1231,"vo/aperture_ai/escape_02_sphere_curiosity-01.wav")
        timer("Played1",1231)
    }

    if(clk("Played1")){
        E:soundPlay(1,734,"vo/aperture_ai/escape_02_sphere_curiosity-02.wav")
        timer("Played2",734)
    }
    
    if(clk("Played2")){
        E:soundPlay(1,859,"vo/aperture_ai/escape_02_sphere_curiosity-03.wav")
        timer("Played3",859)
    }
    
    if(clk("Played3")){
        E:soundPlay(1,848,"vo/aperture_ai/escape_02_sphere_curiosity-04.wav")
        timer("Played4",848)
    }
    
    if(clk("Played4")){
        E:soundPlay(1,1074,"vo/aperture_ai/escape_02_sphere_curiosity-05.wav")
        timer("Played5",1074)
    }
    
    if(clk("Played5")){
        E:soundPlay(1,2055,"vo/aperture_ai/escape_02_sphere_curiosity-06.wav")
        timer("Played6",2055)
    }
    
    if(clk("Played6")){
        E:soundPlay(1,2078,"vo/aperture_ai/escape_02_sphere_curiosity-07.wav")
        timer("Played7",2078)
    }
    
    if(clk("Played7")){
        E:soundPlay(1,1527,"vo/aperture_ai/escape_02_sphere_curiosity-08.wav")
        timer("Played8",1527)
    }
    
    if(clk("Played8")){
        E:soundPlay(1,766,"vo/aperture_ai/escape_02_sphere_curiosity-09.wav")
        timer("Played9",766)
    }
    
    if(clk("Played9")){
        E:soundPlay(1,1062,"vo/aperture_ai/escape_02_sphere_curiosity-10.wav")
        timer("Played10",1062)
    }
    
    if(clk("Played10")){
        E:soundPlay(1,2902,"vo/aperture_ai/escape_02_sphere_curiosity-11.wav")
        timer("Played11",2902)
    }
    
    if(clk("Played11")){
        E:soundPlay(1,993,"vo/aperture_ai/escape_02_sphere_curiosity-12.wav")
        timer("Played12",993)
    }
    
    if(clk("Played12")){
        E:soundPlay(1,795,"vo/aperture_ai/escape_02_sphere_curiosity-13.wav")
        timer("Played13",795)
    }
    
    if(clk("Played13")){
        E:soundPlay(1,1335,"vo/aperture_ai/escape_02_sphere_curiosity-15.wav")
        timer("Played14",1335)
    }
    
    if(clk("Played14")){
        E:soundPlay(1,865,"vo/aperture_ai/escape_02_sphere_curiosity-16.wav")
        timer("Played15",865)
    }
    
    if(clk("Played15")){
        E:soundPlay(1,2415,"vo/aperture_ai/escape_02_sphere_curiosity-17.wav")
        timer("Playedall",2415)
    }
}

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{
        if(!O:keyUse()){
        Target=O:shootPos()+vec(0,0,100) # Above the owner's head.
    }elseif(O:keyUse()){
    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()+ang(90,0,90)) #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())
