Skip to content

Commit

Permalink
GET MOLES??? IS THAT YOU???
Browse files Browse the repository at this point in the history
  • Loading branch information
JixS4v committed Aug 31, 2024
1 parent 3754526 commit 2ce2fc1
Show file tree
Hide file tree
Showing 30 changed files with 134 additions and 163 deletions.
14 changes: 14 additions & 0 deletions code/__DEFINES/atmospherics/atmos_helpers.dm
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ GLOBAL_LIST_INIT(atmos_adjacent_savings, list(0,0))
#define TURF_SHARES(T) (LAZYLEN(T.atmos_adjacent_turfs))
/// Rounding
#define QUANTIZE(variable) (round((variable), (MOLAR_ACCURACY)))

// Macros to access moles. Used instead of listmos only when nullchecking is necessary.

///Gets the moles of a specific gas in a gas mixture.
#define GET_MOLES(gas, gas_mixture) (gas_mixture.gases[gas] ? gas_mixture.gases[gas][MOLES] : 0)

///Adds moles to a specific gas in a gas mixture, asserts the gas is present.
#define ADD_MOLES(gas, gas_mixture, moles) (ASSERT_GAS(gas, gas_mixture); gas_mixture.gases[gas][MOLES] += moles)

///Removes moles from a specific gas in a gas mixture, asserts the gas is present.
#define REMOVE_MOLES(gas, gas_mixture, moles) (ASSERT_GAS(gas, gas_mixture); max(gas_mixture.gases[gas][MOLES] -= moles, 0))

///Sets the moles of a specific gas in a gas mixture, asserts the gas is present.
#define SET_MOLES(gas, gas_mixture, moles) (ASSERT_GAS(gas, gas_mixture); gas_mixture.gases[gas][MOLES] = moles)
6 changes: 3 additions & 3 deletions code/__HELPERS/turfs.dm
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,12 @@ Turf and target are separate in case you want to teleport some distance from a t
if(id in GLOB.hardcoded_gases)
continue
return FALSE
if(air.gases[/datum/gas/oxygen][MOLES] < 16 || air.gases[/datum/gas/plasma][MOLES] || air.gases[/datum/gas/carbon_dioxide][MOLES] >= 10)
if(GET_MOLES(/datum/gas/oxygen, air) < 16 || air.gases[/datum/gas/plasma][MOLES] || GET_MOLES(/datum/gas/carbon_dioxide, air) >= 10)
return FALSE
var/temperature = air.return_temperature()
var/temperature = air.temperature
if(temperature <= 270 || temperature >= 360)
return FALSE
var/pressure = air.return_pressure()
var/pressure = air.pressure
if(pressure <= 20 || pressure >= 550)
return FALSE
return TRUE
Expand Down
3 changes: 1 addition & 2 deletions code/controllers/subsystem/air.dm
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,7 @@ GLOBAL_LIST_EMPTY(colored_images)
var/path = id
if(!ispath(path))
path = gas_id2path(path) //a lot of these strings can't have embedded expressions (especially for mappers), so support for IDs needs to stick around
ADD_GAS(path, gases)
gases[path][MOLES] = text2num(gas[id])
SET_MOLES(path, gases, text2num(gas[id]))

if(istype(canonical_mix, /datum/gas_mixture/immutable))
return canonical_mix
Expand Down
14 changes: 7 additions & 7 deletions code/datums/atmosphere/_atmosphere.dm
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
var/datum/gas_mixture/gasmix = new
gasmix.temperature = rand(minimum_temp, maximum_temp)
for(var/i in base_gases)
gasmix.gases[i][MOLES] = base_gases[i]
SET_MOLES(gasmix, i, base_gases[i])


// Now let the random choices begin
var/datum/gas/gastype
var/amount
while(gasmix.return_pressure() < target_pressure)
while(gasmix.pressure < target_pressure)
if(!prob(restricted_chance))
gastype = pick(normal_gases)
amount = normal_gases[gastype]
Expand All @@ -45,16 +45,16 @@
amount *= pressure_scalar // If we pick a really small target pressure we want roughly the same mix but less of it all
amount = CEILING(amount, 0.1)

gasmix.gases[gastype][MOLES] = gasmix.gases[gastype][MOLES] + amount
ADD_MOLES(gastype, gasmix, amount)

// That last one put us over the limit, remove some of it
while(gasmix.return_pressure() > target_pressure)
gasmix.gases[gastype][MOLES] = gasmix.gases[gastype][MOLES] - (gasmix.gases[gastype][MOLES] * 0.1)
gasmix.gases[gastype][MOLES] = FLOOR(gasmix.gases[gastype][MOLES], 0.1)
while(gasmix.pressure > target_pressure)
SET_MOLES(gastype, gasmix, GET_MOLES(gastype, gasmix) * 0.9)
SET_MOLES(gastype, gasmix, FLOOR(GET_MOLES(gastype,gasmix), 0.1))

// Now finally lets make that string
var/list/gas_string_builder = list()
for(var/i in gasmix.gases)
gas_string_builder += "[gasmix.gases[i][GAS_META][META_GAS_ID]]=[gasmix.gases[i][MOLES]]"
gas_string_builder += "[gasmix.gases[i][GAS_META][META_GAS_ID]]=[GET_MOLES(i, gasmix)]"
gas_string_builder += "TEMP=[gasmix.return_temperature()]"
gas_string = gas_string_builder.Join(";")
10 changes: 4 additions & 6 deletions code/datums/elements/atmos_requirements.dm
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@
if(!ST.air && (atmos_requirements["min_oxy"] || atmos_requirements["min_tox"] || atmos_requirements["min_n2"] || atmos_requirements["min_co2"]))
return FALSE

ST.air.assert_gases(/datum/gas/plasma, /datum/gas/oxygen, /datum/gas/nitrogen, /datum/gas/carbon_dioxide)

var/plas = ST.air.gases[/datum/gas/plasma][MOLES]
var/oxy = ST.air.gases[/datum/gas/oxygen][MOLES]
var/n2 = ST.air.gases[/datum/gas/nitrogen][MOLES]
var/co2 = ST.air.gases[/datum/gas/carbon_dioxide][MOLES]
var/plas = GET_MOLES(/datum/gas/plasma, ST.air)
var/oxy = GET_MOLES(/datum/gas/oxygen, ST.air)
var/n2 = GET_MOLES(/datum/gas/nitrogen, ST.air)
var/co2 = GET_MOLES(/datum/gas/carbon_dioxide, ST.air)

. = TRUE
if(atmos_requirements["min_oxy"] && oxy < atmos_requirements["min_oxy"])
Expand Down
2 changes: 1 addition & 1 deletion code/game/atoms.dm
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@
/atom/proc/plasma_ignition(strength, mob/user, reagent_reaction)
var/turf/T = get_turf(src)
var/datum/gas_mixture/environment = T.return_air()
if(environment.gases[/datum/gas/oxygen][MOLES] >= PLASMA_MINIMUM_OXYGEN_NEEDED) //Flashpoint ignition can only occur with at least this much oxygen present
if(GET_MOLES(/datum/gas/oxygen, environment) >= PLASMA_MINIMUM_OXYGEN_NEEDED) //Flashpoint ignition can only occur with at least this much oxygen present
//no reason to alert admins or create an explosion if there's not enough power to actually make an explosion
if(strength > 1)
if(user)
Expand Down
2 changes: 1 addition & 1 deletion code/game/gamemodes/objective_items.dm
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
var/target_amount = text2num(name)
var/found_amount = 0
var/datum/gas_mixture/mix = T.return_air()
found_amount += mix.gases[/datum/gas/plasma] ? mix.gases[/datum/gas/plasma][MOLES] : 0
found_amount += GET_MOLES(/datum/gas/plasma, mix)
return found_amount>=target_amount

/datum/objective_item/steal/functionalai
Expand Down
2 changes: 1 addition & 1 deletion code/game/machinery/shuttle/shuttle_heater.dm
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
/obj/machinery/atmospherics/components/unary/shuttle/heater/examine(mob/user)
. = ..()
var/datum/gas_mixture/air_contents = airs[1]
. += "The engine heater's gas dial reads [air_contents.gases[gas_type][MOLES]] moles of gas.<br>"
. += "The engine heater's gas dial reads [GET_MOLES(gas_type, air_contents)] moles of gas.<br>"

/obj/machinery/atmospherics/components/unary/shuttle/heater/proc/updateGasStats()
var/datum/gas_mixture/air_contents = airs[1]
Expand Down
8 changes: 4 additions & 4 deletions code/game/objects/effects/effect_system/effects_foam.dm
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
if(hotspot && istype(T) && T.air)
qdel(hotspot)
var/datum/gas_mixture/G = T.air
var/plas_amt = min(30,G.gases[/datum/gas/plasma][MOLES]) //Absorb some plasma
G.gases[/datum/gas/plasma][MOLES] += -plas_amt
var/plas_amt = min(30,GET_MOLES(/datum/gas/plasma, G)) //Absorb some plasma
REMOVE_MOLES(/datum/gas/plasma, G, plas_amt)
absorbed_plasma += plas_amt
if(G.return_temperature() > T20C)
if(G.temperature > T20C)
G.temperature = max(G.return_temperature()/2,T20C)
T.air_update_turf(FALSE, FALSE)

Expand Down Expand Up @@ -342,7 +342,7 @@
for(var/I in G.gases)
if(I == /datum/gas/oxygen || I == /datum/gas/nitrogen)
continue
G.gases[I][MOLES] = 0
SET_MOLES(I , G, 0)

for(var/obj/machinery/atmospherics/components/unary/U in O)
if(!U.welded)
Expand Down
2 changes: 1 addition & 1 deletion code/game/objects/effects/effect_system/effects_smoke.dm
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
for(var/obj/effect/hotspot/H in T)
qdel(H)
if(G.gases[/datum/gas/plasma][MOLES])
G.gases[/datum/gas/nitrogen][MOLES] += G.gases[/datum/gas/plasma][MOLES]
ADD_MOLES(/datum/gas/nitrogen, G, G.gases[/datum/gas/plasma][MOLES])
G.gases[/datum/gas/plasma][MOLES] = 0

if (weldvents)
Expand Down
6 changes: 2 additions & 4 deletions code/game/objects/effects/spawners/bombspawner.dm
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
var/datum/gas_mixture/plasma_mix = plasma_tank.return_air()
var/datum/gas_mixture/oxygen_mix = oxygen_tank.return_air()

plasma_mix.assert_gas(/datum/gas/plasma)
plasma_mix.gases[/datum/gas/plasma][MOLES] = pressure_p*plasma_mix.volume/(R_IDEAL_GAS_EQUATION*CELSIUS_TO_KELVIN(temp_p))
SET_MOLES(plasma_mix, /datum/gas/plasma, pressure_p*plasma_mix.volume/(R_IDEAL_GAS_EQUATION*CELSIUS_TO_KELVIN(temp_p)))
plasma_mix.temperature = CELSIUS_TO_KELVIN(temp_p)

oxygen_mix.assert_gas(/datum/gas/oxygen)
oxygen_mix.gases[/datum/gas/oxygen][MOLES] = pressure_o*oxygen_mix.volume/(R_IDEAL_GAS_EQUATION*CELSIUS_TO_KELVIN(temp_o))
SET_MOLES(oxygen_mix, /datum/gas/oxygen, pressure_o*oxygen_mix.volume/(R_IDEAL_GAS_EQUATION*CELSIUS_TO_KELVIN(temp_o)))
oxygen_mix.temperature = CELSIUS_TO_KELVIN(temp_o)

V.tank_one = plasma_tank
Expand Down
6 changes: 2 additions & 4 deletions code/game/objects/items/chrono_eraser.dm
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,8 @@

/obj/structure/chrono_field/return_air() //we always have nominal air and temperature
var/datum/gas_mixture/GM = new
GM.gases[/datum/gas/oxygen][MOLES] = MOLES_O2STANDARD

GM.gases[/datum/gas/nitrogen][MOLES] = MOLES_N2STANDARD

SET_MOLES(GM, /datum/gas/oxygen, MOLES_O2STANDARD)
SET_MOLES(GM, /datum/gas/nitrogen, MOLES_N2STANDARD)
GM.temperature = T20C
return GM

Expand Down
34 changes: 17 additions & 17 deletions code/game/objects/items/devices/scanners.dm
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,8 @@ GENE SCANNER
message += "<span class='notice'>Pressure: [round(pressure,0.01)] kPa</span>"

for(var/id in air_contents.gases)
var/gas_concentration = air_contents.gases[id][MOLES]/total_moles
message += "<span class='notice'>[air_contents.gases[id][GAS_META][META_GAS_NAME]]: [round(gas_concentration*100, 0.01)] % ([round(air_contents.gases[id][MOLES], 0.01)] mol)</span>"
var/gas_concentration = GET_MOLES(id,air_contents)/total_moles
message += "<span class='notice'>[air_contents.gases[id][GAS_META][META_GAS_NAME]]: [round(gas_concentration*100, 0.01)] % ([round(GET_MOLES(id, air_contents), 0.01)] mol)</span>"
message += "<span class='notice'>Temperature: [round(temperature - T0C,0.01)] &deg;C ([round(temperature, 0.01)] K)</span>"

else
Expand Down Expand Up @@ -702,37 +702,37 @@ GENE SCANNER
else
message += "<span class='alert'>Pressure: [round(pressure, 0.01)] kPa</span>"
if(total_moles)
var/o2_concentration = environment.gases[/datum/gas/oxygen][MOLES]/total_moles
var/n2_concentration = environment.gases[/datum/gas/nitrogen][MOLES]/total_moles
var/co2_concentration = environment.gases[/datum/gas/carbon_dioxide][MOLES]/total_moles
var/plasma_concentration = environment.gases[/datum/gas/plasma][MOLES]/total_moles
var/o2_concentration = GET_MOLES(/datum/gas/oxygen, environment)/total_moles
var/n2_concentration = GET_MOLES(/datum/gas/nitrogen, environment)/total_moles
var/co2_concentration = GET_MOLES(/datum/gas/carbon_dioxide, environment)/total_moles
var/plasma_concentration =

if(abs(n2_concentration - N2STANDARD) < 20)
message += "<span class='info'>Nitrogen: [round(n2_concentration*100, 0.01)] % ([round(environment.gases[/datum/gas/nitrogen][MOLES], 0.01)] mol)</span>"
message += "<span class='info'>Nitrogen: [round(n2_concentration*100, 0.01)] % ([round(n2_concentration*total_moles, 0.01)] mol)</span>"
else
message += "<span class='alert'>Nitrogen: [round(n2_concentration*100, 0.01)] % ([round(environment.gases[/datum/gas/nitrogen][MOLES], 0.01)] mol)</span>"
message += "<span class='alert'>Nitrogen: [round(n2_concentration*100, 0.01)] % ([round(n2_concentration*total_moles, 0.01)] mol)</span>"

if(abs(o2_concentration - O2STANDARD) < 2)
message += "<span class='info'>Oxygen: [round(o2_concentration*100, 0.01)] % ([round(environment.gases[/datum/gas/oxygen][MOLES], 0.01)] mol)</span>"
message += "<span class='info'>Oxygen: [round(o2_concentration*100, 0.01)] % ([round(o2_concentration*total_moles, 0.01)] mol)</span>"
else
message += "<span class='alert'>Oxygen: [round(o2_concentration*100, 0.01)] % ([round(environment.gases[/datum/gas/oxygen][MOLES], 0.01)] mol)</span>"
message += "<span class='alert'>Oxygen: [round(o2_concentration*100, 0.01)] % ([round(o2_concentration*total_moles, 0.01)] mol)</span>"

if(co2_concentration > 0.01)
message += "<span class='alert'>CO2: [round(co2_concentration*100, 0.01)] % ([round(environment.gases[/datum/gas/carbon_dioxide][MOLES], 0.01)] mol)</span>"
message += "<span class='alert'>CO2: [round(co2_concentration*100, 0.01)] % ([round(co2_concentration*total_moles, 0.01)] mol)</span>"
else
message += "<span class='info'>CO2: [round(co2_concentration*100, 0.01)] % ([round(environment.gases[/datum/gas/carbon_dioxide][MOLES], 0.01)] mol)</span>"
message += "<span class='info'>CO2: [round(co2_concentration*100, 0.01)] % ([round(co2_concentration*total_moles, 0.01)] mol)</span>"

if(plasma_concentration > 0.005)
message += "<span class='alert'>Plasma: [round(plasma_concentration*100, 0.01)] % ([round(environment.gases[/datum/gas/plasma][MOLES], 0.01)] mol)</span>"
message += "<span class='alert'>Plasma: [round(plasma_concentration*100, 0.01)] % ([round(plasma_concentration*total_moles, 0.01)] mol)</span>"
else
message += "<span class='info'>Plasma: [round(plasma_concentration*100, 0.01)] % ([round(environment.gases[/datum/gas/plasma][MOLES], 0.01)] mol)</span>"
message += "<span class='info'>Plasma: [round(plasma_concentration*100, 0.01)] % ([round(plasma_concentration*total_moles, 0.01)] mol)</span>"

for(var/id in environment.gases)
if(id in GLOB.hardcoded_gases)
continue
var/gas_concentration = environment.gases[id][MOLES]/total_moles
message += "<span class='alert'>[environment.gases[id][GAS_META][META_GAS_NAME]]: [round(gas_concentration*100, 0.01)] % ([round(environment.gases[id][MOLES], 0.01)] mol)</span>"
message += "<span class='info'>Temperature: [round(environment.return_temperature()-T0C, 0.01)] &deg;C ([round(environment.return_temperature(), 0.01)] K)</span>"
var/gas_concentration = GET_MOLES(id, environment)/total_moles
message += "<span class='alert'>[environment.gases[id][GAS_META][META_GAS_NAME]]: [round(gas_concentration*100, 0.01)] % ([round(gas_concentration*total_moles, 0.01)] mol)</span>"
message += "<span class='info'>Temperature: [round(environment.temperature-T0C, 0.01)] &deg;C ([round(environment.temperature, 0.01)] K)</span>"
to_chat(user, EXAMINE_BLOCK(jointext(message, "\n")))

/obj/item/analyzer/ranged
Expand Down
22 changes: 9 additions & 13 deletions code/game/objects/items/tanks/jetpack.dm
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@

/obj/item/tank/jetpack/populate_gas()
if(gas_type)
air_contents.assert_gas(gas_type)
air_contents.gases[gas_type][MOLES] = ((6 * ONE_ATMOSPHERE * volume / (R_IDEAL_GAS_EQUATION * T20C)))
SET_MOLES(air_contents, gas_type, ((6 * ONE_ATMOSPHERE * volume / (R_IDEAL_GAS_EQUATION * T20C))))

/obj/item/tank/jetpack/ui_action_click(mob/user, action)
if(istype(action, /datum/action/item_action/toggle_jetpack))
Expand Down Expand Up @@ -338,11 +337,8 @@
var/ideal_o2_percent = (1 / PLASMA_OXYGEN_FULLBURN) * 2
var/datum/gas_mixture/temp_air_contents = return_air()

temp_air_contents.assert_gas(/datum/gas/plasma)
temp_air_contents.gases[/datum/gas/plasma][MOLES] = moles_full * (1 - ideal_o2_percent)

temp_air_contents.assert_gas(/datum/gas/oxygen)
temp_air_contents.gases[/datum/gas/oxygen][MOLES] = moles_full * ideal_o2_percent
SET_MOLES(temp_air_contents, /datum/gas/plasma, moles_full*(1-ideal_o2_percent))
SET_MOLES(temp_air_contents, /datum/gas/oxygen, moles_full*ideal_o2_percent)


/obj/item/tank/jetpack/combustion/allow_thrust(num, mob/living/user, use_fuel = TRUE)
Expand All @@ -358,12 +354,12 @@
// Also produces no waste products (CO2/Trit)
var/oxygen_burn_rate = (OXYGEN_BURN_RATE_BASE - 1)
var/plasma_burn_rate = 0
if(our_mix.gases[/datum/gas/oxygen][MOLES] > our_mix.gases[/datum/gas/plasma][MOLES]*PLASMA_OXYGEN_FULLBURN)
plasma_burn_rate = our_mix.gases[/datum/gas/plasma][MOLES]/PLASMA_BURN_RATE_DELTA
if(GET_MOLES(/datum/gas/oxygen, our_mix) > GET_MOLES(/datum/gas/plasma, our_mix) * PLASMA_OXYGEN_FULLBURN)
plasma_burn_rate = GET_MOLES(/datum/gas/plasma, our_mix)/PLASMA_BURN_RATE_DELTA
else
plasma_burn_rate = (our_mix.gases[/datum/gas/oxygen][MOLES]/PLASMA_OXYGEN_FULLBURN)/PLASMA_BURN_RATE_DELTA
plasma_burn_rate = (GET_MOLES(/datum/gas/plasma, our_mix)/PLASMA_OXYGEN_FULLBURN)/PLASMA_BURN_RATE_DELTA
if(plasma_burn_rate > MINIMUM_HEAT_CAPACITY)
plasma_burn_rate = min(plasma_burn_rate,our_mix.gases[/datum/gas/plasma][MOLES],our_mix.gases[/datum/gas/oxygen][MOLES]/oxygen_burn_rate) //Ensures matter is conserved properly
plasma_burn_rate = min(plasma_burn_rate,GET_MOLES(/datum/gas/plasma, our_mix),GET_MOLES(/datum/gas/oxygen, our_mix)/oxygen_burn_rate) //Ensures matter is conserved properly
potential_energy = FIRE_PLASMA_ENERGY_RELEASED * (plasma_burn_rate)

// Normalize thrust volume to joules
Expand All @@ -377,8 +373,8 @@

// Consume
if(use_fuel)
our_mix.gases[/datum/gas/plasma][MOLES] = QUANTIZE(our_mix.gases[/datum/gas/plasma][MOLES] - plasma_burn_rate)
our_mix.gases[/datum/gas/oxygen][MOLES] = QUANTIZE(our_mix.gases[/datum/gas/oxygen][MOLES] - (plasma_burn_rate * oxygen_burn_rate))
SET_MOLES(/datum/gas/plasma, our_mix, QUANTIZE(GET_MOLES(/datum/gas/plasma, our_mix) - plasma_burn_rate))
SET_MOLES(/datum/gas/oxygen, our_mix, QUANTIZE(GET_MOLES(/datum/gas/oxygen, our_mix) - (plasma_burn_rate * oxygen_burn_rate)))
update_fade(15)
update_lifespan(4)

Expand Down
Loading

0 comments on commit 2ce2fc1

Please sign in to comment.