-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathzas.dm
577 lines (459 loc) · 15.1 KB
/
zas.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
Overview:
The air controller does everything. There are tons of procs in here.
Class Vars:
zones - All zones currently holding one or more turfs.
edges - All processing edges.
tiles_to_update - Tiles scheduled to update next tick.
zones_to_update - Zones which have had their air changed and need air archival.
active_hotspots - All processing fire objects.
active_zones - The number of zones which were archived last tick. Used in debug verbs.
next_id - The next UID to be applied to a zone. Mostly useful for debugging purposes as zones do not need UIDs to function.
Class Procs:
mark_for_update(turf/T)
Adds the turf to the update list. When updated, update_air_properties() will be called.
When stuff changes that might affect airflow, call this. It's basically the only thing you need.
add_zone(zone/Z) and remove_zone(zone/Z)
Adds zones to the zones list. Does not mark them for update.
air_blocked(turf/A, turf/B)
Returns a bitflag consisting of:
AIR_BLOCKED - The connection between turfs is physically blocked. No air can pass.
ZONE_BLOCKED - There is a door between the turfs, so zones cannot cross. Air may or may not be permeable.
has_valid_zone(turf/T)
Checks the presence and validity of T's zone.
May be called on unsimulated turfs, returning 0.
merge(zone/A, zone/B)
Called when zones have a direct connection and equivalent pressure and temperature.
Merges the zones to create a single zone.
connect(turf/simulated/A, turf/B)
Called by turf/update_air_properties(). The first argument must be simulated.
Creates a connection between A and B.
mark_zone_update(zone/Z)
Adds zone to the update list. Unlike mark_for_update(), this one is called automatically whenever
air is returned from a simulated turf.
get_edge(zone/A, zone/B)
get_edge(zone/A, turf/B)
Gets a valid connection_edge between A and B, creating a new one if necessary.
has_same_air(turf/A, turf/B)
Used to determine if an unsimulated edge represents a specific turf.
Simulated edges use connection_edge/contains_zone() for the same purpose.
Returns 1 if A has identical gases and temperature to B.
remove_edge(connection_edge/edge)
Called when an edge is erased. Removes it from processing.
*/
GLOBAL_REAL(zas_settings, /datum/zas_controller) = new
SUBSYSTEM_DEF(zas)
name = "Air Core"
priority = FIRE_PRIORITY_AIR
init_order = INIT_ORDER_AIR
flags = SS_KEEP_TIMING
runlevels = RUNLEVEL_GAME | RUNLEVEL_POSTGAME
wait = 2 SECONDS
var/cached_cost = 0
var/cost_tiles = 0
var/cost_deferred_tiles = 0
var/cost_check_edges = 0
var/cost_edges = 0
var/cost_fires = 0
var/cost_hotspots = 0
var/cost_zones = 0
var/cost_exposure = 0
//The variable setting controller
var/datum/zas_controller/settings
//A reference to the global var
var/datum/xgm_gas_data/gas_data
///A global cache of unsimulated gas mixture singletons, associative by type.
var/list/unsimulated_gas_cache = list()
//Geometry lists
var/list/zones = list()
var/list/edges = list()
//Geometry updates lists
var/list/tiles_to_update = list()
var/list/zones_to_update = list()
var/list/active_hotspots = list()
var/list/active_edges = list()
var/list/zones_with_sensitive_contents = list()
var/tmp/list/deferred = list()
var/tmp/list/processing_edges
var/tmp/list/processing_fires
var/tmp/list/processing_hotspots
var/tmp/list/processing_zones
var/tmp/list/processing_exposure
#ifdef ZASDBG
/// Profile data for zone.tick(), in milliseconds
var/list/zonetime = list()
#endif
#ifdef PROFILE_ZAS_CANPASS
var/list/canpass_step_usage = list()
var/list/canpass_time_spent = list()
var/list/canpass_time_average = list()
#endif
var/active_zones = 0
var/next_id = 1
///The last process, as a string, before the previous run ended.
var/last_process
///Stops processing while all ZAS-controlled airs and fires are nulled and the subsystem is reinitialized.
/datum/controller/subsystem/zas/proc/Reboot()
// Stop processing while we rebuild.
can_fire = FALSE
times_fired = 0 // This is done to prevent the geometry bug explained in connect()
next_id = 0 //Reset atmos zone count.
// Make sure we don't rebuild mid-tick.
if (state != SS_IDLE)
to_chat(world, span_boldannounce("ZAS Rebuild initiated. Waiting for current air tick to complete before continuing."))
UNTIL(state == SS_IDLE)
zas_settings = new //Reset the global zas settings
while (zones.len)
var/zone/zone = zones[zones.len]
zones.len--
zone.invalidate()
edges.Cut()
tiles_to_update.Cut()
zones_to_update.Cut()
active_hotspots.Cut()
active_edges.Cut()
// Re-run setup without air settling.
Initialize(REALTIMEOFDAY, simulate = FALSE)
// Update next_fire so the MC doesn't try to make up for missed ticks.
next_fire = world.time + wait
can_fire = TRUE
/datum/controller/subsystem/zas/stat_entry(msg)
if(!can_fire)
msg += "REBOOTING..."
else
msg += "TtU: [length(tiles_to_update)] "
msg += "ZtU: [length(zones_to_update)] "
msg += "AH: [length(active_hotspots)] "
msg += "AE: [length(active_edges)]"
return ..()
/datum/controller/subsystem/zas/Initialize(timeofday, simulate = TRUE)
var/starttime = REALTIMEOFDAY
settings = zas_settings
gas_data = xgm_gas_data
to_chat(world, span_debug("ZAS: Processing Geometry..."))
var/simulated_turf_count = 0
for(var/turf/S as turf in world)
if(!S.simulated)
continue
simulated_turf_count++
S.update_air_properties()
CHECK_TICK
to_chat(world, span_debug("ZAS:\n - Total Simulated Turfs: [simulated_turf_count]\n - Total Zones: [zones.len]\n - Total Edges: [edges.len]\n - Total Active Edges: [active_edges.len ? "<span class='danger'>[active_edges.len]</span>" : "None"]\n - Total Unsimulated Turfs: [world.maxx*world.maxy*world.maxz - simulated_turf_count]"))
to_chat(world, span_debug("ZAS: Geometry processing completed in [(REALTIMEOFDAY - starttime)/10] seconds!"))
if (simulate)
to_chat(world, span_debug("ZAS: Firing once..."))
starttime = REALTIMEOFDAY
fire(FALSE, TRUE)
to_chat(world, span_debug("ZAS: Air settling completed in [(REALTIMEOFDAY - starttime)/10] seconds!"))
..(timeofday)
/datum/controller/subsystem/zas/fire(resumed = FALSE, no_mc_tick)
var/timer = TICK_USAGE_REAL
if (!resumed)
processing_edges = active_edges.Copy()
processing_hotspots = active_hotspots.Copy()
processing_exposure = zones_with_sensitive_contents.Copy()
var/list/curr_tiles = tiles_to_update
var/list/curr_defer = deferred
var/list/curr_edges = processing_edges
var/list/curr_hotspot = processing_hotspots
var/list/curr_zones = zones_to_update
var/list/curr_zones_again = zones_to_update.Copy()
var/list/curr_sensitive_zones = processing_exposure
last_process = "TILES"
/////////TILES//////////
cached_cost = 0
while (curr_tiles.len)
var/turf/T = curr_tiles[curr_tiles.len]
curr_tiles.len--
if (!T)
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
return
continue
//check if the turf is self-zone-blocked
var/c_airblock
ATMOS_CANPASS_TURF(c_airblock, T, T)
if(c_airblock & ZONE_BLOCKED)
deferred += T
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
return
continue
T.update_air_properties()
T.post_update_air_properties()
T.needs_air_update = 0
#ifdef ZASDBG
T.remove_viscontents(zasdbgovl_mark)
//updated++
#endif
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
return
cached_cost += TICK_USAGE_REAL - timer
cost_tiles = MC_AVERAGE(cost_tiles, TICK_DELTA_TO_MS(cached_cost))
//////////DEFERRED TILES//////////
last_process = "DEFERRED TILES"
timer = TICK_USAGE_REAL
cached_cost = 0
while (curr_defer.len)
var/turf/T = curr_defer[curr_defer.len]
curr_defer.len--
T.update_air_properties()
T.post_update_air_properties()
T.needs_air_update = 0
#ifdef ZASDBG
T.remove_viscontents(zasdbgovl_mark)
//updated++
#endif
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
return
cached_cost += TICK_USAGE_REAL - timer
cost_deferred_tiles = MC_AVERAGE(cost_deferred_tiles, TICK_DELTA_TO_MS(cached_cost))
//////////CHECK_EDGES/////////
last_process = "CHECK EDGES"
timer = TICK_USAGE_REAL
cached_cost = 0
while (curr_zones_again.len)
var/zone/Z = curr_zones_again[curr_zones_again.len]
curr_zones_again.len--
var/list/cache_for_speed = Z.edges
for(var/edge_source in Z.edges)
var/connection_edge/E = cache_for_speed[edge_source]
if(!E.excited)
E.recheck()
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
return
cached_cost += TICK_USAGE_REAL - timer
cost_check_edges = MC_AVERAGE(cost_check_edges, TICK_DELTA_TO_MS(cached_cost))
//////////EDGES//////////
last_process = "EDGES"
timer = TICK_USAGE_REAL
cached_cost = 0
while (curr_edges.len)
var/connection_edge/edge = curr_edges[curr_edges.len]
curr_edges.len--
if (!edge)
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
return
continue
edge.tick()
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
return
cached_cost += TICK_USAGE_REAL - timer
cost_edges = MC_AVERAGE(cost_edges, TICK_DELTA_TO_MS(cached_cost))
//////////HOTSPOTS//////////
last_process = "HOTSPOTS"
timer = TICK_USAGE_REAL
cached_cost = 0
while (curr_hotspot.len)
var/obj/effect/hotspot/F = curr_hotspot[curr_hotspot.len]
curr_hotspot.len--
F.process()
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
return
cached_cost += TICK_USAGE_REAL - timer
cost_hotspots = MC_AVERAGE(cost_hotspots, TICK_DELTA_TO_MS(cached_cost))
//////////ZONES//////////
last_process = "ZONES"
timer = TICK_USAGE_REAL
cached_cost = 0
while (curr_zones.len)
var/zone/Z = curr_zones[curr_zones.len]
curr_zones.len--
Z.tick()
Z.needs_update = FALSE
if (no_mc_tick)
CHECK_TICK
else if (MC_TICK_CHECK)
return
cached_cost += TICK_USAGE_REAL - timer
cost_zones = MC_AVERAGE(cost_zones, TICK_DELTA_TO_MS(cached_cost))
if(no_mc_tick) //Initialization doesn't need to process exposure, waste of time
return
/////////ATMOS EXPOSE//////
last_process = "ATMOS EXPOSE"
timer = TICK_USAGE_REAL
cached_cost = 0
while(curr_sensitive_zones.len)
var/zone/Z = curr_sensitive_zones[curr_sensitive_zones.len]
curr_sensitive_zones.len--
for(var/atom/sensitive as area|turf|obj|mob in Z.atmos_sensitive_contents)
sensitive.atmos_expose(Z.air, Z.air.temperature)
if(MC_TICK_CHECK)
return
cached_cost += TICK_USAGE_REAL - timer
cost_exposure = MC_AVERAGE(cost_exposure, TICK_DELTA_TO_MS(cached_cost))
///Adds a zone to the subsystem, gives it's identifer, and marks it for update.
/datum/controller/subsystem/zas/proc/add_zone(zone/z)
zones += z
z.name = "Zone [next_id++]"
mark_zone_update(z)
///Removes a zone from the subsystem.
/datum/controller/subsystem/zas/proc/remove_zone(zone/z)
zones -= z
zones_to_update -= z
zones_with_sensitive_contents -= z
///Checks to see if air can flow between A and B.
/datum/controller/subsystem/zas/proc/air_blocked(turf/A, turf/B)
#ifdef ZASDBG
ASSERT(isturf(A))
ASSERT(isturf(B))
#endif
var/ablock
ATMOS_CANPASS_TURF(ablock, A, B)
if(ablock & AIR_BLOCKED)
return AIR_BLOCKED|ZONE_BLOCKED
ATMOS_CANPASS_TURF(., B, A)
return ablock | .
///Merges two zones. Largest by turf count wins.
/datum/controller/subsystem/zas/proc/merge(zone/A, zone/B)
#ifdef ZASDBG
ASSERT(istype(A))
ASSERT(istype(B))
ASSERT(!A.invalid)
ASSERT(!B.invalid)
ASSERT(A != B)
#endif
if(A.contents.len < B.contents.len)
A.merge_into(B)
mark_zone_update(B)
else
B.merge_into(A)
mark_zone_update(A)
///Forms a /connection/ between two turfs.
/datum/controller/subsystem/zas/proc/connect(turf/A, turf/B)
#ifdef ZASDBG
ASSERT(A.simulated)
ASSERT(isturf(B))
ASSERT(A.zone)
ASSERT(!A.zone.invalid)
//ASSERT(B.zone)
ASSERT(A != B)
#endif
var/block = air_blocked(A,B)
if(block & AIR_BLOCKED)
return
var/direct = !(block & ZONE_BLOCKED)
//var/space = istype(B, /turf/open/space)
var/space = !B.simulated
if(!space)
// Ok. This is super fucking cursed, but, it has to be this way.
// Basically, it's possible that during the initial geometry build, a zone can be created in a spot
// such that it merges over zone-blocker due to the minimum size requirement being met to merge over blockers.
// To fix this, we disable that during the geometry build, which takes place during Initialize()
if((times_fired && min(length(A.zone.contents), length(B.zone.contents)) < ZONE_MIN_SIZE) || (direct && A.zone.air.compare(B.zone.air)))
merge(A.zone,B.zone)
return TRUE
else if(isnull(B.air))
/// The logic around get_edge() requires air to exist at this point, which it probably should.
B.make_air()
var/a_to_b = get_dir(A,B)
var/b_to_a = get_dir(B,A)
if(!A.connections)
A.connections = new
if(!B.connections)
B.connections = new
if(A.connections.get(a_to_b))
return
if(B.connections.get(b_to_a))
return
if(!space && (A.zone == B.zone))
return
var/connection/c = new /connection(A,B)
A.connections.place(c, a_to_b)
B.connections.place(c, b_to_a)
if(direct)
c.mark_direct()
return TRUE
///Marks a turf for update.
/datum/controller/subsystem/zas/proc/mark_for_update(turf/T)
#ifdef ZASDBG
ASSERT(isturf(T))
#endif
if(T.needs_air_update)
return
tiles_to_update += T
#ifdef ZASDBG
T.add_viscontents(zasdbgovl_mark)
#endif
T.needs_air_update = 1
///Marks a zone for update.
/datum/controller/subsystem/zas/proc/mark_zone_update(zone/Z)
#ifdef ZASDBG
ASSERT(istype(Z))
#endif
if(Z.needs_update)
return
zones_to_update += Z
Z.needs_update = 1
///Sleeps an edge, preventing it from processing.
/datum/controller/subsystem/zas/proc/sleep_edge(connection_edge/E)
#ifdef ZASDBG
ASSERT(istype(E))
#endif
if(!E.excited)
return
active_edges -= E
E.excited = FALSE
#ifdef ZASDBG
for(var/turf/T as anything in E.connecting_turfs)
T.remove_viscontents(zasdbgovl_edge)
#endif
///Wakes an edge, adding it to the active process list.
/datum/controller/subsystem/zas/proc/excite_edge(connection_edge/E)
#ifdef ZASDBG
ASSERT(istype(E))
#endif
if(E.excited)
return
active_edges += E
E.excited = TRUE
#ifdef ZASDBG
for(var/turf/T as anything in E.connecting_turfs)
T.add_viscontents(zasdbgovl_edge)
#endif
///Returns the edge between zones A and B. If one doesn't exist, it creates one. See header for more information
/datum/controller/subsystem/zas/proc/get_edge(zone/A, zone/B) //Note: B can also be a turf.
var/connection_edge/edge
if(isturf(B)) //Zone-to-turf connection.
for(var/turf/T in A.edges)
if(B.air.isEqual(T.air)) //Operator overloading :)
return A.edges[T]
else
edge = A.edges[B] //Zone-to-zone connection
edge ||= create_edge(A,B)
return edge
///Create an edge of the appropriate type between zone A and zone-or-turf B.
/datum/controller/subsystem/zas/proc/create_edge(zone/A, datum/B)
var/connection_edge/edge
if(B.type == /zone) //Zone-to-zone connection
edge = new/connection_edge/zone(A,B)
else //Zone-to-turf connection
edge = new/connection_edge/unsimulated(A,B)
return edge
///Removes an edge from the subsystem.
/datum/controller/subsystem/zas/proc/remove_edge(connection_edge/E)
edges -= E
if(E.excited)
active_edges -= E
if(processing_edges)
processing_edges -= E
/datum/controller/subsystem/zas/StartLoadingMap()
. = ..()
can_fire = FALSE
/datum/controller/subsystem/zas/StopLoadingMap()
. = ..()
can_fire = TRUE