SerraAvenger
Cuz I can
- Reaction score
- 234
This is for indexing rawids and strings with unique indicies between 1 and 8191, abusing hashtables and structs. It is quite simple indeed, but very useful.
It actually _is_ able to get indicies for handleid's, too. Not for handles though, so when the handleid get's recycled you might have a bunch of trouble.
Note that a string might get the same index like a handleid (or rawid) and vv.
If you require more than 8k for something special (like a random numbers indexer), _please_ instanciate the textmacro again (although you aren't supposed to ;D)
Especially useful with the ObjectAttachers that ease the syntax and do nasty stuff for you.
The Object attacher uses textmacros to create simple attachers for rawids and strings (no handles).
Best wishes, Serra
It actually _is_ able to get indicies for handleid's, too. Not for handles though, so when the handleid get's recycled you might have a bunch of trouble.
Note that a string might get the same index like a handleid (or rawid) and vv.
If you require more than 8k for something special (like a random numbers indexer), _please_ instanciate the textmacro again (although you aren't supposed to ;D)
JASS:
//...
call GetUniqueIndex( 039;I005039; ) // -> integer between 1 and 8k, doesn't change
call GetUniqueStringIndex( "Hey, babe!" ) // -> integer between 1 and 8k, maybe the same.
//...
JASS:
library Hashich
// This is for 8000 indicies per field only. If you need more, please instanciate the texmacro again.
//! textmacro HASHICH takes NAME, VAL, TYPE
globals
hashtable Hashich$NAME$_saver = InitHashtable()
integer Hashich$NAME$_count = 1
endglobals
function GetUnique$NAME$Index takes $TYPE$ val returns integer
local integer value = $VAL$
local integer id = LoadInteger( Hashich$NAME$_saver, value, 0 )
if id == 0 then
set id = Hashich$NAME$_count
set Hashich$NAME$_count = Hashich$NAME$_count + 1
call SaveInteger( Hashich$NAME$_saver, value, 0, id )
endif
return id
endfunction
//! endtextmacro
//! runtextmacro HASHICH( "", "val", "integer" )
//! runtextmacro HASHICH( "String", "StringHash( val )", "string" )
//! runtextmacro HASHICH( "Handle", "GetHandleId( val )", "handle" )
endlibrary
Especially useful with the ObjectAttachers that ease the syntax and do nasty stuff for you.
The Object attacher uses textmacros to create simple attachers for rawids and strings (no handles).
JASS:
//...
// Outside of any library/scope:
//! runtextmacro AddObjectValue( "Cost", "integer", "0" )
//! runtextmacro AddStringValue( "PlayerByName", "player", "null" )
//...
call SetObjectCost( 039;I005039;, 200 )
call BJDebugMsg( "The current cost are " + I2S( Cost[ 039;I005039; ] ) + " Gold." )
set Cost[ 039;I005039; ] = 50000
call BJDebugMsg( "The current cost are " + I2S( GetObjectCost( 039;I005039; ) ) + " Gold." ) // Prices grew a bit.
//...
local integer i = 0
loop
exitwhen i == 12
call SetStringPlayerByName( GetPlayerName( Player( i ) ), Player( i ) )
set i = i + 1
endloop
if PlayerByName[ "SerraAvenger" ] != null then
call BJDebugMsg( "SerraAvenger is in the game! He occupies slot #" + I2S( GetPlayerId( GetStringPlayerByName( "SerraAvenger" ) ) ) )
endif
JASS:
///// VER 02 03 - ObjectAttacher
//! textmacro AddObjectValue takes NAME, TYPE, DEFAULT
library_once $NAME$Attacher needs Hashich
globals
private boolean array isUsed
private $TYPE$ array Val
private $TYPE$ Default = $DEFAULT$
endglobals
public function GetObjectIndex takes integer id returns integer
return GetUniqueIndex( id )
endfunction
function SetDefault$NAME$ takes $TYPE$ value returns nothing
set Default = value
endfunction
function GetDefault$NAME$ takes nothing returns $TYPE$
return Default
endfunction
function SetObject$NAME$ takes integer id, $TYPE$ value returns nothing
local integer index = GetUniqueIndex( id )
if value == Default then
set isUsed[ index ] = false
else
set isUsed[ index ] = true
set Val[ index ] = value
endif
endfunction
function GetObject$NAME$ takes integer id returns $TYPE$
local integer index = GetObjectIndex( id )
local $TYPE$ result = Default
if isUsed[ index ] then
set result = Val[ index ]
endif
return result
endfunction
struct $NAME$
static method operator [] takes integer id returns $TYPE$
return GetObject$NAME$( id )
endmethod
static method operator []= takes integer id, $TYPE$ value returns nothing
call SetObject$NAME$( id, value )
endmethod
endstruct
endlibrary
//! endtextmacro
//! textmacro AddStringValue takes NAME, TYPE, DEFAULT
library_once $NAME$Attacher needs Hashich
globals
private boolean array isUsed
private $TYPE$ array Val
private $TYPE$ Default = $DEFAULT$
endglobals
public function GetStringIndex takes string id returns integer
return GetUniqueStringIndex( id )
endfunction
function SetDefault$NAME$ takes $TYPE$ value returns nothing
set Default = value
endfunction
function GetDefault$NAME$ takes nothing returns $TYPE$
return Default
endfunction
function SetString$NAME$ takes string id, $TYPE$ value returns nothing
local integer index = GetStringIndex( id )
if value == Default then
set isUsed[ index ] = false
else
set isUsed[ index ] = true
set Val[ index ] = value
endif
endfunction
function GetString$NAME$ takes string id returns $TYPE$
local integer index = GetStringIndex( id )
local $TYPE$ result = Default
if isUsed[ index ] then
set result = Val[ index ]
endif
return result
endfunction
struct $NAME$
static method operator [] takes string id returns $TYPE$
return GetString$NAME$( id )
endmethod
static method operator []= takes string id, $TYPE$ value returns nothing
call SetString$NAME$( id, value )
endmethod
endstruct
endlibrary
//! endtextmacro
Best wishes, Serra