bevy_society 0.2.0

Living societies for Bevy: personalities, moods, needs, memories, relationships, opinions, rumors, reputation and factions; utility-AI daily lives; Rhai triggers; dialogue through bevy_speech_bubbles.
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
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
//! Daily-life data types: what a person is doing and where it is walking to.
//!
//! The decision and arrival systems that fill these components belong to `BehaviorPlugin`. This
//! module only defines the shared shapes, so the steering plugin and game code can read them
//! without depending on the behaviour systems.

use bevy::ecs::query::QueryData;
use bevy::prelude::*;
use bevy::transform::components::Transform;
use bevy_emerge_rng::DetRng;

use crate::Tuning;
use crate::core::{Perception, PersonIndex, SocietyClock, SocietySettings};
use crate::defs::{ActivityId, ActivityTarget, Signal, SocietyDefs, Term};
use crate::groups::Memberships;
use crate::mind::{Emotions, Mood, Needs, Personality};
use crate::relationships::Ties;
use crate::rumor::RumorBook;

/// The target of a [`Doing`] activity.
#[derive(Reflect, Debug, Clone, Copy, PartialEq)]
pub enum DoingTarget {
    /// The activity needs no place or person (for example `idle`, or `sleep` at home).
    Itself,
    /// A point on the society plane, chosen by a wander option.
    Point(Vec2),
    /// A place entity, chosen by a tag option.
    Place(Entity),
    /// Another person, chosen by a person option.
    Person(Entity),
}

/// The activity a person is currently performing.
///
/// A person re-decides only when its `Doing` ends. While `arrived_day` is `None` the person is
/// travelling; the day it is set, the activity's needs and duration start counting.
#[derive(Component, Reflect, Debug, Clone, PartialEq)]
#[reflect(Component)]
pub struct Doing {
    /// The activity definition this person is following.
    pub activity: ActivityId,
    /// What the activity is directed at.
    pub target: DoingTarget,
    /// Game day the activity started.
    pub started_day: f64,
    /// Game day the person reached the target, or `None` while still travelling.
    pub arrived_day: Option<f64>,
}

impl Doing {
    /// Whether the person is still walking to the activity's target, and so is being paid nothing yet.
    ///
    /// The activity's needs and duration start counting only once `arrived_day` is set, so a reader that
    /// wants "what is this person doing **now**" must check this first. [`Doing::label`] does it for you.
    pub fn is_travelling(&self) -> bool {
        self.arrived_day.is_none()
    }

    /// The honest answer to "what is this person doing?", for a HUD, a log or a script.
    ///
    /// `Doing::activity` names the activity that was **chosen**, from the moment it is chosen, so reading it
    /// raw reports a person as asleep while it is still walking to bed. Measured on the shipped village at
    /// 64 s/day, that over-reports sleep by more than three points of every person-tick. The convention here is
    /// `"travelling:<activity>"` while `arrived_day` is `None`, and the bare activity name once it is set.
    ///
    /// This is the convention the crate's own evidence harness had to invent privately before it existed,
    /// which is the argument for owning it here: a host that reads `activity` alone gets a number nobody can
    /// interpret, and the fix should not depend on every consumer rediscovering the same workaround.
    pub fn label(&self, defs: &crate::defs::SocietyDefs) -> String {
        let base = defs.activity_name(self.activity);
        if self.is_travelling() {
            format!("travelling:{base}")
        } else {
            base.to_owned()
        }
    }
}

/// Where a travelling person is walking to, on the society plane.
///
/// Inserted and updated while travelling and removed on arrival or when the activity ends. A host
/// that does not steer moves people with [`Tuning::walk_speed`](crate::Tuning) — see
/// `walk_toward_destination`. A host that adds a `SocialMover` per person steers them itself and this
/// system leaves them alone.
#[derive(Component, Reflect, Debug, Clone, Copy, PartialEq, Default)]
#[reflect(Component)]
pub struct Destination(pub Vec2);

// -------------------------------------------------------------------------------------------------
// Messages
// -------------------------------------------------------------------------------------------------

/// A social exchange finished (plan D7).
///
/// `refused_by_rule` distinguishes "the target said no" from "the `requires` script said no", which a
/// host usually wants to treat differently.
#[derive(Message, Debug, Clone, PartialEq, Eq)]
pub struct InteractionResolved {
    /// The person who started the exchange.
    pub actor: Entity,
    /// The person it was directed at.
    pub target: Entity,
    /// Name of the `InteractionDef` that ran.
    pub interaction: String,
    /// Whether the target accepted.
    pub accepted: bool,
    /// Whether a `requires` script refused it before the acceptance draw.
    pub refused_by_rule: bool,
    /// The winner of a contest, when the interaction had one.
    pub winner: Option<Entity>,
}

/// A person began doing an activity (plan D7).
///
/// Written when the person **decides**, which is the same instant its [`Doing`] is inserted and
/// `Doing::started_day` is stamped — not when it arrives. The pair therefore brackets the component:
/// one [`ActivityStarted`] when `Doing` appears, one [`ActivityEnded`] when it is removed, including
/// when a walk times out and the activity never runs. A host may read an end as the close of
/// something it was told about, whatever the reason it closed.
///
/// Arrival is a separate fact: it begins the activity's duration and its `satisfies` payout, and is
/// visible on [`Doing::arrived_day`].
#[derive(Message, Debug, Clone, PartialEq, Eq)]
pub struct ActivityStarted {
    /// The person.
    pub person: Entity,
    /// Name of the activity.
    pub activity: String,
    /// The person it is directed at, when it is a `Person` target.
    pub target: Option<Entity>,
}

/// A person stopped doing an activity (plan D7).
#[derive(Message, Debug, Clone, PartialEq, Eq)]
pub struct ActivityEnded {
    /// The person.
    pub person: Entity,
    /// Name of the activity.
    pub activity: String,
    /// Whether the activity ran its full duration rather than being interrupted or timing out.
    pub completed: bool,
}

// -------------------------------------------------------------------------------------------------
// Queues
// -------------------------------------------------------------------------------------------------

/// [`Interact`](crate::Interact) requests waiting for the next society tick.
///
/// Drained on every fixed step, so a request written between two ticks is not dropped by Bevy's
/// two-swap message buffer.
#[derive(Resource, Debug, Clone, Default)]
pub struct InteractQueue(pub(crate) Vec<crate::interact::Interact>);

/// Exchanges that became due because a person arrived at a `Person` target (plan D7), in
/// [`PersonIndex::sorted`] order.
#[derive(Resource, Debug, Clone, Default)]
pub struct ArrivalQueue(pub(crate) Vec<crate::interact::Interact>);

/// Drain [`Interact`](crate::Interact) into [`InteractQueue`].
///
/// Ungated, in [`crate::SocietySystems::Clock`]: a game writes an `Interact` whenever it likes, and a
/// society tick is sixteen fixed steps away, so a tick-gated drain would lose most requests.
fn queue_interacts(
    mut reader: MessageReader<crate::interact::Interact>,
    mut queue: Option<ResMut<InteractQueue>>,
) {
    let Some(queue) = queue.as_mut() else {
        return;
    };
    for message in reader.read() {
        queue.0.push(message.clone());
    }
}

// -------------------------------------------------------------------------------------------------
// Plugin
// -------------------------------------------------------------------------------------------------

/// Runs daily life and social exchanges (plan D7): autonomous people choose activities, travel,
/// arrive, satisfy needs, and exchange with each other.
///
/// Requires [`crate::SocietyCorePlugin`]. Decision and exchange are exclusive systems: a decision
/// reads ties, needs, personality, mood, emotions, places and the clock for one person and writes
/// that person's `Doing`; an exchange can touch the actor, the target and every witness, which no
/// single `Query` can borrow at once.
pub struct BehaviorPlugin;

impl Plugin for BehaviorPlugin {
    fn build(&self, app: &mut App) {
        app.register_type::<Doing>()
            .register_type::<DoingTarget>()
            .register_type::<Destination>()
            .init_resource::<InteractQueue>()
            .init_resource::<ArrivalQueue>()
            .init_resource::<crate::rumor::RumorBook>()
            .add_message::<InteractionResolved>()
            .add_message::<ActivityStarted>()
            .add_message::<ActivityEnded>()
            .add_message::<crate::interact::Interact>()
            .add_message::<crate::interact::SocietyBark>()
            .add_message::<crate::script::ScriptError>()
            .add_message::<crate::rumor::RumorLearned>()
            .add_message::<crate::reputation::NormViolated>()
            .add_systems(
                FixedUpdate,
                queue_interacts.in_set(crate::SocietySystems::Clock),
            )
            .init_resource::<EndRequests>()
            .add_message::<TravelFailed>()
            .add_message::<PersonDied>()
            .init_resource::<DeathQueue>()
            .add_observer(on_deceased_added)
            .add_message::<EndActivity>()
            .add_message::<ReportDeed>()
            .init_resource::<DeedReportQueue>()
            .add_systems(
                FixedUpdate,
                queue_reported_deeds.in_set(crate::SocietySystems::Clock),
            )
            .add_systems(
                FixedUpdate,
                // Ungated, in `Clock`: a host writes `TravelFailed` whenever it likes, and a
                // message written between two society ticks must survive to the tick that reads it.
                queue_end_requests.in_set(crate::SocietySystems::Clock),
            )
            .add_systems(
                FixedUpdate,
                (end_activities, decide_activities)
                    .chain()
                    .in_set(crate::SocietySystems::Decide),
            )
            .add_systems(
                FixedUpdate,
                // The walk runs every fixed step, not only on society ticks, so motion stays smooth
                // while the simulation ticks slowly — the same rule `steer_movers` follows.
                walk_toward_destination.after(crate::SocietySystems::Rules),
            )
            .add_systems(
                FixedUpdate,
                // **Before `apply_appraisals`**, so the grief a death produces counts in the tick it
                // happened. `apply_appraisals` is in `Clock`, which is earlier than `Interact`, so this
                // joins `Clock` and orders itself against that system explicitly.
                announce_deaths
                    .in_set(crate::SocietySystems::Clock)
                    .before(crate::mind::apply_appraisals),
            )
            .add_systems(
                FixedUpdate,
                (
                    arrive_and_satisfy,
                    resolve_interactions,
                    resolve_reported_deeds,
                )
                    .chain()
                    .in_set(crate::SocietySystems::Interact),
            )
            .add_systems(
                FixedUpdate,
                activity_barks.in_set(crate::SocietySystems::Interact),
            )
            // Contact gossip reads the contact list `expose_contacts` has just written, so it runs in
            // `Relate` and queues into the same `InteractQueue` the arrival path uses.
            .add_systems(
                FixedUpdate,
                contact_gossip.in_set(crate::SocietySystems::Relate),
            )
            .add_systems(
                FixedUpdate,
                report_to_triggers
                    .after(crate::SocietySystems::Interact)
                    .before(crate::SocietySystems::Rules),
            );
    }

    fn finish(&self, app: &mut App) {
        crate::core::require_core(app, "BehaviorPlugin");
        // Both resources are present: `SocietyCorePlugin` initialized them.
        let settings = app.world().resource::<SocietySettings>().clone();
        let defs = app.world().resource::<SocietyDefs>();
        warn_short_activities(defs, &settings);
        #[cfg(not(feature = "fields"))]
        {
            warn_unscored_fields(defs);
        }
        warn_short_travel_reach(&settings);
        warn_walk_outlasts_activity(defs, &settings);
        warn_unbalanced_needs(defs);
        app.add_systems(Startup, warn_unreachable_places);
    }
}

/// Warns about a need whose best source cannot cover its decay at a plausible rate.
///
/// Every need decays per game-day and is supplied by activities that pay `satisfies · duration_hours`
/// per use. A need whose source pays less than its decay needs more than one use a day; one whose
/// source is also gated to a few hours, or sits behind a walk, will starve in practice. Measured on the
/// starter pack: `fun` decays 0.8/day, its only source `relax` pays 0.6 per use, and a person therefore
/// needs 1.33 of them a day — while a 24-person village manages 0.10, so `fun` sits at zero for a third
/// of every run.
///
/// The threshold is **four uses a day**, not one. A cheap `Person`-targeted source that pays little is
/// fine — `social` needs 2.78 chats a day and gets them, because the other person walks to the actor
/// and a chat is three ticks. The crate cannot tell from definitions alone which sources are expensive
/// to reach, so it warns only where the count is implausible for any activity shape.
fn warn_unbalanced_needs(defs: &SocietyDefs) {
    let mut starving: Vec<String> = Vec::new();
    for (index, need) in defs.needs.iter().enumerate() {
        let Some(best) = defs
            .activities
            .iter()
            .filter_map(|activity| {
                let rate = activity
                    .satisfies
                    .iter()
                    .find(|(slot, _)| usize::from(slot.0) == index)
                    .map(|(_, rate)| *rate)?;
                Some((activity.name.as_str(), rate * activity.duration_hours))
            })
            .fold(None::<(&str, f32)>, |best, candidate| match best {
                Some((_, amount)) if amount >= candidate.1 => best,
                _ => Some(candidate),
            })
        else {
            starving.push(format!("{} (no activity satisfies it)", need.name));
            continue;
        };
        if best.1 <= 0.0 {
            starving.push(format!("{} ({} supplies nothing)", need.name, best.0));
            continue;
        }
        let uses_per_day = need.decay_per_day / best.1;
        if uses_per_day > MAX_USES_PER_DAY {
            starving.push(format!(
                "{} (best source {} pays {:.2} per use against {:.2}/day decay, so {:.2} uses a day)",
                need.name, best.0, best.1, need.decay_per_day, uses_per_day
            ));
        }
    }
    if starving.is_empty() {
        return;
    }
    warn!(
        "bevy_society: {} needs cannot be met by a plausible number of activities: {}. A need whose \
         best source supplies less than a quarter of its daily decay will starve whatever the choice \
         rule does.",
        starving.len(),
        starving.join("; ")
    );
}

/// The most uses a day a need's best source may require before the crate says it is unbalanced.
///
/// `social` sits at 2.78 and is fine; a source behind a walk and an hour window at that count is not.
const MAX_USES_PER_DAY: f32 = 4.0;

/// Warns that `travel_timeout_hours` and the day length disagree badly enough that walks will time out.
///
/// Two clocks meet at a walk: [`SocialMover::speed`](crate::SocialMover) is metres per **real** second
/// applied at the fixed step, while `travel_timeout_hours` is **game** hours. A host sets both, and
/// nothing told it they must agree. The coupling is `needs-decision/travel-time-in-two-clocks.md`,
/// still open — but the *silence* is not a decision the crate may defer, because it can compute how
/// much walking a timeout buys.
///
/// Measured on the project's own village at `seconds_per_day = 4`, where a 40-hour timeout is 426
/// ticks: an 8.2 m walk takes 308 ticks and fits, and the 13 m corner takes **527** and does not. The
/// far corner's people always time out, 78 of 108 `eat`s were interrupted, and `food` sat at 0.000
/// for the whole run.
///
/// The warning fires only when the timeout buys less than `MIN_WALK_METRES`, a distance no village
/// layout can be smaller than and still be a village. Above that the crate stays quiet: the reachable
/// distance depends on the host's layout, which `finish` cannot see.
fn warn_short_travel_reach(settings: &SocietySettings) {
    // **Walking is the host's under `Locomotion::Host`, and there may be no timeout at all.** Either
    // way this check has nothing to say: its whole subject is a crate walk bounded by a crate timeout.
    if settings.locomotion != crate::Locomotion::Builtin {
        return;
    }
    let Some(timeout_hours) = settings.tuning.travel_timeout_hours else {
        return;
    };
    let seconds_per_tick = f64::from(settings.tick_secs);
    let day_seconds = f64::from(settings.seconds_per_day);
    if seconds_per_tick <= 0.0 || day_seconds <= 0.0 {
        return;
    }
    let ticks_available = f64::from(timeout_hours) / 24.0 / (seconds_per_tick / day_seconds);
    // The default walking speed. Named here because the warning must speak for the default a host
    // gets; a host that sets its own `SocialMover::speed` is beyond what this check can see.
    let speed = 1.4_f64;
    let metres = speed * seconds_per_tick * ticks_available;
    if metres >= MIN_WALK_METRES {
        return;
    }
    warn!(
        "bevy_society: a travel_timeout_hours of {} at seconds_per_day {} allows {:.0} ticks, which at \
         {speed} m/s is only {:.1} metres of walking — less than {MIN_WALK_METRES:.0} m. People whose \
         activity is further away will always time out, and the needs those activities satisfy will \
         never be met. Raise travel_timeout_hours or shorten the walks.",
        timeout_hours, settings.seconds_per_day, ticks_available, metres
    );
}

/// Warns that `seconds_per_day` makes a typical walk longer than the activities it reaches.
///
/// The same two clocks meet, and this is the consequence the timeout warning cannot see: a walk costs
/// `seconds_per_day`, but every `duration_hours` is fixed in game time. So the **pacing knob rescales
/// every journey against every activity**, and the society changes when a host only meant to change how
/// fast it watches.
///
/// Measured on `village-evening`, seed 7, the same 16.4 game-days and the same 24 people, with only
/// `seconds_per_day` varied:
///
/// | `seconds_per_day` | travelling | need mean |
/// |---|---|---|
/// | 64 | **58.9%** | 0.545 |
/// | 144 | 50.4% | 0.603 |
/// | 480 | **43.9%** | 0.624 |
///
/// Fifteen points of a villager's life, from a setting that reads like a rendering speed. The crate's
/// own rule is that every rate is per game-day; walking is the exception, so the walk must be checked
/// against the activities rather than assumed consistent.
///
/// The check compares `MIN_WALK_METRES` of walking against the **mean** activity duration. The mean, not
/// the median, because a day's balance is carried by its long activities: the starter pack's median is
/// 0.5 h, which a 240-second day's 0.71 h walk already exceeds, yet that day is healthy (`food` 0.494,
/// needs 0.621). Over the mean the check separates the two cases that matter — the project's own
/// `village-evening` at 64 s/day, where a 10 m walk costs 2.68 game-hours against a 1.61 h mean and
/// villagers spend **58.9%** of their ticks travelling; and the shipped 240-second default, where the
/// walk is 0.71 h and the same measure is 46.8% with every need met.
fn warn_walk_outlasts_activity(defs: &SocietyDefs, settings: &SocietySettings) {
    // Walking is the host's under `Locomotion::Host`: how long a journey takes is its business, not a
    // consequence of `seconds_per_day`, so this warning would be about a walk the crate never takes.
    if settings.locomotion != crate::Locomotion::Builtin {
        return;
    }
    let day_seconds = f64::from(settings.seconds_per_day);
    if day_seconds <= 0.0 {
        return;
    }
    let durations: Vec<f64> = defs
        .activities
        .iter()
        .enumerate()
        .filter(|(index, _)| *index != usize::from(crate::defs::IDLE_ACTIVITY.0))
        .map(|(_, activity)| f64::from(activity.duration_hours))
        .collect();
    if durations.is_empty() {
        return;
    }
    let mean = durations.iter().sum::<f64>() / durations.len() as f64;
    // The default walking speed, for the same reason `warn_short_travel_reach` names it: the warning
    // speaks for the default a host gets, and cannot see a host's own `SocialMover::speed`.
    let speed = 1.4_f64;
    let walk_hours = MIN_WALK_METRES / speed / day_seconds * 24.0;
    if walk_hours <= mean {
        return;
    }
    warn!(
        "bevy_society: at seconds_per_day {} a {MIN_WALK_METRES:.0} m walk costs {walk_hours:.2} game-hours, \
         longer than the mean activity ({mean:.2}h) — people will spend most of the day walking to \
         things that are over before they arrive, and the needs those activities satisfy will lag. This is \
         the coupling `needs-decision/travel-time-in-two-clocks.md` records: walking is in real time and \
         durations are in game time. Lengthen the activities, raise seconds_per_day, or move the places \
         closer together.",
        settings.seconds_per_day
    );
}

/// The shortest walk a village layout can reasonably ask for, in metres. Below this a timeout is
/// certainly too short; above it the crate cannot tell, because it does not know the layout.
const MIN_WALK_METRES: f64 = 10.0;

/// Warns when `travel_timeout_hours` cannot cover a walk between two places the world actually has.
///
/// [`warn_short_travel_reach`] has to stay quiet above `MIN_WALK_METRES` because `finish` cannot see the
/// layout. **A system can.** Every `Place` is an entity with a `Transform`, so at `Startup` the crate knows
/// exactly how far apart the places are and can say whether the timeout reaches them.
///
/// Measured on the project's own `village-evening`: a 4-game-hour timeout at 64 s/day buys **14.9 m**, the
/// farthest place-to-place walk in that village is **16.4 m**, and **150 of 400** interrupted activities
/// were `sleep` walks that ran out of time before arrival — every one of them `timed_out=true`. The old
/// warning stayed quiet throughout, because 14.9 m is above its 10 m threshold. Being above a generic floor
/// and being able to reach your own tavern are different things.
fn warn_unreachable_places(
    settings: Res<SocietySettings>,
    places: Query<(Entity, &Name, &Transform), With<crate::core::Place>>,
    homes: Query<&crate::Home>,
) {
    if settings.locomotion != crate::Locomotion::Builtin {
        return;
    }
    let Some(timeout_hours) = settings.tuning.travel_timeout_hours else {
        return;
    };
    let seconds_per_tick = f64::from(settings.tick_secs);
    let day_seconds = f64::from(settings.seconds_per_day);
    if seconds_per_tick <= 0.0 || day_seconds <= 0.0 {
        return;
    }
    let ticks_available = f64::from(timeout_hours) / 24.0 / (seconds_per_tick / day_seconds);
    // The default walking speed, for the same reason `warn_short_travel_reach` names it.
    let speed = 1.4_f64;
    let reachable = speed * seconds_per_tick * ticks_available;

    let plane = settings.plane;
    let mut worst = 0.0_f32;
    let mut worst_pair = (String::new(), String::new());
    for (_, name, transform) in &places {
        for (_, other_name, other) in &places {
            let here = plane.project(transform.translation);
            let there = plane.project(other.translation);
            let distance = here.distance(there);
            if distance > worst {
                worst = distance;
                worst_pair = (name.as_str().to_owned(), other_name.as_str().to_owned());
            }
        }
    }
    // **Homes holding more people than their arrival disc can seat.** Everyone goes to bed at the same hour,
    // so every resident of one place aims at the same coordinate inside `interaction_radius`. ORCA holds two
    // centres one body diameter apart, so the disc packs solid and the queue behind it times out. Measured on
    // the project's own village: **150 sleeps interrupted**, and at every one the goal already held a median of
    // **6** bodies — exactly that home's declared capacity.
    let mut residents: std::collections::BTreeMap<Entity, usize> =
        std::collections::BTreeMap::new();
    for home in &homes {
        *residents.entry(home.0).or_insert(0) += 1;
    }
    let body_diameter = 0.7_f32;
    for (entity, name, _) in &places {
        let Some(&count) = residents.get(&entity) else {
            continue;
        };
        // How many bodies the arrival disc can seat, and the radius that would seat them all. Hex
        // packing overestimates what ORCA manages (everyone aims at the centre rather than around it), so
        // this is a lower bound on the crowding, not a simulation of it.
        let seats = (settings.interaction_radius / body_diameter).powi(2);
        if (count as f32) <= seats || count < 3 {
            continue;
        }
        let needed = body_diameter * (count as f32).sqrt();
        warn!(
            "bevy_society: '{place}' is home to {count} people, but an arrival disc of radius {radius} seats \
             about {seats}. They all go to bed at the same hour, so the queue behind the disc times out and \
             their sleeps are interrupted. Give them more room: raise `interaction_radius` to about {needed:.1} \
             or spread the residents over more homes.",
            place = name.as_str(),
            radius = settings.interaction_radius,
            seats = seats as usize,
            needed = needed,
        );
    }
    if worst <= reachable as f32 || places.iter().len() < 2 {
        return;
    }
    warn!(
        "bevy_society: a travel_timeout_hours of {timeout_hours} at seconds_per_day {} buys {reachable:.1}          metres of walking, but the farthest pair of places is '{a}' -> '{b}' at {worst:.1} m. Every walk          between them times out before arrival, so the activities that need one end interrupted and the          needs they satisfy are never met. Raise travel_timeout_hours to at least {needed:.1}, or move the          places closer.",
        settings.seconds_per_day,
        a = worst_pair.0,
        b = worst_pair.1,
        needed = f64::from(worst) * 24.0 / (speed * day_seconds),
    );
}

/// Warns when a definition reads a social field that **cannot be populated**.
///
/// `Signal::Field` and `ActivityTarget::Gradient` were both dead: the signal returned a constant `0.0`
/// and the gradient produced no targets, so a definition using either compiled and could never be
/// chosen. Both are wired now — measured, an activity whose only consideration is `Field(Crowd)` is
/// chosen 6 times in 2 400 ticks against a 1-hour fallback.
///
/// What remains worth warning about is the **feature being off**. The grids are built by
/// `SocialFieldPlugin`, which only exists with the `fields` feature; without it any `Field` definition
/// is still dead, and the definitions file accepts it either way. That is the same silence as before,
/// one layer down.
#[cfg_attr(feature = "fields", allow(dead_code))]
fn warn_unscored_fields(defs: &SocietyDefs) {
    let mut offenders: Vec<&str> = Vec::new();
    for (name, signals) in defs.activity_signals() {
        // An activity with *both* a Field input and a Gradient target is one offender, not two: it was
        // counted twice before, which made the warning overstate how many definitions were affected.
        let reads_a_field = signals
            .iter()
            .any(|signal| matches!(signal, crate::defs::Signal::Field(_)));
        let walks_a_gradient = defs.activity_targets_gradient(name);
        if reads_a_field || walks_a_gradient {
            offenders.push(name);
        }
    }
    if offenders.is_empty() {
        return;
    }
    warn!(
        "bevy_society: {} definitions read social fields (`Field` considerations or a `Gradient` \
         target) but the `fields` feature is off, so no grid exists and those activities will never be \
         chosen. Enable `fields`, or remove the definitions. See `docs/loop/needs-decision/` — {}",
        offenders.len(),
        offenders.join(", ")
    );
}

/// Warns about activities that are shorter than a couple of society ticks.
///
/// `ActivitySpec::duration_hours` is game hours and one tick is `dt_days`, so the two only
/// correspond for a given `seconds_per_day`: a 0.1-hour `snub` lasts four ticks in the default
/// 240-second day and **one** tick in a four-second day. An activity that ends on the tick it
/// starts degenerates the society — a person re-decides every tick and repeats whatever scored
/// highest — and that is silent. Measured, a crowd ran `snub` 38 times per person per game-day
/// before anyone noticed.
///
/// The crate cannot fix the mismatch (which clock walking and durations belong to is a design
/// decision), but it can say so at startup instead of leaving a host to discover it in a plot.
fn warn_short_activities(defs: &SocietyDefs, settings: &SocietySettings) {
    let tick_hours = f64::from(settings.tick_secs) / f64::from(settings.seconds_per_day) * 24.0;
    if tick_hours <= 0.0 {
        return;
    }
    let mut degenerate: Vec<String> = Vec::new();
    for (index, activity) in defs.activities.iter().enumerate() {
        // The reserved idle activity is a one-hour fallback and is meant to be short.
        if index == usize::from(crate::defs::IDLE_ACTIVITY.0) {
            continue;
        }
        let ticks = f64::from(activity.duration_hours) / tick_hours;
        if ticks < 2.0 {
            degenerate.push(format!(
                "{} ({:.1}h = {:.1} ticks)",
                activity.name, activity.duration_hours, ticks
            ));
        }
    }
    if degenerate.is_empty() {
        return;
    }
    warn!(
        "bevy_society: {} activities are shorter than two society ticks at seconds_per_day {} \
         (one tick is {:.3} game-hours), so their people will re-decide every tick: {}",
        degenerate.len(),
        settings.seconds_per_day,
        tick_hours,
        degenerate.join(", ")
    );
}

// -------------------------------------------------------------------------------------------------
// Arrival
// -------------------------------------------------------------------------------------------------

/// Marks people who have reached their target and feeds the needs they are satisfying (plan D7).
///
/// A person has arrived within `interaction_radius` of the target. For a `Person` target the
/// destination is the target's position offset `0.75 · interaction_radius` toward the actor, so two
/// people stop short of each other rather than occupying the same point.
#[allow(clippy::too_many_arguments)]
fn arrive_and_satisfy(
    clock: Res<SocietyClock>,
    settings: Res<SocietySettings>,
    defs: Res<SocietyDefs>,
    index: Res<PersonIndex>,
    perception: Res<Perception>,
    transforms: Query<&GlobalTransform>,
    mut commands: Commands,
    mut people: Query<(&mut Doing, &mut Needs)>,
    mut arrivals: Option<ResMut<ArrivalQueue>>,
) {
    let now = clock.now_days;
    let dt_hours = f64::from(clock.dt_days) * 24.0;
    let radius = settings.interaction_radius;
    let plane = settings.plane;

    for (slot, &(_, person)) in index.sorted.iter().enumerate() {
        let Ok((mut doing, mut needs)) = people.get_mut(person) else {
            continue;
        };
        let Some(spec) = defs.activities.get(usize::from(doing.activity.0)).cloned() else {
            continue;
        };
        let here = perception.pos.get(slot).copied().unwrap_or(Vec2::ZERO);

        if doing.arrived_day.is_none() {
            let Some(target_pos) = target_position(
                doing.target,
                person,
                here,
                &transforms,
                &index,
                plane,
                radius,
            ) else {
                // A `Person` target that despawned: the activity is over.
                continue;
            };
            // **A `Person` target moves, so its `Destination` is rewritten every tick**, whichever
            // locomotion the host chose. Otherwise a chase would follow where the target *was* when the
            // activity started, which under `Locomotion::Host` means the host is asked to walk to a
            // stale point and reports failure for a walk that was never impossible.
            //
            // This is before the arrival gate below on purpose: a person still far away is exactly the
            // one that needs its Destination updated, and the gate `continue`s.
            if let DoingTarget::Person(_) = doing.target {
                commands.entity(person).insert(Destination(target_pos));
            }
            // **A crowded place is still an arrival.** Everyone sleeps at the same hour, so every resident
            // aims at one coordinate inside `interaction_radius`; ORCA holds centres one body diameter apart,
            // so the disc packs solid and the queue behind it times out. Measured: the goal already holds a
            // median of 6 bodies when a sleep fails, and a 1.5 m disc seats about 4.6.
            let crowd = match doing.target {
                DoingTarget::Place(_) => perception
                    .pos
                    .iter()
                    .filter(|p| p.distance(target_pos) <= radius)
                    .count(),
                _ => 0,
            };
            let allowed = radius + settings.tuning.crowded_arrival_growth * (crowd as f32).sqrt();
            if here.distance(target_pos) > allowed {
                continue;
            }
            doing.arrived_day = Some(now);
            commands.entity(person).remove::<Destination>();
            // No `ActivityStarted` here: the decision already wrote it, paired with the
            // `ActivityEnded` that closes it. Arrival begins the *duration* and the `satisfies`
            // payout, which is what `arrived_day` is for.
            if let Some(arrivals) = arrivals.as_mut()
                && let Some(interaction) = spec.interaction
            {
                arrivals.0.push(crate::interact::Interact {
                    actor: person,
                    target: match doing.target {
                        DoingTarget::Person(other) => other,
                        _ => person,
                    },
                    interaction: defs.interaction_name(interaction).to_owned(),
                });
            }
        }

        if doing.arrived_day.is_some() {
            // `satisfies` is a per-game-hour delta, so a tick is `dt_days · 24` hours of it.
            for &(need, delta) in &spec.satisfies {
                if let Some(level) = needs.0.get_mut(usize::from(need.0)) {
                    *level = (*level + f64::from(delta) as f32 * dt_hours as f32).clamp(0.0, 1.0);
                }
            }
        }
    }
}

/// Where a `Doing` target is on the society plane, or `None` when a person target is gone.
fn target_position(
    target: DoingTarget,
    _actor: Entity,
    actor_pos: Vec2,
    transforms: &Query<&GlobalTransform>,
    index: &PersonIndex,
    plane: crate::SocietyPlane,
    interaction_radius: f32,
) -> Option<Vec2> {
    let of = |entity: Entity| -> Option<Vec2> {
        let _ = index;
        transforms
            .get(entity)
            .ok()
            .map(|transform| plane.project(transform.translation()))
    };
    match target {
        DoingTarget::Itself | DoingTarget::Point(_) => Some(actor_pos),
        DoingTarget::Place(place) => of(place),
        DoingTarget::Person(other) => {
            let there = of(other)?;
            // Stop short of the other person rather than walking into them.
            let toward = (actor_pos - there).normalize_or_zero();
            Some(there + toward * 0.75 * interaction_radius)
        }
    }
}

// -------------------------------------------------------------------------------------------------
// Ending
// -------------------------------------------------------------------------------------------------

/// Moves every person with a [`Destination`] toward it, unless something else is steering them.
///
/// This is the promise `Destination`'s own doc comment makes — *"games without the steering plugin
/// move persons themselves toward this point"* — kept by the plugin that creates the destination.
/// Without it a society has no way to arrive: measured, a person given `Destination(6.5, -5.0)` sat at
/// `(0, 0)` for 600 ticks, every `Place`- and `Home`-targeted activity timed out, and the needs those
/// activities satisfy never rose.
///
/// A person carrying a `SocialMover` is skipped: that is the steering plugin's, and two systems
/// writing one `Transform` would fight. The speed is [`Tuning::walk_speed`], metres per real second
/// at the fixed step, which is the same unit `SocialMover::speed` uses.
#[cfg(feature = "steering")]
type NotSteered = Without<crate::SocialMover>;
/// Without the `steering` feature there is no `SocialMover` component to exclude, so every traveller
/// is walked by this system. The alias keeps one function body for both builds.
#[cfg(not(feature = "steering"))]
type NotSteered = ();

fn walk_toward_destination(
    settings: Res<SocietySettings>,
    time: Res<Time<Fixed>>,
    mut walkers: Query<(&Destination, &mut Transform), NotSteered>,
) {
    // **No crate system writes a person's `Transform` under `Locomotion::Host`.** That is the whole
    // contract of the setting: the host walks them, on its own ground, around its own houses.
    if settings.locomotion != crate::Locomotion::Builtin {
        return;
    }
    let step = (settings.tuning.walk_speed * time.delta_secs()) as f64;
    if step <= 0.0 {
        return;
    }
    let plane = settings.plane;
    for (destination, mut transform) in &mut walkers {
        let mut here = plane.project(transform.translation);
        let toward = destination.0 - here;
        let distance = toward.length();
        if distance <= f32::EPSILON {
            continue;
        }
        let advanced = if f64::from(distance) <= step {
            distance
        } else {
            step as f32
        };
        here += toward / distance * advanced;
        // Keep the body's own height: `SocietyPlane::lift` puts the other axis at zero, which is right
        // for a ground marker and wrong for a capsule.
        transform.translation = match plane {
            crate::SocietyPlane::XZ => Vec3::new(here.x, transform.translation.y, here.y),
            crate::SocietyPlane::XY => Vec3::new(here.x, here.y, transform.translation.z),
        };
    }
}

/// The host reports a deed the society could not see, naming the people who saw it.
///
/// Sanctuary resolves events the society has no eyes for: a townsperson found afflicted at dawn, a
/// person accused by a crowd at the well. They must enter reputation and rumor as seen by **the people
/// the game names** — the family, the work crew, the crowd at the well — and then spread person to
/// person. A deed that enters through an accepted `Interact` gets its witnesses from perception; this is
/// the same path with the witness list supplied instead.
///
/// ```ignore
/// commands.write_message(ReportDeed {
///     actor,
///     patient: Some(found_afflicted),
///     deed: "affliction".to_owned(),
///     witnesses: vec![kin, neighbour, priest],
/// });
/// ```
///
/// An unknown deed is a `warn!` and nothing happens. A witness that is not a person is dropped with one
/// warning per report. The actor and the patient are never witnesses of their own deed. With no
/// witnesses left, there is no rumor and no reaction — a deed nobody saw is not a deed.
#[derive(Message, Debug, Clone, PartialEq)]
pub struct ReportDeed {
    /// Who did it.
    pub actor: Entity,
    /// Who it was done to, when there was someone.
    pub patient: Option<Entity>,
    /// The deed's id, as the defs declare it.
    pub deed: String,
    /// The people who saw it, by name from the game. Order does not matter.
    pub witnesses: Vec<Entity>,
}

/// Host-reported deeds waiting for the next society tick.
///
/// **Drained by an ungated system in `Clock`**, the pattern `queue_interacts` and `EndRequests` use: a
/// society tick is many fixed steps apart and Bevy keeps a message for two buffer swaps, so a
/// `ReportDeed` written in `Update` would be gone before the tick that resolves it.
#[derive(Resource, Debug, Clone, Default)]
pub(crate) struct DeedReportQueue(pub(crate) Vec<ReportDeed>);

/// Drains every host-written [`ReportDeed`] into [`DeedReportQueue`] on every fixed step.
fn queue_reported_deeds(
    mut reports: MessageReader<ReportDeed>,
    mut queue: ResMut<DeedReportQueue>,
) {
    for report in reports.read() {
        queue.0.push(report.clone());
    }
}

/// The host asks for a person's current activity to end, without dormancy.
///
/// The use a host has: a place that has closed for the night, a shift that ended, an activity the game
/// has made impossible. The activity ends with `ActivityEnded { completed: false }` in the same tick
/// and the person re-decides. A request about a person with no activity is ignored.
#[derive(Message, Debug, Clone, Copy, PartialEq, Eq)]
pub struct EndActivity {
    /// The person whose activity should end.
    pub person: Entity,
}

/// A person died: written once, when [`Deceased`](crate::Deceased) is inserted.
///
/// The crate writes it itself, from an `On<Add<Deceased>>` observer, so a host gets it by inserting the
/// marker and nothing else. Use it to keep a roster, to spawn an undead under the dead person's name, or
/// to mark the town in mourning.
#[derive(Message, Debug, Clone, Copy, PartialEq, Eq)]
pub struct PersonDied {
    /// The person who died.
    pub person: Entity,
}

/// Deaths waiting for the next society tick.
///
/// **A resource rather than the message alone**, for the same reason `EndRequests` is one: an observer
/// runs whenever the world is mutated — possibly in `Update`, between two fixed steps — and the
/// bereavement pass must run inside the tick. The queue is drained once by [`announce_deaths`].
#[derive(Resource, Debug, Clone, Default)]
pub(crate) struct DeathQueue(pub(crate) Vec<Entity>);

/// Records a death the moment `Deceased` lands.
fn on_deceased_added(add: On<Add<crate::Deceased>>, mut queue: ResMut<DeathQueue>) {
    queue.0.push(add.entity);
}

/// Announces every death recorded since the last tick: the message, its trigger, and the bereavement.
///
/// **Runs before `apply_appraisals`**, so the grief a death produces counts in the tick it happened rather
/// than a tick later.
///
/// Bereavement is proportional to closeness, because that is what the literature says it is: Prigerson et
/// al. 2009 (DOI `10.1371/journal.pmed.1000121`) key prolonged grief disorder to the loss of a **close
/// relationship** rather than to any death, so a stranger's death is a fact and a friend's is a wound.
/// Intensity is `(0.5 · (1 + affinity)).clamp(0, 1)`: an enemy's death is not a grief, a tie at affinity 0
/// is half a grief, and a friend at 1.0 is a full one.
#[allow(clippy::too_many_arguments)]
fn announce_deaths(
    clock: Res<SocietyClock>,
    settings: Res<SocietySettings>,
    index: Res<PersonIndex>,
    deceased: Query<(), With<crate::Deceased>>,
    mut queue: ResMut<DeathQueue>,
    mut died: MessageWriter<PersonDied>,
    mut triggers: Option<ResMut<crate::rules::TriggerQueue>>,
    mut appraisals: MessageWriter<crate::Appraise>,
    mut memories: Query<&mut crate::Memories>,
    ties: Query<&Ties>,
) {
    let deaths = std::mem::take(&mut queue.0);
    if deaths.is_empty() {
        return;
    }
    let now = clock.now_days;
    // Sorted and deduped by `PersonId`, so two deaths in one tick give the same society however the
    // observer happened to fire, and a death recorded twice is announced once.
    let mut ordered: Vec<(u32, Entity)> = deaths
        .into_iter()
        .filter_map(|entity| {
            index
                .sorted
                .iter()
                .find(|(_, e)| *e == entity)
                .map(|(id, _)| (id.0, entity))
        })
        .collect();
    ordered.sort_unstable();
    ordered.dedup();

    for (_, person) in ordered {
        died.write(PersonDied { person });
        if let Some(triggers) = triggers.as_mut() {
            let actor = index
                .sorted
                .iter()
                .find(|(_, e)| *e == person)
                .map_or(-1, |(id, _)| i64::from(id.0));
            triggers
                .0
                .push(crate::rules::TriggerEvent::Deceased { actor });
        }

        // Everyone living who was close to the dead person grieves.
        for &(_, other) in &index.sorted {
            if other == person || deceased.get(other).is_ok() {
                continue;
            }
            let Ok(their_ties) = ties.get(other) else {
                continue;
            };
            let Some(tie) = their_ties.get(person) else {
                continue;
            };
            let close = tie.label == crate::TieLabel::Friend
                || tie.familiarity_peak >= settings.tuning.bereavement_familiarity;
            if !close {
                continue;
            }
            let intensity = (0.5 * (1.0 + tie.affinity)).clamp(0.0, 1.0);
            appraisals.write(crate::Appraise {
                person: other,
                kind: crate::EmotionKind::Distress,
                intensity,
                toward: Some(person),
            });
            if let Ok(mut theirs) = memories.get_mut(other) {
                theirs.record(
                    crate::Memory {
                        day: now as f32,
                        kind: crate::MemoryKind::Custom("bereaved".to_owned()),
                        other: Some(person),
                        valence: -intensity,
                        importance: 1.0,
                    },
                    now,
                    settings.memory_capacity,
                );
            }
        }
    }
}

/// The host reports that it cannot walk a person to its `Destination`.
///
/// Under [`Locomotion::Host`](crate::Locomotion::Host) the crate cannot see the host's paths, so it
/// cannot tell a slow walk from an impossible one. This is how the host says which. The activity ends
/// with `ActivityEnded { completed: false }` in the same tick, and the person re-decides.
#[derive(Message, Debug, Clone, Copy, PartialEq, Eq)]
pub struct TravelFailed {
    /// The person whose walk failed.
    pub person: Entity,
}

/// Requests to end an activity, drained from host-written messages so a request written between two
/// society ticks is never lost to a buffer swap.
///
/// **Ungated, in `Clock`**, because Bevy keeps a message for two buffer swaps and a society tick is
/// many fixed steps apart: a `TravelFailed` written in `Update` would be gone before the tick that
/// consumes it. The same pattern `queue_interacts` uses.
#[derive(Resource, Debug, Clone, Default)]
pub(crate) struct EndRequests(pub(crate) Vec<Entity>);

/// Drains every host-written end-request message into [`EndRequests`] on every fixed step.
fn queue_end_requests(
    mut travel_failed: MessageReader<TravelFailed>,
    mut end_activity: MessageReader<EndActivity>,
    mut requests: ResMut<EndRequests>,
) {
    for failed in travel_failed.read() {
        requests.0.push(failed.person);
    }
    for asked in end_activity.read() {
        requests.0.push(asked.person);
    }
}

/// Ends every activity that is over (plan D7): its duration passed, its `Person` target left
/// perception, or it never arrived within `travel_timeout_hours`.
///
/// Gathers first and writes after, so the messages and the removals happen outside any borrow of the
/// people it read.
#[allow(clippy::too_many_arguments)]
/// The two per-person queries `end_activities` reads.
///
/// Bundled because Bevy's system-parameter limit is a hard cap and `end_activities` is close to it
/// once the dormancy query joins the `Doing` one; this crate already bundles this way in
/// `TriggerBookkeeping`.
#[derive(bevy::ecs::system::SystemParam)]
struct ActivityQueries<'w, 's> {
    doing: Query<'w, 's, &'static Doing>,
    dormant: Query<'w, 's, (), With<crate::Dormant>>,
}

#[allow(clippy::too_many_arguments)]
fn end_activities(
    clock: Res<SocietyClock>,
    settings: Res<SocietySettings>,
    defs: Res<SocietyDefs>,
    index: Res<PersonIndex>,
    perception: Res<Perception>,
    mut commands: Commands,
    queries: ActivityQueries,
    mut ended: MessageWriter<ActivityEnded>,
    mut requests: ResMut<EndRequests>,
) {
    let ActivityQueries {
        doing: people,
        dormant,
    } = queries;
    let now = clock.now_days;
    // `None` means a walk never times out: only the host may declare it impossible, through
    // `TravelFailed`, because only the host can see whether a path exists.
    let timeout_hours = settings.tuning.travel_timeout_hours.map(f64::from);
    let mut endings: Vec<(Entity, String, bool)> = Vec::new();

    // **Host end-requests come first**, so a `TravelFailed` written this tick ends its activity in
    // this tick rather than a tick later, and so a person the host cancelled is not also counted as a
    // timeout below. `BTreeSet` order by `PersonId`, so several requests in one tick give the same
    // society however the host's vec happened to be ordered.
    let cancelled: std::collections::BTreeSet<u32> = std::mem::take(&mut requests.0)
        .into_iter()
        .filter_map(|entity| {
            index
                .sorted
                .iter()
                .find(|(_, e)| *e == entity)
                .map(|(id, _)| id.0)
        })
        .collect();
    for id in &cancelled {
        let Some(&(person_id, person)) = index.sorted.iter().find(|(p, _)| p.0 == *id) else {
            continue;
        };
        let _ = person_id;
        let Ok(doing) = people.get(person) else {
            // A request about a person with no activity, or one that no longer exists, is ignored:
            // the host reports what it observes, and a despawn mid-walk is not an error.
            continue;
        };
        if let Some(spec) = defs.activities.get(usize::from(doing.activity.0)) {
            endings.push((person, spec.name.clone(), false));
        }
    }

    for &(_, person) in &index.sorted {
        let Ok(doing) = people.get(person) else {
            continue;
        };
        let Some(spec) = defs.activities.get(usize::from(doing.activity.0)) else {
            continue;
        };
        if endings.iter().any(|(ended, _, _)| *ended == person) {
            continue;
        }
        // **A dormant person is out of its daily life, and an activity that targets a dormant person
        // can no longer happen.** Both end now, `completed: false`: dormancy is a suspension, not a
        // completion, and whoever was walking toward them has to choose again.
        let suspended = dormant.get(person).is_ok()
            || match doing.target {
                DoingTarget::Person(target) => dormant.get(target).is_ok(),
                _ => false,
            };
        if suspended {
            endings.push((person, spec.name.clone(), false));
            continue;
        }
        let (finished, completed) = match doing.arrived_day {
            None => {
                let timed_out =
                    timeout_hours.is_some_and(|hours| (now - doing.started_day) * 24.0 > hours);
                // A `Person` target that no longer exists can never be reached, so the activity ends
                // now rather than at a timeout that may not exist at all.
                // **`contains`, not `binary_search`.** `Perception::persons` is filled in
                // `PersonIndex.sorted` order — by `PersonId` — while an `Entity`'s ordering is by index
                // and generation. `binary_search` therefore reports "gone" for people who are present,
                // and every activity targeting them is interrupted on the very tick it starts. Measured
                // on `village-evening`: **1 045 of 1 045** `compliment` interruptions took this branch
                // with the target standing in a list of 24; `binary_search` said gone and `contains` said
                // present on the same call.
                let gone = match doing.target {
                    DoingTarget::Person(target) => !perception.persons.contains(&target),
                    _ => false,
                };
                (timed_out || gone, false)
            }
            Some(arrived) => {
                let done = (now - arrived) * 24.0 >= f64::from(spec.duration_hours);
                if done {
                    (true, true)
                } else if let DoingTarget::Person(target) = doing.target {
                    (!in_perception(&perception, &index, person, target), false)
                } else {
                    (false, false)
                }
            }
        };
        if finished {
            endings.push((person, spec.name.clone(), completed));
        }
    }

    for (person, activity, completed) in endings {
        ended.write(ActivityEnded {
            person,
            activity,
            completed,
        });
        commands.entity(person).remove::<(Doing, Destination)>();
    }
}

/// Whether `target` is within perception of `person`, compared by index position.
fn in_perception(
    perception: &Perception,
    index: &PersonIndex,
    person: Entity,
    target: Entity,
) -> bool {
    let Some(from) = index.sorted.iter().position(|(_, e)| *e == person) else {
        return false;
    };
    let Some(to) = index.sorted.iter().position(|(_, e)| *e == target) else {
        return false;
    };
    perception
        .near
        .get(from)
        .is_some_and(|list| list.iter().any(|(j, _)| *j as usize == to))
}

// -------------------------------------------------------------------------------------------------
// Deciding
// -------------------------------------------------------------------------------------------------

/// One place as a decision sees it (plan D7): its tag set, how full it is, and **where it is**.
///
/// The position is what makes [`Signal::Distance`] meaningful for a `Place`. Without it the signal reported
/// `1.0` — the maximum, "as far as it can see" — for every place, so a definition that asked for `Distance`
/// on a `Place` target scored `0` and the activity could never be chosen. Measured: adding
/// `Distance Linear(-1, 1)` to the starter pack's `eat` took `food` from a mean of 0.402 to **0.034**, at
/// zero 93% of the time. The activity was not made travel-aware; it was disabled.
struct PlaceView {
    entity: Entity,
    tags: Vec<String>,
    capacity: u32,
    used: u32,
    position: Vec2,
}

/// Everything scoring one candidate needs, copied out of the world once per decision (plan D7).
///
/// Copied rather than borrowed because a decision ends by writing `Doing` on the very person it
/// scored, and a `&Ties` from the world cannot coexist with `&mut World`.
struct Decision {
    position: Vec2,
    needs: Vec<f32>,
    /// The host-written signal values at the tick start, indexed by `SignalId`.
    signals: Vec<f32>,
    personality: Personality,
    mood: [f32; 3],
    emotions: Emotions,
    ties: Vec<crate::relationships::Tie>,
    /// Neighbours with their plane positions, for `Distance`.
    near: Vec<(Entity, Vec2)>,
    /// Places with their plane positions, so `Distance` works for a `Place` target too. Without these the
    /// signal reported the maximum for every place, which silently disabled any activity targeting one.
    place_positions: Vec<(Entity, Vec2)>,
    /// The person's factions at the tick start, for the `Field` inputs.
    own_factions: Vec<crate::FactionId>,
    /// The person's close ties at the tick start, for `SocialProof`.
    close_ties: Vec<Entity>,
    /// What each **close tie** is doing, so `SocialProof` can count who else chose this activity.
    ///
    /// Only the close ties, not the whole population. This used to be `all_doing.clone()` inside the
    /// per-person loop: N copies of an N-length vector per tick, which is exactly N² and was most of the
    /// scale curve. Measured on the benchmark: 14.55 ms a tick at 1 000 people and **254.5 ms** at 5 000,
    /// with the per-pair cost flat, i.e. the only variable was the population squared. `SocialProof` only
    /// ever looks at `close_ties`, so filtering to them is the same answer at a `close_ties`-sized cost.
    close_doing: Vec<(Entity, u16)>,
    hour: f32,
    perception_radius: f32,
}

/// The components a decision reads for one person.
#[derive(QueryData)]
#[query_data(mutable)]
struct Decider<'w> {
    ties: &'w Ties,
    memberships: &'w crate::Memberships,
    needs: &'w Needs,
    signals: &'w crate::mind::Signals,
    personality: &'w Personality,
    mood: &'w Mood,
    emotions: &'w Emotions,
    home: Option<&'w crate::Home>,
    bound: Option<&'w crate::BoundPlaces>,
}

/// Everything a `Signal::Script` consideration needs to be evaluated (plan D7).
///
/// The defs and the snapshot are the two things a script reads; the queue is where its actions go so
/// they land at the same application point as every other script's.
struct ScriptContext<'a> {
    defs: &'a SocietyDefs,
    snapshot: &'a std::sync::Arc<crate::snapshot::SocietySnapshot>,
    engine: &'a crate::script::ScriptEngine,
    actor: i64,
    target: i64,
}

/// One option a decision is weighing: an activity and what it points at.
#[derive(Debug, Clone)]
struct Candidate {
    activity: u16,
    /// The targets that tied for the activity's best score. The draw happens over *activities*
    /// (Dill's "option" is an action), then one of these is picked uniformly — so an activity with
    /// many eligible targets is not made more likely by having them.
    targets: Vec<DoingTarget>,
    weight: f32,
    score: f32,
}

/// The four queries a decision reads. Bundled because Bevy's system-parameter limit is a hard cap and
/// `decide_activities` was already at it before the bound-place check joined.
#[derive(bevy::ecs::system::SystemParam)]
struct DecisionQueries<'w, 's> {
    people: Query<'w, 's, Decider<'static>>,
    places: Query<
        'w,
        's,
        (
            Entity,
            &'static crate::Place,
            &'static crate::PlaceTags,
            &'static Transform,
        ),
    >,
    doing: Query<'w, 's, &'static Doing>,
}

/// Chooses an activity for every autonomous person that has none (plan D7).
///
/// Each consideration `s` is compensated to `s′ = s + (1 − s)·(1 − 1/n)·s` and the option scores
/// `weight · Π s′`. Options are cut to the highest `rank` that has any positive score, then to
/// `tier_cutoff × best`, then one is drawn by weight from a single `unit()` draw.
///
/// A person with no positive option takes the reserved idle activity for one game-hour. Without that
/// floor a stalled society would re-decide every tick and drain the RNG stream doing nothing.
#[allow(clippy::too_many_arguments)]
fn decide_activities(
    clock: Res<SocietyClock>,
    settings: Res<SocietySettings>,
    defs: Res<SocietyDefs>,
    perception: Res<Perception>,
    index: Res<PersonIndex>,
    mut rng: ResMut<crate::SocietyRng>,
    mut commands: Commands,
    world: DecisionQueries,
    // Two markers in one query: `Has` reads both without a second one, which is what keeps this
    // system inside Bevy's system-parameter cap.
    marks: Query<(Has<crate::Autonomous>, Has<crate::Dormant>)>,
    mut started: MessageWriter<ActivityStarted>,
    engine: Option<Res<crate::script::ScriptEngine>>,
    cell: Res<crate::snapshot::SnapshotCell>,
    fields: Option<Res<FieldGrids>>,
    mut trace: Option<ResMut<crate::trace::DecisionTrace>>,
    // Bindings already warned about, by `(PersonId, name)`. A `Local` rather than a resource because
    // it is this system's own bookkeeping, and a `BTreeSet` so its iteration can never matter.
    mut warned_bindings: Local<std::collections::BTreeSet<(u32, String)>>,
) {
    let DecisionQueries {
        people,
        places,
        doing,
    } = world;
    use bevy_emerge_rng::DetRng;

    // (person, scored options, the host signals the options were scored with) for the trace; only
    // filled when a host added the resource.
    type Scored = (String, f32, bool);
    let mut recorded: Vec<(Entity, Vec<Scored>, Vec<f32>)> = Vec::new();

    let now = clock.now_days;
    let tuning = &settings.tuning;
    let tier_cutoff = tuning.tier_cutoff;
    let hour = clock.hour();

    // Occupancy: how many people already target each place.
    let mut used: Vec<(Entity, u32)> = Vec::new();
    for doing in &doing {
        if let DoingTarget::Place(place) = doing.target {
            match used.iter_mut().find(|(p, _)| *p == place) {
                Some((_, n)) => *n += 1,
                None => used.push((place, 1)),
            }
        }
    }
    let mut place_views: Vec<PlaceView> = places
        .iter()
        .map(|(entity, place, tags, transform)| PlaceView {
            entity,
            tags: tags.0.clone(),
            capacity: place.capacity,
            used: used
                .iter()
                .find(|(p, _)| *p == entity)
                .map_or(0, |(_, n)| *n),
            position: settings.plane.project(transform.translation),
        })
        .collect();
    place_views.sort_by_key(|p| p.entity.to_bits());

    // What everyone is doing, for `SocialProof`, as a lookup by `Entity::to_bits` so one person's
    // close ties can be read without scanning the population. `all_doing` used to be cloned whole into
    // every `Decision`; the filtered list is now built from this map at a `close_ties`-sized cost.
    let all_doing: Vec<(Entity, u16)> = index
        .sorted
        .iter()
        .filter_map(|&(_, entity)| doing.get(entity).ok().map(|d| (entity, d.activity.0)))
        .collect();

    // The engine and the tick's snapshot are the same for every consideration in this pass, so they
    // are read once and shared by reference.
    let snapshot = cell.0.clone();
    // Plane positions of everything a destination can name, gathered once: `Commands` cannot read
    // the world while a plan is being written. The places query carries their `Transform`, and a
    // person's position comes from `Perception`, so no separate transform query is needed — which is
    // also what keeps this system inside Bevy's sixteen-parameter limit.
    let mut positions: Vec<(Entity, Vec2)> = place_views
        .iter()
        .map(|view| (view.entity, view.position))
        .chain(
            index
                .sorted
                .iter()
                .enumerate()
                .filter_map(|(slot, (_, entity))| {
                    perception.pos.get(slot).map(|at| (*entity, *at))
                }),
        )
        .collect();
    positions.sort_by_key(|(e, _)| e.to_bits());
    let position_of = |entity: Entity| {
        positions
            .binary_search_by_key(&entity.to_bits(), |(e, _)| e.to_bits())
            .ok()
            .map(|i| positions[i].1)
    };

    let mut plans: Vec<(
        Entity,
        ActivityId,
        DoingTarget,
        bool,
        String,
        Option<Entity>,
    )> = Vec::new();

    for (slot, &(_, person)) in index.sorted.iter().enumerate() {
        // **A dormant person is out of its daily life**: it keeps its ties, memories and knowledge,
        // but the crate stops choosing for it until the marker is removed.
        let (autonomous, dormant) = marks.get(person).unwrap_or((false, false));
        if !autonomous || dormant || doing.get(person).is_ok() {
            continue;
        }
        let Ok(view) = people.get(person) else {
            continue;
        };
        let (ties, memberships, needs, signals, personality, mood, emotions, home, bound) = (
            view.ties,
            view.memberships,
            view.needs,
            view.signals,
            view.personality,
            view.mood,
            view.emotions,
            view.home,
            view.bound,
        );
        let position = perception.pos.get(slot).copied().unwrap_or(Vec2::ZERO);
        // `near` and `near_ids` are built from one pass over `perception.near`, so index `k` of
        // each describes the same neighbour. The id comes from `PersonIndex.sorted`, whose order is
        // the same index order `perception.near` uses.
        let mut near: Vec<(Entity, Vec2)> = Vec::new();
        let mut near_ids: Vec<u32> = Vec::new();
        if let Some(list) = perception.near.get(slot) {
            for (j, _) in list {
                let j = *j as usize;
                let (Some(other), Some(pos)) = (
                    perception.persons.get(j).copied(),
                    perception.pos.get(j).copied(),
                ) else {
                    continue;
                };
                if other == person {
                    continue;
                }
                near.push((other, pos));
                near_ids.push(index.sorted.get(j).map_or(u32::MAX, |(id, _)| id.0));
            }
        }

        let place_positions: Vec<(Entity, Vec2)> = place_views
            .iter()
            .map(|view| (view.entity, view.position))
            .collect();
        let close = ties.close_targets(now as f32, tuning, settings.close_ties);
        let own_factions = memberships.0.clone();
        let decision = Decision {
            own_factions: own_factions.clone(),
            place_positions: place_positions.clone(),
            position,
            needs: needs.0.clone(),
            signals: signals.0.clone(),
            personality: *personality,
            mood: mood.as_array(),
            emotions: emotions.clone(),
            ties: ties.0.clone(),
            near: near.clone(),
            close_ties: close.clone(),
            close_doing: all_doing
                .iter()
                .filter(|(entity, _)| close.contains(entity))
                .copied()
                .collect(),
            hour,
            perception_radius: settings.perception_radius,
        };

        // The actor is fixed for this person; the target changes with every option, so it is bound
        // per candidate in `score_option` rather than here.
        let actor_id = index
            .sorted
            .iter()
            .find(|(_, e)| *e == person)
            // `-1`, not `0`, when the person is missing from the index: `0` is a real person id and a
            // script would read that person's state instead of finding nobody.
            .map_or(-1, |(id, _)| i64::from(id.0));
        // The scripts read the tick-start snapshot and the compiled definitions; both are the same
        // for every option, so they are borrowed once and only the target is rebound per candidate.
        let script_ready = match (&engine, &snapshot) {
            (Some(engine), Some(snapshot)) => Some((snapshot, engine)),
            _ => None,
        };
        // **A binding to something that is not a place is a wiring mistake, and it is warned about
        // once per person and name.** An unbound person is not warned about at all: a host that binds
        // some people and not others (a night watch has no work place) would otherwise be told once per
        // person for nothing.
        if let Some(bindings) = bound {
            let actor_id = index
                .sorted
                .iter()
                .find(|(_, e)| *e == person)
                .map(|(id, _)| id.0)
                .unwrap_or(u32::MAX);
            for (name, entity) in &bindings.0 {
                if place_views.iter().any(|place| place.entity == *entity) {
                    continue;
                }
                if !warned_bindings.insert((actor_id, name.clone())) {
                    continue;
                }
                warn!(
                    "bevy_society: person {actor_id} has \"{name}\" bound to {entity:?}, which is not a \
                     place; the activity has no target"
                );
            }
        }

        let mut best: Option<(u8, Vec<Candidate>)> = None;
        // Every option that scored, for the trace only. `best` keeps the winning rank tier, so
        // without this a host would never see the lower-rank activities a choice ruled out.
        let mut all_scored: Vec<(String, f32)> = Vec::new();
        for (i, spec) in defs.activities.iter().enumerate() {
            // Index 0 is the reserved idle activity: it is the fallback, never a candidate.
            if i == 0 {
                continue;
            }
            let targets = targets_for(
                &spec.target,
                position,
                &near,
                &place_views,
                home.map(|h| h.0),
                bound,
                &own_factions,
                fields.as_deref(),
                &mut rng.0,
            );
            // **One candidate per activity, not per target.** Dill's weight-based draw is over
            // *options*, and an option is an action; an action with eight eligible targets is not
            // eight times as desirable as one with a single target. Scoring every target and
            // keeping the best (with its ties) gives the activity one score, and the target is
            // picked from the ties afterwards. Before this, `gossip` with eight neighbours held
            // 8/9 of the selection mass and `relax` starved: measured, a Person activity went from
            // 0.00 of all starts at one neighbour to 0.97 at twelve.
            let mut best_for_activity: Option<(f32, Vec<DoingTarget>)> = None;
            for target in targets {
                // `near` is built from `perception.near`, whose indices are positions in
                // `PersonIndex.sorted`, so the id is a lookup rather than a scan. The scan this
                // replaces was a measurable part of the decision cost at a thousand people.
                let target_id = match target {
                    DoingTarget::Person(other) => near_ids
                        .get(
                            near.iter()
                                .position(|(e, _)| *e == other)
                                .unwrap_or(usize::MAX),
                        )
                        .map_or(-1, |id| i64::from(*id)),
                    _ => -1,
                };
                let script_ref = script_ready.map(|(snapshot, engine)| ScriptContext {
                    defs: &defs,
                    snapshot,
                    engine,
                    actor: actor_id,
                    target: target_id,
                });
                let score = score_option(
                    &spec.considerations,
                    &decision,
                    target,
                    i as u16,
                    script_ref.as_ref(),
                    fields.as_deref(),
                );
                if score <= 0.0 {
                    continue;
                }
                match &mut best_for_activity {
                    Some((best, ties)) if score > *best + f32::EPSILON => {
                        *best = score;
                        *ties = vec![target];
                    }
                    Some((best, ties)) if (score - *best).abs() <= f32::EPSILON => {
                        ties.push(target);
                    }
                    Some(_) => {}
                    None => best_for_activity = Some((score, vec![target])),
                }
            }
            let Some((score, targets)) = best_for_activity else {
                continue;
            };
            let candidate = Candidate {
                activity: i as u16,
                targets,
                weight: spec.weight,
                score,
            };
            if trace.is_some() {
                all_scored.push((
                    defs.activity_name(ActivityId(candidate.activity))
                        .to_owned(),
                    candidate.score,
                ));
            }
            match &mut best {
                Some((rank, list)) if *rank == spec.rank => list.push(candidate),
                Some((rank, list)) if spec.rank > *rank => {
                    *rank = spec.rank;
                    *list = vec![candidate];
                }
                Some(_) => {}
                None => best = Some((spec.rank, vec![candidate])),
            }
        }

        if trace.is_some() {
            let mut scored: Vec<(String, f32, bool)> = all_scored
                .into_iter()
                .map(|(name, score)| (name, score, false))
                .collect();
            scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
            recorded.push((person, scored, decision.signals.clone()));
        }

        let picked = best.and_then(|(_, list)| {
            let top = list.iter().map(|c| c.score).fold(0.0_f32, f32::max);
            let floor = top * tier_cutoff;
            let viable: Vec<&Candidate> = list.iter().filter(|c| c.score >= floor).collect();
            let total: f32 = viable.iter().map(|c| c.weight * c.score).sum();
            if total <= 0.0 {
                return None;
            }
            let mut roll = rng.0.unit() as f32 * total;
            let mut chosen = (*viable.last()?).clone();
            for candidate in &viable {
                roll -= candidate.weight * candidate.score;
                if roll <= 0.0 {
                    chosen = (*candidate).clone();
                    break;
                }
            }
            // The activity is chosen; now which of its tied targets.
            //
            // **A `Place` is a fixed destination, so the nearest one wins.** Every place carrying the tag
            // scores the same — a `Place` target has no `Distance` term — so they tie, and the tie used to
            // be a uniform pull: measured, a person with a tavern 1 m away and another 9 m away chose the
            // far one **2 979 times in 6 000 decisions**. That coin flip is most of why `village-evening`
            // spends **62.5% of all person-ticks travelling**.
            //
            // A `Person` keeps the uniform pull: chatting with whichever neighbour comes up is variety,
            // not a defect. Ties on distance fall back to the seeded draw, so the result stays
            // deterministic.
            let target = match chosen.targets.first() {
                Some(DoingTarget::Place(_) | DoingTarget::Point(_)) => chosen
                    .targets
                    .iter()
                    .min_by(|a, b| {
                        let da = place_distance(**a, position, &place_positions);
                        let db = place_distance(**b, position, &place_positions);
                        da.partial_cmp(&db).unwrap_or(std::cmp::Ordering::Equal)
                    })
                    .copied()
                    .unwrap_or(chosen.targets[0]),
                _ => {
                    let index = rng.0.below(chosen.targets.len().max(1));
                    *chosen.targets.get(index)?
                }
            };
            Some((chosen.activity, target))
        });

        if trace.is_some()
            && let Some((_, scored, _)) = recorded.iter_mut().find(|(p, _, _)| *p == person)
        {
            let chosen = picked.map(|(activity, _)| activity);
            if scored.is_empty() {
                scored.push((
                    defs.activity_name(crate::defs::IDLE_ACTIVITY).to_owned(),
                    1.0,
                    true,
                ));
            } else {
                for (name, _, is_chosen) in scored.iter_mut() {
                    *is_chosen = defs.activity_id(name).map(|id| id.0) == chosen;
                }
            }
        }

        match picked {
            Some((activity, target)) => {
                let activity = ActivityId(activity);
                let name = defs.activity_name(activity).to_owned();
                plans.push((
                    person,
                    activity,
                    target,
                    false,
                    name,
                    match target {
                        DoingTarget::Person(other) => Some(other),
                        _ => None,
                    },
                ));
            }
            None => plans.push((
                person,
                crate::defs::IDLE_ACTIVITY,
                DoingTarget::Itself,
                true,
                defs.activity_name(crate::defs::IDLE_ACTIVITY).to_owned(),
                None,
            )),
        }
    }

    if let Some(trace) = trace.as_mut() {
        for (person, scored, signals) in recorded {
            trace.push(crate::trace::Decision {
                tick: clock.tick,
                person,
                // Resolved by `Decision::describe` when read: `decide_activities` is already at
                // Bevy's 16-parameter limit, and a `Name` query would be the seventeenth.
                name: None,
                signals,
                options: scored
                    .into_iter()
                    .map(|(activity, score, chosen)| crate::trace::ScoredOption {
                        activity,
                        score,
                        chosen,
                    })
                    .collect(),
            });
        }
    }

    for (person, activity, target, in_place, name, person_target) in plans {
        let mut entity = commands.entity(person);
        entity.insert(Doing {
            activity,
            target,
            started_day: now,
            arrived_day: in_place.then_some(now),
        });
        // Every target that is somewhere other than where the person stands needs a destination:
        // a `Point` is that point, a `Place` is its transform, and a `Person` is that person's
        // position offset toward the actor. Setting one only for `Point` meant a village whose
        // activities all target places never walked anywhere.
        match target {
            DoingTarget::Point(point) => {
                entity.insert(Destination(point));
            }
            DoingTarget::Place(place) => {
                if let Some(at) = position_of(place) {
                    entity.insert(Destination(at));
                }
            }
            DoingTarget::Person(other) => {
                if let (Some(theirs), Some(mine)) = (position_of(other), position_of(person)) {
                    let toward = (mine - theirs).normalize_or_zero();
                    entity.insert(Destination(
                        theirs + toward * 0.75 * settings.interaction_radius,
                    ));
                }
            }
            DoingTarget::Itself => {}
        }
        // Written at decision, not on arrival: `Doing::started_day` is the day the activity
        // started, and it is stamped just above. Emitting only on arrival meant a person who
        // never arrived — its target out of reach, `travel_timeout_hours` elapsed — produced an
        // `ActivityEnded` for a start no host was ever told about. Measured: 0 starts against
        // 149 ends for one person whose place was 50 m away with no steering plugin.
        started.write(ActivityStarted {
            person,
            activity: name,
            target: person_target,
        });
    }
}

impl ScriptContext<'_> {
    /// Evaluates one `Signal::Script` consideration through the sandboxed engine.
    ///
    /// The result is clamped to `[0, 1]` like every other consideration input. A failure is an
    /// `error!` naming the script, and scores 0 — the rule the rest of the crate follows: a failed
    /// consideration scores 0 rather than aborting the decision.
    fn eval(
        &self,
        id: crate::script::ScriptId,
        decision: &Decision,
        target: DoingTarget,
    ) -> Option<f32> {
        let _ = (decision, target);
        let bindings = crate::script::Bindings {
            actor: self.actor,
            target: self.target,
            ..crate::script::Bindings::default()
        };
        match self
            .engine
            .eval_f32(&self.defs.scripts, id, self.snapshot, bindings)
        {
            Ok(value) => Some(value.clamp(0.0, 1.0)),
            Err(err) => {
                error!(
                    "bevy_society: script {} failed: {err}",
                    crate::script::ScriptEngine::owner(&self.defs.scripts, id)
                );
                None
            }
        }
    }
}

/// The targets an activity offers this decision (plan D7), with full places excluded.
///
/// `Wander` draws its point here, from the two `unit()` draws the SPEC names: radius · √u₁ at angle
/// 2πu₂, which is uniform over the disc rather than bunched at its centre.
#[allow(clippy::too_many_arguments)]
fn targets_for(
    target: &ActivityTarget,
    position: Vec2,
    near: &[(Entity, Vec2)],
    places: &[PlaceView],
    home: Option<Entity>,
    bound: Option<&crate::BoundPlaces>,
    own_factions: &[crate::FactionId],
    fields: Option<&FieldGrids>,
    rng: &mut rand_chacha::ChaCha8Rng,
) -> Vec<DoingTarget> {
    use bevy_emerge_rng::DetRng;
    match target {
        ActivityTarget::Itself => vec![DoingTarget::Itself],
        ActivityTarget::Home => home.map_or_else(Vec::new, |h| vec![DoingTarget::Place(h)]),
        ActivityTarget::Wander(radius) => {
            let r = radius * (rng.unit() as f32).sqrt();
            let angle = std::f32::consts::TAU * rng.unit() as f32;
            vec![DoingTarget::Point(
                position + Vec2::new(r * angle.cos(), r * angle.sin()),
            )]
        }
        ActivityTarget::Place(tag) => places
            .iter()
            .filter(|place| place.used < place.capacity && place.tags.iter().any(|t| t == tag))
            .map(|place| DoingTarget::Place(place.entity))
            .collect(),
        // The place this person bound under the name, when they have one and it is a place with
        // room. No binding means no candidate at all: the activity simply does not apply to them.
        ActivityTarget::Bound(name) => bound
            .and_then(|bindings| bindings.get(name))
            .filter(|entity| {
                places
                    .iter()
                    .any(|place| place.entity == *entity && place.used < place.capacity)
            })
            .map_or_else(Vec::new, |entity| vec![DoingTarget::Place(entity)]),
        ActivityTarget::Person => near.iter().map(|(e, _)| DoingTarget::Person(*e)).collect(),
        // A gradient target walks toward the slope of the named field. Without the `fields` feature, or
        // before the grid exists on the first tick, there is no slope and the option has no target —
        // which is the honest answer, and what the startup warning is about when the feature is off.
        ActivityTarget::Gradient(input, toward) => fields
            .and_then(|grids| grids.gradient_target(*input, own_factions, position, *toward))
            .map_or_else(Vec::new, |point| vec![DoingTarget::Point(point)]),
    }
}

/// The social-field grids, when the `fields` feature is on.
///
/// With the feature off this is an empty resource, so the decision keeps one shape and `Signal::Field`
/// still has an arm to read — a zero, because no grid exists. That is what the startup warning tells
/// the host about, rather than a compile error for a definition file the crate already accepts.
#[cfg(feature = "fields")]
type FieldGrids = crate::SocialFields;
/// An empty stand-in for [`crate::SocialFields`] when the `fields` feature is off.
///
/// It answers the two calls the decision makes, with the only honest values: no cell, no slope. A
/// definition that reads a field therefore scores `0` and is never chosen, which is exactly what the
/// startup warning says will happen — rather than a compile error for a definition file the crate
/// accepts in either configuration.
#[cfg(not(feature = "fields"))]
#[derive(bevy::prelude::Resource, Default)]
pub struct NoFields;

#[cfg(not(feature = "fields"))]
impl NoFields {
    fn world_to_cell(&self, _at: Vec2) -> bevy::math::IVec2 {
        bevy::math::IVec2::ZERO
    }

    fn field_value(
        &self,
        _input: crate::FieldInput,
        _own: &[crate::FactionId],
        _cell: bevy::math::IVec2,
    ) -> f32 {
        0.0
    }

    fn gradient_target(
        &self,
        _input: crate::FieldInput,
        _own: &[crate::FactionId],
        _pos: Vec2,
        _toward: bool,
    ) -> Option<Vec2> {
        None
    }
}
#[cfg(not(feature = "fields"))]
type FieldGrids = NoFields;

/// How far a target is from `from`, on the society plane. `Person` and `Itself` report zero, because a
/// person target's position is not what a `Place` tie is choosing between.
fn place_distance(target: DoingTarget, from: Vec2, places: &[(Entity, Vec2)]) -> f32 {
    let of = |entity: Entity| {
        places
            .iter()
            .find(|(place, _)| *place == entity)
            .map(|(_, at)| *at)
    };
    match target {
        DoingTarget::Place(place) => of(place).map_or(f32::MAX, |at| from.distance(at)),
        DoingTarget::Point(point) => from.distance(point),
        DoingTarget::Person(_) | DoingTarget::Itself => 0.0,
    }
}

/// Scores one option: `weight · Π s′` over its considerations (plan D7).
///
/// The compensation `s′ = s + (1 − s)·(1 − 1/n)·s` is a design choice. Game AI Pro 3 ch. 13
/// multiplies raw scores; compensation keeps an activity described with five considerations
/// comparable to one described with two, instead of losing purely for being described more fully.
fn score_option(
    considerations: &[Term],
    decision: &Decision,
    target: DoingTarget,
    activity: u16,
    scripts: Option<&ScriptContext<'_>>,
    fields: Option<&FieldGrids>,
) -> f32 {
    let n = considerations.len();
    if n == 0 {
        return 1.0;
    }
    let mut product = 1.0_f32;
    for term in considerations {
        let s = term.curve.eval(signal_value(
            term.signal,
            decision,
            target,
            activity,
            scripts,
            fields,
        ));
        let compensated = s + (1.0 - s) * (1.0 - 1.0 / n as f32) * s;
        product *= compensated;
    }
    product
}

/// One consideration's normalised `[0, 1]` input (plan D7).
///
/// **A zero is final.** Scoring is multiplicative, so one consideration at 0 zeroes the whole option
/// and the compensation term cannot rescue it — `s + (1-s)(1-1/n)s` is still 0 at `s = 0`. A
/// definition that wants "almost never" must say `0.01`, not `0`; a definition that wants "never"
/// says `0`. This is deliberate: a need that is entirely unmet should make its activity impossible
/// now, not unlikely.
///
/// Every arm returns a real value computed from the decision's copied state. `Field` and `Script`
/// read the social-field grids and the Rhai snapshot, which a decision does not hold; they score 0
/// and a host that wants them should say so through a definition the decision can score instead.
fn signal_value(
    signal: Signal,
    decision: &Decision,
    target: DoingTarget,
    activity: u16,
    scripts: Option<&ScriptContext<'_>>,
    fields: Option<&FieldGrids>,
) -> f32 {
    let tie = match target {
        DoingTarget::Person(other) => decision.ties.iter().find(|t| t.target == other).copied(),
        _ => None,
    };
    let value = match signal {
        Signal::Need(id) => decision
            .needs
            .get(usize::from(id.0))
            .copied()
            .unwrap_or(0.0),
        Signal::Host(id) => decision
            .signals
            .get(usize::from(id.0))
            .copied()
            .unwrap_or(0.0),
        Signal::Hour => decision.hour / 24.0,
        Signal::Pleasure => (decision.mood[0] + 1.0) / 2.0,
        Signal::Arousal => (decision.mood[1] + 1.0) / 2.0,
        Signal::Dominance => (decision.mood[2] + 1.0) / 2.0,
        Signal::Trait(which) => decision.personality.value(which),
        Signal::Emotion(kind) => decision.emotions.intensity_of(kind),
        Signal::Affinity => (tie.map_or(0.0, |t| t.affinity) + 1.0) / 2.0,
        Signal::Trust => tie.map_or(0.5, |t| t.trust),
        Signal::Respect => (tie.map_or(0.0, |t| t.respect) + 1.0) / 2.0,
        Signal::Familiarity => match target {
            DoingTarget::Person(other) => decision
                .ties
                .iter()
                .find(|t| t.target == other)
                .map_or(0.0, |t| t.familiarity_peak),
            _ => 0.0,
        },
        Signal::Distance => {
            let there = match target {
                DoingTarget::Person(other) => decision
                    .near
                    .iter()
                    .find(|(e, _)| *e == other)
                    .map(|(_, p)| *p),
                DoingTarget::Place(place) => decision
                    .place_positions
                    .iter()
                    .find(|(e, _)| *e == place)
                    .map(|(_, p)| *p),
                // A `Point` is chosen by `Wander` and has no entry in the place list; `Itself` is the
                // actor's own position, which is not what a distance consideration is asking about.
                DoingTarget::Point(_) | DoingTarget::Itself => None,
            };
            there.map_or(1.0, |p| {
                (decision.position.distance(p) / decision.perception_radius).min(1.0)
            })
        }
        Signal::SocialProof => {
            if decision.close_ties.is_empty() {
                0.0
            } else {
                let following = decision
                    .close_ties
                    .iter()
                    .filter(|t| {
                        decision
                            .close_doing
                            .iter()
                            .any(|(e, a)| e == *t && *a == activity)
                    })
                    .count();
                following as f32 / decision.close_ties.len() as f32
            }
        }
        Signal::Script(id) => match scripts {
            Some(context) => context.eval(id, decision, target).unwrap_or(0.0),
            // A decision made with no scripting context (no plugin, no snapshot yet) cannot score a
            // script consideration. It scores 0 and the activity is skipped for this tick, which is
            // the same answer a failed script gives.
            None => 0.0,
        },
        // A field input reads the stigmergy grid at the actor's own cell. Without the `fields` feature
        // there is no grid and the honest answer is 0 — but with it the value is real, and returning a
        // constant here made every `Field` definition dead while the definitions file accepted it.
        Signal::Field(input) => match fields {
            Some(grids) => {
                let cell = grids.world_to_cell(decision.position);
                grids.field_value(input, &decision.own_factions, cell)
            }
            None => 0.0,
        },
    };
    value.clamp(0.0, 1.0)
}

// -------------------------------------------------------------------------------------------------
// Exchanging
// -------------------------------------------------------------------------------------------------

/// Resolves every due social exchange (plan D7): the autonomous arrivals first, then the game's
/// [`Interact`](crate::interact::Interact) requests in arrival order.
///
/// Exclusive because one exchange can touch the actor, the target and every witness. No single
/// `Query` can borrow that set, and splitting the steps across systems would make the RNG draw order
/// depend on system order rather than on [`PersonIndex::sorted`] order.
fn resolve_interactions(world: &mut World) {
    // One gather scope: every read the exchanges need is copied out before any of them writes.
    let (now, settings, defs, perception, index, due) = {
        let now = world.resource::<SocietyClock>().now_days;
        let settings = world.resource::<SocietySettings>().clone();
        let defs = world.resource::<SocietyDefs>().clone();
        let perception = world.resource::<Perception>().clone();
        let index = world.resource::<PersonIndex>().sorted.clone();

        // Arrivals first, then game requests, both in the order they were queued.
        let mut due: Vec<crate::interact::Interact> = {
            let mut arrivals = world.remove_resource::<ArrivalQueue>().unwrap_or_default();
            std::mem::take(&mut arrivals.0)
        };
        let mut queue = world.remove_resource::<InteractQueue>().unwrap_or_default();
        due.extend(std::mem::take(&mut queue.0));
        world.insert_resource(queue);
        world.insert_resource(ArrivalQueue::default());
        (now, settings, defs, perception, index, due)
    };

    for request in due {
        exchange(world, &request, &defs, &settings, &perception, &index, now);
    }
}

/// One exchange, start to finish (plan D7).
#[allow(clippy::too_many_arguments)]
fn exchange(
    world: &mut World,
    request: &crate::interact::Interact,
    defs: &SocietyDefs,
    settings: &SocietySettings,
    perception: &Perception,
    index: &[(crate::PersonId, Entity)],
    now: f64,
) {
    let (actor, target) = (request.actor, request.target);
    let Some(id) = defs.interaction_id(&request.interaction) else {
        error!(
            "bevy_society: Interact names unknown interaction '{}'; ignored",
            request.interaction
        );
        return;
    };
    let Some(spec) = defs.interactions.get(usize::from(id.0)).cloned() else {
        return;
    };
    if world.get::<Ties>(actor).is_none() || world.get::<Ties>(target).is_none() {
        return;
    }
    // **A dormant person takes part in nothing**, and that includes an exchange the host asked for
    // directly with `Interact`: dormancy is the host saying this person is not living their daily life
    // right now, and honouring it only for crate-chosen exchanges would make the marker a suggestion.
    if world.get::<crate::Dormant>(actor).is_some() || world.get::<crate::Dormant>(target).is_some()
    {
        return;
    }

    // (1) `requires` first: a script that says no ends it with no draw and no effect.
    if let Some(requires) = spec.requires
        && !condition_holds(world, defs, requires, actor, target)
    {
        world.write_message(InteractionResolved {
            actor,
            target,
            interaction: spec.name.clone(),
            accepted: false,
            refused_by_rule: true,
            winner: None,
        });
        return;
    }

    // (2) The acceptance draw.
    let target_ties = world.get::<Ties>(target).cloned().unwrap_or_default();
    let target_personality = world
        .get::<Personality>(target)
        .copied()
        .unwrap_or_default();
    let target_mood = world.get::<Mood>(target).copied().unwrap_or_default();
    let toward_actor = target_ties
        .get(actor)
        .copied()
        .unwrap_or_else(|| crate::relationships::Tie::new(actor));
    let shared_faction = {
        let mine = world.get::<Memberships>(actor).cloned().unwrap_or_default();
        let theirs = world
            .get::<Memberships>(target)
            .cloned()
            .unwrap_or_default();
        mine.shares_with(&theirs)
    };
    let accept = &spec.accept;
    let probability = (accept.base
        + accept.affinity * toward_actor.affinity
        + accept.trust * (toward_actor.trust - 0.5)
        + accept.agreeableness * (target_personality.agreeableness - 0.5)
        + accept.pleasure * target_mood.pleasure
        + if shared_faction { accept.ingroup } else { 0.0 })
    .clamp(0.02, 0.98);
    let roll = world.resource_mut::<crate::SocietyRng>().0.unit() as f32;
    let accepted = roll < probability;

    // (3) The effects for the outcome.
    let effects = if accepted {
        &spec.on_accept
    } else {
        &spec.on_reject
    };
    apply_effects(world, effects, actor, target, &spec.name, now);

    let mut winner = None;
    if accepted {
        // (4) Exchange proper: influence, gossip, contest, deed, closure, contact.
        influence(world, &spec, actor, target, &settings.tuning);
        if spec.gossip {
            tell(world, actor, target, settings, now);
        }
        if spec.contest {
            winner = contest(world, actor, target, &settings.tuning, perception, index);
        }
        if let Some(deed) = spec.deed {
            // The witnesses of an observed exchange are whoever was within perception of the actor,
            // excluding the two parties. That is `Perception`'s job; a host-reported deed brings its own
            // list, which is why `witness_deed` takes one rather than computing it.
            let seen_by: Vec<Entity> = index
                .iter()
                .position(|(_, e)| *e == actor)
                .and_then(|slot| perception.near.get(slot))
                .map(|list| {
                    list.iter()
                        .filter_map(|(j, _)| perception.persons.get(*j as usize).copied())
                        .filter(|w| *w != actor && *w != target)
                        .collect()
                })
                .unwrap_or_default();
            witness_deed(world, deed, actor, target, defs, settings, now, &seen_by);
        }
        close_the_triad(world, actor, target, perception, index, settings, now);
        if !shared_faction
            && spec.deed.is_none_or(|d| {
                defs.deeds
                    .get(usize::from(d.0))
                    .is_none_or(|s| s.harm >= 0.0)
            })
        {
            intergroup(world, actor, target, &settings.tuning);
        }
    } else {
        // (5) Rejection penalty.
        if let Some(mut ties) = world.get_mut::<Ties>(actor) {
            let tie = ties.entry(target);
            tie.add_affinity(
                -settings.tuning.rejection_affinity_penalty,
                &settings.tuning,
            );
        }
        // **The definition's own `on_reject` emotion wins.** A pack that says what a refusal feels
        // like is not second-guessed; the crate's number is the fallback for a pack that says nothing,
        // which the starter pack did. Emitting both stacked `0.3` on top of the definition's `0.1` and
        // measured *worse* mood than either alone — two paths for one event.
        if effects.actor_emotion.is_none() && settings.tuning.rejection_distress > 0.0 {
            world.write_message(crate::mind::Appraise {
                person: actor,
                kind: crate::mind::EmotionKind::Distress,
                intensity: settings.tuning.rejection_distress,
                toward: Some(target),
            });
        }
    }

    // (6) Memories, both sides.
    //
    // The magnitude of a **deed's** memory is `0.3 + its harm`, but the deed fires only on accept — see
    // step (4). Sizing every outcome by `harm` therefore records an injury that never happened: a
    // *refused* insult wrote `-0.7` into the snubber, about the person who refused.
    //
    // That closed a loop. `snub`'s only consideration is `ctx.grudge(actor, target)`, so a refused snub
    // deepened the very grudge that motivated the next one, and because `snub` satisfies no need nothing
    // pushed back. Measured on `rumor-crosses-a-cluster` (12 people, seed 11): `snub` was **170 of 338**
    // activity starts, ten of them in a row, and the narrative read `snub, compliment, snub, compliment`
    // through the evening. A single refusal scored the next snub at 0.7 through `Linear(1.0, 0.0)`; two
    // clamped the grudge to 1.0.
    //
    // A refusal is not an injury. It costs the interaction's own weight — the `0.3` every resolved
    // exchange carries — and the deed's `harm` sizes a memory only when the deed actually fired.
    let harm = if accepted {
        spec.deed
            .and_then(|d| defs.deeds.get(usize::from(d.0)))
            .map_or(0.0, |spec| spec.harm)
    } else {
        0.0
    };
    let magnitude = 0.3 + harm.abs();
    let actor_valence = if accepted { magnitude } else { -magnitude };
    let target_valence = if harm < 0.0 {
        -magnitude
    } else if accepted {
        magnitude
    } else {
        -magnitude
    };
    record(
        world,
        actor,
        target,
        id,
        accepted,
        true,
        actor_valence,
        effects.importance,
        now,
    );
    record(
        world,
        target,
        actor,
        id,
        accepted,
        false,
        target_valence,
        effects.importance,
        now,
    );

    // (7) Exposure for the pair, both ways.
    expose(world, actor, target, settings, now);
    expose(world, target, actor, settings, now);

    // (8) The report.
    world.write_message(InteractionResolved {
        actor,
        target,
        interaction: spec.name.clone(),
        accepted,
        refused_by_rule: false,
        winner,
    });

    // (9) Barks.
    bark(world, &spec, actor, target, accepted, settings);
}

/// (3) Applies one effect set: tie deltas both ways, emotions, needs, and the optional script.
fn apply_effects(
    world: &mut World,
    effects: &crate::defs::EffectsSpec,
    actor: Entity,
    target: Entity,
    _interaction: &str,
    _now: f64,
) {
    // Read once: the affinity clamp is `Tuning::affinity_ceiling`, so every writer needs it.
    let tuning = world
        .get_resource::<SocietySettings>()
        .map_or_else(Default::default, |settings| settings.tuning.clone());
    // Tie deltas, clamped exactly as `fx.adjust` clamps them.
    for (owner, other, delta) in [
        (actor, target, effects.actor_to_target),
        (target, actor, effects.target_to_actor),
    ] {
        let Some(mut ties) = world.get_mut::<Ties>(owner) else {
            continue;
        };
        let tie = ties.entry(other);
        tie.add_affinity(delta.affinity, &tuning);
        tie.trust = (tie.trust + delta.trust).clamp(0.0, 1.0);
        tie.respect = (tie.respect + delta.respect).clamp(-1.0, 1.0);
        if delta.familiarity != 0.0 {
            tie.familiarity_peak = (tie.familiarity_peak + delta.familiarity).clamp(0.0, 1.0);
        }
    }
    for (person, emotion) in [
        (actor, effects.actor_emotion),
        (target, effects.target_emotion),
    ] {
        if let Some((kind, intensity)) = emotion {
            world.write_message(crate::mind::Appraise {
                person,
                kind,
                intensity,
                toward: Some(if person == actor { target } else { actor }),
            });
        }
    }
    for (person, deltas) in [
        (actor, &effects.actor_needs),
        (target, &effects.target_needs),
    ] {
        let Some(mut needs) = world.get_mut::<Needs>(person) else {
            continue;
        };
        for &(need, delta) in deltas {
            if let Some(level) = needs.0.get_mut(usize::from(need.0)) {
                *level = (*level + delta).clamp(0.0, 1.0);
            }
        }
    }
    // A failed script applies none of its actions, so it runs as one unit through the normal
    // application point rather than writing the world here.
    if let Some(script) = effects.script {
        run_effect(world, script, actor, target);
    }
}

/// (4a) Opinion exchange over the interaction's topics, both directions (plan D5).
fn influence(
    world: &mut World,
    spec: &crate::defs::InteractionSpec,
    actor: Entity,
    target: Entity,
    tuning: &Tuning,
) {
    if spec.influence.is_empty() {
        return;
    }
    let Some(a_opinions) = world.get::<crate::Opinions>(actor).cloned() else {
        return;
    };
    let Some(b_opinions) = world.get::<crate::Opinions>(target).cloned() else {
        return;
    };
    let a_openness = world.get::<Personality>(actor).map_or(0.5, |p| p.openness);
    let b_openness = world.get::<Personality>(target).map_or(0.5, |p| p.openness);
    // Both sides' pre-step values, so neither direction sees the other's write.
    let a_values = crate::beliefs::snapshot_values(&a_opinions, &spec.influence);
    let b_values = crate::beliefs::snapshot_values(&b_opinions, &spec.influence);
    // Each side's opinions and ties are cloned out and written back: `exchange_opinions` needs both
    // at once, which two `get_mut` calls on one world cannot provide.
    for (holder, other, openness, other_values) in [
        (actor, target, a_openness, b_values),
        (target, actor, b_openness, a_values),
    ] {
        let (Some(mut opinions), Some(mut ties)) = (
            world.get::<crate::Opinions>(holder).cloned(),
            world.get::<Ties>(holder).cloned(),
        ) else {
            continue;
        };
        crate::beliefs::exchange_opinions(
            &mut opinions,
            &mut ties,
            openness,
            other,
            &other_values,
            tuning,
        );
        if let Some(mut slot) = world.get_mut::<crate::Opinions>(holder) {
            *slot = opinions;
        }
        if let Some(mut slot) = world.get_mut::<Ties>(holder) {
            *slot = ties;
        }
    }
}

/// (4b) One telling (plan D5): the teller picks its heaviest rumor and the listener learns it, or the
/// teller stifles because the listener already knew.
fn tell(world: &mut World, teller: Entity, listener: Entity, settings: &SocietySettings, now: f64) {
    let Some(teller_knowledge) = world.get::<crate::Knowledge>(teller).cloned() else {
        return;
    };
    let trust_in_teller = world
        .get::<Ties>(listener)
        .and_then(|ties| ties.get(teller).map(|tie| tie.trust))
        .unwrap_or(0.5);
    let close = world
        .get::<Ties>(listener)
        .map(|ties| ties.close_targets(now as f32, &settings.tuning, settings.close_ties))
        .unwrap_or_default();

    // Clone the listener's state out, run the telling, write it back: `gossip` needs four of the
    // listener's components at once, which four `get_mut` calls on one world cannot provide.
    let Some(mut listener_knowledge) = world.get::<crate::Knowledge>(listener).cloned() else {
        return;
    };
    let Some(mut listener_ties) = world.get::<Ties>(listener).cloned() else {
        return;
    };
    let mut listener_emotions = world.get::<Emotions>(listener).cloned().unwrap_or_default();
    let mut listener_memories = world
        .get::<crate::mind::Memories>(listener)
        .cloned()
        .unwrap_or_default();

    let mut book = world
        .remove_resource::<crate::RumorBook>()
        .unwrap_or_default();
    let outcome = crate::rumor::gossip(
        &mut book,
        &teller_knowledge,
        &mut listener_knowledge,
        &mut listener_ties,
        &mut listener_emotions,
        &mut listener_memories,
        listener,
        trust_in_teller,
        &close,
        settings,
        now as f32,
    );

    if let Some(mut slot) = world.get_mut::<crate::Knowledge>(listener) {
        *slot = listener_knowledge;
    }
    if let Some(mut slot) = world.get_mut::<Ties>(listener) {
        *slot = listener_ties;
    }
    if let Some(mut slot) = world.get_mut::<Emotions>(listener) {
        *slot = listener_emotions;
    }
    if let Some(mut slot) = world.get_mut::<crate::mind::Memories>(listener) {
        *slot = listener_memories;
    }

    // The DK rule: a telling to someone who already knows stifles the teller.
    if let crate::rumor::Gossip::Stifled(id) = outcome
        && let Some(mut knowledge) = world.get_mut::<crate::Knowledge>(teller)
        && let Some(known) = knowledge.get_mut(id)
    {
        known.spreader = false;
    }
    if let crate::rumor::Gossip::Learned { rumor, .. } = outcome {
        world.write_message(crate::rumor::RumorLearned {
            person: listener,
            rumor,
            // **This is the gossip path, so the teller is known.** What a listener makes of a rumor
            // depends on who told them, and a rumor learned by seeing the deed is not someone's word.
            from: Some(teller),
        });
    }
    world.insert_resource(book);
}

/// (4c) A contest between the two (plan D6), with every witness of the actor raising its respect for
/// the winner.
fn contest(
    world: &mut World,
    actor: Entity,
    target: Entity,
    tuning: &Tuning,
    perception: &Perception,
    index: &[(crate::PersonId, Entity)],
) -> Option<Entity> {
    let dominance = |world: &World, e: Entity| world.get::<Mood>(e).map_or(0.0, |m| m.dominance);
    let standing =
        |world: &World, e: Entity| world.get::<crate::Status>(e).map_or(0.0, |s| s.standing);
    let a_dom = dominance(world, actor);
    let a_stand = standing(world, actor);
    let t_dom = dominance(world, target);
    let t_stand = standing(world, target);

    // Both contestants' state comes out of the world, because `resolve_contest` needs both at once
    // and two `get_mut` calls on one world cannot coexist.
    let mut a_ties = world.get::<Ties>(actor).cloned()?;
    let mut t_ties = world.get::<Ties>(target).cloned()?;
    let mut a_emotions = world.get::<Emotions>(actor).cloned()?;
    let mut t_emotions = world.get::<Emotions>(target).cloned()?;

    let resolved = crate::groups::resolve_contest(
        &mut world.resource_mut::<crate::SocietyRng>().0,
        tuning.contest_steepness,
        crate::groups::Contestant {
            entity: actor,
            dominance: a_dom,
            standing: a_stand,
            ties: &mut a_ties,
            emotions: &mut a_emotions,
        },
        crate::groups::Contestant {
            entity: target,
            dominance: t_dom,
            standing: t_stand,
            ties: &mut t_ties,
            emotions: &mut t_emotions,
        },
    );

    if let Some(mut ties) = world.get_mut::<Ties>(actor) {
        *ties = a_ties;
    }
    if let Some(mut ties) = world.get_mut::<Ties>(target) {
        *ties = t_ties;
    }
    if let Some(mut emotions) = world.get_mut::<Emotions>(actor) {
        *emotions = a_emotions;
    }
    if let Some(mut emotions) = world.get_mut::<Emotions>(target) {
        *emotions = t_emotions;
    }

    // Every neighbour that can see the actor raises its respect for the winner.
    let actor_slot = index.iter().position(|(_, e)| *e == actor)?;
    let witnesses: Vec<Entity> = perception
        .near
        .get(actor_slot)
        .map(|list| {
            list.iter()
                .filter_map(|(j, _)| perception.persons.get(*j as usize).copied())
                .filter(|w| *w != actor && *w != target)
                .collect()
        })
        .unwrap_or_default();
    for watcher in witnesses {
        if let Some(mut ties) = world.get_mut::<Ties>(watcher) {
            crate::groups::witness_contest(&mut ties, resolved);
        }
    }
    Some(resolved)
}

/// (4d) One deed and its witnesses (plan D5): each witness judges it, a harsh judgement is a
/// `NormViolated`, and a heavy enough deed becomes a rumor every witness already knows.
#[allow(clippy::too_many_arguments)]
fn witness_deed(
    world: &mut World,
    deed: crate::defs::DeedId,
    actor: Entity,
    patient: Entity,
    defs: &SocietyDefs,
    settings: &SocietySettings,
    now: f64,
    witnesses: &[Entity],
) {
    let Some(deed_spec) = defs.deeds.get(usize::from(deed.0)).cloned() else {
        return;
    };

    // **A deed deposits its harm into the commotion field, at the actor's cell.** The channel is what
    // `FieldInput::Commotion` reads and what a `Gradient(Commotion, true)` activity climbs, and until
    // this line nothing wrote it: the field was always zero, so a `gawk` activity had no direction and
    // every `Commotion` consideration scored 0. `at` is a plane position, which is what the message
    // takes, so a host writing one needs no grid geometry and neither does this.
    #[cfg(feature = "fields")]
    if deed_spec.harm != 0.0
        && let Some(transform) = world.get::<GlobalTransform>(actor)
    {
        world.write_message(crate::fields::Commotion {
            at: settings.plane.project(transform.translation()),
            amount: deed_spec.harm.abs(),
        });
    }

    let mut approvals: Vec<(Entity, f32)> = Vec::new();
    // Everyone who saw it, for `Tuning::witness_dilution`; the crowd is the same for every witness.
    let watcher_count = witnesses.len();
    for &watcher in witnesses {
        let opinions = world
            .get::<crate::Opinions>(watcher)
            .cloned()
            .unwrap_or_default();
        let personality = world
            .get::<Personality>(watcher)
            .copied()
            .unwrap_or_default();
        let Some(mut ties) = world.get::<Ties>(watcher).cloned() else {
            continue;
        };
        let mut emotions = world.get::<Emotions>(watcher).cloned().unwrap_or_default();
        let mut memories = world
            .get::<crate::mind::Memories>(watcher)
            .cloned()
            .unwrap_or_default();
        let trust_in_patient = ties.get(patient).map(|tie| tie.trust);
        let reaction = crate::reputation::witness_reaction(
            &mut ties,
            &mut emotions,
            &mut memories,
            &opinions,
            &personality,
            deed,
            deed_spec.topic,
            deed_spec.harm,
            actor,
            Some(patient),
            trust_in_patient,
            settings,
            watcher_count,
            now,
        );
        // Write every component back, unconditionally: a lost `Knowledge` restore is silent amnesia.
        if let Some(mut slot) = world.get_mut::<Ties>(watcher) {
            *slot = ties;
        }
        if let Some(mut slot) = world.get_mut::<Emotions>(watcher) {
            *slot = emotions;
        }
        if let Some(mut slot) = world.get_mut::<crate::mind::Memories>(watcher) {
            *slot = memories;
        }
        if reaction.violated {
            world.write_message(crate::reputation::NormViolated {
                witness: watcher,
                actor,
                deed: deed_spec.name.clone(),
            });
        }
        approvals.push((watcher, reaction.approval));
    }

    // The patient and the actor react to the deed itself.
    let gain = {
        let conscientiousness = world
            .get::<Personality>(patient)
            .map_or(0.5, |p| p.conscientiousness);
        crate::reputation::trust_gain(&settings.tuning, conscientiousness)
    };
    if let Some(mut ties) = world.get::<Ties>(patient).cloned() {
        let mut emotions = world.get::<Emotions>(patient).cloned().unwrap_or_default();
        crate::reputation::patient_reaction(&mut ties, &mut emotions, deed_spec.harm, gain, actor);
        if let Some(mut slot) = world.get_mut::<Ties>(patient) {
            *slot = ties;
        }
        if let Some(mut slot) = world.get_mut::<Emotions>(patient) {
            *slot = emotions;
        }
    }
    if let Some(opinions) = world.get::<crate::Opinions>(actor).cloned()
        && let Some(mut emotions) = world.get::<Emotions>(actor).cloned()
    {
        crate::reputation::actor_reaction(&mut emotions, &opinions, deed_spec.topic, patient);
        if let Some(mut slot) = world.get_mut::<Emotions>(actor) {
            *slot = emotions;
        }
    }

    // A rumor, when the deed is heavy enough or a witness felt strongly.
    let heavy = deed_spec.harm.abs() >= settings.tuning.rumor_threshold;
    let strong = approvals.iter().any(|(_, a)| a.abs() >= 0.5);
    if (heavy || strong) && !approvals.is_empty() {
        let mut book = world
            .remove_resource::<crate::RumorBook>()
            .unwrap_or_default();
        let mut known: Vec<(Entity, crate::Knowledge)> = Vec::new();
        for (watcher, _) in &approvals {
            if let Some(knowledge) = world.get::<crate::Knowledge>(*watcher) {
                known.push((*watcher, knowledge.clone()));
            }
        }
        let id = crate::reputation::spread_witnessed_rumor(
            &mut book,
            &mut known,
            actor,
            Some(patient),
            deed,
            deed_spec.harm,
            now as f32,
            true,
            world
                .get_resource::<crate::core::SocietySettings>()
                .map_or(64, |s| s.knowledge_capacity),
        );
        for (watcher, knowledge) in known {
            if let Some(mut slot) = world.get_mut::<crate::Knowledge>(watcher) {
                *slot = knowledge;
            }
        }
        if let Some(rumor) = id {
            for (watcher, _) in &approvals {
                world.write_message(crate::rumor::RumorLearned {
                    person: *watcher,
                    rumor,
                    // A witnessed deed: nobody told them, they saw it.
                    from: None,
                });
            }
        }
        world.insert_resource(book);
    }
}

/// Resolves every host-reported deed (issue #8), after the exchanges have run.
///
/// Exclusive because [`witness_deed`] writes to whoever it likes, the same as the exchange path.
///
/// The named witnesses are the point: perception is not consulted at all, so a host can report what its
/// own rules saw. Ordering is fixed for determinism — the witnesses are deduped and ordered by
/// [`PersonId`](crate::PersonId) before the deed is reported, so two runs with the same witness set give
/// the same society however the host's `Vec` happened to be ordered.
fn resolve_reported_deeds(world: &mut World) {
    let (now, defs, reports, index) = {
        let now = world.resource::<SocietyClock>().now_days;
        let defs = world.resource::<SocietyDefs>().clone();
        let index = world.resource::<PersonIndex>().sorted.clone();
        let mut queue = world
            .remove_resource::<DeedReportQueue>()
            .unwrap_or_default();
        let reports = std::mem::take(&mut queue.0);
        world.insert_resource(queue);
        (now, defs, reports, index)
    };
    if reports.is_empty() {
        return;
    }
    let settings = world.resource::<SocietySettings>().clone();

    for report in reports {
        let Some(deed) = defs.deed_id(&report.deed) else {
            warn!(
                "bevy_society: ReportDeed names unknown deed '{}'; ignored",
                report.deed
            );
            continue;
        };
        // **The named witnesses, deduped and in `PersonId` order.** The actor and the patient are never
        // witnesses of their own deed, and a dormant person still counts if the host named them: the
        // host saw something the crate cannot, and `Dormant` is about daily life, not about eyes.
        let mut witnesses: Vec<Entity> = Vec::new();
        let mut not_people = 0usize;
        for &entity in &report.witnesses {
            if entity == report.actor || Some(entity) == report.patient {
                continue;
            }
            if !index.iter().any(|(_, e)| *e == entity) {
                not_people += 1;
                continue;
            }
            if !witnesses.contains(&entity) {
                witnesses.push(entity);
            }
        }
        if not_people > 0 {
            warn!(
                "bevy_society: ReportDeed for '{}' names {not_people} witness(es) that are not persons; \
                 dropped",
                report.deed
            );
        }
        if witnesses.is_empty() {
            // A deed nobody saw is not a deed: no reactions and no rumor.
            continue;
        }
        witnesses.sort_unstable_by_key(|entity| {
            index
                .iter()
                .find(|(_, e)| e == entity)
                .map_or(u32::MAX, |(id, _)| id.0)
        });
        let patient = report.patient.unwrap_or(report.actor);
        witness_deed(
            world,
            deed,
            report.actor,
            patient,
            &defs,
            &settings,
            now,
            &witnesses,
        );
    }
}

/// (4e) Triadic closure (plan D4): every close tie of each party in contact gains familiarity with
/// the other.
fn close_the_triad(
    world: &mut World,
    actor: Entity,
    target: Entity,
    perception: &Perception,
    index: &[(crate::PersonId, Entity)],
    settings: &SocietySettings,
    now: f64,
) {
    for (holder, other) in [(actor, target), (target, actor)] {
        let (Some(slot), Some(other_slot)) = (
            index.iter().position(|(_, e)| *e == holder),
            index.iter().position(|(_, e)| *e == other),
        ) else {
            continue;
        };
        let in_contact = perception
            .contact
            .get(slot)
            .is_some_and(|list| list.iter().any(|j| *j as usize == other_slot));
        if !in_contact {
            continue;
        }
        let Some(ties) = world.get::<Ties>(holder).cloned() else {
            continue;
        };
        let close = ties.close_targets(now as f32, &settings.tuning, settings.close_ties);
        for friend in close {
            if friend == other {
                continue;
            }
            if let Some(mut friend_ties) = world.get_mut::<Ties>(friend) {
                crate::relationships::introduce_toward(
                    &mut friend_ties,
                    other,
                    now as f32,
                    &settings.tuning,
                );
            }
        }
    }
}

/// (4f) Intergroup contact (plan D6): an accepted exchange with no harmful deed between people who
/// share no faction adds affinity both ways.
fn intergroup(world: &mut World, actor: Entity, target: Entity, tuning: &Tuning) {
    let Some(mut a_ties) = world.get::<Ties>(actor).cloned() else {
        return;
    };
    let Some(mut t_ties) = world.get::<Ties>(target).cloned() else {
        return;
    };
    {
        let mut a_to_t = *a_ties.entry(target);
        let mut t_to_a = *t_ties.entry(actor);
        crate::groups::intergroup_contact(&mut a_to_t, &mut t_to_a, tuning);
        *a_ties.entry(target) = a_to_t;
        *t_ties.entry(actor) = t_to_a;
    }
    if let Some(mut ties) = world.get_mut::<Ties>(actor) {
        *ties = a_ties;
    }
    if let Some(mut ties) = world.get_mut::<Ties>(target) {
        *ties = t_ties;
    }
}

/// (6) Records one side's memory of the exchange (plan D7).
#[allow(clippy::too_many_arguments)]
fn record(
    world: &mut World,
    person: Entity,
    other: Entity,
    interaction: crate::defs::InteractionId,
    accepted: bool,
    initiated: bool,
    valence: f32,
    importance: f32,
    now: f64,
) {
    let capacity = world.resource::<SocietySettings>().memory_capacity;
    if let Some(mut memories) = world.get_mut::<crate::mind::Memories>(person) {
        memories.record(
            crate::mind::Memory {
                day: now as f32,
                kind: crate::mind::MemoryKind::Interaction {
                    id: interaction,
                    accepted,
                    initiated,
                },
                other: Some(other),
                valence: valence.clamp(-1.0, 1.0),
                importance: importance.clamp(0.0, 1.0),
            },
            now,
            capacity,
        );
    }
}

/// (7) One direction of mere exposure for the pair (plan D4).
fn expose(world: &mut World, holder: Entity, other: Entity, settings: &SocietySettings, now: f64) {
    let bias = {
        let mine = world
            .get::<Memberships>(holder)
            .cloned()
            .unwrap_or_default();
        let theirs = world.get::<Memberships>(other).cloned().unwrap_or_default();
        let defs = world.resource::<SocietyDefs>().clone();
        crate::groups::shared_faction_bias(&mine, &theirs, &defs)
    };
    let dt = world_dt(world);
    let tuning = settings.tuning.clone();
    if let Some(mut ties) = world.get_mut::<Ties>(holder) {
        crate::relationships::expose_toward(&mut ties, other, bias, now as f32, dt, &tuning);
    }
}

/// The length of one society tick in game-days.
fn world_dt(world: &World) -> f32 {
    world.resource::<SocietyClock>().dt_days
}

/// (9) Barks (plan D7): the actor's line, then an independent draw for the target's.
fn bark(
    world: &mut World,
    spec: &crate::defs::InteractionSpec,
    actor: Entity,
    target: Entity,
    accepted: bool,
    settings: &SocietySettings,
) {
    let chance = settings.bark_chance;
    let actor_roll = world.resource_mut::<crate::SocietyRng>().0.unit() as f32;
    if actor_roll < chance && !spec.barks.actor.is_empty() {
        let index = world
            .resource_mut::<crate::SocietyRng>()
            .0
            .below(spec.barks.actor.len());
        let line = spec.barks.actor[index].clone();
        say(world, actor, target, &line);
    }
    let lines = if accepted {
        spec.barks.accepted.clone()
    } else {
        spec.barks.rejected.clone()
    };
    let target_roll = world.resource_mut::<crate::SocietyRng>().0.unit() as f32;
    if target_roll < chance && !lines.is_empty() {
        let index = world
            .resource_mut::<crate::SocietyRng>()
            .0
            .below(lines.len());
        let line = lines[index].clone();
        say(world, target, actor, &line);
    }
}

/// Writes one [`SocietyBark`](crate::SocietyBark), rendering `{self}`, `{target}` and `{place}`.
fn say(world: &mut World, speaker: Entity, target: Entity, template: &str) {
    let name_of = |entity: Entity| {
        world
            .get::<Name>(entity)
            .map_or_else(|| "someone".to_owned(), |n| n.as_str().to_owned())
    };
    let place = match world.get::<Doing>(speaker).map(|d| d.target) {
        Some(DoingTarget::Place(place)) => world
            .get::<Name>(place)
            .map_or_else(|| "somewhere".to_owned(), |n| n.as_str().to_owned()),
        _ => "somewhere".to_owned(),
    };
    let text = template
        .replace("{self}", &name_of(speaker))
        .replace("{target}", &name_of(target))
        .replace("{place}", &place);
    let feeling = world.get::<Emotions>(speaker).map_or(
        crate::interact::Feeling::Neutral,
        crate::interact::Feeling::from_emotions,
    );
    world.write_message(crate::interact::SocietyBark {
        speaker,
        text,
        thought: false,
        feeling,
    });
}

// -------------------------------------------------------------------------------------------------
// Scripts
// -------------------------------------------------------------------------------------------------

/// Evaluates an interaction's `requires` condition against the tick-start snapshot.
///
/// A missing snapshot (before the first tick) or a failed script counts as "does not hold", which is
/// what the rule set says a failed condition means: it is false, not an error the caller must handle.
fn condition_holds(
    world: &mut World,
    defs: &SocietyDefs,
    condition: crate::script::ScriptId,
    actor: Entity,
    target: Entity,
) -> bool {
    let Some(snapshot) = world
        .get_resource::<crate::snapshot::SnapshotCell>()
        .and_then(|cell| cell.0.clone())
    else {
        return false;
    };
    let engine = match world.remove_resource::<crate::script::ScriptEngine>() {
        Some(engine) => engine,
        None => return false,
    };
    let bindings = crate::script::Bindings {
        actor: person_id(world, actor),
        target: person_id(world, target),
        ..crate::script::Bindings::default()
    };
    let outcome = engine.eval_bool(&defs.scripts, condition, &snapshot, bindings);
    world.insert_resource(engine);
    match outcome {
        Ok(holds) => holds,
        Err(err) => {
            let owner = crate::script::ScriptEngine::owner(&defs.scripts, condition).to_owned();
            error!("bevy_society: script {owner} failed: {err}");
            world.write_message(crate::script::ScriptError {
                owner,
                message: err,
            });
            false
        }
    }
}

/// Runs an effect's script and queues its actions for the single application point.
///
/// The actions are queued rather than applied here: one application point per tick is what keeps a
/// script from changing what another part of the same tick reads.
fn run_effect(world: &mut World, script: crate::script::ScriptId, actor: Entity, target: Entity) {
    let Some(defs) = world.get_resource::<SocietyDefs>().cloned() else {
        return;
    };
    let Some(snapshot) = world
        .get_resource::<crate::snapshot::SnapshotCell>()
        .and_then(|cell| cell.0.clone())
    else {
        return;
    };
    let Some(engine) = world.remove_resource::<crate::script::ScriptEngine>() else {
        return;
    };
    let bindings = crate::script::Bindings {
        actor: person_id(world, actor),
        target: person_id(world, target),
        ..crate::script::Bindings::default()
    };
    let outcome = engine.run(&defs.scripts, script, &snapshot, bindings);
    world.insert_resource(engine);
    match outcome {
        Ok(actions) => {
            if let Some(mut pending) = world.get_resource_mut::<crate::core::PendingActions>() {
                pending.0.extend(actions);
            }
        }
        Err(err) => {
            let owner = crate::script::ScriptEngine::owner(&defs.scripts, script).to_owned();
            error!("bevy_society: script {owner} failed: {err}");
            world.write_message(crate::script::ScriptError {
                owner,
                message: err,
            });
        }
    }
}

/// A person's id as the script bindings expect, or −1 when it is not a live person.
fn person_id(world: &World, entity: Entity) -> i64 {
    world
        .get_resource::<PersonIndex>()
        .and_then(|index| {
            index
                .sorted
                .iter()
                .find(|(_, e)| *e == entity)
                .map(|(id, _)| i64::from(id.0))
        })
        .unwrap_or(-1)
}

// -------------------------------------------------------------------------------------------------
// Activity barks
// -------------------------------------------------------------------------------------------------

/// One thought line per activity start, with `bark_chance` (plan D7).
///
/// Reads the [`ActivityStarted`] messages the decision wrote, so the draw happens once per start in
/// `ActivityStarted` order — the same order every other per-person loop uses.
fn activity_barks(
    mut starts: MessageReader<ActivityStarted>,
    defs: Res<SocietyDefs>,
    settings: Res<SocietySettings>,
    mut rng: ResMut<crate::SocietyRng>,
    names: Query<&Name>,
    doing: Query<&Doing>,
    mut barks: MessageWriter<crate::interact::SocietyBark>,
) {
    for start in starts.read() {
        let Some(spec) = defs.activity_id(&start.activity) else {
            continue;
        };
        let Some(activity) = defs.activities.get(usize::from(spec.0)) else {
            continue;
        };
        if activity.barks.is_empty() || (rng.0.unit() as f32) >= settings.bark_chance {
            continue;
        }
        let line = activity.barks[rng.0.below(activity.barks.len())].clone();
        let of = |entity: Entity| {
            names
                .get(entity)
                .map_or_else(|_| "someone".to_owned(), |n| n.as_str().to_owned())
        };
        let place = doing
            .get(start.person)
            .ok()
            .and_then(|d| match d.target {
                DoingTarget::Place(place) => names.get(place).ok(),
                _ => None,
            })
            .map_or_else(|| "somewhere".to_owned(), |n| n.as_str().to_owned());
        let mut text = line
            .replace("{self}", &of(start.person))
            .replace("{place}", &place);
        if let Some(target) = start.target {
            text = text.replace("{target}", &of(target));
        }
        barks.write(crate::interact::SocietyBark {
            speaker: start.person,
            text,
            thought: true,
            feeling: crate::interact::Feeling::from_emotions(&crate::mind::Emotions::default()),
        });
    }
}

// -------------------------------------------------------------------------------------------------
// Reporting to the trigger queue
// -------------------------------------------------------------------------------------------------

/// Pushes a [`TriggerEvent`](crate::rules) for each exchange and activity this tick, so the trigger
/// queue sees them in the same tick they happened (plan D7).
///
/// Runs after the exchange set finishes. `Option<ResMut<…>>` because the behaviour layer is usable
/// without `RulesPlugin`: a host that took only the mind and behaviour plugins still gets its
/// activities, just no triggers.
fn report_to_triggers(
    mut exchanges: MessageReader<InteractionResolved>,
    mut activities: MessageReader<ActivityStarted>,
    defs: Option<Res<SocietyDefs>>,
    index: Option<Res<PersonIndex>>,
    mut queue: Option<ResMut<crate::rules::TriggerQueue>>,
) {
    let (Some(defs), Some(index), Some(queue)) = (defs, index, queue.as_mut()) else {
        for _ in exchanges.read() {}
        for _ in activities.read() {}
        return;
    };
    let id_of = |entity: Entity| {
        index
            .sorted
            .iter()
            .find(|(_, e)| *e == entity)
            .map_or(-1, |(id, _)| i64::from(id.0))
    };
    for exchange in exchanges.read() {
        let Some(id) = defs.interaction_id(&exchange.interaction) else {
            continue;
        };
        queue.0.push(crate::rules::TriggerEvent::Interaction {
            interaction: id,
            actor: id_of(exchange.actor),
            target: id_of(exchange.target),
            accepted: exchange.accepted,
        });
    }
    for activity in activities.read() {
        let Some(id) = defs.activity_id(&activity.activity) else {
            continue;
        };
        queue.0.push(crate::rules::TriggerEvent::Activity {
            activity: id,
            actor: id_of(activity.person),
            target: activity.target.map_or(-1, id_of),
        });
    }
}

/// The reads [`contact_gossip`] needs, grouped so the system stays inside clippy's argument limit.
#[derive(bevy::ecs::system::SystemParam)]
struct ContactGossipWorld<'w> {
    clock: Res<'w, SocietyClock>,
    settings: Res<'w, SocietySettings>,
    perception: Res<'w, Perception>,
    index: Res<'w, PersonIndex>,
    book: Res<'w, RumorBook>,
    defs: Res<'w, SocietyDefs>,
    queue: Option<ResMut<'w, InteractQueue>>,
}

/// **People speak when they have news** (plan D7b). Two people who share a place and are not otherwise
/// exchanging still talk *if the speaker has a rumor the listener does not know* — the gate the
/// `how-can-two-groups-ever-meet` decision never tried.
///
/// The earlier attempt queued a `chat` for every pair in contact, which nearly tripled all social contact
/// (13 742 requests over 17 game-days against a 7 500 baseline) and turned the village into a clique: 267
/// friend pairs out of 276 possible. The fault was not the rate but the *gate*: a `chat` is offered
/// whether or not either person has anything to say.
///
/// [`Knowledge::best_telling`] is self-limiting in exactly the way contact-chat was not. It requires a rumor
/// that is `alive`, whose subject is not the listener, that the teller still `spreader`s, and it ranks by
/// `|valence| · belief · 0.995^(hours since heard)` (Park et al. 2023, DOI 10.48550/arXiv.2304.03442). Once
/// the listener knows the rumor the telling stifles (Daley & Kendall 1964, DOI 10.1038/2041118a0), so the
/// pair stops speaking. Contact carries *news*, not company: a village with nothing to say stays quiet.
///
/// Proximity is the channel because that is what proximity is for — Eckland 1968, quoted in Buss 1993
/// (DOI 10.1037/0033-295x.100.2.204): "the chances are about 50-50 that the 'one and only' lives within
/// walking distance."
///
/// Determinism: iterates [`PersonIndex::sorted`] and each person's `contact` list in index order; draws
/// nothing from `SocietyRng`.
fn contact_gossip(
    world: ContactGossipWorld,
    knowledge: Query<&crate::Knowledge>,
    mut ties: Query<&mut Ties>,
) {
    let ContactGossipWorld {
        clock,
        settings,
        perception,
        index,
        book,
        defs,
        queue,
    } = world;
    let Some(mut queue) = queue else {
        return;
    };
    let Some(gossip_id) = defs.interaction_id("gossip") else {
        return;
    };
    let tuning = &settings.tuning;
    let now = clock.now_days as f32;
    for (i, &(_, entity)) in index.sorted.iter().enumerate() {
        let Ok(mine) = knowledge.get(entity) else {
            continue;
        };
        let Some(contacts) = perception.contact.get(i) else {
            continue;
        };
        for &j in contacts {
            let Some(&other) = perception.persons.get(j as usize) else {
                continue;
            };
            // **Never tell someone who already knows.** The Daley–Kendall rule *stifles* a teller that
            // repeats itself, and that is measured harm here: without this check every contact-tell to a
            // neighbour who already heard burns a spreader, and village rumor reach fell **0.58 -> 0.375**.
            // The check protects the spreader pool; the pair goes quiet because there is no news between
            // them, not because one of them has been silenced.
            let Ok(theirs) = knowledge.get(other) else {
                continue;
            };
            let telling = mine.best_telling(now, |rumor| {
                let entry = book.get(rumor)?;
                if !entry.alive || entry.subject == other || theirs.knows(rumor) {
                    return None;
                }
                Some(entry.valence)
            });
            if telling.is_none() {
                continue;
            }
            // A cooldown per directed tie: sharing a bench for a whole activity is one conversation,
            // not one per fixed step.
            let Ok(mut owned) = ties.get_mut(entity) else {
                continue;
            };
            let Some(tie) = owned.get_mut(other) else {
                continue;
            };
            if now - tie.last_contact_day < tuning.contact_gossip_cooldown_days {
                continue;
            }
            tie.last_contact_day = now;
            queue.0.push(crate::interact::Interact {
                actor: entity,
                target: other,
                interaction: "gossip".to_owned(),
            });
            let _ = gossip_id;
        }
    }
}