-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy patharea_contents.dm
84 lines (75 loc) · 3.86 KB
/
area_contents.dm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#define ALLOWED_LOOSE_TURFS 100
/**
* Responsible for managing the sizes of area.contained_turfs and area.turfs_to_uncontain
* These lists do not check for duplicates, which is fine, but it also means they can balloon in size over time
* as a consequence of repeated changes in area in a space
* They additionally may not always resolve often enough to avoid memory leaks
* This is annoying, so lets keep an eye on them and cut them down to size if needed
*/
SUBSYSTEM_DEF(area_contents)
name = "Area Contents"
flags = SS_NO_INIT
runlevels = RUNLEVEL_LOBBY|RUNLEVELS_DEFAULT
var/list/currentrun
var/list/area/marked_for_clearing = list()
/datum/controller/subsystem/area_contents/stat_entry(msg)
var/total_clearing_from = 0
var/total_to_clear = 0
for(var/area/to_clear as anything in marked_for_clearing)
for (var/area_zlevel in 1 to length(to_clear.turfs_to_uncontain_by_zlevel))
if (length(to_clear.turfs_to_uncontain_by_zlevel[area_zlevel]))
total_to_clear += length(to_clear.turfs_to_uncontain_by_zlevel[area_zlevel])
if (length(to_clear.turfs_by_zlevel) >= area_zlevel) //this should always be true, but stat_entry is no place for runtimes. fire() can handle that
total_clearing_from += length(to_clear.turfs_by_zlevel[area_zlevel])
msg = "A:[length(currentrun)] MR:[length(marked_for_clearing)] TC:[total_to_clear] CF:[total_clearing_from]"
return ..()
/datum/controller/subsystem/area_contents/get_metrics()
. = ..()
var/total_clearing_from = 0
var/total_to_clear = 0
for(var/area/to_clear as anything in marked_for_clearing)
for (var/area_zlevel in 1 to length(to_clear.turfs_to_uncontain_by_zlevel))
if (length(to_clear.turfs_to_uncontain_by_zlevel[area_zlevel]))
total_to_clear += length(to_clear.turfs_to_uncontain_by_zlevel[area_zlevel])
if (length(to_clear.turfs_by_zlevel) >= area_zlevel) //this should always be true, but stat_entry is no place for runtimes. fire() can handle that
total_clearing_from += length(to_clear.turfs_by_zlevel[area_zlevel])
.["areas"] = length(currentrun)
.["marked_for_clearing"] = length(marked_for_clearing)
.["total_to_clear"] = total_to_clear
.["total_clearing_from"] = total_clearing_from
/datum/controller/subsystem/area_contents/fire(resumed)
if(!resumed)
currentrun = GLOB.areas.Copy()
while(length(currentrun))
var/area/test = currentrun[length(currentrun)]
for (var/area_zlevel in 1 to length(test.turfs_to_uncontain_by_zlevel))
if(length(test.turfs_to_uncontain_by_zlevel[area_zlevel]) > ALLOWED_LOOSE_TURFS)
marked_for_clearing |= test
break
currentrun.len--
if(MC_TICK_CHECK)
return
// Alright, if we've done a scan on all our areas, it's time to knock the existing ones down to size
while(length(marked_for_clearing))
var/area/clear = marked_for_clearing[length(marked_for_clearing)]
for (var/area_zlevel in 1 to length(clear.turfs_to_uncontain_by_zlevel))
if (!length(clear.turfs_to_uncontain_by_zlevel[area_zlevel]))
continue
if (length(clear.turfs_by_zlevel) < area_zlevel)
stack_trace("[clear]([clear.type])'s turfs_by_zlevel is length [length(clear.turfs_by_zlevel)] but we are being asked to remove turfs from zlevel [area_zlevel] from it.")
clear.turfs_to_uncontain_by_zlevel[area_zlevel] = list()
continue
// The operation of cutting large lists can be expensive
// It scales almost directly with the size of the list we're cutting with
// Because of this, we're gonna stick to cutting 1 entry at a time
// There's no reason to batch it I promise, this is faster. No overtime too
var/amount_cut = 0
var/list/cut_from = clear.turfs_to_uncontain_by_zlevel[area_zlevel]
for(amount_cut in 1 to length(cut_from))
clear.turfs_by_zlevel[area_zlevel] -= cut_from[amount_cut]
if(MC_TICK_CHECK)
cut_from.Cut(1, amount_cut + 1)
return
clear.turfs_to_uncontain_by_zlevel = list()
marked_for_clearing.len--
#undef ALLOWED_LOOSE_TURFS