• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

lumber harvesting building?

Status
Not open for further replies.
Level 9
Joined
Jul 26, 2005
Messages
553
I am trying to get a building to harvest lumber within it's range, but it gets buggy if left to the ability alone (it won't auto-return and find new targets)

So, Equal suggested to open a thread, where he could post proper help...
 
You can create a dummy harvester unit to harvest for the building, everytime it finishes construction
  • Init Build
    • Events
      • Unit - A unit Finishes construction
    • Conditions
      • (Unit-type of (Triggering unit)) Equal to Scout Tower
    • Actions
      • Set Point1 = (Position of (Triggering unit))
      • Unit - Create 1 Dummy Harvester for (Triggering player) at Point1 facing Default building facing degrees
      • Unit - Order (Last created unit) to Harvest Nearby Lumber
      • Hashtable - Save Handle Of(Last created unit) as 0 of (Key (Triggering unit)) in Hash
      • Custom script: call RemoveLocation (udg_Point1)
and kill it on death of the building:
  • Death
    • Events
      • Unit - A unit Dies
    • Conditions
      • (Unit-type of (Triggering unit)) Equal to Scout Tower
    • Actions
      • Set LoadU = (Load 0 of (Key (Triggering unit)) in Hash)
      • Unit - Kill LoadU
      • Unit - Remove LoadU from the game
      • Hashtable - Clear all child hashtables of child (Key (Triggering unit)) in Hash
The Harvest Lumber ability should have a radius of 99999. If you want the building to harvest from nearby trees, you should lower the value, everything depends on what you want to accomplish.

Test map:
 

Attachments

  • Harvest.w3x
    18.6 KB · Views: 58
Level 8
Joined
Mar 23, 2007
Messages
302
Here is the Code for harvesting destructables in a rectangle every x seconds by y amount.

Its Req:
- Know how to import a spell/ability
- Know how to work with Hashtables​

JASS:
function Trig_HarvestLumber_Conditions takes nothing returns boolean
return GetUnitAbilityLevel(GetTriggerUnit(),'A000') >= 1
endfunction

function isTree takes nothing returns boolean
local destructable tree = GetFilterDestructable()
local integer ID = GetDestructableTypeId(tree)
return (ID == 'LTlt' or ID == 'BTtw' or ID == 'JTtw' or ID == 'VTlt' or ID == 'DTsh') and not (GetDestructableLife(tree) <= 0)
// LTlt means lordearon sommer tree, you can check the id's with ctrl+D in the object editor, use destructables only!
endfunction


function Trig_HarvestLumber_Timer takes nothing returns nothing
    local timer T = GetExpiredTimer()
    local unit mill = LoadUnitHandle(udg_Hash, GetHandleId(T), StringHash("millOFtimer"))
    local player P = GetOwningPlayer(mill)
    local real range = 300 // 300 in top , bottom, left and right
    local real damage = 10
    local integer gain = 10
    local real x = GetUnitX(mill)
    local real y = GetUnitY(mill)
    local destructable d = null
    local rect r = Rect(x-range,y+range,x+range,y-range)
    //local string eff = "Objects\\Spawnmodels\\Naga\\NagaDeath\\NagaDeath.mdl"
    set d = RandomDestructableInRectBJ(r, Filter(function isTree))

    
    if GetUnitState(mill, UNIT_STATE_LIFE) <=0 then
        call DestroyTimer(T)
        call FlushChildHashtable(udg_Hash,GetHandleId(T))
        call FlushChildHashtable(udg_Hash,GetHandleId(mill))

    elseif d != null then
        call SetDestructableLife(d, GetDestructableLife(d)-damage)
        call SetPlayerState(P,PLAYER_STATE_RESOURCE_LUMBER,GetPlayerState(P,PLAYER_STATE_RESOURCE_LUMBER)+gain)
    endif
    
set T = null
set mill = null
call RemoveRect(r)
set r = null
set d = null
set P = null
endfunction

function Trig_HarvestLumber_Actions takes nothing returns nothing
    local unit mill = GetTriggerUnit()
    local timer T = CreateTimer()
    local real interval = 0.1

call SaveUnitHandle(udg_Hash, GetHandleId(T), StringHash("millOFtimer"), mill)
call TimerStart(T, interval, true, function Trig_HarvestLumber_Timer)

set mill = null
set T = null
endfunction

//===========================================================================
function InitTrig_HarvestLumber takes nothing returns nothing
    set gg_trg_HarvestLumber = CreateTrigger(  )
    call TriggerRegisterEnterRectSimple( gg_trg_HarvestLumber, GetEntireMapRect() )
    call TriggerAddCondition( gg_trg_HarvestLumber, Condition( function Trig_HarvestLumber_Conditions ) )
    call TriggerAddAction( gg_trg_HarvestLumber, function Trig_HarvestLumber_Actions )
endfunction

Important notes:

When a unit comes to the map newly and it has a given ability (
JASS:
'A000'
) it will gain a timer.
This timer runs every /interval/ seconds (see variables).
Then it deals the /damage/ (see variables) to the tree and gives you the /gain/ (see variables)


The /range/ is 300, in top, bottom, left and right of the unit.
The isTree function checks if a destructable is a given type, so
JASS:
'LTlt'
means Sommer Lordaeron Tree. You can paste in here any kind of type or thing, as long as it is a destructable. You can even add any amount of types.

I could not get rid of the BJ, so this is a point of leak.
 
Last edited:
Level 8
Joined
Mar 23, 2007
Messages
302
Do u refer just to the BJ? As i stated this already!

About Hashtable, when one knows how to work with them (what is a req, as said above) then it should be not even worth to be mentioned.

The tree-checking is my first use of filter-methodes, better ways of doing it are welcome, also i would like to learn how to improve my code ( WHILE STAYING IN PLAIN JASS! ) .

When you say messy code, i think u mean that it is not (so called) "properly" intendet, well thats how i do it.
 
Status
Not open for further replies.
Top