[Ready] Atmos trinary devices improvement (#40525)

* Atmos trinary devices improvement

In here: A slightly more compact mixer

* improving filter pipes

adding early returns and stuff

* !var is truthy for negative numbers

* see above

* Update filter.dm

* Update filter.dm

* you voted for the runtimes, you get the runtimes

* Mandatory return

* mandatory returns
This commit is contained in:
Shdorsh
2018-10-13 10:47:46 -06:00
committed by duncathan salt
parent f1d19d5787
commit cd217148d7
2 changed files with 45 additions and 49 deletions
@@ -122,7 +122,11 @@
if(!on || !(nodes[1] && nodes[2] && nodes[3]) || !is_operational())
return
//Early return
var/datum/gas_mixture/air1 = airs[1]
if(!air1 || air1.temperature <= 0)
return
var/datum/gas_mixture/air2 = airs[2]
var/datum/gas_mixture/air3 = airs[3]
@@ -132,43 +136,40 @@
//No need to transfer if target is already full!
return
//Calculate necessary moles to transfer using PV=nRT
var/pressure_delta = target_pressure - output_starting_pressure
var/transfer_moles
if(air1.temperature > 0)
transfer_moles = pressure_delta*air3.volume/(air1.temperature * R_IDEAL_GAS_EQUATION)
//Calculate necessary moles to transfer using PV=nRT, no need to create a new var that will be used only once (delta)
var/transfer_moles = (target_pressure - output_starting_pressure)*air3.volume/(air1.temperature * R_IDEAL_GAS_EQUATION)
//Actually transfer the gas
if(transfer_moles > 0)
var/datum/gas_mixture/removed = air1.remove(transfer_moles)
if(transfer_moles <= 0)
return
if(!removed)
return
var/datum/gas_mixture/removed = air1.remove(transfer_moles)
var/filtering = TRUE
if(!ispath(filter_type))
if(filter_type)
filter_type = gas_id2path(filter_type) //support for mappers so they don't need to type out paths
else
filtering = FALSE
if(!removed)
return
if(filtering && removed.gases[filter_type])
var/datum/gas_mixture/filtered_out = new
var/filtering = TRUE
if(!ispath(filter_type))
if(filter_type)
filter_type = gas_id2path(filter_type) //support for mappers so they don't need to type out paths
else
filtering = FALSE
filtered_out.temperature = removed.temperature
filtered_out.add_gas(filter_type)
filtered_out.gases[filter_type][MOLES] = removed.gases[filter_type][MOLES]
if(filtering && removed.gases[filter_type])
var/datum/gas_mixture/filtered_out = new
removed.gases[filter_type][MOLES] = 0
removed.garbage_collect()
filtered_out.temperature = removed.temperature
filtered_out.add_gas(filter_type)
filtered_out.gases[filter_type][MOLES] = removed.gases[filter_type][MOLES]
var/datum/gas_mixture/target = (air2.return_pressure() < target_pressure ? air2 : air1) //if there's no room for the filtered gas; just leave it in air1
target.merge(filtered_out)
removed.gases[filter_type][MOLES] = 0
removed.garbage_collect()
air3.merge(removed)
var/datum/gas_mixture/target = (air2.return_pressure() < target_pressure ? air2 : air1) //if there's no room for the filtered gas; just leave it in air1
target.merge(filtered_out)
air3.merge(removed)
update_parents()
@@ -93,8 +93,13 @@
if(!on || !(nodes[1] && nodes[2] && nodes[3]) && !is_operational())
return
//Get those gases, mah boiiii
var/datum/gas_mixture/air1 = airs[1]
var/datum/gas_mixture/air2 = airs[2]
if(!air1 || !air2 || air1.temperature <= 0 || air2.temperature <= 0)
return
var/datum/gas_mixture/air3 = airs[3]
var/output_starting_pressure = air3.return_pressure()
@@ -105,40 +110,32 @@
//Calculate necessary moles to transfer using PV=nRT
var/pressure_delta = target_pressure - output_starting_pressure
var/transfer_moles1 = 0
var/transfer_moles2 = 0
var/general_transfer = (target_pressure - output_starting_pressure) * air3.volume / R_IDEAL_GAS_EQUATION
if(air1.temperature > 0)
transfer_moles1 = (node1_concentration * pressure_delta) * air3.volume / (air1.temperature * R_IDEAL_GAS_EQUATION)
var/transfer_moles1 = node1_concentration * general_transfer / air1.temperature
var/transfer_moles2 = node2_concentration * general_transfer / air2.temperature
if((transfer_moles2 <= 0) || (transfer_moles1 <= 0))
return
if(air2.temperature > 0)
transfer_moles2 = (node2_concentration * pressure_delta) * air3.volume / (air2.temperature * R_IDEAL_GAS_EQUATION)
var/air1_moles = air1.total_moles()
var/air2_moles = air2.total_moles()
if((air1_moles < transfer_moles1) || (air2_moles < transfer_moles2))
var/ratio = 0
if((transfer_moles1 > 0 ) && (transfer_moles2 > 0))
ratio = min(air1_moles / transfer_moles1, air2_moles / transfer_moles2)
if((transfer_moles2 == 0 ) && ( transfer_moles1 > 0))
ratio = air1_moles / transfer_moles1
if((transfer_moles1 == 0 ) && ( transfer_moles2 > 0))
ratio = air2_moles / transfer_moles2
ratio = min(air1_moles / transfer_moles1, air2_moles / transfer_moles2)
transfer_moles1 *= ratio
transfer_moles2 *= ratio
//Actually transfer the gas
if(transfer_moles1 > 0)
var/datum/gas_mixture/removed1 = air1.remove(transfer_moles1)
air3.merge(removed1)
var/datum/gas_mixture/removed1 = air1.remove(transfer_moles1)
air3.merge(removed1)
if(transfer_moles2 > 0)
var/datum/gas_mixture/removed2 = air2.remove(transfer_moles2)
air3.merge(removed2)
var/datum/gas_mixture/removed2 = air2.remove(transfer_moles2)
air3.merge(removed2)
if(transfer_moles1)
var/datum/pipeline/parent1 = parents[1]
@@ -151,8 +148,6 @@
var/datum/pipeline/parent3 = parents[3]
parent3.update = TRUE
return
/obj/machinery/atmospherics/components/trinary/mixer/ui_interact(mob/user, ui_key = "main", datum/tgui/ui = null, force_open = FALSE, \
datum/tgui/master_ui = null, datum/ui_state/state = GLOB.default_state)
ui = SStgui.try_update_ui(user, src, ui_key, ui, force_open)