CatStation/Content.Shared/Botany/Systems/PlantGrowthSystem.cs
Pok 6f6a5694c8
Botany refactor (#44576)
* Restarting files from scratch

* reduce file count, more progress

* Additional partial work

* update 1

* update 2

* TODO execution

* fix

* Update PlantGrowthComponent.cs

* fix 1

* growth correction

* PlantGrowthComponent

* update 99

* last update

* oops

* Update vegan-meatball.yml

* Data loss

* ToxinsComponent

* Default components

* minor fix

* PlantTraitsComponent

* fix error

* fix error 2

* Copy growth components

* last update within this PR

* AutoHarvestGrowth remote

* Viable

* fix

* Fix growth rate

* Improvements to the YAML

* merge update

* fix

* code cleanup

* TODO

* fix

* oops

* oops 2

* more TODO

* fix names

* fix names oops

* Harvest bug fixes

* fix tests

* refactor: cleanup after mering master

* Update ConsumeExudeGasGrowthSystem.cs

* refactor: make this piece of happiness and ponies work

* review

* oops

* fix <summary>

* Separation of plant components into PlantComponent and PlantTraitsComponent and cleaning

* EnsureUniqueSeed

* refactor: change Resolve to TryComp where appropriate

* refactor: review comments - try get for components, clenaups

* refactor: minor yaml cleanups

* fixing trifles

* botany-rework-part-3

* API and mutations

* minor fixes

* Update WeedPestGrowthSystem.cs

* Update plants.yml

* PlantChemicals

* YAMLLinter

* review and some lost code

* YAMLLinter part 2

* plants.yml

* Aaaa tests

* seedId

* clone plant component

* Update clone.yml

* refactor: remove suppress possible null reference exception

* refactor: changed HasPlantAlive to TryGetAlivePlant, minor cleanup

* Separation of tray and plant

* oops

* PlantingPlant

* a little more cleaning

* oops 2

* botany-rework-part-4

* predict and API

* Field OldAgeDamage

* oops

* YMAL

* YMAL 2

* YMAAAAAL

* Update PlantToxinsSystem.cs

* review

* Update randomMutations.yml

* minor fix design

* Updates the plants visuals not every frame

* bug fixes

* some fixes

* bugfix and renames

* Dirty

* Replace ComponentRegistry

* unnecessary API

* fix merge

* build

* build 2

* Review by AJCM

* del PlantDataComponent and fix harvest

* fix all

* final review

* Update PlantTrayVisualizerSystem.cs

* fix merge

* Update PlantTraitsComponent.cs

* Traits LocId fix

* Update SpawnEntitiesBehavior.cs

* aaaaaaaaa

* fix all part 2

* PlantDataComponent revert

* aoao

* fix all part 3

* Update PlantTraitsComponent.cs

* Update clone.yml

* Reviews

---------

Co-authored-by: PraxisMapper <praxismapper@gmail.com>
Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
Co-authored-by: Princess Cheeseballs <66055347+Princess-Cheeseballs@users.noreply.github.com>
Co-authored-by: AJCM-git <ajcm_mail@disroot.org>
2026-08-08 21:58:01 +00:00

113 lines
4.4 KiB
C#

using JetBrains.Annotations;
using Content.Shared.Botany.Components;
using Content.Shared.Botany.Events;
using Content.Shared.Random.Helpers;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Shared.Botany.Systems;
/// <summary>
/// Handles baseline plant progression each growth tick: aging, resource consumption,
/// simple viability checks.
/// </summary>
public sealed partial class PlantGrowthSystem : EntitySystem
{
[Dependency] private BotanySystem _botany = default!;
[Dependency] private IGameTiming _timing = default!;
[Dependency] private PlantMutationSystem _mutation = default!;
[Dependency] private PlantHarvestSystem _plantHarvest = default!;
[Dependency] private PlantHolderSystem _plantHolder = default!;
[Dependency] private PlantTraySystem _plantTray = default!;
[Dependency] private EntityQuery<PlantHolderComponent> _holderQuery = default!;
[Dependency] private EntityQuery<PlantTrayComponent> _trayQuery = default!;
[SubscribeLocalEvent]
private void OnCrossPollinate(Entity<PlantGrowthComponent> ent, ref PlantCrossPollinateEvent args)
{
if (!_botany.TryGetPlantComponent<PlantGrowthComponent>(args.PollenData, args.PollenProtoId, out var pollenData))
return;
_mutation.CrossFloat(ent, ref ent.Comp.WaterConsumption, pollenData.WaterConsumption);
_mutation.CrossFloat(ent, ref ent.Comp.NutrientConsumption, pollenData.NutrientConsumption);
Dirty(ent);
}
[SubscribeLocalEvent]
private void OnPlantGrow(Entity<PlantGrowthComponent> ent, ref PlantGrowEvent args)
{
var (plantUid, plantComp) = ent;
var trayUid = GetEntity(args.Tray);
if (!_trayQuery.TryComp(trayUid, out var trayComp))
return;
if (!_holderQuery.TryComp(plantUid, out var holder))
return;
var random = SharedRandomExtensions.PredictedRandom(_timing, GetNetEntity(plantUid));
// TODO: There are too many magic numbers that don't really make sense to add to the component. Balance needs to be reworked
// Advance plant age here.
if (holder.SkipAging > 0)
_plantHolder.AdjustsSkipAging((plantUid, holder), -1);
else if (random.Prob(0.8f))
_plantHolder.AdjustsAge((plantUid, holder), 1);
if (plantComp.WaterConsumption > 0 && trayComp.WaterLevel > 0 && random.Prob(0.75f))
_plantTray.AdjustWater((trayUid, trayComp), -MathF.Max(0f, plantComp.WaterConsumption * trayComp.TrayConsumptionMultiplier));
if (plantComp.NutrientConsumption > 0 && trayComp.NutritionLevel > 0 && random.Prob(0.75f))
_plantTray.AdjustNutrient((trayUid, trayComp), -MathF.Max(0f, plantComp.NutrientConsumption * trayComp.TrayConsumptionMultiplier));
var healthMod = random.Next(1, 3);
if (holder.SkipAging < 10)
{
// Make sure the plant is not thirsty.
if (trayComp.WaterLevel > 10)
_plantHolder.AdjustsHealth((plantUid, holder), (random.Prob(0.35f) ? 1 : 0) * healthMod);
else
{
_plantHarvest.AffectGrowth(plantUid, -1);
_plantHolder.AdjustsHealth((plantUid, holder), -healthMod);
}
if (trayComp.NutritionLevel > 5)
{
_plantHolder.AdjustsHealth((plantUid, holder), (random.Prob(0.35f) ? 1 : 0) * healthMod);
}
else
{
_plantHarvest.AffectGrowth(plantUid, -1);
_plantHolder.AdjustsHealth((plantUid, holder), -healthMod);
}
}
}
/// <summary>
/// Adjusts the water consumption of a plant.
/// </summary>
[PublicAPI]
public void AdjustWaterConsumption(Entity<PlantGrowthComponent?> ent, float amount)
{
if (!Resolve(ent.Owner, ref ent.Comp, false))
return;
ent.Comp.WaterConsumption = MathF.Max(0f, ent.Comp.WaterConsumption + amount);
DirtyField(ent, nameof(ent.Comp.WaterConsumption));
}
/// <summary>
/// Adjusts the nutrient consumption of a plant.
/// </summary>
[PublicAPI]
public void AdjustNutrientConsumption(Entity<PlantGrowthComponent?> ent, float amount)
{
if (!Resolve(ent.Owner, ref ent.Comp, false))
return;
ent.Comp.NutrientConsumption = MathF.Max(0f, ent.Comp.NutrientConsumption + amount);
DirtyField(ent, nameof(ent.Comp.NutrientConsumption));
}
}