kingkingyyk3
Visitor (Welcome to the Jungle, Baby!)
- Reaction score
- 216
JASS:
/*
Heal v2.0.0
by kingking
- Library that transforms healing into a breeze, allowing full control on healing.
CreateHealType()-> HealType
~ Generate a heal type.
HealUnit(unit a, unit b,real,HealType)
~ Heal the unit + fire event.
~ Unit a -> Healer
~ Unit b -> Target
RegisterHealEvent(trigger,integer)
~ trigger - Trigger to be registered, integer - priority
UnregisterHealEvent(trigger)
~ trigger - Trigger to be registered
RegisterHealUnitEvent(trigger,unit,integer)
~ trigger - Trigger to be registered, unit - healer, integer - priority
~ Fires if the unit heals anything.
UnregisterHealUnitEvent(trigger,unit)
~ trigger - Trigger to be registered, unit - healer
RegisterUnitHealedEvent(trigger,unit,integer)
~ trigger - Trigger to be registered, unit - healed, integer - priority
~ Fires if the unit is healed by anything.
UnregisterUnitHealedEvent(trigger,unit)
~ trigger - Trigger to be registered, unit - healed
GetHealAmount() -> real
~ Get the amount of healing at the moment.
SetHealAmount(real)
~ Update the amount of healing.
GetHealType()-> HealType
~ Get the type of healing.
GetHealer() -> unit
~ Get healer.
GetHealedUnit()-> unit
~ Get healed unit.
Requires : A vJass complier
*/
library Heal
struct HealType extends array
endstruct
globals
HealType HEAL_TYPE_NORMAL=1
private integer HealTypeCount=1
private real Final=0.0
private unit Healer=null
private unit Target=null
private HealType CurrHealType=1
private key GenericCallback
private key HealUnitCallback
private key UnitHealedCallback
private integer array TrigType
private unit array HealedUnitList
private unit array HealerList
private trigger array Trig
private integer array Next
private integer array Prev
private integer array Priority
private integer array IndexStack
private integer IndexStackMax=0
private integer IndexMax=0
endglobals
function CreateHealType takes nothing returns HealType
set HealTypeCount=HealTypeCount+1
return HealTypeCount
endfunction
function GetHealer takes nothing returns unit
return Healer
endfunction
function GetHealedUnit takes nothing returns unit
return Target
endfunction
function GetHealType takes nothing returns HealType
return CurrHealType
endfunction
function GetHealAmount takes nothing returns real
return Final
endfunction
function SetHealAmount takes real r returns nothing
set Final=r
endfunction
private function FireEvent takes unit healer, unit target, real amount, HealType htype returns nothing
local unit oldhealer=Healer
local unit oldtarget=Target
local real oldamount=Final
local HealType oldhealtype=CurrHealType
local integer i=Next[0]
local boolean ToFire
set Healer=healer
set Target=target
set Final=amount
set CurrHealType=htype
loop
exitwhen i==0
set ToFire=false
if TrigType<i>==GenericCallback then
set ToFire=true
elseif TrigType<i>==HealUnitCallback and HealerList<i>==healer then
set ToFire=true
elseif TrigType<i>==UnitHealedCallback and HealedUnitList<i>==target then
set ToFire=true
endif
if ToFire==true and IsTriggerEnabled(Trig<i>) and TriggerEvaluate(Trig<i>) then
call TriggerExecute(Trig<i>)
endif
set i=Next<i>
endloop
if Final>amount then
call SetWidgetLife(target,GetWidgetLife(target)+(Final-amount))
elseif Final<amount and Final>=0.0 then
call SetWidgetLife(target,GetWidgetLife(target)-(amount-Final))
endif
set Healer=oldhealer
set Target=oldtarget
set Final=oldamount
set CurrHealType=oldhealtype
set oldhealer=null
set oldtarget=null
endfunction
function HealUnit takes unit healer, unit target, real amount, HealType htype returns nothing
local real result
if GetWidgetLife(target)>.405 then
set result=GetWidgetLife(target)+amount
call SetWidgetLife(target,result)
call FireEvent(healer,target,amount,htype)
endif
endfunction
function RegisterHealEvent takes trigger trig, integer prio returns nothing
local integer head
local integer i
local boolean isAdded
if trig!=null then
if IndexStackMax==0 then
set IndexMax=IndexMax+1
set i=IndexMax
else
set i=IndexStack[IndexStackMax]
set IndexStackMax=IndexStackMax-1
endif
set Trig<i>=trig
set Priority<i>=prio
set TrigType<i>=GenericCallback
set isAdded=false
set head=Next[0]
loop
if prio>Priority[head] then
set Next[Prev[head]]=i
set Prev<i>=Prev[head]
set Next<i>=head
set Prev[head]=i
set isAdded=true
set head=Prev[0]
endif
set head=Next[head]
exitwhen head==0
endloop
if isAdded==false then
set Next[Prev[head]]=i
set Prev<i>=Prev[head]
set Next<i>=head
set Prev[head]=i
endif
endif
endfunction
function UnregisterHealEvent takes trigger trig returns nothing
local integer i=Next[0]
loop
exitwhen i==0
if Trig<i>==trig and TrigType<i>==GenericCallback then
set Next[Prev<i>]=Next<i>
set Prev[Next<i>]=Prev<i>
set IndexStackMax=IndexStackMax+1
set IndexStack[IndexStackMax]=i
return
endif
set i=Next<i>
endloop
endfunction
function RegisterHealUnitEvent takes trigger trig, unit u, integer prio returns nothing
local integer head
local integer i
local boolean isAdded
if trig!=null then
if IndexStackMax==0 then
set IndexMax=IndexMax+1
set i=IndexMax
else
set i=IndexStack[IndexStackMax]
set IndexStackMax=IndexStackMax-1
endif
set Trig<i>=trig
set Priority<i>=prio
set TrigType<i>=HealUnitCallback
set HealerList<i>=u
set isAdded=false
set head=Next[0]
loop
if prio>Priority[head] then
set Next[Prev[head]]=i
set Prev<i>=Prev[head]
set Next<i>=head
set Prev[head]=i
set isAdded=true
set head=Prev[0]
set isAdded=true
endif
set head=Next[head]
exitwhen head==0
endloop
if isAdded==false then
set Next[Prev[head]]=i
set Prev<i>=Prev[head]
set Next<i>=head
set Prev[head]=i
endif
endif
endfunction
function UnregisterHealUnitEvent takes trigger trig, unit u returns nothing
local integer i=Next[0]
loop
exitwhen i==0
if Trig<i>==trig and HealerList<i>==u then
set Next[Prev<i>]=Next<i>
set Prev[Next<i>]=Prev<i>
set HealerList<i>=null
set IndexStackMax=IndexStackMax+1
set IndexStack[IndexStackMax]=i
return
endif
set i=Next<i>
endloop
endfunction
function RegisterUnitHealedEvent takes trigger trig, unit u, integer prio returns nothing
local integer head
local integer i
local boolean isAdded
if trig!=null then
if IndexStackMax==0 then
set IndexMax=IndexMax+1
set i=IndexMax
else
set i=IndexStack[IndexStackMax]
set IndexStackMax=IndexStackMax-1
endif
set Trig<i>=trig
set Priority<i>=prio
set TrigType<i>=UnitHealedCallback
set HealedUnitList<i>=u
set isAdded=false
set head=Next[0]
loop
if prio>Priority[head] then
set Next[Prev[head]]=i
set Prev<i>=Prev[head]
set Next<i>=head
set Prev[head]=i
set isAdded=true
set head=Prev[0]
set isAdded=true
endif
set head=Next[head]
exitwhen head==0
endloop
if isAdded==false then
set Next[Prev[head]]=i
set Prev<i>=Prev[head]
set Next<i>=head
set Prev[head]=i
endif
endif
endfunction
function UnregisterUnitHealedEvent takes trigger trig, unit u returns nothing
local integer i=Next[0]
loop
exitwhen i==0
if Trig<i>==trig and HealedUnitList<i>==u then
set Next[Prev<i>]=Next<i>
set Prev[Next<i>]=Prev<i>
set HealedUnitList<i>=null
set IndexStackMax=IndexStackMax+1
set IndexStack[IndexStackMax]=i
return
endif
set i=Next<i>
endloop
endfunction
endlibrary
</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>
Heal
v1.0.0 - Release
v1.0.1 - Changed Healable from a boolean into an integer stack.
v1.0.2 - Changed Healable to Heal_Block/Heal_BlockAll.
v1.0.3 - Fixed bug that caused the system ignore the amount of healing.
v1.0.4 - Added heal type, it is useful for spell making.
v1.0.5 - Fixed heal type's bug. Changed Heal_GetTriggerUnit() to Heal_Target().
v1.0.6 - Removed useless parameter in Heal_BlockAll(). Fixed block healing code.
v1.0.7 - Moved initializer into module to fix bug (Heal_RegisterEvent did not work in struct initializers) which is caused by jasshelper.
v1.0.8 - Removed stack, replaced with local variables. A little efficiency gain in getting source/target/amount/type and adding block. Removed the usage of UnitAlive to avoid problem from map optimizer.
v2.0.0 - Rewrote whole library from scratch. Added prioritized event firing, ability to register heal events for particular unit, and modify amount of healing. Heal can never be done so easily before this!