- Joined
- Mar 10, 2009
- Messages
- 5,016
I would like to request coders to test this, but before you do, I can say that this is tested and works
for me and I need more proof that it works for you;
if it does, which one is better?...
Allocator #1
Allocator #2
TESTING:
for me and I need more proof that it works for you;
if it does, which one is better?...
Allocator #1
JASS:
library Allocator
module Allocator
private static integer index = 0
private static integer recycle = 0
private integer renew
static method allocate takes nothing returns integer
local thistype this
if recycle==0 then
set index = index + 1
set this = index
else
set this = recycle
set recycle = renew
endif
return this
endmethod
method deallocate takes nothing returns nothing
set renew = recycle
set recycle = this
endmethod
endmodule
endlibrary
JASS:
module Allocator
static integer Allocator_instance = 0
static integer Allocator_recycle = 0
static integer array Allocator_instanceArray
static method allocate takes nothing returns integer
local thistype this = Allocator_recycle
if (this==8191) then
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,1000.,"Allocator ERROR: Unable to allocate instance "+"["+I2S(this)+"]"+" anymore!")
return 0
else
if this==0 then
set Allocator_instance = Allocator_instance + 1
set this = Allocator_instance
else
set Allocator_recycle = Allocator_instanceArray[this]
endif
endif
return this
endmethod
method deallocate takes nothing returns nothing
if Allocator_instance==0 then
call DisplayTimedTextToPlayer(GetLocalPlayer(),0,0,1000.,"Allocator ERROR: Instance is 0!")
return
endif
set Allocator_instanceArray[this] = Allocator_recycle
set Allocator_recycle = this
endmethod
endmodule
TESTING:
JASS:
scope AllocatorDEMO
struct AllocatorDEMO extends array
implement Allocator
unit c
real dur
static integer instance = 0
static integer array insAr
static timer t = CreateTimer()
static method looper takes nothing returns nothing
local thistype this
local texttag tag
local integer index = 0
loop
set index = index + 1
set this = insAr[index]
if .dur > 0 then
set tag = CreateTextTag()
call SetTextTagPosUnit(tag, .c, 0)
call SetTextTagText(tag, R2S(.dur), 0.025)
call SetTextTagPermanent(tag, false)
call SetTextTagVelocity(tag, 0.03, 0.03)
call SetTextTagLifespan(tag, 3)
call SetTextTagFadepoint(tag, 0.01)
set tag = null
set .dur = .dur - 1.0
else
call BJDebugMsg("DEALLOCATE")
call KillUnit(.c)
set .c = null
call .deallocate()
set insAr[index] = insAr[instance]
set insAr[instance] = this
set index = index - 1
set instance = instance - 1
if instance==0 then
call BJDebugMsg("PAUSED")
call PauseTimer(t)
endif
endif
exitwhen index==instance
endloop
endmethod
static method run takes nothing returns nothing
local thistype this = allocate()
local rect ma = bj_mapInitialPlayableArea
local real x = GetRandomReal(GetRectMinX(ma),GetRectMaxX(ma))
local real y = GetRandomReal(GetRectMinY(ma),GetRectMaxY(ma))
if instance==0 then
call TimerStart(t,1.0,true,function thistype.looper)
endif
call BJDebugMsg(I2S(this))
set instance = instance + 1
set insAr[instance] = this
set .c = CreateUnit(Player(0), 'hpea', x, y,0)
set .dur = I2R(GetRandomInt(15,25))
set ma = null
endmethod
static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterPlayerEvent(t, Player(0), EVENT_PLAYER_END_CINEMATIC)
call TriggerAddAction(t,function thistype.run)
endmethod
endstruct
endscope