Spell from DotA to practice vJASS. It's fine?

Zeth

Member
Reaction score
3
Well, I'm learning vJass and i make an spell from DotA, Elune's Arrow of Priestess of the moon.

Does it have any leak? Or something else I should change?

Map download

Here's the code:

JASS:
scope Elune initializer Init

globals


    private            hashtable ht         = InitHashtable()
    private  constant  integer   Spell_Id   = 'A001' // Spell based on Channel.
    private  constant  integer   Stun_Id    = 'A000' // Stun for the dummy, bassed on thunderbolt.
    private  constant  integer   Arrow_Id   = 'h001' // Arrow.
    private  constant  integer   Dummy_Id   = 'h000' // Dummy that causes the damage and stun.
    private  constant  real      ArrowSpeed = .03    // Velocity of the arrow (basically, callback interval), don't put more than .04 (higger number, lower speed)
    private  constant  real      Collision  = 80.    // Radius of collision of the arrow.
    private  constant  integer   Max_Range  = 3000   // Distance traveled by the arrow
    
    
endglobals

private function Elune_Filter takes nothing returns boolean
    local timer t=GetExpiredTimer()
    local data d=LoadInteger(ht,0,GetHandleId(t))
    local unit stunner
    local unit stunned=GetFilterUnit()
    local player p=GetOwningPlayer(d.caster)
    local integer array D
    if IsUnitEnemy(stunned,p) and GetWidgetLife(stunned)>.405 and not IsUnitType(stunned,UNIT_TYPE_STRUCTURE) then
        set stunner=CreateUnit(p,Dummy_Id,GetUnitX(d.arrow),GetUnitY(d.arrow),0.)
        call UnitApplyTimedLife(stunner,'BTLF',.5)
        
        // Calculating the duration of the stun.
        set D[0]=R2I(d.distance/150)
        set D[1]=10
        if D[0]<D[1] then
            call SetUnitAbilityLevel(stunner,Stun_Id,D[0])
        else
            call SetUnitAbilityLevel(stunner,Stun_Id,D[1])
        endif
        //------------

        call IssueTargetOrder(stunner,"thunderbolt",stunned)
        call UnitDamageTarget(stunner,stunned,GetUnitAbilityLevel(d.caster,Spell_Id)*90.,true,false,ATTACK_TYPE_NORMAL,DAMAGE_TYPE_MAGIC,null)
        call KillUnit(d.arrow)
        //call RemoveUnit(d.arrow) ???
        call RemoveSavedInteger(ht,0,GetHandleId(t))
        call d.destroy()
        call PauseTimer(t)
        call DestroyTimer(t)
    endif
    set t=null
    set stunned=null
    set stunner=null
    set p=null
    return false
endfunction

private function Elune_Callback takes nothing returns nothing
    local timer t=GetExpiredTimer()
    local data d=LoadInteger(ht,0,GetHandleId(t))
    local real nx=GetUnitX(d.arrow)+30.*Cos(d.angle)
    local real ny=GetUnitY(d.arrow)+30.*Sin(d.angle)
    local rect max=bj_mapInitialPlayableArea
    call SetUnitPosition(d.arrow,nx,ny)
    set d.distance=d.distance+30
    call GroupEnumUnitsInRange(d.g,nx,ny,Collision,Filter(function Elune_Filter))
    if d.distance>=Max_Range or GetUnitY(d.arrow)>=GetRectMaxY(max) or GetUnitY(d.arrow)<=GetRectMinY(max) or GetUnitX(d.arrow)>=GetRectMaxX(max) or GetUnitX(d.arrow)<=GetRectMinX(max)  then
        call KillUnit(d.arrow)
        //call RemoveUnit(d.arrow)??
        call RemoveSavedInteger(ht,0,GetHandleId(t))
        call d.destroy()
        call PauseTimer(t)
        call DestroyTimer(t)
    endif
    set max=null
    set t=null
endfunction

struct data

    unit caster
    unit arrow
    real angle
    integer distance=50
    group g=CreateGroup()
    timer t=CreateTimer()
    
    static method create takes nothing returns thistype
        local data this=.allocate()
        local real sx=GetSpellTargetX()
        local real sy=GetSpellTargetY()
        local real cx
        local real cy
        set .caster=GetTriggerUnit()
        set cx=GetUnitX(.caster)
        set cy=GetUnitY(.caster)
        set .angle=Atan2(sy-cy,sx-cx)
        set .arrow=CreateUnit(GetOwningPlayer(.caster),Arrow_Id,cx,cy,.angle*57.2957795)
        call SaveInteger(ht,0,GetHandleId(.t),this)
        call TimerStart(.t,ArrowSpeed,true,function Elune_Callback)
        return this
    endmethod
    
    method onDestroy takes nothing returns nothing
        set .caster=null
        set .arrow=null
        set .g=null
        set .t=null
    endmethod
    
endstruct

private function Elune_Conditions takes nothing returns boolean
    if GetSpellAbilityId()==Spell_Id then
    call data.create()
    endif
    return false
endfunction
    
private function Init takes nothing returns nothing
    local integer i=0 
    local trigger t=CreateTrigger()
    
    // Bye bye, BJ.
    loop
      call TriggerRegisterPlayerUnitEvent(t, Player(i),EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
      set i=i+1
      exitwhen i==bj_MAX_PLAYER_SLOTS
    endloop
    // ---------
    
    call TriggerAddCondition(t,Condition(function Elune_Conditions))
    set t=null
endfunction

endscope
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
1) Use a proper attaching system.
Spamming hashtable is not a good idea.
This causes bad data management, hashtable is not fully utilized.
2) Recycle timers.
3) Use a global dummy to stun unit.
4) Use SetUnitX/Y instead of position, faster by 2700%
5) The formula for stun duration is maxDistance/currentDistance.
6) Not all BJ are evil. BJ function for spell initialization is fine. Your init function is just a simple c'n'p of BJ function, same. Why not just use BJ for better readability.
7) I think the units in range when arrow is collided will be stunned. Just group them up and use FirstOfGroup/GroupPickRandomUnit to get the unit for stunning.
8) Follow proper variable naming convention. GLOBALS, Dynamic globals, structMembers.
 

Zeth

Member
Reaction score
3
1-2) Ok....... I used TimerUtils.
3) Why?
4)Done.
5) It's the same.
6) Ok.....
7) In DotA just hit one unit.
8) Ouch, this was just for practice, anyway, i'll keep that in mind.

JASS:
    scope Elune initializer Init

    globals


        private  constant  integer   Spell_Id   = 'A001'
        private  constant  integer   Stun_Id    = 'A000'
        private  constant  integer   Arrow_Id   = 'h001'
        private  constant  integer   Dummy_Id   = 'h000'
        private  constant  real      ArrowSpeed = .025   
        private  constant  real      Collision  = 80.    
        private  constant  integer   Max_Range  = 3000   
       
       
    endglobals

    private function Elune_Filter takes nothing returns boolean
        local timer t=GetExpiredTimer()
        local data d=GetTimerData(t)
        local unit stunned=GetFilterUnit()
        local player p=GetOwningPlayer(d.caster)
        local integer array D
        if IsUnitEnemy(stunned,p) and GetWidgetLife(stunned)>.405 and not IsUnitType(stunned,UNIT_TYPE_STRUCTURE) then
            set d.stunner=CreateUnit(p,Dummy_Id,GetUnitX(d.arrow),GetUnitY(d.arrow),0.)
            call UnitApplyTimedLife(d.stunner,'BTLF',.5)
           
            set D[0]=R2I(d.distance/150)
            set D[1]=10
            if D[0]<D[1] then
                call SetUnitAbilityLevel(d.stunner,Stun_Id,D[0])
            else
                call SetUnitAbilityLevel(d.stunner,Stun_Id,D[1])
            endif

            call IssueTargetOrder(d.stunner,"thunderbolt",stunned)
            call UnitDamageTarget(d.stunner,stunned,GetUnitAbilityLevel(d.caster,Spell_Id)*90.,true,false,ATTACK_TYPE_NORMAL,DAMAGE_TYPE_MAGIC,null)
            call KillUnit(d.arrow)
            //call RemoveUnit(d.arrow) ???
            call d.destroy()
            call ReleaseTimer(t)
        endif
        set stunned=null
        set p=null
        return false
    endfunction

    private function Elune_Callback takes nothing returns boolean
        local timer t=GetExpiredTimer()
        local data d=GetTimerData(t)
        local real nx=GetUnitX(d.arrow)+30.*Cos(d.angle)
        local real ny=GetUnitY(d.arrow)+30.*Sin(d.angle)
        local rect max=bj_mapInitialPlayableArea
        call SetUnitX(d.arrow,nx)
        call SetUnitY(d.arrow,ny)
        set d.distance=d.distance+30
        call GroupEnumUnitsInRange(d.g,nx,ny,Collision,Filter(function Elune_Filter))
        if d.distance>=Max_Range or GetUnitY(d.arrow)>=GetRectMaxY(max) or GetUnitY(d.arrow)<=GetRectMinY(max) or GetUnitX(d.arrow)>=GetRectMaxX(max) or GetUnitX(d.arrow)<=GetRectMinX(max)  then
            call KillUnit(d.arrow)
            //call RemoveUnit(d.arrow)??
            call d.destroy()
            call ReleaseTimer(t)
            return true
        endif
        set max=null
        return false
    endfunction

    struct data

        unit caster
        unit arrow
        unit stunner
        real angle
        integer distance=50
        group g=CreateGroup()
        timer t
       
        static method create takes nothing returns thistype
            local data this=.allocate()
            local real sx=GetSpellTargetX()
            local real sy=GetSpellTargetY()
            local real cx
            local real cy
            set .t=NewTimer()
            set .caster=GetTriggerUnit()
            set cx=GetUnitX(.caster)
            set cy=GetUnitY(.caster)
            set .angle=Atan2(sy-cy,sx-cx)
            set .arrow=CreateUnit(GetOwningPlayer(.caster),Arrow_Id,cx,cy,.angle*57.2957795)
            call SetTimerData(.t,this)
            call TimerStart(.t,ArrowSpeed,true,function Elune_Callback)
            return this
        endmethod
       
        method onDestroy takes nothing returns nothing
            set .caster=null
            set .arrow=null
            set .g=null
            set .stunner=null
            set .t=null
        endmethod
       
    endstruct

    private function Elune_Conditions takes nothing returns boolean
        if GetSpellAbilityId()==Spell_Id then
        call data.create()
        endif
        return false
    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 Elune_Conditions))
        set t=null
    endfunction

    endscope
 

muzk

Member
Reaction score
3
3) Globadummy is more efficient that just spawn create/kill or remove unit. With a global dummy you can do everything like stuns slows cripples etc using 1 DUMMY ONLY.

hmm isnt necesary but its like a convention: use capital letters for global variables.


30.*Cos(d.angle)
30.*Sin(d.angle)
You can save in your struct these values
eg
JASS:
set d.cos = 30*Cos(angle)
set d.sin= 30*Sin(angle)


You are spamming GetOwningPlayer(d.caster) in your filter , I recomment to save it in your struct too.

For these periods T32 or KT2 is good (as timer attach)

Sae it in your data: GetUnitAbilityLevel(d.caster,Spell_Id)*90
 

Laiev

Hey Listen!!
Reaction score
188
7) Ya, right, what Kingking said is to you group enum units when missile hit someunit and get one unit of that group (just one) and then stun that unit...

in dota, this really happen
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
hmm isnt necesary but its like a convention: use capital letters for global variables.
Better readability, less trouble when editing the spell in future.
 

DioD

New Member
Reaction score
57
CAPITALS is for constants, this let you know, this global declared elsewhere and its same for every system. (like PI, ECF, ARRAYSIZE and soo on)

First Capital for Globals, let you see,Global is Soo Global.

no capitals for params, you dont need to null params

sEcond cApital for lOcals, yOu sHoud nUll lOcals

any naming rule is just for you, but, it provide wide range of bonuses if used, since you always see status of variable.

For code, i suggest to use single timer for all instances of spell, this is truly vJass style, your current implementation is not vJass at all.
 

Zeth

Member
Reaction score
3
7) Ya, right, what Kingking said is to you group enum units when missile hit someunit and get one unit of that group (just one) and then stun that unit...

in dota, this really happen
Is not more optimal if units never enter to group?
 

muzk

Member
Reaction score
3
Also, you used ArrowSpeed like a period (time), and speed is distance/time. You should so something like
JASS:
private constant real SPEED = 700.00
private constant real PERIOD = 0.025

You are leaking a group, you didnt destroy or recycled it. Put it in a global, I think everyone use a global group for things like that.
 

Laiev

Hey Listen!!
Reaction score
188
@Zeth

well, maybe, maybe not... in my map, i found a bug (with a line missile, like Elune Arrow)

when it hit a hero, it should be destroyed but it can hit 2 heroes before the project be removed.

this is the problem :/ it can hit 2 heroes at the same time before the project be removed.
 

muzk

Member
Reaction score
3
@Laiev
Add a boolean (X) to your struct or w/e you was using, put it in your group filter (if not .X) and when your proj hit a unit, set it to true (set .X = true).
I think that would solve your problem.

EDIT
or if you kill your proj, kill it when it hit a unit, so add in your filter the condition proj is alive.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • 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