Snippet Units in sector

AceHart

Your Friendly Neighborhood Admin
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

Samael88

Evil always finds a way
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"
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!
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!
Nice system, this surely will make it into one of my map, i love it, good job!
 

AceHart

Your Friendly Neighborhood Admin
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
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.
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.
  • Ghan Ghan:
    The old shoutbox wasn't supported anymore. We updated xenForo, so it had to be replaced.
  • jonas jonas:
    let's see if everyone finds it... the nice thing about the shoutbox was I could check on it even while logged out, but the existence of this one is hidden when you're not logged in
  • Ghan Ghan:
    We can fix that.
  • Ghan Ghan:
    Chat should show on the sidebar when not logged in now.
  • Ghan Ghan:
    (You'll still need to log in to post messages)
  • Ghan Ghan:
    Test!
  • tom_mai78101 tom_mai78101:
    I must be in a test server.
  • tom_mai78101 tom_mai78101:
    Nice, Twitter tweets embedding now works
  • Wizard Wizard:
    Yup.
  • Ghan Ghan:
    Excellent.
  • Ghan Ghan:
    @tom_mai78101 Hello there.
  • Ghan Ghan:
    Tagging works in the chat too.
  • tom_mai78101 tom_mai78101:
    @Ghan Missed it.
  • Wizard Wizard:
    Still fixing things here and there. Added widgets to the portal, will make it match the ones here on the forum index tomorrow.
  • Ghan Ghan:
    The venerable World Editor Tutorials site has been converted to HTTPS at last.
  • jonas jonas:
    cool
  • jonas jonas:
    and I can even edit my messages, nice
  • seph ir oth seph ir oth:
    GENERAL CHIT CHAT, YOU ARE A BOLD ONE
  • Ghan Ghan:
    Hello there
  • The Helper The Helper:
    this new chatbox is great and the forum software update is great too
    +1
  • The Helper The Helper:
    upgrade has fixed forum registration spam problem
  • tom_mai78101 tom_mai78101:
    Something tells me we might be able to customize the chatbox a bit, considering that there's a gap under every message.
  • Wizard Wizard:
    Going to deploy a fix soon, just had to take some time for myself this weekend.
  • Varine Varine:
    Unbelievable. Time for yourself? How dare you!

    Members online

    No members online now.

    Affiliates

    Hive Workshop
    Top