zc
This commit is contained in:
@@ -126,29 +126,28 @@ namespace WulaFallenEmpire
|
||||
return true;
|
||||
}
|
||||
|
||||
// 修复:ConsumeResources 方法,使用产物的costList
|
||||
public bool ConsumeResources()
|
||||
// 修复:TryDeductResources 方法,尝试扣除资源
|
||||
public bool TryDeductResources()
|
||||
{
|
||||
var globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
|
||||
if (globalStorage == null) return false;
|
||||
|
||||
// 首先消耗产物的costList(对于武器等物品)
|
||||
// 检查资源是否足够
|
||||
if (!HasEnoughResources()) return false;
|
||||
|
||||
// 扣除资源
|
||||
var productCostList = GetProductCostList();
|
||||
if (productCostList.Count > 0)
|
||||
{
|
||||
foreach (var kvp in productCostList)
|
||||
{
|
||||
if (!globalStorage.RemoveFromInputStorage(kvp.Key, kvp.Value))
|
||||
return false;
|
||||
globalStorage.RemoveFromInputStorage(kvp.Key, kvp.Value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 如果没有costList,则消耗配方的ingredients(对于加工类配方)
|
||||
foreach (var ingredient in recipe.ingredients)
|
||||
{
|
||||
bool consumedThisIngredient = false;
|
||||
|
||||
foreach (var thingDef in ingredient.filter.AllowedThingDefs)
|
||||
{
|
||||
int requiredCount = ingredient.CountRequiredOfFor(thingDef, recipe);
|
||||
@@ -156,16 +155,10 @@ namespace WulaFallenEmpire
|
||||
|
||||
if (availableCount >= requiredCount)
|
||||
{
|
||||
if (globalStorage.RemoveFromInputStorage(thingDef, requiredCount))
|
||||
{
|
||||
consumedThisIngredient = true;
|
||||
break;
|
||||
}
|
||||
globalStorage.RemoveFromInputStorage(thingDef, requiredCount);
|
||||
break; // 只扣除一种满足条件的材料
|
||||
}
|
||||
}
|
||||
|
||||
if (!consumedThisIngredient)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -258,20 +251,18 @@ namespace WulaFallenEmpire
|
||||
return;
|
||||
}
|
||||
|
||||
if (HasEnoughResources())
|
||||
// 自动状态切换逻辑(仅用于从Gathering切换到Producing)
|
||||
// 注意:现在资源的扣除是显式的,所以这里只检查是否可以开始
|
||||
if (state == ProductionState.Gathering && !paused)
|
||||
{
|
||||
if (state == ProductionState.Gathering && !paused)
|
||||
if (HasEnoughResources())
|
||||
{
|
||||
state = ProductionState.Producing;
|
||||
progress = 0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (state == ProductionState.Producing)
|
||||
{
|
||||
state = ProductionState.Gathering;
|
||||
progress = 0f;
|
||||
// 尝试扣除资源并开始生产
|
||||
if (TryDeductResources())
|
||||
{
|
||||
state = ProductionState.Producing;
|
||||
progress = 0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,31 +172,28 @@ namespace WulaFallenEmpire
|
||||
|
||||
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.UpdateState();
|
||||
|
||||
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");
|
||||
}
|
||||
order.state = GlobalProductionOrder.ProductionState.Completed;
|
||||
orders.RemoveAt(index);
|
||||
Log.Message($"[COMPLETE] Order {order.recipe.defName} completed and removed");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 修复:资源不足,回到等待状态
|
||||
// 如果还有剩余数量,回到Gathering状态准备下一轮
|
||||
order.state = GlobalProductionOrder.ProductionState.Gathering;
|
||||
order.progress = 0f;
|
||||
Log.Message($"[WARNING] Failed to consume resources for {order.recipe.defName}, resetting");
|
||||
// UpdateState 会自动检查资源并尝试开始下一轮
|
||||
order.UpdateState();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -629,7 +629,7 @@ namespace WulaFallenEmpire
|
||||
return;
|
||||
|
||||
// 尝试消耗资源(如果可能)
|
||||
bool resourcesConsumed = order.ConsumeResources();
|
||||
bool resourcesConsumed = order.TryDeductResources();
|
||||
|
||||
if (!resourcesConsumed)
|
||||
{
|
||||
|
||||
@@ -77,7 +77,7 @@ namespace WulaFallenEmpire
|
||||
|
||||
if (allSatisfied)
|
||||
{
|
||||
// 消耗容器中的材料并上传到云端
|
||||
// 1. 消耗容器中的材料并上传到云端
|
||||
foreach (var kvp in costList)
|
||||
{
|
||||
int needed = kvp.Value;
|
||||
@@ -100,9 +100,19 @@ namespace WulaFallenEmpire
|
||||
}
|
||||
}
|
||||
|
||||
// 切换状态
|
||||
order.state = GlobalProductionOrder.ProductionState.Producing;
|
||||
Messages.Message("WULA_OrderStarted".Translate(order.Label), table, MessageTypeDefOf.PositiveEvent);
|
||||
// 2. 立即尝试扣除资源并开始生产
|
||||
// 这会从云端扣除刚刚上传的资源,防止其他订单抢占
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user