# Next time dont use so many times
@name Mining Rig
@inputs 
@persist [E, O]:entity 
@persist Fuel HP OutputTime OutputInt
@outputs
@model models/props_combine/combinethumper002.mdl

# Models:
# Fuel - models/props_junk/gascan001a.mdl ( +25% each )
# Raw Ore - models/props/cs_militia/militiarock05.mdl ( 1 per minute )

###########
#Main Code#
###########
if ( first() || dupefinished() )
{
    runOnTick(1)
    E = entity()
    O = owner()
    
    HP=75
    Fuel=0
    
    # Holo Stuff
    I = 1
    holoCreate(I)
    holoModel(I, "hq_cylinder")
    holoPos(I, E:toWorld(vec(13.5,-120,0)))
    holoAng(I, E:toWorld(ang(0,0,0)))
    holoParent(I,E)
    holoScaleUnits(I, vec(50,50,0.1))
    holoColor( I,vec(255,0,0) )
    
    # Start timers
    OutputTime=20
    OutputInt=0
    timer("fuel",2000)
    timer("degrade",3500)
    timer("progress",1000)
    
    E:soundPlay(2,0,"ambient/machines/pump_loop_1.wav")
}

# Check if refueling or repairing
findIncludeModel("models/props_junk/gascan001a.mdl")
findInSphere(E:pos(),50)
findSortByDistance(E:pos())
Can=find()
if ( Can:isValid() )
{
    if ( Fuel+25 > 100 ) {Fuel=100}
    else {Fuel+=25}
    
    Can:propDelete()
    E:soundPlay(1,3,"ambient/gas/steam_loop1.wav")   
}
elseif (owner():weapon() == owner():weapon("weapon_stunstick") && owner():aimEntity() == E && O:pos():distance(E:pos()) <100 && changed(O:keyAttack1()) && O:keyAttack1() && HP <100)
{
    HP+=2        
}

# Degrade and loose fuel etc
if ( clk("fuel") )
{
    timer("fuel",2000)
    if ( Fuel > 0 ) { Fuel-=1 }
    else { soundStop(2) }        
}
elseif ( clk("degrade") )
{
    timer("degrade",3500)
    if (Fuel > 0) {HP-=1}
    if ( HP<=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()   
    }    
}
elseif ( clk("progress") ) { timer("progress",1000)
    if (Fuel > 0) {OutputInt++}
    if ( OutputInt >= OutputTime )
    {
        OutputInt=0
        Ore=propSpawn("models/props/cs_militia/militiarock05.mdl",E:toWorld(vec(13.5,-120,150)),ang(0),0)
        Ore:setMass( random(1000,3000) )        
    }       
}

# Update Name
setName("Mining Rig - Fuel: "+Fuel+"% - Quality: "+HP+"%")

if ( changed(Fuel>0) && Fuel>0 )
{
    E:soundPlay(2,0,"ambient/machines/pump_loop_1.wav")    
}
elseif ( changed(!Fuel>0) && !Fuel>0 ) { soundStop(2) }
