File: output.cc

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (2898 lines) | stat: -rw-r--r-- 82,259 bytes parent folder | download | duplicates (2)
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
/**
 * @file
 * @brief Functions used to print player related info.
**/

#include "AppHdr.h"

#include "output.h"

#include <cmath>
#include <cstdlib>
#include <sstream>

#include "ability.h"
#include "art-enum.h"
#include "areas.h"
#include "branch.h"
#include "colour.h"
#include "describe.h"
#ifndef USE_TILE_LOCAL
#endif
#include "english.h"
#include "env.h"
#include "files.h"
#include "god-abil.h"
#include "god-passive.h"
#include "initfile.h"
#include "invent.h"
#include "item-name.h"
#include "item-prop.h"
#include "jobs.h"
#include "lang-fake.h"
#include "libutil.h"
#include "macro.h" // command_to_string
#include "menu.h"
#include "message.h"
#include "misc.h"
#include "mutation.h"
#include "notes.h"
#include "ouch.h"
#include "player-equip.h"
#include "player-stats.h"
#include "prompt.h"
#include "religion.h"
#include "scroller.h"
#include "showsymb.h"
#include "skills.h"
#include "state.h"
#include "status.h"
#include "stringutil.h"
#include "tag-version.h"
#include "throw.h"
#include "tiles-build-specific.h"
#include "transform.h"
#include "viewchar.h"
#include "view.h"
#include "xom.h"


static bool _is_using_small_layout()
{
#ifdef USE_TILE_LOCAL
    return tiles.is_using_small_layout();
#endif
    return false;
}

static string _level_description_string_hud()
{
    const PlaceInfo& place = you.get_place_info();
    string short_name = branches[place.branch].shortname;

    if (brdepth[place.branch] > 1)
        short_name += make_stringf(":%d", you.depth);
    // Indefinite articles
    else if (place.branch != BRANCH_PANDEMONIUM
             && place.branch != BRANCH_DESOLATION
             && place.branch != BRANCH_ARENA
             && !is_connected_branch(place.branch))
    {
        short_name = article_a(short_name);
    }
    return short_name;
}

#ifdef USE_TILE_LOCAL

static bool _low_vertical_space()
{
    return crawl_view.hudsz.y < 30;
}

/*
 * this glorious piece of code works by:
    - overriding cgotoxy and cprintf
    - mapping the x,y coordinate of each part of the HUD to a
      value in the touchui_states enum and storing the current value
    - using the current state to decide what and where to actually
      render each part of the HUD

   123456789
 1 Nameaname
 2 TrDk
 3 Lugonu
 4 *.....
 5
 6 St Dx In
 7 nn nn nn
 8 AC SH EV
 9 nn nn nn
10 XL Next
11 10 25%
12
13 Noise
14 ########
15 Place
16 Dungeo:10
17 Time
18 1234.5
19
20 W: foobar
22 Abil: Bes
23
24 XXXXXXXXX      status lights
.
y  HPP MPP
 */
#include <stdarg.h>
#define CGOTOXY _cgotoxy_touchui
#define CPRINTF _cprintf_touchui
#define NOWRAP_EOL_CPRINTF _nowrap_eol_cprintf_touchui

enum touchui_states
{
    TOUCH_S_INIT  = 0x0,
    TOUCH_S_NULL  = 0x1,
    TOUCH_V_TITLE = 0x0101,
    TOUCH_V_TITL2 = 0x0102,
    TOUCH_V_GOD   = 0x0202, // dummy
    TOUCH_V_GOD2  = 0x0302, // dummy
    TOUCH_T_HP    = 0x0103,
    TOUCH_V_HP    = 0x0203, // dummy
    TOUCH_T_MP    = 0x0104,
    TOUCH_V_MP    = 0x0204, // dummy
    TOUCH_T_AC    = 0x0105,
    TOUCH_V_AC    = 0x0505,
    TOUCH_T_EV    = 0x0106,
    TOUCH_V_EV    = 0x0506,
    TOUCH_T_SH    = 0x0107,
    TOUCH_V_SH    = 0x0507,
    TOUCH_T_STR   = 0x1305,
    TOUCH_V_STR   = 0x1805,
    TOUCH_T_INT   = 0x1306,
    TOUCH_V_INT   = 0x1806,
    TOUCH_T_DEX   = 0x1307,
    TOUCH_V_DEX   = 0x1807,
    TOUCH_T_XL    = 0x0108,
    TOUCH_V_XL    = 0x0508,
    TOUCH_V_XL2   = 0x0E08,
    TOUCH_T_PLACE = 0x1308,
    TOUCH_V_PLACE = 0x1A08,
    TOUCH_T_NOISE = 0x0109,
    TOUCH_V_NOISE = 0x0809,
    TOUCH_V_NOISW = 0x0B09, // wizard mode
    TOUCH_V_NOISX = 0x1109, // extra wide
    TOUCH_T_TIME  = 0x1309,
    TOUCH_V_TIME  = 0x1909,
    TOUCH_T_WP    = 0x010A,
    TOUCH_V_WP    = 0x020A, // dummy
    TOUCH_T_QV    = 0x010B,
    TOUCH_V_QV    = 0x020B, // dummy
    TOUCH_V_LIGHT = 0x010C,
};
touchui_states TOUCH_UI_STATE = TOUCH_S_INIT;
static void _cgotoxy_touchui(int x, int y, GotoRegion region = GOTO_CRT)
{
    bool super_small = _low_vertical_space();
    if (_is_using_small_layout())
        TOUCH_UI_STATE = (touchui_states)((x<<8)+y);
    else
        TOUCH_UI_STATE = TOUCH_S_INIT;
    switch (TOUCH_UI_STATE)
    {
        case TOUCH_V_HP:
        case TOUCH_V_MP:
        case TOUCH_V_TITLE:
        case TOUCH_V_TITL2:
        case TOUCH_S_NULL:
            // no special behaviour for these
            break;
        case TOUCH_V_GOD:
            x = 1; y = 3;
            break;
        case TOUCH_V_GOD2:
            x = 1; y = 4;
            break;
        case TOUCH_T_AC:
            x = 1; y = (super_small) ? 5 : 6;
            break;
        case TOUCH_T_EV:
            x = 4; y = (super_small) ? 5 : 6;
            break;
        case TOUCH_T_SH:
            x = 7; y = (super_small) ? 5 : 6;
            break;
        case TOUCH_V_AC:
            x = 1; y = (super_small) ? 6 : 7;
            break;
        case TOUCH_V_EV:
            x = 4; y = (super_small) ? 6 : 7;
            break;
        case TOUCH_V_SH:
            x = 7; y = (super_small) ? 6 : 7;
            break;
        case TOUCH_T_STR:
            x = 1; y = (super_small) ? 7 : 8;
            break;
        case TOUCH_T_INT:
            x = 4; y = (super_small) ? 7 : 8;
            break;
        case TOUCH_T_DEX:
            x = 7; y = (super_small) ? 7 : 8;
            break;
        case TOUCH_V_STR:
            x = 1; y = (super_small) ? 8 : 9;
            break;
        case TOUCH_V_INT:
            x = 4; y = (super_small) ? 8 : 9;
            break;
        case TOUCH_V_DEX:
            x = 7; y = (super_small) ? 8 : 9;
            break;
        case TOUCH_T_XL:
            x = 1; y = (super_small) ? 9 : 10;
            break;
        case TOUCH_V_XL:
            x = 1; y = (super_small) ? 10 : 11;
            break;
        case TOUCH_V_XL2:
            x = 4; y = (super_small) ? 10 : 11;
            break;
        case TOUCH_T_NOISE:
            x = 1; y = (super_small) ? 11 : 13;
            break;
        case TOUCH_V_NOISE:
            x = 1; y = (super_small) ? 12 : 14;
            break;
        case TOUCH_V_NOISW:
            x = 4; y = (super_small) ? 12 : 14;
            break;
        case TOUCH_V_NOISX:
            x = 9; y = (super_small) ? 12 : 14;
            break;
        case TOUCH_T_PLACE:
            x = 1; y = (super_small) ? 13 : 15;
            break;
        case TOUCH_V_PLACE:
            x = 1; y = (super_small) ? 14 : 16;
            break;
        case TOUCH_T_TIME:
            x = 1; y = (super_small) ? 15 : 17;
            break;
        case TOUCH_V_TIME:
            x = 1; y = (super_small) ? 16 : 18;
            break;
        case TOUCH_T_WP:
            x = 1; y = (super_small) ? 17 : 20;
            break;
        case TOUCH_V_WP:
            x = 4; y = (super_small) ? 17 : 20;
            break;
        case TOUCH_T_QV:
            x = 1; y = (super_small) ? 18 : 21;
            break;
        case TOUCH_V_QV:
            x = 4; y = (super_small) ? 18 : 21;
            break;
        case TOUCH_V_LIGHT:
            x = 1; y = (super_small) ? 19 : 23;
            break;
        case TOUCH_T_HP:
            x = 2; y = crawl_view.hudsz.y;
            break;
        case TOUCH_T_MP:
            x = 6; y = crawl_view.hudsz.y;
            break;
        default:
            // reset state
            TOUCH_UI_STATE = TOUCH_S_INIT;
    }
    y = min(y, crawl_view.hudsz.y);
    cgotoxy(x,y,region);
}

static void _cprintf_touchui(const char *format, ...)
{
    va_list args;
    string  buf;
    vector<string> parts;
    va_start(args, format);
    buf = vmake_stringf(format, args);

    switch (TOUCH_UI_STATE)
    {
        case TOUCH_V_TITL2:
        case TOUCH_S_NULL:
            // don't draw these
            break;
        case TOUCH_T_HP:
            TOUCH_UI_STATE = TOUCH_V_HP;
            break;
        case TOUCH_T_MP:
            TOUCH_UI_STATE = TOUCH_V_MP;
            break;
        case TOUCH_V_TITLE:
            cprintf("%s", you.your_name.c_str());
            break;
        case TOUCH_V_HP:
        case TOUCH_V_MP:
            // suppress everything after initial print; rjustify
            cprintf("%3s", buf.c_str());
            TOUCH_UI_STATE = TOUCH_S_NULL;
            break;
        case TOUCH_T_STR:
        case TOUCH_T_INT:
        case TOUCH_T_DEX:
        case TOUCH_T_AC:
        case TOUCH_T_EV:
        case TOUCH_T_SH:
        case TOUCH_V_STR:
        case TOUCH_V_INT:
        case TOUCH_V_DEX:
        case TOUCH_V_AC:
        case TOUCH_V_EV:
        case TOUCH_V_SH:
            buf = buf.substr(0, 2);
            trim_string(buf);
            cprintf("%2s", buf.c_str());
            break;
        case TOUCH_T_XL:
            cprintf("XL Next");
            break;
        case TOUCH_T_PLACE:
            cprintf("Place");
            break;
        case TOUCH_V_PLACE:
            parts = split_string(":", _level_description_string_hud());
            if (parts.size() == 1)
                cprintf("%-9s", parts[0].substr(0,9).c_str());
            else
                cprintf("%s:%s", parts[0].substr(0,8-parts[1].size()).c_str(), parts[1].c_str());
            break;
        case TOUCH_T_NOISE:
            cprintf("Noise");
            break;
        case TOUCH_T_TIME:
            buf = buf.substr(0, buf.size()-1);
            cprintf(buf.c_str());
            break;
        case TOUCH_T_WP:
            TOUCH_UI_STATE = TOUCH_V_WP;
            cprintf("%s", buf.c_str());
            break;
        case TOUCH_T_QV:
            TOUCH_UI_STATE = TOUCH_V_QV;
            cprintf("%s", buf.c_str());
            break;

        default:
            cprintf("%s", buf.c_str());
    }
    va_end(args);
}

static void _nowrap_eol_cprintf_touchui(const char *format, ...)
{
    va_list args;
    string  buf;
    va_start(args, format);
    buf = vmake_stringf(format, args);

    // N.B. this should really be factored out and merged with the other switch-case above
    switch (TOUCH_UI_STATE)
    {
        case TOUCH_S_NULL:
            // don't print these
            break;
        case TOUCH_V_TITL2:
            cprintf("%s%s", species::get_abbrev(you.species),
                            get_job_abbrev(you.char_class));
            TOUCH_UI_STATE = TOUCH_S_NULL; // suppress whatever else it was going to print
            break;
        default:
            nowrap_eol_cprintf("%s", buf.c_str());
    }
    va_end(args);
}

#else
#define CGOTOXY cgotoxy
#define CPRINTF wrapcprintf
#define NOWRAP_EOL_CPRINTF nowrap_eol_cprintf
#endif

static string _god_powers();
static string _god_asterisks(bool leading_space = false);
static int _god_status_colour(int default_colour);

// Colour for captions like 'Health:', 'Str:', etc.
#define HUD_CAPTION_COLOUR Options.status_caption_colour

// Colour for values, which come after captions.
static const auto HUD_VALUE_COLOUR = LIGHTGREY;

// ----------------------------------------------------------------------
// colour_bar
// ----------------------------------------------------------------------

class colour_bar
{
    typedef unsigned short colour_t;
public:
    colour_t m_default;
    colour_t m_change_pos;
    colour_t m_change_neg;
    colour_t m_empty;
    int horiz_bar_width;

    colour_bar(colour_t default_colour,
               colour_t change_pos,
               colour_t change_neg,
               colour_t empty)
        : m_default(default_colour), m_change_pos(change_pos),
          m_change_neg(change_neg), m_empty(empty),
          horiz_bar_width(-1),
          m_old_disp(-1),
          m_request_redraw_after(0)
    {
        // m_old_disp < 0 means it's invalid and needs to be initialised.
    }

    bool wants_redraw() const
    {
        return m_request_redraw_after
               && you.num_turns >= m_request_redraw_after;
    }

    void draw(int ox, int oy, int val, int max_val, int sub_val = 0)
    {
        ASSERT(val <= max_val);
        if (max_val <= 0)
        {
            m_old_disp = -1;
            return;
        }
        const int width = (horiz_bar_width != -1) ?
                                  horiz_bar_width :
                                  crawl_view.hudsz.x - (ox - 1);
        const int sub_disp = (width * val / max_val);
        int disp  = width * max(0, val - sub_val) / max_val;
        const int old_disp = (m_old_disp < 0) ? sub_disp : m_old_disp;
        m_old_disp = sub_disp;

        // Always show at least one sliver of the sub-bar, if it exists
        if (sub_val)
            disp = max(0, min(sub_disp - 1, disp));

        CGOTOXY(ox, oy, GOTO_STAT);

        textcolour(BLACK);
        for (int cx = 0; cx < width; cx++)
        {
#ifdef USE_TILE_LOCAL
            // Maybe this should use textbackground too?
            textcolour(BLACK + m_empty * 16);

            if (cx < disp && cx < old_disp)
                textcolour(BLACK + m_default * 16);
            else if (cx < disp)
                textcolour(BLACK + m_change_pos * 16);
            else if (cx < sub_disp)
                textcolour(BLACK + YELLOW * 16);
            else if (old_disp >= sub_disp && cx < old_disp)
                textcolour(BLACK + m_change_neg * 16);
            putwch(' ');
#else
            if (cx < disp && cx < old_disp)
            {
                textcolour(m_default);
                putwch('=');
            }
            else if (cx < disp)
            {
                textcolour(m_change_pos);
                putwch('=');
            }
            else if (cx < sub_disp)
            {
                textcolour(YELLOW);
                putwch('=');
            }
            else if (cx < old_disp)
            {
                textcolour(m_change_neg);
                putwch('-');
            }
            else
            {
                textcolour(m_empty);
                putwch('-');
            }
#endif

            // If some change colour was rendered, redraw in a few
            // turns to clear it out.
            if (old_disp != disp)
                m_request_redraw_after = you.num_turns + 4;
            else
                m_request_redraw_after = 0;
        }

        textcolour(LIGHTGREY);
        textbackground(BLACK);
        // the cursor position is now invalid, because we are past the end of
        // the stat region: leave it somewhere valid.
        CGOTOXY(ox, oy, GOTO_STAT);
    }

    void vdraw(int ox, int oy, int val, int max_val)
    {
        // ox is width from l/h edge; oy is height from top
        // bars are 3chars wide and render down to hudsz.y-1
        const int bar_width = 3;
        const int obase     = crawl_view.hudsz.y-1;

        ASSERT(val <= max_val);
        if (max_val <= 0)
        {
            m_old_disp = -1;
            return;
        }

        const int height   = bar_width * (obase-oy+1);
        const int disp     = height * val / max_val;
        const int old_disp = (m_old_disp < 0) ? disp : m_old_disp;

        CGOTOXY(ox, obase, GOTO_STAT);

        textcolour(WHITE);
        for (int cx = 0; cx < height; cx++)
        {
            // Maybe this should use textbackground too?
            textcolour(BLACK + m_empty * 16);

            if (cx < disp)
                textcolour(BLACK + m_default * 16);
            else if (old_disp > disp && cx < old_disp)
                textcolour(BLACK + m_change_neg * 16);
            putwch(' ');

            // move up a line if we've drawn this bit of the bar
            if ((cx+1) % bar_width == 0)
                CGOTOXY(ox, obase-cx/bar_width, GOTO_STAT);

            // If some change colour was rendered, redraw in a few
            // turns to clear it out.
            if (old_disp != disp)
                m_request_redraw_after = you.num_turns + 4;
            else
                m_request_redraw_after = 0;
        }

        textcolour(LIGHTGREY);
        textbackground(BLACK);
    }

    void reset()
    {
        m_old_disp = -1;
        m_request_redraw_after = 0;
    }

 private:
    int m_old_disp;
    int m_request_redraw_after; // force a redraw at this turn count
};

static colour_bar HP_Bar(LIGHTGREEN, GREEN, RED, DARKGREY);

#ifdef USE_TILE_LOCAL
static colour_bar MP_Bar(BLUE, BLUE, LIGHTBLUE, DARKGREY);
#else
static colour_bar MP_Bar(LIGHTBLUE, BLUE, MAGENTA, DARKGREY);
#endif

#ifdef USE_TILE_LOCAL
static colour_bar Noise_Bar(WHITE, LIGHTGREY, LIGHTGREY, DARKGREY);
#else
static colour_bar Noise_Bar(LIGHTGREY, LIGHTGREY, MAGENTA, DARKGREY);
#endif

void reset_hud()
{
    HP_Bar.reset();
    MP_Bar.reset();
    Noise_Bar.reset();
}

// ----------------------------------------------------------------------
// Status display
// ----------------------------------------------------------------------

static bool _boosted_hp()
{
    return you.duration[DUR_DIVINE_VIGOUR]
           || you.berserk();
}

static bool _boosted_mp()
{
    return you.duration[DUR_DIVINE_VIGOUR];
}

static colour_t _colour_from_stat_mod(int mod)
{
    if (mod == 0)
        return HUD_VALUE_COLOUR;
    else if (mod < 0)
        return RED;
    else
        return LIGHTBLUE;
}

#ifdef DGL_SIMPLE_MESSAGING
void update_message_status()
{
    if (!SysEnv.have_messages)
        return;

    static const char * const msg = "(Hit _)";

    textcolour(LIGHTBLUE);

    CGOTOXY(crawl_view.hudsz.x - strwidth(msg) + 1, 1, GOTO_STAT);
    CPRINTF(msg);

    textcolour(LIGHTGREY);
}
#endif

void update_turn_count()
{
    if (crawl_state.game_is_arena())
        return;

    // Don't update turn counter when running/resting/traveling to
    // prevent pointless screen updates.
    if (mouse_control::current_mode() == MOUSE_MODE_NORMAL
        && (you.running > 0 || you.running < 0 && Options.travel_delay == -1))
    {
        return;
    }

    const int turncount_start_x = 19 + 6;
    int ypos = 9;
    // TODO: unify this with the calculation in print_stats
    if (you.has_mutation(MUT_HP_CASTING) && !_is_using_small_layout())
        ypos--;
    CGOTOXY(turncount_start_x, ypos, GOTO_STAT);

    textcolour(HUD_VALUE_COLOUR);
    string time = Options.show_game_time
        ? make_stringf("%.1f", you.elapsed_time / 10.0)
        : make_stringf("%d", you.num_turns);

    if (!_is_using_small_layout())
    {
        time += make_stringf(" (%.1f)",
                (you.elapsed_time - you.elapsed_time_at_last_input) / 10.0);
        CPRINTF("%s",
            chop_string(time, crawl_view.hudsz.x - turncount_start_x + 1).c_str());
    }
    else
        CPRINTF("%s", chop_string(time, 7).c_str());

    textcolour(LIGHTGREY);
}

static int _count_digits(int val)
{
    if (val > 999)
        return 4;
    else if (val > 99)
        return 3;
    else if (val > 9)
        return 2;
    return 1;
}

static const equipment_slot slot_order[] =
{
    SLOT_WEAPON, SLOT_OFFHAND, SLOT_WEAPON_OR_OFFHAND, SLOT_BODY_ARMOUR,
    SLOT_HAUNTED_AUX,
    SLOT_HELMET, SLOT_CLOAK, SLOT_GLOVES, SLOT_BOOTS, SLOT_BARDING,
    SLOT_AMULET, SLOT_RING, SLOT_GIZMO
};

static void _print_stats_equip(int x, int y)
{
    CGOTOXY(x, y, GOTO_STAT);
    textcolour(HUD_CAPTION_COLOUR);

    int total_slots = 0;
    for (int i = SLOT_FIRST_STANDARD; i < NUM_EQUIP_SLOTS; ++i)
        total_slots += you.equipment.num_slots[i];

    cprintf(total_slots > 8 ? "Eq: " : "Equip: ");
    textcolour(LIGHTGREY);
    for (equipment_slot slot : slot_order)
    {
        const int num_slots = you.equipment.num_slots[slot];
        if (num_slots == 0)
            continue;

        if (slot_is_melded(slot))
            for (int i = 0; i < num_slots; ++i)
                cprintf(" ");
        else
        {
            vector<player_equip_entry> entries = you.equipment.get_slot_entries(slot);
            for (int i = 0; i < num_slots; ++i)
            {
                if (i >= (int)entries.size())
                    cprintf(".");
                else if (entries[i].is_overflow)
                    cprintf(" ");
                else
                {
                    const item_def& item = entries[i].get_item();
                    cglyph_t g = get_item_glyph(item);
                    g.col = element_colour(g.col, you.pos(),
                                           !Options.animate_equip_bar);
                    formatted_string::parse_string(glyph_to_tagstr(g)).display();
                }
            }
        }
    }
    you.gear_change = false;
}

/*
 * Print the noise bar to the HUD with appropriate coloring.
 * if in wizmode, also print the numeric noise value.
 */
static void _print_stats_noise(int x, int y)
{
    CGOTOXY(x, y, GOTO_STAT);
    if (mouse_control::current_mode() == MOUSE_MODE_NORMAL
        && (you.running > 0 || you.running < 0 && Options.travel_delay == -1))
    {
        return;
    }

    bool silence = silenced(you.pos());
    int level = silence ? 0 : you.get_noise_perception(true);
    textcolour(HUD_CAPTION_COLOUR);
    CPRINTF("Noise: ");
    colour_t noisecolour;

    // This is calibrated roughly so that in an open-ish area:
    //   LIGHTGREY = not very likely to carry outside of your los
    //               (though it is possible depending on terrain).
    //   YELLOW = likely to carry outside of your los, up to double.
    //   RED = likely to carry at least 16 spaces, up to much further.
    //   LIGHTMAGENTA = really f*cking loud. (Gong, etc.)
    // In more enclosed areas, these values will be attenuated,
    // and this isn't represented.
    // See player::get_noise_perception for the mapping from internal noise
    // values to this 0-1000 scale.
    // NOTE: This color scheme is duplicated in player.js.
    if (level <= 333)
        noisecolour = LIGHTGREY;
    else if (level <= 666)
        noisecolour = YELLOW;
    else if (level < 1000)
        noisecolour = RED;
    else
        noisecolour = LIGHTMAGENTA;

    int bar_position;
    if (you.wizard && !silence)
    {
        Noise_Bar.horiz_bar_width = 6;
        bar_position = 10;

        // numeric noise level, basically the internal value used by noise
        // propagation (see shout.cc:noisy). The exact value is too hard to
        // interpret to show outside of wizmode, because noise propagation is
        // very complicated.
        CGOTOXY(x + bar_position - 3, y, GOTO_STAT);
        textcolour(noisecolour);
        CPRINTF("%2d ", you.get_noise_perception(false));
    }
    else
    {
        Noise_Bar.horiz_bar_width = 9;
        bar_position = 7;
    }

    if (_is_using_small_layout())
        Noise_Bar.horiz_bar_width--;

    if (silence)
    {
        CGOTOXY(x + bar_position, y, GOTO_STAT);
        textcolour(LIGHTMAGENTA);

        // This needs to be one extra wide in case silence happens
        // immediately after super-loud (magenta) noise
        CPRINTF("Silenced  ");
        Noise_Bar.reset(); // so it doesn't display a change bar after silence ends
    }
    else
    {
        if (level == 1000)
        {
            // the bar goes up to 11 for extra loud sounds! (Well, really 10.)
            Noise_Bar.horiz_bar_width += 1;
        }
        else
        {
            CGOTOXY(x + 16, y, GOTO_STAT);
            CPRINTF(" "); // clean up after the extra wide bar
        }
#ifndef USE_TILE_LOCAL
        // use the previous color for negative change in console; there's a
        // visual difference in bar width. Negative change doesn't get shown
        // in local tiles.
        Noise_Bar.m_change_neg = Noise_Bar.m_default;
#endif
        Noise_Bar.m_default = noisecolour;
        Noise_Bar.m_change_pos = noisecolour;
        Noise_Bar.draw(x + bar_position, y,
                       div_round_up((level * Noise_Bar.horiz_bar_width), 1000),
                       Noise_Bar.horiz_bar_width);
    }
    // intentional non-reset: after it has started drawing, we always redraw
    // noise. There's not a lot of cost to this, and the logic for detecting
    // if/when silenced status has changed is extremely annoying. So
    // you.redraw_noise is essentially only used to keep the noise bar from
    // drawing while the game is starting up. (If someone can figure out how
    // to correctly detect all the silence special cases, feel free to add that
    // in and I will see if you succeeded -advil.)
}

static void _print_stats_gold(int x, int y)
{
    textcolour(HUD_CAPTION_COLOUR);
    if (!_is_using_small_layout())
    {
        CGOTOXY(x, y, GOTO_STAT);
        CPRINTF("Gold:");
        CGOTOXY(x+6, y, GOTO_STAT);
    }
    else
    {
        CGOTOXY(3, 2, GOTO_STAT);
        CPRINTF("Gd ");
    }
    if (you.duration[DUR_GOZAG_GOLD_AURA])
        textcolour(LIGHTBLUE);
    else if (you.attribute[ATTR_VOUCHER] > 0)
        textcolour(MAGENTA);
    else
        textcolour(HUD_VALUE_COLOUR);
    CPRINTF("%-6d", you.gold);
}

static void _print_stats_mp(int x, int y)
{
    CGOTOXY(x, y, GOTO_STAT);
    if (you.has_mutation(MUT_HP_CASTING))
    {
        clear_to_end_of_line();
        return;
    }
    // Calculate colour
    short mp_colour = HUD_VALUE_COLOUR;

    const bool boosted = _boosted_mp();

    if (boosted)
        mp_colour = LIGHTBLUE;
    else
    {
        int mp_percent = (you.max_magic_points == 0
                          ? 100
                          : (you.magic_points * 100) / you.max_magic_points);

        for (const auto &entry : Options.mp_colour)
            if (mp_percent <= entry.first)
                mp_colour = entry.second;
    }

    textcolour(HUD_CAPTION_COLOUR);
    CPRINTF(player_drained() ? "MP: " : "Magic:  ");
    textcolour(mp_colour);
    CPRINTF("%d", you.magic_points);
    if (!boosted)
        textcolour(HUD_VALUE_COLOUR);
    CPRINTF("/%d", you.max_magic_points);

    int col = _count_digits(you.magic_points)
              + _count_digits(you.max_magic_points) + 1;

#if TAG_MAJOR_VERSION == 34
    int real_mp = get_real_mp(false);
    if (you.species == SP_DEEP_DWARF
        && real_mp != you.max_magic_points)
    {
        CPRINTF(" (%d)", real_mp);
        col += _count_digits(real_mp) + 3;
    }
#endif

    if (boosted)
        textcolour(HUD_VALUE_COLOUR);

    for (int i = 11-col; i > 0; i--)
        CPRINTF(" ");

#ifdef USE_TILE_LOCAL
    if (_is_using_small_layout())
    {
        if (_low_vertical_space())
            MP_Bar.vdraw(6, 19, you.magic_points, you.max_magic_points);
        else
            MP_Bar.vdraw(6, 24, you.magic_points, you.max_magic_points);
    }
    else
#endif
        MP_Bar.draw(19, y, you.magic_points, you.max_magic_points);

    you.redraw_magic_points = false;
}

static void _print_stats_hp(int x, int y)
{
    int max_max_hp = get_real_hp(true, false);

    // Calculate colour
    short hp_colour = HUD_VALUE_COLOUR;

    const bool boosted = _boosted_hp();

    if (boosted)
        hp_colour = LIGHTBLUE;
    else
    {
        const int hp_percent =
            (you.hp * 100) / get_real_hp(true, true);

        for (const auto &entry : Options.hp_colour)
            if (hp_percent <= entry.first)
                hp_colour = entry.second;
    }

    // 01234567890123456789
    // Health: xxx/yyy (zzz)
    CGOTOXY(x, y, GOTO_STAT);
    textcolour(HUD_CAPTION_COLOUR);
    CPRINTF(player_drained() ? "HP: " : "Health: ");
    textcolour(hp_colour);
    CPRINTF("%d", you.hp);
    if (!boosted)
        textcolour(HUD_VALUE_COLOUR);
    CPRINTF("/%d", you.hp_max);
    if (max_max_hp != you.hp_max)
        CPRINTF(" (%d)", max_max_hp);
    if (boosted)
        textcolour(HUD_VALUE_COLOUR);

    int col = wherex() - crawl_view.hudp.x;
    for (int i = 18-col; i > 0; i--)
        CPRINTF(" ");

#ifdef USE_TILE_LOCAL
    if (_is_using_small_layout())
    {
        if (_low_vertical_space())
            HP_Bar.vdraw(2, 19, you.hp, you.hp_max);
        else
            HP_Bar.vdraw(2, 24, you.hp, you.hp_max);
    }
    else
#endif
        HP_Bar.draw(19, y, you.hp, you.hp_max, you.hp - max(0, poison_survival()));

    you.redraw_hit_points = false;
}

static short _get_stat_colour(stat_type stat)
{
    if (you.stat(stat) <= 0)
        return LIGHTRED;

    // Check the stat_colour option for warning thresholds.
    for (const auto &entry : Options.stat_colour)
        if (you.stat(stat) <= entry.first)
            return entry.second;

    // Stat is magically increased.
    if (you.duration[DUR_DIVINE_STAMINA])
        return LIGHTBLUE;  // no end of effect warning

    return HUD_VALUE_COLOUR;
}

static void _print_stat(stat_type stat, int x, int y)
{
    CGOTOXY(x+5, y, GOTO_STAT);

    textcolour(_get_stat_colour(stat));
    CPRINTF("%d", you.stat(stat, false));
    if (!_is_using_small_layout())
        CPRINTF("    ");
}

static void _print_stats_doom(int x, int y)
{
    CGOTOXY(x, y, GOTO_STAT);

    // Hide the bar entirely if there is no active doom (since that will be true
    // most of the time).
    if (you.attribute[ATTR_DOOM] == 0 && !Options.always_show_doom_contam)
    {
        CPRINTF("          ");
        return;
    }

    CGOTOXY(x, y, GOTO_STAT);
    textcolour(HUD_CAPTION_COLOUR);
    CPRINTF("Doom: ");

    if (you.attribute[ATTR_DOOM] >= 75)
        textcolour(LIGHTMAGENTA);
    else if (you.attribute[ATTR_DOOM] >= 50)
        textcolour(LIGHTRED);
    else if (you.attribute[ATTR_DOOM] >= 25)
        textcolour(YELLOW);
    else if (you.attribute[ATTR_DOOM] > 0)
        textcolour(LIGHTGRAY);
    else
        textcolour(DARKGRAY);

    CPRINTF("%d%% ", you.attribute[ATTR_DOOM]);
    you.redraw_doom = false;
}

static void _print_stats_contam(int x, int y)
{
    CGOTOXY(x, y, GOTO_STAT);

    // Hide the bar entirely if the player has no contam
    if (you.magic_contamination == 0 && !Options.always_show_doom_contam)
    {
        CPRINTF("            ");
        return;
    }

    CGOTOXY(x, y, GOTO_STAT);
    textcolour(HUD_CAPTION_COLOUR);
    CPRINTF("Contam: ");

    const int contam = max(you.magic_contamination > 0 ? 1 : 0,
                           you.magic_contamination / 10);
    if (contam >= 200)
        textcolour(RED);
    else if (contam >= 100)
        textcolour(YELLOW);
    else
        textcolour(DARKGRAY);

    CPRINTF("%d%% ", contam);
    you.redraw_contam = false;
}

static void _print_stats_ac(int x, int y)
{
    // AC:
    auto text_col = _colour_from_stat_mod(you.temp_ac_mod());
    string ac = make_stringf("%2d ", you.armour_class_scaled(1));
#ifdef WIZARD
    if (you.wizard && !_is_using_small_layout())
        ac += make_stringf("(%d%%) ", you.gdr_perc(false));
#endif
    textcolour(text_col);
    CGOTOXY(x+4, y, GOTO_STAT);
    CPRINTF("%-12s", ac.c_str());

    // SH: (two lines lower)
    text_col = _colour_from_stat_mod(you.temp_sh_mod());

    string sh = make_stringf("%2d ", player_displayed_shield_class());
    textcolour(text_col);
    CGOTOXY(x+4, y+2, GOTO_STAT);
    CPRINTF("%-12s", sh.c_str());

    you.redraw_armour_class = false;
}

static void _print_stats_ev(int x, int y)
{
    CGOTOXY(x+4, y, GOTO_STAT);

    // Color EV based on whether temporary effects are raising or lowering it
    const int bonus = you.evasion_scaled(100) - you.evasion_scaled(100, true);
    textcolour(bonus < 0 ? RED
                         : bonus > 0 ? LIGHTBLUE
                                     : HUD_VALUE_COLOUR);
    CPRINTF("%2d ", you.evasion_scaled(1));

    you.redraw_evasion = false;
}

/**
 * Get the appropriate colour for the UI text describing the given weapon.
 *
 * @return     A colour enum for the given weapon.
 */
static int _wpn_name_colour(const item_def &wpn)
{
    if (you.corrosion_amount())
        return RED;

    const string prefix = item_prefix(wpn);
    const int prefcol = menu_colour(wpn.name(DESC_INVENTORY),
                                    prefix, "stats", false);
    if (prefcol != -1)
        return prefcol;
    return LIGHTGREY;
}

static string _wpn_name_corroded(const item_def &weapon)
{
    if (!you.corrosion_amount() || weapon.base_type != OBJ_WEAPONS)
        return weapon.name(DESC_PLAIN, true);

    item_def wpn_copy = weapon;
    wpn_copy.plus -= you.corrosion_amount();
    return wpn_copy.name(DESC_PLAIN, true);
}

static void _print_unarmed_name()
{
    textcolour(HUD_CAPTION_COLOUR);
    const string slot_name = "-) ";
    CPRINTF("%s", slot_name.c_str());
    textcolour(you.corrosion_amount() ? RED : get_form()->uc_colour);
    const int max_name_width = crawl_view.hudsz.x - slot_name.size();
    CPRINTF("%s", chop_string(you.unarmed_attack_name(),
                              max_name_width).c_str());
    textcolour(LIGHTGREY);
}

static void _print_weapon_name(const item_def &weapon, int width)
{
    textcolour(HUD_CAPTION_COLOUR);
    const char slot_letter = weapon.slot;
    const string slot_name = make_stringf("%c) ", slot_letter);
    CPRINTF("%s", slot_name.c_str());
    textcolour(_wpn_name_colour(weapon));
    const int max_name_width = width - slot_name.size();
    const string name = _wpn_name_corroded(weapon);
    CPRINTF("%s", chop_string(name, max_name_width).c_str());
    textcolour(LIGHTGREY);
}

/**
 * Print a description of the player's weapon (or lack thereof) to the UI.
 *
 * @param y     The y-coordinate to print the description at.
 */
static void _print_stats_wp(int y)
{
    if (mouse_control::current_mode() == MOUSE_MODE_NORMAL
        && (you.running > 0 || you.running < 0 && Options.travel_delay == -1))
    {
        return;
    }

    CGOTOXY(1, y, GOTO_STAT);

    const item_def *weapon = you.weapon();
    const item_def *offhand = you.offhand_weapon();
    if (weapon && offhand)
    {
        _print_weapon_name(*weapon, crawl_view.hudsz.x/2-1);
        CPRINTF("  ");
        _print_weapon_name(*offhand, crawl_view.hudsz.x/2-1);
    }
    else if (weapon || offhand)
        _print_weapon_name(weapon ? *weapon : *offhand, crawl_view.hudsz.x);
    else
        _print_unarmed_name();

    you.wield_change  = false;
}

static void _print_stats_qv(int y)
{
    CGOTOXY(1, y, GOTO_STAT);
    if (mouse_control::current_mode() == MOUSE_MODE_NORMAL
        && (you.running > 0 || you.running < 0 && Options.travel_delay == -1))
    {
        return;
    }

    formatted_string qdesc = quiver::get_secondary_action()->quiver_description();
#ifdef USE_TILE_LOCAL
    const int max_width = crawl_view.hudsz.x - (_is_using_small_layout() ? 0 : 4);
#else
    const int max_width = crawl_view.hudsz.x - 4;
#endif
    qdesc.chop(max_width, true).display();

    you.redraw_quiver = false;
}

struct status_light
{
    status_light(int c, string t) : colour(c), text(t) {}
    colour_t colour;
    string text;
};

static void _add_status_light_to_out(int i, vector<status_light>& out)
{
    status_info inf;

    if (fill_status_info(i, inf) && !inf.light_text.empty())
    {
        status_light sl(inf.light_colour, inf.light_text);
        out.push_back(sl);
    }
}

// The colour scheme for these flags is currently:
//
// - yellow, "orange", red      for bad conditions
// - light grey, white          for god based conditions
// - green, light green         for good conditions
// - blue, light blue           for good enchantments
// - magenta, light magenta     for "better" enchantments (deflect, fly)
//
// Note the usage of bad_ench_colour() correspond to levels that
// can be found in player.cc, ie those that the player can tell by
// using the '@' command. Things like confusion and sticky flame
// hide their amounts and are thus always the same colour (so
// we're not really exposing any new information). --bwr
static void _get_status_lights(vector<status_light>& out)
{
#ifdef DEBUG_DIAGNOSTICS
    if (mouse_control::current_mode() != MOUSE_MODE_NORMAL
        || !(you.running > 0 || you.running < 0 && Options.travel_delay == -1))
    {
        static char static_pos_buf[80];
        snprintf(static_pos_buf, sizeof(static_pos_buf),
                 "%2d,%2d", you.pos().x, you.pos().y);
        out.emplace_back(LIGHTGREY, static_pos_buf);
    }
#endif

    // We used to have to hardcode every status, now we just hardcode the
    // statuses important enough to appear first. (Rightmost)
    const unsigned int important_statuses[] =
    {
        STATUS_TESSERACT,
        STATUS_ORB,
        STATUS_ZOT,
        STATUS_STAT_ZERO,
        DUR_PARALYSIS,
        DUR_CONF,
        DUR_PETRIFYING,
        DUR_PETRIFIED,
        DUR_BERSERK,
        DUR_TELEPORT,
        DUR_ENKINDLED,
        STATUS_MNEMOPHAGE,
        DUR_HASTE,
        DUR_SLOW,
        STATUS_SPEED,
        DUR_DEATHS_DOOR,
        DUR_BLINK_COOLDOWN,
        DUR_BERSERK_COOLDOWN,
        DUR_EXHAUSTED,
        DUR_WORD_OF_CHAOS_COOLDOWN,
        DUR_DEATHS_DOOR_COOLDOWN,
        DUR_QUAD_DAMAGE,
        STATUS_SERPENTS_LASH,
    };

    bitset<STATUS_LAST_STATUS + 1> done;
    for (unsigned important : important_statuses)
    {
        _add_status_light_to_out(important, out);
        done.set(important);
    }

    for (unsigned status = 0; status <= STATUS_LAST_STATUS ; ++status)
        if (!done[status])
            _add_status_light_to_out(status, out);
}

static void _print_status_lights(int y)
{
    vector<status_light> lights;
    static int last_number_of_lights = 0;
    _get_status_lights(lights);
    if (lights.empty() && last_number_of_lights == 0)
    {
        you.redraw_status_lights = false;
        return;
    }
    last_number_of_lights = lights.size();

    size_t line_cur = y;
    const size_t line_end = crawl_view.hudsz.y+1;

    CGOTOXY(1, line_cur, GOTO_STAT);
#ifdef ASSERTS
    if (wherex() != crawl_view.hudp.x)
    {
        save_game(false); // should be safe
        die("misaligned HUD (is %d, should be %d)", wherex(), crawl_view.hudp.x);
    }
#endif

#ifdef USE_TILE_LOCAL
    if (!_is_using_small_layout())
    {
#endif
    size_t i_light = 0;
    while (true)
    {
        const int end_x = (wherex() - crawl_view.hudp.x)
                + (i_light < lights.size() ? strwidth(lights[i_light].text)
                                           : 10000);

        if (end_x <= crawl_view.hudsz.x)
        {
            textcolour(lights[i_light].colour);
            NOWRAP_EOL_CPRINTF("%s", lights[i_light].text.c_str());
            if (end_x < crawl_view.hudsz.x)
                NOWRAP_EOL_CPRINTF(" ");
            ++i_light;
        }
        else
        {
            clear_to_end_of_line();
            ++line_cur;
            // Careful not to trip the )#(*$ CGOTOXY ASSERT
            if (line_cur == line_end)
                break;
            CGOTOXY(1, line_cur, GOTO_STAT);
        }
    }
#ifdef USE_TILE_LOCAL
    }
    else
    {
        size_t i_light = 0;
        if (lights.size() == 1)
        {
            textcolour(lights[0].colour);
            CPRINTF("%s", lights[0].text.c_str());
        }
        else
        {
            while (i_light < lights.size() && (int)i_light < crawl_view.hudsz.x - 1)
            {
                textcolour(lights[i_light].colour);
                if (i_light == lights.size() - 1
                    && strwidth(lights[i_light].text) < crawl_view.hudsz.x - wherex())
                {
                    CPRINTF("%s",lights[i_light].text.c_str());
                }
                else if ((int)lights.size() > crawl_view.hudsz.x / 2)
                    CPRINTF("%.1s",lights[i_light].text.c_str());
                else
                    CPRINTF("%.1s ",lights[i_light].text.c_str());
                ++i_light;
            }
        }
        clear_to_end_of_line();
    }
#endif

    // Reset cursor position so it doesn't complain if we completely fill our space.
    CGOTOXY(1, 1, GOTO_STAT);

    you.redraw_status_lights = false;
}

static void _draw_wizmode_flag(const char *word)
{
    textcolour(LIGHTMAGENTA);
    // 3+ for the " **"
    CGOTOXY(1 + crawl_view.hudsz.x - (3 + strlen(word)), 1, GOTO_STAT);
    CPRINTF(" *%s*", word);
}

static void _redraw_title()
{
    const unsigned int WIDTH = crawl_view.hudsz.x;
    string title = filtered_lang(player_title());
    title = you.your_name + (title[0] == ',' ? "" : " ") + title;
    const bool small_layout = _is_using_small_layout();

    if (small_layout)
        title = you.your_name;
    else
    {
        unsigned int in_len = strwidth(title);
        if (in_len > WIDTH)
        {
            in_len -= 3;  // strwidth(" the ") - strwidth(", ")

            const unsigned int name_len = strwidth(you.your_name);
            string trimmed_name = you.your_name;
            // Squeeze name if required, the "- 8" is to not squeeze too much.
            if (in_len > WIDTH && (name_len - 8) > (in_len - WIDTH))
            {
                trimmed_name = chop_string(trimmed_name,
                                           name_len - (in_len - WIDTH) - 1);
            }

            title = trimmed_name + ", " + filtered_lang(player_title(false));
        }
    }

    // Line 1: Foo the Bar    *WIZARD*
    CGOTOXY(1, 1, GOTO_STAT);
    textcolour(small_layout && (you.wizard || you.explore) ? LIGHTMAGENTA : YELLOW);
    CPRINTF("%s", chop_string(title, WIDTH).c_str());
    if (you.wizard && !small_layout)
        _draw_wizmode_flag("WIZARD");
    else if (you.suppress_wizard && !small_layout)
        _draw_wizmode_flag("EX-WIZARD");
    else if (you.explore && !small_layout)
        _draw_wizmode_flag("EXPLORE");
#ifdef DGL_SIMPLE_MESSAGING
    update_message_status();
#endif

    // Line 2:
    // Minotaur [of God] [Piety]
    textcolour(YELLOW);
    CGOTOXY(1, 2, GOTO_STAT);
    string species = player_species_name();
    NOWRAP_EOL_CPRINTF("%s", species.c_str());
    if (you_worship(GOD_NO_GOD))
    {
        if (you.char_class == JOB_MONK
            && !you.has_mutation(MUT_FORLORN) // XX is this necessary?
            && !had_gods())
        {
            if (small_layout)
                CGOTOXY(3, 2, GOTO_STAT);
            string godpiety = "**....";
            textcolour(DARKGREY);
            if (small_layout || ((unsigned int)(strwidth(species) + strwidth(godpiety) + 1) <= WIDTH))
                NOWRAP_EOL_CPRINTF(" %s", godpiety.c_str());
            clear_to_end_of_line();
        }
        else
        {
            // Still need to clear in case the player was excommunicated
            clear_to_end_of_line();
        }
    }
    else
    {
        string god;
        if (small_layout)
        {
            CGOTOXY(2, 2, GOTO_STAT);
            god = "";
        }
        else
            god = " of ";
        god += you_worship(GOD_JIYVA) ? god_name_jiyva(true)
                                      : god_name(you.religion);
        NOWRAP_EOL_CPRINTF("%s", god.c_str());
        formatted_string piety = formatted_string::parse_string(_god_asterisks(true));
        textcolour(_god_status_colour(YELLOW));
        const unsigned int textwidth = (unsigned int)(strwidth(species) + strwidth(god) + strwidth(piety) + 1);
        if (small_layout)
        {
            CGOTOXY(3, 2, GOTO_STAT);
            piety.display();
        }
        else if (textwidth <= WIDTH)
            piety.display();
        clear_to_end_of_line();
        if (you_worship(GOD_GOZAG))
            _print_stats_gold(textwidth + 2, 2);
    }

    textcolour(LIGHTGREY);

    you.redraw_title = false;
}

void print_stats()
{
#ifndef USE_TILE_LOCAL
    if (crawl_state.smallterm)
        return;
#endif
    int ac_pos = 5;
    int ev_pos = ac_pos + 1;

    cursor_control coff(false);
    textcolour(LIGHTGREY);

    // Displayed evasion is tied to dex/str.
    if (you.redraw_stats[STAT_DEX]
        || you.redraw_stats[STAT_STR])
    {
        you.redraw_evasion = true;
    }

    if (HP_Bar.wants_redraw())
        you.redraw_hit_points = true;

    if (MP_Bar.wants_redraw())
        you.redraw_magic_points = true;

    // Poison display depends on regen rate, so should be redrawn every turn.
    if (you.duration[DUR_POISONING])
    {
        you.redraw_hit_points = true;
        you.redraw_status_lights = true;
    }

    if (you.redraw_title)
        _redraw_title();
    if (you.redraw_hit_points)
        _print_stats_hp(1, 3);

    int rows_hidden = 0;
    // hide the MP bar for djinni
    if (you.has_mutation(MUT_HP_CASTING))
    {
        if (!_is_using_small_layout())
            rows_hidden++;
    }
    else if (you.redraw_magic_points)
        _print_stats_mp(1, 4);

    // several of the following field names are printed in draw_border, not
    // here. It's supposed to be for things that don't ever change, but it's
    // mostly just a mess.
    if (you.redraw_armour_class)
        _print_stats_ac(1, ac_pos - rows_hidden);
    if (you.redraw_evasion)
        _print_stats_ev(1, ev_pos - rows_hidden);

    for (int i = 0; i < NUM_STATS; ++i)
        if (you.redraw_stats[i])
            _print_stat(static_cast<stat_type>(i), 19, 5 + i - rows_hidden);
    you.redraw_stats.init(false);

    if (you.redraw_doom)
        _print_stats_doom(32, 5 - rows_hidden);

    if (you.redraw_contam)
        _print_stats_contam(30, 6 - rows_hidden);

    if (you.redraw_experience)
    {
        CGOTOXY(1, 8 - rows_hidden, GOTO_STAT);
        textcolour(Options.status_caption_colour);
        CPRINTF("XL: ");
        if (_is_using_small_layout())
            CGOTOXY(5, 8, GOTO_STAT);
        textcolour(HUD_VALUE_COLOUR);
        CPRINTF("%2d ", you.experience_level);
        if (you.experience_level >= you.get_max_xl())
            CPRINTF("%10s", "");
        else
        {
            textcolour(Options.status_caption_colour);
            if (!_is_using_small_layout())
                CPRINTF("Next: ");
            else
                CGOTOXY(14, 8, GOTO_STAT);
            textcolour(HUD_VALUE_COLOUR);
            CPRINTF("%2d%% ", get_exp_progress());
        }
        you.redraw_experience = false;
    }

    // Line 9 is Noise and Turns
    if (Options.equip_bar)
    {
         if (you.gear_change || you.wield_change)
            _print_stats_equip(1, 9 - rows_hidden);
    }
    else if (you.redraw_noise)
        _print_stats_noise(1, 9 - rows_hidden);

    if (you.wield_change)
        _print_stats_wp(10 - rows_hidden);

    if (you.redraw_quiver)
        _print_stats_qv(11 - rows_hidden);

    if (you.redraw_status_lights)
        _print_status_lights(12 - rows_hidden);

#ifndef USE_TILE_LOCAL
    assert_valid_cursor_pos();
#endif
}

void print_stats_level()
{
    int ypos = 8;
    // TODO: unify this with the calculation in print_stats
    if (you.has_mutation(MUT_HP_CASTING) && !_is_using_small_layout())
        ypos--;

    CGOTOXY(19, ypos, GOTO_STAT);
    textcolour(HUD_CAPTION_COLOUR);
    CPRINTF("Place: ");

    if (_is_using_small_layout())
        CGOTOXY(26, ypos, GOTO_STAT);
    textcolour(HUD_VALUE_COLOUR);
#ifdef DEBUG_DIAGNOSTICS
    CPRINTF("(%d) ", env.absdepth0 + 1);
#endif
    CPRINTF("%s", _level_description_string_hud().c_str());
    clear_to_end_of_line();
}

void draw_border()
{
    textcolour(HUD_CAPTION_COLOUR);

    CGOTOXY(1,1, GOTO_CRT);
    clrscr();

    textcolour(Options.status_caption_colour);

    int ac_pos;
    // TODO: unify this calculation with rows_hidden in print_stats in a
    // non-insane way
    if (you.has_mutation(MUT_HP_CASTING) && !_is_using_small_layout())
        ac_pos = 4;
    else
        ac_pos = 5;

    int ev_pos = ac_pos + 1;
    int sh_pos = ac_pos + 2;
    int str_pos = ac_pos;
    int int_pos = ev_pos;
    int dex_pos = sh_pos;

    // "Health:" and "Magic:" printed elsewhere
    CGOTOXY(1, ac_pos, GOTO_STAT); CPRINTF("AC:");
    CGOTOXY(1, ev_pos, GOTO_STAT); CPRINTF("EV:");
    CGOTOXY(1, sh_pos, GOTO_STAT); CPRINTF("SH:");

    CGOTOXY(19, str_pos, GOTO_STAT); CPRINTF("Str:");
    CGOTOXY(19, int_pos, GOTO_STAT); CPRINTF("Int:");
    CGOTOXY(19, dex_pos, GOTO_STAT); CPRINTF("Dex:");

    // "XL:" and "Place:" printed elsewhere
    // "Noise:" printed elsewhere
    CGOTOXY(19, ac_pos + 4, GOTO_STAT);
    CPRINTF(Options.show_game_time ? "Time:" : "Turn:");
}

#ifndef USE_TILE_LOCAL
void smallterm_warning()
{
    CGOTOXY(1,1, GOTO_CRT);
    clrscr();
    CPRINTF("Your terminal window is too small; please resize to at least %d,%d", MIN_COLS, MIN_LINES);
}
#endif

void redraw_screen(bool show_updates)
{
    if (!crawl_state.need_save)
    {
        // If the game hasn't started, don't do much.
        clrscr();
        return;
    }

#ifdef USE_TILE_WEB
    if (!ui::has_layout())
        tiles.pop_all_ui_layouts();
#endif

#ifndef USE_TILE_LOCAL
    if (crawl_state.smallterm)
    {
        smallterm_warning();
        return;
    }
#endif

    draw_border();

    you.redraw_stats.init(true);
    you.redraw_title         = true;
    you.redraw_hit_points    = true;
    you.redraw_magic_points  = true;
    you.redraw_armour_class  = true;
    you.redraw_evasion       = true;
    you.redraw_experience    = true;
    you.wield_change         = true;
    you.redraw_quiver        = true;
    you.redraw_status_lights = true;
    you.redraw_noise         = true;
    you.gear_change          = true;
    you.redraw_doom          = true;
    you.redraw_contam        = true;

    print_stats();

    {
        no_notes nx;
        print_stats_level();
#ifdef DGL_SIMPLE_MESSAGING
        update_message_status();
#endif
        update_turn_count();
    }

    if (Options.messages_at_top)
    {
        display_message_window();
        viewwindow(show_updates);
    }
    else
    {
        viewwindow(show_updates);
        display_message_window();
    }

#ifndef USE_TILE_LOCAL
    assert_valid_cursor_pos();
#endif
}

// ----------------------------------------------------------------------
// Monster pane
// ----------------------------------------------------------------------

static string _get_monster_name(const monster_info& mi, int count, bool fullname)
{
    string desc = "";

    const char * const adj = mi.attitude == ATT_FRIENDLY ? "friendly"
                           : mi.attitude == ATT_HOSTILE  ? nullptr
                                                         : "neutral";

    string monpane_desc;
    int col;
    mi.to_string(count, monpane_desc, col, fullname, adj);

    if (count == 1)
    {
        if (!mi.is(MB_NAME_THE))
            desc = (is_vowel(monpane_desc[0]) ? "an " : "a ") + desc;
        else if (adj || !mi.is(MB_NAME_UNQUALIFIED))
            desc = "the " + desc;
    }

    desc += monpane_desc;
    return desc;
}

// If past is true, the messages should be printed in the past tense
// because they're needed for the morgue dump.
string mpr_monster_list(bool past)
{
    // Get monsters via the monster_pane_info, sorted by difficulty.
    vector<monster_info> mons;
    get_monster_info(mons);

    string msg = "";
    if (mons.empty())
    {
        msg  = "There ";
        msg += (past ? "were" : "are");
        msg += " no monsters in sight!";

        return msg;
    }

    vector<string> describe;

    int count = 0;
    for (unsigned int i = 0; i < mons.size(); ++i)
    {
        if (i > 0 && monster_info::less_than(mons[i-1], mons[i]))
        {
            describe.push_back(_get_monster_name(mons[i-1], count, true).c_str());
            count = 0;
        }
        count++;
    }

    describe.push_back(_get_monster_name(mons[mons.size()-1], count, true).c_str());

    msg = "You ";
    msg += (past ? "could" : "can");
    msg += " see ";

    if (describe.size() == 1)
        msg += describe[0];
    else
        msg += comma_separated_line(describe.begin(), describe.end());
    msg += ".";

    return msg;
}

#ifndef USE_TILE_LOCAL
static void _print_next_monster_desc(const vector<monster_info>& mons,
                                     int& start, bool zombified = false)
{
    // Skip forward to past the end of the range of identical monsters.
    unsigned int end;
    for (end = start + 1; end < mons.size(); ++end)
    {
        // Array is sorted, so if !(m1 < m2), m1 and m2 are "equal".
        if (monster_info::less_than(mons[start], mons[end], zombified, zombified))
            break;
    }
    // Postcondition: all monsters in [start, end) are "equal"

    // Print info on the monsters we've found.
    {
        int printed = 0;

        // One glyph for each monster.
        for (unsigned int i_mon = start; i_mon < end; i_mon++)
        {
            monster_info mi = mons[i_mon];
            cglyph_t g = get_mons_glyph(mi);
            textcolour(g.col);
            CPRINTF("%s", stringize_glyph(g.ch).c_str());
            ++printed;

            // Printing too many looks pretty bad, though.
            if (i_mon > 6)
                break;
        }
        textcolour(LIGHTGREY);

        const int count = (end - start);

        if (count == 1)  // Print an icon representing damage level.
        {
            CPRINTF(" ");

            monster_info mi = mons[start];
#ifdef TARGET_OS_WINDOWS
            textcolour(real_colour(dam_colour(mi) | COLFLAG_ITEM_HEAP, mi.pos));
#else
            textcolour(real_colour(dam_colour(mi) | COLFLAG_REVERSE, mi.pos));
#endif
            CPRINTF(" ");
            textbackground(BLACK);
            textcolour(LIGHTGREY);
            CPRINTF(" ");
            printed += 3;
        }
        else
        {
            textcolour(LIGHTGREY);
            CPRINTF("  ");
            printed += 2;
        }

        if (printed < crawl_view.mlistsz.x)
        {
            int desc_colour;
            string desc;
            mons_to_string_pane(desc, desc_colour, zombified,
                                mons, start, count);
            textcolour(desc_colour);
            if (static_cast<int>(desc.length()) > crawl_view.mlistsz.x - printed)
            {
                ASSERT(crawl_view.mlistsz.x - 2 - printed >= 0);
                desc.resize(crawl_view.mlistsz.x - 2 - printed, ' ');
                desc += "…)";
            }
            else
                desc.resize(crawl_view.mlistsz.x - printed, ' ');
            CPRINTF("%s", desc.c_str());
        }
    }

    // Set start to the next un-described monster.
    start = end;
    textcolour(LIGHTGREY);
}
#endif

#ifndef USE_TILE_LOCAL
// #define BOTTOM_JUSTIFY_MONSTER_LIST
// Returns -1 if the monster list is empty, 0 if there are so many monsters
// they have to be consolidated, and 1 otherwise.
int update_monster_pane()
{
    if (!map_bounds(you.pos()) && !crawl_state.game_is_arena())
        return -1;

    const int max_print = crawl_view.mlistsz.y;
    textbackground(BLACK);

    if (max_print <= 0)
        return -1;

    {
        save_cursor_pos save;

        vector<monster_info> mons;
        get_monster_info(mons);

        // Count how many groups of monsters there are.
        unsigned int lines_needed = mons.size();
        for (unsigned int i = 1; i < mons.size(); i++)
            if (!monster_info::less_than(mons[i-1], mons[i]))
                --lines_needed;

        bool full_info = true;
        if (lines_needed > (unsigned int) max_print)
        {
            full_info = false;

            // Use type names rather than full names ("small zombie" vs
            // "rat zombie") in order to take up fewer lines.

            lines_needed = mons.size();
            for (unsigned int i = 1; i < mons.size(); i++)
                if (!monster_info::less_than(mons[i-1], mons[i], false, false))
                    --lines_needed;
        }

    #ifdef BOTTOM_JUSTIFY_MONSTER_LIST
        const int skip_lines = max<int>(0, crawl_view.mlistsz.y-lines_needed);
    #else
        const int skip_lines = 0;
    #endif

        // Print the monsters!
        string blank;
        blank.resize(crawl_view.mlistsz.x, ' ');
        int i_mons = 0;
        for (int i_print = 0; i_print < max_print; ++i_print)
        {
            CGOTOXY(1, 1 + i_print, GOTO_MLIST);
            // i_mons is incremented by _print_next_monster_desc
            if (i_print >= skip_lines && i_mons < (int) mons.size())
                _print_next_monster_desc(mons, i_mons, full_info);
            else
                CPRINTF("%s", blank.c_str());
        }

        if (i_mons < (int)mons.size())
        {
            // Didn't get to all of them.
            CGOTOXY(crawl_view.mlistsz.x - 2, crawl_view.mlistsz.y, GOTO_MLIST);
            textbackground(COLFLAG_REVERSE);
            CPRINTF("(…)");
            textbackground(BLACK);
        }

        if (mons.empty())
            return -1;

        return full_info;
    }
}
#else
// FIXME: Implement this for Tiles!
int update_monster_pane()
{
    return false;
}
#endif

int equip_slot_by_name(const char *s)
{
    for (int i = SLOT_FIRST_STANDARD; i <= SLOT_LAST_STANDARD; ++i)
    {
        const equipment_slot slot = static_cast<equipment_slot>(i);
        if (!strcasecmp(s, equip_slot_name(slot, true)) ||
            !strcasecmp(s, equip_slot_name(slot, false)))
        {
            return i;
        }
    }

    return -1;
}

// Colour the string according to the level of an ability/resistance.
// Take maximum possible level into account.
static const char* _determine_colour_string(int level, int max_level,
                                            bool immune = false)
{
    if (immune)
        return "<lightgreen>";

    // No colouring for larger bars.
    if (max_level > 3)
        return "<lightgrey>";

    switch (level)
    {
    case 3:
    case 2:
        if (max_level > 1)
            return "<lightgreen>";
        // else fall-through
    case 1:
        return "<green>";
    case -2:
    case -3:
        if (max_level > 1)
            return "<lightred>";
        // else fall-through
    case -1:
        return "<red>";
    default:
        return "<lightgrey>";
    }
}

int stealth_pips()
{
    // round up.
    return (player_stealth() + STEALTH_PIP - 1) / STEALTH_PIP;
}

static string _stealth_bar(int label_length, int sw)
{
    string bar;
    //no colouring
    bar += _determine_colour_string(0, 5);
    bar += chop_string("Stlth", label_length);

    const int unadjusted_pips = stealth_pips();
    const int bar_len = 10;
    const int num_high_pips = unadjusted_pips % bar_len;
    static const vector<char> pip_tiers = { ' ', '+', '*', '#', '!' };
    const int max_tier = pip_tiers.size() - 1;
    const int low_tier = min(unadjusted_pips / bar_len, max_tier);
    const int high_tier = min(low_tier + 1, max_tier);

    bar.append(num_high_pips, pip_tiers[high_tier]);
    bar.append(bar_len-num_high_pips, pip_tiers[low_tier]);
    bar += "\n";
    linebreak_string(bar, sw);
    return bar;
}
static string _status_mut_rune_list(int sw);

static void _append_overview_screen_item(column_composer& cols,
                                         vector<char>& equip_chars,
                                         int sw,
                                         const item_def& item,
                                         bool melded)
{
    const string prefix = item_prefix(item);
    const int prefcol = menu_colour(item.name(DESC_INVENTORY), prefix, "resists", false);
    const int col = prefcol == -1 ? LIGHTGREY : prefcol;

    // Colour melded equipment dark grey.
    string colname = melded ? "darkgrey" : colour_to_str(col);

    const int item_idx = item.link;
    const char equip_char = index_to_letter(item_idx);

    string str = make_stringf(
                    "<w>%c</w> - <%s>%s%s</%s>",
                    equip_char,
                    colname.c_str(),
                    melded ? "melded " : "",
                    chop_string(item.name(DESC_PLAIN, true),
                            melded ? sw - 32 : sw - 25, false).c_str(),
                    colname.c_str());
    equip_chars.push_back(equip_char);

    cols.add_formatted(1, str.c_str(), false);
}

// helper for print_overview_screen
static void _print_overview_screen_equip(column_composer& cols,
                                         vector<char>& equip_chars,
                                         int sw)
{
    sw = min(max(sw, 79), 640);
    if (Options.show_resist_percent)
        sw -= 3;

    for (equipment_slot slot : slot_order)
    {
        // Skip slots the player doesn't have any of
        const int num_slots = you.equipment.num_slots[slot];
        if (num_slots == 0)
            continue;

        const string slot_name_lwr = lowercase_string(equip_slot_name(slot));
        const bool slot_melded = slot_is_melded(slot);
        string str;

        vector<player_equip_entry> equipped = you.equipment.get_slot_entries(slot);
        for (int i = 0; i < num_slots; ++i)
        {
            if (i >= (int)equipped.size())
            {
                // Some special-cased messages:
                if (slot == SLOT_WEAPON && i == 0)
                    str = "  - " + you.unarmed_attack_name();
                else if (slot_melded)
                    str = "<darkgrey>(" + slot_name_lwr + " unavailable)</darkgrey>";
                else
                    str = "<darkgrey>(no " + slot_name_lwr + ")</darkgrey>";

                cols.add_formatted(1, str, false);
                continue;
            }
            else if (equipped[i].is_overflow)
            {
                str = "  <darkgrey>[" + slot_name_lwr + " occupied]</darkgrey>";
                cols.add_formatted(1, str, false);
                continue;
            }

            const item_def& item = equipped[i].get_item();
            const bool melded    = equipped[i].melded;
            _append_overview_screen_item(cols, equip_chars, sw, item, melded);
        }
    }

    if (item_def* item = you.active_talisman())
    {
        _append_overview_screen_item(cols, equip_chars, sw, *item,
                                     you.form != you.default_form);
    }
}

static string _overview_screen_title(int sw)
{
    string title = player_title();
    title = (title[0] == ',' ? "" : " ") + title + " ";

    string species_job = make_stringf("(%s %s)",
                                      species::name(you.species).c_str(),
                                      get_job_name(you.char_class));

    handle_real_time();
    string time_turns = make_stringf(" Turns: %d, Time: ", you.num_turns)
                      + make_time_string(you.real_time(), true);

    const int char_width = strwidth(species_job);
    const int title_width = strwidth(title);

    int linelength = strwidth(you.your_name) + title_width
                   + char_width + strwidth(time_turns);

    if (linelength >= sw)
    {
        species_job = make_stringf("(%s%s)",
                                    species::get_abbrev(you.species),
                                    get_job_abbrev(you.char_class));
        linelength -= (char_width - strwidth(species_job));
    }

    // Still not enough?
    if (linelength >= sw)
    {
        title = " ";
        linelength -= (title_width - 1);
    }

    string text;
    text = "<yellow>";
    text += you.your_name;
    text += title;
    text += species_job;

    const int num_spaces = sw - linelength - 1;
    if (num_spaces > 0)
        text += string(num_spaces, ' ');

    text += time_turns;
    text += "</yellow>\n";

    return text;
}

#ifdef WIZARD
static string _wiz_god_powers()
{
    string godpowers = god_name(you.religion);
    return make_stringf("%s %d (%d)", god_name(you.religion).c_str(),
                                      you.raw_piety,
                                      you.duration[DUR_PIETY_POOL]);
}
#endif

static string _god_powers()
{
    if (you_worship(GOD_NO_GOD))
        return "";

    const string name = god_name(you.religion);
    if (you_worship(GOD_GOZAG))
        return colour_string(name, _god_status_colour(god_colour(you.religion)));

    return colour_string(chop_string(name, 20, false)
              + " [" + _god_asterisks() + "]",
              _god_status_colour(god_colour(you.religion)));
}

static string _god_asterisks(bool leading_space)
{
    if (you_worship(GOD_NO_GOD))
        return "";

    if (you_worship(GOD_GOZAG))
        return "";

    string str;
    if (you_worship(GOD_XOM))
    {
        const int p_rank = xom_favour_rank() - 1;
        if (p_rank >= 0)
        {
            str = string(p_rank, '.') + "*"
                  + string(NUM_PIETY_STARS - 1 - p_rank, '.');
        }
        else
            str = string(NUM_PIETY_STARS, '.'); // very special plaything
    }
    else
    {
        const int prank = piety_rank();
        const int pips = ostracism_pips();
        if (pips > 0)
        {
            str = string(prank, '*')
                    + string(NUM_PIETY_STARS - prank - pips, '.')
                    + "<lightmagenta>"
                    + string(pips, 'X')
                    + "</lightmagenta>";
        }
        else
            str = string(prank, '*') + string(NUM_PIETY_STARS - prank, '.');
    }

    return make_stringf("%s%s", leading_space ? " " : "", str.c_str());
}

/**
 * What colour should the god status display be?
 *
 * @param default_colour   The default colour, if not under penance or boredom.
 * @return                 A colour for the god status display.
 */
static int _god_status_colour(int default_colour)
{
    if (player_under_penance())
        return RED;

    if (you_worship(GOD_XOM) && you.gift_timeout <= 1)
        return you.gift_timeout ? RED : LIGHTRED;

    return default_colour;
}

static vector<formatted_string> _get_overview_stats()
{
    formatted_string entry;

    // 4 columns
    int col1 = 20;
    int col2 = 10;
    int col3 = 11;

    if (player_drained())
        col1 += 1;

    column_composer cols(4, col1, col1 + col2, col1 + col2 + col3);

    entry.textcolour(HUD_CAPTION_COLOUR);
    if (player_drained())
        entry.cprintf("HP:   ");
    else
        entry.cprintf("Health: ");

    if (_boosted_hp())
        entry.textcolour(LIGHTBLUE);
    else
        entry.textcolour(HUD_VALUE_COLOUR);

    entry.cprintf("%d/%d", you.hp, you.hp_max);
    if (player_drained())
        entry.cprintf(" (%d)", get_real_hp(true, false));

    cols.add_formatted(0, entry.to_colour_string(), false);
    entry.clear();

    if (!you.has_mutation(MUT_HP_CASTING))
    {
        entry.textcolour(HUD_CAPTION_COLOUR);
        if (player_drained())
            entry.cprintf("MP:   ");
        else
            entry.cprintf("Magic:  ");

        if (_boosted_mp())
            entry.textcolour(LIGHTBLUE);
        else
            entry.textcolour(HUD_VALUE_COLOUR);

        entry.cprintf("%d/%d", you.magic_points, you.max_magic_points);
#if TAG_MAJOR_VERSION == 34
        if (you.species == SP_DEEP_DWARF
            && get_real_mp(false) != you.max_magic_points)
        {
            entry.cprintf(" (%d)", get_real_mp(false));
        }
#endif

        cols.add_formatted(0, entry.to_colour_string(), false);
        entry.clear();
    }

    entry.textcolour(HUD_CAPTION_COLOUR);
    if (player_drained())
        entry.cprintf("Gold: ");
    else
        entry.cprintf("Gold:   ");

    entry.textcolour(HUD_VALUE_COLOUR);

    entry.cprintf("%d", you.gold);

    cols.add_formatted(0, entry.to_colour_string(), false);
    entry.clear();

    entry.textcolour(HUD_CAPTION_COLOUR);
    entry.cprintf("AC: ");

    entry.textcolour(_colour_from_stat_mod(you.temp_ac_mod()));

    entry.cprintf("%2d", you.armour_class_scaled(1));

    cols.add_formatted(1, entry.to_colour_string(), false);
    entry.clear();

    entry.textcolour(HUD_CAPTION_COLOUR);
    entry.cprintf("EV: ");

    entry.textcolour(_colour_from_stat_mod(you.temp_ev_mod()));

    entry.cprintf("%2d", you.evasion_scaled(1));

    cols.add_formatted(1, entry.to_colour_string(), false);
    entry.clear();

    entry.textcolour(HUD_CAPTION_COLOUR);
    entry.cprintf("SH: ");

    entry.textcolour(_colour_from_stat_mod(you.temp_sh_mod()));

    entry.cprintf("%2d", player_displayed_shield_class());

    cols.add_formatted(1, entry.to_colour_string(), false);
    entry.clear();

    entry.textcolour(HUD_CAPTION_COLOUR);
    entry.cprintf("Str: ");

    entry.textcolour(_get_stat_colour(STAT_STR));

    entry.cprintf("%2d", you.strength(false));

    cols.add_formatted(2, entry.to_colour_string(), false);
    entry.clear();

    entry.textcolour(HUD_CAPTION_COLOUR);
    entry.cprintf("Int: ");

    entry.textcolour(_get_stat_colour(STAT_INT));

    entry.cprintf("%2d", you.intel(false));

    cols.add_formatted(2, entry.to_colour_string(), false);
    entry.clear();

    entry.textcolour(HUD_CAPTION_COLOUR);
    entry.cprintf("Dex: ");

    entry.textcolour(_get_stat_colour(STAT_DEX));

    entry.cprintf("%2d", you.dex(false));

    cols.add_formatted(2, entry.to_colour_string(), false);
    entry.clear();

    entry.textcolour(HUD_CAPTION_COLOUR);
    entry.cprintf("XL:     ");

    entry.textcolour(HUD_VALUE_COLOUR);
    entry.cprintf("%d", you.experience_level);

    if (you.experience_level < you.get_max_xl())
    {
        entry.textcolour(HUD_CAPTION_COLOUR);
        entry.cprintf("   Next: ");

        entry.textcolour(HUD_VALUE_COLOUR);
        entry.cprintf("%d%%", get_exp_progress());
    }

    cols.add_formatted(3, entry.to_colour_string(), false);
    entry.clear();

    entry.textcolour(HUD_CAPTION_COLOUR);
    entry.cprintf("God:    ");

    entry.textcolour(HUD_VALUE_COLOUR);

    string godpowers = _god_powers();
#ifdef WIZARD
    if (you.wizard)
        godpowers = _wiz_god_powers();
#endif
    entry += formatted_string::parse_string(godpowers);

    cols.add_formatted(3, entry.to_colour_string(), false);
    entry.clear();

    if (!you.has_mutation(MUT_INNATE_CASTER))
    {
        entry.textcolour(HUD_CAPTION_COLOUR);
        entry.cprintf("Spells: ");

        entry.textcolour(HUD_VALUE_COLOUR);
        entry.cprintf("%d/%d levels left",
                      player_spell_levels(), player_total_spell_levels());

        cols.add_formatted(3, entry.to_colour_string(), false);
        entry.clear();
    }

    if (you.has_mutation(MUT_MULTILIVED))
    {
        entry.textcolour(HUD_CAPTION_COLOUR);
        entry.cprintf("Lives:  ");

        entry.textcolour(HUD_VALUE_COLOUR);
        entry.cprintf("%d", you.lives);

        entry.textcolour(HUD_CAPTION_COLOUR);
        entry.cprintf("   Deaths: ");

        entry.textcolour(HUD_VALUE_COLOUR);
        entry.cprintf("%d", you.deaths);

        cols.add_formatted(3, entry.to_colour_string(), false);
        entry.clear();
    }

    return cols.formatted_lines();
}

// generator of resistance strings:
// params :
//      name : name of the resist, correct spacing is handled here
//      spacing : width of the name column
//      value : actual value of the resistance (can be negative)
//      max : maximum value of the resistance (for colour AND representation),
//          default is the most common case (1)
//      immune : overwrites normal pip display for full immunity
static string _resist_composer(const char * name, int spacing, int value,
                               int max = 1, mon_resist_flags type = MR_NO_FLAGS)
{
    string out;

    const bool immune = ((type == MR_RES_POISON && value == 3)
                         || type == MR_NO_FLAGS && value == WILL_INVULN);

    string colour = _determine_colour_string(value, max, immune);
    out += chop_string(name, spacing);
    out += desc_resist(value, max, immune);

    if ((!Options.show_resist_percent) || type == MR_NO_FLAGS)
        return make_stringf("%s%s", colour.c_str(), out.c_str());

    int res_percent = -1;

    const static int _basic_res[] = {150, 100, 50, 33, 20};
    const static int _neg_res[]   = {-1, 100, 50, 20, 0};
    const static int _pois_res[]  = {150, 100, 33, 33, 0};
    const static int _corr_res[]  = {-1, 100, 50, -1, -1};

    if (value < -1)
        value = -1;
    ASSERT(value <= 3);
    switch (type)
    {
        case MR_RES_FIRE:
        case MR_RES_COLD:
            res_percent = _basic_res[value + 1];
            break;

        case MR_RES_NEG:
            res_percent = _neg_res[value + 1];
            break;

        case MR_RES_POISON:
        case MR_RES_ELEC:
            res_percent = _pois_res[value + 1];
            break;

        case MR_RES_CORR:
            res_percent = _corr_res[value + 1];
            break;

        default:
            return out;
    }

    string num_colour = colour.substr(1, colour.length() - 2);
    if (num_colour == "lightgrey")
        num_colour = "darkgrey";

    string num_str = make_stringf("(%d%%)", res_percent);
    int padding = std::max(0, (int)(15 - strwidth(out)));

    out = make_stringf("%s%s%s<%s>%s</%s>", colour.c_str(), out.c_str(),
                        string(padding, ' ').c_str(),
                        num_colour.c_str(),
                        num_str.c_str(),
                        num_colour.c_str());

    return out;
}

static vector<formatted_string> _get_overview_resistances(
    vector<char> &equip_chars, int sw)
{
    // Two columns.
    column_composer cols(2, Options.show_resist_percent ? 25 : 22);

    // First column, resist name is up to 8 chars
    int cwidth = 8;
    string out;

    const int rfire = player_res_fire(false);
    out += _resist_composer("rFire", cwidth, rfire, 3, MR_RES_FIRE) + "\n";

    const int rcold = player_res_cold(false);
    out += _resist_composer("rCold", cwidth, rcold, 3, MR_RES_COLD) + "\n";

    const int rlife = player_prot_life(false);
    out += _resist_composer("rNeg", cwidth, rlife, 3, MR_RES_NEG) + "\n";

    const int rpois = player_res_poison(false);
    out += _resist_composer("rPois", cwidth, rpois, 1, MR_RES_POISON) + "\n";

    const int relec = player_res_electricity(false);
    out += _resist_composer("rElec", cwidth, relec, 1, MR_RES_ELEC) + "\n";

    const int rcorr = player_res_corrosion(false);
    out += _resist_composer("rCorr", cwidth, rcorr, 1, MR_RES_CORR) + "\n";

    const int sinv = you.can_see_invisible();
    out += _resist_composer("SInv", cwidth, sinv) + "\n";

    const int rmagi = player_willpower() / WL_PIP;
    out += _resist_composer("Will", cwidth, rmagi, MAX_WILL_PIPS) + "\n";

    out += _stealth_bar(cwidth, 20) + "\n";

    const int regen = player_regen(); // round up
    out += chop_string("HPRegen", cwidth);
    out += make_stringf("%d.%02d/turn\n", regen/100, regen%100);

    if (!you.has_mutation(MUT_HP_CASTING))
    {
        out += chop_string("MPRegen", cwidth);
#if TAG_MAJOR_VERSION == 34
        const bool etheric = you.unrand_equipped(UNRAND_ETHERIC_CAGE);
        const int mp_regen = player_mp_regen() //round up
                            + (etheric ? 50 : 0); // on average
        out += make_stringf("%d.%02d/turn%s\n", mp_regen / 100, mp_regen % 100,
                            etheric ? "*" : "");
#else
        const int mp_regen = player_mp_regen(); // round up
        out += make_stringf("%d.%02d/turn\n", mp_regen / 100, mp_regen % 100);
#endif
    }

    cols.add_formatted(0, out, false);

    // Second column.
    _print_overview_screen_equip(cols, equip_chars, sw);

    return cols.formatted_lines();
}

class overview_popup : public formatted_scroller
{
public:
    overview_popup() {};
    vector<char> equip_chars;
private:
    maybe_bool process_key(int ch) override
    {
        if (find(equip_chars.begin(), equip_chars.end(), ch) != equip_chars.end())
        {
            item_def& item = you.inv[letter_to_index(ch)];
            return describe_item(item);
        }
        return formatted_scroller::process_key(ch);
    };
};

void print_overview_screen()
{
    // TODO: this should handle window resizes
    constexpr int num_cols = 80;
    overview_popup overview;

    overview.set_more();
    overview.set_tag("resists");

    overview.add_text(_overview_screen_title(num_cols));

    for (const formatted_string &bline : _get_overview_stats())
        overview.add_formatted_string(bline, true);
    overview.add_text("\n");

    {
        vector<formatted_string> blines =
            _get_overview_resistances(overview.equip_chars, num_cols);

        for (unsigned int i = 0; i < blines.size(); ++i)
            overview.add_text(blines[i].to_colour_string() + "\n");
    }

    overview.add_text("\n");
    overview.add_text(_status_mut_rune_list(num_cols));
    overview.show();
}

string dump_overview_screen()
{
    string text = formatted_string::parse_string(_overview_screen_title(80));
    text += "\n";

    for (const formatted_string &bline : _get_overview_stats())
    {
        text += bline;
        text += "\n";
    }
    text += "\n";

    vector<char> equip_chars;
    for (const formatted_string &bline
            : _get_overview_resistances(equip_chars, 640))
    {
        text += bline;
        text += "\n";
    }
    text += "\n";

    text += formatted_string::parse_string(_status_mut_rune_list(80));

    string ability_list = formatted_string::parse_string(print_abilities());
    linebreak_string(ability_list, 80);
    text += ability_list;

    text += "\n";

    return text;
}

static string _rampage_passive_string()
{
    string desc = "";
    const int rampage = you.rampaging();
    if (rampage)
    {
        desc += you.has_mutation(MUT_ROLLPAGE) ? "roll" : "rampage";

        const bool infinite = you.unrand_equipped(UNRAND_SEVEN_LEAGUE_BOOTS);
        const char *inf = Options.char_set == CSET_ASCII ? "+inf"
                                                         : "+\u221e"; //"∞"

        desc += infinite    ? inf :
                rampage > 1 ? make_stringf("+%d", rampage) : "";
    }
    return desc;
}

static string _extra_passive_effects()
{
    vector<string> passives;

    // Fo don't need a reminder that they can't teleport
    if (!you.stasis() && you.no_tele(false, false))
        passives.emplace_back("no teleportation");

    if (you.no_cast())
        passives.emplace_back("no spellcasting");

    if (you.inaccuracy())
    {
        passives.emplace_back(
            make_stringf("inaccuracy (-%d)", you.inaccuracy_penalty()).c_str());
    }

    const int anger = you.angry();
    if (anger && !you.stasis() && !you.clarity() && !you.is_lifeless_undead())
    {
        passives.emplace_back(
            make_stringf("random rage (%d%%)", anger).c_str());
    }

    const int corrode = you.scan_artefacts(ARTP_CORRODE);
    if (corrode)
    {
        passives.emplace_back(
            make_stringf("corrode self (%d%%)",
                         corrosion_chance(corrode)).c_str());
    }

    const int slow = you.scan_artefacts(ARTP_SLOW);
    if (you.scan_artefacts(ARTP_SLOW))
    {
        passives.emplace_back(
            make_stringf("slow self (%d%%)", slow).c_str());
    }

    const int harm = you.extra_harm();
    if (harm)
    {
        passives.emplace_back(
            make_stringf("harm (+%d%% outgoing, +%d%% incoming)",
                         outgoing_harm_amount(harm),
                         incoming_harm_amount(harm)).c_str());
    }

    if (you.wearing_ego(OBJ_ARMOUR, SPARM_MAYHEM))
        passives.emplace_back("mayhem");

    if (you.missile_repulsion())
        passives.emplace_back("repel missiles");

    if (you.reflection())
        passives.emplace_back("reflection");

    if (player_acrobatic())
        passives.emplace_back("acrobat");

    if (you.clarity())
        passives.emplace_back("clarity");

    if (you.rmut_from_item()
        || you.get_mutation_level(MUT_MUTATION_RESISTANCE) == 3)
    {
        passives.emplace_back("resist mutation");
    }

    if (you.spirit_shield())
        passives.emplace_back("guardian spirit");

    if (you.rampaging())
        passives.emplace_back(_rampage_passive_string().c_str());

    if (you.faith())
        passives.emplace_back("faith");

    const int wizardry = player_wizardry();
    if (wizardry)
        passives.emplace_back(make_stringf("wizardry (x%d)", wizardry));

    if (you.archmagi())
        passives.emplace_back("archmagi");

    if (you.wearing_ego(OBJ_ARMOUR, SPARM_ENERGY))
    {
        const int channel = player_channelling_chance();
        if (channel)
        {
            passives.emplace_back(
                make_stringf("channel magic (%d%%)", channel).c_str());
        }
    }

    if (you.infusion_amount())
    {
        passives.emplace_back(
            make_stringf("infuse magic (%d %s)",
                         you.infusion_amount(),
                         you.has_mutation(MUT_HP_CASTING) ? "HP"
                                                          : "MP").c_str());
    }

    if (passives.empty())
        return "no passive effects";
    else
        return comma_separated_line(passives.begin(), passives.end(),
                                    ", ", ", ");

}

/// Creates rows of short descriptions for current status effects, mutations,
/// and runes/Orbs of Zot.
static string _status_mut_rune_list(int sw)
{
    // print passive information
    string text = "<w>%:</w> ";
    text += _extra_passive_effects();

    // print status information
    text += "\n<w>@:</w> ";
    vector<string> status;

    status_info inf;
    for (unsigned i = 0; i <= STATUS_LAST_STATUS; ++i)
    {
        if (fill_status_info(i, inf) && !inf.short_text.empty())
            status.emplace_back(inf.short_text);
    }

    int move_cost = (player_speed() * player_movement_speed()) / 10;
    if (move_cost != 10)
    {
        const char *help = (move_cost <   8) ? "very quick" :
                           (move_cost <  10) ? "quick" :
                           (move_cost <  13) ? "slow"
                                             : "very slow";
        status.emplace_back(help);
    }

    if (status.empty())
        text += "no status effects";
    else
        text += comma_separated_line(status.begin(), status.end(), ", ", ", ");
    text += "\n";

    // print mutation information
    text += "<w>A:</w> ";

    text += terse_mutation_list();

    // print the Orb
    if (player_has_orb())
        text += "\n<w>0:</w> Orb of Zot";

    // print runes
    vector<string> runes;
    for (int i = 0; i < NUM_RUNE_TYPES; i++)
        if (you.runes[i])
            runes.emplace_back(rune_type_name(i));
    if (!runes.empty())
    {
        text += make_stringf("\n<w>%s:</w> %d/%d rune%s: %s",
                    command_to_string(CMD_DISPLAY_RUNES).c_str(),
                    (int)runes.size(), you.obtainable_runes,
                    you.obtainable_runes == 1 ? "" : "s",
                    comma_separated_line(runes.begin(), runes.end(),
                                         ", ", ", ").c_str());
    }

    linebreak_string(text, sw);

    return text;
}