- Joined
- Apr 5, 2011
- Messages
- 245
My System.
Import:
WorldBounds
Native Extension:
In-struct Arrays:
Error Stater:
Indexed Data:
very Simple Unit Indexer:
Game:
Import:
WorldBounds
Native Extension:
JASS:
//=======================
//=== NativeExtension ===
//======= v0.1000 =======
// Makes some code shorter.
library NativeExtension
globals
constant integer MAX_PLAYER_INDEX = 15
endglobals
function GroupEnumUnits takes group whichGroup, boolexpr filter returns nothing
local integer i = 0
local player p
loop
set p = Player(i)
call GroupEnumUnitsOfPlayer(whichGroup, p, filter)
exitwhen i == MAX_PLAYER_INDEX
set i = i + 1
endloop
endfunction
endlibrary
JASS:
//======================
//=== InstructArrays ===
//======= v0.900 =======
// Provides declaring arrays inside structs to exploit dot syntax.
// There is a max size declaration for arrays of this sort.
library InstructArrays
globals
private constant integer MAX_INDEX = 1023
endglobals
//! textmacro CREATE_INSTRUCTARRAY_TYPE takes TYPE
type $TYPE$Array extends $TYPE$ array[MAX_INDEX]
//! endtextmacro
//=== Create all needed in-struct array types ===
//Sample:
// runtextmacro CREATE_INSTRUCTARRAY_TYPE("<TYPE>")
//Execution:
//!runtextmacro CREATE_INSTRUCTARRAY_TYPE("boolean")
//!runtextmacro CREATE_INSTRUCTARRAY_TYPE("integer")
//!runtextmacro CREATE_INSTRUCTARRAY_TYPE("real")
//!runtextmacro CREATE_INSTRUCTARRAY_TYPE("unit")
//Use "<TYPE>Array" to declare in-struct array of <TYPE>.
endlibrary
JASS:
//===================
//=== ErrorStater ===
//===== v0.1001 =====
// Checks errors and informs about them.
library ErrorStater
//=== Settings ===
globals
private constant INFORM = true
endglobals
//================
globals
private constant string ARRAY_OUTINDEX_ERROR = "Array out index error."
private constant string ARRAY_RAWINDEX_ERROR = "Array raw index error."
endglobals
function CheckArrayIndexError takes integer index, integer max_index, integer current_size returns boolean
if index > max_index or index < 0 then
if INFORM then
call BJDebugMsg(ARRAY_OUTINDEX_ERROR)
endif
return true
endif
if index >= current_size then
if INFORM then
call BJDebugMsg(ARRAY_RAWINDEX_ERROR)
endif
return true
endif
return false
endfunction
//Operator [] uses it usually to not address with impossible index or index with no value.
function CheckArrayOutIndexError takes integer index, integer max_index returns boolean
if index > max_index or index < 0 then
if INFORM then
call BJDebugMsg(ARRAY_OUTINDEX_ERROR)
endif
return true
endif
return false
endfunction
//Operator []= uses it usually to not address with impossible index.
endlibrary
JASS:
//===================
//=== IndexedData ===
//===== v0.9003 =====
// Provides incapsulated data arrays of any allowed type.
// This type of data called indexed data type.
// Indexed data type can be specified to indexed object type with registrar
// to perform a list of objects and tools for manipulation them.
// Marker can be included to indexed object type for quick conversions
// object <-> index. Marker functions (<SETMARK_FUNC>, <GETMARK_FUNC>)
// cannot be changed dynamic, they all are determined upon compilation.
//Interface:
//| method operator[] takes integer index returns <TYPE>
//| method operator[]= takes integer index returns nothing
//| readonly integer size
//+ registrar:
//| method register takes <TYPE> object returns nothing
//| method free takes integer index returns nothing
//| method unregister takes <TYPE> object returns nothing
//+ marker:
// static constant boolean setMark
// static constant boolean getMark
library IndexedData uses InstructArrays, optional ErrorStater
globals
private constant INSTANCES_LIMIT = 10000
private constant NULL_INDEX = -1
endglobals
//! textmacro CREATE_INDEXEDDATA_TYPE takes NAME, TYPE, NULL_VALUE, OBJECTS, SETMARK_FUNC, GETMARK_FUNC
struct $NAME$[INSTANCES_LIMIT]
private $TYPE$Array data
//In-struct array format is used above
readonly integer size = 0
//=== Get data by index ===
method operator[] takes integer index returns $TYPE$
//Check an error
static if LIBRARY_ErrorStater then
if CheckArrayIndexError(index, ARRAY_MAX_INDEX, size) then
return $NULL_VALUE$
endif
endif
return data[index]
endmethod
//=== Set data by index ===
method operator[]= takes integer index, $TYPE$ data returns nothing
//Check an error
static if LIBRARY_ErrorStater then
if CheckArrayOutIndexError(index, ARRAY_MAX_INDEX) then
return
endif
endif
//Set data
set this.data[index] = data
//Apply new size
if index >= size then
set size = index + 1
endif
endmethod
static if $OBJECTS$ then
static constant boolean setMark = $SETMARK_FUNC$ != "null"
static constant boolean getMark = $GETMARK_FUNC$ != "null"
private integerArray freeIndex
private integer freeIndex_count = 0
//Check below is used at register
readonly boolean check = false
method checkOn takes nothing returns nothing
set check = true
endmethod
method checkOff takes nothing returns nothing
set check = false
endmethod
//=== Get index of object by handle ===
//Object is supposed as unique in register.
method getIndexOf takes $TYPE$ object returns integer
local integer index
static if getMark then
//Get index by mark
set index = $GETMARK_FUNC$(object)
if this[index] == object then
return index
endif
else
//Seek through array for identity
set index = 0
loop
exitwhen index == size
if data[index] == object then
return index
set index = index + 1
endloop
endif
return NULL_INDEX
endmethod
//=== Register object by handle ===
method register takes $TYPE$ object returns nothing
local integer index
//Check if there is no this object already in register
if check then
static if getMark then
//Get index supposed to accord with object by mark
set index = $GETMARK_FUNC$(object)
if this[index] == object then
return
endif
else
//Seek through array for identity
set index = 0
loop
exitwhen index == size
if data[index] == object then
return
set index = index + 1
endloop
endif
//Prepare index
if freeIndex_count > 0 then
set index = freeIndex[freeIndex_count - 1]
set freeIndex_count = freeIndex_count - 1
//Bind object to index ...
set data[index] = object
else
set index = size
//Bind object to index ... *
set this[index] = object
endif
//... and mark it if set mark function is set
if setMark then
call $SETMARK_FUNC$(object, index)
endif
endmethod
//=== Unregister object by index ===
method free takes integer index returns nothing
//Check an error
static if LIBRARY_ErrorStater then
if CheckArrayIndexError(index, ARRAY_MAX_INDEX, size) then
return
endif
endif
//Free index
if this[index] != $NULL_VALUE$ then
set data[index] = $NULL_VALUE$
set freeIndex[freeIndex_count] = index
set freeIndex_count = freeIndex_count + 1
endif
endmethod
//=== Unregister object by handle ===
method unregister takes $TYPE$ object returns nothing
call free(getIndexOf(object))
endmethod
//~ Destructor
method destroy takes nothing returns nothing
call data.destroy()
call freeIndex.destroy(
endmethod
else
//~ Destructor
method destroy takes nothing returns nothing
call data.destroy()
endmethod
endif
endstruct
//! endtextmacro
//=== Create all needed indexedData types ===
//Sample:
// runtextmacro CREATE_INDEXEDDATA_TYPE("<NAME>", "<TYPE>", "<NULL_VALUE>", "<OBJECTS>", "<SETMARK_FUNC>", "<GETMARK_FUNC>")
//Execution:
//! runtextmacro CREATE_INDEXEDDATA_TYPE("indexedUnit", "unit", "null", "true", "SetUnitUserData", "GetUnitUserData")
//! runtextmacro CREATE_INDEXEDDATA_TYPE("indexedBoolean", "boolean", "false", "false", "null", "null")
//! runtextmacro CREATE_INDEXEDDATA_TYPE("indexedInteger", "integer", "0", "false", "false", "null", "null")
//! runtextmacro CREATE_INDEXEDDATA_TYPE("indexedReal", "real", "0.", "false", "false", "null", "null")
endlibrary
JASS:
//=============================
//=== verySimpleUnitIndexer ===
//========== v0.1000 ==========
library vSUI initializer Init uses IndexedData, NativeExtension, WorldBounds
//=== Settings ===
globals
private constant boolean INSTANT_INIT = true
private constant boolean REGISTER_START_UNITS = true
endglobals
//================
globals
indexedUnit Unit
endglobals
struct vSUI
private static boolean enabled = false
private static trigger t
//=== Register filtered unit ===
private static method registerFilter takes nothing returns boolean
call Unit.register(GetFilterUnit())
return false
endmethod
//=== Register all units present at this moment ===
static method registerPresent takes nothing returns nothing
local group g = CreateGroup()
call GroupEnumUnits(g, Condition(function registerFilter))
call DestroyGroup(g)
set g = null
endmethod
static method turnOn takes nothing returns nothing
if not enabled then
call EnableTrigger(t)
set enabled = true
endif
endmethod
static method turnOff takes nothing returns nothing
if enabled then
set enabled = false
call DisableTrigger(t)
endif
endmethod
//=== Register start units and turn on dynamic register ===
static method start takes nothing returns nothing
static if not INSTANT_INIT then
set Unit = Unit.create()
set vSUI.t = CreateTrigger()
endif
static if REGISTER_START_UNITS then
call registerPresent()
endif
call TriggerRegisterEnterRegion(t, WorldBounds.worldRegion, Condition(function registerFilter))
set enabled = true
endmethod
endstruct
function Init takes nothing returns nothing
//Create all needed dynamic variables if vSUI init is set instant
static if INSTANT_INIT then
set Unit = Unit.create()
set vSUI.t = CreateTrigger()
endif
endfunction
endlibrary
JASS:
//============
//=== Game ===
//============
library Game initializer Init uses vSUI
globals
endglobals
function GameOn takes nothing returns nothing
call DestroyTimer(GetExpiredTimer())
call vSUI.start()
endfunction
function Init takes nothing returns nothing
call TimerStart(CreateTimer(), 0, false, function GameOn)
endfunction
endlibrary
Last edited: