mirror of
https://github.com/SPLURT-Station/S.P.L.U.R.T-Station-13.git
synced 2025-12-10 18:02:57 +00:00
686 lines
19 KiB
Plaintext
686 lines
19 KiB
Plaintext
/*
|
|
* Holds procs to help with list operations
|
|
* Contains groups:
|
|
* Misc
|
|
* Sorting
|
|
*/
|
|
|
|
/*
|
|
* Misc
|
|
*/
|
|
|
|
#define LAZYINITLIST(L) if (!L) L = list()
|
|
#define UNSETEMPTY(L) if (L && !length(L)) L = null
|
|
#define LAZYCOPY(L) (L ? L.Copy() : list() )
|
|
#define LAZYREMOVE(L, I) if(L) { L -= I; if(!length(L)) { L = null; } }
|
|
#define LAZYADD(L, I) if(!L) { L = list(); } L += I;
|
|
#define LAZYOR(L, I) if(!L) { L = list(); } L |= I;
|
|
#define LAZYFIND(L, V) L ? L.Find(V) : 0
|
|
#define LAZYACCESS(L, I) (L ? (isnum(I) ? (I > 0 && I <= length(L) ? L[I] : null) : L[I]) : null)
|
|
#define LAZYSET(L, K, V) if(!L) { L = list(); } L[K] = V;
|
|
#define LAZYLEN(L) length(L)
|
|
#define LAZYCLEARLIST(L) if(L) L.Cut()
|
|
#define SANITIZE_LIST(L) ( islist(L) ? L : list() )
|
|
#define reverseList(L) reverseRange(L.Copy())
|
|
#define LAZYADDASSOC(L, K, V) if(!L) { L = list(); } L[K] += list(V);
|
|
#define LAZYREMOVEASSOC(L, K, V) if(L) { if(L[K]) { L[K] -= V; if(!length(L[K])) L -= K; } if(!length(L)) L = null; }
|
|
|
|
/// Passed into BINARY_INSERT to compare keys
|
|
#define COMPARE_KEY __BIN_LIST[__BIN_MID]
|
|
/// Passed into BINARY_INSERT to compare values
|
|
#define COMPARE_VALUE __BIN_LIST[__BIN_LIST[__BIN_MID]]
|
|
|
|
/****
|
|
* Binary search sorted insert
|
|
* INPUT: Object to be inserted
|
|
* LIST: List to insert object into
|
|
* TYPECONT: The typepath of the contents of the list
|
|
* COMPARE: The object to compare against, usualy the same as INPUT
|
|
* COMPARISON: The variable on the objects to compare
|
|
* COMPTYPE: How the current bin item to compare against COMPARE is fetched. By key or value.
|
|
*/
|
|
#define BINARY_INSERT(INPUT, LIST, TYPECONT, COMPARE, COMPARISON, COMPTYPE) \
|
|
do {\
|
|
var/list/__BIN_LIST = LIST;\
|
|
var/__BIN_CTTL = length(__BIN_LIST);\
|
|
if(!__BIN_CTTL) {\
|
|
__BIN_LIST += INPUT;\
|
|
} else {\
|
|
var/__BIN_LEFT = 1;\
|
|
var/__BIN_RIGHT = __BIN_CTTL;\
|
|
var/__BIN_MID = (__BIN_LEFT + __BIN_RIGHT) >> 1;\
|
|
var/##TYPECONT/__BIN_ITEM;\
|
|
while(__BIN_LEFT < __BIN_RIGHT) {\
|
|
__BIN_ITEM = COMPTYPE;\
|
|
if(__BIN_ITEM.##COMPARISON <= COMPARE.##COMPARISON) {\
|
|
__BIN_LEFT = __BIN_MID + 1;\
|
|
} else {\
|
|
__BIN_RIGHT = __BIN_MID;\
|
|
};\
|
|
__BIN_MID = (__BIN_LEFT + __BIN_RIGHT) >> 1;\
|
|
};\
|
|
__BIN_ITEM = COMPTYPE;\
|
|
__BIN_MID = __BIN_ITEM.##COMPARISON > COMPARE.##COMPARISON ? __BIN_MID : __BIN_MID + 1;\
|
|
__BIN_LIST.Insert(__BIN_MID, INPUT);\
|
|
};\
|
|
} while(FALSE)
|
|
|
|
//Returns a list in plain english as a string
|
|
/proc/english_list(list/input, nothing_text = "nothing", and_text = " and ", comma_text = ", ", final_comma_text = "" )
|
|
var/total = input.len
|
|
if (!total)
|
|
return nothing_text
|
|
else if (total == 1)
|
|
return "[input[1]]"
|
|
else if (total == 2)
|
|
return "[input[1]][and_text][input[2]]"
|
|
else
|
|
var/output = ""
|
|
var/index = 1
|
|
while (index < total)
|
|
if (index == total - 1)
|
|
comma_text = final_comma_text
|
|
|
|
output += "[input[index]][comma_text]"
|
|
index++
|
|
|
|
return "[output][and_text][input[index]]"
|
|
|
|
//Returns list element or null. Should prevent "index out of bounds" error.
|
|
/proc/listgetindex(list/L, index)
|
|
if(LAZYLEN(L))
|
|
if(isnum(index) && ISINTEGER(index))
|
|
if(ISINRANGE(index,1,L.len))
|
|
return L[index]
|
|
else if(index in L)
|
|
return L[index]
|
|
return
|
|
|
|
//Return either pick(list) or null if list is not of type /list or is empty
|
|
/proc/safepick(list/L)
|
|
if(LAZYLEN(L))
|
|
return pick(L)
|
|
|
|
//Checks if the list is empty
|
|
/proc/isemptylist(list/L)
|
|
if(!L.len)
|
|
return TRUE
|
|
return FALSE
|
|
|
|
//Checks for specific types in a list
|
|
/proc/is_type_in_list(atom/A, list/L)
|
|
if(!LAZYLEN(L) || !A)
|
|
return FALSE
|
|
for(var/type in L)
|
|
if(istype(A, type))
|
|
return TRUE
|
|
return FALSE
|
|
|
|
//Checks for specific types in specifically structured (Assoc "type" = TRUE) lists ('typecaches')
|
|
#define is_type_in_typecache(A, L) (A && length(L) && L[(ispath(A) ? A : A:type)])
|
|
|
|
//Checks for a string in a list
|
|
/proc/is_string_in_list(string, list/L)
|
|
if(!LAZYLEN(L) || !string)
|
|
return
|
|
for(var/V in L)
|
|
if(string == V)
|
|
return TRUE
|
|
return
|
|
|
|
//Removes a string from a list
|
|
/proc/remove_strings_from_list(string, list/L)
|
|
if(!LAZYLEN(L) || !string)
|
|
return
|
|
for(var/V in L)
|
|
if(V == string)
|
|
L -= V //No return here so that it removes all strings of that type
|
|
return
|
|
|
|
//returns a new list with only atoms that are in typecache L
|
|
/proc/typecache_filter_list(list/atoms, list/typecache)
|
|
RETURN_TYPE(/list)
|
|
. = list()
|
|
for(var/thing in atoms)
|
|
var/atom/A = thing
|
|
if (typecache[A.type])
|
|
. += A
|
|
|
|
/proc/typecache_filter_list_reverse(list/atoms, list/typecache)
|
|
RETURN_TYPE(/list)
|
|
. = list()
|
|
for(var/thing in atoms)
|
|
var/atom/A = thing
|
|
if(!typecache[A.type])
|
|
. += A
|
|
|
|
/proc/typecache_filter_multi_list_exclusion(list/atoms, list/typecache_include, list/typecache_exclude)
|
|
. = list()
|
|
for(var/thing in atoms)
|
|
var/atom/A = thing
|
|
if(typecache_include[A.type] && !typecache_exclude[A.type])
|
|
. += A
|
|
|
|
//Like typesof() or subtypesof(), but returns a typecache instead of a list
|
|
/proc/typecacheof(path, ignore_root_path, only_root_path = FALSE)
|
|
if(ispath(path))
|
|
var/list/types = list()
|
|
if(only_root_path)
|
|
types = list(path)
|
|
else
|
|
types = ignore_root_path ? subtypesof(path) : typesof(path)
|
|
var/list/L = list()
|
|
for(var/T in types)
|
|
L[T] = TRUE
|
|
return L
|
|
else if(islist(path))
|
|
var/list/pathlist = path
|
|
var/list/L = list()
|
|
if(ignore_root_path)
|
|
for(var/P in pathlist)
|
|
for(var/T in subtypesof(P))
|
|
L[T] = TRUE
|
|
else
|
|
for(var/P in pathlist)
|
|
if(only_root_path)
|
|
L[P] = TRUE
|
|
else
|
|
for(var/T in typesof(P))
|
|
L[T] = TRUE
|
|
return L
|
|
|
|
/proc/typecacheof_assoc_list(list/pathlist, ignore_root_path = FALSE)
|
|
. = list()
|
|
if(!istype(pathlist))
|
|
return
|
|
for(var/P in pathlist)
|
|
var/value = pathlist[P]
|
|
for(var/T in (ignore_root_path ? subtypesof(P) : typesof(P)))
|
|
.[T] = value
|
|
|
|
//Empties the list by setting the length to 0. Hopefully the elements get garbage collected
|
|
/proc/clearlist(list/list)
|
|
if(istype(list))
|
|
list.len = 0
|
|
return
|
|
|
|
//Removes any null entries from the list
|
|
//Returns TRUE if the list had nulls, FALSE otherwise
|
|
/proc/listclearnulls(list/L)
|
|
var/start_len = L.len
|
|
var/list/N = new(start_len)
|
|
L -= N
|
|
return L.len < start_len
|
|
|
|
/*
|
|
* Returns list containing all the entries from first list that are not present in second.
|
|
* If skiprep = 1, repeated elements are treated as one.
|
|
* If either of arguments is not a list, returns null
|
|
*/
|
|
/proc/difflist(list/first, list/second, skiprep=0)
|
|
if(!islist(first) || !islist(second))
|
|
return
|
|
var/list/result = new
|
|
if(skiprep)
|
|
for(var/e in first)
|
|
if(!(e in result) && !(e in second))
|
|
result += e
|
|
else
|
|
result = first - second
|
|
return result
|
|
|
|
/*
|
|
* Returns list containing entries that are in either list but not both.
|
|
* If skipref = 1, repeated elements are treated as one.
|
|
* If either of arguments is not a list, returns null
|
|
*/
|
|
/proc/uniquemergelist(list/first, list/second, skiprep=0)
|
|
if(!islist(first) || !islist(second))
|
|
return
|
|
var/list/result = new
|
|
if(skiprep)
|
|
result = difflist(first, second, skiprep)+difflist(second, first, skiprep)
|
|
else
|
|
result = first ^ second
|
|
return result
|
|
|
|
//Picks a random element from a list based on a weighting system:
|
|
//1. Adds up the total of weights for each element
|
|
//2. Gets the total from 0% to 100% of previous total value.
|
|
//3. For each element in the list, subtracts its weighting from that number
|
|
//4. If that makes the number 0 or less, return that element.
|
|
/proc/pickweight(list/L, base_weight = 1)
|
|
var/total = 0
|
|
var/item
|
|
for (item in L)
|
|
if (!L[item])
|
|
L[item] = base_weight
|
|
total += L[item]
|
|
|
|
total = rand() * total
|
|
for (item in L)
|
|
total -= L[item]
|
|
if (total <= 0)
|
|
return item
|
|
|
|
//Picks a number of elements from a list based on weight.
|
|
//This is highly optimised and good for things like grabbing 200 items from a list of 40,000
|
|
//Much more efficient than many pickweight calls
|
|
/proc/pickweight_mult(list/L, quantity, base_weight = 1)
|
|
//First we total the list as normal
|
|
var/total = 0
|
|
var/item
|
|
for (item in L)
|
|
if (!L[item])
|
|
L[item] = base_weight
|
|
total += L[item]
|
|
|
|
//Next we will make a list of randomly generated numbers, called Requests
|
|
//It is critical that this list be sorted in ascending order, so we will build it in that order
|
|
//First one is free, so we start counting at 2
|
|
var/list/requests = list(rand(1, total))
|
|
for (var/i in 2 to quantity)
|
|
//Each time we generate the next request
|
|
var/newreq = rand()* total
|
|
//We will loop through all existing requests
|
|
for (var/j in 1 to requests.len)
|
|
//We keep going through the list until we find an element which is bigger than the one we want to add
|
|
if (requests[j] > newreq)
|
|
//And then we insert the newqreq at that point, pushing everything else forward
|
|
requests.Insert(j, newreq)
|
|
break
|
|
|
|
|
|
|
|
//Now when we get here, we have a list of random numbers sorted in ascending order.
|
|
//The length of that list is equal to Quantity passed into this function
|
|
//Next we make a list to store results
|
|
var/list/results = list()
|
|
|
|
//Zero the total, we'll reuse it
|
|
total = 0
|
|
|
|
//Now we will iterate forward through the items list, adding each weight to the total
|
|
for (item in L)
|
|
total += L[item]
|
|
|
|
//After each item we do a while loop
|
|
while (requests.len && total >= requests[1])
|
|
//If the total is higher than the value of the first request
|
|
results += item //We add this item to the results list
|
|
requests.Cut(1,2) //And we cut off the top of the requests list
|
|
|
|
//This while loop will repeat until the next request is higher than the total.
|
|
//The current item might be added to the results list many times, in this process
|
|
|
|
//By the time we get here:
|
|
//Requests will be empty
|
|
//Results will have a length of quality
|
|
return results
|
|
|
|
//Pick a random element from the list and remove it from the list.
|
|
/proc/pick_n_take(list/L)
|
|
RETURN_TYPE(L[_].type)
|
|
if(L.len)
|
|
var/picked = rand(1,L.len)
|
|
. = L[picked]
|
|
L.Cut(picked,picked+1) //Cut is far more efficient that Remove()
|
|
|
|
//Pick a random element from the list by weight and remove it from the list.
|
|
//Result is returned as a list in the format list(key, value)
|
|
/proc/pickweight_n_take(list/L, base_weight = 1)
|
|
if (L.len)
|
|
. = pickweight(L, base_weight)
|
|
L.Remove(.)
|
|
|
|
//Returns the top(last) element from the list and removes it from the list (typical stack function)
|
|
/proc/pop(list/L)
|
|
if(L.len)
|
|
. = L[L.len]
|
|
L.len--
|
|
|
|
/proc/popleft(list/L)
|
|
if(L.len)
|
|
. = L[1]
|
|
L.Cut(1,2)
|
|
|
|
/proc/sorted_insert(list/L, thing, comparator)
|
|
var/pos = L.len
|
|
while(pos > 0 && call(comparator)(thing, L[pos]) > 0)
|
|
pos--
|
|
L.Insert(pos+1, thing)
|
|
|
|
// Returns the next item in a list
|
|
/proc/next_list_item(item, list/L)
|
|
var/i
|
|
i = L.Find(item)
|
|
if(i == L.len)
|
|
i = 1
|
|
else
|
|
i++
|
|
return L[i]
|
|
|
|
// Returns the previous item in a list
|
|
/proc/previous_list_item(item, list/L)
|
|
var/i
|
|
i = L.Find(item)
|
|
if(i == 1)
|
|
i = L.len
|
|
else
|
|
i--
|
|
return L[i]
|
|
|
|
//Randomize: Return the list in a random order
|
|
/proc/shuffle(list/L)
|
|
if(!L)
|
|
return
|
|
L = L.Copy()
|
|
|
|
for(var/i=1, i<L.len, ++i)
|
|
L.Swap(i,rand(i,L.len))
|
|
|
|
return L
|
|
|
|
//same, but returns nothing and acts on list in place
|
|
/proc/shuffle_inplace(list/L)
|
|
if(!L)
|
|
return
|
|
|
|
for(var/i=1, i<L.len, ++i)
|
|
L.Swap(i,rand(i,L.len))
|
|
|
|
//Return a list with no duplicate entries
|
|
/proc/uniqueList(list/L)
|
|
. = list()
|
|
for(var/i in L)
|
|
. |= i
|
|
|
|
//same, but returns nothing and acts on list in place (also handles associated values properly)
|
|
/proc/uniqueList_inplace(list/L)
|
|
var/temp = L.Copy()
|
|
L.len = 0
|
|
for(var/key in temp)
|
|
if (isnum(key))
|
|
L |= key
|
|
else
|
|
L[key] = temp[key]
|
|
|
|
//for sorting clients or mobs by ckey
|
|
/proc/sortKey(list/L, order=1)
|
|
return sortTim(L, order >= 0 ? /proc/cmp_ckey_asc : /proc/cmp_ckey_dsc)
|
|
|
|
//Specifically for record datums in a list.
|
|
/proc/sortRecord(list/L, field = "name", order = 1)
|
|
GLOB.cmp_field = field
|
|
return sortTim(L, order >= 0 ? /proc/cmp_records_asc : /proc/cmp_records_dsc)
|
|
|
|
//any value in a list
|
|
/proc/sortList(list/L, cmp=/proc/cmp_text_asc)
|
|
return sortTim(L.Copy(), cmp)
|
|
|
|
//uses sortList() but uses the var's name specifically. This should probably be using mergeAtom() instead
|
|
/proc/sortNames(list/L, order=1)
|
|
return sortTim(L, order >= 0 ? /proc/cmp_name_asc : /proc/cmp_name_dsc)
|
|
|
|
|
|
//Converts a bitfield to a list of numbers (or words if a wordlist is provided)
|
|
/proc/bitfield2list(bitfield = 0, list/wordlist)
|
|
var/list/r = list()
|
|
if(islist(wordlist))
|
|
var/max = min(wordlist.len,16)
|
|
var/bit = 1
|
|
for(var/i=1, i<=max, i++)
|
|
if(bitfield & bit)
|
|
r += wordlist[i]
|
|
bit = bit << 1
|
|
else
|
|
for(var/bit=1, bit<=65535, bit = bit << 1)
|
|
if(bitfield & bit)
|
|
r += bit
|
|
|
|
return r
|
|
|
|
// Returns the key based on the index
|
|
#define KEYBYINDEX(L, index) (((index <= length(L)) && (index > 0)) ? L[index] : null)
|
|
|
|
/proc/count_by_type(list/L, type)
|
|
var/i = 0
|
|
for(var/T in L)
|
|
if(istype(T, type))
|
|
i++
|
|
return i
|
|
|
|
/proc/count_occurences_of_value(list/L, val, limit) //special thanks to salmonsnake
|
|
. = 0
|
|
for (var/i in 1 to limit)
|
|
if (L[i] == val)
|
|
.++
|
|
|
|
/proc/find_record(field, value, list/L)
|
|
for(var/datum/data/record/R in L)
|
|
if(R.fields[field] == value)
|
|
return R
|
|
|
|
|
|
//Move a single element from position fromIndex within a list, to position toIndex
|
|
//All elements in the range [1,toIndex) before the move will be before the pivot afterwards
|
|
//All elements in the range [toIndex, L.len+1) before the move will be after the pivot afterwards
|
|
//In other words, it's as if the range [fromIndex,toIndex) have been rotated using a <<< operation common to other languages.
|
|
//fromIndex and toIndex must be in the range [1,L.len+1]
|
|
//This will preserve associations ~Carnie
|
|
/proc/moveElement(list/L, fromIndex, toIndex)
|
|
if(fromIndex == toIndex || fromIndex+1 == toIndex) //no need to move
|
|
return
|
|
if(fromIndex > toIndex)
|
|
++fromIndex //since a null will be inserted before fromIndex, the index needs to be nudged right by one
|
|
|
|
L.Insert(toIndex, null)
|
|
L.Swap(fromIndex, toIndex)
|
|
L.Cut(fromIndex, fromIndex+1)
|
|
|
|
|
|
//Move elements [fromIndex,fromIndex+len) to [toIndex-len, toIndex)
|
|
//Same as moveElement but for ranges of elements
|
|
//This will preserve associations ~Carnie
|
|
/proc/moveRange(list/L, fromIndex, toIndex, len=1)
|
|
var/distance = abs(toIndex - fromIndex)
|
|
if(len >= distance) //there are more elements to be moved than the distance to be moved. Therefore the same result can be achieved (with fewer operations) by moving elements between where we are and where we are going. The result being, our range we are moving is shifted left or right by dist elements
|
|
if(fromIndex <= toIndex)
|
|
return //no need to move
|
|
fromIndex += len //we want to shift left instead of right
|
|
|
|
for(var/i=0, i<distance, ++i)
|
|
L.Insert(fromIndex, null)
|
|
L.Swap(fromIndex, toIndex)
|
|
L.Cut(toIndex, toIndex+1)
|
|
else
|
|
if(fromIndex > toIndex)
|
|
fromIndex += len
|
|
|
|
for(var/i=0, i<len, ++i)
|
|
L.Insert(toIndex, null)
|
|
L.Swap(fromIndex, toIndex)
|
|
L.Cut(fromIndex, fromIndex+1)
|
|
|
|
//Move elements from [fromIndex, fromIndex+len) to [toIndex, toIndex+len)
|
|
//Move any elements being overwritten by the move to the now-empty elements, preserving order
|
|
//Note: if the two ranges overlap, only the destination order will be preserved fully, since some elements will be within both ranges ~Carnie
|
|
/proc/swapRange(list/L, fromIndex, toIndex, len=1)
|
|
var/distance = abs(toIndex - fromIndex)
|
|
if(len > distance) //there is an overlap, therefore swapping each element will require more swaps than inserting new elements
|
|
if(fromIndex < toIndex)
|
|
toIndex += len
|
|
else
|
|
fromIndex += len
|
|
|
|
for(var/i=0, i<distance, ++i)
|
|
L.Insert(fromIndex, null)
|
|
L.Swap(fromIndex, toIndex)
|
|
L.Cut(toIndex, toIndex+1)
|
|
else
|
|
if(toIndex > fromIndex)
|
|
var/a = toIndex
|
|
toIndex = fromIndex
|
|
fromIndex = a
|
|
|
|
for(var/i=0, i<len, ++i)
|
|
L.Swap(fromIndex++, toIndex++)
|
|
|
|
//replaces reverseList ~Carnie
|
|
/proc/reverseRange(list/L, start=1, end=0)
|
|
if(L.len)
|
|
start = start % L.len
|
|
end = end % (L.len+1)
|
|
if(start <= 0)
|
|
start += L.len
|
|
if(end <= 0)
|
|
end += L.len + 1
|
|
|
|
--end
|
|
while(start < end)
|
|
L.Swap(start++,end--)
|
|
|
|
return L
|
|
|
|
|
|
//return first thing in L which has var/varname == value
|
|
//this is typecaste as list/L, but you could actually feed it an atom instead.
|
|
//completely safe to use
|
|
/proc/getElementByVar(list/L, varname, value)
|
|
varname = "[varname]"
|
|
for(var/datum/D in L)
|
|
if(D.vars.Find(varname))
|
|
if(D.vars[varname] == value)
|
|
return D
|
|
|
|
//Copies a list, and all lists inside it recusively
|
|
//Does not copy any other reference type
|
|
/proc/deepCopyList(list/l)
|
|
if(!islist(l))
|
|
return l
|
|
. = l.Copy()
|
|
for(var/i = 1 to l.len)
|
|
var/key = .[i]
|
|
if(isnum(key))
|
|
// numbers cannot ever be associative keys
|
|
continue
|
|
var/value = .[key]
|
|
if(islist(value))
|
|
value = deepCopyList(value)
|
|
.[key] = value
|
|
if(islist(key))
|
|
key = deepCopyList(key)
|
|
.[i] = key
|
|
.[key] = value
|
|
|
|
//takes an input_key, as text, and the list of keys already used, outputting a replacement key in the format of "[input_key] ([number_of_duplicates])" if it finds a duplicate
|
|
//use this for lists of things that might have the same name, like mobs or objects, that you plan on giving to a player as input
|
|
/proc/avoid_assoc_duplicate_keys(input_key, list/used_key_list)
|
|
if(!input_key || !istype(used_key_list))
|
|
return
|
|
if(used_key_list[input_key])
|
|
used_key_list[input_key]++
|
|
input_key = "[input_key] ([used_key_list[input_key]])"
|
|
else
|
|
used_key_list[input_key] = 1
|
|
return input_key
|
|
|
|
#if DM_VERSION > 513
|
|
#error Remie said that lummox was adding a way to get a lists
|
|
#error contents via list.values, if that is true remove this
|
|
#error otherwise, update the version and bug lummox
|
|
#endif
|
|
//Flattens a keyed list into a list of it's contents
|
|
/proc/flatten_list(list/key_list)
|
|
if(!islist(key_list))
|
|
return null
|
|
. = list()
|
|
for(var/key in key_list)
|
|
. |= key_list[key]
|
|
|
|
/proc/make_associative(list/flat_list)
|
|
. = list()
|
|
for(var/thing in flat_list)
|
|
.[thing] = TRUE
|
|
|
|
/proc/deep_list2params(list/deep_list)
|
|
var/list/L = list()
|
|
for(var/i in deep_list)
|
|
var/key = i
|
|
if(isnum(key))
|
|
L += "[key]"
|
|
continue
|
|
if(islist(key))
|
|
key = deep_list2params(key)
|
|
else if(!istext(key))
|
|
key = "[REF(key)]"
|
|
L += "[key]"
|
|
var/value = deep_list[key]
|
|
if(!isnull(value))
|
|
if(islist(value))
|
|
value = deep_list2params(value)
|
|
else if(!(istext(key) || isnum(key)))
|
|
value = "[REF(value)]"
|
|
L["[key]"] = "[value]"
|
|
return list2params(L)
|
|
|
|
#define NUMLIST2TEXTLIST(list) splittext(list2params(list), "&")
|
|
|
|
//Picks from the list, with some safeties, and returns the "default" arg if it fails
|
|
#define DEFAULTPICK(L, default) ((islist(L) && length(L)) ? pick(L) : default)
|
|
|
|
/* Definining a counter as a series of key -> numeric value entries
|
|
|
|
* All these procs modify in place.
|
|
*/
|
|
|
|
/proc/counterlist_scale(list/L, scalar)
|
|
var/list/out = list()
|
|
for(var/key in L)
|
|
out[key] = L[key] * scalar
|
|
. = out
|
|
|
|
/proc/counterlist_sum(list/L)
|
|
. = 0
|
|
for(var/key in L)
|
|
. += L[key]
|
|
|
|
/proc/counterlist_normalise(list/L)
|
|
var/avg = counterlist_sum(L)
|
|
if(avg != 0)
|
|
. = counterlist_scale(L, 1 / avg)
|
|
else
|
|
. = L
|
|
|
|
/proc/counterlist_combine(list/L1, list/L2)
|
|
for(var/key in L2)
|
|
var/other_value = L2[key]
|
|
if(key in L1)
|
|
L1[key] += other_value
|
|
else
|
|
L1[key] = other_value
|
|
|
|
/proc/assoc_list_strip_value(list/input)
|
|
var/list/ret = list()
|
|
for(var/key in input)
|
|
ret += key
|
|
return ret
|
|
|
|
/proc/is_type_in_ref_list(path, list/L)
|
|
if(!ispath(path))//not a path
|
|
return
|
|
for(var/i in L)
|
|
var/datum/D = i
|
|
if(!istype(D))//not an usable reference
|
|
continue
|
|
if(istype(D, path))
|
|
return TRUE
|
|
|
|
/proc/safe_json_encode(list/L, default = "")
|
|
. = default
|
|
return json_encode(L)
|
|
|
|
/proc/safe_json_decode(string, default = list())
|
|
. = default
|
|
return json_decode(string)
|