mirror of
https://github.com/Xevion/RimWorld-Hydroponics-Expanded.git
synced 2025-12-08 04:08:21 -06:00
Prevent growth overflow, add .Between(min, max) extension methods to simplify time/temp checks
This commit is contained in:
@@ -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
|
||||
// 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;
|
||||
growthTrackingPlant.Growth += def.fertility * growthAmount;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@
|
||||
<Compile Include="Enums\HydroponicsStage.cs" />
|
||||
<Compile Include="ModExtension\CapacityExtension.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Utility\BetweenExtension.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
||||
21
Source/HydroponicsExpanded/Utility/BetweenExtension.cs
Normal file
21
Source/HydroponicsExpanded/Utility/BetweenExtension.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace HydroponicsExpanded.Utility {
|
||||
public static class BetweenExtension {
|
||||
public static bool Between(this float value, float min, float max, bool inclusive = false) {
|
||||
if (inclusive)
|
||||
return value >= min && value <= max;
|
||||
return value > min && value < max;
|
||||
}
|
||||
|
||||
public static bool Between(this double value, double min, double max, bool inclusive = false) {
|
||||
if (inclusive)
|
||||
return value >= min && value <= max;
|
||||
return value > min && value < max;
|
||||
}
|
||||
|
||||
public static bool Between(this int value, int min, int max, bool inclusive = false) {
|
||||
if (inclusive)
|
||||
return value >= min && value <= max;
|
||||
return value > min && value < max;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user