From c3bbf4d9a097e484f74975fd235f48ad87f329da Mon Sep 17 00:00:00 2001 From: SyncIt21 <110812394+SyncIt21@users.noreply.github.com> Date: Wed, 1 Nov 2023 17:33:03 +0530 Subject: [PATCH] [NO GBP] Limits plumbing reaction chamber ph balance attempts (#79429) ## About The Pull Request - Fixes #79427 It's not a memory leak but a dedicated while loop that will run as many times as required to accurately balance the ph of the solution. Sometimes it makes way too many attempts causing high tick usage. Now the loop will run for a maximum of 5 times before giving up preventing lag. I decided 5 because that should be sufficient for most cases but it can be adjusted later by anyone interested by changing the value of the `MAX_PH_ADJUSTMENTS` define ## Changelog :cl: fix: plumbing reaction chamber will attempt to balance the ph of the solution a maximum of 5 times before giving up and thus preventing infinite loops, high tick usage /:cl: --- .../modules/plumbing/plumbers/reaction_chamber.dm | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/code/modules/plumbing/plumbers/reaction_chamber.dm b/code/modules/plumbing/plumbers/reaction_chamber.dm index d41c502ecf9..c1e56378408 100644 --- a/code/modules/plumbing/plumbers/reaction_chamber.dm +++ b/code/modules/plumbing/plumbers/reaction_chamber.dm @@ -3,6 +3,9 @@ /// coefficient to convert temperature to joules. same lvl as acclimator #define HEATER_COEFFICIENT 0.05 +/// maximum number of attempts the reaction chamber will make to balance the ph(More means better results but higher tick usage) +#define MAX_PH_ADJUSTMENTS 5 + /obj/machinery/plumbing/reaction_chamber name = "mixing chamber" desc = "Keeps chemicals separated until given conditions are met." @@ -170,7 +173,8 @@ return ..() /obj/machinery/plumbing/reaction_chamber/chem/handle_reagents(seconds_per_tick) - while(reagents.ph < acidic_limit || reagents.ph > alkaline_limit) + var/ph_balance_attempts = 0 + while(ph_balance_attempts < MAX_PH_ADJUSTMENTS && (reagents.ph < acidic_limit || reagents.ph > alkaline_limit)) //no power if(machine_stat & NOPOWER) return @@ -195,8 +199,9 @@ if(!buffer.trans_to(reagents, buffer_amount * seconds_per_tick)) return - //some power for accurate ph balancing + //some power for accurate ph balancing & keep track of attempts made use_power(active_power_usage * 0.03 * buffer_amount * seconds_per_tick) + ph_balance_attempts += 1 /obj/machinery/plumbing/reaction_chamber/chem/ui_interact(mob/user, datum/tgui/ui) ui = SStgui.try_update_ui(user, src, ui) @@ -215,11 +220,11 @@ switch(action) if("acidic") - acidic_limit = clamp(round(text2num(params["target"])), 0, alkaline_limit) + acidic_limit = clamp(round(text2num(params["target"])), CHEMICAL_MIN_PH, alkaline_limit - 1) if("alkaline") - alkaline_limit = clamp(round(text2num(params["target"])), acidic_limit + 0.01, 14) + alkaline_limit = clamp(round(text2num(params["target"])), acidic_limit + 1, CHEMICAL_MAX_PH) else return FALSE - #undef HEATER_COEFFICIENT +#undef MAX_PH_ADJUSTMENTS