• 🏆 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!

[vJASS] UnitFilter

Level 9
Joined
Jun 21, 2012
Messages
432
I have time that I do not upload something here...

This library is similar to The Filtering System but with less code and more understandable, or I think so:

JASS:
library UnitFilter
//* =========================================================================== *
//*   UnitFilter                                                                *
//*   ¯¯¯¯¯¯¯¯¯¯                                                                *
//*   v0.1.1.2                                                                  *
//*   by Trigger.edge                                                           *
//*                                                                             *
//*   struct UnitFilter extends array                                             *
//*                                                                               *
//*       - method pass takes unit target, player source returns boolean        *
//*                                                                            *
//*   How to use:                                                               *
//*   ¯¯¯¯¯¯¯¯¯¯                                                                *
//*       local UnitFilter uf = UF_NOT_ENEMY + UF_STRUCTURE + UF_OR_MAGIC_IMMUNE*
//*                                                                             *
//*       if uf.pass(target_unit, source_player) then                           *
//*           // actions                                                      *
//*       endif                                                                    *
//*                                                                             *
//* =========================================================================== */
   
    native UnitAlive takes unit id returns boolean
   
    /*
    UnitFilter globals 
    */
    //! textmacro UF_baseFilters takes FILTER_NAME,FILTER_SIZE_1,FILTER_SIZE_2,FILTER_SIZE_3,FILTER_SIZE_4
        constant integer UF_AND_$FILTER_NAME$=$FILTER_SIZE_1$
        constant integer UF_NOT_$FILTER_NAME$=$FILTER_SIZE_2$
        constant integer UF_OR_$FILTER_NAME$=$FILTER_SIZE_3$
        constant integer UF_OR_NOT_$FILTER_NAME$=$FILTER_SIZE_4$
    //! endtextmacro

    globals
        //! runtextmacro UF_baseFilters( "ALLY            ", "0x1"  , "0x2"  , "0x4"  , "0x8"  )
        //! runtextmacro UF_baseFilters( "ALIVE            ", "0x10" , "0x12" , "0x14" , "0x18" )
        //! runtextmacro UF_baseFilters( "HERO            ", "0x20" , "0x22" , "0x24" , "0x28" )
        //! runtextmacro UF_baseFilters( "STRUCTURE        ", "0x30" , "0x32" , "0x34" , "0x38" )
        //! runtextmacro UF_baseFilters( "MAGIC_IMMUNE    ", "0x40" , "0x42" , "0x44" , "0x48" )
       
        //! runtextmacro UF_baseFilters( "ILLUSION        ", "0x50" , "0x52", "0x54", "0x58" )
        //! runtextmacro UF_baseFilters( "SUMMONED        ", "0x60" , "0x62", "0x64", "0x68" )
        //! runtextmacro UF_baseFilters( "STUNNED        ", "0x70" , "0x72", "0x74", "0x78" )
       
        //! runtextmacro UF_baseFilters( "VISIBLE        ", "0x80" , "0x82", "0x84", "0x88" )
        //! runtextmacro UF_baseFilters( "INVISIBLE        ", "0x90" , "0x92", "0x94", "0x98" )
        //! runtextmacro UF_baseFilters( "FOGGED        ", "0x100", "0x102", "0x104", "0x108" )
        //! runtextmacro UF_baseFilters( "MASKED        ", "0x110", "0x112", "0x114", "0x118" )
        //! runtextmacro UF_baseFilters( "PAUSED        ", "0x120", "0x122", "0x124", "0x128" )
        //! runtextmacro UF_baseFilters( "SELECTED        ", "0x130", "0x132", "0x134", "0x138" )

        //! runtextmacro UF_baseFilters( "FLYING        ", "0x140", "0x142", "0x144", "0x148" )
        //! runtextmacro UF_baseFilters( "GROUND        ", "0x150", "0x152", "0x154", "0x158" )
        //! runtextmacro UF_baseFilters( "MELEE_ATTACKER ", "0x160", "0x162", "0x164", "0x168" )
        //! runtextmacro UF_baseFilters( "RANGED_ATTACKER", "0x170", "0x172", "0x174", "0x178" )
       
        //! runtextmacro UF_baseFilters( "POLYMORPHED    ", "0x180", "0x182", "0x184", "0x188" )
        //! runtextmacro UF_baseFilters( "SLEEPING        ", "0x190", "0x192", "0x194", "0x198" )
        //! runtextmacro UF_baseFilters( "ETHEREAL        ", "0x200", "0x202", "0x204", "0x208" )
       
        //! runtextmacro UF_baseFilters( "UNDEAD        ", "0x210", "0x212", "0x214", "0x218" )
        //! runtextmacro UF_baseFilters( "MECHANICAL    ", "0x220", "0x222", "0x224", "0x228" )
        //! runtextmacro UF_baseFilters( "PEON            ", "0x230", "0x232", "0x234", "0x238" )
        //! runtextmacro UF_baseFilters( "TOWNHALL        ", "0x240", "0x242", "0x244", "0x248" )
    endglobals
   
    /*
    Textmacros operators: AND, NOT, OR, OR_NOT
    */
    //! textmacro UF_bitwise takes BOOLEAN,AND_FILTER_SIZE,NOT_FILTER_SIZE,OR_FILTER_SIZE,OR_NOT_FILTER_SIZE
        if(this>=$AND_FILTER_SIZE$)then
            if(not $BOOLEAN$)then
                return false
            endif
            set this=this-$AND_FILTER_SIZE$
        endif
        if(this>=$NOT_FILTER_SIZE$)then
            if($BOOLEAN$)then
                return false
            endif
            set this=this-$NOT_FILTER_SIZE$
        endif
        if(this>=$OR_FILTER_SIZE$)then
            if($BOOLEAN$)then
                return true
            endif
            set this=this-$OR_FILTER_SIZE$
        endif
        if(this>=$OR_NOT_FILTER_SIZE$)then
            if(not $BOOLEAN$)then
                return true
            endif
            set this=this-$OR_NOT_FILTER_SIZE$
        endif
    //! endtextmacro
   
    struct UnitFilter extends array
       
        method pass takes unit target, player source returns boolean
            if(0x200<=this)then
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_TOWNHALL)       ", "0x240", "0x242", "0x244", "0x248" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_PEON)           ", "0x230", "0x232", "0x234", "0x238" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_MECHANICAL)     ", "0x220", "0x222", "0x224", "0x228" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_UNDEAD)         ", "0x210", "0x212", "0x214", "0x218" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_ETHEREAL)       ", "0x200", "0x202", "0x204", "0x208" )
            endif
            if(0x150<=this)then
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_SLEEPING)       ", "0x190", "0x192", "0x194", "0x198" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_POLYMORPHED)    ", "0x180", "0x182", "0x184", "0x188" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_RANGED_ATTACKER)", "0x170", "0x172", "0x174", "0x178" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_MELEE_ATTACKER) ", "0x160", "0x162", "0x164", "0x168" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_GROUND)         ", "0x150", "0x152", "0x154", "0x158" )
            endif
            if(0x100<=this)then
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_FLYING)         ", "0x140", "0x142", "0x144", "0x148" )
                //! runtextmacro UF_bitwise( "IsUnitSelected(target,source)              ", "0x130", "0x132", "0x134", "0x138" )
                //! runtextmacro UF_bitwise( "IsUnitPaused(target)                          ", "0x120", "0x122", "0x124", "0x128" )
                //! runtextmacro UF_bitwise( "IsUnitMasked(target,source)                 ", "0x110", "0x112", "0x114", "0x118" )
                //! runtextmacro UF_bitwise( "IsUnitFogged(target,source)                 ", "0x100", "0x102", "0x104", "0x108" )
            endif
            if(0x50<=this)then
                //! runtextmacro UF_bitwise( "IsUnitInvisible(target,source)              ", "0x90", "0x92", "0x94", "0x98" )
                //! runtextmacro UF_bitwise( "IsUnitVisible(target,source)                ", "0x80", "0x82", "0x84", "0x88" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_STUNNED)        ", "0x70", "0x72", "0x74", "0x78" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_SUMMONED)       ", "0x60", "0x62", "0x64", "0x68" )
                //! runtextmacro UF_bitwise( "IsUnitIllusion(target)                       ", "0x50", "0x52", "0x54", "0x58" )
            endif
            if(0x01<=this)then
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_MAGIC_IMMUNE)   ", "0x40", "0x42", "0x44", "0x48" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_STRUCTURE)      ", "0x30", "0x32", "0x34", "0x38" )
                //! runtextmacro UF_bitwise( "IsUnitType(target,UNIT_TYPE_HERO)           ", "0x20", "0x22", "0x24", "0x28" )
                //! runtextmacro UF_bitwise( "UnitAlive(target)                           ", "0x10", "0x12", "0x14", "0x18" )
                //! runtextmacro UF_bitwise( "IsUnitEnemy(target,source)                  ", "0x1", "0x2", "0x4", "0x8" )
            endif
            return true
        endmethod
    endstruct
endlibrary
 
Last edited:

Deleted member 219079

D

Deleted member 219079

Nice alternative. Yes, this is more understandable.

I wouldn't mix lower and uppercase in the constants.

You can have the assignment below the inner if block.
 

Deleted member 219079

D

Deleted member 219079

Ye both of those are fine. To me the dot syntax seems intuitive.

JASS:
                $FILTER$
                    return false
                endif
                set this=this-$FILTER_SIZE$
 
Level 9
Joined
Jun 21, 2012
Messages
432
Okay I got it:
JASS:
//! textmacro UnitFilter_size takes FILTER_SIZE,FILTER
    $FILTER$
        return false
    endif
    set this=this-$FILTER_SIZE$
//! endtextmacro

method pass takes unit target, player source returns boolean
    if(this>=0x80000)then
        //! runtextmacro UnitFilter_size("0x80000","if(IsUnitIllusion(target))then")
    endif
    //etc...
 

Deleted member 219079

D

Deleted member 219079

Well you don't have to externalize the outer if block from the textmacro.
JASS:
        //! textmacro UnitFilter_size takes FILTER_SIZE,FILTER
            if(this>=$FILTER_SIZE$)then
                $FILTER$
                    return false
                endif
                set this=this-$FILTER_SIZE$
            endif
        //! endtextmacro
 

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
Although the script is much shorter and arguably understandable, if this is to replace the FilteringSystem it should not only include all features (this lib only supports units) but also some useful improvements compared to the it (FilteringSystem). I could give you a feature that would be good to include in this lib.

The FilteringSystem only allows the and type of filtering which means that if you do UnitFilter(u, p, FS_UNIT_HERO + FS_UNIT_ALIVE + FS_UNIT_DEAD), only if u passes the 3 classification will the function returns true. But how about if the user wants the object editor kind of filtering (The or type of filtering)? This is very useful in many cases as well (ex: spells). So this is good thing to consider if you want this to replace the FilteringSystem.

Currently, the script is just like a rip and shortened code from the FilteringSystem and I don't think it warrants approval.
 
Last edited:

AGD

AGD

Level 16
Joined
Mar 29, 2016
Messages
688
Just in case you haven't noticed yet,
JASS:
        //! runtextmacro UF_baseFilters( "ALLY            ", "0x1"  , "0x2"  , "0x4"  , "0x8"  )
        //! runtextmacro UF_baseFilters( "ALIVE            ", "0x10" , "0x12" , "0x14" , "0x18" )
        //! runtextmacro UF_baseFilters( "HERO            ", "0x20" , "0x22" , "0x24" , "0x28" )
        //! runtextmacro UF_baseFilters( "STRUCTURE        ", "0x30" , "0x32" , "0x34" , "0x38" )
        //! runtextmacro UF_baseFilters( "MAGIC_IMMUNE    ", "0x40" , "0x42" , "0x44" , "0x48" )
all filtersize should be atleast twice as big as the previous one no matter what filter group they belong.
Currently, you're having a constant offset between the filtergroups, it should be a constant ratio (atleast a ratio of 2) regardless of what group the filter belongs to.
JASS:
        //! runtextmacro UF_baseFilters( "ALLY            ", "0x1"  , "0x2"  , "0x4"  , "0x8"  )
        //! runtextmacro UF_baseFilters( "ALIVE            ", "0x10" , "0x20" , "0x40" , "0x80" )
        //! runtextmacro UF_baseFilters( "HERO            ", "0x100" , "0x200" , "0x400" , "0x800" )
        //! runtextmacro UF_baseFilters( "STRUCTURE        ", "0x1000" , "0x2000" , "0x4000" , "0x8000" )
        //! runtextmacro UF_baseFilters( "MAGIC_IMMUNE    ", "0x10000" , "0x20000" , "0x40000" , "0x80000" )
        //...
Could you try and see if this somewhat solves the problem?
 
Level 9
Joined
Jun 21, 2012
Messages
432
Just in case you haven't noticed yet,
JASS:
        //! runtextmacro UF_baseFilters( "ALLY            ", "0x1"  , "0x2"  , "0x4"  , "0x8"  )
        //! runtextmacro UF_baseFilters( "ALIVE            ", "0x10" , "0x12" , "0x14" , "0x18" )
        //! runtextmacro UF_baseFilters( "HERO            ", "0x20" , "0x22" , "0x24" , "0x28" )
        //! runtextmacro UF_baseFilters( "STRUCTURE        ", "0x30" , "0x32" , "0x34" , "0x38" )
        //! runtextmacro UF_baseFilters( "MAGIC_IMMUNE    ", "0x40" , "0x42" , "0x44" , "0x48" )
all filtersize should be atleast twice as big as the previous one no matter what filter group they belong.
Currently, you're having a constant offset between the filtergroups, it should be a constant ratio (atleast a ratio of 2) regardless of what group the filter belongs to.
JASS:
        //! runtextmacro UF_baseFilters( "ALLY            ", "0x1"  , "0x2"  , "0x4"  , "0x8"  )
        //! runtextmacro UF_baseFilters( "ALIVE            ", "0x10" , "0x20" , "0x40" , "0x80" )
        //! runtextmacro UF_baseFilters( "HERO            ", "0x100" , "0x200" , "0x400" , "0x800" )
        //! runtextmacro UF_baseFilters( "STRUCTURE        ", "0x1000" , "0x2000" , "0x4000" , "0x8000" )
        //! runtextmacro UF_baseFilters( "MAGIC_IMMUNE    ", "0x10000" , "0x20000" , "0x40000" , "0x80000" )
        //...
Could you try and see if this somewhat solves the problem?
Yes, this solves it, but would have another problem: filters would pass the MAX_INT limit (2147483648), so I am asking for help on this thread ^^ Bitwise problem
 
Top