- Joined
- Oct 12, 2011

- Messages
- 3,449

JASS:

```
library Regeneration uses TimerUtils, UnitIndexer
globals
private constant real REGENERATION_RATE = 0.2
endglobals
/*
Regeneration v1.0
by: Quilnez
Allows you to apply hp and mp regeneration on units.
Requires:
- UnitIndexer by Nestharus | hiveworkshop.com/forums/spells-569/unit-indexer-v5-3-0-1-a-260859/
- TimerUtils by Vexorian | wc3c.net/showthread.php?t=101322
API
struct Regeneration
1. Apply/add regeneration to a unit
static method apply takes unit whichUnit, boolean hp, real regenVal returns Regeneration
static method add takes unit whichUnit, boolean hp, real regenVal returns Regeneration
- boolean hp => false to apply to mp regeneration
- real regenVal => regenerated hp/hp per second
2. Get unit regeneration rate (per second)
static method get takes unit whichUnit, boolean hp returns real
3. Stop any regeneration on a unit
static method stop takes unit whichUnit returns nothing
4. Set/get unit regenerations (per second)
method operator hpRegen= takes real regenVal returns nothing
method operator mpRegen= takes real regenVal returns nothing
method operator hpRegen takes nothing returns real
method operator mpRegen takes nothing returns real
function SetUnitHPRegeneration takes unit whichUnit, real regenVal returns Regeneration
function SetUnitMPRegeneration takes unit whichUnit, real regenVal returns Regeneration
function AddUnitHPRegeneration takes unit whichUnit, real regenVal returns Regeneration
function AddUnitMPRegeneration takes unit whichUnit, real regenVal returns Regeneration
function GetUnitHPRegeneration takes unit whichUnit, real regenVal returns real
function GetUnitMPRegeneration takes unit whichUnit, real regenVal returns real
*/
native UnitAlive takes unit id returns boolean
struct Regeneration
private unit target
private timer time
private real hpInc
private real mpInc
private static thistype array Index
method destroy takes nothing returns nothing
set Index[GetUnitUserData(target)] = 0
call ReleaseTimer(time)
call deallocate()
set target = null
set time = null
endmethod
method operator hpRegen= takes real regenVal returns nothing
set hpInc = REGENERATION_RATE/1.*regenVal
endmethod
method operator mpRegen= takes real regenVal returns nothing
set mpInc = REGENERATION_RATE/1.*regenVal
endmethod
method operator hpRegen takes nothing returns real
return 1./REGENERATION_RATE*hpInc
endmethod
method operator mpRegen takes nothing returns real
return 1./REGENERATION_RATE*mpInc
endmethod
private static method onPeriodic takes nothing returns nothing
local thistype this = GetTimerData(GetExpiredTimer())
if UnitAlive(target) then
call SetWidgetLife(target, GetWidgetLife(target)+hpInc)
call SetUnitState(target, UNIT_STATE_MANA, GetUnitState(target, UNIT_STATE_MANA)+mpInc)
endif
endmethod
static method stop takes unit whichUnit returns nothing
call Index[GetUnitUserData(whichUnit)].destroy()
endmethod
static method get takes unit whichUnit, boolean hp returns real
if hp then
return Index[GetUnitUserData(whichUnit)].hpRegen
else
return Index[GetUnitUserData(whichUnit)].mpRegen
endif
endmethod
static method add takes unit whichUnit, boolean hp, real regenVal returns thistype
return apply(whichUnit, hp, get(whichUnit, hp)+regenVal)
endmethod
static method apply takes unit whichUnit, boolean hp, real regenVal returns thistype
local thistype this = Index[GetUnitUserData(whichUnit)]
if this == 0 then
set this = allocate()
set Index[GetUnitUserData(whichUnit)] = this
set target = whichUnit
set time = NewTimerEx(this)
call TimerStart(time, REGENERATION_RATE, true, function thistype.onPeriodic)
endif
if hp then
set hpRegen = regenVal
else
set mpRegen = regenVal
endif
return this
endmethod
private static method onDeindex takes nothing returns boolean
local thistype this = Index[GetUnitUserData(GetIndexedUnit())]
if this != 0 then
call destroy()
endif
return false
endmethod
private static method onInit takes nothing returns nothing
call RegisterUnitIndexEvent(Condition(function thistype.onDeindex), UnitIndexer.DEINDEX)
endmethod
endstruct
function SetUnitHPRegeneration takes unit whichUnit, real regenVal returns Regeneration
return Regeneration.apply(whichUnit, true, regenVal)
endfunction
function SetUnitMPRegeneration takes unit whichUnit, real regenVal returns Regeneration
return Regeneration.apply(whichUnit, false, regenVal)
endfunction
function AddUnitHPRegeneration takes unit whichUnit, real regenVal returns Regeneration
return Regeneration.add(whichUnit, true, regenVal)
endfunction
function AddUnitMPRegeneration takes unit whichUnit, real regenVal returns Regeneration
return Regeneration.add(whichUnit, false, regenVal)
endfunction
function GetUnitHPRegeneration takes unit whichUnit, real regenVal returns real
return Regeneration.get(whichUnit, true)
endfunction
function GetUnitMPRegeneration takes unit whichUnit, real regenVal returns real
return Regeneration.get(whichUnit, false)
endfunction
endlibrary
```