Files
Bubberstation/code/datums/components/rot.dm
SyncIt21 0495a19beb Refactor for reagent signals (#88909)
## About The Pull Request
Refactors the way we listen for reagent changes. The changes made can be
listed as points

**1. Removes `COMSIG_REAGENTS_PRE_ADD_REAGENT`**
Used to stop new reagents from being added to the holder, its only
application is with the BRPED to stop inserting reagents into
beakers/cells stored inside it.

Rather than using this signal a cleaner solution is to simply remove the
component part's reagent holders' flags which allow us to insert
reagents into it(i.e. `REFILABLE`, `INJECTIBLE`, `DRAINABLE`) and
restore them back when that part is removed thus achieving the same
results.

Thus `add_reagent()` is now slightly faster because it no longer uses
this signal

**2. Removes every other signal used by the reagent holder**
Removes pretty much every other signal used by `holder.dm` which are
`COMSIG_REAGENTS_[NEW_REAGENT, ADD_REAGENT,
DEL_REAGENT, REM_REAGENT, CLEAR_REAGENTS]`

While yes, it is true that all these signals are unique & serve a
specific purpose the problem is no object in code respects their
uniqueness & instead clumps them up all together & hooks them onto one
proc to listen for "reagent changes". You see this code pattern repeated
in so many places


9277364ef6/code/modules/power/power_store.dm (L105)

Not only does this look ugly but it also has a memory overhead (4 to 5
signal slots all performing the same action which is a lot compared to
the solution i implemented below). Bonus is that "none" of the
parameters passed to this proc are used so they go to waste as well.

So after removing a ton of code we need something that can still make
the code function which brings us to point 3

**3. Adds a new signal `COMSIG_REAGENTS_HOLDER_UPDATED` to rule them
all**

So if all objects in game are listening for "reagent
changes"[adding/removing, reagents] then we need to look at the proc
that is always called during these changes & that is none other than
`update_total()` so we let that send out a signal and cause all objects
to hook onto this 1 signal instead of 4 to 5 signals as explained in
point 2

## Why It's Good For The Game

This section isn't necessary but i want us to better appreciate both the
code & performance benifits of this PR.

1. First of all its waaaay less code and signals to worry about. Just
look at the number of lines of code removed compared to added. Nothing
more to say
2. Overhead of `RegisterSignal` compared to `RegisterSignals` is less
for obvious reasons
3. `remove_all` is significantly faster as it no longer calls
`remove_reagent()`[which in turn calls `update_total()` &
`handle_reactions()` per call & uses a for loop so its a nested for loop
of doom] for every reagent it removes, instead it does the work by
itself & calls the above 2 procs just once
4. Usually when a reagent is deleted it calls
`COMSIG_REAGENTS_REM_REAGENT` & `COMSIG_REAGENTS_DEL_REAGENT`. So if you
have a holder with like 3 reagents upon transferring/deleting them you
get a total of 6 signal calls!!. Now it's just 3(when using `trans_to`)
and just 1 when using `remove_all/clear_reagents`. Need i say more no

## Changelog
🆑
fix: hydrophonics circuit component actually sets output level when
reagents are changed in the tray
refactor: refactors how code listens for reagent changes. Report bugs on
github
/🆑
2025-01-28 03:12:59 +01:00

153 lines
5.6 KiB
Plaintext

#define REAGENT_BLOCKER (1<<0)
#define TEMPERATURE_BLOCKER (1<<1)
#define HUSK_BLOCKER (1<<2)
///Makes a thing rotting, carries with it a start delay and some things that can halt the rot, along with infection logic
/datum/component/rot
///The time we were created, allows for cheese smell
var/start_time = 0
///The delay in ticks between the start of rot and effects kicking in
var/start_delay = 0
///The time in ticks before a rot component reaches its full effectiveness
var/scaling_delay = 0
///How strong is the rot? used for scaling different aspects of the component. Between 0 and 1
var/strength = 0
///Is the component active right now?
var/active = FALSE
///Bitfield of sources preventing the component from rotting
var/blockers = NONE
var/static/list/loc_connections = list(
COMSIG_ATOM_ENTERED = PROC_REF(on_entered),
)
/datum/component/rot/Initialize(delay, scaling, severity)
if(!isatom(parent))
return COMPONENT_INCOMPATIBLE
if(isliving(parent))
var/mob/living/living_parent = parent
//I think this can break in cases where someone becomes a robot post death, but I uh, I don't know how to account for that
if(!(living_parent.mob_biotypes & (MOB_ORGANIC|MOB_UNDEAD)))
return COMPONENT_INCOMPATIBLE
start_delay = delay
scaling_delay = scaling
strength = severity
RegisterSignals(parent, list(COMSIG_ATOM_HULK_ATTACK, COMSIG_ATOM_ATTACK_ANIMAL, COMSIG_ATOM_ATTACK_HAND), PROC_REF(rot_react_touch))
RegisterSignal(parent, COMSIG_ATOM_ATTACKBY, PROC_REF(rot_hit_react))
if(ismovable(parent))
AddComponent(/datum/component/connect_loc_behalf, parent, loc_connections)
RegisterSignal(parent, COMSIG_MOVABLE_BUMP, PROC_REF(rot_react))
if(isliving(parent))
RegisterSignal(parent, COMSIG_LIVING_REVIVE, PROC_REF(react_to_revive)) //mobs stop this when they come to life
RegisterSignal(parent, COMSIG_LIVING_GET_PULLED, PROC_REF(rot_react_touch))
if(iscarbon(parent))
var/mob/living/carbon/carbon_parent = parent
RegisterSignal(carbon_parent.reagents, COMSIG_REAGENTS_HOLDER_UPDATED, PROC_REF(check_reagent))
RegisterSignals(parent, list(SIGNAL_ADDTRAIT(TRAIT_HUSK), SIGNAL_REMOVETRAIT(TRAIT_HUSK)), PROC_REF(check_husk_trait))
check_reagent(carbon_parent.reagents, null)
check_husk_trait(null)
if(ishuman(parent))
var/mob/living/carbon/human/human_parent = parent
RegisterSignal(parent, COMSIG_HUMAN_CORETEMP_CHANGE, PROC_REF(check_for_temperature))
check_for_temperature(null, 0, human_parent.coretemperature)
start_up(NONE) //If nothing's blocking it, start
/datum/component/rot/UnregisterFromParent()
. = ..()
if(ismovable(parent))
qdel(GetComponent(/datum/component/connect_loc_behalf))
///One of two procs that modifies blockers, this one handles removing a blocker and potentially restarting the rot
/datum/component/rot/proc/start_up(blocker_type)
blockers &= ~blocker_type //Yeet the type
if(blockers || active) //If it's not empty
return
start_time = world.time
active = TRUE
///One of two procs that modifies blockers, this one handles adding a blocker and potentially ending the rot
/datum/component/rot/proc/rest(blocker_type)
var/old_blockers = blockers
blockers |= blocker_type
if(old_blockers || !active) //If it had anything before this
return
start_delay = max((start_time + start_delay) - world.time, 0) //Account for the time spent rotting
active = FALSE
/datum/component/rot/proc/react_to_revive()
SIGNAL_HANDLER
qdel(src)
/datum/component/rot/proc/check_reagent(datum/reagents/source)
SIGNAL_HANDLER
if(source.has_reagent(/datum/reagent/toxin/formaldehyde, 15) || source.has_reagent(/datum/reagent/cryostylane))
rest(REAGENT_BLOCKER)
return
start_up(REAGENT_BLOCKER)
/datum/component/rot/proc/check_for_temperature(datum/source, old_temp, new_temp)
SIGNAL_HANDLER
if(new_temp <= T0C-10)
rest(TEMPERATURE_BLOCKER)
return
start_up(TEMPERATURE_BLOCKER)
/datum/component/rot/proc/check_husk_trait()
SIGNAL_HANDLER
if(HAS_TRAIT(parent, TRAIT_HUSK))
rest(HUSK_BLOCKER)
return
start_up(HUSK_BLOCKER)
/datum/component/rot/proc/rot_hit_react(datum/source, obj/item/hit_with, mob/living/attacker, params)
SIGNAL_HANDLER
rot_react_touch(source, attacker)
/datum/component/rot/proc/rot_react_touch(datum/source, mob/living/react_to)
SIGNAL_HANDLER
rot_react(source, react_to, pick(GLOB.arm_zones))
/// Triggered when something enters the component's parent.
/datum/component/rot/proc/on_entered(datum/source, atom/movable/arrived, atom/old_loc, list/atom/old_locs)
SIGNAL_HANDLER
rot_react(source, arrived)
///The main bit of logic for the rot component, does a temperature check and has a chance to infect react_to
/datum/component/rot/proc/rot_react(source, mob/living/react_to, target_zone = null)
SIGNAL_HANDLER
if(!isliving(react_to))
return
// Don't infect if you're chilled (I'd like to link this with the signals, but I can't come up with a good way to pull it off)
var/atom/atom_parent = parent
var/datum/gas_mixture/our_mix = atom_parent.return_air()
if(our_mix?.temperature <= T0C-10)
return
if(!active)
return
var/time_delta = world.time - start_time
// Wait a bit before decaying
if(time_delta < start_delay)
return
var/time_scaling = min((time_delta - start_delay) / scaling_delay, 1)
if(!prob(strength * 1 * time_scaling))
return
//We're running just under the "worst disease", since we don't want these to be too strong
var/datum/disease/advance/random/rand_disease = new(rand(4 * strength * time_scaling), rand(strength * 5 * time_scaling))
rand_disease.name = "Unknown"
react_to.ContactContractDisease(rand_disease, target_zone)
#undef REAGENT_BLOCKER
#undef TEMPERATURE_BLOCKER
#undef HUSK_BLOCKER