Skip to content

Commit

Permalink
Initialize Atmospherics much faster (#27862)
Browse files Browse the repository at this point in the history
* Read atmos from MILLA instead.

* Runtime fix

* Build Rust library

---------

Co-authored-by: paradisess13[bot] <165046124+paradisess13[bot]@users.noreply.github.com>
FunnyMan3595 and paradisess13[bot] authored Jan 7, 2025
1 parent 2fd9200 commit 24f6787
Showing 10 changed files with 103 additions and 51 deletions.
5 changes: 5 additions & 0 deletions code/__DEFINES/rust.dm
Original file line number Diff line number Diff line change
@@ -60,6 +60,11 @@
/proc/milla_init_z(z)
return RUSTLIB_CALL(milla_initialize, z)

/proc/milla_load_turfs(turf/low_corner, turf/high_corner)
ASSERT(istype(low_corner))
ASSERT(istype(high_corner))
return RUSTLIB_CALL(milla_load_turfs, "milla_data", low_corner, high_corner)

/proc/set_tile_atmos(turf/T, airtight_north, airtight_east, airtight_south, airtight_west, atmos_mode, environment_id, oxygen, carbon_dioxide, nitrogen, toxins, sleeping_agent, agent_b, temperature, innate_heat_capacity, hotspot_temperature, hotspot_volume)
return RUSTLIB_CALL(milla_set_tile, T, airtight_north, airtight_east, airtight_south, airtight_west, atmos_mode, environment_id, oxygen, carbon_dioxide, nitrogen, toxins, sleeping_agent, agent_b, temperature, innate_heat_capacity, hotspot_temperature, hotspot_volume)

13 changes: 4 additions & 9 deletions code/controllers/subsystem/SSair.dm
Original file line number Diff line number Diff line change
@@ -157,8 +157,7 @@ SUBSYSTEM_DEF(air)
in_milla_safe_code = TRUE

setup_overlays() // Assign icons and such for gas-turf-overlays
setup_allturfs()
setup_write_to_milla()
setup_turfs()
setup_atmos_machinery(GLOB.machines)
setup_pipenets(GLOB.machines)
for(var/obj/machinery/atmospherics/A in machinery_to_construct)
@@ -614,14 +613,10 @@ SUBSYSTEM_DEF(air)
last_bound_mixtures = length(bound_mixtures)
return

/datum/controller/subsystem/air/proc/setup_allturfs(list/turfs_to_init = block(locate(1, 1, 1), locate(world.maxx, world.maxy, world.maxz)))
for(var/turf/T as anything in turfs_to_init)
T.Initialize_Atmos(times_fired)
CHECK_TICK

/datum/controller/subsystem/air/proc/setup_allturfs_sleepless(list/turfs_to_init = block(locate(1, 1, 1), locate(world.maxx, world.maxy, world.maxz)))
for(var/turf/T as anything in turfs_to_init)
/datum/controller/subsystem/air/proc/setup_turfs(turf/low_corner = locate(1, 1, 1), turf/high_corner = locate(world.maxx, world.maxy, world.maxz))
for(var/turf/T as anything in block(low_corner, high_corner))
T.Initialize_Atmos(times_fired)
milla_load_turfs(low_corner, high_corner)

/datum/controller/subsystem/air/proc/setup_write_to_milla()
var/watch = start_watch()
4 changes: 2 additions & 2 deletions code/datums/helper_datums/map_template.dm
Original file line number Diff line number Diff line change
@@ -63,13 +63,13 @@
catch(var/exception/e)
GLOB.space_manager.remove_dirt(placement.z)
var/datum/milla_safe/late_setup_level/milla = new()
milla.invoke_async(block(bot_left, top_right), block(ST_bot_left, ST_top_right))
milla.invoke_async(bot_left, top_right, block(ST_bot_left, ST_top_right))
message_admins("Map template [name] threw an error while loading. Safe exit attempted, but check for errors at [ADMIN_COORDJMP(placement)].")
log_admin("Map template [name] threw an error while loading. Safe exit attempted.")
throw e
GLOB.space_manager.remove_dirt(placement.z)
var/datum/milla_safe/late_setup_level/milla = new()
milla.invoke_async(block(bot_left, top_right), block(ST_bot_left, ST_top_right))
milla.invoke_async(bot_left, top_right, block(ST_bot_left, ST_top_right))

log_game("[name] loaded at [min_x],[min_y],[placement.z]")
return 1
8 changes: 8 additions & 0 deletions code/game/turfs/turf.dm
Original file line number Diff line number Diff line change
@@ -85,6 +85,14 @@
/// The effect used to render a pressure overlay from this tile.
var/obj/effect/pressure_overlay/pressure_overlay

var/list/milla_atmos_airtight = list(FALSE, FALSE, FALSE, FALSE)
var/list/milla_superconductivity = list(
OPEN_HEAT_TRANSFER_COEFFICIENT,
OPEN_HEAT_TRANSFER_COEFFICIENT,
OPEN_HEAT_TRANSFER_COEFFICIENT,
OPEN_HEAT_TRANSFER_COEFFICIENT)
var/list/milla_data = list()

/turf/Initialize(mapload)
SHOULD_CALL_PARENT(FALSE)
if(initialized)
69 changes: 32 additions & 37 deletions code/modules/atmospherics/environmental/LINDA_turf_tile.dm
Original file line number Diff line number Diff line change
@@ -172,18 +172,10 @@
#define INDEX_WEST 4

/turf/proc/Initialize_Atmos(times_fired)
recalculate_atmos_connectivity()

if(!blocks_air)
var/datum/gas_mixture/air = new()
air.set_oxygen(oxygen)
air.set_carbon_dioxide(carbon_dioxide)
air.set_nitrogen(nitrogen)
air.set_toxins(toxins)
air.set_sleeping_agent(sleeping_agent)
air.set_agent_b(agent_b)
air.set_temperature(temperature)
blind_set_air(air)
// This is one of two places expected to call this otherwise-unsafe method.
private_unsafe_recalculate_atmos_connectivity()
var/list/air = list(oxygen, carbon_dioxide, nitrogen, toxins, sleeping_agent, agent_b, temperature)
milla_data = milla_atmos_airtight + list(atmos_mode, SSmapping.environments[atmos_environment]) + air + milla_superconductivity

/turf/proc/recalculate_atmos_connectivity()
var/datum/milla_safe/recalculate_atmos_connectivity/milla = new()
@@ -192,48 +184,51 @@
/datum/milla_safe/recalculate_atmos_connectivity

/datum/milla_safe/recalculate_atmos_connectivity/on_run(turf/T)
if(isnull(T))
if(!istype(T))
return

if(T.blocks_air)
set_tile_airtight(T, list(TRUE, TRUE, TRUE, TRUE))
// Will be needed when we go back to having solid tile conductivity.
//reset_superconductivity(src)
reduce_superconductivity(T, list(0, 0, 0, 0))
// This is one of two places expected to call this otherwise-unsafe method.
T.private_unsafe_recalculate_atmos_connectivity()

set_tile_airtight(T, T.milla_atmos_airtight)
reset_superconductivity(T)
reduce_superconductivity(T, T.milla_superconductivity)

/// This method is unsafe to use because it only updates milla_* properties, but does not write them to MILLA. Use recalculate_atmos_connectivity() instead.
/turf/proc/private_unsafe_recalculate_atmos_connectivity()
if(blocks_air)
milla_atmos_airtight = list(TRUE, TRUE, TRUE, TRUE)
milla_superconductivity = list(0, 0, 0, 0)
return

var/list/atmos_airtight = list(
!T.CanAtmosPass(NORTH, FALSE),
!T.CanAtmosPass(EAST, FALSE),
!T.CanAtmosPass(SOUTH, FALSE),
!T.CanAtmosPass(WEST, FALSE))
milla_atmos_airtight = list(
!CanAtmosPass(NORTH, FALSE),
!CanAtmosPass(EAST, FALSE),
!CanAtmosPass(SOUTH, FALSE),
!CanAtmosPass(WEST, FALSE))

var/list/superconductivity = list(
milla_superconductivity = list(
OPEN_HEAT_TRANSFER_COEFFICIENT,
OPEN_HEAT_TRANSFER_COEFFICIENT,
OPEN_HEAT_TRANSFER_COEFFICIENT,
OPEN_HEAT_TRANSFER_COEFFICIENT)

for(var/obj/O in T)
for(var/obj/O in src)
if(istype(O, /obj/item))
// Items can't block atmos.
continue
if(!O.CanAtmosPass(NORTH))
atmos_airtight[INDEX_NORTH] = TRUE
milla_atmos_airtight[INDEX_NORTH] = TRUE
if(!O.CanAtmosPass(EAST))
atmos_airtight[INDEX_EAST] = TRUE
milla_atmos_airtight[INDEX_EAST] = TRUE
if(!O.CanAtmosPass(SOUTH))
atmos_airtight[INDEX_SOUTH] = TRUE
milla_atmos_airtight[INDEX_SOUTH] = TRUE
if(!O.CanAtmosPass(WEST))
atmos_airtight[INDEX_WEST] = TRUE
superconductivity[INDEX_NORTH] = min(superconductivity[INDEX_NORTH], O.get_superconductivity(NORTH))
superconductivity[INDEX_EAST] = min(superconductivity[INDEX_EAST], O.get_superconductivity(EAST))
superconductivity[INDEX_SOUTH] = min(superconductivity[INDEX_SOUTH], O.get_superconductivity(SOUTH))
superconductivity[INDEX_WEST] = min(superconductivity[INDEX_WEST], O.get_superconductivity(WEST))

set_tile_airtight(T, atmos_airtight)
reset_superconductivity(T)
reduce_superconductivity(T, superconductivity)
milla_atmos_airtight[INDEX_WEST] = TRUE
milla_superconductivity[INDEX_NORTH] = min(milla_superconductivity[INDEX_NORTH], O.get_superconductivity(NORTH))
milla_superconductivity[INDEX_EAST] = min(milla_superconductivity[INDEX_EAST], O.get_superconductivity(EAST))
milla_superconductivity[INDEX_SOUTH] = min(milla_superconductivity[INDEX_SOUTH], O.get_superconductivity(SOUTH))
milla_superconductivity[INDEX_WEST] = min(milla_superconductivity[INDEX_WEST], O.get_superconductivity(WEST))

/obj/effect/wind
anchored = TRUE
6 changes: 3 additions & 3 deletions code/modules/awaymissions/zlevel_helpers.dm
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/datum/milla_safe/late_setup_level

// Ensures that atmos and environment are set up.
/datum/milla_safe/late_setup_level/on_run(turfs, smoothTurfs)
/datum/milla_safe/late_setup_level/on_run(turf/bot_left, turf/top_right, smoothTurfs)
var/total_timer = start_watch()
var/subtimer = start_watch()
if(!smoothTurfs)
smoothTurfs = turfs
smoothTurfs = block(bot_left, top_right)

log_debug("Setting up atmos")
/* setup_allturfs is superfluous during server initialization because
* air subsystem will call subsequently call setup_allturfs with _every_
* turf in the world */
if(SSair && SSair.initialized)
SSair.setup_allturfs_sleepless(turfs)
SSair.setup_turfs(bot_left, top_right)
log_debug("\tTook [stop_watch(subtimer)]s")

subtimer = start_watch()
49 changes: 49 additions & 0 deletions rust/src/milla/api.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ use crate::milla::simulate;
use crate::milla::statics::*;
use crate::milla::tick;
use byondapi::global_call::call_global;
use byondapi::map::byond_block;
use byondapi::map::byond_xyz;
use byondapi::prelude::*;
use eyre::eyre;
@@ -97,6 +98,54 @@ pub(crate) fn internal_create_environment(
buffers.create_environment(tile)
}

/// BYOND API for loading a block of turfs into MILLA with their default air.
#[byondapi::bind]
fn milla_load_turfs(data_property: ByondValue, low_corner: ByondValue, high_corner: ByondValue) -> eyre::Result<ByondValue> {
let property_ref = data_property.get_strid()?;
for turf in byond_block(byond_xyz(&low_corner)?, byond_xyz(&high_corner)?)? {
let (x, y, z) = byond_xyz(&turf)?.coordinates();
let data = turf.read_var_id(property_ref)?.get_list_values()?;
if data.len() != 17 {
return Err(eyre!("data property has the wrong length: {} vs {}", data.len(), 17));
}

internal_set_tile(
x as i32 - 1,
y as i32 - 1,
z as i32 - 1,
conversion::byond_to_option_f32(data[0])?,
conversion::byond_to_option_f32(data[1])?,
conversion::byond_to_option_f32(data[2])?,
conversion::byond_to_option_f32(data[3])?,
conversion::byond_to_option_f32(data[4])?,
conversion::byond_to_option_f32(data[5])?,
conversion::bounded_byond_to_option_f32(data[6], 0.0, f32::INFINITY)?,
conversion::bounded_byond_to_option_f32(data[7], 0.0, f32::INFINITY)?,
conversion::bounded_byond_to_option_f32(data[8], 0.0, f32::INFINITY)?,
conversion::bounded_byond_to_option_f32(data[9], 0.0, f32::INFINITY)?,
conversion::bounded_byond_to_option_f32(data[10], 0.0, f32::INFINITY)?,
conversion::bounded_byond_to_option_f32(data[11], 0.0, f32::INFINITY)?,
conversion::bounded_byond_to_option_f32(data[12], 0.0, f32::INFINITY)?,
None,
Some(0.0),
Some(0.0),
Some(0.0),
)?;

internal_reset_superconductivity(x as i32 - 1, y as i32 - 1, z as i32 - 1)?;
internal_reduce_superconductivity(
x as i32 - 1,
y as i32 - 1,
z as i32 - 1,
conversion::bounded_byond_to_option_f32(data[13], 0.0, 1.0)?,
conversion::bounded_byond_to_option_f32(data[14], 0.0, 1.0)?,
conversion::bounded_byond_to_option_f32(data[15], 0.0, 1.0)?,
conversion::bounded_byond_to_option_f32(data[16], 0.0, 1.0)?,
)?;
}
Ok(ByondValue::null())
}

/// BYOND API for setting the atmos details of a tile.
#[byondapi::bind]
fn milla_set_tile(
Binary file modified rustlibs.dll
Binary file not shown.
Binary file modified rustlibs_prod.dll
Binary file not shown.
Binary file modified tools/ci/librustlibs_ci.so
Binary file not shown.

0 comments on commit 24f6787

Please sign in to comment.