qstbak/Questionable/Questionable.Controller.Utils/QuestWorkUtils.cs
2025-10-09 07:47:19 +10:00

98 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
{
public static bool HasCompletionFlags(IList<QuestWorkValue?> completionQuestVariablesFlags)
{
if (completionQuestVariablesFlags.Count == 6)
{
return completionQuestVariablesFlags.Any((QuestWorkValue x) => x != null && (x.High != 0 || x.Low != 0));
}
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);
byte? high = questWorkValue.High;
byte? low = questWorkValue.Low;
byte valueOrDefault = high.GetValueOrDefault();
byte valueOrDefault2 = low.GetValueOrDefault();
switch (mode)
{
case EQuestWorkMode.Exact:
if (high.HasValue && b != valueOrDefault)
{
return false;
}
if (low.HasValue && b2 != valueOrDefault2)
{
return false;
}
break;
case EQuestWorkMode.Bitwise:
if (high.HasValue && (b & high) != valueOrDefault)
{
return false;
}
if (low.HasValue && (b2 & low) != valueOrDefault2)
{
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.LogInformation("Should execute step");
return false;
}
}