@name Portal Turret
@inputs 
@outputs Scale Open Move Pitch:angle Cos
@persist [H O E]:entity
@model models/hunter/blocks/cube05x1x025.mdl

if(first() | duped()){
    runOnTick(1)
    runOnLast(1)
    runOnChat(1)
E = entity():isWeldedTo() ? entity():isWeldedTo() : entity()
O = owner()

Scale = 0.6
Cut = vec(0,5+1.5,0)*Scale

Ang = ang(0,0,90)

H = holoCreate(1,E:toWorld(vec(0,0,0):rotate(Ang)*Scale),Scale*vec(30,20,60)/12,E:toWorld(Ang))
holoClipEnabled(1,1,1)
holoClipEnabled(1,2,1)
holoClip(1,1,-Cut,vec(0,1,0),0)
holoClip(1,2,Cut,vec(0,-1,0),0)
holoModel(1,"hq_sphere")
holoParent(1,E)

holoCreate(2,E:toWorld(vec(0,0,0):rotate(Ang)*Scale),Scale*-vec(30,20,60)/12,E:toWorld(Ang),vec()+70)
holoClipEnabled(2,1,1)
holoClipEnabled(2,2,1)
holoClip(2,1,-Cut,vec(0,1,0),0)
holoClip(2,2,Cut,vec(0,-1,0),0)
holoModel(2,"hq_sphere")
holoParent(2,1)

holoCreate(3,E:toWorld(vec(0,0,0):rotate(Ang)*Scale),Scale*vec(30,20,60)/12,E:toWorld(Ang))
holoClipEnabled(3,1)
holoClip(3,-Cut,vec(0,-1,0),0)
holoModel(3,"hq_sphere")
holoParent(3,1)

holoCreate(4,E:toWorld(vec(0,0,0):rotate(Ang)*Scale),Scale*-vec(30,20,60)/12,E:toWorld(Ang),vec()+70)
holoClipEnabled(4,1)
holoClip(4,-Cut,vec(0,-1,0),0)
holoModel(4,"hq_sphere")
holoParent(4,3)

    holoCreate(5,E:toWorld(vec(0,0,0):rotate(Ang)*Scale),Scale*vec(30,20,60)/12,E:toWorld(Ang))
    holoClipEnabled(5,1)
    holoClip(5,Cut,vec(0,1,0),0)
    holoModel(5,"hq_sphere")
    holoParent(5,1)
    
    holoCreate(6,E:toWorld(vec(0,0,0):rotate(Ang)*Scale),Scale*-vec(30,20,60)/12,E:toWorld(Ang),vec()+70)
    holoClipEnabled(6,1)
    holoClip(6,Cut,vec(0,1,0),0)
    holoModel(6,"hq_sphere")
    holoParent(6,5)
    
E:setAlpha(0)
}

Clk = O:keyUse() & O:aimEntity()==E

if(changed(Clk) & Clk){
    Open = !Open
}
Move = clamp(Move+(!Open-Open)*3,0,180)

Cos = (1+cos(Move))*5*Scale
Off = 0.1
holoPos(3,H:toWorld(vec(0,-Cos-Off,0)))
#holoAng(3,H:toWorld(ang(0,0,-Cos*2)))

holoPos(5,H:toWorld(vec(0,Cos+Off,0)))
#holoAng(5,H:toWorld(ang(0,0,Cos*2)))

Pitch = E:angles()
Over = !inrange(abs(Pitch[3]),70,110)

if(changed(Over) & Over){
    soundPlay(1,1,"buttons/blip2.wav")
    Open = 0
}
