@name 3d holograms music thing
@inputs 
@outputs [Offset Div Pos Box]:vector I Set Zed Note Saved:table Play Y
@persist [H O E S]:entity Snap [BarPos Min Max]:vector Z M4:matrix4 Tempo Synth:string Idx Bars
@model models/hunter/plates/plate1x5.mdl



Base = entity()
Scale = 0.90
TardisMaterial = "phoenix_storms/grey_chrome"
Shiny = "models/shiny"
Debug = "models/debug/debugwhite"
Blue = vec(0.25,0.25,0.75)*255
Red = vec(0.75,0.25,0.25)*255
Green = vec(0.25,0.75,0.25)*255
Fade = 10       
Orange = vec(19,0.30,0)*255
White = vec(255,255,255)*255

if(first() | duped()){
    runOnTick(1)
    runOnChat(1)
E = entity()
#O = findPlayerByName("Zenith")
O = owner()
Plate = vec2(1,5)
Box = round( (E:boxSize()-0.5) / (vec(Plate[1],Plate[2],0)*4) , 2):setZ(0)
Snap = Box[1]
H = holoCreate(1,E:pos(),vec()+1,E:angles(),vec(255,0,0))
Min = -vec(Snap*Plate[1]*2,Snap*Plate[2]*2,0)
Max = vec(Snap*Plate[1]*2-Snap,Snap*Plate[2]*2-Snap,0)
Z = E:boxSize()[3]/3+6
I=1
Div = vec(Plate*1.5)*Snap
Tempo = 250
    Y = Plate[2]*2-1
    Bars = Plate[2]*4
    BarPos = vec(0,-Snap*Y-Snap/2,2)
    holoCreate(101,E:toWorld(BarPos),vec(Snap*4,Snap,1)/12,E:angles())
    holoColor(101,Red)
    holoParent(101,E)
    holoCreate(102,E:pos(),E:boxSize()/12,E:angles())
    holoParent(102,E)
    holoColor(102,Orange)
    E:setAlpha(0)
    
    Synth = "synth/square.wav"
}

ShootPos = O:shootPos() , Direction = O:eye()

if(O:weapon():type()=="weapon_physcannon"){
    Set = O:keyUse()
}

if(!Set){
    if($Set){
        Saved[Offset[1]+","+Offset[2],number] = Note
        Saved["Ent "+Offset[1]+","+Offset[2],number] = S:id()
        Note = 0
        printColor(vec(0,255,0),"Saved "+Offset[1]+","+Offset[2])
    }
    Pos = E:toLocal( (ShootPos+(E:up():dot(E:pos()-ShootPos)/E:up():dot(Direction))*Direction) )
    Pos = clamp(round(Pos/Box)*Box,Min,Max)+vec(Snap,Snap,0)/2
    Offset = (Div+Pos)/Snap+vec(1,2,0)
    V = E:toWorld(Pos:setZ(Z))
    M4 = matrix4(E:angles(),V)
    Zed = 0
    soundStop("holo",0.5)
}else{
Zed = E:toLocal((ShootPos+(M4:x():dot(M4:pos()-ShootPos)/M4:x():dot(Direction))*Direction))[3]
Zed = clamp( round(Zed/Snap)*Snap , 0 , Snap*10 )
    CheckEnt = Saved["Ent "+Offset[1]+","+Offset[2],number]
    if($Set>0){
        if(!CheckEnt){
            I++
            S = holoCreate(I,H:pos(),vec()+Snap/12,E:angles())
            holoParent(I,E)
            Idx = I
        }else{
            S = entity(CheckEnt)
            Idx = holoIndex(S)
            printColor(vec(255,0,0),"Replacing Holo "+Idx)
        }
        soundPlay("holo",0,Synth)
    }
    Off = (H:pos()-M4:pos())
    if(changed(Off) | $Set){
        Note = ((Zed/Snap)+1)+Offset[1]*10+40
        holoPos(Idx,M4:pos()+Off/2)
        
        Queue = round(random(1,3))
        round(Queue)
        print("Que:"+Queue)
        if(Queue == 1){
            holoColor(Idx,Red)
        }
        if(Queue == 2){
            holoColor(Idx,Blue)
        }
        if(Queue == 3){
            holoColor(Idx,Green)
        }
        
        holoScale(Idx,vec(Snap,Snap,Off:length()+Snap)/12)
        soundPitch("holo",2^(Note/12))
        soundVolume("holo",0.5)
    }
}
holoPos(1,M4:pos()+M4:z()*Zed)
holoAng(1,quat(M4:x(),M4:z()):toAngle())
holoColor(1,Red)

SetPlay = O:isCrouch() & O:keyAttack2()
if(changed(SetPlay)&SetPlay){
    Play = !Play
    Y = 0
}
if(Play){
    if($Play){
        holoColor(101,Red)
    }
    timer("play",Tempo)
    if(clk("play")){
        Y = Y%Bars+1
        for(X=1,4){
            ID = Saved["Ent "+X+","+Y,number]
            Pitch = Saved[X+","+Y,number]
            Ent = entity(ID)
            Ent:soundPlay(ID,0,Synth,Tempo/1000)
            soundPitch(ID,2^(Pitch/12))
        }
        holoPos(101,E:toWorld(BarPos+vec(0,Y*Snap-Snap,0)))
    }
}elseif($Play<0){
    Y = 0
    holoPos(101,E:toWorld(BarPos))
    holoColor(101,vec(0,160,70))
    
}

Remove = !O:isCrouch() & O:keyAttack2()

if(changed(Remove) & Remove){
    CheckEnt = Saved["Ent "+Offset[1]+","+Offset[2],number]
    S = entity(CheckEnt)
    Idx = holoIndex(S)
    printColor(vec(255,0,0),"Removing Holo "+Idx+"!!")
    Saved[Offset[1]+","+Offset[2],number] = 0
    Saved["Ent "+Offset[1]+","+Offset[2],number] = 0
    holoDelete(Idx)
}

if(chatClk(O)){
    Lsx = O:lastSaid():lower():explode(" ")
    Cmd = Lsx[1,string]
    if(Cmd=="tempo"){
        hideChat(1)
        Tempo = clamp(Lsx[2,number],40,500)
    }
}
