Custom Gold mining help

Status
Not open for further replies.
Level 8
Joined
Jul 29, 2010
Messages
319
Is it possible to make workers mine a gold mine sorta the same way they cut at trees, i'm using a custom model to replace the gold mine which is a crystal on the ground and i'd rather the workers swing their pickaxe at it similar to how they attack, instead of walking up to it and disappearing for a sec then reappearing and running off, thanks in advance :D :)
 
Level 13
Joined
May 10, 2009
Messages
868
Yes, it's possible. I remember that I've helped someone to make harvest not destroy trees. However it's written in jass, but it uses a few GUI variables in order to help the other user to modify a few things easily. If that's not a problem for you, I'll post the code here so you can modify the function that heals the trees and change it to give gold to a player instead.
 
Last edited:
Level 8
Joined
Jul 29, 2010
Messages
319
Yes, it's possible. I remember that I've helped someone to make harvest not destroy trees. However it's written in jass, but it uses a few GUI variables in order to help the other user to modify a few things easily. If that's not a problem for you, I'll post the code here so you can modify the function that heals the trees and change it to give gold to a player instead.
Yes please, that'd be amazing :D
 
Level 13
Joined
May 10, 2009
Messages
868
All right, here it is:

This snippet uses a hashtable. There's also a small little problem that it only works for only one unit type for now. You can remove that limitation by deleting this piece of code
JASS:
GetUnitTypeId(u) == udg_Harvest_Worker
from this function:
JASS:
function HarvestOrder takes nothing returns nothing
    local destructable d = GetOrderTargetDestructable()
    local unit u = GetTriggerUnit()
 
    if GetUnitTypeId(u) == udg_Harvest_Worker and udg_Harvest_TreeChecker != u then
        if IssueTargetOrderById(udg_Harvest_TreeChecker, 852018, d) and (GetUnitCurrentOrder(u) == 851971 or GetUnitCurrentOrder(u) == 852018 or GetUnitCurrentOrder(u) == 851970) then
            if FirstOfGroup(udg_Harvest_Group[0]) == null then
                call TimerStart(LoadTimerHandle(udg_Harvest_Hashtable, 0, 0), udg_Harvest_Interval, true, function HarvestLoop)
            endif
            call GroupAddUnit(udg_Harvest_Group[0], u)
            call SaveDestructableHandle(udg_Harvest_Hashtable, GetHandleId(u), 0, d)
        else
            call GroupRemoveUnit(udg_Harvest_Group[0], u)
            call FlushChildHashtable(udg_Harvest_Hashtable, GetHandleId(u))
        endif
        call IssueImmediateOrderById(udg_Harvest_TreeChecker, 851972)
    endif
 
    set d = null
    set u = null
endfunction

Of course, don't forget to delete that variable that will probably be unused:
  • Set Harvest_Worker = Peasant
GUI - Variable Creator
  • Needed variables
    • Events
    • Conditions
    • Actions
      • -------- This is just here in order to help you create these variables without needing to create them manually. --------
      • -------- But first you need to enable copying/pasting variables automatically in your editor preferences. --------
      • -------- File -> Preferences... -> General (tab) -> Mark: Automatically create unknown variables while pasting trigger data. --------
      • Set Harvest_Group[0] = (Last created unit group)
      • Set Harvest_Hashtable = (Last created hashtable)
      • Set Harvest_TreeChecker = No unit
      • Set Harvest_Distance = 0.00
      • Set Harvest_Heal = 0.00
      • Set Harvest_Interval = 0.00
      • Set Harvest_Worker = No unit-type
GUI - Initialization
  • Your Map Initialization
    • Events
      • Map initialization
    • Conditions
    • Actions
      • -------- You don't necessarily need this trigger. For example, if you already have a trigger that runs when the game starts, then copy the variables below --------
      • -------- ...paste in your initialization, and delete this trigger. --------
      • -------- - --------
      • -------- - --------
      • -------- Configure the following variables according to your necessity --------
      • -------- - --------
      • -------- Select here your worker that won't destroy trees. --------
      • Set Harvest_Worker = Peasant
      • -------- - --------
      • -------- How much time does it take to increase a tree's hit points. (In seconds) --------
      • Set Harvest_Interval = 1.00
      • -------- - --------
      • -------- The amount of HP a tree will receive every X seconds. (Default: 1 second) --------
      • Set Harvest_Heal = 2.00
      • -------- - --------
      • -------- How close to a tree a worker must be in order to heal such tree. --------
      • Set Harvest_Distance = 150.00
      • -------- - --------
      • -------- If, at any given point, you need to change which worker will use this script, then you need to run these actions below, to make sure that --------
      • -------- you remove all the previous workers from the group. Otherwise, they will be able to heal trees too. --------
      • Unit Group - Pick every unit in Harvest_Group[0] and do (Actions)
        • Loop - Actions
          • Unit Group - Remove (Picked unit) from Harvest_Group[0]
          • Custom script: call FlushChildHashtable(udg_Harvest_Hashtable, GetHandleId(GetEnumUnit()))
The code
JASS:
function HarvestLoop takes nothing returns nothing
    local unit fog = null
    local destructable d = null
 
    if FirstOfGroup(udg_Harvest_Group[0]) == null then
        call PauseTimer(LoadTimerHandle(udg_Harvest_Hashtable, 0, 0))
        return
    endif
 
    loop
        set fog = FirstOfGroup(udg_Harvest_Group[0])
        exitwhen fog == null
        call GroupRemoveUnit(udg_Harvest_Group[0], fog)
        if GetWidgetLife(fog) > .405 then
            call GroupAddUnit(udg_Harvest_Group[1], fog)
     
            set d = LoadDestructableHandle(udg_Harvest_Hashtable, GetHandleId(fog), 0)
            if d != null and GetWidgetLife(d) > .405 and IsUnitInRangeXY(fog, GetDestructableX(d), GetDestructableY(d), udg_Harvest_Distance) then
                call SetWidgetLife(d, GetWidgetLife(d) + udg_Harvest_Heal)
            endif
            set d = null
        else
            call GroupRemoveUnit(udg_Harvest_Group[0], fog)
            call FlushChildHashtable(udg_Harvest_Hashtable, GetHandleId(fog))
        endif
    endloop
 
    // Swap
 
    set udg_Harvest_Group[2] = udg_Harvest_Group[0]
    set udg_Harvest_Group[0] = udg_Harvest_Group[1]
    set udg_Harvest_Group[1] = udg_Harvest_Group[2]
endfunction

function HarvestOrder takes nothing returns nothing
    local destructable d = GetOrderTargetDestructable()
    local unit u = GetTriggerUnit()
 
    if GetUnitTypeId(u) == udg_Harvest_Worker and udg_Harvest_TreeChecker != u then
        if IssueTargetOrderById(udg_Harvest_TreeChecker, 852018, d) and (GetUnitCurrentOrder(u) == 851971 or GetUnitCurrentOrder(u) == 852018 or GetUnitCurrentOrder(u) == 851970) then
            if FirstOfGroup(udg_Harvest_Group[0]) == null then
                call TimerStart(LoadTimerHandle(udg_Harvest_Hashtable, 0, 0), udg_Harvest_Interval, true, function HarvestLoop)
            endif
            call GroupAddUnit(udg_Harvest_Group[0], u)
            call SaveDestructableHandle(udg_Harvest_Hashtable, GetHandleId(u), 0, d)
        else
            call GroupRemoveUnit(udg_Harvest_Group[0], u)
            call FlushChildHashtable(udg_Harvest_Hashtable, GetHandleId(u))
        endif
        call IssueImmediateOrderById(udg_Harvest_TreeChecker, 851972)
    endif
 
    set d = null
    set u = null
endfunction

//===========================================================================
function InitTrig_Harvest takes nothing returns nothing
    local trigger harvest = CreateTrigger()
 
    //=============CONFIG==============//
 
    //set udg_Harvest_Worker      = 'hpea'        // The raw code of your worker.
    //set udg_Harvest_Interval    = 1.            // This is the interval that it takes to heal a tree.
    //set udg_Harvest_Heal        = 2.            // How much life should a tree receive for every Harvest_Interval. (Default: 2 HP/sec)
    //set udg_Harvest_Distance    = 150.          // The maximum distance your worker must be from the tree in order to start healing it.
 
    //=============CONFIG==============//
 
    // Harvest order
    call TriggerRegisterAnyUnitEventBJ(harvest, EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER)
    call TriggerRegisterAnyUnitEventBJ(harvest, EVENT_PLAYER_UNIT_ISSUED_POINT_ORDER)
    call TriggerRegisterAnyUnitEventBJ(harvest, EVENT_PLAYER_UNIT_ISSUED_ORDER)
    call TriggerAddAction(harvest, function HarvestOrder)
 
    // Tree Checker
    set udg_Harvest_TreeChecker = CreateUnit(Player(15), 'hpea', 0, 0, 270)
    call UnitAddAbility(udg_Harvest_TreeChecker, 'Aloc')
    call UnitAddAbility(udg_Harvest_TreeChecker, 'Ahar')
    call SetUnitUseFood(udg_Harvest_TreeChecker, false)
    call ShowUnit(udg_Harvest_TreeChecker, false)
 
    set udg_Harvest_Hashtable = InitHashtable()
    call SaveTimerHandle(udg_Harvest_Hashtable, 0, 0, CreateTimer())
 
    set harvest = null
endfunction

EDIT: Order IDs used
Code:
851971 - Smart
852018 - Harvest
851970 - Rally
851972 - Stop


EDIT: Why not just attach the map to my own post...
 

Attachments

  • harvest.w3x
    22.5 KB · Views: 55
Last edited:
Level 13
Joined
May 10, 2009
Messages
868
I actually like that idea (a lot) and wanted to make some improvements to it. These are the changes that I've made:

- Source of gold is now a unit instead of a "tree". (It's better to avoid any problems related to other abilities that gathers lumber or gold from mines)
- Added floating text (text tag) in order to show the amount of gold collected. It's displayed when a worker collects X amount of gold.
- Your worker will look for a new source of gold if the current one collapses.
- Workers now play their animations correctly.
- Added a few more variables in order to configure the "spell" better.

However, there's no "return gold phase" yet. I plan on adding that feature later.

This is how it looks like now:
Constants
JASS:
    set udg_Harvest_Ability          = 'A000'               // Ability that makes the unit cappable of collecting gold
    set udg_Harvest_Gold_Amount      = 1                    // Amount of gold that a unit gathers every attack
    set udg_Harvest_Gold_Capacity    = 5                    // Maximum amount of gold that a unit can collect
    set udg_Harvest_Gold_Source      = 'h000'               // Raw ID of your gold source
    set udg_Harvest_LookForMore_Area = 900.00               // Size of the area when a worker looks for a new source of gold.
    set udg_Harvest_Distance         = 130.00               // The maximum distance which a worker must be in order to start collecting gold.
    set udg_Harvest_Collect_Interval = 0.90                 // Amount of time that your worker takes in order to collect gold.
    set udg_Harvest_EarlierAnim      = 0.50                 // Time that your worker needs to wait to do the attack animation
    set udg_Harvest_PeriodicTime     = 0.10                 // Time out of the timer
    set udg_Harvest_Anim_Crystal     = "stand hit"          // Animation that should play when your gold source gets hit by a worker
    set udg_Harvest_Anim_WorkerAttck = "attack gold"        // Your worker's attack animation
    set udg_Harvest_Anim_WorkerStand = "stand ready gold"   // This happens after your worker has hit a gold source
    set udg_Harvest_Text_Color       = "|cffffcc00+"        // The color of your floating text (text tag) and also the PLUS (+) sign
Screenshot
6t8bzsA.jpg
Credits to Uncle Fester for making that custom model (Orange Crystal)

As soon as I finish modifying this, I'll post it here if you're interested in it.


EDIT: Do you guys know if there's a way to hide temporarily a worker's idle icon? (The one that appears at the bottom left corner - F8 Hotkey).

EDIT 2: It looks like the functions below work perfectly.
JASS:
call UnitRemoveType(u, UNIT_TYPE_PEON)
call UnitAddType(u, UNIT_TYPE_PEON)
However, I've read here that they wouldn't receive their idle icon back if I did that.

EDIT 3: All right, I've made the changes, and it's working naturally.
 
Last edited:
Status
Not open for further replies.
Top