@name Random Generation
@inputs DamageDetector:wirelink Clk 
@outputs 
@persist M I HN Health On Damage Trees:table Props:entity Tree:array 
runOnChat(1)

if (chatClk(owner()) & owner():lastSaid():lower():find("!go")) {
    hideChat(1)
    M = 30
    HN = 100
    
    Damage = DamageDetector["Damage",number]
    Health = Damage > 0 ? HN + Damage : HN
}

if (first()) {
    rangerPersist(1)
    rangerHitEntities(0)
    rangerHitWater(0)
    
    Tree = array()
    
    On = 1
}

if (I < M & holoCanCreate() & propCanCreate()) {
    interval(45)
    
    Trees = table(
        1 = "models/props_foliage/mall_tree_large01.mdl",
        2 = "models/props_foliage/mall_tree_medium01.mdl",
        3 = "models/props_foliage/tree_city01.mdl",
        4 = "models/props_foliage/tree_dead01.mdl",
        5 = "models/props_foliage/tree_dead02.mdl",
        6 = "models/props_foliage/tree_dead04.mdl",
        7 = "models/props_foliage/tree_deciduous_01a-lod.mdl",
        8 = "models/props_foliage/tree_deciduous_01a.mdl",
        9 = "models/props_foliage/tree_dry01.mdl",
        10 = "models/props_foliage/tree_dry02.mdl",
        11 = "models/props_foliage/tree_pine04.mdl",
        12 = "models/props_foliage/tree_pine05.mdl",
        13 = "models/props_foliage/tree_pine06.mdl"
    )
    
    I++
    
    Rng = rangerOffset(1010,entity():toWorld(vec(randint(-500,500)*1,randint(-500,500)*1,1000)),vec(0,0,-1))
    
    holoCreate(I,Rng:pos()+holoEntity(I):boxSize())
    holoAng(I,Rng:hitNormal():toAngle()+ang(90,0,0))
    holoModel(I,Trees[randint(1,Trees:count()),string])
    holoScale(I,vec(0.6,0.6,0.6))
    
    Props = propSpawn("models/sprops/cylinders/size_5/cylinder_12x96.mdl",holoEntity(I):pos() + vec(0,0,96/2),1)
    Props:setAlpha(0)
    Tree:pushEntity(Props)
    
    DamageDetector["Entities",array] = Tree
} else {interval(100)}

if (On == 1) {
    if (changed(Clk) & Clk) {
        Health = clamp(Health-Damage,0,HN)
    }
}

for (K = 1,Tree:count()) {
    T = Tree[K,entity]
    if (Health == 0) {T:setColor(vec(255,0,0),255)}
}

if (chatClk(owner()) & owner():lastSaid():lower():find("!r")) {
    hideChat(1)
    propDeleteAll()
    holoDeleteAll()
}

if (last()) {Props:propDelete()}
