From 96267e251cc86ba05cb04631e89b867c15d338a4 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 16 Jul 2023 01:36:46 -0500 Subject: [PATCH] Prevent growth overflow, add .Between(min, max) extension methods to simplify time/temp checks --- .../Building_DenseHydroponicsBasin.cs | 25 ++++++++++++------- .../HydroponicsExpanded.csproj | 1 + .../Utility/BetweenExtension.cs | 21 ++++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 Source/HydroponicsExpanded/Utility/BetweenExtension.cs diff --git a/Source/HydroponicsExpanded/Buildings/Building_DenseHydroponicsBasin.cs b/Source/HydroponicsExpanded/Buildings/Building_DenseHydroponicsBasin.cs index 5dab00a..e090601 100644 --- a/Source/HydroponicsExpanded/Buildings/Building_DenseHydroponicsBasin.cs +++ b/Source/HydroponicsExpanded/Buildings/Building_DenseHydroponicsBasin.cs @@ -88,20 +88,27 @@ namespace HydroponicsExpanded { // Despite the name, this is just a power check. We don't want to grow the plants if there is no power. if (!base.CanAcceptSowNow()) return; - // Temperature & time of day check. - float temperature = Position.GetTemperature(Map); - if (!(temperature > 10f) || !(temperature < 42f) || - !(GenLocalDate.DayPercent(this) > 0.25f) || !(GenLocalDate.DayPercent(this) < 0.8f)) return; // The first plant in the container is the 'tracked' plant. - var growthTrackingPlant = _innerContainer[0] as Plant; + // This set/type check is for preventing NPE warnings. The previous .Count check implicitly makes this impossible, but the compiler doesn't know that. + if (!(_innerContainer[0] is Plant growthTrackingPlant)) { + Log.Message( + $"Unexpected thing in BuildingDenseHydroponicsBasin.innerContainer ({_innerContainer.GetType()})"); + return; + } - // ReSharper disable once PossibleNullReferenceException - float growthAmount = 1f / (60_000f * growthTrackingPlant.def.plant.growDays) * 250f; - growthTrackingPlant.Growth += def.fertility * growthAmount; - _highestGrowth = growthTrackingPlant.Growth; + // Temperature & time of day check. + float temperature = Position.GetTemperature(Map); + if (temperature.Between(10f, 42f) && GenLocalDate.DayPercent(this).Between(0.25f, 0.8f)) { + float growthAmount = 1f / (60_000f * growthTrackingPlant.def.plant.growDays) * 250f; + + // Debug gizmo can set growth to 100%, thus Math.min check here. + growthTrackingPlant.Growth = Math.Min(1f, def.fertility * growthAmount); + _highestGrowth = growthTrackingPlant.Growth; + } // When growth is complete, move to the harvesting stage. + // This is ran even if growing is not available as debug gizmo can push growth to 'mature' stage outside normal growing times. if (growthTrackingPlant.LifeStage == PlantLifeStage.Mature) Stage = HydroponicsStage.Harvest; } diff --git a/Source/HydroponicsExpanded/HydroponicsExpanded.csproj b/Source/HydroponicsExpanded/HydroponicsExpanded.csproj index dd263f3..8acc2fd 100644 --- a/Source/HydroponicsExpanded/HydroponicsExpanded.csproj +++ b/Source/HydroponicsExpanded/HydroponicsExpanded.csproj @@ -51,6 +51,7 @@ +