Optimize jass code

muzk

Member
Reaction score
3
Hello, I did my first spell in jass , I neeed help to improve/optimize this code.

General Idea about the spell:
Ok, caster cast the spell targering a unit then in the unit position appears a tree (dummy unit)(this tree stats with a size of (0,0,0) in 1 second it grows to (1.2,1.2,1.2)) while caster is doing his spell animation he appears at target location.

General Idea About the code:
-Caster cast the spell (spell animation = stand)
-Hide caster
-Create a dummy unit at caster location looking at target location (dummy unit is visible and uses caster model)
-Move instantly caster to target location looking at target
-Play dummy spell animation
-Change the visibility of Caster to 0
-Unhide caster
-Change the visibility of dummy from 255 to 0 and caster fomr 0 to 255
-Change tree size from (1.2,1.2,1.2) to (0,0,0)

Code:
JASS:

//Timers Data Field 0 = tree
//Timers Data Field 1 = caster copy
//Timers Data Field 2 = caster
//Timers Data Field 3 = real//100
//Timers Data Field 4 = real
//'A000'= spell
//v1,v2 auxiliar variables to change UnitVertexColor
//c, auxiliar varbiable to change tree size
//'H002' = tree , 'H001' = caster

function Trig_SPELLCond takes nothing returns boolean
    return GetSpellAbilityId() == 'A000'
endfunction

function GrowUp takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer i = GetHandleId(t)
    local unit tree = LoadUnitHandle(udg_GlobalHashtable, i, 0)
    local unit casterCopy = LoadUnitHandle(udg_GlobalHashtable, i, 1)
    local unit caster = LoadUnitHandle(udg_GlobalHashtable, i, 2)
    local real v1 = LoadReal(udg_GlobalHashtable, i, 3)
    local real v2 = 100-v1
    local real c = LoadReal(udg_GlobalHashtable, i, 4)
    //Unhide Caster
    if (c==0.2) then 
      call ShowUnit(Caster, true) 
    endif
    //Change size of tree, visibility of caster and casterCopy
    call SetUnitScale(tree,c,c,c)
    call SetUnitVertexColor(caster, 255,255,255,PercentToInt(v2, 255))
    call SetUnitVertexColor(casterCopy, 255,255,255,PercentToInt(v1, 255))

    if (v1<=0) then
      call FlushChildHashtable(udg_GlobalHashtable,i)
      call PauseTimer(t)
      call DestroyTimer(t)
    else
      call SaveUnitHandle(udg_GlobalHashtable, i, 0, arbol)
      call SaveUnitHandle(udg_GlobalHashtable, i, 1, casterCopy)
      call SaveUnitHandle(udg_GlobalHashtable, i, 2, caster)
      call SaveReal(udg_GlobalHashtable, i, 3, v1-10)
      call SaveReal(udg_GlobalHashtable, i, 4, c+0.1)
    endif    
endfunction


function GrowDown takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer i = GetHandleId(t)
    local unit tree = LoadUnitHandle(udg_GlobalHashtable, i, 0)
    local real c = LoadReal(udg_GlobalHashtable, i, 1) - 0.1
    //Decreases tree size  
    call SetUnitScale(tree,c,c,c)
    if (c==0) then
      call FlushChildHashtable(udg_GlobalHashtable,i)
      call PauseTimer(t)
      call DestroyTimer(t)
    else
      call SaveUnitHandle(udg_GlobalHashtable, i, 0, tree)
      call SaveReal(udg_GlobalHashtable, i, 1, c)
    endif 
endfunction

function Trig_SPELL_Actions takes nothing returns nothing
    //variables
    local timer t
    local timer t1
    local integer i
    local integer i1
    local unit caster = GetTriggerUnit()
    local unit target = GetSpellTargetUnit()
    local unit casterCopy
    local unit tree
    local real Angle = bj_RADTODEG * Atan2(GetUnitY(caster) - GetUnitY(carget), GetUnitX(caster) - GetUnitX(target))
    local location tempPoint1 = GetUnitLoc(caster)
    local location tempPoint2 = GetUnitLoc(carget)
    local location tempPoint3 = Location(GetUnitX(target)-90*Cos(Angle*bj_DEGTORAD),GetUnitY(target)-90*Sin(Angle*bj_DEGTORAD))//Point where tree is going to appear
    call TriggerSleepAction(0.02)
    call SetUnitFacing(target, AngleBetweenPoints(tempPoint2, tempPoint1))//makes target look at caster
    call ShowUnit(caster, false)//hide caster
    call SetUnitVertexColor(caster, 255,255,255,0)//change caster visibilty to o
    call SetUnitPositionLoc(caster, tempPoint2)//move caster near to target
    call SetUnitFacing(caster, AngleBetweenPoints(GetUnitLoc(caster), tempPoint2))//makes caster look at target    

    set casterCopy = CreateUnitAtLoc(GetOwningPlayer(caster),'h001',tempPoint1,AngleBetweenPoints(tempPoint1, tempPoint2))//creates dummy unit 
    set tree = CreateUnitAtLoc(GetOwningPlayer(caster),'h002',tempPoint3,.0)//creates tree
    call SetUnitScale(tree,.0,.0,.0)//change size of  tree to 0
    call UnitApplyTimedLife(tree,'BTLF',4.00)
    call UnitApplyTimedLife(tree,'BTLF',4.00)
    call SetUnitAnimation(casterCopy,"spell")//plays casterCopy spell animation
    call TriggerSleepAction(1)

    set t = CreateTimer()
    set i = GetHandleId(t)
    call SaveUnitHandle(udg_GlobalHashtable, i, 0, tree)
    call SaveUnitHandle(udg_GlobalHashtable, i, 1, casterCopy)
    call SaveUnitHandle(udg_GlobalHashtable, i, 2, caster)
    call SaveReal(udg_GlobalHashtable, i, 3, 100)
    call SaveReal(udg_GlobalHashtable, i, 4, 0.2)
    call TimerStart(t,0.1,true,function GrowUp)
    call PolledWait(2)

    set t1 = CreateTimer()
    set i1 = GetHandleId(t1)
    call SaveUnitHandle(udg_GlobalHashtable, i1, 0, tree)
    call SaveReal(udg_GlobalHashtable, i1, 1, 1.2)
    call TimerStart(t1,0.1,true,function GrowDown)

    set arbol = null
    set copy = null
    set Caster = null
    set Target = null
    set t = null
    set t1 = null
    call RemoveLocation(tempPoint1)
    call RemoveLocation(tempPoint2)
    call RemoveLocation(tempPoint3)
    set tempPoint1 = null
    set tempPoint2 = null
    set tempPoint3 = null

endfunction

function InitTrig_SPELL takes nothing returns nothing
    set gg_trg_SPELL = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Ulti, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( gg_trg_SPELL, Condition( function Trig_SPELLCond ) )
    call TriggerAddAction( gg_trg_SPELL, function Trig_SPELL_Actions )
endfunction


OOOps used a polled wait :thdown: I have to change it.

This spell works good, but I dont know if it will cause leaks/lag.

Is this code ok or I should change it?
 

Hatebreeder

So many apples
Reaction score
381
Why use udg_?
You have the globals Block :D

Hmmm... If you are using Timers, I'd recomend T32 for handling (for Spells, mostly)

use [ljass] Atan2(PointBY - PointBX, PointAY - PointAX) [/ljass] instead of the Angle BJ
 
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