- Joined
- Feb 28, 2007
- Messages
- 3,479
Hello. I don't really have a problem as much as a question. I have made a simple ability, and it's the first time I've made an ability using structs, so I wonder how to best improve and optimize my code.
Thanks in advance.
JASS:
scope RadarScan initializer Init
//-----------------------------------
//--! CONFIG CONSTANTS !-------------
//-----------------------------------
globals
private constant integer ABILITY_ID = 'A014'
private constant integer DETECTION_ABILITY_ID = 'A00Y'
private constant real DURATION = 15.0
private constant string DETECT_EFFECT = "Abilities\\Spells\\Other\\Andt\\Andt.mdl"
endglobals
//-----------------------------------
//--! CODE !-------------------------
//-----------------------------------
struct Scan_Data
unit u
real d
endstruct
globals
private timer timerExpiration
private Scan_Data array ar
private integer totalUnits = 0
endglobals
private function Counter takes nothing returns nothing
local Scan_Data sd = Scan_Data.create()
local integer i = 0
loop
exitwhen i >= totalUnits
set sd = ar[i]
set sd.d = sd.d - 1.0
if sd.d <= 0.0 then
call UnitRemoveAbility(sd.u,DETECTION_ABILITY_ID)
set ar[i] = ar[totalUnits - 1]
set totalUnits = totalUnits - 1
call sd.destroy()
endif
set i = i + 1
endloop
if totalUnits == 0 then
call PauseTimer(timerExpiration)
endif
endfunction
private function OnSpellEffect takes nothing returns nothing
local Scan_Data sd = Scan_Data.create()
set sd.u = GetTriggerUnit()
set sd.d = DURATION
call UnitAddAbility(sd.u,DETECTION_ABILITY_ID)
call DestroyEffect(AddSpecialEffect(DETECT_EFFECT,GetUnitX(sd.u),GetUnitY(sd.u)))
if totalUnits == 0 then
call TimerStart(timerExpiration,1.0,true,function Counter)
endif
set totalUnits = totalUnits + 1
set ar[totalUnits - 1] = sd
endfunction
private function SpellIdMatch takes nothing returns boolean
return GetSpellAbilityId() == ABILITY_ID
endfunction
private function Init takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddCondition(t,Condition(function SpellIdMatch))
call TriggerAddAction(t,function OnSpellEffect)
set timerExpiration = NewTimer()
set t = null
endfunction
endscope
Thanks in advance.