From f7eea33908925d7eaa26e0efb3d036223444f410 Mon Sep 17 00:00:00 2001 From: Oznogon Date: Mon, 27 Oct 2025 11:24:38 -0700 Subject: [PATCH 1/3] Clamp system coolant to 100% when using autocoolant --- src/systems/coolantsystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/systems/coolantsystem.cpp b/src/systems/coolantsystem.cpp index 271a8e905f..bb64e1c8d2 100644 --- a/src/systems/coolantsystem.cpp +++ b/src/systems/coolantsystem.cpp @@ -20,7 +20,7 @@ void CoolantSystem::update(float delta) for(int n = 0; n < ShipSystem::COUNT; n++) { auto sys = ShipSystem::get(entity, ShipSystem::Type(n)); if (!sys) continue; - sys->coolant_request = coolant.max * sys->heat_level / total_heat; + sys->coolant_request = std::min(coolant.max * sys->heat_level / total_heat, coolant.max_coolant_per_system); } } } From 7917a4c7c802c3bcdb26675910a1697f4369e6af Mon Sep 17 00:00:00 2001 From: Oznogon Date: Mon, 27 Oct 2025 18:04:07 -0700 Subject: [PATCH 2/3] Refactor autocoolant distribution Autocoolant behavior is inconsistent when max coolant exceeds the default of 10 (equal to max_coolant_per_system). Coolant requests would exceed 100% of max_coolant_per_system, and capping the request to max_coolant_per_system would distribute the remaining coolant inefficiently. Instead, track the ideal request amounts, then modify them to fit within the limits while redistributing the excess coolant to other systems in need. --- src/systems/coolantsystem.cpp | 89 +++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 14 deletions(-) diff --git a/src/systems/coolantsystem.cpp b/src/systems/coolantsystem.cpp index bb64e1c8d2..2916f04a34 100644 --- a/src/systems/coolantsystem.cpp +++ b/src/systems/coolantsystem.cpp @@ -3,45 +3,106 @@ #include "components/coolant.h" #include "components/shipsystem.h" - void CoolantSystem::update(float delta) { - for(auto[entity, coolant] : sp::ecs::Query()) { - // Automate cooling if auto_coolant_enabled is true. Distributes coolant to - // subsystems proportionally to their share of the total generated heat. - if (coolant.auto_levels) { + for (auto[entity, coolant] : sp::ecs::Query()) + { + // Automate coolant distribution if auto_levels is set. Distribute to + // systems in proportion to their share of the total generated heat. + if (coolant.auto_levels) + { float total_heat = 0.0f; - for(int n = 0; n < ShipSystem::COUNT; n++) { + + for (int n = 0; n < ShipSystem::COUNT; n++) + { auto sys = ShipSystem::get(entity, ShipSystem::Type(n)); if (!sys) continue; total_heat += sys->heat_level; } - if (total_heat > 0.0f) { - for(int n = 0; n < ShipSystem::COUNT; n++) { + + if (total_heat > 0.0f) + { + bool excess_redistributed; + + // Calculate ideal proportional distribution. + for (int n = 0; n < ShipSystem::COUNT; n++) + { auto sys = ShipSystem::get(entity, ShipSystem::Type(n)); if (!sys) continue; - sys->coolant_request = std::min(coolant.max * sys->heat_level / total_heat, coolant.max_coolant_per_system); + sys->coolant_request = coolant.max * sys->heat_level / total_heat; } + + // Check for excess coolant from capped systems and redistribute + // it if necessary. + do + { + excess_redistributed = false; + float excess_coolant = 0.0f; + float available_heat = 0.0f; + + // Find systems requesting more than max_coolant_per_system + // and calculate excess. + for (int n = 0; n < ShipSystem::COUNT; n++) + { + auto sys = ShipSystem::get(entity, ShipSystem::Type(n)); + if (!sys) continue; + + if (sys->coolant_request > coolant.max_coolant_per_system) + { + excess_coolant += sys->coolant_request - coolant.max_coolant_per_system; + sys->coolant_request = coolant.max_coolant_per_system; + } + else if (sys->coolant_request < coolant.max_coolant_per_system && sys->heat_level > 0.0f) + available_heat += sys->heat_level; + } + + // Redistribute excess coolant proportionally to uncapped + // systems that have heat, if any. + if (excess_coolant > 0.0f && available_heat > 0.0f) + { + for (int n = 0; n < ShipSystem::COUNT; n++) + { + auto sys = ShipSystem::get(entity, ShipSystem::Type(n)); + if (!sys) continue; + + if (sys->coolant_request < coolant.max_coolant_per_system && sys->heat_level > 0.0f) + { + float additional = excess_coolant * sys->heat_level / available_heat; + sys->coolant_request += additional; + excess_redistributed = true; + } + } + } + } while (excess_redistributed); } } + // Otherwise, distribute coolant manually. System limits are instead + // enforced by UI controls. + // Check how much coolant we have requested in total, and if that's beyond the - // amount of coolant we have, see how much we need to adjust our request. + // amount of coolant we have, see how much we need to adjust our request. float total_coolant_request = 0.0f; - for(int n = 0; n < ShipSystem::COUNT; n++) { + float coolant_request_factor = 1.0f; + + for (int n = 0; n < ShipSystem::COUNT; n++) + { auto sys = ShipSystem::get(entity, ShipSystem::Type(n)); if (sys) total_coolant_request += sys->coolant_request; } - float coolant_request_factor = 1.0f; + if (total_coolant_request > coolant.max) coolant_request_factor = coolant.max / total_coolant_request; - for(int n = 0; n < ShipSystem::COUNT; n++) { + for (int n = 0; n < ShipSystem::COUNT; n++) + { auto sys = ShipSystem::get(entity, ShipSystem::Type(n)); if (!sys) continue; float coolant_request = sys->coolant_request * coolant_request_factor; - if (coolant_request > sys->coolant_level) { + + if (coolant_request > sys->coolant_level) + { sys->coolant_level += delta * sys->coolant_change_rate_per_second; if (sys->coolant_level > coolant_request) sys->coolant_level = coolant_request; From bb34422fac4804fb5ed92f91297231267fa279cb Mon Sep 17 00:00:00 2001 From: Oznogon Date: Mon, 27 Oct 2025 18:24:09 -0700 Subject: [PATCH 3/3] Remove extraneous variable --- src/systems/coolantsystem.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/systems/coolantsystem.cpp b/src/systems/coolantsystem.cpp index 2916f04a34..69520c525a 100644 --- a/src/systems/coolantsystem.cpp +++ b/src/systems/coolantsystem.cpp @@ -67,8 +67,7 @@ void CoolantSystem::update(float delta) if (sys->coolant_request < coolant.max_coolant_per_system && sys->heat_level > 0.0f) { - float additional = excess_coolant * sys->heat_level / available_heat; - sys->coolant_request += additional; + sys->coolant_request += excess_coolant * sys->heat_level / available_heat; excess_redistributed = true; } }