System UIR (Unit Identity Registrator)

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
This is an unit indexing system I made.

System will assigns a unique id for units by using GetUnitId().

JASS:
/////////////////////////////////////////////////////////////////////////////////////////////
//
//   ( . ~ . ~ . ~ . ~ . ~ . ~ . ~ . ~ . ~ . ~ . ~ .)    
//   . Unit Identity Registrator v1.1               .    
//   )  by kingking                                 ( 
//   .                                              .   
//   ) ~ . ~ . ~ . ~ . ~ . ~ . ~ . ~ . ~ . ~ . ~ . ~( 
//
//  Functions available :                     
//  GetUnitId(unit, lock?) -> integer
//  GetUnitById(integer) -> unit
//  LockIndex(integer)
//  UnlockIndex(integer)
//
//  This system registers units through GetUnitId.
//      The function will returns a unique identity to each unit.
//          When registered units are removed from game,
//              the recycler will frees up the identity for other units if the identity is unlocked.
//
//  Locking index for heroes is highly recommend because they will die and revive.
//
//  PROS : Index recycler. (Yeah!)
//         Can extends up to many plug-ins.
//         High compability.
//         Index Locking service.
//  
//  CONS : Uses Hashtable itself.
//         (1.23 or older Warcraft can use this system by using Simulated Hashtable.)
//
//  COMPABILITY(Through plug-ins) :
//  1) Users are not needed to change the code when switching other unit indexing system to this.
//  2) Users are not needed to change the textmacro when switching other unit indexing system to this.
//
/////////////////////////////////////////////////////////////////////////////////////////////
library UIR initializer Init requires HASHTABLE

///////////////////////////You may change it but not recommended/////////////////////////////
globals
    private constant real RECYCLER_PERIOD = .03125 //32 fps, means max 32 units can be recycled in a second.
    public constant boolean DEFAULT_LOCK = false//Used by plugins.
endglobals
/////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////NO MORE TOUCH BELOW HERE///////////////////////////////////////////
globals
    private constant timer RECYCLER = CreateTimer()
    private integer INDEX_COUNT = 0
    private integer array RECYCLED_INDEXES
    private integer RECYCLED_INDEX_COUNT = 0
    private integer RECYCLE = 0
    private integer INDEX = 0
    private unit DECAYING_UNIT = null
    private boolean LOCK_INDEX = false
    private hashtable ht
endglobals

function GetUnitById takes integer i returns unit
    return LoadUnitHandle(ht,i,0)
endfunction

function LockIndex takes integer i returns nothing
    call SaveBoolean(ht,i,0,true)
endfunction

function UnlockIndex takes integer i returns nothing
    call SaveBoolean(ht,i,0,false)
endfunction

private function Recycle takes nothing returns nothing
    if RECYCLE > INDEX_COUNT then
        set RECYCLE = 1
    else
        set RECYCLE = RECYCLE + 1
    endif
    debug call BJDebugMsg("Recycling index : #" + I2S(RECYCLE))
    set DECAYING_UNIT = LoadUnitHandle(ht,RECYCLE,0)
    set LOCK_INDEX = LoadBoolean(ht,RECYCLE,0)
    if DECAYING_UNIT != null and GetWidgetLife(DECAYING_UNIT) == 0.000 and not LOCK_INDEX then
        //Through some testing, a removed unit has 0.000 life.
        //A unit is there but removed..
        call RemoveSavedHandle(ht,RECYCLE,0)
        call RemoveSavedBoolean(ht,RECYCLE,0)
        //Merge the recycled index to recycle registry
        set RECYCLED_INDEX_COUNT = RECYCLED_INDEX_COUNT + 1
        set RECYCLED_INDEXES[RECYCLED_INDEX_COUNT] = RECYCLE
        call BJDebugMsg("Index : #" + I2S(RECYCLE) + " is recycled!")
    endif
endfunction

function GetUnitId takes unit u, boolean b returns integer
    set INDEX = LoadInteger(ht,GetHandleId(u),0)
    
    if INDEX == 0 then
        if RECYCLED_INDEX_COUNT > 0 then
            set INDEX = RECYCLED_INDEXES[RECYCLED_INDEX_COUNT]
            set RECYCLED_INDEX_COUNT = RECYCLED_INDEX_COUNT - 1
        else
            set INDEX_COUNT = INDEX_COUNT + 1
            set INDEX = INDEX_COUNT
            debug call BJDebugMsg("Max Index is #" + I2S(INDEX_COUNT))
        endif
        
        call SaveUnitHandle(ht,INDEX,0,u)
        call SaveBoolean(ht,INDEX,0,b)
        call SaveInteger(ht,GetHandleId(u),0,INDEX)
    endif
    
    return INDEX
endfunction

private function Init takes nothing returns nothing
    set ht = InitHashtable()
    call TimerStart(RECYCLER,RECYCLER_PERIOD,true,function Recycle)
endfunction

endlibrary


Plugins :
JASS:
//===============================================================================
//            Compability Plugin for Unit Identity Registrator
//
//       This plugin converts other system's function to UID function.
//
//  With this plugin, PUI based systems can be supported by UID.
//===============================================================================
library UIRC requires UIR

//PUI Compability
function GetUnitIndex takes unit u returns integer
    return GetUnitId(u,UIR_DEFAULT_LOCK)
endfunction

//Auto Index and AIDS have same function, GetUnitId()
endlibrary

JASS:
//===============================================================================
//          Textmacro Compability Plugin for Unit Identity Registrator
//
//       This plugin converts other system's textmacro to UID function.
//
//  With this plugin, PUI and AutoIndex based systems can supported by UID.
//===============================================================================
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~PUI textmacro~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//===========================================================================
//  Allowed PUI_PROPERTY TYPES are: unit, integer, real, boolean, string
//  Do NOT put handles that need to be destroyed here (timer, trigger, ...)
//  Instead put them in a struct and use PUI textmacro
//===========================================================================
library PUI requires UIR
//! textmacro PUI_PROPERTY takes VISIBILITY, TYPE, NAME, DEFAULT
$VISIBILITY$ struct $NAME$
    private static unit   array pui_unit
    private static $TYPE$ array pui_data
    
    //-----------------------------------------------------------------------
    //  Returns default value when first time used
    //-----------------------------------------------------------------------
    static method operator[] takes unit whichUnit returns $TYPE$
        local integer pui = GetUnitId(whichUnit,UIR_DEFAULT_LOCK)
        if .pui_unit[pui] != whichUnit then
            set .pui_unit[pui] = whichUnit
            set .pui_data[pui] = $DEFAULT$
        endif
        return .pui_data[pui]
    endmethod
    
    //-----------------------------------------------------------------------
    static method operator[]= takes unit whichUnit, $TYPE$ whichData returns nothing
        local integer pui = GetUnitId(whichUnit,UIR_DEFAULT_LOCK)
        set .pui_unit[pui] = whichUnit
        set .pui_data[pui] = whichData
    endmethod
endstruct
//! endtextmacro

//===========================================================================
//  Never destroy PUI structs directly.
//  Use .release() instead, will call .destroy()
//===========================================================================
//! textmacro PUI
    private static unit    array pui_unit
    private static integer array pui_data
    private static integer array pui_id
    
    //-----------------------------------------------------------------------
    //  Returns zero if no struct is attached to unit
    //-----------------------------------------------------------------------
    static method operator[] takes unit whichUnit returns integer
        local integer pui = GetUnitId(whichUnit,UIR_DEFAULT_LOCK)
        if .pui_data[pui] != 0 then
            if .pui_unit[pui] != whichUnit then
                // recycled handle detected
                call .destroy(.pui_data[pui])
                set .pui_unit[pui] = null
                set .pui_data[pui] = 0            
            endif
        endif
        return .pui_data[pui]
    endmethod
    
    //-----------------------------------------------------------------------
    //  This will overwrite already attached struct if any
    //-----------------------------------------------------------------------
    static method operator[]= takes unit whichUnit, integer whichData returns nothing
        local integer pui = GetUnitId(whichUnit,UIR_DEFAULT_LOCK)
        if .pui_data[pui] != 0 then
            call .destroy(.pui_data[pui])
        endif
        set .pui_unit[pui] = whichUnit
        set .pui_data[pui] = whichData
        set .pui_id[whichData] = pui
    endmethod

    //-----------------------------------------------------------------------
    //  If you do not call release struct will be destroyed when unit handle gets recycled
    //-----------------------------------------------------------------------
    method release takes nothing returns nothing
        local integer pui= .pui_id[integer(this)]
        call .destroy()
        set .pui_unit[pui] = null
        set .pui_data[pui] = 0
    endmethod
//! endtextmacro
endlibrary
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Auto Index~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library AutoIndex requires UIR
    module AutoData
        private static thistype array data
        
        // Fixed up the below to use thsitype instead of integer.
        static method operator []= takes unit u, thistype i returns nothing
            set .data[GetUnitId(u)] = i //Just attaching a struct to the unit
        endmethod                       //using the module's thistype array.
        
        static method operator [] takes unit u returns thistype
            return .data[GetUnitId(u)] //Just returning the attached struct.
        endmethod
    endmodule
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
endlibrary


Simulated Hashtable : http://www.thehelper.net/forums/showthread.php?t=129880

Here's a demo map (Modified from Cohadar's PUI demo map) :
 

T.s.e

Wish I was old and a little sentimental
Reaction score
133
It looks awfully like you just copied and pasted parts of AIDS and then changed the names.

See the similarity?
AIDS:
JASS:

    function GetUnitIndex takes unit u returns integer // Cannot be recursive.
        set getindex=GetUnitId(u)
        
        if getindex==0 then
            // Get new index, from recycler first, else new.
            // Store the current index in getindex.
            if MaxRecycledIndex==0 then // Get new.
                set MaxIndex=MaxIndex+1
                set getindex=MaxIndex
            else // Get from recycle stack.
                set getindex=RecycledIndex[MaxRecycledIndex]
                set MaxRecycledIndex=MaxRecycledIndex-1
            endif
            
            // Store index on unit.
            call SetUnitUserData(u,getindex)
            set IndexUnit[getindex]=u
            
            // Add index to recycle list.
            set MaxDecayingIndex=MaxDecayingIndex+1
            set DecayingIndex[MaxDecayingIndex]=getindex
            
            // Do not fire things here. No AIDS structs will be made at this point.
        endif
        
        return getindex
    endfunction

UIR:
JASS:
function GetUnitId takes unit u returns integer
    set INDEX = GetUnitUserData(u)
    
    if INDEX == 0 then
        if RECYCLED_INDEX_COUNT > 0 then
            set INDEX = RECYCLED_INDEXES[RECYCLED_INDEX_COUNT]
            set RECYCLED_INDEX_COUNT = RECYCLED_INDEX_COUNT - 1
        else
            set INDEX_COUNT = INDEX_COUNT + 1
            set INDEX = INDEX_COUNT
            debug call BJDebugMsg("Max Index is #" + I2S(INDEX_COUNT))
        endif
        set INDEXED_UNITS[INDEX] = u
        call SetUnitUserData(u, INDEX)
        set INDEX = GetUnitUserData(u)
    endif
    
    return INDEX
endfunction

They're even the same length as each other when removing the comments!

It seems to me that you tried to copy AIDS, but only with far less uses.
 

Jesus4Lyf

Good Idea™
Reaction score
397
This doesn't even come close to doing what AIDS does with structs and stuff.

Ugh, he doesn't even know what he's doing. He hasn't even used RECYCLE_TIME_TICK either. It's like he tried to copy PUI and AIDS at once and forgot what he was trying to achieve...

Oh, and there is about 2 lines missing from your AIDS comparison moving to UIR. Those two lines are used to keep the recycler complexity correct. Sure enough, I read his recycler, and the complexity is wrong.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
RECYCLE_TIME_TICK
Ops, forget to remove it, because it is used in old version. :p

See the similarity?
UIR contains 2 free data "boxes".

UIRUserData is broken
Fixed. :p

P/S : I forgot to remove the "AIDS" from the documentation after removing the AIDS textmacro compability support.
 

wraithseeker

Tired.
Reaction score
122
Stop reinventing the wheel kingkingykk3.

People have already made countless systems on this kind of stuffs. Isn't PUI and AutoIndex good enough for your indexing needs?
 

Jesus4Lyf

Good Idea™
Reaction score
397

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
You didn't fix anything.
Huh?

JASS:
function SetUnitData takes unit u, integer i returns nothing
    local integer index = GetUnitId(u)
    if index > 0 then
        set UserData[index] = i
    endif
endfunction


JASS:
function GetUnitData takes unit u returns integer
    local integer i = GetUnitId(u)
    if i > 0 then
        return UserData<i>
    endif
    return 0
endfunction
</i>
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
How about this?

JASS:
function SetUnitData takes unit u, integer i returns nothing
    local integer index = GetUnitId(u)
    set Units[index] = u
    set UserData[index] = i
endfunction

function GetUnitData takes unit u returns integer
    local integer i = GetUnitId(u)
    if Units<i> == u then
        return UserData<i>
    else
        set Units<i> = u
        set UserData<i> = 0
    endif
    return 0
endfunction
</i></i></i></i>
 

Flare

Stops copies me!
Reaction score
662
JASS:
    if DECAYING_UNIT != null and GetWidgetLife(DECAYING_UNIT) &gt; .405 and not LOCK_INDEX then
        //Through some testing, a removed unit has 0.000 life.
        //A unit is there but removed..
        call RemoveSavedHandle(ht,RECYCLE,0)
        call RemoveSavedBoolean(ht,RECYCLE,0)
        //Merge the recycled index to recycle registry
        set RECYCLED_INDEX_COUNT = RECYCLED_INDEX_COUNT + 1
        set RECYCLED_INDEXES[RECYCLED_INDEX_COUNT] = RECYCLE
        call BJDebugMsg(&quot;Index : #&quot; + I2S(RECYCLE) + &quot; is recycled!&quot;)
    endif

1) That BJDebugMsg should have debug in front of it, we don't want "Index : #1 is recycled" appearing in-game unless there is a problem that would require the indexes to be displayed
2) "a removed unit has 0.000 life" - well, so does a dead unit. With that condition, you are unable to distinguish between dead and removed units (provided you are right in saying that removed units have 0 life). Could be changed to
JASS:
IsUnitInGroup (DECAYING_UNIT, IndexedUnits) == false //whereby all units added to the system would be added to IndexedUnits, and any manually removed units (if that&#039;s allowed with this system) would be removed
or...
GetUnitTypeId (DECAYING_UNIT) == 0 //last I checked, non-existant units return 0 on a GetUnitTypeId call

3) "a unit is there but removed..." - is there some hidden, cryptic meaning to that statement? :p

Stop reinventing the wheel kingkingykk3.

People have already made countless systems on this kind of stuffs. Isn't PUI and AutoIndex good enough for your indexing needs?

Pot calling the kettle black much? Let's see what's on your list... stun system, revival, knockback (how original and unique!) and, finally, timed effects.
 

Jesus4Lyf

Good Idea™
Reaction score
397
UIR now uses hashtable as storage.
UIR is now even more broken than before.
JASS:
if DECAYING_UNIT != null and GetWidgetLife(DECAYING_UNIT) &gt; .405 and not LOCK_INDEX then

So... the unit isn't null... IS alive... and isn't locked? So like... You recycle units while they're on the map for no apparent reason. Good stuff.

Your locking is broken and useless because it doesn't allow multiple locks on one index. Please don't try to randomly implement stuff you don't know the purpose of. At very least, understand it before you rip it off.
JASS:
function GetUnitId takes unit u, boolean b returns integer

Really... this is just bad. You don't seem to know what you're doing. All locking does in your system is let you leak indexes.

Can't this get graveyarded yet? :(
 

RaiJin

New Member
Reaction score
40
i agree with what jesus says, this is like another PUI, its like making the same system over and over and over again.
 

Romek

Super Moderator
Reaction score
964
Ouch.. This is a terrible system. =|
Need I repeat what others have said?

(ROFL at recycling alive units indices)
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      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