// Variables
// -> real array udg_ListValues
// -> location array udg_ListArray
// Conversions
function R2L takes real r returns location
return r
return null
endfunction
function I2L takes integer i returns location
return i
return null
endfunction
function ItoR takes integer i returns real
return i
return 0.
endfunction
function RtoI takes real r returns integer
return r
return 0
endfunction
function H2I takes handle h returns integer
return h
return 0
endfunction
function H2R takes handle h returns real
return h
return 0.
endfunction
// List Functions
function NewList takes integer index,real value returns location
// Generates a new list and adds a first item to it
return Location(0,H2R(Location(ItoR(index),value)))
endfunction
function ListAdd takes location list,integer index,real value returns nothing
// Adds a new element behind the first element of the list
call MoveLocation(list,H2R(Location(GetLocationX(list),H2R(Location(ItoR(index),value)))),GetLocationY(list))
endfunction
function ListGetRem takes location list,integer index returns real
// Returns the value from a specific index saved in a list
// and removes the item
local location data
local location lastreferrer=list
local location referrer=list
local integer i
local real r
loop
exitwhen referrer==null
set data=R2L(GetLocationY(referrer))
set i=RtoI(GetLocationX(data))
if i==index then
set r=GetLocationY(data)
if referrer==list then
set referrer=R2L(GetLocationX(referrer))
if referrer==null then
set referrer=list
else
call MoveLocation(list,GetLocationX(referrer),GetLocationY(referrer))
endif
else
call MoveLocation(lastreferrer,GetLocationX(referrer),GetLocationY(lastreferrer))
endif
call RemoveLocation(data)
call RemoveLocation(referrer)
set referrer=null
set lastreferrer=null
set data=null
return r
endif
set lastreferrer=referrer
set referrer=R2L(GetLocationX(referrer))
endloop
set lastreferrer=null
set data=null
return 0.
endfunction
function ListGet takes location list,integer index returns real
// Returns the value from a specific index saved in a list
local location data
local location referrer=list
local integer i
local real r
loop
exitwhen referrer==null
set data=R2L(GetLocationY(referrer))
set i=RtoI(GetLocationX(data))
if i==index then
set r=GetLocationY(data)
set data=null
set referrer=null
return r
endif
set referrer=R2L(GetLocationX(referrer))
endloop
set data=null
return 0.
endfunction
function ListSet takes location list,integer index,real value returns nothing
// Changes an existing item with the given index
local location data
local location referrer=list
local integer i
loop
exitwhen referrer==null
set data=R2L(GetLocationY(referrer))
set i=RtoI(GetLocationX(data))
if i==index then
call MoveLocation(data,GetLocationX(data),value)
set referrer=null
set data=null
return
endif
set referrer=R2L(GetLocationX(referrer))
endloop
set data=null
call ListAdd(list,index,value)
endfunction
function ReadListRem takes location list returns nothing
// Saves the values of a list into a global array
// This cannot save values with an index higher than 8192
// Afterwards destroys the list
local location data
local location referrer=R2L(GetLocationX(list))
local integer i
local real r
loop
exitwhen referrer==null
set data=R2L(GetLocationY(referrer))
set i=RtoI(GetLocationX(data))
set udg_ListValues[i]=GetLocationY(data)
set r = GetLocationX(referrer)
call RemoveLocation(data)
call RemoveLocation(referrer)
set referrer=R2L(r)
endloop
set data=null
endfunction
function ReadList takes location list returns nothing
// Saves the values of a list into a global array
// This cannot save values with an index higher than 8192
local location data
local location referrer=R2L(GetLocationX(list))
local integer i
loop
exitwhen referrer==null
set data=R2L(GetLocationY(referrer))
set i=RtoI(GetLocationX(data))
set udg_ListValues[i]=GetLocationY(data)
set referrer=R2L(GetLocationX(referrer))
endloop
set data=null
endfunction
function DestroyList takes location list returns nothing
// Removes all items and the list
local location referrer=list
local location data
local real r
loop
exitwhen referrer==null
set data=R2L(GetLocationY(referrer))
set r = GetLocationX(referrer)
call RemoveLocation(data)
call RemoveLocation(referrer)
set referrer=R2L(r)
endloop
set data=null
endfunction
// Handle Functions
function GetHandleList takes handle h returns location
// Returns the list attached to the given handle
local integer handleid=H2I(h)
return ListGet(udg_ListArray[ModuloInteger(handleid,8192)],handleid/8192)
return null
endfunction
function UnsetHandleList takes handle h returns nothing
// Removes the list from the handle
// This does not destroy the list itself
local integer handleid=H2I(h)
call ListGetRem(udg_ListArray[ModuloInteger(handleid,8192)],handleid/8192)
endfunction
function SetHandleList takes handle h,location list returns nothing
// Attachs a list to the given handle
local integer handleid=H2I(h)
local integer i = ModuloInteger(handleid,8192)
if udg_ListArray[i]==null then
set udg_ListArray[i] = Location(0,H2R(Location(ItoR(handleid/8192),H2R(list))))
else
call ListAdd(udg_ListArray[i],handleid/8192,H2R(list))
endif
endfunction
function NewHandleList takes handle h,integer index,real value returns location
// Creates a list with a first item and instantly attachs it to the given handle
local integer handleid=H2I(h)
local integer i = ModuloInteger(handleid,8192)
local real r=H2R(Location(0,H2R(Location(ItoR(index),value))))
if udg_ListArray[i]==null then
set udg_ListArray[i] = Location(0,H2R(Location(ItoR(handleid/8192),r)))
else
call ListAdd(udg_ListArray[i],handleid/8192,r)
endif
return r
return null
endfunction
// Execution Helpers
function ListGetLocationRem takes location list,integer index returns location
return ListGetRem(list,index)
return null
endfunction
function ListGetLocation takes location list,integer index returns location
return ListGet(list,index)
return null
endfunction
function ListGetUnitRem takes location list,integer index returns unit
return ListGetRem(list,index)
return null
endfunction
function ListGetUnit takes location list,integer index returns unit
return ListGet(list,index)
return null
endfunction
function ListGetGroupRem takes location list,integer index returns group
return ListGetRem(list,index)
return null
endfunction
function ListGetGroup takes location list,integer index returns group
return ListGet(list,index)
return null
endfunction
function ListGetEffectRem takes location list,integer index returns effect
return ListGetRem(list,index)
return null
endfunction
function ListGetEffect takes location list,integer index returns effect
return ListGet(list,index)
return null
endfunction
function ListSetHandle takes location list, integer index,handle value returns nothing
call ListSet(list,index,H2R(value))
endfunction
function NewHandleListHandle takes handle h,integer index,handle value returns location
return NewHandleList(h,index,H2R(value))
endfunction
|