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.
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