Snippet Regrowing Trees

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,497
Regrowing trees... who wouldn't want that?
I tend to add it even to maps that can't even kill any :p

Anyway, given it's simple but still annoying to recreate it for every map, I made a library out of that thing.
Copy&paste to your map, map header for example, done.


Required edits:
- "RegrowAfter" is simply the time in seconds it takes a tree until it regrows.


JASS:
library REGROWTREES initializer Init
globals
    private constant real RegrowAfter = 23.0 // seconds to wait until tree regrows
    private unit u
endglobals
private function IsTree takes nothing returns boolean
    local boolean b
    call SetUnitX(u, GetWidgetX(GetFilterDestructable()))
    call SetUnitY(u, GetWidgetY(GetFilterDestructable()))
    set b = IssueTargetOrder(u, "harvest", GetFilterDestructable())
    call IssueImmediateOrder(u, "stop")
    return b
endfunction
private function Regrow takes nothing returns nothing
    call TriggerSleepAction(RegrowAfter)
    call DestructableRestoreLife(GetDyingDestructable(), GetDestructableMaxLife(GetDyingDestructable()), true)
endfunction
private function Register takes nothing returns nothing
    call TriggerRegisterDeathEvent(bj_destInRegionDiesTrig, GetEnumDestructable())
endfunction
private function Init takes nothing returns nothing
    set u = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), 'nmpe', 0, 0, 0) // Mur'gul Slave...
    call SetUnitInvulnerable(u, true)
    call ShowUnit(u, false)
    call UnitAddAbility(u, 'Aloc') // Locust
    call UnitAddAbility(u, 'Ahrl') // Harvest Lumber
    set bj_destInRegionDiesTrig = CreateTrigger()
    call EnumDestructablesInRect(bj_mapInitialPlayableArea, Condition(function IsTree), function Register)
    call TriggerAddAction(bj_destInRegionDiesTrig, function Regrow)
    call RemoveUnit(u)
    set u = null
endfunction
endlibrary



Thanks to a brilliant idea by PitzerMike, no tree types or related input is required.
A tree is any destructible that can be harvested for Lumber.
Works on custom trees too.


By popular request, here's a version that waits for all units within a given range of the dead tree to first leave before it attempts to regrow:
JASS:
library REGROWTREES initializer Init
globals
    private constant real RegrowAfter = 23.0 // seconds to wait until tree regrows
    private constant real Range = 250.0      // distance around tree that needs to be free of units before regrowing
    private unit u
    private group g = CreateGroup()
    private boolexpr b
    private boolean ok
endglobals
private function IsTree takes nothing returns boolean
    local boolean b
    call SetUnitX(u, GetWidgetX(GetFilterDestructable()))
    call SetUnitY(u, GetWidgetY(GetFilterDestructable()))
    set b = IssueTargetOrder(u, "harvest", GetFilterDestructable())
    call IssueImmediateOrder(u, "stop")
    return b
endfunction
private function W2D takes widget w returns destructable
    return w // do not inline
    return null
endfunction
private function IsUnitNear takes nothing returns boolean
    if GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0.5 then
        set ok = false
    endif
    return false
endfunction
private function Regrow takes nothing returns nothing
    local widget t = GetTriggerWidget()
    call TriggerSleepAction(RegrowAfter)
    loop
        set ok = true
        call GroupClear(g)
        call GroupEnumUnitsInRange(g, GetWidgetX(t), GetWidgetY(t), Range, b)
        exitwhen ok
        call TriggerSleepAction(3.0)
    endloop
    call DestructableRestoreLife(W2D(t), GetDestructableMaxLife(W2D(t)), true)
    set t = null
endfunction
private function Register takes nothing returns nothing
    call TriggerRegisterDeathEvent(bj_destInRegionDiesTrig, GetEnumDestructable())
endfunction
private function Init takes nothing returns nothing
    set u = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), 'nmpe', 0, 0, 0) // Mur'gul Slave...
    call SetUnitInvulnerable(u, true)
    call ShowUnit(u, false)
    call UnitAddAbility(u, 'Aloc') // Locust
    call UnitAddAbility(u, 'Ahrl') // Harvest Lumber
    set bj_destInRegionDiesTrig = CreateTrigger()
    call EnumDestructablesInRect(bj_mapInitialPlayableArea, Condition(function IsTree), function Register)
    call TriggerAddAction(bj_destInRegionDiesTrig, function Regrow)
    set b = Condition(function IsUnitNear)
    call RemoveUnit(u)
    set u = null
endfunction
endlibrary


Usage is still as simple:
Copy&paste to your map, done.
 
JASS:
    return t == 'VTlt' or t == 'CTtr' or t == 'Cttc' // Edit tree-types as needed


Shove into the globals block?
 
JASS:
globals
    private constant real RegrowAfter = 23.0 // seconds to wait until tree regrows
    private constant integer TREE1 = 'VTlt'
    private constant integer TREE2 = 'CTtr'
    private constant integer TREE3 = 'Cttc'
    private constant integer TREE4-10 = blah
endglobals
private function TreeTypeFilter takes nothing returns boolean
    local integer t = GetDestructableTypeId(GetFilterDestructable())
    return t == TREE1 or t == TREE2 or t == TREE3 or t == TREE4-10
endfunction


It'll make it more newbie friendly. And you can do up to 10 and throw in instructions on how to go over 10.
 
You can do the
JASS:
return t == TREE1 or t == TREE2 or t == TREE3 or t == TREE4 or t == TREE5 or t == TREE6 or t == TREE7 or t == TREE8 or t == TREE9 or t == TREE10

Just make it so that users can easily change them.
 
Considering this is a 21 line snippet, I'm pretty confident people can locate the 7:th line with the comment "//Edit tree-types as needed".
 
Nice script :thup: I was just wondering if a unit is in the way does it get moved out of the area or does the tree not grow?
 
The trees ALWAYS grow back :p
In theory, it's possible to end up with a configuration that traps a unit in the middle of a forest.
Though that's dark theory only, and not "limited" to this script.
 
The trees ALWAYS grow back :p
In theory, it's possible to end up with a configuration that traps a unit in the middle of a forest.
Though that's dark theory only, and not "limited" to this script.

Oh :p I didn't know because usually I play melee games and the trees never grow back to know what will happen.
 
You could use this:

JASS:
t == 'ATtr' or t=='BTtw'or t=='KTtw' or t=='YTft' or t=='JTct' or t=='YTst' or t=='YTct' or t=='YTwt' or t=='JTwt' or t=='DTsh' or t=='FTtw' or t=='CTtr' or t=='ITtw' or t=='NTtw' or t=='OTtw' or t=='ZTtw' or t=='WTst' or t=='LTlt' or t=='GTsh' or t=='Xtlt' or t=='WTtw' or t=='Attc' or t=='BTtc' or t=='CTtc' or t=='ITtc' or t=='NTtc' or t=='ZTtc'


And maybe make another function where the user can put in their own trees:
JASS:
private function TreeFilterCustom takes nothing returns boolean
    local integer t = GetDestructableTypeId(GetFilterDestructable())
    return false // Remove "false" and put in the Raw codes of custom trees here
endfunction

private function TreeTypeFilter takes nothing returns boolean
    local integer t = GetDestructableTypeId(GetFilterDestructable())
    return t == 'ATtr' or t=='BTtw'or t=='KTtw' or t=='YTft' or t=='JTct' or t=='YTst' or t=='YTct' or t=='YTwt' or t=='JTwt' or t=='DTsh' or t=='FTtw' or t=='CTtr' or t=='ITtw' or t=='NTtw' or t=='OTtw' or t=='ZTtw' or t=='WTst' or t=='LTlt' or t=='GTsh' or t=='Xtlt' or t=='WTtw' or t=='Attc' or t=='BTtc' or t=='CTtc' or t=='ITtc' or t=='NTtc' or t=='ZTtc' or TreeFilterCustom() 
endfunction
 
I remember that there is a IsDestTree function, try to search on wc3jass.com or hive
 
JASS:
//=======================================//
//Credits to PitzerMike for this function//
//=======================================//

globals
    private constant integer DUMMY_ID = 'h000'
endglobals

private function TreeFilter takes nothing returns boolean
    local destructable d = GetFilterDestructable()
    local boolean i = IsDestructableInvulnerable(d)
    local unit u = CreateUnit(Player(PLAYER_NEUTRAL_PASSIVE), DUMMY_ID, GetWidgetX(d), GetWidgetY(d), 0)
    local boolean result = false

    call UnitAddAbility(u, 'Ahrl')

    if i then
        call SetDestructableInvulnerable(d, false)
    endif

    set result = IssueTargetOrder(u, "harvest", d)
    call RemoveUnit(u)

    if i then
      call SetDestructableInvulnerable(d, true)
    endif

    set u = null
    set d = null
    return result
endfunction
 
Quick noob question:
How do i copy and paste it to my map?
And is this for all types of trees?

(i'm a noob at jass, i know what some mean, not all)
 
i'm very noob

sory but... can u explain how to copy to the map? i'm very very noob TT

in my map.... copy to..... where?

maybe i should open anything else first ?
after i opened the map...
then paste it....

thx..
 
Make a new trigger.
Go to "Edit -> Convert to Custom Text".
Erase what's there and paste the script into it.
Save the map.
GG!

Oh, and nice snippet AceHart!
Might use this in my AoS.
 
error =C

i just did what u told me to do to paste the config wolf... but there's an error when i saved the map...

it said "script error"
all script are error....

how's that?

maybe there is another editor?

i only use editor that i got from the frozen throne cd

the error is like this

47 compile error
...
Note: rest edited out
 
Put all that in a quote tag please :eek: .
And you need Jass NewGen WE (i think?)

EDIT: @AceHart: Could you make the unit "holdposition" instead of "stop"?
I know it's easily configured, but for people who don't know how it's nice.
 
General chit-chat
Help Users
  • The Helper The Helper:
    alternatively if you not making at least 23 an hour you could work in an Aldi warehouse
  • Varine Varine:
    Yeah I've been thinking about using AI for shit. I'm on vacation next week so I'm going to spend some time reorganizing everything and getting all my shit back in order
  • Varine Varine:
    lol I technically make less than 23 right now because I'm on salary and am there all the time, but it's a lot more than a normal wage still. I also have a meeting soon to renegotiate my pay because I want about a 25% increase to account for what I'm actually doing or a re-evaluation of our duties so that that my responsibilities are more in line with my pay. Depending on how that goes I'm prepared to give notice and move on, I don't mind taking less money so I'd have time for the rest of my life, but I'd rather they just fucking pay me enough to justify the commitment on my end. Plus right now I hold pretty much all the cards since I'm the only one actually qualified for my position.
    +1
  • Varine Varine:
    The other chef was there before me and got his position by virtue of being the only adult when the old general manager got married and didn't want to deal with the kitchen all the time, and happened to be in the position when the GM quit. New GM is fine with front of house but doesn't know enough about the kitchen side to really do anything or notice that I'm the one primarily maintaining it. Last time I left they hired like 3 people to replace me and there was still a noticeable drop in quality, so I got offered like 6 dollars an hour more and a pretty significant summer bonus to come back
  • Varine Varine:
    So honestly even if I leave I think it would last a couple of months until it's obvious that I am not exactly replaceable and then I would be in an even better position.
  • Varine Varine:
    But as of right now I have two other job offers that are reasonably close to what my hourly equivalency would be, and I would probably have more time for my other projects. The gap would be pretty easy to fill up if I had time to do my side jobs. I use to do some catering and private events, personal lessons, consultations, ect, and I charge like 120 an hour for those. But they aren't consistent enough for a full time job, too small of a town. And I'm not allowed to move for another year until my probation is over
  • Varine Varine:
    I guess I could get it transferred, but that seems like a hassle.
  • Varine Varine:
    Plus I have a storage room full of broken consoles I need to fix. I need to build a little reflow oven so I can manufacture some mod chips still, but it'll get there.
    +1
  • Varine Varine:
    I would like to get out of cooking in general at some point in the next ten years, but for the time being I can make decent money and pump that into savings. I've been taking some engineering classes online, but those aren't exactly career oriented at the moment, but I think getting into electronic or computer engineering of some sort would be ideal. I'm just going to keep taking some classes here and there until there's one that I am really into.
    +2
  • The Helper The Helper:
    There is money in fixing and reselling consoles. Problem is people know that so they are doing it
  • The Helper The Helper:
    If you can find a source of broken consoles though you can make money fixing them - sometimes some big money
  • Varine Varine:
    I was buying them on Ebay, but it's pretty competitive, so more recently I've just been telling the thrift stores to call me and I will come take all their electronics they can't sell. I've volunteered there before and people use them like a dump sometimes, and so they just have a massive amount of broken shit they throw away
  • Varine Varine:
    The local GoodWill was pretty into it, surprisingly the animal shelter store was not. The lady I talked to there seemed to think I was trying to steal stuff or something, she wasn't very nice about it. Like I'm just trying to stop you having to throw a bunch of electronics away, if you can sell it great. I'd probably pay you for the broken shit too if you wanted
  • Varine Varine:
    Then I make posts on Facebook yard sale pages sometimes saying I want your old electronics, but Facebook being Facebook people on there are also wary about why I want it, then want a bunch of money like it's going to be very worth it
  • Varine Varine:
    Sooner than later I'm going to get my archives business going a little more. I need some office space that is kind of hard to get at the moment, but without it, I have to be like "Yeah so go ahead and just leave your family heirlooms and hundred year old photographs here at my shitty apartment and give me a thousand dollars, and next month I'll give you a thumb drive. I promise I'll take care of them!"
    +1
  • Varine Varine:
    I can do some things with them at their home, but when people have thousands of slides and very delicate newspaper clippings and things, not really practical. I
  • Varine Varine:
    I would be there for days, even with my camera set up slides can take a long time, and if they want perfect captures I really need to use my scanners that are professionally made for that. My camera rig works well for what it is, but for enlargements and things it's not as good.
  • Varine Varine:
    I've only had a couple clients with that so far, though. I don't have a website or anything yet though.
  • Varine Varine:
    Console repair can be worthwhile, but it's also not a thing I can do at scale in my house. I just don't have room for the equipment. I need an office that I can segregate out for archival and then electronic restoration.
  • Varine Varine:
    But in order for that to be real, I need more time, and for more time I need to work less, and to work less I need a different job, and for a different job I need more money to fall back on so that I can make enough to just pay like, rent and utilities and use my savings to find these projects
    +1
  • Varine Varine:
    Another couple years. I just need to take it slow and it'll get there.
  • jonas jonas:
    any chance to get that stolen money back?
  • jonas jonas:
    Maybe you can do console repair just as a side thing, especially if there's so much competition business might be slow. Or do you need a lot of special equipment for that?
  • jonas jonas:
    I recently bought a used sauna and the preowner told me some component is broken, I took a look and it was just a burnt fuse, really cheap to fix. I was real proud of my self since I usually have two left hands for this kinda stuff :p
  • tom_mai78101 tom_mai78101:
    I am still playing Shapez 2. What an awful thing to happen, Varine, and hopefully everything has been sorted out soon. Always use multi-factor authentication whenever you have the opportunity to do so.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top