@name Flashbang
@inputs 
@outputs 
@persist [Owner]:entity [Active C Smoke Size Prime]:normal [PS]:string
@trigger 

if (first()) {
    Owner=owner()
    runOnChat(1)
    print("Welcome "+Owner:name()+", to the LambdaTech Grenade System! Say 'flash' for a flash grenade and 'smoke' for a smoke grenade!")
}

if (chatClk(Owner)) {
    LastSaid=Owner:lastSaid():lower()
    if (LastSaid == "flash") {
        hideChat(1)
        runOnTick(1)
        PS="Flash"
        Prime+=3
        hint("Three nades added",4)
    }
    if (LastSaid == "smoke") {
        hideChat(1)
        runOnTick(1)
        PS="Smoke"
        Prime=1
        hint("Smoke ready",4)
    }
}

if (Prime & changed(Owner:keyUse()) & Owner:keyUse() & !Active & !Smoke) {
        if (PS == "Flash") {
        MVec=min(Owner:pos()+Owner:eye():setZ(0)*50,Owner:aimPos())
        holoCreate(1,MVec)
        holoCreate(2,MVec+vec(0,0,6))
        holoScale(2,vec(0.3,0.3,0.3))
        holoModel(2,"hqsphere")
        holoModel(1,"hqsphere")
        holoColor(1,vec(0,0,0))
        holoColor(2,vec(255,0,0))
        Active=1
        timer("3",500)
        soundPlay(1,6,"physics/metal/weapon_impact_hard1.wav")
        soundPlay(2,10,"buttons/blip1.wav")
        soundPlay(3,6,"ambient/machines/electric_machine.wav")
        hint("Flash grenade explosion in 1.5 seconds!",5)
        holoMaterial(1,"phoenix_storms/metalset_1-2")
        holoMaterial(2,"models/shiny")
    }
    if (PS == "Smoke") {
        holoCreate(3,Owner:shootPos())
        holoModel(3,"hqsphere")
        holoColor(3,vec(50,50,50))
        holoCreate(4,Owner:shootPos())
        holoModel(4,"hqsphere")
        holoColor(4,vec(50,50,50))
        Smoke=1
        timer("ResetS",8000)
        Size=0
        holoEntity(3):soundPlay(5,5,"ambient/gas/steam_loop1.wav")
        hint("Popping smoke!",4)
        holoMaterial(3,"props_combine/citadel_cable")
        holoMaterial(4,"props_combine/citadel_cable")
        #holoAlpha(3,224)
        #holoMaterial(3,"models/props/cs_office/clouds")
    }
    Prime--
}

if (Active) {
    C=!C
    holoColor(2,vec((C ? 0 : 255),0,0))
    
    if (clk("3")) {
        holoEntity(1):soundPlay(2,10,"buttons/blip1.wav")
        soundPitch(2,110)
        soundPitch(3,110)
        timer("2",500)
    }
    
    if (clk("2")) {
        holoEntity(1):soundPlay(2,10,"buttons/blip1.wav")
        soundPitch(2,120)
        soundPitch(3,130)
        timer("1",500)
    }
    
    if (clk("1")) {
        soundStop(3)
        holoEntity(1):soundPlay(4,10,"ambient/explosions/explode_1.wav")
        
        findIncludeClass("player")
        findIncludeClass("npc")
        findInSphere(holoEntity(1):pos(),200)
        FA=findToArray()
        
        
        
        for (G=1,FA:count()) {
            holoCreate(G+4,FA[G,entity]:pos())
            holoScaleUnits(G+4,-FA[G,entity]:boxSize():setZ(FA[G,entity]:height()))
            holoParent(G+4,FA[G,entity]:weapon())
            holoShadow(G+4,0)
            holoMaterial(G+4,"models/debug/debugwhite")
            holoAng(G+4,ang(0,0,0))
            FA[G,entity]:npcStop()
        }
        
        Active=0
        
        timer("Reset",6000)
        
        holoDelete(1)
        holoScale(2,vec(2,2,2))
        holoColor(2,vec(255,255,255))
    }
}

if (Smoke) {
    Size+=4
    holoScaleUnits(3,vec(Size,Size,Size))
    holoScaleUnits(4,-vec(Size,Size,Size))
}

if (clk("Reset") | clk("ResetS")) {
    holoDeleteAll()
    Size=0
    Smoke=0
    Active=0
    runOnTick(0)
    soundPurge()
}
