Can this be optimized more?

cr4xzZz

Also known as azwraith_ftL.
Reaction score
51
I've made a JASS version of Flare's Frozen Orb ability for my map. I think that this could be optimized a lot more but I'm not sure. Uses ABC and TT for trigger and timer attaching.

Frozen orb is a Diablo II spell.

JASS:

scope FrozenOrb

globals
// ability raw code
    private constant integer ABILITY_RAW = 'A000'
// missile frozen orb unit
    private constant integer MISSILE_RAW = 'h001'
// shards unit
    private constant integer BOLT_RAW = 'h002'
// dummy caster
    private constant integer DUMMY = 'h000'
// frost nova raw
    private constant integer FROST_NOVA = 'A002'
// buff frost
    private constant integer BUFF_RAW = 'Bfro'
// max travel distance for the orb
    private constant real MAX_DIST = 1050.
// travel speed
    private constant real SPEED = 425.
// radius of effect of the orb
    private constant real M_RADIUS = 225.
// radius of effect of the shards
    private constant real S_RADIUS = 100.
endglobals

private struct Data
    unit missile
    integer ticks
    real dx
    real dy
    trigger trig = CreateTrigger()
    
    method onDestroy takes nothing returns nothing
        call RemoveUnit(.missile)
        call ClearTriggerStructA(.trig)
        call DestroyTrigger(.trig)
    endmethod
endstruct

private struct Shard
    unit shard
    trigger trig = CreateTrigger()
    
    method onDestroy takes nothing returns nothing
        call RemoveUnit(.shard)
        call ClearTriggerStructA(.trig)
        call DestroyTrigger(.trig)
    endmethod
endstruct

private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == ABILITY_RAW
endfunction

private function Move takes nothing returns boolean
    local Data d = TT_GetData()
    local real x = GetUnitX(d.missile)
    local real y = GetUnitY(d.missile)
    local real dx = x + d.dx
    local real dy = y + d.dy
    call SetUnitPosition(d.missile, dx, dy) 
    set d.ticks = d.ticks - 1
    if d.ticks <= 0 then
        call d.destroy()
        return true
    endif
    return false
endfunction

private function Damage takes nothing returns boolean
    local Data d = GetTriggerStructA(GetTriggeringTrigger())
    local unit pick = GetTriggerUnit()
    local unit dummy 
    if IsUnitEnemy(pick, GetOwningPlayer(d.missile)) and GetWidgetLife(pick) > 0.405 and IsUnitType(pick, UNIT_TYPE_STRUCTURE) == false then
        if GetUnitAbilityLevel(pick, BUFF_RAW) > 0 then
        else
            set dummy = CreateUnit(GetOwningPlayer(d.missile), DUMMY, 0., 0., 0.)
            call UnitAddAbility(dummy, FROST_NOVA)
            call IssueTargetOrder(dummy, "frostnova", pick)
            call UnitApplyTimedLife(dummy, 'BTLF', 2.)
        endif
        call UnitDamageTarget(d.missile, pick, 10., false, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_MAGIC, null)
    endif
    set dummy = null
    set pick = null
    return false
endfunction

private function RemoveShard takes nothing returns boolean
    local Shard i = TT_GetData()
    if GetWidgetLife(i.shard) < 0.405 then
        call i.destroy()
        return true 
    endif
    return false
endfunction

private function ShardDmg takes nothing returns boolean
    local Shard i = GetTriggerStructA(GetTriggeringTrigger())
    local unit pick = GetTriggerUnit()
    local unit dummy 
    if IsUnitEnemy(pick, GetOwningPlayer(i.shard)) and GetWidgetLife(pick) > 0.405 and IsUnitType(pick, UNIT_TYPE_STRUCTURE) == false then
        if GetUnitAbilityLevel(pick, BUFF_RAW) > 0 then
        else
            set dummy = CreateUnit(GetOwningPlayer(i.shard), DUMMY, 0., 0., 0.)
            call UnitAddAbility(dummy, FROST_NOVA)
            call IssueTargetOrder(dummy, "frostnova", pick)
            call UnitApplyTimedLife(dummy, 'BTLF', 2.)
        endif
        call UnitDamageTarget(i.shard, pick, 10., false, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_MAGIC, null)
    endif
    set dummy = null
    set pick = null
    return false
endfunction

private function Shards takes nothing returns boolean
    local Data d = TT_GetData()
    local Shard i = Shard.create()
    local real x 
    local real y 
    local unit shard
    local location loc
    local location move 
    if GetWidgetLife(d.missile) > 0.405 then
        set x = GetUnitX(d.missile)
        set y = GetUnitY(d.missile)
        set i.shard = CreateUnit(GetOwningPlayer(d.missile), BOLT_RAW, x, y, GetRandomReal(1, 360))
        set loc = GetUnitLoc(i.shard)
        set x = GetLocationX(loc) + 850. * Cos(GetUnitFacing(i.shard) * bj_DEGTORAD)
        set y = GetLocationY(loc) + 850. * Sin(GetUnitFacing(i.shard) * bj_DEGTORAD)
        set move = Location(x, y)
        call IssuePointOrderLoc(i.shard, "move", move)
        call RemoveLocation(move)
        call RemoveLocation(loc)
        
        call TriggerRegisterUnitInRange(i.trig, i.shard, S_RADIUS, null)
        call TriggerAddCondition(i.trig, Condition(function ShardDmg))
        call SetTriggerStructA(i.trig, i)
        call TT_Start(function RemoveShard, i)
        call UnitApplyTimedLife(i.shard, 'BTLF', GetRandomReal(0.4, 1.2))
    else
        return true
    endif
    set shard = null
    set loc = null
    set move = null
    return false
endfunction

private function Actions takes nothing returns nothing
    local unit cast = GetTriggerUnit()
    local location targ = GetSpellTargetLoc()
    local real x = GetUnitX(cast)
    local real y = GetUnitY(cast)
    local real dx = GetLocationX(targ) - x
    local real dy = GetLocationY(targ) - y
    local real distance = RMinBJ(MAX_DIST, SquareRoot(dx * dx + dy * dy))
    local real angle = Atan2(dy, dx)
    
    local Data d = Data.create()
    set d.missile = CreateUnit(GetOwningPlayer(cast), MISSILE_RAW, x, y, GetUnitFacing(cast))
    set d.ticks = R2I(distance / (SPEED * TT_PERIOD))
    set d.dx = (SPEED * TT_PERIOD) * Cos(angle)
    set d.dy = (SPEED * TT_PERIOD) * Sin(angle)
    call RemoveLocation(targ)
    
    call TT_Start(function Move, d)
    call TT_Start(function Shards, d)

    call TriggerRegisterUnitInRange(d.trig, d.missile, M_RADIUS, null)
    call TriggerAddCondition(d.trig, Condition(function Damage))
    call SetTriggerStructA(d.trig, d)
    set cast = null
    set targ = null
endfunction

function InitTrig_FrozenOrb takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
    call TriggerAddCondition(t, Condition(function Conditions))
    call TriggerAddAction(t, function Actions)
endfunction

endscope


Don't worry, it works fine, I just wanna know if it can be optimized more :D
 

Tom Jones

N/A
Reaction score
437
How about using GetUnitX instead of GetLocationX(Unit loc)? Saves you a bunch of location calls and removals.
 

Cohadar

master of fugue
Reaction score
209
Why are you using ABC timer functions when you already use ABCT?

And if you are going with low periods like 0.03125 better use TT.
 

cr4xzZz

Also known as azwraith_ftL.
Reaction score
51
> And if you are going with low periods like 0.03125 better use TT.

Yeah, was thinking of that.

> How about using GetUnitX instead of GetLocationX(Unit loc)? Saves you a bunch of location calls and removals.
Had some problems with that in the past and decided not to use it. I'll try it again anyway.

> Why are you using ABC timer functions when you already use ABCT?

Oh right. Forgot that.. ^^

Ok, made some improvements. Anything else to improve?

JASS:

scope FrozenOrb

globals
    private constant integer ABILITY_RAW = 'A000'
    private constant integer MISSILE_RAW = 'h001'
    private constant integer BOLT_RAW = 'h002'
    private constant integer DUMMY = 'h000'
    private constant integer FROST_NOVA = 'A002'
    private constant real MAX_DIST = 1050.
    private constant real SPEED = 425.
    private constant real M_RADIUS = 225.
    private constant real S_RADIUS = 100.
endglobals

private struct Data
    unit missile
    integer ticks
    real dx
    real dy
    trigger trig = CreateTrigger()
    
    method onDestroy takes nothing returns nothing
        call ClearTriggerStructA(.trig)
        call DestroyTrigger(.trig)
    endmethod
endstruct

private struct Shard
    unit shard
    trigger trig = CreateTrigger()
    
    method onDestroy takes nothing returns nothing
        call ClearTriggerStructA(.trig)
        call DestroyTrigger(.trig)
    endmethod
endstruct

private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == ABILITY_RAW
endfunction

private function Move takes nothing returns boolean
    local Data d = TT_GetData()
    local real x = GetUnitX(d.missile)
    local real y = GetUnitY(d.missile)
    local real dx = x + d.dx
    local real dy = y + d.dy
    call SetUnitPosition(d.missile, dx, dy) 
    set d.ticks = d.ticks - 1
    if d.ticks <= 0 then
        call RemoveUnit(d.missile)
        call d.destroy()
        return true
    endif
    return false
endfunction

private function Damage takes nothing returns boolean
    local Data d = GetTriggerStructA(GetTriggeringTrigger())
    local unit pick = GetTriggerUnit()
    local unit dummy = CreateUnit(GetOwningPlayer(d.missile), DUMMY, 0., 0., 0.)
    if IsUnitEnemy(pick, GetOwningPlayer(d.missile)) and GetWidgetLife(pick) > 0.405 and not IsUnitType(pick, UNIT_TYPE_STRUCTURE) then
        call UnitDamageTarget(d.missile, pick, 75., false, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_MAGIC, null)
    endif
    call UnitAddAbility(dummy, FROST_NOVA)
    call IssueTargetOrder(dummy, "frostnova", pick)
    call UnitApplyTimedLife(dummy, 'BTLF', 2.)
    set dummy = null
    set pick = null
    return false
endfunction

private function RemoveShard takes nothing returns boolean
    local Shard i = TT_GetData()
    if GetWidgetLife(i.shard) < 0.405 then
        call RemoveUnit(i.shard)
        call i.destroy()
        return true 
    endif
    return false
endfunction

private function ShardDmg takes nothing returns boolean
    local Shard i = GetTriggerStructA(GetTriggeringTrigger())
    local unit pick = GetTriggerUnit()
    local unit dummy = CreateUnit(GetOwningPlayer(i.shard), DUMMY, 0., 0., 0.)
    if IsUnitEnemy(pick, GetOwningPlayer(i.shard)) and GetWidgetLife(pick) > 0.405 and not IsUnitType(pick, UNIT_TYPE_STRUCTURE) then
        call UnitDamageTarget(i.shard, pick, 75., false, false, ATTACK_TYPE_MAGIC, DAMAGE_TYPE_MAGIC, null)
    endif
    call UnitAddAbility(dummy, FROST_NOVA)
    call IssueTargetOrder(dummy, "frostnova", pick)
    call UnitApplyTimedLife(dummy, 'BTLF', 2.)
    set dummy = null
    set pick = null
    return false
endfunction

private function Shards takes nothing returns boolean
    local Data d = TT_GetData()
    local Shard i = Shard.create()
    local real x 
    local real y 
    local unit shard
    local location loc
    local location move 
    if GetWidgetLife(d.missile) > 0.405 then
        set x = GetUnitX(d.missile)
        set y = GetUnitY(d.missile)
        set i.shard = CreateUnit(GetOwningPlayer(d.missile), BOLT_RAW, x, y, GetRandomReal(1, 360))
        set loc = GetUnitLoc(i.shard)
        set x = GetLocationX(loc) + 850. * Cos(GetUnitFacing(i.shard) * bj_DEGTORAD)
        set y = GetLocationY(loc) + 850. * Sin(GetUnitFacing(i.shard) * bj_DEGTORAD)
        set move = Location(x, y)
        call IssuePointOrderLoc(i.shard, "move", move)
        call RemoveLocation(move)
        call RemoveLocation(loc)
        
        call TriggerRegisterUnitInRange(i.trig, i.shard, S_RADIUS, null)
        call TriggerAddCondition(i.trig, Condition(function ShardDmg))
        call SetTriggerStructA(i.trig, i)
        call TT_Start(function RemoveShard, i)
        call UnitApplyTimedLife(i.shard, 'BTLF', GetRandomReal(0.4, 1.2))
    else
        return true
    endif
    set shard = null
    set loc = null
    set move = null
    return false
endfunction

private function Actions takes nothing returns nothing
    local unit cast = GetTriggerUnit()
    local location targ = GetSpellTargetLoc()
    local real x = GetUnitX(cast)
    local real y = GetUnitY(cast)
    local real dx = GetLocationX(targ) - x
    local real dy = GetLocationY(targ) - y
    local real distance = RMinBJ(MAX_DIST, SquareRoot(dx * dx + dy * dy))
    local real angle = Atan2(dy, dx)
    
    local Data d = Data.create()
    set d.missile = CreateUnit(GetOwningPlayer(cast), MISSILE_RAW, x, y, GetUnitFacing(cast))
    set d.ticks = R2I(distance / (SPEED * TT_PERIOD))
    set d.dx = (SPEED * TT_PERIOD) * Cos(angle)
    set d.dy = (SPEED * TT_PERIOD) * Sin(angle)
    call RemoveLocation(targ)
    
    call TT_Start(function Move, d)
    call TT_Start(function Shards, d)

    call TriggerRegisterUnitInRange(d.trig, d.missile, M_RADIUS, null)
    call TriggerAddCondition(d.trig, Condition(function Damage))
    call SetTriggerStructA(d.trig, d)
    set cast = null
    set targ = null
endfunction

function InitTrig_FrozenOrb takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
    call TriggerAddCondition(t, Condition(function Conditions))
    call TriggerAddAction(t, function Actions)
endfunction

endscope

EDIT: Oh wait, forgot to replace getlocationx with getunitx... xD
EDIT2: Done.
 

Cohadar

master of fugue
Reaction score
209
JASS:

call RemoveUnit(d.missile)

Put that inside onDestroy

Also you should not create dummy casters all the time.

You can create one dummy caster put it into struct and than make it cast nova multiple times. (just make sure you set cooldown to zero)
That will save a lot of lag...
 

cr4xzZz

Also known as azwraith_ftL.
Reaction score
51
> That will save a lot of lag...
Thanks. I made it cast frost nova only if the target doesn't have buff slowed. +rep u guys if I can
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top