- Joined
- Sep 9, 2007
- Messages
- 6,759
Please check the script header for description, how to use, requirements, changelogs and more!
Example
JASS:
// --------------------------------------------------------------------------------------------------------------------
//
// HandleStack
// ===========
//
// Version: 1.4.0
// Author: Anachron
//
// Requirements:
// Stack [by Anachron] (v. 1.3.0)
// (New) Table [by Bribe] (v. 3.1) [url]http://www.hiveworkshop.com/forums/jass-resources-412/snippet-new-table-188084/[/url]
//
// Description:
// HandleStack is a handletype based stack collection.
// You can easily create unitgroups, playergroups, effectgroups
// and basically group and handletype together.
// Instead of returning ids of the handles in stack, you get
// the real handle that is saved.
//
// History:
// 1.0.0: Initial Release
// 1.1.0: Added missing clear method
// 1.2.0: Adopt API changes of new Stack
// 1.3.0: Added switching of handles
// 1.4.0: Added sorting of handles
//
// API:
// Exactly the same than Stack, but returns handletypes instead
//
// ---------------------------------------------------------------------------------------------------------------------------
library HandleStack requires Stack
//! textmacro CreateHandleStack takes NAME, TYPE
private function interface $NAME$SortFunc takes $NAME$Stack stack, $TYPE$ toAdd, $TYPE$ toCheck, boolean asc returns boolean defaults false
struct $NAME$Stack
private delegate Stack Index = 0
private Table $NAME$s = 0
public static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set .Index = Stack.create()
set .$NAME$s = Table.create()
return this
endmethod
public method add takes $TYPE$ theHandle returns boolean
local integer id = GetHandleId(theHandle)
local boolean added = .Index.add(id)
if added then
set .$NAME$s.$TYPE$[id] = theHandle
endif
return added
endmethod
public method delete takes $TYPE$ theHandle returns boolean
local integer id = GetHandleId(theHandle)
local boolean deleted = .Index.delete(id)
if deleted then
call .$NAME$s.remove(id)
endif
return deleted
endmethod
public method clear takes nothing returns nothing
call .Index.clear()
call .$NAME$s.flush()
endmethod
public method get takes integer id returns $TYPE$
if .Index.has(id) then
return .$NAME$s.$TYPE$[id]
endif
return null
endmethod
public method getNext takes nothing returns $TYPE$
return .get(.Index.getNext())
endmethod
public method getPrev takes nothing returns $TYPE$
return .get(.Index.getPrev())
endmethod
public method getFirst takes nothing returns $TYPE$
return .get(.Index.getFirst())
endmethod
public method getLast takes nothing returns $TYPE$
return .get(.Index.getLast())
endmethod
public method random takes nothing returns $TYPE$
return .get(.Index.random())
endmethod
public method switch takes $TYPE$ first, $TYPE$ second returns boolean
return .Index.switch(GetHandleId(first), GetHandleId(second))
endmethod
public method sort takes $NAME$SortFunc sortFunc, boolean asc returns nothing
local integer out = -1
local integer ins = -1
local $TYPE$ outVal = null
local $TYPE$ insVal = null
debug local boolean print = .print
debug set .print = false
set out = 0
set ins = out +1
set outVal = .get(out)
set insVal = .get(ins)
loop
exitwhen out >= .Index.count
if sortFunc.evaluate(this, insVal, outVal, asc) then
call .switch(insVal, outVal)
debug if .print then
debug set STACK_MSG = STACK_COLOR + "Stack|r[" + STACK_COLOR+ I2S(this) + "|r]: "
debug set STACK_MSG = STACK_MSG + "Replaced " + STACK_COLOR + I2S(GetHandleId(outVal)) + "|r "
debug set STACK_MSG = STACK_MSG + "with " + STACK_COLOR + I2S(GetHandleId(insVal)) + "|r"
debug call BJDebugMsg(STACK_MSG)
debug endif
endif
set ins = ins +1
if ins >= .Index.count then
set out = out +1
set ins = out +1
endif
set insVal = .get(ins)
set outVal = .get(out)
endloop
set insVal = null
set outVal = null
debug set .print = print
endmethod
public method destroy takes nothing returns nothing
call .Index.destroy()
call .$NAME$s.destroy()
endmethod
endstruct
//! endtextmacro
endlibrary
Example
JASS:
//! runtextmacro CreateHandleStack("Unit", "unit")
private function testHandleStack takes nothing returns nothing
local UnitStack myGroup = UnitStack.create()
call myGroup.add(CreateUnit(Player(0), 'hpea', 0., 0., 0.))
call myGroup.add(CreateUnit(Player(0), 'hpea', 0., 0., 0.))
call myGroup.add(CreateUnit(Player(0), 'hpea', 0., 0., 0.))
call myGroup.add(CreateUnit(Player(0), 'hpea', 0., 0., 0.))
call myGroup.add(CreateUnit(Player(0), 'hpea', 0., 0., 0.))
call myGroup.reset()
loop
exitwhen not myGroup.hasNext()
call SetUnitState(myGroup.getNext(), UNIT_STATE_LIFE, 123.456)
endloop
endfunction
Last edited: