qstbak/Questionable/Questionable.Controller.Utils/QuestWorkUtils.cs
2026-08-17 20:29:32 +10:00

96 lines
2.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Logging;
using Questionable.Model;
using Questionable.Model.Questing;
namespace Questionable.Controller.Utils;
internal static class QuestWorkUtils
{
private const int QuestVariableCount = 6;
public static bool HasCompletionFlags(IList<QuestWorkValue?> completionQuestVariablesFlags)
{
if (completionQuestVariablesFlags.Count == 6)
{
return completionQuestVariablesFlags.Any((QuestWorkValue x) => x != null && (x.High.HasValue || x.Low.HasValue));
}
return false;
}
public static bool MatchesQuestWork(IList<QuestWorkValue?> completionQuestVariablesFlags, QuestProgressInfo questProgressInfo)
{
if (!HasCompletionFlags(completionQuestVariablesFlags) || questProgressInfo.Variables.Count != 6)
{
return false;
}
for (int i = 0; i < questProgressInfo.Variables.Count; i++)
{
QuestWorkValue questWorkValue = completionQuestVariablesFlags[i];
if (questWorkValue == null)
{
continue;
}
EQuestWorkMode mode = questWorkValue.Mode;
byte b = (byte)(questProgressInfo.Variables[i] >> 4);
byte b2 = (byte)(questProgressInfo.Variables[i] & 0xF);
switch (mode)
{
case EQuestWorkMode.Exact:
if (questWorkValue.High.HasValue && b != questWorkValue.High.Value)
{
return false;
}
if (questWorkValue.Low.HasValue && b2 != questWorkValue.Low.Value)
{
return false;
}
break;
case EQuestWorkMode.Bitwise:
if (questWorkValue.High.HasValue && (b & questWorkValue.High.Value) != questWorkValue.High.Value)
{
return false;
}
if (questWorkValue.Low.HasValue && (b2 & questWorkValue.Low.Value) != questWorkValue.Low.Value)
{
return false;
}
break;
default:
throw new InvalidOperationException($"Unknown qw mode {mode}");
}
}
return true;
}
public static bool MatchesRequiredQuestWorkConfig(List<List<QuestWorkValue>?> requiredQuestVariables, QuestProgressInfo questWork, ILogger logger)
{
if (requiredQuestVariables.Count != 6 || requiredQuestVariables.All((List<QuestWorkValue> x) => x == null || x.Count == 0))
{
logger.LogDebug("No RequiredQW defined");
return true;
}
for (int num = 0; num < 6; num++)
{
if (requiredQuestVariables[num] == null)
{
logger.LogDebug("No RequiredQW {Index} defined", num);
continue;
}
byte b = (byte)(questWork.Variables[num] >> 4);
byte b2 = (byte)(questWork.Variables[num] & 0xF);
foreach (QuestWorkValue item in requiredQuestVariables[num])
{
logger.LogDebug("H: {ExpectedHigh} - {ActualHigh}, L: {ExpectedLow} - {ActualLow}", item.High, b, item.Low, b2);
if ((!item.High.HasValue || item.High == b) && (!item.Low.HasValue || item.Low == b2))
{
return true;
}
}
}
logger.LogDebug("Required quest variables not matched");
return false;
}
}