Snippet Units in sector

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,497
Sector:
Imagine a cake, the round type,
cut it into pieces, taking care to cut through the center,
take one piece,
look at it from the top.
That's a sector.

Still not seeing it? Here's an image.


JASS:
library FILTERS initializer Init
globals
    boolexpr FILTER_True
endglobals
private function ReturnTrue takes nothing returns boolean
    return true
endfunction
private function Init takes nothing returns nothing
    set FILTER_True = Condition(function ReturnTrue)
endfunction
endlibrary
JASS:
// whichGroup: the group the units will be added to
// xc, yc: the starting point, sort of like the center of the circle
// facing: the direction the "middle" of the sector goes to, in radians
// radius: the "length" of the sector
// angle: the maximum opening angle of the sector, in radians
// filter: the condition function to apply to the units in the sector.
function GroupEnumUnitsInSector takes group whichGroup,real xc,real yc,real facing,real radius,real angle,boolexpr filter returns nothing
    local group g = CreateGroup()
    local unit u
    local real x
    local real y
    local real x1
    local real y1
    local real x2
    local real y2

    call GroupEnumUnitsInRange(g, xc, yc, radius, filter)

    set angle = angle * 0.5
    set x1 = Cos(facing - angle)
    set y1 = Sin(facing - angle)
    set x2 = Cos(facing + angle)
    set y2 = Sin(facing + angle)

    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        call GroupRemoveUnit(g, u)

        set x = GetUnitX(u) - xc
        set y = GetUnitY(u) - yc
        if y * x1 >= x * y1 and y * x2 <= x * y2 then
            call GroupAddUnit(whichGroup, u)
        endif
    endloop

    call DestroyGroup(g)
    set g = null
endfunction

// point: the starting point, sort of like the center of the circle
// facing: the direction the "middle" of the sector goes to, in degrees
// radius: the "length" of the sector
// angle: the maximum opening angle of the sector, in degrees
// filter: the condition function to apply to the units in the sector. Pass null for all units
function GetUnitsInSector takes location point,real facing,real radius,real angle,boolexpr filter returns group
    local group g = CreateGroup()
    if filter == null then
        set filter = FILTER_True
    endif
    call GroupEnumUnitsInSector(g, GetLocationX(point), GetLocationY(point), facing * bj_DEGTORAD, radius, angle * bj_DEGTORAD, filter)
    return g
endfunction



Yes, I actually think it works...

The demo map is simple to use, select the Blood Mage, move somewhere, press "escape".
The list of names you get would be the units within 400 distance and 60 degrees in front.
Yes, it includes the Mage... that's because the filter function simply picks anything...
 

Attachments

  • UnitsInSector_Demo_AceHart.w3x
    14.8 KB · Views: 353

Samael88

Evil always finds a way
Reaction score
181
Thanks Acehart:) This will really come in handy:D
Keep up the good work:thup:

Edit: Does this require vJass or is it standard Jass?
 

Dameon

"All the power in the world resides in the eyes"
Reaction score
127
So put simply this enables you to chose unit's in certain view points, mainly in front, to the left, to the right and behind. I would have never thought of doing a trigger like this but I can think of a lot of uses for it, thanks ace.

By the way is this possible in MUI?
 

Dr.Jack

That's Cap'n to you!
Reaction score
108
Refreshingly simple and useful, good job, thanks for sharing.

A small question if you don't mind.
As I see it ReturnTrue serves as the conditions when the group is created. That make sense, but how about letting the players give their own set of conditions? You could always take another boolexpr parameter. Obviously one could always simply loop and use If/Then/Else action, but I believe just giving boolexpr parameter would ease things.

Do correct me if I'm wrong.
 

Don

Rise with the Fallens!
Reaction score
52
Nice system, this surely will make it into one of my map, i love it, good job!
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,497
Just the sector functions, or the library too?


The library is only there to provide "FILTER_True".
Unfortunately, that means you need NewGen.

Alternatively, if you don't want, or can not, use NewGen, add a function like
JASS:
function ReturnTrue takes nothing returns boolean
    return true
endfunction


And replace "FILTER_True" with "Condition(function ReturnTrue)".
 

black.sheep

Active Member
Reaction score
24
So is supposed to look like this? Tell me what im doing wrong
Code:
function GetUnitsInSector takes location point,real facing,real radius,real angle,boolexpr filter returns group
    local group g = CreateGroup()
    if filter == null then
        Condition(function ReturnTrue)
    endif
    call GroupEnumUnitsInSector(g, GetLocationX(point), GetLocationY(point), facing * bj_DEGTORAD, radius, angle * bj_DEGTORAD, filter)
    return g
endfunction

function ReturnTrue takes nothing returns boolean
    return true
endfunction
 

Viikuna

No Marlo no game.
Reaction score
265
Your function creates groups every time its called, so its slower than it should be + it leaks.

This should be fixxed. I suggest to make it require GroupUtils, and use ENUM_GROUP for GroupEnum calls..

Also in that GetUnitsInSector function you should replace CreateGroup with NewGroup.


edit. Ok, this is thread pretty old. Just noticed. Well, anyways, I kinda wonder if AceHart is still around..
 
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