This commit is contained in:
2025-11-03 12:04:10 +08:00
parent 242866bada
commit d72edae9a3
28 changed files with 1709 additions and 633 deletions

View File

@@ -0,0 +1,61 @@
// Building_GlobalWorkTable.cs (修复版)
using RimWorld;
using System.Collections.Generic;
using Verse;
namespace WulaFallenEmpire
{
public class Building_GlobalWorkTable : Building_WorkTable
{
public GlobalProductionOrderStack globalOrderStack;
private CompPowerTrader powerComp;
private int lastProcessTick = -1;
private const int ProcessInterval = 60; // 每60tick处理一次
public Building_GlobalWorkTable()
{
globalOrderStack = new GlobalProductionOrderStack(this);
}
public override void ExposeData()
{
base.ExposeData();
Scribe_Deep.Look(ref globalOrderStack, "globalOrderStack", this);
}
public override void SpawnSetup(Map map, bool respawningAfterLoad)
{
base.SpawnSetup(map, respawningAfterLoad);
powerComp = GetComp<CompPowerTrader>();
}
public override void Tick()
{
base.Tick();
// 每60tick处理一次生产订单
if (Find.TickManager.TicksGame % ProcessInterval == 0 &&
Find.TickManager.TicksGame != lastProcessTick)
{
lastProcessTick = Find.TickManager.TicksGame;
if (powerComp == null || powerComp.PowerOn)
{
Log.Message($"[DEBUG] Processing orders at tick {Find.TickManager.TicksGame}");
globalOrderStack.ProcessOrders();
}
else
{
Log.Message("[DEBUG] No power, skipping order processing");
}
}
}
public bool CurrentlyUsableForGlobalBills()
{
return (powerComp == null || powerComp.PowerOn) &&
(GetComp<CompBreakdownable>() == null || !GetComp<CompBreakdownable>().BrokenDown);
}
}
}

View File

@@ -0,0 +1,120 @@
// GlobalProductionOrder.cs (修复版)
using RimWorld;
using System.Collections.Generic;
using Verse;
namespace WulaFallenEmpire
{
public class GlobalProductionOrder : IExposable
{
public RecipeDef recipe;
public int targetCount = 1;
public int currentCount = 0;
public bool paused = true; // 初始状态为暂停
public float progress = 0f;
// 生产状态
public ProductionState state = ProductionState.Waiting;
public enum ProductionState
{
Waiting, // 等待资源
Producing, // 生产中
Completed // 完成
}
public void ExposeData()
{
Scribe_Defs.Look(ref recipe, "recipe");
Scribe_Values.Look(ref targetCount, "targetCount", 1);
Scribe_Values.Look(ref currentCount, "currentCount", 0);
Scribe_Values.Look(ref paused, "paused", true);
Scribe_Values.Look(ref progress, "progress", 0f);
Scribe_Values.Look(ref state, "state", ProductionState.Waiting);
}
public string Label => recipe.LabelCap;
public string Description => $"{currentCount}/{targetCount} {recipe.products[0].thingDef.label}";
// 检查是否有足够资源 - 修复逻辑只检查costList
public bool HasEnoughResources()
{
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage == null)
{
Log.Warning("GlobalStorageWorldComponent not found");
return false;
}
// 只检查costList不检查ingredients
if (recipe.costList != null && recipe.costList.Count > 0)
{
foreach (var cost in recipe.costList)
{
int required = cost.count;
int available = globalStorage.GetInputStorageCount(cost.thingDef);
Log.Message($"[DEBUG] Checking {cost.thingDef.defName}: required={required}, available={available}");
if (available < required)
{
Log.Message($"[DEBUG] Insufficient {cost.thingDef.defName}");
return false;
}
}
Log.Message("[DEBUG] All resources available");
return true;
}
else
{
Log.Warning($"Recipe {recipe.defName} has no costList");
return false;
}
}
// 消耗资源 - 修复逻辑只消耗costList
public bool ConsumeResources()
{
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage == null) return false;
// 只消耗costList中的资源
if (recipe.costList != null)
{
foreach (var cost in recipe.costList)
{
if (!globalStorage.RemoveFromInputStorage(cost.thingDef, cost.count))
{
Log.Warning($"Failed to consume {cost.count} {cost.thingDef.defName}");
return false;
}
Log.Message($"[DEBUG] Consumed {cost.count} {cost.thingDef.defName}");
}
return true;
}
return false;
}
// 生产一个产品
public void Produce()
{
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage == null) return;
foreach (var product in recipe.products)
{
globalStorage.AddToOutputStorage(product.thingDef, product.count);
Log.Message($"[DEBUG] Produced {product.count} {product.thingDef.defName}");
}
currentCount++;
progress = 0f;
if (currentCount >= targetCount)
{
state = ProductionState.Completed;
Log.Message("[DEBUG] Order completed");
}
}
}
}

View File

@@ -0,0 +1,98 @@
// GlobalProductionOrderStack.cs (修复版)
using RimWorld;
using System.Collections.Generic;
using Verse;
namespace WulaFallenEmpire
{
public class GlobalProductionOrderStack : IExposable
{
public Building_GlobalWorkTable table;
public List<GlobalProductionOrder> orders = new List<GlobalProductionOrder>();
public GlobalProductionOrderStack(Building_GlobalWorkTable table)
{
this.table = table;
}
public void ExposeData()
{
Scribe_References.Look(ref table, "table");
Scribe_Collections.Look(ref orders, "orders", LookMode.Deep);
}
public void AddOrder(GlobalProductionOrder order)
{
orders.Add(order);
// 添加到全局存储中统一管理
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage != null)
{
globalStorage.AddProductionOrder(order);
}
}
public void Delete(GlobalProductionOrder order)
{
orders.Remove(order);
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage != null)
{
globalStorage.RemoveProductionOrder(order);
}
}
public void ProcessOrders()
{
foreach (var order in orders)
{
if (order.paused || order.state == GlobalProductionOrder.ProductionState.Completed)
continue;
// 检查资源并更新状态
if (order.state == GlobalProductionOrder.ProductionState.Waiting)
{
if (order.HasEnoughResources())
{
order.state = GlobalProductionOrder.ProductionState.Producing;
Log.Message($"[DEBUG] Order {order.recipe.defName} started producing");
}
else
{
continue;
}
}
// 生产中
if (order.state == GlobalProductionOrder.ProductionState.Producing)
{
// 更清晰的进度计算
float progressPerTick = 1f / (order.recipe.workAmount * 5f); // 调整系数以控制生产速度
order.progress += progressPerTick;
if (order.progress >= 1f)
{
// 消耗资源并生产 - 在结束时扣除资源
if (order.ConsumeResources())
{
order.Produce();
}
else
{
// 资源被其他订单消耗,回到等待状态
order.state = GlobalProductionOrder.ProductionState.Waiting;
order.progress = 0f;
Log.Message("[DEBUG] Resources consumed by another order, returning to waiting state");
}
}
else
{
Log.Message($"[DEBUG] Order {order.recipe.defName} progress: {order.progress:P0}");
}
}
}
}
}
}

View File

@@ -0,0 +1,148 @@
using LudeonTK;
using RimWorld;
using RimWorld.Planet;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Verse;
namespace WulaFallenEmpire
{
public class GlobalStorageWorldComponent : WorldComponent
{
public Dictionary<ThingDef, int> inputStorage = new Dictionary<ThingDef, int>();
public Dictionary<ThingDef, int> outputStorage = new Dictionary<ThingDef, int>();
// 存储生产订单
public List<GlobalProductionOrder> productionOrders = new List<GlobalProductionOrder>();
public GlobalStorageWorldComponent(World world) : base(world) { }
public override void ExposeData()
{
base.ExposeData();
// 序列化输入存储
Scribe_Collections.Look(ref inputStorage, "inputStorage", LookMode.Def, LookMode.Value);
if (inputStorage == null) inputStorage = new Dictionary<ThingDef, int>();
// 序列化输出存储
Scribe_Collections.Look(ref outputStorage, "outputStorage", LookMode.Def, LookMode.Value);
if (outputStorage == null) outputStorage = new Dictionary<ThingDef, int>();
// 序列化生产订单
Scribe_Collections.Look(ref productionOrders, "productionOrders", LookMode.Deep);
if (productionOrders == null) productionOrders = new List<GlobalProductionOrder>();
}
// 输入存储方法
public void AddToInputStorage(ThingDef thingDef, int count)
{
if (inputStorage.ContainsKey(thingDef))
inputStorage[thingDef] += count;
else
inputStorage[thingDef] = count;
}
public bool RemoveFromInputStorage(ThingDef thingDef, int count)
{
if (inputStorage.ContainsKey(thingDef) && inputStorage[thingDef] >= count)
{
inputStorage[thingDef] -= count;
if (inputStorage[thingDef] <= 0)
inputStorage.Remove(thingDef);
return true;
}
return false;
}
public int GetInputStorageCount(ThingDef thingDef)
{
return inputStorage.ContainsKey(thingDef) ? inputStorage[thingDef] : 0;
}
// 输出存储方法
public void AddToOutputStorage(ThingDef thingDef, int count)
{
if (outputStorage.ContainsKey(thingDef))
outputStorage[thingDef] += count;
else
outputStorage[thingDef] = count;
}
public bool RemoveFromOutputStorage(ThingDef thingDef, int count)
{
if (outputStorage.ContainsKey(thingDef) && outputStorage[thingDef] >= count)
{
outputStorage[thingDef] -= count;
if (outputStorage[thingDef] <= 0)
outputStorage.Remove(thingDef);
return true;
}
return false;
}
public int GetOutputStorageCount(ThingDef thingDef)
{
return outputStorage.ContainsKey(thingDef) ? outputStorage[thingDef] : 0;
}
// 生产订单管理
public void AddProductionOrder(GlobalProductionOrder order)
{
productionOrders.Add(order);
}
public void RemoveProductionOrder(GlobalProductionOrder order)
{
productionOrders.Remove(order);
}
// 添加调试方法
[DebugAction("WULA", "Add Test Resources", actionType = DebugActionType.Action)]
public static void DebugAddTestResources()
{
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage != null)
{
globalStorage.AddToInputStorage(ThingDefOf.Steel, 200);
globalStorage.AddToInputStorage(ThingDefOf.ComponentIndustrial, 100);
Log.Message("Added test resources to global storage");
}
}
[DebugAction("WULA", "Spawn All Products", actionType = DebugActionType.Action)]
public static void DebugSpawnAllProducts()
{
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage != null)
{
// 查找任意工作台来生成物品
var workTable = Find.CurrentMap?.listerBuildings?.allBuildingsColonist?
.FirstOrDefault(b => b is Building_GlobalWorkTable) as Building_GlobalWorkTable;
if (workTable != null && workTable.Spawned)
{
foreach (var kvp in globalStorage.outputStorage.ToList()) // 使用ToList避免修改时枚举
{
ThingDef thingDef = kvp.Key;
int count = kvp.Value;
while (count > 0)
{
int stackSize = Mathf.Min(count, thingDef.stackLimit);
Thing thing = ThingMaker.MakeThing(thingDef);
thing.stackCount = stackSize;
GenPlace.TryPlaceThing(thing, workTable.Position, workTable.Map, ThingPlaceMode.Near);
globalStorage.RemoveFromOutputStorage(thingDef, stackSize);
count -= stackSize;
}
}
Log.Message("Spawned all output products");
}
}
}
}
}

View File

@@ -0,0 +1,285 @@
// ITab_GlobalBills.cs (修复版)
using System.Collections.Generic;
using RimWorld;
using UnityEngine;
using Verse;
using Verse.Sound;
namespace WulaFallenEmpire
{
public class ITab_GlobalBills : ITab
{
private float viewHeight = 1000f;
private Vector2 scrollPosition;
private GlobalProductionOrder mouseoverOrder;
private static readonly Vector2 WinSize = new Vector2(420f, 480f);
protected Building_GlobalWorkTable SelTable => (Building_GlobalWorkTable)base.SelThing;
public ITab_GlobalBills()
{
size = WinSize;
labelKey = "WULA_GlobalBillsTab";
tutorTag = "GlobalBills";
}
protected override void FillTab()
{
Rect mainRect = new Rect(0f, 0f, WinSize.x, WinSize.y).ContractedBy(10f);
// 标题
Text.Font = GameFont.Medium;
Widgets.Label(new Rect(mainRect.x, mainRect.y, mainRect.width, 30f), "WULA_GlobalProduction".Translate());
Text.Font = GameFont.Small;
// 开发模式按钮区域
if (Prefs.DevMode)
{
Rect devButtonRect = new Rect(mainRect.x, mainRect.y + 35f, mainRect.width, 25f);
DoDevButtons(devButtonRect);
}
// 订单列表区域 - 调整位置
float ordersRectY = Prefs.DevMode ? mainRect.y + 65f : mainRect.y + 35f;
Rect ordersRect = new Rect(mainRect.x, ordersRectY, mainRect.width, mainRect.height - (Prefs.DevMode ? 110f : 80f));
mouseoverOrder = DoOrdersListing(ordersRect);
// 添加订单按钮
Rect addButtonRect = new Rect(mainRect.x, mainRect.yMax - 35f, mainRect.width, 30f);
if (Widgets.ButtonText(addButtonRect, "WULA_AddProductionOrder".Translate()))
{
Find.WindowStack.Add(new FloatMenu(GenerateRecipeOptions()));
}
}
private void DoDevButtons(Rect rect)
{
Rect button1Rect = new Rect(rect.x, rect.y, rect.width / 2 - 5f, rect.height);
Rect button2Rect = new Rect(rect.x + rect.width / 2 + 5f, rect.y, rect.width / 2 - 5f, rect.height);
if (Widgets.ButtonText(button1Rect, "DEV: Add Resources"))
{
AddTestResources();
}
if (Widgets.ButtonText(button2Rect, "DEV: Spawn Products"))
{
SpawnOutputProducts();
}
}
private void AddTestResources()
{
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage != null)
{
// 添加200钢铁
ThingDef steelDef = ThingDefOf.Steel;
globalStorage.AddToInputStorage(steelDef, 200);
// 添加100零部件
ThingDef componentDef = ThingDefOf.ComponentIndustrial;
globalStorage.AddToInputStorage(componentDef, 100);
Messages.Message("Added 200 Steel and 100 Components to global storage", MessageTypeDefOf.PositiveEvent);
Log.Message("[DEBUG] Added test resources");
}
}
private void SpawnOutputProducts()
{
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage != null && SelTable != null && SelTable.Spawned)
{
Map map = SelTable.Map;
IntVec3 spawnCell = SelTable.Position;
int totalSpawned = 0;
// 复制列表以避免修改时枚举
var outputCopy = new Dictionary<ThingDef, int>(globalStorage.outputStorage);
foreach (var kvp in outputCopy)
{
ThingDef thingDef = kvp.Key;
int count = kvp.Value;
if (count > 0)
{
// 创建物品并放置到地图上
while (count > 0)
{
int stackSize = Mathf.Min(count, thingDef.stackLimit);
Thing thing = ThingMaker.MakeThing(thingDef);
thing.stackCount = stackSize;
if (GenPlace.TryPlaceThing(thing, spawnCell, map, ThingPlaceMode.Near))
{
globalStorage.RemoveFromOutputStorage(thingDef, stackSize);
count -= stackSize;
totalSpawned += stackSize;
}
else
{
break;
}
}
}
}
Messages.Message($"Spawned {totalSpawned} items at worktable location", MessageTypeDefOf.PositiveEvent);
Log.Message($"[DEBUG] Spawned {totalSpawned} output products");
}
}
private GlobalProductionOrder DoOrdersListing(Rect rect)
{
GlobalProductionOrder result = null;
Widgets.DrawMenuSection(rect);
Rect outRect = rect.ContractedBy(5f);
Rect viewRect = new Rect(0f, 0f, outRect.width - 16f, viewHeight);
Widgets.BeginScrollView(outRect, ref scrollPosition, viewRect);
float curY = 0f;
for (int i = 0; i < SelTable.globalOrderStack.orders.Count; i++)
{
var order = SelTable.globalOrderStack.orders[i];
// 增加订单行高度
Rect orderRect = new Rect(0f, curY, viewRect.width, 90f);
if (DoOrderRow(orderRect, order))
{
result = order;
}
curY += 95f; // 增加行间距
// 分隔线
if (i < SelTable.globalOrderStack.orders.Count - 1)
{
Widgets.DrawLineHorizontal(0f, curY - 2f, viewRect.width);
curY += 5f;
}
}
if (Event.current.type == EventType.Layout)
{
viewHeight = curY;
}
Widgets.EndScrollView();
return result;
}
private bool DoOrderRow(Rect rect, GlobalProductionOrder order)
{
Widgets.DrawHighlightIfMouseover(rect);
// 增加内边距和行高
float padding = 8f;
float lineHeight = 20f;
// 订单信息
Rect infoRect = new Rect(rect.x + padding, rect.y + padding, rect.width - 100f, lineHeight);
Widgets.Label(infoRect, order.Label);
Rect descRect = new Rect(rect.x + padding, rect.y + padding + lineHeight + 2f, rect.width - 100f, lineHeight);
Widgets.Label(descRect, order.Description);
// 状态显示区域
Rect statusRect = new Rect(rect.x + padding, rect.y + padding + (lineHeight + 2f) * 2, rect.width - 100f, lineHeight);
if (order.state == GlobalProductionOrder.ProductionState.Producing)
{
// 进度条
Rect progressRect = new Rect(rect.x + padding, rect.y + padding + (lineHeight + 2f) * 2, rect.width - 100f, 18f);
Widgets.FillableBar(progressRect, order.progress);
// 进度文本
Text.Anchor = TextAnchor.MiddleCenter;
Widgets.Label(progressRect, $"{order.progress:P0}");
Text.Anchor = TextAnchor.UpperLeft;
}
else
{
string statusText = order.state switch
{
GlobalProductionOrder.ProductionState.Waiting => "WULA_WaitingForResources".Translate(),
GlobalProductionOrder.ProductionState.Completed => "WULA_Completed".Translate(),
_ => "WULA_Unknown".Translate()
};
Widgets.Label(statusRect, statusText);
}
// 控制按钮
float buttonY = rect.y + padding;
Rect pauseButtonRect = new Rect(rect.xMax - 90f, buttonY, 40f, 25f);
if (Widgets.ButtonText(pauseButtonRect, order.paused ? "WULA_Resume".Translate() : "WULA_Pause".Translate()))
{
order.paused = !order.paused;
SoundDefOf.Click.PlayOneShotOnCamera();
}
Rect deleteButtonRect = new Rect(rect.xMax - 45f, buttonY, 40f, 25f);
if (Widgets.ButtonText(deleteButtonRect, "WULA_Delete".Translate()))
{
SelTable.globalOrderStack.Delete(order);
SoundDefOf.Click.PlayOneShotOnCamera();
}
// 资源检查提示 - 修复逻辑
bool shouldShowRedBorder = (order.state == GlobalProductionOrder.ProductionState.Waiting && !order.HasEnoughResources());
if (shouldShowRedBorder)
{
TooltipHandler.TipRegion(rect, "WULA_InsufficientResources".Translate());
GUI.color = Color.red;
Widgets.DrawBox(rect, 2);
GUI.color = Color.white;
}
return Mouse.IsOver(rect);
}
private List<FloatMenuOption> GenerateRecipeOptions()
{
var options = new List<FloatMenuOption>();
foreach (var recipe in SelTable.def.AllRecipes)
{
if (recipe.AvailableNow && recipe.AvailableOnNow(SelTable))
{
string label = recipe.LabelCap;
options.Add(new FloatMenuOption(label, () =>
{
var newOrder = new GlobalProductionOrder
{
recipe = recipe,
targetCount = 1,
paused = true // 初始暂停
};
SelTable.globalOrderStack.AddOrder(newOrder);
SoundDefOf.Click.PlayOneShotOnCamera();
Log.Message($"[DEBUG] Added order for {recipe.defName}");
}));
}
}
if (options.Count == 0)
{
options.Add(new FloatMenuOption("WULA_NoAvailableRecipes".Translate(), null));
}
return options;
}
public override void TabUpdate()
{
base.TabUpdate();
}
}
}