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().
Plugins :
Simulated Hashtable : http://www.thehelper.net/forums/showthread.php?t=129880
Here's a demo map (Modified from Cohadar's PUI demo map) :
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) :