File: tags.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 (8740 lines) | stat: -rw-r--r-- 279,002 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
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
/**
 * @file
 * @brief Auxiliary functions to make savefile versioning simpler.
**/

/*
   The marshalling and unmarshalling of data is done in big endian and
   is meant to keep savefiles cross-platform. Note also that the marshalling
   sizes are 1, 2, and 4 for byte, short, and int. If a strange platform
   with different sizes of these basic types pops up, please sed it to fixed-
   width ones. For now, that wasn't done in order to keep things convenient.
*/

#include "AppHdr.h"

#include "feature.h"
#include "mpr.h"
#include "tags.h"

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <vector>
#ifdef UNIX
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif

#include "abyss.h"
#include "act-iter.h"
#include "artefact.h"
#include "art-enum.h"
#include "branch.h"
#include "chardump.h"
#include "colour.h"
#include "coordit.h"
#if TAG_MAJOR_VERSION == 34
 #include "decks.h"
#endif
#include "dbg-scan.h"
#include "dbg-util.h"
#include "describe.h"
#include "dgn-overview.h"
#include "dungeon.h"
#include "end.h"
#include "tile-env.h"
#include "errors.h"
#include "ghost.h"
#include "god-abil.h" // just for the Ru sac penalty key
#include "god-passive.h"
#include "god-companions.h"
#include "invent.h"
#include "item-name.h"
#include "item-prop.h"
#include "item-status-flag-type.h"
#include "item-type-id-state-type.h"
#include "items.h"
#include "jobs.h"
#include "mapmark.h"
#include "misc.h"
#include "mon-death.h"
#include "mon-ench.h"
#if TAG_MAJOR_VERSION == 34
 #include "mon-place.h"
 #include "mon-poly.h"
 #include "mon-tentacle.h"
 #include "mon-util.h"
#endif
#include "mutation.h"
#include "place.h"
#include "player-equip.h"
#include "player-stats.h"
#include "player-save-info.h"
#include "prompt.h" // index_to_letter
#include "religion.h"
#include "skills.h"
#include "species.h"
#include "spl-damage.h" // vortex_power_key
#include "state.h"
#include "stringutil.h"
#include "syscalls.h"
#include "tag-version.h"
#include "terrain.h"
#include "rltiles/tiledef-dngn.h"
#include "rltiles/tiledef-player.h"
#include "tilepick.h"
#include "tileview.h"
#ifdef USE_TILE
 #include "tilemcache.h"
#endif
#include "transform.h"
#include "unwind.h"
#include "version.h"

vector<ghost_demon> global_ghosts; // only for reading/writing

#if TAG_MAJOR_VERSION == 34
#define ORIGINAL_BRAND_KEY "orig brand"
#endif

// defined in dgn-overview.cc
extern map<branch_type, set<level_id> > stair_level;
extern map<level_pos, shop_type> shops_present;
extern map<level_pos, god_type> altars_present;
extern map<level_pos, branch_type> portals_present;
extern map<level_pos, string> portal_notes;
extern map<level_id, string> level_annotations;
extern map<level_id, string> level_exclusions;
extern map<level_id, string> level_uniques;
extern set<pair<string, level_id> > auto_unique_annotations;

// defined in abyss.cc
extern abyss_state abyssal_state;

reader::reader(const string &_read_filename, int minorVersion)
    : _filename(_read_filename), _chunk(0), _pbuf(nullptr), _read_offset(0),
      _minorVersion(minorVersion), _safe_read(false)
{
    _file       = fopen_u(_filename.c_str(), "rb");
    opened_file = !!_file;
}

reader::reader(package *save, const string &chunkname, int minorVersion)
    : _file(0), _chunk(0), opened_file(false), _pbuf(0), _read_offset(0),
     _minorVersion(minorVersion), _safe_read(false)
{
    ASSERT(save);
    _chunk = new chunk_reader(save, chunkname);
}

reader::~reader()
{
    if (_chunk)
        delete _chunk;
    close();
}

void reader::close()
{
    if (opened_file && _file)
        fclose(_file);
    _file = nullptr;
}

void reader::advance(size_t offset)
{
    char junk[128];

    while (offset)
    {
        const size_t junklen = min(sizeof(junk), offset);
        offset -= junklen;
        read(junk, junklen);
    }
}

bool reader::valid() const
{
    return (_file && !feof(_file)) ||
           (_pbuf && _read_offset < _pbuf->size());
}

static NORETURN void _short_read(bool safe_read)
{
    if (!crawl_state.need_save || safe_read)
        throw short_read_exception();
    // Would be nice to name the save chunk here, but in interesting cases
    // we're reading a copy from memory (why?).
    die_noline("short read while reading save");
}

// Reads input in network byte order, from a file or buffer.
unsigned char reader::readByte()
{
    if (_file)
    {
        int b = fgetc(_file);
        if (b == EOF)
            _short_read(_safe_read);
        return b;
    }
    else if (_chunk)
    {
        unsigned char buf;
        if (_chunk->read(&buf, 1) != 1)
            _short_read(_safe_read);
        return buf;
    }
    else
    {
        if (_read_offset >= _pbuf->size())
            _short_read(_safe_read);
        return (*_pbuf)[_read_offset++];
    }
}

void reader::read(void *data, size_t size)
{
    if (_file)
    {
        if (data)
        {
            if (fread(data, 1, size, _file) != size)
                _short_read(_safe_read);
        }
        else
            fseek(_file, (long)size, SEEK_CUR);
    }
    else if (_chunk)
    {
        if (_chunk->read(data, size) != size)
            _short_read(_safe_read);
    }
    else
    {
        if (_read_offset+size > _pbuf->size())
            _short_read(_safe_read);
        if (data && size)
            memcpy(data, &(*_pbuf)[_read_offset], size);

        _read_offset += size;
    }
}

int reader::getMinorVersion() const
{
    ASSERT(_minorVersion != TAG_MINOR_INVALID);
    return _minorVersion;
}

void reader::setMinorVersion(int minorVersion)
{
    _minorVersion = minorVersion;
}

void reader::fail_if_not_eof(const string &name)
{
    char dummy;
    if (_chunk ? _chunk->read(&dummy, 1) :
        _file ? (fgetc(_file) != EOF) :
        _read_offset >= _pbuf->size())
    {
        fail("Incomplete read of \"%s\" - aborting.", name.c_str());
    }
}

void writer::check_ok(bool ok)
{
    if (!ok && !failed)
    {
        failed = true;
        if (!_ignore_errors)
            end(1, true, "Error writing to %s", _filename.c_str());
    }
}

void writer::writeByte(unsigned char ch)
{
    if (failed)
        return;

    if (_chunk)
        _chunk->write(&ch, 1);
    else if (_file)
        check_ok(fputc(ch, _file) != EOF);
    else
        _pbuf->push_back(ch);
}

void writer::write(const void *data, size_t size)
{
    if (failed)
        return;

    if (_chunk)
        _chunk->write(data, size);
    else if (_file)
        check_ok(fwrite(data, 1, size, _file) == size);
    else
    {
        const unsigned char* cdata = static_cast<const unsigned char*>(data);
        _pbuf->insert(_pbuf->end(), cdata, cdata+size);
    }
}

long writer::tell()
{
    ASSERT(!_chunk);
    return _file? ftell(_file) : _pbuf->size();
}

#ifdef DEBUG_GLOBALS
// Force a conditional jump valgrind may pick up, no matter the optimizations.
static volatile uint32_t hashroll;
static void CHECK_INITIALIZED(uint32_t x)
{
    hashroll = 0;
    if ((hashroll += x) & 1)
        hashroll += 2;
}
#else
#define CHECK_INITIALIZED(x)
#endif

// static helper declarations
static void _tag_construct_char(writer &th);
static void _tag_construct_you(writer &th);
static void _tag_construct_you_items(writer &th);
static void _tag_construct_you_dungeon(writer &th);
static void _tag_construct_lost_monsters(writer &th);
static void _tag_construct_companions(writer &th);
static void _tag_read_you(reader &th);
static void _tag_read_you_items(reader &th);
static void _tag_read_you_dungeon(reader &th);
static void _tag_read_lost_monsters(reader &th);
#if TAG_MAJOR_VERSION == 34
static void _tag_read_lost_items(reader &th);
#endif
static void _tag_read_companions(reader &th);

static void _tag_construct_level(writer &th);
static void _tag_construct_level_items(writer &th);
static void _tag_construct_level_monsters(writer &th);
static void _tag_construct_level_tiles(writer &th);
static void _tag_read_level(reader &th);
static void _tag_read_level_items(reader &th);
static void _tag_read_level_monsters(reader &th);
static void _tag_read_level_tiles(reader &th);
static void _regenerate_tile_flavour();
static void _draw_tiles();

static void _tag_construct_ghost(writer &th, vector<ghost_demon> &);
static vector<ghost_demon> _tag_read_ghost(reader &th);

static void _marshallGhost(writer &th, const ghost_demon &ghost);
static ghost_demon _unmarshallGhost(reader &th);

static void _marshallSpells(writer &, const monster_spells &);

static void _marshallMonsterInfo (writer &, const monster_info &);
static void _unmarshallMonsterInfo (reader &, monster_info &mi);

template<typename T, typename T_iter, typename T_marshal>
static void _marshall_iterator(writer &th, T_iter beg, T_iter end,
                               T_marshal marshal);
template<typename T, typename U>
static void _unmarshall_vector(reader& th, vector<T>& vec, U T_unmarshall);

template<int SIZE>
static void _marshallFixedBitVector(writer& th, const FixedBitVector<SIZE>& arr);
template<int SIZE>
static void _unmarshallFixedBitVector(reader& th, FixedBitVector<SIZE>& arr);

void marshallByte(writer &th, int8_t data)
{
    CHECK_INITIALIZED(data);
    th.writeByte(data);
}

int8_t unmarshallByte(reader &th)
{
    return th.readByte();
}

void marshallUByte(writer &th, uint8_t data)
{
    CHECK_INITIALIZED(data);
    th.writeByte(data);
}

uint8_t unmarshallUByte(reader &th)
{
    return th.readByte();
}

// Marshall 2 byte short in network order.
void marshallShort(writer &th, short data)
{
    // TODO: why does this use `short` and `char` when unmarshall uses int16_t??
    CHECK_INITIALIZED(data);
    const char b2 = (char)(data & 0x00FF);
    const char b1 = (char)((data & 0xFF00) >> 8);
    th.writeByte(b1);
    th.writeByte(b2);
}

// Unmarshall 2 byte short in network order.
int16_t unmarshallShort(reader &th)
{
    int16_t b1 = th.readByte();
    int16_t b2 = th.readByte();
    int16_t data = (b1 << 8) | (b2 & 0x00FF);
    return data;
}

// Marshall 4 byte int in network order.
void marshallInt(writer &th, int32_t data)
{
    CHECK_INITIALIZED(data);
    char b4 = (char) (data & 0x000000FF);
    char b3 = (char)((data & 0x0000FF00) >> 8);
    char b2 = (char)((data & 0x00FF0000) >> 16);
    char b1 = (char)((data & 0xFF000000) >> 24);

    th.writeByte(b1);
    th.writeByte(b2);
    th.writeByte(b3);
    th.writeByte(b4);
}

// Useful for using marshallMap with ints.
static void marshallIntReference(writer &th, const int32_t &data)
{
    marshallInt(th, data);
}

// Unmarshall 4 byte signed int in network order.
int32_t unmarshallInt(reader &th)
{
    int32_t b1 = th.readByte();
    int32_t b2 = th.readByte();
    int32_t b3 = th.readByte();
    int32_t b4 = th.readByte();

    int32_t data = (b1 << 24) | ((b2 & 0x000000FF) << 16);
    data |= ((b3 & 0x000000FF) << 8) | (b4 & 0x000000FF);
    return data;
}

void marshallUnsigned(writer& th, uint64_t v)
{
    do
    {
        unsigned char b = (unsigned char)(v & 0x7f);
        v >>= 7;
        if (v)
            b |= 0x80;
        th.writeByte(b);
    }
    while (v);
}

uint64_t unmarshallUnsigned(reader& th)
{
    unsigned i = 0;
    uint64_t v = 0;
    for (;;)
    {
        unsigned char b = th.readByte();
        v |= (uint64_t)(b & 0x7f) << i;
        i += 7;
        if (!(b & 0x80))
            break;
    }
    return v;
}

void marshallSigned(writer& th, int64_t v)
{
    if (v < 0)
        marshallUnsigned(th, (uint64_t)((-v - 1) << 1) | 1);
    else
        marshallUnsigned(th, (uint64_t)(v << 1));
}

int64_t unmarshallSigned(reader& th)
{
    uint64_t u;
    unmarshallUnsigned(th, u);
    if (u & 1)
        return (int64_t)(-(u >> 1) - 1);
    else
        return (int64_t)(u >> 1);
}

// Optimized for short vectors that have only the first few bits set, and
// can have invalid length. For long ones you might want to do this
// differently to not lose 1/8 bits and speed.
template<int SIZE>
void _marshallFixedBitVector(writer& th, const FixedBitVector<SIZE>& arr)
{
    int last_bit;
    for (last_bit = SIZE - 1; last_bit > 0; last_bit--)
        if (arr[last_bit])
            break;

    int i = 0;
    while (1)
    {
        uint8_t byte = 0;
        for (int j = 0; j < 7; j++)
            if (i < SIZE && arr[i++])
                byte |= 1 << j;
        if (i <= last_bit)
            marshallUByte(th, byte);
        else
        {
            marshallUByte(th, byte | 0x80);
            break;
        }
    }
}

template<int SIZE>
void _unmarshallFixedBitVector(reader& th, FixedBitVector<SIZE>& arr)
{
    arr.reset();

    int i = 0;
    while (1)
    {
        uint8_t byte = unmarshallUByte(th);
        for (int j = 0; j < 7; j++)
            if (i < SIZE)
                arr.set(i++, !!(byte & (1 << j)));
        if (byte & 0x80)
            break;
    }
}

// FIXME: Kill this abomination - it will break!
template<typename T>
static void _marshall_as_int(writer& th, const T& t)
{
    marshallInt(th, static_cast<int>(t));
}

template <typename data>
void marshallSet(writer &th, const set<data> &s,
                 void (*marshall)(writer &, const data &))
{
    marshallInt(th, s.size());
    for (const data &elt : s)
        marshall(th, elt);
}

template<typename key, typename value>
void marshallMap(writer &th, const map<key,value>& data,
                 void (*key_marshall)(writer&, const key&),
                 void (*value_marshall)(writer&, const value&))
{
    marshallInt(th, data.size());
    for (const auto &entry : data)
    {
        key_marshall(th, entry.first);
        value_marshall(th, entry.second);
    }
}

template<typename T_iter, typename T_marshall_t>
static void _marshall_iterator(writer &th, T_iter beg, T_iter end,
                               T_marshall_t T_marshall)
{
    marshallInt(th, distance(beg, end));
    while (beg != end)
    {
        T_marshall(th, *beg);
        ++beg;
    }
}

template<typename T, typename U>
static void _unmarshall_vector(reader& th, vector<T>& vec, U T_unmarshall)
{
    vec.clear();
    const int num_to_read = unmarshallInt(th);
    for (int i = 0; i < num_to_read; ++i)
        vec.push_back(T_unmarshall(th));
}

template <typename T_container, typename T_inserter, typename T_unmarshall>
static void unmarshall_container(reader &th, T_container &container,
                                 T_inserter inserter, T_unmarshall unmarshal)
{
    container.clear();
    const int num_to_read = unmarshallInt(th);
    for (int i = 0; i < num_to_read; ++i)
        (container.*inserter)(unmarshal(th));
}

static unsigned short _pack(const level_id& id)
{
    return (static_cast<int>(id.branch) << 8) | (id.depth & 0xFF);
}

void marshall_level_id(writer& th, const level_id& id)
{
    marshallShort(th, _pack(id));
}

static void _marshall_level_id_set(writer& th, const set<level_id>& id)
{
    marshallSet(th, id, marshall_level_id);
}

// XXX: Redundant with level_pos.save()/load().
static void _marshall_level_pos(writer& th, const level_pos& lpos)
{
    marshallInt(th, lpos.pos.x);
    marshallInt(th, lpos.pos.y);
    marshall_level_id(th, lpos.id);
}

template <typename data, typename set>
void unmarshallSet(reader &th, set &dset,
                   data (*data_unmarshall)(reader &))
{
    dset.clear();
    int len = unmarshallInt(th);
    for (int i = 0; i < len; ++i)
        dset.insert(data_unmarshall(th));
}

template<typename key, typename value, typename map>
void unmarshallMap(reader& th, map& data,
                   key   (*key_unmarshall)  (reader&),
                   value (*value_unmarshall)(reader&))
{
    const int len = unmarshallInt(th);
    key k;
    for (int i = 0; i < len; ++i)
    {
        k = key_unmarshall(th);
        pair<key, value> p(k, value_unmarshall(th));
        data.insert(p);
    }
}

template<typename T>
static T unmarshall_int_as(reader& th)
{
    return static_cast<T>(unmarshallInt(th));
}

#if TAG_MAJOR_VERSION == 34
level_id level_id::from_packed_place(unsigned short place)
#else
static level_id _unpack(unsigned short place)
#endif
{
    level_id id;

    id.branch     = static_cast<branch_type>((place >> 8) & 0xFF);
    id.depth      = (int8_t)(place & 0xFF);

    return id;
}

level_id unmarshall_level_id(reader& th)
{
#if TAG_MAJOR_VERSION == 34
    return level_id::from_packed_place(unmarshallShort(th));
#else
    return _unpack(unmarshallShort(th));
#endif
}

static set<level_id> _unmarshall_level_id_set(reader& th)
{
    set<level_id> id;
    unmarshallSet(th, id, unmarshall_level_id);
    return id;
}

static level_pos _unmarshall_level_pos(reader& th)
{
    level_pos lpos;
    lpos.pos.x = unmarshallInt(th);
    lpos.pos.y = unmarshallInt(th);
    lpos.id    = unmarshall_level_id(th);
    return lpos;
}

void marshallCoord(writer &th, const coord_def &c)
{
    marshallInt(th, c.x);
    marshallInt(th, c.y);
}

coord_def unmarshallCoord(reader &th)
{
    coord_def c;
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_COORD_SERIALIZER
        && th.getMinorVersion() != TAG_MINOR_0_11)
    {
#endif
        c.x = unmarshallInt(th);
        c.y = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    }
    else
    {
        c.x = unmarshallShort(th);
        c.y = unmarshallShort(th);
    }
#endif
    return c;
}

#if TAG_MAJOR_VERSION == 34
static species_type final_species_cleanup = NUM_SPECIES;

// Between TAG_MINOR_OPTIONAL_PARTS and TAG_MINOR_FIXED_CONSTRICTION
// we neglected to marshall the constricting[] map of monsters. Fix
// those up.
static void _fix_missing_constrictions()
{
    for (int i = -1; i < MAX_MONSTERS; ++i)
    {
        const actor* m = i < 0 ? (actor*)&you : (actor*)&env.mons[i];
        if (!m->alive())
            continue;
        if (!m->constricted_by)
            continue;
        actor *h = actor_by_mid(m->constricted_by);
        // Not a known bug, so don't fix this up.
        if (!h)
            continue;

        if (!h->constricting)
            h->constricting = new vector<mid_t>;
        if (!h->is_constricting(*m))
        {
            dprf("Fixing missing constriction for %s (mindex=%d mid=%d)"
                 " of %s (mindex=%d mid=%d)",
                 h->name(DESC_PLAIN, true).c_str(), h->mindex(), h->mid,
                 m->name(DESC_PLAIN, true).c_str(), m->mindex(), m->mid);

            h->constricting->push_back(m->mid);
        }
    }
}
#endif

static void _marshall_constriction(writer &th, const actor *who)
{
    marshallInt(th, who->constricted_by);
    marshallByte(th, who->constricted_type);
    marshallInt(th, who->escape_attempts);

    // Assumes an empty vector is marshalled as just the int 0.
    const vector<mid_t> * const cvec = who->constricting;
    if (cvec)
        _marshall_iterator(th, cvec->begin(), cvec->end(), marshallInt);
    else
        marshallInt(th, 0);
}

static void _unmarshall_constriction(reader &th, actor *who)
{
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_NO_ACTOR_HELD)
        unmarshallInt(th);
#endif
    who->constricted_by = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_CONSTRICTED_TYPE)
    {
        // Deduce constriction type from enchants.
        if (monster* mon = who->as_monster())
        {
            if (mon->has_ench(ENCH_VILE_CLUTCH_OLD))
            {
                who->constricted_type = CONSTRICT_BVC;

                // Convert duration over to the new standardised constriction ench
                mon_enchant ench = mon->get_ench(ENCH_VILE_CLUTCH_OLD);
                ench.ench = ENCH_CONSTRICTED;
                mon->add_ench(ench);
                mon->del_ench(ENCH_VILE_CLUTCH_OLD);
            }
            // Used to be ENCH_GRASPING_ROOTS before this minor version
            else if (mon->has_ench(ENCH_CONSTRICTED))
                who->constricted_type = CONSTRICT_ROOTS;
            else
            {
                who->constricted_type = (who->constricted_by ? CONSTRICT_MELEE
                                                             : CONSTRICT_NONE);
            }
        }
        else
        {
            if (you.duration[DUR_VILE_CLUTCH_OLD])
            {
                who->constricted_type = CONSTRICT_BVC;
                you.duration[DUR_CONSTRICTED] = you.duration[DUR_VILE_CLUTCH_OLD];
                you.duration[DUR_VILE_CLUTCH_OLD] = 0;
            }
            // Used to be DUR_GRASPING_ROOTS before this minor version
            else if (you.duration[DUR_CONSTRICTED])
                who->constricted_type = CONSTRICT_ROOTS;
            else
            {
                who->constricted_type = (who->constricted_by ? CONSTRICT_MELEE
                                                             : CONSTRICT_NONE);
            }
        }
    }
    else
#endif
        who->constricted_type = static_cast<constrict_type>(unmarshallByte(th));
    who->escape_attempts = unmarshallInt(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_NO_CONSTRICTION_DUR)
    {
        map<mid_t, int> cmap;
        unmarshallMap(th, cmap, unmarshall_int_as<mid_t>, unmarshallInt);

        if (cmap.size() == 0)
            who->constricting = 0;
        else
        {
            vector<mid_t> cvec;
            for (const auto &entry : cmap)
                cvec.push_back(entry.first);

            if (!cvec.empty())
                who->constricting = new vector<mid_t>(cvec);
        }
    }
    else
#endif
    {
        vector<mid_t> cvec;
        unsigned int count = unmarshallInt(th);
        for (unsigned int i = 0; i < count; ++i)
            cvec.push_back(unmarshallInt(th));

        if (!cvec.empty())
            who->constricting = new vector<mid_t>(cvec);
    }
}

template <typename marshall, typename grid>
static void _run_length_encode(writer &th, marshall m, const grid &g,
                               int width, int height)
{
    int last = 0, nlast = 0;
    for (int y = 0; y < height; ++y)
        for (int x = 0; x < width; ++x)
        {
            if (!nlast)
                last = g[x][y];
            if (last == g[x][y] && nlast < 255)
            {
                nlast++;
                continue;
            }

            marshallByte(th, nlast);
            m(th, last);

            last = g[x][y];
            nlast = 1;
        }

    marshallByte(th, nlast);
    m(th, last);
}

template <typename unmarshall, typename grid>
static void _run_length_decode(reader &th, unmarshall um, grid &g,
                               int width, int height)
{
    const int end = width * height;
    int offset = 0;
    while (offset < end)
    {
        const int run = unmarshallUByte(th);
        const int value = um(th);

        for (int i = 0; i < run; ++i)
        {
            const int y = offset / width;
            const int x = offset % width;
            g[x][y] = value;
            ++offset;
        }
    }
}

union float_marshall_kludge
{
    float    f_num;
    int32_t  l_num;
};
COMPILE_CHECK(sizeof(float) == sizeof(int32_t));

// single precision float -- marshall in network order.
void marshallFloat(writer &th, float data)
{
    float_marshall_kludge k;
    k.f_num = data;
    marshallInt(th, k.l_num);
}

// Useful for using marshallMap with floats.
static void marshallFloatReference(writer &th, const float &data)
{
    marshallFloat(th, data);
}

// single precision float -- unmarshall in network order.
float unmarshallFloat(reader &th)
{
    float_marshall_kludge k;
    k.l_num = unmarshallInt(th);
    return k.f_num;
}

// string -- 2 byte length, string data
void marshallString(writer &th, const string &data)
{
    size_t len = data.length();
    // A limit of 32K. TODO: why doesn't this use int16_t?
    if (len > SHRT_MAX)
        die("trying to marshall too long a string (len=%ld)", (long int)len);
    marshallShort(th, len);

    th.write(data.c_str(), len);
}

string unmarshallString(reader &th)
{
    char buffer[SHRT_MAX]; // TODO: why doesn't this use int16_t?

    short len = unmarshallShort(th);
    ASSERT(len >= 0);
    ASSERT(len <= (ssize_t)sizeof(buffer));

    th.read(buffer, len);

    return string(buffer, len);
}

// This one must stay with a 16 bit signed big-endian length tag, to allow
// older versions to browse and list newer saves.
static void marshallString2(writer &th, const string &data)
{
    marshallString(th, data);
}

static string unmarshallString2(reader &th)
{
    return unmarshallString(th);
}

// string -- 4 byte length, non-terminated string data.
void marshallString4(writer &th, const string &data)
{
    const size_t len = data.length();
    if (len > static_cast<size_t>(numeric_limits<int32_t>::max()))
        die("trying to marshall too long a string (len=%ld)", (long int) len);
    marshallInt(th, len);
    th.write(data.c_str(), len);
}

void unmarshallString4(reader &th, string& s)
{
    const int len = unmarshallInt(th);
    ASSERT(len >= 0);
    s.resize(len);
    if (len)
        th.read(&s.at(0), len);
}

// boolean (to avoid system-dependent bool implementations)
void marshallBoolean(writer &th, bool data)
{
    th.writeByte(data ? 1 : 0);
}

// boolean (to avoid system-dependent bool implementations)
bool unmarshallBoolean(reader &th)
{
    return th.readByte() != 0;
}

// Saving the date as a string so we're not reliant on a particular epoch.
string make_date_string(time_t in_date)
{
    if (in_date <= 0)
        return "";

    struct tm *date = TIME_FN(&in_date);

    return make_stringf(
              "%4d%02d%02d%02d%02d%02d%s",
              date->tm_year + 1900, date->tm_mon, date->tm_mday,
              date->tm_hour, date->tm_min, date->tm_sec,
              ((date->tm_isdst > 0) ? "D" : "S"));
}

static void marshallStringVector(writer &th, const vector<string> &vec)
{
    _marshall_iterator(th, vec.begin(), vec.end(), marshallString);
}

static vector<string> unmarshallStringVector(reader &th)
{
    vector<string> vec;
    _unmarshall_vector(th, vec, unmarshallString);
    return vec;
}

// This code looks totally busted but I don't really want to look further...
static monster_type _fixup_monster_type(reader &th, monster_type x)
{
#if TAG_MAJOR_VERSION == 34
    if (x >= MONS_NO_MONSTER)
        return x;

# define AXED(a) if (x > a) --x
    if (th.getMinorVersion() == TAG_MINOR_0_11)
    {
        AXED(MONS_KILLER_BEE); // killer bee larva
        AXED(MONS_SHADOW_IMP); // midge
        AXED(MONS_AGNES);      // Jozef
    }
#else
    UNUSED(th);
#endif

    return x;
}

static monster_type unmarshallMonType(reader &th)
{
    monster_type x;
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_MONSTER_TYPE_SIZE)
        x = static_cast<monster_type>(unmarshallShort(th));
    else
#endif
        x = static_cast<monster_type>(unmarshallUnsigned(th));

    return _fixup_monster_type(th, x);
}

// yay marshalling inconsistencies
static monster_type unmarshallMonType_Info(reader &th)
{
    monster_type x = static_cast<monster_type>(unmarshallUnsigned(th));
    return _fixup_monster_type(th, x);
}

static void marshallMonType(writer &th, monster_type mt)
{
    marshallUnsigned(th, mt);
}

static spell_type unmarshallSpellType(reader &th
#if TAG_MAJOR_VERSION == 34
                                      , bool mons = false
#endif
                                      )
{
    spell_type x = SPELL_NO_SPELL;
#if TAG_MAJOR_VERSION == 34
    if (!mons && th.getMinorVersion() < TAG_MINOR_SHORT_SPELL_TYPE)
        x = static_cast<spell_type>(unmarshallUByte(th));
    else
#endif
        x = static_cast<spell_type>(unmarshallShort(th));

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() == TAG_MINOR_0_11)
    {
        AXED(SPELL_DEBUGGING_RAY); // projected noise
        AXED(SPELL_HEAL_OTHER);    // summon greater holy
    }
#endif

    return x;
}

static dungeon_feature_type rewrite_feature(dungeon_feature_type x,
                                            int minor_version)
{
#if TAG_MAJOR_VERSION == 34
    if (minor_version == TAG_MINOR_0_11)
    {
        // turn old trees into...trees
        if (x == DNGN_OPEN_SEA)
            x = DNGN_TREE;
        else if (x >= DNGN_LAVA_SEA && x < 30)
            x = (dungeon_feature_type)(x - 1);
    }
    // turn mangroves into trees
    else if (minor_version < TAG_MINOR_MANGROVES && x == DNGN_OPEN_SEA)
        x = DNGN_TREE;
    else if (minor_version < TAG_MINOR_FIX_FEAT_SHIFT
             && x > DNGN_OPEN_SEA && x < DNGN_LAVA)
    {
        x = static_cast<dungeon_feature_type>(x - 1);
    }

    if (x >= DNGN_DRY_FOUNTAIN_BLUE && x <= DNGN_DRY_FOUNTAIN_BLOOD)
        x = DNGN_DRY_FOUNTAIN;

    if (x == DNGN_SEALED_DOOR && minor_version < TAG_MINOR_0_12)
        x = DNGN_CLOSED_DOOR;
    if (x == DNGN_BADLY_SEALED_DOOR)
        x = DNGN_SEALED_DOOR;
    if (x == DNGN_ESCAPE_HATCH_UP && player_in_branch(BRANCH_LABYRINTH))
        x = DNGN_EXIT_LABYRINTH;
    if (x == DNGN_DEEP_WATER && player_in_branch(BRANCH_SHOALS)
        && minor_version < TAG_MINOR_SHOALS_LITE)
    {
        x = DNGN_SHALLOW_WATER;
    }

    // ensure that killing TRJ opens the slime:$ vaults
    if (you.where_are_you == BRANCH_SLIME && you.depth == brdepth[BRANCH_SLIME]
        && minor_version < TAG_MINOR_SLIME_WALL_CLEAR
        && x == DNGN_STONE_WALL)
    {
        x = DNGN_CLEAR_STONE_WALL;
    }

    if (x == DNGN_ENTER_LABYRINTH)
        x = DNGN_ENTER_GAUNTLET;

    if (minor_version < TAG_MINOR_NEW_TREES && x == DNGN_TREE)
    {
        if (you.where_are_you == BRANCH_SWAMP)
            x = DNGN_MANGROVE;
        else if (you.where_are_you == BRANCH_ABYSS
                || you.where_are_you == BRANCH_PANDEMONIUM)
        {
            x = DNGN_DEMONIC_TREE;
        }
    }
    if (minor_version < TAG_MINOR_SPLIT_HELL_GATE && x == DNGN_ENTER_HELL
        && player_in_hell())
    {
        x = branches[you.where_are_you].exit_stairs;
    }
#else
    UNUSED(minor_version);
#endif

    return x;
}

dungeon_feature_type unmarshallFeatureType(reader &th)
{
    dungeon_feature_type x = static_cast<dungeon_feature_type>(unmarshallUByte(th));
    return rewrite_feature(x, th.getMinorVersion());
}

#if TAG_MAJOR_VERSION == 34
// yay marshalling inconsistencies
static dungeon_feature_type unmarshallFeatureType_Info(reader &th)
{
    dungeon_feature_type x = static_cast<dungeon_feature_type>(unmarshallUnsigned(th));
    x = rewrite_feature(x, th.getMinorVersion());

    // There was a period of time when this function (only this one, not
    // unmarshallFeatureType) lacked some of the conversions now done by
    // rewrite_feature. In case any saves were transferred through those
    // versions, replace bad features with DNGN_UNSEEN. Questionable, but
    // this is just map_knowledge so the impact should be low.
    return is_valid_feature_type(x) ? x : DNGN_UNSEEN;
}
#endif

#define CANARY     marshallUByte(th, 171)
#if TAG_MAJOR_VERSION == 34
#define EAT_CANARY do if (th.getMinorVersion() >= TAG_MINOR_CANARIES    \
                          && unmarshallUByte(th) != 171)                \
                   {                                                    \
                       die("save corrupted: canary gone");              \
                   } while (0)
#else
#define EAT_CANARY do if ( unmarshallUByte(th) != 171)                  \
                   {                                                    \
                       die("save corrupted: canary gone");              \
                   } while (0)
#endif

#if TAG_MAJOR_VERSION == 34
static void _ensure_entry(branch_type br)
{
    dungeon_feature_type entry = branches[br].entry_stairs;
    for (rectangle_iterator ri(1); ri; ++ri)
        if (orig_terrain(*ri) == entry)
            return;

    // Find primary upstairs.
    for (rectangle_iterator ri(1); ri; ++ri)
        if (orig_terrain(*ri) == DNGN_STONE_STAIRS_UP_I)
        {
            for (distance_iterator di(*ri); di; ++di)
                if (in_bounds(*di) && env.grid(*di) == DNGN_FLOOR)
                {
                    env.grid(*di) = entry; // No need to update LOS, etc.
                    // Announce the repair even in non-debug builds.
                    mprf(MSGCH_ERROR, "Placing missing branch entry: %s.",
                         dungeon_feature_name(entry));
                    return;
                }
            die("no floor to place a branch entrance");
        }
    die("no upstairs on %s???", level_id::current().describe().c_str());
}

static void _ensure_exit(branch_type br)
{
    dungeon_feature_type exit = branches[br].exit_stairs;

    for (rectangle_iterator ri(1); ri; ++ri)
        if (orig_terrain(*ri) == exit)
            return;

    // Find primary downstairs.
    for (rectangle_iterator ri(1); ri; ++ri)
        if (orig_terrain(*ri) == DNGN_STONE_STAIRS_DOWN_I)
        {
            for (distance_iterator di(*ri); di; ++di)
                if (in_bounds(*di)
                    && (env.grid(*di) == DNGN_FLOOR
                        || env.grid(*di) == DNGN_SHALLOW_WATER))
                {
                    env.grid(*di) = exit; // No need to update LOS, etc.
                    // Announce the repair even in non-debug builds.
                    mprf(MSGCH_ERROR, "Placing missing branch exit: %s.",
                         dungeon_feature_name(exit));
                    return;
                }
            die("no floor to place a branch exit");
        }
    die("no downstairs on %s???", level_id::current().describe().c_str());
}

static void _add_missing_branches()
{
    if (crawl_state.game_is_descent())
        return;

    const level_id lc = level_id::current();

    // Could do all just in case, but this seems safer:
    if (brentry[BRANCH_VAULTS] == lc)
        _ensure_entry(BRANCH_VAULTS);
    if (brentry[BRANCH_ZOT] == lc)
        _ensure_entry(BRANCH_ZOT);
    // TODO: centralize these numbers
    // crosscheck with check_map_validity when changing
    if (lc == level_id(BRANCH_DEPTHS, 1) || lc == level_id(BRANCH_DUNGEON, 21))
        _ensure_entry(BRANCH_VESTIBULE);
    if (lc == level_id(BRANCH_DEPTHS, 2) || lc == level_id(BRANCH_DUNGEON, 24))
        _ensure_entry(BRANCH_PANDEMONIUM);
    if (lc == level_id(BRANCH_DEPTHS, 3) || lc == level_id(BRANCH_DUNGEON, 25))
        _ensure_entry(BRANCH_ABYSS);
    if (player_in_branch(BRANCH_VESTIBULE))
    {
        for (rectangle_iterator ri(0); ri; ++ri)
        {
            if (env.grid(*ri) == DNGN_STONE_ARCH)
            {
                map_marker *marker = env.markers.find(*ri, MAT_FEATURE);
                if (marker)
                {
                    map_feature_marker *featm =
                        dynamic_cast<map_feature_marker*>(marker);
                    // [ds] Ensure we're activating the correct feature
                    // markers. Feature markers are also used for other things,
                    // notably to indicate the return point from a portal
                    // vault.
                    switch (featm->feat)
                    {
                    case DNGN_ENTER_COCYTUS:
                    case DNGN_ENTER_DIS:
                    case DNGN_ENTER_GEHENNA:
                    case DNGN_ENTER_TARTARUS:
                        env.grid(*ri) = featm->feat;
                        dprf("opened %s", dungeon_feature_name(featm->feat));
                        env.markers.remove(marker);
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    }
}
#endif


// Write a tagged chunk of data to the FILE*.
// tagId specifies what to write.
void tag_write(tag_type tagID, writer &outf)
{
    vector<unsigned char> buf;
    writer th(&buf);
    switch (tagID)
    {
    case TAG_CHR:
        _tag_construct_char(th);
        break;
    case TAG_YOU:
        _tag_construct_you(th);
        CANARY;
        _tag_construct_you_items(th);
        CANARY;
        _tag_construct_you_dungeon(th);
        CANARY;
        _tag_construct_lost_monsters(th);
        CANARY;
        _tag_construct_companions(th);
        break;
    case TAG_LEVEL:
        _tag_construct_level(th);
        CANARY;
        _tag_construct_level_items(th);
        CANARY;
        _tag_construct_level_monsters(th);
        CANARY;
        _tag_construct_level_tiles(th);
        break;
    case TAG_GHOST:
        _tag_construct_ghost(th, global_ghosts);
        break;
    default:
        // I don't know how to make that!
        break;
    }

    // make sure there is some data to write!
    if (buf.empty())
        return;

    // Write tag header.
    marshallInt(outf, buf.size());

    // Write tag data.
    outf.write(&buf[0], buf.size());
}

static void _shunt_monsters_out_of_walls()
{
    for (int i = 0; i < MAX_MONSTERS; ++i)
    {
        monster &m(env.mons[i]);
        if (m.alive() && in_bounds(m.pos()) && cell_is_solid(m.pos())
            // Allow wall dwellers
            && !m.is_habitable(m.pos()))
        {
            for (distance_iterator di(m.pos()); di; ++di)
                if (!actor_at(*di) && !cell_is_solid(*di))
                {
#if TAG_MAJOR_VERSION == 34
                    // Could have been a rock worm or a dryad from old saves.
                    if (m.type != MONS_GHOST)
#endif
                    mprf(MSGCH_ERROR, "Error: monster %s in %s at (%d,%d)",
                         m.name(DESC_PLAIN, true).c_str(),
                         dungeon_feature_name(env.grid(m.pos())),
                         m.pos().x, m.pos().y);
                    env.mgrid(m.pos()) = NON_MONSTER;
                    m.position = *di;
                    env.mgrid(*di) = i;
                    break;
                }
        }
    }
}

#if TAG_MAJOR_VERSION == 34
static bool _is_spectral_weapon(const item_def& weapon)
{
    return get_weapon_brand(weapon) == SPWPN_SPECTRAL
           || is_unrandom_artefact(weapon, UNRAND_GUARD);
}

static void _fix_player_spectral_weapon()
{
    if (!you.props.exists(SPECTRAL_WEAPON_KEY))
        return;

    mid_t weapon_mid = you.props[SPECTRAL_WEAPON_KEY].get_int();
    you.props.erase(SPECTRAL_WEAPON_KEY);

    monster* spectral_weapon = monster_by_mid(weapon_mid);
    if (!spectral_weapon)
        return;

    vector<item_def*> weapons = you.equipment.get_slot_items(SLOT_WEAPON);
    weapons.erase(remove_if(weapons.begin(), weapons.end(),
        [](const item_def* w) { return !_is_spectral_weapon(*w); }),
        weapons.end());

    item_def* spectral_item = spectral_weapon->mslot_item(MSLOT_WEAPON);

    if (weapons.empty() || !spectral_item)
    {
        monster_die(*spectral_weapon, KILL_RESET, NON_MONSTER, true);
        return;
    }

    // Because the spectral weapon monster holds a copy of the weapon and
    // the weapon can be changed afterwards (e.g. by being inscribed), we may
    // not be able to find an exact match.
    item_def* best_match = weapons[0];
    for (size_t i = 1; i < weapons.size(); ++i)
    {
        item_def* weapon = weapons[i];

        if (weapon->sub_type == spectral_item->sub_type
            && best_match->sub_type != spectral_item->sub_type)
        {
            best_match = weapon;
            break;
        }

        if (weapon->plus == spectral_item->plus
            && best_match->plus != spectral_item->plus)
        {
            best_match = weapon;
            break;
        }

        string spectral_item_name = "";
        if (spectral_item->props.exists(WEAPON_NAME_KEY))
            spectral_item_name = spectral_item->props[WEAPON_NAME_KEY].get_string();
        string best_match_name = "";
        if (best_match->props.exists(WEAPON_NAME_KEY))
            best_match_name = best_match->props[WEAPON_NAME_KEY].get_string();
        string weapon_name = "";
        if (weapon->props.exists(WEAPON_NAME_KEY))
            weapon_name = weapon->props[WEAPON_NAME_KEY].get_string();
        if (weapon_name == spectral_item_name
            && best_match_name != spectral_item_name)
        {
            best_match = weapon;
            break;
        }

        if (weapon->inscription == spectral_item->inscription
            && best_match->inscription != spectral_item->inscription)
        {
            best_match = weapon;
            break;
        }
    }
    best_match->props[SPECTRAL_WEAPON_KEY].get_int() = weapon_mid;
}

static void _fix_spectral_weapons()
{
    _fix_player_spectral_weapon();
    for (monster_iterator mi; mi; ++mi)
    {
        monster* mons = *mi;
        // Monsters as of TAG_MINOR_SPECTRAL_DUAL_WIELDING can only have one
        // spectral weapon that we'd have to fix.
        if (mons->props.exists(SPECTRAL_WEAPON_KEY))
        {
            mid_t weapon_mid = mons->props[SPECTRAL_WEAPON_KEY].get_int();
            mons->props.erase(SPECTRAL_WEAPON_KEY);

            item_def* weapon = mons->mslot_item(MSLOT_WEAPON);
            if (weapon && _is_spectral_weapon(*weapon))
            {
                weapon->props[SPECTRAL_WEAPON_KEY].get_int() = weapon_mid;
                continue;
            }

            weapon = mons->mslot_item(MSLOT_ALT_WEAPON);
            if (weapon && _is_spectral_weapon(*weapon))
            {
                weapon->props[SPECTRAL_WEAPON_KEY].get_int() = weapon_mid;
                continue;
            }

            monster* spectral_weapon = monster_by_mid(weapon_mid);
            if (spectral_weapon)
                monster_die(*spectral_weapon, KILL_RESET, NON_MONSTER, true);
        }
    }
}
#endif

// Read a piece of data from inf into memory, then run the appropriate reader.
//
// minorVersion is available for any sub-readers that need it
void tag_read(reader &inf, tag_type tag_id)
{
    // Read header info and data
    vector<unsigned char> buf;
    const int data_size = unmarshallInt(inf);
    ASSERT(data_size >= 0);

    // Fetch data in one go
    buf.resize(data_size);
    inf.read(&buf[0], buf.size());

    // Ok, we have data now.
    reader th(buf, inf.getMinorVersion());
    switch (tag_id)
    {
    case TAG_YOU:
        _tag_read_you(th);
        EAT_CANARY;
        _tag_read_you_items(th);
        EAT_CANARY;
        _tag_read_you_dungeon(th);
        EAT_CANARY;
        _tag_read_lost_monsters(th);
        EAT_CANARY;
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_NO_ITEM_TRANSIT)
        {
            _tag_read_lost_items(th);
            EAT_CANARY;
        }
        if (th.getMinorVersion() >= TAG_MINOR_COMPANION_LIST)
#endif
        _tag_read_companions(th);

        // If somebody SIGHUP'ed out of the skill menu with every skill
        // disabled. Doing this here rather in _tag_read_you() because
        // you.can_currently_train() requires the player's equipment be loaded.
        init_can_currently_train();

#if TAG_MAJOR_VERSION == 34
        // Set up Marks and major destruction mutation for current worshippers.
        // (Done outside _tag_read_you since evidently giving a mutation
        // requires equipment to be loaded.)
        if (th.getMinorVersion() < TAG_MINOR_MAKHLEB_REVAMP
            && you_worship(GOD_MAKHLEB))
        {
            makhleb_initialize_marks();
            if (you.raw_piety >= piety_breakpoint(3))
            {
                mutation_type mut = random_choose(MUT_MAKHLEB_DESTRUCTION_GEH,
                                                MUT_MAKHLEB_DESTRUCTION_COC,
                                                MUT_MAKHLEB_DESTRUCTION_TAR,
                                                MUT_MAKHLEB_DESTRUCTION_DIS);

                perma_mutate(mut, 1, "Makhleb's blessing");
            }
        }
#endif
        break;
    case TAG_LEVEL:
        _tag_read_level(th);
        EAT_CANARY;
        _tag_read_level_items(th);
        // We have to do this here because _tag_read_level_monsters()
        // might kill an elsewhere Ilsuiw follower, which ends up calling
        // terrain.cc:_dgn_check_terrain_items, which checks env.item.
        link_items();
        EAT_CANARY;
        _tag_read_level_monsters(th);
        EAT_CANARY;
#if TAG_MAJOR_VERSION == 34
        _add_missing_branches();

        // If an eleionoma destroyed the Swamp exit due to the bug fixed in
        // 0d5cf04, put the branch exit on the closest floor or shallow water
        // square we can find near the first down stairs.
        if (you.where_are_you == BRANCH_SWAMP
            && you.depth == 1
            && !crawl_state.game_is_descent())
        {
            _ensure_exit(BRANCH_SWAMP);
        }
#endif
        _shunt_monsters_out_of_walls();
        // The Abyss needs to visit other levels during level gen, before
        // all cells have been filled. We mustn't crash when it returns
        // from those excursions, and generate_abyss will check_map_validity
        // itself after the grid is fully populated.
        // Descent mode breaks levels by destroying the entrances; don't check
        // validity on a reload.
        if (!player_in_branch(BRANCH_ABYSS) && !crawl_state.game_is_descent())
        {
            unwind_var<coord_def> you_pos(you.position, coord_def());
            check_map_validity();
        }
        _tag_read_level_tiles(th);
#if TAG_MAJOR_VERSION == 34
        if (you.where_are_you == BRANCH_GAUNTLET
            && th.getMinorVersion() < TAG_MINOR_GAUNTLET_TRAPPED)
        {
            vault_placement *place = dgn_vault_at(you.pos());
            if (place && place->map.desc_or_name()
                         == "gammafunk_gauntlet_branching")
            {
                auto exit = DNGN_EXIT_GAUNTLET;
                env.grid(you.pos()) = exit;
                // Announce the repair even in non-debug builds.
                mprf(MSGCH_ERROR, "Placing emergency exit: %s.",
                     dungeon_feature_name(exit));
            }
        }

        // We can't do this when we unmarshall shops, since we haven't
        // unmarshalled items yet...
        if (th.getMinorVersion() < TAG_MINOR_SHOP_HACK)
            for (auto& entry : env.shop)
            {
                // Shop items were heaped up at this cell.
                for (stack_iterator si(coord_def(0, entry.second.num+5)); si; ++si)
                {
                    entry.second.stock.push_back(*si);
                    dec_mitm_item_quantity(si.index(), si->quantity);
                }
            }

#if TAG_MAJOR_VERSION == 34
        // We must do this after loading the player, monsters, and items, but
        // before removing any items.
        if (th.getMinorVersion() < TAG_MINOR_SPECTRAL_DUAL_WIELDING)
            _fix_spectral_weapons();
#endif

        // These you-related changes have to be after terrain is loaded,
        // because they might cause you to lose flight. That will check
        // the terrain below you and crash if the map hasn't loaded yet.
        {
            vector<item_def*> to_remove = you.equipment.get_forced_removal_list(true, true);
            for (item_def* item : to_remove)
                unequip_item(*item);
        }
#endif
        break;
    case TAG_GHOST:
        global_ghosts = _tag_read_ghost(th);
        break;
    default:
        // I don't know how to read that!
        die("unknown tag type");
    }
}

static void _tag_construct_char(writer &th)
{
    marshallByte(th, TAG_CHR_FORMAT);
    // Important: you may never remove or alter a field without bumping
    // CHR_FORMAT. Bumping it makes all saves invisible when browsed in an
    // older version.
    // Please keep this compatible even over major version breaks!

    // Appending fields is fine, but inserting new fields anywhere other than
    // the end of this function is not!

    marshallString2(th, you.your_name);
    marshallString2(th, Version::Long);

    marshallByte(th, you.species);
    marshallByte(th, you.char_class);
    marshallByte(th, you.experience_level);
    marshallString2(th, string(get_job_name(you.char_class)));
    marshallByte(th, you.religion);
    marshallString2(th, you.jiyva_second_name);

    // don't save wizmode suppression
    marshallByte(th, you.wizard || you.suppress_wizard);

    marshallByte(th, crawl_state.type);
    if (crawl_state.game_is_tutorial())
        marshallString2(th, crawl_state.map);

    marshallString2(th, species::name(you.species));
    marshallString2(th, you.religion ? god_name(you.religion) : "");

    // separate from the tutorial so we don't have to bump TAG_CHR_FORMAT
    marshallString2(th, crawl_state.map);

    marshallByte(th, you.explore);
}

/// is a custom scoring mechanism being stored?
static bool _calc_score_exists()
{
    lua_stack_cleaner clean(dlua);
    dlua.pushglobal("dgn.persist.calc_score");
    return !lua_isnil(dlua, -1);
}

static void _tag_construct_you(writer &th)
{
    marshallInt(th, you.last_mid);
    marshallByte(th, you.raw_piety);
    marshallShort(th, you.pet_target);

    marshallByte(th, you.max_level);
    marshallByte(th, you.where_are_you);
    marshallByte(th, you.depth);
    marshallByte(th, you.chapter);
    marshallByte(th, you.royal_jelly_dead);
    marshallByte(th, you.transform_uncancellable);
    marshallByte(th, you.berserk_penalty);
    marshallInt(th, you.abyss_speed);

    ASSERT(you.hp > 0 || you.pending_revival);
    marshallShort(th, you.pending_revival ? 0 : you.hp);

    marshallBoolean(th, you.fishtail);
    _marshall_as_int(th, you.form);
    _marshall_as_int(th, you.default_form);
    CANARY;

    // We *only* need to marshal player_equip_set::items. Everything else in
    // player_equip_set can be recreated live.
    marshallByte(th, you.equipment.items.size());
    for (player_equip_entry& entry : you.equipment.items)
    {
        marshallByte(th, entry.item);
        marshallByte(th, entry.slot);
        marshallBoolean(th, entry.melded);
        marshallBoolean(th, entry.attuned);
        marshallBoolean(th, entry.is_overflow);
    }

    ASSERT_RANGE(you.magic_points, 0, you.max_magic_points + 1);
    marshallUByte(th, you.magic_points);
    marshallByte(th, you.max_magic_points);

    COMPILE_CHECK(NUM_STATS == 3);
    for (int i = 0; i < NUM_STATS; ++i)
        marshallByte(th, you.base_stats[i]);

    CANARY;

    marshallInt(th, you.hit_points_regeneration);
    marshallInt(th, you.magic_points_regeneration);

    marshallInt(th, you.experience);
    marshallInt(th, you.total_experience);
    marshallInt(th, you.gold);

    marshallInt(th, you.exp_available);

    marshallInt(th, you.zigs_completed);
    marshallByte(th, you.zig_max);

    marshallString(th, you.banished_by);

    marshallShort(th, you.hp_max_adj_temp);
    marshallShort(th, you.hp_max_adj_perm);
    marshallShort(th, you.mp_max_adj);

    marshallShort(th, you.pos().x);
    marshallShort(th, you.pos().y);

    _marshallFixedBitVector<NUM_SPELLS>(th, you.spell_library);
    _marshallFixedBitVector<NUM_SPELLS>(th, you.hidden_spells);

    // how many spells?
    marshallUByte(th, MAX_KNOWN_SPELLS);
    for (int i = 0; i < MAX_KNOWN_SPELLS; ++i)
        marshallShort(th, you.spells[i]);

    marshallByte(th, 52);
    for (int i = 0; i < 52; i++)
        marshallByte(th, you.spell_letter_table[i]);

    marshallByte(th, 52);
    for (int i = 0; i < 52; i++)
        marshallShort(th, you.ability_letter_table[i]);

    marshallUByte(th, you.old_vehumet_gifts.size());
    for (auto spell : you.old_vehumet_gifts)
        marshallShort(th, spell);

    marshallUByte(th, you.vehumet_gifts.size());
    for (auto spell : you.vehumet_gifts)
        marshallShort(th, spell);

    CANARY;

    // how many skills?
    marshallByte(th, NUM_SKILLS);
    for (int j = 0; j < NUM_SKILLS; ++j)
    {
        marshallUByte(th, you.skills[j]);
        marshallByte(th, you.train[j]);
        marshallByte(th, you.train_alt[j]);
        marshallInt(th, you.training[j]);
        marshallInt(th, you.skill_points[j]);
        marshallByte(th, you.skill_order[j]);   // skills ordering
        marshallInt(th, you.training_targets[j]);
        marshallInt(th, you.skill_manual_points[j]);
    }

    marshallBoolean(th, you.auto_training);
    marshallByte(th, you.exercises.size());
    for (auto sk : you.exercises)
        marshallInt(th, sk);

    marshallByte(th, you.exercises_all.size());
    for (auto sk : you.exercises_all)
        marshallInt(th, sk);

    marshallByte(th, you.skill_menu_do);
    marshallByte(th, you.skill_menu_view);

    CANARY;

    // how many durations?
    marshallUByte(th, NUM_DURATIONS);
    for (int j = 0; j < NUM_DURATIONS; ++j)
        marshallInt(th, you.duration[j]);

    // how many attributes?
    marshallByte(th, NUM_ATTRIBUTES);
    for (int j = 0; j < NUM_ATTRIBUTES; ++j)
        marshallInt(th, you.attribute[j]);

    // Event timers.
    marshallByte(th, NUM_TIMERS);
    for (int j = 0; j < NUM_TIMERS; ++j)
    {
        marshallInt(th, you.last_timer_effect[j]);
        marshallInt(th, you.next_timer_effect[j]);
    }

    // how many mutations/demon powers?
    marshallShort(th, NUM_MUTATIONS);
    for (int j = 0; j < NUM_MUTATIONS; ++j)
    {
        marshallByte(th, you.mutation[j]);
        marshallByte(th, you.innate_mutation[j]);
        marshallByte(th, you.temp_mutation[j]);
        marshallByte(th, you.sacrifices[j]);
    }

    marshallByte(th, you.demonic_traits.size());
    for (int j = 0; j < int(you.demonic_traits.size()); ++j)
    {
        marshallByte(th, you.demonic_traits[j].level_gained);
        marshallShort(th, you.demonic_traits[j].mutation);
    }

    // set up sacrifice piety by ability
    marshallShort(th, 1 + ABIL_FINAL_SACRIFICE - ABIL_FIRST_SACRIFICE);
    for (int j = ABIL_FIRST_SACRIFICE; j <= ABIL_FINAL_SACRIFICE; ++j)
        marshallByte(th, you.sacrifice_piety[j]);

    marshallUByte(th, NUM_BANES);
    for (int j = 0; j < NUM_BANES; ++j)
        marshallInt(th, you.banes[j]);

    CANARY;

    // how many penances?
    marshallByte(th, NUM_GODS);
    for (god_iterator it; it; ++it)
        marshallByte(th, you.penance[*it]);

    // which gods have been worshipped by this character?
    for (god_iterator it; it; ++it)
        marshallByte(th, you.worshipped[*it]);

    // what is the extent of divine generosity?
    for (god_iterator it; it; ++it)
        marshallShort(th, you.num_current_gifts[*it]);
    for (god_iterator it; it; ++it)
        marshallShort(th, you.num_total_gifts[*it]);
    for (god_iterator it; it; ++it)
        marshallBoolean(th, you.one_time_ability_used[*it]);

    // how much piety have you achieved at highest with each god?
    for (god_iterator it; it; ++it)
        marshallByte(th, you.piety_max[*it]);

    marshallByte(th, you.gift_timeout);
    marshallUByte(th, you.saved_good_god_piety);
    marshallByte(th, you.previous_good_god);

    for (god_iterator it; it; ++it)
        marshallInt(th, you.exp_docked[*it]);
    for (god_iterator it; it; ++it)
        marshallInt(th, you.exp_docked_total[*it]);

    // elapsed time
    marshallInt(th, you.elapsed_time);

    // time of game start
    marshallInt(th, you.birth_time);

    handle_real_time();

    // TODO: maybe switch to marshalling real_time_ms.
    marshallInt(th, you.real_time());
    marshallInt(th, you.num_turns);
    marshallInt(th, you.exploration);

    marshallInt(th, you.magic_contamination);

#if TAG_MAJOR_VERSION == 34
    marshallUByte(th, 0);
#endif
    marshallUByte(th, you.transit_stair);
    marshallByte(th, you.entering_level);

    marshallByte(th, you.deaths);
    marshallByte(th, you.lives);

    CANARY;

    marshallInt(th, you.dactions.size());
    for (daction_type da : you.dactions)
        marshallByte(th, da);

    marshallInt(th, you.level_stack.size());
    for (const level_pos &lvl : you.level_stack)
        lvl.save(th);

    // List of currently beholding monsters (usually empty).
    marshallShort(th, you.beholders.size());
    for (mid_t beh : you.beholders)
         _marshall_as_int(th, beh);

    marshallShort(th, you.fearmongers.size());
    for (mid_t monger : you.fearmongers)
        _marshall_as_int(th, monger);

    marshallByte(th, you.piety_hysteresis);

    you.quiver_action.save(QUIVER_MAIN_SAVE_KEY);

    CANARY;

    // Action counts.
    marshallShort(th, you.action_count.size());
    for (const auto &ac : you.action_count)
    {
        marshallShort(th, ac.first.first);
        marshallInt(th, ac.first.second);
        for (int k = 0; k < 27; k++)
            marshallInt(th, ac.second[k]);
    }

    marshallByte(th, NUM_BRANCHES);
    for (int i = 0; i < NUM_BRANCHES; i++)
        marshallBoolean(th, you.branches_left[i]);

    marshallCoord(th, abyssal_state.major_coord);
    marshallInt(th, abyssal_state.seed);
    marshallInt(th, abyssal_state.depth);
    marshallFloat(th, abyssal_state.phase);
    marshall_level_id(th, abyssal_state.level);

#if TAG_MAJOR_VERSION == 34
    if (abyssal_state.level.branch == BRANCH_DWARF || !abyssal_state.level.is_valid())
        abyssal_state.level = level_id(static_cast<branch_type>(BRANCH_DUNGEON), 19);
#endif

    _marshall_constriction(th, &you);

    marshallUByte(th, you.octopus_king_rings);

    marshallUnsigned(th, you.uncancel.size());
    for (const pair<uncancellable_type, int>& unc : you.uncancel)
    {
        marshallUByte(th, unc.first);
        marshallInt(th, unc.second);
    }

    marshallUByte(th, 1); // number of seeds, for historical reasons: always 1
    marshallUnsigned(th, you.game_seed);
    marshallBoolean(th, you.fully_seeded); // TODO: remove on major version inc?
    marshallBoolean(th, you.deterministic_levelgen);
    CrawlVector rng_states = rng::generators_to_vector();
    rng_states.write(th);

    CANARY;

    // don't let vault caching errors leave a normal game with sprint scoring
    if (!crawl_state.game_is_sprint())
        ASSERT(!_calc_score_exists());

    if (!dlua.callfn("dgn_save_data", "u", &th))
        mprf(MSGCH_ERROR, "Failed to save Lua data: %s", dlua.error.c_str());

    CANARY;

    // Write a human-readable string out on the off chance that
    // we fail to be able to read this file back in using some later version.
    string revision = "Git:";
    revision += Version::Long;
    marshallString(th, revision);

    you.props.write(th);
}

static void _tag_construct_you_items(writer &th)
{
    // ENDOFPACK is the end of our real inventory, but there is one hidden slot
    // after that to temporarily hold items for examining items, so it's
    // important not to marshall the entire array.
    marshallByte(th, ENDOFPACK);
    for (int i = 0; i < ENDOFPACK; ++i)
        marshallItem(th, you.inv[i]);
    marshallByte(th, you.cur_talisman);

    _marshallFixedBitVector<NUM_RUNE_TYPES>(th, you.runes);
    marshallByte(th, you.obtainable_runes);

    _marshallFixedBitVector<NUM_GEM_TYPES>(th, you.gems_found);
    _marshallFixedBitVector<NUM_GEM_TYPES>(th, you.gems_shattered);
    for (const int time_spent : you.gem_time_spent)
        marshallInt(th, time_spent);

    // Item descrip for each type & subtype.
    // how many types?
    marshallUByte(th, NUM_IDESC);
    // how many subtypes?
    marshallUByte(th, MAX_SUBTYPES);
    for (int i = 0; i < NUM_IDESC; ++i)
        for (int j = 0; j < MAX_SUBTYPES; ++j)
            marshallInt(th, you.item_description[i][j]);

    marshallUByte(th, NUM_OBJECT_CLASSES);
    for (int i = 0; i < NUM_OBJECT_CLASSES; ++i)
    {
        if (!item_type_has_ids((object_class_type)i))
            continue;
        for (int j = 0; j < MAX_SUBTYPES; ++j)
            marshallBoolean(th, you.type_ids[i][j]);
    }

    CANARY;

    // how many unique items?
    marshallUByte(th, MAX_UNRANDARTS);
    for (int j = 0; j < MAX_UNRANDARTS; ++j)
        marshallByte(th,you.unique_items[j]);

    marshallShort(th, NUM_WEAPONS);
    for (int j = 0; j < NUM_WEAPONS; ++j)
        marshallInt(th,you.seen_weapon[j]);

    marshallShort(th, NUM_ARMOURS);
    for (int j = 0; j < NUM_ARMOURS; ++j)
        marshallInt(th,you.seen_armour[j]);

    _marshallFixedBitVector<NUM_MISCELLANY>(th, you.seen_misc);
    _marshallFixedBitVector<NUM_TALISMANS>(th, you.seen_talisman);

    for (int i = 0; i < NUM_OBJECT_CLASSES; i++)
        for (int j = 0; j < MAX_SUBTYPES; j++)
            marshallInt(th, you.force_autopickup[i][j]);

    you.equipment.update();
}

static void marshallPlaceInfo(writer &th, PlaceInfo place_info)
{
    marshallInt(th, place_info.branch);

    marshallInt(th, place_info.num_visits);
    marshallInt(th, place_info.levels_seen);

    marshallInt(th, place_info.mon_kill_exp);

    for (int i = 0; i < KC_NCATEGORIES; i++)
        marshallInt(th, place_info.mon_kill_num[i]);

    marshallInt(th, place_info.turns_total);
    marshallInt(th, place_info.turns_explore);
    marshallInt(th, place_info.turns_travel);
    marshallInt(th, place_info.turns_interlevel);
    marshallInt(th, place_info.turns_resting);
    marshallInt(th, place_info.turns_other);

    marshallInt(th, place_info.elapsed_total);
    marshallInt(th, place_info.elapsed_explore);
    marshallInt(th, place_info.elapsed_travel);
    marshallInt(th, place_info.elapsed_interlevel);
    marshallInt(th, place_info.elapsed_resting);
    marshallInt(th, place_info.elapsed_other);
}

static void marshallLevelXPInfo(writer &th, LevelXPInfo xp_info)
{
    marshall_level_id(th, xp_info.level);

    marshallInt(th, xp_info.non_vault_xp);
    marshallInt(th, xp_info.non_vault_count);
    marshallInt(th, xp_info.vault_xp);
    marshallInt(th, xp_info.vault_count);
}

static void marshallRankPietyInfo(writer &th, RankPietyInfo r)
{
    marshallByte(th, r.god);
    marshallInt(th, r.initial_piety);
    marshallInt(th, r.start_time);
    marshallInt(th, r.piety_lost);
    marshallInt(th, r.piety_gained);
    marshallInt(th, r.piety_decayed);
    marshallInt(th, r.piety_on_penance);
    marshallInt(th, r.piety_on_gifts);
    marshallInt(th, r.piety_on_stepdowns);
}

static void marshallConductInfo(writer &th, const ConductPietyInfo &cp_info)
{
    marshallMap(th, cp_info.conducts_count,
                _marshall_as_int<conduct_type>, marshallIntReference);
    marshallMap(th, cp_info.piety_from_conducts,
                _marshall_as_int<conduct_type>, marshallFloatReference);
}

static void marshallXLToConductMap(writer &th,
    const map<int, ConductPietyInfo> &conduct_info_by_xl)
{
    marshallMap(th, conduct_info_by_xl,
                marshallIntReference, marshallConductInfo);
}

static void marshallPietyInfo(writer &th, PietyInfo piety_info)
{
    marshallShort(th, piety_info.rank_info.size());
    for (auto &r : piety_info.rank_info)
        marshallRankPietyInfo(th, r);
    marshallMap(th, piety_info.conduct_info_by_god, _marshall_as_int<god_type>,
                marshallXLToConductMap);
    marshallInt(th, piety_info.rank);
}

static RankPietyInfo unmarshallRankPietyInfo(reader &th)
{
    RankPietyInfo r;
    r.god = static_cast<god_type>(unmarshallUByte(th));
    r.initial_piety = unmarshallInt(th);
    r.start_time = unmarshallInt(th);
    r.piety_lost = unmarshallInt(th);
    r.piety_gained = unmarshallInt(th);
    r.piety_decayed = unmarshallInt(th);
    r.piety_on_penance = unmarshallInt(th);
    r.piety_on_gifts = unmarshallInt(th);
    r.piety_on_stepdowns = unmarshallInt(th);
    return r;
}

static ConductPietyInfo unmarshallConductInfo(reader &th)
{
    ConductPietyInfo cp_info;
    unmarshallMap(th, cp_info.conducts_count,
                  unmarshall_int_as<conduct_type>, unmarshallInt);
    unmarshallMap(th, cp_info.piety_from_conducts,
                  unmarshall_int_as<conduct_type>, unmarshallFloat);
    return cp_info;
}

static map<int, ConductPietyInfo> unmarshallXLToConductInfo(reader &th)
{
    map<int, ConductPietyInfo> conduct_info_by_xl;
    unmarshallMap(th, conduct_info_by_xl,
                  unmarshallInt, unmarshallConductInfo);
    return conduct_info_by_xl;
}

static PietyInfo unmarshallPietyInfo(reader &th)
{
    PietyInfo piety_info;
    int rank_info_size = unmarshallShort(th);
    for (int i = 0; i < rank_info_size; ++i)
        piety_info.rank_info.push_back(unmarshallRankPietyInfo(th));
    unmarshallMap(th, piety_info.conduct_info_by_god,
        unmarshall_int_as<god_type>, unmarshallXLToConductInfo);
    piety_info.rank = unmarshallInt(th);
    return piety_info;
}

static void _tag_construct_you_dungeon(writer &th)
{
    // how many unique creatures?
    marshallShort(th, NUM_MONSTERS);
    for (int j = 0; j < NUM_MONSTERS; ++j)
        marshallByte(th,you.unique_creatures[j]); // unique beasties

    // how many branches?
    marshallByte(th, NUM_BRANCHES);
    for (int j = 0; j < NUM_BRANCHES; ++j)
    {
        marshallInt(th, brdepth[j]);
        marshall_level_id(th, brentry[j]);
        marshallSet(th, branch_uniq_map_tags[j], marshallString);
        marshallInt(th, branch_bribe[j]);
    }

    // Root of the dungeon; usually BRANCH_DUNGEON.
    marshallInt(th, root_branch);

    marshallMap(th, stair_level,
                _marshall_as_int<branch_type>, _marshall_level_id_set);
    marshallMap(th, shops_present,
                _marshall_level_pos, _marshall_as_int<shop_type>);
    marshallMap(th, altars_present,
                _marshall_level_pos, _marshall_as_int<god_type>);
    marshallMap(th, portals_present,
                _marshall_level_pos, _marshall_as_int<branch_type>);
    marshallMap(th, portal_notes,
                _marshall_level_pos, marshallString);
    marshallMap(th, level_annotations,
                marshall_level_id, marshallString);
    marshallMap(th, level_exclusions,
                marshall_level_id, marshallString);
    marshallMap(th, level_uniques,
            marshall_level_id, marshallString);
    marshallUniqueAnnotations(th);

    marshallPlaceInfo(th, you.global_info);
    vector<PlaceInfo> list = you.get_all_place_info();
    // How many different places we have info on?
    marshallShort(th, list.size());

    for (const PlaceInfo &place : list)
        marshallPlaceInfo(th, place);

    marshallLevelXPInfo(th, you.global_xp_info);

    vector<LevelXPInfo> xp_info_list = you.get_all_xp_info();
    // How many different levels do we have info on?
    marshallShort(th, xp_info_list.size());
    for (const LevelXPInfo &info : xp_info_list)
        marshallLevelXPInfo(th, info);

    _marshall_iterator(th, you.uniq_map_tags.begin(), you.uniq_map_tags.end(),
                       marshallString);
    _marshall_iterator(th, you.uniq_map_names.begin(), you.uniq_map_names.end(),
                       marshallString);
    _marshall_iterator(th, you.uniq_map_tags_abyss.begin(),
                       you.uniq_map_tags_abyss.end(), marshallString);
    _marshall_iterator(th, you.uniq_map_names_abyss.begin(),
                       you.uniq_map_names_abyss.end(), marshallString);
    marshallMap(th, you.vault_list, marshall_level_id, marshallStringVector);

    write_level_connectivity(th);

    marshallMonType(th, you.zot_orb_monster);
    marshallBoolean(th, you.zot_orb_monster_known);

    marshallPietyInfo(th, you.piety_info);
}

static void marshall_follower(writer &th, const follower &f)
{
    ASSERT(!invalid_monster_type(f.mons.type));
    ASSERT(f.mons.alive());
    marshallMonster(th, f.mons);
    marshallInt(th, f.transit_start_time);
    for (int i = 0; i < NUM_MONSTER_SLOTS; ++i)
        marshallItem(th, f.items[i]);
}

static follower unmarshall_follower(reader &th)
{
    follower f;
    unmarshallMonster(th, f.mons);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_FOLLOWER_TRANSIT_TIME)
#endif
        f.transit_start_time = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    else
    {
        //Set transit_start_time to 0 and let follower heal completely
        f.transit_start_time = 0;
    }
#endif
    for (int i = 0; i < NUM_MONSTER_SLOTS; ++i)
        unmarshallItem(th, f.items[i]);
    return f;
}

static void marshall_companion(writer &th, const companion &c)
{
    marshall_follower(th, c.mons);
    marshall_level_id(th, c.level);
    marshallInt(th, c.timestamp);
}

static companion unmarshall_companion(reader &th)
{
    companion c;
    c.mons = unmarshall_follower(th);
    c.level = unmarshall_level_id(th);
    c.timestamp = unmarshallInt(th);
    return c;
}

static void marshall_apostle(writer &th, const apostle_data &a)
{
    marshall_follower(th, a.apostle);
    marshall_level_id(th, a.corpse_location);
    marshallInt(th, a.state);
    marshallInt(th, a.vengeance_bonus);
}

static apostle_data unmarshall_apostle_data(reader &th)
{
    apostle_data a;
    a.apostle = unmarshall_follower(th);
    a.corpse_location = unmarshall_level_id(th);
    a.state = static_cast<apostle_state>(unmarshallInt(th));
    a.vengeance_bonus = unmarshallInt(th);
    return a;
}

static void marshall_follower_list(writer &th, const m_transit_list &mlist)
{
    marshallShort(th, mlist.size());

    for (const auto &follower : mlist)
        marshall_follower(th, follower);
}

static m_transit_list unmarshall_follower_list(reader &th)
{
    m_transit_list mlist;

    const int size = unmarshallShort(th);

    for (int i = 0; i < size; ++i)
    {
        follower f = unmarshall_follower(th);
        if (!f.mons.alive())
        {
            mprf(MSGCH_ERROR,
                 "Dead monster %s in transit list in saved game, ignoring.",
                 f.mons.name(DESC_PLAIN, true).c_str());
        }
        else
            mlist.push_back(f);
    }

    return mlist;
}

#if TAG_MAJOR_VERSION == 34
static i_transit_list unmarshall_item_list(reader &th)
{
    i_transit_list ilist;

    const int size = unmarshallShort(th);

    for (int i = 0; i < size; ++i)
    {
        item_def item;
        unmarshallItem(th, item);
        ilist.push_back(item);
    }

    return ilist;
}
#endif

static void marshall_level_map_masks(writer &th)
{
    for (rectangle_iterator ri(0); ri; ++ri)
    {
        marshallInt(th, env.level_map_mask(*ri));
        marshallInt(th, env.level_map_ids(*ri));
    }
}

static void unmarshall_level_map_masks(reader &th)
{
    for (rectangle_iterator ri(0); ri; ++ri)
    {
        env.level_map_mask(*ri) = unmarshallInt(th);
        env.level_map_ids(*ri)  = unmarshallInt(th);
    }
}

static void marshall_level_map_unique_ids(writer &th)
{
    marshallSet(th, env.level_uniq_maps, marshallString);
    marshallSet(th, env.level_uniq_map_tags, marshallString);
    // Note: env.current_branch_uniq_map_tags is not persisted, it only needs
    // to be correct during level generation
}

static void unmarshall_level_map_unique_ids(reader &th)
{
    unmarshallSet(th, env.level_uniq_maps, unmarshallString);
    unmarshallSet(th, env.level_uniq_map_tags, unmarshallString);
}

static void marshall_subvault_place(writer &th,
                                    const subvault_place &subvault_place);

static void marshall_mapdef(writer &th, const map_def &map)
{
    marshallString(th, map.name);
    map.write_index(th);
    map.write_maplines(th);
    marshallString(th, map.description);
    marshallMap(th, map.feat_renames,
                _marshall_as_int<dungeon_feature_type>, marshallString);
    _marshall_iterator(th,
                       map.subvault_places.begin(),
                       map.subvault_places.end(),
                       marshall_subvault_place);
}

static void marshall_subvault_place(writer &th,
                                    const subvault_place &subvault_place)
{
    marshallCoord(th, subvault_place.tl);
    marshallCoord(th, subvault_place.br);
    marshall_mapdef(th, *subvault_place.subvault);
}

static subvault_place unmarshall_subvault_place(reader &th);
static map_def unmarshall_mapdef(reader &th)
{
    map_def map;
    map.name = unmarshallString(th);
    map.read_index(th);
    map.read_maplines(th);
    map.description = unmarshallString(th);
    unmarshallMap(th, map.feat_renames,
                  unmarshall_int_as<dungeon_feature_type>,
                  unmarshallString);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_REIFY_SUBVAULTS
        && th.getMinorVersion() != TAG_MINOR_0_11)
#endif
        _unmarshall_vector(th, map.subvault_places, unmarshall_subvault_place);

    // reload the map epilogue from the current cache in case it hasn't yet
    // been run.
    // it would probably be better game-design-wise to marshall the epilogue,
    // but currently I don't think we marshall any lua code and I'm not sure
    // this is the best practice to get into.
    map.reload_epilogue();
    return map;
}

static subvault_place unmarshall_subvault_place(reader &th)
{
    subvault_place subvault;
    subvault.tl = unmarshallCoord(th);
    subvault.br = unmarshallCoord(th);
    subvault.set_subvault(unmarshall_mapdef(th));
    return subvault;
}

static void marshall_vault_placement(writer &th, const vault_placement &vp)
{
    marshallCoord(th, vp.pos);
    marshallCoord(th, vp.size);
    marshallShort(th, vp.orient);
    marshall_mapdef(th, vp.map);
    _marshall_iterator(th, vp.exits.begin(), vp.exits.end(), marshallCoord);
#if TAG_MAJOR_VERSION == 34
    marshallShort(th, -1);
#endif
    marshallByte(th, vp.seen);
}

static vault_placement unmarshall_vault_placement(reader &th)
{
    vault_placement vp;
    vp.pos = unmarshallCoord(th);
    vp.size = unmarshallCoord(th);
    vp.orient = static_cast<map_section_type>(unmarshallShort(th));
    vp.map = unmarshall_mapdef(th);
    _unmarshall_vector(th, vp.exits, unmarshallCoord);
#if TAG_MAJOR_VERSION == 34
    unmarshallShort(th);
#endif
    vp.seen = !!unmarshallByte(th);

    return vp;
}

static void marshall_level_vault_placements(writer &th)
{
    marshallShort(th, env.level_vaults.size());
    for (unique_ptr<vault_placement> &vp : env.level_vaults)
        marshall_vault_placement(th, *vp);
}

static void unmarshall_level_vault_placements(reader &th)
{
    const int nvaults = unmarshallShort(th);
    ASSERT(nvaults >= 0);
    dgn_clear_vault_placements();
    for (int i = 0; i < nvaults; ++i)
    {
        env.level_vaults.emplace_back(
            new vault_placement(unmarshall_vault_placement(th)));
    }
}

static void marshall_level_vault_data(writer &th)
{
    marshallString(th, env.level_build_method);
    marshallSet(th, env.level_layout_types, marshallString);

    marshall_level_map_masks(th);
    marshall_level_map_unique_ids(th);
#if TAG_MAJOR_VERSION == 34
    marshallInt(th, 0);
#endif
    marshall_level_vault_placements(th);
}

static void unmarshall_level_vault_data(reader &th)
{
    env.level_build_method = unmarshallString(th);
    unmarshallSet(th, env.level_layout_types, unmarshallString);

    unmarshall_level_map_masks(th);
    unmarshall_level_map_unique_ids(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_VAULT_LIST) // 33:17 has it
        unmarshallStringVector(th);
#endif
    unmarshall_level_vault_placements(th);
}

static void marshall_shop(writer &th, const shop_struct& shop)
{
    marshallByte(th, shop.type);
    marshallByte(th, shop.keeper_name[0]);
    marshallByte(th, shop.keeper_name[1]);
    marshallByte(th, shop.keeper_name[2]);
    marshallByte(th, shop.pos.x);
    marshallByte(th, shop.pos.y);
    marshallByte(th, shop.greed);
    marshallByte(th, shop.level);
    marshallString(th, shop.shop_name);
    marshallString(th, shop.shop_type_name);
    marshallString(th, shop.shop_suffix_name);
    _marshall_iterator(th, shop.stock.begin(), shop.stock.end(),
                       bind(marshallItem, placeholders::_1, placeholders::_2,
                            false));
}

static void unmarshall_shop(reader &th, shop_struct& shop)
{
    shop.type  = static_cast<shop_type>(unmarshallByte(th));
#if TAG_MAJOR_VERSION == 34
    if (shop.type == SHOP_UNASSIGNED)
        return;
    if (th.getMinorVersion() < TAG_MINOR_MISC_SHOP_CHANGE
        && shop.type == NUM_SHOPS)
    {
        // This was SHOP_MISCELLANY, which is now part of SHOP_EVOKABLES.
        shop.type = SHOP_EVOKABLES;
    }
#else
    ASSERT(shop.type != SHOP_UNASSIGNED);
#endif
    shop.keeper_name[0] = unmarshallUByte(th);
    shop.keeper_name[1] = unmarshallUByte(th);
    shop.keeper_name[2] = unmarshallUByte(th);
    shop.pos.x = unmarshallByte(th);
    shop.pos.y = unmarshallByte(th);
    shop.greed = unmarshallByte(th);
    shop.level = unmarshallByte(th);
    shop.shop_name = unmarshallString(th);
    shop.shop_type_name = unmarshallString(th);
    shop.shop_suffix_name = unmarshallString(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_SHOP_HACK)
        shop.stock.clear();
    else
#endif
    _unmarshall_vector(th, shop.stock, [] (reader& r) -> item_def
                                       {
                                           item_def ret;
                                           unmarshallItem(r, ret);
                                           return ret;
                                       });
}

void ShopInfo::save(writer& outf) const
{
    marshall_shop(outf, shop);
}

void ShopInfo::load(reader& inf)
{
#if TAG_MAJOR_VERSION == 34
    if (inf.getMinorVersion() < TAG_MINOR_SHOPINFO
        || inf.getMinorVersion() == TAG_MINOR_UNSHOPINFO)
    {
        shop.type = static_cast<shop_type>(unmarshallShort(inf));

        shop.pos.x = unmarshallShort(inf);
        shop.pos.x &= 0xFF;

        shop.pos.y = unmarshallShort(inf);

        int itemcount = unmarshallShort(inf);

        // xref hack in shopping.cc:shop_name()
        shop.shop_name = " ";
        unmarshallString4(inf, shop.shop_type_name);
        for (int i = 0; i < itemcount; ++i)
        {
            shop.stock.emplace_back();
            unmarshallItem(inf, shop.stock.back());
            int cost = unmarshallShort(inf);
            shop.greed = cost * 10 / item_value(shop.stock.back(),
                shoptype_identifies_stock(shop.type));
        }
    }
    else
#endif
    unmarshall_shop(inf, shop);
}

static void _tag_construct_lost_monsters(writer &th)
{
    marshallMap(th, the_lost_ones, marshall_level_id,
                 marshall_follower_list);
}

static void _tag_construct_companions(writer &th)
{
#if TAG_MAJOR_VERSION == 34
    fixup_bad_companions();
#endif
    marshallMap(th, companion_list, _marshall_as_int<mid_t>,
                 marshall_companion);

    const uint8_t size = apostles.size();
    marshallByte(th, size);
    for (auto &apostle: apostles)
        marshall_apostle(th, apostle);
}

// Save versions 30-32.26 are readable but don't store the names.
static const char* old_species[]=
{
    "Human", "High Elf", "Deep Elf", "Sludge Elf", "Mountain Dwarf", "Halfling",
    "Hill Orc", "Kobold", "Mummy", "Naga", "Ogre", "Troll",
    "Red Draconian", "White Draconian", "Green Draconian", "Yellow Draconian",
    "Grey Draconian", "Black Draconian", "Purple Draconian", "Mottled Draconian",
    "Pale Draconian", "Draconian", "Centaur", "Demigod", "Spriggan", "Minotaur",
    "Demonspawn", "Ghoul", "Tengu", "Merfolk", "Vampire", "Deep Dwarf", "Felid",
    "Octopode",
};

static const char* old_gods[]=
{
    "", "Zin", "The Shining One", "Kikubaaqudgha", "Yredelemnul", "Xom",
    "Vehumet", "Okawaru", "Makhleb", "Sif Muna", "Trog", "Nemelex Xobeh",
    "Elyvilon", "Lugonu", "Beogh", "Jiyva", "Fedhas", "Cheibriados",
    "Ashenzari",
};

player_save_info tag_read_char_info(reader &th, uint8_t /*format*/,
                                    uint8_t major, uint32_t minor)
{
    player_save_info r;
    // Important: the beginning of this chunk is read in
    // files.cc:_read_char_chunk, which handles loading save version info.
    // Values out of bounds are good here, the save browser needs to
    // be forward-compatible. We validate them only on an actual restore.
    r.name = unmarshallString2(th);
    r.prev_save_version = unmarshallString2(th);
    dprf("Saved character %s, version: %s", r.name.c_str(),
                                            r.prev_save_version.c_str());

    r.species = static_cast<species_type>(unmarshallUByte(th));
    r.job = static_cast<job_type>(unmarshallUByte(th));
    r.experience_level = unmarshallByte(th);
    r.class_name = unmarshallString2(th);
    r.religion = static_cast<god_type>(unmarshallUByte(th));
    r.jiyva_second_name = unmarshallString2(th);
    r.wizard = unmarshallBoolean(th);

    // this was mistakenly inserted in the middle for a few tag versions - this
    // just makes sure that games generated in that time period are still
    // readable, but should not be used for new games
#if TAG_CHR_FORMAT == 0
    // TAG_MINOR_EXPLORE_MODE and TAG_MINOR_FIX_EXPLORE_MODE
    if (major == 34 && (minor >= 121 && minor < 130))
        r.explore = unmarshallBoolean(th);
#endif

    r.saved_game_type = static_cast<game_type>(unmarshallUByte(th));
    // normalize invalid game types so they can be treated uniformly elsewhere
    if (r.saved_game_type > NUM_GAME_TYPE)
        r.saved_game_type = NUM_GAME_TYPE;

    if (r.saved_game_type == GAME_TYPE_TUTORIAL)
        r.map = unmarshallString2(th);

    if (major > 32 || major == 32 && minor > 26)
    {
        r.species_name = unmarshallString2(th);
        r.god_name = unmarshallString2(th);
    }
    else
    {
        if (r.species >= 0 && r.species < (int)ARRAYSZ(old_species))
            r.species_name = old_species[you.species];

        if (r.religion >= 0 && r.religion < (int)ARRAYSZ(old_gods))
            r.god_name = old_gods[you.religion];
    }

    if (major > 34 || major == 34 && minor >= 29)
        r.map = unmarshallString2(th);

    if (major > 34 || major == 34 && minor >= 130)
        r.explore = unmarshallBoolean(th);

    return r;
}

#if TAG_MAJOR_VERSION == 34
static void _cap_mutation_at(mutation_type mut, int cap)
{
    if (you.mutation[mut] > cap)
    {
        // Don't convert real mutation levels to temporary.
        int real_levels = you.get_base_mutation_level(mut, true, false, true);
        you.temp_mutation[mut] = max(cap - real_levels, 0);

        you.mutation[mut] = cap;
    }
    if (you.innate_mutation[mut] > cap)
        you.innate_mutation[mut] = cap;
    if (you.sacrifices[mut] > cap)
        you.sacrifices[mut] = cap;
}

static void _clear_mutation(mutation_type mut)
{
    _cap_mutation_at(mut, 0);
}

static spell_type _fixup_removed_spells(spell_type s)
{
    switch (s)
    {
        case SPELL_FORCE_LANCE:
        case SPELL_VENOM_BOLT:
        case SPELL_POISON_ARROW:
        case SPELL_BOLT_OF_COLD:
        case SPELL_BOLT_OF_DRAINING:
        case SPELL_THROW_FLAME:
        case SPELL_THROW_FROST:
        case SPELL_RING_OF_FLAMES:
        case SPELL_HASTE:
        case SPELL_STICKS_TO_SNAKES:
        case SPELL_GRAVITAS:
            return SPELL_NO_SPELL;

        case SPELL_FLAME_TONGUE:
            return SPELL_FOXFIRE;

        case SPELL_THROW_ICICLE:
            return SPELL_HAILSTORM;

        case SPELL_BOLT_OF_FIRE:
            return SPELL_STARBURST;

        case SPELL_CONFUSE:
            return SPELL_CONFUSING_TOUCH;

        case SPELL_IRON_SHOT:
            return SPELL_BOMBARD;

        case SPELL_AGONISING_TOUCH:
            return SPELL_CURSE_OF_AGONY;

        case SPELL_ANIMATE_SKELETON:
            return SPELL_SOUL_SPLINTER;

        case SPELL_STING:
            return SPELL_POISONOUS_VAPOURS;

        case SPELL_MONSTROUS_MENAGERIE:
            return SPELL_SPHINX_SISTERS;

        default:
            return s;
    }
}

static spell_type _fixup_positional_monster_spell(spell_type s)
{
    switch (s)
    {
        case SPELL_GLOOM:
        case SPELL_INNER_FLAME:
        case SPELL_CONJURE_FLAME:
            return SPELL_NO_SPELL;

        case SPELL_ISKENDERUNS_MYSTIC_BLAST:
            return SPELL_FORCE_LANCE;

        case SPELL_AGONISING_TOUCH:
            return SPELL_AGONY;

        case SPELL_DISPEL_UNDEAD:
            return SPELL_DISPEL_UNDEAD_RANGE;

        default:
            return s;
    }
}

static void _fixup_library_spells(FixedBitVector<NUM_SPELLS>& lib)
{
    for (int i = 0; i < NUM_SPELLS; ++i)
    {
        spell_type newspell = _fixup_removed_spells((spell_type) i);

        if (newspell == SPELL_NO_SPELL)
            lib.set(i, false);
        else if (newspell != (spell_type) i)
        {
            // Only give the fixup if they had the spell, don't remove
            // replacements
            if (lib[i])
                lib.set(newspell, lib[i]);
            lib.set(i, false);
        }
    }
}
#endif

void unmarshall_vehumet_spells(reader &th, set<spell_type>& old_gifts,
        set<spell_type>& gifts)
{
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_VEHUMET_SPELL_GIFT
        && th.getMinorVersion() != TAG_MINOR_0_11)
    {
#endif
        const auto num_old_gifts = unmarshallUByte(th);
        for (int i = 0; i < num_old_gifts; ++i)
        {
            const auto spell = unmarshallSpellType(th);
            if (!spell_removed(spell))
                old_gifts.insert(spell);
        }

#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_VEHUMET_MULTI_GIFTS)
        {
            const auto spell = unmarshallSpellType(th);
            if (!spell_removed(spell))
                gifts.insert(spell);
        }
        else
        {
#endif
            const auto num_gifts = unmarshallUByte(th);
            for (int i = 0; i < num_gifts; ++i)
            {
                const auto spell = unmarshallSpellType(th);
                if (!spell_removed(spell))
                    gifts.insert(spell);
            }
#if TAG_MAJOR_VERSION == 34
        }
    }
#endif
}

FixedVector<spell_type, MAX_KNOWN_SPELLS> unmarshall_player_spells(reader &th)
{
    FixedVector<spell_type, MAX_KNOWN_SPELLS> spells(SPELL_NO_SPELL);

    const auto count = unmarshallUByte(th);
    ASSERT(count >= 0);

    for (int i = 0; i < count && i < MAX_KNOWN_SPELLS; ++i)
    {
        spells[i] = unmarshallSpellType(th);
#if TAG_MAJOR_VERSION == 34
        spells[i] = _fixup_removed_spells(spells[i]);
#endif
        if (spell_removed(spells[i])
#if TAG_MAJOR_VERSION == 34
            // We'll clean up form spells much later, so that we can give out
            // compensatory talismans.
            && !spell_was_form(spells[i])
#endif
            )
        {
            spells[i] = SPELL_NO_SPELL;
        }
    }

    for (int i = MAX_KNOWN_SPELLS; i < count; ++i)
        unmarshallSpellType(th);

    return spells;
}

FixedVector<int, 52> unmarshall_player_spell_letter_table(reader &th)
{
    FixedVector<int, 52> spell_letter_table;

    const auto count = unmarshallByte(th);
    ASSERT(count == (int)spell_letter_table.size());

    for (int i = 0; i < count; i++)
    {
        int s = unmarshallByte(th);
        ASSERT_RANGE(s, -1, MAX_KNOWN_SPELLS);
        spell_letter_table[i] = s;
    }

    return spell_letter_table;
}


void remove_removed_library_spells(FixedBitVector<NUM_SPELLS>& lib)
{
    for (int i = 0; i < NUM_SPELLS; ++i)
        lib.set(i, lib[i] && !spell_removed(static_cast<spell_type>(i)));
}


static void _fixup_species_mutations(mutation_type mut)
{
    // this is *not safe* to use with any mutations where there could be a
    // physiology conflict, or with mutations where there could be random
    // upgrades on top of the innate levels (e.g. MUT_SPIT_POISON).
    int total = 0;
    // Don't perma_mutate since that gives messages.
    for (const auto& lum : get_species_def(you.species).level_up_mutations)
        if (lum.xp_level <= you.experience_level && lum.mut == mut)
            total += lum.mut_level;

    you.innate_mutation[mut] = you.mutation[mut] = total;
}

#if TAG_MAJOR_VERSION == 34
// Copy action counts from one action to another, possibly modifying the
// sub-action in the process. Retain any counts which were already against the
// "new" action.
static void _move_action_count(caction_type old_action, caction_type new_action,
                               int old_subtype, int new_subtype)
{
    pair<caction_type, int> oldkey(old_action, caction_compound(old_subtype)),
        newkey(new_action, caction_compound(new_subtype));
    if (!you.action_count.count(oldkey))
        return;
    if (!you.action_count.count(newkey))
        you.action_count[newkey].init(0);
    for (int i = 0; i < 27; i++)
        you.action_count[newkey][i] += you.action_count[oldkey][i];
    you.action_count.erase(oldkey);
}


// Vectors of information from old-style equipment slots.
// Since player equip info is unmarshalled long before items in our inventory
// are, we need to store this information as an interim step, since the new
// mappings cannot be created without seeing what items are in them.
vector<int8_t> old_eq;
vector<bool> old_melded;
vector<bool> old_attuned;

// Read old style equip arrays and equip items in the new system. (Will later
// be used by _convert_old_player_equipment())
static void _read_old_player_equipment(reader &th)
{
    // First, read old data.
    const int count = unmarshallByte(th);
    for (int i = 0; i < count; ++i)
        old_eq.push_back(unmarshallByte(th));
    for (int i = 0; i < count; ++i)
        old_melded.push_back(unmarshallBoolean(th));
    if (th.getMinorVersion() >= TAG_MINOR_TRACK_REGEN_ITEMS)
    {
        for (int i = 0; i < count; ++i)
            old_attuned.push_back(unmarshallBoolean(th));
    }
    else
    {
        for (int i = 0; i < count; ++i)
            old_attuned.push_back(false);
    }
}
#endif

static void _tag_read_you(reader &th)
{
    int count;
    UNUSED(_fixup_species_mutations); // prevent a tag upgrade warning

    // these `you` values come from the "chr" chunk, but aren't validated during
    // the reading of that chunk. Let's make sure they actually make sense...
    ASSERT(species::is_valid(you.species));
    ASSERT(job_type_valid(you.char_class));
    ASSERT_RANGE(you.experience_level, 1, 28);
    ASSERT(you.religion < NUM_GODS);
    ASSERT_RANGE(crawl_state.type, GAME_TYPE_UNSPECIFIED + 1, NUM_GAME_TYPE);
    // now start reading the chunk proper
    you.last_mid          = unmarshallInt(th);
    you.raw_piety             = unmarshallUByte(th);
    ASSERT(you.raw_piety <= MAX_PIETY);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_ROTTING)
        unmarshallUByte(th);
#endif
    you.pet_target        = unmarshallShort(th);

    you.max_level         = unmarshallByte(th);
    you.where_are_you     = static_cast<branch_type>(unmarshallUByte(th));
    ASSERT(you.where_are_you < NUM_BRANCHES);
    you.depth             = unmarshallByte(th);
    ASSERT(you.depth > 0);
    you.chapter           = static_cast<game_chapter>(unmarshallUByte(th));
    ASSERT(you.chapter < NUM_CHAPTERS);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_REMOVE_AK && you.chapter == CHAPTER_POCKET_ABYSS)
        you.chapter = CHAPTER_ORB_HUNTING;

    if (th.getMinorVersion() < TAG_MINOR_ZOT_OPEN)
        unmarshallBoolean(th);
#endif
    you.royal_jelly_dead = unmarshallBoolean(th);
    you.transform_uncancellable = unmarshallBoolean(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_IS_UNDEAD)
        unmarshallUByte(th);
    if (th.getMinorVersion() < TAG_MINOR_CALC_UNRAND_REACTS)
        unmarshallShort(th);
#endif
    you.berserk_penalty   = unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_GARGOYLE_DR
      && th.getMinorVersion() < TAG_MINOR_RM_GARGOYLE_DR)
    {
        unmarshallInt(th); // Slough an integer.
    }

    if (th.getMinorVersion() < TAG_MINOR_AUTOMATIC_MANUALS)
    {
        unmarshallShort(th);
        unmarshallInt(th);
    }
#endif

    you.abyss_speed = unmarshallInt(th);

#if TAG_MAJOR_VERSION == 34
    // was you.disease
    if (th.getMinorVersion() < TAG_MINOR_DISEASE)
        unmarshallInt(th);
#endif
    you.hp              = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    // was you.hunger
    if (th.getMinorVersion() < TAG_MINOR_LOAF_BUST)
        unmarshallShort(th);
#endif
    you.fishtail        = unmarshallBoolean(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_VAMPIRE_NO_EAT
        && th.getMinorVersion() < TAG_MINOR_REMOVE_VAMPIRES)
    {
        unmarshallBoolean(th);
    }
#endif
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_NOME_NO_MORE)
        unmarshallInt(th);
#endif
    you.form            = unmarshall_int_as<transformation>(th);
    ASSERT_RANGE(static_cast<int>(you.form), 0, NUM_TRANSFORMS);
#if TAG_MAJOR_VERSION == 34
    // Fix the effects of #7668 (Vampire lose undead trait once coming back
    // from lich form).
    if (you.form == transformation::none)
        you.transform_uncancellable = false;

    if (th.getMinorVersion() < TAG_MINOR_TALISMANS)
        you.default_form = transformation::none;
    else
#endif
        you.default_form = unmarshall_int_as<transformation>(th);
    ASSERT_RANGE(static_cast<int>(you.default_form), 0, NUM_TRANSFORMS);
    ASSERT(you.form != transformation::none || !you.transform_uncancellable);
    ASSERT(you.form != transformation::none
           || you.default_form == transformation::none);
    EAT_CANARY;

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_SAGE_REMOVAL)
    {
        count = unmarshallShort(th);
        ASSERT_RANGE(count, 0, 32768);
        for (int i = 0; i < count; ++i)
        {
            unmarshallByte(th);
            unmarshallInt(th);
            unmarshallInt(th);
        }
    }
#endif

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_EQUIP_SLOT_REWRITE)
        _read_old_player_equipment(th);

    if (th.getMinorVersion() >= TAG_MINOR_EQUIP_SLOT_REWRITE)
    {
#endif

    // Unmarshall equipment slot data
    count = unmarshallByte(th);
    for (int i = 0; i < count; ++i)
    {
        const int8_t item = unmarshallByte(th);
        const equipment_slot slot = static_cast<equipment_slot>(unmarshallByte(th));
        const bool melded = unmarshallBoolean(th);
        const bool attuned = unmarshallBoolean(th);
        const bool is_overflow = unmarshallBoolean(th);
        you.equipment.items.emplace_back(item, slot, melded, attuned, is_overflow);
    }

#if TAG_MAJOR_VERSION == 34
    }
#endif

    you.magic_points              = unmarshallUByte(th);
    you.max_magic_points          = unmarshallByte(th);

    for (int i = 0; i < NUM_STATS; ++i)
        you.base_stats[i] = unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
    // Gnolls previously had stats fixed at 7/7/7, so randomly award them stats
    // based on the points they'd have gotten from XL/3 selection and XL/4
    // random SID.
    if (th.getMinorVersion() >= TAG_MINOR_STATLOCKED_GNOLLS
        && th.getMinorVersion() < TAG_MINOR_GNOLLS_REDUX
        && you.species == SP_GNOLL)
    {
        const species_def& sd = get_species_def(you.species);

        // Give base stat points.
        species_stat_init(you.species);

        const set<stat_type> all_stats = {STAT_STR, STAT_INT, STAT_DEX};
        int num_points = you.experience_level / 3;
        for (int i = 0; i < num_points; ++i)
            modify_stat(*random_iterator(all_stats), 1, false);

        num_points = you.experience_level / sd.how_often;
        for (int i = 0; i < num_points; ++i)
            modify_stat(*random_iterator(sd.level_stats), 1, false);
    }
#endif
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_REMOVE_STAT_DRAIN)
    {
        for (int i = 0; i < NUM_STATS; ++i)
            unmarshallByte(th);
    }
    if (th.getMinorVersion() < TAG_MINOR_STAT_ZERO_DURATION)
    {
        for (int i = 0; i < NUM_STATS; ++i)
            unmarshallUByte(th);
    }
    if (th.getMinorVersion() < TAG_MINOR_STAT_ZERO)
    {
        for (int i = 0; i < NUM_STATS; ++i)
            unmarshallString(th);
    }
#endif
    EAT_CANARY;

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_INT_REGEN)
    {
        you.hit_points_regeneration   = unmarshallByte(th);
        you.magic_points_regeneration = unmarshallByte(th);
        unmarshallShort(th);
    }
    else
    {
#endif
    you.hit_points_regeneration   = unmarshallInt(th);
    you.magic_points_regeneration = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    }
#endif

    you.experience                = unmarshallInt(th);
    you.total_experience = unmarshallInt(th);
    you.gold                      = unmarshallInt(th);
    you.exp_available             = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_XP_SCALING)
    {
        you.total_experience *= 10;
        you.exp_available *= 10;
    }
    if (th.getMinorVersion() < TAG_MINOR_NO_ZOTDEF)
        unmarshallInt(th);
#endif
    you.zigs_completed            = unmarshallInt(th);
    you.zig_max                   = unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_TRACK_BANISHER)
        you.banished_by = "";
    else
#endif
        you.banished_by           = unmarshallString(th);

    you.hp_max_adj_temp           = unmarshallShort(th);
    you.hp_max_adj_perm           = unmarshallShort(th);
    you.mp_max_adj                = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_REMOVE_BASE_MP)
    {
        int baseadj = unmarshallShort(th);
        you.mp_max_adj += baseadj;
    }
    if (th.getMinorVersion() < TAG_MINOR_CLASS_HP_0)
        you.hp_max_adj_perm -= 8;
#endif

    const int x = unmarshallShort(th);
    const int y = unmarshallShort(th);
    // SIGHUP during Step from Time/etc is ok.
    ASSERT(!x && !y || in_bounds(x, y));
    you.move_to(coord_def(x, y), MV_INTERNAL);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_WEIGHTLESS)
        unmarshallShort(th);
#endif

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_GOLDIFY_BOOKS)
    {
#endif
        _unmarshallFixedBitVector<NUM_SPELLS>(th, you.spell_library);
        _unmarshallFixedBitVector<NUM_SPELLS>(th, you.hidden_spells);
#if TAG_MAJOR_VERSION == 34
        _fixup_library_spells(you.spell_library);
        _fixup_library_spells(you.hidden_spells);
    }
#endif

    remove_removed_library_spells(you.spell_library);
    remove_removed_library_spells(you.hidden_spells);

    you.spells = unmarshall_player_spells(th);
    you.spell_letter_table = unmarshall_player_spell_letter_table(th);
    you.spell_no = count_if(begin(you.spells), end(you.spells),
            [](const spell_type spell) { return spell != SPELL_NO_SPELL; });

    count = unmarshallByte(th);
    ASSERT(count == (int)you.ability_letter_table.size());
    for (int i = 0; i < count; i++)
    {
        int a = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_ABIL_1000)
        {
            if (a >= 230)
                a += 2000 - 230;
            else if (a >= 50)
                a += 1000 - 50;
        }
        if (th.getMinorVersion() < TAG_MINOR_ABIL_GOD_FIXUP)
        {
            if (a >= ABIL_ASHENZARI_END_TRANSFER + 1
                && a <= ABIL_ASHENZARI_END_TRANSFER + 3)
            {
                a += ABIL_STOP_RECALL - ABIL_ASHENZARI_END_TRANSFER - 1;
            }
        }
        if (a == ABIL_FLY
            || a == ABIL_WISP_BLINK // was ABIL_FLY_II
               && th.getMinorVersion() < TAG_MINOR_0_12)
        {
            a = ABIL_NON_ABILITY;
        }
        if (a == ABIL_EVOKE_STOP_LEVITATING
            || a == ABIL_STOP_FLYING)
        {
            a = ABIL_NON_ABILITY;
        }

        if (th.getMinorVersion() < TAG_MINOR_NO_JUMP)
        {
            // ABIL_JUMP deleted (ABIL_DIG has its old spot), map it
            // away and shift following intrinsic abilities down.
            // ABIL_EVOKE_JUMP was also deleted, but was the last
            // evocable ability, so just map it away.
            if (a == ABIL_DIG || a == ABIL_EVOKE_TELEPORT_CONTROL + 1)
                a = ABIL_NON_ABILITY;
            else if (a > ABIL_DIG && a < ABIL_MIN_EVOKE)
                a -= 1;
        }

        if (th.getMinorVersion() < TAG_MINOR_NEW_DRACONIAN_BREATH
            && species::is_draconian(you.species) && you.experience_level >= 7)
        {
            // XXX: Used to be ABIL_BREATHE_FIRE
            if (a == ABIL_GOLDEN_BREATH)
                a = ABIL_COMBUSTION_BREATH;

            // Give some charges to existing draconians
            you.props[DRACONIAN_BREATH_USES_KEY] = 3;
        }

        // Bad offset from games transferred prior to 0.17-a0-2121-g4af814f.
        if (a == NUM_ABILITIES)
            a = ABIL_NON_ABILITY;
#endif
        ASSERT_RANGE(a, ABIL_NON_ABILITY, NUM_ABILITIES);
        ASSERT(a != 0);
        you.ability_letter_table[i] = static_cast<ability_type>(a);
    }

    unmarshall_vehumet_spells(th, you.old_vehumet_gifts, you.vehumet_gifts);
    EAT_CANARY;

    // how many skills?
    count = unmarshallUByte(th);
    ASSERT(count <= NUM_SKILLS);
    for (int j = 0; j < count; ++j)
    {
        you.skills[j]          = unmarshallUByte(th);
        ASSERT(you.skills[j] <= 27 || you.wizard);

        you.train[j]    = (training_status)unmarshallByte(th);
        you.train_alt[j]    = (training_status)unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
        // Gnolls always train all skills.
        if (th.getMinorVersion() < TAG_MINOR_GNOLLS_REDUX
            && you.species == SP_GNOLL)
        {
            you.train[j] = you.train_alt[j] = TRAINING_ENABLED;
        }
#endif
        you.training[j] = unmarshallInt(th);
        you.skill_points[j]    = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_REMOVE_CT_SKILLS)
            unmarshallInt(th);
#endif
        you.skill_order[j]     = unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() >= TAG_MINOR_TRAINING_TARGETS)
        {
#endif
            you.training_targets[j] = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
        }
        else
            you.training_targets[j] = 0;

        if (th.getMinorVersion() >= TAG_MINOR_GOLDIFY_MANUALS)
        {
#endif
            you.skill_manual_points[j] = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
        }
        else
            you.skill_manual_points[j] = 0;
#endif
    }
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_SHAPESHIFTING)
    {
        // Historical note: SK_FORGECRAFT was SK_TRANSMUTATIONS at the time
        you.skills[SK_SHAPESHIFTING]              = you.skills[SK_FORGECRAFT];
        you.train[SK_SHAPESHIFTING]               = you.train[SK_FORGECRAFT];
        you.train_alt[SK_SHAPESHIFTING]           = you.train_alt[SK_FORGECRAFT];
        you.training[SK_SHAPESHIFTING]            = you.training[SK_FORGECRAFT];
        you.skill_points[SK_SHAPESHIFTING]        = you.skill_points[SK_FORGECRAFT];
        you.skill_order[SK_SHAPESHIFTING]         = you.skill_order[SK_FORGECRAFT] + 1;
        you.training_targets[SK_SHAPESHIFTING]    = you.training_targets[SK_FORGECRAFT];
        you.skill_manual_points[SK_SHAPESHIFTING] = you.skill_manual_points[SK_FORGECRAFT];
    }
#endif

    you.auto_training = unmarshallBoolean(th);

    count = unmarshallByte(th);
    for (int i = 0; i < count; i++)
        you.exercises.push_back((skill_type)unmarshallInt(th));

    count = unmarshallByte(th);
    for (int i = 0; i < count; i++)
        you.exercises_all.push_back((skill_type)unmarshallInt(th));

    you.skill_menu_do = static_cast<skill_menu_state>(unmarshallByte(th));
    you.skill_menu_view = static_cast<skill_menu_state>(unmarshallByte(th));
#if TAG_MAJOR_VERSION == 34
    if (you.skill_menu_view == SKM_VIEW_TRANSFER)
        you.skill_menu_view = SKM_NONE;
    // Was Ashenzari skill transfer information
    // Four ints to discard
    if (th.getMinorVersion() < TAG_MINOR_NEW_ASHENZARI)
        for (int j = 0; j < 4; ++j)
            unmarshallInt(th);
#endif

    // Set up you.skill_cost_level.
    you.skill_cost_level = 0;
    check_skill_cost_change();

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_MERGE_RANGED)
    {
        // should be a check for MUT_DISTRIBUTED_SKILL_TRAINING but
        // I don't remember if muts have been unmarshalled here.
        if (you.species != SP_GNOLL)
        {
            you.skill_points[SK_RANGED_WEAPONS] += you.skill_points[SK_CROSSBOWS]
                                                 + you.skill_points[SK_SLINGS];
        }
        you.train[SK_RANGED_WEAPONS] = max(you.train[SK_RANGED_WEAPONS],
                                           max(you.train[SK_CROSSBOWS],
                                               you.train[SK_SLINGS]));
        you.train_alt[SK_RANGED_WEAPONS] = max(you.train_alt[SK_RANGED_WEAPONS],
                                               max(you.train_alt[SK_CROSSBOWS],
                                                   you.train_alt[SK_SLINGS]));
        you.training_targets[SK_RANGED_WEAPONS] = max(you.training_targets[SK_RANGED_WEAPONS],
                                                      max(you.training_targets[SK_CROSSBOWS],
                                                          you.training_targets[SK_SLINGS]));
        // fixup_skills is called at the end of loading a character, in
        // _post_init
    }

    // should be a check for MUT_INNATE_CASTER but I don't remember if muts
    // have been unmarshalled here.
    if (th.getMinorVersion() < TAG_MINOR_DJ_SPLIT && you.species == SP_DJINNI)
    {
        // Balance XP from spellcasting across all other skills.
        cleanup_innate_magic_skills();
        // Fix which skills are enabled. (Don't bother fixing autotraining %s,
        // it'll all get balanced across skills anyway.)
        for (skill_type sk = SK_FIRST_MAGIC_SCHOOL; sk <= SK_LAST_MAGIC; ++sk)
        {
            you.train[sk] = you.train[SK_SPELLCASTING];
            you.train_alt[sk] = you.train_alt[SK_SPELLCASTING];
        }
        // Based on this, reset skill distribution percentages.
        reset_training();
    }

    if (th.getMinorVersion() < TAG_MINOR_ALCHEMY_MERGER)
    {
        // Historical note: SK_FORGECRAFT was SK_TRANSMUTATIONS at the time
        if (you.species != SP_GNOLL)
            you.skill_points[SK_ALCHEMY] += you.skill_points[SK_FORGECRAFT];

        you.train[SK_ALCHEMY] = max(you.train[SK_ALCHEMY],
                                    you.train[SK_FORGECRAFT]);
        you.train_alt[SK_ALCHEMY] = max(you.train_alt[SK_ALCHEMY],
                                        you.train_alt[SK_FORGECRAFT]);
        you.training_targets[SK_ALCHEMY] = max(you.training_targets[SK_ALCHEMY],
                                               you.training_targets[SK_FORGECRAFT]);
    }

    if (th.getMinorVersion() < TAG_MINOR_ADD_FORGECRAFT)
    {
        you.skills[SK_FORGECRAFT]              = you.skills[SK_SUMMONINGS];
        you.train[SK_FORGECRAFT]               = you.train[SK_SUMMONINGS];
        you.train_alt[SK_FORGECRAFT]           = you.train_alt[SK_SUMMONINGS];
        you.training[SK_FORGECRAFT]            = you.training[SK_SUMMONINGS];
        you.skill_points[SK_FORGECRAFT]        = you.skill_points[SK_SUMMONINGS];
        you.skill_order[SK_FORGECRAFT]         = you.skill_order[SK_SUMMONINGS] + 1;
        you.training_targets[SK_FORGECRAFT]    = you.training_targets[SK_SUMMONINGS];
        you.skill_manual_points[SK_FORGECRAFT] = you.skill_manual_points[SK_SUMMONINGS];
    }
#endif

    EAT_CANARY;

    // how many durations?
    count = unmarshallUByte(th);
    COMPILE_CHECK(NUM_DURATIONS < 256);
    for (int j = 0; j < count && j < NUM_DURATIONS; ++j)
        you.duration[j] = unmarshallInt(th);
    for (int j = NUM_DURATIONS; j < count; ++j)
        unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    if (you.species == SP_LAVA_ORC)
        you.duration[DUR_MAGIC_ARMOUR] = 0;

    if (th.getMinorVersion() < TAG_MINOR_FUNGUS_FORM
        && you.form == transformation::fungus)
    {
        you.duration[DUR_CONFUSING_TOUCH] = 0;
    }

    you.duration[DUR_JELLY_PRAYER] = 0;
#endif

    // how many attributes?
    count = unmarshallUByte(th);
    COMPILE_CHECK(NUM_ATTRIBUTES < 256);
    for (int j = 0; j < count && j < NUM_ATTRIBUTES; ++j)
    {
#if TAG_MAJOR_VERSION == 34
        if (j == ATTR_BANISHMENT_IMMUNITY && th.getMinorVersion() == TAG_MINOR_0_11)
        {
            unmarshallInt(th); // ATTR_UNUSED_1
            count--;
        }
        if (j == ATTR_NOISES && th.getMinorVersion() == TAG_MINOR_CLASS_HP_0
            && count == 40)
        {
            dprf("recovering ATTR_NOISES");
            j++, count++;
        }
#endif
        you.attribute[j] = unmarshallInt(th);
    }
#if TAG_MAJOR_VERSION == 34
    if (count == ATTR_PAKELLAS_EXTRA_MP && you_worship(GOD_PAKELLAS))
        you.attribute[ATTR_PAKELLAS_EXTRA_MP] = POT_MAGIC_MP;
#endif
    for (int j = count; j < NUM_ATTRIBUTES; ++j)
        you.attribute[j] = 0;
    for (int j = NUM_ATTRIBUTES; j < count; ++j)
        unmarshallInt(th);

#if TAG_MAJOR_VERSION == 34
    if (you.attribute[ATTR_DIVINE_REGENERATION])
    {
        you.attribute[ATTR_DIVINE_REGENERATION] = 0;
        you.duration[DUR_TROGS_HAND] = max(you.duration[DUR_TROGS_HAND],
                                           you.duration[DUR_REGENERATION]);
        you.duration[DUR_REGENERATION] = 0;
    }

    if (you.attribute[ATTR_DELAYED_FIREBALL])
        you.attribute[ATTR_DELAYED_FIREBALL] = 0;

    if (th.getMinorVersion() < TAG_MINOR_ENDLESS_DIVINE_SHIELD)
    {
        // Prevent players who upgraded with Divine Shield active from starting
        // with potentially hundreds of stored blocks.
        if (you.duration[DUR_DIVINE_SHIELD])
            you.duration[DUR_DIVINE_SHIELD] = you.attribute[ATTR_DIVINE_SHIELD];
    }

    if (th.getMinorVersion() < TAG_MINOR_NEGATIVE_DIVINE_SHIELD)
    {
        // Fix bugged negative charges.
        if (you.duration[DUR_DIVINE_SHIELD] < 0)
            you.duration[DUR_DIVINE_SHIELD] = 0;
    }

    if (th.getMinorVersion() < TAG_MINOR_SIMPLIFY_STAT_ZERO)
    {
        // Remove old stat-zero statuses.
        you.duration[DUR_COLLAPSE] = 0;
        you.duration[DUR_BRAINLESS] = 0;
        you.duration[DUR_CLUMSY] = 0;

        // Set new stat zero tracking, if any stats are currently below zero.
        you.attribute[ATTR_STAT_ZERO] = 0;
        for (int i = STAT_STR; i <= STAT_DEX; ++i)
            if (you.stat(static_cast<stat_type>(i), false) <= 0)
                you.attribute[ATTR_STAT_ZERO] |= 1 << i;
    }

    // Don't make pre-upgrade saves have to kill potentially thousands of
    // monsters to fix their temp mutations.
    if (th.getMinorVersion() < TAG_MINOR_TEMP_MUT_KILLS
        && you.attribute[ATTR_TEMP_MUT_KILLS] > 0)
    {
        you.attribute[ATTR_TEMP_MUT_KILLS] = 1;
    }

#endif

#if TAG_MAJOR_VERSION == 34
    // Nemelex item type sacrifice toggles.
    if (th.getMinorVersion() < TAG_MINOR_NEMELEX_WEIGHTS)
    {
        count = unmarshallByte(th);
        ASSERT(count <= NUM_OBJECT_CLASSES);
        for (int j = 0; j < count; ++j)
            unmarshallInt(th);
    }
#endif

    int timer_count = 0;
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_EVENT_TIMERS)
    {
#endif
    timer_count = unmarshallByte(th);
    ASSERT(timer_count <= NUM_TIMERS);
    for (int j = 0; j < timer_count; ++j)
    {
        you.last_timer_effect[j] = unmarshallInt(th);
        you.next_timer_effect[j] = unmarshallInt(th);
    }
#if TAG_MAJOR_VERSION == 34
    }
    else
        timer_count = 0;
#endif
    // We'll have to fix up missing/broken timer entries after
    // we unmarshall you.elapsed_time.

    // how many mutations/demon powers?
    count = unmarshallShort(th);
    ASSERT_RANGE(count, 0, NUM_MUTATIONS + 1);
    for (int j = 0; j < count; ++j)
    {
        you.mutation[j]         = unmarshallUByte(th);
        you.innate_mutation[j]  = unmarshallUByte(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() >= TAG_MINOR_TEMP_MUTATIONS
            && th.getMinorVersion() != TAG_MINOR_0_11)
        {
#endif
        you.temp_mutation[j]    = unmarshallUByte(th);
#if TAG_MAJOR_VERSION == 34
        }
        if (th.getMinorVersion() < TAG_MINOR_RU_SACRIFICES)
            you.sacrifices[j]   = 0;
        else
        {
#endif
        you.sacrifices[j]       = unmarshallUByte(th);
#if TAG_MAJOR_VERSION == 34
        }

        if (you.innate_mutation[j] + you.temp_mutation[j] > you.mutation[j])
        {
            if (th.getMinorVersion() >= TAG_MINOR_SPIT_POISON_AGAIN
                && th.getMinorVersion() < TAG_MINOR_SPIT_POISON_AGAIN_AGAIN
                && j == MUT_SPIT_POISON)
            {
                // this special case needs to be handled differently or
                // the level will be set too high; innate is what's corrupted.
                you.mutation[j] = you.innate_mutation[j] = 1;
                you.temp_mutation[j] = 0;
            }
            else
            {
                mprf(MSGCH_ERROR, "Mutation #%d out of sync, fixing up.", j);
                you.mutation[j] = you.innate_mutation[j] + you.temp_mutation[j];
            }
        }
#endif
    }


    // mutation fixups happen below here.
    // *REMINDER*: if you fix up an innate mutation, remember to adjust both
    // `you.mutation` and `you.innate_mutation`.

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_STAT_MUT)
    {
        // Convert excess mutational stats into base stats.
        mutation_type stat_mutations[] = { MUT_STRONG, MUT_CLEVER, MUT_AGILE };
        stat_type stat_types[] = { STAT_STR, STAT_INT, STAT_DEX };
        for (int j = 0; j < 3; ++j)
        {
            mutation_type mut = stat_mutations[j];
            stat_type stat = stat_types[j];
            int total_mutation_level = you.temp_mutation[mut] + you.mutation[mut];
            if (total_mutation_level > 2)
            {
                int new_level = max(0, min(you.temp_mutation[mut] - you.mutation[mut], 2));
                you.temp_mutation[mut] = new_level;
            }
            if (you.mutation[mut] > 2)
            {
                int excess = you.mutation[mut] - 4;
                if (excess > 0)
                    you.base_stats[stat] += excess;
                you.mutation[mut] = 2;
            }
        }
        mutation_type bad_stat_mutations[] = { MUT_WEAK, MUT_DOPEY, MUT_CLUMSY };
        for (int j = 0; j < 3; ++j)
        {
            mutation_type mut = bad_stat_mutations[j];
            int level = you.mutation[mut];
            switch (level)
            {
            case 0:
            case 1:
                you.mutation[mut] = 0;
                break;
            case 2:
            case 3:
                you.mutation[mut] = 1;
                break;
            default:
                you.mutation[mut] = 2;
                break;
            };
            if (you.temp_mutation[mut] > 2 && you.mutation[mut] < 2)
                you.temp_mutation[mut] = 1;
            else
                you.temp_mutation[mut] = 0;
        }
    }
    you.mutation[MUT_FAST] = you.innate_mutation[MUT_FAST];
    you.mutation[MUT_SLOW] = you.innate_mutation[MUT_SLOW];
    if (you.species != SP_NAGA)
        _clear_mutation(MUT_SPIT_POISON);
#endif

    // TODO: this code looks really out of context, it should at least have
    // an ASSERT identifying count, but I'm not 100% sure what that would be
    for (int j = count; j < NUM_MUTATIONS; ++j)
        you.mutation[j] = you.innate_mutation[j] = you.sacrifices[j];

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_NO_POTION_HEAL)
    {   // These use to apply no matter what the minor tag
        // was, so when TAG_MINOR_NO_POTION_HEAL was added
        // these were all moved to only apply to previous
        // tags.

        // some mutations from this tag are handled by generic cleanup code
        // below.

        if (you.species == SP_GARGOYLE)
            _clear_mutation(MUT_POISON_RESISTANCE);

        if (you.species == SP_FORMICID)
            you.mutation[MUT_ANTENNAE] = you.innate_mutation[MUT_ANTENNAE] = 3;
    }

    if (th.getMinorVersion() < TAG_MINOR_SAPROVOROUS
        && you.species == SP_ONI)
    {
        // Remove the innate level of fast metabolism
        you.mutation[MUT_FAST_METABOLISM] -= 1;
        you.innate_mutation[MUT_FAST_METABOLISM] -= 1;
    }

    if (th.getMinorVersion() < TAG_MINOR_ROT_IMMUNITY
                                        && you.species == SP_VINE_STALKER)
    {
        you.mutation[MUT_NO_POTION_HEAL] =
                you.innate_mutation[MUT_NO_POTION_HEAL] = 2;
    }

    if (th.getMinorVersion() < TAG_MINOR_DS_CLOUD_MUTATIONS
        && you.species == SP_DEMONSPAWN)
    {
        if (you.innate_mutation[MUT_CONSERVE_POTIONS])
        {
            // cleanup handled below
            you.mutation[MUT_FREEZING_CLOUD_IMMUNITY] =
                    you.innate_mutation[MUT_FREEZING_CLOUD_IMMUNITY] = 1;
        }
        if (you.innate_mutation[MUT_CONSERVE_SCROLLS])
        {
            // cleanup handled below
            you.mutation[MUT_FLAME_CLOUD_IMMUNITY] =
                    you.innate_mutation[MUT_FLAME_CLOUD_IMMUNITY] = 1;
        }
    }

    // Autogenerated species enums resulted in armataurs turning
    // into meteorans, or possibly mayflytaurs. Fix those, and also fix
    // the meteorans generated in the meantime.
    if (th.getMinorVersion() < TAG_MINOR_METEORAN_ENUM)
    {
        switch (you.species)
        {
        case SP_MAYFLYTAUR:
            if (you.mutation[MUT_SHORT_LIFESPAN])
            {
                final_species_cleanup = SP_METEORAN;
                break;
            }
            // fallthrough
        case SP_METEORAN:
            if (you.mutation[MUT_ROLL])
                final_species_cleanup = SP_ARMATAUR;
            break;
        case SP_ARMATAUR:
            if (you.mutation[MUT_SHORT_LIFESPAN])
                final_species_cleanup = SP_METEORAN;
            break;
        default:
            break;
        }
    }
    // The initial fix implementation turned Me->My, fix those too.
    else if (th.getMinorVersion() < TAG_MINOR_MY_ENUM)
    {
        switch (you.species)
        {
        case SP_MAYFLYTAUR:
            if (you.mutation[MUT_SHORT_LIFESPAN])
                final_species_cleanup = SP_METEORAN;
            break;
        default:
            break;
        }
    }

    // fixup trick for species-specific mutations. Not safe for mutations that
    // can also appear randomly, or have other uses. This ensures that
    // if `s` should have `m`, then it does, and also if the player has `m`
    // and is the wrong species, that they don't.
    // TODO: can we automate this from the species def?
    // (There's a lot of weird interactions and special cases to worry about..)
    #define SP_MUT_FIX(m, s) if (you.has_innate_mutation(m) || you.species == (s)) _fixup_species_mutations(m)

    SP_MUT_FIX(MUT_QUADRUMANOUS, SP_FORMICID);
    SP_MUT_FIX(MUT_NO_DRINK, SP_MUMMY);
    SP_MUT_FIX(MUT_REFLEXIVE_HEADBUTT, SP_MINOTAUR);
    SP_MUT_FIX(MUT_STEAM_RESISTANCE, SP_PALE_DRACONIAN);
    SP_MUT_FIX(MUT_PAWS, SP_FELID);
    SP_MUT_FIX(MUT_NO_GRASPING, SP_FELID);
    SP_MUT_FIX(MUT_NO_ARMOUR, SP_FELID);
    SP_MUT_FIX(MUT_MULTILIVED, SP_FELID);
    SP_MUT_FIX(MUT_CONSTRICTING_TAIL, SP_NAGA);
    SP_MUT_FIX(MUT_DISTRIBUTED_TRAINING, SP_GNOLL);
    SP_MUT_FIX(MUT_MERTAIL, SP_MERFOLK);
    SP_MUT_FIX(MUT_TENTACLE_ARMS, SP_OCTOPODE);
    SP_MUT_FIX(MUT_FLOAT, SP_DJINNI);
    SP_MUT_FIX(MUT_INNATE_CASTER, SP_DJINNI);
    SP_MUT_FIX(MUT_HP_CASTING, SP_DJINNI);
    SP_MUT_FIX(MUT_FLAT_HP, SP_DJINNI);
    SP_MUT_FIX(MUT_FORLORN, SP_DEMIGOD);
    SP_MUT_FIX(MUT_DIVINE_ATTRS, SP_DEMIGOD);
    SP_MUT_FIX(MUT_DAYSTALKER, SP_BARACHI);
    SP_MUT_FIX(MUT_TENGU_FLIGHT, SP_TENGU);
    SP_MUT_FIX(MUT_ACROBATIC, SP_TENGU);
    SP_MUT_FIX(MUT_DOUBLE_POTION_HEAL, SP_ONI);
    SP_MUT_FIX(MUT_DRUNKEN_BRAWLING, SP_ONI);
    SP_MUT_FIX(MUT_ARMOURED_TAIL, SP_ARMATAUR);

    if (you.has_innate_mutation(MUT_NIMBLE_SWIMMER)
        || you.species == SP_MERFOLK || you.species == SP_OCTOPODE)
    {
        _fixup_species_mutations(MUT_NIMBLE_SWIMMER);
    }
    if (you.species == SP_GARGOYLE || you.species == SP_MUMMY
        || you.species == SP_GHOUL)
    {
        // not safe for SP_MUT_FIX because demonspawn use this and it doesn't
        // handle ds muts
        _fixup_species_mutations(MUT_TORMENT_RESISTANCE);
    }
    if (you.species == SP_MUMMY)
    {
        // not safe for SP_MUT_FIX
        _fixup_species_mutations(MUT_HEAT_VULNERABILITY);
    }
    // not sure this is safe for SP_MUT_FIX, leaving it out for now
    if (you.species == SP_GREY_DRACONIAN || you.species == SP_GARGOYLE
        || you.species == SP_GHOUL || you.species == SP_MUMMY)
    {
        _fixup_species_mutations(MUT_UNBREATHING);
    }

    if (you.species == SP_FELID && you.has_innate_mutation(MUT_FAST))
        _fixup_species_mutations(MUT_FAST);

    if (species::is_draconian(you.species))
        _fixup_species_mutations(MUT_ARMOURED_TAIL);

    if ((you.species == SP_NAGA || you.species == SP_BARACHI)
        && you.has_innate_mutation(MUT_SLOW))
    {
        _fixup_species_mutations(MUT_SLOW);
    }

    if (you.species == SP_MUMMY || you.species == SP_POLTERGEIST
        || you.species == SP_REVENANT)
    {
        _fixup_species_mutations(MUT_ACCURSED);
    }

    #undef SP_MUT_FIX

    if (th.getMinorVersion() < TAG_MINOR_SPIT_POISON
        && you.species == SP_NAGA)
    {
        if (you.innate_mutation[MUT_SPIT_POISON] < 2)
        {
            you.mutation[MUT_SPIT_POISON] =
                    you.innate_mutation[MUT_SPIT_POISON] = 2;
        }
        // cleanup handled below
        if (you.mutation[MUT_BREATHE_POISON])
            you.mutation[MUT_SPIT_POISON] = 3;
    }

    // Give nagas constrict, tengu flight, and mummies restoration/enhancers.
    if (th.getMinorVersion() < TAG_MINOR_REAL_MUTS
        && (you.species == SP_NAGA
            || you.species == SP_TENGU
            || you.species == SP_MUMMY))
    {
        for (int xl = 2; xl <= you.experience_level; ++xl)
            give_level_mutations(you.species, xl);
    }

    if (th.getMinorVersion() < TAG_MINOR_MP_WANDS)
    {
        if (you.mutation[MUT_MP_WANDS] > 1)
            you.mutation[MUT_MP_WANDS] = 1;
    }

    if (th.getMinorVersion() < TAG_MINOR_DETERIORATION)
    {
        if (you.mutation[MUT_POOR_CONSTITUTION] > 2)
            you.mutation[MUT_POOR_CONSTITUTION] = 2;
    }

    if (th.getMinorVersion() < TAG_MINOR_BLINK_MUT)
    {
        if (you.mutation[MUT_BLINK] > 1)
            you.mutation[MUT_BLINK] = 1;
    }

    if (th.getMinorVersion() < TAG_MINOR_SPIT_POISON_AGAIN)
    {
        if (you.mutation[MUT_SPIT_POISON] > 1)
            you.mutation[MUT_SPIT_POISON] -= 1;
        // Before TAG_MINOR_SPIT_POISON_AGAIN_AGAIN this second if was missing.
        if (you.innate_mutation[MUT_SPIT_POISON] > 1)
            you.innate_mutation[MUT_SPIT_POISON] -= 1;
    }
    else if (th.getMinorVersion() < TAG_MINOR_SPIT_POISON_AGAIN_AGAIN)
    {
        // Between these two tags the value for you.innate_mutation could get
        // corrupted. No valid save after TAG_MINOR_SPIT_POISON_AGAIN should
        // have innate set to 2 for this for this mutation.

        // this doesn't correct you.mutation, because the 2,2 configuration
        // can result from two cases: (i) a save was upgraded across
        // TAG_MINOR_SPIT_POISON_AGAIN, had its mutations corrupted, and
        // then was fixed up to 2,2 on load, or (ii) a save-pre-
        // TAG_MINOR_SPIT_POISON_AGAIN had exhale poison, had 1 subtracted
        // from mutation, and ends up as 2,2. So, some lucky upgrades will get
        // exhale poison.

        if (you.innate_mutation[MUT_SPIT_POISON] == 2)
            you.innate_mutation[MUT_SPIT_POISON] = 1;
    }

    // Slow regeneration split into two single-level muts:
    // * Inhibited regeneration (no regen in los of monsters, what Gh get)
    // * No regeneration (what DDs get)
    {
        if (you.species == SP_DEEP_DWARF
            && (you.mutation[MUT_INHIBITED_REGENERATION] > 0
                || you.mutation[MUT_NO_REGENERATION] != 1))
        {
            you.innate_mutation[MUT_INHIBITED_REGENERATION] = 0;
            you.mutation[MUT_INHIBITED_REGENERATION] = 0;
            you.innate_mutation[MUT_NO_REGENERATION] = 1;
            you.mutation[MUT_NO_REGENERATION] = 1;
        }
        else if (you.species == SP_GHOUL
                 && you.mutation[MUT_INHIBITED_REGENERATION] > 1)
        {
            you.innate_mutation[MUT_INHIBITED_REGENERATION] = 1;
            you.mutation[MUT_INHIBITED_REGENERATION] = 1;
        }
        else if (you.mutation[MUT_INHIBITED_REGENERATION] > 1)
            you.mutation[MUT_INHIBITED_REGENERATION] = 1;
    }

    if (th.getMinorVersion() < TAG_MINOR_YELLOW_DRACONIAN_RACID
        && you.species == SP_YELLOW_DRACONIAN)
    {
        you.mutation[MUT_ACID_RESISTANCE] = 1;
        you.innate_mutation[MUT_ACID_RESISTANCE] = 1;
    }

    if (th.getMinorVersion() < TAG_MINOR_COMPRESS_BADMUTS)
    {
        if (you.mutation[MUT_SCREAM] > 2)
            you.mutation[MUT_SCREAM] = 2;

        if (you.species == SP_VINE_STALKER)
            _fixup_species_mutations(MUT_NO_POTION_HEAL);
        else if (you.mutation[MUT_NO_POTION_HEAL] > 2)
            you.mutation[MUT_NO_POTION_HEAL] = 2;
    }

    if (th.getMinorVersion() < TAG_MINOR_RECOMPRESS_BADMUTS)
    {
        if (you.mutation[MUT_BERSERK] > 2)
            you.mutation[MUT_BERSERK] = 2;

        if (you.mutation[MUT_TELEPORTITIS] > 2)
            you.mutation[MUT_TELEPORTITIS] = 2;
    }

    if (th.getMinorVersion() < TAG_MINOR_RAMPAGE_HEAL
        && you.species == SP_ARMATAUR)
    {
        _fixup_species_mutations(MUT_RUGGED_BROWN_SCALES);
        _fixup_species_mutations(MUT_TOUGH_SKIN);
        _fixup_species_mutations(MUT_ROLLPAGE);
        _fixup_species_mutations(MUT_AWKWARD_TONGUE);
    }

    if (th.getMinorVersion() < TAG_MINOR_COMPRESS_MAPPING)
    {
        if (you.mutation[MUT_PASSIVE_MAPPING] > 2)
            you.mutation[MUT_PASSIVE_MAPPING] = 2;
    }

    if (th.getMinorVersion() < TAG_MINOR_EXCLUSIVE_ROLLPAGE
        && you.species == SP_ARMATAUR
        && you.mutation[MUT_INHIBITED_REGENERATION] > 0)
    {
        _clear_mutation(MUT_INHIBITED_REGENERATION);
    }

    if (you.mutation[MUT_STOCHASTIC_TORMENT_RESISTANCE])
        you.mutation[MUT_TORMENT_RESISTANCE] = 1;

    _cap_mutation_at(MUT_RENOUNCE_SCROLLS, 1);
    _cap_mutation_at(MUT_RENOUNCE_POTIONS, 1);

    // fully clean up any removed mutations
    for (auto m : get_removed_mutations())
        _clear_mutation(m);

    // Fixup for Sacrifice XP from XL 27 (#9895). No minor tag, but this
    // should still be removed on a major bump.
    const int xl_remaining = you.get_max_xl() - you.experience_level;
    if (xl_remaining < 0)
        adjust_level(xl_remaining);

    if (th.getMinorVersion() < TAG_MINOR_EVOLUTION_XP)
        set_evolution_mut_xp(you.has_mutation(MUT_DEVOLUTION));
#endif

    count = unmarshallUByte(th);
    you.demonic_traits.clear();
    for (int j = 0; j < count; ++j)
    {
        player::demon_trait dt;
        dt.level_gained = unmarshallByte(th);
        ASSERT_RANGE(dt.level_gained, 1, 28);
        dt.mutation = static_cast<mutation_type>(unmarshallShort(th));
#if TAG_MAJOR_VERSION == 34
        if (dt.mutation == MUT_CONSERVE_POTIONS)
            dt.mutation = MUT_FREEZING_CLOUD_IMMUNITY;
        else if (dt.mutation == MUT_CONSERVE_SCROLLS)
            dt.mutation = MUT_FLAME_CLOUD_IMMUNITY;
#endif
        ASSERT_RANGE(dt.mutation, 0, NUM_MUTATIONS);
        you.demonic_traits.push_back(dt);
    }

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_SAC_PIETY_LEN)
    {
        const int OLD_NUM_ABILITIES = 1503;

        // set up sacrifice piety by abilities
        for (int j = 0; j < NUM_ABILITIES; ++j)
        {
            if (th.getMinorVersion() < TAG_MINOR_RU_PIETY_CONSISTENCY
                || j >= OLD_NUM_ABILITIES) // NUM_ABILITIES may have increased
            {
                you.sacrifice_piety[j] = 0;
            }
            else
                you.sacrifice_piety[j] = unmarshallUByte(th);
        }

        // If NUM_ABILITIES decreased, discard the extras.
        if (th.getMinorVersion() >= TAG_MINOR_RU_PIETY_CONSISTENCY)
        {
            for (int j = NUM_ABILITIES; j < OLD_NUM_ABILITIES; ++j)
                (void) unmarshallUByte(th);
        }
    }
    else
#endif
    {
        const int num_saved = unmarshallShort(th);

        you.sacrifice_piety.init(0);
        for (int j = 0; j < num_saved; ++j)
        {
            const int idx = ABIL_FIRST_SACRIFICE + j;
            const uint8_t val = unmarshallUByte(th);
            if (idx <= ABIL_FINAL_SACRIFICE)
                you.sacrifice_piety[idx] = val;
        }
    }

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_BANES)
    {
#endif
        count = unmarshallUByte(th);
        ASSERT(count <= NUM_BANES);
        for (int i = 0; i < count; ++i)
            you.banes[i] = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    }
#endif

    EAT_CANARY;

    // how many penances?
    count = unmarshallUByte(th);
    ASSERT(count <= NUM_GODS);
    for (int i = 0; i < count; i++)
    {
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_XP_PENANCE && i == GOD_GOZAG)
        {
            unmarshallUByte(th);
            you.penance[i] = 0;
            continue;
        }
#endif
        you.penance[i] = unmarshallUByte(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_NEMELEX_WRATH
            && player_under_penance(GOD_NEMELEX_XOBEH)
            && i == GOD_NEMELEX_XOBEH)
        {
            you.penance[i] = max(you.penance[i] - 100, 0);
        }
#endif
        ASSERT(you.penance[i] <= MAX_PENANCE);
    }

#if TAG_MAJOR_VERSION == 34
    // Fix invalid ATTR_GOD_WRATH_XP if no god is giving penance.
    // cf. 0.14-a0-2640-g5c5a558
    if (you.attribute[ATTR_GOD_WRATH_XP] != 0
        || you.attribute[ATTR_GOD_WRATH_COUNT] != 0)
    {
        god_iterator it;
        for (; it; ++it)
        {
            if (player_under_penance(*it))
                break;
        }
        if (!it)
        {
            you.attribute[ATTR_GOD_WRATH_XP] = 0;
            you.attribute[ATTR_GOD_WRATH_COUNT] = 0;
        }
    }
#endif

    for (int i = 0; i < count; i++)
        you.worshipped[i] = unmarshallByte(th);

    for (int i = 0; i < count; i++)
        you.num_current_gifts[i] = unmarshallShort(th);
    for (int i = 0; i < count; i++)
        you.num_total_gifts[i] = unmarshallShort(th);
    for (int i = 0; i < count; i++)
        you.one_time_ability_used.set(i, unmarshallBoolean(th));
    for (int i = 0; i < count; i++)
        you.piety_max[i] = unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_NEMELEX_DUNGEONS)
    {
        unmarshallByte(th);
        for (int i = 0; i < NEM_GIFT_SUMMONING; i++)
            unmarshallBoolean(th);
        unmarshallBoolean(th); // dungeons weight
        for (int i = NEM_GIFT_SUMMONING; i < NUM_NEMELEX_GIFT_TYPES; i++)
            unmarshallBoolean(th);
    }
    else if (th.getMinorVersion() < TAG_MINOR_NEMELEX_WEIGHTS)
    {
        count = unmarshallByte(th);
        ASSERT(count == NUM_NEMELEX_GIFT_TYPES);
        for (int i = 0; i < count; i++)
            unmarshallBoolean(th);
    }
#endif

    you.gift_timeout   = unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_SAVED_PIETY)
    {
        you.saved_good_god_piety = 0;
        you.previous_good_god = GOD_NO_GOD;
    }
    else
    {
#endif
    you.saved_good_god_piety = unmarshallUByte(th);
    you.previous_good_god = static_cast<god_type>(unmarshallByte(th));
#if TAG_MAJOR_VERSION == 34
    }
#endif

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_BRANCH_ENTRY)
    {
        int depth = unmarshallByte(th);
        branch_type br = static_cast<branch_type>(unmarshallByte(th));
        ASSERT(br < NUM_BRANCHES);
        brentry[BRANCH_VESTIBULE] = level_id(br, depth);
    }
#endif

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_XP_PENANCE)
    {
        for (god_iterator it; it; ++it)
        {
            if (*it == GOD_ASHENZARI)
                you.exp_docked[*it] = unmarshallInt(th);
            else
                you.exp_docked[*it] = 0;
        }
        for (god_iterator it; it; ++it)
        {
            if (*it == GOD_ASHENZARI)
                you.exp_docked_total[*it] = unmarshallInt(th);
            else
                you.exp_docked_total[*it] = 0;
        }
    }
    else
    {
#endif
    for (int i = 0; i < count; i++)
        you.exp_docked[i] = unmarshallInt(th);
    for (int i = 0; i < count; i++)
        you.exp_docked_total[i] = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    }
    if (th.getMinorVersion() < TAG_MINOR_PAKELLAS_WRATH
        && player_under_penance(GOD_PAKELLAS))
    {
        you.exp_docked[GOD_PAKELLAS] = excom_xp_docked();
        you.exp_docked_total[GOD_PAKELLAS] = you.exp_docked[GOD_PAKELLAS];
    }
    if (th.getMinorVersion() < TAG_MINOR_ELYVILON_WRATH
        && player_under_penance(GOD_ELYVILON))
    {
        you.exp_docked[GOD_ELYVILON] = excom_xp_docked();
        you.exp_docked_total[GOD_ELYVILON] = you.exp_docked[GOD_ELYVILON];
    }

#endif

    // elapsed time
    you.elapsed_time   = unmarshallInt(th);
    you.elapsed_time_at_last_input = you.elapsed_time;

    // Initialize new timers now that we know the time.
    const int last_20_turns = you.elapsed_time - (you.elapsed_time % 200);
    for (int j = timer_count; j < NUM_TIMERS; ++j)
    {
        you.last_timer_effect[j] = last_20_turns;
        you.next_timer_effect[j] = last_20_turns + 200;
    }

    // Verify that timers aren't scheduled for the past.
    for (int j = 0; j < NUM_TIMERS; ++j)
    {
        if (you.next_timer_effect[j] < you.elapsed_time)
        {
#if TAG_MAJOR_VERSION == 34
            if (th.getMinorVersion() >= TAG_MINOR_EVENT_TIMERS
                && th.getMinorVersion() < TAG_MINOR_EVENT_TIMER_FIX)
            {
                dprf("Fixing up timer %d from %d to %d",
                     j, you.next_timer_effect[j], last_20_turns + 200);
                you.last_timer_effect[j] = last_20_turns;
                you.next_timer_effect[j] = last_20_turns + 200;
            }
            else
#endif
            mprf(MSGCH_ERROR, "Timer %d next trigger in the past [%d < %d]",
                j, you.next_timer_effect[j], you.elapsed_time);
        }
    }

    // time of character creation
    you.birth_time = unmarshallInt(th);

    const int real_time  = unmarshallInt(th);
    you.real_time_ms = chrono::milliseconds(real_time * 1000);
    you.num_turns  = unmarshallInt(th);
    you.exploration = unmarshallInt(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_CONTAM_SCALE)
        you.magic_contamination = unmarshallShort(th) * 1000;
    else
#endif
        you.magic_contamination = unmarshallInt(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_CONTAM_PERCENT)
        you.magic_contamination = min(3000, you.magic_contamination / 5);
#endif

#if TAG_MAJOR_VERSION == 34
    unmarshallUByte(th);
#endif
    you.transit_stair  = unmarshallFeatureType(th);
    you.entering_level = unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_TRAVEL_ALLY_PACE
        && th.getMinorVersion() < TAG_MINOR_UNTRAVEL_ALLY_PACE)
    {
#endif
        unmarshallBoolean(th);
#if TAG_MAJOR_VERSION == 34
    }
#endif

    you.deaths = unmarshallByte(th);
    you.lives = unmarshallByte(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_LORC_TEMPERATURE &&
        th.getMinorVersion() < TAG_MINOR_NO_MORE_LORC)
    {
        // These were once the temperature fields on player for lava orcs.
        // Still need to unmarshall them from older saves.
        unmarshallFloat(th); // was you.temperature
        unmarshallFloat(th); // was you.temperature_last
    }
#endif

    you.pending_revival = !you.hp;

    EAT_CANARY;

    int n_dact = unmarshallInt(th);
    ASSERT_RANGE(n_dact, 0, 100000); // arbitrary, sanity check
    you.dactions.resize(n_dact, NUM_DACTIONS);
    for (int i = 0; i < n_dact; i++)
    {
        int a = unmarshallUByte(th);
        ASSERT(a < NUM_DACTIONS);
        you.dactions[i] = static_cast<daction_type>(a);
    }

    you.level_stack.clear();
    int n_levs = unmarshallInt(th);
    for (int k = 0; k < n_levs; k++)
    {
        level_pos pos;
        pos.load(th);
        you.level_stack.push_back(pos);
    }

    // List of currently beholding monsters (usually empty).
    count = unmarshallShort(th);
    ASSERT(count >= 0);
    for (int i = 0; i < count; i++)
    {
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_MID_BEHOLDERS)
    {
        unmarshallShort(th);
        you.duration[DUR_MESMERISED] = 0;
    }
    else
#endif
        you.beholders.push_back(unmarshall_int_as<mid_t>(th));
    }

    // Also usually empty.
    count = unmarshallShort(th);
    ASSERT(count >= 0);
    for (int i = 0; i < count; i++)
    {
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_MID_BEHOLDERS)
    {
        unmarshallShort(th);
        you.duration[DUR_AFRAID] = 0;
    }
    else
#endif
        you.fearmongers.push_back(unmarshall_int_as<mid_t>(th));
    }

    you.piety_hysteresis = unmarshallByte(th);

#if TAG_MAJOR_VERSION == 34
    you.m_quiver_history.load(th);

    if (th.getMinorVersion() < TAG_MINOR_FRIENDLY_PICKUP)
        unmarshallByte(th);
    if (th.getMinorVersion() < TAG_MINOR_NO_ZOTDEF)
        unmarshallString(th);
#endif

    EAT_CANARY;

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() == TAG_MINOR_0_11)
    {
        for (unsigned int k = 0; k < 5; k++)
            unmarshallInt(th);
    }
#endif

    // Counts of actions made, by type.
    count = unmarshallShort(th);
    for (int i = 0; i < count; i++)
    {
        caction_type caction = (caction_type)unmarshallShort(th);
        int subtype = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
        if ((th.getMinorVersion() < TAG_MINOR_ACTION_THROW
             || th.getMinorVersion() == TAG_MINOR_0_11) && caction == CACT_THROW)
        {
            subtype = subtype | (OBJ_MISSILES << 16);
        }
#endif
        for (int j = 0; j < 27; j++)
            you.action_count[make_pair(caction, subtype)][j] = unmarshallInt(th);
    }

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_WU_ABILITIES)
    {
        _move_action_count(CACT_INVOKE, CACT_ABIL, ABIL_WU_JIAN_LUNGE,
                                                   ABIL_WU_JIAN_LUNGE);
        _move_action_count(CACT_INVOKE, CACT_ABIL, ABIL_WU_JIAN_WHIRLWIND,
                                                   ABIL_WU_JIAN_WHIRLWIND);
    }
    if (th.getMinorVersion() < TAG_MINOR_ATTACK_ACTION_COUNTS)
    {
        _move_action_count(CACT_ABIL, CACT_ATTACK, ABIL_WU_JIAN_LUNGE,
                                                   ATTACK_LUNGE);
        _move_action_count(CACT_ABIL, CACT_ATTACK, ABIL_WU_JIAN_WHIRLWIND,
                                                   ATTACK_WHIRLWIND);
    }
    if (th.getMinorVersion() >= TAG_MINOR_BRANCHES_LEFT) // 33:17 has it
    {
#endif
    count = unmarshallByte(th);
    for (int i = 0; i < count; i++)
        you.branches_left.set(i, unmarshallBoolean(th));
#if TAG_MAJOR_VERSION == 34
    }
    else
    {
        // Assume all branches already exited in transferred games.
        you.branches_left.init(true);
    }
#endif

    abyssal_state.major_coord = unmarshallCoord(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_DEEP_ABYSS
        && th.getMinorVersion() != TAG_MINOR_0_11)
    {
        if (th.getMinorVersion() >= TAG_MINOR_REMOVE_ABYSS_SEED
            && th.getMinorVersion() < TAG_MINOR_ADD_ABYSS_SEED)
        {
            abyssal_state.seed = rng::get_uint32();
        }
        else
#endif
            abyssal_state.seed = unmarshallInt(th);
        abyssal_state.depth = unmarshallInt(th);
        abyssal_state.destroy_all_terrain = false;
#if TAG_MAJOR_VERSION == 34
    }
    else
    {
        unmarshallFloat(th); // converted abyssal_state.depth to int.
        abyssal_state.depth = 0;
        abyssal_state.destroy_all_terrain = true;
        abyssal_state.seed = rng::get_uint32();
    }
#endif
    abyssal_state.phase = unmarshallFloat(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_ABYSS_BRANCHES)
        abyssal_state.level = unmarshall_level_id(th);
    if (!abyssal_state.level.is_valid())
    {
        abyssal_state.level.branch = BRANCH_DEPTHS;
        abyssal_state.level.depth = 1;
    }
#else
    abyssal_state.level = unmarshall_level_id(th);
#endif

    _unmarshall_constriction(th, &you);

    you.octopus_king_rings = unmarshallUByte(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_GENERATED_MISC
        && th.getMinorVersion() < TAG_MINOR_STACKABLE_EVOKERS_TWO)
    {
        set<int> dummy;
        unmarshallSet(th, dummy, unmarshallInt);
    }
#endif

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_UNCANCELLABLES
        && th.getMinorVersion() != TAG_MINOR_0_11)
    {
#endif
    count = unmarshallUnsigned(th);
    ASSERT_RANGE(count, 0, 16); // sanity check
    you.uncancel.resize(count);
    for (int i = 0; i < count; i++)
    {
        you.uncancel[i].first = (uncancellable_type)unmarshallUByte(th);
        you.uncancel[i].second = unmarshallInt(th);
    }
#if TAG_MAJOR_VERSION == 34
    // Cancel any item-based deck manipulations
    if (th.getMinorVersion() < TAG_MINOR_REMOVE_DECKS)
    {
        erase_if(you.uncancel,
                 [](const pair<uncancellable_type, int> uc) {
                    return uc.first == UNC_DRAW_THREE
                           || uc.first == UNC_STACK_FIVE;
                });
    }
    }

    if (th.getMinorVersion() >= TAG_MINOR_INCREMENTAL_RECALL
        && th.getMinorVersion() < TAG_MINOR_NO_INCREMENTAL_RECALL)
    {
        count = unmarshallUnsigned(th);
        for (int i = 0; i < count; i++)
            unmarshallInt(th);
    }

    if (th.getMinorVersion() < TAG_MINOR_SEEDS)
    {
        // XX code duplication
        you.game_seed = rng::get_uint64();
        dprf("Upgrading from ancient unseeded game.");
        crawl_state.seed = you.game_seed;
        you.fully_seeded = false;
        you.deterministic_levelgen = false; // TAG_MINOR_INCREMENTAL_PREGEN fixup
    }
    else
    {
#endif
    count = unmarshallUByte(th);

#if TAG_MAJOR_VERSION == 34
    ASSERT(th.getMinorVersion() < TAG_MINOR_GAMESEEDS || count == 1);
    if (th.getMinorVersion() < TAG_MINOR_GAMESEEDS)
    {
        you.game_seed = count > 0 ? unmarshallInt(th) : rng::get_uint64();
        dprf("Upgrading from unseeded game.");
        crawl_state.seed = you.game_seed;
        you.fully_seeded = false;
        you.deterministic_levelgen = false; // TAG_MINOR_INCREMENTAL_PREGEN fixup
        for (int i = 1; i < count; i++)
            unmarshallInt(th);
    }
    else
    {
#endif
        // RNG block: game seed (uint64), whether the game is properly seeded,
        // and then internal RNG states stored as a vector.
        ASSERT(count == 1);
        you.game_seed = unmarshallUnsigned(th);
        dprf("Unmarshalling seed %" PRIu64, you.game_seed);
        crawl_state.seed = you.game_seed;
        you.fully_seeded = unmarshallBoolean(th);
#if TAG_MAJOR_VERSION == 34
        // there is no way to tell the levelgen method for games before this
        // tag, unfortunately. Though if there are unvisited generated levels,
        // that guarantees some form of deterministic pregen.
        if (th.getMinorVersion() < TAG_MINOR_INCREMENTAL_PREGEN)
            you.deterministic_levelgen = false;
        else
#endif
        you.deterministic_levelgen = unmarshallBoolean(th);
        CrawlVector rng_states;
        rng_states.read(th);
        rng::load_generators(rng_states);
#if TAG_MAJOR_VERSION == 34
    }
#endif

#if TAG_MAJOR_VERSION == 34
    }
#endif

    EAT_CANARY;

    if (!dlua.callfn("dgn_load_data", "u", &th))
    {
        mprf(MSGCH_ERROR, "Failed to load Lua persist table: %s",
             dlua.error.c_str());
    }

    EAT_CANARY;

    crawl_state.save_rcs_version = unmarshallString(th);

    you.props.clear();
    you.props.read(th);
#if TAG_MAJOR_VERSION == 34
    if (!you.props.exists(TIME_PER_LEVEL_KEY) && you.elapsed_time > 0)
    {
        CrawlHashTable &time_tracking = you.props[TIME_PER_LEVEL_KEY].get_table();
        time_tracking["upgrade"] = -1;
    }

    if (th.getMinorVersion() < TAG_MINOR_STICKY_FLAME)
    {
        if (you.props.exists("napalmer"))
            you.props[STICKY_FLAMER_KEY] = you.props["napalmer"];
        if (you.props.exists("napalm_aux"))
            you.props[STICKY_FLAME_AUX_KEY] = you.props["napalm_aux"];
    }

    if (you.duration[DUR_EXCRUCIATING_WOUNDS] && !you.props.exists(ORIGINAL_BRAND_KEY))
        you.props[ORIGINAL_BRAND_KEY] = SPWPN_NORMAL;

    // Both saves prior to TAG_MINOR_RU_DELAY_STACKING, and saves transferred
    // from before that tag to a version where this minor tag was backwards.
    if (!you.props.exists(RU_SACRIFICE_PENALTY_KEY))
        you.props[RU_SACRIFICE_PENALTY_KEY] = 0;
    if (th.getMinorVersion() < TAG_MINOR_ZIGFIGS)
        you.props["zig-fixup"] = true;

    // For partially used lightning rods, set the XP debt based on charges.
    if (th.getMinorVersion() < TAG_MINOR_LIGHTNING_ROD_XP_FIX
        && you.props.exists(THUNDERBOLT_CHARGES_KEY)
        && evoker_debt(MISC_LIGHTNING_ROD) == 0)
    {
        for (int i = 0; i < you.props[THUNDERBOLT_CHARGES_KEY].get_int(); i++)
            expend_xp_evoker(MISC_LIGHTNING_ROD);
    }
    if (th.getMinorVersion() < TAG_MINOR_SINGULAR_THEY
        && you.props.exists(HEPLIAKLQANA_ALLY_GENDER_KEY))
    {
        if (you.props[HEPLIAKLQANA_ALLY_GENDER_KEY].get_int() == GENDER_NEUTER)
            you.props[HEPLIAKLQANA_ALLY_GENDER_KEY] = GENDER_NEUTRAL;
    }

    if (th.getMinorVersion() < TAG_MINOR_SHAFT_CARD
            && you.props.exists(NEMELEX_STACK_KEY))
    {
        auto oldstack = you.props[NEMELEX_STACK_KEY].get_vector();
        you.props[NEMELEX_STACK_KEY].get_vector().clear();
        for (auto c : oldstack)
        {
            card_type card = static_cast<card_type>(c.get_int());
            if (card != CARD_SHAFT_REMOVED)
                you.props[NEMELEX_STACK_KEY].get_vector().push_back(card);
        }
    }

    // Appendage changed to meld, so let's untransform players who were using
    // the old one
    if (th.getMinorVersion() < TAG_MINOR_APPENDAGE
        && you.form == transformation::appendage)
    {
        you.form = transformation::none;
        you.duration[DUR_TRANSFORMATION] = 0;
        const mutation_type app = static_cast<mutation_type>(you.attribute[ATTR_UNUSED3]);
        const int levels = you.get_base_mutation_level(app);
        const int beast_levels = app == MUT_HORNS ? 2 : 3;
        // Preserve extra mutation levels acquired after transforming.
        const int extra = max(0, levels - you.get_innate_mutation_level(app)
                                        - beast_levels);
        you.mutation[app] = you.get_innate_mutation_level(app) + extra;
        you.attribute[ATTR_UNUSED3] = 0;
    }

    if (you.props.exists(WU_JIAN_HEAVENLY_STORM_KEY) && !you.duration[DUR_HEAVENLY_STORM])
    {
        mprf(MSGCH_ERROR, "Fixing up incorrect heavenly storm key");
        wu_jian_end_heavenly_storm();
    }

    if (you.props.exists("tornado_since"))
    {
        you.props[POLAR_VORTEX_KEY] = you.props["tornado_since"].get_int();
        you.props.erase("tornado_since");
    }

    if (th.getMinorVersion() < TAG_MINOR_VORTEX_POWER
        && you.duration[DUR_VORTEX])
    {
        // trying to calculate power here is scary and won't work well.
        // instead, just give em a high power vortex. let em have fun.
        // it's one vortex. how much could it cost, mennas? 20 sultanas?
        you.props[VORTEX_POWER_KEY] = 150;
    }

    initialise_item_sets();

    // ?butterflies previously alternated with ?fog. If we load such a
    // game, then make ?summoning the set choice. (Otherwise, neither
    // ?summoning nor ?butterflies will spawn!)
    if (th.getMinorVersion() < TAG_MINOR_BUTTERSUMMONS
        && item_for_set(ITEM_SET_ALLY_SCROLLS) == SCR_FOG)
    {
        force_item_set_choice(ITEM_SET_ALLY_SCROLLS, SCR_SUMMONING);
    }

    const string APPENDAGE_KEY = "beastly_appendages";
    if (you.props.exists(APPENDAGE_KEY))
    {
        for (auto mut : you.props[APPENDAGE_KEY].get_vector())
        {
            const mutation_type app = static_cast<mutation_type>(mut.get_int());
            const int levels = you.get_base_mutation_level(app);
            const int beast_lvl = app == MUT_TENTACLE_SPIKE ? 3 : 2;
            const int innate_lvl = you.get_innate_mutation_level(app);
            // Preserve extra mutation levels acquired after transforming.
            const int extra = max(0, levels - innate_lvl - beast_lvl);
            you.mutation[app] = innate_lvl + extra;
        }
        you.props.erase(APPENDAGE_KEY);
        // This leaves you in a very silly beastly appendage
        // state with no associated mutations. It's fine, it'll
        // all clear up once the form ends.
    }

    // Set up recharge info so players can actually cast the spell ever.
    if (th.getMinorVersion() < TAG_MINOR_GRAVE_CLAW_CHARGES
        && you.has_spell(SPELL_GRAVE_CLAW))
    {
        gain_grave_claw_soul(true);
    }

    // Unify handling of multiple wait spells into common attributes
    if (th.getMinorVersion() < TAG_MINOR_REFACTOR_CHANNEL_SPELLS)
    {
        const string FLAME_WAVE_KEY = "flame_waves";

        if (you.props.exists(FLAME_WAVE_KEY))
        {
            you.attribute[ATTR_CHANNELLED_SPELL] = SPELL_FLAME_WAVE;
            you.attribute[ATTR_CHANNEL_DURATION] = you.props[FLAME_WAVE_KEY].get_int();
        }
        else if (you.props.exists(COUPLING_TIME_KEY))
        {
            you.attribute[ATTR_CHANNELLED_SPELL] = SPELL_MAXWELLS_COUPLING;
            you.attribute[ATTR_CHANNEL_DURATION] = 1; // Irrelevant in this case.
            you.props[COUPLING_TIME_KEY].get_int() += you.elapsed_time - 10;
        }
        // Old Searing Ray handling
        else if (you.attribute[ATTR_CHANNEL_DURATION] != 0)
        {
            // -1 used to be special-cased for the first turn of channelling.
            // This is no longer done.
            if (you.attribute[ATTR_CHANNEL_DURATION] == -1)
                you.attribute[ATTR_CHANNEL_DURATION] = 1;

            you.attribute[ATTR_CHANNELLED_SPELL] = SPELL_SEARING_RAY;
        }
        else
        {
            you.attribute[ATTR_CHANNELLED_SPELL] = SPELL_NO_SPELL;
            you.attribute[ATTR_CHANNEL_DURATION] = 0;
        }
    }
#endif
}

#if TAG_MAJOR_VERSION == 34
/// _cleanup_book_ids handles unmarshalling of old ID data for books.
static void _cleanup_book_ids(reader &th, int n_subtypes)
{
    if (th.getMinorVersion() >= TAG_MINOR_BOOK_UNID
        || th.getMinorVersion() < TAG_MINOR_BOOK_ID)
    {
        return;
    }

    const bool ubyte = th.getMinorVersion() < TAG_MINOR_ID_STATES;
    for (int j = 0; j < n_subtypes; ++j)
    {
        if (ubyte)
            unmarshallUByte(th);
        else
            unmarshallBoolean(th);
    }
}

// Attempt to convert data loaded from the old equip slot system into valid data
// in the new one. Since there isn't an exact 1-to-1 mapping of slots, we
// simply tell the game to equip each item into the 'most appropriate' slot it
// finds for it (handling overflow slots gracefully in the process). This should
// hopefully 'just work' in basically all normal cases.
static void _convert_old_player_equipment()
{
    bool dummy;
    // Calculate current player slots first.
    you.equipment.update();
    for (int i = 0; i < (int)old_eq.size(); ++i)
    {
        // Skip empty slots.
        if (old_eq[i] == -1)
            continue;

        item_def& item = you.inv[old_eq[i]];
        equipment_slot slot = you.equipment.find_slot_to_equip_item(item, dummy);

        // This mostly handles cases of not having enough room for all rings due
        // to wearing the Macabre Finger. (The previous line should already have
        // ensured that coglins have weapons in the right places).
        if (slot == SLOT_UNUSED)
            slot = get_item_slot(item);

        // If we still don't have a proper slot for this item (probably because
        // we're upgrading the save of someone wielding a non-weapon), skip this
        // item entirely.
        if (slot == SLOT_UNUSED)
            continue;

        you.equipment.add(item, slot);

        // If the old item was melded or attuned, we need to find its entries
        // (there may be more than one of them, if it's an overflow item!) and
        // mark them appropriately.
        if (old_melded[i] || old_attuned[i])
        {
            for (player_equip_entry& entry : you.equipment.items)
            {
                if (entry.item == old_eq[i])
                {
                    if (old_melded[i])
                        entry.melded = true;
                    if (old_attuned[i])
                        entry.attuned = true;
                }
            }
        }
    }

    you.equipment.update();

    // Clear interim storage data, in case the user returns to the main menu and
    // tries to upgrade another save before quitting
    old_eq.clear();
    old_melded.clear();
    old_attuned.clear();
}

#endif

static void _tag_read_you_items(reader &th)
{
    int count, count2;

    // how many inventory slots?
    count = unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
    string bad_slots;
#endif
    for (int i = 0; i < count; ++i)
    {
        item_def &it = you.inv[i];
        unmarshallItem(th, it);
#if TAG_MAJOR_VERSION == 34
        // Fixups for actual items.
        if (it.defined())
        {
            // From 0.18-a0-273-gf174401 to 0.18-a0-290-gf199c8b, stash
            // search would change the position of items in inventory.
            if (it.pos != ITEM_IN_INVENTORY)
            {
                bad_slots += index_to_letter(i);
                it.pos = ITEM_IN_INVENTORY;
            }

            // Items in inventory have already been handled.
            if (th.getMinorVersion() < TAG_MINOR_ISFLAG_HANDLED)
                it.flags |= ISFLAG_HANDLED;
        }
#endif
    }
#if TAG_MAJOR_VERSION == 34
    if (!bad_slots.empty())
    {
        mprf(MSGCH_ERROR, "Fixed bad positions for inventory slots %s",
                          bad_slots.c_str());
    }

    if (th.getMinorVersion() < TAG_MINOR_CONSUMABLE_INV)
    {
        int consumable_slot = MAX_GEAR;
        for (int i = 0; i < MAX_GEAR; ++i)
        {
            if (inventory_category_for(you.inv[i]) == INVENT_CONSUMABLE)
            {
                you.inv[consumable_slot] = you.inv[i];
                you.inv[consumable_slot].link = consumable_slot;
                you.inv[i].clear();
                ++consumable_slot;
            }
        }
    }

    if (th.getMinorVersion() < TAG_MINOR_SAVE_TALISMANS)
        you.cur_talisman = -1;
    else if (th.getMinorVersion() < TAG_MINOR_EQUIP_TALISMAN)
    {
        item_def talisman;
        unmarshallItem(th, talisman);

        if (talisman.defined())
        {
            if (inv_count(INVENT_GEAR) < MAX_GEAR)
            {
                int slot = find_free_slot(talisman);
                you.inv[slot] = talisman;
                you.inv[slot].link = slot;
                you.inv[slot].pos = ITEM_IN_INVENTORY;
                you.cur_talisman = slot;
            }
            // In the *incredibly* unlikely case that the player is transformed via
            // a talisman they're not carrying *and* they have 52 pieces of gear in
            // their inventory, just drop the talisman at their feet.
            else
            {
                // We can't drop items on the ground at this point in loading, so
                // cache the talisman to drop it later on.
                you.props["consolation_talisman"].get_item() = talisman;
                you.cur_talisman = -1;
                you.default_form = transformation::none;
                return_to_default_form();
            }
        }
        else
            you.cur_talisman = -1;
    }
    else
#endif
        you.cur_talisman = unmarshallByte(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_EQUIP_SLOT_REWRITE)
        _convert_old_player_equipment();
#endif

    // Recalculate cached properties of equipment
    you.equipment.update();
    for (player_equip_entry& entry : you.equipment.items)
    {
        if (entry.is_overflow)
            continue;

        const item_def& item = entry.get_item();
        if (is_unrandom_artefact(item))
        {
            const unrandart_entry *u_entry = get_unrand_entry(item.unrand_idx);
            if (u_entry->world_reacts_func)
                ++you.equipment.do_unrand_reacts;
            if (u_entry->death_effects)
                ++you.equipment.do_unrand_death_effects;
        }
    }

    _unmarshallFixedBitVector<NUM_RUNE_TYPES>(th, you.runes);
    you.obtainable_runes = unmarshallByte(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_GEMS)
#endif
    {
        _unmarshallFixedBitVector<NUM_GEM_TYPES>(th, you.gems_found);
        _unmarshallFixedBitVector<NUM_GEM_TYPES>(th, you.gems_shattered);
        for (int i = 0; i < NUM_GEM_TYPES; i++)
            you.gem_time_spent[i] = unmarshallInt(th);
    }
    // Otherwise, it should be initialized to a reasonable zero value.

    // Item descrip for each type & subtype.
    // how many types?
    count = unmarshallUByte(th);
    ASSERT(count <= NUM_IDESC);
    // how many subtypes?
    count2 = unmarshallUByte(th);
    ASSERT(count2 <= MAX_SUBTYPES);
    for (int i = 0; i < count; ++i)
        for (int j = 0; j < count2; ++j)
#if TAG_MAJOR_VERSION == 34
        {
            if (th.getMinorVersion() < TAG_MINOR_CONSUM_APPEARANCE)
                you.item_description[i][j] = unmarshallUByte(th);
            else
#endif
                you.item_description[i][j] = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
            // We briefly had a problem where we sign-extended the old
            // 8-bit item_descriptions on conversion. Fix those up.
            if (th.getMinorVersion() < TAG_MINOR_NEG_IDESC
                && (int)you.item_description[i][j] < 0)
            {
                you.item_description[i][j] &= 0xff;
            }
        }
#endif
    for (int i = 0; i < count; ++i)
        for (int j = count2; j < MAX_SUBTYPES; ++j)
            you.item_description[i][j] = 0;
    int iclasses = unmarshallUByte(th);
    ASSERT(iclasses <= NUM_OBJECT_CLASSES);

    // Identification status.
    for (int i = 0; i < iclasses; ++i)
    {
        if (!item_type_has_ids((object_class_type)i))
        {
        #if TAG_MAJOR_VERSION == 34
            if (i == OBJ_BOOKS)
                _cleanup_book_ids(th, count2);
        #endif
            continue;
        }
        for (int j = 0; j < count2; ++j)
        {
#if TAG_MAJOR_VERSION == 34
            if (th.getMinorVersion() < TAG_MINOR_ID_STATES)
            {
                const uint8_t x = unmarshallUByte(th);
                ASSERT(x < NUM_ID_STATE_TYPES);
                if (x > ID_UNKNOWN_TYPE)
                    you.type_ids[i][j] = true;
                else
                    you.type_ids[i][j] = false;
            }
            else
#endif
            you.type_ids[i][j] = unmarshallBoolean(th);
        }
        for (int j = count2; j < MAX_SUBTYPES; ++j)
            you.type_ids[i][j] = false;
    }

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_ID_STATES)
    {
        CrawlHashTable old_type_id_props;
        old_type_id_props.read(th);
    }
#endif

    EAT_CANARY;

    // how many unique items?
    count = unmarshallUByte(th);
    COMPILE_CHECK(NUM_UNRANDARTS <= 256);
    for (int j = 0; j < count && j < NUM_UNRANDARTS; ++j)
    {
        you.unique_items[j] =
            static_cast<unique_item_status_type>(unmarshallByte(th));
    }
    // # of unrandarts could certainly change.
    // If it does, the new ones won't exist yet - zero them out.
    for (int j = count; j < NUM_UNRANDARTS; j++)
        you.unique_items[j] = UNIQ_NOT_EXISTS;
    for (int j = NUM_UNRANDARTS; j < count; j++)
        unmarshallByte(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_GOLDIFY_BOOKS)
    {
        // how many books?
        count = unmarshallUByte(th);
        COMPILE_CHECK(NUM_FIXED_BOOKS <= 256);
        for (int j = 0; j < count && j < NUM_FIXED_BOOKS; ++j)
            unmarshallByte(th);
        for (int j = NUM_FIXED_BOOKS; j < count; ++j)
            unmarshallByte(th);

        // how many spells?
        count = unmarshallShort(th);
        ASSERT(count >= 0);
        for (int j = 0; j < count && j < NUM_SPELLS; ++j)
            unmarshallByte(th);
        for (int j = NUM_SPELLS; j < count; ++j)
            unmarshallByte(th);
    }
#endif

    count = unmarshallShort(th);
    ASSERT(count >= 0);
    for (int j = 0; j < count && j < NUM_WEAPONS; ++j)
        you.seen_weapon[j] = unmarshallInt(th);
    for (int j = count; j < NUM_WEAPONS; ++j)
        you.seen_weapon[j] = 0;
    for (int j = NUM_WEAPONS; j < count; ++j)
        unmarshallInt(th);

    count = unmarshallShort(th);
    ASSERT(count >= 0);
    for (int j = 0; j < count && j < NUM_ARMOURS; ++j)
        you.seen_armour[j] = unmarshallInt(th);
    for (int j = count; j < NUM_ARMOURS; ++j)
        you.seen_armour[j] = 0;
    for (int j = NUM_ARMOURS; j < count; ++j)
        unmarshallInt(th);
    _unmarshallFixedBitVector<NUM_MISCELLANY>(th, you.seen_misc);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_TALISMANS_SEEN)
#endif
    _unmarshallFixedBitVector<NUM_TALISMANS>(th, you.seen_talisman);

    for (int i = 0; i < iclasses; i++)
        for (int j = 0; j < count2; j++)
            you.force_autopickup[i][j] = unmarshallInt(th);

    // preconditions: need to have read items, and you (incl props).
    you.quiver_action.load(QUIVER_MAIN_SAVE_KEY);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_MOSTLY_REMOVE_AMMO)
    {
        quiver::action_cycler ac;
        ac.load(QUIVER_LAUNCHER_SAVE_KEY);
    }

    if (th.getMinorVersion() < TAG_MINOR_FOOD_AUTOPICKUP)
    {
        const int oldstate = you.force_autopickup[OBJ_FOOD][NUM_FOODS];
        you.force_autopickup[OBJ_FOOD][FOOD_RATION] = oldstate;

        you.force_autopickup[OBJ_BOOKS][BOOK_MANUAL] =
            you.force_autopickup[OBJ_BOOKS][0];
    }
    if (th.getMinorVersion() < TAG_MINOR_FOOD_PURGE_AP_FIX)
    {
        FixedVector<int, MAX_SUBTYPES> &food_pickups =
            you.force_autopickup[OBJ_FOOD];

        // If fruit pickup was not set explicitly during the time between
        // FOOD_PURGE and FOOD_PURGE_AP_FIX, copy the old exemplar FOOD_PEAR.
        if (food_pickups[FOOD_FRUIT] == AP_FORCE_NONE)
            food_pickups[FOOD_FRUIT] = food_pickups[FOOD_PEAR];
    }

    if (th.getMinorVersion() < TAG_MINOR_CONSUM_APPEARANCE)
    {
        // merge scroll seeds
        for (int subtype = 0; subtype < MAX_SUBTYPES; subtype++)
        {
            const int seed1 = you.item_description[IDESC_SCROLLS][subtype]
                              & 0xff;
            const int seed2 = you.item_description[IDESC_SCROLLS_II][subtype]
                              & 0xff;
            const int seed3 = OBJ_SCROLLS & 0xff;
            you.item_description[IDESC_SCROLLS][subtype] =    seed1
                                                           | (seed2 << 8)
                                                           | (seed3 << 16);
        }
    }

    // Remove any decks now that items have been loaded.
    if (th.getMinorVersion() < TAG_MINOR_REMOVE_DECKS)
        reclaim_decks();

    // Reset training arrays for transferred gnolls that didn't train all skills.
    if (th.getMinorVersion() < TAG_MINOR_GNOLLS_REDUX)
        reset_training();

    // Move any books from inventory into the player's library.
    // (Likewise for manuals.)
    if (th.getMinorVersion() < TAG_MINOR_GOLDIFY_MANUALS)
        add_held_books_to_library();

    for (int i = 0; i < ENDOFPACK; ++i)
        if (you.inv[i].defined())
            ash_id_item(you.inv[i], true);

    if (you.duration[DUR_EXCRUCIATING_WOUNDS])
    {
        ASSERT(you.props.exists(ORIGINAL_BRAND_KEY));
        item_def *weapon = you.weapon();
        ASSERT(weapon);
        set_item_ego_type(*weapon, OBJ_WEAPONS, you.props[ORIGINAL_BRAND_KEY]);
        you.props.erase(ORIGINAL_BRAND_KEY);
        you.duration[DUR_EXCRUCIATING_WOUNDS] = 0;
        if (get_weapon_brand(*weapon) == SPWPN_ANTIMAGIC)
            calc_mp();
        // In principle, we should check to see if the weapon was originally
        // holy AND if you're in lich form, and unwield the weapon if so.
        // However, this is a corner case that involves a lot of scary side
        // effects while loading. Let it slide.
    }

    // See notes on TAG_MINOR_METEORAN_ENUM.
    if (final_species_cleanup != NUM_SPECIES)
        change_species_to(final_species_cleanup);
#endif
}

static PlaceInfo unmarshallPlaceInfo(reader &th)
{
    PlaceInfo place_info;

#if TAG_MAJOR_VERSION == 34
    int br = unmarshallInt(th);
    // This is for extremely old saves that predate NUM_BRANCHES, probably only
    // a very small window of time in the 34 major version.
    if (br == -1)
        br = GLOBAL_BRANCH_INFO;
    ASSERT(br >= 0);
    // at the time NUM_BRANCHES was one above BRANCH_DEPTHS, so we check that
    if (th.getMinorVersion() < TAG_MINOR_GLOBAL_BR_INFO && br == BRANCH_DEPTHS+1)
        br = GLOBAL_BRANCH_INFO;
    place_info.branch      = static_cast<branch_type>(br);
#else
    place_info.branch      = static_cast<branch_type>(unmarshallInt(th));
#endif

    place_info.num_visits  = unmarshallInt(th);
    place_info.levels_seen = unmarshallInt(th);

    place_info.mon_kill_exp       = unmarshallInt(th);

    for (int i = 0; i < KC_NCATEGORIES; i++)
        place_info.mon_kill_num[i] = unmarshallInt(th);

    place_info.turns_total      = unmarshallInt(th);
    place_info.turns_explore    = unmarshallInt(th);
    place_info.turns_travel     = unmarshallInt(th);
    place_info.turns_interlevel = unmarshallInt(th);
    place_info.turns_resting    = unmarshallInt(th);
    place_info.turns_other      = unmarshallInt(th);

    place_info.elapsed_total      = unmarshallInt(th);
    place_info.elapsed_explore    = unmarshallInt(th);
    place_info.elapsed_travel     = unmarshallInt(th);
    place_info.elapsed_interlevel = unmarshallInt(th);
    place_info.elapsed_resting    = unmarshallInt(th);
    place_info.elapsed_other      = unmarshallInt(th);

    return place_info;
}

static LevelXPInfo unmarshallLevelXPInfo(reader &th)
{
    LevelXPInfo xp_info;

    xp_info.level = unmarshall_level_id(th);

#if TAG_MAJOR_VERSION == 34
    // Track monster placement from vaults instead of tracking spawns.
    if (th.getMinorVersion() < TAG_MINOR_LEVEL_XP_VAULTS)
    {
        // Spawned/generated xp and counts have to be combined as non-vault
        // info. We have no vault info on dead monsters, so this is the best we
        // can do.
        xp_info.non_vault_xp     = unmarshallInt(th);
        xp_info.non_vault_xp    += unmarshallInt(th);
        xp_info.non_vault_count  = unmarshallInt(th);
        xp_info.non_vault_count += unmarshallInt(th);
        // turns spent on level, which we don't need.
        unmarshallInt(th);
    }
    else
    {
#endif
    xp_info.non_vault_xp    = unmarshallInt(th);
    xp_info.non_vault_count = unmarshallInt(th);
    xp_info.vault_xp        = unmarshallInt(th);
    xp_info.vault_count     = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    }
#endif

    return xp_info;
}

#if TAG_MAJOR_VERSION == 34
static branch_type old_entries[] =
{
    /* D */      NUM_BRANCHES,
    /* Temple */ BRANCH_DUNGEON,
    /* Orc */    BRANCH_DUNGEON,
    /* Elf */    BRANCH_ORC,
    /* Dwarf */  BRANCH_ELF,
    /* Lair */   BRANCH_DUNGEON,
    /* Swamp */  BRANCH_LAIR,
    /* Shoals */ BRANCH_LAIR,
    /* Snake */  BRANCH_LAIR,
    /* Spider */ BRANCH_LAIR,
    /* Slime */  BRANCH_LAIR,
    /* Vaults */ BRANCH_DUNGEON,
    /* Blade */  BRANCH_VAULTS,
    /* Crypt */  BRANCH_VAULTS,
    /* Tomb */   BRANCH_CRYPT, // or Forest
    /* Hell */   NUM_BRANCHES,
    /* Dis */    BRANCH_VESTIBULE,
    /* Geh */    BRANCH_VESTIBULE,
    /* Coc */    BRANCH_VESTIBULE,
    /* Tar */    BRANCH_VESTIBULE,
    /* Zot */    BRANCH_DUNGEON,
    /* Forest */ BRANCH_VAULTS,
    /* Abyss */  NUM_BRANCHES,
    /* Pan */    NUM_BRANCHES,
    /* various portal branches */ NUM_BRANCHES,
    NUM_BRANCHES, NUM_BRANCHES, NUM_BRANCHES, NUM_BRANCHES, NUM_BRANCHES,
    NUM_BRANCHES, NUM_BRANCHES, NUM_BRANCHES, NUM_BRANCHES, NUM_BRANCHES,
    NUM_BRANCHES,
};
#endif

static void _tag_read_you_dungeon(reader &th)
{
    // how many unique creatures?
    int count = unmarshallShort(th);
    you.unique_creatures.reset();
    for (int j = 0; j < count; ++j)
    {
        const bool created = unmarshallBoolean(th);

        if (j < NUM_MONSTERS && created)
            you.unique_creatures.set(j, created);
    }

    // how many branches?
    count = unmarshallUByte(th);
    ASSERT(count <= NUM_BRANCHES);
    for (int j = 0; j < count; ++j)
    {
        brdepth[j]    = unmarshallInt(th);
        ASSERT_RANGE(brdepth[j], -1, MAX_BRANCH_DEPTH + 1);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_BRANCH_ENTRY)
        {
            int depth = unmarshallInt(th);
            if (j != BRANCH_VESTIBULE)
                brentry[j] = level_id(old_entries[j], depth);
        }
        else
#endif
        brentry[j]    = unmarshall_level_id(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() >= TAG_MINOR_BRANCH_UNIQ_MAPS)
#endif
            unmarshallSet(th, branch_uniq_map_tags[j], unmarshallString);
#if TAG_MAJOR_VERSION == 34
        // Have to check this in case of old saves with 6-floor Depths.
        if (th.getMinorVersion() < TAG_MINOR_ZOT_ENTRY_FIXUP
            && j == BRANCH_ZOT
            && brentry[j] == level_id(BRANCH_DEPTHS, 5))
        {
            brentry[j].depth = branches[j].mindepth;
        }
        if (th.getMinorVersion() < TAG_MINOR_BRIBE_BRANCH)
            branch_bribe[j] = 0;
        else
#endif
        branch_bribe[j] = unmarshallInt(th);
    }
    // Initialize data for any branches added after this save version.
    for (int j = count; j < NUM_BRANCHES; ++j)
    {
        brdepth[j] = branches[j].numlevels;
        brentry[j] = level_id(branches[j].parent_branch, branches[j].mindepth);
        branch_bribe[j] = 0;
        branch_uniq_map_tags[j].clear();
    }

#if TAG_MAJOR_VERSION == 34
    // Deepen the Abyss; this is okay since new abyssal stairs will be
    // generated as the place shifts.
    if (crawl_state.game_is_normal() && th.getMinorVersion() <= TAG_MINOR_ABYSS_SEVEN)
        brdepth[BRANCH_ABYSS] = 7;
#endif

    ASSERT(you.depth <= brdepth[you.where_are_you]);

    // Root of the dungeon; usually BRANCH_DUNGEON.
    root_branch = static_cast<branch_type>(unmarshallInt(th));
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_BRANCH_ENTRY)
    {
        brentry[root_branch].clear();
        if (brentry[BRANCH_FOREST].is_valid())
            brentry[BRANCH_TOMB].branch = BRANCH_FOREST;
    }
#endif

    unmarshallMap(th, stair_level,
                  unmarshall_int_as<branch_type>,
                  _unmarshall_level_id_set);
    unmarshallMap(th, shops_present,
                  _unmarshall_level_pos, unmarshall_int_as<shop_type>);
    unmarshallMap(th, altars_present,
                  _unmarshall_level_pos, unmarshall_int_as<god_type>);
    unmarshallMap(th, portals_present,
                  _unmarshall_level_pos, unmarshall_int_as<branch_type>);
    unmarshallMap(th, portal_notes,
                  _unmarshall_level_pos, unmarshallString);
    unmarshallMap(th, level_annotations,
                  unmarshall_level_id, unmarshallString);
    unmarshallMap(th, level_exclusions,
                  unmarshall_level_id, unmarshallString);
    unmarshallMap(th, level_uniques,
                  unmarshall_level_id, unmarshallString);
    unmarshallUniqueAnnotations(th);

    PlaceInfo place_info = unmarshallPlaceInfo(th);
    ASSERT(place_info.is_global());
    you.set_place_info(place_info);

    unsigned short count_p = (unsigned short) unmarshallShort(th);

    auto places = you.get_all_place_info();
    // Use "<=" so that adding more branches or non-dungeon places
    // won't break save-file compatibility.
    ASSERT(count_p <= places.size());

    for (int i = 0; i < count_p; i++)
    {
        place_info = unmarshallPlaceInfo(th);
#if TAG_MAJOR_VERSION == 34
        if (place_info.is_global())
        {
            // This is to fix some crashing saves that didn't import
            // correctly, where under certain circumstances upgrading
            // a game to a version with an added branch could fail to
            // initialize the branch number. This has happened at least three
            // times now for slightly different reasons, for depths,
            // desolation, and gauntlet. The depths fixup is old enough that
            // it is handled differently.
            //
            // The basic assumption is that if a place is marked as global, it's
            // not properly initialized. The fixup assumes that logical branch
            // order (used by get_all_place_info) has not changed since the
            // save except at the end.

            const branch_type branch_to_fix = places[i].branch;
            mprf(MSGCH_ERROR,
                "Save file has uninitialized PlaceInfo for branch %s",
                branches[places[i].branch].shortname);
            // these are the known cases where this fix applies. It would
            // probably be possible to drop this ASSERT...
            ASSERT(branch_to_fix == BRANCH_DESOLATION ||
                   branch_to_fix == BRANCH_GAUNTLET);
            place_info.branch = branch_to_fix;
        }
#endif
        ASSERT(!place_info.is_global());
        you.set_place_info(place_info);
    }

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_TOMB_HATCHES)
    {
        PlaceInfo pinfo = you.get_place_info(BRANCH_TOMB);
        if (pinfo.levels_seen > 0)
            you.props[TOMB_STONE_STAIRS_KEY] = true;
    }
#endif

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_LEVEL_XP_INFO)
    {
#endif
    auto xp_info = unmarshallLevelXPInfo(th);
    ASSERT(xp_info.is_global());
    you.set_level_xp_info(xp_info);

    count_p = (unsigned short) unmarshallShort(th);
    for (int i = 0; i < count_p; i++)
    {
        xp_info = unmarshallLevelXPInfo(th);
        ASSERT(!xp_info.is_global());
        you.set_level_xp_info(xp_info);
    }
#if TAG_MAJOR_VERSION == 34
    }
#endif

    typedef pair<string_set::iterator, bool> ssipair;
    unmarshall_container(th, you.uniq_map_tags,
                         (ssipair (string_set::*)(const string &))
                         &string_set::insert,
                         unmarshallString);
    unmarshall_container(th, you.uniq_map_names,
                         (ssipair (string_set::*)(const string &))
                         &string_set::insert,
                         unmarshallString);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_ABYSS_UNIQUE_VAULTS)
    {
#endif
    unmarshall_container(th, you.uniq_map_tags_abyss,
                         (ssipair (string_set::*)(const string &))
                         &string_set::insert,
                         unmarshallString);
    unmarshall_container(th, you.uniq_map_names_abyss,
                         (ssipair (string_set::*)(const string &))
                         &string_set::insert,
                         unmarshallString);
#if TAG_MAJOR_VERSION == 34
    }
    if (th.getMinorVersion() >= TAG_MINOR_VAULT_LIST) // 33:17 has it
#endif
    unmarshallMap(th, you.vault_list, unmarshall_level_id,
                  unmarshallStringVector);

    read_level_connectivity(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_ZOT_ORB_ROTATION)
        you.zot_orb_monster = MONS_ORB_OF_FIRE;
    else
#endif
    you.zot_orb_monster = unmarshallMonType(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_ZOT_ORB_MEMORY)
        you.zot_orb_monster_known = false;
    else
#endif
    you.zot_orb_monster_known = unmarshallBoolean(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_PIETY_LOGGING)
        you.piety_info = unmarshallPietyInfo(th);
#endif
}

static void _tag_read_lost_monsters(reader &th)
{
    the_lost_ones.clear();
    unmarshallMap(th, the_lost_ones,
                  unmarshall_level_id, unmarshall_follower_list);
}

#if TAG_MAJOR_VERSION == 34
static void _tag_read_lost_items(reader &th)
{
    items_in_transit transiting_items;

    unmarshallMap(th, transiting_items,
                  unmarshall_level_id, unmarshall_item_list);
}
#endif

static void _tag_read_companions(reader &th)
{
    companion_list.clear();

    unmarshallMap(th, companion_list, unmarshall_int_as<mid_t>,
                  unmarshall_companion);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_APOSTLE_DATA)
        return;
#endif

    apostles.clear();
    int count = unmarshallByte(th);
    for (int i = 0; i < count; ++i)
        apostles.push_back(unmarshall_apostle_data(th));
}

template <typename Z>
static int _last_used_index(const Z &thinglist, int max_things)
{
    for (int i = max_things - 1; i >= 0; --i)
        if (thinglist[i].defined())
            return i + 1;
    return 0;
}

// ------------------------------- level tags ---------------------------- //

static void _tag_construct_level(writer &th)
{
    marshallByte(th, env.floor_colour);
    marshallByte(th, env.rock_colour);

    marshallInt(th, you.on_current_level ? you.elapsed_time : env.elapsed_time);
    marshallCoord(th, you.on_current_level ? you.pos() : env.old_player_pos);

    // Map grids.
    // how many X?
    marshallShort(th, GXM);
    // how many Y?
    marshallShort(th, GYM);

    marshallInt(th, env.turns_on_level);

    CANARY;

    for (int count_x = 0; count_x < GXM; count_x++)
        for (int count_y = 0; count_y < GYM; count_y++)
        {
            marshallByte(th, env.grid[count_x][count_y]);
            marshallMapCell(th, env.map_knowledge[count_x][count_y]);
            marshallInt(th, env.pgrid[count_x][count_y].flags);
        }

    marshallBoolean(th, !!env.map_forgotten);
    if (env.map_forgotten)
        for (int x = 0; x < GXM; x++)
            for (int y = 0; y < GYM; y++)
                marshallMapCell(th, (*env.map_forgotten)[x][y]);

    _run_length_encode(th, marshallByte, env.grid_colours, GXM, GYM);

    CANARY;

    // how many clouds?
    marshallShort(th, env.cloud.size());
    for (const auto& entry : env.cloud)
    {
        const cloud_struct& cloud = entry.second;
        marshallByte(th, cloud.type);
        ASSERT(cloud.type != CLOUD_NONE);
        ASSERT_IN_BOUNDS(cloud.pos);
        marshallByte(th, cloud.pos.x);
        marshallByte(th, cloud.pos.y);
        marshallShort(th, cloud.decay);
        marshallByte(th, cloud.spread_rate);
        marshallByte(th, cloud.whose);
        marshallByte(th, cloud.killer);
        marshallInt(th, cloud.source);
        marshallInt(th, cloud.excl_rad);
    }

    CANARY;

    // how many shops?
    marshallShort(th, env.shop.size());
    for (const auto& entry : env.shop)
        marshall_shop(th, entry.second);

    CANARY;

    marshallCoord(th, env.sanctuary_pos);
    marshallByte(th, env.sanctuary_time);

    env.markers.write(th);
    env.properties.write(th);

    marshallInt(th, env.dactions_done);

    // Save heightmap, if present.
    marshallByte(th, !!env.heightmap);
    if (env.heightmap)
    {
        grid_heightmap &heightmap(*env.heightmap);
        for (rectangle_iterator ri(0); ri; ++ri)
            marshallShort(th, heightmap(*ri));
    }

    CANARY;

    marshallInt(th, env.forest_awoken_until);
    marshall_level_vault_data(th);
    marshallInt(th, env.density);
}

void marshallItem(writer &th, const item_def &item, bool iinfo)
{
    marshallByte(th, item.base_type);
    if (item.base_type == OBJ_UNASSIGNED)
        return;

#if TAG_MAJOR_VERSION == 34
    if (!item.is_valid(iinfo, true))
    {
        string name;
        item_def dummy = item;
        if (!item.quantity)
        {
            name = "(quantity: 0) ";
            dummy.quantity = 1;
        }
        name += dummy.name(DESC_PLAIN, true);
        die("Invalid item: %s", name.c_str());
    }
#endif
    ASSERT(item.is_valid(iinfo));

    marshallByte(th, item.sub_type);
    marshallShort(th, item.plus);
    marshallShort(th, item.plus2);
    marshallInt(th, item.special);
    marshallShort(th, item.quantity);

    marshallByte(th, item.rnd);
    marshallShort(th, item.pos.x);
    marshallShort(th, item.pos.y);
    marshallInt(th, item.flags);

    marshallShort(th, item.link);
    if (item.pos.x >= 0 && item.pos.y >= 0)
        marshallShort(th, env.igrid(item.pos));  //  unused
    else
        marshallShort(th, -1); // unused

    marshallByte(th, item.slot);

    item.orig_place.save(th);
    marshallShort(th, item.orig_monnum);
    marshallString(th, item.inscription);

    item.props.write(th);
}

#if TAG_MAJOR_VERSION == 34
static void _trim_god_gift_inscrip(item_def& item)
{
    item.inscription = replace_all(item.inscription, "god gift, ", "");
    item.inscription = replace_all(item.inscription, "god gift", "");
    item.inscription = replace_all(item.inscription, "Psyche", "");
    item.inscription = replace_all(item.inscription, "Sonja", "");
    item.inscription = replace_all(item.inscription, "Donald", "");
}

/// Replace "dragon armour" with "dragon scales" in an artefact's name.
static void _fixup_dragon_artefact_name(item_def &item, string name_key)
{
    if (!item.props.exists(name_key))
        return;

    string &name = item.props[name_key].get_string();
    static const string to_repl = "dragon armour";
    string::size_type found = name.find(to_repl, 0);
    if (found != string::npos)
        name.replace(found, to_repl.length(), "dragon scales");
}
#endif

void unmarshallItem(reader &th, item_def &item)
{
    item.base_type   = static_cast<object_class_type>(unmarshallByte(th));
    if (item.base_type == OBJ_UNASSIGNED)
        return;
    item.sub_type    = unmarshallUByte(th);
    item.plus        = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_RUNE_TYPE
        && item.is_type(OBJ_MISCELLANY, MISC_RUNE_OF_ZOT))
    {
        item.base_type = OBJ_RUNES;
        item.sub_type = item.plus;
        item.plus = 0;
    }
    if (th.getMinorVersion() < TAG_MINOR_ZIGFIGS
        // enum was accidentally inserted in the middle
        && item.is_type(OBJ_MISCELLANY, MISC_ZIGGURAT))
    {
        item.sub_type = MISC_PHANTOM_MIRROR;
    }
#endif
    item.plus2       = unmarshallShort(th);
    item.special     = unmarshallInt(th);
    item.quantity    = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    // These used to come in stacks in monster inventory as throwing weapons.
    // Replace said stacks (but not single items) with boomerangs.
    if (item.quantity > 1 && item.base_type == OBJ_WEAPONS
        && (item.sub_type == WPN_CLUB || item.sub_type == WPN_HAND_AXE
            || item.sub_type == WPN_DAGGER || item.sub_type == WPN_SPEAR))
    {
        item.base_type = OBJ_MISSILES;
        item.sub_type = MI_BOOMERANG;
        item.plus = item.plus2 = 0;
        item.brand = SPMSL_NORMAL;
    }

    // Strip vestiges of distracting gold.
    if (item.base_type == OBJ_GOLD)
        item.special = 0;

    if (th.getMinorVersion() < TAG_MINOR_REMOVE_ITEM_COLOUR)
        /* item.colour = */ unmarshallUByte(th);
#endif

    item.rnd          = unmarshallUByte(th);

    item.pos.x       = unmarshallShort(th);
    item.pos.y       = unmarshallShort(th);
    item.flags       = unmarshallInt(th);
    item.link        = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    // ITEM_IN_SHOP was briefly NON_ITEM + NON_ITEM (1e85cf0), but that
    // doesn't fit in a short.
    if (item.link == static_cast<signed short>(54000))
        item.link = ITEM_IN_SHOP;
#endif

    unmarshallShort(th);  // env.igrid[item.x][item.y] -- unused

    item.slot        = unmarshallByte(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_PLACE_UNPACK)
    {
        unsigned short packed = unmarshallShort(th);
        if (packed == 0)
            item.orig_place.clear();
        else if (packed == 0xFFFF)
            item.orig_place = level_id(BRANCH_DUNGEON, 0);
        else
            item.orig_place = level_id::from_packed_place(packed);
    }
    else
#endif
    item.orig_place.load(th);

    item.orig_monnum = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_ORIG_MONNUM && item.orig_monnum > 0)
        item.orig_monnum--;
#endif
    item.inscription = unmarshallString(th);

    item.props.clear();
    item.props.read(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_CORPSE_COLOUR
        && item.base_type == OBJ_CORPSES
        && item.props.exists(FORCED_ITEM_COLOUR_KEY)
        && !item.props[FORCED_ITEM_COLOUR_KEY].get_int())
    {
        item.props[FORCED_ITEM_COLOUR_KEY] = LIGHTRED;
    }
#endif
    // Fixup artefact props to handle reloading items when the new version
    // of Crawl has more artefact props.
    if (is_artefact(item))
        artefact_fixup_props(item);

#if TAG_MAJOR_VERSION == 34
    // Remove artefact autoinscriptions from the saved inscription.
    if ((th.getMinorVersion() < TAG_MINOR_AUTOINSCRIPTIONS
         || th.getMinorVersion() == TAG_MINOR_0_11) && is_artefact(item))
    {
        string art_ins = artefact_inscription(item);
        if (!art_ins.empty())
        {
            item.inscription = replace_all(item.inscription, art_ins + ",", "");
            item.inscription = replace_all(item.inscription, art_ins, "");

            // Avoid q - the ring "Foo" {+Fly rF+, +Lev rF+}
            art_ins = replace_all(art_ins, "+Fly", "+Lev");
            item.inscription = replace_all(item.inscription, art_ins + ",", "");
            item.inscription = replace_all(item.inscription, art_ins, "");

            trim_string(item.inscription);
        }
    }

    if (item.base_type == OBJ_POTIONS)
    {
        switch (item.sub_type)
        {
            case POT_GAIN_STRENGTH:
            case POT_GAIN_DEXTERITY:
            case POT_GAIN_INTELLIGENCE:
            case POT_POISON:
            case POT_SLOWING:
            case POT_PORRIDGE:
            case POT_DECAY:
            case POT_WATER:
            case POT_RESTORE_ABILITIES:
            case POT_STRONG_POISON:
            case POT_BLOOD:
            case POT_BLOOD_COAGULATED:
                item.sub_type = POT_MOONSHINE;
                break;
            case POT_CURE_MUTATION:
            case POT_BENEFICIAL_MUTATION:
                item.sub_type = POT_MUTATION;
                break;
            case POT_DUMMY_AGILITY:
                item.sub_type = POT_ATTRACTION;
                break;
            default:
                break;
        }

        // Check on save load that the above switch has
        // converted all removed potion types.
        switch (item.sub_type)
        {
            default:
                break;
            CASE_REMOVED_POTIONS(item.sub_type)
        }
    }

    if (item.is_type(OBJ_STAVES, STAFF_CHANNELLING))
        item.sub_type = STAFF_ENERGY;

    if (th.getMinorVersion() < TAG_MINOR_GOD_GIFT)
    {
        _trim_god_gift_inscrip(item);
        if (is_stackable_item(item))
            origin_reset(item);
    }

    if (th.getMinorVersion() < TAG_MINOR_NO_SPLINT
        && item.base_type == OBJ_ARMOUR && item.sub_type > ARM_CHAIN_MAIL)
    {
        --item.sub_type;
    }

    if (th.getMinorVersion() < TAG_MINOR_BOX_OF_BEASTS_CHARGES
        && item.is_type(OBJ_MISCELLANY, MISC_BOX_OF_BEASTS))
    {
        // Give charges to box of beasts. If the player used it
        // already then, well, they got some freebies.
        item.plus = random_range(5, 15, 2);
    }

    if (item.is_type(OBJ_MISCELLANY, MISC_BUGGY_EBONY_CASKET))
    {
        item.sub_type = MISC_BOX_OF_BEASTS;
        item.plus = 1;
    }

    // was spiked flail
    if (item.is_type(OBJ_WEAPONS, WPN_SPIKED_FLAIL)
        && th.getMinorVersion() <= TAG_MINOR_FORGOTTEN_MAP)
    {
        item.sub_type = WPN_FLAIL;
    }

    if (item.base_type == OBJ_WEAPONS
        && (item.brand == SPWPN_RETURNING
            || item.brand == SPWPN_REACHING
            || item.brand == SPWPN_ORC_SLAYING
            || item.brand == SPWPN_DRAGON_SLAYING
            || item.brand == SPWPN_EVASION))
    {
        item.brand = SPWPN_NORMAL;
    }

    // Not putting these in a minor tag since it's possible for an old
    // random monster spawn list to place flame/frost weapons.
    if (item.base_type == OBJ_WEAPONS && get_weapon_brand(item) == SPWPN_FROST)
    {
        if (is_artefact(item))
            artefact_set_property(item, ARTP_BRAND, SPWPN_FREEZING);
        else
            item.brand = SPWPN_FREEZING;
    }
    if (item.base_type == OBJ_WEAPONS && get_weapon_brand(item) == SPWPN_FLAME)
    {
        if (is_artefact(item))
            artefact_set_property(item, ARTP_BRAND, SPWPN_FLAMING);
        else
            item.brand = SPWPN_FLAMING;
    }

    // Rescale old MR (range 35-99) to new discrete steps (40/80/120)
    // Negative MR was only supposed to exist for Folly, but paranoia.
    if (th.getMinorVersion() < TAG_MINOR_MR_ITEM_RESCALE
        && is_artefact(item)
        && item.base_type != OBJ_BOOKS
        && artefact_property(item, ARTP_WILLPOWER))
    {
        int prop_mr = artefact_property(item, ARTP_WILLPOWER);
        if (prop_mr > 99)
            artefact_set_property(item, ARTP_WILLPOWER, 3);
        else if (prop_mr > 79)
            artefact_set_property(item, ARTP_WILLPOWER, 2);
        else if (prop_mr < -40)
            artefact_set_property(item, ARTP_WILLPOWER, -2);
        else if (prop_mr < 0)
            artefact_set_property(item, ARTP_WILLPOWER, -1);
        else
            artefact_set_property(item, ARTP_WILLPOWER, 1);
    }

    // Rescale stealth (range 10..79 and -10..-98) to discrete steps (+-50/100)
    if (th.getMinorVersion() < TAG_MINOR_STEALTH_RESCALE
        && is_artefact(item)
        && item.base_type != OBJ_BOOKS)
    {
        if (artefact_property(item, ARTP_STEALTH))
        {
            int prop_st = artefact_property(item, ARTP_STEALTH);
            if (prop_st > 60)
                artefact_set_property(item, ARTP_STEALTH, 2);
            else if (prop_st < -70)
                artefact_set_property(item, ARTP_STEALTH, -2);
            else if (prop_st < 0)
                artefact_set_property(item, ARTP_STEALTH, -1);
            else
                artefact_set_property(item, ARTP_STEALTH, 1);
        }

        // Remove fast metabolism property
        if (artefact_property(item, ARTP_METABOLISM))
        {
            artefact_set_property(item, ARTP_METABOLISM, 0);
            artefact_set_property(item, ARTP_STEALTH, -1);
        }

        // Make sure no weird fake-rap combinations are produced by the upgrade
        // from rings of sustenance with {Stlth} to stealth
        if (item.base_type == OBJ_JEWELLERY && item.sub_type == RING_STEALTH)
            artefact_set_property(item, ARTP_STEALTH, 0);
    }

    if (th.getMinorVersion() < TAG_MINOR_NO_POT_FOOD)
    {
        // Replace War Chants with Battle to avoid empty-book errors.

        // Moved under TAG_MINOR_NO_POT_FOOD because it was formerly
        // not restricted to a particular range of minor tags.
        if (item.is_type(OBJ_BOOKS, BOOK_WAR_CHANTS))
            item.sub_type = BOOK_BATTLE;

        if (item.base_type == OBJ_FOOD && (item.sub_type == FOOD_UNUSED
                                           || item.sub_type == FOOD_AMBROSIA))
        {
            item.sub_type = FOOD_ROYAL_JELLY; // will be fixed up later
        }
    }

    if (th.getMinorVersion() < TAG_MINOR_FOOD_PURGE)
    {
        if (item.base_type == OBJ_FOOD)
        {
            if (item.sub_type == FOOD_SAUSAGE)
                item.sub_type = FOOD_BEEF_JERKY;
            if (item.sub_type == FOOD_CHEESE)
                item.sub_type = FOOD_PIZZA;
            if (item.sub_type == FOOD_PEAR
                || item.sub_type == FOOD_APPLE
                || item.sub_type == FOOD_CHOKO
                || item.sub_type == FOOD_APRICOT
                || item.sub_type == FOOD_ORANGE
                || item.sub_type == FOOD_BANANA
                || item.sub_type == FOOD_STRAWBERRY
                || item.sub_type == FOOD_RAMBUTAN
                || item.sub_type == FOOD_GRAPE
                || item.sub_type == FOOD_SULTANA
                || item.sub_type == FOOD_LYCHEE
                || item.sub_type == FOOD_LEMON)
            {
                item.sub_type = FOOD_FRUIT; // will be fixed up later
            }
        }
    }
    if (th.getMinorVersion() < TAG_MINOR_FOOD_PURGE_RELOADED)
    {
        if (item.base_type == OBJ_FOOD)
        {
            if (item.sub_type == FOOD_BEEF_JERKY
                || item.sub_type == FOOD_PIZZA)
            {
                item.sub_type = FOOD_ROYAL_JELLY; // will be fixed up later
            }
        }
    }

    // Combine old rings of slaying (Acc/Dam) to new (Dam).
    // Also handle the changes to the respective ARTP_.
    if (th.getMinorVersion() < TAG_MINOR_SLAYRING_PLUSES)
    {
        int acc, dam, slay = 0;

        if (item.props.exists(ARTEFACT_PROPS_KEY))
        {
            acc = artefact_property(item, ARTP_ACCURACY);
            dam = artefact_property(item, ARTP_SLAYING);
            slay = dam < 0 ? dam : max(acc, dam);

            artefact_set_property(item, ARTP_SLAYING, slay);
        }

        if (item.is_type(OBJ_JEWELLERY, RING_SLAYING))
        {
            acc = item.plus;
            dam = item.plus2;
            slay = dam < 0 ? dam : max(acc, dam);

            item.plus = slay;
            item.plus2 = 0; // probably harmless but might as well
        }
    }

    if (th.getMinorVersion() < TAG_MINOR_MERGE_EW)
    {
        // Combine EW1/EW2/EW3 scrolls into single enchant weapon scroll.
        if (item.base_type == OBJ_SCROLLS
            && (item.sub_type == SCR_ENCHANT_WEAPON_II
                || item.sub_type == SCR_ENCHANT_WEAPON_III))
        {
            item.sub_type = SCR_ENCHANT_WEAPON;
        }
    }

    if (th.getMinorVersion() < TAG_MINOR_WEAPON_PLUSES)
    {
        int acc, dam, slay = 0;

        if (item.base_type == OBJ_WEAPONS)
        {
            acc = item.plus;
            dam = item.plus2;
            slay = dam < 0 ? dam : max(acc,dam);

            item.plus = slay;
            item.plus2 = 0; // probably harmless but might as well
        }
    }

    if (th.getMinorVersion() < TAG_MINOR_SIMPLIFY_ID)
    {
        // If this item has any of the old ID flags, give it the new one.
        constexpr int OLD_ISFLAG_IDENT_MASK = 0x0000000F;
        if (item.flags & OLD_ISFLAG_IDENT_MASK)
        {
            item.flags &= ~OLD_ISFLAG_IDENT_MASK;
            item.flags |= ISFLAG_IDENTIFIED;
        }
    }

    if (th.getMinorVersion() < TAG_MINOR_CUT_CUTLASSES)
    {
        if (item.is_type(OBJ_WEAPONS, WPN_CUTLASS))
            item.sub_type = WPN_RAPIER;
    }

    if (th.getMinorVersion() < TAG_MINOR_INIT_RND)
    {
        // 0 is now reserved to indicate that rnd is uninitialized
        if (item.rnd == 0)
            item.rnd = 1 + random2(255);
    }

    if (th.getMinorVersion() < TAG_MINOR_RING_PLUSSES)
        if (item.base_type == OBJ_JEWELLERY && item.plus > 6)
            item.plus = 6;

    if (th.getMinorVersion() < TAG_MINOR_BLESSED_WPNS
        && item.base_type == OBJ_WEAPONS)
    {
        const int initial_type = item.sub_type;
        switch (item.sub_type)
        {
        case WPN_BLESSED_FALCHION:     item.sub_type = WPN_FALCHION; break;
        case WPN_BLESSED_LONG_SWORD:   item.sub_type = WPN_LONG_SWORD; break;
        case WPN_BLESSED_SCIMITAR:     item.sub_type = WPN_SCIMITAR; break;
        case WPN_BLESSED_DOUBLE_SWORD: item.sub_type = WPN_DOUBLE_SWORD; break;
        case WPN_BLESSED_GREAT_SWORD:  item.sub_type = WPN_GREAT_SWORD; break;
        case WPN_BLESSED_TRIPLE_SWORD: item.sub_type = WPN_TRIPLE_SWORD; break;
        default:                       break;
        }
        if (initial_type != item.sub_type)
            set_item_ego_type(item, OBJ_WEAPONS, SPWPN_HOLY_WRATH);
    }

    if (th.getMinorVersion() < TAG_MINOR_CONSUM_APPEARANCE)
    {
        if (item.base_type == OBJ_POTIONS)
            item.subtype_rnd = item.plus; // was consum_desc
        else if (item.base_type == OBJ_SCROLLS)
        {
            // faithfully preserve weirdness
            item.subtype_rnd = item.subtype_rnd
                               | (item.plus << 8) // was consum_desc
                               | (OBJ_SCROLLS << 16);
        }
    }

    if (th.getMinorVersion() < TAG_MINOR_MANGLE_CORPSES)
        if (item.props.exists("never_hide"))
            item.props.erase("never_hide");

    if (th.getMinorVersion() < TAG_MINOR_ISFLAG_HANDLED
        && item.flags & (ISFLAG_DROPPED | ISFLAG_THROWN))
    {
       // Items we've dropped or thrown have been handled already.
       item.flags |= ISFLAG_HANDLED;
    }

    if (th.getMinorVersion() < TAG_MINOR_UNSTACKABLE_EVOKERS
        && is_xp_evoker(item))
    {
        item.quantity = 1;
    }

    if (th.getMinorVersion() < TAG_MINOR_UNSTACK_TREMORSTONES
        && item.base_type == OBJ_MISCELLANY
        && item.sub_type == MISC_TIN_OF_TREMORSTONES)
    {
        item.quantity = 1;
    }

    if (th.getMinorVersion() < TAG_MINOR_REALLY_UNSTACK_EVOKERS
        && item.base_type == OBJ_MISCELLANY
        && (item.sub_type == MISC_PHANTOM_MIRROR
            || item.sub_type == MISC_BOX_OF_BEASTS
            || item.sub_type == MISC_SACK_OF_SPIDERS) )
    {
        item.quantity = 1;
    }

    if (th.getMinorVersion() < TAG_MINOR_NO_NEGATIVE_VULN
        && is_artefact(item)
        && item.base_type != OBJ_BOOKS
        && artefact_property(item, ARTP_NEGATIVE_ENERGY))
    {
        if (artefact_property(item, ARTP_NEGATIVE_ENERGY) < 0)
            artefact_set_property(item, ARTP_NEGATIVE_ENERGY, 0);
    }

    if (th.getMinorVersion() < TAG_MINOR_NO_RPOIS_MINUS
        && is_artefact(item)
        && item.base_type != OBJ_BOOKS
        && artefact_property(item, ARTP_POISON))
    {
        if (artefact_property(item, ARTP_POISON) < 0)
            artefact_set_property(item, ARTP_POISON, 0);
    }

    if (th.getMinorVersion() < TAG_MINOR_TELEPORTITIS
        && is_artefact(item)
        && item.base_type != OBJ_BOOKS
        && artefact_property(item, ARTP_CAUSE_TELEPORTATION) > 1)
    {
        artefact_set_property(item, ARTP_CAUSE_TELEPORTATION, 1);
    }

    if (th.getMinorVersion() < TAG_MINOR_NO_TWISTER
        && is_artefact(item)
        && item.base_type != OBJ_BOOKS
        && artefact_property(item, ARTP_TWISTER))
    {
        artefact_set_property(item, ARTP_TWISTER, 0);
    }

    if (th.getMinorVersion() < TAG_MINOR_ALCHEMY_MERGER
        && is_artefact(item)
        && item.base_type != OBJ_BOOKS
        && artefact_property(item, ARTP_ENHANCE_TMUT))
    {
        artefact_set_property(item, ARTP_ENHANCE_TMUT, 0);
    }

    // Monsters could zap wands below zero from
    // 0.17-a0-739-g965e8eb to 0.17-a0-912-g3e33c8f.
    if (item.base_type == OBJ_WANDS && item.charges < 0)
        item.charges = 0;

    // Prevent weird states for saves between UNCURSE and NEW_ASHENZARI
    if (th.getMinorVersion() < TAG_MINOR_NEW_ASHENZARI && item.cursed())
        item.flags &= (~ISFLAG_CURSED);

    // turn old hides into the corresponding armour
    static const map<int, armour_type> hide_to_armour = {
        { ARM_TROLL_HIDE,               ARM_TROLL_LEATHER_ARMOUR },
        { ARM_FIRE_DRAGON_HIDE,         ARM_FIRE_DRAGON_ARMOUR },
        { ARM_ICE_DRAGON_HIDE,          ARM_ICE_DRAGON_ARMOUR },
        { ARM_STEAM_DRAGON_HIDE,        ARM_STEAM_DRAGON_ARMOUR },
        { ARM_STORM_DRAGON_HIDE,        ARM_STORM_DRAGON_ARMOUR },
        { ARM_GOLDEN_DRAGON_HIDE,       ARM_GOLDEN_DRAGON_ARMOUR },
        { ARM_SWAMP_DRAGON_HIDE,        ARM_SWAMP_DRAGON_ARMOUR },
        { ARM_PEARL_DRAGON_HIDE,        ARM_PEARL_DRAGON_ARMOUR },
        { ARM_SHADOW_DRAGON_HIDE,       ARM_SHADOW_DRAGON_ARMOUR },
        { ARM_QUICKSILVER_DRAGON_HIDE,  ARM_QUICKSILVER_DRAGON_ARMOUR },
    };
    // ASSUMPTION: there was no such thing as an artefact hide
    if (item.base_type == OBJ_ARMOUR && hide_to_armour.count(item.sub_type))
    {
        auto subtype_ptr = map_find(hide_to_armour, item.sub_type);
        ASSERT(subtype_ptr);
        item.sub_type = *subtype_ptr;
    }

    if (th.getMinorVersion() < TAG_MINOR_HIDE_TO_SCALE && armour_is_hide(item))
    {
        _fixup_dragon_artefact_name(item, ARTEFACT_NAME_KEY);
        _fixup_dragon_artefact_name(item, ARTEFACT_APPEAR_KEY);
    }

    if (item.is_type(OBJ_FOOD, FOOD_BREAD_RATION))
        item.sub_type = FOOD_RATION;
    else if (item.is_type(OBJ_FOOD, FOOD_ROYAL_JELLY))
    {
        item.sub_type = FOOD_RATION;
        item.quantity = max(1, div_rand_round(item.quantity, 3));
    }
    else if (item.is_type(OBJ_FOOD, FOOD_FRUIT))
    {
        item.sub_type = FOOD_RATION;
        item.quantity = max(1, div_rand_round(item.quantity, 5));
    }
    if (item.is_type(OBJ_FOOD, FOOD_RATION) && item.pos == ITEM_IN_INVENTORY)
    {
        item.props[ITEM_TILE_NAME_KEY] = "food_ration_inventory";
        bind_item_tile(item);
    }

    if (th.getMinorVersion() < TAG_MINOR_THROW_CONSOLIDATION
        && item.base_type == OBJ_MISSILES)
    {
        if (item.sub_type == MI_NEEDLE)
        {
            item.sub_type = MI_DART;

            switch (item.brand)
            {
                case SPMSL_PARALYSIS:
                case SPMSL_SLOW:
                case SPMSL_SLEEP:
                case SPMSL_CONFUSION:
                case SPMSL_SICKNESS:
                    item.brand = SPMSL_BLINDING;
                    break;
                default: break;
            }
        }
        else if (item.sub_type == MI_BOOMERANG || item.sub_type == MI_JAVELIN)
        {
            switch (item.brand)
            {
                case SPMSL_RETURNING:
                case SPMSL_EXPLODING:
                case SPMSL_POISONED:
                case SPMSL_PENETRATION:
                    item.brand = SPMSL_NORMAL;
                    break;
                case SPMSL_STEEL:
                    item.brand = SPMSL_SILVER;
                    break;
            }
        }
    }

    if (th.getMinorVersion() < TAG_MINOR_BARDING_MERGE)
    {
        if (item.is_type(OBJ_ARMOUR, ARM_CENTAUR_BARDING))
            item.sub_type = ARM_BARDING;
    }
#endif

    if (is_unrandom_artefact(item))
        setup_unrandart(item, false);

#if TAG_MAJOR_VERSION == 34
    if (item.is_type(OBJ_WEAPONS, WPN_FUSTIBALUS))
        item.sub_type = WPN_HAND_CANNON;
#endif

    bind_item_tile(item);
}

#define MAP_SERIALIZE_FLAGS_MASK 3
#define MAP_SERIALIZE_FLAGS_8 1
#define MAP_SERIALIZE_FLAGS_16 2
#define MAP_SERIALIZE_FLAGS_32 3

#define MAP_SERIALIZE_FEATURE 4
#define MAP_SERIALIZE_FEATURE_COLOUR 8
#define MAP_SERIALIZE_ITEM 0x10
#define MAP_SERIALIZE_CLOUD 0x20
#define MAP_SERIALIZE_MONSTER 0x40

void marshallMapCell(writer &th, const map_cell &cell)
{
    unsigned flags = 0;

    if (cell.flags > 0xffff)
        flags |= MAP_SERIALIZE_FLAGS_32;
    else if (cell.flags > 0xff)
        flags |= MAP_SERIALIZE_FLAGS_16;
    else if (cell.flags)
        flags |= MAP_SERIALIZE_FLAGS_8;

    if (cell.feat() != DNGN_UNSEEN)
        flags |= MAP_SERIALIZE_FEATURE;

    if (cell.feat_colour())
        flags |= MAP_SERIALIZE_FEATURE_COLOUR;

    if (cell.cloud() != CLOUD_NONE)
        flags |= MAP_SERIALIZE_CLOUD;

    if (cell.item())
        flags |= MAP_SERIALIZE_ITEM;

    if (cell.monster() != MONS_NO_MONSTER)
        flags |= MAP_SERIALIZE_MONSTER;

    marshallUnsigned(th, flags);

    switch (flags & MAP_SERIALIZE_FLAGS_MASK)
    {
    case MAP_SERIALIZE_FLAGS_8:
        marshallByte(th, static_cast<int8_t>(cell.flags));
        break;
    case MAP_SERIALIZE_FLAGS_16:
        marshallShort(th, static_cast<int16_t>(cell.flags));
        break;
    case MAP_SERIALIZE_FLAGS_32:
        marshallInt(th, static_cast<int32_t>(cell.flags));
        break;
    }

    if (flags & MAP_SERIALIZE_FEATURE)
#if TAG_MAJOR_VERSION == 34
        marshallUnsigned(th, cell.feat());
#else
        marshallUByte(th, cell.feat());
#endif

    if (flags & MAP_SERIALIZE_FEATURE_COLOUR)
        marshallUnsigned(th, cell.feat_colour());

    if (feat_is_trap(cell.feat()))
        marshallByte(th, cell.trap());

    if (flags & MAP_SERIALIZE_CLOUD)
    {
        cloud_info* ci = cell.cloudinfo();
        marshallUnsigned(th, ci->type);
        marshallUnsigned(th, ci->colour);
        marshallUnsigned(th, ci->variety);
        marshallShort(th, ci->tile);
        marshallUByte(th, ci->killer);
    }

    if (flags & MAP_SERIALIZE_ITEM)
        marshallItem(th, *cell.item(), true);

    if (flags & MAP_SERIALIZE_MONSTER)
        _marshallMonsterInfo(th, *cell.monsterinfo());
}

void unmarshallMapCell(reader &th, map_cell& cell)
{
    unsigned flags = unmarshallUnsigned(th);
    unsigned cell_flags = 0;
    trap_type trap = TRAP_UNASSIGNED;

    cell.clear();

    switch (flags & MAP_SERIALIZE_FLAGS_MASK)
    {
    case MAP_SERIALIZE_FLAGS_8:
        cell_flags = static_cast<uint8_t>(unmarshallByte(th));
        break;
    case MAP_SERIALIZE_FLAGS_16:
        cell_flags = static_cast<uint16_t>(unmarshallShort(th));
        break;
    case MAP_SERIALIZE_FLAGS_32:
        cell_flags = static_cast<uint32_t>(unmarshallInt(th));
        break;
    }

    dungeon_feature_type feature = DNGN_UNSEEN;
    unsigned feat_colour = 0;

    if (flags & MAP_SERIALIZE_FEATURE)
#if TAG_MAJOR_VERSION == 34
        feature = unmarshallFeatureType_Info(th);
#else
        feature = unmarshallFeatureType(th);
#endif

    if (flags & MAP_SERIALIZE_FEATURE_COLOUR)
        feat_colour = unmarshallUnsigned(th);

    if (feat_is_trap(feature))
    {
        trap = (trap_type)unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() == TAG_MINOR_0_11 && trap >= TRAP_TELEPORT)
            trap = (trap_type)(trap - 1);
        if (trap == TRAP_ALARM)
            feature = DNGN_TRAP_ALARM;
        else if (trap == TRAP_ZOT)
            feature = DNGN_TRAP_ZOT;
        else if (trap == TRAP_GOLUBRIA)
            feature = DNGN_PASSAGE_OF_GOLUBRIA;
#endif
    }

    cell.set_feature(feature, feat_colour, trap);

    if (flags & MAP_SERIALIZE_CLOUD)
    {
        cloud_info ci;
        ci.type = (cloud_type)unmarshallUnsigned(th);
        unmarshallUnsigned(th, ci.colour);
        unmarshallUnsigned(th, ci.variety);
        ci.tile = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() >= TAG_MINOR_CLOUD_OWNER)
#endif
        ci.killer = static_cast<killer_type>(unmarshallUByte(th));
        cell.set_cloud(ci);
    }

    if (flags & MAP_SERIALIZE_ITEM)
    {
        item_def item;
        unmarshallItem(th, item);
        cell.set_item(item, false);
    }

    if (flags & MAP_SERIALIZE_MONSTER)
    {
        monster_info mi;
        _unmarshallMonsterInfo(th, mi);
        cell.set_monster(mi);
    }

    // set this last so the other sets don't override this
    cell.flags = cell_flags;
}

static void _tag_construct_level_items(writer &th)
{
    // how many traps?
    marshallShort(th, env.trap.size());
    for (const auto& entry : env.trap)
    {
        const trap_def& trap = entry.second;
        marshallByte(th, trap.type);
        marshallCoord(th, trap.pos);
        marshallShort(th, trap.ammo_qty);
    }

    // how many items?
    const int ni = _last_used_index(env.item, MAX_ITEMS);
    marshallShort(th, ni);
    for (int i = 0; i < ni; ++i)
        marshallItem(th, env.item[i]);
}

static void marshall_mon_enchant(writer &th, const mon_enchant &me)
{
    marshallShort(th, me.ench);
    marshallShort(th, me.degree);
    marshallShort(th, me.who);
    marshallInt(th, me.source);
    marshallShort(th, min(me.duration, INFINITE_DURATION));
    marshallShort(th, min(me.maxduration, INFINITE_DURATION));
    marshallByte(th, me.ench_is_aura);
}

static mon_enchant unmarshall_mon_enchant(reader &th)
{
    mon_enchant me;
    me.ench        = static_cast<enchant_type>(unmarshallShort(th));
    me.degree      = unmarshallShort(th);
    me.who         = static_cast<kill_category>(unmarshallShort(th));
    me.source      = unmarshallInt(th);
    me.duration    = unmarshallShort(th);
    me.maxduration = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_MON_AURA_REFACTORING)
        me.ench_is_aura = static_cast<ench_aura_type>(unmarshallByte(th));

    if (th.getMinorVersion() < TAG_MINOR_NO_FAKE_ABJ
        && me.ench == ENCH_FAKE_ABJURATION || me.ench == ENCH_SHORT_LIVED)
    {
        me.ench = ENCH_SUMMON_TIMER;
    }
#endif
    return me;
}

enum mon_part_t
{
    MP_GHOST_DEMON      = BIT(0),
    MP_CONSTRICTION     = BIT(1),
    MP_ITEMS            = BIT(2),
    MP_SPELLS           = BIT(3),
};

void marshallMonster(writer &th, const monster& m)
{
    if (!m.alive())
    {
        marshallMonType(th, MONS_NO_MONSTER);
        return;
    }

    uint32_t parts = 0;
    if (mons_is_ghost_demon(m.type))
        parts |= MP_GHOST_DEMON;
    if (m.is_constricted() || m.is_constricting())
        parts |= MP_CONSTRICTION;
    for (int i = 0; i < NUM_MONSTER_SLOTS; i++)
        if (m.inv[i] != NON_ITEM)
            parts |= MP_ITEMS;
    if (m.spells.size() > 0)
        parts |= MP_SPELLS;

    marshallMonType(th, m.type);
    marshallUnsigned(th, parts);
    ASSERT(m.mid > 0);
    marshallInt(th, m.mid);
    marshallString(th, m.mname);
    marshallByte(th, m.xp_tracking);
    marshallByte(th, m.get_experience_level());
    marshallByte(th, m.speed);
    marshallByte(th, m.speed_increment);
    marshallByte(th, m.behaviour);
    marshallByte(th, m.pos().x);
    marshallByte(th, m.pos().y);
    marshallByte(th, m.target.x);
    marshallByte(th, m.target.y);
    marshallCoord(th, m.firing_pos);
    marshallCoord(th, m.patrol_point);
    int help = m.travel_target;
    marshallByte(th, help);

    marshallShort(th, m.travel_path.size());
    for (coord_def pos : m.travel_path)
        marshallCoord(th, pos);

    marshallUnsigned(th, m.flags.flags);

    marshallShort(th, m.enchantments.size());
    for (const auto &entry : m.enchantments)
        marshall_mon_enchant(th, entry.second);
    marshallByte(th, m.ench_countdown);

    marshallShort(th, min(m.hit_points, MAX_MONSTER_HP));
    marshallShort(th, min(m.max_hit_points, MAX_MONSTER_HP));
    marshallInt(th, m.exp);
    marshallInt(th, m.number);
    marshallMonType(th, m.base_monster);
    marshallShort(th, m.colour);
    marshallInt(th, m.summoner);

    if (parts & MP_ITEMS)
        for (int j = 0; j < NUM_MONSTER_SLOTS; j++)
            marshallShort(th, m.inv[j]);
    if (parts & MP_SPELLS)
        _marshallSpells(th, m.spells);
    marshallByte(th, m.god);
    marshallByte(th, m.attitude);
    marshallShort(th, m.foe);
    marshallInt(th, m.foe_memory);
    marshallShort(th, m.damage_friendly);
    marshallShort(th, m.damage_total);
    marshallByte(th, m.revealed_this_turn);
    marshallCoord(th, m.revealed_at_pos);
    marshall_level_id(th, m.origin_level);

    if (parts & MP_GHOST_DEMON)
    {
        // *Must* have ghost field set.
        ASSERT(m.ghost);
        _marshallGhost(th, *m.ghost);
    }

    if (parts & MP_CONSTRICTION)
        _marshall_constriction(th, &m);

    m.props.write(th);
}

static void _marshall_mi_attack(writer &th, const mon_attack_def &attk)
{
    marshallInt(th, attk.type);
    marshallInt(th, attk.flavour);
    marshallInt(th, attk.damage);
}

static mon_attack_def _unmarshall_mi_attack(reader &th)
{
    mon_attack_def attk;
    attk.type = static_cast<attack_type>(unmarshallInt(th));
    attk.flavour = static_cast<attack_flavour>(unmarshallInt(th));
    attk.damage = unmarshallInt(th);

    return attk;
}

void _marshallMonsterInfo(writer &th, const monster_info& mi)
{
    _marshallFixedBitVector<NUM_MB_FLAGS>(th, mi.mb);
    marshallString(th, mi.mname);
    marshallUnsigned(th, mi.type);
    marshallUnsigned(th, mi.base_type);
    marshallUnsigned(th, mi.number);
    marshallInt(th, mi._colour);
    marshallUnsigned(th, mi.attitude);
    marshallUnsigned(th, mi.threat);
    marshallUnsigned(th, mi.dam);
    marshallUnsigned(th, mi.fire_blocker);
    marshallUnsigned(th, mi.holi.flags);
    marshallUnsigned(th, mi.mintel);
    marshallUnsigned(th, mi.hd);
    marshallUnsigned(th, mi.ac);
    marshallUnsigned(th, mi.ev);
    marshallUnsigned(th, mi.base_ev);
    marshallUnsigned(th, mi.sh);
    marshallUnsigned(th, mi.wl);
    marshallUnsigned(th, mi.slay);
    marshallInt(th, mi.mresists);
    marshallUnsigned(th, mi.mitemuse);
    marshallByte(th, mi.mbase_speed);
    marshallByte(th, mi.menergy.move);
    marshallByte(th, mi.menergy.swim);
    marshallByte(th, mi.menergy.attack);
    marshallByte(th, mi.menergy.missile);
    marshallByte(th, mi.menergy.spell);
    for (int i = 0; i < MAX_NUM_ATTACKS; ++i)
        _marshall_mi_attack(th, mi.attack[i]);
    for (unsigned int i = 0; i <= MSLOT_LAST_VISIBLE_SLOT; ++i)
    {
        if (mi.inv[i])
        {
            marshallBoolean(th, true);
            marshallItem(th, *mi.inv[i], true);
        }
        else
            marshallBoolean(th, false);
    }
    if (mons_is_pghost(mi.type))
    {
        marshallUnsigned(th, mi.i_ghost.species);
        marshallUnsigned(th, mi.i_ghost.job);
        marshallUnsigned(th, mi.i_ghost.religion);
        marshallUnsigned(th, mi.i_ghost.best_skill);
        marshallShort(th, mi.i_ghost.best_skill_rank);
        marshallShort(th, mi.i_ghost.xl_rank);
        marshallShort(th, mi.i_ghost.damage);
        marshallShort(th, mi.i_ghost.ac);
        marshallString(th, mi.i_ghost.title);
    }

    mi.props.write(th);
}

void _unmarshallMonsterInfo(reader &th, monster_info& mi)
{
    _unmarshallFixedBitVector<NUM_MB_FLAGS>(th, mi.mb);
    mi.mname = unmarshallString(th);

    mi.type = unmarshallMonType_Info(th);
    ASSERT(!invalid_monster_type(mi.type));
    mi.base_type = unmarshallMonType_Info(th);

#if TAG_MAJOR_VERSION == 34
    if ((mons_genus(mi.type) == MONS_DRACONIAN
        || (mons_genus(mi.type) == MONS_DEMONSPAWN
            && th.getMinorVersion() >= TAG_MINOR_DEMONSPAWN))
        && th.getMinorVersion() < TAG_MINOR_NO_DRACO_TYPE)
    {
        unmarshallMonType_Info(th); // was draco_type
    }
#endif

    unmarshallUnsigned(th, mi.number);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_MON_COLOUR_LOOKUP)
        mi._colour = int(unmarshallUnsigned(th));
    else
#endif
        mi._colour = unmarshallInt(th);
    unmarshallUnsigned(th, mi.attitude);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_CUT_STRICT_NEUTRAL
        && mi.attitude == ATT_OLD_STRICT_NEUTRAL)
    {
        mi.attitude = ATT_GOOD_NEUTRAL;
    }
#endif

    unmarshallUnsigned(th, mi.threat);
    unmarshallUnsigned(th, mi.dam);
    unmarshallUnsigned(th, mi.fire_blocker);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_MONINFO_CLEANUP)
    {
        unmarshallString(th);
        unmarshallString(th);
    }
#endif

    uint64_t holi_flags = unmarshallUnsigned(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_MULTI_HOLI)
    {
#endif
        mi.holi.flags = holi_flags;
#if TAG_MAJOR_VERSION == 34
    }
    else
        mi.holi.flags = 1<<holi_flags;
#endif

#if TAG_MAJOR_VERSION == 34
    // XXX: special case MH_UNDEAD becoming MH_UNDEAD | MH_NATURAL
    // to save MF_FAKE_UNDEAD. Beware if you add a NATURAL bit
    // to an undead monster.
    if (mons_class_holiness(mi.type) & ~mi.holi
        && !(mi.holi & MH_UNDEAD) && !(mons_class_holiness(mi.type) & MH_NATURAL))
    {
        mi.holi |= mons_class_holiness(mi.type);
    }
#endif

    unmarshallUnsigned(th, mi.mintel);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_MON_HD_INFO)
    {
#endif
        unmarshallUnsigned(th, mi.hd);
#if TAG_MAJOR_VERSION == 34
    }
    else
        mi.hd = mons_class_hit_dice(mi.type);

    if (th.getMinorVersion() >= TAG_MINOR_DISPLAY_MON_AC_EV)
    {
#endif
        unmarshallUnsigned(th, mi.ac);
        unmarshallUnsigned(th, mi.ev);
        unmarshallUnsigned(th, mi.base_ev);
#if TAG_MAJOR_VERSION == 34
    }
    else
    {
        mi.ac = get_mons_class_ac(mi.type);
        mi.ev = mi.base_ev = get_mons_class_ev(mi.type);
    }
#endif

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_MON_SH_INFO)
        unmarshallUnsigned(th, mi.sh);
    else
#endif
        mi.sh = 0;

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_MONINFO_CLEANUP)
        mi.wl = mons_class_willpower(mi.type, mi.base_type);
    else
#endif
    unmarshallUnsigned(th, mi.wl);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_MONINFO_CLEANUP)
        mi.slay = 0;
    else
#endif
    unmarshallUnsigned(th, mi.slay);

    mi.mresists = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    if (mi.mresists & MR_OLD_RES_ACID)
        set_resist(mi.mresists, MR_RES_CORR, 3);
#endif
    unmarshallUnsigned(th, mi.mitemuse);
    mi.mbase_speed = unmarshallByte(th);

#if TAG_MAJOR_VERSION == 34
    // See comment in unmarshallMonster(): this could be an elemental
    // wellspring masquerading as a spectral weapon, or a polymoth masquerading
    // as a wellspring.
    if (th.getMinorVersion() < TAG_MINOR_CANARIES
        && th.getMinorVersion() >= TAG_MINOR_WAR_DOG_REMOVAL
        && mi.type >= MONS_SPECTRAL_WEAPON
        && mi.type <= MONS_POLYMOTH)
    {
        switch (mi.base_speed())
        {
        case 10:
            mi.type = MONS_ELEMENTAL_WELLSPRING;
            break;
        case 12:
            mi.type = MONS_POLYMOTH;
            break;
        case 25:
        case 30:
            mi.type = MONS_SPECTRAL_WEAPON;
            break;
        default:
            die("Unexpected monster_info with type %d and speed %d",
                mi.type, mi.base_speed());
        }
    }

    // As above; this could be one of several monsters.
    if (th.getMinorVersion() < TAG_MINOR_DEMONSPAWN
        && mi.type >= MONS_MONSTROUS_DEMONSPAWN
        && mi.type <= MONS_SALAMANDER_MYSTIC)
    {
        switch (mi.colour(true))
        {
        case BROWN:        // monstrous demonspawn, naga ritualist
            if (mi.spells[0].spell == SPELL_FORCE_LANCE)
                mi.type = MONS_NAGA_RITUALIST;
            else
                mi.type = MONS_MONSTROUS_DEMONSPAWN;
            break;
        case BLUE:         // gelid demonspawn
            mi.type = MONS_GELID_DEMONSPAWN;
            break;
        case RED:          // infernal demonspawn
            mi.type = MONS_INFERNAL_DEMONSPAWN;
            break;
        case LIGHTGRAY:    // torturous demonspawn, naga sharpshooter
            if (mi.spells[0].spell == SPELL_PORTAL_PROJECTILE)
                mi.type = MONS_NAGA_SHARPSHOOTER;
            else
                mi.type = MONS_TORTUROUS_DEMONSPAWN;
            break;
        case LIGHTBLUE:    // blood saint, shock serpent
            if (mi.base_type != MONS_NO_MONSTER)
                mi.type = MONS_DEMONSPAWN_BLOOD_SAINT;
            else
                mi.type = MONS_SHOCK_SERPENT;
            break;
        case LIGHTCYAN:    // warmonger, drowned soul
            if (mi.base_type != MONS_NO_MONSTER)
                mi.type = MONS_DEMONSPAWN_WARMONGER;
            else
                mi.type = MONS_DROWNED_SOUL;
            break;
        case LIGHTGREEN:   // corrupter
            mi.type = MONS_DEMONSPAWN_CORRUPTER;
            break;
        case LIGHTMAGENTA: // soul scholar
            mi.type = MONS_DEMONSPAWN_SOUL_SCHOLAR;
            break;
        case CYAN:         // worldbinder
            mi.type = MONS_WORLDBINDER;
            break;
        case MAGENTA:      // vine stalker, mana viper, grand avatar
            if (mi.base_speed() == 30)
                mi.type = MONS_GRAND_AVATAR;
            else
                mi.type = MONS_MANA_VIPER;
            break;
        case WHITE:        // salamander firebrand
            mi.type = MONS_SALAMANDER_FIREBRAND;
            break;
        case YELLOW:       // salamander mystic
            mi.type = MONS_SALAMANDER_MYSTIC;
            break;
        default:
            die("Unexpected monster with type %d and colour %d",
                mi.type, mi.colour(true));
        }
    }

    if (th.getMinorVersion() < TAG_MINOR_MONINFO_ENERGY)
        mi.menergy = mons_class_energy(mi.type);
    else
    {
#endif
    mi.menergy.move = unmarshallByte(th);
    mi.menergy.swim = unmarshallByte(th);
    mi.menergy.attack = unmarshallByte(th);
    mi.menergy.missile = unmarshallByte(th);
    mi.menergy.spell = unmarshallByte(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_NO_SPECIAL_ENERGY)
    {
        unmarshallByte(th); // special
        unmarshallByte(th); // item
        unmarshallByte(th); // pickup_percent
    }
    }
#endif

    // Some TAG_MAJOR_VERSION == 34 saves suffered data loss here, beware.
    // Should be harmless, hopefully.
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_BOOL_FLIGHT)
        unmarshallUnsigned(th);
#endif
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_ATTACK_DESCS)
    {
        for (int i = 0; i < MAX_NUM_ATTACKS; ++i)
        {
            mi.attack[i] = get_monster_data(mi.type)->attack[i];
            mi.attack[i].damage = 0;
        }
    }
    else
#endif
    for (int i = 0; i < MAX_NUM_ATTACKS; ++i)
        mi.attack[i] = _unmarshall_mi_attack(th);

    for (unsigned int i = 0; i <= MSLOT_LAST_VISIBLE_SLOT; ++i)
    {
        if (unmarshallBoolean(th))
        {
            mi.inv[i].reset(new item_def());
            unmarshallItem(th, *mi.inv[i]);
        }
    }

    if (mons_is_pghost(mi.type))
    {
        unmarshallUnsigned(th, mi.i_ghost.species);
        unmarshallUnsigned(th, mi.i_ghost.job);
        unmarshallUnsigned(th, mi.i_ghost.religion);
        unmarshallUnsigned(th, mi.i_ghost.best_skill);
        mi.i_ghost.best_skill_rank = unmarshallShort(th);
        mi.i_ghost.xl_rank = unmarshallShort(th);
        mi.i_ghost.damage = unmarshallShort(th);
        mi.i_ghost.ac = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_GHOST_TITLE)
            mi.i_ghost.title = "";
        else
#endif
            unmarshallString(th);
    }
#if TAG_MAJOR_VERSION == 34
    if ((mons_is_ghost_demon(mi.type)
         || (mi.type == MONS_LICH || mi.type == MONS_ANCIENT_LICH
             || mi.type == MONS_SPELLSPARK_SERVITOR)
            && th.getMinorVersion() < TAG_MINOR_EXORCISE)
        && th.getMinorVersion() >= TAG_MINOR_GHOST_SINV
        && th.getMinorVersion() < TAG_MINOR_GHOST_NOSINV)
    {
        unmarshallBoolean(th); // was can_sinv
    }
#endif

    mi.props.clear();
    mi.props.read(th);

#if TAG_MAJOR_VERSION == 34
    if (mi.props.exists(MONSTER_TILE_KEY)
        && mi.props[MONSTER_TILE_KEY].get_type() == SV_SHORT)
    {
        mi.props[MONSTER_TILE_KEY].get_int() = mi.props[MONSTER_TILE_KEY];
    }

    if (mi.type == MONS_ZOMBIE_SMALL || mi.type == MONS_ZOMBIE_LARGE)
        mi.type = MONS_ZOMBIE;
    if (mi.type == MONS_SKELETON_SMALL || mi.type == MONS_SKELETON_LARGE)
        mi.type = MONS_DRAUGR;
    if (mi.type == MONS_SIMULACRUM_SMALL || mi.type == MONS_SIMULACRUM_LARGE)
        mi.type = MONS_SIMULACRUM;
    if (th.getMinorVersion() < TAG_MINOR_WAR_DOG_REMOVAL)
    {
        if (mi.type == MONS_WAR_DOG)
            mi.type = MONS_WOLF;
    }
#endif

    if (mons_is_removed(mi.type))
    {
        mi.type = MONS_GHOST;
        mi.props.clear();
    }
}

static void _tag_construct_level_monsters(writer &th)
{
    int nm = 0;
    for (int i = 0; i < MAX_MONS_ALLOC; ++i)
        if (env.mons_alloc[i] != MONS_NO_MONSTER)
            nm = i + 1;

    // how many mons_alloc?
    marshallByte(th, nm);
    for (int i = 0; i < nm; ++i)
        marshallMonType(th, env.mons_alloc[i]);

    // how many monsters?
    nm = _last_used_index(env.mons, MAX_MONSTERS);
    marshallShort(th, nm);

    for (int i = 0; i < nm; i++)
    {
        monster& m(env.mons[i]);

#if defined(DEBUG) || defined(DEBUG_MONS_SCAN)
        if (m.type != MONS_NO_MONSTER)
        {
            if (invalid_monster_type(m.type))
            {
                mprf(MSGCH_ERROR, "Marshalled monster #%d %s",
                     i, m.name(DESC_PLAIN, true).c_str());
            }
            if (!in_bounds(m.pos()))
            {
                mprf(MSGCH_ERROR,
                     "Marshalled monster #%d %s out of bounds at (%d, %d)",
                     i, m.name(DESC_PLAIN, true).c_str(),
                     m.pos().x, m.pos().y);
            }
        }
#endif
        marshallMonster(th, m);
    }
}

void _tag_construct_level_tiles(writer &th)
{
    // Map grids.
    // how many X?
    marshallShort(th, GXM);
    // how many Y?
    marshallShort(th, GYM);

    marshallShort(th, tile_env.names.size());
    for (const string &name : tile_env.names)
    {
        marshallString(th, name);
#ifdef DEBUG_TILE_NAMES
        mprf("Writing '%s' into save.", name.c_str());
#endif
    }

    // flavour
    marshallShort(th, tile_env.default_flavour.wall_idx);
    marshallShort(th, tile_env.default_flavour.floor_idx);

    marshallShort(th, tile_env.default_flavour.wall);
    marshallShort(th, tile_env.default_flavour.floor);
    marshallShort(th, tile_env.default_flavour.special);

    for (int count_x = 0; count_x < GXM; count_x++)
        for (int count_y = 0; count_y < GYM; count_y++)
        {
            marshallShort(th, tile_env.flv[count_x][count_y].wall_idx);
            marshallShort(th, tile_env.flv[count_x][count_y].floor_idx);
            marshallShort(th, tile_env.flv[count_x][count_y].feat_idx);

            marshallShort(th, tile_env.flv[count_x][count_y].wall);
            marshallShort(th, tile_env.flv[count_x][count_y].floor);
            marshallShort(th, tile_env.flv[count_x][count_y].feat);
            marshallShort(th, tile_env.flv[count_x][count_y].special);
        }

    marshallInt(th, TILE_WALL_MAX);
}

#if TAG_MAJOR_VERSION == 34
static void _fixup_blood_knowledge(MapKnowledge& map_knowledge)
{
    for (rectangle_iterator ri(0); ri; ++ri)
    {
        constexpr uint32_t blood_flags = MAP_BLOOD_WEST | MAP_BLOOD_NORTH
                                         | MAP_OLD_BLOOD;
        map_knowledge(*ri).flags &= ~blood_flags;
        if (map_knowledge(*ri).flags & MAP_BLOODY)
        {
            if (testbits(env.pgrid(*ri), FPROP_BLOOD_WEST))
                map_knowledge(*ri).flags |= MAP_BLOOD_WEST;
            if (testbits(env.pgrid(*ri), FPROP_BLOOD_NORTH))
                map_knowledge(*ri).flags |= MAP_BLOOD_NORTH;
            if (testbits(env.pgrid(*ri), FPROP_OLD_BLOOD))
                map_knowledge(*ri).flags |= MAP_OLD_BLOOD;
        }
    }
}

static void _fixup_cloud_varieties(MapKnowledge& map_knowledge)
{
    for (rectangle_iterator ri(0); ri; ++ri)
    {
        cloud_info* ci = map_knowledge(*ri).cloudinfo();
        if (ci && ci->type == CLOUD_VORTEX)
            ci->variety = get_vortex_phase(*ri);
    }
}
#endif

static void _tag_read_level(reader &th)
{
    env.floor_colour = unmarshallUByte(th);
    env.rock_colour  = unmarshallUByte(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_NO_LEVEL_FLAGS)
        unmarshallInt(th);
#endif

    env.elapsed_time = unmarshallInt(th);
    env.old_player_pos = unmarshallCoord(th);
    env.absdepth0 = absdungeon_depth(you.where_are_you, you.depth);

    // Map grids.
    // how many X?
    const int gx = unmarshallShort(th);
    // how many Y?
    const int gy = unmarshallShort(th);
    ASSERT(gx == GXM);
    ASSERT(gy == GYM);

    env.turns_on_level = unmarshallInt(th);

    EAT_CANARY;

    env.map_seen.reset();
#if TAG_MAJOR_VERSION == 34
    vector<coord_def> transporters;
#endif
    for (int i = 0; i < gx; i++)
        for (int j = 0; j < gy; j++)
        {
            dungeon_feature_type feat = unmarshallFeatureType(th);
            env.grid[i][j] = feat;
            ASSERT(feat < NUM_FEATURES);

#if TAG_MAJOR_VERSION == 34
            // Save these for potential destination clean up.
            if (env.grid[i][j] == DNGN_TRANSPORTER)
                transporters.push_back(coord_def(i, j));
#endif
            unmarshallMapCell(th, env.map_knowledge[i][j]);
            // Fixup positions
            if (env.map_knowledge[i][j].monsterinfo())
                env.map_knowledge[i][j].monsterinfo()->pos = coord_def(i, j);
            if (env.map_knowledge[i][j].cloudinfo())
                env.map_knowledge[i][j].cloudinfo()->pos = coord_def(i, j);

            env.map_knowledge[i][j].flags &= ~MAP_VISIBLE_FLAG;
            if (env.map_knowledge[i][j].seen())
                env.map_seen.set(i, j);
            env.pgrid[i][j].flags = unmarshallInt(th);

            env.mgrid[i][j] = NON_MONSTER;
        }

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_FIX_BLOOD_KNOWLEDGE)
        _fixup_blood_knowledge(env.map_knowledge);
    if (th.getMinorVersion() <= TAG_MINOR_FIX_POLAR_VORTEX_INFO_LEAK)
        _fixup_cloud_varieties(env.map_knowledge);
#endif

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_FORGOTTEN_MAP)
        env.map_forgotten.reset();
    else
#endif
    if (unmarshallBoolean(th))
    {
        MapKnowledge *f = new MapKnowledge();
        for (int x = 0; x < GXM; x++)
            for (int y = 0; y < GYM; y++)
                unmarshallMapCell(th, (*f)[x][y]);

#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_FIX_BLOOD_KNOWLEDGE)
            _fixup_blood_knowledge(*f);
        if (th.getMinorVersion() <= TAG_MINOR_FIX_POLAR_VORTEX_INFO_LEAK)
            _fixup_cloud_varieties(*f);
#endif
        env.map_forgotten.reset(f);
    }
    else
        env.map_forgotten.reset();

    env.grid_colours.init(BLACK);
    _run_length_decode(th, unmarshallByte, env.grid_colours, GXM, GYM);

    EAT_CANARY;

    env.cloud.clear();
    // how many clouds?
    const int num_clouds = unmarshallShort(th);
    cloud_struct cloud;
    for (int i = 0; i < num_clouds; i++)
    {
        cloud.type  = static_cast<cloud_type>(unmarshallByte(th));
#if TAG_MAJOR_VERSION == 34
        // old system marshalled empty clouds this way
        if (cloud.type == CLOUD_NONE)
            continue;
#else
        ASSERT(cloud.type != CLOUD_NONE);
#endif
        cloud.pos.x = unmarshallByte(th);
        cloud.pos.y = unmarshallByte(th);
        ASSERT_IN_BOUNDS(cloud.pos);
        cloud.decay = unmarshallShort(th);
        cloud.spread_rate = unmarshallUByte(th);
        cloud.whose = static_cast<kill_category>(unmarshallUByte(th));
        cloud.killer = static_cast<killer_type>(unmarshallUByte(th));
        cloud.source = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_DECUSTOM_CLOUDS)
        {
            unmarshallShort(th); // was cloud.colour
            unmarshallString(th); // was cloud.name
            unmarshallString(th); // was cloud.tile
        }
#endif
        cloud.excl_rad = unmarshallInt(th);

#if TAG_MAJOR_VERSION == 34
        // Remove clouds stuck in walls, from 0.18-a0-603-g332275c to
        // 0.18-a0-629-g16988c9.
        if (!cell_is_solid(cloud.pos))
#endif
            env.cloud[cloud.pos] = cloud;
    }

    EAT_CANARY;

    // how many shops?
    const int num_shops = unmarshallShort(th);
    shop_struct shop;
    env.shop.clear();
    for (int i = 0; i < num_shops; i++)
    {
        unmarshall_shop(th, shop);
        if (shop.type == SHOP_UNASSIGNED)
            continue;
#if TAG_MAJOR_VERSION == 34
        shop.num = i;
#endif
        env.shop[shop.pos] = shop;
    }

    EAT_CANARY;

    env.sanctuary_pos  = unmarshallCoord(th);
    env.sanctuary_time = unmarshallByte(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_SPAWN_RATE)
        unmarshallInt(th); // was env.spawn_random_rate
#endif

    env.markers.read(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_TRANSPORTER_LANDING)
    {
        for (auto& tr : transporters)
        {
            if (env.grid(tr) != DNGN_TRANSPORTER)
                continue;

            const coord_def dest = get_transporter_dest(tr);
            if (dest != INVALID_COORD)
                env.grid(dest) = DNGN_TRANSPORTER_LANDING;
        }
    }
    if (th.getMinorVersion() < TAG_MINOR_VETO_DISINT)
    {
        for (map_marker *mark : env.markers.get_all(MAT_ANY))
        {
            if (mark->property("veto_disintegrate") == "veto")
            {
                map_wiz_props_marker *marker =
                    new map_wiz_props_marker(mark->pos);
                marker->set_property("veto_dig", "veto");
                env.markers.add(marker);
            }
        }
    }
    if (th.getMinorVersion() < TAG_MINOR_MERGE_VETOES)
    {
        for (map_marker *mark : env.markers.get_all(MAT_ANY))
        {
            if (mark->property("veto_dig") == "veto"
                || mark->property("veto_fire") == "veto"
                || mark->property("veto_shatter") == "veto"
                || mark->property("veto_tornado") == "veto")
            {
                map_wiz_props_marker *marker =
                    new map_wiz_props_marker(mark->pos);
                marker->set_property("veto_destroy", "veto");
                env.markers.add(marker);
            }
        }
    }
#endif

    env.properties.clear();
    env.properties.read(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_PLACE_UNPACK)
    {
        CrawlHashTable &props = env.properties;
        if (props.exists(VAULT_MON_BASES_KEY))
        {
            ASSERT(!props.exists(VAULT_MON_PLACES_KEY));
            CrawlVector &type_vec = props[VAULT_MON_TYPES_KEY].get_vector();
            CrawlVector &base_vec = props[VAULT_MON_BASES_KEY].get_vector();
            size_t size = type_vec.size();
            props[VAULT_MON_PLACES_KEY].new_vector(SV_LEV_ID).resize(size);
            CrawlVector &place_vec = props[VAULT_MON_PLACES_KEY].get_vector();
            for (size_t i = 0; i < size; i++)
            {
                if (type_vec[i].get_int() == -1)
                   place_vec[i] = level_id::from_packed_place(base_vec[i].get_int());
                else
                   place_vec[i] = level_id();
            }
        }
    }
    if (th.getMinorVersion() < TAG_MINOR_BOX_OF_BEASTS_CHARGES)
    {
        // this is a fairly approximate fixup for obscure cases where new
        // random types were added and broke handling of draconian zig levels;
        // requires a save where the game crashed during levelgen on such a
        // zig level.
        CrawlHashTable &props = env.properties;
        CrawlVector &type_vec = props[VAULT_MON_TYPES_KEY].get_vector();
        for (size_t i = 0; i < type_vec.size(); i++)
        {
            monster_type type = static_cast<monster_type>(type_vec[i].get_int());
            if (type == RANDOM_MOBILE_MONSTER || type == RANDOM_COMPATIBLE_MONSTER)
                type_vec[i] = RANDOM_DRACONIAN;
        }
        // ensure that these exist to satisfy some ASSERTs
        props[VAULT_MON_BASES_KEY].get_vector();
        props[VAULT_MON_WEIGHTS_KEY].get_vector();
        props[VAULT_MON_BANDS_KEY].get_vector();
        props[VAULT_MON_PLACES_KEY].get_vector();

    }

#endif

    env.dactions_done = unmarshallInt(th);

    // Restore heightmap
    env.heightmap.reset(nullptr);
    const bool have_heightmap = unmarshallBoolean(th);
    if (have_heightmap)
    {
        env.heightmap.reset(new grid_heightmap);
        grid_heightmap &heightmap(*env.heightmap);
        for (rectangle_iterator ri(0); ri; ++ri)
            heightmap(*ri) = unmarshallShort(th);
    }

    EAT_CANARY;

    env.forest_awoken_until = unmarshallInt(th);
    unmarshall_level_vault_data(th);
    env.density = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34

    if (th.getMinorVersion() < TAG_MINOR_NO_SUNLIGHT)
    {
        int num_lights = unmarshallShort(th);
        ASSERT(num_lights >= 0);
        while (num_lights-- > 0)
        {
            unmarshallCoord(th);
            unmarshallInt(th);
        }
    }
#endif
}

#if TAG_MAJOR_VERSION == 34
static spell_type _fixup_soh_breath(monster_type mtyp)
{
    switch (mtyp)
    {
        case MONS_SERPENT_OF_HELL:
        default:
            return SPELL_SERPENT_OF_HELL_GEH_BREATH;
        case MONS_SERPENT_OF_HELL_COCYTUS:
            return SPELL_SERPENT_OF_HELL_COC_BREATH;
        case MONS_SERPENT_OF_HELL_DIS:
            return SPELL_SERPENT_OF_HELL_DIS_BREATH;
        case MONS_SERPENT_OF_HELL_TARTARUS:
            return SPELL_SERPENT_OF_HELL_TAR_BREATH;
    }
}

static bool _need_poly_refresh(const monster &mon)
{
    if (!mon.props.exists(POLY_SET_KEY))
        return true;
    const CrawlVector &set = mon.props[POLY_SET_KEY].get_vector();
    for (int poly_mon : set)
    {
        const monster_type mc = (monster_type)poly_mon;
        // removed monster
        if (mc == MONS_PROGRAM_BUG || mons_species(mc) == MONS_PROGRAM_BUG)
            return true;
    }
    return false;
}
#endif

static void _tag_read_level_items(reader &th)
{
    unwind_bool dont_scan(crawl_state.crash_debug_scans_safe, false);
    env.trap.clear();
    // how many traps?
    const int trap_count = unmarshallShort(th);
    trap_def trap;
    for (int i = 0; i < trap_count; ++i)
    {
        trap.type = static_cast<trap_type>(unmarshallUByte(th));
#if TAG_MAJOR_VERSION == 34
        if (trap.type == TRAP_UNASSIGNED)
            continue;
#else
        ASSERT(trap.type != TRAP_UNASSIGNED);
#endif
        trap.pos      = unmarshallCoord(th);
        trap.ammo_qty = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() == TAG_MINOR_0_11 && trap.type >= TRAP_TELEPORT)
            trap.type = (trap_type)(trap.type - 1);
        if (th.getMinorVersion() < TAG_MINOR_REVEAL_TRAPS)
            env.grid(trap.pos) = trap.feature();
        if (th.getMinorVersion() >= TAG_MINOR_TRAPS_DETERM
            && th.getMinorVersion() != TAG_MINOR_0_11
            && th.getMinorVersion() < TAG_MINOR_REVEALED_TRAPS)
        {
            unmarshallUByte(th);
        }
#endif
        env.trap[trap.pos] = trap;
    }

#if TAG_MAJOR_VERSION == 34
    // Fix up floor that trap_def::destroy left as a trap (from
    // 0.18-a0-605-g5e852a4 to 0.18-a0-614-gc92b81f).
    for (int i = 0; i < GXM; i++)
        for (int j = 0; j < GYM; j++)
        {
            coord_def pos(i, j);
            if (feat_is_trap(env.grid(pos)) && !map_find(env.trap, pos))
                env.grid(pos) = DNGN_FLOOR;
        }

#endif

    // how many items?
    const int item_count = unmarshallShort(th);
    ASSERT_RANGE(item_count, 0, MAX_ITEMS + 1);
    for (int i = 0; i < item_count; ++i)
        unmarshallItem(th, env.item[i]);
    for (int i = item_count; i < MAX_ITEMS; ++i)
        env.item[i].clear();

#ifdef DEBUG_ITEM_SCAN
    // There's no way to fix this, even with wizard commands, so get
    // rid of it when restoring the game.
    for (int i = 0; i < item_count; ++i)
    {
        if (env.item[i].defined() && env.item[i].pos.origin())
        {
            debug_dump_item(env.item[i].name(DESC_PLAIN).c_str(), i, env.item[i],
                                        "Fixing up unlinked temporary item:");
            env.item[i].clear();
        }
    }
#endif
}

void unmarshallMonster(reader &th, monster& m)
{
    m.reset();

    m.type = unmarshallMonType(th);
    if (m.type == MONS_NO_MONSTER)
        return;

    ASSERT(!invalid_monster_type(m.type));

#if TAG_MAJOR_VERSION == 34
    uint32_t parts    = 0;
    if (th.getMinorVersion() < TAG_MINOR_MONSTER_PARTS)
    {
        if (mons_is_ghost_demon(m.type))
            parts |= MP_GHOST_DEMON;
    }
    else
        parts         = unmarshallUnsigned(th);
    if (th.getMinorVersion() < TAG_MINOR_OPTIONAL_PARTS)
        parts |= MP_CONSTRICTION | MP_ITEMS | MP_SPELLS;
#else
    uint32_t parts    = unmarshallUnsigned(th);
#endif
    m.mid             = unmarshallInt(th);
    ASSERT(m.mid > 0);
    m.mname           = unmarshallString(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() >= TAG_MINOR_LEVEL_XP_INFO)
    {
        // This was monster::is_spawn before the level XP info fix.
        if (th.getMinorVersion() < TAG_MINOR_LEVEL_XP_INFO_FIX)
        {
            // We no longer track spawns but instead whether the monster comes
            // from a vault. This gets determined from props below for
            // transferred games.
            unmarshallByte(th);
            m.xp_tracking = XP_NON_VAULT;
        }
        else
#endif
    m.xp_tracking     = static_cast<xp_tracking_type>(unmarshallUByte(th));
#if TAG_MAJOR_VERSION == 34
    }
    // Don't track monsters generated before TAG_MINOR_LEVEL_XP_INFO.
    else
        m.xp_tracking = XP_UNTRACKED;


    if (th.getMinorVersion() < TAG_MINOR_REMOVE_MON_AC_EV)
    {
        unmarshallByte(th);
        unmarshallByte(th);
    }
#endif
    m.set_hit_dice(     unmarshallByte(th));
#if TAG_MAJOR_VERSION == 34
    // Draining used to be able to take a monster to 0 HD, but that
    // caused crashes if they tried to cast spells.
    m.set_hit_dice(max(m.get_experience_level(), 1));
#else
    ASSERT(m.get_experience_level() > 0);
#endif
    m.speed           = unmarshallByte(th);
    // Avoid sign extension when loading files (Elethiomel's hang)
    m.speed_increment = unmarshallUByte(th);
    m.behaviour       = static_cast<beh_type>(unmarshallUByte(th));
    int x             = unmarshallByte(th);
    int y             = unmarshallByte(th);
    m.set_position(coord_def(x,y));
    m.target.x        = unmarshallByte(th);
    m.target.y        = unmarshallByte(th);

    m.firing_pos      = unmarshallCoord(th);
    m.patrol_point    = unmarshallCoord(th);

    int help = unmarshallByte(th);
    m.travel_target = static_cast<montravel_target_type>(help);

    const int len = unmarshallShort(th);
    for (int i = 0; i < len; ++i)
        m.travel_path.push_back(unmarshallCoord(th));

    m.flags.flags = unmarshallUnsigned(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_REMOVE_MONSTER_XP)
        unmarshallInt(th);
#endif

    m.enchantments.clear();
    const int nenchs = unmarshallShort(th);
    for (int i = 0; i < nenchs; ++i)
    {
        mon_enchant me = unmarshall_mon_enchant(th);
        m.enchantments[me.ench] = me;
        m.ench_cache.set(me.ench, true);
    }

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_FRENZY_FIXUP
        && m.has_ench(ENCH_FRENZIED))
    {
        m.del_ench(ENCH_HASTE);
        m.del_ench(ENCH_MIGHT);
    }
#endif

    m.ench_countdown = unmarshallByte(th);

    m.hit_points     = unmarshallShort(th);
    m.max_hit_points = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_SPECIFY_EXP)
        m.exp = 0;
    else
#endif
    m.exp            = unmarshallInt(th);
    m.number         = unmarshallInt(th);
    m.base_monster   = unmarshallMonType(th);
    m.colour         = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_SUMMONER)
        m.summoner = 0;
    else
#endif
    m.summoner       = unmarshallInt(th);

    if (parts & MP_ITEMS)
        for (int j = 0; j < NUM_MONSTER_SLOTS; j++)
            m.inv[j] = unmarshallShort(th);

    if (parts & MP_SPELLS)
    {
        unmarshallSpells(th, m.spells
#if TAG_MAJOR_VERSION == 34
                         , m.get_experience_level()
#endif
                         );
#if TAG_MAJOR_VERSION == 34
    monster_spells oldspells = m.spells;
    m.spells.clear();
    for (mon_spell_slot &slot : oldspells)
    {
        if (th.getMinorVersion() < TAG_MINOR_MORE_GHOST_MAGIC)
            slot.spell = _fixup_positional_monster_spell(slot.spell);

        if (th.getMinorVersion() < TAG_MINOR_GLASS_EYES
            && mons_genus(m.type) == MONS_FLOATING_EYE
            && slot.spell == SPELL_PARALYSIS_GAZE)
        {
            slot.spell = SPELL_VITRIFYING_GAZE;
            m.del_ench(ENCH_SPELL_CHARGED);
        }

        if (mons_is_zombified(m) && m.type != MONS_BOUND_SOUL
            && slot.spell != SPELL_CREATE_TENTACLES)
        {
            // zombies shouldn't have (most) spells
        }
        else if (slot.spell == SPELL_DRACONIAN_BREATH)
        {
            // Replace Draconian Breath with the colour-specific spell,
            // and remove Azrael's bad breath while we're at it.
            if (mons_genus(m.type) == MONS_DRACONIAN)
                m.spells.push_back(drac_breath(draconian_subspecies(m)));
        }
        // Give Mnoleg back malign gateway in place of tentacles.
        else if (slot.spell == SPELL_CREATE_TENTACLES
                 && m.type == MONS_MNOLEG)
        {
            slot.spell = SPELL_MALIGN_GATEWAY;
            slot.freq = 27;
            m.spells.push_back(slot);
        }
        else if (slot.spell == SPELL_CHANT_FIRE_STORM)
        {
            slot.spell = SPELL_FIRE_STORM;
            m.spells.push_back(slot);
        }
        else if (slot.spell == SPELL_SERPENT_OF_HELL_BREATH_REMOVED)
        {
            slot.spell = _fixup_soh_breath(m.type);
            m.spells.push_back(slot);
        }
        else if (slot.spell == SPELL_ELECTRIC_CHARGE)
        {
            slot.spell = SPELL_ELECTROLUNGE;
            m.spells.push_back(slot);
        }
        else if (slot.spell == SPELL_CAUSTIC_BREATH)
        {
            slot.spell = SPELL_SPIT_ACID;
            m.spells.push_back(slot);
        }
        else if (slot.spell == SPELL_FREEZING_CLOUD)
        {
            slot.spell = SPELL_FREEZING_GUST;
            m.spells.push_back(slot);
        }
        else if (slot.spell == SPELL_SWIFTNESS)
        {
            slot.spell = SPELL_FLEETFOOT;
            m.spells.push_back(slot);
        }
#if TAG_MAJOR_VERSION == 34
        else if (slot.spell != SPELL_DELAYED_FIREBALL
                 && slot.spell != SPELL_GRAVITAS
                 && slot.spell != SPELL_MELEE
                 && slot.spell != SPELL_NO_SPELL)
        {
            m.spells.push_back(slot);
        }
#endif
        else if (slot.spell == SPELL_CORRUPT_BODY)
        {
            slot.spell = SPELL_CORRUPTING_PULSE;
            m.spells.push_back(slot);
        }
    }
#endif
    }

    m.god      = static_cast<god_type>(unmarshallByte(th));
    m.attitude = static_cast<mon_attitude_type>(unmarshallByte(th));
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_CUT_STRICT_NEUTRAL
        && m.attitude == ATT_OLD_STRICT_NEUTRAL)
    {
        m.attitude = ATT_GOOD_NEUTRAL;
    }
#endif
    m.foe      = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    // In 0.16 alpha we briefly allowed YOU_FAULTLESS as a monster's foe.
    if (m.foe == YOU_FAULTLESS)
        m.foe = MHITYOU;
#endif
    m.foe_memory = unmarshallInt(th);

    m.damage_friendly = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_XP_CONTRIBUTE_FIXUP)
        m.damage_friendly /= 2;
#endif
    m.damage_total = unmarshallShort(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_UNSEEN_MONSTER)
    {
        m.revealed_this_turn = false;
        m.revealed_at_pos = coord_def(0, 0);
    }
    else
    {
#endif
        m.revealed_this_turn = unmarshallByte(th);
        m.revealed_at_pos = unmarshallCoord(th);
#if TAG_MAJOR_VERSION == 34
    }
    if (th.getMinorVersion() < TAG_MINOR_TRACK_ORIGIN_LEVEL)
        m.origin_level = level_id::current();
    else
    {
#endif
        m.origin_level = unmarshall_level_id(th);
#if TAG_MAJOR_VERSION == 34
    }
#endif

#if TAG_MAJOR_VERSION == 34
    if (m.type == MONS_LABORATORY_RAT)
        _unmarshallGhost(th), m.type = MONS_RAT;

    // MONS_SPECTRAL_WEAPON was inserted into the wrong place
    // (0.13-a0-1964-g2fab1c1, merged into trunk in 0.13-a0-1981-g9e80fb2),
    // and then had a ghost_demon structure added (0.13-a0-2055-g6cfaa00).
    // Neither event had an associated tag, but both were between the
    // same two adjacent tags.
    if (th.getMinorVersion() < TAG_MINOR_CANARIES
        && th.getMinorVersion() >= TAG_MINOR_WAR_DOG_REMOVAL
        && m.type >= MONS_SPECTRAL_WEAPON
        && m.type <= MONS_POLYMOTH)
    {
        // But fortunately the three monsters it could be all have different
        // speeds, and none of those speeds are 3/2 or 2/3 any others. We will
        // assume that none of these had the wretched enchantment. Ugh.
        switch (m.speed)
        {
        case 6: case 7: // slowed
        case 10:
        case 15: // hasted/berserked
            m.type = MONS_GHOST; // wellspring
            break;
        case 8: // slowed
        case 12:
        case 18: // hasted/berserked
            m.type = MONS_POLYMOTH;
            break;
        case 16: case 17: case 20: // slowed
        case 25:
        case 30:
        case 37: case 38: case 45: // hasted/berserked
            m.type = MONS_SPECTRAL_WEAPON;
            break;
        default:
            die("Unexpected monster with type %d and speed %d",
                m.type, m.speed);
        }
    }

    // Spectral weapons became speed 30 in the commit immediately preceding
    // the one that added the ghost_demon. Since the commits were in the
    // same batch, no one should have saves where the speed is 30 and the
    // spectral weapon didn't have a ghost_demon, or where the speed is
    // 25 and it did.
    if (th.getMinorVersion() < TAG_MINOR_CANARIES
        && m.type == MONS_SPECTRAL_WEAPON
        // normal, slowed, and hasted, respectively.
        && m.speed != 30 && m.speed != 20 && m.speed != 45)
    {
        // Don't bother trying to fix it up.
        m.type = MONS_WOOD_GOLEM; // anything removed
        m.mid = ++you.last_mid;   // sabotage the bond
        ASSERT(m.mid < MID_FIRST_NON_MONSTER);
        parts &= MP_GHOST_DEMON;
    }
    else if (m.type == MONS_CHIMERA
             && th.getMinorVersion() < TAG_MINOR_CHIMERA_GHOST_DEMON)
    {
        // Don't unmarshall the ghost demon if this is an invalid chimera
    }
    else if (th.getMinorVersion() < TAG_MINOR_DEMONSPAWN
             && m.type >= MONS_MONSTROUS_DEMONSPAWN
             && m.type <= MONS_SALAMANDER_MYSTIC)
    {
        // The demonspawn-enemies branch was merged in such a fashion
        // that it bumped several monster enums (see merge commit:
        // 0.14-a0-2321-gdab6825).
        // Try to figure out what it is.
        switch (m.colour)
        {
        case BROWN:        // monstrous demonspawn, naga ritualist
            if (m.spells[0].spell == SPELL_FORCE_LANCE)
                m.type = MONS_NAGA_RITUALIST;
            else
                m.type = MONS_MONSTROUS_DEMONSPAWN;
            break;
        case BLUE:         // gelid demonspawn
            m.type = MONS_GELID_DEMONSPAWN;
            break;
        case RED:          // infernal demonspawn
            m.type = MONS_INFERNAL_DEMONSPAWN;
            break;
        case LIGHTGRAY:    // torturous demonspawn, naga sharpshooter
            if (m.spells[0].spell == SPELL_PORTAL_PROJECTILE)
                m.type = MONS_NAGA_SHARPSHOOTER;
            else
                m.type = MONS_TORTUROUS_DEMONSPAWN;
            break;
        case LIGHTBLUE:    // blood saint, shock serpent
            if (m.base_monster != MONS_NO_MONSTER)
                m.type = MONS_DEMONSPAWN_BLOOD_SAINT;
            else
                m.type = MONS_SHOCK_SERPENT;
            break;
        case LIGHTCYAN:    // warmonger, drowned soul
            if (m.base_monster != MONS_NO_MONSTER)
                m.type = MONS_DEMONSPAWN_WARMONGER;
            else
                m.type = MONS_DROWNED_SOUL;
            break;
        case LIGHTGREEN:   // corrupter
            m.type = MONS_DEMONSPAWN_CORRUPTER;
            break;
        case LIGHTMAGENTA: // black sun
            m.type = MONS_DEMONSPAWN_SOUL_SCHOLAR;
            break;
        case CYAN:         // worldbinder
            m.type = MONS_WORLDBINDER;
            break;
        case MAGENTA:      // vine stalker, mana viper, grand avatar
            switch (m.speed)
            {
                case 20:
                case 30:
                case 45:
                    m.type = MONS_GRAND_AVATAR;
                    break;
                case 9:
                case 10:
                case 14:
                case 21:
                    m.type = MONS_MANA_VIPER;
                    break;
                default:
                    die("Unexpected monster with type %d and speed %d",
                        m.type, m.speed);
            }
            break;
        case WHITE:        // salamander firebrand
            m.type = MONS_SALAMANDER_FIREBRAND;
            break;
        case YELLOW:       // salamander mystic
            m.type = MONS_SALAMANDER_MYSTIC;
            break;
        default:
            die("Unexpected monster with type %d and colour %d",
                m.type, m.colour);
        }
    }
    else if (th.getMinorVersion() < TAG_MINOR_EXORCISE
        && th.getMinorVersion() >= TAG_MINOR_RANDLICHES
        && (m.type == MONS_LICH || m.type == MONS_ANCIENT_LICH
            || m.type == MONS_SPELLSPARK_SERVITOR))
    {
        m.spells = _unmarshallGhost(th).spells;
    }
    else
#endif
    if (parts & MP_GHOST_DEMON)
        m.set_ghost(_unmarshallGhost(th));

#if TAG_MAJOR_VERSION == 34
    // Turn elephant slugs into ghosts because they are dummies now.
    if (m.type == MONS_ELEPHANT_SLUG)
        m.type = MONS_GHOST;
#endif

    if (parts & MP_CONSTRICTION)
        _unmarshall_constriction(th, &m);

    m.props.clear();
    m.props.read(th);

#if TAG_MAJOR_VERSION == 34
    if (m.props.exists(MONSTER_TILE_KEY)
        && m.props[MONSTER_TILE_KEY].get_type() == SV_SHORT)
    {
        m.props[MONSTER_TILE_KEY].get_int() = m.props[MONSTER_TILE_KEY];
    }
#endif

    if (m.props.exists(MONSTER_TILE_NAME_KEY))
    {
        string tile = m.props[MONSTER_TILE_NAME_KEY].get_string();
        tileidx_t index;
        if (!tile_player_index(tile.c_str(), &index))
        {
            // If invalid tile name, complain and discard the props.
            dprf("bad tile name: \"%s\".", tile.c_str());
            m.props.erase(MONSTER_TILE_NAME_KEY);
            if (m.props.exists(MONSTER_TILE_KEY))
                m.props.erase(MONSTER_TILE_KEY);
        }
        else // Update monster tile.
            m.props[MONSTER_TILE_KEY] = int(index);
    }

#if TAG_MAJOR_VERSION == 34
    // Forget seen spells if the monster doesn't have any, most likely because
    // of a polymorph that happened before polymorph began removing this key.
    if (m.spells.empty())
        m.props.erase(SEEN_SPELLS_KEY);

    // Battlespheres that don't know their creator's mid must have belonged
    // to the player pre-monster-battlesphere.
    if (th.getMinorVersion() < TAG_MINOR_BATTLESPHERE_MID
        && m.type == MONS_BATTLESPHERE && !m.props.exists("bs_mid"))
    {
        // It must have belonged to the player.
        m.summoner = MID_PLAYER;
    }
    else if (m.props.exists("bs_mid"))
    {
        m.summoner = m.props["bs_mid"].get_int();
        m.props.erase("bs_mid");
    }

    if (m.props.exists(IOOD_MID))
        m.summoner = m.props[IOOD_MID].get_int(), m.props.erase(IOOD_MID);

    if (m.props.exists("siren_call"))
    {
        m.props[MERFOLK_AVATAR_CALL_KEY] = m.props["siren_call"].get_bool();
        m.props.erase("siren_call");
    }

    if (m.type == MONS_ZOMBIE_SMALL || m.type == MONS_ZOMBIE_LARGE)
        m.type = MONS_ZOMBIE;
    if (m.type == MONS_SKELETON_SMALL || m.type == MONS_SKELETON_LARGE)
        m.type = MONS_DRAUGR;
    if (m.type == MONS_SIMULACRUM_SMALL || m.type == MONS_SIMULACRUM_LARGE)
        m.type = MONS_SIMULACRUM;

    if (th.getMinorVersion() < TAG_MINOR_WAR_DOG_REMOVAL)
    {
        if (m.type == MONS_WAR_DOG)
            m.type = MONS_WOLF;
    }

    if (m.props.exists("no_hide"))
        m.props.erase("no_hide");

    if (m.props.exists("original_name"))
    {
        m.props[ORIGINAL_TYPE_KEY].get_int() =
            get_monster_by_name(m.props["original_name"].get_string());
    }

    // fixup for versions of frenzy that involved a permanent attitude change,
    // with the original attitude stored in a prop.
    if (m.props.exists("old_attitude"))
    {
        m.attitude = static_cast<mon_attitude_type>(
                                        m.props["old_attitude"].get_short());
        m.props.erase("old_attitude");
    }

    if (th.getMinorVersion() < TAG_MINOR_LEVEL_XP_VAULTS
        && m.props.exists("map"))
    {
        m.xp_tracking = XP_VAULT;
    }

    if (th.getMinorVersion() < TAG_MINOR_SETPOLY || _need_poly_refresh(m))
        init_poly_set(&m);

    if (m.type == MONS_ORC_APOSTLE && m.damage_friendly > m.damage_total)
    {
        mprf(MSGCH_ERROR, "apostle \"%s\" had incorrect damage tracking: %d > %d",
            m.full_name(DESC_PLAIN).c_str(), m.damage_friendly, m.damage_total);
        m.damage_total = m.damage_friendly = 0;
    }

    if (m.type == MONS_SLYMDRA && m.num_heads <= 0)
        m.num_heads = 1;
#endif

    if (m.type != MONS_PROGRAM_BUG && mons_species(m.type) == MONS_PROGRAM_BUG)
    {
        m.type = MONS_GHOST;
        m.props.clear();
    }

    // If an upgrade synthesizes ghost_demon, please mark it in "parts" above.
    ASSERT(parts & MP_GHOST_DEMON || !mons_is_ghost_demon(m.type));
}

static void _tag_read_level_monsters(reader &th)
{
    unwind_bool dont_scan(crawl_state.crash_debug_scans_safe, false);
    int count;

    reset_all_monsters();

    // how many mons_alloc?
    count = unmarshallByte(th);
    ASSERT(count >= 0);
    for (int i = 0; i < count && i < MAX_MONS_ALLOC; ++i)
        env.mons_alloc[i] = unmarshallMonType(th);
    for (int i = MAX_MONS_ALLOC; i < count; ++i)
        unmarshallShort(th);
    for (int i = count; i < MAX_MONS_ALLOC; ++i)
        env.mons_alloc[i] = MONS_NO_MONSTER;

    // how many monsters?
    count = unmarshallShort(th);
    ASSERT_RANGE(count, 0, MAX_MONSTERS + 1);

    env.max_mon_index = max(0, count - 1);
    for (int i = 0; i < count; i++)
    {
        monster& m = env.mons[i];
        unmarshallMonster(th, m);

        // place monster
        if (!m.alive())
            continue;

        monster *dup_m = monster_by_mid(m.mid);

#if TAG_MAJOR_VERSION == 34
        // clear duplicates of followers who got their god cleared as the result
        // of a bad polymorph prior to e6d7efa92cb0. This only fires on level
        // load *when there are duplicate mids*, because otherwise the clones
        // aren't uniquely identifiable. This fix may still result in duplicate
        // mid errors from time to time, but should never crash; saving and
        // loading will fix up the duplicate errors. A similar check also
        // happens in follower::place (since that runs after the level is
        // loaded).
        if (dup_m)
        {
            if (maybe_bad_priest_monster(*dup_m))
                fixup_bad_priest_monster(*dup_m);
            else if (maybe_bad_priest_monster(m))
            {
                fixup_bad_priest_monster(m);
                env.mid_cache[dup_m->mid] = dup_m->mindex();
                // dup_m should already be placed, so nothing else is needed.
                continue;
            }
            // we could print an error on the else case, but this is already
            // going to be handled by debug_mons_scan.
        }
#endif

        // companion_is_elsewhere checks the mid cache
        env.mid_cache[m.mid] = i;
        if (m.is_divine_companion() && companion_is_elsewhere(m.mid))
        {
            dprf("Killed elsewhere companion %s(%d) on %s",
                    m.name(DESC_PLAIN, true).c_str(), m.mid,
                    level_id::current().describe(false, true).c_str());
            monster_die(m, KILL_RESET, -1, true);
            // avoid "mid cache bogosity" if there's an unhandled clone bug
            if (dup_m && dup_m->alive())
            {
                mprf(MSGCH_ERROR, "elsewhere companion has duplicate mid %d: %s",
                    dup_m->mid, dup_m->full_name(DESC_PLAIN).c_str());
                env.mid_cache[dup_m->mid] = dup_m->mindex();
            }
            continue;
        }

#if defined(DEBUG) || defined(DEBUG_MONS_SCAN)
        if (invalid_monster_type(m.type))
        {
            mprf(MSGCH_ERROR, "Unmarshalled monster #%d %s",
                 i, m.name(DESC_PLAIN, true).c_str());
        }
        if (!in_bounds(m.pos()))
        {
            mprf(MSGCH_ERROR,
                 "Unmarshalled monster #%d %s out of bounds at (%d, %d)",
                 i, m.name(DESC_PLAIN, true).c_str(),
                 m.pos().x, m.pos().y);
        }
        int midx = env.mgrid(m.pos());
        if (midx != NON_MONSTER)
        {
            mprf(MSGCH_ERROR, "(%d, %d) for %s already occupied by %s",
                 m.pos().x, m.pos().y,
                 m.name(DESC_PLAIN, true).c_str(),
                 env.mons[midx].name(DESC_PLAIN, true).c_str());
        }
#endif
        env.mgrid(m.pos()) = i;
    }
#if TAG_MAJOR_VERSION == 34
    // This relies on TAG_YOU (including lost monsters) being unmarshalled
    // on game load before the initial level.
    if (th.getMinorVersion() < TAG_MINOR_FIXED_CONSTRICTION
        && th.getMinorVersion() >= TAG_MINOR_OPTIONAL_PARTS)
    {
        _fix_missing_constrictions();
    }
    if (th.getMinorVersion() < TAG_MINOR_TENTACLE_MID)
    {
        for (monster_iterator mi; mi; ++mi)
        {
            if (mi->props.exists(INWARDS_KEY))
            {
                const int old_midx = mi->props[INWARDS_KEY].get_int();
                if (invalid_monster_index(old_midx))
                    mi->props[INWARDS_KEY].get_int() = MID_NOBODY;
                else
                    mi->props[INWARDS_KEY].get_int() = env.mons[old_midx].mid;
            }
            if (mi->props.exists(OUTWARDS_KEY))
            {
                const int old_midx = mi->props[OUTWARDS_KEY].get_int();
                if (invalid_monster_index(old_midx))
                    mi->props[OUTWARDS_KEY].get_int() = MID_NOBODY;
                else
                    mi->props[OUTWARDS_KEY].get_int() = env.mons[old_midx].mid;
            }
            if (mons_is_tentacle_or_tentacle_segment(mi->type))
                mi->tentacle_connect = env.mons[mi->tentacle_connect].mid;
        }
    }
#endif
}

static void _debug_count_tiles()
{
#ifdef DEBUG_DIAGNOSTICS
# ifdef USE_TILE
    map<int,bool> found;
    int t, cnt = 0;
    for (int i = 0; i < GXM; i++)
        for (int j = 0; j < GYM; j++)
        {
            t = tile_env.bk_bg[i][j];
            if (!found.count(t))
                cnt++, found[t] = true;
            t = tile_env.bk_fg[i][j];
            if (!found.count(t))
                cnt++, found[t] = true;
            t = tile_env.bk_cloud[i][j];
            if (!found.count(t))
                cnt++, found[t] = true;
        }
    dprf("Unique tiles found: %d", cnt);
# endif
#endif
}

void _tag_read_level_tiles(reader &th)
{
    // Map grids.
    // how many X?
    const int gx = unmarshallShort(th);
    // how many Y?
    const int gy = unmarshallShort(th);

    tile_env.names.clear();
    unsigned int num_tilenames = unmarshallShort(th);
    for (unsigned int i = 0; i < num_tilenames; ++i)
    {
#ifdef DEBUG_TILE_NAMES
        string temp = unmarshallString(th);
        mprf("Reading tile_names[%d] = %s", i, temp.c_str());
        tile_env.names.push_back(temp);
#else
        tile_env.names.push_back(unmarshallString(th));
#endif
    }

    // flavour
    tile_env.default_flavour.wall_idx  = unmarshallShort(th);
    tile_env.default_flavour.floor_idx = unmarshallShort(th);
    tile_env.default_flavour.wall      = unmarshallShort(th);
    tile_env.default_flavour.floor     = unmarshallShort(th);
    tile_env.default_flavour.special   = unmarshallShort(th);

    for (int x = 0; x < gx; x++)
        for (int y = 0; y < gy; y++)
        {
            tile_env.flv[x][y].wall_idx  = unmarshallShort(th);
            tile_env.flv[x][y].floor_idx = unmarshallShort(th);
            tile_env.flv[x][y].feat_idx  = unmarshallShort(th);

            // These get overwritten by _regenerate_tile_flavour
            tile_env.flv[x][y].wall    = unmarshallShort(th);
            tile_env.flv[x][y].floor   = unmarshallShort(th);
            tile_env.flv[x][y].feat    = unmarshallShort(th);
            tile_env.flv[x][y].special = unmarshallShort(th);
        }

    _debug_count_tiles();

    _regenerate_tile_flavour();

    // Draw remembered map
    _draw_tiles();
}

static tileidx_t _get_tile_from_vector(const unsigned int idx)
{
    if (idx <= 0 || idx > tile_env.names.size())
    {
#ifdef DEBUG_TILE_NAMES
        mprf("Index out of bounds: idx = %d - 1, size(tile_names) = %d",
            idx, tile_env.names.size());
#endif
        return 0;
    }
    string tilename = tile_env.names[idx - 1];

    tileidx_t tile;
    if (!tile_dngn_index(tilename.c_str(), &tile))
    {
#ifdef DEBUG_TILE_NAMES
        mprf("tilename %s (index %d) not found",
             tilename.c_str(), idx - 1);
#endif
        return 0;
    }
#ifdef DEBUG_TILE_NAMES
    mprf("tilename %s (index %d) resolves to tile %d",
         tilename.c_str(), idx - 1, (int) tile);
#endif

    return tile;
}

static void _regenerate_tile_flavour()
{
    /* Remember the wall_idx and floor_idx; tile_init_default_flavour
       sets them to 0 */
    tileidx_t default_wall_idx = tile_env.default_flavour.wall_idx;
    tileidx_t default_floor_idx = tile_env.default_flavour.floor_idx;
    tile_init_default_flavour();
    if (default_wall_idx)
    {
        tileidx_t new_wall = _get_tile_from_vector(default_wall_idx);
        if (new_wall)
        {
            tile_env.default_flavour.wall_idx = default_wall_idx;
            tile_env.default_flavour.wall = new_wall;
        }
    }
    if (default_floor_idx)
    {
        tileidx_t new_floor = _get_tile_from_vector(default_floor_idx);
        if (new_floor)
        {
            tile_env.default_flavour.floor_idx = default_floor_idx;
            tile_env.default_flavour.floor = new_floor;
        }
    }

    for (rectangle_iterator ri(coord_def(0, 0), coord_def(GXM-1, GYM-1));
         ri; ++ri)
    {
        tile_flavour &flv = tile_env.flv(*ri);
        flv.wall = 0;
        flv.floor = 0;
        flv.feat = 0;
        flv.special = 0;

        if (flv.wall_idx)
        {
            tileidx_t new_wall = _get_tile_from_vector(flv.wall_idx);
            if (!new_wall)
                flv.wall_idx = 0;
            else
                flv.wall = new_wall;
        }
        if (flv.floor_idx)
        {
            tileidx_t new_floor = _get_tile_from_vector(flv.floor_idx);
            if (!new_floor)
                flv.floor_idx = 0;
            else
                flv.floor = new_floor;
        }
        if (flv.feat_idx)
        {
            tileidx_t new_feat = _get_tile_from_vector(flv.feat_idx);
            if (!new_feat)
                flv.feat_idx = 0;
            else
                flv.feat = new_feat;
        }
    }

    tile_new_level(true, false);
}

static void _draw_tiles()
{
#ifdef USE_TILE
    for (rectangle_iterator ri(coord_def(0, 0), coord_def(GXM-1, GYM-1));
         ri; ++ri)
    {
        tile_draw_map_cell(*ri);
    }
#endif
}
// ------------------------------- ghost tags ---------------------------- //

static void _marshallSpells(writer &th, const monster_spells &spells)
{
    const uint8_t spellsize = spells.size();
    marshallByte(th, spellsize);
    for (int j = 0; j < spellsize; ++j)
    {
        marshallShort(th, spells[j].spell);
        marshallByte(th, spells[j].freq);
        marshallShort(th, spells[j].flags.flags);
    }
}

#if TAG_MAJOR_VERSION == 34
static const uint8_t NUM_MONSTER_SPELL_SLOTS = 6;

static void _fixup_spells(monster_spells &spells, int hd)
{
    for (auto& slot : spells)
        slot.flags |= MON_SPELL_WIZARD;

    if (spells.size() >= NUM_MONSTER_SPELL_SLOTS)
        spells[NUM_MONSTER_SPELL_SLOTS-1].flags |= MON_SPELL_EMERGENCY;

    for (auto& slot : spells)
        slot.freq = (hd + 50) / spells.size();
}
#endif

void unmarshallSpells(reader &th, monster_spells &spells
#if TAG_MAJOR_VERSION == 34
                             , unsigned hd
#endif
                            )
{
    const uint8_t spellsize =
#if TAG_MAJOR_VERSION == 34

        (th.getMinorVersion() < TAG_MINOR_ARB_SPELL_SLOTS)
            ? NUM_MONSTER_SPELL_SLOTS :
#endif
        unmarshallByte(th);
    spells.clear();
    spells.resize(spellsize);
    for (int j = 0; j < spellsize; ++j)
    {
        spells[j].spell = unmarshallSpellType(th

#if TAG_MAJOR_VERSION == 34
            , true
#endif
            );
#if TAG_MAJOR_VERSION == 34
        if (th.getMinorVersion() < TAG_MINOR_MALMUTATE
            && spells[j].spell == SPELL_POLYMORPH)
        {
            spells[j].spell = SPELL_MALMUTATE;
        }

        if (spells[j].spell == SPELL_FAKE_RAKSHASA_SUMMON)
            spells[j].spell = SPELL_PHANTOM_MIRROR;

        if (spells[j].spell == SPELL_SUNRAY)
            spells[j].spell = SPELL_STONE_ARROW;

        if (th.getMinorVersion() >= TAG_MINOR_MONSTER_SPELL_SLOTS)
        {
#endif
            spells[j].freq = unmarshallByte(th);
            spells[j].flags.flags = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
            if (th.getMinorVersion() < TAG_MINOR_DEMONIC_SPELLS)
            {
                if (spells[j].flags & MON_SPELL_VOCAL)
                {
                    spells[j].flags &= ~MON_SPELL_VOCAL;
                    spells[j].flags |= MON_SPELL_MAGICAL;
                }
            }
        }
#endif

        if (spell_removed(spells[j].spell))
            spells[j].spell = SPELL_NO_SPELL;
    }

    int total_given_freq = 0;
    for (const auto &slot : spells)
        total_given_freq += slot.freq;

    erase_if(spells, [](const mon_spell_slot &t) {
        return t.spell == SPELL_NO_SPELL;
    });

#if TAG_MAJOR_VERSION == 34
    // This will turn all old spells into wizard spells, which
    // isn't right but is the simplest way to do this.
    if (th.getMinorVersion() < TAG_MINOR_MONSTER_SPELL_SLOTS)
    {
        _fixup_spells(spells, hd);
        total_given_freq = spell_freq_for_hd(hd);  // would be zero otherwise
    }
#endif

    normalize_spell_freq(spells, total_given_freq);
}

static void _marshallGhost(writer &th, const ghost_demon &ghost)
{
    // save compat changes with minor tags here must be added to bones_minor_tags
    marshallString(th, ghost.name);

    marshallShort(th, ghost.species);
    marshallShort(th, ghost.job);
    marshallByte(th, ghost.religion);
    marshallShort(th, ghost.best_skill);
    marshallShort(th, ghost.best_skill_level);
    marshallShort(th, ghost.xl);
    marshallShort(th, ghost.max_hp);
    marshallShort(th, ghost.ev);
    marshallShort(th, ghost.ac);
    marshallShort(th, ghost.willpower);
    marshallShort(th, ghost.damage);
    marshallShort(th, ghost.speed);
    marshallShort(th, ghost.move_energy);
    marshallByte(th, ghost.see_invis);
    marshallShort(th, ghost.brand);
    marshallShort(th, ghost.att_type);
    marshallShort(th, ghost.att_flav);
    marshallInt(th, ghost.resists);
    marshallByte(th, ghost.colour);
    marshallBoolean(th, ghost.flies);
    marshallShort(th, ghost.umbra_rad);
    marshallString(th, ghost.title);

    _marshallSpells(th, ghost.spells);
}

static ghost_demon _unmarshallGhost(reader &th)
{
    // save compat changes with minor tags here must be added to bones_minor_tags
    ghost_demon ghost;

    ghost.name             = unmarshallString(th);
    ghost.species          = static_cast<species_type>(unmarshallShort(th));
    ghost.job              = static_cast<job_type>(unmarshallShort(th));
    ghost.religion         = static_cast<god_type>(unmarshallByte(th));
    ghost.best_skill       = static_cast<skill_type>(unmarshallShort(th));
    ghost.best_skill_level = unmarshallShort(th);
    ghost.xl               = unmarshallShort(th);
    ghost.max_hp           = unmarshallShort(th);
    ghost.ev               = unmarshallShort(th);
    if (ghost.ev > MAX_GHOST_EVASION)
        ghost.ev = MAX_GHOST_EVASION;
    ghost.ac               = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_GHOST_WILLPOWER)
        ghost.willpower  = -1;
    else
#endif
    ghost.willpower        = unmarshallShort(th);
    ghost.damage           = unmarshallShort(th);
    ghost.speed            = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_GHOST_ENERGY)
        ghost.move_energy  = 10;
    else
#endif
    ghost.move_energy      = unmarshallShort(th);
    // fix up ghost_demons that forgot to have move_energy initialized
    if (ghost.move_energy < FASTEST_PLAYER_MOVE_SPEED)
        ghost.move_energy = FASTEST_PLAYER_MOVE_SPEED;
    else if (ghost.move_energy > 30)
        ghost.move_energy = 30;
#if TAG_MAJOR_VERSION == 34
    // If loading a ghost from back when all species had normal move speed,
    // apply default move speed of their species.
    if (ghost.move_energy == 10
        && th.getMinorVersion() < TAG_MINOR_GHOST_MOVE_SPEED_FIX)
    {
        if (ghost.species == SP_SPRIGGAN)
            ghost.move_energy = 6;
        else if (ghost.species == SP_BARACHI)
            ghost.move_energy = 12;
        else if (ghost.species == SP_NAGA)
            ghost.move_energy = 14;
    }
#endif

    ghost.see_invis        = unmarshallByte(th);
    ghost.brand            = static_cast<brand_type>(unmarshallShort(th));
    ghost.att_type = static_cast<attack_type>(unmarshallShort(th));
    ghost.att_flav = static_cast<attack_flavour>(unmarshallShort(th));
    ghost.resists          = unmarshallInt(th);
#if TAG_MAJOR_VERSION == 34
    if (ghost.resists & MR_OLD_RES_ACID)
        set_resist(ghost.resists, MR_RES_CORR, 3);
    if (th.getMinorVersion() < TAG_MINOR_NO_GHOST_SPELLCASTER)
        unmarshallByte(th);
    if (th.getMinorVersion() < TAG_MINOR_MON_COLOUR_LOOKUP)
        unmarshallByte(th);
#endif
    ghost.colour           = unmarshallByte(th);

#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_BOOL_FLIGHT)
        ghost.flies        = unmarshallShort(th);
    else
#endif
    ghost.flies        = unmarshallBoolean(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_GHOST_UMBRAS)
        ghost.umbra_rad    = -1;
    else
#endif
    ghost.umbra_rad    = unmarshallShort(th);
#if TAG_MAJOR_VERSION == 34
    if (th.getMinorVersion() < TAG_MINOR_GHOST_TITLE)
        ghost.title = "";
    else
#endif
    ghost.title        = unmarshallString(th);


    unmarshallSpells(th, ghost.spells
#if TAG_MAJOR_VERSION == 34
                     , ghost.xl
#endif
                    );

#if TAG_MAJOR_VERSION == 34
    monster_spells oldspells = ghost.spells;
    ghost.spells.clear();
    for (mon_spell_slot &slot : oldspells)
    {
        if (th.getMinorVersion() < TAG_MINOR_GHOST_MAGIC)
            slot.spell = _fixup_positional_monster_spell(slot.spell);
        if (slot.spell == SPELL_FREEZING_CLOUD)
        {
            slot.spell = SPELL_FREEZING_GUST;
            ghost.spells.push_back(slot);
        }
        // Gravitas needs special handling, since it was removed for monsters
        // but NOT players (and thus isn't a 'removed spell' in general)
        else if (!spell_removed(slot.spell) && slot.spell != SPELL_GRAVITAS)
            ghost.spells.push_back(slot);
    }
#endif

    return ghost;
}

static void _tag_construct_ghost(writer &th, vector<ghost_demon> &ghosts)
{
    // How many ghosts?
    marshallShort(th, ghosts.size());

    for (const ghost_demon &ghost : ghosts)
        _marshallGhost(th, ghost);
}

static vector<ghost_demon> _tag_read_ghost(reader &th)
{
    vector<ghost_demon> result;
    int nghosts = unmarshallShort(th);

    if (nghosts < 1 || nghosts > MAX_GHOSTS)
    {
        string error = "Bones file has an invalid ghost count (" +
                                                    to_string(nghosts) + ")";
        throw corrupted_save(error);
    }

    for (int i = 0; i < nghosts; ++i)
        result.push_back(_unmarshallGhost(th));
    return result;
}

vector<ghost_demon> tag_read_ghosts(reader &th)
{
    global_ghosts.clear();
    tag_read(th, TAG_GHOST);
    return global_ghosts; // should use copy semantics?
}

void tag_write_ghosts(writer &th, const vector<ghost_demon> &ghosts)
{
    global_ghosts = ghosts;
    tag_write(TAG_GHOST, th);
}