CatStation/Content.Shared/Botany/Systems/PlantToxinsSystem.cs
ahandleman c46f74c355
Various small botany bugfixes and reverts to prior behavior (#45842)
* Various small botany bugfixes and reverts to prior behavior

* oops rainbow cannabis needs it too

* oops, need to keep remove around for stuff like phal and plant-b-gone

* fix comment

* Fixed to make it selectable

* No need for plantPhalaxEntityEffect

* Ok moving removing this to its own PR because actually the guidebook stuff is weird and I wanna get this out the door.

* endless fixes to the botany

* renaming methods

* Refactored to remove PlantPhalanximineEntityEffectSystem because it's just a trait.

* removed the actual file

* renamed to noun/description for clarity

---------

Co-authored-by: Pok245 <113675512+Pok27@users.noreply.github.com>
2026-09-06 17:46:38 +00:00

70 lines
2.6 KiB
C#

using JetBrains.Annotations;
using Content.Shared.Botany.Components;
using Content.Shared.Botany.Events;
namespace Content.Shared.Botany.Systems;
/// <summary>
/// Handles toxin accumulation and tolerance for plants, applying health damage
/// and decrementing toxins based on per-tick uptake.
/// </summary>
public sealed partial class PlantToxinsSystem : EntitySystem
{
[Dependency] private BotanySystem _botany = default!;
[Dependency] private PlantMutationSystem _mutation = default!;
[Dependency] private PlantHolderSystem _plantHolder = default!;
[Dependency] private PlantTraySystem _plantTray = default!;
[Dependency] private EntityQuery<PlantTrayComponent> _trayQuery = default!;
[Dependency] private EntityQuery<PlantHolderComponent> _holderQuery = default!;
[SubscribeLocalEvent]
private void OnCrossPollinate(Entity<PlantToxinsComponent> ent, ref PlantCrossPollinateEvent args)
{
if (!_botany.TryGetPlantComponent<PlantToxinsComponent>(args.PollenData, args.PollenProtoId, out var pollenData))
return;
_mutation.CrossFloat(ref ent.Comp.ToxinsTolerance, pollenData.ToxinsTolerance);
_mutation.CrossFloat(ref ent.Comp.ToxinUptakeDivisor, pollenData.ToxinUptakeDivisor);
Dirty(ent);
}
[SubscribeLocalEvent]
private void OnPlantGrow(Entity<PlantToxinsComponent> ent, ref PlantGrowEvent args)
{
var trayUid = args.Tray;
if (!_trayQuery.TryComp(trayUid, out var tray)
|| !_holderQuery.TryComp(ent.Owner, out var holder))
return;
if (ent.Comp.ToxinUptakeDivisor <= 0)
return;
var toxinUptake = MathF.Max(1, MathF.Round(tray.ToxinLevel / ent.Comp.ToxinUptakeDivisor));
if (tray.ToxinLevel > ent.Comp.ToxinsTolerance)
{
// Get minimum value between health left and toxin uptake.
var actualUptake = Math.Min(toxinUptake, holder.Health);
_plantHolder.AdjustsHealth(ent.Owner, -actualUptake);
_plantTray.AdjustToxin((trayUid, tray), -actualUptake);
}
else
{
_plantTray.AdjustToxin((trayUid, tray), -toxinUptake);
}
}
/// <summary>
/// Adjusts maximum toxin level the plant can tolerate before taking damage.
/// </summary>
[PublicAPI]
public void AdjustToxinsTolerance(Entity<PlantToxinsComponent?> ent, float amount)
{
if (!Resolve(ent.Owner, ref ent.Comp, false))
return;
ent.Comp.ToxinsTolerance = MathF.Max(0f, ent.Comp.ToxinsTolerance + amount);
DirtyField(ent, nameof(ent.Comp.ToxinsTolerance));
}
}