- Joined
- Jul 10, 2007
- Messages
- 6,306
JASS:
library GenericData
/*Information
===================================================================
Name: Generic Data Attachment
Version: 4.3
Author: Nestharus
Description:
Generic data attachment for handles and types
This simply creates an id for a handle id or type id that can be used as
an index in an array or struct. I found myself needing generic id attachment over
and over again.
It includes permanent handle ids (once made cannot be released), type ids (permanent), and
regular handle ids (can be released).
Requirements: NA
Installation: NA
API
------------------------------------------------------------------
module PermanentId
[integer id]
Retrieves a unique id for the struct given an id
local TypeStruct peasant = TypeStruct['hpea'] //let's get the index for peasant
module DynamicId
[integer id]
Retrieves a unique id for the struct given an id
local TypeStruct peasant = TypeStruct['hpea'] //let's get the index for peasant
release(integer id)
Releases an id for the struct
TypeStruct.release('hpea')
struct Order
[integer id]
Converts an order id into an instance id
local OrderStruct move = Order[OrderId("move")]
===================================================================*/
globals
public hashtable hashId = InitHashtable()
public integer kIndex = 0
endglobals
struct Order extends array
private static constant integer offset = 0xD0000
public static method operator[] takes integer id returns integer
return id-.offset
endmethod
endstruct
module PermanentId
private static integer hashIdCount = 0
private static integer k
public static method operator[] takes integer id returns integer
if not HaveSavedInteger(GenericData_hashId, id, .k) then
debug if .hashIdCount < 8191 then
call SaveInteger(GenericData_hashId, id, .k, .hashIdCount)
set .hashIdCount = .hashIdCount + 1
return .hashIdCount-1
debug else
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Permanent ID Overload!")
debug return 0
debug endif
endif
return LoadInteger(GenericData_hashId, id, .k)
endmethod
private static method onInit takes nothing returns nothing
set .k = GenericData_kIndex
set GenericData_kIndex = GenericData_kIndex + 1
endmethod
endmodule
module DynamicId
private static integer hashIdCount = 0
private static integer hashIdRecycleCount = 0
private static integer array hashIdRecycle
private static integer k
public static method operator[] takes integer id returns integer
if not HaveSavedInteger(GenericData_hashId, id, .k) then
if .hashIdRecycleCount > 0 then
set hashIdRecycleCount = hashIdRecycleCount - 1
call SaveInteger(GenericData_hashId id, .k, .hashIdrecycle[.hashIdRecycleCount])
return .hashIdRecycle[.hashIdRecycleCount]
else
debug if .hashIdCount < 8191 then
call SaveInteger(GenericData_hashId, id, .k, .hashIdCount)
set .hashIdCount = .hashIdCount + 1
return .hashIdCount-1
debug else
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Dynamic ID Overload!")
debug return 0
debug endif
endif
endif
return LoadInteger(GenericData_hashId, id, .k)
endmethod
public static method release takes integer id returns nothing
debug if .hashIdRecycleCount < 8191 then
set .hashIdRecycle[.hashIdRecycleCount] = LoadInteger(handleId, id, .k)
set .hashIdRecycleCount = .hashIdRecycleCount + 1
call RemoveSavedInteger(GenericData_hashId, id, .k)
debug else
debug call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Recycle ID Overload!")
debug endif
endmethod
private static method onInit takes nothing returns nothing
set .k = GenericData_kIndex
set GenericData_kIndex = GenericData_kIndex + 1
endmethod
endmodule
endlibrary
JASS:
scope Demo initializer Demonstration
private struct TypeStruct extends array
public integer bonus
implement PermanentId
endstruct
private function Demonstration takes nothing returns nothing
local TypeStruct peasant = TypeStruct['hpea'] //let's get the index for peasant
set peasant.bonus = 50
endfunction
endscope
Last edited: