Skip to content

Commit

Permalink
algorithm fix
Browse files Browse the repository at this point in the history
  • Loading branch information
silicons committed Oct 26, 2024
1 parent 45b03ef commit 4887d57
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 6 additions & 0 deletions code/modules/reagents/chemistry/chemical_reaction.dm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
/// required reagents as ratios. path or id is supported, prefer paths for compile time checking.
/// all of these will then make 1 unit of the reagent.
var/list/required_reagents = list()
/// cached: total volume of all required reagents for a single unit-multiplier (every 1 multiplier basically) reacted
var/tmp/required_reagents_unit_volume
/// require whole numbers of all reagents taken
///
/// * has no effect on ticked (non-instant) reactions
Expand Down Expand Up @@ -194,6 +196,10 @@
if(isnull(display_description) && !isnull(resolved))
display_description = resolved.display_description

required_reagents_unit_volume = 0
for(var/id in required_reagents)
required_reagents_unit_volume += required_reagents[id]

//obtains any special data that will be provided to the reaction products
//this is called just before reactants are removed.
// todo: rework data system
Expand Down
16 changes: 10 additions & 6 deletions code/modules/reagents/chemistry/reagent_holder-reactions.dm
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@
maximum_multiplier = min(maximum_multiplier, amount / reaction.required_reagents[id])
total_reactant_volume += amount

// results
// results; take into account ingredients consumed if we were to run to completion
if(reaction.result_amount > 0)
maximum_multiplier = min(maximum_multiplier, ((maximum_volume - total_volume) + total_reactant_volume) / reaction.result_amount)
maximum_multiplier = min(maximum_multiplier, ((maximum_volume - total_volume) + maximum_multiplier * required_reagents_unit_volume) / reaction.result_amount)

// equilibrium (this relies on ingredients and must be #2 after ingredients which is #1)
if(reaction.equilibrium != INFINITY)
Expand Down Expand Up @@ -275,15 +275,19 @@
var/maximum_multiplier = INFINITY

if(reaction.require_whole_numbers)
if(reaction.result && reaction.result_amount > 0)
maximum_multiplier = min(maximum_multiplier, floor((maximum_volume - total_volume) / reaction.result_amount))
// tally up required reagents
for(var/id in reaction.required_reagents)
maximum_multiplier = min(maximum_multiplier, floor(legacy_direct_access_reagent_amount(id) / reaction.required_reagents[id]))
else
// tally up remaining space, taking into account the reagents we would consume if we reacted to completion
if(reaction.result && reaction.result_amount > 0)
maximum_multiplier = min(maximum_multiplier, (maximum_volume - total_volume) / reaction.result_amount)
maximum_multiplier = min(maximum_multiplier, floor(((maximum_volume - total_volume) + (maximum_multiplier * required_reagents_unit_volume)) / reaction.result_amount))
else
// tally up required reagents
for(var/id in reaction.required_reagents)
maximum_multiplier = min(maximum_multiplier, legacy_direct_access_reagent_amount(id) / reaction.required_reagents[id])
// tally up remaining space, taking into account the reagents we would consume if we reacted to completion
if(reaction.result && reaction.result_amount > 0)
maximum_multiplier = min(maximum_multiplier, ((maximum_volume - total_volume) + (maximum_multiplier * required_reagents_unit_volume)) / reaction.result_amount)

if(maximum_multiplier <= 0)
continue
Expand Down

0 comments on commit 4887d57

Please sign in to comment.