This commit is contained in:
2025-09-17 14:53:26 +08:00
parent ee9ada0edb
commit 0c118ca8a6
5 changed files with 99 additions and 49 deletions

View File

@@ -6,7 +6,8 @@ namespace ArachnaeSwarm
{
public class CompProperties_GrowthVatBooster : CompProperties_Facility
{
public StatModifier statOffset; // 从XML读取
// We will use the base class's statOffsets field.
// public List<StatModifier> statOffsets;
public CompProperties_GrowthVatBooster()
{
@@ -16,30 +17,31 @@ namespace ArachnaeSwarm
public class CompFacility_GrowthVatBooster : CompFacility
{
private new CompProperties_GrowthVatBooster Props => (CompProperties_GrowthVatBooster)props;
private List<StatModifier> cachedStatOffsets = new List<StatModifier>();
private bool lastHadPawn = false;
private Building_NutrientVat Vat => parent as Building_NutrientVat;
public override List<StatModifier> StatOffsets
{
get
{
// Building_Enterable 使用 innerContainer 存放 Pawn
bool hasPawn = Vat != null && Vat.innerContainer.Any;
if (cachedStatOffsets.Count == 0 || hasPawn != lastHadPawn)
// If the vat contains a pawn, return the stat offsets defined in XML.
if (Vat != null && Vat.innerContainer.Any)
{
cachedStatOffsets.Clear();
if (hasPawn && Props.statOffset != null)
{
cachedStatOffsets.Add(Props.statOffset);
}
lastHadPawn = hasPawn;
return base.StatOffsets; // Returns the list from Props.statOffsets
}
return cachedStatOffsets;
// Otherwise, return an empty list, providing no bonus.
return new List<StatModifier>();
}
}
public override string CompInspectStringExtra()
{
// If we are not providing any bonus, don't call the base method to avoid empty lines.
if (StatOffsets.NullOrEmpty())
{
return null;
}
return base.CompInspectStringExtra();
}
}
}

View File

@@ -2,6 +2,7 @@ using RimWorld;
using Verse;
using System.Linq;
using UnityEngine;
using System.Text;
namespace ArachnaeSwarm
{
@@ -15,37 +16,90 @@ namespace ArachnaeSwarm
selfRefuelable = parent.GetComp<CompRefuelableNutrition>();
}
public override void CompTickRare()
public override string CompInspectStringExtra()
{
base.CompTickRare();
StringBuilder sb = new StringBuilder();
float efficiency = Mathf.Clamp(parent.GetStatValue(StatDef.Named("NutrientTransmissionEfficiency")), 0f, 0.1f);
sb.AppendLine("生物质传输效率".Translate() + ": " + efficiency.ToStringPercent());
sb.AppendLine("链接的建筑".Translate() + ":");
if (LinkedBuildings.NullOrEmpty())
{
sb.AppendLine(" (" + "None".Translate() + ")");
}
else
{
foreach (var building in LinkedBuildings)
{
var comp = (building as ThingWithComps)?.GetComp<CompRefuelableNutrition>();
if (comp != null)
{
bool needsFuel = NeedsFuel(comp);
sb.AppendLine($" - {building.LabelCap}: {comp.Fuel:F0}/{comp.Props.fuelCapacity:F0} (目标: {comp.TargetFuelLevel:F0}) -> {(needsFuel ? "" : "")}");
}
}
}
return sb.ToString().TrimEnd();
}
public override void CompTick()
{
base.CompTick();
// Log.Message($"[NutrientProvider] CompTick called for {parent.Label}."); // This will be too spammy
if (!parent.IsHashIntervalTick(250))
{
return;
}
Log.Message($"[NutrientProvider] Rare Tick Logic executing for {parent.Label}.");
if (selfRefuelable == null || !selfRefuelable.HasFuel)
{
return;
}
// 找到最需要燃料的已连接建筑
var targetBuilding = LinkedBuildings
.Select(b => new { Building = b, Comp = (b as ThingWithComps)?.GetComp<CompRefuelableNutrition>() })
.Where(x => x.Comp != null && x.Comp.Fuel < x.Comp.TargetFuelLevel)
.OrderBy(x => x.Comp.Fuel / x.Comp.Props.fuelCapacity) // 按燃料百分比排序
.Where(x => x.Comp != null && NeedsFuel(x.Comp))
.OrderBy(x => x.Comp.Fuel / x.Comp.Props.fuelCapacity)
.FirstOrDefault();
if (targetBuilding != null)
{
var consumerComp = targetBuilding.Comp;
float fuelNeeded = consumerComp.TargetFuelLevel - consumerComp.Fuel;
float desiredLevel = GetDesiredFuelLevel(consumerComp);
float fuelNeeded = desiredLevel - consumerComp.Fuel;
float fuelToTransfer = Mathf.Min(fuelNeeded, selfRefuelable.Fuel);
// 计算效率加成,并设置上限为 10%
float efficiency = Mathf.Clamp(parent.GetStatValue(StatDef.Named("NutrientTransmissionEfficiency")), 0f, 0.1f);
float finalCost = fuelToTransfer * (1 - efficiency);
Log.Message($"[NutrientProvider] Found target: {targetBuilding.Building.Label}. Fuel: {consumerComp.Fuel:F0}/{consumerComp.Props.fuelCapacity:F0}. Needs: {fuelNeeded:F2}. Transferring: {fuelToTransfer:F2}. Final cost: {finalCost:F2}");
selfRefuelable.ConsumeFuel(finalCost);
consumerComp.ReceiveFuel(fuelToTransfer);
// 可以在这里添加一个 Mote 来显示传输效果
}
}
private bool NeedsFuel(CompRefuelableNutrition comp)
{
if (comp == null) return false;
return comp.Fuel < GetDesiredFuelLevel(comp);
}
private float GetDesiredFuelLevel(CompRefuelableNutrition comp)
{
// If target is 0 (initial default) or max (UI default), treat as "fill to full".
if (comp.TargetFuelLevel <= 0.01f || comp.TargetFuelLevel >= comp.Props.fuelCapacity * 0.99f)
{
return comp.Props.fuelCapacity;
}
// Otherwise, respect the player's custom setting.
return comp.TargetFuelLevel;
}
}
}