Dirac
22710180
- Reaction score
- 147
JASS:
library DamageId /* v1.1.0
*/uses/*
*/ Damage /* thehelper.net/forums/showthread.php/168144-Damage-Struct
A useful tool for damage identification
**********************************************************************/
globals
//*********************************************************************
// These are the public constants that determine the damage's id.
// Custom id represents damage done through coding.
constant key DAMAGE_ID_CUSTOM
constant key DAMAGE_ID_MELEE
constant key DAMAGE_ID_RANGED
constant key DAMAGE_ID_SPELLS
endglobals
/**********************************************************************
*
* function RegisterDamageById takes code func, integer damageId returns triggercondition
* - Registers the function to the damage id.
* function UnregisterDamageById takes triggercondition whichCondition, integer damageId returns nothing
* - Unregisters the function from the damage id.
*
***********************************************************************
*
* readonly integer id
* - Adds this variable to the Damage struct, returns the
* - current damage id.
*
**********************************************************************
*
* HOW TO IMPLEMENT:
* - Import this code into an empty trigger in your map,
* - requires NewGen installed. Save the map, reopen it, and
* - then delete the "!" exclamation mark from the textmacro
* - below.
*/
//! runtextmacro DAMAGE_ID_SETUP()
/*********************************************************************
This needs to be a textmacro in order to keep the encapsulation correct
and private within the Damage library. Consider this textmacro private.
**********************************************************************/
//! textmacro DAMAGE_ID_EX1
globals
private trigger array DamageIdFire
private boolean DamageIdHook = false
private integer DamageId = 0
endglobals
function RegisterDamageById takes code func, integer damageId returns triggercondition
if DamageIdFire[damageId]==null then
set DamageIdFire[damageId]=CreateTrigger()
endif
return TriggerAddCondition(DamageIdFire[damageId],Filter(func))
endfunction
function UnregisterDamageById takes triggercondition whichCondition, integer damageId returns nothing
call TriggerRemoveCondition(DamageIdFire[damageId],whichCondition)
endfunction
private function CustomDamageDetection takes unit whichUnit, widget target, real amount, boolean attack, boolean ranged, attacktype attackType, damagetype damageType, weapontype weaponType returns boolean
set DamageIdHook=true
return false
endfunction
hook UnitDamageTarget CustomDamageDetection
//! endtextmacro
//! textmacro DAMAGE_ID_EX2
static method operator id takes nothing returns integer
return DamageId
endmethod
//! endtextmacro
//! textmacro DAMAGE_ID_EX3
local integer prevAdvType = DamageId
//! endtextmacro
//! textmacro DAMAGE_ID_EX4
if not(DamageIdHook) then
if GetUnitAbilityLevel(target,'ADab')>0 then
if IsUnitType(source,UNIT_TYPE_RANGED_ATTACKER) then
set DamageId = DAMAGE_ID_RANGED
else
set DamageId = DAMAGE_ID_MELEE
endif
else
set DamageId = DAMAGE_ID_SPELLS
endif
else
set DamageId = DAMAGE_ID_CUSTOM
set DamageIdHook = false
endif
call UnitRemoveAbility(target,'ADab')
call TriggerEvaluate(DamageIdFire[DamageId])
//! endtextmacro
//! textmacro DAMAGE_ID_EX5
set DamageId = prevAdvType
//! endtextmacro
//! textmacro DAMAGE_ID_EX6
call UnitAddAbility(indexUnit,'ADfb')
//! endtextmacro
//! textmacro DAMAGE_ID_SETUP
//! externalblock extension=lua ObjectMerger $FILENAME$
//! i setobjecttype("buffs")
//! i createobject("BHab","ADab")
//! i makechange(current,"ftat","")
//! i makechange(current,"fnam","DAMAGE_DETECTION_BUFF")
//! i setobjecttype("abilities")
//! i createobject("AIob","ADfb")
//! i makechange(current,"amat","")
//! i makechange(current,"asat","")
//! i makechange(current,"atat","")
//! i makechange(current,"Idam",1,"0")
//! i makechange(current,"Iob5",1,"0")
//! i makechange(current,"abuf",1,"ADab")
//! i makechange(current,"anam","DAMAGE_DETECTION_ABILITY")
//! endexternalblock
//! endtextmacro
endlibrary