Snippet GetNearestUnit

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
>An instant casted Chain Lightning which jumps thorugh each unit, from the closest to the farthest.

Well, the way I see it a chain lightning works basiclly in the following way:

1. a [ljass]lightning[/ljass] effect is created ruffly(maybe it has some z for example) from the origin of the caster to the spell target unit.
2. if number of jumps/bounces of the lightning > 0 go to 2.1. else END
---- 2.1. find the closest unit (matching some conditions ex: enemy) which is also not equall to the last spell-target/jump-target unit (otherwise the chain lightning will just hit the first spell target unit multiple times), decrement bounces and go to 2.

My point is that you always have to call GetNearestUnitEx(..., 1), in order for the spell to work properly because not all of the targets of the chain lightning would be located within the initial range of the spell, and this means that the sorting is pretty useless in this case because you don't actaully need GetNearstUnitEx(..., n).

A good example of where the sorting might do well (and no I am not using my imagiantion I am recalling from memory) would be the Healing Wave spell of the Head Hunter (one of orc's heros), the way it works is that after the initial healing of the spell target unit it jumps/finds the unit with least amount of hp and from there it jumps again and again, although the same thing applies here that you still need to call GetLowestHpUnit(..., 1) which again means the sorting is pretty useless.
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
@Sgqvur

Dear mate, your coding is too ugly.....keep variable names more human.
Also this:
JASS:
set R1 = $x$ - GetUnitX(U0)
set R2 = $y$ - GetUnitY(U0)
set R3 = R1 * R1 + R2 * R2

is slower than this:
JASS:
set de=(GetUnitX(e)-x)*(GetUnitX(e)-x)+(GetUnitY(e)-y)*(GetUnitY(e)-y)


Sometimes speed doesn't matter when it comes to understanding.
If you want the fastest coding, work with "0/1"
 

lep

Active Member
Reaction score
8
jass helper updated?
Try it yourself. Or tell me where that [ljass]this[/ljass] comes from in the method shown below taken from the op.
Or tell me where it returns some value of type [ljass]thistype[/ljass].
JASS:


        static method for takes real x, real y, real r, boolexpr c returns thistype
            local List node
            local unit e
            call GroupEnumUnitsInRange(bj_lastCreatedGroup,x,y,r,c)
            loop
                set e = FirstOfGroup(bj_lastCreatedGroup)
                exitwhen e==null
                set node = List.allocate()
                set node.unit = e
                set node.distance = (GetUnitX(e)-x)*(GetUnitX(e)-x)+(GetUnitY(e)-y)*(GetUnitY(e)-y)
                call base.insertNode(node)
                call GroupRemoveUnit(bj_lastCreatedGroup,e)
            endloop
            call sort(this)
            set e = null
        endmethod


Also: 0.A.2.B.
 

Dirac

22710180
Reaction score
147
My bad, forgot to change the "returns thistype" to "returns nothing" from the previous version.
Code updated with the fix, it now compiles
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
>Dear mate, your coding is too ugly.....keep variable names more human.

You mean more human-readable? The point of the script was:
"Well, actually the fastest way to do it, that I know of would be this:"
For whatever/uknown reason Jass2/bytecode or something executes slower with longer variable names.

>Also this:
Jass:

set R1 = $x$ - GetUnitX(U0)
set R2 = $y$ - GetUnitY(U0)
set R3 = R1 * R1 + R2 * R2

is slower than this:
set de=(GetUnitX(e)-x)*(GetUnitX(e)-x)+(GetUnitY(e)-y)*(GetUnitY(e)-y)


What?! No offence but lets try to count the operations, mate:

3 liner:
asignments = 3
additions = 1
subtractions = 2
multiplication = 2
natives called = 2
total = 10 (of which only two function calls, i.e the slowest operations in this case)

1 liner:
asignments = 1
additions = 1
subtractions = 4
multiplication = 2
natives called = 4
total = 12 (of which 4 are function calls)


=)
 

Dirac

22710180
Reaction score
147
You forgot local declaration or global read, which are actually slower
And I have proof

But in this case declaring locals to operate might increase effiency.
When i have the time i'll update.
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
@Sgqvur

"For whatever/uknown reason Jass2/bytecode or something executes slower with longer variable names."
Dude you act like getting a intel core i7-3960X processor to have better experience in microsoft word.

Well your idea is making 10 actions and my 12......so what. I don't use locals to declare, and the assignments and maths are done in the native lang and not in jass.
 

Laiev

Hey Listen!!
Reaction score
188
When you call the same function multiply times in a row (like [ljass]GetUnitX[/ljass] in the example) is more efficient and faster you store it in a variable and use the variable.
 

lep

Active Member
Reaction score
8
The fastest, easiest and with more flexible way to retrieve the nearest unit from a point

[…]

It is nice that you reuse other libraries but this really is not the fastest way to get the unit(s).
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      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