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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top