/* * Holds procs to help with list operations * Contains groups: * Misc * Sorting */ /* * Misc */ //Returns a list in plain english as a string /proc/english_list(var/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]]" /proc/ConvertReqString2List(var/list/source_list) var/list/temp_list = params2list(source_list) for(var/O in temp_list) temp_list[O] = text2num(temp_list[O]) return temp_list //Checks for specific types in a list /proc/is_type_in_list(var/atom/A, var/list/L) for(var/type in L) if(istype(A, type)) return 1 return 0 /proc/instances_of_type_in_list(atom/A, list/L, strict = FALSE) . = 0 if (strict) for (var/type in L) if (type == A.type) .++ else for(var/type in L) if(istype(A, type)) .++ //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(var/list/first, var/list/second, var/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(var/list/first, var/list/second, var/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 by weight from a list. The list must be correctly constructed in this format: //mylist[myelement1] = myweight1 //mylist[myelement2] = myweight2 //The proc will return the element index, and not the weight. /proc/pickweight(list/L) var/total = 0 var/item for (item in L) if (isnull(L[item])) //Change by nanako, a default weight will no longer overwrite an explicitly set weight of 0 //It will only use a default if no weight is defined L[item] = 1 total += L[item] total = rand() * total//Fix by nanako, allows it to handle noninteger weights for (item in L) total -= L[item] if (total <= 0) return item return null //Pick a random element from the list and remove it from the list. /proc/pick_n_take(list/listfrom) if (listfrom.len > 0) var/picked = pick(listfrom) listfrom -= picked return picked return null //Returns the top(last) element from the list and removes it from the list (typical stack function) /proc/pop(list/listfrom) if (listfrom.len > 0) var/picked = listfrom[listfrom.len] listfrom.len-- return picked return null //Returns the next element in parameter list after first appearance of parameter element. If it is the last element of the list or not present in list, returns first element. /proc/next_in_list(element, list/L) for(var/i=1, i= 1; i--) output += L[i] return output //Randomize: Return the list in a random order /proc/shuffle(var/list/L) if(!L) return L = L.Copy() for(var/i=1; i 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 toIndex) fromIndex += len for(var/i=0, i