- Joined
- Sep 12, 2008
- Messages
- 657
Well, hello.
I've made a vjass spell, using a struct, after a year of being away from WC in general.. Been quite a hassle, but managed to put something together,
the problem is, I'm not sure if its as efficient as possible.
Anyhow, about the spell:
Its a single unit ability, called Chained Dagger.
Technically, once cast on a unit, it gets replaced with a new ability for duration seconds.
after the duration is passed, it restores the original ability.
Right now it works quite well, but theres a 0.5 second lag or something around that at the first cast of the spell.
heres the code:
P.S: I've always been bad at explaining stuff, tell me if I should give more details regarding something.. ><
Thanks for the help in advance.
I've made a vjass spell, using a struct, after a year of being away from WC in general.. Been quite a hassle, but managed to put something together,
the problem is, I'm not sure if its as efficient as possible.
Anyhow, about the spell:
Its a single unit ability, called Chained Dagger.
Technically, once cast on a unit, it gets replaced with a new ability for duration seconds.
after the duration is passed, it restores the original ability.
Right now it works quite well, but theres a 0.5 second lag or something around that at the first cast of the spell.
heres the code:
JASS:
library ChainedDagger initializer onInit/*
*/ uses /*
*/ SpellEvent /*
*/ TimerUtils
struct ChainedDagger
public static integer Ability_Identifier = 'A001'
public static integer NewAbility_Identifier = 'A002'
public static integer AuraSpellBook_Identifier = 'A005'
public static timer Timer
public static real TimerSpeed = 0.032
public static thistype array Instances
public static integer Counter = 0
public unit caster
public unit target
public real duration
public integer id
public boolean started
public static method create takes unit caster, unit target, real duration returns thistype
local thistype d = thistype.allocate()
set d.caster = caster
set d.target = target
set d.duration = duration
set d.id = d.assignIdentifier()
set d.started = false
return d
endmethod
public method assignIdentifier takes nothing returns integer
set Instances[ChainedDagger.Counter] = this
set ChainedDagger.Counter = ChainedDagger.Counter + 1
return ChainedDagger.Counter - 1
endmethod
static method operator [] takes integer id returns thistype
if (id >= Counter) then
return -1
endif
return Instances[id]
endmethod
public method startCast takes nothing returns nothing
local player p = GetOwningPlayer(.caster)
call SetPlayerAbilityAvailable(p, ChainedDagger.Ability_Identifier, false)
call UnitAddAbility(.caster, ChainedDagger.NewAbility_Identifier)
call SetUnitAbilityLevel(.caster, ChainedDagger.NewAbility_Identifier, GetUnitAbilityLevel(.caster, ChainedDagger.Ability_Identifier))
call UnitAddAbility(.target, ChainedDagger.AuraSpellBook_Identifier)
set .started = true
set p = null
endmethod
public method stopCast takes nothing returns nothing
local player p = GetOwningPlayer(.caster)
call SetPlayerAbilityAvailable(p, ChainedDagger.Ability_Identifier, true)
call UnitRemoveAbility(.caster, ChainedDagger.NewAbility_Identifier)
call UnitRemoveAbility(.target, ChainedDagger.AuraSpellBook_Identifier)
set p = null
call .destroy()
endmethod
private method destroy takes nothing returns nothing
//Fix the indexing system
local integer cnt = 0
local integer max = ChainedDagger.Counter
loop
set ChainedDagger[cnt].id = ChainedDagger[cnt].id - 1
set cnt = cnt + 1
exitwhen cnt <= max
endloop
set ChainedDagger.Instances[.id] = -1
//Remove the leaking data
set .caster = null
set .target = null
set .started = false
endmethod
public static method onUpdate takes nothing returns nothing
local integer data
local thistype d
if (ChainedDagger.Counter <= 0) then
set ChainedDagger.Counter = 0
call ReleaseTimer(ChainedDagger.Timer)
endif
set data = GetTimerData(ChainedDagger.Timer)
set d = ChainedDagger[data]
if (d.started == true) then
set d.duration = d.duration - ChainedDagger.TimerSpeed
if (d.duration <= 0) then
call d.stopCast()
endif
endif
if (data < ChainedDagger.Counter - 1) then
call SetTimerData(ChainedDagger.Timer, data + 1)
else
call SetTimerData(ChainedDagger.Timer, 0)
endif
endmethod
public static method onSpellCast takes nothing returns nothing
local unit caster = SpellEvent.CastingUnit
local unit target = SpellEvent.TargetUnit
local real duration = 3 // 3 seconds so I could test the lag thingy, it only happens on the first run.
// there's going to be an equation used by it, but should suffice for now.
local thistype d = thistype.create(caster, target, duration)
call d.startCast()
if (ChainedDagger.Counter == 1) then
set ChainedDagger.Timer = NewTimerEx(0)
call TimerStart(ChainedDagger.Timer, ChainedDagger.TimerSpeed, true, function ChainedDagger.onUpdate)
endif
set caster = null
set target = null
endmethod
endstruct
public function onInit takes nothing returns nothing
call RegisterSpellCastResponse(ChainedDagger.Ability_Identifier, ChainedDagger.onSpellCast)
endfunction
endlibrary
P.S: I've always been bad at explaining stuff, tell me if I should give more details regarding something.. ><
Thanks for the help in advance.