Snippet ConstructionStatus

Troll-Brain

You can change this now in User CP.
Reaction score
85
Requires vJass and GroupUtils.

This is just a simple library to know if an unit is being built, upgraded or finished, it will return false if the unit is dead.

JASS:
library ConstructionStatus initializer init uses GroupUtils

globals
    private constant real TIME_OUT = 30.0 // It is the periodic time (seconds) which the group will be refreshed (ghost units are removed)
endglobals

//==============================================================================
//  ConstructionStatus -- by Troll-Brain -- v 1.2
//==============================================================================
//
//  PURPOUSE:
//       * To know if an unit is finished, or being built/upgraded
//       * Note that it doesn't care about trained units, but ofc a trained unit will be considered as finished
//
//  HOW TO USE:
//       * function IsUnitFinished takes unit u returns boolean
//       * function IsUnitBeingBuilt takes unit u returns boolean
//       * function IsUnitBeingUpgraded takes unit u returns boolean
//
//       * Note that these 3 functions will return false if the unit is dead or not valid (for example == null)
//
//  PROS: 
//       * Easy to use, can be done in a single custom script for gui users
//         
//  CONS:
//       * It will fail if the unit has an usable revive ability if the unit die during the upgrade or construction
//       * I could handle it thought, let me know if you have the use of it, but then you have to know that it will require more stuff
//
//  DETAILS:
//       * I simply catch construct and upgrade events
//
//  THANKS TO:
//       * Rising_Dusk : to tell me that i've forgotten the upgraded units
//       * grim001 : for more sexy functions/library names 

//
//  HOW TO IMPORT:
//       * Just create a trigger named ConstructionStatus
//       * convert it to text and replace the whole trigger text with this one
//       * Do the same for the library GroupUtils if you don't already use it
//
//==============================================================================

globals
    private group BeingBuiltUnits
    private group BeingUpgradedUnits
endglobals

function IsUnitFinished takes unit u returns boolean
    return GetUnitTypeId(u) != 0 and not IsUnitType(u,UNIT_TYPE_DEAD) and not IsUnitInGroup(u,BeingBuiltUnits) and not IsUnitInGroup(u,BeingUpgradedUnits)
endfunction

function IsUnitBeingBuilt takes unit u returns boolean
    return not IsUnitType(u,UNIT_TYPE_DEAD) and IsUnitInGroup(u,BeingBuiltUnits)
endfunction

function IsUnitBeingUpgraded takes unit u returns boolean
    return not IsUnitType(u,UNIT_TYPE_DEAD) and IsUnitInGroup(u,BeingUpgradedUnits)
endfunction

private function HandleUnits takes nothing returns boolean
    
    if GetTriggerEventId() == EVENT_PLAYER_UNIT_CONSTRUCT_START then
        call GroupAddUnit(BeingBuiltUnits,GetConstructingStructure())
        
    elseif GetTriggerEventId() == EVENT_PLAYER_UNIT_CONSTRUCT_FINISH then
        call GroupRemoveUnit(BeingBuiltUnits,GetConstructedStructure())
        
    elseif GetTriggerEventId() == EVENT_PLAYER_UNIT_UPGRADE_START then
        call GroupAddUnit(BeingUpgradedUnits,(GetTriggerUnit()))
        
    else
        call GroupRemoveUnit(BeingUpgradedUnits,GetTriggerUnit())
    endif

    return false
endfunction

private function RefreshGroup takes nothing returns nothing
    call GroupRefresh(BeingBuiltUnits)
    call GroupRefresh(BeingUpgradedUnits)
endfunction

private function init takes nothing returns nothing
    local trigger trig = CreateTrigger()
    local timer tim = CreateTimer()

    set BeingBuiltUnits = NewGroup()
    set BeingUpgradedUnits = NewGroup()
    call TimerStart(tim,TIME_OUT,true,function RefreshGroup)
    call TriggerRegisterAnyUnitEventBJ(trig,EVENT_PLAYER_UNIT_CONSTRUCT_START)
    call TriggerRegisterAnyUnitEventBJ(trig,EVENT_PLAYER_UNIT_CONSTRUCT_FINISH)
    call TriggerRegisterAnyUnitEventBJ(trig,EVENT_PLAYER_UNIT_UPGRADE_START)
    call TriggerRegisterAnyUnitEventBJ(trig,EVENT_PLAYER_UNIT_UPGRADE_FINISH)
    call TriggerRegisterAnyUnitEventBJ(trig,EVENT_PLAYER_UNIT_UPGRADE_CANCEL)
    call TriggerAddCondition(trig,Condition(function HandleUnits))
    
    // unneeded but i tend to null all handles, even if it is never destroyed 
    set trig = null
    set tim = null
endfunction

//==============================================================================
//  CHANGELOG
//==============================================================================
//
//  v 1.2 :
//       * IsUnitFinished return false instead of true when the unit argument is not valid.
//
//  v 1.1 :
//       * It handles upgrading units as well, so IsUnitBeingUpgraded is added
//
//  v 1.0 :
//       * Since the initial release didn't handle morphed units and units created by trigger,
//       * i've fixed that
//
//  v 0.1 :
//       * Initial release

endlibrary
 

Attachments

  • ConstructionStatus v1.2.w3x
    26.7 KB · Views: 296

Troll-Brain

You can change this now in User CP.
Reaction score
85
Why not, but is not a big deal to use the functions inside your own boolexpr, because you may need to do other checks, if unit is invulnerable, an enemy of the player X, and so one.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Last bump ever.
If you want approve it but something is wrong with this for you, tell me what.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Well i guess i'm a liar because it's a kind of bump, but meh nevermind, i only try to get the best answer to this problem :

I've found a bug.
When an unit die during it was constructed or upgraded and the unit has a revive ability, then when she is coming to life it takes the finished form (constructed/upgraded).
And the code doesn't handle that, since events doesn't fire in these special cases.

But now i'm wondering if someone would ever need a such thing, so i suggest to add theses kind of abilities during the game after the end constructing event, and disable them during the upgrading event. (ofc if the unit can be constructed/upgraded)
Let me know if i think wrong.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Wait, wait wait.
Maybe Jesus4Lyf is right. In the original version GroupRefresh was needed because i used only finish event, and added units only when it was finished, but then it didn't handle morphed and created units with CreateUnit...
But now maybe the event finish fire when an unit under construction dead or is removed, i will test it tomorrow.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Ok tested and the CANCEL events don't fire when an unit is killed/removed, so GroupRefresh is still needed.
For the worst and the best GroupRefresh is not anymore inside a single library but included in GroupUtils, so for completeness reason i use NewGroup(), instead of CreateGroup().

Back to the problem :

Troll-Brain said:
I've found a bug.
When an unit die during it was constructed or upgraded and the unit has a revive ability, then when she is coming to life it takes the finished form (constructed/upgraded).
And the code doesn't handle that, since events doesn't fire in these special cases.

But now i'm wondering if someone would ever need a such thing, so i suggest to add theses kind of abilities during the game after the end constructing event, and disable them during the upgrading event. (ofc if the unit can be constructed/upgraded)
Let me know if i think wrong.

What do you think about that ?
Should i track the revive event and handle it, or simply say in the documentation to avoid theses cases ?
 

asipo

New Member
Reaction score
15
Forgive me if because im new with JASS
I download the map and run it and it work just fine

However, when I save the map (ctrl+s)
It said there is an error on it which is on trigger ConstructionStatus, GroupUtils and display status
So does when I'm try to enable the trigger

So... how to solve this?
Because I want to use those trigger and put on my other map
 

Romek

Super Moderator
Reaction score
963
Approved. :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top