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: 298

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
964
Approved. :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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