File: tilespec.c

package info (click to toggle)
freeciv 2.6.2-1~bpo10+1
  • links: PTS, VCS
  • area: main
  • in suites: buster-backports
  • size: 212,500 kB
  • sloc: ansic: 443,831; cpp: 29,541; sh: 7,982; makefile: 7,886; python: 1,933; xml: 945
file content (6919 lines) | stat: -rw-r--r-- 234,825 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
/***********************************************************************
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
***********************************************************************/

/***********************************************************************
  Functions for handling the tilespec files which describe
  the files and contents of tilesets.
  original author: David Pfitzner <dwp@mso.anu.edu.au>
***********************************************************************/

#ifdef HAVE_CONFIG_H
#include <fc_config.h>
#endif

#include <stdio.h>
#include <stdlib.h>		/* exit */
#include <stdarg.h>
#include <string.h>

/* utility */
#include "astring.h"
#include "bitvector.h"
#include "capability.h"
#include "deprecations.h"
#include "fcintl.h"
#include "log.h"
#include "mem.h"
#include "rand.h"
#include "registry.h"
#include "shared.h"
#include "string_vector.h"
#include "support.h"

/* common */
#include "base.h"
#include "effects.h"
#include "game.h"		/* game.control.styles_count */
#include "government.h"
#include "map.h"
#include "movement.h"
#include "nation.h"
#include "player.h"
#include "road.h"
#include "specialist.h"
#include "unit.h"
#include "unitlist.h"

/* client/include */
#include "dialogs_g.h"
#include "graphics_g.h"
#include "gui_main_g.h"
#include "mapview_g.h"          /* for update_map_canvas_visible */
#include "menu_g.h"
#include "themes_g.h"

/* client */
#include "citydlg_common.h"	/* for generate_citydlg_dimensions() */
#include "client_main.h"
#include "climap.h"		/* for client_tile_get_known() */
#include "colors_common.h"
#include "control.h"		/* for fill_xxx */
#include "editor.h"
#include "goto.h"
#include "helpdata.h"
#include "options.h"		/* for fill_xxx */
#include "themes_common.h"

#include "tilespec.h"

#define TILESPEC_CAPSTR "+Freeciv-2.6-tilespec duplicates_ok"
/*
 * Tilespec capabilities acceptable to this program:
 *
 * +Freeciv-2.6-tilespec
 *    - basic format for Freeciv versions 2.6.x; required
 *
 * +Freeciv-tilespec-Devel-YYYY.MMM.DD
 *    - tilespec of the development version at the given data
 *
 * duplicates_ok
 *    - we can handle existence of duplicate tags (lattermost tag which
 *      appears is used; tilesets which have duplicates should specify
 *      "duplicates_ok")
 */

#define SPEC_CAPSTR "+Freeciv-2.6-spec"
/*
 * Individual spec file capabilities acceptable to this program:
 *
 * +Freeciv-2.6-spec
 *    - basic format for Freeciv versions 2.6.x; required
 */

#define TILESPEC_SUFFIX ".tilespec"
#define TILE_SECTION_PREFIX "tile_"

/* This the way directional indices are now encoded: */
#define MAX_INDEX_CARDINAL 		64
#define MAX_INDEX_HALF                  16
#define MAX_INDEX_VALID			256

#define NUM_TILES_HP_BAR 11
#define NUM_TILES_DIGITS 10
#define NUM_TILES_SELECT 4
#define MAX_NUM_UPKEEP_SPRITES 10

#define SPECENUM_NAME extrastyle_id
#define SPECENUM_VALUE0 ESTYLE_ROAD_ALL_SEPARATE
#define SPECENUM_VALUE0NAME "RoadAllSeparate"
#define SPECENUM_VALUE1 ESTYLE_ROAD_PARITY_COMBINED
#define SPECENUM_VALUE1NAME "RoadParityCombined"
#define SPECENUM_VALUE2 ESTYLE_ROAD_ALL_COMBINED
#define SPECENUM_VALUE2NAME "RoadAllCombined"
#define SPECENUM_VALUE3 ESTYLE_RIVER
#define SPECENUM_VALUE3NAME "River"
#define SPECENUM_VALUE4 ESTYLE_SINGLE1
#define SPECENUM_VALUE4NAME "Single1"
#define SPECENUM_VALUE5 ESTYLE_SINGLE2
#define SPECENUM_VALUE5NAME "Single2"
#define SPECENUM_VALUE6 ESTYLE_3LAYER
#define SPECENUM_VALUE6NAME "3Layer"
#define SPECENUM_VALUE7 ESTYLE_CARDINALS
#define SPECENUM_VALUE7NAME "Cardinals"
#define SPECENUM_COUNT ESTYLE_COUNT
#include "specenum_gen.h"

/* This could be moved to common/map.h if there's more use for it. */
enum direction4 {
  DIR4_NORTH = 0, DIR4_SOUTH, DIR4_EAST, DIR4_WEST
};
static const char direction4letters[4] = "udrl";
/* This must correspond to enum edge_type. */
static const char edge_name[EDGE_COUNT][3] = {"ns", "we", "ud", "lr"};

static const int DIR4_TO_DIR8[4] =
    { DIR8_NORTH, DIR8_SOUTH, DIR8_EAST, DIR8_WEST };

enum match_style {
  MATCH_NONE,
  MATCH_SAME,		/* "boolean" match */
  MATCH_PAIR,
  MATCH_FULL
};

enum sprite_type {
  CELL_WHOLE,		/* entire tile */
  CELL_CORNER		/* corner of tile */
};

struct drawing_data {
  bool init;

  char *name;

  int num_layers; /* 1 thru MAX_NUM_LAYERS. */
#define MAX_NUM_LAYERS 3

  struct drawing_layer {
    bool is_tall;
    int offset_x, offset_y;

#define MAX_NUM_MATCH_WITH 8
    enum match_style match_style;
    int match_index[1 + MAX_NUM_MATCH_WITH];
    int match_indices; /* 0 = no match_type, 1 = no match_with */

    enum sprite_type sprite_type;

    struct sprite_vector base;
    struct sprite *match[MAX_INDEX_CARDINAL];
    struct sprite **cells;

    /* List of those sprites in 'cells' that are allocated by some other
     * means than load_sprite() and thus are not freed by unload_all_sprites(). */
    struct sprite_vector allocated;
  } layer[MAX_NUM_LAYERS];

  bool is_reversed;

  int blending; /* layer, 0 = none */
  struct sprite *blender;
  struct sprite *blend[4]; /* indexed by a direction4 */
};

struct city_style_threshold {
  struct sprite *sprite;
};

struct city_sprite {
  struct {
    int land_num_thresholds;
    struct city_style_threshold *land_thresholds;
  } *styles;
  int num_styles;
};

struct river_sprites {
  struct sprite
    *spec[MAX_INDEX_CARDINAL],
    *outlet[MAX_INDEX_CARDINAL];
};

struct named_sprites {
  struct sprite
    *indicator[INDICATOR_COUNT][NUM_TILES_PROGRESS],
    *treaty_thumb[2],     /* 0=disagree, 1=agree */
    *arrow[ARROW_LAST], /* 0=right arrow, 1=plus, 2=minus */

    *icon[ICON_COUNT],

    *events[E_COUNT],

    /* The panel sprites for showing tax % allocations. */
    *tax_luxury, *tax_science, *tax_gold,
    *dither_tile;     /* only used for isometric view */

  struct {
    struct sprite
      *tile,
      *worked_tile,
      *unworked_tile;
  } mask;

  struct sprite *tech[A_LAST];
  struct sprite *building[B_LAST];
  struct sprite *government[G_LAST];

  struct {
    struct sprite *icon[U_LAST];
    struct sprite *facing[U_LAST][DIR8_MAGIC_MAX]; 
    unsigned int default_dir_seed[U_LAST];
  } units;

  struct sprite *resource[MAX_NUM_RESOURCES];

  struct sprite_vector nation_flag;
  struct sprite_vector nation_shield;

  struct citizen_graphic {
    /* Each citizen type has up to MAX_NUM_CITIZEN_SPRITES different
     * sprites, as defined by the tileset. */
    int count;
    struct sprite *sprite[MAX_NUM_CITIZEN_SPRITES];
  } citizen[CITIZEN_LAST], specialist[SP_MAX];
  struct sprite *spaceship[SPACESHIP_COUNT];
  struct {
    int hot_x, hot_y;
    struct sprite *frame[NUM_CURSOR_FRAMES];
  } cursor[CURSOR_LAST];
  struct {
    struct sprite_vector unit;
    struct sprite *nuke;
  } explode;
  struct {
    struct sprite
      *hp_bar[NUM_TILES_HP_BAR],
      *vet_lev[MAX_VET_LEVELS],
      *select[NUM_TILES_SELECT],
      *auto_attack,
      *auto_settler,
      *auto_explore,
      *fortified,
      *fortifying,
      *go_to,			/* goto is a C keyword :-) */
      *irrigate,
      *plant,
      *pillage,
      *sentry,
      *stack,
      *loaded,
      *transform,
      *connect,
      *patrol,
      *convert,
      *battlegroup[MAX_NUM_BATTLEGROUPS],
      *lowfuel,
      *tired;
  } unit;
  struct {
    struct sprite
      *unhappy[MAX_NUM_UPKEEP_SPRITES],
      *output[O_LAST][MAX_NUM_UPKEEP_SPRITES];
  } upkeep;
  struct {
    struct sprite
      *disorder,
      *size[NUM_TILES_DIGITS],
      *size_tens[NUM_TILES_DIGITS],
      *size_hundreds[NUM_TILES_DIGITS],
      *tile_foodnum[NUM_TILES_DIGITS],
      *tile_shieldnum[NUM_TILES_DIGITS],
      *tile_tradenum[NUM_TILES_DIGITS];
    struct city_sprite
      *tile,
      *single_wall,
      *wall[NUM_WALL_TYPES],
      *occupied;
    struct sprite_vector worked_tile_overlay;
    struct sprite_vector unworked_tile_overlay;
  } city;
  struct citybar_sprites citybar;
  struct editor_sprites editor;
  struct {
    struct {
      struct sprite *specific;
      struct sprite *turns[NUM_TILES_DIGITS];
      struct sprite *turns_tens[NUM_TILES_DIGITS];
      struct sprite *turns_hundreds[NUM_TILES_DIGITS];
    } s[GTS_COUNT];
    struct sprite *waypoint;
  } path;
  struct {
    struct sprite *attention;
  } user;
  struct {
    struct sprite
      *fog,
      **fullfog,
      *darkness[MAX_INDEX_CARDINAL]; /* first unused */
  } tx;				/* terrain extra */
  struct {
    struct sprite
      *activity,
      *rmact;
    enum extrastyle_id extrastyle;
    union {
      struct sprite *single;
      struct sprite *cardinals[MAX_INDEX_CARDINAL];
      struct {
        struct sprite
          *background,
          *middleground,
          *foreground;
      } bmf;
      struct {
        struct sprite
        /* for extrastyles ESTYLE_ROAD_ALL_SEPARATE and ESTYLE_ROAD_PARITY_COMBINED */
          *isolated,
          *corner[8]; /* Indexed by direction; only non-cardinal dirs used. */
        union {
          /* for ESTYLE_ROAD_ALL_SEPARATE */
          struct sprite *dir[8];     /* all entries used */
          /* ESTYLE_ROAD_PARITY_COMBINED */
          struct {
            struct sprite
              *even[MAX_INDEX_HALF],    /* first unused */
              *odd[MAX_INDEX_HALF];     /* first unused */
          } combo;
          /* ESTYLE_ALL_SEPARATE */
          struct sprite *total[MAX_INDEX_VALID];
          struct river_sprites rivers;
        } ru;
      } road;
    } u;
  } extras[MAX_EXTRA_TYPES];
  struct {
    struct sprite
      *main[EDGE_COUNT],
      *city[EDGE_COUNT],
      *worked[EDGE_COUNT],
      *unavailable,
      *nonnative,
      *selected[EDGE_COUNT],
      *coastline[EDGE_COUNT],
      *borders[EDGE_COUNT][2];
  } grid;
  struct {
    struct sprite_vector overlays;
  } colors;
  struct {
    struct sprite *color; /* Generic background color */
    struct sprite *graphic; /* Generic background graphic */
  } background;
  struct {
    struct sprite *grid_borders[EDGE_COUNT][2];
    struct sprite *color;
    struct sprite *background;
  } player[MAX_NUM_PLAYER_SLOTS];

  struct drawing_data *drawing[MAX_NUM_ITEMS];
};

struct specfile {
  struct sprite *big_sprite;
  char *file_name;
};

#define SPECLIST_TAG specfile
#define SPECLIST_TYPE struct specfile
#include "speclist.h"

#define specfile_list_iterate(list, pitem) \
    TYPED_LIST_ITERATE(struct specfile, list, pitem)
#define specfile_list_iterate_end  LIST_ITERATE_END

/* 
 * Information about an individual sprite. All fields except 'sprite' are
 * filled at the time of the scan of the specfile. 'Sprite' is
 * set/cleared on demand in load_sprite/unload_sprite.
 */
struct small_sprite {
  int ref_count;

  /* The sprite is in this file. */
  char *file;

  /* Or, the sprite is in this file at the location. */
  struct specfile *sf;
  int x, y, width, height;

  /* A little more (optional) data. */
  int hot_x, hot_y;

  struct sprite *sprite;
};

/* 'struct small_sprite_list' and related functions. */
#define SPECLIST_TAG small_sprite
#define SPECLIST_TYPE struct small_sprite
#include "speclist.h"
#define small_sprite_list_iterate(list, pitem)                              \
  TYPED_LIST_ITERATE(struct small_sprite, list, pitem)
#define small_sprite_list_iterate_end LIST_ITERATE_END

/* 'struct sprite_hash' and related functions. */
#define SPECHASH_TAG sprite
#define SPECHASH_ASTR_KEY_TYPE
#define SPECHASH_IDATA_TYPE struct small_sprite *
#include "spechash.h"
#define sprite_hash_iterate(hash, tag_name, sprite)                         \
  TYPED_HASH_ITERATE(const char *, struct small_sprite *,                   \
                     hash, tag_name, sprite)
#define sprite_hash_iterate_end HASH_ITERATE_END

/* 'struct drawing_hash' and related functions. */
static void drawing_data_destroy(struct drawing_data *draw);

#define SPECHASH_TAG drawing
#define SPECHASH_CSTR_KEY_TYPE
#define SPECHASH_IDATA_TYPE struct drawing_data *
#define SPECHASH_IDATA_FREE drawing_data_destroy
#include "spechash.h"

#define SPECHASH_TAG estyle
#define SPECHASH_ASTR_KEY_TYPE
#define SPECHASH_ENUM_DATA_TYPE extrastyle_id
#include "spechash.h"

struct tileset {
  char name[512];
  char given_name[MAX_LEN_NAME];
  char version[MAX_LEN_NAME];
  int priority;

  char *summary;
  char *description;
  float scale;

  enum ts_type type;
  int hex_width, hex_height;
  int ts_topo_idx;

  int normal_tile_width, normal_tile_height;
  int full_tile_width, full_tile_height;
  int unit_tile_width, unit_tile_height;
  int small_sprite_width, small_sprite_height;

  int max_upkeep_height;

  char *main_intro_filename;
  char *minimap_intro_filename;

  int city_names_font_size, city_productions_font_size;

  enum fog_style fogstyle;
  enum darkness_style darkness_style;
  int darkness_layer;

  int unit_flag_offset_x, unit_flag_offset_y;
  int city_flag_offset_x, city_flag_offset_y;
  int unit_offset_x, unit_offset_y;
  int city_offset_x, city_offset_y;

  int citybar_offset_y;
  int tilelabel_offset_y;
  int activity_offset_x;
  int activity_offset_y;
  int occupied_offset_x;
  int occupied_offset_y;
  int unit_upkeep_offset_y;
  int unit_upkeep_small_offset_y;

#define NUM_CORNER_DIRS 4
#define TILES_PER_CORNER 4
  int num_valid_tileset_dirs, num_cardinal_tileset_dirs;
  int num_index_valid, num_index_cardinal;
  enum direction8 valid_tileset_dirs[8], cardinal_tileset_dirs[8];

  struct tileset_layer {
    char **match_types;
    size_t match_count;
  } layers[MAX_NUM_LAYERS];

  struct specfile_list *specfiles;
  struct small_sprite_list *small_sprites;

  /* This hash table maps tilespec tags to struct small_sprites. */
  struct sprite_hash *sprite_hash;

  /* This hash table maps terrain graphic strings to drawing data. */
  struct drawing_hash *tile_hash;

  struct estyle_hash *estyle_hash;

  struct named_sprites sprites;

  struct color_system *color_system;

  struct extra_type_list *style_lists[ESTYLE_COUNT];

  struct extra_type_list *flagged_bases_list;

  int num_preferred_themes;
  char** preferred_themes;
};

struct tileset *tileset;
struct tileset *unscaled_tileset;

int focus_unit_state = 0;

static bool tileset_update = FALSE;


static int fill_unit_type_sprite_array(const struct tileset *t,
                                       struct drawn_sprite *sprs,
                                       const struct unit_type *putype,
                                       enum direction8 facing);
static int fill_unit_sprite_array(const struct tileset *t,
                                  struct drawn_sprite *sprs,
                                  const struct unit *punit,
                                  bool stack, bool backdrop);
static bool load_river_sprites(struct tileset *t,
                               struct river_sprites *store, const char *tag_pfx);

static void tileset_setup_base(struct tileset *t,
                               const struct extra_type *pextra);
static void tileset_setup_road(struct tileset *t,
                               struct extra_type *pextra);

static bool is_extra_drawing_enabled(struct extra_type *pextra);

static int fill_basic_road_sprite_array(const struct tileset *t,
                                        struct drawn_sprite *sprs,
                                        const struct extra_type *pextra);
static int fill_basic_base_sprite_array(const struct tileset *t,
                                        struct drawn_sprite *sprs,
                                        const struct extra_type *pextra);

static void tileset_player_free(struct tileset *t, int plrid);

/****************************************************************************
  Called when ever there's problem in ruleset/tileset compatibility
****************************************************************************/
void tileset_error(enum log_level level, const char *format, ...)
{
  char buf[2048];
  va_list args;

  va_start(args, format);
  fc_vsnprintf(buf, sizeof(buf), format, args);
  va_end(args);

  log_base(level, "%s", buf);

  if (level <= LOG_NORMAL) {
    show_tileset_error(buf);
  }

  if (level == LOG_FATAL) {
    exit(EXIT_FAILURE);
  }
}

/****************************************************************************
  Create a new drawing data.
****************************************************************************/
static struct drawing_data *drawing_data_new(void)
{
  struct drawing_data *draw = fc_calloc(1, sizeof(*draw));

  draw->name = NULL;

  return draw;
}

/****************************************************************************
  Free a drawing data.
****************************************************************************/
static void drawing_data_destroy(struct drawing_data *draw)
{
  int i;

  fc_assert_ret(NULL != draw);

  if (draw->name != NULL) {
    free(draw->name);
  }
  for (i = 0; i < 4; i++) {
    if (draw->blend[i]) {
      free_sprite(draw->blend[i]);
    }
  }
  for (i = 0; i < draw->num_layers; i++) {
    int vec_size = sprite_vector_size(&draw->layer[i].allocated);
    int j;

    for (j = 0; j < vec_size; j++) {
      free_sprite(draw->layer[i].allocated.p[j]);
    }

    sprite_vector_free(&draw->layer[i].base);
    sprite_vector_free(&draw->layer[i].allocated);
    free(draw->layer[i].cells);
  }
  free(draw);
}

/****************************************************************************
  Return unscaled tileset if it exists, or default otherwise
****************************************************************************/
struct tileset* get_tileset(void)
{
  if (unscaled_tileset != NULL) {
    return unscaled_tileset;
  } else {
    return tileset;
  }
}

/****************************************************************************
  Return the name of the given tileset.
****************************************************************************/
const char *tileset_basename(const struct tileset *t)
{
  return t->name;
}

/****************************************************************************
  Return whether the current tileset is isometric.
****************************************************************************/
bool tileset_is_isometric(const struct tileset *t)
{
  return t->type == TS_ISOMETRIC;
}

/****************************************************************************
  Return the hex_width of the current tileset. For iso-hex tilesets this
  value will be > 0 and is_isometric will be set.
****************************************************************************/
int tileset_hex_width(const struct tileset *t)
{
  return t->hex_width;
}

/****************************************************************************
  Return the hex_height of the current tileset. For hex tilesets this
  value will be > 0 and is_isometric will be set.
****************************************************************************/
int tileset_hex_height(const struct tileset *t)
{
  return t->hex_height;
}

/****************************************************************************
  Return the tile width of the current tileset.  This is the tesselation
  width of the tiled plane.  This means it's the width of the bounding box
  of the basic map tile.

  For best results:
    - The value should be even (or a multiple of 4 in iso-view).
    - In iso-view, the width should be twice the height (to give a
      perspective of 30 degrees above the horizon).
    - In non-iso-view, width and height should be equal (overhead
      perspective).
    - In hex or iso-hex view, remember this is the tesselation vector.
      hex_width and hex_height then give the size of the side of the
      hexagon.  Calculating the dimensions of a "regular" hexagon or
      iso-hexagon may be tricky.
  However these requirements are not absolute and callers should not
  depend on them (although some do).
****************************************************************************/
int tileset_tile_width(const struct tileset *t)
{
  return t->normal_tile_width;
}

/****************************************************************************
  Return the tile height of the current tileset.  This is the tesselation
  height of the tiled plane.  This means it's the height of the bounding box
  of the basic map tile.

  See also tileset_tile_width.
****************************************************************************/
int tileset_tile_height(const struct tileset *t)
{
  return t->normal_tile_height;
}

/****************************************************************************
  Return the full tile width of the current tileset.  This is the maximum
  width that any mapview sprite will have.

  Note: currently this is always equal to the tile width.
****************************************************************************/
int tileset_full_tile_width(const struct tileset *t)
{
  return t->full_tile_width;
}

/****************************************************************************
  Return the full tile height of the current tileset.  This is the maximum
  height that any mapview sprite will have.  This may be greater than the
  tile width in which case the extra area is above the "normal" tile.

  Some callers assume the full height is 50% larger than the height in
  iso-view, and equal in non-iso view.
****************************************************************************/
int tileset_full_tile_height(const struct tileset *t)
{
  return t->full_tile_height;
}

/****************************************************************************
  Return the unit tile width of the current tileset.
****************************************************************************/
int tileset_unit_width(const struct tileset *t)
{
  return t->unit_tile_width;
}

/****************************************************************************
  Return the unit tile height of the current tileset.
****************************************************************************/
int tileset_unit_height(const struct tileset *t)
{
  return t->unit_tile_height;
}

/****************************************************************************
  Calculate the height of a unit upkeep icons.
****************************************************************************/
static int calculate_max_upkeep_height(const struct tileset *t)
{
  int i;
  int max = 0;

  for (i = 0; i < MAX_NUM_UPKEEP_SPRITES; i++) {
    if (t->sprites.upkeep.unhappy[i] != NULL) {
      int width, height;

      /* TODO: We want only height, getting the width might waste CPU
       * depending on gui-specific implementation. */
      get_sprite_dimensions(t->sprites.upkeep.unhappy[i], &width, &height);

      max = MAX(max, height);
    }
  }

  output_type_iterate(o) {
    for (i = 0; i < MAX_NUM_UPKEEP_SPRITES; i++) {
      if (t->sprites.upkeep.output[o][i] != NULL) {
        int width, height;

        /* TODO: We want only height, getting the width might waste CPU
         * depending on gui-specific implementation. */
        get_sprite_dimensions(t->sprites.upkeep.output[o][i], &width, &height);

        max = MAX(max, height);
      }
    }
  } output_type_iterate_end;

  return max;
}

/****************************************************************************
  Get the height of a unit upkeep icons.
****************************************************************************/
static int tileset_upkeep_height(const struct tileset *t)
{
  /* Return cached value */
  return t->max_upkeep_height;
}

/****************************************************************************
  Suitable canvas height for a unit icon that includes upkeep sprites.
****************************************************************************/
int tileset_unit_with_upkeep_height(const struct tileset *t)
{
  int uk_bottom = tileset_unit_layout_offset_y(tileset) + tileset_upkeep_height(tileset);
  int u_bottom = tileset_unit_height(tileset);

  return MAX(uk_bottom, u_bottom);
}

/****************************************************************************
  Suitable canvas height for a unit icon that includes upkeep sprites,
  using small space layout.
****************************************************************************/
int tileset_unit_with_small_upkeep_height(const struct tileset *t)
{
  return tileset_unit_layout_small_offset_y(tileset) + tileset_upkeep_height(tileset);
}

/****************************************************************************
  Offset to layout extra unit sprites, such as upkeep.
****************************************************************************/
int tileset_unit_layout_offset_y(const struct tileset *t)
{
  return t->unit_upkeep_offset_y;
}

/****************************************************************************
  Offset to layout extra unit sprites, such as upkeep, requesting small
  space layout.
****************************************************************************/
int tileset_unit_layout_small_offset_y(const struct tileset *t)
{
  return t->unit_upkeep_small_offset_y;
}

/****************************************************************************
  Return the small sprite width of the current tileset.  The small sprites
  are used for various theme graphics (e.g., citymap citizens/specialists
  as well as panel indicator icons).
****************************************************************************/
int tileset_small_sprite_width(const struct tileset *t)
{
  return t->small_sprite_width;
}

/****************************************************************************
  Return the offset from the origin of the city tile at which to place the
  city bar text.
****************************************************************************/
int tileset_citybar_offset_y(const struct tileset *t)
{
  return t->citybar_offset_y;
}

/****************************************************************************
  Return the offset from the origin of the tile at which to place the
  label text.
****************************************************************************/
int tileset_tilelabel_offset_y(const struct tileset *t)
{
  return t->tilelabel_offset_y;
}

/****************************************************************************
  Returns tileset scale
****************************************************************************/
float tileset_scale(const struct tileset *t)
{
  return tileset->scale;
}

/****************************************************************************
  Return the small sprite height of the current tileset.  The small sprites
  are used for various theme graphics (e.g., citymap citizens/specialists
  as well as panel indicator icons).
****************************************************************************/
int tileset_small_sprite_height(const struct tileset *t)
{
  return t->small_sprite_height;
}

/****************************************************************************
  Return the path within the data directories where the main intro graphics
  file can be found.  (It is left up to the GUI code to load and unload this
  file.)
****************************************************************************/
const char *tileset_main_intro_filename(const struct tileset *t)
{
  return t->main_intro_filename;
}

/****************************************************************************
  Return the path within the data directories where the mini intro graphics
  file can be found.  (It is left up to the GUI code to load and unload this
  file.)
****************************************************************************/
const char *tileset_mini_intro_filename(const struct tileset *t)
{
  return t->minimap_intro_filename;
}

/****************************************************************************
  Return the number of possible colors for city overlays.
****************************************************************************/
int tileset_num_city_colors(const struct tileset *t)
{
  return t->sprites.city.worked_tile_overlay.size;
}

/****************************************************************************
  Return TRUE if the client will use the code to generate the fog.
****************************************************************************/
bool tileset_use_hard_coded_fog(const struct tileset *t)
{
  return FOG_AUTO == t->fogstyle;
}

/**************************************************************************
  Initialize.
**************************************************************************/
static struct tileset *tileset_new(void)
{
  struct tileset *t = fc_calloc(1, sizeof(*t));

  t->specfiles = specfile_list_new();
  t->small_sprites = small_sprite_list_new();
  return t;
}

/**************************************************************************
  Return the tileset name of the direction.  This is similar to
  dir_get_name but you shouldn't change this or all tilesets will break.
**************************************************************************/
static const char *dir_get_tileset_name(enum direction8 dir)
{
  switch (dir) {
  case DIR8_NORTH:
    return "n";
  case DIR8_NORTHEAST:
    return "ne";
  case DIR8_EAST:
    return "e";
  case DIR8_SOUTHEAST:
    return "se";
  case DIR8_SOUTH:
    return "s";
  case DIR8_SOUTHWEST:
    return "sw";
  case DIR8_WEST:
    return "w";
  case DIR8_NORTHWEST:
    return "nw";
  }
  log_error("Wrong direction8 variant: %d.", dir);
  return "";
}

/****************************************************************************
  Return TRUE iff the dir is valid in this tileset.
****************************************************************************/
static bool is_valid_tileset_dir(const struct tileset *t,
				 enum direction8 dir)
{
  if (t->hex_width > 0) {
    return dir != DIR8_NORTHEAST && dir != DIR8_SOUTHWEST;
  } else if (t->hex_height > 0) {
    return dir != DIR8_NORTHWEST && dir != DIR8_SOUTHEAST;
  } else {
    return TRUE;
  }
}

/****************************************************************************
  Return TRUE iff the dir is cardinal in this tileset.

  "Cardinal", in this sense, means that a tile will share a border with
  another tile in the direction rather than sharing just a single vertex.
****************************************************************************/
static bool is_cardinal_tileset_dir(const struct tileset *t,
				    enum direction8 dir)
{
  if (t->hex_width > 0 || t->hex_height > 0) {
    return is_valid_tileset_dir(t, dir);
  } else {
    return (dir == DIR8_NORTH || dir == DIR8_EAST
            || dir == DIR8_SOUTH || dir == DIR8_WEST);
  }
}

/**********************************************************************
  Convert properties of the actual topology to an index of different
  tileset topology types.
***********************************************************************/
static int ts_topology_index(int actual_topology)
{
  int idx;

  if ((actual_topology & TF_HEX)
      && (actual_topology & TF_ISO)) {
    idx = TS_TOPO_ISOHEX;
  } else if (actual_topology & TF_ISO) {
    idx = TS_TOPO_ISO;
  } else if (actual_topology & TF_HEX) {
    idx = TS_TOPO_HEX;
  } else {
    idx = TS_TOPO_OVERHEAD;
  }

  return idx;
}

/**********************************************************************
  Returns a static list of tilesets available on the system by
  searching all data directories for files matching TILESPEC_SUFFIX.
***********************************************************************/
const struct strvec *get_tileset_list(const struct option *poption)
{
  static struct strvec *tilesets[4] = { NULL, NULL, NULL, NULL };
  int topo = option_get_cb_data(poption);
  int idx;

  idx = ts_topology_index(topo);

  fc_assert_ret_val(idx < ARRAY_SIZE(tilesets), NULL);

  if (tilesets[idx] == NULL) {
    /* Note: this means you must restart the client after installing a new
       tileset. */
    struct strvec *list = fileinfolist(get_data_dirs(), TILESPEC_SUFFIX);

    tilesets[idx] = strvec_new();
    strvec_iterate(list, file) {
      struct tileset *t = tileset_read_toplevel(file, FALSE, topo, 1.0f);

      if (t) {
        strvec_append(tilesets[idx], file);
        tileset_free(t);
      }
    } strvec_iterate_end;
    strvec_destroy(list);
  }

  return tilesets[idx];
}

/**********************************************************************
  Gets full filename for tilespec file, based on input name.
  Returned data is allocated, and freed by user as required.
  Input name may be null, in which case uses default.
  Falls back to default if can't find specified name;
  dies if can't find default.
***********************************************************************/
static char *tilespec_fullname(const char *tileset_name)
{
  if (tileset_name) {
    char fname[strlen(tileset_name) + strlen(TILESPEC_SUFFIX) + 1];
    const char *dname;

    fc_snprintf(fname, sizeof(fname),
                "%s%s", tileset_name, TILESPEC_SUFFIX);

    dname = fileinfoname(get_data_dirs(), fname);

    if (dname) {
      return fc_strdup(dname);
    }
  }

  return NULL;
}

/**********************************************************************
  Checks options in filename match what we require and support.
  Die if not.
  'which' should be "tilespec" or "spec".
***********************************************************************/
static bool check_tilespec_capabilities(struct section_file *file,
					const char *which,
					const char *us_capstr,
					const char *filename,
                                        bool verbose)
{
  enum log_level level = verbose ? LOG_ERROR : LOG_DEBUG;

  const char *file_capstr = secfile_lookup_str(file, "%s.options", which);

  if (NULL == file_capstr) {
    log_base(level, "\"%s\": %s file doesn't have a capability string",
             filename, which);
    return FALSE;
  }
  if (!has_capabilities(us_capstr, file_capstr)) {
    log_base(level, "\"%s\": %s file appears incompatible:",
             filename, which);
    log_base(level, "  datafile options: %s", file_capstr);
    log_base(level, "  supported options: %s", us_capstr);
    return FALSE;
  }
  if (!has_capabilities(file_capstr, us_capstr)) {
    log_base(level, "\"%s\": %s file requires option(s) "
             "that client doesn't support:", filename, which);
    log_base(level, "  datafile options: %s", file_capstr);
    log_base(level, "  supported options: %s", us_capstr);
    return FALSE;
  }

  return TRUE;
}

/**********************************************************************
  Frees the tilespec toplevel data, in preparation for re-reading it.

  See tilespec_read_toplevel().
***********************************************************************/
static void tileset_free_toplevel(struct tileset *t)
{
  int i, j;

  if (t->main_intro_filename) {
    free(t->main_intro_filename);
    t->main_intro_filename = NULL;
  }
  if (t->minimap_intro_filename) {
    free(t->minimap_intro_filename);
    t->minimap_intro_filename = NULL;
  }
  
  if (t->preferred_themes) {
    for (i = 0; i < t->num_preferred_themes; i++) {
      free(t->preferred_themes[i]);
    }
    free(t->preferred_themes);
    t->preferred_themes = NULL;
  }
  t->num_preferred_themes = 0;

  if (t->tile_hash) {
    drawing_hash_destroy(t->tile_hash);
    t->tile_hash = NULL; /* Helpful for sanity. */
  }
  if (t->estyle_hash) {
    estyle_hash_destroy(t->estyle_hash);
    t->estyle_hash = NULL;
  }
  for (i = 0; i < ESTYLE_COUNT; i++) {
    if (t->style_lists[i] != NULL) {
      extra_type_list_destroy(t->style_lists[i]);
      t->style_lists[i] = NULL;
    }
  }

  if (t->flagged_bases_list != NULL) {
    extra_type_list_destroy(t->flagged_bases_list);
    t->flagged_bases_list = NULL;
  }
 
  for (i = 0; i < MAX_NUM_LAYERS; i++) {
    struct tileset_layer *tslp = &t->layers[i];

    if (tslp->match_types) {
      for (j = 0; j < tslp->match_count; j++) {
	free(tslp->match_types[j]);
      }
      free(tslp->match_types);
      tslp->match_types = NULL;
    }
  }

  if (t->color_system) {
    color_system_free(t->color_system);
    t->color_system = NULL;
  }

  if (t->summary != NULL) {
    free(t->summary);
    t->summary = NULL;
  }
  if (t->description != NULL) {
    free(t->description);
    t->description = NULL;
  }
}

/**************************************************************************
  Clean up.
**************************************************************************/
void tileset_free(struct tileset *t)
{
  int i;

  tileset_free_tiles(t);
  tileset_free_toplevel(t);
  for (i = 0; i < ARRAY_SIZE(t->sprites.player); i++) {
    tileset_player_free(t, i);
  }
  specfile_list_destroy(t->specfiles);
  small_sprite_list_destroy(t->small_sprites);
  free(t);
}

/**********************************************************************
  Read a new tilespec in when first starting the game.

  Call this function with the (guessed) name of the tileset, when
  starting the client.

  Returns TRUE iff tileset with suggested tileset_name was loaded.
***********************************************************************/
bool tilespec_try_read(const char *tileset_name, bool verbose, int topo_id,
                       bool global_default)
{
  bool original;

  if (tileset_name == NULL
      || !(tileset = tileset_read_toplevel(tileset_name, verbose,
                                           topo_id, 1.0f))) {
    struct strvec *list = fileinfolist(get_data_dirs(), TILESPEC_SUFFIX);

    original = FALSE;
    strvec_iterate(list, file) {
      struct tileset *t = tileset_read_toplevel(file, FALSE, topo_id, 1.0f);

      if (t) {
        if (!tileset) {
          tileset = t;
        } else if (t->priority > tileset->priority
                   || (topo_id >= 0
                       && tileset_topo_index(tileset) != tileset_topo_index(t))) {
          tileset_free(tileset);
          tileset = t;
        } else {
          tileset_free(t);
        }
      }
    } strvec_iterate_end;
    strvec_destroy(list);

    if (!tileset) {
      tileset_error(LOG_FATAL, _("No usable default tileset found, aborting!"));
    }

    log_verbose("Trying tileset \"%s\".", tileset->name);
  } else {
    original = TRUE;
  }
  option_set_default_ts(tileset);

  if (global_default) {
    sz_strlcpy(gui_options.default_tileset_name, tileset_basename(tileset));
  }

  return original;
}

/**********************************************************************
  Read a new tilespec in from scratch.

  Unlike the initial reading code, which reads pieces one at a time,
  this gets rid of the old data and reads in the new all at once.  If the
  new tileset fails to load the old tileset may be reloaded; otherwise the
  client will exit.  If a NULL name is given the current tileset will be
  reread.

  It will also call the necessary functions to redraw the graphics.

  Returns TRUE iff new tileset has been succesfully loaded.
***********************************************************************/
bool tilespec_reread(const char *new_tileset_name,
                     bool game_fully_initialized, float scale)
{
  int id;
  struct tile *center_tile;
  enum client_states state = client_state();
  const char *name = new_tileset_name ? new_tileset_name : tileset->name;
  char tileset_name[strlen(name) + 1], old_name[strlen(tileset->name) + 1];
  bool new_tileset_in_use;

  /* Make local copies since these values may be freed down below */
  sz_strlcpy(tileset_name, name);
  sz_strlcpy(old_name, tileset->name);

  log_normal(_("Loading tileset \"%s\"."), tileset_name);

  /* Step 0:  Record old data.
   *
   * We record the current mapcanvas center, etc.
   */
  center_tile = get_center_tile_mapcanvas();

  /* Step 1:  Cleanup.
   *
   * Free old tileset or keep it in memeory if we are loading the same
   * tileset with scaling and old one was not scaled.
   */

  if (strcmp(tileset_name, old_name) == 0 && tileset->scale == 1.0f
      && scale != 1.0f) {
    if (unscaled_tileset) {
      tileset_free(unscaled_tileset);
    }
    unscaled_tileset = tileset;
  } else {
    tileset_free(tileset);
  }

  /* Step 2:  Read.
   *
   * We read in the new tileset.  This should be pretty straightforward.
   */
  tileset = tileset_read_toplevel(tileset_name, FALSE, -1, scale);
  if (tileset != NULL) {
    new_tileset_in_use = TRUE;
  } else {
    new_tileset_in_use = FALSE;

    if (!(tileset = tileset_read_toplevel(old_name, FALSE, -1, scale))) {
      /* Always fails. */
      fc_assert_exit_msg(NULL != tileset,
                         "Failed to re-read the currently loaded tileset.");
    }
  }
  tileset_load_tiles(tileset);
  if (game_fully_initialized) {
    tileset_use_preferred_theme(tileset);
  }

  if (game_fully_initialized) {
    if (game.client.ruleset_ready) {
      tileset_background_init(tileset);
    } /* else we'll get round to it on PACKET_RULESET_GAME */
    players_iterate(pplayer) {
      tileset_player_init(tileset, pplayer);
    } players_iterate_end;
    boot_help_texts(); /* "About Current Tileset" */
  }

  /* Step 3: Setup
   *
   * This is a seriously sticky problem.  On startup, we build a hash
   * from all the sprite data. Then, when we connect to a server, the
   * server sends us ruleset data a piece at a time and we use this data
   * to assemble the sprite structures.  But if we change while connected
   *  we have to reassemble all of these.  This should just involve
   * calling tilespec_setup_*** on everything.  But how do we tell what
   * "everything" is?
   *
   * The below code just does things straightforwardly, by setting up
   * each possible sprite again.  Hopefully it catches everything, and
   * doesn't mess up too badly if we change tilesets while not connected
   * to a server.
   */
  if (!game.client.ruleset_ready) {
    /* The ruleset data is not sent until this point. */
    return new_tileset_in_use;
  }

  if (tileset_map_topo_compatible(game.map.topology_id, tileset)
      == TOPO_INCOMP_HARD) {
    tileset_error(LOG_NORMAL, _("Map topology and tileset incompatible."));
  }

  terrain_type_iterate(pterrain) {
    tileset_setup_tile_type(tileset, pterrain);
  } terrain_type_iterate_end;
  resource_type_iterate(presource) {
    tileset_setup_resource(tileset, presource);
  } resource_type_iterate_end;
  unit_type_iterate(punittype) {
    tileset_setup_unit_type(tileset, punittype);
  } unit_type_iterate_end;
  governments_iterate(gov) {
    tileset_setup_government(tileset, gov);
  } governments_iterate_end;
  extra_type_iterate(pextra) {
    tileset_setup_extra(tileset, pextra);
  } extra_type_iterate_end;
  nations_iterate(pnation) {
    tileset_setup_nation_flag(tileset, pnation);
  } nations_iterate_end;
  improvement_iterate(pimprove) {
    tileset_setup_impr_type(tileset, pimprove);
  } improvement_iterate_end;
  advance_iterate(A_FIRST, padvance) {
    tileset_setup_tech_type(tileset, padvance);
  } advance_iterate_end;
  specialist_type_iterate(sp) {
    tileset_setup_specialist_type(tileset, sp);
  } specialist_type_iterate_end;

  for (id = 0; id < game.control.styles_count; id++) {
    tileset_setup_city_tiles(tileset, id);
  }

  if (state < C_S_RUNNING) {
    /* Below redraws do not apply before this. */
    return new_tileset_in_use;
  }

  /* Step 4:  Draw.
   *
   * Do any necessary redraws.
   */
  generate_citydlg_dimensions();
  tileset_changed();
  can_slide = FALSE;
  center_tile_mapcanvas(center_tile);
  /* update_map_canvas_visible forces a full redraw.  Otherwise with fast
   * drawing we might not get one.  Of course this is slower. */
  update_map_canvas_visible();
  can_slide = TRUE;

  return new_tileset_in_use;
}

/**************************************************************************
  This is merely a wrapper for tilespec_reread (above) for use in
  options.c and the client local options dialog.
**************************************************************************/
void tilespec_reread_callback(struct option *poption)
{
  const char *tileset_name;
  enum client_states state = client_state();

  if ((state == C_S_RUNNING || state == C_S_OVER)
      && option_get_cb_data(poption) != (game.map.topology_id & (TF_ISO | TF_HEX))) {
    /* Changed option was not for current topology */
    return;
  }

  tileset_name = option_str_get(poption);

  /* As it's going to be 'current' tileset, make it global default if
   * options saved. */
  sz_strlcpy(gui_options.default_tileset_name, tileset_name);

  fc_assert_ret(NULL != tileset_name && tileset_name[0] != '\0');
  tileset_update = TRUE;
  tilespec_reread(tileset_name, client.conn.established, 1.0f);
  tileset_update = FALSE;
  menus_init();
}

/**************************************************************************
  Read a new tilespec in from scratch. Keep UI frozen while tileset is
  partially loaded; in inconsistent state.

  See tilespec_reread() for details.
**************************************************************************/
void tilespec_reread_frozen_refresh(const char *tname)
{
  tileset_update = TRUE;
  tilespec_reread(tname, TRUE, 1.0f);
  tileset_update = FALSE;
  menus_init();
}

/**************************************************************************
  Loads the given graphics file (found in the data path) into a newly
  allocated sprite.
**************************************************************************/
static struct sprite *load_gfx_file(const char *gfx_filename)
{
  const char **gfx_fileexts = gfx_fileextensions(), *gfx_fileext;
  struct sprite *s;

  /* Try out all supported file extensions to find one that works. */
  while ((gfx_fileext = *gfx_fileexts++)) {
    const char *real_full_name;
    char full_name[strlen(gfx_filename) + strlen(".")
                   + strlen(gfx_fileext) + 1];

    sprintf(full_name, "%s.%s", gfx_filename, gfx_fileext);
    if ((real_full_name = fileinfoname(get_data_dirs(), full_name))) {
      log_debug("trying to load gfx file \"%s\".", real_full_name);
      s = load_gfxfile(real_full_name);
      if (s) {
	return s;
      }
    }
  }

  log_error("Could not load gfx file \"%s\".", gfx_filename);
  return NULL;
}

/**************************************************************************
  Ensure that the big sprite of the given spec file is loaded.
**************************************************************************/
static void ensure_big_sprite(struct specfile *sf)
{
  struct section_file *file;
  const char *gfx_filename;

  if (sf->big_sprite) {
    /* Looks like it's already loaded. */
    return;
  }

  /* Otherwise load it.  The big sprite will sometimes be freed and will have
   * to be reloaded, but most of the time it's just loaded once, the small
   * sprites are extracted, and then it's freed. */
  if (!(file = secfile_load(sf->file_name, TRUE))) {
    tileset_error(LOG_FATAL, _("Could not open '%s':\n%s"), sf->file_name, secfile_error());
  }

  if (!check_tilespec_capabilities(file, "spec",
				   SPEC_CAPSTR, sf->file_name, TRUE)) {
    tileset_error(LOG_FATAL, _("Incompatible tileset capabilities"));
  }

  gfx_filename = secfile_lookup_str(file, "file.gfx");

  sf->big_sprite = load_gfx_file(gfx_filename);

  if (!sf->big_sprite) {
    tileset_error(LOG_FATAL, _("Could not load gfx file for the spec file \"%s\"."),
                  sf->file_name);
  }
  secfile_destroy(file);
}

/**************************************************************************
  Scan all sprites declared in the given specfile.  This means that the
  positions of the sprites in the big_sprite are saved in the
  small_sprite structs.
**************************************************************************/
static void scan_specfile(struct tileset *t, struct specfile *sf,
			  bool duplicates_ok)
{
  struct section_file *file;
  struct section_list *sections;
  int i;

  if (!(file = secfile_load(sf->file_name, TRUE))) {
    tileset_error(LOG_FATAL, _("Could not open '%s':\n%s"), sf->file_name, secfile_error());
  }
  if (!check_tilespec_capabilities(file, "spec",
				   SPEC_CAPSTR, sf->file_name, TRUE)) {
    tileset_error(LOG_FATAL, _("Specfile %s has incompatible capabilities"), sf->file_name);
  }

  /* Currently unused */
  (void) secfile_entry_lookup(file, "info.artists");

  /* Not used here */
  (void) secfile_entry_lookup(file, "file.gfx");

  if ((sections = secfile_sections_by_name_prefix(file, "grid_"))) {
    section_list_iterate(sections, psection) {
      int j, k;
      int x_top_left, y_top_left, dx, dy;
      int pixel_border_x;
      int pixel_border_y;
      const char *sec_name = section_name(psection);

      pixel_border_x = secfile_lookup_int_default(file, 0, "%s.pixel_border",
                                                  sec_name);
      pixel_border_y = secfile_lookup_int_default(file, pixel_border_x, "%s.pixel_border_y",
                                                  sec_name);
      pixel_border_x = secfile_lookup_int_default(file, pixel_border_x, "%s.pixel_border_x",
                                                  sec_name);
      if (!secfile_lookup_int(file, &x_top_left, "%s.x_top_left", sec_name)
          || !secfile_lookup_int(file, &y_top_left,
                                 "%s.y_top_left", sec_name)
          || !secfile_lookup_int(file, &dx, "%s.dx", sec_name)
          || !secfile_lookup_int(file, &dy, "%s.dy", sec_name)) {
        log_error("Grid \"%s\" invalid: %s", sec_name, secfile_error());
        continue;
      }

      j = -1;
      while (NULL != secfile_entry_lookup(file, "%s.tiles%d.tag",
                                          sec_name, ++j)) {
        struct small_sprite *ss;
        int row, column;
        int xr, yb;
        const char **tags;
        size_t num_tags;
        int hot_x, hot_y;

        if (!secfile_lookup_int(file, &row, "%s.tiles%d.row", sec_name, j)
            || !secfile_lookup_int(file, &column, "%s.tiles%d.column",
                                   sec_name, j)
            || !(tags = secfile_lookup_str_vec(file, &num_tags,
                                               "%s.tiles%d.tag",
                                               sec_name, j))) {
          log_error("Small sprite \"%s.tiles%d\" invalid: %s",
                    sec_name, j, secfile_error());
          continue;
        }
        hot_x = secfile_lookup_int_default(file, 0, "%s.tiles%d.hot_x",
                                           sec_name, j);
        hot_y = secfile_lookup_int_default(file, 0, "%s.tiles%d.hot_y",
                                           sec_name, j);

        /* there must be at least 1 because of the while(): */
        fc_assert_action(num_tags > 0, continue);

        xr = x_top_left + (dx + pixel_border_x) * column;
        yb = y_top_left + (dy + pixel_border_y) * row;

        ss = fc_malloc(sizeof(*ss));
        ss->ref_count = 0;
        ss->file = NULL;
        ss->x = xr;
        ss->y = yb;
        ss->width = dx;
        ss->height = dy;
        ss->sf = sf;
        ss->sprite = NULL;
        ss->hot_x = hot_x;
        ss->hot_y = hot_y;

        small_sprite_list_prepend(t->small_sprites, ss);

        if (!duplicates_ok) {
          for (k = 0; k < num_tags; k++) {
            if (!sprite_hash_insert(t->sprite_hash, tags[k], ss)) {
              log_error("warning: already have a sprite for \"%s\".",
                        tags[k]);
            }
          }
        } else {
          for (k = 0; k < num_tags; k++) {
            (void) sprite_hash_replace(t->sprite_hash, tags[k], ss);
          }
        }

        free(tags);
        tags = NULL;
      }
    } section_list_iterate_end;
    section_list_destroy(sections);
  }

  /* Load "extra" sprites.  Each sprite is one file. */
  i = -1;
  while (NULL != secfile_entry_lookup(file, "extra.sprites%d.tag", ++i)) {
    struct small_sprite *ss;
    const char **tags;
    const char *filename;
    size_t num_tags, k;
    int hot_x, hot_y;

    if (!(tags = secfile_lookup_str_vec(file, &num_tags,
                                        "extra.sprites%d.tag", i))
        || !(filename = secfile_lookup_str(file,
                                           "extra.sprites%d.file", i))) {
      log_error("Extra sprite \"extra.sprites%d\" invalid: %s",
                i, secfile_error());
      continue;
    }
    hot_x = secfile_lookup_int_default(file, 0, "extra.sprites%d.hot_x", i);
    hot_y = secfile_lookup_int_default(file, 0, "extra.sprites%d.hot_y", i);

    ss = fc_malloc(sizeof(*ss));
    ss->ref_count = 0;
    ss->file = fc_strdup(filename);
    ss->sf = NULL;
    ss->sprite = NULL;
    ss->hot_x = hot_x;
    ss->hot_y = hot_y;

    small_sprite_list_prepend(t->small_sprites, ss);

    if (!duplicates_ok) {
      for (k = 0; k < num_tags; k++) {
        if (!sprite_hash_insert(t->sprite_hash, tags[k], ss)) {
          log_error("warning: already have a sprite for \"%s\".", tags[k]);
        }
      }
    } else {
      for (k = 0; k < num_tags; k++) {
        (void) sprite_hash_replace(t->sprite_hash, tags[k], ss);
      }
    }
    free(tags);
  }

  secfile_check_unused(file);
  secfile_destroy(file);
}

/**********************************************************************
  Returns the correct name of the gfx file (with path and extension)
  Must be free'd when no longer used
***********************************************************************/
static char *tilespec_gfx_filename(const char *gfx_filename)
{
  const char  *gfx_current_fileext;
  const char **gfx_fileexts = gfx_fileextensions();

  while ((gfx_current_fileext = *gfx_fileexts++)) {
    const char *real_full_name;
    char *full_name =
      fc_malloc(strlen(gfx_filename) + strlen(".")
                + strlen(gfx_current_fileext) + 1);

    sprintf(full_name, "%s.%s", gfx_filename, gfx_current_fileext);

    real_full_name = fileinfoname(get_data_dirs(), full_name);
    free(full_name);
    if (real_full_name) {
      return fc_strdup(real_full_name);
    }
  }

  tileset_error(LOG_FATAL, _("Couldn't find a supported gfx file extension for \"%s\"."),
                gfx_filename);

  return NULL;
}

/**********************************************************************
  Determine the sprite_type string.
***********************************************************************/
static int check_sprite_type(const char *sprite_type, const char *tile_section)
{
  if (fc_strcasecmp(sprite_type, "corner") == 0) {
    return CELL_CORNER;
  }
  if (fc_strcasecmp(sprite_type, "single") == 0) {
    return CELL_WHOLE;
  }
  if (fc_strcasecmp(sprite_type, "whole") == 0) {
    return CELL_WHOLE;
  }
  log_error("[%s] unknown sprite_type \"%s\".", tile_section, sprite_type);
  return CELL_WHOLE;
}

/**********************************************************************
  Finds and reads the toplevel tilespec file based on given name.
  Sets global variables, including tile sizes and full names for
  intro files.
  topology_id of -1 means any topology is acceptable.
***********************************************************************/
struct tileset *tileset_read_toplevel(const char *tileset_name, bool verbose,
                                      int topology_id, float scale)
{
  struct section_file *file;
  char *fname;
  const char *c;
  int i;
  size_t num_spec_files;
  const char **spec_filenames;
  size_t num_preferred_themes;
  struct section_list *sections = NULL;
  const char *file_capstr;
  bool duplicates_ok, is_hex;
  enum direction8 dir;
  struct tileset *t = NULL;
  const char *extraname;
  const char *tstr;
  int topo;

  fname = tilespec_fullname(tileset_name);
  if (!fname) {
    if (verbose) {
      log_error("Can't find tileset \"%s\".", tileset_name); 
    }
    return NULL;
  }
  log_verbose("tilespec file is \"%s\".", fname);

  if (!(file = secfile_load(fname, TRUE))) {
    log_error("Could not open '%s':\n%s", fname, secfile_error());
    free(fname);
    return NULL;
  }

  if (!check_tilespec_capabilities(file, "tilespec",
                                   TILESPEC_CAPSTR, fname, verbose)) {
    secfile_destroy(file);
    free(fname);
    return NULL;
  }

  t = tileset_new();
  t->scale = scale;
  file_capstr = secfile_lookup_str(file, "%s.options", "tilespec");
  duplicates_ok = (NULL != file_capstr
                   && has_capabilities("+duplicates_ok", file_capstr));

  tstr = secfile_lookup_str(file, "tilespec.name");
  /* Tileset name found */
  sz_strlcpy(t->given_name, tstr);
  tstr = secfile_lookup_str_default(file, "", "tilespec.version");
  if (tstr[0] != '\0') {
    /* Tileset version found */
    sz_strlcpy(t->version, tstr);
  } else {
    /* No version information */
    t->version[0] = '\0';
  }

  tstr = secfile_lookup_str_default(file, "", "tilespec.summary");
  if (tstr[0] != '\0') {
    int len;

    /* Tileset summary found */
    len = strlen(tstr);
    t->summary = fc_malloc(len + 1);
    fc_strlcpy(t->summary, tstr, len + 1);
  } else {
    /* No summary */
    if (t->summary != NULL) {
      free(t->summary);
      t->summary = NULL;
    }
  }

  tstr = secfile_lookup_str_default(file, "", "tilespec.description");
  if (tstr[0] != '\0') {
    int len;

    /* Tileset description found */
    len = strlen(tstr);
    t->description = fc_malloc(len + 1);
    fc_strlcpy(t->description, tstr, len + 1);
  } else {
    /* No description */
    if (t->description != NULL) {
      free(t->description);
      t->description = NULL;
    }
  }

  sz_strlcpy(t->name, tileset_name);
  if (!secfile_lookup_int(file, &t->priority, "tilespec.priority")
      || !secfile_lookup_bool(file, &is_hex, "tilespec.is_hex")) {
    log_error("Tileset \"%s\" invalid: %s", t->name, secfile_error());
    goto ON_ERROR;
  }

  tstr = secfile_lookup_str(file, "tilespec.type");
  if (tstr == NULL) {
    log_error("Tileset \"%s\": no tileset type", t->name);
    goto ON_ERROR;
  }

  t->type = ts_type_by_name(tstr, fc_strcasecmp);
  if (!ts_type_is_valid(t->type)) {
    log_error("Tileset \"%s\": unknown tileset type \"%s\"", t->name, tstr);
    goto ON_ERROR;
  }

  if (t->type == TS_ISOMETRIC) {
    topo = TF_ISO;
  } else {
    topo = 0;
  }

  /* Read hex-tileset information. */
  t->hex_width = t->hex_height = 0;
  if (is_hex) {
    int hex_side;

    if (!secfile_lookup_int(file, &hex_side, "tilespec.hex_side")) {
      log_error("Tileset \"%s\" invalid: %s", t->name, secfile_error());
      goto ON_ERROR;
    }
    hex_side = hex_side * t->scale;
    if (t->type == TS_ISOMETRIC) {
      t->hex_width = hex_side;
    } else {
      t->hex_height = hex_side;
    }

    topo |= TF_HEX;

    /* Hex tilesets are drawn the same as isometric. */
    /* FIXME: There will be other legal values to be used with hex
     * tileset in the future, and this would just overwrite it. */
    t->type = TS_ISOMETRIC;
  }

  if (topology_id >= 0 && topo != (topology_id & (TF_ISO | TF_HEX))) {
    /* Not of requested topology */
    goto ON_ERROR;
  }

  t->ts_topo_idx = ts_topology_index(topo);

  if (!is_view_supported(t->type)) {
    /* TRANS: "Overhead" or "Isometric" */
    log_normal(_("Client does not support %s tilesets."),
               _(ts_type_name(t->type)));
    log_normal(_("Using default tileset instead."));
    fc_assert(tileset_name != NULL);
    goto ON_ERROR;
  }

  /* Create arrays of valid and cardinal tileset dirs.  These depend
   * entirely on the tileset, not the topology.  They are also in clockwise
   * rotational ordering. */
  t->num_valid_tileset_dirs = t->num_cardinal_tileset_dirs = 0;
  dir = DIR8_NORTH;
  do {
    if (is_valid_tileset_dir(t, dir)) {
      t->valid_tileset_dirs[t->num_valid_tileset_dirs] = dir;
      t->num_valid_tileset_dirs++;
    }
    if (is_cardinal_tileset_dir(t, dir)) {
      t->cardinal_tileset_dirs[t->num_cardinal_tileset_dirs] = dir;
      t->num_cardinal_tileset_dirs++;
    }

    dir = dir_cw(dir);
  } while (dir != DIR8_NORTH);
  fc_assert(t->num_valid_tileset_dirs % 2 == 0); /* Assumed elsewhere. */
  t->num_index_valid = 1 << t->num_valid_tileset_dirs;
  t->num_index_cardinal = 1 << t->num_cardinal_tileset_dirs;

  if (!secfile_lookup_int(file, &t->normal_tile_width,
                          "tilespec.normal_tile_width")
      || !secfile_lookup_int(file, &t->normal_tile_height,
                             "tilespec.normal_tile_height")) {
    log_error("Tileset \"%s\" invalid: %s", t->name, secfile_error());
    goto ON_ERROR;
  }
  t->normal_tile_width = ceil(t->scale * t->normal_tile_width);
  // Adjust width to be multiple of 8
  if (scale != 1.0f) {
    i = t->normal_tile_width;
    while (i % 8 != 0) {
      i++;
    }
    t->scale = (t->scale * i) / t->normal_tile_width;
    t->normal_tile_width = i;
  }
  t->normal_tile_height = ceil(t->scale * t->normal_tile_height);
  if (t->type == TS_ISOMETRIC) {
    t->full_tile_width = t->normal_tile_width;
    if (tileset_hex_height(t) > 0) {
      t->full_tile_height = t->normal_tile_height;
    } else {
      t->full_tile_height = 3 * t->normal_tile_height / 2;
    }
  } else {
    t->full_tile_width = t->normal_tile_width;
    t->full_tile_height = t->normal_tile_height;
  }
  t->unit_tile_width
    = secfile_lookup_int_default(file, t->full_tile_width, "tilespec.unit_width");
  t->unit_tile_height
    = secfile_lookup_int_default(file, t->full_tile_height, "tilespec.unit_height");
  if (!secfile_lookup_int(file, &t->small_sprite_width,
                          "tilespec.small_tile_width")
      || !secfile_lookup_int(file, &t->small_sprite_height,
                             "tilespec.small_tile_height")) {
    log_error("Tileset \"%s\" invalid: %s", t->name, secfile_error());
    goto ON_ERROR;
  }
  if (t->unit_tile_width != t->full_tile_width && t->scale != 1.0f) {
    t->unit_tile_width = ceil(t->unit_tile_width * t->scale);
  }
  if (t->unit_tile_height != t->full_tile_height && t->scale != 1.0f) {
    t->unit_tile_height = ceil(t->unit_tile_height * t->scale);
  }
  t->small_sprite_width = t->small_sprite_width * t->scale;
  t->small_sprite_height = t->small_sprite_height * t->scale;
  log_verbose("tile sizes %dx%d, %d%d unit, %d%d small",
              t->normal_tile_width, t->normal_tile_height,
              t->full_tile_width, t->full_tile_height,
              t->small_sprite_width, t->small_sprite_height);

  tstr = secfile_lookup_str(file, "tilespec.fog_style");
  if (tstr == NULL) {
    log_error("Tileset \"%s\": no fog_style", t->name);
    goto ON_ERROR;
  }

  t->fogstyle = fog_style_by_name(tstr, fc_strcasecmp);
  if (!fog_style_is_valid(t->fogstyle)) {
    log_error("Tileset \"%s\": unknown fog_style \"%s\"", t->name, tstr);
    goto ON_ERROR;
  }

  tstr = secfile_lookup_str(file, "tilespec.darkness_style");
  if (tstr == NULL) {
    log_error("Tileset \"%s\": no darkness_style", t->name);
    goto ON_ERROR;
  }

  t->darkness_style = darkness_style_by_name(tstr, fc_strcasecmp);
  if (!darkness_style_is_valid(t->darkness_style)) {
    log_error("Tileset \"%s\": unknown darkness_style \"%s\"", t->name, tstr);
    goto ON_ERROR;
  }

  if (t->darkness_style == DARKNESS_ISORECT
      && (t->type == TS_OVERHEAD || t->hex_width > 0 || t->hex_height > 0)) {
    log_error("Invalid darkness style set in tileset \"%s\".", t->name);
    goto ON_ERROR;
  }
  t->darkness_layer = secfile_lookup_int_default(file, 0, "tilespec.darkness_layer");
  if (t->darkness_layer < 0 || t->darkness_layer >= TERRAIN_LAYER_COUNT) {
    tileset_error(LOG_ERROR, "Invalid darkness layer %d in tileset \"%s\"",
                  t->darkness_layer, t->name);
    goto ON_ERROR;
  }

  if (!secfile_lookup_int(file, &t->unit_flag_offset_x,
                          "tilespec.unit_flag_offset_x")
      || !secfile_lookup_int(file, &t->unit_flag_offset_y,
                             "tilespec.unit_flag_offset_y")
      || !secfile_lookup_int(file, &t->city_flag_offset_x,
                             "tilespec.city_flag_offset_x")
      || !secfile_lookup_int(file, &t->city_flag_offset_y,
                             "tilespec.city_flag_offset_y")
      || !secfile_lookup_int(file, &t->unit_offset_x,
                             "tilespec.unit_offset_x")
      || !secfile_lookup_int(file, &t->unit_offset_y,
                             "tilespec.unit_offset_y")
      || !secfile_lookup_int(file, &t->activity_offset_x,
                             "tilespec.activity_offset_x")
      || !secfile_lookup_int(file, &t->activity_offset_y,
                             "tilespec.activity_offset_y")
      || !secfile_lookup_int(file, &t->city_offset_x,
                             "tilespec.city_offset_x")
      || !secfile_lookup_int(file, &t->city_offset_y,
                             "tilespec.city_offset_y")
      || !secfile_lookup_int(file, &t->citybar_offset_y,
                             "tilespec.citybar_offset_y")
      || !secfile_lookup_int(file, &t->tilelabel_offset_y,
                             "tilespec.tilelabel_offset_y")
      || !secfile_lookup_int(file, &t->occupied_offset_x,
                             "tilespec.occupied_offset_x")
      || !secfile_lookup_int(file, &t->occupied_offset_y,
                             "tilespec.occupied_offset_y")
      || !secfile_lookup_int(file, &t->city_names_font_size,
                             "tilespec.city_names_font_size")
      || !secfile_lookup_int(file, &t->city_productions_font_size,
                             "tilespec.city_productions_font_size")) {
    log_error("Tileset \"%s\" invalid: %s", t->name, secfile_error());
    goto ON_ERROR;
  }

  t->unit_upkeep_offset_y = secfile_lookup_int_default(file, tileset_tile_height(t),
                                                       "tilespec.unit_upkeep_offset_y");
  t->unit_upkeep_small_offset_y = secfile_lookup_int_default(file, t->unit_upkeep_offset_y,
                                                             "tilespec.unit_upkeep_small_offset_y");
  t->unit_flag_offset_x = t->scale * t->unit_flag_offset_x;
  t->unit_flag_offset_y = t->scale * t->unit_flag_offset_y;
  t->city_flag_offset_x = t->scale * t->city_flag_offset_x;
  t->city_flag_offset_y = t->scale * t->city_flag_offset_y;
  t->unit_offset_x = t->scale * t->unit_offset_x;
  t->unit_offset_y = t->scale * t->unit_offset_y;
  t->activity_offset_x = t->scale * t->activity_offset_x;
  t->activity_offset_y = t->scale * t->activity_offset_y;
  t->city_offset_x = t->scale * t->city_offset_x;
  t->city_offset_y = t->scale * t->city_offset_y;
  t->citybar_offset_y = t->scale * t->citybar_offset_y;
  t->tilelabel_offset_y = t->scale * t->tilelabel_offset_y;
  t->occupied_offset_x = t->scale * t->occupied_offset_x;
  t->occupied_offset_y = t->scale * t->occupied_offset_y;
  if (t->scale != 1.0f
      && t->unit_upkeep_offset_y != tileset_tile_height(t)) {
    t->unit_upkeep_offset_y = t->scale * t->unit_upkeep_offset_y;
  }
  if (t->scale != 1.0f
      && t->unit_upkeep_small_offset_y != t->unit_upkeep_offset_y) {
    t->unit_upkeep_small_offset_y = t->scale * t->unit_upkeep_small_offset_y;
  }
  /* Tell client, although many clients ignore this in favour of their
   * own configuration */
  set_city_names_font_sizes(t->city_names_font_size,
                            t->city_productions_font_size);

  c = secfile_lookup_str(file, "tilespec.main_intro_file");
  t->main_intro_filename = tilespec_gfx_filename(c);
  log_debug("intro file %s", t->main_intro_filename);

  c = secfile_lookup_str_default(file, NULL, "tilespec.minimap_intro_file");
  if (c == NULL) {
    t->minimap_intro_filename = NULL;
  } else {
    t->minimap_intro_filename = tilespec_gfx_filename(c);
    log_debug("radar file %s", t->minimap_intro_filename);
  }

  /* Terrain layer info. */
  for (i = 0; i < MAX_NUM_LAYERS; i++) {
    struct tileset_layer *tslp = &t->layers[i];
    int j, k;

    tslp->match_types =
        (char **) secfile_lookup_str_vec(file, &tslp->match_count,
                                         "layer%d.match_types", i);
    for (j = 0; j < tslp->match_count; j++) {
      tslp->match_types[j] = fc_strdup(tslp->match_types[j]);

      for (k = 0; k < j; k++) {
        if (tslp->match_types[k][0] == tslp->match_types[j][0]) {
          tileset_error(LOG_FATAL, _("[layer%d] match_types: \"%s\" initial "
                                     "('%c') is not unique."),
                        i, tslp->match_types[j], tslp->match_types[j][0]);
          /* FIXME: Returns NULL. */
        }
      }
    }
  }

  /* Tile drawing info. */
  sections = secfile_sections_by_name_prefix(file, TILE_SECTION_PREFIX);
  if (NULL == sections || 0 == section_list_size(sections)) {
    tileset_error(LOG_ERROR, _("No [%s] sections supported by tileset \"%s\"."),
                  TILE_SECTION_PREFIX, fname);
    goto ON_ERROR;
  }

  fc_assert(t->tile_hash == NULL);
  t->tile_hash = drawing_hash_new();

  section_list_iterate(sections, psection) {
    const char *sec_name = section_name(psection);
    struct drawing_data *draw = drawing_data_new();
    const char *sprite_type;
    int l;
    const char *terrain_name;

    terrain_name = secfile_lookup_str(file, "%s.tag", sec_name);

    if (terrain_name != NULL) {
      draw->name = fc_strdup(terrain_name);
    } else {
      tileset_error(LOG_ERROR, _("No terrain tag given in section [%s]."), sec_name);
      drawing_data_destroy(draw);
      goto ON_ERROR;
    }

    draw->blending = secfile_lookup_int_default(file, 0, "%s.blend_layer",
                                                sec_name);
    draw->blending = CLIP(0, draw->blending, MAX_NUM_LAYERS);

    draw->is_reversed = secfile_lookup_bool_default(file, FALSE,
                                                    "%s.is_reversed",
                                                    sec_name);
    draw->num_layers = secfile_lookup_int_default(file, 0, "%s.num_layers",
                                                  sec_name);
    draw->num_layers = CLIP(1, draw->num_layers, MAX_NUM_LAYERS);

    for (l = 0; l < draw->num_layers; l++) {
      struct drawing_layer *dlp = &draw->layer[l];
      struct tileset_layer *tslp = &t->layers[l];
      const char *match_type;
      const char **match_with;
      size_t count;

      dlp->is_tall
        = secfile_lookup_bool_default(file, FALSE, "%s.layer%d_is_tall",
                                      sec_name, l);
      dlp->offset_x
        = secfile_lookup_int_default(file, 0, "%s.layer%d_offset_x",
                                     sec_name, l);
      dlp->offset_y
        = secfile_lookup_int_default(file, 0, "%s.layer%d_offset_y",
                                     sec_name, l);
      dlp->offset_x = ceil(t->scale * dlp->offset_x);
      dlp->offset_y = ceil(t->scale * dlp->offset_y);

      match_type = secfile_lookup_str_default(file, NULL,
                                              "%s.layer%d_match_type",
                                              sec_name, l);
      if (match_type) {
        int j;

        /* Determine our match_type. */
        for (j = 0; j < tslp->match_count; j++) {
          if (fc_strcasecmp(tslp->match_types[j], match_type) == 0) {
            break;
          }
        }
        if (j >= tslp->match_count) {
          log_error("[%s] invalid match_type \"%s\".", sec_name, match_type);
        } else {
          dlp->match_index[dlp->match_indices++] = j;
        }
      }

      match_with = secfile_lookup_str_vec(file, &count,
                                          "%s.layer%d_match_with",
                                          sec_name, l);
      if (match_with) {
        int j, k;

        if (count > MAX_NUM_MATCH_WITH) {
          log_error("[%s] match_with has too many types (%d, max %d)",
                    sec_name, (int) count, MAX_NUM_MATCH_WITH);
          count = MAX_NUM_MATCH_WITH;
        }

        if (1 < dlp->match_indices) {
          log_error("[%s] previous match_with ignored.", sec_name);
          dlp->match_indices = 1;
        } else if (1 > dlp->match_indices) {
          log_error("[%s] missing match_type, using \"%s\".",
                    sec_name, tslp->match_types[0]);
          dlp->match_index[0] = 0;
          dlp->match_indices = 1;
        }

        for (k = 0; k < count; k++) {
          for (j = 0; j < tslp->match_count; j++) {
            if (fc_strcasecmp(tslp->match_types[j], match_with[k]) == 0) {
              break;
            }
          }
          if (j >= tslp->match_count) {
            log_error("[%s] layer%d_match_with: invalid  \"%s\".",
                      sec_name, l, match_with[k]);
          } else if (1 < count) {
            int m;

            for (m = 0; m < dlp->match_indices; m++) {
              if (dlp->match_index[m] == j) {
                log_error("[%s] layer%d_match_with: duplicate \"%s\".",
                          sec_name, l, match_with[k]);
                break;
              }
            }
            if (m >= dlp->match_indices) {
              dlp->match_index[dlp->match_indices++] = j;
            }
          } else {
            dlp->match_index[dlp->match_indices++] = j;
          }
        }
        free(match_with);
        match_with = NULL;
      }

      /* Check match_indices */
      switch (dlp->match_indices) {
      case 0:
      case 1:
        dlp->match_style = MATCH_NONE;
        break;
      case 2:
        if (dlp->match_index[0] == dlp->match_index[1] ) {
          dlp->match_style = MATCH_SAME;
        } else {
          dlp->match_style = MATCH_PAIR;
        }
        break;
      default:
        dlp->match_style = MATCH_FULL;
        break;
      };

      sprite_type
	= secfile_lookup_str_default(file, "whole", "%s.layer%d_sprite_type",
                                     sec_name, l);
      dlp->sprite_type = check_sprite_type(sprite_type, sec_name);

      switch (dlp->sprite_type) {
      case CELL_WHOLE:
	/* OK, no problem */
	break;
      case CELL_CORNER:
	if (dlp->is_tall
	    || dlp->offset_x > 0
	    || dlp->offset_y > 0) {
          log_error("[%s] layer %d: you cannot have tall terrain or\n"
                    "a sprite offset with a cell-based drawing method.",
                    sec_name, l);
	  dlp->is_tall = FALSE;
	  dlp->offset_x = dlp->offset_y = 0;
	}
	break;
      };
    }

    if (!drawing_hash_insert(t->tile_hash, draw->name, draw)) {
      log_error("warning: multiple tile sections containing terrain tag \"%s\".",
                draw->name);
      goto ON_ERROR;
    }
  } section_list_iterate_end;
  section_list_destroy(sections);
  sections = NULL;

  t->estyle_hash = estyle_hash_new();

  for (i = 0; i < ESTYLE_COUNT; i++) {
    t->style_lists[i] = extra_type_list_new();
  }
  t->flagged_bases_list = extra_type_list_new();

  for (i = 0; (extraname = secfile_lookup_str_default(file, NULL,
                                                      "extras.styles%d.name",
                                                      i)); i++) {
    const char *style_name;
    enum extrastyle_id style;

    style_name = secfile_lookup_str_default(file, "Single1",
                                            "extras.styles%d.style", i);
    style = extrastyle_id_by_name(style_name, fc_strcasecmp);
    if (!extrastyle_id_is_valid(style)) {
      log_error("Unknown extra style \"%s\" for road \"%s\"",
                style_name, extraname);
      goto ON_ERROR;
    }

    if (!estyle_hash_insert(t->estyle_hash, extraname, style)) {
      log_error("warning: duplicate extrastyle entry [%s].", extraname);
      goto ON_ERROR;
    }
  }

  spec_filenames = secfile_lookup_str_vec(file, &num_spec_files,
                                          "tilespec.files");
  if (NULL == spec_filenames || 0 == num_spec_files) {
    log_error("No tile graphics files specified in \"%s\"", fname);
    goto ON_ERROR;
  }

  fc_assert(t->sprite_hash == NULL);
  t->sprite_hash = sprite_hash_new();
  for (i = 0; i < num_spec_files; i++) {
    struct specfile *sf = fc_malloc(sizeof(*sf));
    const char *dname;

    log_debug("spec file %s", spec_filenames[i]);
    
    sf->big_sprite = NULL;
    dname = fileinfoname(get_data_dirs(), spec_filenames[i]);
    if (!dname) {
      if (verbose) {
        log_error("Can't find spec file \"%s\".", spec_filenames[i]);
      }
      goto ON_ERROR;
    }
    sf->file_name = fc_strdup(dname);
    scan_specfile(t, sf, duplicates_ok);

    specfile_list_prepend(t->specfiles, sf);
  }
  free(spec_filenames);

  t->color_system = color_system_read(file);

  /* FIXME: remove this hack. */
  t->preferred_themes =
    (char **) secfile_lookup_str_vec(file, &num_preferred_themes,
                                     "tilespec.preferred_themes");
  if (num_preferred_themes <= 0) {
    t->preferred_themes =
      (char **) secfile_lookup_str_vec(file, &num_preferred_themes,
                                       "tilespec.prefered_themes");
    if (num_preferred_themes > 0) {
      log_deprecation("Entry tilespec.prefered_themes in tilespec."
                      " Use correct spelling tilespec.preferred_themes instead");
    }
  }
  t->num_preferred_themes = num_preferred_themes;
  for (i = 0; i < t->num_preferred_themes; i++) {
    t->preferred_themes[i] = fc_strdup(t->preferred_themes[i]);
  }

  secfile_check_unused(file);
  secfile_destroy(file);
  log_verbose("finished reading \"%s\".", fname);
  free(fname);

  return t;

ON_ERROR:
  secfile_destroy(file);
  free(fname);
  tileset_free(t);
  if (NULL != sections) {
    section_list_destroy(sections);
  }
  return NULL;
}

/**********************************************************************
  Returns a text name for the citizen, as used in the tileset.
***********************************************************************/
static const char *citizen_rule_name(enum citizen_category citizen)
{
  /* These strings are used in reading the tileset.  Do not
   * translate. */
  switch (citizen) {
  case CITIZEN_HAPPY:
    return "happy";
  case CITIZEN_CONTENT:
    return "content";
  case CITIZEN_UNHAPPY:
    return "unhappy";
  case CITIZEN_ANGRY:
    return "angry";
  default:
    break;
  }
  log_error("Unknown citizen type: %d.", (int) citizen);
  return NULL;
}

/****************************************************************************
  Return a directional string for the cardinal directions.  Normally the
  binary value 1000 will be converted into "n1e0s0w0".  This is in a
  clockwise ordering.
****************************************************************************/
static const char *cardinal_index_str(const struct tileset *t, int idx)
{
  static char c[64];
  int i;

  c[0] = '\0';
  for (i = 0; i < t->num_cardinal_tileset_dirs; i++) {
    int value = (idx >> i) & 1;

    cat_snprintf(c, sizeof(c), "%s%d",
                 dir_get_tileset_name(t->cardinal_tileset_dirs[i]), value);
  }

  return c;
}

/****************************************************************************
  Do the same thing as cardinal_str, except including all valid directions.
  The returned string is a pointer to static memory.
****************************************************************************/
static char *valid_index_str(const struct tileset *t, int idx)
{
  static char c[64];
  int i;

  c[0] = '\0';
  for (i = 0; i < t->num_valid_tileset_dirs; i++) {
    int value = (idx >> i) & 1;

    cat_snprintf(c, sizeof(c), "%s%d",
		 dir_get_tileset_name(t->valid_tileset_dirs[i]), value);
  }

  return c;
}

/**************************************************************************
  Loads the sprite. If the sprite is already loaded a reference
  counter is increased. Can return NULL if the sprite couldn't be
  loaded.
  Scale means if sprite should be scaled, smooth if scaling might use
  other scaling algorithm than nearest neighbor.
**************************************************************************/
static struct sprite *load_sprite(struct tileset *t, const char *tag_name,
                                  bool scale, bool smooth)
{
  struct small_sprite *ss;
  float sprite_scale = 1.0f;

  log_debug("load_sprite(tag='%s')", tag_name);
  /* Lookup information about where the sprite is found. */
  if (!sprite_hash_lookup(t->sprite_hash, tag_name, &ss)) {
    return NULL;
  }

  fc_assert(ss->ref_count >= 0);

  if (!ss->sprite) {
    /* If the sprite hasn't been loaded already, then load it. */
    fc_assert(ss->ref_count == 0);
    if (ss->file) {
      int w, h;
      struct sprite *s;

      if (scale) {
        s = load_gfx_file(ss->file);
        get_sprite_dimensions(s, &w, &h);
        ss->sprite = crop_sprite(s, 0, 0, w,
                                 h, NULL, -1, -1, t->scale, smooth);
        free_sprite(s);
      } else {
        ss->sprite = load_gfx_file(ss->file);
      }
      if (!ss->sprite) {
        tileset_error(LOG_FATAL, _("Couldn't load gfx file \"%s\" for sprite '%s'."),
                      ss->file, tag_name);
      }
    } else {
      int sf_w, sf_h;

      ensure_big_sprite(ss->sf);
      get_sprite_dimensions(ss->sf->big_sprite, &sf_w, &sf_h);
      if (ss->x < 0 || ss->x + ss->width > sf_w
	  || ss->y < 0 || ss->y + ss->height > sf_h) {
        tileset_error(LOG_ERROR, _("Sprite '%s' in file \"%s\" isn't within the image!"),
                      tag_name, ss->sf->file_name);
	return NULL;
      }
      if (scale) {
        sprite_scale = t->scale;
      }
      ss->sprite = crop_sprite(ss->sf->big_sprite, ss->x, ss->y, ss->width,
                               ss->height, NULL, -1, -1, sprite_scale,
                               smooth);
    }
  }

  /* Track the reference count so we know when to free the sprite. */
  ss->ref_count++;

  return ss->sprite;
}

/**************************************************************************
  Create a sprite with the given color and tag.
**************************************************************************/
static struct sprite *create_plr_sprite(struct color *pcolor)
{
  struct sprite *sprite;

  fc_assert_ret_val(pcolor != NULL, NULL);

  if (tileset->scale == 1.0f) {
    sprite = create_sprite(128, 64, pcolor);
  } else {
    sprite = create_sprite(tileset->full_tile_width,
                           tileset->full_tile_height, pcolor);
  }

  return sprite;
}

/**************************************************************************
  Unloads the sprite. Decrease the reference counter. If the last
  reference is removed the sprite is freed.
**************************************************************************/
static void unload_sprite(struct tileset *t, const char *tag_name)
{
  struct small_sprite *ss;

  sprite_hash_lookup(t->sprite_hash, tag_name, &ss);
  fc_assert_ret(ss);
  fc_assert_ret(ss->ref_count >= 1);
  fc_assert_ret(ss->sprite);

  ss->ref_count--;

  if (ss->ref_count == 0) {
    /* Nobody's using the sprite anymore, so we should free it.  We know
     * where to find it if we need it again. */
    log_debug("freeing sprite '%s'.", tag_name);
    free_sprite(ss->sprite);
    ss->sprite = NULL;
  }
}

/**************************************************************************
  Return TRUE iff the specified sprite exists in the tileset (whether
  or not it is currently loaded).
**************************************************************************/
static bool sprite_exists(const struct tileset *t, const char *tag_name)
{
  /* Lookup information about where the sprite is found. */
  return sprite_hash_lookup(t->sprite_hash, tag_name, NULL);
}

/* Not very safe, but convenient: */
#define SET_SPRITE(field, tag)					  \
  do {								  \
    t->sprites.field = load_sprite(t, tag, TRUE, TRUE);			  \
    if (t->sprites.field == NULL) {                               \
      tileset_error(LOG_FATAL, _("Sprite for tag '%s' missing."), tag);    \
    }                                                             \
  } while(FALSE)

#define SET_SPRITE_NOTSMOOTH(field, tag)					  \
  do {								  \
    t->sprites.field = load_sprite(t, tag, TRUE, FALSE);			  \
    if (t->sprites.field == NULL) {                               \
      tileset_error(LOG_FATAL, _("Sprite for tag '%s' missing."), tag);    \
    }                                                             \
  } while(FALSE)

#define SET_SPRITE_UNSCALED(field, tag)           \
  do {                  \
    t->sprites.field = load_sprite(t, tag, FALSE, FALSE);       \
    if (t->sprites.field == NULL) {                               \
      tileset_error(LOG_FATAL, _("Sprite for tag '%s' missing."), tag);    \
    }                                                             \
  } while(FALSE)

/* Sets sprites.field to tag or (if tag isn't available) to alt */
#define SET_SPRITE_ALT(field, tag, alt)					    \
  do {									    \
    t->sprites.field = load_sprite(t, tag, TRUE, TRUE);			    \
    if (!t->sprites.field) {						    \
      t->sprites.field = load_sprite(t, alt, TRUE, TRUE);			    \
    }									    \
    if (t->sprites.field == NULL) {                                         \
      tileset_error(LOG_FATAL, _("Sprite for tags '%s' and alternate '%s' are "  \
                                 "both missing."), tag, alt);               \
    }                                                                       \
  } while(FALSE)

/* Sets sprites.field to tag, or NULL if not available */
#define SET_SPRITE_OPT(field, tag) \
  t->sprites.field = load_sprite(t, tag, TRUE, TRUE)

#define SET_SPRITE_ALT_OPT(field, tag, alt)				    \
  do {									    \
    t->sprites.field = tiles_lookup_sprite_tag_alt(t, LOG_VERBOSE, tag, alt,\
						   "sprite", #field, TRUE);	    \
  } while (FALSE)

/****************************************************************************
  Setup the graphics for specialist types.
****************************************************************************/
void tileset_setup_specialist_type(struct tileset *t, Specialist_type_id id)
{
  /* Load the specialist sprite graphics. */
  char buffer[512];
  int j;
  const char *name = specialist_rule_name(specialist_by_number(id));
  const char *graphic_alt = specialist_by_number(id)->graphic_alt;

  for (j = 0; j < MAX_NUM_CITIZEN_SPRITES; j++) {
    /* Try rule name + index number */
    fc_snprintf(buffer, sizeof(buffer), "specialist.%s_%d", name, j);
    t->sprites.specialist[id].sprite[j] = load_sprite(t, buffer, FALSE,
                                                      FALSE);

    /* Break if no more index specific sprites are defined */
    if (!t->sprites.specialist[id].sprite[j]) {
      break;
    }
  }

  /* Nothing? Try the alt tag */
  if (j == 0) {
    t->sprites.specialist[id].sprite[j] = load_sprite(t, graphic_alt, FALSE,
                                                      FALSE);

    if (t->sprites.specialist[id].sprite[j]) {
      j = 1;
    }
  }

  t->sprites.specialist[id].count = j;

  /* Still nothing? Give up. */
  if (j == 0) {
    tileset_error(LOG_FATAL, _("No graphics for specialist \"%s\"."), name);
  }
}

/****************************************************************************
  Setup the graphics for (non-specialist) citizen types.
****************************************************************************/
static void tileset_setup_citizen_types(struct tileset *t)
{
  int i, j;
  char buffer[512];

  /* Load the citizen sprite graphics, no specialist. */
  for (i = 0; i < CITIZEN_LAST; i++) {
    const char *name = citizen_rule_name(i);

    for (j = 0; j < MAX_NUM_CITIZEN_SPRITES; j++) {
      fc_snprintf(buffer, sizeof(buffer), "citizen.%s_%d", name, j);
      t->sprites.citizen[i].sprite[j] = load_sprite(t, buffer, FALSE, FALSE);
      if (!t->sprites.citizen[i].sprite[j]) {
	break;
      }
    }
    t->sprites.citizen[i].count = j;
    if (j == 0) {
      tileset_error(LOG_FATAL, _("No graphics for citizen \"%s\"."), name);
    }
  }
}

/****************************************************************************
  Return the sprite in the city_sprite listing that corresponds to this
  city - based on city style and size.

  See also load_city_sprite, free_city_sprite.
****************************************************************************/
static struct sprite *get_city_sprite(const struct city_sprite *city_sprite,
				      const struct city *pcity)
{
  /* get style and match the best tile based on city size */
  int style = style_of_city(pcity);
  int num_thresholds;
  struct city_style_threshold *thresholds;
  int img_index;

  fc_assert_ret_val(style < city_sprite->num_styles, NULL);

  num_thresholds = city_sprite->styles[style].land_num_thresholds;
  thresholds = city_sprite->styles[style].land_thresholds;

  if (num_thresholds == 0) {
    return NULL;
  }

  /* Get the sprite with the index defined by the effects. */
  img_index = pcity->client.city_image;
  if (img_index == -100) {
    /* Server doesn't know right value as this is from old savegame.
     * Guess here based on *client* side information as was done in
     * versions where information was not saved to savegame - this should
     * give us right answer of what city looked like by the time it was
     * put under FoW. */
    img_index = get_city_bonus(pcity, EFT_CITY_IMAGE);
  }
  img_index = CLIP(0, img_index, num_thresholds - 1);

  return thresholds[img_index].sprite;
}

/****************************************************************************
  Allocates one threshold set for city sprite
****************************************************************************/
static int load_city_thresholds_sprites(struct tileset *t, const char *tag,
                                        char *graphic, char *graphic_alt,
                                        struct city_style_threshold **thresholds)
{
  char buffer[128];
  char *gfx_in_use = graphic;
  int num_thresholds = 0;
  struct sprite *sprite;
  int size;

  *thresholds = NULL;

  for (size = 0; size < MAX_CITY_SIZE; size++) {
    fc_snprintf(buffer, sizeof(buffer), "%s_%s_%d",
                gfx_in_use, tag, size);
    if ((sprite = load_sprite(t, buffer, TRUE, TRUE))) {
      num_thresholds++;
      *thresholds = fc_realloc(*thresholds, num_thresholds * sizeof(**thresholds));
      (*thresholds)[num_thresholds - 1].sprite = sprite;
    } else if (size == 0) {
      if (gfx_in_use == graphic) {
        /* Try again with graphic_alt. */
        size--;
        gfx_in_use = graphic_alt;
      } else {
        /* Don't load any others if the 0 element isn't there. */
        break;
      }
    }
  }

  return num_thresholds;
}

/****************************************************************************
  Allocates and loads a new city sprite from the given sprite tags.

  tag may be NULL.

  See also get_city_sprite, free_city_sprite.
****************************************************************************/
static struct city_sprite *load_city_sprite(struct tileset *t,
					    const char *tag)
{
  struct city_sprite *city_sprite = fc_malloc(sizeof(*city_sprite));
  int style;

  /* Store number of styles we have allocated memory for.
   * game.control.styles_count might change if client disconnects from
   * server and connects new one. */
  city_sprite->num_styles = game.control.styles_count;
  city_sprite->styles = fc_malloc(city_sprite->num_styles
				  * sizeof(*city_sprite->styles));

  for (style = 0; style < city_sprite->num_styles; style++) {
    city_sprite->styles[style].land_num_thresholds =
      load_city_thresholds_sprites(t, tag, city_styles[style].graphic,
                                   city_styles[style].graphic_alt,
                                   &city_sprite->styles[style].land_thresholds);
  }

  return city_sprite;
}

/****************************************************************************
  Frees a city sprite.

  See also get_city_sprite, load_city_sprite.
****************************************************************************/
static void free_city_sprite(struct city_sprite *city_sprite)
{
  int style;

  if (!city_sprite) {
    return;
  }
  for (style = 0; style < city_sprite->num_styles; style++) {
    if (city_sprite->styles[style].land_thresholds) {
      free(city_sprite->styles[style].land_thresholds);
    }
  }
  free(city_sprite->styles);
  free(city_sprite);
}

/**********************************************************************
  Initialize 'sprites' structure based on hardwired tags which
  freeciv always requires. 
***********************************************************************/
static void tileset_lookup_sprite_tags(struct tileset *t)
{
  char buffer[512], buffer2[512];
  const int W = t->normal_tile_width, H = t->normal_tile_height;
  int i, j, f;

  fc_assert_ret(t->sprite_hash != NULL);

  SET_SPRITE_UNSCALED(treaty_thumb[0], "treaty.disagree_thumb_down");
  SET_SPRITE_UNSCALED(treaty_thumb[1], "treaty.agree_thumb_up");

  for (j = 0; j < INDICATOR_COUNT; j++) {
    const char *names[] = {"science_bulb", "warming_sun", "cooling_flake"};

    for (i = 0; i < NUM_TILES_PROGRESS; i++) {
      fc_snprintf(buffer, sizeof(buffer), "s.%s_%d", names[j], i);
      SET_SPRITE_UNSCALED(indicator[j][i], buffer);
    }
  }

  SET_SPRITE(arrow[ARROW_RIGHT], "s.right_arrow");
  SET_SPRITE(arrow[ARROW_PLUS], "s.plus");
  SET_SPRITE(arrow[ARROW_MINUS], "s.minus");
  if (t->type == TS_ISOMETRIC) {
    SET_SPRITE(dither_tile, "t.dither_tile");
  }

  if (tileset_is_isometric(tileset)) {
    SET_SPRITE_NOTSMOOTH(mask.tile, "mask.tile");
  } else {
    SET_SPRITE(mask.tile, "mask.tile");
  }
  SET_SPRITE(mask.worked_tile, "mask.worked_tile");
  SET_SPRITE(mask.unworked_tile, "mask.unworked_tile");

  SET_SPRITE_UNSCALED(tax_luxury, "s.tax_luxury");
  SET_SPRITE_UNSCALED(tax_science, "s.tax_science");
  SET_SPRITE_UNSCALED(tax_gold, "s.tax_gold");

  tileset_setup_citizen_types(t);

  for (i = 0; i < SPACESHIP_COUNT; i++) {
    const char *names[SPACESHIP_COUNT]
      = {"solar_panels", "life_support", "habitation",
	 "structural", "fuel", "propulsion", "exhaust"};

    fc_snprintf(buffer, sizeof(buffer), "spaceship.%s", names[i]);
    SET_SPRITE(spaceship[i], buffer);
  }

  for (i = 0; i < CURSOR_LAST; i++) {
    for (f = 0; f < NUM_CURSOR_FRAMES; f++) {
      const char *names[CURSOR_LAST] =
               {"goto", "patrol", "paradrop", "nuke", "select", 
		"invalid", "attack", "edit_paint", "edit_add", "wait"};
      struct small_sprite *ss;

      fc_assert(ARRAY_SIZE(names) == CURSOR_LAST);
      fc_snprintf(buffer, sizeof(buffer), "cursor.%s%d", names[i], f);
      SET_SPRITE(cursor[i].frame[f], buffer);
      if (sprite_hash_lookup(t->sprite_hash, buffer, &ss)) {
        t->sprites.cursor[i].hot_x = ss->hot_x;
        t->sprites.cursor[i].hot_y = ss->hot_y;
      }
    }
  }

  for (i = 0; i < ICON_COUNT; i++) {
    const char *names[ICON_COUNT] = {"freeciv", "citydlg"};

    fc_snprintf(buffer, sizeof(buffer), "icon.%s", names[i]);
    SET_SPRITE(icon[i], buffer);
  }

  for (i = 0; i < E_COUNT; i++) {
    const char *tag = get_event_tag(i);

    SET_SPRITE(events[i], tag);
  }

  SET_SPRITE(explode.nuke, "explode.nuke");

  sprite_vector_init(&t->sprites.explode.unit);
  for (i = 0; ; i++) {
    struct sprite *sprite;

    fc_snprintf(buffer, sizeof(buffer), "explode.unit_%d", i);
    sprite = load_sprite(t, buffer, TRUE, TRUE);
    if (!sprite) {
      break;
    }
    sprite_vector_append(&t->sprites.explode.unit, sprite);
  }

  SET_SPRITE(unit.auto_attack,  "unit.auto_attack");
  SET_SPRITE(unit.auto_settler, "unit.auto_settler");
  SET_SPRITE(unit.auto_explore, "unit.auto_explore");
  SET_SPRITE(unit.fortified,	"unit.fortified");     
  SET_SPRITE(unit.fortifying,	"unit.fortifying");
  SET_SPRITE(unit.go_to,	"unit.goto");
  SET_SPRITE(unit.irrigate,     "unit.irrigate"); 
  SET_SPRITE(unit.plant,        "unit.plant");
  SET_SPRITE(unit.pillage,	"unit.pillage");
  SET_SPRITE(unit.sentry,	"unit.sentry");
  SET_SPRITE(unit.convert,      "unit.convert");      
  SET_SPRITE(unit.stack,	"unit.stack");
  SET_SPRITE(unit.loaded,       "unit.loaded");
  SET_SPRITE(unit.transform,    "unit.transform");
  SET_SPRITE(unit.connect,      "unit.connect");
  SET_SPRITE(unit.patrol,       "unit.patrol");
  for (i = 0; i < MAX_NUM_BATTLEGROUPS; i++) {
    fc_snprintf(buffer, sizeof(buffer), "unit.battlegroup_%d", i);
    fc_snprintf(buffer2, sizeof(buffer2), "city.size_%d", i + 1);
    fc_assert(MAX_NUM_BATTLEGROUPS < NUM_TILES_DIGITS);
    SET_SPRITE_ALT(unit.battlegroup[i], buffer, buffer2);
  }
  SET_SPRITE(unit.lowfuel, "unit.lowfuel");
  SET_SPRITE(unit.tired, "unit.tired");

  for(i=0; i<NUM_TILES_HP_BAR; i++) {
    fc_snprintf(buffer, sizeof(buffer), "unit.hp_%d", i*10);
    SET_SPRITE(unit.hp_bar[i], buffer);
  }

  for (i = 0; i < MAX_VET_LEVELS; i++) {
    /* Veteran level sprites are optional.  For instance "green" units
     * usually have no special graphic. */
    fc_snprintf(buffer, sizeof(buffer), "unit.vet_%d", i);
    t->sprites.unit.vet_lev[i] = load_sprite(t, buffer, TRUE, TRUE);
  }

  t->sprites.unit.select[0] = NULL;
  if (sprite_exists(t, "unit.select0")) {
    for (i = 0; i < NUM_TILES_SELECT; i++) {
      fc_snprintf(buffer, sizeof(buffer), "unit.select%d", i);
      SET_SPRITE(unit.select[i], buffer);
    }
  }

  SET_SPRITE(citybar.shields, "citybar.shields");
  SET_SPRITE(citybar.food, "citybar.food");
  SET_SPRITE(citybar.trade, "citybar.trade");
  SET_SPRITE(citybar.occupied, "citybar.occupied");
  SET_SPRITE(citybar.background, "citybar.background");
  sprite_vector_init(&t->sprites.citybar.occupancy);
  for (i = 0; ; i++) {
    struct sprite *sprite;

    fc_snprintf(buffer, sizeof(buffer), "citybar.occupancy_%d", i);
    sprite = load_sprite(t, buffer, TRUE, TRUE);
    if (!sprite) {
      break;
    }
    sprite_vector_append(&t->sprites.citybar.occupancy, sprite);
  }
  if (t->sprites.citybar.occupancy.size < 2) {
    tileset_error(LOG_FATAL, _("Missing necessary citybar.occupancy_N sprites."));
  }

#define SET_EDITOR_SPRITE(x) SET_SPRITE(editor.x, "editor." #x)
  SET_EDITOR_SPRITE(erase);
  SET_EDITOR_SPRITE(brush);
  SET_EDITOR_SPRITE(copy);
  SET_EDITOR_SPRITE(paste);
  SET_EDITOR_SPRITE(copypaste);
  SET_EDITOR_SPRITE(startpos);
  SET_EDITOR_SPRITE(terrain);
  SET_EDITOR_SPRITE(terrain_resource);
  SET_EDITOR_SPRITE(terrain_special);
  SET_EDITOR_SPRITE(unit);
  SET_EDITOR_SPRITE(city);
  SET_EDITOR_SPRITE(vision);
  SET_EDITOR_SPRITE(territory);
  SET_EDITOR_SPRITE(properties);
  SET_EDITOR_SPRITE(road);
  SET_EDITOR_SPRITE(military_base);
#undef SET_EDITOR_SPRITE

  SET_SPRITE(city.disorder, "city.disorder");

  /* Fallbacks for goto path turn numbers:
   *   path.step_%d, path.exhausted_mp_%d
   *   --> path.turns_%d
   *       --> city.size_%d */
#define SET_GOTO_TURN_SPRITE(state, state_name, factor, factor_name)        \
  fc_snprintf(buffer, sizeof(buffer), "path." state_name "_%d" #factor, i); \
  SET_SPRITE_OPT(path.s[state].turns ## factor_name [i], buffer);           \
  if (t->sprites.path.s[state].turns ## factor_name [i] == NULL) {          \
    t->sprites.path.s[state].turns ## factor_name [i] =                     \
        t->sprites.path.s[GTS_MP_LEFT].turns ## factor_name [i];            \
  }
  for(i=0; i<NUM_TILES_DIGITS; i++) {
    fc_snprintf(buffer, sizeof(buffer), "city.size_%d", i);
    SET_SPRITE(city.size[i], buffer);
    fc_snprintf(buffer2, sizeof(buffer2), "path.turns_%d", i);
    SET_SPRITE_ALT(path.s[GTS_MP_LEFT].turns[i], buffer2, buffer);
    SET_GOTO_TURN_SPRITE(GTS_TURN_STEP, "step",,);
    SET_GOTO_TURN_SPRITE(GTS_EXHAUSTED_MP, "exhausted_mp",,);

    fc_snprintf(buffer, sizeof(buffer), "city.size_%d0", i);
    SET_SPRITE(city.size_tens[i], buffer);
    fc_snprintf(buffer2, sizeof(buffer2), "path.turns_%d0", i);
    SET_SPRITE_ALT(path.s[GTS_MP_LEFT].turns_tens[i], buffer2, buffer);
    SET_GOTO_TURN_SPRITE(GTS_TURN_STEP, "step", 0, _tens);
    SET_GOTO_TURN_SPRITE(GTS_EXHAUSTED_MP, "exhausted_mp", 0, _tens);

    fc_snprintf(buffer, sizeof(buffer), "city.size_%d00", i);
    SET_SPRITE_OPT(city.size_hundreds[i], buffer);
    fc_snprintf(buffer2, sizeof(buffer2), "path.turns_%d00", i);
    SET_SPRITE_ALT_OPT(path.s[GTS_MP_LEFT].turns_hundreds[i], buffer2,
                       buffer);
    SET_GOTO_TURN_SPRITE(GTS_TURN_STEP, "step", 00, _hundreds);
    SET_GOTO_TURN_SPRITE(GTS_EXHAUSTED_MP, "exhausted_mp", 00, _hundreds);

    fc_snprintf(buffer, sizeof(buffer), "city.t_food_%d", i);
    SET_SPRITE(city.tile_foodnum[i], buffer);
    fc_snprintf(buffer, sizeof(buffer), "city.t_shields_%d", i);
    SET_SPRITE(city.tile_shieldnum[i], buffer);
    fc_snprintf(buffer, sizeof(buffer), "city.t_trade_%d", i);
    SET_SPRITE(city.tile_tradenum[i], buffer);
  }
#undef SET_GOTO_TURN_SPRITE

  /* Must have at least one upkeep sprite per output type (and unhappy) */
  /* The rest are optional; we copy the previous sprite for unspecified ones */
  fc_strlcpy(buffer, "upkeep.unhappy", sizeof(buffer));
  SET_SPRITE(upkeep.unhappy[0], buffer);
  for(i=1; i<MAX_NUM_UPKEEP_SPRITES; i++) {
    fc_snprintf(buffer2, sizeof(buffer2), "upkeep.unhappy%d", i+1);
    if (sprite_exists(t, buffer2)) {
      SET_SPRITE(upkeep.unhappy[i], buffer2);
      fc_strlcpy(buffer, buffer2, sizeof(buffer));
    } else {
      SET_SPRITE(upkeep.unhappy[i], buffer);
    }
  }
  output_type_iterate(o) {
    fc_snprintf(buffer, sizeof(buffer),
                "upkeep.%s", get_output_identifier(o));
    SET_SPRITE_OPT(upkeep.output[o][0], buffer);
    for(i=1; i<MAX_NUM_UPKEEP_SPRITES; i++) {
      fc_snprintf(buffer2, sizeof(buffer2),
                  "upkeep.%s%d", get_output_identifier(o), i+1);
      if (sprite_exists(t, buffer2)) {
        SET_SPRITE(upkeep.output[o][i], buffer2);
        fc_strlcpy(buffer, buffer2, sizeof(buffer));
      } else {
        /* Optional, as maybe the upkeep 1 sprite didn't exist either */
        SET_SPRITE_OPT(upkeep.output[o][i], buffer);
      }
    }
  } output_type_iterate_end;

  t->max_upkeep_height = calculate_max_upkeep_height(t);

  SET_SPRITE(user.attention, "user.attention");

  SET_SPRITE_OPT(path.s[GTS_MP_LEFT].specific, "path.normal");
  SET_SPRITE_OPT(path.s[GTS_EXHAUSTED_MP].specific, "path.exhausted_mp");
  SET_SPRITE_OPT(path.s[GTS_TURN_STEP].specific, "path.step");
  SET_SPRITE(path.waypoint, "path.waypoint");

  SET_SPRITE_NOTSMOOTH(tx.fog, "tx.fog");

  sprite_vector_init(&t->sprites.colors.overlays);
  for (i = 0; ; i++) {
    struct sprite *sprite;

    fc_snprintf(buffer, sizeof(buffer), "colors.overlay_%d", i);
    sprite = load_sprite(t, buffer, TRUE, TRUE);
    if (!sprite) {
      break;
    }
    sprite_vector_append(&t->sprites.colors.overlays, sprite);
  }
  if (i == 0) {
    tileset_error(LOG_FATAL, _("Missing overlay-color sprite colors.overlay_0."));
  }

  /* Chop up and build the overlay graphics. */
  sprite_vector_reserve(&t->sprites.city.worked_tile_overlay,
			sprite_vector_size(&t->sprites.colors.overlays));
  sprite_vector_reserve(&t->sprites.city.unworked_tile_overlay,
			sprite_vector_size(&t->sprites.colors.overlays));
  for (i = 0; i < sprite_vector_size(&t->sprites.colors.overlays); i++) {
    struct sprite *color, *color_mask;
    struct sprite *worked, *unworked;

    color = *sprite_vector_get(&t->sprites.colors.overlays, i);
    color_mask = crop_sprite(color, 0, 0, W, H, t->sprites.mask.tile, 0, 0,
                             1.0f, FALSE);
    worked = crop_sprite(color_mask, 0, 0, W, H,
			 t->sprites.mask.worked_tile, 0, 0, 1.0f, FALSE);
    unworked = crop_sprite(color_mask, 0, 0, W, H,
			   t->sprites.mask.unworked_tile, 0, 0, 1.0f, FALSE);
    free_sprite(color_mask);
    t->sprites.city.worked_tile_overlay.p[i] =  worked;
    t->sprites.city.unworked_tile_overlay.p[i] = unworked;
  }


  {
    SET_SPRITE(grid.unavailable, "grid.unavailable");
    SET_SPRITE_OPT(grid.nonnative, "grid.nonnative");

    for (i = 0; i < EDGE_COUNT; i++) {
      int be;

      if (i == EDGE_UD && t->hex_width == 0) {
	continue;
      } else if (i == EDGE_LR && t->hex_height == 0) {
	continue;
      }

      fc_snprintf(buffer, sizeof(buffer), "grid.main.%s", edge_name[i]);
      SET_SPRITE(grid.main[i], buffer);

      fc_snprintf(buffer, sizeof(buffer), "grid.city.%s", edge_name[i]);
      SET_SPRITE(grid.city[i], buffer);

      fc_snprintf(buffer, sizeof(buffer), "grid.worked.%s", edge_name[i]);
      SET_SPRITE(grid.worked[i], buffer);

      fc_snprintf(buffer, sizeof(buffer), "grid.selected.%s", edge_name[i]);
      SET_SPRITE(grid.selected[i], buffer);

      fc_snprintf(buffer, sizeof(buffer), "grid.coastline.%s", edge_name[i]);
      SET_SPRITE(grid.coastline[i], buffer);

      for (be = 0; be < 2; be++) {
        fc_snprintf(buffer, sizeof(buffer), "grid.borders.%c",
                    edge_name[i][be]);
        SET_SPRITE(grid.borders[i][be], buffer);
      }
    }
  }

  switch (t->darkness_style) {
  case DARKNESS_NONE:
    /* Nothing. */
    break;
  case DARKNESS_ISORECT:
    {
      /* Isometric: take a single tx.darkness tile and split it into 4. */
      struct sprite *darkness = load_sprite(t, "tx.darkness", TRUE, FALSE);
      const int ntw = t->normal_tile_width, nth = t->normal_tile_height;
      int offsets[4][2] = {{ntw / 2, 0}, {0, nth / 2}, {ntw / 2, nth / 2}, {0, 0}};

      if (!darkness) {
        tileset_error(LOG_FATAL, _("Sprite tx.darkness missing."));
      }
      for (i = 0; i < 4; i++) {
        t->sprites.tx.darkness[i] = crop_sprite(darkness, offsets[i][0],
                                                offsets[i][1], ntw / 2,
                                                nth / 2, NULL, 0, 0, 1.0f,
                                                FALSE);
      }
    }
    break;
  case DARKNESS_CARD_SINGLE:
    for (i = 0; i < t->num_cardinal_tileset_dirs; i++) {
      enum direction8 dir = t->cardinal_tileset_dirs[i];

      fc_snprintf(buffer, sizeof(buffer), "tx.darkness_%s",
		  dir_get_tileset_name(dir));
      SET_SPRITE_NOTSMOOTH(tx.darkness[i], buffer);
    }
    break;
  case DARKNESS_CARD_FULL:
    for(i = 1; i < t->num_index_cardinal; i++) {
      fc_snprintf(buffer, sizeof(buffer), "tx.darkness_%s",
		  cardinal_index_str(t, i));
      SET_SPRITE_NOTSMOOTH(tx.darkness[i], buffer);
    }
    break;
  case DARKNESS_CORNER:
    t->sprites.tx.fullfog = fc_realloc(t->sprites.tx.fullfog,
				       81 * sizeof(*t->sprites.tx.fullfog));
    for (i = 0; i < 81; i++) {
      /* Unknown, fog, known. */
      char ids[] = {'u', 'f', 'k'};
      char buf[512] = "t.fog";
      int values[4], vi, k = i;

      for (vi = 0; vi < 4; vi++) {
	values[vi] = k % 3;
	k /= 3;

	cat_snprintf(buf, sizeof(buf), "_%c", ids[values[vi]]);
      }
      fc_assert(k == 0);

      t->sprites.tx.fullfog[i] = load_sprite(t, buf, TRUE, FALSE);
    }
    break;
  };

  /* no other place to initialize these variables */
  sprite_vector_init(&t->sprites.nation_flag);
  sprite_vector_init(&t->sprites.nation_shield);
}

/**************************************************************************
  Load sprites of one river type.
**************************************************************************/
static bool load_river_sprites(struct tileset *t,
                               struct river_sprites *store, const char *tag_pfx)
{
  int i;
  char buffer[512];

  for (i = 0; i < t->num_index_cardinal; i++) {
    fc_snprintf(buffer, sizeof(buffer), "%s_s_%s",
                tag_pfx, cardinal_index_str(t, i));
    store->spec[i] = load_sprite(t, buffer, TRUE, TRUE);
    if (store->spec[i] == NULL) {
      return FALSE;
    }
  }

  for (i = 0; i < t->num_cardinal_tileset_dirs; i++) {
    fc_snprintf(buffer, sizeof(buffer), "%s_outlet_%s",
                tag_pfx, dir_get_tileset_name(t->cardinal_tileset_dirs[i]));
    store->outlet[i] = load_sprite(t, buffer, TRUE, TRUE);
    if (store->outlet[i] == NULL) {
      if (t->cardinal_tileset_dirs[i] == DIR8_NORTHWEST
          || t->cardinal_tileset_dirs[i] == DIR8_NORTHEAST
          || t->cardinal_tileset_dirs[i] == DIR8_SOUTHEAST
          || t->cardinal_tileset_dirs[i] == DIR8_SOUTHWEST) {
        log_debug("Missing \"%s\", support for this is deprecated.", buffer);
      } else {
        return FALSE;
      }
    }
  }

  return TRUE;
}

/**************************************************************************
  Frees any internal buffers which are created by load_sprite. Should
  be called after the last (for a given period of time) load_sprite
  call.  This saves a fair amount of memory, but it will take extra time
  the next time we start loading sprites again.
**************************************************************************/
void finish_loading_sprites(struct tileset *t)
{
  specfile_list_iterate(t->specfiles, sf) {
    if (sf->big_sprite) {
      free_sprite(sf->big_sprite);
      sf->big_sprite = NULL;
    }
  } specfile_list_iterate_end;
}

/**********************************************************************
  Load the tiles; requires tilespec_read_toplevel() called previously.
  Leads to tile_sprites being allocated and filled with pointers
  to sprites.   Also sets up and populates sprite_hash, and calls func
  to initialize 'sprites' structure.
***********************************************************************/
void tileset_load_tiles(struct tileset *t)
{
  tileset_lookup_sprite_tags(t);
  finish_loading_sprites(t);
}

/**********************************************************************
  Lookup sprite to match tag, or else to match alt if don't find,
  or else return NULL, and emit log message.
***********************************************************************/
struct sprite *tiles_lookup_sprite_tag_alt(struct tileset *t,
                                           enum log_level level,
                                           const char *tag, const char *alt,
                                           const char *what,
                                           const char *name,
                                           bool scale)
{
  struct sprite *sp;

  /* (should get sprite_hash before connection) */
  fc_assert_ret_val_msg(NULL != t->sprite_hash, NULL,
                        "attempt to lookup for %s \"%s\" before "
                        "sprite_hash setup", what, name);

  sp = load_sprite(t, tag, scale, TRUE);
  if (sp) return sp;

  sp = load_sprite(t, alt, scale, TRUE);
  if (sp) {
    log_verbose("Using alternate graphic \"%s\" "
                "(instead of \"%s\") for %s \"%s\".",
                alt, tag, what, name);
    return sp;
  }

  tileset_error(level, _("Don't have graphics tags \"%s\" or \"%s\" for %s \"%s\"."),
                  tag, alt, what, name);

  return NULL;
}

/**********************************************************************
  Helper function to load sprite for one unit orientation
***********************************************************************/
static bool tileset_setup_unit_direction(struct tileset *t,
                                         int uidx,
                                         const char *base_str,
                                         enum direction8 dir,
                                         const char *dirsuffix)
{
  char buf[2048];

  fc_snprintf(buf, sizeof(buf), "%s_%s", base_str, dirsuffix);

  /* We don't use _alt graphics here, as that could lead to loading
   * real icon gfx, but alternative orientation gfx. Tileset author
   * probably meant icon gfx to be used as fallback for all orientations */
  t->sprites.units.facing[uidx][dir] = load_sprite(t, buf, TRUE, TRUE);

  if (t->sprites.units.facing[uidx][dir] != NULL) {
    return TRUE;
  }

  return FALSE;
}

/**********************************************************************
  Try to setup all unit type sprites from single tag
***********************************************************************/
static bool tileset_setup_unit_type_from_tag(struct tileset *t,
                                             int uidx, const char *tag)
{
  bool facing_sprites = TRUE;

  t->sprites.units.icon[uidx] = load_sprite(t, tag, TRUE, TRUE);

#define LOAD_FACING_SPRITE(dir, dname)                           \
  if (!tileset_setup_unit_direction(t, uidx, tag, dir, dname)) { \
    facing_sprites = FALSE;                                      \
  }

  LOAD_FACING_SPRITE(DIR8_NORTHWEST, "nw");
  LOAD_FACING_SPRITE(DIR8_NORTH, "n");
  LOAD_FACING_SPRITE(DIR8_NORTHEAST, "ne");
  LOAD_FACING_SPRITE(DIR8_WEST, "w");
  LOAD_FACING_SPRITE(DIR8_EAST, "e");
  LOAD_FACING_SPRITE(DIR8_SOUTHWEST, "sw");
  LOAD_FACING_SPRITE(DIR8_SOUTH, "s");
  LOAD_FACING_SPRITE(DIR8_SOUTHEAST, "se");

  if (!facing_sprites && t->sprites.units.icon[uidx] == NULL) {
    /* Neither icon gfx or orientation sprites */
    return FALSE;
  }

  /* Fix a random orientation for displaying unit type in help etc.
   * We don't necessarily know the map topology yet, so choose a seed
   * that can be used to pick a valid direction later (24 is lcm(6,8)).
   * See get_unittype_sprite(). */
  t->sprites.units.default_dir_seed[uidx] = fc_rand(24);

  return TRUE;

#undef LOAD_FACING_SPRITE
}

/**********************************************************************
  Set unit_type sprite value; should only happen after
  tilespec_load_tiles().
***********************************************************************/
void tileset_setup_unit_type(struct tileset *t, struct unit_type *ut)
{
  int uidx = utype_index(ut);

  if (!tileset_setup_unit_type_from_tag(t, uidx, ut->graphic_str)
      && !tileset_setup_unit_type_from_tag(t, uidx, ut->graphic_alt)) {
    tileset_error(LOG_FATAL, _("Missing %s unit sprite for tags \"%s\" and alternative \"%s\"."),
                  utype_rule_name(ut), ut->graphic_str, ut->graphic_alt);
  }
}

/**********************************************************************
  Set improvement_type sprite value; should only happen after
  tilespec_load_tiles().
***********************************************************************/
void tileset_setup_impr_type(struct tileset *t,
			     struct impr_type *pimprove)
{
  t->sprites.building[improvement_index(pimprove)] =
    tiles_lookup_sprite_tag_alt(t, LOG_VERBOSE, pimprove->graphic_str,
				pimprove->graphic_alt, "improvement",
				improvement_rule_name(pimprove), FALSE);

  /* should maybe do something if NULL, eg generic default? */
}

/**********************************************************************
  Set tech_type sprite value; should only happen after
  tilespec_load_tiles().
***********************************************************************/
void tileset_setup_tech_type(struct tileset *t,
			     struct advance *padvance)
{
  if (valid_advance(padvance)) {
    t->sprites.tech[advance_index(padvance)] =
      tiles_lookup_sprite_tag_alt(t, LOG_VERBOSE, padvance->graphic_str,
				  padvance->graphic_alt, "technology",
				  advance_rule_name(padvance), FALSE);

    /* should maybe do something if NULL, eg generic default? */
  } else {
    t->sprites.tech[advance_index(padvance)] = NULL;
  }
}

/****************************************************************************
  Set resource sprite values; should only happen after
  tilespec_load_tiles().
****************************************************************************/
void tileset_setup_resource(struct tileset *t,
                            const struct resource *presource)
{
  fc_assert_ret(NULL != presource);
  t->sprites.resource[resource_index(presource)] =
    tiles_lookup_sprite_tag_alt(t, LOG_VERBOSE, presource->graphic_str,
                                presource->graphic_alt, "resource",
                                resource_rule_name(presource), TRUE);
}

/****************************************************************************
  Set extra sprite values; should only happen after
  tilespec_load_tiles().
****************************************************************************/
void tileset_setup_extra(struct tileset *t,
                         struct extra_type *pextra)
{
  const int id = extra_index(pextra);
  enum extrastyle_id extrastyle;

  if (!fc_strcasecmp(pextra->graphic_str, "none")) {
    /* Extra without graphics */
    t->sprites.extras[id].extrastyle = extrastyle_id_invalid();
  } else {

    if (!estyle_hash_lookup(t->estyle_hash, pextra->graphic_str,
                            &extrastyle)
        && !estyle_hash_lookup(t->estyle_hash, pextra->graphic_alt,
                               &extrastyle)) {
      tileset_error(LOG_FATAL, _("No extrastyle for \"%s\" or \"%s\"."),
                    pextra->graphic_str,
                    pextra->graphic_alt);
    }

    t->sprites.extras[id].extrastyle = extrastyle;

    extra_type_list_append(t->style_lists[extrastyle], pextra);

    if (extra_has_flag(pextra, EF_SHOW_FLAG)) {
      extra_type_list_append(t->flagged_bases_list, pextra);
    }

    switch (extrastyle) {
    case ESTYLE_3LAYER:
      tileset_setup_base(t, pextra);
      break;

    case ESTYLE_ROAD_ALL_SEPARATE:
    case ESTYLE_ROAD_PARITY_COMBINED:
    case ESTYLE_ROAD_ALL_COMBINED:
    case ESTYLE_RIVER:
      tileset_setup_road(t, pextra);
      break;

    case ESTYLE_SINGLE1:
    case ESTYLE_SINGLE2:
      SET_SPRITE_ALT(extras[id].u.single, pextra->graphic_str, pextra->graphic_alt);
      break;

    case ESTYLE_CARDINALS:
      {
        int i;
        char buffer[512];

        /* We use direction-specific irrigation and farmland graphics, if they
         * are available.  If not, we just fall back to the basic irrigation
         * graphics. */
        for (i = 0; i < t->num_index_cardinal; i++) {
          fc_snprintf(buffer, sizeof(buffer), "%s_%s",
                      pextra->graphic_str, cardinal_index_str(t, i));
          t->sprites.extras[id].u.cardinals[i] = load_sprite(t, buffer,
                                                             TRUE, TRUE);
          if (!t->sprites.extras[id].u.cardinals[i]) {
            t->sprites.extras[id].u.cardinals[i] = load_sprite(t,
                                                  pextra->graphic_str, TRUE,
                                                  TRUE);
          }
          if (!t->sprites.extras[id].u.cardinals[i]) {
            fc_snprintf(buffer, sizeof(buffer), "%s_%s",
                        pextra->graphic_alt, cardinal_index_str(t, i));
            t->sprites.extras[id].u.cardinals[i] = load_sprite(t, buffer,
                                                               TRUE, TRUE);
          }
          if (!t->sprites.extras[id].u.cardinals[i]) {
            t->sprites.extras[id].u.cardinals[i] = load_sprite(t,
                                                  pextra->graphic_alt, TRUE,
                                                  TRUE);
          }
          if (!t->sprites.extras[id].u.cardinals[i]) {
            tileset_error(LOG_FATAL, _("Sprite for tags '%s' and alternate '%s' are "
                                       "both missing."),
                          pextra->graphic_str, pextra->graphic_alt);
          }
        }
      }
      break;
    case ESTYLE_COUNT:
      break;
    }
  }

  if (!fc_strcasecmp(pextra->activity_gfx, "none")) {
    t->sprites.extras[id].activity = NULL;
  } else {
    t->sprites.extras[id].activity = load_sprite(t, pextra->activity_gfx,
                                                 TRUE, TRUE);
    if (t->sprites.extras[id].activity == NULL) {
      t->sprites.extras[id].activity = load_sprite(t, pextra->act_gfx_alt,
                                                   TRUE, TRUE);
    }
    if (t->sprites.extras[id].activity == NULL) {
      t->sprites.extras[id].activity = load_sprite(t, pextra->act_gfx_alt2,
                                                   TRUE, TRUE);
    }
    if (t->sprites.extras[id].activity == NULL) {
      tileset_error(LOG_FATAL, _("Missing %s building activity sprite for tags \"%s\" and alternatives \"%s\" and \"%s\"."),
                    extra_rule_name(pextra), pextra->activity_gfx,
                    pextra->act_gfx_alt, pextra->act_gfx_alt2);
    }
  }

  if (!fc_strcasecmp(pextra->rmact_gfx, "none")) {
    t->sprites.extras[id].rmact = NULL;
  } else {
    t->sprites.extras[id].rmact = load_sprite(t, pextra->rmact_gfx, TRUE,
                                              TRUE);
    if (t->sprites.extras[id].rmact == NULL) {
      t->sprites.extras[id].rmact = load_sprite(t, pextra->rmact_gfx_alt,
                                                TRUE, TRUE);
      if (t->sprites.extras[id].rmact == NULL) {
        tileset_error(LOG_FATAL, _("Missing %s removal activity sprite for tags \"%s\" and alternative \"%s\"."),
                      extra_rule_name(pextra), pextra->rmact_gfx, pextra->rmact_gfx_alt);
      }
    }
  }
}

/****************************************************************************
  Set road sprite values; should only happen after
  tilespec_load_tiles().
****************************************************************************/
static void tileset_setup_road(struct tileset *t,
                               struct extra_type *pextra)
{
  char full_tag_name[MAX_LEN_NAME + strlen("_isolated")];
  char full_alt_name[MAX_LEN_NAME + strlen("_isolated")];
  const int id = extra_index(pextra);
  int i;
  enum extrastyle_id extrastyle = t->sprites.extras[id].extrastyle;

  /* Isolated road graphics are used by ESTYLE_ROAD_ALL_SEPARATE and
     ESTYLE_ROAD_PARITY_COMBINED. */
  if (extrastyle == ESTYLE_ROAD_ALL_SEPARATE
      || extrastyle == ESTYLE_ROAD_PARITY_COMBINED) {
    fc_snprintf(full_tag_name, sizeof(full_tag_name),
                "%s_isolated", pextra->graphic_str);
    fc_snprintf(full_alt_name, sizeof(full_alt_name),
                "%s_isolated", pextra->graphic_alt);

    SET_SPRITE_ALT(extras[id].u.road.isolated, full_tag_name, full_alt_name);
  }

  if (extrastyle == ESTYLE_ROAD_ALL_SEPARATE) {
    /* ESTYLE_ROAD_ALL_SEPARATE has just 8 additional sprites for each road type:
     * one going off in each direction. */
    for (i = 0; i < t->num_valid_tileset_dirs; i++) {
      enum direction8 dir = t->valid_tileset_dirs[i];
      const char *dir_name = dir_get_tileset_name(dir);

      fc_snprintf(full_tag_name, sizeof(full_tag_name),
                  "%s_%s", pextra->graphic_str, dir_name);
      fc_snprintf(full_alt_name, sizeof(full_alt_name),
                  "%s_%s", pextra->graphic_alt, dir_name);

      SET_SPRITE_ALT(extras[id].u.road.ru.dir[i], full_tag_name, full_alt_name);
    }
  } else if (extrastyle == ESTYLE_ROAD_PARITY_COMBINED) {
    int num_index = 1 << (t->num_valid_tileset_dirs / 2), j;

    /* ESTYLE_ROAD_PARITY_COMBINED has 32 additional sprites for each road type:
     * 16 each for cardinal and diagonal directions.  Each set
     * of 16 provides a NSEW-indexed sprite to provide connectors for
     * all rails in the cardinal/diagonal directions.  The 0 entry is
     * unused (the "isolated" sprite is used instead). */

    for (i = 1; i < num_index; i++) {
      char c[64] = "", d[64] = "";

      for (j = 0; j < t->num_valid_tileset_dirs / 2; j++) {
	int value = (i >> j) & 1;

	cat_snprintf(c, sizeof(c), "%s%d",
		     dir_get_tileset_name(t->valid_tileset_dirs[2 * j]),
		     value);
	cat_snprintf(d, sizeof(d), "%s%d",
		     dir_get_tileset_name(t->valid_tileset_dirs[2 * j + 1]),
		     value);
      }

      fc_snprintf(full_tag_name, sizeof(full_tag_name),
                  "%s_c_%s", pextra->graphic_str, c);
      fc_snprintf(full_alt_name, sizeof(full_alt_name),
                  "%s_c_%s", pextra->graphic_alt, c);

      SET_SPRITE_ALT(extras[id].u.road.ru.combo.even[i], full_tag_name, full_alt_name);

      fc_snprintf(full_tag_name, sizeof(full_tag_name),
                  "%s_d_%s", pextra->graphic_str, d);
      fc_snprintf(full_alt_name, sizeof(full_alt_name),
                  "%s_d_%s", pextra->graphic_alt, d);

      SET_SPRITE_ALT(extras[id].u.road.ru.combo.odd[i], full_tag_name, full_alt_name);
    }
  } else if (extrastyle == ESTYLE_ROAD_ALL_COMBINED) {
    /* ESTYLE_ROAD_ALL_COMBINED includes 256 sprites, one for every possibility.
     * Just go around clockwise, with all combinations. */
    for (i = 0; i < t->num_index_valid; i++) {
      char *idx_str = valid_index_str(t, i);

      fc_snprintf(full_tag_name, sizeof(full_tag_name),
                  "%s_%s", pextra->graphic_str, idx_str);
      fc_snprintf(full_alt_name, sizeof(full_alt_name),
                  "%s_%s", pextra->graphic_alt, idx_str);

      SET_SPRITE_ALT(extras[id].u.road.ru.total[i], full_tag_name, full_alt_name);
    }
  } else if (extrastyle == ESTYLE_RIVER) {
    if (!load_river_sprites(t, &t->sprites.extras[id].u.road.ru.rivers,
                            pextra->graphic_str)) {
      if (!load_river_sprites(t, &t->sprites.extras[id].u.road.ru.rivers,
                              pextra->graphic_alt)) {
        tileset_error(LOG_FATAL, _("Cannot load river \"%s\" or \"%s\""),
                      pextra->graphic_str, pextra->graphic_alt);
      }
    }
  } else {
    fc_assert(FALSE);
  }

  /* Corner road graphics are used by ESTYLE_ROAD_ALL_SEPARATE and
   * ESTYLE_ROAD_PARITY_COMBINED. */
  if (extrastyle == ESTYLE_ROAD_ALL_SEPARATE
      || extrastyle == ESTYLE_ROAD_PARITY_COMBINED) {
    for (i = 0; i < t->num_valid_tileset_dirs; i++) {
      enum direction8 dir = t->valid_tileset_dirs[i];

      if (!is_cardinal_tileset_dir(t, dir)) {
        const char *dtn = dir_get_tileset_name(dir);

        fc_snprintf(full_tag_name, sizeof(full_tag_name),
                    "%s_c_%s", pextra->graphic_str, dtn);
        fc_snprintf(full_alt_name, sizeof(full_alt_name),
                    "%s_c_%s", pextra->graphic_alt, dtn);

        SET_SPRITE_ALT_OPT(extras[id].u.road.corner[dir], full_tag_name, full_alt_name);
      }
    }
  }
}

/****************************************************************************
  Set base sprite values; should only happen after
  tilespec_load_tiles().
****************************************************************************/
static void tileset_setup_base(struct tileset *t,
                               const struct extra_type *pextra)
{
  char full_tag_name[MAX_LEN_NAME + strlen("_fg")];
  const int id = extra_index(pextra);

  fc_assert_ret(id >= 0 && id < extra_count());

  sz_strlcpy(full_tag_name, pextra->graphic_str);
  strcat(full_tag_name, "_bg");
  t->sprites.extras[id].u.bmf.background = load_sprite(t, full_tag_name,
                                                       TRUE, TRUE);

  sz_strlcpy(full_tag_name, pextra->graphic_str);
  strcat(full_tag_name, "_mg");
  t->sprites.extras[id].u.bmf.middleground = load_sprite(t, full_tag_name,
                                                         TRUE, TRUE);

  sz_strlcpy(full_tag_name, pextra->graphic_str);
  strcat(full_tag_name, "_fg");
  t->sprites.extras[id].u.bmf.foreground = load_sprite(t, full_tag_name,
                                                       TRUE, TRUE);

  if (t->sprites.extras[id].u.bmf.background == NULL
      && t->sprites.extras[id].u.bmf.middleground == NULL
      && t->sprites.extras[id].u.bmf.foreground == NULL) {
    /* No primary graphics at all. Try alternative */
    log_verbose("Using alternate graphic \"%s\" "
                "(instead of \"%s\") for extra \"%s\".",
                pextra->graphic_alt, pextra->graphic_str,
                extra_rule_name(pextra));

    sz_strlcpy(full_tag_name, pextra->graphic_alt);
    strcat(full_tag_name, "_bg");
    t->sprites.extras[id].u.bmf.background = load_sprite(t, full_tag_name,
                                                         TRUE, TRUE);

    sz_strlcpy(full_tag_name, pextra->graphic_alt);
    strcat(full_tag_name, "_mg");
    t->sprites.extras[id].u.bmf.middleground = load_sprite(t, full_tag_name,
                                                           TRUE, TRUE);

    sz_strlcpy(full_tag_name, pextra->graphic_alt);
    strcat(full_tag_name, "_fg");
    t->sprites.extras[id].u.bmf.foreground = load_sprite(t, full_tag_name,
                                                         TRUE, TRUE);

    if (t->sprites.extras[id].u.bmf.background == NULL
        && t->sprites.extras[id].u.bmf.middleground == NULL
        && t->sprites.extras[id].u.bmf.foreground == NULL) {
      /* Cannot find alternative graphics either */
      tileset_error(LOG_FATAL, _("No graphics for extra \"%s\" at all!"),
                    extra_rule_name(pextra));
    }
  }
}

/**********************************************************************
  Set tile_type sprite values; should only happen after
  tilespec_load_tiles().
***********************************************************************/
void tileset_setup_tile_type(struct tileset *t,
			     const struct terrain *pterrain)
{
  struct drawing_data *draw;
  struct sprite *sprite;
  char buffer[MAX_LEN_NAME + 20];
  int i, l;

  if (!drawing_hash_lookup(t->tile_hash, pterrain->graphic_str, &draw)
      && !drawing_hash_lookup(t->tile_hash, pterrain->graphic_alt, &draw)) {
    tileset_error(LOG_FATAL, _("Terrain \"%s\": no graphic tile \"%s\" or \"%s\"."),
                  terrain_rule_name(pterrain), pterrain->graphic_str,
                  pterrain->graphic_alt);
  }

  if (draw->init) {
    t->sprites.drawing[terrain_index(pterrain)] = draw;
    return;
  }

  /* Set up each layer of the drawing. */
  for (l = 0; l < draw->num_layers; l++) {
    struct drawing_layer *dlp = &draw->layer[l];
    struct tileset_layer *tslp = &t->layers[l];
    sprite_vector_init(&dlp->base);
    sprite_vector_init(&dlp->allocated);

    switch (dlp->sprite_type) {
    case CELL_WHOLE:
      switch (dlp->match_style) {
      case MATCH_NONE:
	/* Load whole sprites for this tile. */
	for (i = 0; ; i++) {
          fc_snprintf(buffer, sizeof(buffer), "t.l%d.%s%d",
                      l, draw->name, i + 1);
	  sprite = load_sprite(t, buffer, TRUE, FALSE);
	  if (!sprite) {
	    break;
	  }
	  sprite_vector_reserve(&dlp->base, i + 1);
	  dlp->base.p[i] = sprite;
	}
	/* check for base sprite, allowing missing sprites above base */
	if (0 == i  &&  0 == l) {
          /* TRANS: 'base' means 'base of terrain gfx', not 'military base' */ 
          tileset_error(LOG_FATAL, _("Missing base sprite for tag \"%s\"."), buffer);
	}
	break;
      case MATCH_SAME:
	/* Load 16 cardinally-matched sprites. */
	for (i = 0; i < t->num_index_cardinal; i++) {
          fc_snprintf(buffer, sizeof(buffer), "t.l%d.%s_%s",
                      l, draw->name, cardinal_index_str(t, i));
	  dlp->match[i] =
	    tiles_lookup_sprite_tag_alt(t, LOG_FATAL, buffer, "",
					"matched terrain",
					terrain_rule_name(pterrain), TRUE);
	}
	break;
      case MATCH_PAIR:
      case MATCH_FULL:
        fc_assert(FALSE); /* not yet defined */
        break;
      };
      break;
    case CELL_CORNER:
      {
	const int count = dlp->match_indices;
	int number = NUM_CORNER_DIRS;

	switch (dlp->match_style) {
	case MATCH_NONE:
	  /* do nothing */
	  break;
	case MATCH_PAIR:
	case MATCH_SAME:
	  /* N directions (NSEW) * 3 dimensions of matching */
          fc_assert(count == 2);
	  number = NUM_CORNER_DIRS * 2 * 2 * 2;
	  break;
	case MATCH_FULL:
	default:
	  /* N directions (NSEW) * 3 dimensions of matching */
	  /* could use exp() or expi() here? */
	  number = NUM_CORNER_DIRS * count * count * count;
	  break;
	};

	dlp->cells
	  = fc_calloc(number, sizeof(*dlp->cells));

	for (i = 0; i < number; i++) {
	  enum direction4 dir = i % NUM_CORNER_DIRS;
	  int value = i / NUM_CORNER_DIRS;

	  switch (dlp->match_style) {
	  case MATCH_NONE:
            fc_snprintf(buffer, sizeof(buffer), "t.l%d.%s_cell_%c",
                        l, draw->name, direction4letters[dir]);
	    dlp->cells[i] =
	      tiles_lookup_sprite_tag_alt(t, LOG_FATAL, buffer, "",
					  "cell terrain",
					  terrain_rule_name(pterrain), TRUE);
	    break;
	  case MATCH_SAME:
            fc_snprintf(buffer, sizeof(buffer), "t.l%d.%s_cell_%c%d%d%d",
                        l, draw->name, direction4letters[dir],
                        (value) & 1, (value >> 1) & 1, (value >> 2) & 1);
	    dlp->cells[i] =
	      tiles_lookup_sprite_tag_alt(t, LOG_FATAL, buffer, "",
					  "same cell terrain",
					  terrain_rule_name(pterrain), TRUE);
	    break;
	  case MATCH_PAIR:
            fc_snprintf(buffer, sizeof(buffer), "t.l%d.%s_cell_%c_%c_%c_%c",
                        l, draw->name, direction4letters[dir],
                        tslp->match_types[dlp->match_index[(value) & 1]][0],
                        tslp->match_types[dlp->match_index[(value >> 1) & 1]][0],
                        tslp->match_types[dlp->match_index[(value >> 2) & 1]][0]);
	    dlp->cells[i] =
	      tiles_lookup_sprite_tag_alt(t, LOG_FATAL, buffer, "",
					  "cell pair terrain",
					  terrain_rule_name(pterrain), TRUE);
	    break;
	  case MATCH_FULL:
	    {
	      int this = dlp->match_index[0];
	      int n, s, e, w;
	      int v1, v2, v3;

	      v1 = dlp->match_index[value % count];
	      value /= count;
	      v2 = dlp->match_index[value % count];
	      value /= count;
	      v3 = dlp->match_index[value % count];

              fc_assert(v1 < count && v2 < count && v3 < count);

	      /* Assume merged cells.  This should be a separate option. */
	      switch (dir) {
	      case DIR4_NORTH:
		s = this;
		w = v1;
		n = v2;
		e = v3;
		break;
	      case DIR4_EAST:
		w = this;
		n = v1;
		e = v2;
		s = v3;
		break;
	      case DIR4_SOUTH:
		n = this;
		e = v1;
		s = v2;
		w = v3;
		break;
	      case DIR4_WEST:
	      default:		/* avoid warnings */
		e = this;
		s = v1;
		w = v2;
		n = v3;
		break;
	      };

	      /* Use first character of match_types,
	       * already checked for uniqueness. */
              fc_snprintf(buffer, sizeof(buffer),
                          "t.l%d.cellgroup_%c_%c_%c_%c", l,
                          tslp->match_types[n][0], tslp->match_types[e][0],
                          tslp->match_types[s][0], tslp->match_types[w][0]);
	      sprite = load_sprite(t, buffer, TRUE, FALSE);

	      if (sprite) {
		/* Crop the sprite to separate this cell. */
                int vec_size = sprite_vector_size(&dlp->allocated);

		const int W = t->normal_tile_width;
		const int H = t->normal_tile_height;
		int x[4] = {W / 4, W / 4, 0, W / 2};
		int y[4] = {H / 2, 0, H / 4, H / 4};
		int xo[4] = {0, 0, -W / 2, W / 2};
		int yo[4] = {H / 2, -H / 2, 0, 0};

        sprite = crop_sprite(sprite, x[dir], y[dir], W / 2, H / 2,
                             t->sprites.mask.tile, xo[dir], yo[dir], 1.0f,
                             FALSE);
                /* We allocated new sprite with crop_sprite. Store its
                 * address so we can free it. */
                sprite_vector_reserve(&dlp->allocated, vec_size + 1);
                dlp->allocated.p[vec_size] = sprite;
	      } else {
                log_error("Terrain graphics sprite for tag \"%s\" missing.", buffer);
	      }

	      dlp->cells[i] = sprite;
	    }
	    break;
	  };
	}
      }
      break;
    };
  }

  /* try an optional special name */
  fc_snprintf(buffer, sizeof(buffer), "t.blend.%s", draw->name);
  draw->blender =
    tiles_lookup_sprite_tag_alt(t, LOG_VERBOSE, buffer, "",
				"blend terrain",
				terrain_rule_name(pterrain), TRUE);

  if (draw->blending > 0) {
    const int bl = draw->blending - 1;

    if (NULL == draw->blender) {
      int li = 0;

      /* try an already loaded base */
      while (NULL == draw->blender
        &&  li < draw->blending
        &&  0 < draw->layer[li].base.size) {
        draw->blender = draw->layer[li++].base.p[0];
      }
    }

    if (NULL == draw->blender) {
      /* try an unloaded base name */
      fc_snprintf(buffer, sizeof(buffer), "t.l%d.%s1", bl, draw->name);
      draw->blender =
	tiles_lookup_sprite_tag_alt(t, LOG_FATAL, buffer, "",
				    "base (blend) terrain",
				    terrain_rule_name(pterrain), TRUE);
    }
  }

  if (NULL != draw->blender) {
    /* Set up blending sprites. This only works in iso-view! */
    const int W = t->normal_tile_width;
    const int H = t->normal_tile_height;
    const int offsets[4][2] = {
      {W / 2, 0}, {0, H / 2}, {W / 2, H / 2}, {0, 0}
    };
    enum direction4 dir = 0;

    for (; dir < 4; dir++) {
      draw->blend[dir] = crop_sprite(draw->blender, offsets[dir][0],
                                     offsets[dir][1], W / 2, H / 2,
                                     t->sprites.dither_tile, 0, 0, 1.0f,
                                     FALSE);
    }
  }

  draw->init = TRUE;
  t->sprites.drawing[terrain_index(pterrain)] = draw;
}

/**********************************************************************
  Set government sprite value; should only happen after
  tilespec_load_tiles().
***********************************************************************/
void tileset_setup_government(struct tileset *t,
			      struct government *gov)
{
  t->sprites.government[government_index(gov)] =
    tiles_lookup_sprite_tag_alt(t, LOG_FATAL, gov->graphic_str,
				gov->graphic_alt, "government",
				government_rule_name(gov), FALSE);
  
  /* should probably do something if NULL, eg generic default? */
}

/**********************************************************************
  Set nation flag sprite value; should only happen after
  tilespec_load_tiles().
***********************************************************************/
void tileset_setup_nation_flag(struct tileset *t, 
			       struct nation_type *nation)
{
  char *tags[] = {nation->flag_graphic_str,
		  nation->flag_graphic_alt,
		  "unknown", NULL};
  int i;
  struct sprite *flag = NULL, *shield = NULL;
  char buf[1024];

  for (i = 0; tags[i] && !flag; i++) {
    fc_snprintf(buf, sizeof(buf), "f.%s", tags[i]);
    flag = load_sprite(t, buf, TRUE, TRUE);
  }
  for (i = 0; tags[i] && !shield; i++) {
    fc_snprintf(buf, sizeof(buf), "f.shield.%s", tags[i]);
    shield = load_sprite(t, buf, TRUE, TRUE);
  }
  if (!flag || !shield) {
    /* Should never get here because of the f.unknown fallback. */
    tileset_error(LOG_FATAL, _("Nation %s: no national flag."), nation_rule_name(nation));
  }

  sprite_vector_reserve(&t->sprites.nation_flag, nation_count());
  t->sprites.nation_flag.p[nation_index(nation)] = flag;

  sprite_vector_reserve(&t->sprites.nation_shield, nation_count());
  t->sprites.nation_shield.p[nation_index(nation)] = shield;
}

/**********************************************************************
  Return the flag graphic to be used by the city.
***********************************************************************/
struct sprite *get_city_flag_sprite(const struct tileset *t,
				    const struct city *pcity)
{
  return get_nation_flag_sprite(t, nation_of_city(pcity));
}

/**********************************************************************
  Return a sprite for the national flag for this unit.
***********************************************************************/
static struct sprite *get_unit_nation_flag_sprite(const struct tileset *t,
						  const struct unit *punit)
{
  struct nation_type *pnation = nation_of_unit(punit);

  if (gui_options.draw_unit_shields) {
    return t->sprites.nation_shield.p[nation_index(pnation)];
  } else {
    return t->sprites.nation_flag.p[nation_index(pnation)];
  }
}

#define FULL_TILE_X_OFFSET ((t->normal_tile_width - t->full_tile_width) / 2)
#define FULL_TILE_Y_OFFSET (t->normal_tile_height - t->full_tile_height)

#define ADD_SPRITE(s, draw_fog, x_offset, y_offset)			    \
  (fc_assert(s != NULL),						    \
   sprs->sprite = s,							    \
   sprs->foggable = (draw_fog && t->fogstyle == FOG_AUTO),		    \
   sprs->offset_x = x_offset,						    \
   sprs->offset_y = y_offset,						    \
   sprs++)
#define ADD_SPRITE_SIMPLE(s) ADD_SPRITE(s, TRUE, 0, 0)
#define ADD_SPRITE_FULL(s)						    \
  ADD_SPRITE(s, TRUE, FULL_TILE_X_OFFSET, FULL_TILE_Y_OFFSET)

/**************************************************************************
  Assemble some data that is used in building the tile sprite arrays.
    (map_x, map_y) : the (normalized) map position
  The values we fill in:
    tterrain_near  : terrain types of all adjacent terrain
    tspecial_near  : specials of all adjacent terrain
**************************************************************************/
static void build_tile_data(const struct tile *ptile,
			    struct terrain *pterrain,
			    struct terrain **tterrain_near,
			    bv_extras *textras_near)
{
  enum direction8 dir;

  /* Loop over all adjacent tiles.  We should have an iterator for this. */
  for (dir = 0; dir < 8; dir++) {
    struct tile *tile1 = mapstep(ptile, dir);

    if (tile1 && client_tile_get_known(tile1) != TILE_UNKNOWN) {
      struct terrain *terrain1 = tile_terrain(tile1);

      if (NULL != terrain1) {
        tterrain_near[dir] = terrain1;
        textras_near[dir] = *tile_extras(tile1);
        continue;
      }
      log_error("build_tile_data() tile (%d,%d) has no terrain!",
                TILE_XY(tile1));
    }
    /* At the edges of the (known) map, pretend the same terrain continued
     * past the edge of the map. */
    tterrain_near[dir] = pterrain;
    BV_CLR_ALL(textras_near[dir]);
  }
}

/**********************************************************************
  Fill in the sprite array for the unit type.
***********************************************************************/
static int fill_unit_type_sprite_array(const struct tileset *t,
                                       struct drawn_sprite *sprs,
                                       const struct unit_type *putype,
                                       enum direction8 facing)
{
  struct drawn_sprite *save_sprs = sprs;
  struct sprite *uspr = get_unittype_sprite(t, putype, facing, FALSE);

  ADD_SPRITE(uspr, TRUE,
             FULL_TILE_X_OFFSET + t->unit_offset_x,
             FULL_TILE_Y_OFFSET + t->unit_offset_y);

  return sprs - save_sprs;
}

/**********************************************************************
  Fill in the sprite array for the unit.
***********************************************************************/
static int fill_unit_sprite_array(const struct tileset *t,
                                  struct drawn_sprite *sprs,
                                  const struct unit *punit,
                                  bool stack, bool backdrop)
{
  struct drawn_sprite *save_sprs = sprs;
  int ihp;
  struct unit_type *ptype = unit_type_get(punit);

  if (backdrop) {
    if (!gui_options.solid_color_behind_units) {
      ADD_SPRITE(get_unit_nation_flag_sprite(t, punit), TRUE,
		 FULL_TILE_X_OFFSET + t->unit_flag_offset_x,
		 FULL_TILE_Y_OFFSET + t->unit_flag_offset_y);
    } else {
      /* Taken care of in the LAYER_BACKGROUND. */
    }
  }

  /* Add the sprite for the unit type. */
  sprs += fill_unit_type_sprite_array(t, sprs, ptype,
                                      punit->facing);

  if (t->sprites.unit.loaded && unit_transported(punit)) {
    ADD_SPRITE_FULL(t->sprites.unit.loaded);
  }

  if (punit->activity != ACTIVITY_IDLE) {
    struct sprite *s = NULL;

    switch (punit->activity) {
    case ACTIVITY_MINE:
      if (punit->activity_target == NULL) {
        s = t->sprites.unit.plant;
      } else {
        s = t->sprites.extras[extra_index(punit->activity_target)].activity;
      }
      break;
    case ACTIVITY_IRRIGATE:
     if (punit->activity_target == NULL) {
        s = t->sprites.unit.irrigate;
      } else {
        s = t->sprites.extras[extra_index(punit->activity_target)].activity;
      }
      break;
    case ACTIVITY_POLLUTION:
    case ACTIVITY_FALLOUT:
      s = t->sprites.extras[extra_index(punit->activity_target)].rmact;
      break;
    case ACTIVITY_PILLAGE:
      s = t->sprites.unit.pillage;
      break;
    case ACTIVITY_EXPLORE:
      s = t->sprites.unit.auto_explore;
      break;
    case ACTIVITY_FORTIFIED:
      s = t->sprites.unit.fortified;
      break;
    case ACTIVITY_FORTIFYING:
      s = t->sprites.unit.fortifying;
      break;
    case ACTIVITY_SENTRY:
      s = t->sprites.unit.sentry;
      break;
    case ACTIVITY_GOTO:
      s = t->sprites.unit.go_to;
      break;
    case ACTIVITY_TRANSFORM:
      s = t->sprites.unit.transform;
      break;
    case ACTIVITY_BASE:
    case ACTIVITY_GEN_ROAD:
      s = t->sprites.extras[extra_index(punit->activity_target)].activity;
      break;
    case ACTIVITY_CONVERT:
      s = t->sprites.unit.convert;
      break;
    default:
      break;
    }

    if (s != NULL) {
      ADD_SPRITE(s, TRUE, FULL_TILE_X_OFFSET + t->activity_offset_x,
                 FULL_TILE_Y_OFFSET + t->activity_offset_y);
    }
  }

  if (punit->ai_controlled && punit->activity != ACTIVITY_EXPLORE) {
    if (is_military_unit(punit)) {
      ADD_SPRITE_FULL(t->sprites.unit.auto_attack);
    } else {
      ADD_SPRITE_FULL(t->sprites.unit.auto_settler);
    }
  }

  if (unit_has_orders(punit)) {
    if (punit->orders.repeat) {
      ADD_SPRITE_FULL(t->sprites.unit.patrol);
    } else if (punit->activity != ACTIVITY_IDLE) {
      ADD_SPRITE_SIMPLE(t->sprites.unit.connect);
    } else {
      ADD_SPRITE(t->sprites.unit.go_to, TRUE, FULL_TILE_X_OFFSET + t->activity_offset_x,
                 FULL_TILE_Y_OFFSET + t->activity_offset_y);
    }
  }

  if (punit->battlegroup != BATTLEGROUP_NONE) {
    ADD_SPRITE_FULL(t->sprites.unit.battlegroup[punit->battlegroup]);
  }

  if (t->sprites.unit.lowfuel
      && utype_fuel(ptype)
      && punit->fuel == 1
      && punit->moves_left <= 2 * SINGLE_MOVE) {
    /* Show a low-fuel graphic if the plane has 2 or fewer moves left. */
    ADD_SPRITE_FULL(t->sprites.unit.lowfuel);
  }
  if (t->sprites.unit.tired
      && punit->moves_left < SINGLE_MOVE
      && ptype->move_rate > 0) {
    /* Show a "tired" graphic if the unit has fewer than one move
     * remaining, except for units for which it's full movement. */
    ADD_SPRITE_FULL(t->sprites.unit.tired);
  }

  if (stack || punit->client.occupied) {
    ADD_SPRITE_FULL(t->sprites.unit.stack);
  }

  if (t->sprites.unit.vet_lev[punit->veteran]) {
    ADD_SPRITE_FULL(t->sprites.unit.vet_lev[punit->veteran]);
  }

  ihp = ((NUM_TILES_HP_BAR-1)*punit->hp) / ptype->hp;
  ihp = CLIP(0, ihp, NUM_TILES_HP_BAR-1);
  ADD_SPRITE_FULL(t->sprites.unit.hp_bar[ihp]);

  return sprs - save_sprs;
}

/**************************************************************************
  Add any corner road sprites to the sprite array.
**************************************************************************/
static int fill_road_corner_sprites(const struct tileset *t,
                                    const struct extra_type *pextra,
				    struct drawn_sprite *sprs,
				    bool road, bool *road_near,
				    bool hider, bool *hider_near)
{
  struct drawn_sprite *saved_sprs = sprs;
  int i;
  int extra_idx = extra_index(pextra);

  if (is_cardinal_only_road(pextra)) {
    return 0;
  }

  /* Roads going diagonally adjacent to this tile need to be
   * partly drawn on this tile. */

  /* Draw the corner sprite if:
   *   - There is a diagonal road (not rail!) between two adjacent tiles.
   *   - There is no diagonal road (not rail!) that intersects this road.
   * The logic is simple: roads are drawn underneath railrods, but are
   * not always covered by them (even in the corners!).  But if a railroad
   * connects two tiles, only the railroad (no road) is drawn between
   * those tiles.
   */
  for (i = 0; i < t->num_valid_tileset_dirs; i++) {
    enum direction8 dir = t->valid_tileset_dirs[i];

    if (!is_cardinal_tileset_dir(t, dir)) {
      /* Draw corner sprites for this non-cardinal direction. */
      int cw = (i + 1) % t->num_valid_tileset_dirs;
      int ccw
	= (i + t->num_valid_tileset_dirs - 1) % t->num_valid_tileset_dirs;
      enum direction8 cwdir = t->valid_tileset_dirs[cw];
      enum direction8 ccwdir = t->valid_tileset_dirs[ccw];

      if (t->sprites.extras[extra_idx].u.road.corner[dir]
          && (road_near[cwdir] && road_near[ccwdir]
              && !(hider_near[cwdir] && hider_near[ccwdir]))
          && !(road && road_near[dir] && !(hider && hider_near[dir]))) {
        ADD_SPRITE_SIMPLE(t->sprites.extras[extra_idx].u.road.corner[dir]);
      }
    }
  }

  return sprs - saved_sprs;
}

/**************************************************************************
  Fill all road and rail sprites into the sprite array.
**************************************************************************/
static int fill_road_sprite_array(const struct tileset *t,
                                  const struct extra_type *pextra,
                                  struct drawn_sprite *sprs,
                                  bv_extras textras,
                                  bv_extras *textras_near,
                                  struct terrain *tterrain_near[8],
                                  const struct city *pcity)
{
  struct drawn_sprite *saved_sprs = sprs;
  bool road, road_near[8], hider, hider_near[8];
  bool land_near[8], hland_near[8];
  bool draw_road[8], draw_single_road;
  enum direction8 dir;
  int extra_idx = -1;
  bool cl = FALSE;
  enum extrastyle_id extrastyle;
  const struct road_type *proad = extra_road_get(pextra);

  extra_idx = extra_index(pextra);

  extrastyle = t->sprites.extras[extra_idx].extrastyle;

  if (extra_has_flag(pextra, EF_CONNECT_LAND)) {
    cl = TRUE;
  } else {
    int i;

    for (i = 0; i < 8; i++) {
      land_near[i] = FALSE;
    }
  }

  /* Fill some data arrays. rail_near and road_near store whether road/rail
   * is present in the given direction.  draw_rail and draw_road store
   * whether road/rail is to be drawn in that direction.  draw_single_road
   * and draw_single_rail store whether we need an isolated road/rail to be
   * drawn. */
  road = BV_ISSET(textras, extra_idx);

  hider = FALSE;
  extra_type_list_iterate(pextra->hiders, phider) {
    if (BV_ISSET(textras, extra_index(phider))) {
      hider = TRUE;
      break;
    }
  } extra_type_list_iterate_end;

  if (road && (!pcity || !gui_options.draw_cities) && !hider) {
    draw_single_road = TRUE;
  } else {
    draw_single_road = FALSE;
  }

  for (dir = 0; dir < 8; dir++) {
    bool roads_exist;

    /* Check if there is adjacent road/rail. */
    if (!is_cardinal_only_road(pextra)
        || is_cardinal_tileset_dir(t, dir)) {
      road_near[dir] = FALSE;
      extra_type_list_iterate(proad->integrators, iextra) {
        if (BV_ISSET(textras_near[dir], extra_index(iextra))) {
          road_near[dir] = TRUE;
          break;
        }
      } extra_type_list_iterate_end;
      if (cl) {
        land_near[dir] = (tterrain_near[dir] != T_UNKNOWN
                          && terrain_type_terrain_class(tterrain_near[dir]) != TC_OCEAN);
      }
    } else {
      road_near[dir] = FALSE;
      land_near[dir] = FALSE;
    }

    /* Draw rail/road if there is a connection from this tile to the
     * adjacent tile.  But don't draw road if there is also a rail
     * connection. */
    roads_exist = road && (road_near[dir] || land_near[dir]);
    draw_road[dir] = roads_exist;
    hider_near[dir] = FALSE;
    hland_near[dir] = tterrain_near[dir] != T_UNKNOWN
      && terrain_type_terrain_class(tterrain_near[dir]) != TC_OCEAN;
    extra_type_list_iterate(pextra->hiders, phider) {
      bool hider_dir = FALSE;
      bool land_dir = FALSE;

      if (!is_cardinal_only_road(phider)
          || is_cardinal_tileset_dir(t, dir)) {
        if (BV_ISSET(textras_near[dir], extra_index(phider))) {
          hider_near[dir] = TRUE;
          hider_dir = TRUE;
        }
        if (hland_near[dir]
            && is_extra_caused_by(phider, EC_ROAD)
            && extra_has_flag(phider, EF_CONNECT_LAND)) {
          land_dir = TRUE;
        }
        if (hider_dir || land_dir) {
          if (BV_ISSET(textras, extra_index(phider))) {
            draw_road[dir] = FALSE;
          }
        }
      }
    } extra_type_list_iterate_end;

    /* Don't draw an isolated road/rail if there's any connection.
     * draw_single_road would be true in the first place only if start tile has road,
     * so it will have road connection with any adjacent road tile. We check from real
     * existence of road (road_near[dir]) and not from whether road gets drawn (draw_road[dir])
     * as latter can be FALSE when road is simply hidden by another one, and we don't want to
     * draw single road in that case either. */
    if (draw_single_road && road_near[dir]) {
      draw_single_road = FALSE;
    }
  }

  /* Draw road corners */
  sprs
    += fill_road_corner_sprites(t, pextra, sprs, road, road_near, hider, hider_near);

  if (extrastyle == ESTYLE_ROAD_ALL_SEPARATE) {
    /* With ESTYLE_ROAD_ALL_SEPARATE, we simply draw one road for every connection.
     * This means we only need a few sprites, but a lot of drawing is
     * necessary and it generally doesn't look very good. */
    int i;

    /* First draw roads under rails. */
    if (road) {
      for (i = 0; i < t->num_valid_tileset_dirs; i++) {
	if (draw_road[t->valid_tileset_dirs[i]]) {
	  ADD_SPRITE_SIMPLE(t->sprites.extras[extra_idx].u.road.ru.dir[i]);
	}
      }
    }
  } else if (extrastyle == ESTYLE_ROAD_PARITY_COMBINED) {
    /* With ESTYLE_ROAD_PARITY_COMBINED, we draw one sprite for cardinal
     * road connections, one sprite for diagonal road connections.
     * This means we need about 4x more sprites than in style 0, but up to
     * 4x less drawing is needed.  The drawing quality may also be
     * improved. */

    /* First draw roads under rails. */
    if (road) {
      int road_even_tileno = 0, road_odd_tileno = 0, i;

      for (i = 0; i < t->num_valid_tileset_dirs / 2; i++) {
	enum direction8 even = t->valid_tileset_dirs[2 * i];
	enum direction8 odd = t->valid_tileset_dirs[2 * i + 1];

	if (draw_road[even]) {
	  road_even_tileno |= 1 << i;
	}
	if (draw_road[odd]) {
	  road_odd_tileno |= 1 << i;
	}
      }

      /* Draw the cardinal/even roads first. */
      if (road_even_tileno != 0) {
	ADD_SPRITE_SIMPLE(t->sprites.extras[extra_idx].u.road.ru.combo.even[road_even_tileno]);
      }
      if (road_odd_tileno != 0) {
	ADD_SPRITE_SIMPLE(t->sprites.extras[extra_idx].u.road.ru.combo.odd[road_odd_tileno]);
      }
    }
  } else if (extrastyle == ESTYLE_ROAD_ALL_COMBINED) {
    /* RSTYLE_ALL_COMBINED is a very simple method that lets us simply retrieve 
     * entire finished tiles, with a bitwise index of the presence of
     * roads in each direction. */

    /* Draw roads first */
    if (road) {
      int road_tileno = 0, i;

      for (i = 0; i < t->num_valid_tileset_dirs; i++) {
        enum direction8 vdir = t->valid_tileset_dirs[i];

        if (draw_road[vdir]) {
          road_tileno |= 1 << i;
        }
      }

      if (road_tileno != 0 || draw_single_road) {
        ADD_SPRITE_SIMPLE(t->sprites.extras[extra_idx].u.road.ru.total[road_tileno]);
      }
    }
  } else {
    fc_assert(FALSE);
  }

  /* Draw isolated rail/road separately (ESTYLE_ROAD_ALL_SEPARATE and
     ESTYLE_ROAD_PARITY_COMBINED only). */
  if (extrastyle == ESTYLE_ROAD_ALL_SEPARATE
      || extrastyle == ESTYLE_ROAD_PARITY_COMBINED) { 
    if (draw_single_road) {
      ADD_SPRITE_SIMPLE(t->sprites.extras[extra_idx].u.road.isolated);
    }
  }

  return sprs - saved_sprs;
}

/**************************************************************************
  Return the index of the sprite to be used for irrigation or farmland in
  this tile.

  We assume that the current tile has farmland or irrigation.  We then
  choose a sprite (index) based upon which cardinally adjacent tiles have
  either farmland or irrigation (the two are considered interchangable for
  this).
**************************************************************************/
static int get_irrigation_index(const struct tileset *t,
                                struct extra_type *pextra,
                                bv_extras *textras_near)
{
  int tileno = 0, i;

  for (i = 0; i < t->num_cardinal_tileset_dirs; i++) {
    enum direction8 dir = t->cardinal_tileset_dirs[i];

    if (BV_ISSET(textras_near[dir], extra_index(pextra))) {
      tileno |= 1 << i;
    }
  }

  return tileno;
}

/**************************************************************************
  Fill in the farmland/irrigation sprite for the tile.
**************************************************************************/
static int fill_irrigation_sprite_array(const struct tileset *t,
                                        struct drawn_sprite *sprs,
                                        bv_extras textras,
                                        bv_extras *textras_near,
                                        const struct city *pcity)
{
  struct drawn_sprite *saved_sprs = sprs;

  /* We don't draw the irrigation if there's a city (it just gets overdrawn
   * anyway, and ends up looking bad). */
  if (!(pcity && gui_options.draw_cities)) {
    extra_type_list_iterate(t->style_lists[ESTYLE_CARDINALS], pextra) {
      if (is_extra_drawing_enabled(pextra)) {
        int eidx = extra_index(pextra);

        if (BV_ISSET(textras, eidx)) {
          bool hidden = FALSE;

          extra_type_list_iterate(pextra->hiders, phider) {
            if (BV_ISSET(textras, extra_index(phider))) {
              hidden = TRUE;
              break;
            }
          } extra_type_list_iterate_end;

          if (!hidden) {
            int idx = get_irrigation_index(t, pextra, textras_near);

            ADD_SPRITE_SIMPLE(t->sprites.extras[eidx].u.cardinals[idx]);
          }
        }
      }
    } extra_type_list_iterate_end;
  }

  return sprs - saved_sprs;
}

/**************************************************************************
  Fill in the city overlays for the tile.  This includes the citymap
  overlays on the mapview as well as the tile output sprites.
**************************************************************************/
static int fill_city_overlays_sprite_array(const struct tileset *t,
					   struct drawn_sprite *sprs,
					   const struct tile *ptile,
					   const struct city *citymode)
{
  const struct city *pcity;
  const struct city *pwork;
  struct unit *psettler;
  struct drawn_sprite *saved_sprs = sprs;
  int city_x, city_y;
  const int NUM_CITY_COLORS = t->sprites.city.worked_tile_overlay.size;

  if (NULL == ptile || TILE_UNKNOWN == client_tile_get_known(ptile)) {
    return 0;
  }
  pwork = tile_worked(ptile);

  if (citymode) {
    pcity = citymode;
  } else {
    pcity = find_city_or_settler_near_tile(ptile, &psettler);
  }

  /* Below code does not work if pcity is invisible.
   * Make sure it is not. */
  fc_assert_ret_val(pcity == NULL || pcity->tile != NULL, 0);
  if (pcity && !pcity->tile) {
    pcity = NULL;
  }

  if (pcity && city_base_to_city_map(&city_x, &city_y, pcity, ptile)) {
    /* FIXME: check elsewhere for valid tile (instead of above) */

    if (!citymode && pcity->client.colored) {
      /* Add citymap overlay for a city. */
      int idx = pcity->client.color_index % NUM_CITY_COLORS;

      if (NULL != pwork && pwork == pcity) {
        ADD_SPRITE_SIMPLE(t->sprites.city.worked_tile_overlay.p[idx]);
      } else if (city_can_work_tile(pcity, ptile)) {
        ADD_SPRITE_SIMPLE(t->sprites.city.unworked_tile_overlay.p[idx]);
      }
    } else if (NULL != pwork && pwork == pcity
               && (citymode || gui_options.draw_city_output)) {
      /* Add on the tile output sprites. */
      int food = city_tile_output_now(pcity, ptile, O_FOOD);
      int shields = city_tile_output_now(pcity, ptile, O_SHIELD);
      int trade = city_tile_output_now(pcity, ptile, O_TRADE);
      const int ox = t->type == TS_ISOMETRIC ? t->normal_tile_width / 3 : 0;
      const int oy = t->type == TS_ISOMETRIC ? -t->normal_tile_height / 3 : 0;

      food = CLIP(0, food, NUM_TILES_DIGITS - 1);
      shields = CLIP(0, shields, NUM_TILES_DIGITS - 1);
      trade = CLIP(0, trade, NUM_TILES_DIGITS - 1);

      ADD_SPRITE(t->sprites.city.tile_foodnum[food], TRUE, ox, oy);
      ADD_SPRITE(t->sprites.city.tile_shieldnum[shields], TRUE, ox, oy);
      ADD_SPRITE(t->sprites.city.tile_tradenum[trade], TRUE, ox, oy);
    }
  } else if (psettler && psettler->client.colored) {
    /* Add citymap overlay for a unit. */
    int idx = psettler->client.color_index % NUM_CITY_COLORS;

    ADD_SPRITE_SIMPLE(t->sprites.city.unworked_tile_overlay.p[idx]);
  }

  return sprs - saved_sprs;
}

/****************************************************************************
  Helper function for fill_terrain_sprite_layer.
  Fill in the sprite array for blended terrain.
****************************************************************************/
static int fill_terrain_sprite_blending(const struct tileset *t,
					struct drawn_sprite *sprs,
					const struct tile *ptile,
					const struct terrain *pterrain,
					struct terrain **tterrain_near)
{
  struct drawn_sprite *saved_sprs = sprs;
  const int W = t->normal_tile_width, H = t->normal_tile_height;
  const int offsets[4][2] = {
    {W/2, 0}, {0, H / 2}, {W / 2, H / 2}, {0, 0}
  };
  enum direction4 dir = 0;

  /*
   * We want to mark unknown tiles so that an unreal tile will be
   * given the same marking as our current tile - that way we won't
   * get the "unknown" dither along the edge of the map.
   */
  for (; dir < 4; dir++) {
    struct tile *tile1 = mapstep(ptile, DIR4_TO_DIR8[dir]);
    struct terrain *other;

    if (!tile1
	|| client_tile_get_known(tile1) == TILE_UNKNOWN
	|| pterrain == (other = tterrain_near[DIR4_TO_DIR8[dir]])
	|| (0 == t->sprites.drawing[terrain_index(other)]->blending
	   &&  NULL == t->sprites.drawing[terrain_index(other)]->blender)) {
      continue;
    }

    ADD_SPRITE(t->sprites.drawing[terrain_index(other)]->blend[dir], TRUE,
               offsets[dir][0], offsets[dir][1]);
  }

  return sprs - saved_sprs;
}

/****************************************************************************
  Add sprites for fog (and some forms of darkness).
****************************************************************************/
static int fill_fog_sprite_array(const struct tileset *t,
				 struct drawn_sprite *sprs,
				 const struct tile *ptile,
				 const struct tile_edge *pedge,
				 const struct tile_corner *pcorner)
{
  struct drawn_sprite *saved_sprs = sprs;

  if (t->fogstyle == FOG_SPRITE && gui_options.draw_fog_of_war
      && NULL != ptile
      && TILE_KNOWN_UNSEEN == client_tile_get_known(ptile)) {
    /* With FOG_AUTO, fog is done this way. */
    ADD_SPRITE_SIMPLE(t->sprites.tx.fog);
  }

  if (t->darkness_style == DARKNESS_CORNER && pcorner
      && gui_options.draw_fog_of_war) {
    int i, tileno = 0;

    for (i = 3; i >= 0; i--) {
      const int unknown = 0, fogged = 1, known = 2;
      int value = -1;

      if (!pcorner->tile[i]) {
	value = fogged;
      } else {
	switch (client_tile_get_known(pcorner->tile[i])) {
	case TILE_KNOWN_SEEN:
	  value = known;
	  break;
	case TILE_KNOWN_UNSEEN:
	  value = fogged;
	  break;
	case TILE_UNKNOWN:
	  value = unknown;
	  break;
	}
      }
      fc_assert(value >= 0 && value < 3);

      tileno = tileno * 3 + value;
    }

    if (t->sprites.tx.fullfog[tileno]) {
      ADD_SPRITE_SIMPLE(t->sprites.tx.fullfog[tileno]);
    }
  }

  return sprs - saved_sprs;
}

/****************************************************************************
  Helper function for fill_terrain_sprite_layer.
****************************************************************************/
static int fill_terrain_sprite_array(struct tileset *t,
				     struct drawn_sprite *sprs,
				     int l, /* layer_num */
				     const struct tile *ptile,
				     const struct terrain *pterrain,
				     struct terrain **tterrain_near,
				     struct drawing_data *draw)
{
  struct drawn_sprite *saved_sprs = sprs;
  struct drawing_layer *dlp = &draw->layer[l];
  int this = dlp->match_index[0];
  int that = dlp->match_index[1];
  int ox = dlp->offset_x;
  int oy = dlp->offset_y;
  int i;

#define MATCH(dir)							    \
    (t->sprites.drawing[terrain_index(tterrain_near[(dir)])]->num_layers > l	    \
     ? t->sprites.drawing[terrain_index(tterrain_near[(dir)])]->layer[l].match_index[0] \
     : -1)

  switch (dlp->sprite_type) {
  case CELL_WHOLE:
    {
      switch (dlp->match_style) {
      case MATCH_NONE:
	{
	  int count = sprite_vector_size(&dlp->base);

	  if (count > 0) {
            /* Pseudo-random reproducable algorithm to pick a sprite. Use modulo
             * to limit the number to a handleable size [0..32000). */
            count = fc_randomly(tile_index(ptile) % 32000, count);

	    if (dlp->is_tall) {
	      ox += FULL_TILE_X_OFFSET;
	      oy += FULL_TILE_Y_OFFSET;
	    }
	    ADD_SPRITE(dlp->base.p[count], TRUE, ox, oy);
	  }
	  break;
	}
      case MATCH_SAME:
	{
	  int tileno = 0;

	  for (i = 0; i < t->num_cardinal_tileset_dirs; i++) {
	    enum direction8 dir = t->cardinal_tileset_dirs[i];

	    if (MATCH(dir) == this) {
	      tileno |= 1 << i;
	    }
	  }

	  if (dlp->is_tall) {
	    ox += FULL_TILE_X_OFFSET;
	    oy += FULL_TILE_Y_OFFSET;
	  }
	  ADD_SPRITE(dlp->match[tileno], TRUE, ox, oy);
	  break;
	}
      case MATCH_PAIR:
      case MATCH_FULL:
        fc_assert(FALSE); /* not yet defined */
        break;
      };
      break;
    }
  case CELL_CORNER:
    {
      /* Divide the tile up into four rectangular cells.  Each of these
       * cells covers one corner, and each is adjacent to 3 different
       * tiles.  For each cell we pick a sprite based upon the adjacent
       * terrains at each of those tiles.  Thus, we have 8 different sprites
       * for each of the 4 cells (32 sprites total).
       *
       * These arrays correspond to the direction4 ordering. */
      const int W = t->normal_tile_width;
      const int H = t->normal_tile_height;
      const int iso_offsets[4][2] = {
	{W / 4, 0}, {W / 4, H / 2}, {W / 2, H / 4}, {0, H / 4}
      };
      const int noniso_offsets[4][2] = {
	{0, 0}, {W / 2, H / 2}, {W / 2, 0}, {0, H / 2}
      };

      /* put corner cells */
      for (i = 0; i < NUM_CORNER_DIRS; i++) {
	const int count = dlp->match_indices;
	int array_index = 0;
	enum direction8 dir = dir_ccw(DIR4_TO_DIR8[i]);
	int x = (t->type == TS_ISOMETRIC ? iso_offsets[i][0] : noniso_offsets[i][0]);
	int y = (t->type == TS_ISOMETRIC ? iso_offsets[i][1] : noniso_offsets[i][1]);
	int m[3] = {MATCH(dir_ccw(dir)), MATCH(dir), MATCH(dir_cw(dir))};
	struct sprite *s;

	/* synthesize 4 dimensional array? */
	switch (dlp->match_style) {
	case MATCH_NONE:
	  /* We have no need for matching, just plug the piece in place. */
	  break;
	case MATCH_SAME:
	  array_index = array_index * 2 + (m[2] != this);
	  array_index = array_index * 2 + (m[1] != this);
	  array_index = array_index * 2 + (m[0] != this);
	  break;
	case MATCH_PAIR:
	  array_index = array_index * 2 + (m[2] == that);
	  array_index = array_index * 2 + (m[1] == that);
	  array_index = array_index * 2 + (m[0] == that);
	  break;
	case MATCH_FULL:
	default:
	  {
	    int n[3];
	    int j = 0;
	    for (; j < 3; j++) {
	      int k = 0;
	      for (; k < count; k++) {
		n[j] = k; /* default to last entry */
		if (m[j] == dlp->match_index[k])
		{
		  break;
		}
	      }
	    }
	    array_index = array_index * count + n[2];
	    array_index = array_index * count + n[1];
	    array_index = array_index * count + n[0];
	  }
	  break;
	};
	array_index = array_index * NUM_CORNER_DIRS + i;

	s = dlp->cells[array_index];
	if (s) {
	  ADD_SPRITE(s, TRUE, x, y);
	}
      }
      break;
    }
  };
#undef MATCH

  return sprs - saved_sprs;
}

/****************************************************************************
  Helper function for fill_terrain_sprite_layer.
  Fill in the sprite array of darkness.
****************************************************************************/
static int fill_terrain_sprite_darkness(struct tileset *t,
				     struct drawn_sprite *sprs,
				     const struct tile *ptile,
				     struct terrain **tterrain_near)
{
  struct drawn_sprite *saved_sprs = sprs;
  int i, tileno;
  struct tile *adjc_tile;

#define UNKNOWN(dir)                                        \
    ((adjc_tile = mapstep(ptile, (dir)))		    \
     && client_tile_get_known(adjc_tile) == TILE_UNKNOWN)

  switch (t->darkness_style) {
  case DARKNESS_NONE:
    break;
  case DARKNESS_ISORECT:
    for (i = 0; i < 4; i++) {
      const int W = t->normal_tile_width, H = t->normal_tile_height;
      int offsets[4][2] = {{W / 2, 0}, {0, H / 2}, {W / 2, H / 2}, {0, 0}};

      if (UNKNOWN(DIR4_TO_DIR8[i])) {
	ADD_SPRITE(t->sprites.tx.darkness[i], TRUE,
		   offsets[i][0], offsets[i][1]);
      }
    }
    break;
  case DARKNESS_CARD_SINGLE:
    for (i = 0; i < t->num_cardinal_tileset_dirs; i++) {
      if (UNKNOWN(t->cardinal_tileset_dirs[i])) {
	ADD_SPRITE_SIMPLE(t->sprites.tx.darkness[i]);
      }
    }
    break;
  case DARKNESS_CARD_FULL:
    /* We're looking to find the INDEX_NSEW for the directions that
     * are unknown.  We want to mark unknown tiles so that an unreal
     * tile will be given the same marking as our current tile - that
     * way we won't get the "unknown" dither along the edge of the
     * map. */
    tileno = 0;
    for (i = 0; i < t->num_cardinal_tileset_dirs; i++) {
      if (UNKNOWN(t->cardinal_tileset_dirs[i])) {
	tileno |= 1 << i;
      }
    }

    if (tileno != 0) {
      ADD_SPRITE_SIMPLE(t->sprites.tx.darkness[tileno]);
    }
    break;
  case DARKNESS_CORNER:
    /* Handled separately. */
    break;
  };
#undef UNKNOWN

  return sprs - saved_sprs;
}

/****************************************************************************
  Add sprites for the base tile to the sprite list.  This doesn't
  include specials or rivers.
****************************************************************************/
static int fill_terrain_sprite_layer(struct tileset *t,
				     struct drawn_sprite *sprs,
				     int layer_num,
				     const struct tile *ptile,
				     const struct terrain *pterrain,
				     struct terrain **tterrain_near)
{
  struct sprite *sprite;
  struct drawn_sprite *saved_sprs = sprs;
  struct drawing_data *draw = t->sprites.drawing[terrain_index(pterrain)];
  const int l = (draw->is_reversed
		 ? (draw->num_layers - layer_num - 1) : layer_num);

  fc_assert(layer_num < TERRAIN_LAYER_COUNT);

  /* Skip the normal drawing process. */
  /* FIXME: this should avoid calling load_sprite since it's slow and
   * increases the refcount without limit. */
  if (ptile->spec_sprite && (sprite = load_sprite(t, ptile->spec_sprite,
                                                  TRUE, FALSE))) {
    if (l == 0) {
      ADD_SPRITE_SIMPLE(sprite);
      return 1;
    } else {
      return 0;
    }
  }

  if (l < draw->num_layers) {
    sprs += fill_terrain_sprite_array(t, sprs, l, ptile, pterrain, tterrain_near, draw);

    if ((l + 1) == draw->blending) {
      sprs += fill_terrain_sprite_blending(t, sprs, ptile, pterrain, tterrain_near);
    }
  }

  /* Add darkness on top of the designed terrain layer. Note that darkness is always
   * drawn, even in citymode, etc. */
  if (layer_num == t->darkness_layer) {
    sprs += fill_terrain_sprite_darkness(t, sprs, ptile, tterrain_near);
  }

  return sprs - saved_sprs;
}

/****************************************************************************
  Indicate whether a unit is to be drawn with a surrounding city outline
  under current conditions.
  (This includes being in focus, but if the caller has already checked that,
  they can bypass this slightly expensive check with check_focus == FALSE.)
****************************************************************************/
bool unit_drawn_with_city_outline(const struct unit *punit, bool check_focus)
{
  /* Display an outline for city-builder type units if they are selected,
   * and on a tile where a city can be built.
   * But suppress the outline if the unit has orders (likely it is in
   * transit to somewhere else and this will just slow down redraws). */
  return gui_options.draw_city_outlines
         && unit_is_cityfounder(punit)
         && !unit_has_orders(punit)
         && (client_tile_get_known(unit_tile(punit)) != TILE_UNKNOWN
             && city_can_be_built_here(unit_tile(punit), punit))
         && (!check_focus || unit_is_in_focus(punit));
}

/****************************************************************************
  Fill in the grid sprites for the given tile, city, and unit.
****************************************************************************/
static int fill_grid_sprite_array(const struct tileset *t,
				  struct drawn_sprite *sprs,
				  const struct tile *ptile,
				  const struct tile_edge *pedge,
				  const struct tile_corner *pcorner,
				  const struct unit *punit,
				  const struct city *pcity,
				  const struct city *citymode)
{
  struct drawn_sprite *saved_sprs = sprs;

  if (pedge) {
    bool known[NUM_EDGE_TILES], city[NUM_EDGE_TILES];
    bool unit[NUM_EDGE_TILES], worked[NUM_EDGE_TILES];
    int i;
    struct unit_list *pfocus_units = get_units_in_focus();

    for (i = 0; i < NUM_EDGE_TILES; i++) {
      int dummy_x, dummy_y;
      const struct tile *tile = pedge->tile[i];
      struct player *powner = tile ? tile_owner(tile) : NULL;

      known[i] = tile && client_tile_get_known(tile) != TILE_UNKNOWN;
      unit[i] = FALSE;
      if (tile && !citymode) {
        unit_list_iterate(pfocus_units, pfocus_unit) {
          if (unit_drawn_with_city_outline(pfocus_unit, FALSE)) {
            struct tile *utile = unit_tile(pfocus_unit);
            int radius = game.info.init_city_radius_sq
              + get_target_bonus_effects(NULL, unit_owner(pfocus_unit), NULL,
                                         NULL, NULL, utile, NULL, NULL,
                                         NULL, NULL,
                                         EFT_CITY_RADIUS_SQ);

            if (city_tile_to_city_map(&dummy_x, &dummy_y, radius,
                                      utile, tile)) {
              unit[i] = TRUE;
              break;
            }
          }
        } unit_list_iterate_end;
      }
      worked[i] = FALSE;

      city[i] = (tile
                 && (NULL == powner || NULL == client.conn.playing
                     || powner == client.conn.playing)
                 && player_in_city_map(client.conn.playing, tile));
      if (city[i]) {
	if (citymode) {
	  /* In citymode, we only draw worked tiles for this city - other
	   * tiles may be marked as unavailable. */
	  worked[i] = (tile_worked(tile) == citymode);
	} else {
	  worked[i] = (NULL != tile_worked(tile));
	}
      }
    }

    if (mapdeco_is_highlight_set(pedge->tile[0])
        || mapdeco_is_highlight_set(pedge->tile[1])) {
      ADD_SPRITE_SIMPLE(t->sprites.grid.selected[pedge->type]);
    } else if (!gui_options.draw_terrain && gui_options.draw_coastline
	       && pedge->tile[0] && pedge->tile[1]
	       && known[0] && known[1]
	       && (is_ocean_tile(pedge->tile[0])
		   ^ is_ocean_tile(pedge->tile[1]))) {
      ADD_SPRITE_SIMPLE(t->sprites.grid.coastline[pedge->type]);
    } else {
      if (gui_options.draw_map_grid) {
        if (worked[0] || worked[1]) {
          ADD_SPRITE_SIMPLE(t->sprites.grid.worked[pedge->type]);
        } else if (city[0] || city[1]) {
          ADD_SPRITE_SIMPLE(t->sprites.grid.city[pedge->type]);
        } else if (known[0] || known[1]) {
          ADD_SPRITE_SIMPLE(t->sprites.grid.main[pedge->type]);
        }
      }
      if (gui_options.draw_city_outlines) {
        if (XOR(city[0], city[1])) {
          ADD_SPRITE_SIMPLE(t->sprites.grid.city[pedge->type]);
        }
        if (XOR(unit[0], unit[1])) {
          ADD_SPRITE_SIMPLE(t->sprites.grid.worked[pedge->type]);
        }
      }
    }

    if (gui_options.draw_borders
        && BORDERS_DISABLED != game.info.borders
        && known[0]
        && known[1]) {
      struct player *owner0 = tile_owner(pedge->tile[0]);
      struct player *owner1 = tile_owner(pedge->tile[1]);

      if (owner0 != owner1) {
        if (owner0) {
          int plrid = player_index(owner0);
          ADD_SPRITE_SIMPLE(t->sprites.player[plrid].grid_borders
                            [pedge->type][0]);
        }
        if (owner1) {
          int plrid = player_index(owner1);
          ADD_SPRITE_SIMPLE(t->sprites.player[plrid].grid_borders
                            [pedge->type][1]);
        }
      }
    }
  } else if (NULL != ptile && TILE_UNKNOWN != client_tile_get_known(ptile)) {
    int cx, cy;

    if (citymode
        /* test to ensure valid coordinates? */
        && city_base_to_city_map(&cx, &cy, citymode, ptile)
        && !client_city_can_work_tile(citymode, ptile)) {
      ADD_SPRITE_SIMPLE(t->sprites.grid.unavailable);
    }

    if (gui_options.draw_native && citymode == NULL) {
      bool native = TRUE;
      struct unit_list *pfocus_units = get_units_in_focus();

      unit_list_iterate(pfocus_units, pfocus) {
        if (!is_native_tile(unit_type_get(pfocus), ptile)) {
          native = FALSE;
          break;
        }
      } unit_list_iterate_end;

      if (!native) {
        if (t->sprites.grid.nonnative != NULL) {
          ADD_SPRITE_SIMPLE(t->sprites.grid.nonnative);
        } else {
          ADD_SPRITE_SIMPLE(t->sprites.grid.unavailable);
        }
      }
    }
  }

  return sprs - saved_sprs;
}

/****************************************************************************
  Fill in the given sprite array with any needed goto sprites.
****************************************************************************/
static int fill_goto_sprite_array(const struct tileset *t,
				  struct drawn_sprite *sprs,
				  const struct tile *ptile,
				  const struct tile_edge *pedge,
				  const struct tile_corner *pcorner)
{
  struct drawn_sprite *saved_sprs = sprs;
  struct sprite *sprite;
  bool warn = FALSE;
  enum goto_tile_state state;
  int length;
  bool waypoint;

  if (goto_tile_state(ptile, &state, &length, &waypoint)) {
    if (length >= 0) {
      fc_assert(state >= 0);
      fc_assert(state < ARRAY_SIZE(t->sprites.path.s));

      sprite = t->sprites.path.s[state].specific;
      if (sprite != NULL) {
        ADD_SPRITE(sprite, FALSE, 0, 0);
      }

      sprite = t->sprites.path.s[state].turns[length % 10];
      ADD_SPRITE_SIMPLE(sprite);
      if (length >= 10) {
        sprite = t->sprites.path.s[state].turns_tens[(length / 10) % 10];
        ADD_SPRITE_SIMPLE(sprite);
        if (length >= 100) {
          sprite = t->sprites.path.s[state].turns_hundreds[(length / 100)
                                                           % 10];

          if (sprite != NULL) {
            ADD_SPRITE_SIMPLE(sprite);
            if (length >= 1000) {
              warn = TRUE;
            }
          } else {
            warn = TRUE;
          }
        }
      }
    }

    if (waypoint) {
      ADD_SPRITE(t->sprites.path.waypoint, FALSE, 0, 0);
    }

    if (warn) {
      /* Warn only once by tileset. */
      static char last_reported[256] = "";

      if (0 != strcmp(last_reported, t->name)) {
        log_normal(_("Tileset \"%s\" doesn't support long goto paths, "
                     "such as %d. Path not displayed as expected."),
                   t->name, length);
        sz_strlcpy(last_reported, t->name);
      }
    }
  }

  return sprs - saved_sprs;
}

/****************************************************************************
  Should the given extra be drawn
****************************************************************************/
static bool is_extra_drawing_enabled(struct extra_type *pextra)
{
  bool no_disable = TRUE; /* Draw if matches no cause */

  if (is_extra_caused_by(pextra, EC_IRRIGATION)) {
    if (gui_options.draw_irrigation) {
      return TRUE;
    }
    no_disable = FALSE;
  }
  if (is_extra_caused_by(pextra, EC_POLLUTION)
      || is_extra_caused_by(pextra, EC_FALLOUT)) {
    if (gui_options.draw_pollution) {
      return TRUE;
    }
    no_disable = FALSE;
  }
  if (is_extra_caused_by(pextra, EC_MINE)) {
    if (gui_options.draw_mines) {
      return TRUE;
    }
    no_disable = FALSE;
  }
  if (is_extra_caused_by(pextra, EC_HUT)) {
    if (gui_options.draw_huts) {
      return TRUE;
    }
    no_disable = FALSE;
  }
  if (is_extra_caused_by(pextra, EC_BASE)) {
    if (gui_options.draw_fortress_airbase) {
      return TRUE;
    }
    no_disable = FALSE;
  }
  if (is_extra_caused_by(pextra, EC_ROAD)) {
    if (gui_options.draw_roads_rails) {
      return TRUE;
    }
    no_disable = FALSE;
  }

  return no_disable;
}

/****************************************************************************
  Fill in the sprite array for the given tile, city, and unit.

  ptile, if specified, gives the tile.  If specified the terrain and specials
  will be drawn for this tile.  In this case (map_x,map_y) should give the
  location of the tile.

  punit, if specified, gives the unit.  For tile drawing this should
  generally be get_drawable_unit(); otherwise it can be any unit.

  pcity, if specified, gives the city.  For tile drawing this should
  generally be tile_city(ptile); otherwise it can be any city.

  citymode specifies whether this is part of a citydlg.  If so some drawing
  is done differently.
****************************************************************************/
int fill_sprite_array(struct tileset *t,
                      struct drawn_sprite *sprs, enum mapview_layer layer,
                      const struct tile *ptile,
                      const struct tile_edge *pedge,
                      const struct tile_corner *pcorner,
                      const struct unit *punit, const struct city *pcity,
                      const struct city *citymode,
                      const struct unit_type *putype)
{
  int tileno, dir;
  bv_extras textras_near[8];
  bv_extras textras;
  struct terrain *tterrain_near[8];
  struct terrain *pterrain = NULL;
  struct drawn_sprite *save_sprs = sprs;
  struct player *owner = NULL;
  /* Unit drawing is disabled when the view options are turned off,
   * but only where we're drawing on the mapview. */
  bool do_draw_unit = (punit && (gui_options.draw_units || !ptile
				 || (gui_options.draw_focus_unit
				     && unit_is_in_focus(punit))));
  bool solid_bg = (gui_options.solid_color_behind_units
		   && (do_draw_unit
		       || (pcity && gui_options.draw_cities)
		       || (ptile && !gui_options.draw_terrain)));

  if (citymode) {
    int count = 0, i, cx, cy;
    const struct tile *const *tiles = NULL;
    bool valid = FALSE;

    if (ptile) {
      tiles = &ptile;
      count = 1;
    } else if (pcorner) {
      tiles = pcorner->tile;
      count = NUM_CORNER_TILES;
    } else if (pedge) {
      tiles = pedge->tile;
      count = NUM_EDGE_TILES;
    }

    for (i = 0; i < count; i++) {
      if (tiles[i] && city_base_to_city_map(&cx, &cy, citymode, tiles[i])) {
	valid = TRUE;
	break;
      }
    }
    if (!valid) {
      return 0;
    }
  }

  if (ptile && client_tile_get_known(ptile) != TILE_UNKNOWN) {
    textras = *tile_extras(ptile);
    pterrain = tile_terrain(ptile);

    if (NULL != pterrain) {
      if (layer == LAYER_TERRAIN1
          || layer == LAYER_TERRAIN2
          || layer == LAYER_TERRAIN3
          || layer == LAYER_WATER
          || layer == LAYER_ROADS) {
        build_tile_data(ptile, pterrain, tterrain_near, textras_near);
      }
    } else {
      log_error("fill_sprite_array() tile (%d,%d) has no terrain!",
                TILE_XY(ptile));
    }
  } else {
    BV_CLR_ALL(textras);
  }

  switch (layer) {
  case LAYER_BACKGROUND:
    /* Set up background color. */
    if (gui_options.solid_color_behind_units) {
      if (do_draw_unit) {
	owner = unit_owner(punit);
      } else if (pcity && gui_options.draw_cities) {
	owner = city_owner(pcity);
      }
    }
    if (owner) {
      ADD_SPRITE_SIMPLE(t->sprites.player[player_index(owner)].background);
    } else if (ptile && !gui_options.draw_terrain) {
      ADD_SPRITE_SIMPLE(t->sprites.background.graphic);
    }
    break;

  case LAYER_TERRAIN1:
    if (NULL != pterrain && gui_options.draw_terrain && !solid_bg) {
      sprs += fill_terrain_sprite_layer(t, sprs, 0, ptile, pterrain, tterrain_near);
    }
    break;

  case LAYER_TERRAIN2:
    if (NULL != pterrain && gui_options.draw_terrain && !solid_bg) {
      sprs += fill_terrain_sprite_layer(t, sprs, 1, ptile, pterrain, tterrain_near);
    }
    break;

  case LAYER_TERRAIN3:
    if (NULL != pterrain && gui_options.draw_terrain && !solid_bg) {
      fc_assert(MAX_NUM_LAYERS == 3);
      sprs += fill_terrain_sprite_layer(t, sprs, 2, ptile, pterrain, tterrain_near);
    }
    break;

  case LAYER_WATER:
    if (NULL != pterrain) {
      if (gui_options.draw_terrain && !solid_bg
          && terrain_type_terrain_class(pterrain) == TC_OCEAN) {
	for (dir = 0; dir < t->num_cardinal_tileset_dirs; dir++) {
          int didx = t->cardinal_tileset_dirs[dir];

          extra_type_list_iterate(t->style_lists[ESTYLE_RIVER], priver) {
            int idx = extra_index(priver);

            if (BV_ISSET(textras_near[didx], idx)) {
              if (t->sprites.extras[idx].u.road.ru.rivers.outlet[dir] != NULL) {
                ADD_SPRITE_SIMPLE(t->sprites.extras[idx].u.road.ru.rivers.outlet[dir]);
              }
              break;
            }
          } extra_type_list_iterate_end;
	}
      }

      sprs += fill_irrigation_sprite_array(t, sprs, textras, textras_near,
					   pcity);

      if (gui_options.draw_terrain && !solid_bg) {
        extra_type_list_iterate(t->style_lists[ESTYLE_RIVER], priver) {
          int idx = extra_index(priver);

          if (BV_ISSET(textras, idx)) {
            int i;

            /* Draw rivers on top of irrigation. */
            tileno = 0;
            for (i = 0; i < t->num_cardinal_tileset_dirs; i++) {
              enum direction8 cdir = t->cardinal_tileset_dirs[i];

              if (terrain_type_terrain_class(tterrain_near[cdir]) == TC_OCEAN
                  || BV_ISSET(textras_near[cdir], idx)) {
                tileno |= 1 << i;
              }
            }

            ADD_SPRITE_SIMPLE(t->sprites.extras[idx].u.road.ru.rivers.spec[tileno]);
          }
        } extra_type_list_iterate_end;
      }
    }
    break;

  case LAYER_ROADS:
    if (NULL != pterrain) {
      extra_type_list_iterate(t->style_lists[ESTYLE_ROAD_ALL_SEPARATE], pextra) {
        if (is_extra_drawing_enabled(pextra)) {
          sprs += fill_road_sprite_array(t, pextra, sprs,
                                         textras, textras_near,
                                         tterrain_near, pcity);
        }
      } extra_type_list_iterate_end;
      extra_type_list_iterate(t->style_lists[ESTYLE_ROAD_PARITY_COMBINED], pextra) {
        if (is_extra_drawing_enabled(pextra)) {
          sprs += fill_road_sprite_array(t, pextra, sprs,
                                         textras, textras_near,
                                         tterrain_near, pcity);
        }
      } extra_type_list_iterate_end;
      extra_type_list_iterate(t->style_lists[ESTYLE_ROAD_ALL_COMBINED], pextra) {
        if (is_extra_drawing_enabled(pextra)) {
          sprs += fill_road_sprite_array(t, pextra, sprs,
                                         textras, textras_near,
                                         tterrain_near, pcity);
        }
      } extra_type_list_iterate_end;
    }
    break;

  case LAYER_SPECIAL1:
    if (NULL != pterrain) {
      if (gui_options.draw_specials) {
	if (tile_resource_is_valid(ptile)) {
	  ADD_SPRITE_SIMPLE(t->sprites.resource[resource_index(tile_resource(ptile))]);
	}
      }

      if (ptile) {
        extra_type_list_iterate(t->style_lists[ESTYLE_3LAYER], pextra) {
          if (tile_has_extra(ptile, pextra)
              && is_extra_drawing_enabled(pextra)
              && t->sprites.extras[extra_index(pextra)].u.bmf.background) {
            bool hidden = FALSE;

            extra_type_list_iterate(pextra->hiders, phider) {
              if (BV_ISSET(textras, extra_index(phider))) {
                hidden = TRUE;
                break;
              }
            } extra_type_list_iterate_end;

            if (!hidden) {
              ADD_SPRITE_FULL(t->sprites.extras[extra_index(pextra)].u.bmf.background);
            }
          }
        } extra_type_list_iterate_end;
      }

      extra_type_list_iterate(t->style_lists[ESTYLE_SINGLE1], pextra) {
        if (BV_ISSET(textras, extra_index(pextra))
            && is_extra_drawing_enabled(pextra)) {
          bool hidden = FALSE;

          extra_type_list_iterate(pextra->hiders, phider) {
            if (BV_ISSET(textras, extra_index(phider))) {
              hidden = TRUE;
              break;
            }
          } extra_type_list_iterate_end;

          if (!hidden) {
            ADD_SPRITE_SIMPLE(t->sprites.extras[extra_index(pextra)].u.single);
          }
        }
      } extra_type_list_iterate_end;
    }
    break;

  case LAYER_GRID1:
    if (t->type == TS_ISOMETRIC) {
      sprs += fill_grid_sprite_array(t, sprs, ptile, pedge, pcorner,
				     punit, pcity, citymode);
    }
    break;

  case LAYER_CITY1:
    /* City.  Some city sprites are drawn later. */
    if (pcity && gui_options.draw_cities) {
      if (!gui_options.draw_full_citybar && !gui_options.solid_color_behind_units) {
	ADD_SPRITE(get_city_flag_sprite(t, pcity), TRUE,
		   FULL_TILE_X_OFFSET + t->city_flag_offset_x,
		   FULL_TILE_Y_OFFSET + t->city_flag_offset_y);
      }
      /* For iso-view the city.wall graphics include the full city, whereas
       * for non-iso view they are an overlay on top of the base city
       * graphic. */
      if (t->type == TS_OVERHEAD || pcity->client.walls <= 0) {
	ADD_SPRITE(get_city_sprite(t->sprites.city.tile, pcity), TRUE,
                   FULL_TILE_X_OFFSET + t->city_offset_x,
                   FULL_TILE_Y_OFFSET + t->city_offset_y);
      }
      if (t->type == TS_ISOMETRIC && pcity->client.walls > 0) {
        struct city_sprite *cspr = t->sprites.city.wall[pcity->client.walls - 1];
        struct sprite *spr = NULL;

        if (cspr != NULL) {
          spr = get_city_sprite(cspr, pcity);
        }
        if (spr == NULL) {
          cspr = t->sprites.city.single_wall;
          if (cspr != NULL) {
            spr = get_city_sprite(cspr, pcity);
          }
        }

        if (spr != NULL) {
          ADD_SPRITE(spr, TRUE,
                     FULL_TILE_X_OFFSET + t->city_offset_x,
                     FULL_TILE_Y_OFFSET + t->city_offset_y);
        }
      }
      if (!gui_options.draw_full_citybar && pcity->client.occupied) {
	ADD_SPRITE(get_city_sprite(t->sprites.city.occupied, pcity), TRUE,
                   FULL_TILE_X_OFFSET + t->occupied_offset_x,
                   FULL_TILE_Y_OFFSET + t->occupied_offset_y);
      }
      if (t->type == TS_OVERHEAD && pcity->client.walls > 0) {
        struct city_sprite *cspr = t->sprites.city.wall[pcity->client.walls - 1];
        struct sprite *spr = NULL;

        if (cspr != NULL) {
          spr = get_city_sprite(cspr, pcity);
        }
        if (spr == NULL) {
          cspr = t->sprites.city.single_wall;
          if (cspr != NULL) {
            spr = get_city_sprite(cspr, pcity);
          }
        }

        if (spr != NULL) {
          ADD_SPRITE_FULL(spr);
        }
      }
      if (pcity->client.unhappy) {
	ADD_SPRITE_FULL(t->sprites.city.disorder);
      }
    }
    break;

  case LAYER_SPECIAL2:
    if (NULL != pterrain) {
      if (ptile) {
        extra_type_list_iterate(t->style_lists[ESTYLE_3LAYER], pextra) {
          if (tile_has_extra(ptile, pextra)
              && is_extra_drawing_enabled(pextra)
              && t->sprites.extras[extra_index(pextra)].u.bmf.middleground) {
            bool hidden = FALSE;

            extra_type_list_iterate(pextra->hiders, phider) {
              if (BV_ISSET(textras, extra_index(phider))) {
                hidden = TRUE;
                break;
              }
            } extra_type_list_iterate_end;

            if (!hidden) {
              ADD_SPRITE_FULL(t->sprites.extras[extra_index(pextra)].u.bmf.middleground);
            }
          }
        } extra_type_list_iterate_end;
      }

      extra_type_list_iterate(t->style_lists[ESTYLE_SINGLE2], pextra) {
        if (BV_ISSET(textras, extra_index(pextra))
            && is_extra_drawing_enabled(pextra)) {
          bool hidden = FALSE;

          extra_type_list_iterate(pextra->hiders, phider) {
            if (BV_ISSET(textras, extra_index(phider))) {
              hidden = TRUE;
              break;
            }
          } extra_type_list_iterate_end;

          if (!hidden) {
            ADD_SPRITE_SIMPLE(t->sprites.extras[extra_index(pextra)].u.single);
          }
        }
      } extra_type_list_iterate_end;
    }
    break;

  case LAYER_UNIT:
  case LAYER_FOCUS_UNIT:
    if (do_draw_unit && XOR(layer == LAYER_UNIT, unit_is_in_focus(punit))) {
      bool stacked = ptile && (unit_list_size(ptile->units) > 1);
      bool backdrop = !pcity;

      if (ptile && unit_is_in_focus(punit)
	  && t->sprites.unit.select[0]) {
	/* Special case for drawing the selection rectangle.  The blinking
	 * unit is handled separately, inside get_drawable_unit(). */
	ADD_SPRITE_SIMPLE(t->sprites.unit.select[focus_unit_state]);
      }

      sprs += fill_unit_sprite_array(t, sprs, punit, stacked, backdrop);
    } else if (putype != NULL && layer == LAYER_UNIT) {
      /* Only the sprite for the unit type. */
      sprs += fill_unit_type_sprite_array(t, sprs, putype,
                                          direction8_invalid());
    }
    break;

  case LAYER_SPECIAL3:
    if (NULL != pterrain) {
      if (ptile) {
        bool show_flag = FALSE;
        struct player *eowner = extra_owner(ptile);

        extra_type_list_iterate(t->style_lists[ESTYLE_3LAYER], pextra) {
          if (is_extra_drawing_enabled(pextra)
              && tile_has_extra(ptile, pextra)
              && t->sprites.extras[extra_index(pextra)].u.bmf.foreground) {
            bool hidden = FALSE;

            extra_type_list_iterate(pextra->hiders, phider) {
              if (BV_ISSET(textras, extra_index(phider))) {
                hidden = TRUE;
                break;
              }
            } extra_type_list_iterate_end;

            if (!hidden) {
              if (t->sprites.extras[extra_index(pextra)].u.bmf.foreground) {
                ADD_SPRITE_FULL(t->sprites.extras[extra_index(pextra)].u.bmf.foreground);
              }
            }
          }
        } extra_type_list_iterate_end;

        /* Show base flag. Not part of previous iteration as
         * "extras of ESTYLE_3_LAYER" != "bases" */
        if (eowner != NULL) {
          extra_type_list_iterate(t->flagged_bases_list, pextra) {
            if (tile_has_extra(ptile, pextra)) {
              bool hidden = FALSE;

              extra_type_list_iterate(pextra->hiders, phider) {
                if (BV_ISSET(textras, extra_index(phider))) {
                  hidden = TRUE;
                  break;
                }
              } extra_type_list_iterate_end;

              if (!hidden) {
                show_flag = TRUE;
              }
            }
          } extra_type_list_iterate_end;

          if (show_flag) {
            ADD_SPRITE(get_nation_flag_sprite(t, nation_of_player(eowner)), TRUE,
                       FULL_TILE_X_OFFSET + t->city_flag_offset_x,
                       FULL_TILE_Y_OFFSET + t->city_flag_offset_y);
          }
        }
      }
    }
    break;

  case LAYER_FOG:
    sprs += fill_fog_sprite_array(t, sprs, ptile, pedge, pcorner);
    break;

  case LAYER_CITY2:
    /* City size.  Drawing this under fog makes it hard to read. */
    if (pcity && gui_options.draw_cities && !gui_options.draw_full_citybar) {
      bool warn = FALSE;
      unsigned int size = city_size_get(pcity);

      ADD_SPRITE(t->sprites.city.size[size % 10],
                 FALSE, FULL_TILE_X_OFFSET, FULL_TILE_Y_OFFSET);
      if (10 <= size) {
        ADD_SPRITE(t->sprites.city.size_tens[(size / 10)
                   % 10], FALSE, FULL_TILE_X_OFFSET, FULL_TILE_Y_OFFSET);
        if (100 <= size) {
          struct sprite *sprite =
              t->sprites.city.size_hundreds[(size / 100) % 10];

          if (NULL != sprite) {
            ADD_SPRITE(sprite, FALSE,
                       FULL_TILE_X_OFFSET, FULL_TILE_Y_OFFSET);
          } else {
            warn = TRUE;
          }
          if (1000 <= size) {
            warn = TRUE;
          }
        }
      }

      if (warn) {
        /* Warn only once by tileset. */
        static char last_reported[256] = "";

        if (0 != strcmp(last_reported, t->name)) {
          log_normal(_("Tileset \"%s\" doesn't support big cities size, "
                       "such as %d. Size not displayed as expected."),
                     t->name, size);
          sz_strlcpy(last_reported, t->name);
        }
      }
    }
    break;

  case LAYER_GRID2:
    if (t->type == TS_OVERHEAD) {
      sprs += fill_grid_sprite_array(t, sprs, ptile, pedge, pcorner,
				     punit, pcity, citymode);
    }
    break;

  case LAYER_OVERLAYS:
    sprs += fill_city_overlays_sprite_array(t, sprs, ptile, citymode);
    if (mapdeco_is_crosshair_set(ptile)) {
      ADD_SPRITE_SIMPLE(t->sprites.user.attention);
    }
    break;

  case LAYER_CITYBAR:
  case LAYER_TILELABEL:
    /* Nothing.  This is just a placeholder. */
    break;

  case LAYER_GOTO:
    if (ptile && goto_is_active()) {
      sprs += fill_goto_sprite_array(t, sprs, ptile, pedge, pcorner);
    }
    break;

  case LAYER_WORKERTASK:
    if (citymode != NULL && ptile != NULL) {
      worker_task_list_iterate(citymode->task_reqs, ptask) {
        if (ptask->ptile == ptile) {
          switch (ptask->act) {
          case ACTIVITY_MINE:
            if (ptask->tgt == NULL) {
              ADD_SPRITE(t->sprites.unit.plant,
                         TRUE, FULL_TILE_X_OFFSET + t->activity_offset_x,
                         FULL_TILE_Y_OFFSET + t->activity_offset_y);
            } else {
              ADD_SPRITE(t->sprites.extras[extra_index(ptask->tgt)].activity,
                         TRUE, FULL_TILE_X_OFFSET + t->activity_offset_x,
                         FULL_TILE_Y_OFFSET + t->activity_offset_y);
            }
            break;
          case ACTIVITY_IRRIGATE:
            if (ptask->tgt == NULL) {
              ADD_SPRITE(t->sprites.unit.irrigate,
                         TRUE, FULL_TILE_X_OFFSET + t->activity_offset_x,
                         FULL_TILE_Y_OFFSET + t->activity_offset_y);
            } else {
              ADD_SPRITE(t->sprites.extras[extra_index(ptask->tgt)].activity,
                         TRUE, FULL_TILE_X_OFFSET + t->activity_offset_x,
                         FULL_TILE_Y_OFFSET + t->activity_offset_y);
            }
            break;
          case ACTIVITY_GEN_ROAD:
            ADD_SPRITE(t->sprites.extras[extra_index(ptask->tgt)].activity,
                       TRUE, FULL_TILE_X_OFFSET + t->activity_offset_x,
                       FULL_TILE_Y_OFFSET + t->activity_offset_y);
            break;
          case ACTIVITY_TRANSFORM:
            ADD_SPRITE(t->sprites.unit.transform,
                         TRUE, FULL_TILE_X_OFFSET + t->activity_offset_x,
                         FULL_TILE_Y_OFFSET + t->activity_offset_y);
            break;
          case ACTIVITY_POLLUTION:
          case ACTIVITY_FALLOUT:
            ADD_SPRITE(t->sprites.extras[extra_index(ptask->tgt)].rmact,
                       TRUE, FULL_TILE_X_OFFSET + t->activity_offset_x,
                       FULL_TILE_Y_OFFSET + t->activity_offset_y);
            break;
          default:
            break;
          }
        }
      } worker_task_list_iterate_end;
    }
    break;

  case LAYER_EDITOR:
    if (ptile && editor_is_active()) {
      if (editor_tile_is_selected(ptile)) {
        int color = 2 % tileset_num_city_colors(tileset);
        ADD_SPRITE_SIMPLE(t->sprites.city.unworked_tile_overlay.p[color]);
      }

      if (NULL != map_startpos_get(ptile)) {
        /* FIXME: Use a more representative sprite. */
        ADD_SPRITE_SIMPLE(t->sprites.user.attention);
      }
    }
    break;

  case LAYER_COUNT:
    fc_assert(FALSE);
    break;
  }

  return sprs - save_sprs;
}

/**********************************************************************
  Set city tiles sprite values; should only happen after
  tilespec_load_tiles().
***********************************************************************/
void tileset_setup_city_tiles(struct tileset *t, int style)
{
  if (style == game.control.styles_count - 1) {
    int i;

    /* Free old sprites */
    free_city_sprite(t->sprites.city.tile);

    for (i = 0; i < NUM_WALL_TYPES; i++) {
      free_city_sprite(t->sprites.city.wall[i]);
      t->sprites.city.wall[i] = NULL;
    }
    free_city_sprite(t->sprites.city.single_wall);
    t->sprites.city.single_wall = NULL;

    free_city_sprite(t->sprites.city.occupied);

    t->sprites.city.tile = load_city_sprite(t, "city");

    for (i = 0; i < NUM_WALL_TYPES; i++) {
      char buffer[256];

      fc_snprintf(buffer, sizeof(buffer), "bldg_%d", i);
      t->sprites.city.wall[i] = load_city_sprite(t, buffer);
    }
    t->sprites.city.single_wall = load_city_sprite(t, "wall");

    t->sprites.city.occupied = load_city_sprite(t, "occupied");

    for (style = 0; style < game.control.styles_count; style++) {
      if (t->sprites.city.tile->styles[style].land_num_thresholds == 0) {
        tileset_error(LOG_FATAL, _("City style \"%s\": no city graphics."),
                      city_style_rule_name(style));
      }
      if (t->sprites.city.occupied->styles[style].land_num_thresholds == 0) {
        tileset_error(LOG_FATAL, _("City style \"%s\": no occupied graphics."),
                      city_style_rule_name(style));
      }
    }
  }
}

/****************************************************************************
  Return the amount of time between calls to toggle_focus_unit_state.
  The main loop needs to call toggle_focus_unit_state about this often
  to do the active-unit animation.
****************************************************************************/
double get_focus_unit_toggle_timeout(const struct tileset *t)
{
  if (t->sprites.unit.select[0]) {
    return 0.1;
  } else {
    return 0.5;
  }
}

/****************************************************************************
  Reset the focus unit state.  This should be called when changing
  focus units.
****************************************************************************/
void reset_focus_unit_state(struct tileset *t)
{
  focus_unit_state = 0;
}

/****************************************************************************
  Setup tileset for showing combat where focus unit participates.
****************************************************************************/
void focus_unit_in_combat(struct tileset *t)
{
  if (!t->sprites.unit.select[0]) {
    reset_focus_unit_state(t);
  }
}

/****************************************************************************
  Toggle/increment the focus unit state.  This should be called once
  every get_focus_unit_toggle_timeout() seconds.
****************************************************************************/
void toggle_focus_unit_state(struct tileset *t)
{
  focus_unit_state++;
  if (t->sprites.unit.select[0]) {
    focus_unit_state %= NUM_TILES_SELECT;
  } else {
    focus_unit_state %= 2;
  }
}

/**********************************************************************
  Find unit that we can display from given tile.
***********************************************************************/
struct unit *get_drawable_unit(const struct tileset *t,
			       struct tile *ptile,
			       const struct city *citymode)
{
  struct unit *punit = find_visible_unit(ptile);

  if (punit == NULL) {
    return NULL;
  }

  if (citymode && unit_owner(punit) == city_owner(citymode)) {
    return NULL;
  }

  if (!unit_is_in_focus(punit)
      || t->sprites.unit.select[0] || focus_unit_state == 0) {
    return punit;
  } else {
    return NULL;
  }
}

/****************************************************************************
  This patch unloads all sprites from the sprite hash (the hash itself
  is left intact).
****************************************************************************/
static void unload_all_sprites(struct tileset *t)
{
  if (t->sprite_hash) {
    sprite_hash_iterate(t->sprite_hash, tag_name, ss) {
      while (ss->ref_count > 0) {
        unload_sprite(t, tag_name);
      }
    } sprite_hash_iterate_end;
  }
}

/**********************************************************************
  Free all sprites from tileset.
***********************************************************************/
void tileset_free_tiles(struct tileset *t)
{
  int i;

  log_debug("tileset_free_tiles()");

  unload_all_sprites(t);

  free_city_sprite(t->sprites.city.tile);
  t->sprites.city.tile = NULL;

  for (i = 0; i < NUM_WALL_TYPES; i++) {
    free_city_sprite(t->sprites.city.wall[i]);
    t->sprites.city.wall[i] = NULL;
  }
  free_city_sprite(t->sprites.city.single_wall);
  t->sprites.city.single_wall = NULL;

  free_city_sprite(t->sprites.city.occupied);
  t->sprites.city.occupied = NULL;

  if (t->sprite_hash) {
    sprite_hash_destroy(t->sprite_hash);
    t->sprite_hash = NULL;
  }

  small_sprite_list_iterate(t->small_sprites, ss) {
    small_sprite_list_remove(t->small_sprites, ss);
    if (ss->file) {
      free(ss->file);
    }
    fc_assert(ss->sprite == NULL);
    free(ss);
  } small_sprite_list_iterate_end;

  specfile_list_iterate(t->specfiles, sf) {
    specfile_list_remove(t->specfiles, sf);
    free(sf->file_name);
    if (sf->big_sprite) {
      free_sprite(sf->big_sprite);
      sf->big_sprite = NULL;
    }
    free(sf);
  } specfile_list_iterate_end;

  sprite_vector_iterate(&t->sprites.city.worked_tile_overlay, psprite) {
    free_sprite(*psprite);
  } sprite_vector_iterate_end;
  sprite_vector_free(&t->sprites.city.worked_tile_overlay);

  sprite_vector_iterate(&t->sprites.city.unworked_tile_overlay, psprite) {
    free_sprite(*psprite);
  } sprite_vector_iterate_end;
  sprite_vector_free(&t->sprites.city.unworked_tile_overlay);

  if (t->sprites.tx.fullfog) {
    free(t->sprites.tx.fullfog);
    t->sprites.tx.fullfog = NULL;
  }

  sprite_vector_free(&t->sprites.colors.overlays);
  sprite_vector_free(&t->sprites.explode.unit);
  sprite_vector_free(&t->sprites.nation_flag);
  sprite_vector_free(&t->sprites.nation_shield);
  sprite_vector_free(&t->sprites.citybar.occupancy);

  tileset_background_free(t);
}

/**************************************************************************
  Return the sprite for drawing the given spaceship part.
**************************************************************************/
struct sprite *get_spaceship_sprite(const struct tileset *t,
				    enum spaceship_part part)
{
  return t->sprites.spaceship[part];
}

/**************************************************************************
  Return a sprite for the given citizen.  The citizen's type is given,
  as well as their index (in the range [0..city_size_get(pcity))).  The
  citizen's city can be used to determine which sprite to use (a NULL
  value indicates there is no city; i.e., the sprite is just being
  used as a picture).
**************************************************************************/
struct sprite *get_citizen_sprite(const struct tileset *t,
				  enum citizen_category type,
				  int citizen_index,
				  const struct city *pcity)
{
  const struct citizen_graphic *graphic;
  int gfx_index = citizen_index;

  if (pcity != NULL) {
    gfx_index += pcity->client.first_citizen_index;
  }

  if (type < CITIZEN_SPECIALIST) {
    fc_assert(type >= 0);
    graphic = &t->sprites.citizen[type];
  } else {
    fc_assert(type < (CITIZEN_SPECIALIST + SP_MAX));
    graphic = &t->sprites.specialist[type - CITIZEN_SPECIALIST];
  }

  if (graphic->count == 0) {
    return NULL;
  }

  return graphic->sprite[gfx_index % graphic->count];
}

/**************************************************************************
  Return the sprite for the nation.
**************************************************************************/
struct sprite *get_nation_flag_sprite(const struct tileset *t,
				      const struct nation_type *pnation)
{
  return t->sprites.nation_flag.p[nation_index(pnation)];
}

/**************************************************************************
  Return the shield sprite for the nation.
**************************************************************************/
struct sprite *get_nation_shield_sprite(const struct tileset *t,
                                        const struct nation_type *pnation)
{
  return t->sprites.nation_shield.p[nation_index(pnation)];
}

/**************************************************************************
  Return the sprite for the technology/advance.
**************************************************************************/
struct sprite *get_tech_sprite(const struct tileset *t, Tech_type_id tech)
{
  fc_assert_ret_val(0 <= tech && tech < advance_count(), NULL);
  return t->sprites.tech[tech];
}

/**************************************************************************
  Return the sprite for the building/improvement.
**************************************************************************/
struct sprite *get_building_sprite(const struct tileset *t,
                                   struct impr_type *pimprove)
{
  fc_assert_ret_val(NULL != pimprove, NULL);
  return t->sprites.building[improvement_index(pimprove)];
}

/****************************************************************************
  Return the sprite for the government.
****************************************************************************/
struct sprite *get_government_sprite(const struct tileset *t,
                                     const struct government *gov)
{
  fc_assert_ret_val(NULL != gov, NULL);
  return t->sprites.government[government_index(gov)];
}

/****************************************************************************
  Return the sprite for the unit type (the base "unit" sprite).
****************************************************************************/
struct sprite *get_unittype_sprite(const struct tileset *t,
                                   const struct unit_type *punittype,
                                   enum direction8 facing,
                                   bool icon)
{
  int uidx = utype_index(punittype);

  fc_assert_ret_val(NULL != punittype, NULL);

  if (!direction8_is_valid(facing) || !is_valid_dir(facing)) {
    /* Fallback to random orientation sprite.
     * The randomness was fixed at tileset load time for stability
     * (in tileset_setup_unit_type_from_tag()); here we need to adapt
     * that to the map topology (which is now known).
     * default_dir_seed was set to give uniform distribution for either
     * 6 or 8 valid directions.
     * Thus this direction is stable for a given unit type unless the
     * map topology changes. */
    unsigned int dir_index;

    fc_assert_ret_val(game.map.num_valid_dirs > 0, NULL);
    dir_index
      = t->sprites.units.default_dir_seed[uidx] % game.map.num_valid_dirs;
    facing = game.map.valid_dirs[dir_index];
    fc_assert_ret_val(is_valid_dir(facing), NULL);
  }

  if (t->sprites.units.icon[uidx]
      && (icon || t->sprites.units.facing[uidx][facing] == NULL)) {
    /* Has icon sprite */
    return t->sprites.units.icon[uidx];
  } else {
    return t->sprites.units.facing[uidx][facing];
  }
}

/**************************************************************************
  Return a "sample" sprite for this city style.
**************************************************************************/
struct sprite *get_sample_city_sprite(const struct tileset *t,
                                      int style_idx)
{
  int num_thresholds =
    t->sprites.city.tile->styles[style_idx].land_num_thresholds;

  if (num_thresholds == 0) {
    return NULL;
  } else {
    return (t->sprites.city.tile->styles[style_idx]
            .land_thresholds[num_thresholds - 1].sprite);
  }
}

/**************************************************************************
  Return a sprite with an "arrow" theme graphic.
**************************************************************************/
struct sprite *get_arrow_sprite(const struct tileset *t,
                                enum arrow_type arrow)
{
  fc_assert_ret_val(arrow >= 0 && arrow < ARROW_LAST, NULL);

  return t->sprites.arrow[arrow];
}

/**************************************************************************
  Return a tax sprite for the given output type (usually gold/lux/sci).
**************************************************************************/
struct sprite *get_tax_sprite(const struct tileset *t, Output_type_id otype)
{
  switch (otype) {
  case O_SCIENCE:
    return t->sprites.tax_science;
  case O_GOLD:
    return t->sprites.tax_gold;
  case O_LUXURY:
    return t->sprites.tax_luxury;
  case O_TRADE:
  case O_FOOD:
  case O_SHIELD:
  case O_LAST:
    break;
  }
  return NULL;
}

/**************************************************************************
  Return event icon sprite
**************************************************************************/
struct sprite *get_event_sprite(const struct tileset *t, enum event_type event)
{
  return t->sprites.events[event];
}

/**************************************************************************
  Return a thumbs-up/thumbs-down sprite to show treaty approval or
  disapproval.
**************************************************************************/
struct sprite *get_treaty_thumb_sprite(const struct tileset *t, bool on_off)
{
  return t->sprites.treaty_thumb[on_off ? 1 : 0];
}

/**************************************************************************
  Return a sprite_vector containing the animation sprites for a unit
  explosion.
**************************************************************************/
const struct sprite_vector *get_unit_explode_animation(const struct
						       tileset *t)
{
  return &t->sprites.explode.unit;
}

/****************************************************************************
  Return a sprite contining the single nuke graphic.

  TODO: This should be an animation like the unit explode animation.
****************************************************************************/
struct sprite *get_nuke_explode_sprite(const struct tileset *t)
{
  return t->sprites.explode.nuke;
}

/**************************************************************************
  Return all the sprites used for city bar drawing.
**************************************************************************/
const struct citybar_sprites *get_citybar_sprites(const struct tileset *t)
{
  if (gui_options.draw_full_citybar) {
    return &t->sprites.citybar;
  } else {
    return NULL;
  }
}

/**************************************************************************
  Return all the sprites used for editor icons, images, etc.
**************************************************************************/
const struct editor_sprites *get_editor_sprites(const struct tileset *t)
{
  return &t->sprites.editor;
}

/**************************************************************************
  Returns a sprite for the given cursor.  The "hot" coordinates (the
  active coordinates of the mouse relative to the sprite) are placed int
  (*hot_x, *hot_y). 
  A cursor can consist of several frames to be used for animation.
**************************************************************************/
struct sprite *get_cursor_sprite(const struct tileset *t,
				 enum cursor_type cursor,
				 int *hot_x, int *hot_y, int frame)
{
  *hot_x = t->sprites.cursor[cursor].hot_x;
  *hot_y = t->sprites.cursor[cursor].hot_y;
  return t->sprites.cursor[cursor].frame[frame];
}

/****************************************************************************
  Return a sprite for the given icon.  Icons are used by the operating
  system/window manager.  Usually freeciv has to tell the OS what icon to
  use.

  Note that this function will return NULL before the sprites are loaded.
  The GUI code must be sure to call tileset_load_tiles before setting the
  top-level icon.
****************************************************************************/
struct sprite *get_icon_sprite(const struct tileset *t, enum icon_type icon)
{
  return t->sprites.icon[icon];
}

/****************************************************************************
  Returns a sprite with the "user-attention" crosshair graphic.

  FIXME: This function shouldn't be needed if the attention graphics are
  drawn natively by the tileset code.
****************************************************************************/
struct sprite *get_attention_crosshair_sprite(const struct tileset *t)
{
  return t->sprites.user.attention;
}

/****************************************************************************
  Returns a sprite for the given indicator with the given index.  The
  index should be in [0, NUM_TILES_PROGRESS).
****************************************************************************/
struct sprite *get_indicator_sprite(const struct tileset *t,
                                    enum indicator_type indicator,
                                    int idx)
{
  idx = CLIP(0, idx, NUM_TILES_PROGRESS - 1);

  fc_assert_ret_val(indicator >= 0 && indicator < INDICATOR_COUNT, NULL);

  return t->sprites.indicator[indicator][idx];
}

/****************************************************************************
  Return a sprite for the unhappiness of the unit - to be shown as an
  overlay on the unit in the city support dialog, for instance.

  May return NULL if there's no unhappiness.
****************************************************************************/
struct sprite *get_unit_unhappy_sprite(const struct tileset *t,
                                       const struct unit *punit,
                                       int happy_cost)
{
  const int unhappy = CLIP(0, happy_cost, MAX_NUM_UPKEEP_SPRITES+1);

  if (unhappy > 0) {
    return t->sprites.upkeep.unhappy[unhappy - 1];
  } else {
    return NULL;
  }
}

/****************************************************************************
  Return a sprite for the upkeep of the unit - to be shown as an overlay
  on the unit in the city support dialog, for instance.

  May return NULL if there's no upkeep of the kind.
****************************************************************************/
struct sprite *get_unit_upkeep_sprite(const struct tileset *t,
                                      Output_type_id otype,
                                      const struct unit *punit,
                                      const int *upkeep_cost)
{
  const int upkeep = CLIP(0, upkeep_cost[otype], MAX_NUM_UPKEEP_SPRITES + 1);

  if (upkeep > 0) {
    return t->sprites.upkeep.output[otype][upkeep - 1];
  } else {
    return NULL;
  }
}

/****************************************************************************
  Return a rectangular sprite containing a fog "color".  This can be used
  for drawing fog onto arbitrary areas (like the overview).
****************************************************************************/
struct sprite *get_basic_fog_sprite(const struct tileset *t)
{
  return t->sprites.tx.fog;
}

/****************************************************************************
  Return the tileset's color system.
****************************************************************************/
struct color_system *get_color_system(const struct tileset *t)
{
  return t->color_system;
}

/****************************************************************************
  Loads preferred theme if there's any.
****************************************************************************/
void tileset_use_preferred_theme(const struct tileset *t)
{
  char *default_theme_name = NULL;
  size_t default_theme_name_sz = 0;
  int i;

  switch (get_gui_type()) {
  case GUI_GTK2:
    default_theme_name = gui_options.gui_gtk2_default_theme_name;
    default_theme_name_sz = sizeof(gui_options.gui_gtk2_default_theme_name);
    break;
  case GUI_GTK3:
    default_theme_name = gui_options.gui_gtk3_default_theme_name;
    default_theme_name_sz = sizeof(gui_options.gui_gtk3_default_theme_name);
    break;
  case GUI_GTK3_22:
    default_theme_name = gui_options.gui_gtk3_22_default_theme_name;
    default_theme_name_sz = sizeof(gui_options.gui_gtk3_22_default_theme_name);
    break;
  case GUI_SDL:
    default_theme_name = gui_options.gui_sdl_default_theme_name;
    default_theme_name_sz = sizeof(gui_options.gui_sdl_default_theme_name);
    break;
  case GUI_SDL2:
    default_theme_name = gui_options.gui_sdl2_default_theme_name;
    default_theme_name_sz = sizeof(gui_options.gui_sdl2_default_theme_name);
    break;
  case GUI_STUB:
  case GUI_XAW:
  case GUI_QT:
  case GUI_WEB:
    break;
  }

  if (NULL == default_theme_name || 0 == default_theme_name_sz) {
    /* Theme is not supported by this client. */
    return;
  }

  for (i = 0; i < t->num_preferred_themes; i++) {
    if (strcmp(t->preferred_themes[i], default_theme_name)) {
      if (popup_theme_suggestion_dialog(t->preferred_themes[i])) {
        log_debug("trying theme \"%s\".", t->preferred_themes[i]);
        if (load_theme(t->preferred_themes[i])) {
          (void) fc_strlcpy(default_theme_name, t->preferred_themes[i],
                            default_theme_name_sz);
          return;
        }
      }
    }
  }
  log_verbose("The tileset doesn't specify preferred themes or none of its "
              "preferred themes can be used. Using system default.");
  gui_clear_theme();
}

/****************************************************************************
  Initialize tileset structure
****************************************************************************/
void tileset_init(struct tileset *t)
{
  int wi;

  /* We currently have no city sprites loaded. */
  t->sprites.city.tile     = NULL;

  for (wi = 0; wi < NUM_WALL_TYPES; wi++) {
    t->sprites.city.wall[wi] = NULL;
  }
  t->sprites.city.single_wall = NULL;

  t->sprites.city.occupied = NULL;

  t->sprites.background.color = NULL;
  t->sprites.background.graphic = NULL;

  player_slots_iterate(pslot) {
    int edge, j, id = player_slot_index(pslot);

    for (edge = 0; edge < EDGE_COUNT; edge++) {
      for (j = 0; j < 2; j++) {
        t->sprites.player[id].grid_borders[edge][j] = NULL;
      }
    }

    t->sprites.player[id].color = NULL;
    t->sprites.player[id].background = NULL;
  } player_slots_iterate_end;

  t->max_upkeep_height = 0;
}

/****************************************************************************
  Fill the sprite array with sprites that together make a representative
  image of the given terrain type. Suitable for use as an icon and in list
  views.

  NB: The 'layer' argument is NOT a LAYER_* value, but rather one of 0, 1, 2.
  Using other values for 'layer' here will result in undefined behaviour. ;)
****************************************************************************/
int fill_basic_terrain_layer_sprite_array(struct tileset *t,
                                          struct drawn_sprite *sprs,
                                          int layer,
                                          struct terrain *pterrain)
{
  struct drawn_sprite *save_sprs = sprs;
  struct drawing_data *draw = t->sprites.drawing[terrain_index(pterrain)];

  struct terrain *tterrain_near[8];
  bv_special tspecial_near[8];

  struct tile dummy_tile; /* :( */

  int i;


  memset(&dummy_tile, 0, sizeof(struct tile));
  
  for (i = 0; i < 8; i++) {
    tterrain_near[i] = pterrain;
    BV_CLR_ALL(tspecial_near[i]);
  }

  i = draw->is_reversed ? draw->num_layers - layer - 1 : layer;
  sprs += fill_terrain_sprite_array(t, sprs, i, &dummy_tile,
                                    pterrain, tterrain_near, draw);

  return sprs - save_sprs;
}

/****************************************************************************
  Return the sprite for the given resource type.
****************************************************************************/
struct sprite *get_resource_sprite(const struct tileset *t,
                                   const struct resource *presource)
{
  if (presource == NULL) {
    return NULL;
  }

  return t->sprites.resource[resource_index(presource)];
}

/****************************************************************************
  Return a representative sprite for the given extra type.
****************************************************************************/
int fill_basic_extra_sprite_array(const struct tileset *t,
                                  struct drawn_sprite *sprs,
                                  const struct extra_type *pextra)
{
  int idx = extra_index(pextra);
  struct drawn_sprite *saved_sprs = sprs;

  switch (t->sprites.extras[idx].extrastyle) {
  case ESTYLE_SINGLE1:
  case ESTYLE_SINGLE2:
    ADD_SPRITE_SIMPLE(t->sprites.extras[idx].u.single);
    break;
  case ESTYLE_CARDINALS:
    ADD_SPRITE_SIMPLE(t->sprites.extras[idx].u.cardinals[0]);
    break;
  case ESTYLE_ROAD_ALL_SEPARATE:
  case ESTYLE_ROAD_PARITY_COMBINED:
  case ESTYLE_ROAD_ALL_COMBINED:
  case ESTYLE_RIVER:
    return fill_basic_road_sprite_array(t, sprs, pextra);
  case ESTYLE_3LAYER:
    return fill_basic_base_sprite_array(t, sprs, pextra);
  case ESTYLE_COUNT:
    fc_assert(t->sprites.extras[idx].extrastyle != ESTYLE_COUNT);
    break;
  }

  return sprs - saved_sprs;
}

/****************************************************************************
  Fills the sprite array with sprites that together make a representative
  image of the given road type. The image is suitable for use as an icon
  for the road type, for example.
****************************************************************************/
int fill_basic_road_sprite_array(const struct tileset *t,
                                 struct drawn_sprite *sprs,
                                 const struct extra_type *pextra)
{
  struct drawn_sprite *saved_sprs = sprs;
  int idx;
  int i;
  enum extrastyle_id extrastyle;

  if (!t || !sprs || !pextra) {
    return 0;
  }

  idx = extra_index(pextra);

  if (!(0 <= idx && idx < game.control.num_extra_types)) {
    return 0;
  }

  extrastyle = t->sprites.extras[idx].extrastyle;

  if (extrastyle == ESTYLE_RIVER) {
    ADD_SPRITE_SIMPLE(t->sprites.extras[idx].u.road.ru.rivers.spec[0]);
  } else {
    for (i = 0; i < t->num_valid_tileset_dirs; i++) {
      if (!t->valid_tileset_dirs[i]) {
        continue;
      }
      if (extrastyle == ESTYLE_ROAD_ALL_SEPARATE) {
        ADD_SPRITE_SIMPLE(t->sprites.extras[idx].u.road.ru.dir[i]);
      } else if (extrastyle == ESTYLE_ROAD_PARITY_COMBINED) {
        if ((i % 2) == 0) {
          ADD_SPRITE_SIMPLE(t->sprites.extras[idx].u.road.ru.combo.even[1 << (i / 2)]);
        }
      } else if (extrastyle == ESTYLE_ROAD_ALL_COMBINED) {
        ADD_SPRITE_SIMPLE(t->sprites.extras[idx].u.road.ru.total[1 << i]);
      }
    }
  }

  return sprs - saved_sprs;
}

/****************************************************************************
  Fills the sprite array with sprites that together make a representative
  image of the given base type. The image is suitable for use as an icon
  for the base type, for example.
****************************************************************************/
int fill_basic_base_sprite_array(const struct tileset *t,
                                 struct drawn_sprite *sprs,
                                 const struct extra_type *pextra)
{
  struct drawn_sprite *saved_sprs = sprs;
  int idx;

  if (!t || !sprs || !pextra) {
    return 0;
  }

  idx = extra_index(pextra);

  if (!(0 <= idx && idx < game.control.num_extra_types)) {
    return 0;
  }

#define ADD_SPRITE_IF_NOT_NULL(x) do {\
  if ((x) != NULL) {\
    ADD_SPRITE_FULL(x);\
  }\
} while (0)

  /* Corresponds to LAYER_SPECIAL{1,2,3} order. */
  ADD_SPRITE_IF_NOT_NULL(t->sprites.extras[idx].u.bmf.background);
  ADD_SPRITE_IF_NOT_NULL(t->sprites.extras[idx].u.bmf.middleground);
  ADD_SPRITE_IF_NOT_NULL(t->sprites.extras[idx].u.bmf.foreground);

#undef ADD_SPRITE_IF_NOT_NULL

  return sprs - saved_sprs;
}

/****************************************************************************
  Setup tiles for one player using the player color.
****************************************************************************/
void tileset_player_init(struct tileset *t, struct player *pplayer)
{
  int plrid, i, j;
  struct sprite *color;

  fc_assert_ret(pplayer != NULL);

  plrid = player_index(pplayer);
  fc_assert_ret(plrid >= 0);
  fc_assert_ret(plrid < ARRAY_SIZE(t->sprites.player));

  /* Free all data before recreating it. */
  tileset_player_free(t, plrid);

  if (player_has_color(t, pplayer)) {
    t->sprites.player[plrid].color = color
      = create_plr_sprite(get_player_color(t, pplayer));
  } else {
    /* XXX: if player hasn't been assigned a color, perhaps there's no
     * point proceeding with an arbitrary color; this should only happen
     * in pregame. Probably blank sprites would be better. */

    fc_assert_ret(t->sprites.background.color != NULL);

    color = t->sprites.background.color;
  }

  t->sprites.player[plrid].background
    = crop_sprite(color, 0, 0,
                  t->normal_tile_width, t->normal_tile_height,
                  t->sprites.mask.tile, 0, 0, t->scale, FALSE);

  for (i = 0; i < EDGE_COUNT; i++) {
    for (j = 0; j < 2; j++) {
      struct sprite *s;

      if (color && t->sprites.grid.borders[i][j]) {
        s = crop_sprite(color, 0, 0,
                        t->normal_tile_width, t->normal_tile_height,
                        t->sprites.grid.borders[i][j], 0, 0, 1.0f, FALSE);
      } else {
        s = t->sprites.grid.borders[i][j];
      }
      t->sprites.player[plrid].grid_borders[i][j] = s;
    }
  }
}

/****************************************************************************
  Free tiles for one player using the player color.
****************************************************************************/
static void tileset_player_free(struct tileset *t, int plrid)
{
  int i, j;

  fc_assert_ret(plrid >= 0);
  fc_assert_ret(plrid < ARRAY_SIZE(t->sprites.player));

  if (t->sprites.player[plrid].color) {
    free_sprite(t->sprites.player[plrid].color);
    t->sprites.player[plrid].color = NULL;
  }
  if (t->sprites.player[plrid].background) {
    free_sprite(t->sprites.player[plrid].background);
    t->sprites.player[plrid].background = NULL;
  }

  for (i = 0; i < EDGE_COUNT; i++) {
    for (j = 0; j < 2; j++) {
      if (t->sprites.player[plrid].grid_borders[i][j]) {
        free_sprite(t->sprites.player[plrid].grid_borders[i][j]);
        t->sprites.player[plrid].grid_borders[i][j] = NULL;
      }
    }
  }
}

/****************************************************************************
  Setup tiles for the background.
****************************************************************************/
void tileset_background_init(struct tileset *t)
{
  /* Free all data before recreating it. */
  tileset_background_free(t);

  /* generate background color */
  t->sprites.background.color
    = create_plr_sprite(ensure_color(game.plr_bg_color));

  /* Chop up and build the background graphics. */
  t->sprites.background.graphic
    = crop_sprite(t->sprites.background.color, 0, 0,
                  t->normal_tile_width, t->normal_tile_height,
                  t->sprites.mask.tile, 0, 0, t->scale, FALSE);
}

/****************************************************************************
  Free tiles for the background.
****************************************************************************/
void tileset_background_free(struct tileset *t)
{
  if (t->sprites.background.color) {
    free_sprite(t->sprites.background.color);
    t->sprites.background.color = NULL;
  }

  if (t->sprites.background.graphic) {
    free_sprite(t->sprites.background.graphic);
    t->sprites.background.graphic = NULL;
  }
}

/****************************************************************************
  Reset tileset data specific to ruleset.
****************************************************************************/
void tileset_ruleset_reset(struct tileset *t)
{
  int i;

  for (i = 0; i < ESTYLE_COUNT; i++) {
    if (t->style_lists[i] != NULL) {
      extra_type_list_destroy(t->style_lists[i]);
      t->style_lists[i] = extra_type_list_new();
    }
  }

  if (t->flagged_bases_list != NULL) {
    extra_type_list_destroy(t->flagged_bases_list);
    t->flagged_bases_list = extra_type_list_new();
  }
}

/****************************************************************************
  Is tileset in sane state?
****************************************************************************/
bool tileset_is_fully_loaded(void)
{
  return !tileset_update;
}

/****************************************************************************
  Return tileset name
****************************************************************************/
const char *tileset_name_get(struct tileset *t)
{
  return t->given_name;
}

/****************************************************************************
  Return tileset version
****************************************************************************/
const char *tileset_version(struct tileset *t)
{
  return t->version;
}

/****************************************************************************
  Return tileset description summary
****************************************************************************/
const char *tileset_summary(struct tileset *t)
{
  return t->summary;
}

/****************************************************************************
  Return tileset description body
****************************************************************************/
const char *tileset_description(struct tileset *t)
{
  return t->description;
}

/****************************************************************************
  Return tileset topology index
****************************************************************************/
int tileset_topo_index(struct tileset *t)
{
  return t->ts_topo_idx;
}