-
-
Notifications
You must be signed in to change notification settings - Fork 444
/
Copy pathair.dm
488 lines (418 loc) · 15.2 KB
/
air.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
SUBSYSTEM_DEF(air)
name = "Atmospherics"
init_order = INIT_ORDER_AIR
priority = FIRE_PRIORITY_AIR
wait = 0.5 SECONDS
flags = SS_BACKGROUND
runlevels = RUNLEVEL_GAME | RUNLEVEL_POSTGAME
loading_points = 10 SECONDS // Yogs -- loading times
var/cached_cost = 0
var/cost_turfs = 0
var/cost_adjacent = 0
var/cost_groups = 0
var/cost_highpressure = 0
var/cost_hotspots = 0
var/cost_post_process = 0
var/cost_superconductivity = 0
var/cost_pipenets = 0
var/cost_machinery = 0
var/cost_rebuilds = 0
var/cost_equalize = 0
var/thread_wait_ticks = 0
var/cur_thread_wait_ticks = 0
var/low_pressure_turfs = 0
var/high_pressure_turfs = 0
var/num_group_turfs_processed = 0
var/num_equalize_processed = 0
var/gas_mixes_count = 0
var/gas_mixes_allocated = 0
var/list/excited_groups = list()
var/list/active_turfs = list()
var/list/hotspots = list()
var/list/networks = list()
var/list/rebuild_queue = list()
var/list/expansion_queue = list()
/// List of turfs to recalculate adjacent turfs on before processing
var/list/adjacent_rebuild = list()
var/list/pipe_init_dirs_cache = list()
var/list/obj/machinery/atmos_machinery = list()
//atmos singletons
var/list/gas_reactions = list()
//Special functions lists
var/list/turf/open/high_pressure_delta = list()
/// A cache of objects that perisists between processing runs when resumed == TRUE. Dangerous, qdel'd objects not cleared from this may cause runtimes on processing.
var/list/currentrun = list()
var/currentpart = SSAIR_PIPENETS
var/map_loading = TRUE
var/list/queued_for_activation
var/lasttick = 0
var/log_explosive_decompression = TRUE // If things get spammy, admemes can turn this off.
/// Max number of turfs equalization will grab.
var/equalize_turf_limit = 10
/// Max number of turfs to look for a space turf, and max number of turfs that will be decompressed.
var/equalize_hard_turf_limit = 2000
/// Whether equalization should be enabled at all.
var/equalize_enabled = TRUE
/// Whether turf-to-turf heat exchanging should be enabled.
var/heat_enabled = FALSE
/// Max number of times process_turfs will share in a tick.
var/share_max_steps = 3
/// Excited group processing will try to equalize groups with total pressure difference less than this amount.
var/excited_group_pressure_goal = 1
/// Planet airs will share this ratio with the planet turfs per tick
var/planet_share_ratio = 0.25
var/list/paused_z_levels //Paused z-levels will not add turfs to active
/datum/controller/subsystem/air/stat_entry(msg)
msg += "C:{"
msg += "HP:[round(cost_highpressure,1)]|"
msg += "HS:[round(cost_hotspots,1)]|"
msg += "SC:[round(cost_superconductivity,1)]|"
msg += "PN:[round(cost_pipenets,1)]|"
msg += "MC:[round(cost_machinery,1)]|"
msg += "RB:[round(cost_rebuilds,1)]|"
msg += "} "
msg += "TC:{"
msg += "AT:[round(cost_turfs,1)]|"
msg += "EG:[round(cost_groups,1)]|"
msg += "EQ:[round(cost_equalize,1)]|"
msg += "PO:[round(cost_post_process,1)]"
msg += "}"
msg += "TH:[round(thread_wait_ticks,1)]|"
msg += "HS:[hotspots.len]|"
msg += "PN:[networks.len]|"
msg += "HP:[high_pressure_delta.len]|"
msg += "HT:[high_pressure_turfs]|"
msg += "LT:[low_pressure_turfs]|"
msg += "ET:[num_equalize_processed]|"
msg += "GT:[num_group_turfs_processed]|"
msg += "GA:[gas_mixes_count]|"
msg += "MG:[gas_mixes_allocated]"
return ..()
/datum/controller/subsystem/air/get_metrics()
. = ..()
.["cost_turfs"] = cost_turfs
.["cost_groups"] = cost_groups
.["cost_highpressure"] = cost_highpressure
.["cost_hotspots"] = cost_hotspots
.["cost_post_process"] = cost_post_process
.["cost_superconductivity"] = cost_superconductivity
.["cost_pipenets"] = cost_pipenets
.["cost_machinery"] = cost_machinery
.["cost_rebuilds"] = cost_rebuilds
.["cost_equalize"] = cost_equalize
.["hotspts"] = hotspots.len
.["networks"] = networks.len
.["gas_mixes_count"] = gas_mixes_count
.["gas_mixes_allocated"] = gas_mixes_allocated
.["high_pressure_turfs"] = high_pressure_turfs
.["low_pressure_turfs"] = low_pressure_turfs
.["num_equalize_processed"] = num_equalize_processed
.["num_group_turfs_processed"] = num_group_turfs_processed
.["high_pressure_delta"] = high_pressure_delta.len
/datum/controller/subsystem/air/Initialize(timeofday)
map_loading = FALSE
setup_allturfs()
setup_atmos_machinery()
setup_pipenets()
gas_reactions = init_gas_reactions()
auxtools_update_reactions()
return SS_INIT_SUCCESS
/datum/controller/subsystem/air/proc/extools_update_ssair()
/proc/reset_all_air()
SSair.can_fire = FALSE
message_admins("Air reset begun.")
for(var/turf/open/T in world)
T.Initalize_Atmos(0)
CHECK_TICK
message_admins("Air reset done.")
SSair.can_fire = TRUE
/datum/controller/subsystem/air/proc/check_threads()
if(thread_running())
cur_thread_wait_ticks++
pause()
return FALSE
return TRUE
/datum/controller/subsystem/air/fire(resumed = 0)
var/timer = TICK_USAGE_REAL
thread_wait_ticks = MC_AVERAGE(thread_wait_ticks, cur_thread_wait_ticks)
cur_thread_wait_ticks = 0
gas_mixes_count = get_amt_gas_mixes()
gas_mixes_allocated = get_max_gas_mixes()
if(length(rebuild_queue) || length(expansion_queue))
timer = TICK_USAGE_REAL
process_rebuilds()
//This does mean that the apperent rebuild costs fluctuate very quickly, this is just the cost of having them always process, no matter what
cost_rebuilds = TICK_USAGE_REAL - timer
if(state != SS_RUNNING)
return
if(currentpart == SSAIR_PIPENETS || !resumed)
timer = TICK_USAGE_REAL
if(!resumed)
cached_cost = 0
process_pipenets(resumed)
cached_cost += TICK_USAGE_REAL - timer
if(state != SS_RUNNING)
return
cost_pipenets = MC_AVERAGE(cost_pipenets, TICK_DELTA_TO_MS(cached_cost))
resumed = 0
currentpart = SSAIR_ATMOSMACHINERY
if(currentpart == SSAIR_ATMOSMACHINERY)
timer = TICK_USAGE_REAL
if(!resumed)
cached_cost = 0
process_atmos_machinery(resumed)
cached_cost += TICK_USAGE_REAL - timer
if(state != SS_RUNNING)
return
resumed = 0
cost_machinery = MC_AVERAGE(cost_machinery, TICK_DELTA_TO_MS(cached_cost))
currentpart = SSAIR_ACTIVETURFS
if(currentpart == SSAIR_ACTIVETURFS)
timer = TICK_USAGE_REAL
process_turfs(resumed)
if(state != SS_RUNNING)
return
resumed = 0
currentpart = SSAIR_EQUALIZE
if(currentpart == SSAIR_EQUALIZE)
process_turf_equalize(resumed)
if(state != SS_RUNNING)
return
resumed = 0
currentpart = SSAIR_EXCITEDGROUPS
if(currentpart == SSAIR_EXCITEDGROUPS)
process_excited_groups(resumed)
if(state != SS_RUNNING)
return
resumed = 0
currentpart = SSAIR_FINALIZE_TURFS
if(currentpart == SSAIR_FINALIZE_TURFS)
finish_turf_processing(resumed)
if(state != SS_RUNNING)
return
resumed = 0
currentpart = SSAIR_HIGHPRESSURE
if(currentpart == SSAIR_HIGHPRESSURE)
timer = TICK_USAGE_REAL
if(!resumed)
cached_cost = 0
process_high_pressure_delta(resumed)
cached_cost += TICK_USAGE_REAL - timer
if(state != SS_RUNNING)
return
cost_highpressure = MC_AVERAGE(cost_highpressure, TICK_DELTA_TO_MS(cached_cost))
resumed = 0
currentpart = SSAIR_HOTSPOTS
if(currentpart == SSAIR_HOTSPOTS)
timer = TICK_USAGE_REAL
if(!resumed)
cached_cost = 0
process_hotspots(resumed)
cached_cost += TICK_USAGE_REAL - timer
if(state != SS_RUNNING)
return
cost_hotspots = MC_AVERAGE(cost_hotspots, TICK_DELTA_TO_MS(cached_cost))
resumed = 0
currentpart = heat_enabled ? SSAIR_TURF_CONDUCTION : SSAIR_PIPENETS
// Heat -- slow and of questionable usefulness. Off by default for this reason. Pretty cool, though.
if(currentpart == SSAIR_TURF_CONDUCTION)
timer = TICK_USAGE_REAL
if(process_turf_heat(MC_TICK_REMAINING_MS))
pause()
cost_superconductivity = MC_AVERAGE(cost_superconductivity, TICK_DELTA_TO_MS(TICK_USAGE_REAL - timer))
if(state != SS_RUNNING)
return
resumed = 0
currentpart = SSAIR_PIPENETS
/datum/controller/subsystem/air/proc/process_pipenets(resumed = FALSE)
if (!resumed)
src.currentrun = networks.Copy()
//cache for sanic speed (lists are references anyways)
var/list/currentrun = src.currentrun
while(currentrun.len)
var/datum/thing = currentrun[currentrun.len]
currentrun.len--
if(thing)
thing.process()
else
networks.Remove(thing)
if(MC_TICK_CHECK)
return
/datum/controller/subsystem/air/proc/add_to_rebuild_queue(obj/machinery/atmospherics/atmos_machine)
if(istype(atmos_machine, /obj/machinery/atmospherics) && !atmos_machine.rebuilding)
rebuild_queue += atmos_machine
atmos_machine.rebuilding = TRUE
/datum/controller/subsystem/air/proc/add_to_expansion(datum/pipeline/line, starting_point)
var/list/new_packet = new(SSAIR_REBUILD_QUEUE)
new_packet[SSAIR_REBUILD_PIPELINE] = line
new_packet[SSAIR_REBUILD_QUEUE] = list(starting_point)
expansion_queue += list(new_packet)
/datum/controller/subsystem/air/proc/remove_from_expansion(datum/pipeline/line)
for(var/list/packet in expansion_queue)
if(packet[SSAIR_REBUILD_PIPELINE] == line)
expansion_queue -= packet
return
/datum/controller/subsystem/air/proc/process_rebuilds()
//Yes this does mean rebuilding pipenets can freeze up the subsystem forever, but if we're in that situation something else is very wrong
var/list/currentrun = rebuild_queue
while(currentrun.len || length(expansion_queue))
while(currentrun.len && !length(expansion_queue)) //If we found anything, process that first
var/obj/machinery/atmospherics/remake = currentrun[currentrun.len]
currentrun.len--
if (!remake)
continue
remake.rebuild_pipes()
if (MC_TICK_CHECK)
return
var/list/queue = expansion_queue
while(queue.len)
var/list/pack = queue[queue.len]
//We operate directly with the pipeline like this because we can trust any rebuilds to remake it properly
var/datum/pipeline/linepipe = pack[SSAIR_REBUILD_PIPELINE]
var/list/border = pack[SSAIR_REBUILD_QUEUE]
expand_pipeline(linepipe, border)
if(state != SS_RUNNING) //expand_pipeline can fail a tick check, we shouldn't let things get too fucky here
return
linepipe.building = FALSE
queue.len--
if (MC_TICK_CHECK)
return
///Rebuilds a pipeline by expanding outwards, while yielding when sane
/datum/controller/subsystem/air/proc/expand_pipeline(datum/pipeline/net, list/border)
while(border.len)
var/obj/machinery/atmospherics/borderline = border[border.len]
border.len--
var/list/result = borderline.pipeline_expansion(net)
if(!length(result))
continue
for(var/obj/machinery/atmospherics/considered_device in result)
if(!istype(considered_device, /obj/machinery/atmospherics/pipe))
considered_device.set_pipenet(net, borderline)
net.add_machinery_member(considered_device)
continue
var/obj/machinery/atmospherics/pipe/item = considered_device
if(net.members.Find(item))
continue
if(item.parent)
var/static/pipenetwarnings = 10
if(pipenetwarnings > 0)
log_mapping("build_pipeline(): [item.type] added to a pipenet while still having one. (pipes leading to the same spot stacking in one turf) around [AREACOORD(item)].")
pipenetwarnings--
if(pipenetwarnings == 0)
log_mapping("build_pipeline(): further messages about pipenets will be suppressed")
net.members += item
border += item
net.air.set_volume(net.air.return_volume() + item.volume)
item.parent = net
if(item.air_temporary)
net.air.merge(item.air_temporary)
item.air_temporary = null
if (MC_TICK_CHECK)
return
/datum/controller/subsystem/air/proc/process_turf_heat()
/datum/controller/subsystem/air/proc/process_hotspots(resumed = FALSE)
if (!resumed)
src.currentrun = hotspots.Copy()
//cache for sanic speed (lists are references anyways)
var/list/currentrun = src.currentrun
while(currentrun.len)
var/obj/effect/hotspot/H = currentrun[currentrun.len]
currentrun.len--
if (H)
H.process()
else
hotspots -= H
if(MC_TICK_CHECK)
return
/datum/controller/subsystem/air/proc/process_high_pressure_delta(resumed = 0)
while (high_pressure_delta.len)
var/turf/open/T = high_pressure_delta[high_pressure_delta.len]
high_pressure_delta.len--
T.high_pressure_movements()
T.pressure_difference = 0
T.pressure_specific_target = null
if(MC_TICK_CHECK)
return
/datum/controller/subsystem/air/proc/process_atmos_machinery(resumed = 0)
if (!resumed)
src.currentrun = atmos_machinery.Copy()
//cache for sanic speed (lists are references anyways)
var/list/currentrun = src.currentrun
while(currentrun.len)
var/obj/machinery/M = currentrun[currentrun.len]
currentrun.len--
if(M == null)
atmos_machinery.Remove(M)
if(!M || (M.process_atmos(wait / (1 SECONDS)) == PROCESS_KILL))
stop_processing_machine(M)
if(MC_TICK_CHECK)
return
/datum/controller/subsystem/air/proc/process_turf_equalize(resumed = 0)
if(process_turf_equalize_auxtools(MC_TICK_REMAINING_MS))
pause()
/datum/controller/subsystem/air/proc/process_turfs(resumed = 0)
if(process_turfs_auxtools(MC_TICK_REMAINING_MS))
pause()
/datum/controller/subsystem/air/proc/process_excited_groups(resumed = 0)
if(process_excited_groups_auxtools(MC_TICK_REMAINING_MS))
pause()
/datum/controller/subsystem/air/proc/finish_turf_processing(resumed = 0)
if(finish_turf_processing_auxtools(MC_TICK_REMAINING_MS))
pause()
/datum/controller/subsystem/air/StartLoadingMap()
map_loading = TRUE
/datum/controller/subsystem/air/StopLoadingMap()
map_loading = FALSE
/datum/controller/subsystem/air/proc/setup_allturfs()
var/times_fired = ++src.times_fired
// Clear active turfs - faster than removing every single turf in the world
// one-by-one, and Initalize_Atmos only ever adds `src` back in.
for(var/turf/setup in ALL_TURFS())
if (setup.blocks_air)
continue
setup.Initalize_Atmos(times_fired)
CHECK_TICK
/datum/controller/subsystem/air/proc/setup_atmos_machinery()
for (var/obj/machinery/atmospherics/AM in atmos_machinery)
AM.atmos_init()
CHECK_TICK
//this can't be done with setup_atmos_machinery() because
// all atmos machinery has to initalize before the first
// pipenet can be built.
/datum/controller/subsystem/air/proc/setup_pipenets()
for (var/obj/machinery/atmospherics/AM in atmos_machinery)
var/list/targets = AM.get_rebuild_targets()
for(var/datum/pipeline/build_off as anything in targets)
build_off.build_pipeline_blocking(AM)
CHECK_TICK
/datum/controller/subsystem/air/proc/setup_template_machinery(list/atmos_machines)
var/obj/machinery/atmospherics/AM
for(var/A in 1 to atmos_machines.len)
AM = atmos_machines[A]
AM.atmos_init()
CHECK_TICK
for(var/A in 1 to atmos_machines.len)
AM = atmos_machines[A]
var/list/targets = AM.get_rebuild_targets()
for(var/datum/pipeline/build_off as anything in targets)
build_off.build_pipeline_blocking(AM)
CHECK_TICK
/datum/controller/subsystem/air/proc/get_init_dirs(type, dir)
if(!pipe_init_dirs_cache[type])
pipe_init_dirs_cache[type] = list()
if(!pipe_init_dirs_cache[type]["[dir]"])
var/obj/machinery/atmospherics/temp = new type(null, FALSE, dir)
pipe_init_dirs_cache[type]["[dir]"] = temp.get_init_directions()
qdel(temp)
return pipe_init_dirs_cache[type]["[dir]"]
/datum/controller/subsystem/air/proc/start_processing_machine(obj/machinery/machine)
if(machine.atmos_processing)
return
machine.atmos_processing = TRUE
atmos_machinery += machine
/datum/controller/subsystem/air/proc/stop_processing_machine(obj/machinery/machine)
if(!machine.atmos_processing)
return
machine.atmos_processing = FALSE
atmos_machinery -= machine
currentrun -= machine