File: eval.c

package info (click to toggle)
gnushogi 1.5~git20140725-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,900 kB
  • sloc: ansic: 12,731; sh: 4,104; makefile: 128
file content (2872 lines) | stat: -rw-r--r-- 68,459 bytes parent folder | download
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
/*
 * FILE: eval.c
 *
 * ----------------------------------------------------------------------
 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
 *
 * GNU SHOGI is based on GNU CHESS
 *
 * Copyright (c) 1988, 1989, 1990 John Stanback
 * Copyright (c) 1992 Free Software Foundation
 *
 * This file is part of GNU SHOGI.
 *
 * GNU Shogi 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 3 of the License,
 * or (at your option) any later version.
 *
 * GNU Shogi 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with GNU Shogi; see the file COPYING. If not, see
 * <http://www.gnu.org/licenses/>.
 * ----------------------------------------------------------------------
 *
 */

#include "gnushogi.h"
#include "pattern.h"

/* Hash table for preventing multiple scoring of the same position */

int EADD = 0;       /* number of writes to the cache table */
int EGET = 0;       /* number of hits to the cache table */
int PUTVAR = false; /* shall the current scoring be cached? */


/* Pieces and colors of initial board setup */

#ifndef MINISHOGI
const small_short Stboard[NO_SQUARES] =
{
    lance, knight, silver,   gold,   king,   gold, silver, knight,  lance,
    0, bishop,      0,      0,      0,      0,      0,   rook,      0,
    pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,
    0,      0,      0,      0,      0,      0,      0,      0,      0,
    0,      0,      0,      0,      0,      0,      0,      0,      0,
    0,      0,      0,      0,      0,      0,      0,      0,      0,
    pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,   pawn,
    0,   rook,      0,      0,      0,      0,      0, bishop,      0,
    lance, knight, silver,   gold,   king,   gold, silver, knight,  lance
};


const small_short Stcolor[NO_SQUARES] =
{
    black,   black,   black,   black,
    black,   black,   black,   black,   black,
    neutral, black, neutral, neutral,
    neutral, neutral, neutral,   black, neutral,
    black,   black,   black,   black,
    black,   black,   black,   black,   black,
    neutral, neutral, neutral, neutral,
    neutral, neutral, neutral, neutral, neutral,
    neutral, neutral, neutral, neutral,
    neutral, neutral, neutral, neutral, neutral,
    neutral, neutral, neutral, neutral,
    neutral, neutral, neutral, neutral, neutral,
    white,   white,   white,   white,
    white,   white,   white, white, white,
    neutral, white, neutral, neutral,
    neutral, neutral, neutral, white, neutral,
    white,   white,   white,   white,
    white,   white,   white, white, white
};
#else
const small_short Stboard[NO_SQUARES] =
{
    king,   gold,   silver, bishop, rook,
    pawn,   0,      0,      0,      0,
    0,      0,      0,      0,      0,
    0,      0,      0,      0,      pawn,
    rook,   bishop, silver, gold,   king,
};


const small_short Stcolor[NO_SQUARES] =
{
    black,   black,   black,   black,   black,
    black,   neutral, neutral, neutral, neutral,
    neutral, neutral, neutral, neutral, neutral,
    neutral, neutral, neutral, neutral, white,
    white,   white,   white,   white,   white
};
#endif

/* Actual pieces and colors */

small_short board[NO_SQUARES], color[NO_SQUARES];


/* relative piece values at the beginning of main stages */

#define MAIN_STAGES 4

static small_short ispvalue[NO_PIECES][MAIN_STAGES] =
{
    {   0,  35,  70,  99 }, /* main stage borders */
    /* ------------------------------------------ */
    {   7,   7,   8,  10 }, /* Pawn               */
#ifndef MINISHOGI
    {  20,  35,  45,  60 }, /* Lance              */
    {  20,  35,  45,  60 }, /* Knight             */
#endif
    {  35,  40,  60,  80 }, /* Silver             */
    {  35,  50,  65,  80 }, /* Gold               */
    {  90,  90,  90,  90 }, /* Bishop             */
    {  95,  95,  95,  95 }, /* Rook               */
    {  15,  25,  40,  65 }, /* promoted Pawn      */
#ifndef MINISHOGI
    {  25,  45,  55,  65 }, /* promoted Lance     */
    {  25,  45,  55,  65 }, /* promoted Knight    */
#endif
    {  35,  55,  75,  75 }, /* promoted Silver    */
    {  99,  99,  99,  99 }, /* promoted Bishop    */
    {  97,  97,  99,  99 }, /* promoted Rook      */
    { 100, 100, 100, 100 }, /* King               */
};

/* Features and Weights */

#define ATTACKED    0
#define HUNGP       1
#define HUNGX       2
#define CNTRL5TH    3
#define HOLES       4
#define PCASTLE     5
#define PATTACK     6
#define CTRLK       7
#define PROTECT     8
#define HCLSD       9
#define PINVAL     10
#define XRAY       11
#define OPENWRONG  12
#define SEED       13
#define LOOSE      14
#define MOBILITY   15
#define TARGET     16
#define KSFTY      17
#define HOPN       18
#define PROMD      19
#define KINGOD     20
#define PWNDROP    21
#define DFFDROP    22
#define FCLATTACK  23
#define KNGATTACK  24
#define KNGPROTECT 25
#define DNGLPC     26
#define LSATTACK   27
#define NIHATTACK  28
#define COHESION   29
#define OPPDROP    30


static small_short weight[NO_FEATURES + 1][MAIN_STAGES + 2] =
{
    {  80, 100, 100,  40,  10,  15 },    /* ATTACKED      */
    {  80, 100, 100,  50,  14,  10 },    /* HUNGP         */
    {  80, 100, 100,  50,  18,  12 },    /* HUNGX         */
    { 100,  50,   0,   0,   2,   1 },    /* CNTRL5TH      */
    { 100, 100,  60,  10,   4,   2 },    /* HOLES         */
    { 100,  50,   0,   0,  14,   7 },    /* PCASTLE       */
    { 100,  50,   0,   0,   6,  12 },    /* PATTACK       */
    {  10,  40,  70, 100,  10,  15 },    /* CTRLK         */
    { 100,  80,  50,  40,   2,   1 },    /* PROTECT       */
    {  40, 100,  40,   5,   4,   4 },    /* HCLSD         */
    {  80, 100,  80,  30,  10,  15 },    /* PINVAL        */
    {  80, 100,  60,  15,   6,  10 },    /* XRAY          */
    { 100,  50,   0,   0,  15,  15 },    /* OPENWRONG     */
    {   0,  40,  70, 100,   8,  12 },    /* SEED          */
    {  50, 100,  80,  20,   5,   3 },    /* LOOSE         */
    {  50, 100,  80,  50, 100, 100 },    /* MOBILITY (%)  */
    {  50, 100,  80,  50,   4,   8 },    /* TARGET        */
    {  50,  40, 100,  80,   8,   4 },    /* KSFTY         */
    {  80, 100,  60,  20,   5,   5 },    /* HOPN          */
    {  20,  40,  80, 100,   3,   6 },    /* PROMD         */
    {  20,  40,  80, 100,   4,   1 },    /* KINGOD        */
    {   5,  40, 100,  50,   0,   4 },    /* PWNDROP       */
    {   0,  20,  80, 100,   0,   4 },    /* DFFDROP       */
    {  20,  50, 100,  80,   0,   4 },    /* FCLATTACK     */
    {   0,  20,  80, 100,   0,   8 },    /* KNGATTACK     */
    {  40,  80, 100,  80,   6,   0 },    /* KNGPROTECT    */
    {  50, 100,  60,  10,   0,   8 },    /* DNGPC         */
    {  30, 100,  60,   5,   0,   6 },    /* LSATTACK      */
    {   0,  50,  80, 100,   0,   8 },    /* NIHATTACK     */
    {  50, 100,  80,  60,   8,   0 },    /* COHESION      */
    { 100, 100,  80,  60,   4,   4 },    /* OPPDROP       */
};


short ADVNCM[NO_PIECES];

/* distance to enemy king */
static const short EnemyKingDistanceBonus[10] =
{ 0, 6, 4, -1, -3, -4, -6, -8, -10, -12 };

/* distance to own king */
static const short OwnKingDistanceBonus[10] =
{ 0, 5, 2, 1, 0, -1, -2, -3, -4, -5 };

/* distance to promotion zone */
#ifndef MINISHOGI
static const int PromotionZoneDistanceBonus[NO_ROWS] =
{ 0, 0, 0, 0, 2, 6, 6, 8, 8 };
#else
static const int PromotionZoneDistanceBonus[NO_ROWS] =
{ 0, 0, 2, 6, 8 };				  /* FIXME ? */
#endif

#define MAX_BMBLTY 20
#define MAX_RMBLTY 20
#define MAX_LMBLTY 8

/* Bishop mobility bonus indexed by # reachable squares */
static const short BMBLTY[MAX_BMBLTY] =
{ 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 16, 16, 16, 16 };

/* Rook mobility bonus indexed by # reachable squares */
static const short RMBLTY[MAX_RMBLTY] =
{ 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 16, 16, 16, 16 };

#ifndef MINISHOGI
/* Lance mobility bonus indexed by # reachable squares */
static const short LMBLTY[MAX_LMBLTY] =
{ 0, 0, 0, 0, 4, 6, 8, 10 };
#endif

static const short MBLTY[NO_PIECES] =
{ 0, 2,
#ifndef MINISHOGI
  1, 10,
#endif
  5, 5, 1, 1, 5,
#ifndef MINISHOGI
  5, 5,
#endif
  5, 1, 1, 4 };

static const short KTHRT[36] =
{   0,  -8, -20, -36, -52, -68, -80, -80, -80, -80, -80, -80,
    -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80,
    -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80 };

static small_short fvalue[2][NO_FEATURES];

long attack[2][NO_SQUARES];         /* threats to squares */
small_short sseed[NO_SQUARES];      /* square occupied by a seed piece? */

struct signature threats_signature[2] = /* statistics valid for position.. */
{ { -1, -1 }, { -1, -1 } };             /* attack and sseed available */

small_short starget[2][NO_SQUARES]; /* significance as a target for a
                                     * side of a square */
small_short sloose[NO_SQUARES];     /* square occupied by a loose piece? */
small_short shole[NO_SQUARES];      /* empty square a hole? */
small_short shung[NO_SQUARES];      /* hung piece? */

struct signature squares_signature =  /* statistics valid for position ... */
{ 0, 0 };                /* starget, sloose, shole, shung available */

short target[2], seed[2], loose[2], hole[2];

short captured[2];  /* number of captured pieces */
short dcaptured[2]; /* different types of captured pieces */

small_short Kdist[2][NO_SQUARES];    /* distance to king */

short MAXADIST, MAXCDIST; /* maximum half move distance to pattern */

char GameType[2] = { UNKNOWN, UNKNOWN }; /* chosen game type of each side */


static short attack_opening_sequence[2];    /* current castle patterns */
static short castle_opening_sequence[2];    /* current attack formations */

static small_short Mpawn  [2][NO_SQUARES];
static small_short Msilver[2][NO_SQUARES];
static small_short Mgold  [2][NO_SQUARES];
static small_short Mking  [2][NO_SQUARES];
#ifndef MINISHOGI
static small_short Mlance [2][NO_SQUARES];
static small_short Mknight[2][NO_SQUARES];
#endif
static small_short Mbishop[2][NO_SQUARES];
static small_short Mrook  [2][NO_SQUARES];

static Mpiece_array Mpawn,
#ifndef MINISHOGI
    Mlance, Mknight,
#endif
    Msilver, Mgold, Mbishop, Mrook, Mking;

Mpiece_array *Mpiece[NO_PIECES] =
{ NULL, &Mpawn,
#ifndef MINISHOGI
  &Mlance, &Mknight,
#endif
  &Msilver, &Mgold, &Mbishop, &Mrook,
  &Mgold,
#ifndef MINISHOGI
  &Mgold, &Mgold,
#endif
  &Mgold, &Mbishop, &Mrook, &Mking };


static short c1, c2;
static small_short *PC1, *PC2;
static small_short *fv1;
static long *atk1, *atk2;
static long  a1, a2;

#define csquare(side, sq) ((side == black) ? sq : (NO_SQUARES - 1 - sq))
#define crow(side, sq)    row(csquare(side, sq))
#define ccolumn(side, sq) column(csquare(side, sq))


inline static short
on_csquare(short side, short piece, short square)
{
    short sq;
    /* FIXME: un-obfuscate this! */
    return ((board[sq = csquare(side, square)] == piece)
            && (color[sq] == side));
}


#ifndef MINISHOGI
inline static short
on_column(short side, short piece, short c)
{
    short sq;

    for (sq = c; sq < NO_SQUARES; sq += NO_COLS)
    {
        if (on_csquare(side, piece, sq))
            return true;
    }

    return false;
}


#define empty_csquare(side, square) \
  (board[csquare(side, square)] == no_piece)

inline static short
on_left_side(short side, short piece)
{
    short c;

    for (c = 0; c < 4; c++)
    {
        if (on_column(side, piece, c))
            return true;
    }

    return false;
}


inline static short
on_right_side(short side, short piece)
{
    short c;

    for (c = 5; c < NO_COLS; c++)
    {
        if (on_column(side, piece, c))
            return true;
    }

    return false;
}
#endif


short pscore[2];  /* piece score for each side */


/*
 * Fill array attack[side][] with info about attacks to a square.  Bits
 * 16-31 are set if the piece (king .. pawn) attacks the square.  Bits 0-15
 * contain a count of total attacks to the square.  Fill array sseed[] with
 * info about occupation by a seed piece.
 */

void
threats(short side)
{
    short  u, sq;
    long   c;
    long  *a;
#ifdef SAVE_NEXTPOS
    short d;
#else
    unsigned char  *ppos, *pdir;
#endif
    short i, kd, piece, xside;
    small_short *PL;

    if (MatchSignature(threats_signature[side]))
    {
        /* data valid for current positional signature */
        return;
    }

    a = attack[side];
    xside = side ^ 1;

    array_zero(a, NO_SQUARES * sizeof(a[0]));

    PL = PieceList[side];

    for (i = PieceCnt[side]; i >= 0; i--)
    {
        short ptyp;

        sq = PL[i];
        piece = board[sq];
        ptyp = ptype[side][piece];
        c = control[piece];
#ifdef SAVE_NEXTPOS
        u = first_direction(ptyp, &d, sq);
#else
        ppos = (*nextpos[ptyp])[sq];
        pdir = (*nextdir[ptyp])[sq];
        u = ppos[sq];
#endif

        do
        {
            a[u] = ((a[u] + 1) | c);

            if ((kd = Kdist[xside][u]) < 2)
            {
                sseed[sq] += 2 - kd;
                seed[side]++;
            }

#ifdef SAVE_NEXTPOS
            u = ((color[u] == neutral)
                 ? next_position(ptyp, &d, sq, u)
                 : next_direction(ptyp, &d, sq));
#else
            u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
#endif
        }
        while (u != sq);
    }

    /* data valid for current positional signature */
    CopySignature(threats_signature[side]);
}



/*
 * Compute the board square with nunmap offset "id".  If side == white,
 * offset is negated.  inunmap[sq] is the corresponding nunmap index isq.
 * nunmap[isq + id] computes the board square. If negative, it is outside
 * the board.
 */

static void
add_target(short sq, short side, short id)
{
    short isq, tsq, xside;
    isq = inunmap[sq];
    tsq = (side == black) ? nunmap[isq + id] : nunmap[isq - id];

    if (tsq >= 0)
    {
        target[xside = side^1]++;

        if (attack[side][tsq])
            starget[xside][tsq]++;  /* protected target square */
        else
            starget[xside][tsq] += 2; /* unprotected target square */
    }
}



/*
 * Target squares can be vertically ahead, diagonally ahead
 * or diagonally behind.
 */

static void
CheckTargetPiece(short sq, short side)
{
    switch (board[sq])
    {
    case pawn: /* vertically ahead if unprotected */
        if (!attack[side][sq])
            add_target(sq, side, 11);
        break;

    case king: /* diagonally and vertically ahead */
        add_target(sq, side, 10);
        add_target(sq, side, 11);
        add_target(sq, side, 12);
        break;

    case rook: /* diagonally ahead and behind */
        add_target(sq, side, 10);
        add_target(sq, side, 12);
        add_target(sq, side, -10);
        add_target(sq, side, -12);
        break;

    case bishop: /* vertically ahead */
        add_target(sq, side, 11);
        break;

#ifndef MINISHOGI
    case knight: /* vertically ahead if advanced */
        /* FIXME: gotta love them magic numbers... */
        if ((sq != 1) && (sq != 7) && (sq != 73) && (sq != 79))
            add_target(sq, side, 11);
        break;
#endif
    }
}



static short
ScoreKingOpeningFeatures(void)
{
    short s = 0, sq = OwnKing, ds;

    if (GameType[c1] == STATIC_ROOK)
    {
        /* Penalty for king on right side or fifth file */
        short c;
        c = 4 - ccolumn(c1, sq);

        if ((c < 0) || ((c == 0) && (sq != kingP[c1])))
            s += (ds = -c - c - fv1[OPENWRONG]);
    }
    else if (GameType[c1] == RANGING_ROOK)
    {
        /* Penalty for king on left side or fifth file */
        short c;
        c = 4 - ccolumn(c1, sq);

        if ((c > 0) || ((c == 0) && (sq != kingP[c1])))
        {
            s += (ds = -c - c - fv1[OPENWRONG]);
        }

        /* Penalty for king moved before rook switch */
        if (sq != kingP[c1])
        {
            if (on_csquare(c1, rook, 16))
            {
                s += (ds = -4 * fv1[OPENWRONG]);
            }
        }
        else
        {
            /* Penalty for sitting king after rook switch */
            if (!on_csquare(c1, rook, 16))
            {
                s += (ds = -2 * fv1[OPENWRONG]);
            }
        }

        /* Penalty for defending general moved
         * before king switch to right side */
        if (ccolumn(c1, sq) < 6)
        {
            if (Mvboard[csquare(c1, 5)] || Mvboard[csquare(c1, 6)])
            {
                s += (ds = -2 * fv1[OPENWRONG]);
            }
        }
    }

    return s;
}




inline static void
ExamineSquares(void)
{
    short sq, side, piece, n;

    if (MatchSignature(squares_signature))
    {
        /* data valid for current positional signature */
        return;
    }

    array_zero(shole,   sizeof(shole));
    array_zero(sloose,  sizeof(sloose));
    array_zero(starget, sizeof(starget));

    hole[0] = hole[1] = loose[0] = loose[1]
        = target[0] = target[1] = 0;

    for (sq = 0; sq < NO_SQUARES; sq++)
    {
        if ((side = color[sq]) == neutral)
        {
            if (InWhiteCamp(sq))
            {
                if (!attack[white][sq])
                {
                    shole[sq] = 1;
                    hole[white]++;
                }
            }
            else if (InBlackCamp(sq))
            {
                if (!attack[black][sq])
                {
                    shole[sq] = 1;
                    hole[black]++;
                }
            }
        }
        else
        {
            /* occupied by "side" piece */
            if (!attack[side][sq])
            {
                sloose[sq] = 1;
                loose[side]++;
            }

            CheckTargetPiece(sq, side);
        }
    }

    for (side = black; side <= white; side++)
    {
        captured[side] = dcaptured[side] = 0;

        for (piece = pawn; piece <= rook; piece++)
        {
            if ((n = Captured[side][piece]) != 0)
            {
                if (piece != pawn)
                    captured[side] += n;

                dcaptured[side]++;
            }
        }
    }

    /* Data valid for current positional signature */
    CopySignature(squares_signature);
}



/* ............    POSITIONAL EVALUATION ROUTINES    ............ */

/*
 * Inputs are:
 * mtl[side]       - value of all material
 * hung[side]      - count of hung pieces
 * Tscore[ply]     - search tree score for ply ply
 * Pscore[ply]     - positional score for ply ply
 * INCscore        - bonus score or penalty for certain moves
 * Sdepth          - search goal depth
 * xwndw           - evaluation window about alpha/beta
 * EWNDW           - second evaluation window about alpha/beta
 * ChkFlag[ply]    - checking piece at level ply or 0 if no check
 * TesujiFlag[ply] - 1 if tesuji move at level ply or 0 if no tesuji
 * PC1[column]     - # of my pawns in this column
 * PC2[column]     - # of opponents pawns in column
 * PieceCnt[side]  - just what it says
 */




/*
 * Compute an estimate of the score by adding the positional score from the
 * previous ply to the material difference. If this score falls inside a
 * window which is 180 points wider than the alpha-beta window (or within a
 * 50 point window during quiescence search) call ScorePosition() to
 * determine a score, otherwise return the estimated score.  "side" is the
 * side which has to move.
 */

int
evaluate(short side,
         short ply,
         short alpha,
         short beta,
         short INCscore,
         short *InChk,     /* output Check flag     */
         short *blockable) /* king threat blockable */
{
    short xside;
    short s;

    xside = side ^ 1;
    s = -Pscore[ply - 1] + mtl[side] - mtl[xside] /* - INCscore */;
    hung[black] = hung[white] = 0;

    /* should we use the estimete or score the position */
    if ((ply == 1)
        || (ply == Sdepth)
        || (ply > Sdepth && s >= (alpha - 30) && s <= (beta + 30))
#ifdef CACHE
        || (use_etable && CheckEETable(side))
#endif
        )
    {
        short sq;

        /* score the position */
        array_zero(sseed, sizeof(sseed));

        seed[0] = seed[1] = 0;
        threats(side);

        if (Anyattack(side, sq = PieceList[xside][0]) && (board[sq] == king))
        {
            *InChk = (board[sq = PieceList[side][0]] == king)
                ? SqAttacked(sq, xside, blockable)
                : false;

            return ((SCORE_LIMIT + 1001) - ply);
        }

        threats(xside);
        *InChk = (board[sq = PieceList[side][0]] == king)
            ? Anyattack(xside, sq)
            : false;
        *blockable = true;
        EvalNodes++;

        if (ply > 4)
            PUTVAR = true;

        ExamineSquares();
        s = ScorePosition(side);
        PUTVAR = false;
    }
    else
    {
        /* use the estimate but look at check */
        short sq;

        *InChk = (board[sq = PieceList[side][0]] == king)
            ? SqAttacked(sq, xside, blockable)
            : false;

        if ((board[sq = PieceList[xside][0]] == king)
            && SqAttacked(sq, side, blockable))
        {
            return ((SCORE_LIMIT + 1001) - ply);
        }
    }

    Pscore[ply] = s - mtl[side] + mtl[xside];
    ChkFlag[ply - 1] = ((*InChk) ? Pindex[TOsquare] : 0);

    return s;
}



static short
value_of_weakest_attacker(long a2)
{
    short piece;
    short min_value, v;
    min_value = SCORE_LIMIT;

    for (piece = pawn; piece <= king; piece++)
    {
        if (control[piece] & a2)
        {
            if (min_value > (v = (*value)[stage][piece]))
                min_value = v;
        }
    }

    return min_value;
}




/*
 * Find (promoted) Bishop, (promoted) Rook, and Lance mobility, x-ray
 * attacks, and pins.  Let BRL be the bishop, rook, or lance.  Let P be the
 * first piece (not a king or a pawn) in a direction and let Q be the
 * second piece in the same direction.  If Q is an unprotected opponent's
 * piece with bigger relative value than BRL, there is a pin if P is an
 * opponent's piece and there is an x-ray attack if P belongs to this side.
 * Increment the hung[] array if a pin is found.
 */

inline int
BRLscan(short sq, short *mob)
{
#ifdef SAVE_NEXTPOS
    short d, dd;
#else
    unsigned char  *ppos, *pdir;
#endif

    short s, mobx;
    short u, xu, pin, ptyp;
#ifndef MINISHOGI
    short csq = column(sq);
#endif
    short piece, upiece, xupiece, rvalue, ds;
    small_short *Kd = Kdist[c2];

    mobx = s = 0;
    piece = board[sq];

    rvalue = (*value)[stage][piece];
    ptyp = ptype[c1][upiece = unpromoted[piece]];
    rvalue = (*value)[stage][upiece];

#ifdef SAVE_NEXTPOS
    u = first_direction(ptyp, &d, sq);
#else
    ppos = (*nextpos[ptyp])[sq];
    pdir = (*nextdir[ptyp])[sq];
    u = ppos[sq];
#endif

    pin = -1;           /* start new direction */

    do
    {
        if (Kd[u] < 2)
        {
            s += (ds = fv1[CTRLK] * (2 - Kd[u]));
        }

        if ((ds = starget[c1][u]) != 0)
        {
            /* threatening a target square */
            if ((pin < 0)               /* direct threat */
                || (color[pin] == c2))  /* pin threat    */
            {
                s += (ds *= fv1[TARGET]);
            }
        }

        if ((ds = shole[u]) != 0)
        {
            /* attacking or protecting a hole */
            s += (ds = fv1[HOLES]);
        }
        else if (InPromotionZone(c1, u))
        {
            /* attacking a square in promotion zone */
            s += (ds = fv1[HOLES] / 2);
        }

        if (color[u] == neutral)
        {
#ifdef SAVE_NEXTPOS
            dd = d;
            xu = next_position(ptyp, &d, sq, u);

            if (xu == next_direction(ptyp, &dd, sq))
                pin = -1;   /* oops new direction */
#else

            if ((xu = ppos[u]) == pdir[u])
                pin = -1;   /* oops new direction */
#endif
            u = xu;
            mobx++;
        }
        else
        {
            /* there is a piece in current direction */
            if (pin < 0)
            {
                /* it's the first piece in the current direction */
                if (color[u] == c1)
                {
#ifndef MINISHOGI
                    /* own intercepting piece in x-ray attack */
                    if (upiece == lance)
                    {
                        /* lance x-ray */
                        if (board[u] == pawn)
                        {
                            s += (ds = 2*fv1[PROTECT]);
                        }
                        else if (in_opening_stage)
                        {
                            s += (ds = -2*fv1[PROTECT]);
                        }
                    }
                    else
#endif
                    {
                        /* bishop or rook x-ray */
                        if ((upiece == bishop) && (board[u] == pawn)
                            && (GameType[c1] == STATIC_ROOK))
                        {
                            s += (ds = -2*fv1[HCLSD]);
                        }
#ifndef MINISHOGI
                        else if ((upiece == rook) && (board[u] == lance)
                                 && (GameType[c1] == STATIC_ROOK)
                                 && (column(u) == csq))
                        {
                            s += (ds = fv1[XRAY]);
                        }
#endif
                    }
                }
                else
                {
                    /* enemy's intercepting piece in pin attack */
#ifndef MINISHOGI
                    if (upiece == lance)
                    {
                        /* lance pin attack */
                        if (board[u] == pawn)
                        {
                            s += (ds = -2*fv1[PROTECT]);
                        }
                        else if (in_opening_stage)
                        {
                            s += (ds = 2 * fv1[PROTECT]);
                        }
                    }
                    else
#endif
                    {
                        /* bishop or rook pin attack */
                        if (board[u] == pawn)
                        {
                            s += (ds = -fv1[HCLSD]);
                        }
                    }
                }

#ifdef SAVE_NEXTPOS
                dd = d;
                xu = next_position(ptyp, &d, sq, u);

                if (xu != next_direction(ptyp, &dd, sq))
                    pin = u;    /* not on the edge and on to find a pin */
#else
                if ((xu = ppos[u]) != pdir[u])
                    pin = u;    /* not on the edge and on to find a pin */
#endif
                u = xu;
            }
            else
            {
                /* it's the second piece in the current direction */

                if (color[u] == c1)
                {
                    /* second piece is an own piece */
                    if ((upiece == bishop) && (board[u] == pawn)
                        && (GameType[c1] == STATIC_ROOK))
                    {
                        s += (ds = -fv1[HCLSD]);
                    }
                }
                else
                {
                    /* second piece is an enemy piece */
                    if (upiece == bishop && board[u] == pawn)
                    {
                        s += (ds = -fv1[HCLSD]/2);
                    }

                    if (((*value)[stage][xupiece = unpromoted[board[u]]]
                         > rvalue)
                        || (atk2[u] == 0))
                    {
                        if (color[pin] == c2)
                        {
                            if ((xupiece == king) && (in_endgame_stage))
                            {
                                s += (ds = 2 * fv1[PINVAL]);
                            }
                            else
                            {
                                s += (ds = fv1[PINVAL]);
                            }

                            if ((atk2[pin] == 0)
                                || (atk1[pin] > control[board[pin]] + 1))
                            {
                                hung[c2]++;
                                shung[u]++;
                            }
                        }
                        else
                        {
#ifndef MINISHOGI
                            if (upiece == lance)
                            {
                                s += (ds = fv1[XRAY] / 2);
                            }
                            else
#endif
                            {
                                s += (ds = fv1[XRAY]);
                            }
                        }
                    }
                }

                pin = -1;   /* new direction */

#ifdef SAVE_NEXTPOS
                u = next_direction(ptyp, &d, sq);
#else
                u = pdir[u];
#endif
            }
        }
    }
    while (u != sq);

    *mob = mobx;

    return s;
}


#define ctlSG (ctlS | ctlG | ctlPp | ctlLp | ctlNp | ctlSp)



/*
 * Assign penalties if king can be threatened by checks, if squares near
 * the king are controlled by the enemy (especially by promoted pieces), or
 * if there are no own generals near the king.
 *
 * The following must be true:
 * board[sq] == king, c1 == color[sq], c2 == otherside[c1]
 */

#define SCORETHREAT \
{ \
    if (color[u] != c2) \
    { \
        if ((atk1[u] == 0) || ((atk2[u] & CNT_MASK) != 0)) \
        { \
            ++cnt; \
        } \
        else \
        { \
            s += (ds = -fv1[CTRLK]); \
        } \
    } \
}




inline short
KingScan(short sq)
{
    short cnt;

#ifdef SAVE_NEXTPOS
    short d;
#else
    unsigned char  *ppos, *pdir;
#endif

    short s;
    short u, ptyp;
    short ok, ds;

    /* Penalties, if a king can be threatened by checks. */

    s = 0;
    cnt = 0;

    {
        short p;

        for (p = pawn; p < king; p++)
        {
            if (HasPiece[c2][p] || Captured[c2][p])
            {
                short ptyp;

                /* if a c1 piece can reach u from sq,
                 * then a c2 piece can reach sq from u.
                 * That means, each u is a square, from which a
                 * piece of type p and color c2 threats square sq.
                 */
                ptyp = ptype[c1][p];

#ifdef SAVE_NEXTPOS
                u = first_direction(ptyp, &d, sq);
#else
                ppos = (*nextpos[ptyp])[sq];
                pdir = (*nextdir[ptyp])[sq];
                u = ppos[sq];
#endif

                do
                {
                    /* If a p piece can reach (controls or can drop to)
                     * square u, then score threat.
                     */

                    if (atk2[u] & control[p])
                        SCORETHREAT
                    else if ((Captured[c2][p] && color[u]) == neutral)
                        SCORETHREAT

#ifdef SAVE_NEXTPOS
                    u = ((color[u] == neutral)
                         ? next_position(ptyp, &d, sq, u)
                         : next_direction(ptyp, &d, sq));
#else
                    u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
#endif
                }
                while (u != sq);
            }
        }
    }

    s += (ds = fv1[KSFTY] * KTHRT[cnt] / 16);

    /* Penalties, if squares near king are controlled by enemy. */

    cnt = 0;
    ok = false;
    ptyp = ptype[c1][king];

#ifdef SAVE_NEXTPOS
    u = first_direction(ptyp, &d, sq);
#else
    pdir = (*nextpos[ptyp])[sq];
    u = pdir[sq];
#endif

    do
    {
        if (!ok && color[u] == c1)
        {
            short ptype_piece = ptype[black][board[u]];

            if ((ptype_piece == ptype_silver) || (ptype_piece == ptype_gold))
                ok = true;
        }

        if (atk2[u] > atk1[u])
        {
            ++cnt;

            if (atk2[u] & ctlSG)
            {
                s += (ds = -fv1[KSFTY] / 2);
            }
        }

#ifdef SAVE_NEXTPOS
        u = next_direction(ptyp, &d, sq);
#else
        u = pdir[u];
#endif
    }
    while (u != sq);

    if (!ok || (cnt > 1))
    {
        if (cnt > 1)
            s += (ds = -fv1[KSFTY]/2);
        else
            s += (ds = -fv1[KSFTY]);
    }

    return s;
}


static short checked_trapped;


/*
 * See if the attacked piece has unattacked squares to move to. The following
 * must be true: c1 == color[sq] c2 == otherside[c1]
 */

inline int
trapped(short sq)
{
    short u, ptyp;
#ifdef SAVE_NEXTPOS
    short d;
#else
    unsigned char *ppos, *pdir;
#endif
    short piece;
    short rvalue;

    piece  = board[sq];
    rvalue = (*value)[stage][piece];
    ptyp   = ptype[c1][piece];

#ifdef SAVE_NEXTPOS
    u = first_direction(ptyp, &d, sq);
#else
    ppos = (*nextpos[ptyp])[sq];
    pdir = (*nextdir[ptyp])[sq];
    u = ppos[sq];
#endif

    do
    {
        if (color[u] != c1)
        {
            if ((atk2[u] == 0) || ((*value)[stage][board[u]] >= rvalue))
                return false;
        }

#ifdef SAVE_NEXTPOS
        u = ((color[u] == neutral)
             ? next_position(ptyp, &d, sq, u)
             : next_direction(ptyp, &d, sq));
#else
        u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
#endif
    }
    while (u != sq);

    checked_trapped = true;

    return true;
}



static int
AttackedPieceValue(short sq)
{
    short s;

    s = 0;

    hung[c1]++;
    shung[sq]++;

    if (trapped(sq))
    {
        hung[c1]  += 2;
        shung[sq] += 2;
    }

    return s;
}



static inline int
OpenFileValue(short sq, short hopn, short hopnx)
{
    short s = 0, fyle;

    if (PC1[fyle = column(sq)] == 0)
    {
        s += hopn;
    }

    if (PC2[fyle] == 0)
    {
        s += hopnx;
    }

    return s;
}




/* Distance bonus */

#define PromotionZoneDistanceValue(sq, dd) \
if ((ds = fv1[PROMD])) \
{ \
    s += (ds = ds * PromotionZoneDistanceBonus[crow(c1, sq)] * dd); \
}

#define OwnKingDistanceValue(sq, dd, maxd) \
if ((ds = fv1[KINGOD]) && ((ad = Kdist[c1][sq]) <= maxd)) \
{ \
    s += (ds = ds * OwnKingDistanceBonus[ad] * dd); \
}

#define EnemyKingDistanceValue(sq, dd, maxd) \
if ((ds = fv1[KINGOD]) && ((ad = Kdist[c2][sq]) <= maxd)) \
{ \
    s += (ds = ds * EnemyKingDistanceBonus[ad] * dd); \
}





/*
 * Calculate the positional value for a pawn on 'sq'.
 */

static inline int
PawnValue(short sq, short side)
{
    short s = 0;
    short ds;
#ifndef MINISHOGI
    short ccol = ccolumn(c1, sq);
#endif

    PromotionZoneDistanceValue(sq, 3);

    /* pawn mobility */
    if (color[(c1 == black) ? (sq + NO_COLS) : (sq - NO_COLS)] == neutral)
    {
        s += (ds = MBLTY[pawn]);
    }

    if ((a1 & ((ctlR | ctlRp) | ctlL)))
    {
        s += (ds = fv1[ATTACKED]);
    }

    if (in_opening_stage)
    {
#ifndef MINISHOGI
/* FIXME: [HGM] The 3rd-rank Pawn section is meaningless in mini-Shogi,
 * (which does not have opposing Pawns), and can do out-of-bound access,
 * as the promotion zone is only 1 rank, so Pawns can be closer than 3 ranks
 * to the board edge.
 */
        if (crow(c1, sq) == 2) /* pawn on 3d rank */
        {
            if (board[(c1 == black) ?
		      (sq + 3*NO_COLS) : (sq - 3*NO_COLS)] == pawn)
            {
                /* opposing pawn has been moved (even column == (sq & 1)) */

                short m;

                switch (ccol)
                {
                case 0:
                case 8:
                    m = (side == c1) ? 3 : 5;
                    break;

                case 4:
                    m = (side == c1) ? 2 : 3;
                    break;

                default:
                    m = (side == c1) ? 1 : 2;
                    break;
                }

                s += (ds = -m * MBLTY[pawn]);
            }
        }

/* FIXME: calculations below are wrong for minishogi, all done for 9x9
 * board - and anyway we don't know the stage really :)
 */
        if ((GameType[c1] == STATIC_ROOK) && (sq == csquare(c1, 43)))
        {
            if ((atk2[csquare(c1, 52)] & CNT_MASK) < 2)
            {
                s += (ds = fv1[ATTACKED]);
            }
        }

        if ((GameType[c2] == STATIC_ROOK) && (ccol == 1))
        {
            if (sq == csquare(c1, 28))
            {
                s += (ds = -fv1[ATTACKED]);
            }

            if (((atk1[csquare(c1, 19)] & CNT_MASK) < 2)
                && ((atk1[csquare(c1, 28)] & CNT_MASK) < 2))
            {
                s += (ds = -2 * fv1[ATTACKED]);
            }
        }
#endif
    }

    return s;
}


#ifndef MINISHOGI
/*
 * Calculate the positional value for a lance on 'sq'.
 */

static inline int
LanceValue(short sq)
{
    short s = 0, ds, ad;

    OwnKingDistanceValue(sq, 1, 2);

    OpenFileValue(sq, -fv1[HOPN], fv1[HOPN]);

    if (!checked_trapped && (crow(c1, sq) > 2))
    {
        if (in_opening_stage || trapped(sq))
        {
            s += (ds = -3 * fv1[ATTACKED]);
        }
        else
        {
            s += (ds = -2 * fv1[ATTACKED]);
        }
    }

    return s;
}



/*
 * Calculate the positional value for a knight on 'sq'.
 */

static inline int
KnightValue(short sq)
{
    short s = 0, ad;
    short ds, checked_trapped = false;
    short c = column(sq);

    PromotionZoneDistanceValue(sq, 1);
    OwnKingDistanceValue(sq, 1, 2);

    if (!checked_trapped && (crow(c1, sq) > 2))
    {
        if (trapped(sq))
        {
            s += (ds = -4 * fv1[ATTACKED]);
        }
        else
        {
            s += (ds = -3 * fv1[ATTACKED]);
        }
    }

    if ((c == 0) || (c == 8))
    {
        s += (ds = -fv1[ATTACKED]);
    }

    return s;
}
#endif



/*
 * Calculate the positional value for a silver on 'sq'.
 */

static inline int
SilverValue(short sq)
{
    short s= 0, ds, ad;

    OwnKingDistanceValue(sq, 2, 3);

    if ((Kdist[c1][sq] < 3)
        && (atk1[sq] & (control[gold] | control[silver])))
    {
        s += (ds = fv1[COHESION]);
    }

    if (in_opening_stage)
    {
#ifndef MINISHOGI
/* FIXME: calculations below are wrong for minishogi, all done for 9x9
 * board - and anyway we don't know the stage really :)
 */
        if (GameType[c1] == STATIC_ROOK)
        {
            if (csquare(c1, sq) == 12)
            {
                short csq;

                if ((board[csq = csquare(c1, 20)] == bishop)
                    && (color[csq] == c1))
                {
                    s += (ds = -2 * fv1[OPENWRONG]);
                }
            }
        }
#endif
    }
    else
    {
        EnemyKingDistanceValue(sq, 2, 3);
    }

    return s;
}



/*
 * Calculate the positional value for a gold on 'sq'.
 */

static inline int
GoldValue(short sq)
{
    short s = 0, ds, ad;

    OwnKingDistanceValue(sq, 2, 3);

    if ((Kdist[c1][sq] < 3)
        && (atk1[sq] & (control[gold] | control[silver])))
    {
        s += (ds = fv1[COHESION]);
    }

    if (in_opening_stage)
    {
#ifndef MINISHOGI
/* FIXME: calculations below are wrong for minishogi, all done for 9x9
 * board - and anyway we don't know the stage really :)
 */
        if ((GameType[c1] == STATIC_ROOK) && (GameType[c2] != STATIC_ROOK))
        {
            if (Mvboard[csquare(c1, 3)])
            {
                s += (ds = -2 * fv1[OPENWRONG]);
            }
        }
#endif
    }
    else
    {
        EnemyKingDistanceValue(sq, 2, 3);
    }

    return s;
}



/*
 * Calculate the positional value for a bishop on 'sq'.
 */

static inline int
BishopValue(short sq)
{
    short s = 0, ds, ad;

    if (in_opening_stage)
    {
#ifndef MINISHOGI
/* FIXME: calculations below are wrong for minishogi, all done for 9x9
 * board - and anyway we don't know the stage really :)
 */
        if (GameType[c1] == RANGING_ROOK)
        {
            /* Bishops diagonal should not be open */
            if (!on_csquare(c1, pawn, 30))
            {
                s += (ds = -fv1[OPENWRONG]);
            }
        }
        else if (GameType[c2] == RANGING_ROOK)
        {
            /* Bishops diagonal should be open */
            if ((csquare(c1, sq) == 10)
                && (!empty_csquare(c1, 20) || !empty_csquare(c1, 30)))
            {
                s += (ds = -fv1[OPENWRONG]);
            }
            else if ((csquare(c1, sq) == 20) && !empty_csquare(c1, 30))
            {
                s += (ds = -fv1[OPENWRONG]);
            }
        }
#endif
    }
    else
    {
        EnemyKingDistanceValue(sq, 1, 3);
    }

    return s;
}



/*
 * Calculate the positional value for a rook on 'sq'.
 */

static inline int
RookValue(short sq, short side)
{
    short s = 0, ds, ad;

    OpenFileValue(sq, 2 * fv1[HOPN], 4*fv1[HOPN]);

    if (in_opening_stage)
    {
#ifndef MINISHOGI
/* FIXME: calculations below are wrong for minishogi, all done for 9x9
 * board - and anyway we don't know the stage really :)
 */
        short WRONG = fv1[OPENWRONG], OPOK = WRONG / 3;

        if (GameType[c1] == STATIC_ROOK)
        {
            short c = ccolumn(c1, sq);

            /* Bonus for rook on 8th file */
            if (c == 7)
            {
                 s += (ds = OPOK);
            }

            /*
             * Bonus for rook on right side,
             * penalty for rook on left side
             */

            c = 4 - c;
            ds = 0;

            if (c < 0)
            {
                s += (ds = c + c + OPOK);
            }
            else if (c >= 0)
            {
                s += (ds = -c - c - WRONG);
            }
        }
        else if (GameType[c1] == RANGING_ROOK)
        {
            /* Bonus for rook on left side and
             * bishops diagonal closed, penalty otherwise. */

            short c;
            c = 4 - ccolumn(c1, sq);
            ds = 0;

            if (c >= 0)
            {
                /* Bishops diagonal should not be open. */
                if (on_csquare(c1, pawn, 30))
                    s += (ds = OPOK);
                else
                    s += (ds = -c - c - WRONG);
            }
            else if (c < 0)
            {
                s += (ds = -c - c - WRONG);

                /* Penalty for king not on initial square. */
                if (!on_csquare(side, king, 4))
                {
                    s  += -4 * WRONG;
                    ds += -4 * WRONG;
                }
            }
        }
#endif
    }
    else
    {
        EnemyKingDistanceValue(sq, 1, 3);
    }

    return s;
}



/*
 * Calculate the positional value for a promoted pawn on 'sq'.
 */

static inline int
PPawnValue(short sq)
{
    short s = 0, ds, ad;

    EnemyKingDistanceValue(sq, 3, 10);

    return s;
}



#ifndef MINISHOGI
/*
 * Calculate the positional value for a promoted lance on 'sq'.
 */

static inline int
PLanceValue(short sq)
{
    short s = 0, ds, ad;

    EnemyKingDistanceValue(sq, 3, 10);

    return s;
}



/*
 * Calculate the positional value for a promoted knight on 'sq'.
 */

static inline int
PKnightValue(short sq)
{
    short s = 0, ds, ad;

    EnemyKingDistanceValue(sq, 3, 10);

    return s;
}
#endif



/*
 * Calculate the positional value for a promoted silver on 'sq'.
 */

static inline int
PSilverValue(short sq)
{
    short s = 0, ds, ad;

    EnemyKingDistanceValue(sq, 3, 10);

    return s;
}



/*
 * Calculate the positional value for a promoted bishop on 'sq'.
 */

static inline int
PBishopValue(short sq)
{
    short s = 0, ds, ad;

    EnemyKingDistanceValue(sq, 3, 4);

    return s;
}



/*
 * Calculate the positional value for a promoted rook on 'sq'.
 */

static inline int
PRookValue(short sq)
{
    short s = 0, ds, ad;

    EnemyKingDistanceValue(sq, 3, 4);

    OpenFileValue(sq, 3 * fv1[HOPN], 2 * fv1[HOPN]);

    return s;
}



/*
 * Calculate the positional value for a king on 'sq'.
 */

static inline int
KingValue(short sq)
{
    short s = 0, ds;

    if (fv1[KSFTY] != 0)
        s += KingScan(sq);

#ifndef MINISHOGI
/* FIXME: calculations below are wrong for minishogi, all done for 9x9
 * board - and anyway we don't know the stage really :)
 */
    if (in_opening_stage)
    {
        if ((GameType[c1] != UNKNOWN) && (ccolumn(c1, sq) == 4))
        {
            s += (ds = -fv1[OPENWRONG] / 3);
        }
        else if ((GameType[c1] == STATIC_ROOK) && on_right_side(c1, sq))
        {
            s += (ds = -fv1[OPENWRONG] / 2);
        }
        else if ((GameType[c1] == RANGING_ROOK) && on_left_side(c1, sq))
        {
            s += (ds = -fv1[OPENWRONG] / 2);
        }
    }
#endif

    /* CHECKME: is this correct? */
    if ((ds = fv1[HOPN]))
    {
        s += OpenFileValue(sq, -2 * ds, -4 * ds);
    }

    return s;
}



/*
 * Calculate the positional value for a piece on 'sq'.
 */

static inline int
PieceValue(short sq, short side)
{
    short s, piece, ds;
    short mob;

    piece = board[sq];

    if (piece == no_piece)
        return 0;

    s = (*Mpiece[piece])[c1][sq];

    checked_trapped = false;

    if (sweep[piece])
    {
        /* pin/x-ray attack and mobility for sweeping pieces */
        s += (ds = BRLscan(sq, &mob));

        if ((piece == bishop) || (piece == pbishop))
            s += (ds = BMBLTY[mob] * fv1[MOBILITY] / 100);
        else if ((piece == rook) || (piece == prook))
            s += (ds = RMBLTY[mob] * fv1[MOBILITY] / 100);
#ifndef MINISHOGI
        else
            s += (ds = LMBLTY[mob] * fv1[MOBILITY] / 100);
#endif
    }
    else
    {
        /* mobility for non-sweeping pieces */
    }

    a2 = atk2[sq];
    a1 = atk1[sq];

    if (a2 > 0)
    {
        /* opponent attacks piece */
        if (a1 == 0)
        {
            /* undefended piece */
            s += AttackedPieceValue(sq);
        }
        else
        {
            /* defended piece */
            short attack_value = value_of_weakest_attacker(a2);
            short piece_value = (*value)[stage][piece];

            if (attack_value < piece_value)
            {
                /* attacked by a weaker piece */
                s += AttackedPieceValue(sq) / 2;
            }
            else if (abs(attack_value - piece_value) < 10)
            {
                /* opponent has the option to exchange equal pieces */
                s += (ds = -fv1[ATTACKED]);
            }
        }
    }

    if (piece != king)
    {

        if (a1 > 0)
        {
            /* piece is defended */
            s += (ds = (a1 & CNT_MASK) * fv1[PROTECT]);
        }

        if (sseed[sq])
        {
            s += (ds = fv1[SEED]);
        }

        if (sloose[sq])
        {
            s += (ds = -fv1[LOOSE]);
        }

        if (starget[c1][sq])
        {
            if (sweep[piece])
            {
                s -= (ds = -fv1[ATTACKED]/2);
            }
            else if (piece == pawn)
            {
                s += (ds = fv1[ATTACKED]);
            }
        }

        if (starget[c2][sq])
        {
            if (piece != pawn)
            {
                s -= (ds = -fv1[ATTACKED] / 3);
            }
            else
            {
                s += (ds = fv1[ATTACKED]);
            }
        }

        if (Kdist[c1][sq] == 1)
        {
            s += (ds = fv1[KSFTY]);
        }
    }

    switch (piece)
    {
    case pawn:
        s += PawnValue(sq, side);
        break;

#ifndef MINISHOGI
    case lance:
        s += LanceValue(sq);
        break;

    case knight:
        s += KnightValue(sq);
        break;
#endif

    case silver:
        s += SilverValue(sq);
        break;

    case gold:
        s += GoldValue(sq);
        break;

    case bishop:
        s += BishopValue(sq);
        break;

    case rook:
        s += RookValue(sq, side);
        break;

    case king:
        s += KingValue(sq);
        break;

    case ppawn:
        s += PPawnValue(sq);
        break;

#ifndef MINISHOGI
    case plance:
        s += PLanceValue(sq);
        break;

    case pknight:
        s += PKnightValue(sq);
        break;
#endif

    case psilver:
        s += PSilverValue(sq);
        break;

    case pbishop:
        s += PBishopValue(sq);
        break;

    case prook:
        s += PRookValue(sq);
        break;
    }

    return s;
}



/*
 * Score distance to pattern regarding the game type which side plays.
 */

short
ScorePatternDistance(short c1)
{
    short ds, s = 0;
    small_short *fv1 = fvalue[c1];
    short os = 0;

    if ((MAXCDIST > 0) && (fv1[PCASTLE] != 0)
        && ((os = castle_opening_sequence[c1]) >= 0))
    {
        ds = board_to_pattern_distance(c1, os, MAXCDIST, GameCnt);

        if (ds != 0)
        {
            s += (ds *= fv1[PCASTLE]);
        }
    }

    if ((MAXADIST > 0) && (fv1[PATTACK] != 0)
        && ((os = attack_opening_sequence[c1]) >= 0))
    {
        ds = board_to_pattern_distance(c1, os, MAXADIST, GameCnt);

        if (ds != 0)
        {
            s += (ds *= fv1[PATTACK]);
        }
    }

    return s;
}




/*
 * Determine castle and attack pattern which should be reached next.
 * Only patterns are considered, which have not been reached yet.
 */

static void
UpdatePatterns(short side, short GameCnt)
{
    char s[12];
    short xside = side ^ 1;
    short os;
    short j, k, n = 0;

    strcpy(s, "CASTLE_?_?");
    s[7] = GameType[side];
    s[9] = GameType[xside];
    castle_opening_sequence[side] = os
        = locate_opening_sequence(side, s, GameCnt);

    if (flag.post && (os != END_OF_SEQUENCES))
    {
        for (j = 0; j < MAX_SEQUENCE; j++)
        {
            for (k = OpeningSequence[os].first_pattern[j];
                 k != END_OF_PATTERNS;
                 k = Pattern[k].next_pattern)
            {
                if (Pattern[k].distance[side] >= 0)
                    n++;
            }
        }
    }

    if (os != END_OF_SEQUENCES)
        update_advance_bonus(side, os);

    strcpy(s, "ATTACK_?_?");
    s[7] = GameType[side];
    s[9] = GameType[xside];
    attack_opening_sequence[side] = os
        = locate_opening_sequence(side, s, GameCnt);

    if (flag.post && (os != END_OF_SEQUENCES))
    {
        for (j = 0; j < MAX_SEQUENCE; j++)
        {
            for (k = OpeningSequence[os].first_pattern[j];
                 k != END_OF_PATTERNS;
                 k = Pattern[k].next_pattern)
            {
                if (Pattern[k].distance[side] >= 0)
                    n++;
            }
        }
    }

    if (flag.post)
        dsp->ShowPatternCount(side, n);

    if (os != END_OF_SEQUENCES)
        update_advance_bonus(side, os);
}



static void
ScoreCaptures(void)
{
    short ds, col, n, m, piece;

    if ((n = Captured[c1][pawn]))
    {
        ds = m = 0;

        for (col = 0; col < NO_COLS; col++)
        {
            if (!PC1[col])
            {
                m++;
                ds += fv1[PWNDROP];
            }
        }

        /* FIXME! another great obfuscated line... */
        pscore[c1] += (ds *= ((n > 2) ? 3 : n));
    }

    if ((m = seed[c1]))
    {
        for (piece = pawn+1, n = 0; piece <= rook; piece++)
        {
            if (Captured[c1][piece])
                n++;
        }

        pscore[c1] += (ds = m * fv1[DFFDROP]);
    }

    for (piece = pawn, n = 0; piece <= rook; piece++)
    {
        if (Captured[c1][piece])
        {
            switch (piece)
            {
            case bishop:
                ds = BMBLTY[MAX_BMBLTY - 1];
                break;

            case rook:
                ds = RMBLTY[MAX_RMBLTY - 1];
                break;

#ifndef MINISHOGI
            case lance:
                ds = LMBLTY[MAX_LMBLTY - 1];
                break;
#endif

            default:
                ds = MBLTY[piece];
            }

            pscore[c1] += ds;

            if (!Captured[c2][piece])
                n += relative_value[piece];
        }
    }

    if (n)
    {
        pscore[c1] += (ds = -n * fv1[OPPDROP] / 2);
    }
}





/*
 * Perform normal static evaluation of board position. A score is generated
 * for each piece and these are summed to get a score for each side.
 */

short
ScorePosition(short side)
{
    short score;
    short sq, i, xside;
    short s;
    short ds;

    xside = side ^ 1;

    UpdateWeights(side);

    hung[black] = hung[white] = pscore[black] = pscore[white] = 0;

    array_zero(shung, sizeof(shung));

#ifdef CACHE
    if (!(use_etable && ProbeEETable(side, &s)))
    {
#endif
        for (c1 = black; c1 <= white; c1++)
        {
            c2 = c1 ^ 1;

            /* atk1 is array of attacks on squares by my side */
            atk1 = attack[c1];

            /* atk2 is array of attacks on squares by other side */
            atk2 = attack[c2];

            /* same for PC1 and PC2 */
            PC1 = PawnCnt[c1];
            PC2 = PawnCnt[c2];

            /* same for fv1 and fv2 */
            fv1 = fvalue[c1];

            for (i = PieceCnt[c1]; i >= 0; i--)
            {
                sq = PieceList[c1][i];

#if defined SAVE_SVALUE
                pscore[c1] += PieceValue(sq, side);
#else
                pscore[c1] += (svalue[sq] = PieceValue(sq, side));
#endif
            }

            ScoreCaptures();
        }

#ifndef MINISHOGI
# define BLACKHOME_START  0
# define BLACKHOME_END   26
# define MIDDLEROW_START 36
# define MIDDLEROW_END   44
# define WHITEHOME_START 54
# define WHITEHOME_END   80
#else
# define BLACKHOME_START  0
# define BLACKHOME_END    4
# define MIDDLEROW_START 10
# define MIDDLEROW_END   14
# define WHITEHOME_START 19
# define WHITEHOME_END   24
#endif
        for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
        {
            short n;

            fv1 = fvalue[c1];

            /* Score fifth rank */
            for (sq = MIDDLEROW_START, n = 0; sq <= MIDDLEROW_END; sq++)
            {
                if ((color[sq] == c1) || (attack[c1][sq] != 0))
                    n++;
            }

            if (n != 0)
            {
                pscore[c1] += (ds = n * fv1[CNTRL5TH]);
            }

            /* Score holes */
            for (sq = ((c1 == black) ? BLACKHOME_START : WHITEHOME_START), n = 0;
                 sq <= ((c1 == black) ? BLACKHOME_END : WHITEHOME_END);
                 sq++)
            {
                if (board[sq] == no_piece && attack[c1][sq] == 0)
                    n++;
            }

            if (n != 0)
            {
                pscore[c1] += (ds = -n * fv1[HOLES]);
            }

            if (hung[c1] > 1)
            {
                pscore[c1] += (ds = -fv1[HUNGX]);
            }

            /* Score opening features and
             * castle/attack pattern distances */

            if (in_opening_stage)
            {
                pscore[c1] += (ds = ScoreKingOpeningFeatures());
                pscore[c1] += (ds = ScorePatternDistance(c1));
            }
        }

        score = mtl[side] - mtl[xside]
            + pscore[side] - pscore[xside] + 10;

#ifdef CACHE
        if (use_etable && PUTVAR)
            PutInEETable(side, score);
#endif

        return score;
#ifdef CACHE /* CHECKME: this looks bad */
    }

    return s;
#endif
}



/*
 * Try to determine the game type of "side".
 */

#ifndef MINISHOGI
inline static void
GuessGameType(short side_to_move)
{
    short side, gt;
    short StaticRook[2]  = { 0, 0 };
    short RangingRook[2] = { 0, 0 };

    for (side = black; side <= white; side++)
    {
        /* computer should not change its mind */

        extern int bookflag;

        gt = GameType[side];

        if (!bookflag && (side == side_to_move))
        {
            if (gt == STATIC_ROOK)
                StaticRook[side] += 4;
            else if (gt == RANGING_ROOK)
                RangingRook[side] += 4;
        }

        /* static rook conditions */

        if (on_column(side, rook, 7))
            StaticRook[side] += 3;

        if (on_csquare(side, pawn, 34))
            StaticRook[side] += 6;
        else if (on_csquare(side, pawn, 43))
            StaticRook[side] += 4;
        else if (!on_column(side, pawn, 7))
            StaticRook[side] += 5;

        if (empty_csquare(side, 5) || empty_csquare(side, 6))
            StaticRook[side] += 2;

        if (on_left_side(side, king))
            StaticRook[side] += 2;

        /* ranging rook conditions */

        if (on_left_side(side, rook))
            RangingRook[side] += 5;
        else if (!on_column(side, rook, 7))
            RangingRook[side] += 3;

        if (on_csquare(side, pawn, 25))
            RangingRook[side] += 1;

        if (on_csquare(side, pawn, 30))
            RangingRook[side] += 1;
        else
            RangingRook[side] -= 2;

        if (!on_right_side(side, rook))
            RangingRook[side] += 4;

        if (on_right_side(side, king))
            RangingRook[side] += 4;

        if (on_csquare(side, bishop, 20))
        {
            if (on_csquare(side, silver, 11)
                || on_csquare(side, silver, 12)
                || on_csquare(side, silver, 21))
            {
                RangingRook[side] += 3;
            }
        }

        if ((StaticRook[side] > 5) || (RangingRook[side] > 5))
        {
            GameType[side] = (StaticRook[side] > RangingRook[side])
                ? STATIC_ROOK : RANGING_ROOK;
        }
        else
        {
            GameType[side] = UNKNOWN;
        }
    }

    if ((GameType[black] == UNKNOWN) || (GameType[white] == UNKNOWN))
    {
        for (side = black; side <= white; side++)
        {
            if ((side == computer) && (GameType[side] == UNKNOWN))
            {
                /*
                 * Game type is UNKNOWN.
                 * Make a decision what type of game to play.
                 * To make computer games more interesting, make a
                 * random decision.
                 */

                if (!on_csquare(side, pawn, 25))
                {
                    /* Play static rook if rook pawn has been pushed! */
                    GameType[side] = STATIC_ROOK;
                }
                else
                {
                    unsigned int random = urand() % 100;
                    short d = StaticRook[side] - RangingRook[side];

                    switch (GameType[side ^ 1])
                    {
                    case STATIC_ROOK:
                        if (random < 35 + d)
                            GameType[side] = STATIC_ROOK;
                        else if (random < 95)
                            GameType[side] = RANGING_ROOK;
                        break;

                    case RANGING_ROOK:
                        if (random < 75 + d)
                            GameType[side] = STATIC_ROOK;
                        else if (random < 95)
                            GameType[side] = RANGING_ROOK;
                        break;

                    default:
                        if (random < 33 + d)
                            GameType[side] = STATIC_ROOK;
                        else if (random < 66)
                            GameType[side] = RANGING_ROOK;
                    }
                }
            }
        }
    }
}
#endif




static void
DetermineGameType(short side_to_move)
{
    short side;

#ifndef MINISHOGI
    /* FIXME: calculations below are wrong for minishogi, all done for 9x9 */
    GuessGameType(side_to_move);
#else
    GameType[black] = UNKNOWN;
    GameType[white] = UNKNOWN;
#endif

    array_zero(Mpawn,   sizeof(Mpawn));
#ifndef MINISHOGI
    array_zero(Mlance,  sizeof(Mlance));
    array_zero(Mknight, sizeof(Mknight));
#endif
    array_zero(Msilver, sizeof(Msilver));
    array_zero(Mgold,   sizeof(Mgold));
    array_zero(Mbishop, sizeof(Mbishop));
    array_zero(Mrook,   sizeof(Mrook));
    array_zero(Mking,   sizeof(Mking));

    if (in_opening_stage)
    {
        for (side = black; side <= white; side++)
            UpdatePatterns(side, GameCnt);
    }
    else
    {
        dsp->ShowPatternCount(black, -1);
        dsp->ShowPatternCount(white, -1);
    }
}



/*
 * This is done one time before the search is started. Set up arrays
 * Mwpawn, Mbpawn, Mknight, Mbishop, Mking which are used in the SqValue()
 * function to determine the positional value of each piece.
 */

void
ExaminePosition(short side)
{
    short c1, piece, sq, i, bsq, wsq;

    /* Build enemy king distance tables. */

    for (sq = 0, bsq = BlackKing, wsq = WhiteKing; sq < NO_SQUARES; sq++)
    {
        Kdist[black][sq] = distance(sq, bsq);
        Kdist[white][sq] = distance(sq, wsq);
    }

    threats(black);
    threats(white);

    ExamineSquares();

    DetermineGameType(side);
    DetermineStage(side);

    UpdateWeights(side);

    array_zero(HasPiece, sizeof(HasPiece));

    for (c1 = black; c1 <= white; c1++)
    {
        for (i = PieceCnt[c1]; i >= 0; i--)
        {
            ++HasPiece[c1][piece = board[sq = PieceList[c1][i]]];
        }
    }
}



/* FIXME: calculations below are wrong for minishogi, all done for 9x9 */
void
DetermineStage(short side)
{
    short xside = side ^ 1, ds, db1, c1, c2, feature;

    /* Determine initial stage */

    balance[side] = balance[xside] = 50;

    if ((GameType[side] == STATIC_ROOK)
        && (GameType[xside] == STATIC_ROOK))
    {
        if (GameCnt < 40)
            stage = 0;
        else if (GameCnt < 60)
            stage = 15;
        else if (GameCnt < 80)
            stage = 25;
        else
            stage = 30;
    }
    else if ((GameType[side] == RANGING_ROOK)
             || (GameType[xside] == RANGING_ROOK))
    {
        if (GameCnt < 30)
            stage = 0;
        else if (GameCnt < 50)
            stage = 15;
        else if (GameCnt < 70)
            stage = 25;
        else
            stage = 30;
    }
    else
    {
        if (GameCnt < 35)
            stage = 0;
        else if (GameCnt < 55)
            stage = 15;
        else if (GameCnt < 75)
            stage = 25;
        else
            stage = 30;
    }

#ifndef MINISHOGI
    /* Update stage depending on board features and attack balance value */

    if (abs(ds = (mtl[side] - mtl[xside]))
        > (db1 = (*value)[stage][lance]))
    {
        db1 = abs(4 * ds / db1);

        if (ds < 0)
            balance[side] += db1;
        else if (ds > 0)
            balance[xside] += db1;

        stage += (ds = db1);
    }
#endif

    for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
    {
        if ((ds = seed[c1]) > 2)
        {
            balance[c1] += (db1 = ds * 2);
            balance[c2] -= db1;

            if (stage < 30)
                stage = 30;

            stage += ds;
        }

        if ((db1 = hung[c1]) > 2)
        {
            balance[c1] -= (db1 *= 2);
            balance[c2] += db1;
        }

        if ((db1 = loose[c1]) > 4)
        {
            balance[c1] -= (db1 /= 2);
            balance[c2] += db1;
            stage += (ds = 1);
        }

        if ((ds = hole[c1]))
        {
            balance[c1] -= (db1 = ds);
            balance[c2] += db1;
            stage += (ds /= 2);
        }

        if ((db1 = target[c1]) > 3)
        {
            balance[c1] += (db1 /= 3);
            balance[c2] -= db1;
            stage += (ds = db1 / 4);
        }

        stage += (ds = captured[c1] / 2);

        if ((db1 = captured[c1]) > 4)
        {
            balance[c1] += (db1 /= 2);
            stage += (ds = 3);
        }

        if ((db1 = dcaptured[c1]) > 3)
        {
            balance[c1] += db1;
            stage += (ds = 3);
        }

        if (balance[c1] > 99)
            balance[c1] = 99;
        else if (balance[c1] < 0)
            balance[c1] = 0;
    }

    if (stage > 99)
        stage = 99;
    else if (stage < 0)
        stage = 0;

    if (flag.post)
        dsp->ShowStage();

    /* Determine stage dependant weights */

    ADVNCM[pawn]   = 1; /* advanced pawn bonus increment   */
#ifndef MINISHOGI
    ADVNCM[lance]  = 1;
    ADVNCM[knight] = 1;
#endif
    ADVNCM[silver] = 1; /* advanced silver bonus increment */
    ADVNCM[gold]   = 1; /* advanced gold bonus increment   */
    ADVNCM[bishop] = 1;
    ADVNCM[rook]   = 1;
    ADVNCM[king]   = 1; /* advanced king bonus increment   */

    MAXCDIST = (stage < 33) ? (33 - stage) / 4 : 0;
    MAXADIST = (stage < 30) ? (30 - stage) / 4 : 0;

    for (c1 = black; c1 <= white; c1++)
    {
        for (feature = 0; feature < NO_FEATURES; feature++)
        {
            fvalue[c1][feature] =
                ((((*fscore)[stage][feature][0] * (99 - balance[c1])) + 50)
                 / 100)
                + ((((*fscore)[stage][feature][1] * balance[c1]) + 50)
                   / 100);
        }
    }
}



void
UpdateWeights(short stage)
{
  /* FIXME: this was emptied between 1.1p02 ans 1.2p03, do we keep it ? */
}



/*
 * Compute stage dependent relative material values assuming
 * linearity between the main stages:
 *
 *   minstage < stage < maxstage =>
 *          stage - minstage        value - minvalue
 *         -------------------  =  -------------------
 *         maxstage - minstage     maxvalue - minvalue
 */


static short
linear_piece_value(short piece, short stage, short i, short j)
{
    short minvalue, maxvalue, minstage, maxstage;

    minstage = ispvalue[0][i];
    maxstage = ispvalue[0][j];
    minvalue = ispvalue[piece][i];
    maxvalue = ispvalue[piece][j];

    /* FIXME: set a variable then return it, puh-leeze! */
    return ((stage - minstage) * (maxvalue - minvalue)
            / (maxstage - minstage)) + minvalue;
}



static short
linear_feature_value(short feature, short stage, short i, short j)
{
    short minvalue, maxvalue, minstage, maxstage;

    minstage = ispvalue[0][i];
    maxstage = ispvalue[0][j];
    minvalue = weight[feature][i];
    maxvalue = weight[feature][j];

    /* FIXME: set a variable then return it, puh-leeze! */
    return ((stage - minstage) * (maxvalue - minvalue)
            / (maxstage - minstage)) + minvalue;
}


/*
 * matweight = percentage_of_max_value * max_value(stage) / 100
 * max_value(0) = MAX_VALUE; max_value(100) = MIN_VALUE
 *  => max_value(stage) = a * stage + b; b = MAX_VALUE,
 *     a = (MIN_VALUE - MAX_VALUE)/100
 */

#define MIN_VALUE 300
#define MAX_VALUE 1000

#define max_value(stage) \
((long)(MIN_VALUE - MAX_VALUE) * stage + (long)100 * MAX_VALUE)
#define matweight(value, stage) \
((long)max_value(stage) * value / 10000)



void
Initialize_eval(void)
{
    short stage, piece, feature, i;

    for (stage = 0; stage < NO_STAGES; stage++)
    {
        for (i = 0; i < MAIN_STAGES; i++)
        {
            if (stage == ispvalue[0][i])
            {
                for (piece = 0; piece < NO_PIECES; piece++)
                {
                    (*value)[stage][piece] =
                        matweight(ispvalue[piece][i], stage);
                }

                for (feature = 0; feature < NO_FEATURES; feature++)
                {
                    (*fscore)[stage][feature][0] =
                        (weight[feature][i]
                         * weight[feature][MAIN_STAGES] + 50) / 100;

                    (*fscore)[stage][feature][1] =
                        (weight[feature][i]
                         * weight[feature][MAIN_STAGES + 1] + 50) / 100;
                }

                break;
            }

            if (stage < ispvalue[0][i + 1])
            {
                for (piece = 0; piece < NO_PIECES; piece++)
                {
                    (*value)[stage][piece] =
                        matweight(
                            linear_piece_value(piece, stage, i, i + 1),
                            stage);
                }

                for (feature = 0; feature < NO_FEATURES; feature++)
                {
                    (*fscore)[stage][feature][0] =
                        (linear_feature_value(feature, stage, i, i + 1)
                         * weight[feature][MAIN_STAGES] + 50) / 100;

                    (*fscore)[stage][feature][1] =
                        (linear_feature_value(feature, stage, i, i + 1)
                         * weight[feature][MAIN_STAGES + 1] + 50) / 100;
                }

                break;
            }
        }
    }
}