• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

IsUnitInRange - Theoretical questions to increase efficiency

Status
Not open for further replies.
Level 7
Joined
Apr 5, 2011
Messages
245
(Multiple questions throughout the post)

I wanna talk about IsUnitInRange function.

Question 1: I should always check if IsUnitInRect before (to prevent a lot of calculations), do I?

Question 2: I should always use squared range constants if calculate distance by geometry (to not use square root), do I?

Question 3: How works UnitInRange event? Does it calculate distances periodically (with rects probably) or something kind of ... smart?

Then I got an idea, that needs to be discussed as well.
In most cases this function uses .../600/700/800/...10N range. There is no real need in more sharpness for most triggered spells.
So, there is an idea to create a function that determines result approximately (but still with high accuracy).
1. Function has a spectre of 100/200/300/.../5000 distances
(50 values)
Let's call this an integer array R[50], R = 100 * i, i = 1..50
2. There is a constant (in that meaning that it is calculated once) real array K[50], K = 100 / R (= 1 / i), i = 1..50
3. There is a boolean array C[2500] that represents an image of 1/4 circle (with some special indexing probably, have not thought about it yet)
4. Condition itself:
JASS:
return C[R2I(Abs(x - x0) * K[Range]), R2I(Abs(y - y0) * K[Range])]
(C array supposed to have simple 2D indexing here)

Question 4: Is this viable idea?
 
Last edited:
Level 21
Joined
Aug 21, 2005
Messages
3,699
You're asking for implementation details to which only blizzard knows the answer. Anything in my post is purely speculation based on some experience in the field.

Question 1: No. IsUnitInRange should be faster than IsUnitInRect for several reasons:
- IsUnitInRange is native, and thus uses native arithmetic. IsUnitInRect is going to be implemented in jass (unless you're talking about IsUnitInRegion)
- IsUnitInRange is just a glorified version of "DistanceBetweenPoints", with the main difference being that IsUnitInRange doesn't use square root. It should be faster than checking if a point is in a rect in any case.

Question 2: Yes. Again, note that IsUnitInRange probably doesn't use square root, unless blizzard is the first game developer in the history of the earth to do so.

Question 3: Every game iteration (60 times per second?), the game checks if any events have triggered. In case of UnitInRange, it simply calculates the distance between units. It obviously depends on the event.
<specific unit> in range of <specific unit> - this probably just uses a single IsUnitInRange call, in addition to checking if the unit wasn't already in range 1 iteration ago.
<generic unit> in range of <specific unit> - this probably uses GroupEnumUnitsInRange, with a filter checking if a unit was already in range 1 iteration ago.

Please note that these things depend on the internal mechanics of the engine. No matter what you do, it's very unlikely that you'll be able to beat the performance of a c++ game engine with some clever jass trick.

Question 4: No.
 
Level 7
Joined
Apr 5, 2011
Messages
245
Thanks for the "speculation". :O
You probably right, there is no need to check rects since it with ~99% probability uses internal check.
To be honest, I forgot that IsUnitInRange function really exist (and wrote ugly code with DistanceBetweenBlablabla (my own function)), I was talking about it just in theory. :D:D:D
 
Status
Not open for further replies.
Top