mirror of
https://github.com/Aurorastation/Aurora.3.git
synced 2025-12-21 15:42:35 +00:00
* Replace SSingulo with SScalamity; processing cleanup
- SScalamity now handles blobs as well as singularity-types.
- Processing subtypes have been simplified to not require a stop_processing definition.
* this is probably important
* Remove cascade disabling SSgarbage
* Fixed a bug where dust() did not qdel the target mob
* Misc fixes
* Fix decals surviving break-to-plating
* Subsystem flag tweaks
* Apparently subsystems are new'd before config is.
* Fix paper icons
* Speculative fix for insane lag
* Better machinery stat
* Make organs not use SSoverlay
* Misc bugfixes & tweaks
* Nightmode fixes
* Changelog for SMC
* Port /tg/'s improved MC crash handling
* Add some more SS Recover() procs
* supply_controller -> SScargo
* More New() -> Initialize()
* pAI and robot construction overlays
* Fix cargo unit tests
* Merge the DMM Suite's atom/New() into atoms_init
* Lighting pre-baking
* Lighting initialization logging
* Fix some bad SS init orders
* Fix SSlighting logging; rename Processes to MC
* Speculative fix for insane GC lag
* Prebaked openturf/icon_smooth & fix lighting prebake
* SS init status; SSatoms LATEQDEL
* Fix bug with MC init stat panel
* Fix parallax
* Misc
* Ignore SS_NO_DISPLAY during init
* apparently this is important
* REEEEEE
* Image GC fixes; broadcaster radio-new sanity
* RCON Cleanup
* Move pAI recruiter into subsystem
* Move global solars list into sun subsystem
* Make chickens not use a global
* Demote SSdisposals to SS_BACKGROUND; garbage-debug cleanup
* Speed up space init a little
* Fix bad timer flags on floor drying
* Subsystem panic-restart verb for mins
* Explosion speedup
* Minor subsystem & MC logging tweaks
* SSopenturf improvements
* Make pipenet actually initialize (whoops)
* Minor tweaks
* Implement lighting rounding
* comments are hard okay
* Minor lattice tweaks
* Fix some timer issues & better closet init
* Timer sanity
* Request console tweaks + Storage init sanity
* Minor SSmachiner RCON improvements
* Further reduce world-start timer count
* Standardize subsystem logging
* Garbage hard delete profiling from /tg/
* Timer hang detection & recovery
* Log machines that sleep in process() and fuck up SSmachinery
* Fix an issue with external airlocks sleeping in process()
* Failsafe logging
* Minor tweaks
* Revert "Request console tweaks + Storage init sanity"
This reverts commit 98d3579e35.
* Re-implement RC changes
* Fix SQL FT saving
* Fix SSmachinery sleep in disposals
* Minor SS tweaks
* Paper fixes
* Blood drying fixes
* Merge gameticker and SSticker
* Minor global list init cleanup
* Lagcheck biogenerator & bags
* Tweak SScargo init order; RIG Initialize()
* Caching tweaks
* Remove rogue comma
* Initialize fixes
* Lighting destroy cleanup
* Fix emagging airlocks
* Initial SSicon implementation
* Tweaks & Fixes
* Fire + Air alarm queued icon updates
* Overlays + Queued icon cleanup
* Runtime & background fixes
* Kill some meaningless set statements
* Kill some image qdels
* Bump up lighting rounding val
* Fix adv. scanner destroy runtimes
* Remove unneeded icon update limiting
* Move icon smoothing into helpers
* Show a warning if DM 510 compiles without memory leak hack enabled
* Re-organize subsystems & MC defines a little
* Airlock SFX
* Log of Changes
* Make SSicon_update disable itself when not doing anything
* Fix respawn verb runtime when used early in server-init
* Add more information to MC's stat_entry()
* Replace direct refernces to gcDestroyed with QDEL* macros
* plant_controller -> SSplants
* More plant tweaks
* Add more humor to changelog
* Move parallax globals into SSparallax
* Lighting responsiveness tweaks
* Fix parallax init order & better MC init panel stat
* Make mobs GC
* More overlays + Remove intercom spawn()
* SSfast_process; make pinpointers not use spawn-recursion to process
Also made the SM Cascade beach process with SSprocessing instead of a spawn loop.
* Update changelog
* Mob GC tweaks
* Del() cleanup
* Fix insomniac ZAS connection edges
* Minor pAI cleanup
* Convert more things to SSoverlay; fix duplicated overlay in field gens
* SM Bluespace turf tweaks
* Update SSgarbage debug globals list
* Human-type qdel tweaks
* Subsystem suspension; stat_entry improvements
* SQL Statistics cleanup
* Fix runtimes with ambrosia
* More disable() -> suspend(); fix nightmode again
* Human qdel fix; minor tweaks
* Update turbolift to work with StonedMC
* Make lifts use timers instead of a subsystem
* Make SSassets start earlier
* Convert the radio controller into a subsystem
* Fix some missing CHECK_TICKs in asteroid generation
* MC stat tweaks; make shouldnt_see a typecache
* Kill some redundant debug-controller entries
* radio_controller -> SSradio
* Better SSgarbage hard-del logging from /tg/ upstream
* Logging tweaks + GELF
* Misc client caching improvements
* Slime SSoverlay
* Oven icon fixes
* Implant fixes
- Death implants will no longer spam Common on death of user.
- Death implants should handle a deleted user better.
* Holder tweaks + Welding tool Initialize()
* Fix some bad subsystem logging
* Fix suit cooling units spawning without cells
* Starlight tweaks
* Gibber infinite gib fix
* More SSoverlay stuff
* Make crates use CUT_OVERLAY_IN
* Make SSarrivals suspend instead of disable
* Make openturf use split/phased tick checks
* Speculative fix for unwet timer runtimes
* Blood overlay tweaks/fixes
* Update crusher to play nice with SMC + SSoverlay
* Openturf improvements and fixes
* Minor turbolift tweaks
* Lighting performance improvements + ChangeTurf tweaks
* this is probably important
* Fix wall weld noises on changeturf
* More ChangeTurf tweaks
* Explosion tweaks
* Pre-game lobby tweaks
* Openturf tweaks
* Prevent admins from starting the game before init finishes
* Fix Travis
* Kill an unused var
* Fix ChangeTurf runtimes on openturfs
* Fixes
* Browser datum fixes, asset caching
* Update changelog
* Changelog
* Lobby tweaks
* Ticker tweaks; kill ticker var
* Further lobby tweaks
* Cascade tweaks
* air_master -> SSair
* Reduce overhead from radio autosay
* alarm_manager -> SSalarm
* bomb_processor -> SSexplosives
* corp_regs -> SSlaw
* ZAS overlay fixes
* Small wall icon optimization
* Fix effects master
* Assembly tweaks
* Megavend fixes
* Shuttle fixes
* Camera alert performance improvements
* Fix some world.log spam from lighting overlays
* Fix some Initialize() procs
* Openspace responsiveness tweaks
* Make HE pipes animate through openturfs
* Kill a spawn
667 lines
19 KiB
Plaintext
667 lines
19 KiB
Plaintext
/*
|
|
* 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
|
|
|
|
//Returns list element or null. Should prevent "index out of bounds" error.
|
|
proc/listgetindex(var/list/list,index)
|
|
if(istype(list) && list.len)
|
|
if(isnum(index))
|
|
if(InRange(index,1,list.len))
|
|
return list[index]
|
|
else if(index in list)
|
|
return list[index]
|
|
return
|
|
|
|
proc/islist(list/list)
|
|
if(istype(list))
|
|
return 1
|
|
return 0
|
|
|
|
//Return either pick(list) or null if list is not of type /list or is empty
|
|
proc/safepick(list/list)
|
|
if(!islist(list) || !list.len)
|
|
return
|
|
return pick(list)
|
|
|
|
//Checks if the list is empty
|
|
proc/isemptylist(list/list)
|
|
if(!list.len)
|
|
return 1
|
|
return 0
|
|
|
|
//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/is_type_in_oview(var/type, var/dist = 0, var/center = src)
|
|
if (!ispath(type))
|
|
CRASH("Not a valid type in 'is_type_in_oview()'")
|
|
if (!isnum(dist))
|
|
CRASH("Not a valid dist in 'is_type_in_oview()'")
|
|
if (!isloc(center))
|
|
CRASH("Not a valid center in 'is_type_in_oview()'")
|
|
var/list/atoms = oview(dist, center)
|
|
for (var/A in atoms)
|
|
if (istype(A, type))
|
|
return 1
|
|
return 0
|
|
|
|
/proc/is_type_in_view(var/type, var/dist = 0, var/center = src)
|
|
if (!ispath(type))
|
|
CRASH("Not a valid type in 'is_type_in_view()'")
|
|
if (!isnum(dist))
|
|
CRASH("Not a valid dist in 'is_type_in_view()'")
|
|
if (!isloc(center))
|
|
CRASH("Not a valid center in 'is_type_in_view()'")
|
|
var/list/atoms = view(dist, center)
|
|
for (var/A in atoms)
|
|
if (istype(A, type))
|
|
return 1
|
|
return 0
|
|
|
|
/proc/instances_of_type_in_list(var/atom/A, var/list/L)
|
|
var/instances = 0
|
|
for(var/type in L)
|
|
if(istype(A, type))
|
|
instances++
|
|
return instances
|
|
|
|
//Empties the list by .Cut(). Setting lenght = 0 has been confirmed to leak references.
|
|
proc/clearlist(var/list/L)
|
|
if(islist(L))
|
|
L.Cut()
|
|
|
|
//Removes any null entries from the list
|
|
proc/listclearnulls(list/list)
|
|
if(istype(list))
|
|
while(null in list)
|
|
list -= null
|
|
return
|
|
|
|
/*
|
|
* 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<L.len, i++)
|
|
if(L[i] == element)
|
|
return L[i+1]
|
|
return L[1]
|
|
|
|
/*
|
|
* Sorting
|
|
*/
|
|
|
|
//Reverses the order of items in the list
|
|
/proc/reverselist(list/L)
|
|
var/list/output = list()
|
|
if(L)
|
|
for(var/i = L.len; 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<L.len; i++)
|
|
L.Swap(i, rand(i,L.len))
|
|
return L
|
|
|
|
//Return a list with no duplicate entries
|
|
/proc/uniquelist(var/list/L)
|
|
. = list()
|
|
for(var/i in L)
|
|
. |= i
|
|
|
|
//Mergesort: divides up the list into halves to begin the sort
|
|
/proc/sortKey(var/list/client/L, var/order = 1)
|
|
if (!L)
|
|
return
|
|
var/list/target = L.Copy()
|
|
return sortTim(target, order ? /proc/cmp_ckey_asc : /proc/cmp_ckey_dsc, FALSE)
|
|
|
|
//Mergesort: divides up the list into halves to begin the sort
|
|
/proc/sortAtom(var/list/atom/L, var/order = 1)
|
|
if (!L)
|
|
return
|
|
var/list/target = L.Copy()
|
|
return sortTim(target, order ? /proc/cmp_name_asc : /proc/cmp_name_dsc, FALSE)
|
|
|
|
//Mergesort: Specifically for record datums in a list.
|
|
/proc/sortRecord(var/list/datum/data/record/L, var/field = "name", var/order = 1)
|
|
if (!L)
|
|
return
|
|
var/list/target = L.Copy()
|
|
var/old_cmp_field = cmp_field
|
|
cmp_field = field
|
|
sortTim(target, order ? /proc/cmp_records_asc : /proc/cmp_records_dsc, FALSE)
|
|
cmp_field = old_cmp_field
|
|
return target
|
|
|
|
//Mergesort: any value in a list
|
|
/proc/sortList(var/list/L)
|
|
if (!L)
|
|
return
|
|
var/list/target = L.Copy()
|
|
return sortTim(target, /proc/cmp_text_asc)
|
|
|
|
//Mergsorge: uses sortList() but uses the var's name specifically. This should probably be using mergeAtom() instead
|
|
/proc/sortNames(var/list/L)
|
|
if (!L)
|
|
return
|
|
var/list/target = L.Copy()
|
|
return sortTim(target, /proc/cmp_name_asc, FALSE)
|
|
|
|
// List of lists, sorts by element[key] - for things like crew monitoring computer sorting records by name.
|
|
/proc/sortByKey(var/list/L, var/key)
|
|
if(L.len < 2)
|
|
return L
|
|
var/middle = L.len / 2 + 1
|
|
return mergeKeyedLists(sortByKey(L.Copy(0, middle), key), sortByKey(L.Copy(middle), key), key)
|
|
|
|
/proc/mergeKeyedLists(var/list/L, var/list/R, var/key)
|
|
var/Li=1
|
|
var/Ri=1
|
|
var/list/result = new()
|
|
while(Li <= L.len && Ri <= R.len)
|
|
if(sorttext(L[Li][key], R[Ri][key]) < 1)
|
|
// Works around list += list2 merging lists; it's not pretty but it works
|
|
result += "temp item"
|
|
result[result.len] = R[Ri++]
|
|
else
|
|
result += "temp item"
|
|
result[result.len] = L[Li++]
|
|
|
|
if(Li <= L.len)
|
|
return (result + L.Copy(Li, 0))
|
|
return (result + R.Copy(Ri, 0))
|
|
|
|
|
|
//Mergesort: any value in a list, preserves key=value structure
|
|
/proc/sortAssoc(var/list/L)
|
|
var/list/ret = L.Copy()
|
|
sortTim(ret, /proc/cmp_text_asc, FALSE)
|
|
return ret
|
|
|
|
// Macros to test for bits in a bitfield. Note, that this is for use with indexes, not bit-masks!
|
|
#define BITTEST(bitfield,index) ((bitfield) & (1 << (index)))
|
|
#define BITSET(bitfield,index) (bitfield) |= (1 << (index))
|
|
#define BITRESET(bitfield,index) (bitfield) &= ~(1 << (index))
|
|
#define BITFLIP(bitfield,index) (bitfield) ^= (1 << (index))
|
|
|
|
//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(istype(wordlist,/list))
|
|
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
|
|
/proc/get_key_by_index(var/list/L, var/index)
|
|
var/i = 1
|
|
for(var/key in L)
|
|
if(index == i)
|
|
return key
|
|
i++
|
|
return null
|
|
|
|
// Returns the key based on the index
|
|
/proc/get_key_by_value(var/list/L, var/value)
|
|
for(var/key in L)
|
|
if(L[key] == value)
|
|
return key
|
|
|
|
/proc/count_by_type(var/list/L, type)
|
|
var/i = 0
|
|
for(var/T in L)
|
|
if(istype(T, type))
|
|
i++
|
|
return i
|
|
|
|
//Don't use this on lists larger than half a dozen or so
|
|
/proc/insertion_sort_numeric_list_ascending(var/list/L)
|
|
//world.log << "ascending len input: [L.len]"
|
|
var/list/out = list(pop(L))
|
|
for(var/entry in L)
|
|
if(isnum(entry))
|
|
var/success = 0
|
|
for(var/i=1, i<=out.len, i++)
|
|
if(entry <= out[i])
|
|
success = 1
|
|
out.Insert(i, entry)
|
|
break
|
|
if(!success)
|
|
out.Add(entry)
|
|
|
|
//world.log << " output: [out.len]"
|
|
return out
|
|
|
|
/proc/insertion_sort_numeric_list_descending(var/list/L)
|
|
//world.log << "descending len input: [L.len]"
|
|
var/list/out = insertion_sort_numeric_list_ascending(L)
|
|
//world.log << " output: [out.len]"
|
|
return reverselist(out)
|
|
|
|
/proc/dd_sortedObjectList(var/list/L, var/cache=list())
|
|
if(L.len < 2)
|
|
return L
|
|
var/middle = L.len / 2 + 1 // Copy is first,second-1
|
|
return dd_mergeObjectList(dd_sortedObjectList(L.Copy(0,middle), cache), dd_sortedObjectList(L.Copy(middle), cache), cache) //second parameter null = to end of list
|
|
|
|
/proc/dd_mergeObjectList(var/list/L, var/list/R, var/list/cache)
|
|
var/Li=1
|
|
var/Ri=1
|
|
var/list/result = new()
|
|
while(Li <= L.len && Ri <= R.len)
|
|
var/LLi = L[Li]
|
|
var/RRi = R[Ri]
|
|
var/LLiV = cache[LLi]
|
|
var/RRiV = cache[RRi]
|
|
if(!LLiV)
|
|
LLiV = LLi:dd_SortValue()
|
|
cache[LLi] = LLiV
|
|
if(!RRiV)
|
|
RRiV = RRi:dd_SortValue()
|
|
cache[RRi] = RRiV
|
|
if(LLiV < RRiV)
|
|
result += L[Li++]
|
|
else
|
|
result += R[Ri++]
|
|
|
|
if(Li <= L.len)
|
|
return (result + L.Copy(Li, 0))
|
|
return (result + R.Copy(Ri, 0))
|
|
|
|
// Insert an object into a sorted list, preserving sortedness
|
|
/proc/dd_insertObjectList(var/list/L, var/O)
|
|
var/min = 1
|
|
var/max = L.len
|
|
var/Oval = O:dd_SortValue()
|
|
|
|
while(1)
|
|
var/mid = min+round((max-min)/2)
|
|
|
|
if(mid == max)
|
|
L.Insert(mid, O)
|
|
return
|
|
|
|
var/Lmid = L[mid]
|
|
var/midval = Lmid:dd_SortValue()
|
|
if(Oval == midval)
|
|
L.Insert(mid, O)
|
|
return
|
|
else if(Oval < midval)
|
|
max = mid
|
|
else
|
|
min = mid+1
|
|
|
|
/*
|
|
proc/dd_sortedObjectList(list/incoming)
|
|
/*
|
|
Use binary search to order by dd_SortValue().
|
|
This works by going to the half-point of the list, seeing if the node in
|
|
question is higher or lower cost, then going halfway up or down the list
|
|
and checking again. This is a very fast way to sort an item into a list.
|
|
*/
|
|
var/list/sorted_list = new()
|
|
var/low_index
|
|
var/high_index
|
|
var/insert_index
|
|
var/midway_calc
|
|
var/current_index
|
|
var/current_item
|
|
var/current_item_value
|
|
var/current_sort_object_value
|
|
var/list/list_bottom
|
|
|
|
var/current_sort_object
|
|
for (current_sort_object in incoming)
|
|
low_index = 1
|
|
high_index = sorted_list.len
|
|
while (low_index <= high_index)
|
|
// Figure out the midpoint, rounding up for fractions. (BYOND rounds down, so add 1 if necessary.)
|
|
midway_calc = (low_index + high_index) / 2
|
|
current_index = round(midway_calc)
|
|
if (midway_calc > current_index)
|
|
current_index++
|
|
current_item = sorted_list[current_index]
|
|
|
|
current_item_value = current_item:dd_SortValue()
|
|
current_sort_object_value = current_sort_object:dd_SortValue()
|
|
if (current_sort_object_value < current_item_value)
|
|
high_index = current_index - 1
|
|
else if (current_sort_object_value > current_item_value)
|
|
low_index = current_index + 1
|
|
else
|
|
// current_sort_object == current_item
|
|
low_index = current_index
|
|
break
|
|
|
|
// Insert before low_index.
|
|
insert_index = low_index
|
|
|
|
// Special case adding to end of list.
|
|
if (insert_index > sorted_list.len)
|
|
sorted_list += current_sort_object
|
|
continue
|
|
|
|
// Because BYOND lists don't support insert, have to do it by:
|
|
// 1) taking out bottom of list, 2) adding item, 3) putting back bottom of list.
|
|
list_bottom = sorted_list.Copy(insert_index)
|
|
sorted_list.Cut(insert_index)
|
|
sorted_list += current_sort_object
|
|
sorted_list += list_bottom
|
|
return sorted_list
|
|
*/
|
|
|
|
proc/dd_sortedtextlist(list/incoming, case_sensitive = 0)
|
|
// Returns a new list with the text values sorted.
|
|
// Use binary search to order by sortValue.
|
|
// This works by going to the half-point of the list, seeing if the node in question is higher or lower cost,
|
|
// then going halfway up or down the list and checking again.
|
|
// This is a very fast way to sort an item into a list.
|
|
var/list/sorted_text = new()
|
|
var/low_index
|
|
var/high_index
|
|
var/insert_index
|
|
var/midway_calc
|
|
var/current_index
|
|
var/current_item
|
|
var/list/list_bottom
|
|
var/sort_result
|
|
|
|
var/current_sort_text
|
|
for (current_sort_text in incoming)
|
|
low_index = 1
|
|
high_index = sorted_text.len
|
|
while (low_index <= high_index)
|
|
// Figure out the midpoint, rounding up for fractions. (BYOND rounds down, so add 1 if necessary.)
|
|
midway_calc = (low_index + high_index) / 2
|
|
current_index = round(midway_calc)
|
|
if (midway_calc > current_index)
|
|
current_index++
|
|
current_item = sorted_text[current_index]
|
|
|
|
if (case_sensitive)
|
|
sort_result = sorttextEx(current_sort_text, current_item)
|
|
else
|
|
sort_result = sorttext(current_sort_text, current_item)
|
|
|
|
switch(sort_result)
|
|
if (1)
|
|
high_index = current_index - 1 // current_sort_text < current_item
|
|
if (-1)
|
|
low_index = current_index + 1 // current_sort_text > current_item
|
|
if (0)
|
|
low_index = current_index // current_sort_text == current_item
|
|
break
|
|
|
|
// Insert before low_index.
|
|
insert_index = low_index
|
|
|
|
// Special case adding to end of list.
|
|
if (insert_index > sorted_text.len)
|
|
sorted_text += current_sort_text
|
|
continue
|
|
|
|
// Because BYOND lists don't support insert, have to do it by:
|
|
// 1) taking out bottom of list, 2) adding item, 3) putting back bottom of list.
|
|
list_bottom = sorted_text.Copy(insert_index)
|
|
sorted_text.Cut(insert_index)
|
|
sorted_text += current_sort_text
|
|
sorted_text += list_bottom
|
|
return sorted_text
|
|
|
|
|
|
proc/dd_sortedTextList(list/incoming)
|
|
var/case_sensitive = 1
|
|
return dd_sortedtextlist(incoming, case_sensitive)
|
|
|
|
|
|
/datum/proc/dd_SortValue()
|
|
return "[src]"
|
|
|
|
/obj/machinery/dd_SortValue()
|
|
return "[sanitize_old(name)]"
|
|
|
|
/obj/machinery/camera/dd_SortValue()
|
|
return "[c_tag]"
|
|
|
|
/datum/alarm/dd_SortValue()
|
|
return "[sanitize_old(last_name)]"
|
|
|
|
/proc/subtypesof(prototype)
|
|
return (typesof(prototype) - prototype)
|
|
|
|
//creates every subtype of prototype (excluding prototype) and adds it to list L.
|
|
//if no list/L is provided, one is created.
|
|
/proc/init_subtypes(prototype, list/L)
|
|
if(!istype(L)) L = list()
|
|
for(var/path in subtypesof(prototype))
|
|
L += new path()
|
|
return L
|
|
|
|
//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
|
|
|
|
//Checks for specific types in specifically structured (Assoc "type" = TRUE) lists ('typecaches')
|
|
/proc/is_type_in_typecache(atom/A, list/L)
|
|
if(!L || !L.len || !A)
|
|
|
|
return 0
|
|
return L[A.type]
|
|
|
|
#define listequal(A, B) (A.len == B.len && !length(A^B))
|
|
|
|
/proc/Sum(var/list/input)
|
|
var/total = 0
|
|
for (var/i=1,i<=input.len,i++)
|
|
total += input[i]
|
|
|
|
return total
|
|
|
|
|
|
//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)
|
|
|
|
//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
|
|
|
|
#define LAZYINITLIST(L) if (!L) L = list()
|
|
#define UNSETEMPTY(L) if (L && !L.len) L = null
|