- Joined
- Mar 19, 2008
- Messages
- 3,141
Some time ago I was in need of script that could tell me the current status of given building. Unfortunately I haven't found nothing interesting untill I went to wc3c.net and saw Troll-Brain's ConstructionStatus. I decided to improve that one in case it was a bit old and didn't feature all the things I've needed.
This simple library is able to detect if unit is currently being constructed, upgrading or if it's training troops. I've also added function which enables user to retrieve worker who started the construction process. Two additional function are here to check if building is 'free', meaning it's status is somewhat empty and another function, just to tell if structure is 'completed' - neither under construction nor is uder upgrade process.
JASS:
/*****************************************************************************
*
* BuildUtils v3.1.0.0
* by Bannar aka Spinnaker
*
* Allows one to get information about current building status.
*
******************************************************************************
*
* Requirements:
*
* UnitDex by TriggerHappy
* hiveworkshop.com/forums/submissions-414/system-unitdex-unit-indexer-248209/
*
* Optionaly uses UnitEvent by Nestharus which helps getting rid of rare build-process issue
* hiveworkshop.com/forums/jass-functions-413/extension-unit-event-172365/
*
* RegisterPlayerUnitEvent library - supports:
*
* | RegisterPlayerUnitEvent by Bannar
* | hiveworkshop.com/forums/submissions-414/snippet-registerevent-pack-250266/
* |
* | RegisterPlayerUnitEvent by Magtheridon96
* | hiveworkshop.com/forums/jass-resources-412/snippet-registerplayerunitevent-203338/
*
*
* Credits to Troll-Brain for original project idea
* Special thanks to Magtheridon96
*
******************************************************************************
*
* Functions:
* function IsUnitIdle takes integer index returns boolean
* - Checks if given structure isn't currently performing any action
* function IsUnitFinished takes integer index returns boolean
* - Checks if unit nethier is upgrading nor under construction
* function IsUnitBeingBuilt takes integer var returns boolean
* - Checks if structure is being built
* function IsUnitUpgrading takes integer var returns boolean
* - Checks if structure currently being upgraded
* function IsUnitTraining takes integer var returns boolean
* - Checks if building currently training unit
* function IsUnitRevivng takes integer var returns boolean
* - Checks if structure currently resurrects a hero
* function IsUnitResearching takes integer var returns boolean
* - Checks if structure currently researching an upgrade
*
*****************************************************************************/
library BuildUtils requires UnitDex optional UnitEvent optional RegisterPlayerUnitEvent
native UnitAlive takes unit id returns boolean
//! textmacro BUILD_UTILS_MACRO takes NAME, TYPE, KEY, NODE
function IsBuilding$NAME$ takes $TYPE$ var returns boolean
return BuildUtils$KEY$.$NODE$
endfunction
//! endtextmacro
function IsBuildingFinished takes integer var returns boolean
return UnitAlive(GetUnitById(var)) and not (BuildUtils(var).cU or BuildUtils(var).uU)
endfunction
//! runtextmacro BUILD_UTILS_MACRO("BeingBuilt","integer","(var)","cU")
//! runtextmacro BUILD_UTILS_MACRO("Upgrading","integer","(var)","uU")
//! runtextmacro BUILD_UTILS_MACRO("Training","integer","(var)","tU")
//! runtextmacro BUILD_UTILS_MACRO("Revivng","integer","(var)","oU")
//! runtextmacro BUILD_UTILS_MACRO("Researching","integer","(var)","rU")
function IsBuildingIdle takes integer v returns boolean
return UnitAlive(GetUnitById(v)) and not (BuildUtils(v).cU or BuildUtils(v).uU or BuildUtils(v).tU or BuildUtils(v).oU or BuildUtils(v).rU)
endfunction
module BuildUtilsModule
//! textmacro BUILD_UTILS_MACRO_EV takes TYPE, NODE, VALUE
static method onEvent$TYPE$ takes nothing returns nothing
set thistype[GetUnitId(GetTriggerUnit())].$NODE$ = $VALUE$
endmethod
//! endtextmacro
//! runtextmacro BUILD_UTILS_MACRO_EV("CS","cU","true")
//! runtextmacro BUILD_UTILS_MACRO_EV("CE","cU","false")
//! runtextmacro BUILD_UTILS_MACRO_EV("US","uU","true")
//! runtextmacro BUILD_UTILS_MACRO_EV("UE","uU","false")
//! runtextmacro BUILD_UTILS_MACRO_EV("TS","tU","true")
//! runtextmacro BUILD_UTILS_MACRO_EV("TE","tU","false")
//! runtextmacro BUILD_UTILS_MACRO_EV("OS","oU","true")
//! runtextmacro BUILD_UTILS_MACRO_EV("OE","oU","false")
//! runtextmacro BUILD_UTILS_MACRO_EV("RS","rU","true")
//! runtextmacro BUILD_UTILS_MACRO_EV("RE","rU","false")
endmodule
private function RegisterAnyUnitEvent takes playerunitevent e, code c returns nothing
static if LIBRARY_RegisterPlayerUnitEvent then
static if LIBRARY_RegisterNativeEvent then
call RegisterAnyPlayerUnitEvent(e, c)
else
call RegisterPlayerUnitEvent(e, c)
endif
else
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, e)
call TriggerAddCondition(t, Condition(c))
set t = null
endif
endfunction
private module RegisterEvents
private static method onInit takes nothing returns nothing
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_CONSTRUCT_START, function thistype.onEventCS)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_CONSTRUCT_FINISH, function thistype.onEventCE)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_UPGRADE_START, function thistype.onEventUS)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_UPGRADE_FINISH, function thistype.onEventUE)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_UPGRADE_CANCEL, function thistype.onEventUE)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_TRAIN_START, function thistype.onEventTS)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_TRAIN_FINISH, function thistype.onEventTE)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_TRAIN_CANCEL, function thistype.onEventTE)
call RegisterAnyUnitEvent(EVENT_PLAYER_HERO_REVIVE_START, function thistype.onEventOS)
call RegisterAnyUnitEvent(EVENT_PLAYER_HERO_REVIVE_CANCEL, function thistype.onEventOE)
call RegisterAnyUnitEvent(EVENT_PLAYER_HERO_REVIVE_FINISH, function thistype.onEventOE)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_RESEARCH_START, function thistype.onEventRS)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_RESEARCH_CANCEL, function thistype.onEventRE)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_RESEARCH_FINISH, function thistype.onEventRE)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_DEATH, function thistype.onDeath)
call RegisterUnitIndexEvent(Condition(function thistype.deindex), EVENT_UNIT_DEINDEX)
endmethod
endmodule
struct BuildUtils extends array
readonly boolean cU
readonly boolean uU
readonly boolean tU
readonly boolean oU
readonly boolean rU
private static method reset takes integer index returns nothing
set thistype(index).cU = false
set thistype(index).uU = false
set thistype(index).tU = false
set thistype(index).oU = false
set thistype(index).rU = false
endmethod
private static method deindex takes nothing returns boolean
if IsUnitType(GetIndexedUnit(), UNIT_TYPE_STRUCTURE) then
call thistype.reset(GetIndexedUnitId())
endif
return false
endmethod
implement BuildUtilsModule
static if LIBRARY_UnitEvent then
private method reincarnate takes nothing returns nothing
if IsUnitType(unit, UNIT_TYPE_STRUCTURE) then
call thistype.reset(this)
endif
endmethod
endif
static method onDeath takes nothing returns nothing
if IsUnitType(GetTriggerUnit(), UNIT_TYPE_STRUCTURE) then
call thistype.reset(GetUnitId(GetTriggerUnit()))
endif
endmethod
implement optional UnitEventStruct
implement RegisterEvents
endstruct
endlibrary
Last edited: