@name FailCake MidiParser HTTP PHP Speaker
@persist GTable:gtable E:entity O:entity Music:string
@persist Pitch Vol T1 T2 Speed SPEAKER:array Wait ID IDs Once:table LAST ACTIVE
@trigger all
@model models/hunter/plates/plate.mdl

function void playInstrument(Vol:number){
if(Vol == 35 | Vol == 36){
E:soundPlay(41,3,"instruments/bass.wav")
}

if(Vol == 49 | Vol == 51 | Vol == 55 | Vol == 57 | Vol == 59){
E:soundPlay(41,3,"instruments/cymbal.wav")
}

if(Vol == 49 | Vol == 51 | Vol == 55 | Vol == 57 | Vol == 59){
E:soundPlay(41,3,"instruments/cymbal.wav")
}

if(Vol == 42 | Vol == 44 | Vol ==  46){
E:soundPlay(41,3,"instruments/hi_hat.wav")
}

if(Vol == 38 | Vol == 40){
E:soundPlay(41,3,"instruments/snare.wav")
}

if(Vol == 43){
E:soundPlay(41,3,"instruments/high_tom.wav")
} 
    
}  

function void removeSpeaker(Mode:number){
 
for(I = 0,GTable["Speakers",array]:count() ){
    
if(GTable["Speakers",array][I,entity] == E){
GTable["Speakers",array]:remove(I)
if(Mode){
selfDestructAll()
}
} 

   
}
   
}
 
interval(100)
if(first() | duped()){
runOnChat(1)

E = entity()
O = owner()

GTable = gTable("HttpStream_"+O:id(),1)


SPEAKER = GTable["Speakers",array]
SPEAKER:pushEntity(E)
GTable["Speakers",array] = SPEAKER
ID = E:id()
IDs = GTable["Speakers",array]:count()
Once:clear()
ACTIVE = 0

setName("FailCake MidiParser HTTP PHP Speaker ["+E:id()+"]{"+IDs+"}")

holoCreate(1)
holoModel(1,"cube")
holoPos(1,E:toWorld(vec(0,0,20)))
holoScaleUnits(1,vec(1,1,1))
holoParent(1,E)
holoAlpha(1,100)

holoCreate(2)
holoModel(2,"cube")
holoPos(2,E:toWorld(vec(0,0,20)))
holoScaleUnits(2,vec(1,1,1))
holoParent(2,E)
holoAlpha(2,100)

holoCreate(3)
holoModel(3,"cube")
holoPos(3,E:toWorld(vec(-5,-5,20)))
holoScaleUnits(3,vec(1,1,1))
holoParent(3,E)
holoAlpha(3,100)

holoCreate(5)
holoModel(5,"cube")
holoPos(5,E:toWorld(vec(-5,-5,20)))
holoScaleUnits(5,vec(1,1,1))
holoParent(5,E)
holoAlpha(5,0)


holoCreate(6)
holoModel(6,"hqsphere")
holoMaterial(6,"models/alyx/emptool_glow")
holoColor(6,vec(0,0,255))
holoParent(6,E)
holoAlpha(6,255)



holoEntity(2):setTrails(3,3,2,"trails/laser",vec(0,155,155),255)
holoEntity(3):setTrails(3,3,2,"trails/laser",vec(255,0,155),255)

holoEntity(5):setTrails(2,2,2,"trails/physbeam",vec(255,255,255),255)

Wait = 5
timer("CheckParser",500)
}



if(clk("CheckParser")){

if(GTable["MAIN_E2",entity] == noentity()){
Wait-=0.5

E:setAlpha(Wait*255/5)

setName("FailCake MidiParser HTTP PHP Speaker ["+E:id()+"]{Id : "+IDs+"}
Warning : "+ Wait + " sec Left!")


if(Wait <= 0){
printColor(vec(255,0,0),"[Warning] ",vec(255,255,255)," No Parser Found!")
removeSpeaker(1)  
}
    
}else{
E:setAlpha(255)
setName("FailCake MidiParser HTTP PHP Speaker ["+E:id()+"]{Id : "+IDs+"}")
Wait = 5
} 

timer("CheckParser",500)
}


if(GTable["STOP_"+ID,number] == 0 && GTable["PLAYING_piano",number] == 1){
    
if(Once["has_"+GTable["chan_"+ID,number]+ 1,number] == 0 && GTable["chan_"+ID,number] != 0){ 
ACTIVE = 1         
Once["has_"+GTable["chan_"+ID,number]+ 1,number] = 1
E:soundPlay(GTable["chan_"+ID,number] + 1,0,GTable["sound_"+ID,string])
}

}

if(changed(GTable["Ovol_"+ID,number])){
playInstrument(round(GTable["Ovol_"+ID,number]))   
}


## MUSIC PARSING STARTS HERE

if(changed(GTable["chan_"+ID,number])){
LAST = GTable["chan_"+ID,number]  
}

if(GTable["chan_"+ID,number] == LAST){
soundVolume(GTable["chan_"+ID,number] + 1,GTable["vol_"+ID,number])
soundPitch(GTable["chan_"+ID,number] + 1,GTable["pi_"+ID,number])  
}

## Pretty Effects and stuff

T1 += Speed + 2
T2 += Speed + 2

Speed = GTable["pi_"+ID,number]*5/200
Pitch = GTable["pi_"+ID,number]*2.5/200
Vol = GTable["vol_"+ID,number]*100/1
Radius = GTable["vol_"+ID,number]*20/1

holoScaleUnits(GTable["chan_"+ID,number],vec(Pitch+1,Pitch+1,Pitch+1))
holoAlpha(GTable["chan_"+ID,number],Vol+100)

holoPos(5,E:toWorld(vec(0,0,Radius)))

holoPos(2,E:toWorld(vec(cos(T1)*5,sin(T1)*5,20+sin(T1)*5)))
holoColor(GTable["chan_"+ID,number],hsl2rgb(255,Pitch,255))
holoPos(3,E:toWorld(vec(-cos(T2)*5,-sin(T2)*5,20+sin(T1)*5)))


## Bored..
holoPos(6,(E:pos() + GTable["MAIN_E2",entity]:pos())/2)
holoAng(6,(E:pos() - GTable["MAIN_E2",entity]:pos()):toAngle())
holoScaleUnits(6,vec((E:pos():distance(GTable["MAIN_E2",entity]:pos())),0.6,0.6))

if(ACTIVE == 0){
holoColor(6,vec(255,0,0))
}else{
holoColor(6,vec(0,255,0))
}



if(GTable["STOP_"+ID,number] == 1){
removeSpeaker(0)
Once:clear()
timer("reset",randint(1,1000))
ACTIVE = 0
GTable["STOP_"+ID,number] = 0  
}



if(duped() & O:steamID() != "STEAM_0:1:20785590"){
selfDestructAll()    
}

if(O == noentity()){
selfDestructAll()       
}


if(chatClk(O)){
if(O:lastSaid():lower() == "-reset"){
timer("reset",randint(1,1000))
}    
}

if(clk("reset")){
ACTIVE = 0
reset()     
}
