Split growth stages into separate methods, optimize and recover variable names/meaning

This commit is contained in:
2023-07-15 09:36:55 -05:00
parent 1b7d69a05c
commit ea4c52331c
@@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using HydroponicsExpanded.Enums; using HydroponicsExpanded.Enums;
using HydroponicsExpanded.ModExtension; using HydroponicsExpanded.ModExtension;
@@ -23,84 +24,115 @@ namespace HydroponicsExpanded {
get { return this.OccupiedRect().Cells; } get { return this.OccupiedRect().Cells; }
} }
public override void TickRare() { private void SowTick() {
int growingPlants = 0; bool capacityReached = _innerContainer.Count >= _capacity;
foreach (Plant plant in PlantsOnMe) {
if (plant.LifeStage != PlantLifeStage.Growing)
continue;
growingPlants++; // TODO: Why is this here?
if (_innerContainer.Count >= _capacity) foreach (Plant plant in PlantsOnMe) {
// Blighted plants will be destroyed and not added to the internal container.
// Once capacity is reached, all plants will be ignored.
if (capacityReached || plant.Blighted) {
plant.Destroy(); plant.Destroy();
continue;
} }
if (growingPlants >= 2) { // Otherwise, we move the plant underground.
foreach (Plant plant in PlantsOnMe) {
if (plant.LifeStage != PlantLifeStage.Growing || plant.Blighted) continue;
plant.DeSpawn(); plant.DeSpawn();
TryAcceptThing(plant); TryAcceptThing(plant);
if (_innerContainer.Count < _capacity) continue; // Recalculate capacity.
capacityReached = _innerContainer.Count >= _capacity;
}
// All plants were planted in hydroponics - switch to grow stage. // If the maximum capacity is reached, then we should move to the growing stage.
if (capacityReached) {
SoundStarter.PlayOneShot(SoundDefOf.CryptosleepCasket_Accept, SoundStarter.PlayOneShot(SoundDefOf.CryptosleepCasket_Accept,
new TargetInfo(Position, Map)); new TargetInfo(Position, Map));
_stage = HydroponicsStage.Grow; _stage = HydroponicsStage.Grow;
break;
} }
} }
private void GrowTick() {
// If there are no plants stored, reset the growth % and move back to the sowing stage.
if (_innerContainer.Count == 0) { if (_innerContainer.Count == 0) {
_stage = HydroponicsStage.Sowing; _stage = HydroponicsStage.Sowing;
_highestGrowth = 0f; _highestGrowth = 0f;
} }
if (CanAcceptSowNow()) { // 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); float temperature = Position.GetTemperature(Map);
if (_stage == HydroponicsStage.Grow && temperature > 10f && temperature < 42f && if (!(temperature > 10f) || !(temperature < 42f) ||
GenLocalDate.DayPercent(this) > 0.25f && GenLocalDate.DayPercent(this) < 0.8f) { !(GenLocalDate.DayPercent(this) > 0.25f) || !(GenLocalDate.DayPercent(this) < 0.8f)) return;
var plant = _innerContainer[0] as Plant;
float num2 = 1f / (60000f * plant.def.plant.growDays) * 250f; // The first plant in the container is the 'tracked' plant.
plant.Growth += _fertility * num2; var growthTrackingPlant = _innerContainer[0] as Plant;
_highestGrowth = plant.Growth;
if (plant.LifeStage == PlantLifeStage.Mature) { float growthAmount = 1f / (60000f * growthTrackingPlant.def.plant.growDays) * 250f;
growthTrackingPlant.Growth += _fertility * growthAmount;
_highestGrowth = growthTrackingPlant.Growth;
// When growth is complete, move to the harvesting stage.
if (growthTrackingPlant.LifeStage == PlantLifeStage.Mature)
_stage = HydroponicsStage.Harvest; _stage = HydroponicsStage.Harvest;
} }
}
if (_stage != HydroponicsStage.Harvest) private void HarvestTick() {
return; // Try to place every plant in the container in any cell.
foreach (Thing nextInnerThing in _innerContainer) {
var nextPlant = (Plant)nextInnerThing;
{ int occupiedCells = 0;
var thing = default(Thing);
foreach (Thing thing1 in _innerContainer) {
var item3 = (Plant)thing1;
// TOOD: What is this counter variable for?
int c = 0;
foreach (IntVec3 current in this.OccupiedRect()) { foreach (IntVec3 current in this.OccupiedRect()) {
List<Thing> list = Map.thingGrid.ThingsListAt(current); List<Thing> list = Map.thingGrid.ThingsListAt(current);
bool flag = list.OfType<Plant>().Any();
if (!flag) { // Skip this cell if it's occupied by another plant.
item3.Growth = 1f; if (list.OfType<Plant>().Any()) {
_innerContainer.TryDrop(item3, current, Map, ThingPlaceMode.Direct, out thing); occupiedCells++;
continue;
}
nextPlant.Growth = 1f;
_innerContainer.TryDrop(nextPlant, current, Map, ThingPlaceMode.Direct, out _);
break; break;
} }
c++; if (occupiedCells >= 4)
}
if (c >= 4) {
break; break;
} }
} }
return; private void TickStage(HydroponicsStage stage) {
switch (stage) {
case HydroponicsStage.Sowing:
SowTick();
break;
case HydroponicsStage.Grow:
GrowTick();
break;
case HydroponicsStage.Harvest:
HarvestTick();
break;
default:
throw new ArgumentOutOfRangeException(
"Unable to select stage tick for BuildingDenseHydroponicsBasin.TickRare.");
} }
} }
// TODO: Why is 1 rotting damage applied to all plants in the container? public override void TickRare() {
// Tick the current stage.
HydroponicsStage initialStage = _stage;
TickStage(_stage);
// If the stage changed, re-run the next tick. This can allow for instant Grow -> Harvest transition.
if (_stage != initialStage)
TickStage(_stage);
// Apply rotting damage to all plants while power is cut.
if (!base.CanAcceptSowNow())
foreach (Thing thing in _innerContainer) foreach (Thing thing in _innerContainer)
((Plant)thing).TakeDamage(new DamageInfo(DamageDefOf.Rotting, 1f)); ((Plant)thing).TakeDamage(new DamageInfo(DamageDefOf.Rotting, 1f));
} }
@@ -142,19 +174,23 @@ namespace HydroponicsExpanded {
} }
protected virtual bool TryAcceptThing(Thing thing) { protected virtual bool TryAcceptThing(Thing thing) {
if (!Accepts(thing)) // Make sure that the plant container can accept the plant Thing.
if (!_innerContainer.CanAcceptAnyOf(thing))
return false; return false;
// If the thing does not have an owner, just add it. Perhaps it was created in memory?
if (thing.holdingOwner == null) return _innerContainer.TryAdd(thing); if (thing.holdingOwner == null) return _innerContainer.TryAdd(thing);
// Thing has an owner, so use TryTransferToContainer.
thing.holdingOwner.TryTransferToContainer(thing, _innerContainer, thing.stackCount); thing.holdingOwner.TryTransferToContainer(thing, _innerContainer, thing.stackCount);
return true; return true;
} }
protected virtual bool Accepts(Thing thing) {
return _innerContainer.CanAcceptAnyOf(thing);
}
/**
* Only allows sowing in the 'sowing' stage. Otherwise, the hydroponics bay is 'sealed' while
* all the plants are growing.
*/
public new bool CanAcceptSowNow() { public new bool CanAcceptSowNow() {
return base.CanAcceptSowNow() && _stage == HydroponicsStage.Sowing; return base.CanAcceptSowNow() && _stage == HydroponicsStage.Sowing;
} }