threat better

This commit is contained in:
Boki 2026-03-07 12:27:25 -05:00
parent 05bbcb244f
commit 703cfbfdee
12 changed files with 581 additions and 228 deletions

View file

@ -14,6 +14,8 @@ public sealed class MovementKeyTracker
private bool _wHeld, _aHeld, _sHeld, _dHeld;
private long _wDownAt, _aDownAt, _sDownAt, _dDownAt;
private int _wMinHold, _aMinHold, _sMinHold, _dMinHold;
private long _wUpAt, _aUpAt, _sUpAt, _dUpAt;
private int _wRepress, _aRepress, _sRepress, _dRepress;
private Vector2? _lastPlayerPos;
private static readonly Random Rng = new();
@ -55,10 +57,10 @@ public sealed class MovementKeyTracker
}
var now = Environment.TickCount64;
SetKey(input, ScanCodes.W, ref _wHeld, ref _wDownAt, ref _wMinHold, wantW, now, _lastPlayerPos);
SetKey(input, ScanCodes.A, ref _aHeld, ref _aDownAt, ref _aMinHold, wantA, now, _lastPlayerPos);
SetKey(input, ScanCodes.S, ref _sHeld, ref _sDownAt, ref _sMinHold, wantS, now, _lastPlayerPos);
SetKey(input, ScanCodes.D, ref _dHeld, ref _dDownAt, ref _dMinHold, wantD, now, _lastPlayerPos);
SetKey(input, ScanCodes.W, ref _wHeld, ref _wDownAt, ref _wMinHold, ref _wUpAt, ref _wRepress, wantW, now, _lastPlayerPos);
SetKey(input, ScanCodes.A, ref _aHeld, ref _aDownAt, ref _aMinHold, ref _aUpAt, ref _aRepress, wantA, now, _lastPlayerPos);
SetKey(input, ScanCodes.S, ref _sHeld, ref _sDownAt, ref _sMinHold, ref _sUpAt, ref _sRepress, wantS, now, _lastPlayerPos);
SetKey(input, ScanCodes.D, ref _dHeld, ref _dDownAt, ref _dMinHold, ref _dUpAt, ref _dRepress, wantD, now, _lastPlayerPos);
}
/// <summary>
@ -78,10 +80,14 @@ public sealed class MovementKeyTracker
};
private static void SetKey(IInputController input, ushort scanCode,
ref bool held, ref long downAt, ref int minHold, bool want, long now, Vector2? pos)
ref bool held, ref long downAt, ref int minHold,
ref long upAt, ref int repressDelay, bool want, long now, Vector2? pos)
{
if (want && !held)
{
// Enforce re-press cooldown after release
if (now - upAt < repressDelay) return;
input.KeyDown(scanCode);
held = true;
downAt = now;
@ -96,8 +102,11 @@ public sealed class MovementKeyTracker
{
var elapsed = now - downAt;
if (elapsed < minHold) return; // enforce minimum hold
input.KeyUp(scanCode);
held = false;
upAt = now;
repressDelay = RepressMs();
if (pos.HasValue)
Log.Information("[WASD] {Key} UP (held={Elapsed}ms, min={MinHold}ms) pos=({X:F0},{Y:F0})",
KeyName(scanCode), elapsed, minHold, pos.Value.X, pos.Value.Y);
@ -119,4 +128,18 @@ public sealed class MovementKeyTracker
var g = u * Math.Sqrt(-2.0 * Math.Log(s) / s);
return Math.Clamp((int)Math.Round(55.0 + g * 6.0), 44, 76);
}
/// <summary>Gaussian re-press cooldown peaked at 40ms, range [25, 65].</summary>
private static int RepressMs()
{
double u, v, s;
do
{
u = Rng.NextDouble() * 2.0 - 1.0;
v = Rng.NextDouble() * 2.0 - 1.0;
s = u * u + v * v;
} while (s >= 1.0 || s == 0.0);
var g = u * Math.Sqrt(-2.0 * Math.Log(s) / s);
return Math.Clamp((int)Math.Round(40.0 + g * 8.0), 25, 65);
}
}