Files
Bubberstation/code/datums/components/reagent_refiller.dm
SkyratBot 020d2ad13e [MIRROR] Code compression for reagent holder. Lowers plumbing reaction chamber tick usage [MDB IGNORE] (#25050)
* Code compression for reagent holder. Lowers plumbing reaction chamber tick usage (#79686)

## About The Pull Request
More code improvements for reagent holder. As you can see it removes a
lot more code than it adds so code savings are significant. This does
not touch on any floating point arithmetic, all that is behind us, this
focuses on removing redundant procs and merging existing procs to
achieve the same functionality so if you do see any changes in reagent
related behaviour it's not intentional and should be reported as a bug
here.

The following code changes can be summarized into points.

**1. Removes procs `get_master_reagent_id()` &
`get_master_reagent_name()`**
Both of these procs have the exact same functionality as
`get_master_reagent()` with the only exception of returning a different
value. Instead we can just call `get_master_reagent()` directly and
infer the name & type of it ourselves rather than creating a wrapper
proc to do it for us, therefore reducing overall code

**2. Removes & Merges `remove_all_type()` proc into `remove_reagent()`**
The proc `remove_all_type()` is highly inefficient, it first uses a for
loop to look for the reagent to remove & then it again calls
`remove_reagent()` on the reagent once it has found it. We can just
embed this functionality directly into `remove_reagent()` by simply
adding an additional parameter `include_subtypes`. This way the
operation is faster, and we reduce the code to get the job done. Also
now `remove_reagent()` will return the total volume of reagents removed
rather that a simple TRUE/FALSE

**3. Removes & Merges `trans_id_to()` proc into `trans_to()`**
Both these procs have the same job of transferring either a single
reagent or all reagents. `trans_id_to()` is a scaled down version of
`trans_to()` because
- It does not have any `method` var. This means if you want to transfer
a single reagent to a mob/organ or any other object it does not have the
functionality to expose the target to that transferred reagent.
- It does not have a `multiplier` var to scale reagent volumes
- It does not have code to deal with organs or stop reactions i.e. it
does not have the `no_react` var.

We can overcome all these short comings by simply adding an extra var
`target_id` to specify what specific reagent to transfer therefore
attaining the same functionality while keeping the benefits of
`trans_to()` proc therefore reducing overall code

**4. Lowers plumbing reaction chamber tick usage for balancing ph.**
Rather than invoking a while loop to balance ph it's much easier for the
player to simply make the reaction chamber wait for e.g. add a reagent
that will never come. This will make the chamber wait therefore giving
the reaction chamber ample time to correctly balance the ph and then
remove that reagent from the list therefore getting correct ph levels.
No need to create code hacks when the player can do it themselves  so
the while loop has been removed

## Changelog
🆑
code: removed redundant procs `get_master_reagent_id()` &
`get_master_reagent_name()`
code: merged `remove_all_type()` proc with `remove_reagent()` now this
proc can perform both functions. `remove_reagent()` now returns the
total volume of reagents removed rather than a simple TRUE/FALSE.
code: merged `trans_id_to()` proc with `trans_to()` now this proc can
perform both functions
refactor: plumbing reaction chamber will now use only a single tick to
balance ph of a solution making it less efficient but more faster. Just
make the reaction chamber wait for longer periods of time to accurately
balance ph
refactor: reagent holder code has been condensed. Report any bugs on
GitHub
/🆑

* Code compression for reagent holder. Lowers plumbing reaction chamber tick usage

* Modular update

* Update alcohol_reagents.dm

---------

Co-authored-by: SyncIt21 <110812394+SyncIt21@users.noreply.github.com>
Co-authored-by: Giz <13398309+vinylspiders@users.noreply.github.com>
2023-11-16 18:27:20 -05:00

75 lines
2.4 KiB
Plaintext

/**
* ## Reagent refiller
* Refills any drinks poured out of the reagent container (and is allowed within the whitelisted reagents).
*/
/datum/component/reagent_refiller
/// Time to refill
var/time_to_refill
/// Callback to consume power
var/datum/callback/power_draw_callback
/// Amount of power to use from the cell
var/power_to_draw
/// Whitelist of reagents allowed to be synthesized
var/list/whitelisted_reagents
/datum/component/reagent_refiller/Initialize(
time_to_refill = 60 SECONDS,
datum/callback/power_draw_callback,
power_to_draw = 30,
whitelisted_reagents = list(/datum/reagent/consumable)
)
if(!is_reagent_container(parent))
return COMPONENT_INCOMPATIBLE
src.time_to_refill = time_to_refill
src.power_draw_callback = power_draw_callback
src.power_to_draw = power_to_draw
src.whitelisted_reagents = whitelisted_reagents
return ..()
/datum/component/reagent_refiller/Destroy(force, silent)
power_draw_callback = null
return ..()
/datum/component/reagent_refiller/RegisterWithParent()
RegisterSignal(parent, COMSIG_ITEM_AFTERATTACK, PROC_REF(refill))
RegisterSignal(parent, COMSIG_ATOM_EXITED, PROC_REF(delete_self))
/datum/component/reagent_refiller/UnregisterFromParent()
UnregisterSignal(parent, list(COMSIG_ITEM_AFTERATTACK, COMSIG_ATOM_EXITED))
/datum/component/reagent_refiller/proc/delete_self()
SIGNAL_HANDLER
qdel(src)
/// Preps the reagent container for being refilled
/datum/component/reagent_refiller/proc/refill()
SIGNAL_HANDLER
. |= COMPONENT_AFTERATTACK_PROCESSED_ITEM
var/obj/item/reagent_containers/container = parent
var/amount = min((container.amount_per_transfer_from_this + container.reagents.total_volume), container.reagents.total_volume)
if (amount == 0)
return
var/datum/reagent/refill = container.reagents.get_master_reagent()
if (!is_path_in_list(refill?.type, whitelisted_reagents))
return
addtimer(CALLBACK(src, PROC_REF(add_reagents), container, container.loc, refill.type, amount), time_to_refill)
/// Refills the reagent container, and uses cell power if applicable
/datum/component/reagent_refiller/proc/add_reagents(obj/item/reagent_containers/target, oldloc, reagent_to_refill, amount)
if (QDELETED(src) || QDELETED(target))
return
if (target.loc != oldloc)
return
target.reagents.add_reagent(reagent_to_refill, amount)
if (!isnull(power_draw_callback))
power_draw_callback.Invoke(power_to_draw)