-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeAutoSegmentLearner.cs
More file actions
217 lines (179 loc) · 8.31 KB
/
Copy pathRuntimeAutoSegmentLearner.cs
File metadata and controls
217 lines (179 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
namespace EntBossHP
{
internal enum RuntimeAutoSegmentDecisionStatus
{
None,
Learned,
Ambiguous
}
internal sealed record RuntimeAutoSegmentMark(bool Started, string MainCounter, int AttemptId);
internal sealed record RuntimeAutoSegmentObservation(bool SuppressAutoCreate);
internal sealed record RuntimeAutoSegmentDecision(
RuntimeAutoSegmentDecisionStatus Status,
string? MainCounter,
string? SegmentCounter,
int Mode,
string Reason);
internal sealed class RuntimeAutoSegmentLearner
{
private const double DefaultWindowSeconds = 1.0;
private const double FloatTolerance = 0.01;
private const int MinimumLearnConfidence = 3;
private readonly double _windowSeconds;
private readonly Dictionary<string, MainState> _states = new(StringComparer.Ordinal);
public RuntimeAutoSegmentLearner(double windowSeconds = DefaultWindowSeconds)
{
_windowSeconds = Math.Max(0.1, windowSeconds);
}
public void Clear()
{
_states.Clear();
}
public void TrackNewMain(string mainCounter, double now)
{
if (string.IsNullOrWhiteSpace(mainCounter)) return;
_states[mainCounter] = new MainState(mainCounter)
{
LastSeenAt = now
};
}
public RuntimeAutoSegmentMark MarkMainHitMin(string mainCounter, double now)
{
if (!_states.TryGetValue(mainCounter, out var state))
{
return new(false, mainCounter, 0);
}
state.AttemptId++;
state.WindowEndsAt = now + _windowSeconds;
state.Candidates.Clear();
return new(true, mainCounter, state.AttemptId);
}
public RuntimeAutoSegmentObservation ObserveCounter(
string counterName,
float currentValue,
float? previousValue,
float? maxValue,
double now,
Func<string, string, bool> namesMatch)
{
if (string.IsNullOrWhiteSpace(counterName)) return new(false);
var mode = TryResolveMode(currentValue, previousValue, maxValue);
if (mode is null) return new(false);
var confidence = GetCandidateConfidence(counterName, mode.Value, previousValue, maxValue);
var suppress = false;
foreach (var state in _states.Values)
{
if (state.AttemptId <= 0) continue;
if (now > state.WindowEndsAt) continue;
if (namesMatch(counterName, state.MainCounter)) continue;
state.Candidates[(counterName, mode.Value)] = new SegmentCandidate(counterName, mode.Value, confidence);
suppress = true;
}
return new(suppress);
}
public RuntimeAutoSegmentDecision Finalize(string mainCounter, int attemptId, double now)
{
if (!_states.TryGetValue(mainCounter, out var state) || state.AttemptId != attemptId)
{
return new(RuntimeAutoSegmentDecisionStatus.None, mainCounter, null, 0, "not tracked");
}
var candidates = state.Candidates.Values
.DistinctBy(candidate => (candidate.SegmentCounter, candidate.Mode))
.ToList();
var finalizedAfterWindow = now > state.WindowEndsAt;
state.Candidates.Clear();
state.WindowEndsAt = 0;
var confidentCandidates = candidates
.Where(candidate => candidate.Confidence >= MinimumLearnConfidence)
.ToList();
if (confidentCandidates.Count == 0)
{
return new(RuntimeAutoSegmentDecisionStatus.None, mainCounter, null, 0, "no confident segment candidates");
}
var bestConfidence = confidentCandidates.Max(candidate => candidate.Confidence);
var bestCandidates = confidentCandidates
.Where(candidate => candidate.Confidence == bestConfidence)
.ToList();
if (bestCandidates.Count > 1)
{
return new(RuntimeAutoSegmentDecisionStatus.Ambiguous, mainCounter, null, 0, "multiple segment candidates");
}
var learned = bestCandidates[0];
_states.Remove(mainCounter);
return new(
RuntimeAutoSegmentDecisionStatus.Learned,
mainCounter,
learned.SegmentCounter,
learned.Mode,
finalizedAfterWindow ? "learned after window" : "learned");
}
private static int? TryResolveMode(float currentValue, float? previousValue, float? maxValue)
{
if (currentValue < 0) return null;
if (previousValue is { } previous)
{
var delta = currentValue - previous;
if (Math.Abs(delta + 1) <= FloatTolerance) return 1;
if (Math.Abs(delta - 1) <= FloatTolerance) return 2;
return null;
}
if (maxValue is not { } max || max <= 0 || max > 64) return null;
var looksLikeSubtract = Math.Abs(max - currentValue - 1) <= FloatTolerance;
var looksLikeAdd = Math.Abs(currentValue - 1) <= FloatTolerance;
if (looksLikeSubtract == looksLikeAdd) return null;
return looksLikeSubtract ? 1 : 2;
}
private static int GetCandidateConfidence(string counterName, int mode, float? previousValue, float? maxValue)
{
var name = counterName.ToLowerInvariant();
var confidence = 0;
if (maxValue is > 0 and <= 64) confidence++;
if (previousValue.HasValue) confidence++;
if (mode == 2) confidence++;
if (LooksLikeStrongSegmentCounterName(name)) confidence += 3;
else if (name.Contains("counter", StringComparison.Ordinal)) confidence++;
if (LooksLikeVisualHpBarCounterName(name)) confidence -= 2;
if (LooksLikeAuxiliaryCounterName(name)) confidence -= 2;
return confidence;
}
private static bool LooksLikeStrongSegmentCounterName(string name)
{
return name.Contains("segment", StringComparison.Ordinal)
|| name.Contains("phase", StringComparison.Ordinal)
|| name.Contains("iteration", StringComparison.Ordinal)
|| name.Contains("healthcount", StringComparison.Ordinal)
|| name.Contains("hpcount", StringComparison.Ordinal)
|| name.Contains("mathcounter", StringComparison.Ordinal);
}
private static bool LooksLikeVisualHpBarCounterName(string name)
{
return name.EndsWith("hp", StringComparison.Ordinal)
|| name.Contains("hpbar", StringComparison.Ordinal)
|| name.Contains("hp_bar", StringComparison.Ordinal);
}
private static bool LooksLikeAuxiliaryCounterName(string name)
{
return name.Contains("attack", StringComparison.Ordinal)
|| name.Contains("button", StringComparison.Ordinal)
|| name.Contains("crystal", StringComparison.Ordinal)
|| name.Contains("door", StringComparison.Ordinal)
|| name.Contains("item", StringComparison.Ordinal)
|| name.Contains("laser", StringComparison.Ordinal)
|| name.Contains("skill", StringComparison.Ordinal)
|| name.Contains("summon", StringComparison.Ordinal)
|| name.Contains("text", StringComparison.Ordinal)
|| name.Contains("timer", StringComparison.Ordinal)
|| name.Contains("track", StringComparison.Ordinal)
|| name.Contains("turn", StringComparison.Ordinal);
}
private sealed class MainState(string mainCounter)
{
public string MainCounter { get; } = mainCounter;
public double LastSeenAt { get; init; }
public int AttemptId { get; set; }
public double WindowEndsAt { get; set; }
public Dictionary<(string SegmentCounter, int Mode), SegmentCandidate> Candidates { get; } = new();
}
private sealed record SegmentCandidate(string SegmentCounter, int Mode, int Confidence);
}
}