From 2d92a871aa92182bf20c55229da7cde0df8b573b Mon Sep 17 00:00:00 2001 From: mwerezak Date: Sat, 30 May 2015 17:17:45 -0400 Subject: [PATCH] Optimizes reaction checks --- code/modules/reagents/Chemistry-Holder.dm | 18 ++++++++++++++++++ code/modules/reagents/Chemistry-Recipes.dm | 17 ++++++----------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/code/modules/reagents/Chemistry-Holder.dm b/code/modules/reagents/Chemistry-Holder.dm index 4ad6e7337c..775da2bbb3 100644 --- a/code/modules/reagents/Chemistry-Holder.dm +++ b/code/modules/reagents/Chemistry-Holder.dm @@ -183,6 +183,24 @@ return 0 return 0 +/datum/reagents/proc/has_any_reagent(var/list/check_reagents) + for(var/datum/reagent/current in reagent_list) + if(current.id in check_reagents) + if(current.volume >= check_reagents[current.id]) + return 1 + else + return 0 + return 0 + +/datum/reagents/proc/has_all_reagents(var/list/check_reagents) + //this only works if check_reagents has no duplicate entries... hopefully okay since it expects an associative list + var/missing = check_reagents.len + for(var/datum/reagent/current in reagent_list) + if(current.id in check_reagents) + if(current.volume >= check_reagents[current.id]) + missing-- + return !missing + /datum/reagents/proc/clear_reagents() for(var/datum/reagent/current in reagent_list) del_reagent(current.id) diff --git a/code/modules/reagents/Chemistry-Recipes.dm b/code/modules/reagents/Chemistry-Recipes.dm index 8615b73452..5db9f290e7 100644 --- a/code/modules/reagents/Chemistry-Recipes.dm +++ b/code/modules/reagents/Chemistry-Recipes.dm @@ -54,21 +54,16 @@ /datum/chemical_reaction/proc/can_happen(var/datum/reagents/holder) //check that all the required reagents are present - for(var/reagent in required_reagents) - if(!holder.has_reagent(reagent)) - return 0 + if(!holder.has_all_reagents(required_reagents)) + return 0 //check that all the required catalysts are present in the required amount - for(var/reagent in catalysts) - var/min_req_amt = catalysts[reagent] - if(!holder.has_reagent(reagent, min_req_amt)) - return 0 + if(!holder.has_all_reagents(catalysts)) + return 0 //check that none of the inhibitors are present in the required amount - for(var/reagent in inhibitors) - var/min_req_amt = inhibitors[reagent] - if(holder.has_reagent(reagent, min_req_amt)) - return 0 + if(holder.has_any_reagent(inhibitors)) + return 0 return 1