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

A noobs privet (simple) Unit Indexer

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
JASS:
scope UnitIndexer initializer Init 

    globals

        constant real RESET = 0.
        constant real UNIT_WAS_INDEXED = 1.
        constant real UNIT_WAS_DEINDEXED = 2.
        private integer indexMax = 0
        private integer recycleMax = 0
        private integer array recycleIndex 
        boolean uDexEnabled
        real uDexEvent
        unit array uDexUnit 
    endglobals
    
    private function CleanUp takes nothing returns boolean
        local integer cur = indexMax
        loop
            exitwhen cur == 0
            if uDexUnit[cur] != null and GetUnitUserData(uDexUnit[cur]) == 0 then
                set uDexUnit[cur] = null
                set recycleMax = recycleMax + 1
                set recycleIndex[recycleMax] = cur
                set uDexEvent = RESET
                set uDexEvent = UNIT_WAS_DEINDEXED
            endif
            set cur = cur - 1
        endloop
        return false
    endfunction 
    
    private function Main takes nothing returns boolean
        local integer cur 
        if uDexEnabled == true then
            if recycleMax == 0 then
                set indexMax = indexMax + 1
                set cur = indexMax
            else 
                set cur = recycleIndex[recycleMax]
                set recycleMax = recycleMax - 1
            endif
            set uDexUnit[cur] = GetFilterUnit()
            call SetUnitUserData(uDexUnit[cur] , cur)
            set uDexEvent = RESET
            set uDexEvent = UNIT_WAS_INDEXED
        endif
        return false 
    endfunction

    private function Init takes nothing returns nothing
        local trigger t1 = CreateTrigger()
        local trigger t2 = CreateTrigger()
        local integer i = 0
        local region world = CreateRegion()
        local rect r = GetWorldBounds() 
        set uDexEnabled = true 
        call RegionAddRect(world, r)
        call TriggerRegisterEnterRegion(t1, world, Filter(function Main))
        call TriggerRegisterTimerEvent(t2, 30., true)
        call TriggerAddCondition(t2, Condition(function CleanUp))
        call RemoveRect(r)
        set world = null
        set r = null
        loop
            exitwhen i == 16
            call GroupEnumUnitsOfPlayer(bj_lastCreatedGroup, Player(i), Filter(function Main))
            set i = i + 1
        endloop
    endfunction

endscope

I took Bribes unit indexer and worked from it, what i could piece together that is. He uses linked lists and mine don't, among other things. Is there really any point in using linked list over for example a boolean? Anyway I would try it but didn't fully understand how it worked.

Maybe I should've posted this in the trigger section since I have no intention of making this a resource, my bad. ;)
 
Last edited:
Level 15
Joined
Nov 30, 2007
Messages
1,202
your cleanup is just nonfunctioning. How exactly you deindex units?(hint: you dont)

Lab is the correct place, the place for code submissions is Jass section, so this is correct section

How you mean I don't? I'm quite positive I managed to fire a deindex event aswell as reuse that custom value. Maybe you are refering to how bad it is and not that it doesn't work at all? :D

I loop through the entire array of indexed units (and deindexed) if their custom value is 0 and uDexUnit[cur] != null then they are deindexed. What happens then is that that index is added to the recycle index which is then reused upon next unit entering the map.

Would have used double lists if I understood them better. ;P
 
Last edited:
Level 15
Joined
Nov 30, 2007
Messages
1,202
very ineffective tho, because you loop over every unit in the map and you will fail to deindex locust units

I'm aware of that there is an algorithm to skip over the blanks, I just failed to implement it.

Locusts, I suppose you use that for spell systems or when do you index those? I would read into Nes indexer but frankly its too much and difficult for me. Maybe I'll see if I can pick up locusts, a question though: Does Bribes GUI version take that into account?
 
Level 6
Joined
Jul 30, 2013
Messages
282
is it really worth doing every 15 allocations?

is there some constant overhead if there are many non-existant/dead units indexed?

ive had games with 5kish units on map (silly bug..) the game was a damn slideshow. so theres no way you will have 8k or anywhere near that units on map and still have an actual game. you could definitely have a little more waste to have less frequent GC..
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
is it really worth doing every 15 allocations?

is there some constant overhead if there are many non-existant/dead units indexed?

ive had games with 5kish units on map (silly bug..) the game was a damn slideshow. so theres no way you will have 8k or anywhere near that units on map and still have an actual game. you could definitely have a little more waste to have less frequent GC..

The only thing that happens is that it will loop longer then it has to next time it is going to deindex units (it checks all units in the index). In my case, this loop span only increases, meaning it will always check for deindexed units at the point which there were most units alive indexed, so if at one point there were 1000 units, it will always loop to 1000 even though at a later stage only 100 units are alive and active indexed.
 
Level 31
Joined
Jul 10, 2007
Messages
6,306
Units fire the undefend ability twice when they are going out of scope. The first time is for the removal. The second time is for the removal of the undefend ability. When the undefend ability is removed, the level of it will be 0. Thus, if the undefend ability fires on a unit, that undefend ability is your special undefend deindex ability, and that undefend ability level is 0, the unit has been removed.

This should also work for Silence and Hex, but I don't know as I've never tested. I probably should ; ).

It's the standard approach for unit deindex events and cleaning.


Mine uses that same approach.
 
Status
Not open for further replies.
Top