Cone Filter?

BlueMirage

Trust, but doubt.
Reaction score
39
I've been trying to make my own function that will filter out units in a group that is not within a cone. I probably messed up with the unit groups. I know for sure that it does not even do any actions for the units in the group, because if it would, I would get a BJDebugMsg from my Angle function.

EDIT: I also know that this function manages to remove all units of the group I'm filtering, despite going through 0 of its units. Done by checking how many units are inside the group before and after the filter.

I'm making my own function as I could not get the ones I found on this site working.

JASS:
// This will give you the angle between two points.                 
function Angle takes real x1, real y1, real x2, real y2 returns real
    local real r = bj_RADTODEG * Atan2(y2 - y1, x2 - x1)
    if r < 0 then
        set r = 360 + r
    endif
    call BJDebugMsg(R2S(r))
    return r
endfunction

//Filters out units not in a cone.
function FilterCone takes group whichGroup, real x ,real y, real facing, real angle returns group
    local unit u
    local group g = CreateGroup()
    local group g2 = CreateGroup()
    local real ua //Unit Angle to centre
    
    set g = whichGroup
    set angle = angle / 2

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

        set ua = Angle(x, y, GetUnitX(u), GetUnitY(u))
        if RAbsBJ(facing - ua) > 180 then //Check for the smallest angle between the two (Gives a difference of 100 instead of 260 for example)
            if facing > ua then
                set ua = ua + 360
            else
                set ua = ua - 360
            endif
        endif

        if facing + angle >= ua and facing - angle <= ua then
            call GroupAddUnit(g2, u)
        endif
    endloop

    call DestroyGroup(g)
    set g = null
    return g2
endfunction
 

BlueMirage

Trust, but doubt.
Reaction score
39
Bumpy post

Well, one day and no answer. It doesn't really matter if you can find the problem, just a general idea of what might be wrong will help. Anything is appreciated.
 

BlueMirage

Trust, but doubt.
Reaction score
39
More bumpy. Still no answer.

Done some more testing.
Something is wierd with the following function. I simplified it as much as I could to see if I could get some debug results if I took it step by step. But it fails right at the start.

JASS:
function FilterCone takes group g, real x ,real y, real facing, real angle returns nothing 
    call BJDebugMsg(I2S(CountGroup(g)))
endfunction

This should only display how many units are in the taken group, but for some reason, the taken group goes to 0 instantly when Filtercone is called. I have no idea why.
 

Prozix

New Member
Reaction score
7
Look at this
JASS:

local group g = CreateGroup()

//and this

set g = whichGroup

It's not your problem :p but it doesn't make sense to create a group, then assign the group handle to another group.
You should read the Leakless groups tutorial from jesus4life and rewrite your function. It might even solve your problem too
 

Prozix

New Member
Reaction score
7
It probably doesn't display anything because there is something wrong with your CountGroup function.
Can you post the complete code that fires the functions and the functions themselves you have now?
 

BlueMirage

Trust, but doubt.
Reaction score
39
I use CountGroup earlier in the whole trigger to check how many units are before the filter, so I know that the function works.

I could not get that snipper to work.
 

Prozix

New Member
Reaction score
7
JASS:
library Group
globals
    group GROUP = CreateGroup()
endglobals
endlibrary

JASS:
library ConeFilter initializer init requires Group

struct ConeSector
    real x
    real y
    real radiusSquared
    real facing
    real halfangle
    
    real x1
    real y1
    real x2
    real y2
    
    static method create takes real x, real y, real radius, real facing, real angle returns thistype
        local thistype n = thistype.allocate()
        set n.x = x
        set n.y = y
        set n.radiusSquared = radius * radius
        set n.facing = facing
        set n.halfangle = angle * 0.5
        set n.x1 = Cos(n.facing - n.halfangle)
        set n.y1 = Sin(n.facing - n.halfangle)
        set n.x2 = Cos(n.facing + n.halfangle)
        set n.y2 = Sin(n.facing + n.halfangle)
        return n
    endmethod
    
    method set takes real x, real y, real radius, real facing, real angle returns thistype
        set this.x = x
        set this.y = y
        set this.radiusSquared = radius * radius
        set this.facing = facing
        set this.halfangle = angle * 0.5
        set this.x1 = Cos(this.facing - this.halfangle)
        set this.y1 = Sin(this.facing - this.halfangle)
        set this.x2 = Cos(this.facing + this.halfangle)
        set this.y2 = Sin(this.facing + this.halfangle)
        return this
    endmethod
endstruct

globals
    ConeSector cs
endglobals

private function action takes unit u returns nothing
    call BJDebugMsg(GetUnitName(u))
endfunction

private function filter takes nothing returns boolean
    local unit u = GetFilterUnit()
    local real dx = GetUnitX(u) - cs.x
    local real dy = GetUnitY(u) - cs.y
    if dx * dx + dy * dy <= cs.radiusSquared and cs.x1 >= dx * cs.y1 and dy * cs.x2 <= dx * cs.y2 then
        call action(u)
    endif
    return false
endfunction
    
private function go takes nothing returns nothing
    call GroupEnumUnitsInRect(GROUP, bj_mapInitialPlayableArea, Condition(function filter))
endfunction

private function init takes nothing returns nothing
    local timer t = CreateTimer()
    set cs = ConeSector.create(0, 0, 256, Deg2Rad(90), Deg2Rad(180))
    call TimerStart(t, 0.03125, true, function go)
    
endfunction

endlibrary

I believe this works, but it uses vJass (it doesn't really need to).
 

Attachments

  • ConeFilter.w3x
    17.9 KB · Views: 210

BlueMirage

Trust, but doubt.
Reaction score
39
GAH vJASS
I'd rather not have a direct solution, just the answer to why my way of doing it doesn't work.
 

BlueMirage

Trust, but doubt.
Reaction score
39
I'll post the full thing then.

JASS:
globals
boolean udg_TempBool //I use GUI declaration, but included this so that it all should
  //be easier to copy-paste if you want/need to.
endglobals

function Angle takes real x1, real y1, real x2, real y2 returns real
    local real r = bj_RADTODEG * Atan2(y2 - y1, x2 - x1)
    if r < 0 then
        set r = 360 - r
    endif
    call BJDebugMsg(R2S(r))
    return r
endfunction

function FindCondition takes nothing returns boolean
    local unit u = GetFilterUnit()
    set udg_TempBool = ( ( (GetUnitState(u, UNIT_STATE_LIFE)) > 0 ) and ( IsUnitType(u, ConvertUnitType(2)) == false ) )
    set u = null
    return udg_TempBool
endfunction

function CountGroup takes group g returns integer 
    local integer i = 0
    local unit u
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        set i = i + 1
        call GroupRemoveUnit(g, u)
    endloop 
    return i    
endfunction

function FilterCone takes group g, real x ,real y, real facing, real angle returns nothing 
    call BJDebugMsg(I2S(CountGroup(g)))
endfunction


JASS:
function Trig_FDB_Conditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A00J'
endfunction

function Trig_FDB_Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local real xu = GetUnitX(u)
    local real yu = GetUnitY(u)
    local real a = Angle(xu, yu, GetSpellTargetX(), GetSpellTargetY())
    local group g = CreateGroup()
    
    call GroupEnumUnitsInRange(g, xu, yu, 600., Condition(function FindCondition))
    call GroupRemoveUnit(g, u)
    call BJDebugMsg(I2S(CountGroup(g)))
    call FilterCone(g, xu, yu, a, 40.)
    call BJDebugMsg(I2S(CountGroup(g)))
    
endfunction

//===========================================================================
function InitTrig_FDB takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( t, Condition( function Trig_FDB_Conditions ) )
    call TriggerAddAction( t, function Trig_FDB_Actions )
endfunction
 

Prozix

New Member
Reaction score
7
I created a map to test your code and it compiled perfectly :)
When I cast the spell it only counts the units once, I'm not sure if that is your problem but it is caused by your CountGroup function
I have rewritten it so that it doesn't remove units from the group because I guess you need those units :p
JASS:

/*//nothing to change
function CountUnitsInGroupEnum takes nothing returns nothing
    set bj_groupCountUnits = bj_groupCountUnits + 1
endfunction
*/
function CountGroup takes group g returns integer
    set bj_groupCountUnits = 0
    call ForGroup(g, function CountUnitsInGroupEnum)
    return bj_groupCountUnits
endfunction
 

BlueMirage

Trust, but doubt.
Reaction score
39
I think I'm starting to see the problem.

Assume I would create the following function:

JASS:
function Count takes integer i returns integer
    set i = i + 1
endfunction


I need to return a value so that I can increase my value by 1. If I don't return a value, nothing will happen because the (+1) only happens in the function Count and not in the function that's calling it. However, from my understanding, groups are different.

JASS:
function GroupRemove takes group g, unit u returns nothing
    call GroupRemoveUnit(g, u)
endfunction

For some reason, I will not need to return a value here. The unit is removed from the group of the function that is calling GroupRemove still. The taken group seems to be some kind of global variable instead of a local one.

I'm going to do some experiments to see if this is true.
 

Prozix

New Member
Reaction score
7
I'm sorry but I don't understand what you are saying.
In the CountGroup function I posted I use a global bj variable to count because the forgroup function can't take a function with arguments.
I can't do this for instance
JASS:

function CountIncr takes integer i returns integer
    return i + 1
endfunction

function CountGroup takes group g returns integer
    local integer count = 0
    call ForGroup(g, function CountIncr(count)) //wrong
    return count
endfunction
 

BlueMirage

Trust, but doubt.
Reaction score
39
It has nothing to do with the ForGroup() function. Sorry for being unclear.
My point is:
JASS:
function GroupRemove takes group g, unit u returns nothing
    call GroupRemoveUnit(g, u)
endfunction

function Start takes nothing returns nothing
    local unit u2 = SomeUnit
    local group G2
    call GroupEnumUnit(G2, Whatever)
    call GroupRemove(G2, u2)
endfunction


Even though the function GroupRemove does not return a value, it still affects G2 even though it is in another function. u2 will not remain in G2 for that reason. I did not know that groups worked that way.
 

Prozix

New Member
Reaction score
7
that is because a group is actually a handle to a place in memory. If you pass the handle to another function it can change the memory using the handle.
An integer for example is a type that is usually 4 bytes in size, just as big as a handle. Unlike groups, the integer is copied to the function, not a handle to the integer. That is why you can change the integer without affecting the original. ;)
 

BlueMirage

Trust, but doubt.
Reaction score
39
That explains a lot.

Then, what happens if I create a new group (let's call it "g") for my CountGroup function that I set equal to the taken group?? Would the taken group be modified if I modify "g"?
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top