-
-
Notifications
You must be signed in to change notification settings - Fork 452
/
filter.dm
275 lines (229 loc) · 9.27 KB
/
filter.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
/obj/machinery/atmospherics/components/trinary/filter
icon_state = "filter_off-0"
density = FALSE
name = "gas filter"
desc = "Very useful for filtering gasses."
can_unwrench = TRUE
var/transfer_rate = MAX_TRANSFER_RATE
///What gases are we filtering, by typepath
var/list/filter_type = list()
var/frequency = 0
var/datum/radio_frequency/radio_connection
construction_type = /obj/item/pipe/trinary/flippable
pipe_state = "filter"
quick_toggle = TRUE
/obj/machinery/atmospherics/components/trinary/filter/AltClick(mob/user)
if(can_interact(user))
transfer_rate = MAX_TRANSFER_RATE
var/msg = "was set to [transfer_rate] L/s by [key_name(usr)]"
investigate_log(msg, INVESTIGATE_ATMOS)
investigate_log(msg, INVESTIGATE_SUPERMATTER) // yogs - make supermatter invest useful
balloon_alert(user, "volume output set to [transfer_rate] L/s")
update_appearance(UPDATE_ICON)
return ..()
/obj/machinery/atmospherics/components/trinary/filter/proc/set_frequency(new_frequency)
SSradio.remove_object(src, frequency)
frequency = new_frequency
if(frequency)
radio_connection = SSradio.add_object(src, frequency, RADIO_ATMOSIA)
/obj/machinery/atmospherics/components/trinary/filter/Destroy()
SSradio.remove_object(src,frequency)
return ..()
/obj/machinery/atmospherics/components/trinary/filter/update_overlays()
. = ..()
for(var/direction in GLOB.cardinals)
if(!(direction & initialize_directions))
continue
var/obj/machinery/atmospherics/node = find_connecting(direction)
var/image/cap
if(node)
cap = get_pipe_image(icon, "cap", direction, node.pipe_color, piping_layer = piping_layer, trinary = TRUE)
else
cap = get_pipe_image(icon, "cap", direction, piping_layer = piping_layer, trinary = TRUE)
. += cap
/obj/machinery/atmospherics/components/trinary/filter/update_icon_nopipes()
var/on_state = on && nodes[1] && nodes[2] && nodes[3] && is_operational()
icon_state = "filter_[on_state ? "on" : "off"]-[set_overlay_offset(piping_layer)][flipped ? "_f" : ""]"
/obj/machinery/atmospherics/components/trinary/filter/process_atmos()
if(!on || !(nodes[1] && nodes[2] && nodes[3]) || !is_operational())
return
//Early return
var/datum/gas_mixture/air1 = airs[1]
if(!air1 || air1.return_temperature() <= 0)
return
var/datum/gas_mixture/air2 = airs[2]
var/datum/gas_mixture/air3 = airs[3]
var/transfer_ratio = transfer_rate / air1.return_volume()
if(transfer_ratio <= 0)
return
// Attempt to transfer the gas.
// If the main output is full, we try to send filtered output to the side port (air2).
// If the side output is full, we try to send the non-filtered gases to the main output port (air3).
// Any gas that can't be moved due to its destination being too full is sent back to the input (air1).
var/side_output_full = air2.return_pressure() >= MAX_OUTPUT_PRESSURE
var/main_output_full = air3.return_pressure() >= MAX_OUTPUT_PRESSURE
// If both output ports are full, there's nothing we can do. Don't bother removing anything from the input.
if (side_output_full && main_output_full)
return
var/datum/gas_mixture/removed = air1.remove_ratio(transfer_ratio)
if(!removed || !removed.total_moles())
return
var/filtering = TRUE
if(!filter_type.len)
filtering = FALSE
// Process if we have a filter set.
// If no filter is set, we just try to forward everything to air3 to avoid gas being outright lost.
if(filtering)
var/datum/gas_mixture/filtered_out = new
for(var/gas in removed.get_gases() & filter_type)
var/datum/gas_mixture/removing = removed.remove_specific_ratio(gas, 1)
if(removing)
filtered_out.merge(removing)
// Send things to the side output if we can, return them to the input if we can't.
// This means that other gases continue to flow to the main output if the side output is blocked.
if (side_output_full)
air1.merge(filtered_out)
else
air2.merge(filtered_out)
// Send things to the main output if we can, return them to the input if we can't.
// This lets filtered gases continue to flow to the side output in a manner consistent with the main output behavior.
if (main_output_full)
air1.merge(removed)
else
air3.merge(removed)
update_parents()
/obj/machinery/atmospherics/components/trinary/filter/atmos_init()
set_frequency(frequency)
return ..()
/obj/machinery/atmospherics/components/trinary/filter/ui_interact(mob/user, datum/tgui/ui)
ui = SStgui.try_update_ui(user, src, ui)
if(!ui)
ui = new(user, src, "AtmosFilter", name)
ui.open()
/obj/machinery/atmospherics/components/trinary/filter/ui_data()
var/data = list()
data["on"] = on
data["rate"] = round(transfer_rate)
data["max_rate"] = round(MAX_TRANSFER_RATE)
data["filter_types"] = list()
for(var/id in GLOB.gas_data.ids)
data["filter_types"] += list(list("name" = GLOB.gas_data.labels[id], "gas_id" = id, "enabled" = (id in filter_type)))
return data
/obj/machinery/atmospherics/components/trinary/filter/ui_act(action, params)
if(..())
return
switch(action)
if("power")
toggle_on(usr)
return TRUE
if("rate")
var/rate = params["rate"]
if(rate == "max")
rate = MAX_TRANSFER_RATE
. = TRUE
else if(rate == "input")
rate = input("New transfer rate (0-[MAX_TRANSFER_RATE] L/s):", name, transfer_rate) as num|null
if(!isnull(rate) && !..())
. = TRUE
else if(text2num(rate) != null)
rate = text2num(rate)
. = TRUE
if(.)
transfer_rate = clamp(rate, 0, MAX_TRANSFER_RATE)
var/msg = "was set to [transfer_rate] L/s by [key_name(usr)]"
investigate_log(msg, INVESTIGATE_ATMOS)
investigate_log(msg, INVESTIGATE_SUPERMATTER) // yogs - make supermatter invest useful
if("toggle_filter")
var/gas = params["val"]
if(gas in GLOB.gas_data.names)
filter_type ^= gas
var/change
if(gas in filter_type)
change = "added"
else
change = "removed"
var/msg = "[key_name(usr)] [change] [gas] from the filter type."
investigate_log(msg, INVESTIGATE_ATMOS)
investigate_log(msg, INVESTIGATE_SUPERMATTER) // yogs - make supermatter invest useful
. = TRUE
update_appearance(UPDATE_ICON)
/obj/machinery/atmospherics/components/trinary/filter/can_unwrench(mob/user)
. = ..()
if(. && on && is_operational())
to_chat(user, span_warning("You cannot unwrench [src], turn it off first!"))
return FALSE
// mapping
/obj/machinery/atmospherics/components/trinary/filter/layer2
piping_layer = 2
icon_state = "filter_off_map-2"
/obj/machinery/atmospherics/components/trinary/filter/layer4
piping_layer = 4
icon_state = "filter_off_map-4"
/obj/machinery/atmospherics/components/trinary/filter/on
on = TRUE
icon_state = "filter_on-0"
/obj/machinery/atmospherics/components/trinary/filter/on/layer2
piping_layer = 2
icon_state = "filter_on_map-2"
/obj/machinery/atmospherics/components/trinary/filter/on/layer4
piping_layer = 4
icon_state = "filter_on_map-4"
/obj/machinery/atmospherics/components/trinary/filter/flipped
icon_state = "filter_off-0_f"
flipped = TRUE
/obj/machinery/atmospherics/components/trinary/filter/flipped/layer2
piping_layer = 2
icon_state = "filter_off_f_map-2"
/obj/machinery/atmospherics/components/trinary/filter/flipped/layer4
piping_layer = 4
icon_state = "filter_off_f_map-4"
/obj/machinery/atmospherics/components/trinary/filter/flipped/on
on = TRUE
icon_state = "filter_on-0_f"
/obj/machinery/atmospherics/components/trinary/filter/flipped/on/layer2
piping_layer = 2
icon_state = "filter_on_f_map-2"
/obj/machinery/atmospherics/components/trinary/filter/flipped/on/layer4
piping_layer = 4
icon_state = "filter_on_f_map-4"
/obj/machinery/atmospherics/components/trinary/filter/atmos //Used for atmos waste loops
on = TRUE
icon_state = "filter_on-0"
/obj/machinery/atmospherics/components/trinary/filter/atmos/n2
name = "Nitrogen filter (N2)"
filter_type = list(GAS_N2)
/obj/machinery/atmospherics/components/trinary/filter/atmos/o2
name = "Oxygen filter (O2)"
filter_type = list(GAS_O2)
/obj/machinery/atmospherics/components/trinary/filter/atmos/co2
name = "Carbon dioxide filter (CO2)"
filter_type = list(GAS_CO2)
/obj/machinery/atmospherics/components/trinary/filter/atmos/n2o
name = "Nitrous oxide filter (N2O)"
filter_type = list(GAS_NITROUS)
/obj/machinery/atmospherics/components/trinary/filter/atmos/plasma
name = "Plasma filter"
filter_type = list(GAS_PLASMA)
/obj/machinery/atmospherics/components/trinary/filter/atmos/flipped //This feels wrong, I know
icon_state = "filter_on-0_f"
flipped = TRUE
/obj/machinery/atmospherics/components/trinary/filter/atmos/flipped/n2
name = "Nitrogen filter (N2)"
filter_type = list(GAS_N2)
/obj/machinery/atmospherics/components/trinary/filter/atmos/flipped/o2
name = "Oxygen filter (O2)"
filter_type = list(GAS_O2)
/obj/machinery/atmospherics/components/trinary/filter/atmos/flipped/co2
name = "Carbon dioxide filter (CO2)"
filter_type = list(GAS_CO2)
/obj/machinery/atmospherics/components/trinary/filter/atmos/flipped/n2o
name = "Nitrous oxide filter (N2O)"
filter_type = list(GAS_NITROUS)
/obj/machinery/atmospherics/components/trinary/filter/atmos/flipped/plasma
name = "Plasma filter"
filter_type = list(GAS_PLASMA)
// These two filter types have critical_machine flagged to on and thus causes the area they are in to be exempt from the Grid Check event.
/obj/machinery/atmospherics/components/trinary/filter/critical
critical_machine = TRUE
/obj/machinery/atmospherics/components/trinary/filter/flipped/critical
critical_machine = TRUE