@name Filterer
@inputs 
@persist [E, O, Find]:entity X
@persist Hold Health Fuel 
@persist OutInc OutTime
@outputs
@model models/props_wasteland/horizontalcoolingtank04.mdl

#models:
#Filtered Ore: models/sprops/geometry/sphere_24.mdl (coloured)
#Input: models/props/cs_militia/militiarock05.mdl
#Fuel: models/props_junk/gascan001a.mdl

###########
#Main Code#
###########
if ( first() || dupefinished() )
{
    runOnTick(1)
    E = entity()
    O = owner()
    
    Fuel=10
    Health=50
    
    # Holo Stuff
    I = 1
    holoCreate(I)
    holoModel(I, "hq_cylinder")
    holoPos(I, E:toWorld(vec(-200,0,-60)))
    holoAng(I, E:toWorld(ang(0,0,0)))
    holoParent(I,E)
    holoScaleUnits(I, vec(50,50,0.1))
    holoColor( I,vec(255,0,0) )
    
    timer("",1000)
}
elseif ( clk("") )
{
    timer("",1000)
    X++
    
    findIncludeModel("models/props_junk/gascan001a.mdl")
    findIncludeModel("models/props/cs_militia/militiarock05.mdl")
    findInSphere(E:pos(),100)
    findAllowPlayerProps(O)
    findSortByDistance(E:pos())
    Find = find()
    if ( Find:isValid() && Find:model()=="models/props_junk/gascan001a.mdl" ) 
    { 
        Find:propDelete()
        if ( Fuel+25>100 ) { Fuel=100 } else { Fuel+=25 } 
    }
    elseif ( Find:isValid() && Find:model()=="models/props/cs_militia/militiarock05.mdl" && Hold<5 )
    {
        Find:propDelete()
        Hold++
        if ( Hold==1 )
        {
            OutInc=0
            OutTime=random(20,45)    
        }            
    }
    
    # Increment/decrement values
    if ( mod(X,2)==0 && Fuel > 0 && Hold>0 ) { Fuel-- }
    
    if ( mod(X,4)==0 && Fuel > 0 && Hold>0 ) 
    { 
        Health-- 
        if ( Health<=0 )
        {
            propSpawn("models/props_junk/garbage128_composite001a.mdl",E:pos()+vec(0,0,50),ang(0),0)
            Bomb=propSpawn("models/props_c17/oildrum001_explosive.mdl",E:pos()+vec(0,0,100),ang(0),0)
            Bomb:propBreak()
            selfDestruct()       
        }
    }
    
    if ( Hold > 0 ) # per second
    {
        OutInc++
        if ( OutInc >= OutTime ) 
        {
            Out=propSpawn("models/sprops/geometry/sphere_24.mdl",E:toWorld(vec(-200,0,10)),ang(0),0)
            Out:setColor( vec(50) )
            Out:setMaterial("models/debug/debugwhite")
            OutInc=0
            OutTime=random(20,45)
            
            Hold--       
        }           
    }
    
    if ( changed(Fuel>0 && Hold>0) && Fuel>0 && Hold>0 ) 
    {
        E:soundPlay(1,0,"ambient/machines/engine4.wav")    
    }
    elseif ( changed(!(Fuel>0 && Hold>0)) && !(Fuel>0 && Hold>0) ) { soundStop(1) }
	
    #Update name
    setName( "Filterer - "+Hold+"/5 ore - F: "+Fuel+"% - H: "+Health+"%" )  
}

if (owner():weapon() == owner():weapon("weapon_stunstick") && owner():aimEntity() == E && O:pos():distance(E:pos()) <200 && changed(O:keyAttack1()) && O:keyAttack1())
{
    if (Health+2 > 100) {Health=100} else {Health+=2}       
}
