- Joined
- May 16, 2012
- Messages
- 636
Hey guys, i just created a small function to control the attack speed bonus of a unit via code instead of abilities.
You need to create a hashtable at map initialization called UnitHash and use Bribe's Unit Indexer or Unit Event. Why? because its amazing and i'm using the on index event to set up a few thinks.
So in this first piece, i just save every indexed unit base attack cooldown and their current bonus (0%)
why is it necessary? well to be able to remove bonusses i need to know the unit base attack time (bat) and i also need to know if its current bonus is not lower than -1 (-100% or no attacks).
now the snippet:
so to use these functions you only need to do:
that will give the "unit" a bonus of 300% attack speed. the max bonus is capped on a attack cooldown of 0.022 or 45.45 attacks per seconds and -99.99% for the lower bound.
Its not neccessary to do:
as its already taken in consideration.
this is cool because by using it you not chained to the 400% max bonus or -80%.]
Note that you can do:
this you remove 300% bonus attack speed ffrom the unit if its current bonus is greater than or equal to 3 and if its lower will stop at the cap (-99.99%) and save -1 as the current bonus, so you are safe to keep adding negative values because its never going to be lower than -1.
Hope you like it.
You need to create a hashtable at map initialization called UnitHash and use Bribe's Unit Indexer or Unit Event. Why? because its amazing and i'm using the on index event to set up a few thinks.
So in this first piece, i just save every indexed unit base attack cooldown and their current bonus (0%)
JASS:
scope Snippet initializer Init
private function Actions takes nothing returns nothing
call SaveReal(udg_UnitHash, GetHandleId(udg_UDexUnits[udg_UDex]), 1, BlzGetUnitAttackCooldown(udg_UDexUnits[udg_UDex], 0)) // Original Base Attack CD
call SaveReal(udg_UnitHash, GetHandleId(udg_UDexUnits[udg_UDex]), 2, 0) // Current Attack Speed Bonus
endfunction
//===========================================================================
private function Init takes nothing returns nothing
set gg_trg_Snippet = CreateTrigger()
call TriggerRegisterVariableEvent(gg_trg_Snippet, "udg_UnitIndexEvent", EQUAL, 1.00)
call TriggerAddAction(gg_trg_Snippet, function Actions)
endfunction
endscope
why is it necessary? well to be able to remove bonusses i need to know the unit base attack time (bat) and i also need to know if its current bonus is not lower than -1 (-100% or no attacks).
now the snippet:
JASS:
function SetUnitAttackSpeedBonus takes unit whichUnit, real bonus returns nothing
local real bat = LoadReal(udg_UnitHash, GetHandleId(whichUnit), 1)
local real currentBonus = LoadReal(udg_UnitHash, GetHandleId(whichUnit), 2)
local real newBonus = currentBonus + bonus
local real newAS
set newAS = bat/(1 + newBonus)
if bonus < 0 then
if newBonus > -1 then
call SaveReal(udg_UnitHash, GetHandleId(whichUnit), 2, newBonus)
call BlzSetUnitAttackCooldown(whichUnit, newAS, 0)
else
set newAS = bat/(1 - 0.9999)//capped at -99.99% AS
call SaveReal(udg_UnitHash, GetHandleId(whichUnit), 2, -1)
call BlzSetUnitAttackCooldown(whichUnit, newAS, 0)
endif
else
if newAS >= 0.022 then
call SaveReal(udg_UnitHash, GetHandleId(whichUnit), 2, newBonus)
call BlzSetUnitAttackCooldown(whichUnit, newAS, 0)
else
call SaveReal(udg_UnitHash, GetHandleId(whichUnit), 2, newBonus)
call BlzSetUnitAttackCooldown(whichUnit, 0.022, 0)
endif
endif
endfunction
function GetUnitAttackSpeedBonus takes unit whichUnit returns real
return LoadReal(udg_UnitHash, GetHandleId(whichUnit), 2)
endfunction
so to use these functions you only need to do:
JASS:
call SetUnitAttackSpeedBonus(unit, 3)
that will give the "unit" a bonus of 300% attack speed. the max bonus is capped on a attack cooldown of 0.022 or 45.45 attacks per seconds and -99.99% for the lower bound.
Its not neccessary to do:
JASS:
call SetUnitAttackSpeedBonus(unit, GetUnitAttackSpeedBonus(unit) + 3)
as its already taken in consideration.
this is cool because by using it you not chained to the 400% max bonus or -80%.]
Note that you can do:
JASS:
call SetUnitAttackSpeedBonus(unit, -3)
this you remove 300% bonus attack speed ffrom the unit if its current bonus is greater than or equal to 3 and if its lower will stop at the cap (-99.99%) and save -1 as the current bonus, so you are safe to keep adding negative values because its never going to be lower than -1.
Hope you like it.