- Joined
- Jun 9, 2011
- Messages
- 91
Buff Generator version 3.3.0
This system allows rapid and easy buff development.
System code:
This system allows rapid and easy buff development.
System code:
JASS:
library BuffGen /* v3.3.0.0
************************************************************************************************
* BUFF GENERATOR by Bills
*
* This system allows rapid and easy buff development.
*
************************************************************************************************
*
* */ uses /*
*
* */ UnitIndexer /* hiveworkshop.com/forums/jass-resources-412/system-unit-indexer-172090/
* */ Event /* hiveworkshop.com/forums/jass-resources-412/snippet-event-186555/
* */ CTL /* hiveworkshop.com/forums/jass-resources-412/snippet-constant-timer-loop-32-a-201381/
* */ TimerUtils /* wc3c.net/showthread.php?t=101322
* */ Table /* hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/
*
***************************************************************************************************
*
* Functions
*
* function UnitAddBuff takes unit whichUnit, Buff whichBuff, real duration returns boolean
* - returns true if added the buff
*
* function UnitRemoveBuff takes unit wichUnit, Buff whichBuff returns boolean
* - returns true if removed the buff
*
* function UnitRemoveAllBuffs takes unit wichUnit returns nothing
* - removes all buffs of unit
*
* function UnitHasBuff takes unit wichUnit, Buff whichBuff returns boolean
* - returns true if unit has the buff
*
* function SetUnitBuffDuration takes unit whichUnit, Buff whichBuff, real duration returns nothing
* - sets the duration of buff
*
* function AddUnitBuffDuration takes unit whichUnit, Buff whichBuff, real duration returns nothing
* - increments the duration of buff
*
* function GetUnitBuffDuration takes unit whichUnit, Buff whichBuff, real duration returns real
* - returns the duration of buff
*
* function CountBuffsOnUnit takes unit whichUnit returns integer
* - returns de amount of buffs that unit have
*
*******************************************************************************************
*
* struct Buff extends array
*
* static constant real TIMEOUT=0.03125
* - Useful to the method onPeriodic
* static Event START
* - fires when a buff is added
* static Event FINISH
* - fires when a buff is removed
*
* Event Responses
* - function GetEventBuff takes nothing returns Buff
* - function GetBuffEventUnit takes nothing returns unit
* - function GetBuffEventUnitId takes nothing returns integer
*
* function TriggerRegisterBuffEvent takes trigger t, Event e returns nothing
* - Jass wrapper of EVENT.registerTrigger(trigger)
*
* function RegisterBuffEvent takes boolexpr b, Event e returns nothing
* - Jass wrapper of EVENT.register(boolexpr)
*
*****************************************************************************
*
* module BuffStruct
*
* static method operator buff takes nothing returns Buff
* - returns the Buff generated by implementation of this module
*
* (interface)
*
* private static constant integer AURAID = 'AURA'
* (required) - this is a tornado slow aura
* private static constant integer BUFFID = 'BUFF'
* (required) - this is the buff of aura
*
* private method onApply takes nothing returns nothing
* (optional) - called all times that AddBuff is called
*
* private method onRemove takes nothing returns nothing
* (optional) - called all times that RemoveBuff is called
*
* private method onFinish takes nothing returns nothing
* (optional) - called only when the buff finish completely your duration
*
* private method onPeriodic takes nothing returns nothing
* (optional) - called 32 times per second
*
* Notes:
* - "thistype this" represents the index of buffed unit in all methods above.
* - If you do not want to use AURAID or/and BUFFID, just set it with 0 (zero).
*
********************************************************************************
*
* ForEachBuff Modules
*
* allows the method ForEachBuff takes unit enumUnit returns nothing
*
* module ForEachBuff (optional)
* - declare local variables here
* module ForEachBuffLoop ( not optional)
* - run loop code
* module ForEachBuffNull (optional)
* - null locals
* module ForEachBuffEnd (not optional)
*
* Example of ForEachBuff
* - struct Test extends array
* private static unit targ
* implement ForEachBuff
* local unit target = targ[GetUnitUserData(GetTriggerUnit())]
* implement ForEachBuffLoop
* // deals 50 damage on target for each buff on enumUnit
* call UnitDamageTarget(enumUnit, target, 50, true, false, ATTACK_TYPE_NORMAL, DAMAGE_TYPE_MAGIC, WEAPON_TYPE_WHOKNOWS)
* implement ForEachBuffNull
* set target=null
* implement ForEachBuffEnd
*
* static method onCast takes nothing returns boolean
* set targ[GetUnitUserData(GetTriggerUnit())] = GetSpellTargetUnit()
* call ForEachBuff(GetTriggerUnit())
* return false
* endmethod
* endstruct
*
************************************************************************************/
globals
private integer total = 0 //Buff counter
private integer array ID //Buff indexer
private integer array aid //ability id
private integer array bid //buff id
private integer array bc
private Table array t
private Event array e
private integer array ed1 // event data
private integer array ed2 // other event data
private boolean ee=true
private unit eu=null
private Buff eb=0
endglobals
private module M
private static method onInit takes nothing returns nothing
set START = Event.create()
set FINISH = Event.create()
endmethod
endmodule
struct Buff extends array
readonly static constant real TIMEOUT = 0.031250000
readonly static Event START
readonly static Event FINISH
implement M
endstruct
private function InitBuff takes integer auraid, integer buffid, code func returns integer
local integer id = total + 1
set total = id
set e[id] = Event.create()
call e[id].register(Filter(func))
set t[id] = Table.create()
set aid[id] = auraid
set bid[id] = buffid
return id
endfunction
function UnitHasBuff takes unit u, Buff b returns boolean
return t[b].boolean[GetUnitUserData(u)]
endfunction
function UnitRemoveBuff takes unit u, Buff b returns boolean
debug if u == null or b <= 0 or integer(b) > total then
debug call BJDebugMsg("[Buff Generator] Error: invalid arguments on RemoveBuff")
debug return false
debug endif
if UnitHasBuff(u,b) then
set t[b].boolean[GetUnitUserData(u)] = false
if UnitMakeAbilityPermanent(u,false,aid[b]) then
call UnitRemoveAbility(u,aid[b])
call UnitRemoveAbility(u,bid[b])
endif
set ed1[b] = GetUnitUserData(u)
set ed2[b] = 2
call e[b].fire()
if ee then
set eu = u
set eb = b
call Buff.FINISH.fire()
endif
return true
endif
return false
endfunction
function UnitAddBuff takes unit u, Buff b, real dur returns boolean
local integer i
static if DEBUG_MODE then
if (u == null or b <= 0 or integer(b) > total) then
call BJDebugMsg("[Buff Generator] Error: invalid arguments on AddBuff")
return false
endif
endif
if UnitHasBuff(u,b) then
call UnitRemoveBuff(u,b)
call UnitAddBuff(u,b,dur)
else
set i = GetUnitUserData(u)
set t[b].boolean[i] = true
if UnitAddAbility(u,aid[b]) then
call UnitMakeAbilityPermanent(u,true,aid[b])
endif
set t[b].real[i] = dur
set ed1[b] = i
set ed2[b] = 1
call e[b].fire()
if ee then
set eu = u
set eb = b
call Buff.START.fire()
endif
return true
endif
return false
endfunction
function UnitRemoveAllBuffs takes unit u returns nothing
local Buff i=total
loop
exitwhen i == 0
call UnitRemoveBuff(u,i)
set i = i - 1
endloop
endfunction
function SetUnitBuffDuration takes unit u, Buff b, real dur returns nothing
local integer i = GetUnitUserData(u)
if UnitHasBuff(u,b) then
set t[b].real[i] = dur
if not (t[b].timer[i] == null) then
set ed1[b] = i
set ed2[b] = 3
call e[b].fire()
endif
endif
endfunction
function GetUnitBuffDuration takes unit u, Buff b returns real
local integer i = GetUnitUserData(u)
if UnitHasBuff(u,b) then
if t[b].timer[i] == null then
return t[b].real[i]
else
return TimerGetRemaining(t[b].timer[i])
endif
endif
return 0.
endfunction
function AddUnitBuffDuration takes unit u, Buff b, real dur returns nothing
call SetUnitBuffDuration(u,b,GetUnitBuffDuration(u,b)+dur)
endfunction
function CountBuffsOnUnit takes unit u returns integer
return bc[GetUnitUserData(u)]
endfunction
function TriggerRegisterBuffEvent takes trigger t, Event e returns nothing
call e.registerTrigger(t)
endfunction
function RegisterBuffEvent takes boolexpr t, Event e returns nothing
call e.register(t)
endfunction
function GetBuffEventUnit takes nothing returns unit
return eu
endfunction
function GetBuffEventUnitId takes nothing returns integer
return GetUnitUserData(eu)
endfunction
function GetEventBuff takes nothing returns Buff
return eb
endfunction
module BuffStruct
static method operator buff takes nothing returns Buff
return ID[thistype.typeid]
endmethod
static if thistype.onPeriodic.exists then
private integer index
private thistype T
implement CTL
local real d
implement CTLExpire
set d = t[ID[thistype.typeid]].real[index]-Buff.TIMEOUT
call thistype(index).onPeriodic()
if d <= 0.00 then
call UnitRemoveBuff(GetUnitById(index),ID[thistype.typeid])
static if thistype.onFinish.exists then
call thistype(index).onFinish()
endif
else
set t[ID[thistype.typeid]].real[index] = d
endif
implement CTLNull
implement CTLEnd
else
private static method exp takes nothing returns nothing
local integer i = GetTimerData(GetExpiredTimer())
call UnitRemoveBuff(GetUnitById(i),ID[thistype.typeid])
static if thistype.onFinish.exists then
call thistype(i).onFinish()
endif
endmethod
endif
private static method m takes nothing returns nothing
local integer i = ed1[ID[thistype.typeid]] //unit id
local integer j = ed2[ID[thistype.typeid]] //data
local thistype this = thistype(i)
if j == 1 then
static if thistype.onPeriodic.exists then
set T = create()
set T.index = i
else
set t[ID[thistype.typeid]].timer[i]=NewTimerEx(i)
call TimerStart(t[ID[thistype.typeid]].timer[i],t[ID[thistype.typeid]].real[i],false,function thistype.exp)
endif
static if thistype.onApply.exists then
call onApply()
endif
set bc[i]= bc[i] + 1
elseif j==2 then
static if thistype.onPeriodic.exists then
call T.destroy()
else
call ReleaseTimer(t[ID[thistype.typeid]].timer[i])
endif
static if thistype.onRemove.exists then
call onRemove()
endif
set bc[i] = bc[i] - 1
elseif j==3 then
static if not thistype.onPeriodic.exists then
call ReleaseTimer(t[ID[thistype.typeid]].timer[i])
set t[ID[thistype.typeid]].timer[i] = NewTimerEx(i)
call TimerStart(t[ID[thistype.typeid]].timer[i],t[ID[thistype.typeid]].real[i],false,function thistype.exp)
endif
endif
endmethod
private static method onInit takes nothing returns nothing
set ID[thistype.typeid] = InitBuff(AURAID,BUFFID,function thistype.m)
endmethod
endmodule
// ForEachBuff Modules
module ForEachBuff
static method ForEachBuff takes unit enumUnit returns nothing
local Buff this = total
endmodule
module ForEachBuffLoop
implement ForEachBuff
loop
exitwhen this == 0
if UnitHasBuff(enumUnit,this) then
endmodule
module ForEachBuffNull
endif
set this = this - 1
endloop
endmodule
module ForEachBuffEnd
implement ForEachBuffNull
endmethod
endmodule
endlibrary
Exemple Usage
JASS:
// stackable buff example
library Demos uses BuffGen, Bonus
struct Innerfire extends array
// increases attack damage by 50 (statckable). Lasts 10 seconds.
private static constant integer AURAID = 'AXXX' // required
private static constant integer BUFFID = 'BXXX' // required
private integer stack // stack count
private static integer MAX=5 // stack max
private boolean toStack // stack condition
private method onApply takes nothing returns nothing
if stack<MAX then
set stack=stack+1
call AddUnitBonus(GetUnitById(this),BONUS_DAMAGE,50*stack) // apply new bonus
endif
endmethod
private method onRemove takes nothing returns nothing
call AddUnitBonus(GetUnitById(this),BONUS_DAMAGE,-50*stack) // remove actual bonus
if not toStack then // before of onApply, is ever called onRemove. Why the buff is replaced.
set stack=0
endif
endmethod
implement BuffStruct // module implementation
// code of spell
private static method onCast takes nothing returns boolean
local unit spellTarget=GetSpellTargetUnit()
local thistype this=GetUnitUserData(spellTarget)
set toStack=true
call UnitAddBuff(spellTarget,thistype.buff,10)
set toStack=false
set spellTarget=null
return false
endmethod
endstruct
endlibrary
JASS:
struct Example extends array
//implement ForEachBuff // it's optional
implement ForEachBuffLoop
// decreases the duration of each buff in 10 seconds
call AddUnitBuffDuration(enumUnit,this,-10)
//implement ForEachBuffNull // it's optional
implement ForEachBuffEnd
static method onCast takes nothing returns boolean
call ForEachBuff(GetTriggerUnit())
return false
endmethod
endstruct
Attachments
Last edited: