1
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using RimWorld;
|
||||
using RimWorld.Planet;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Verse;
|
||||
using RimWorld.QuestGen;
|
||||
@@ -12,19 +13,38 @@ namespace WulaFallenEmpire
|
||||
public ThingDef resourceDef;
|
||||
public int requiredCount;
|
||||
public int retryDelayTicks = 60;
|
||||
|
||||
[NoTranslate]
|
||||
public string successSignal;
|
||||
|
||||
[NoTranslate]
|
||||
public string failSignal;
|
||||
|
||||
public bool deductOnSuccess = true;
|
||||
public bool useInputStorage = true;
|
||||
public string debugInfo;
|
||||
|
||||
// 状态变量
|
||||
private int nextRetryTick = -1;
|
||||
public int nextRetryTick = -1;
|
||||
private bool hasSucceeded = false;
|
||||
private bool hasFailed = false;
|
||||
private int retryCount = 0;
|
||||
private const int MAX_RETRY_COUNT = 1000;
|
||||
|
||||
public override void AssignDebugData()
|
||||
{
|
||||
base.AssignDebugData();
|
||||
|
||||
resourceDef = ThingDefOf.Steel;
|
||||
requiredCount = 100;
|
||||
retryDelayTicks = 60;
|
||||
successSignal = "TaxPaymentSuccess";
|
||||
failSignal = "TaxPaymentFailed";
|
||||
deductOnSuccess = true;
|
||||
useInputStorage = true;
|
||||
debugInfo = "Debug: Tax Collection Check";
|
||||
}
|
||||
|
||||
protected override void Enable(SignalArgs receivedArgs)
|
||||
{
|
||||
base.Enable(receivedArgs);
|
||||
@@ -34,32 +54,28 @@ namespace WulaFallenEmpire
|
||||
Log.Message($"QuestPart_GlobalResourceCheck Enabled: Will check for {requiredCount} {resourceDef?.defName} in {(useInputStorage ? "Input" : "Output")} Storage");
|
||||
}
|
||||
|
||||
public override void Notify_QuestSignalReceived(Signal signal)
|
||||
{
|
||||
base.Notify_QuestSignalReceived(signal);
|
||||
|
||||
// 如果任务已经结束,停止所有操作
|
||||
if (quest.State != QuestState.Ongoing && quest.State != QuestState.NotYetAccepted)
|
||||
{
|
||||
DoCleanup();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public override void QuestPartTick()
|
||||
{
|
||||
base.QuestPartTick();
|
||||
|
||||
// 如果已经成功或失败,或者任务已结束,不再处理
|
||||
if (hasSucceeded || hasFailed || (quest.State != QuestState.Ongoing && quest.State != QuestState.NotYetAccepted))
|
||||
// 如果任务已经结束,停止处理
|
||||
if (quest.State != QuestState.Ongoing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果已经成功或失败,不再处理
|
||||
if (hasSucceeded || hasFailed)
|
||||
{
|
||||
DoCleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否到了重试时间
|
||||
if (Find.TickManager.TicksGame < nextRetryTick && nextRetryTick != -1)
|
||||
int currentTick = Find.TickManager.TicksGame;
|
||||
if (nextRetryTick == -1 || currentTick < nextRetryTick)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 执行资源检查
|
||||
CheckGlobalResource();
|
||||
@@ -67,44 +83,52 @@ namespace WulaFallenEmpire
|
||||
|
||||
private void CheckGlobalResource()
|
||||
{
|
||||
// 更新下次重试时间
|
||||
nextRetryTick = Find.TickManager.TicksGame + retryDelayTicks;
|
||||
retryCount++;
|
||||
|
||||
// 获取全局资源储存器
|
||||
GlobalStorageWorldComponent globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
|
||||
if (globalStorage == null)
|
||||
try
|
||||
{
|
||||
Log.Error("QuestPart_GlobalResourceCheck: GlobalStorageWorldComponent not found");
|
||||
HandleFailure("Global storage component missing");
|
||||
return;
|
||||
// 更新下次重试时间
|
||||
nextRetryTick = Find.TickManager.TicksGame + retryDelayTicks;
|
||||
retryCount++;
|
||||
|
||||
// 获取全局资源储存器
|
||||
GlobalStorageWorldComponent globalStorage = Find.World.GetComponent<GlobalStorageWorldComponent>();
|
||||
if (globalStorage == null)
|
||||
{
|
||||
Log.Error("QuestPart_GlobalResourceCheck: GlobalStorageWorldComponent not found");
|
||||
HandleFailure("Global storage component missing");
|
||||
return;
|
||||
}
|
||||
|
||||
if (resourceDef == null)
|
||||
{
|
||||
Log.Error("QuestPart_GlobalResourceCheck: resourceDef is null");
|
||||
HandleFailure("Resource definition is null");
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查资源是否足够
|
||||
int currentAmount = useInputStorage ?
|
||||
globalStorage.GetInputStorageCount(resourceDef) :
|
||||
globalStorage.GetOutputStorageCount(resourceDef);
|
||||
|
||||
bool hasEnough = currentAmount >= requiredCount;
|
||||
|
||||
Log.Message($"GlobalResourceCheck [{retryCount}]: {currentAmount}/{requiredCount} {resourceDef.defName} in {(useInputStorage ? "Input" : "Output")} Storage - Enough: {hasEnough}");
|
||||
|
||||
if (hasEnough)
|
||||
{
|
||||
// 资源足够,处理成功
|
||||
HandleSuccess(globalStorage);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 资源不足,安排重试
|
||||
HandleFailure($"Insufficient resources: {currentAmount}/{requiredCount}");
|
||||
}
|
||||
}
|
||||
|
||||
if (resourceDef == null)
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error("QuestPart_GlobalResourceCheck: resourceDef is null");
|
||||
HandleFailure("Resource definition is null");
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查资源是否足够
|
||||
int currentAmount = useInputStorage ?
|
||||
globalStorage.GetInputStorageCount(resourceDef) :
|
||||
globalStorage.GetOutputStorageCount(resourceDef);
|
||||
|
||||
bool hasEnough = currentAmount >= requiredCount;
|
||||
|
||||
Log.Message($"GlobalResourceCheck [{retryCount}]: {currentAmount}/{requiredCount} {resourceDef.defName} in {(useInputStorage ? "Input" : "Output")} Storage - Enough: {hasEnough}");
|
||||
|
||||
if (hasEnough)
|
||||
{
|
||||
// 资源足够,处理成功
|
||||
HandleSuccess(globalStorage);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 资源不足,安排重试
|
||||
HandleFailure($"Insufficient resources: {currentAmount}/{requiredCount}");
|
||||
Log.Error($"GlobalResourceCheck: Exception during check - {ex}");
|
||||
HandleFailure($"Exception: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,16 +151,15 @@ namespace WulaFallenEmpire
|
||||
|
||||
Log.Message($"GlobalResourceCheck: SUCCESS - {(deductOnSuccess ? "Deducted" : "Found")} {requiredCount} {resourceDef.defName} from {(useInputStorage ? "Input" : "Output")} Storage");
|
||||
|
||||
// 发送成功信号 - 使用 QuestGenUtility 来生成带任务前缀的信号
|
||||
// 发送成功信号
|
||||
if (!successSignal.NullOrEmpty())
|
||||
{
|
||||
string fullSignal = QuestGenUtility.HardcodedSignalWithQuestID(successSignal);
|
||||
Find.SignalManager.SendSignal(new Signal(fullSignal));
|
||||
Log.Message($"GlobalResourceCheck: Sent success signal '{fullSignal}'");
|
||||
Find.SignalManager.SendSignal(new Signal(successSignal));
|
||||
Log.Message($"GlobalResourceCheck: Sent success signal '{successSignal}'");
|
||||
}
|
||||
|
||||
// 清理这个任务部分
|
||||
DoCleanup();
|
||||
// 完成这个任务部分
|
||||
Complete();
|
||||
}
|
||||
|
||||
private void HandleFailure(string reason = "")
|
||||
@@ -150,12 +173,11 @@ namespace WulaFallenEmpire
|
||||
// 发送失败信号
|
||||
if (!failSignal.NullOrEmpty())
|
||||
{
|
||||
string fullSignal = QuestGenUtility.HardcodedSignalWithQuestID(failSignal);
|
||||
Find.SignalManager.SendSignal(new Signal(fullSignal));
|
||||
Log.Message($"GlobalResourceCheck: Sent fail signal '{fullSignal}' after max retries");
|
||||
Find.SignalManager.SendSignal(new Signal(failSignal));
|
||||
Log.Message($"GlobalResourceCheck: Sent fail signal '{failSignal}' after max retries");
|
||||
}
|
||||
|
||||
DoCleanup();
|
||||
Complete();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -171,13 +193,6 @@ namespace WulaFallenEmpire
|
||||
Log.Message($"GlobalResourceCheck: Scheduled retry #{retryCount + 1} in {retryDelayTicks} ticks for {requiredCount} {resourceDef.defName}. Reason: {reason}");
|
||||
}
|
||||
|
||||
// 使用新名称避免与基类冲突
|
||||
private void DoCleanup()
|
||||
{
|
||||
// 标记为已完成,停止tick更新
|
||||
nextRetryTick = -1;
|
||||
}
|
||||
|
||||
public override void ExposeData()
|
||||
{
|
||||
base.ExposeData();
|
||||
@@ -197,29 +212,39 @@ namespace WulaFallenEmpire
|
||||
Scribe_Values.Look(ref retryCount, "retryCount");
|
||||
}
|
||||
|
||||
public override void AssignDebugData()
|
||||
{
|
||||
base.AssignDebugData();
|
||||
|
||||
resourceDef = ThingDefOf.Steel;
|
||||
requiredCount = 100;
|
||||
retryDelayTicks = 60;
|
||||
successSignal = "TaxPaymentSuccess";
|
||||
failSignal = "TaxPaymentFailed";
|
||||
deductOnSuccess = true;
|
||||
useInputStorage = true;
|
||||
debugInfo = "Debug: Tax Collection Check";
|
||||
}
|
||||
|
||||
public override string DescriptionPart
|
||||
{
|
||||
get
|
||||
{
|
||||
string status = hasSucceeded ? "SUCCEEDED" :
|
||||
hasFailed ? "FAILED" :
|
||||
$"CHECKING (retry #{retryCount}, next in {nextRetryTick - Find.TickManager.TicksGame} ticks)";
|
||||
if (State != QuestPartState.Enabled)
|
||||
return "WULA_TaxCollection.Waiting".Translate(); // 等待激活
|
||||
|
||||
string statusKey = hasSucceeded ? "WULA_TaxCollection.Status.Succeeded" :
|
||||
hasFailed ? "WULA_TaxCollection.Status.Failed" :
|
||||
"";
|
||||
|
||||
return $"Tax Collection: {requiredCount} {resourceDef?.defName ?? "NULL"} - {status}";
|
||||
string statusText;
|
||||
if (hasSucceeded || hasFailed)
|
||||
{
|
||||
statusText = statusKey.Translate();
|
||||
}
|
||||
else
|
||||
{
|
||||
// 只有在调试模式下才显示下一次检查的tick
|
||||
if (Prefs.DevMode)
|
||||
{
|
||||
statusText = statusKey.Translate(retryCount, Math.Max(0, nextRetryTick - Find.TickManager.TicksGame));
|
||||
}
|
||||
else
|
||||
{
|
||||
statusText = statusKey.Translate(retryCount);
|
||||
}
|
||||
}
|
||||
|
||||
// 硬编码状态文本颜色为 #820D13
|
||||
string coloredStatusText = $"<color=#820D13>{statusText}</color>";
|
||||
|
||||
return "WULA_TaxCollection.Status".Translate(requiredCount, resourceDef?.label ?? "NULL", coloredStatusText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user