Anti-cheat evidence (2026-10-16)
================================

Purpose
-------
Server-side flags could already say "this player looks wrong". This layer
records HOW each hit happened so staff can say what kind of cheat it looks
like, review the shooter's own recording, and compare the player with the
rest of the population before acting. Nothing in it kicks or bans by itself.

Pieces
------
scripts/mp/ac_evidence.gsc  game script; one record per bullet hit between players
scripts/mp/anti_cheat.gsc   the older behavioural flags (!acflags, !acclear, !acspeed);
                            ac_evidence raises its two live flags through it
ranked_lobby/anticheat.py   record parser, validator, per-player summary and hint
ranked_lobby/bridge.py      ships records to the lobby; holds and uploads demos
ranked_lobby/service.py     POST /v1/evidence (JSON) and /v1/evidence/demo (binary),
                            bridge token, stores rows under the shooter's account
zeal_accounts/anticheat.py  anticheat_events / anticheat_demos tables, retention,
                            staff views, demo download
zeal_accounts/static/admin.* staff dashboard: Anti-cheat column, per-player view,
                            per-hit angle trace, demo downloads
tools/anticheat/report.py   offline report and population baseline

How a record is made
--------------------
Every server frame (50 ms) the script stores each player's view angles, eye
position and feet position in a 20-slot ring with a shared index, so every
player's history is aligned in time. loadout_and_reflector's damage callback
calls level.fn_ac_damage first; that function only copies values and starts a
thread, so a script error can never block damage. 160 ms later the thread has
the 17 samples taken before the shot (850 ms) and the 3 after it.

For each of those frames it replays two sight traces from the shooter's eye to
the victim's eye and chest (world geometry only) and measures the angle
between the crosshair and the victim. From that:

  snap_in     degrees between the last sample and the shot
  snap2       change in angular velocity into the shot (IW4MAdmin's "snap")
  ret         degrees between the sample before and the sample after the shot
  offset      degrees between the shot and the midpoint of before/after
  max_vel     fastest frame-to-frame turn in the window (a lock-on aimbot shows a
              big turn followed by a dead-still crosshair; snap_in alone misses it)
  vis_hit     victim visible at the shot (FMJ wall shots are legitimately 0)
  vis_run     frames the victim had been continuously visible (17 = whole window)
  preaim      frames with the crosshair within 3 deg of a HIDDEN victim
  preaim_min  closest the crosshair came to a hidden victim
  button_ms   ms since the shooter's last +attack press (-1 = none seen)
  ads         aim-down-sight fraction, distance, hit location, damage, kill
  pitch/yaw/xhair lists and a visibility string for the 20 frames

Reading the patterns (none of these is proof)
  aimbot       large snap_in/snap2 with ret also large; the view really moved
  silent aim   large snap_in with ret about 0; the view jumped for one frame
               and came straight back
  triggerbot   vis_run 0-1 on most hits; the shot lands within a frame of the
               target appearing, often with ADS and a human-looking snap
  wallhack     preaim on many hits: the crosshair tracked the victim through
               walls before they appeared; vis_hit 0 with FMJ is normal, long
               pre-aim on a victim who never appeared is not
  software fire button_ms -1 or > 1500 repeatedly: the gun fired without the key
  humanised cheats hide in these numbers; the demo and the population baseline
               are what staff compare against

Live flags (WARN through anti_cheat.gsc, visible to in-game admins, !acflags)
  BUTTON       3 hits in a map with no +attack press in the last 1.5 s
  SNAP_RETURN  2 hits with snap_in >= 25 deg, ret <= 2 deg, distance >= 200
Both skip test clients.

BUTTON was a false alarm until 2026-11-06. The +attack notify reaches the
script in the frame after the hit, so reading it at the hit gave the time since
the previous shot: 65% of all hits by every player looked like a missing press,
and all 71 flags up to then were this bug. _ev_finish now reads it 160 ms later.
ranked_lobby/anticheat.py BUTTON_RELIABLE_FROM marks when that went live; older
BUTTON rows stay stored but show as false alarms and do not count.

Review flags (set by the bridge, no in-game warning; they exist to hold a demo)
  REVIEW_WALLS    3 of the shooter's last 20 hits had 500 ms or more of crosshair
                  on the hidden target (preaim >= 10)
  REVIEW_INSTANT  4 of the last 20 hits within 50 ms of the target appearing
Measured on the first 3276 live hits (2.2 days, 11 players): each fired 9 times.
A rolling window is used because a level can run for hours, so per-map counts
trip for everyone. A trigger clears the window.

Record format (one line, written to scriptdata/ranked_service/evidence/<sys>_<ms>_<slot>.txt)
  AC1|sys|level_ms|phase|mode|attacker_guid|attacker_name|victim_guid|victim_name|
  weapon|hitloc|mod|damage|kill|ads*10|distance|button_ms|snap_in*10|snap2*10|
  ret*10|offset*10|max_vel*10|vis_hit|vis_run|preaim|preaim_min*10|flag|
  hit_pitch*10|hit_yaw*10|n|pitch,...|yaw,...|xhair,...|vis
Angles are tenths of a degree as signed integers (metrics such as snap are non-negative, -1 = not available); "-" marks a frame without data;
n is 20 or 0 (a record that would exceed the 1024-character script string
limit is written without its lists). The script also caps itself at 500
records per shooter per map.

Transport and storage
---------------------
The bridge reads the record files every pass, posts up to 50 per request to
POST /v1/evidence with its server token, and deletes them once stored. The
lobby validates every field again, maps the shooter's GUID to the account
(linked PCs included) and writes anticheat_events through the accounts store.
Failures back off 30 s and keep the files.

Demos: the game servers record every real player's own point of view
(sv_demoAutoRecord 1, serverdemos/ under the instance's userraw, kept to
sv_demosKeep files). When a record carries a flag, the bridge notes the
shooter's current demo (auto_<gametype>_<map>_<label>_<start>.dm_13, newest
start before the hit) and uploads it once the recording has closed (its
.dm_13.json sidecar exists and the file has been quiet for 10 s). Uploads go
to POST /v1/evidence/demo as raw bytes with metadata in X-Zeal-Demo, at most
64 MB, one per bridge pass. The lobby stores them under evidence_dir/demos/
<server>/ and records anticheat_demos. Staff download them from the dashboard
(GET /staff/demo?id=, staff cookie required) as a zip with the .dm_13, the
.dm_13.json Theater needs to list it, and a text list of every hit in the
recording with its time. Put the first two in userraw/demos and open Theater.

Retention: 90 days, 20,000 records per account, kept while the account holds a
permanent Ranked restriction (like IP and device evidence). Demo files are
removed with their rows. /device-privacy discloses the collection.

Staff use
---------
Dashboard: the Players table shows hits / flagged / demos per account. Click
those numbers (no reason needed; the lookup is audited) for: a summary with a
one-line reading, why the player was flagged in plain words with a link to the
demo and the time in it, how they compare with every other player with 30 or
more hits, held demos, and the last 200 hits. Explain shows one hit in plain
words with its 20-frame angle and visibility table.
In game: !acflags still shows the live flags; the two new ones name the
pattern and the distance.

Calibration
-----------
This is a sniper-only game: quickscope flicks are fast, so numbers borrowed
from IW4MAdmin (tuned on normal MW2) must not be used as-is. After a couple of
weeks of play run

  python3 tools/anticheat/report.py --database /var/lib/zeal/accounts.sqlite3 --baseline
  python3 tools/anticheat/report.py --database ... --player <name>

The baseline prints the population's median/90th/99th for every summary
number. Only move the live-flag thresholds, or add new ones, against that
table, and prefer flags that need several hits over any single-hit rule. The
first review of the baseline is due 2026-10-30.

Deployment
----------
MS-02: deploy zeal_accounts (schema) and restart zeal-accounts first; set
"evidence_dir": "/var/lib/zeal/evidence" in /etc/zeal/accounts.json and
/etc/zeal/lobby.json; create the directory (zeallobby:zealcentral, 2770);
restart iw4x-qa-lobby when no match is running (lobby restarts drop parties).
Game hosts: sync scripts/mp and ranked_lobby/bridge.py; create
userraw/serverdemos in every instance (game user, group zi-<id>, 2770, default
ACL rwx for user and group so the bridge can read); copy server_configs/
server.cfg (sv_demoAutoRecord 1) into each instance's userraw; restart
zeal-ranked-game@ and zeal-ranked-lobby-bridge@ only while presence.txt ends
END|0. The bridge creates ranked_service/evidence itself. Settings:
ac_evidence 0 turns recording off without a restart; ac_evidence_bots 1 is
for local QA with test clients only.

Limits
------
Everything is sampled at the server frame rate (20 Hz). A humanised aimbot
that moves over several frames looks like a fast human here; the demo review
and the population comparison carry that case. Wallhacks that only change what
the cheater sees leave traces only through pre-aim. Lag switches and packet
tricks are not measured. Client-side integrity checks in the Zeal client
(hooked input/render functions, unbacked executable memory, patched code) are
the planned next layer; they need a client release and a /device-privacy
update, and the systematic review rates them as bypassable, so they add
evidence rather than replace this.

References
----------
IW4MAdmin anti-cheat (MIT): Plugins/Stats/Cheat/Detection.cs and
GameFiles/IW4x/userraw/scripts/_customcallbacks.gsc, RaidMax/IW4M-Admin.
YAACS, server-side aimbot detection with sequence models: arXiv 2607.04336.
Systematic review of technical anti-cheat defenses: arXiv 2512.21377.
