Data Attachment System
kthxbye?
kthxbye?
So, basicly, this system is like ABC, HAIL, HSAS; it stores struct data. Now, does ABC, HAIL, HSAS allow recycling? This does. It allows timer and unit recycling, and then every single thing-struct data attaching!
NOTICE - Requires vJASS!!!
Code: (Sorry, it's pretty heavy.)
JASS:
//===================================================================\\
// D A T A A T T A C H M E N T S Y S T E M \\
// B Y V E S T R A S \\
//===================================================================\\
//===================================================================\\
// U S A G E: \\
// \\
// In the top of your trigger, run DAS_CreateFunctions textmacro, \\
// this will create all necessary data attachment functions for use. \\
// \\
//===================================================================\\
//===================================================================\\
// P R O S / C O N S: \\
// \\
// Pros: \\
// Using this system, you are able to store data in whatever you \\
// want, whether it's stupid or not. (Timers, units, triggers, you \\
// name it.) You can also recycle timers and units. \\
// \\
// Cons: \\
// A little bit lower max than Vexorian's TimerUtils, but isn't so \\
// much that it means something. I think it's just as fast as \\
// TimerUtils (Blue), so speed isn't a problem. \\
//===================================================================\\
//===================================================================\\
// G I V E C R E D I T T O V E S T R A S !!! \\
//===================================================================\\
library DAS
// Globals
globals
public constant string SYSTEMCOLOR = "|cffff0000" // The color used in system messages
// End of globals
endglobals
public function H2I takes handle h returns integer
return h
return 0
endfunction
globals
public integer INTEGER = 0x100000
// Taken from Vexorian's TimerUtils
public constant integer MAX = 400000
// A little bit lower than TimerUtils (Blue)
endglobals
//! textmacro DAS_CreateFunctions takes SCOPE, TYPE, NAME
globals
$SCOPE$ integer array DATA[DAS_MAX]
// Used for storing the data
$SCOPE$ integer TIMES = 0
// Counter
endglobals
$SCOPE$ function Set$NAME$Data takes $TYPE$ h, integer d returns nothing
set TIMES = TIMES + 1
if TIMES > DAS_MAX then
call BJDebugMsg(DAS_SYSTEMCOLOR + "System: number of struct data attached at the same time greater than max. You are in big trouble my friend!")
return
endif
set DATA[DAS_H2I(h) - DAS_INTEGER] = d
endfunction
$SCOPE$ function Get$NAME$Data takes $TYPE$ h returns integer
set TIMES = TIMES - 1
return DATA[DAS_H2I(h) - DAS_INTEGER]
endfunction
//! endtextmacro
// Setting handle data, for recycling (can be for public use as well though)
globals
// Used for displaying stuff
private integer HTIMES = 0
endglobals
//! runtextmacro DAS_CreateFunctions("public", "handle", "Handle")
globals
private timer array T
private integer TN = 0
private unit array U
private integer UN = 0
private constant integer RANDOM = 0x558329029
// Used for displaying stuff
private integer UNITS = 0
private integer TIMERS = 0
endglobals
// Credits to Vexorian for this idea, this does not work with units, please use non-textmacro function below
public function NewTimer takes nothing returns timer
if TN == 0 then
set T[0] = CreateTimer()
else
set TN = TN - 1
endif
call SetHandleData(T[TN], 0)
return T[TN]
endfunction
public function ReleaseTimer takes timer t returns nothing
if t == null then
call BJDebugMsg(SYSTEMCOLOR + "System: cannot release null timers")
return
endif
if TN == 8191 then
call DestroyTimer(t)
else
call PauseTimer(t)
if GetHandleData(t) == RANDOM then
debug call BJDebugMsg(SYSTEMCOLOR + "System: double free!")
return
endif
call SetHandleData(t, RANDOM)
set T[TN] = t
set TN = TN + 1
set TIMERS = TN
endif
endfunction
public function NewUnit takes player p, integer id, real x, real y returns unit
local integer i = 0
local boolean b = false
if UN != 0 then
loop
exitwhen i > UN or b
if id == GetUnitTypeId(U<i>) then
call ShowUnit(U<i>, true)
call SetUnitX(U<i>, x)
call SetUnitY(U<i>, y)
call SetUnitOwner(U<i>, p, true)
set b = true
else
set i = i + 1
endif
endloop
if b != true then
call CreateUnit(p, id, x, y, 0)
endif
endif
if UN == 0 then
set U[0] = CreateUnit(p, id, x, y, 0)
else
set UN = UN - 1
endif
call SetHandleData(U[UN], 0)
return U<i>
endfunction
public function ReleaseUnit takes unit u returns nothing
if u == null then
call BJDebugMsg(SYSTEMCOLOR + "System: cannot release null units")
return
endif
if UN == 8191 then
call RemoveUnit(u)
else
call ShowUnit(u, false)
if GetHandleData(u) == RANDOM then
debug call BJDebugMsg(SYSTEMCOLOR + "System: double free!")
return
endif
call SetHandleData(u, RANDOM)
set U[UN] = u
set UN = UN + 1
set UNITS = UN
endif
endfunction
public function DisplayData takes nothing returns nothing
call BJDebugMsg(SYSTEMCOLOR + "//=====================================================\\")
call BJDebugMsg(SYSTEMCOLOR + "//Current number of units in system: " + I2S(UNITS))
call BJDebugMsg(SYSTEMCOLOR + "//Current number of timers in system: " + I2S(TIMERS))
call BJDebugMsg(SYSTEMCOLOR + "//Current number of struct attachments for handles: " + I2S(HTIMES))
call BJDebugMsg(SYSTEMCOLOR + "//=====================================================\\")
endfunction
endlibrary</i></i></i></i></i></i>
I hope you guys goddamnit enjoys this one, it was really hard making.