mirror of
https://github.com/Xevion/RimWorld-Hydroponics-Expanded.git
synced 2026-01-31 04:25:36 -06:00
Split growth stages into separate methods, optimize and recover variable names/meaning
This commit is contained in:
@@ -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,86 +24,117 @@ 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;
|
||||||
|
|
||||||
|
// TODO: Why is this here?
|
||||||
foreach (Plant plant in PlantsOnMe) {
|
foreach (Plant plant in PlantsOnMe) {
|
||||||
if (plant.LifeStage != PlantLifeStage.Growing)
|
// Blighted plants will be destroyed and not added to the internal container.
|
||||||
continue;
|
// Once capacity is reached, all plants will be ignored.
|
||||||
|
if (capacityReached || plant.Blighted) {
|
||||||
growingPlants++;
|
|
||||||
if (_innerContainer.Count >= _capacity)
|
|
||||||
plant.Destroy();
|
plant.Destroy();
|
||||||
}
|
continue;
|
||||||
|
|
||||||
if (growingPlants >= 2) {
|
|
||||||
foreach (Plant plant in PlantsOnMe) {
|
|
||||||
if (plant.LifeStage != PlantLifeStage.Growing || plant.Blighted) continue;
|
|
||||||
plant.DeSpawn();
|
|
||||||
TryAcceptThing(plant);
|
|
||||||
|
|
||||||
if (_innerContainer.Count < _capacity) continue;
|
|
||||||
|
|
||||||
// All plants were planted in hydroponics - switch to grow stage.
|
|
||||||
SoundStarter.PlayOneShot(SoundDefOf.CryptosleepCasket_Accept,
|
|
||||||
new TargetInfo(Position, Map));
|
|
||||||
_stage = HydroponicsStage.Grow;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise, we move the plant underground.
|
||||||
|
plant.DeSpawn();
|
||||||
|
TryAcceptThing(plant);
|
||||||
|
|
||||||
|
// Recalculate capacity.
|
||||||
|
capacityReached = _innerContainer.Count >= _capacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the maximum capacity is reached, then we should move to the growing stage.
|
||||||
|
if (capacityReached) {
|
||||||
|
SoundStarter.PlayOneShot(SoundDefOf.CryptosleepCasket_Accept,
|
||||||
|
new TargetInfo(Position, Map));
|
||||||
|
_stage = HydroponicsStage.Grow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
float temperature = Position.GetTemperature(Map);
|
if (!base.CanAcceptSowNow()) return;
|
||||||
if (_stage == HydroponicsStage.Grow && temperature > 10f && temperature < 42f &&
|
|
||||||
GenLocalDate.DayPercent(this) > 0.25f && GenLocalDate.DayPercent(this) < 0.8f) {
|
|
||||||
var plant = _innerContainer[0] as Plant;
|
|
||||||
float num2 = 1f / (60000f * plant.def.plant.growDays) * 250f;
|
|
||||||
plant.Growth += _fertility * num2;
|
|
||||||
_highestGrowth = plant.Growth;
|
|
||||||
if (plant.LifeStage == PlantLifeStage.Mature) {
|
|
||||||
_stage = HydroponicsStage.Harvest;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_stage != HydroponicsStage.Harvest)
|
// Temperature & time of day check.
|
||||||
return;
|
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 thing = default(Thing);
|
var growthTrackingPlant = _innerContainer[0] as Plant;
|
||||||
foreach (Thing thing1 in _innerContainer) {
|
|
||||||
var item3 = (Plant)thing1;
|
|
||||||
|
|
||||||
// TOOD: What is this counter variable for?
|
float growthAmount = 1f / (60000f * growthTrackingPlant.def.plant.growDays) * 250f;
|
||||||
int c = 0;
|
growthTrackingPlant.Growth += _fertility * growthAmount;
|
||||||
foreach (IntVec3 current in this.OccupiedRect()) {
|
_highestGrowth = growthTrackingPlant.Growth;
|
||||||
List<Thing> list = Map.thingGrid.ThingsListAt(current);
|
|
||||||
bool flag = list.OfType<Plant>().Any();
|
|
||||||
|
|
||||||
if (!flag) {
|
// When growth is complete, move to the harvesting stage.
|
||||||
item3.Growth = 1f;
|
if (growthTrackingPlant.LifeStage == PlantLifeStage.Mature)
|
||||||
_innerContainer.TryDrop(item3, current, Map, ThingPlaceMode.Direct, out thing);
|
_stage = HydroponicsStage.Harvest;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
c++;
|
private void HarvestTick() {
|
||||||
}
|
// Try to place every plant in the container in any cell.
|
||||||
|
foreach (Thing nextInnerThing in _innerContainer) {
|
||||||
|
var nextPlant = (Plant)nextInnerThing;
|
||||||
|
|
||||||
if (c >= 4) {
|
int occupiedCells = 0;
|
||||||
break;
|
|
||||||
}
|
foreach (IntVec3 current in this.OccupiedRect()) {
|
||||||
|
List<Thing> list = Map.thingGrid.ThingsListAt(current);
|
||||||
|
|
||||||
|
// Skip this cell if it's occupied by another plant.
|
||||||
|
if (list.OfType<Plant>().Any()) {
|
||||||
|
occupiedCells++;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
nextPlant.Growth = 1f;
|
||||||
|
_innerContainer.TryDrop(nextPlant, current, Map, ThingPlaceMode.Direct, out _);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (occupiedCells >= 4)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: Why is 1 rotting damage applied to all plants in the container?
|
private void TickStage(HydroponicsStage stage) {
|
||||||
foreach (Thing thing in _innerContainer)
|
switch (stage) {
|
||||||
((Plant)thing).TakeDamage(new DamageInfo(DamageDefOf.Rotting, 1f));
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
((Plant)thing).TakeDamage(new DamageInfo(DamageDefOf.Rotting, 1f));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Draw() {
|
public override void Draw() {
|
||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user