library Auramancer
//-----------------------------------------------------------------------
// Auramancer v1.3
// Author: Blightsower
//
// DESCRIPTION:
// A system that assigns bonuses (in the form of spell books) to any specific de/buff.
// Works with de/buffs obtained from anywhere including auras.
//
// HOW TO USE:
// Call the function below at map initialization.
//
// API:
// call Auramancer.register(BUFF_ID, ABILITY_ID, AURA_LEVEL)
//
// Expectation from the system:
// The system accepts a pair of BUFF_ID and ABILITY_ID.
// Whenever a unit gains the BUFF_ID you registered, the system applies the
// appropriate ABILITY_ID and AURA_LEVEL to the unit. Should a moment arise
// where the unit obtains two BUFF_ID's that shares the same ABILITY_ID, the
// ABILITY_ID with the higher AURA_LEVEL gets applied to the unit.
//
// Expectation from the users:
// The user is expected to call Auramancer.register(BUFF_ID, ABILITY_ID, AURA_LEVEL)
// at map initialization either through GUI or vJass initialization.
//
// If the user's BUFF_ID supports multiple levels, the user must declare each BUFF_ID for each
// level.BUFF_ID with multiple levels can share the same spell book. The user must specify
// the AURA_LEVEL. ABILITY_ID must support the levels defined by AURA_LEVEL.
//
//---------------------------------------------------------------
// Requirements:
// - Enable JassHelper
//
// How to enable JassHelper
// - In the Trigger Editor window, look for the menu labeled JassHelper
// and click Enable JassHelper
//-------------------------------------------------------------------------
// HOW TO IMPORT:
// - Copy and paste Auramancer to your map
//-----------------------------------------------------------------------
// SPECIAL THANKS
// Antares
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
// CONFIGURATION
//-----------------------------------------------------------------------
globals
private real timerInterval = .03
private real auramancerStartDelay = 0.
endglobals
//-----------------------------------------------------------------------
// END OF CONFIGURATION
//-----------------------------------------------------------------------
private module CircularLinkedList
readonly thistype next
readonly thistype prev
method init takes nothing returns thistype
set next = this
set prev = this
return this
endmethod
method pushBack takes thistype node returns thistype
set node.prev = prev
set node.next = this
set prev.next = node
set prev = node
return node
endmethod
method pushFront takes thistype node returns thistype
set node.prev = this
set node.next = next
set next.prev = node
set next = node
return node
endmethod
method pop takes nothing returns nothing
set prev.next = next
set next.prev = prev
endmethod
endmodule
module Auramancer
implement CircularLinkedList
private integer buffId
private integer bonusId
private integer auraLevel
private group buffGroup = CreateGroup()
private static timer auraTimer = CreateTimer()
private static group g = CreateGroup()
private static rect r
private static thistype curr = 0
private static method Check takes nothing returns boolean
local unit u = GetFilterUnit()
local thistype this = curr
local integer bonusLevel
if GetUnitAbilityLevel(u, .buffId) > 0 then
if not(IsUnitInGroup(u, .buffGroup)) then
// when unit is first added to the group, its ability level will match the
// aura level
if GetUnitAbilityLevel(u, .bonusId) == 0 then
call UnitAddAbility(u, .bonusId)
endif
call SetUnitAbilityLevel(u, .bonusId, .auraLevel)
call GroupAddUnit(.buffGroup , u)
else
// only happens when the unit is already in the group but the system
// is unsure if the unit has the correct level of bonuses applied
set bonusLevel = GetUnitAbilityLevel(u, .bonusId)
if bonusLevel == 0 then
call UnitAddAbility(u, .bonusId)
call SetUnitAbilityLevel(u, .bonusId, .auraLevel)
else
if bonusLevel < .auraLevel then
call SetUnitAbilityLevel(u, .bonusId, .auraLevel)
else
call SetUnitAbilityLevel(u, .bonusId, bonusLevel)
endif
endif
endif
// unit is expelled from the group and bonuses revoked when it no longer bears the buff
elseif IsUnitInGroup(u, .buffGroup) then
call UnitRemoveAbility(u, .bonusId)
call GroupRemoveUnit(.buffGroup , u)
endif
call GroupRemoveUnit(g,u)
return false
endmethod
private static method auraLoop takes nothing returns nothing
call GroupEnumUnitsInRect(g, r, Filter(function Auramancer.Check))
set curr = curr.next
endmethod
private static method disableAbility takes integer a returns nothing
local integer i = 1
loop
exitwhen i > 24
call SetPlayerAbilityAvailableBJ( false, a, ConvertedPlayer(i))
set i = i + 1
endloop
endmethod
static method register takes integer boof, integer bonus, integer level returns nothing
local thistype this = thistype.allocate()
set .buffId = boof
set .bonusId = bonus
set .auraLevel = level
call disableAbility(.bonusId)
if curr == 0 then
call this.init()
set curr = this
else
call curr.pushBack(this)
endif
endmethod
static method auramancerTimerStart takes nothing returns nothing
set .r = GetPlayableMapRect()
call TimerStart(auraTimer, timerInterval, true, function thistype.auraLoop)
endmethod
endmodule
struct Auramancer
implement Auramancer
private static method auramancerDelayedStart takes nothing returns nothing
call auramancerTimerStart()
endmethod
static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterTimerEventSingle(t, auramancerStartDelay)
call TriggerAddAction(t, function thistype.auramancerDelayedStart)
set t = null
endmethod
endstruct
endlibrary