source_sdk_2013 0.1.0-alpha.6

Higher-level bindings for the Source SDK.
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
//! TF2's scoreboard, changed through the game's own scoring state.
//!
//! Everything here writes the state the game itself keeps and computes the
//! scoreboard from. The game, clients, and every other reader then agree on
//! the new values, and nothing has to be restored or kept applied: a change
//! lasts until the game itself resets or replaces the value, as described
//! [below](#resets).
//!
//! # Where the scoreboard comes from
//!
//! TF2 clients build the scoreboard from two kinds of networked entities:
//!
//! - `tf_player_manager` (`CTFPlayerResource`), which holds an array per
//!   column with an element per player slot. For each connected player, the
//!   game recomputes the elements from its own state at every think, ten
//!   times a second (`player_resource.cpp:94-101`); the damage, healing,
//!   support and credit columns at most once a second
//!   (`tf_player_resource.cpp:218-230`).
//! - `tf_team` (`CTFTeam`), whose score and flag captures the game only
//!   assigns when they change.
//!
//! The game stores no player's score. It keeps statistics per player, which
//! `sdk_raw`'s [`GameStats`] finds: for the session, which the scoreboard's
//! Score is computed from, and for the current round, which the round's score
//! and its MVPs are computed from. At each think, the player resource scores
//! the session's statistics with `CTFGameRules::CalcPlayerScore`, and copies
//! both scores and some of the statistics into the player's local scoring data
//! (`tf_player_resource.cpp:200-258`). [`PlayerScore`] reads and changes those
//! statistics, and scores them with the game's own function.
//!
//! # Points
//!
//! [`PlayerScore::add_points`] and [`PlayerScore::set_total`] change a
//! player's Score by exactly the amount asked, through
//! [`Stat::KillsRuneCarrier`]: the game scores it one point each, in every
//! mode and whatever the player's attributes, and shows or uploads it nowhere
//! else. The game also counts a kill of any player carrying a Mannpower rune
//! there, so [`PlayerScore::points`] includes those. The game never shows a
//! Score below 0: a total taken below it shows 0, and the deficit absorbs the
//! points the player earns next. [`Applied`] tells what clients see change.
//!
//! The other [`Stat`]s change the Score by their own weights, some of them
//! divided and floored (`tf_gamerules.cpp:17031-17093`), and feed the match
//! summaries and medals the game sends at the end of a competitive match.
//!
//! # What the game reports
//!
//! At each think, the player resource first copies each connected player's
//! session and round scores into the player's scoring data
//! (`m_Shared.m_ScoreData.m_iPoints` and `m_RoundScoreData.m_iPoints`). When
//! that raises the session's points, the game fires `player_score_changed`,
//! which the war tracker and match experience listen to
//! (`tf_player_shared.cpp:14574-14620`); a drop fires nothing. Then it
//! computes the player's Score again and compares it with the one it last
//! sent, the player's element of the resource's `m_iTotalScore`, and stores
//! the new one. Only that comparison reports anything: the game reports the
//! difference to the item servers as Strange "Points Scored" progress of the
//! player's cosmetics, or in Mann vs. Machine to its statistics
//! (`tf_player_resource.cpp:200-258`). A drop is reported as negative
//! progress.
//!
//! Changing statistics with [`PlayerScore`] reports none of this when it is
//! made: it also adds the change in each score to the values the game compares
//! against, and marks them changed for clients, which receive the new values at
//! once. Points the game awards are still reported as usual, including those it
//! awards between a change and its next think. The plugin's change stays in
//! those values, though, so when one of the game's [resets](#resets) later
//! drops the player's Score, the game reports the whole drop, including the
//! plugin's points, as negative progress. Vanilla would report only the points
//! the game awarded.
//!
//! # Reporting nothing
//!
//! [`PlayerScore::rebase`] sets the values the game compares against to the
//! scores themselves, discarding every difference the game has not sent yet.
//! Called at each of these points, it leaves the game nothing to report:
//!
//! - From a [`GameEventId::PlayerScoreChanged`] listener, which covers every
//!   point the game awards. The game fires the event from the think, after
//!   writing the session's points and before computing and comparing the
//!   Score, with nothing changing statistics in between, and the engine calls
//!   each listener before the event returns.
//! - From a [`GameEventId::ScorestatsAccumulatedReset`] listener. The game
//!   fires the event right after resetting every player's scores for
//!   `mp_restartgame`, a tournament restart, or the end of the wait for
//!   players, before any think (`teamplayroundbased_gamerules.cpp:3235-3282`).
//! - From a [`GameEventId::PlayerActivate`] listener. The engine creates,
//!   spawns, and activates a player in one call, before any think. The game
//!   never clears a slot's element of `m_iTotalScore` when its player leaves,
//!   so a newcomer's first think compares against whoever last had the slot.
//!   A newcomer wears no items yet then, so vanilla's report of that
//!   difference reaches no Strange counter in practice; the rebase covers a
//!   player who spawns with items before that think.
//! - For every player when the plugin starts, unpauses, or a level starts,
//!   discarding what the game has not sent since its last think.
//!
//! The game reports as usual when nothing rebases, as while the plugin is
//! paused, and in these cases:
//!
//! - After the plugin stops rebasing, a reset reports the whole drop of each
//!   player's Score, including the points the game awarded while it was
//!   rebased and never reported, as negative progress. Disconnecting and
//!   changing maps report nothing.
//! - [`reset_scores`] fires no `scorestats_accumulated_reset`, so the next
//!   think reports the drop unless [`PlayerScore::rebase`] follows it.
//! - The Score can change while no statistic does, and without an event: when
//!   the player's `scoreboard_minigame` attribute comes or goes, when the game
//!   switches to Mann vs. Machine's or Mannpower's scoring, when healing passes
//!   10,000,000, beyond which the game stops scoring it, or when a sum
//!   overflows. The next think reports the difference.
//! - A player resource created while a level runs starts every element of
//!   `m_iTotalScore` at 0, and so reports each player's whole Score at its
//!   first think.
//! - Mann vs. Machine's population manager resets players' scores without an
//!   event.
//! - Another plugin's `player_score_changed` listener that runs after the
//!   rebasing one and changes statistics leaves its change to be reported.
//!
//! # Resets
//!
//! The game resets a player's statistics, and so their Score, when they
//! connect, when they disconnect, when `mp_restartgame`, a tournament restart,
//! or the end of the wait for players resets every player's scores (after
//! which it fires `scorestats_accumulated_reset`), when [`reset_scores`] resets
//! them, which fires no event, and when Mann vs. Machine's population manager
//! resets them, which fires none either. A map change reconnects everyone. Changing team
//! resets nothing, and a team scramble only the teams' scores. The round's
//! statistics reset with every round (`stats_resetround`) and Mann vs.
//! Machine wave, leaving the session's alone. Frags and deaths reset with the
//! scores.
//!
//! A plugin that keeps its own points across these resets applies them again
//! after them: the session's after [`GameEventId::PlayerActivate`],
//! [`GameEventId::ScorestatsAccumulatedReset`], and its own [`reset_scores`],
//! but not after [`GameEventId::StatsResetround`], which leaves the session's
//! statistics, and so points already applied, in place.
//!
//! # Holding a score
//!
//! To keep a player's Score at a value of the plugin's choosing while they
//! play, listen to [`GameEventId::PlayerScoreChanged`] and call
//! [`PlayerScore::set_total`] and then [`PlayerScore::rebase`] from the
//! listener. The game fires the event from the player resource's think, after
//! updating the player's scoring data but before computing the Score it
//! sends, so the Score clients receive never changes, and the rebase leaves
//! the game nothing to report. `set_total` alone keeps the difference the game
//! has not sent, so the game would still report the points it awarded.
//!
//! The event only fires when the session's points rise, so the listener sees
//! no decrease or reset: set the held value and rebase again after the resets
//! above. Clients then receive the held Score at once.
//! A script's `ResetScores` on the player clears their scoring data but not
//! their statistics, so the next think fires the event with their whole
//! Score as its increase.
//!
//! # Limits
//!
//! The scoreboard's Score is networked as an unsigned 32-bit varint, the
//! player's own points readout with 10 bits, so it wraps above 1023, and
//! frags and deaths with 12 signed bits, -2048 to 2047 in the SDK.
//!
//! # Unverified
//!
//! The game's functions and statistics are found and checked in TF2's 64-bit
//! Windows `server.dll` as [`GameStats`] describes. On Linux GNU servers they
//! are found by symbols inferred from the SDK's source, which have not been
//! checked against a retail build. The order of the player resource's think
//! and of `player_score_changed`'s listeners was read from the 64-bit Windows
//! `server.dll` and `engine.dll`; on Linux it is inferred from the SDK's
//! source. No change described here has yet been observed on a live server:
//! that clients receive it at once, that items' Strange counts stay as they
//! were, with or without [`PlayerScore::rebase`], and the client-side effects
//! of [`PlayerScore::set_killstreak`].
//!
//! [`GameEventId::PlayerActivate`]: crate::tf2::game_events::GameEventId::PlayerActivate
//! [`GameEventId::PlayerScoreChanged`]: crate::tf2::game_events::GameEventId::PlayerScoreChanged
//! [`GameEventId::ScorestatsAccumulatedReset`]: crate::tf2::game_events::GameEventId::ScorestatsAccumulatedReset
//! [`GameEventId::StatsResetround`]: crate::tf2::game_events::GameEventId::StatsResetround

#[cfg(test)]
#[path = "../../tests/tf2/scoreboard.rs"]
mod tests;

use crate::NotThreadSafe;
use crate::datatables::{NetPropError, PropFlags, PropKind, SendProp, ServerClass, Storage};
use crate::edicts::Edict;
use crate::entities::{Entity, EntityHandle};
use crate::interfaces::{ServerGameDll, ServerTools, ValveEngine};
use crate::{Game, InterfaceError, Server};
use sdk_raw::edicts::MAX_CHANGE_OFFSETS;

use sdk_raw::tf2::scoreboard::{
	ELEMENT_SIZE, GameStats, GameStatsError, KILL_STREAK, MAX_PLAYERS_ARRAY_SAFE, PlayerStats,
	RoundStats, TF_TEAM_BLUE, TF_TEAM_RED, stat,
};

use sdk_raw::vcall;
use std::ffi::{CStr, c_int};
use std::mem::offset_of;
use std::ops::RangeInclusive;
use std::ptr::NonNull;

/// An exclusive bound on the datamap offsets trusted for a player's fields.
const MAX_FIELD_OFFSET: usize = 1 << 16;

/// The class name of TF2's `CTFPlayerResource` (`tf_player_resource.cpp:52`).
const RESOURCE_CLASS_NAME: &CStr = c"tf_player_manager";

/// The class name of TF2's `CTFTeam` (`tf_team.cpp:61`).
const TEAM_CLASS_NAME: &CStr = c"tf_team";

/// Which of a player's statistics a change applies to, besides the session's.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Adjust {
	/// Whether the current round's statistics change too, and with them the
	/// round's score, which the game picks the round's MVPs by, and the
	/// player's round summary. `true` by default.
	///
	/// The game resets the round's statistics every round, but not the
	/// session's.
	pub round: bool,
}

impl Default for Adjust {
	fn default() -> Self {
		Self { round: true }
	}
}

/// How much a change moved the scores clients are shown.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Applied {
	/// The change in the round's score, 0 unless [`Adjust::round`].
	pub round_shown: i32,

	/// The change in the session's score, the scoreboard's Score. It differs
	/// from the change asked for where the game's clamping at 0 hides part of
	/// it, or a statistic's weight is not 1.
	pub shown: i32,
}

/// Where a player column's array lives in the player resource, as resolved
/// from the resource's server class.
#[derive(Debug, Clone, Copy)]
struct ArrayLayout {
	/// Bytes from the start of the entity to the first element.
	first: usize,

	/// The number of elements, each [`ELEMENT_SIZE`] bytes after the last.
	len: usize,

	/// The values an element can be networked with.
	range: NetRange,
}

impl ArrayLayout {
	/// Resolves the array `name` in `class`, checking that it is a contiguous
	/// array of `int`s that holds at least player slot 1.
	fn resolve(
		dll: ServerGameDll<'_>,
		class: ServerClass<'_>,
		name: &'static CStr,
	) -> Result<Self, ScoreError> {
		let display_name = name.to_str().unwrap_or_default();
		let unexpected = || ScoreError::UnexpectedLayout {
			name: display_name,
			needed: 2,
		};

		let array = dll.net_prop(class, name)?;
		let len = array
			.element_count()
			.filter(|&len| len >= 2)
			.ok_or_else(unexpected)?;
		let first = array.element(0)?;

		for index in 0..len {
			let element = array.element(index)?;
			let expected = index
				.checked_mul(ELEMENT_SIZE)
				.and_then(|bytes| first.offset().checked_add(bytes));

			if element.prop().kind() != PropKind::Int
				|| !element.storage().is_compatible(Storage::I32)
				|| Some(element.offset()) != expected
			{
				return Err(unexpected());
			}
		}

		Ok(Self {
			first: first.offset(),
			len,
			range: encodable_range(first.prop()),
		})
	}

	/// Bytes from the start of the entity to the element for `slot`, or an
	/// error naming the array `name` if it does not reach it.
	fn offset(self, name: &'static str, slot: usize) -> Result<usize, ScoreError> {
		if slot < self.len {
			// `resolve` checked that every element's offset is this.
			Ok(self.first + slot * ELEMENT_SIZE)
		} else {
			Err(ScoreError::UnexpectedLayout {
				name,
				needed: slot.saturating_add(1),
			})
		}
	}
}

/// The variables of one entity marked changed during one write, sent to the
/// engine's change tracking together.
#[derive(Debug, Default)]
struct Changes {
	/// The offsets of the variables to mark changed, without duplicates.
	offsets: Vec<usize>,
}

impl Changes {
	/// Tells the engine about the changes to `edict`'s entity.
	///
	/// More offsets than the engine records per frame, or one it cannot
	/// record, mark the whole entity changed at once, as recording them one by
	/// one would end up doing.
	fn flush(self, engine: ValveEngine<'_>, edict: Edict<'_>) {
		if self.offsets.is_empty() {
			return;
		}

		let offsets: Option<Vec<u16>> = self
			.offsets
			.iter()
			.map(|&offset| u16::try_from(offset).ok())
			.collect();

		match offsets {
			Some(offsets) if offsets.len() <= usize::from(MAX_CHANGE_OFFSETS) => {
				for offset in offsets {
					edict.state_changed(engine, offset);
				}
			}

			_ => edict.full_state_changed(engine),
		}
	}

	/// Marks the variable at `offset` changed.
	fn mark(&mut self, offset: usize) {
		if !self.offsets.contains(&offset) {
			self.offsets.push(offset);
		}
	}
}

/// The interfaces the scoreboard uses within one callback.
#[derive(Debug, Clone, Copy)]
struct Context<'s> {
	dll: ServerGameDll<'s>,
	engine: ValveEngine<'s>,
	tools: ServerTools<'s>,
}

impl<'s> Context<'s> {
	fn new(server: Server<'s>) -> Result<Self, InterfaceError> {
		Ok(Self {
			dll: server.server_game_dll()?,
			engine: server.valve_engine()?,
			tools: server.server_tools()?,
		})
	}
}

/// A count `CBasePlayer` keeps twice: as a datamap field the game counts
/// with, and in the player state the engine reads.
#[derive(Debug, Clone, Copy)]
struct Count<'s> {
	/// The datamap field, such as `m_iFrags`.
	field: IntField<'s>,

	/// The player state's copy, such as `pl.frags`.
	mirror: IntField<'s>,
}

impl Count<'_> {
	/// Writes `value` to the field, and to the player state's copy if that
	/// still agreed with the field. Returns whether it did.
	fn set(self, value: i32) -> bool {
		let agreed = self.mirror.read() == self.field.read();

		self.field.write(value);

		if agreed {
			self.mirror.write(value);
		}

		agreed
	}
}

/// An `int` member of a live entity, at an offset resolved for the entity's
/// class.
#[derive(Debug, Clone, Copy)]
struct IntField<'s> {
	entity: Entity<'s>,
	offset: usize,
}

impl<'s> IntField<'s> {
	/// # Safety
	///
	/// `offset` must be where an `int` member lives in every entity of
	/// `entity`'s class, as a send table or datamap of that class gives it.
	const unsafe fn new(entity: Entity<'s>, offset: usize) -> Self {
		Self { entity, offset }
	}

	fn read(self) -> i32 {
		// SAFETY: `new`'s contract places an `int` at the offset, within the
		// entity, which stays allocated for `'s`. It is read without forming a
		// reference, since the game writes it through its own pointers.
		unsafe {
			self.entity
				.as_ptr()
				.byte_add(self.offset)
				.cast::<i32>()
				.read_unaligned()
		}
	}

	fn write(self, value: i32) {
		// SAFETY: As for `read`. The game writes its members the same way, on
		// the main thread.
		unsafe {
			self.entity
				.as_ptr()
				.byte_add(self.offset)
				.cast::<i32>()
				.write_unaligned(value);
		}
	}
}

/// The values a networked integer can hold, from [`encodable_range`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct NetRange {
	min: i32,
	max: i32,
}

impl NetRange {
	/// Checks that `value` can be networked as the variable `name`.
	fn check(self, name: &'static str, value: i32) -> Result<(), ScoreError> {
		if (self.min..=self.max).contains(&value) {
			Ok(())
		} else {
			Err(ScoreError::OutOfRange {
				name,
				value,
				min: self.min,
				max: self.max,
			})
		}
	}

	const fn inclusive(self) -> RangeInclusive<i32> {
		self.min..=self.max
	}
}

/// Where a player class keeps its networked points, as resolved from its
/// server class.
#[derive(Debug, Clone, Copy)]
struct PlayerLayout {
	/// The address of the player's server class, which this describes.
	class: usize,

	/// `m_Shared.m_ScoreData.m_iPoints`: the session's score, as the player's
	/// own client receives it.
	points: usize,

	/// `m_Shared.m_RoundScoreData.m_iPoints`: the round's score.
	round_points: usize,
}

impl PlayerLayout {
	fn resolve(dll: ServerGameDll<'_>, class: ServerClass<'_>) -> Result<Self, ScoreError> {
		Ok(Self {
			class: class.as_ptr().addr(),
			points: scoring_points(dll, class, c"m_ScoreData")?,
			round_points: scoring_points(dll, class, c"m_RoundScoreData")?,
		})
	}
}

/// One TF2 player's scoring state, for one callback.
///
/// It reads and changes the statistics the game keeps for the player, which
/// the scoreboard's Score and the round's score are computed from, as the
/// [module documentation](self) describes, and the player's frags, deaths, and
/// kill streak. Every change is the game's own state from then on.
///
/// Like the [`Entity`] it is made for, it belongs to the callback its
/// [`Server`] does, and is `Copy`, but neither `Send` nor `Sync`.
#[derive(Debug, Clone, Copy)]
pub struct PlayerScore<'s> {
	edict: Edict<'s>,
	game_stats: GameStats,
	index: usize,
	layout: PlayerLayout,
	player: Entity<'s>,
	resource: Entity<'s>,
	resource_edict: Edict<'s>,
	server: Server<'s>,

	/// The game's statistics block for the player's entity index.
	stats: NonNull<PlayerStats>,

	/// The offset of the player's element of `m_iTotalScore` in the resource.
	total_score: usize,
}

impl<'s> PlayerScore<'s> {
	/// The scoring state of `player`, a TF2 player, as
	/// [`ScoreboardLayout::player`] finds it with a fresh layout.
	pub fn new(server: Server<'s>, player: Entity<'s>) -> Result<Self, ScoreError> {
		ScoreboardLayout::new().player(server, player)
	}

	/// Adds `delta` deaths, as [`Self::set_deaths`] sets them.
	///
	/// Fails as [`Self::add_frags`] does.
	pub fn add_deaths(self, delta: i32) -> Result<bool, ScoreError> {
		let count = self.count(c"m_iDeaths", offset_of!(sys::CPlayerState, deaths))?;
		let deaths = count
			.field
			.read()
			.checked_add(delta)
			.ok_or(ScoreError::Overflow)?;

		Ok(count.set(deaths))
	}

	/// Adds `delta` frags, as [`Self::set_frags`] sets them.
	///
	/// Fails as [`Self::set_frags`] does, or with [`ScoreError::Overflow`],
	/// writing nothing, if the sum does not fit an `i32`.
	pub fn add_frags(self, delta: i32) -> Result<bool, ScoreError> {
		let count = self.count(c"m_iFrags", offset_of!(sys::CPlayerState, frags))?;
		let frags = count
			.field
			.read()
			.checked_add(delta)
			.ok_or(ScoreError::Overflow)?;

		Ok(count.set(frags))
	}

	/// Changes the player's Score by `delta` points, through
	/// [`Stat::KillsRuneCarrier`], as [`Self::add_stat`] does.
	///
	/// Each point counts once, whatever the game mode and the player's
	/// attributes, but the Score clients see never drops below 0:
	/// [`Applied::shown`] is the change they see. Points taken below 0 are
	/// owed, and absorb the points the player earns next.
	pub fn add_points(self, delta: i32, adjust: Adjust) -> Result<Applied, ScoreError> {
		self.add_stat(Stat::KillsRuneCarrier, delta, adjust)
	}

	/// Adds `delta` to one of the player's statistics for the session, and
	/// for the round if [`Adjust::round`], and returns how much that moved the
	/// scores clients see.
	///
	/// The scores move by the statistic's weight in the game's
	/// `CalcPlayerScore`, as the [module documentation](self#points) describes,
	/// and clients receive them at once. The game does not report the change
	/// to item servers, its Mann vs. Machine statistics, or
	/// `player_score_changed` listeners when it is made, but a later reset of
	/// the player's statistics reports the change as part of the Score it
	/// drops, as the [module documentation](self#what-the-game-reports)
	/// describes, unless [`Self::rebase`] follows the reset. Differences the
	/// game has not sent yet are kept, and reported as usual; [`Self::rebase`]
	/// discards them.
	///
	/// Fails with [`ScoreError::Overflow`], writing nothing, if a statistic, or
	/// a score the game compares against, would not fit an `i32`. The game sums
	/// the statistics as `int`s too, so keep them far from that bound.
	pub fn add_stat(self, stat: Stat, delta: i32, adjust: Adjust) -> Result<Applied, ScoreError> {
		let index = stat.index();
		let session = self.read_block(StatScope::Session);
		let round = self.read_block(StatScope::Round);

		let mut new_session = session;
		new_session.stat[index] = session.stat[index]
			.checked_add(delta)
			.ok_or(ScoreError::Overflow)?;

		let new_round = if adjust.round {
			let mut new_round = round;

			new_round.stat[index] = round.stat[index]
				.checked_add(delta)
				.ok_or(ScoreError::Overflow)?;
			Some(new_round)
		} else {
			None
		};

		// Both scores are clamped at 0, so their difference fits.
		let shown = self.score(&new_session) - self.score(&session);
		let round_shown =
			new_round.map_or(0, |new_round| self.score(&new_round) - self.score(&round));

		// The values the game compares against keep any difference it has not
		// sent yet, so only the change itself is added to them.
		let fields = self.reported_fields();
		let old = fields.read();
		let compensate =
			|value: i32, shown: i32| value.checked_add(shown).ok_or(ScoreError::Overflow);
		let new = ReportedScores {
			total: compensate(old.total, shown)?,
			points: compensate(old.points, shown)?,
			round_points: compensate(old.round_points, round_shown)?,
		};
		let engine = self.server.valve_engine()?;

		self.write_stat(StatScope::Session, index, new_session.stat[index]);

		if let Some(new_round) = new_round {
			self.write_stat(StatScope::Round, index, new_round.stat[index]);
		}

		self.write_reported(fields, new, engine);
		Ok(Applied { round_shown, shown })
	}

	/// The player's statistics block for `scope`, which the game owns.
	fn block(self, scope: StatScope) -> *mut RoundStats {
		match scope {
			StatScope::Session => PlayerStats::accumulated(self.stats.as_ptr()),
			StatScope::Round => PlayerStats::current_round(self.stats.as_ptr()),
		}
	}

	/// The values the player resource's column `name` can network.
	fn column_range(self, name: &'static CStr) -> Result<RangeInclusive<i32>, ScoreError> {
		let (class, _) = networking(self.resource)?;
		let layout = ArrayLayout::resolve(self.server.server_game_dll()?, class, name)?;

		layout.offset(name.to_str().unwrap_or_default(), self.index)?;

		Ok(layout.range.inclusive())
	}

	/// The count `name` and its copy in the player state, from
	/// `CBasePlayer`'s datamap.
	///
	/// The copy's place is the datamap's embedded `pl` plus its offset in the
	/// bindings' `CPlayerState`, `mirror`, which the game's `CPlayerState`
	/// datamap leaves out. The datamap must agree with the bindings on the
	/// fields it does declare.
	fn count(self, name: &'static CStr, mirror: usize) -> Result<Count<'s>, ScoreError> {
		let missing = |name: &'static CStr| ScoreError::MissingField {
			class: "CBasePlayer",
			name: name.to_str().unwrap_or_default(),
		};

		let map = self
			.player
			.data_maps()
			.find(|&map| map.class_name() == Some(c"CBasePlayer"))
			.ok_or_else(|| missing(name))?;

		let offset = map
			.field_offset(name, sys::_fieldtypes_FIELD_INTEGER)
			.filter(|&offset| offset < MAX_FIELD_OFFSET && offset.is_multiple_of(ELEMENT_SIZE))
			.ok_or_else(|| missing(name))?;

		let state = map
			.fields()
			.iter()
			.filter(|field| field.fieldType == sys::_fieldtypes_FIELD_EMBEDDED)
			.find(|field| field.name() == Some(c"pl"))
			.filter(|field| {
				field.embedded().next().is_some_and(|state| {
					state.class_name() == Some(c"CPlayerState")
						&& state.field_offset(c"deadflag", sys::_fieldtypes_FIELD_BOOLEAN)
							== Some(offset_of!(sys::CPlayerState, deadflag))
						&& state.field_offset(c"v_angle", sys::_fieldtypes_FIELD_VECTOR)
							== Some(offset_of!(sys::CPlayerState, v_angle))
				})
			})
			.and_then(|field| field.offset())
			.filter(|&state| {
				state.is_multiple_of(align_of::<sys::CPlayerState>())
					&& state
						.checked_add(size_of::<sys::CPlayerState>())
						.is_some_and(|end| end <= MAX_FIELD_OFFSET)
			})
			.ok_or_else(|| missing(c"pl"))?;

		// SAFETY: `CBasePlayer`'s own datamap, which the player's chain
		// includes, declares an `int` at `offset`, and embeds a `CPlayerState` at
		// `state`, whose datamap agrees with the bindings' layout, which places
		// an `int` at `mirror`. The game assigns both without notifying
		// anything.
		Ok(unsafe {
			Count {
				field: IntField::new(self.player, offset),
				mirror: IntField::new(self.player, state + mirror),
			}
		})
	}

	/// The player's death count, `CBasePlayer::m_iDeaths`, which the
	/// statistics panel shows.
	///
	/// Fails as [`Self::set_deaths`] does.
	#[doc(alias("m_iDeaths", "DeathCount"))]
	pub fn deaths(self) -> Result<i32, ScoreError> {
		Ok(self
			.count(c"m_iDeaths", offset_of!(sys::CPlayerState, deaths))?
			.field
			.read())
	}

	/// The deaths clients can be shown, from the player resource's `m_iDeaths`
	/// as the running game networks it: -2048 to 2047 in the SDK.
	pub fn deaths_range(self) -> Result<RangeInclusive<i32>, ScoreError> {
		self.column_range(c"m_iDeaths")
	}

	/// The player's frag count, `CBasePlayer::m_iFrags`, which the statistics
	/// panel shows as kills.
	///
	/// Fails as [`Self::set_frags`] does.
	#[doc(alias("m_iFrags", "FragCount"))]
	pub fn frags(self) -> Result<i32, ScoreError> {
		Ok(self
			.count(c"m_iFrags", offset_of!(sys::CPlayerState, frags))?
			.field
			.read())
	}

	/// The frags clients can be shown, from the player resource's `m_iScore`
	/// as the running game networks it: -2048 to 2047 in the SDK.
	pub fn frags_range(self) -> Result<RangeInclusive<i32>, ScoreError> {
		self.column_range(c"m_iScore")
	}

	/// The player's entity index, which is also their player slot.
	pub const fn index(self) -> usize {
		self.index
	}

	/// The player's kill streak, `m_nStreaks[kTFStreak_Kills]`, which clients
	/// show on the scoreboard and use for kill streak effects.
	///
	/// Fails if the running game does not network it as an `int`.
	#[doc(alias("m_nStreaks", "kTFStreak_Kills", "GetStreak"))]
	pub fn killstreak(self) -> Result<i32, ScoreError> {
		Ok(self.streak()?.read())
	}

	/// The player.
	pub const fn player(self) -> Entity<'s> {
		self.player
	}

	/// The player's points: the session's [`Stat::KillsRuneCarrier`], which
	/// [`Self::add_points`] changes, including the kills of rune carriers the
	/// game counts there itself.
	pub fn points(self) -> i32 {
		self.stat(Stat::KillsRuneCarrier, StatScope::Session)
	}

	/// A copy of the player's statistics for `scope`.
	fn read_block(self, scope: StatScope) -> RoundStats {
		// SAFETY: `GameStats::player_stats` found the block in the game's
		// singleton, aligned for `int`s, and its statistics blocks within it; the
		// singleton lives as long as the game server module, which `Server::new`
		// keeps loaded during the callback. It is copied without forming a
		// reference, since the game writes it through its own pointers, on this
		// thread.
		unsafe { self.block(scope).read() }
	}

	/// Sets the values the game compares the player's scores against, and
	/// networks, to the scores themselves, and marks those that change for
	/// clients, so that the game reports nothing for the scores as they are.
	/// Returns what the game would otherwise have reported.
	///
	/// It writes the player's element of the player resource's
	/// `m_iTotalScore`, the Score the game last sent, and the session's and
	/// round's points of the player's scoring data
	/// (`m_Shared.m_ScoreData.m_iPoints` and `m_RoundScoreData.m_iPoints`), to
	/// the session's and round's scores the game's `CalcPlayerScore` computes
	/// for the player. The game's next think then finds no difference to
	/// report as Strange "Points Scored" progress, or in Mann vs. Machine to its
	/// statistics, and no rise of the session's points to fire
	/// `player_score_changed` for. Clients receive the scores at once.
	///
	/// Where and when to call it, and what it cannot cover, are in the
	/// [module documentation](self#reporting-nothing). It only reads and writes
	/// memory and runs `CalcPlayerScore`, which fires no event, so it can be
	/// called from inside a `player_score_changed` listener, which the game
	/// fires from the think it would report from.
	///
	/// Fails, writing nothing, if the engine's interface is unavailable.
	#[doc(alias("m_iTotalScore", "kKillEaterEvent_PointsScored"))]
	pub fn rebase(self) -> Result<Rebased, ScoreError> {
		let engine = self.server.valve_engine()?;
		let total = self.total();
		let fields = self.reported_fields();
		let old = fields.read();

		self.write_reported(
			fields,
			ReportedScores {
				total,
				points: total,
				round_points: self.round_total(),
			},
			engine,
		);

		Ok(Rebased {
			discarded: total.wrapping_sub(old.total),
			points_discarded: total.wrapping_sub(old.points),
		})
	}

	/// The values the game compares the player's scores against, and
	/// networks.
	fn reported_fields(self) -> ReportedFields<'s> {
		// SAFETY: The offsets were resolved from the send tables of the classes
		// the resource and the player had when this was made, during this
		// callback, and are `int`s, as `ArrayLayout` and `scoring_points`
		// checked.
		unsafe {
			ReportedFields {
				total: IntField::new(self.resource, self.total_score),
				points: IntField::new(self.player, self.layout.points),
				round_points: IntField::new(self.player, self.layout.round_points),
			}
		}
	}

	/// The round's score, as the game computes it for the round's MVPs and
	/// the player's round summary.
	pub fn round_total(self) -> i32 {
		self.score(&self.read_block(StatScope::Round))
	}

	/// Scores `stats` for the player with the game's `CalcPlayerScore`.
	fn score(self, stats: &RoundStats) -> i32 {
		// SAFETY: `Server::new` keeps the game server module, which `game_stats`
		// was resolved in, loaded for the whole callback (condition 1), and this
		// runs on the server's main thread within it (condition 3). `stats` is
		// borrowed for the call, which only reads it. The player is a live
		// `CTFPlayer` of the callback, as its datamaps showed, whose entity
		// pointer is its `CTFPlayer` pointer, as `sdk_raw::tf2` asserts; the game
		// reads its attributes through its items, which are entities of the
		// callback too, and frees nothing.
		unsafe {
			self.game_stats
				.calc_player_score(stats, self.player.as_ptr().cast::<sys::CTFPlayer>())
		}
	}

	/// Sets the player's death count, as [`Self::set_frags`] sets frags.
	///
	/// Fails as [`Self::set_frags`] does.
	#[doc(alias("m_iDeaths"))]
	pub fn set_deaths(self, deaths: i32) -> Result<bool, ScoreError> {
		Ok(self
			.count(c"m_iDeaths", offset_of!(sys::CPlayerState, deaths))?
			.set(deaths))
	}

	/// Sets the player's frag count, `CBasePlayer::m_iFrags`, which the game
	/// keeps counting from, and the copy in the player's state (`pl.frags`),
	/// which the engine, rather than the game, reports, as in the player list
	/// it gives Steam's server browser.
	///
	/// The player resource copies the frags into the statistics panel's kills
	/// at its next update. Clients see them wrapped into [`Self::frags_range`].
	/// The Score is computed from [`Stat::Kills`] instead, and does not change.
	///
	/// The engine's copy is only written if it agreed with the frag count
	/// before, as the game keeps it; returns whether it was. Code that sets the
	/// frag count alone leaves the copy behind until the game next counts a
	/// frag.
	///
	/// Fails if `CBasePlayer`'s datamap does not declare the count as an `int`
	/// at a plausible offset, or does not embed a player state laid out as the
	/// bindings' `CPlayerState`.
	#[doc(alias("m_iFrags"))]
	pub fn set_frags(self, frags: i32) -> Result<bool, ScoreError> {
		Ok(self
			.count(c"m_iFrags", offset_of!(sys::CPlayerState, frags))?
			.set(frags))
	}

	/// Sets the player's kill streak, which the player resource copies into
	/// the scoreboard at its next update, and marks it changed for clients.
	///
	/// The game counts on from it at the player's next kill, announcing kill
	/// streaks by it, and resets it when the player dies or respawns.
	///
	/// Fails if `killstreak` is negative or cannot be networked, or as
	/// [`Self::killstreak`] does.
	#[doc(alias("m_nStreaks", "kTFStreak_Kills"))]
	pub fn set_killstreak(self, killstreak: i32) -> Result<(), ScoreError> {
		let (field, range) = self.streak_with_range()?;
		let engine = self.server.valve_engine()?;

		range.check("m_nStreaks", killstreak)?;
		field.write(killstreak);

		let mut changes = Changes::default();

		changes.mark(field.offset);
		changes.flush(engine, self.edict);
		Ok(())
	}

	/// Sets one of the player's statistics for the session to `value`, and
	/// changes the round's by as much if [`Adjust::round`], as
	/// [`Self::add_stat`] does.
	pub fn set_stat(self, stat: Stat, value: i32, adjust: Adjust) -> Result<Applied, ScoreError> {
		let delta = value
			.checked_sub(self.stat(stat, StatScope::Session))
			.ok_or(ScoreError::Overflow)?;

		self.add_stat(stat, delta, adjust)
	}

	/// Sets the player's Score to `target` exactly, by changing their points,
	/// as [`Self::add_points`] does, and the round's points by as much if
	/// [`Adjust::round`].
	///
	/// While the player's statistics score below 0, which the game shows as 0,
	/// the change needed is found by scoring copies of them with more points,
	/// the game's scoring being linear in points.
	///
	/// Fails as [`Self::add_points`] does, or with [`ScoreError::Overflow`],
	/// writing nothing, if `target` exceeds `i32::MAX`.
	pub fn set_total(self, target: u32, adjust: Adjust) -> Result<Applied, ScoreError> {
		let target = i32::try_from(target).map_err(|_| ScoreError::Overflow)?;
		let unclamped = self.unclamped(self.read_block(StatScope::Session))?;

		if target == 0 && unclamped <= 0 {
			return Ok(Applied::default());
		}

		let delta = target.checked_sub(unclamped).ok_or(ScoreError::Overflow)?;

		self.add_points(delta, adjust)
	}

	/// One of the player's statistics, for `scope`.
	pub fn stat(self, stat: Stat, scope: StatScope) -> i32 {
		self.read_block(scope).stat[stat.index()]
	}

	/// The player's kill streak variable.
	fn streak(self) -> Result<IntField<'s>, ScoreError> {
		self.streak_with_range().map(|(field, _)| field)
	}

	/// The player's kill streak variable, and the values it can hold.
	fn streak_with_range(self) -> Result<(IntField<'s>, NetRange), ScoreError> {
		let element = self
			.server
			.server_game_dll()?
			.entity_net_prop(self.player, c"m_nStreaks")?
			.element(KILL_STREAK)?;
		let range = int_range(element.prop(), element.storage(), "m_nStreaks")?;

		// SAFETY: The offset was resolved from the send table of the player's
		// class, and is an `int`, as `int_range` checked.
		let field = unsafe { IntField::new(self.player, element.offset()) };

		Ok((
			field,
			NetRange {
				min: range.min.max(0),
				max: range.max,
			},
		))
	}

	/// The player's Score, as the game computes and shows it: the session's
	/// statistics scored with the game's `CalcPlayerScore`, including the terms
	/// of the player's `scoreboard_minigame` attribute, if any.
	#[doc(alias("m_iTotalScore", "CalcPlayerScore"))]
	pub fn total(self) -> i32 {
		self.score(&self.read_block(StatScope::Session))
	}

	/// The unclamped score of `stats`, which the game clamps at 0.
	///
	/// Below 0, it adds points to a copy, doubling them until the copy scores
	/// above 0, then takes them back off: the score is linear in points.
	fn unclamped(self, stats: RoundStats) -> Result<i32, ScoreError> {
		let score = self.score(&stats);

		if score > 0 {
			return Ok(score);
		}

		let points = stats.stat[stat::KILLS_RUNECARRIER];
		let mut lift: i32 = 1;

		loop {
			let mut probe = stats;

			probe.stat[stat::KILLS_RUNECARRIER] =
				points.checked_add(lift).ok_or(ScoreError::Overflow)?;

			let score = self.score(&probe);

			if score > 0 {
				return Ok(score - lift);
			}

			lift = lift.checked_mul(2).ok_or(ScoreError::Overflow)?;
		}
	}

	/// Writes each of `new`'s values that differs from `fields`', and marks it
	/// changed for clients.
	///
	/// The game compares what it computes with these, and only reports, and
	/// marks changed, a difference, so a value written without a mark would
	/// not reach clients until the game itself changes it.
	fn write_reported(
		self,
		fields: ReportedFields<'s>,
		new: ReportedScores,
		engine: ValveEngine<'_>,
	) {
		let old = fields.read();
		let mut resource_changes = Changes::default();
		let mut player_changes = Changes::default();

		if new.total != old.total {
			fields.total.write(new.total);
			resource_changes.mark(fields.total.offset);
		}

		if new.points != old.points {
			fields.points.write(new.points);
			player_changes.mark(fields.points.offset);
		}

		if new.round_points != old.round_points {
			fields.round_points.write(new.round_points);
			player_changes.mark(fields.round_points.offset);
		}

		resource_changes.flush(engine, self.resource_edict);
		player_changes.flush(engine, self.edict);
	}

	/// Writes one statistic of the player's block for `scope`.
	fn write_stat(self, scope: StatScope, index: usize, value: i32) {
		// SAFETY: As for `read_block`, and `index` is one of the block's
		// `TFSTAT_TOTAL` statistics, since a `Stat` gave it. The game writes its
		// statistics the same way, on this thread.
		unsafe {
			(&raw mut (*self.block(scope)).stat)
				.cast::<c_int>()
				.add(index)
				.write(value);
		}
	}
}

/// What [`PlayerScore::rebase`] discarded: the differences the game had not
/// sent yet. Each wraps as the game's own `int` subtraction does.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct Rebased {
	/// The Score minus the one the game last sent: the change the player
	/// resource would have reported at its next think, as Strange "Points
	/// Scored" progress or to Mann vs. Machine's statistics.
	pub discarded: i32,

	/// The Score minus the session's points of the player's scoring data. Above
	/// 0, the game would have fired `player_score_changed` with this increase
	/// at its next think.
	pub points_discarded: i32,
}

/// The values of one player that the player resource compares the scores it
/// computes against at each think, and networks.
#[derive(Debug, Clone, Copy)]
struct ReportedFields<'s> {
	/// The player's element of the resource's `m_iTotalScore`: the Score the
	/// game last sent, which it reports changes from.
	total: IntField<'s>,

	/// `m_Shared.m_ScoreData.m_iPoints`: the session's score the game last
	/// copied into the player's scoring data, whose rises fire
	/// `player_score_changed`.
	points: IntField<'s>,

	/// `m_Shared.m_RoundScoreData.m_iPoints`: the round's score the game last
	/// copied into the player's scoring data.
	round_points: IntField<'s>,
}

impl ReportedFields<'_> {
	fn read(self) -> ReportedScores {
		ReportedScores {
			total: self.total.read(),
			points: self.points.read(),
			round_points: self.round_points.read(),
		}
	}
}

/// The values of [`ReportedFields`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ReportedScores {
	total: i32,
	points: i32,
	round_points: i32,
}

/// What [`ScoreboardLayout`] keeps of the player resource it last found.
#[derive(Debug)]
struct ResourceCache {
	handle: EntityHandle,

	/// The address of the resource's server class, which `total_score`
	/// describes.
	class: usize,

	/// The array of `m_iTotalScore`, the scoreboard's Score.
	total_score: Result<ArrayLayout, ScoreError>,
}

impl ResourceCache {
	fn resolve(dll: ServerGameDll<'_>, entity: Entity<'_>, class: ServerClass<'_>) -> Self {
		Self {
			handle: entity.handle(),
			class: class.as_ptr().addr(),
			total_score: ArrayLayout::resolve(dll, class, c"m_iTotalScore"),
		}
	}
}

/// Where a team's variable lives, as resolved from the team's server class.
#[derive(Debug, Clone, Copy)]
struct Scalar {
	offset: usize,
	range: NetRange,
}

impl Scalar {
	fn resolve(
		dll: ServerGameDll<'_>,
		class: ServerClass<'_>,
		stat: TeamStat,
	) -> Result<Self, ScoreError> {
		let variable = dll.net_prop(class, stat.name())?;

		Ok(Self {
			offset: variable.offset(),
			range: int_range(variable.prop(), variable.storage(), stat.display_name())?,
		})
	}
}

/// Why a scoreboard operation failed.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ScoreError {
	/// The game's statistics or score calculation could not be found or
	/// trusted.
	#[error(transparent)]
	GameStats(#[from] GameStatsError),

	/// A required engine or game interface is unavailable.
	#[error(transparent)]
	Interface(#[from] InterfaceError),

	/// The player's datamaps lack a field, or give it an implausible offset
	/// or layout.
	#[error("the `{class}` datamap has no usable `{name}` field")]
	MissingField {
		/// The class whose datamap declares the field.
		class: &'static str,

		/// The field's name.
		name: &'static str,
	},

	/// A networked variable could not be found or accessed.
	#[error(transparent)]
	NetProp(#[from] NetPropError),

	/// No player resource exists, as while no level runs.
	#[error("no `tf_player_manager` entity exists; no level is running")]
	NoPlayerResource,

	/// The game keeps no statistics block for the player's entity index, as
	/// for an entity index above `MAX_PLAYERS`, or a player without an edict.
	#[error("the game keeps no statistics for the player's entity index")]
	NoPlayerStats,

	/// No team entity has the team's number, as while no level runs.
	#[error("no `tf_team` entity has team number {}", .0.to_raw())]
	NoTeam(ScoringTeam),

	/// A variable is not a networked `int`.
	#[error("`{name}` is not a networked 32-bit integer")]
	NotAnInteger {
		/// The variable's name.
		name: &'static str,
	},

	/// The server does not run TF2.
	#[error("the scoreboard requires a TF2 server")]
	NotTf2,

	/// The entity is not a TF2 player.
	#[error("the entity is not a TF2 player")]
	NotTfPlayer,

	/// The value cannot be networked as the variable.
	#[error("{value} cannot be networked as `{name}`, which holds {min}..={max}")]
	OutOfRange {
		/// The variable's name.
		name: &'static str,

		/// The value given.
		value: i32,

		/// The lowest value the variable can network.
		min: i32,

		/// The highest value the variable can network.
		max: i32,
	},

	/// A statistic, score, or count would not fit a 32-bit integer.
	#[error("the change would take a value outside 32-bit integers")]
	Overflow,

	/// A player column is not laid out as an array of `int`s with an element
	/// for the player's slot.
	#[error(
		"`{name}` is not a contiguous array of 32-bit integers with at least {needed} elements"
	)]
	UnexpectedLayout {
		/// The array's name.
		name: &'static str,

		/// The number of elements needed.
		needed: usize,
	},
}

/// What the scoreboard resolves in the running game, kept by a plugin
/// between callbacks so that each [`PlayerScore`] and team change does not
/// resolve it again.
///
/// It holds no engine pointer: each call finds the entities again by handle,
/// and checks them against the server class their layout was resolved from,
/// so it stays sound across levels. The free functions and
/// [`PlayerScore::new`] use a fresh one each call.
#[derive(Debug, Default)]
pub struct ScoreboardLayout {
	player: Option<PlayerLayout>,
	resource: Option<ResourceCache>,
	teams: [Option<TeamCache>; ScoringTeam::ALL.len()],
	_not_thread_safe: NotThreadSafe,
}

impl ScoreboardLayout {
	/// An empty layout, which resolves everything at its first use.
	pub fn new() -> Self {
		Self::default()
	}

	/// Adds `delta` to one of a team's numbers, as [`Self::set_team_stat`]
	/// sets it, and returns the new value.
	///
	/// Fails as [`Self::set_team_stat`] does, or with
	/// [`ScoreError::Overflow`], writing nothing.
	pub fn add_team_stat(
		&mut self,
		server: Server<'_>,
		team: ScoringTeam,
		stat: TeamStat,
		delta: i32,
	) -> Result<i32, ScoreError> {
		let (variable, range, edict, engine) = self.team_variable(server, team, stat)?;
		let value = variable
			.read()
			.checked_add(delta)
			.ok_or(ScoreError::Overflow)?;

		range.check(stat.display_name(), value)?;
		write_team_variable(variable, value, edict, engine);
		Ok(value)
	}

	/// The scoring state of `player`, a TF2 player.
	///
	/// The first call in the game server module finds and tests the game's
	/// statistics and score calculation, as `sdk_raw`'s [`GameStats::cached`]
	/// describes, running the game's `CalcPlayerScore` once; later calls reuse
	/// them. This relies on Source never unloading that module while plugins
	/// are loaded.
	///
	/// Fails if the server does not run TF2, `player` is not a TF2 player with
	/// an entity index from 1 to `MAX_PLAYERS`, the game's statistics cannot
	/// be found or trusted, or no player resource exists or the running game
	/// lays out the scores differently.
	pub fn player<'s>(
		&mut self,
		server: Server<'s>,
		player: Entity<'s>,
	) -> Result<PlayerScore<'s>, ScoreError> {
		self.player_with(server, player, || {
			// SAFETY: `Server::new` guarantees that the game server module, whose
			// factory this is, stays loaded through the callback, on the server's
			// main thread, where the resolution's test calls `CalcPlayerScore`. A
			// cached resolution for the same factory and module base was made in
			// this same image: Source never unloads the game server module while
			// plugins are loaded, since Metamod:Source and the engine unload
			// plugins first, and the cache, a static of this plugin, is unloaded
			// with it.
			unsafe { GameStats::cached(server.game_server_factory().as_raw()) }
		})
	}

	/// The layout of `class`, a player's server class.
	fn player_layout(
		&mut self,
		dll: ServerGameDll<'_>,
		class: ServerClass<'_>,
	) -> Result<PlayerLayout, ScoreError> {
		if let Some(layout) = self.player
			&& layout.class == class.as_ptr().addr()
		{
			return Ok(layout);
		}

		let layout = PlayerLayout::resolve(dll, class)?;

		self.player = Some(layout);
		Ok(layout)
	}

	/// [`Self::player`], with the game's statistics from `game_stats`, which
	/// is only called once the player is checked.
	fn player_with<'s>(
		&mut self,
		server: Server<'s>,
		player: Entity<'s>,
		game_stats: impl FnOnce() -> Result<GameStats, GameStatsError>,
	) -> Result<PlayerScore<'s>, ScoreError> {
		check_game(server)?;

		if !player.has_data_map_class(c"CTFPlayer") {
			return Err(ScoreError::NotTfPlayer);
		}

		let index = player
			.index()
			.filter(|index| (1..MAX_PLAYERS_ARRAY_SAFE).contains(index))
			.ok_or(ScoreError::NoPlayerStats)?;
		let (class, edict) = networking(player)?;
		let context = Context::new(server)?;
		let game_stats = game_stats()?;
		let base = NonNull::new(player.as_ptr().cast::<sys::CBasePlayer>())
			.ok_or(ScoreError::NotTfPlayer)?;

		// SAFETY: As for `Self::player`'s resolution, the module stays loaded
		// and this runs on the main thread. The player is a live `CTFPlayer`, as
		// its datamaps show, whose entity pointer is its `CBasePlayer` pointer,
		// as `sdk_raw::tf2` asserts, with its live edict.
		let stats =
			unsafe { game_stats.player_stats(base, index) }.ok_or(ScoreError::NoPlayerStats)?;

		let layout = self.player_layout(context.dll, class)?;
		let (resource, resource_edict, cache) = resource_entity(&mut self.resource, context)?;
		let total_score = cache.total_score.clone()?.offset("m_iTotalScore", index)?;

		Ok(PlayerScore {
			edict,
			game_stats,
			index,
			layout,
			player,
			resource,
			resource_edict,
			server,
			stats,
			total_score,
		})
	}

	/// Sets one of a team's numbers in the game's own state, with the
	/// consequences [`set_team_score`] and [`set_team_flag_captures`]
	/// document, and marks it changed for clients.
	///
	/// Fails if the server does not run TF2, an interface is unavailable, the
	/// team has no entity, the running game does not network the number as an
	/// `int`, or `value` is outside [`Self::team_stat_range`].
	#[doc(alias("SetScore", "SetFlagCaptures"))]
	pub fn set_team_stat(
		&mut self,
		server: Server<'_>,
		team: ScoringTeam,
		stat: TeamStat,
		value: i32,
	) -> Result<(), ScoreError> {
		let (variable, range, edict, engine) = self.team_variable(server, team, stat)?;

		range.check(stat.display_name(), value)?;
		write_team_variable(variable, value, edict, engine);
		Ok(())
	}

	/// One of a team's numbers, as the game keeps it.
	///
	/// Fails as [`Self::set_team_stat`] does.
	pub fn team_stat(
		&mut self,
		server: Server<'_>,
		team: ScoringTeam,
		stat: TeamStat,
	) -> Result<i32, ScoreError> {
		let (variable, ..) = self.team_variable(server, team, stat)?;

		Ok(variable.read())
	}

	/// The values the running game can network for one of a team's numbers,
	/// from its networked variable's bit count and flags.
	pub fn team_stat_range(
		&mut self,
		server: Server<'_>,
		team: ScoringTeam,
		stat: TeamStat,
	) -> Result<RangeInclusive<i32>, ScoreError> {
		let (_, range, ..) = self.team_variable(server, team, stat)?;

		Ok(range.inclusive())
	}

	/// One of a team's numbers in its live entity, with what it can network,
	/// the entity's edict, and the engine.
	fn team_variable<'s>(
		&mut self,
		server: Server<'s>,
		team: ScoringTeam,
		stat: TeamStat,
	) -> Result<(IntField<'s>, NetRange, Edict<'s>, ValveEngine<'s>), ScoreError> {
		check_game(server)?;

		let context = Context::new(server)?;
		let (entity, edict, cache) = team_entity(&mut self.teams[team.index()], context, team)?;
		let scalar = cache.fields[stat.index()].clone()?;

		// SAFETY: The offset was resolved from the send table of the class
		// `team_entity` checked the entity still has, and is an `int`, as
		// `int_range` checked.
		let variable = unsafe { IntField::new(entity, scalar.offset) };

		Ok((variable, scalar.range, edict, context.engine))
	}
}

/// A team with a score: `TF_TEAM_RED` or `TF_TEAM_BLUE`, numbered as TF2 does
/// (`tf_shareddefs.h:31-35`).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum ScoringTeam {
	/// RED.
	#[doc(alias("TF_TEAM_RED"))]
	Red = TF_TEAM_RED,

	/// BLU.
	#[doc(alias("TF_TEAM_BLUE"))]
	Blue = TF_TEAM_BLUE,
}

impl ScoringTeam {
	/// Both teams.
	pub const ALL: [Self; 2] = [Self::Red, Self::Blue];

	/// The team with this team number, or `None` for any other number.
	pub const fn from_raw(raw: i32) -> Option<Self> {
		match raw {
			TF_TEAM_RED => Some(Self::Red),
			TF_TEAM_BLUE => Some(Self::Blue),
			_ => None,
		}
	}

	/// The team's position in [`Self::ALL`].
	const fn index(self) -> usize {
		match self {
			Self::Red => 0,
			Self::Blue => 1,
		}
	}

	/// The team's number, as `m_iTeamNum` holds it.
	pub const fn to_raw(self) -> i32 {
		self as i32
	}
}

/// One of the statistics TF2 keeps for each player, `TFStatType_t`, numbered
/// as `sdk_raw`'s [`stat`] indices.
///
/// The game's `CalcPlayerScore` scores some of them, with the weights of
/// `sdk_raw`'s `TF_SCORE_` constants, as the [module documentation](self#points)
/// describes. The others only appear in summaries the game sends.
#[doc(alias("TFStatType_t"))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(usize)]
pub enum Stat {
	/// Shots that hit.
	#[doc(alias("TFSTAT_SHOTS_HIT"))]
	ShotsHit = stat::SHOTS_HIT,

	/// Shots fired.
	#[doc(alias("TFSTAT_SHOTS_FIRED"))]
	ShotsFired = stat::SHOTS_FIRED,

	/// Kills, one point each, and one more with the `scoreboard_minigame`
	/// attribute.
	#[doc(alias("TFSTAT_KILLS"))]
	Kills = stat::KILLS,

	/// Deaths, which only score with the `scoreboard_minigame` attribute,
	/// minus three points each.
	#[doc(alias("TFSTAT_DEATHS"))]
	Deaths = stat::DEATHS,

	/// Damage dealt, a point per 600, or per 250 in Mann vs. Machine, together
	/// with the other damage and healing statistics the game scores.
	#[doc(alias("TFSTAT_DAMAGE"))]
	Damage = stat::DAMAGE,

	/// Captures of control points and flags, two points each, five in
	/// Mannpower, and one more with the `scoreboard_minigame` attribute.
	#[doc(alias("TFSTAT_CAPTURES"))]
	Captures = stat::CAPTURES,

	/// Defenses of control points and flags, one point each, and one more with
	/// the `scoreboard_minigame` attribute.
	#[doc(alias("TFSTAT_DEFENSES"))]
	Defenses = stat::DEFENSES,

	/// Dominations.
	#[doc(alias("TFSTAT_DOMINATIONS"))]
	Dominations = stat::DOMINATIONS,

	/// Revenges, one point each.
	#[doc(alias("TFSTAT_REVENGE"))]
	Revenge = stat::REVENGE,

	/// Points scored, which the game counts per life only.
	#[doc(alias("TFSTAT_POINTSSCORED"))]
	PointsScored = stat::POINTSSCORED,

	/// Enemy buildings destroyed, one point each, and one more with the
	/// `scoreboard_minigame` attribute.
	#[doc(alias("TFSTAT_BUILDINGSDESTROYED"))]
	BuildingsDestroyed = stat::BUILDINGSDESTROYED,

	/// Headshots, a point per two.
	#[doc(alias("TFSTAT_HEADSHOTS"))]
	Headshots = stat::HEADSHOTS,

	/// Seconds played.
	#[doc(alias("TFSTAT_PLAYTIME"))]
	PlayTime = stat::PLAYTIME,

	/// Healing given, scored as [`Self::Damage`] is, while it lies within 0 to
	/// 10,000,000.
	#[doc(alias("TFSTAT_HEALING"))]
	Healing = stat::HEALING,

	/// Invulnerabilities given, one point each.
	#[doc(alias("TFSTAT_INVULNS"))]
	Invulns = stat::INVULNS,

	/// Kill assists, a point per two.
	#[doc(alias("TFSTAT_KILLASSISTS"))]
	KillAssists = stat::KILLASSISTS,

	/// Backstabs, one point each.
	#[doc(alias("TFSTAT_BACKSTABS"))]
	Backstabs = stat::BACKSTABS,

	/// Health leached.
	#[doc(alias("TFSTAT_HEALTHLEACHED"))]
	HealthLeached = stat::HEALTHLEACHED,

	/// Buildings built.
	#[doc(alias("TFSTAT_BUILDINGSBUILT"))]
	BuildingsBuilt = stat::BUILDINGSBUILT,

	/// The most kills of one sentry gun.
	#[doc(alias("TFSTAT_MAXSENTRYKILLS"))]
	MaxSentryKills = stat::MAXSENTRYKILLS,

	/// Teleports given by the player's teleporters, a point per two.
	#[doc(alias("TFSTAT_TELEPORTS"))]
	Teleports = stat::TELEPORTS,

	/// Damage dealt by fire.
	#[doc(alias("TFSTAT_FIREDAMAGE"))]
	FireDamage = stat::FIREDAMAGE,

	/// Bonus points, a point per ten. The player resource's bonus column shows
	/// the round's.
	#[doc(alias("TFSTAT_BONUS_POINTS"))]
	BonusPoints = stat::BONUS_POINTS,

	/// Damage dealt by explosions.
	#[doc(alias("TFSTAT_BLASTDAMAGE"))]
	BlastDamage = stat::BLASTDAMAGE,

	/// Damage taken.
	#[doc(alias("TFSTAT_DAMAGETAKEN"))]
	DamageTaken = stat::DAMAGETAKEN,

	/// Health kits picked up.
	#[doc(alias("TFSTAT_HEALTHKITS"))]
	HealthKits = stat::HEALTHKITS,

	/// Ammunition kits picked up.
	#[doc(alias("TFSTAT_AMMOKITS"))]
	AmmoKits = stat::AMMOKITS,

	/// Changes of class.
	#[doc(alias("TFSTAT_CLASSCHANGES"))]
	ClassChanges = stat::CLASSCHANGES,

	/// Critical hits.
	#[doc(alias("TFSTAT_CRITS"))]
	Crits = stat::CRITS,

	/// Suicides.
	#[doc(alias("TFSTAT_SUICIDES"))]
	Suicides = stat::SUICIDES,

	/// Credits collected in Mann vs. Machine, a point per twenty.
	#[doc(alias("TFSTAT_CURRENCY_COLLECTED"))]
	CurrencyCollected = stat::CURRENCY_COLLECTED,

	/// Damage dealt by others with the player's help, scored as
	/// [`Self::Damage`] is.
	#[doc(alias("TFSTAT_DAMAGE_ASSIST"))]
	DamageAssist = stat::DAMAGE_ASSIST,

	/// Healing given by others with the player's help, scored as
	/// [`Self::Damage`] is.
	#[doc(alias("TFSTAT_HEALING_ASSIST"))]
	HealingAssist = stat::HEALING_ASSIST,

	/// Damage dealt to bosses, scored as [`Self::Damage`] is.
	#[doc(alias("TFSTAT_DAMAGE_BOSS"))]
	DamageBoss = stat::DAMAGE_BOSS,

	/// Damage blocked, scored as [`Self::Damage`] is.
	#[doc(alias("TFSTAT_DAMAGE_BLOCKED"))]
	DamageBlocked = stat::DAMAGE_BLOCKED,

	/// Damage dealt at range.
	#[doc(alias("TFSTAT_DAMAGE_RANGED"))]
	DamageRanged = stat::DAMAGE_RANGED,

	/// Damage dealt at range by random critical hits.
	#[doc(alias("TFSTAT_DAMAGE_RANGED_CRIT_RANDOM"))]
	DamageRangedCritRandom = stat::DAMAGE_RANGED_CRIT_RANDOM,

	/// Damage dealt at range by crit-boosted hits.
	#[doc(alias("TFSTAT_DAMAGE_RANGED_CRIT_BOOSTED"))]
	DamageRangedCritBoosted = stat::DAMAGE_RANGED_CRIT_BOOSTED,

	/// Revivals.
	#[doc(alias("TFSTAT_REVIVED"))]
	Revived = stat::REVIVED,

	/// Hits with throwables.
	#[doc(alias("TFSTAT_THROWABLEHIT"))]
	ThrowableHit = stat::THROWABLEHIT,

	/// Kills with throwables.
	#[doc(alias("TFSTAT_THROWABLEKILL"))]
	ThrowableKill = stat::THROWABLEKILL,

	/// The longest kill streak.
	#[doc(alias("TFSTAT_KILLSTREAK_MAX"))]
	KillstreakMax = stat::KILLSTREAK_MAX,

	/// Kills of players carrying a Mannpower rune, one point each, whatever
	/// the mode and the player's attributes. The game counts them for any rune
	/// carrier, and the scoreboard's points are kept here, as the
	/// [module documentation](self#points) describes.
	#[doc(alias("TFSTAT_KILLS_RUNECARRIER"))]
	KillsRuneCarrier = stat::KILLS_RUNECARRIER,

	/// Flags returned, four points each.
	#[doc(alias("TFSTAT_FLAGRETURNS"))]
	FlagReturns = stat::FLAGRETURNS,
}

impl Stat {
	/// The statistic's index in a `RoundStats_t`, its `TFStatType_t` value.
	pub const fn index(self) -> usize {
		self as usize
	}
}

/// Which of a player's statistics to read: the session's or the current
/// round's.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum StatScope {
	/// Since the player connected or their scores were last reset,
	/// `statsAccumulated`, which the scoreboard's Score is computed from.
	#[doc(alias("statsAccumulated"))]
	Session,

	/// Since the current round started, `statsCurrentRound`, which the
	/// round's score is computed from.
	#[doc(alias("statsCurrentRound"))]
	Round,
}

/// What [`ScoreboardLayout`] keeps of a team entity it last found.
#[derive(Debug)]
struct TeamCache {
	handle: EntityHandle,

	/// The address of the team's server class, which `fields` describes.
	class: usize,

	/// Each number's variable, in [`TeamStat::ALL`]'s order.
	fields: [Result<Scalar, ScoreError>; TeamStat::ALL.len()],
}

impl TeamCache {
	fn resolve(dll: ServerGameDll<'_>, entity: Entity<'_>, class: ServerClass<'_>) -> Self {
		Self {
			handle: entity.handle(),
			class: class.as_ptr().addr(),
			fields: TeamStat::ALL.map(|stat| Scalar::resolve(dll, class, stat)),
		}
	}
}

/// A team's number on the scoreboard and HUD.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TeamStat {
	/// The team's score, `CTeam::m_iScore`: rounds won in most game modes.
	#[doc(alias("m_iScore"))]
	Score,

	/// The flags the team captured this round, `CTFTeam::m_nFlagCaptures`,
	/// which the HUD shows instead of the score while
	/// `tf_flag_caps_per_round` is above 0 (`tf_hud_flagstatus.cpp:643-677`).
	/// PASS Time keeps its goals here, and its HUD shows them as the team's
	/// score (`tf_hud_passtime.cpp:200-213`).
	#[doc(alias("m_nFlagCaptures"))]
	FlagCaptures,
}

impl TeamStat {
	/// Both numbers.
	pub const ALL: [Self; 2] = [Self::Score, Self::FlagCaptures];

	/// The networked variable's name, as an `&str` for errors.
	fn display_name(self) -> &'static str {
		self.name().to_str().unwrap_or_default()
	}

	/// The number's position in [`Self::ALL`].
	const fn index(self) -> usize {
		match self {
			Self::Score => 0,
			Self::FlagCaptures => 1,
		}
	}

	/// The networked variable's name in `DT_TFTeam`, such as
	/// `m_nFlagCaptures`.
	pub const fn name(self) -> &'static CStr {
		match self {
			Self::Score => c"m_iScore",
			Self::FlagCaptures => c"m_nFlagCaptures",
		}
	}
}

/// Adds `delta` to a team's score, as [`set_team_score`] sets it, and returns
/// the new score.
///
/// Fails as [`set_team_score`] does, or with [`ScoreError::Overflow`].
#[doc(alias("AddScore", "m_iScore"))]
pub fn add_team_score(
	server: Server<'_>,
	team: ScoringTeam,
	delta: i32,
) -> Result<i32, ScoreError> {
	ScoreboardLayout::new().add_team_stat(server, team, TeamStat::Score, delta)
}

/// Fails with [`ScoreError::NotTf2`] unless the server runs TF2.
fn check_game(server: Server<'_>) -> Result<(), ScoreError> {
	if server.game() == Game::TeamFortress2 {
		Ok(())
	} else {
		Err(ScoreError::NotTf2)
	}
}

/// The values a networked integer can hold, from its property's live bit
/// count and flags.
///
/// `SendPropInt` replaces a bit count of 0 or less with the variable's width
/// (`dt_send.cpp:519-524`), which is 32 for these variables, and so does this.
/// Integers reuse `SPROP_NORMAL` as `SPROP_VARINT`, which encodes all 32
/// bits. Unsigned ranges stop at `i32::MAX`, since the variables are `int`s.
fn encodable_range(prop: SendProp<'_>) -> NetRange {
	let flags = prop.flags();
	let bits = match prop.bits() {
		_ if flags.contains(PropFlags::NORMAL) => 32,
		bits @ 1..=32 => bits,
		_ => 32,
	};

	let (min, max) = if flags.contains(PropFlags::UNSIGNED) {
		(0, (1_i64 << bits) - 1)
	} else {
		(-(1_i64 << (bits - 1)), (1_i64 << (bits - 1)) - 1)
	};

	NetRange {
		min: saturate(min),
		max: saturate(max),
	}
}

/// Finds the `tf_team` entity of `team`.
fn find_team<'s>(context: Context<'s>, team: ScoringTeam) -> Result<Entity<'s>, ScoreError> {
	let mut after = None;

	for _ in 0..EntityHandle::SLOTS {
		let Some(entity) = context.tools.find_by_class_name(after, TEAM_CLASS_NAME) else {
			break;
		};

		let number = context
			.dll
			.entity_net_prop(entity, c"m_iTeamNum")?
			.get::<i32>(entity)?;

		if number == team.to_raw() {
			return Ok(entity);
		}

		after = Some(entity);
	}

	Err(ScoreError::NoTeam(team))
}

/// The range of an `int` variable named `name`, or an error if it is not one.
fn int_range(
	prop: SendProp<'_>,
	storage: Storage,
	name: &'static str,
) -> Result<NetRange, ScoreError> {
	if prop.kind() == PropKind::Int && storage.is_compatible(Storage::I32) {
		Ok(encodable_range(prop))
	} else {
		Err(ScoreError::NotAnInteger { name })
	}
}

/// The entity a cache describes, if `handle` still finds it and it still has
/// the server class at `class`, with its edict.
fn live<'s>(
	tools: ServerTools<'s>,
	handle: EntityHandle,
	class: usize,
) -> Option<(Entity<'s>, Edict<'s>)> {
	let entity = tools.entity_by_handle(handle)?;
	let (found, edict) = networking(entity).ok()?;

	(found.as_ptr().addr() == class).then_some((entity, edict))
}

/// An entity's server class and edict, which every networked entity has.
fn networking(entity: Entity<'_>) -> Result<(ServerClass<'_>, Edict<'_>), ScoreError> {
	match (entity.server_class(), entity.edict()) {
		(Some(class), Some(edict)) => Ok((class, edict)),

		_ => Err(NetPropError::NotNetworked {
			class_name: entity.class_name().to_string_lossy().into_owned(),
		}
		.into()),
	}
}

/// Resets `player`'s scoring through `CTFPlayer::ResetScores`, as
/// `mp_restartgame`, a tournament restart, and the end of the wait for players
/// do for every player.
///
/// This resets the statistics the game keeps for the player, which the
/// "Score" column is computed from, their frags and deaths, the statistics
/// panel's counts, and every domination and revenge relationship they are part
/// of, and drops their Mann vs. Machine events (`tf_player.cpp:3286-3294`).
/// Unlike the game's resets of every player, it fires no
/// `scorestats_accumulated_reset`. The player resource then reports the drop
/// in their Score at its next think, as a change of Strange "Points Scored"
/// progress, or of Mann vs. Machine statistics, unless
/// [`PlayerScore::rebase`] follows this, as the
/// [module documentation](self#reporting-nothing) describes.
///
/// # Re-entrancy
///
/// Other code runs before this returns. Removing the relationships fires the
/// `remove_nemesis_relationships` game event (`tf_player.cpp:4005-4010`), so
/// every listener runs during the call: the game's, other plugins', map
/// scripts', and this plugin's own. For a human player, dropping the Mann vs.
/// Machine events sends the `MVMResetPlayerStats` user message
/// (`tf_mann_vs_machine_stats.cpp:361-373`), which plugins hooking user
/// messages see. Release every borrow of plugin state those may need, such as
/// a `RefCell` or thread-local, before calling this.
///
/// Fails if the server does not run TF2, or `player`'s datamaps do not include
/// `CTFPlayer`'s.
#[doc(alias("ResetScores"))]
pub fn reset_scores(server: Server<'_>, player: Entity<'_>) -> Result<(), ScoreError> {
	check_game(server)?;

	if !player.has_data_map_class(c"CTFPlayer") {
		return Err(ScoreError::NotTfPlayer);
	}

	let player = player.as_ptr().cast::<sys::CTFPlayer>();

	// SAFETY: The datamaps show a `CTFPlayer`, whose entity base, and so its
	// primary vtable, `sdk_raw::tf2` asserts is at offset zero, and the
	// generated field gives `ResetScores`' slot in it under both ABIs. The
	// game's own code resets statistics and relationships, and frees no entity.
	// It also fires a game event to every listener and, for a human player,
	// sends a user message, both before returning; `Server::new`'s contract
	// requires the code those reach to free entities only through deferred
	// deletion, so every entity of the scope stays allocated.
	unsafe {
		vcall!(player as sys::CTFPlayer__bindgen_vtable => CTFPlayer_ResetScores());
	}

	Ok(())
}

/// Finds the player resource again through `cache`, or anew if the cached
/// entity is gone or has another class.
fn resource_entity<'c, 's>(
	cache: &'c mut Option<ResourceCache>,
	context: Context<'s>,
) -> Result<(Entity<'s>, Edict<'s>, &'c ResourceCache), ScoreError> {
	let found = cache
		.as_ref()
		.and_then(|cached| live(context.tools, cached.handle, cached.class));

	let (entity, edict) = match found {
		Some(found) => found,

		None => {
			*cache = None;

			let entity = context
				.tools
				.find_by_class_name(None, RESOURCE_CLASS_NAME)
				.ok_or(ScoreError::NoPlayerResource)?;
			let (class, edict) = networking(entity)?;

			*cache = Some(ResourceCache::resolve(context.dll, entity, class));
			(entity, edict)
		}
	};

	let cached = cache.as_ref().ok_or(ScoreError::NoPlayerResource)?;

	Ok((entity, edict, cached))
}

/// Converts to `i32`, saturating.
fn saturate(value: i64) -> i32 {
	i32::try_from(value).unwrap_or(if value < 0 { i32::MIN } else { i32::MAX })
}

/// The offset of `m_iPoints` in the player's scoring data `table`, such as
/// `m_ScoreData`, from the player's class.
///
/// A name search finds only the first `m_iPoints`, so the variable is looked
/// up within the scoring data's own nested table.
fn scoring_points(
	dll: ServerGameDll<'_>,
	class: ServerClass<'_>,
	table: &'static CStr,
) -> Result<usize, ScoreError> {
	let data = dll.net_prop(class, table)?;

	if data.prop().kind() == PropKind::DataTable {
		for index in 0..data.element_count().unwrap_or(0) {
			let element = data.element(index)?;

			if element.prop().name() == c"m_iPoints" {
				int_range(element.prop(), element.storage(), "m_iPoints")?;

				return Ok(element.offset());
			}
		}
	}

	Err(NetPropError::NotFound {
		table: table.to_string_lossy().into_owned(),
		name: "m_iPoints".to_owned(),
	}
	.into())
}

/// Sets how many flags a team captured this round, as
/// `CTFTeam::SetFlagCaptures` does, and marks it changed for clients.
///
/// # Game consequences
///
/// This is the game's state, not only what clients see:
///
/// - In capture the flag, the game wins the round for a team whose captures
///   reach `tf_flag_caps_per_round` at its next check
///   (`tf_gamerules.cpp:9383-9419`).
/// - In PASS Time, the number is the team's score. The game wins the round for
///   a team whose score reaches `tf_passtime_scores_per_round` at its next
///   check (`tf_gamerules.cpp:9350-9378`) or when the ball next respawns
///   (`tf_passtime_logic.cpp:937-941`), and works out goals' points and the
///   winner when time runs out from it (`tf_passtime_logic.cpp:1345`,
///   `1706-1707`, `1785-1812`).
///
/// The game resets it when each round starts (`tf_gamerules.cpp:15089-15098`).
///
/// Fails as [`ScoreboardLayout::set_team_stat`] does; the SDK networks -128 to
/// 127.
#[doc(alias("SetFlagCaptures", "m_nFlagCaptures"))]
pub fn set_team_flag_captures(
	server: Server<'_>,
	team: ScoringTeam,
	captures: i32,
) -> Result<(), ScoreError> {
	ScoreboardLayout::new().set_team_stat(server, team, TeamStat::FlagCaptures, captures)
}

/// Sets a team's score, as `CTeam::SetScore` does, and marks it changed for
/// clients.
///
/// # Game consequences
///
/// This is the game's state, not only what clients see. A score reaching
/// `mp_winlimit`, or leading by `mp_windifference`, ends the map at the game's
/// next check (`tf_gamerules.cpp:9424-9470`), and arena and team balancing use
/// it too. Restarts and some modes reset it.
///
/// Fails as [`ScoreboardLayout::set_team_stat`] does.
#[doc(alias("SetScore", "m_iScore"))]
pub fn set_team_score(server: Server<'_>, team: ScoringTeam, score: i32) -> Result<(), ScoreError> {
	ScoreboardLayout::new().set_team_stat(server, team, TeamStat::Score, score)
}

/// Finds a team's entity again through `cache`, or anew if the cached entity
/// is gone or has another class.
fn team_entity<'c, 's>(
	cache: &'c mut Option<TeamCache>,
	context: Context<'s>,
	team: ScoringTeam,
) -> Result<(Entity<'s>, Edict<'s>, &'c TeamCache), ScoreError> {
	let found = cache
		.as_ref()
		.and_then(|cached| live(context.tools, cached.handle, cached.class));

	let (entity, edict) = match found {
		Some(found) => found,

		None => {
			*cache = None;

			let entity = find_team(context, team)?;
			let (class, edict) = networking(entity)?;

			*cache = Some(TeamCache::resolve(context.dll, entity, class));
			(entity, edict)
		}
	};

	let cached = cache.as_ref().ok_or(ScoreError::NoTeam(team))?;

	Ok((entity, edict, cached))
}

/// Writes a team's variable and marks it changed, as the game's setters do.
fn write_team_variable(
	variable: IntField<'_>,
	value: i32,
	edict: Edict<'_>,
	engine: ValveEngine<'_>,
) {
	let mut changes = Changes::default();

	variable.write(value);
	changes.mark(variable.offset);
	changes.flush(engine, edict);
}