This commit is contained in:
2025-11-22 17:42:47 +08:00
parent 5b1c9ca5ec
commit b8c548aedf
6 changed files with 52 additions and 53 deletions

1
.gitignore vendored
View File

@@ -43,3 +43,4 @@ package-lock.json
package.json package.json
dark-server/dark-server.js dark-server/dark-server.js
node_modules/ node_modules/
gemini-websocket-proxy/

View File

@@ -126,29 +126,28 @@ namespace WulaFallenEmpire
return true; return true;
} }
// 修复:ConsumeResources 方法,使用产物的costList // 修复:TryDeductResources 方法,尝试扣除资源
public bool ConsumeResources() public bool TryDeductResources()
{ {
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>(); var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
if (globalStorage == null) return false; if (globalStorage == null) return false;
// 首先消耗产物的costList对于武器等物品 // 检查资源是否足够
if (!HasEnoughResources()) return false;
// 扣除资源
var productCostList = GetProductCostList(); var productCostList = GetProductCostList();
if (productCostList.Count > 0) if (productCostList.Count > 0)
{ {
foreach (var kvp in productCostList) foreach (var kvp in productCostList)
{ {
if (!globalStorage.RemoveFromInputStorage(kvp.Key, kvp.Value)) globalStorage.RemoveFromInputStorage(kvp.Key, kvp.Value);
return false;
} }
return true; return true;
} }
// 如果没有costList则消耗配方的ingredients对于加工类配方
foreach (var ingredient in recipe.ingredients) foreach (var ingredient in recipe.ingredients)
{ {
bool consumedThisIngredient = false;
foreach (var thingDef in ingredient.filter.AllowedThingDefs) foreach (var thingDef in ingredient.filter.AllowedThingDefs)
{ {
int requiredCount = ingredient.CountRequiredOfFor(thingDef, recipe); int requiredCount = ingredient.CountRequiredOfFor(thingDef, recipe);
@@ -156,16 +155,10 @@ namespace WulaFallenEmpire
if (availableCount >= requiredCount) if (availableCount >= requiredCount)
{ {
if (globalStorage.RemoveFromInputStorage(thingDef, requiredCount)) globalStorage.RemoveFromInputStorage(thingDef, requiredCount);
{ break; // 只扣除一种满足条件的材料
consumedThisIngredient = true;
break;
}
} }
} }
if (!consumedThisIngredient)
return false;
} }
return true; return true;
@@ -258,20 +251,18 @@ namespace WulaFallenEmpire
return; return;
} }
if (HasEnoughResources()) // 自动状态切换逻辑仅用于从Gathering切换到Producing
// 注意:现在资源的扣除是显式的,所以这里只检查是否可以开始
if (state == ProductionState.Gathering && !paused)
{ {
if (state == ProductionState.Gathering && !paused) if (HasEnoughResources())
{ {
state = ProductionState.Producing; // 尝试扣除资源并开始生产
progress = 0f; if (TryDeductResources())
} {
} state = ProductionState.Producing;
else progress = 0f;
{ }
if (state == ProductionState.Producing)
{
state = ProductionState.Gathering;
progress = 0f;
} }
} }
} }

View File

@@ -172,31 +172,28 @@ namespace WulaFallenEmpire
private void CompleteProduction(GlobalProductionOrder order, int index) private void CompleteProduction(GlobalProductionOrder order, int index)
{ {
// 修复:生产完成,消耗资源 // 生产完成(资源已经在开始生产时扣除)
if (order.ConsumeResources()) order.Produce();
Log.Message($"[SUCCESS] Produced {order.recipe.products[0].thingDef.defName}, " +
$"count: {order.currentCount}/{order.targetCount}");
// 重置进度
order.progress = 0f;
// 检查是否完成所有目标数量
if (order.currentCount >= order.targetCount)
{ {
order.Produce(); order.state = GlobalProductionOrder.ProductionState.Completed;
order.UpdateState(); orders.RemoveAt(index);
Log.Message($"[COMPLETE] Order {order.recipe.defName} completed and removed");
Log.Message($"[SUCCESS] Produced {order.recipe.products[0].thingDef.defName}, " +
$"count: {order.currentCount}/{order.targetCount}");
// 重置进度
order.progress = 0f;
// 如果订单完成,移除它
if (order.state == GlobalProductionOrder.ProductionState.Completed)
{
orders.RemoveAt(index);
Log.Message($"[COMPLETE] Order {order.recipe.defName} completed and removed");
}
} }
else else
{ {
// 修复:资源不足,回到等待状态 // 如果还有剩余数量回到Gathering状态准备下一轮
order.state = GlobalProductionOrder.ProductionState.Gathering; order.state = GlobalProductionOrder.ProductionState.Gathering;
order.progress = 0f; // UpdateState 会自动检查资源并尝试开始下一轮
Log.Message($"[WARNING] Failed to consume resources for {order.recipe.defName}, resetting"); order.UpdateState();
} }
} }

View File

@@ -629,7 +629,7 @@ namespace WulaFallenEmpire
return; return;
// 尝试消耗资源(如果可能) // 尝试消耗资源(如果可能)
bool resourcesConsumed = order.ConsumeResources(); bool resourcesConsumed = order.TryDeductResources();
if (!resourcesConsumed) if (!resourcesConsumed)
{ {

View File

@@ -77,7 +77,7 @@ namespace WulaFallenEmpire
if (allSatisfied) if (allSatisfied)
{ {
// 消耗容器中的材料并上传到云端 // 1. 消耗容器中的材料并上传到云端
foreach (var kvp in costList) foreach (var kvp in costList)
{ {
int needed = kvp.Value; int needed = kvp.Value;
@@ -100,9 +100,19 @@ namespace WulaFallenEmpire
} }
} }
// 切换状态 // 2. 立即尝试扣除资源并开始生产
order.state = GlobalProductionOrder.ProductionState.Producing; // 这会从云端扣除刚刚上传的资源,防止其他订单抢占
Messages.Message("WULA_OrderStarted".Translate(order.Label), table, MessageTypeDefOf.PositiveEvent); if (order.TryDeductResources())
{
order.state = GlobalProductionOrder.ProductionState.Producing;
order.progress = 0f;
Messages.Message("WULA_OrderStarted".Translate(order.Label), table, MessageTypeDefOf.PositiveEvent);
}
else
{
// 理论上不应该发生,因为前面检查了 allSatisfied
Log.Error($"[WULA] Failed to deduct resources for {order.Label} immediately after upload.");
}
} }
} }
} }