File: ucinternal.cc

package info (click to toggle)
exult 1.12.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,608 kB
  • sloc: cpp: 169,917; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 539; objc: 416
file content (3487 lines) | stat: -rw-r--r-- 94,111 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
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
/*
 *  ucinternal.cc - Interpreter for usecode.
 *
 *  Copyright (C) 1999  Jeffrey S. Freedman
 *  Copyright (C) 2000-2022  The Exult Team
 *
 *  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 of the License, 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <limits>
#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include "Audio.h"
#include "Gump.h"
#include "Gump_manager.h"
#include "Notebook_gump.h"
#include "Text_gump.h"
#include "actions.h"
#include "actors.h"
#include "animate.h"
#include "barge.h"
#include "chunks.h"
#include "conversation.h"
#include "databuf.h"
#include "effects.h"
#include "egg.h"
#include "exult.h"
#include "game.h"
#include "gamemap.h"
#include "gamewin.h"
#include "ios_state.hpp"
#include "keyring.h"
#include "miscinf.h"
#include "monsters.h"
#include "mouse.h"
#include "opcodes.h"
#include "party.h"
#include "schedule.h"
#include "stackframe.h"
#include "touchui.h"
#include "tqueue.h"
#include "ucfunction.h"
#include "ucinternal.h"
#include "ucsched.h"
#include "ucsymtbl.h"
#include "usefuns.h"
#include "useval.h"

#if (defined(USE_EXULTSTUDIO) && defined(USECODE_DEBUGGER))
#	include "debugmsg.h"
#	include "debugserver.h"
#	include "servemsg.h"
#	include "server.h"
#endif

#include <algorithm>    // STL function things
#include <cstdio>       /* Debugging.           */
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <map>

#ifdef XWIN
#	include <csignal>
#endif

using std::cerr;
using std::cout;
using std::dec;
using std::endl;
using std::exit;
using std::hex;
using std::ifstream;
using std::ios;
using std::istream;
using std::ofstream;
using std::ostream;
using std::setfill;
using std::setw;
using std::size_t;
using std::strchr;
using std::string;
using std::vector;

// External globals..

extern bool intrinsic_trace;
extern int  usecode_trace;

#ifdef USECODE_DEBUGGER

extern bool      usecode_debugging;
std::vector<int> intrinsic_breakpoints;

void initialise_usecode_debugger() {
	// Summon up the configuration file

	// List all the keys.

	// Render intrinsic names to numbers (unless already given as
	// a number (which might be hex. Convert from that.

	// push them all onto the list
}

#endif

void Usecode_internal::stack_trace(ostream& out) {
	if (call_stack.empty()) {
		return;
	}

	auto iter = call_stack.begin();

	const boost::io::ios_flags_saver flags(out);
	const boost::io::ios_fill_saver  fill(out);
	out << std::hex << std::setfill('0');
	do {
		out << *(*iter);
		auto it = except_stack.find(*iter);
		if (it != except_stack.end()) {
			out << ", active catch at 0x" << std::setw(8) << it->second;
		}
		out << endl;
		if ((*iter)->call_depth == 0) {
			break;
		}
		++iter;
	} while (true);
}

Usecode_function* Usecode_internal::find_function(int funcid) {
	Usecode_function* fun;
	// locate function
	const unsigned int slotnum = funcid / 0x100;
	if (slotnum >= funs.size()) {
		fun = nullptr;
	} else {
		Funs256&     slot  = funs[slotnum];
		const size_t index = funcid % 0x100;
		fun                = index < slot.size() ? slot[index] : nullptr;
	}
	if (!fun) {
#ifdef DEBUG
		cout << "Usecode " << funcid << " not found." << endl;
#endif
	}
	return fun;
}

inline Usecode_class_symbol* Usecode_internal::get_class(int n) {
	return symtbl->get_class(n);
}

inline Usecode_class_symbol* Usecode_internal::get_class(const char* nm) {
	return symtbl->get_class(nm);
}

inline int Usecode_internal::get_shape_fun(int n) {
	return n < 0x400 ? n
					 : (symtbl ? symtbl->get_high_shape_fun(n)
							   // Default to 'old-style' high shape functions.
							   : 0x1000 + (n - 0x400));
}

inline bool Usecode_internal::is_object_fun(int n) {
	if (!symtbl) {
		return n < 0x800;
	}
	return symtbl->is_object_fun(n);
}

bool Usecode_internal::call_function(
		int funcid, int eventid, Game_object* caller, bool entrypoint,
		bool orig, int givenargs) {
	Usecode_function* fun = find_function(funcid);
	if (!fun) {
		return false;
	}
	if (orig) {
		if (!(fun = fun->orig)) {
#ifdef DEBUG
			cout << "Original usecode " << funcid << " not found." << endl;
#endif
			return false;
		}
	}

	int depth;
	int oldstack;
	int chain;

	if (entrypoint) {
		depth    = 0;
		oldstack = 0;
		chain    = Stack_frame::getCallChainID();

	} else {
		Stack_frame* parent = call_stack.front();

		// find new depth
		depth = parent->call_depth + 1;

		// find number of elements available to pop from stack (as arguments)
		oldstack = sp - parent->save_sp;

		chain = parent->call_chain;

		if (caller == nullptr) {
			caller = parent->caller_item.get();    // use parent's
		}
	}

	auto* frame = new Stack_frame(fun, eventid, caller, chain, depth);

	int num_args = std::max(frame->num_args, givenargs);
	// Many functions have 'itemref' as a 'phantom' arg.
	// In the originals, this was probably so that the games
	// could know how much memory the function would need.
	// In any case, do this only if this was not an indirect call.
	if (givenargs == 0 && is_object_fun(funcid)) {
		if (--num_args < 0) {
			// Backwards compatibility with older mods.
			cerr << "Called usecode function " << hex << setfill('0') << funcid
				 << dec << setfill(' ');
			cerr << " with negative number of arguments." << endl
				 << "The mod/game was likely compiled with an outdated version "
					"of UCC"
				 << endl;
			num_args = 0;
		}
	}
#ifdef DEBUG
	const int added_args = num_args - oldstack;
#endif
	while (num_args > oldstack) {    // Not enough args pushed?
		pushi(0);                    // add zeroes
		oldstack++;
	}

	// Store args in first num_args locals
	int i;
	for (i = 0; i < num_args; i++) {
		const Usecode_value val         = pop();
		frame->locals[num_args - i - 1] = val;
	}

	// save stack pointer
	frame->save_sp = sp;

	// add new stack frame to top of stack
	call_stack.push_front(frame);

#ifdef DEBUG
	Usecode_symbol* fsym = symtbl ? (*symtbl)[funcid] : nullptr;
	cout << "Running usecode ";
	if (fsym) {
		cout << fsym->get_name() << " [";
	}
	cout << setw(4) << hex << setfill('0') << funcid << dec << setfill(' ');
	if (fsym) {
		cout << "]";
	}
	cout << " (";
	for (i = 0; i < num_args; i++) {
		if (i) {
			cout << ", ";
		}
		frame->locals[i].print(cout);
	}
	cout << ") with event " << eventid << ", depth " << frame->call_depth
		 << endl;
	if (added_args > 0) {
		cout << added_args << (added_args > 1 ? " args" : " arg")
			 << " had to be added to the stack for this call" << endl;
	}
#endif

	return true;
}

void Usecode_internal::previous_stack_frame() {
	// remove current frame from stack
	Stack_frame* frame = call_stack.front();
	call_stack.pop_front();

	// restore stack pointer
	sp = frame->save_sp;

	// Get rid of exception handler for the current function (say, due to return
	// in a try block).
	auto it = except_stack.find(frame);
	if (it != except_stack.end()) {
		except_stack.erase(it);
	}

	if (frame->call_depth == 0) {
		// this was the function called from 'the outside'
		// push a marker (nullptr) for the interpreter onto the call stack,
		// so it knows it has to return instead of continuing
		// further up the call stack
		call_stack.push_front(nullptr);
	}

	delete frame;
}

static ostream& print_usecode_function(
		Usecode_symbol_table* symtbl, const int function) {
	Usecode_symbol* fsym = symtbl ? (*symtbl)[function] : nullptr;
	if (fsym) {
		cout << fsym->get_name() << " (";
	}
	cout << hex << setw(4) << setfill('0') << function << dec << setfill(' ');
	if (fsym) {
		cout << ')';
	}
	return cout;
}

void Usecode_internal::return_from_function(Usecode_value& retval) {
#ifdef DEBUG
	// store old function ID for debugging output
	const int oldfunction = call_stack.front()->function->id;
#endif

	// back up a stack frame
	previous_stack_frame();

	// push the return value
	push(retval);

#ifdef DEBUG
	Stack_frame* parent_frame = call_stack.front();

	cout << "Returning (";
	retval.print(cout);
	cout << ") from usecode ";
	print_usecode_function(symtbl, oldfunction) << endl;

	if (parent_frame) {
		const int newfunction = call_stack.front()->function->id;
		cout << "...back into usecode ";
		print_usecode_function(symtbl, newfunction) << endl;
	}
#endif
}

void Usecode_internal::return_from_procedure() {
#ifdef DEBUG
	// store old function ID for debugging output
	const int oldfunction = call_stack.front()->function->id;
#endif

	// back up a stack frame
	previous_stack_frame();

#ifdef DEBUG
	Stack_frame* parent_frame = call_stack.front();

	cout << "Returning from usecode ";
	print_usecode_function(symtbl, oldfunction) << endl;

	if (parent_frame) {
		const int newfunction = call_stack.front()->function->id;
		cout << "...back into usecode ";
		print_usecode_function(symtbl, newfunction) << endl;
	}
#endif
}

void Usecode_internal::abort_function(Usecode_value& retval) {
#ifdef DEBUG
	const int functionid = call_stack.front()->function->id;

	cout << "Aborting from usecode " << hex << setw(4) << setfill('0')
		 << functionid << dec << setfill(' ') << endl;
#endif

	// clear the entire call stack up to either a catch or the entry point
	while (call_stack.front() != nullptr) {
		previous_stack_frame();
		Stack_frame* frame = call_stack.front();
		auto         it    = except_stack.find(frame);
		if (it != except_stack.end()) {
			const uint8* target = it->second;
#ifdef DEBUG
			const int functionid = frame->function->id;

			cout << "Abort caught in usecode " << hex << setw(4) << setfill('0')
				 << functionid << " at location " << setw(8) << setfill('0')
				 << target << dec << setfill(' ') << endl;
#endif
			except_stack.erase(it);
			frame->ip = target;
			// push the return value
			push(retval);
			break;
		}
	}
}

/*
 *  Append a string.
 */

void Usecode_internal::append_string(const char* str) {
	if (!str) {
		return;
	}
	// Figure new length.
	int len = String ? strlen(String) : 0;
	len += strlen(str);
	char* newstr = new char[len + 1];
	if (String) {
		strcpy(newstr, String);
		delete[] String;
		String = strcat(newstr, str);
	} else {
		String = strcpy(newstr, str);
	}
}

// Push/pop stack.
inline void Usecode_internal::push(const Usecode_value& val) {
	*sp++ = val;
}

inline Usecode_value Usecode_internal::pop() {
	if (sp <= stack) {
		// Happens in SI #0x939
		cerr << "Stack underflow on function ";
		print_usecode_function(symtbl, call_stack.front()->function->id);
		cerr << " at IP ";
		cout << hex << setw(4) << setfill('0') << (frame->ins_ip - frame->code)
			 << dec << setfill(' ') << std::endl;
		return Usecode_value(0);
	}

	// +++++SHARED:  Shouldn't we reset *sp.
	return *--sp;
}

inline Usecode_value Usecode_internal::peek() {
	return sp[-1];
}

inline void Usecode_internal::pushref(Game_object* obj) {
	const Usecode_value v(obj);
	push(v);
}

inline void Usecode_internal::pushref(Game_object_shared obj) {
	const Usecode_value v(std::move(obj));
	push(v);
}

inline void Usecode_internal::pushi(long val) {    // Push/pop integers.
	const Usecode_value v(val);
	push(v);
}

inline int Usecode_internal::popi() {
	const Usecode_value val = pop();
	return val.need_int_value();
}

// Push/pop strings.
inline void Usecode_internal::pushs(const char* s) {
	const Usecode_value val(s);
	push(val);
}

/*
 *  Get a game object from an "itemref", which might be the actual
 *  pointer, or might be -(npc number).
 *
 *  Output: ->game object.
 */

Game_object* Usecode_internal::get_item(const Usecode_value& itemref) {
	// If array, take 1st element.
	const Usecode_value& elemval = itemref.get_elem0();

	if (elemval.is_ptr()) {
		return elemval.get_ptr_value();
	}

	const long val = elemval.get_int_value();
	if (!val) {
		return nullptr;
	}
	Game_object* obj = nullptr;
	if (val == -356) {    // Avatar.
		return gwin->get_main_actor();
	} else if (val < -356 && val > -360) {    // Special cases.
		return nullptr;
	}
	if (val < 0 && val > -gwin->get_num_npcs()) {
		obj = gwin->get_npc(-val);
	} else if (val >= 0) {
		// Special case:  palace guards, Time Lord.
		if (val < 0x400 && !itemref.is_array() && caller_item
			&& ((GAME_BG && val == 0x269)
				|| val == caller_item->get_shapenum())) {
			obj = caller_item.get();
		} else {
			return nullptr;
		}
	}
	return obj;
}

/*
 *  Check for an actor.
 */

Actor* Usecode_internal::as_actor(Game_object* obj) {
	if (!obj) {
		return nullptr;
	}
	return obj->as_actor();
}

/*
 *  Get a position.
 */

Tile_coord Usecode_internal::get_position(Usecode_value& itemval) {
	Game_object* obj;    // An object?
	if ((itemval.get_array_size() == 1 || !itemval.get_array_size())
		&& (obj = get_item(itemval))) {
		return obj->get_outermost()->get_tile();
	} else if (itemval.get_array_size() == 3) {
		// An array of coords.?
		return Tile_coord(
				itemval.get_elem(0).get_int_value(),
				itemval.get_elem(1).get_int_value(),
				itemval.get_elem(2).get_int_value());
	} else if (itemval.get_array_size() == 4) {
		// Result of click_on_item() with
		//  array = (null, tx, ty, tz)?
		return Tile_coord(
				itemval.get_elem(1).get_int_value(),
				itemval.get_elem(2).get_int_value(),
				itemval.get_elem(3).get_int_value());
	} else {    // Else assume caller_item.
		return caller_item->get_tile();
	}
}

/*
 *  Make sure pending text has been seen.
 */

void Usecode_internal::show_pending_text() {
	if (book) {    // Book mode?
		int x;
		int y;
		while (book->show_next_page()
			   && Get_click(x, y, Mouse::hand, nullptr, false, book, true))
			;
		gwin->paint();
	}
	// Normal conversation:
	else if (conv->is_npc_text_pending()) {
		click_to_continue();
	}
}

/*
 *  Show book or scroll text.
 */

void Usecode_internal::show_book() {
	char* str = String;
	book->add_text(str);
	delete[] String;
	String = nullptr;
}

/*
 *  Say the current string and empty it.
 */

void Usecode_internal::say_string() {
	//  user_choice = 0;        // Clear user's response.
	if (!String) {
		return;
	}
	if (book) {    // Displaying a book?
		show_book();
		return;
	}
	show_pending_text();    // Make sure prev. text was seen.
	char* str = String;
	while (*str) {            // Look for stopping points ("~~").
		if (*str == '*') {    // Just gets an extra click.
			click_to_continue();
			str++;
			continue;
		}
		char* eol = strchr(str, '~');
		if (!eol) {    // Not found?
			conv->show_npc_message(str);
			click_to_continue();
			break;
		}
		*eol = 0;
		conv->show_npc_message(str);
		click_to_continue();
		str = eol + 1;
		if (*str == '~') {
			str++;    // 2 in a row.
		}
	}
	delete[] String;
	String = nullptr;
}

/*
 *  Gets the face for an NPC.
 */
int Usecode_internal::get_face_shape(
		Usecode_value& arg1, Actor*& npc, int& frame) {
	Conversation::noface = false;
	npc       = as_actor(get_item(arg1));
	int shape = -1;
	if (arg1.is_int()) {
		shape = std::abs(arg1.get_int_value());
		if (shape == 356) {    // Avatar.
			shape = 0;
		}
	} else if (npc) {
		shape = npc->get_face_shapenum();
	}

	if (shape < 0) {    // No need to do anything else.
		return shape;
	}

	// Checks for Petra flag.
	shape = Shapeinfo_lookup::GetFaceReplacement(shape);

	if (Game::get_game_type() == SERPENT_ISLE) {
		// Special case: Nightmare Smith.
		//   (But don't mess up Guardian.)
		Actor* iact;
		if (shape == 296 && this->frame->caller_item
			&& (iact = this->frame->caller_item->as_actor()) != nullptr
			&& iact->get_npc_num() == 277) {
			// we set a face but we are not displaying it
			shape = 277;
			Conversation::noface = true;
		}
	}

	// Another special case: map face shape 0 to
	// the avatar's correct face shape and frame:
	if (shape == 0) {
		Actor*     ava      = gwin->get_main_actor();
		const bool sishapes = Shape_manager::get_instance()->have_si_shapes();
		Skin_data* skin     = Shapeinfo_lookup::GetSkinInfoSafe(
                ava->get_skin_color(),
                npc ? (npc->get_type_flag(Actor::tf_sex))
						: (ava->get_type_flag(Actor::tf_sex)),
                sishapes);
		if (gwin->get_main_actor()->get_flag(Obj_flags::tattooed)) {
			shape = skin->alter_face_shape;
			frame = skin->alter_face_frame;
		} else {
			shape = skin->face_shape;
			frame = skin->face_frame;
		}
	}
	return shape;
}

/*
 *  Show an NPC's face.
 */

void Usecode_internal::show_npc_face(
		Usecode_value& arg1,    // Shape (NPC #).
		Usecode_value& arg2,    // Frame.
		int            slot     // 0, 1, or -1 to find free spot.
) {
	show_pending_text();
	Actor*    npc;
	int       frame = arg2.get_int_value();
	const int shape = get_face_shape(arg1, npc, frame);

	if (shape < 0) {
		return;
	}

	if (Game::get_game_type() == BLACK_GATE && npc) {
		// Only do this if the NPC is the caller item.
		if (npc->get_npc_num() != -1) {
			npc->set_flag(Obj_flags::met);
		}
	}

	if (!conv->get_num_faces_on_screen()) {
		gwin->get_effects()->remove_text_effects();
	}
	// Only non persistent
	if (gumpman->showing_gumps(true)) {
		gumpman->close_all_gumps();
		gwin->set_all_dirty();
		init_conversation();    // jsf-Added 4/20/01 for SI-Lydia.
	}
	gwin->paint_dirty();
	conv->show_face(shape, frame, slot);
	//	user_choice = 0;     // Seems like a good idea.
	// Also seems to create a conversation bug in Test of Love :-(
}

/*
 *  Remove an NPC's face.
 */

void Usecode_internal::remove_npc_face(Usecode_value& arg1    // Shape (NPC #).
) {
	show_pending_text();
	Actor*    npc;
	const int shape = get_face_shape(arg1, npc);
	if (shape < 0) {
		return;
	}
	conv->remove_face(shape);
}

/*
 *  Set an item's shape.
 */

void Usecode_internal::set_item_shape(
		Usecode_value& item_arg, Usecode_value& shape_arg) {
	const int    shape = shape_arg.get_int_value();
	Game_object* item  = get_item(item_arg);
	if (!item) {
		return;
	}
	// See if light turned on/off.
	const bool light_changed = item->get_info().is_light_source()
							   != ShapeID::get_info(shape).is_light_source();
	if (item->get_owner()) {    // Inside something?
		item->get_owner()->change_member_shape(item, shape);
		if (light_changed) {    // Maybe we should repaint all.
			gwin->paint();      // Repaint finds all lights.
		} else {
			Gump* gump = gumpman->find_gump(item);
			if (gump) {
				gump->paint();
			}
		}
		return;
	}
	gwin->add_dirty(item);
	// Get chunk it's in.
	Map_chunk* chunk = item->get_chunk();
	if (!chunk) {
		CERR("Object " << item
					   << " not in chunk and not owned by another object");
		item->set_shape(shape);
		return;
	}
	const Game_object_shared keep = item->shared_from_this();
	chunk->remove(item);    // Remove and add to update cache.
	item->set_shape(shape);
	chunk->add(item);
	gwin->add_dirty(item);
	//	rect = gwin->get_shape_rect(item).add(rect);
	//	rect.enlarge(8);
	//	rect = gwin->clip_to_win(rect);
	if (light_changed) {
		gwin->paint();    // Complete repaint refigures lights.
	}
	//	else
	//		gwin->paint(rect);  // Not sure...
	//	gwin->show();            // Not sure if this is needed.
}

/*
 *  Set an item's frame.
 */

void Usecode_internal::set_item_frame(
		Game_object* item, int frame,
		int check_empty,    // If 1, don't set empty frame.
		int set_rotated     // Set 'rotated' bit to one in 'frame'.
) {
	if (!item) {
		return;
	}
	// Added 9/16/2001:
	if (!set_rotated) {    // Leave bit alone?
		frame = (item->get_framenum() & 32) | (frame & 31);
	}
	if (frame == item->get_framenum()) {
		return;    // Already set to that.
	}
	Actor* act = as_actor(item);
	// Actors have frame replacements for empty frames:
	if (act) {
		act->change_frame(frame);
	} else {
		// Check for empty frame.
		const ShapeID sid(item->get_shapenum(), frame, item->get_shapefile());
		Shape_frame*  shape = sid.get_shape();
		if (!shape || (check_empty && shape->is_empty())) {
			return;
		}
		// cout << "Set_item_frame: " << item->get_shapenum()
		//              << ", " << frame << endl;
		// (Don't mess up rotated frames.)
		if ((frame & 0xf) < item->get_num_frames()) {
			item->change_frame(frame);
		}
	}
	gwin->set_painted();    // Make sure paint gets done.
}

/*
 *  Set to repaint an object.
 */

void Usecode_internal::add_dirty(Game_object* obj) {
	gwin->add_dirty(obj);
}

/*
 *  Remove an item from the world.
 */

void Usecode_internal::remove_item(Game_object* obj) {
	if (!obj) {
		return;
	}
	if (!last_created.empty() && obj == last_created.back().get()) {
		last_created.pop_back();
	}
	add_dirty(obj);
	if (GAME_SI && frame && frame->function->id == 0x70e
		&& obj->get_shapenum() == 0x113 && obj->get_quality() == 0xd) {
		// Hack to fix broken trap switch in SI temple of Discipline.
		// This works better than the original, in that the trap will stay
		// disabled if you go away and come back, instead of returning and being
		// impossible to disarm.
		Egg_vector vec;    // Gets list.
		// Same parameters used in the egg to activate the trap.
		if (obj->find_nearby_eggs(vec, 0xc8, 0xf)) {
			for (auto* egg : vec) {
				egg->remove_this(nullptr);
			}
		}
	}
	Game_object_shared keep;
	obj->remove_this(obj->as_actor() ? &keep : nullptr);
}

/*
 *  Return an array containing the party, with the Avatar first.
 */

Usecode_value Usecode_internal::get_party() {
	const int     cnt = partyman->get_count();
	Usecode_value arr(1 + cnt, nullptr);
	// Add avatar.
	Usecode_value aval(gwin->get_main_actor());
	arr.put_elem(0, aval);
	int num_added = 1;
	for (int i = 0; i < cnt; i++) {
		Game_object* obj = gwin->get_npc(partyman->get_member(i));
		if (!obj) {
			continue;
		}
		Usecode_value val(obj);
		arr.put_elem(num_added++, val);
	}
	// cout << "Party:  "; arr.print(cout); cout << endl;
	return arr;
}

/*
 *  Put text near an item.
 */

void Usecode_internal::item_say(Usecode_value& objval, Usecode_value& strval) {
	Game_object* obj = get_item(objval);
	const char*  str = strval.get_str_value();
	if (obj && str && *str) {
		Effects_manager* eman = gwin->get_effects();
		// Added Nov01,01 to fix 'locate':
		eman->remove_text_effect(obj);
		if (gwin->failed_copy_protection()) {
			str = "Oink!";
		}
		eman->add_text(str, obj);
	}
}

/*
 *  Activate all cached-in usecode eggs near a given spot.
 */

void Usecode_internal::activate_cached(const Tile_coord& pos) {
	if (Game::get_game_type() != BLACK_GATE) {
		return;    // ++++Since we're not sure about it.
	}
	const int  dist = 16;
	Egg_vector vec;    // Find all usecode eggs.
	Game_object::find_nearby_eggs(vec, pos, 275, dist, c_any_qual, 7);
	for (auto* egg : vec) {
		if (egg->get_criteria() == Egg_object::cached_in) {
			egg->activate();
		}
	}
}

/*
 *  For sorting up-to-down, right-to-left, and near-to-far:
 */
class Object_reverse_sorter {
public:
	bool operator()(const Game_object* o1, const Game_object* o2) {
		const Tile_coord t1 = o1->get_tile();
		const Tile_coord t2 = o2->get_tile();
		if (t1.ty > t2.ty) {
			return true;
		} else if (t1.ty == t2.ty) {
			if (t1.tx > t2.tx) {
				return true;
			} else {
				return t1.tx == t2.tx && t1.tz > t2.tz;
			}
		} else {
			return false;
		}
	}
};

/*
 *  Return an array of nearby objects.
 */

Usecode_value Usecode_internal::find_nearby(
		Usecode_value& objval,      // Find them near this.
		Usecode_value& shapeval,    // Shape to find, or -1 for any,
		//  c_any_shapenum for any npc.
		Usecode_value& distval,    // Distance in tiles?
		Usecode_value& mval        // Some kind of mask?  Guessing:
								   //   4 == party members only.
								   //   8 == non-party NPC's only.
								   //  16 == something with eggs???
								   //  32 == monsters? invisible?
) {
	Game_object_vector vec;    // Gets list.

	int shapenum;

	if (shapeval.is_array()) {
		// fixes 'lightning whip sacrifice' in Silver Seed
		shapenum = shapeval.get_elem(0).get_int_value();
		if (shapeval.get_array_size() > 1) {
			cerr << "Calling find_nearby with an array > 1 !!!!" << endl;
		}
	} else {
		shapenum = shapeval.get_int_value();
	}

	// It might be (tx, ty, tz).
	const int arraysize = objval.get_array_size();
	if (arraysize == 4) {    // Passed result of click_on_item.
		Game_object::find_nearby(
				vec,
				Tile_coord(
						objval.get_elem(1).get_int_value(),
						objval.get_elem(2).get_int_value(),
						objval.get_elem(3).get_int_value()),
				shapenum, distval.get_int_value(), mval.get_int_value());
	} else if (arraysize == 3 || arraysize == 5) {
		// Coords(x,y,z) [qual, frame]
		// Qual is 4th if there.
		const int qual = arraysize == 5 ? objval.get_elem(3).get_int_value()
										: c_any_qual;
		// Frame is 5th if there.
		const int frnum = arraysize == 5 ? objval.get_elem(4).get_int_value()
										 : c_any_framenum;
		Game_object::find_nearby(
				vec,
				Tile_coord(
						objval.get_elem(0).get_int_value(),
						objval.get_elem(1).get_int_value(),
						objval.get_elem(2).get_int_value()),
				shapenum, distval.get_int_value(), mval.get_int_value(), qual,
				frnum);
	} else {
		Game_object* obj = get_item(objval);
		if (!obj) {
			return Usecode_value(0, nullptr);
		}
		obj = obj->get_outermost();    // Might be inside something.
		obj->find_nearby(
				vec, shapenum, distval.get_int_value(), mval.get_int_value());
	}
	if (vec.size() > 1) {    // Sort right-left, near-far to fix
		//   SI/SS cask bug.
		std::sort(vec.begin(), vec.end(), Object_reverse_sorter());
	}
	Usecode_value nearby(vec.size(), nullptr);    // Create return array.
	int           i = 0;
	for (auto* each : vec) {
		Usecode_value val(each);
		nearby.put_elem(i++, val);
	}
	return nearby;
}

/*
 *  Look for a barge that an object is a part of, or on, using the same
 *  sort (right-left, front-back) as ::find_nearby().  If there are more
 *  than one beneath 'obj', the highest is returned.
 *
 *  Output: ->barge if found, else 0.
 */

Barge_object* Get_barge(Game_object* obj) {
	// Check object itself.
	Barge_object* barge = obj->as_barge();
	if (barge) {
		return barge;
	}
	Game_object_vector vec;    // Find it within 20 tiles (egglike).
	obj->find_nearby(vec, 961, 20, 0x10);
	if (vec.size() > 1) {    // Sort right-left, near-far.
		std::sort(vec.begin(), vec.end(), Object_reverse_sorter());
	}
	// Object must be inside it.
	const Tile_coord pos  = obj->get_tile();
	Barge_object*    best = nullptr;
	for (auto* it : vec) {
		barge = it->as_barge();
		if (barge
			&& barge->get_tile_footprint().has_world_point(pos.tx, pos.ty)) {
			const int lift = barge->get_lift();
			if (!best ||    // First qualifying?
							// First beneath obj.?
				(best->get_lift() > pos.tz && lift <= pos.tz) ||
				// Highest beneath?
				(lift <= pos.tz && lift > best->get_lift())) {
				best = barge;
			}
		}
	}
	return best;
}

/*
 *  Return object of given shape nearest given obj.
 */

Usecode_value Usecode_internal::find_nearest(
		Usecode_value& objval,      // Find near this.
		Usecode_value& shapeval,    // Shape to find
		Usecode_value& distval      // Guessing it's distance.
) {
	Game_object* obj = get_item(objval);
	if (!obj) {
		return Usecode_value(static_cast<Game_object*>(nullptr));
	}
	Game_object_vector vec;                    // Gets list.
	obj             = obj->get_outermost();    // Might be inside something.
	int       dist  = distval.get_int_value();
	const int shnum = shapeval.get_int_value();
	// Kludge for Test of Courage:
	if (frame->function->id == 0x70a && shnum == 0x9a && dist == 0) {
		dist = 16;    // Mage may have wandered.
	}
	obj->find_nearby(vec, shnum, dist, 0);
	Game_object*     closest  = nullptr;
	uint32           bestdist = 100000;    // Distance-squared in tiles.
	const Tile_coord t1       = obj->get_tile();
	for (auto* each : vec) {
		const Tile_coord t2   = each->get_tile();
		const int        dx   = t1.tx - t2.tx;
		const int        dy   = t1.ty - t2.ty;
		const int        dz   = t1.tz - t2.tz;
		const uint32     dist = dx * dx + dy * dy + dz * dz;
		if (dist < bestdist) {
			bestdist = dist;
			closest  = each;
		}
	}
	return Usecode_value(closest);
}

/*
 *  Find the angle (0-7) from one object to another.
 */

Usecode_value Usecode_internal::find_direction(
		Usecode_value& from, Usecode_value& to) {
	unsigned         angle;    // Gets angle 0-7 (north - northwest)
	const Tile_coord t1 = get_position(from);
	const Tile_coord t2 = get_position(to);
	// Treat as cartesian coords.
	angle = static_cast<int>(Get_direction(t1.ty - t2.ty, t2.tx - t1.tx));
	return Usecode_value(angle);
}

/*
 *  Count objects of a given shape in a container, or in the whole party.
 */

Usecode_value Usecode_internal::count_objects(
		Usecode_value& objval,    // The container, or -357 for party.
		Usecode_value&
				shapeval,    // Object shape to count (c_any_shapenum=any).
		Usecode_value& qualval,    // Quality (c_any_qual=any).
		Usecode_value& frameval    // Frame (c_any_framenum=any).
) {
	const long oval     = objval.get_int_value();
	const int  shapenum = shapeval.get_int_value();
	const int  qualnum  = qualval.get_int_value();
	const int  framenum = frameval.get_int_value();
	if (oval != -357) {
		Game_object* obj = get_item(objval);
		return Usecode_value(
				!obj ? 0 : obj->count_objects(shapenum, qualnum, framenum));
	}
	// Look through whole party.
	Usecode_value party = get_party();
	const int     cnt   = party.get_array_size();
	int           total = 0;
	for (int i = 0; i < cnt; i++) {
		Game_object* obj = get_item(party.get_elem(i));
		if (obj) {
			total += obj->count_objects(shapenum, qualnum, framenum);
		}
	}
	return Usecode_value(total);
}

/*
 *  Get objects of a given shape in a container.
 */

Usecode_value Usecode_internal::get_objects(
		Usecode_value& objval,    // The container.
		Usecode_value&
				shapeval,    // Object shape to get or c_any_shapenum for any.
		Usecode_value& qualval,    // Quality (c_any_qual=any).
		Usecode_value& frameval    // Frame (c_any_framenum=any).
) {
	Game_object* obj = get_item(objval);
	if (!obj) {
		return Usecode_value(static_cast<Game_object*>(nullptr));
	}
	const int          shapenum = shapeval.get_int_value();
	const int          framenum = frameval.get_int_value();
	const int          qual     = qualval.get_int_value();
	Game_object_vector vec;    // Gets list.
	obj->get_objects(vec, shapenum, qual, framenum);

	//	cout << "Container objects found:  " << cnt << << endl;
	Usecode_value within(vec.size(), nullptr);    // Create return array.
	int           i = 0;
	for (auto* each : vec) {
		Usecode_value val(each);
		within.put_elem(i++, val);
	}
	return within;
}

/*
 *  Remove a quantity of an item from the party.
 *
 *  Output: 1 (or the object) if successful, else 0.
 */

Usecode_value Usecode_internal::remove_party_items(
		Usecode_value& quantval,    // Quantity to remove.
		Usecode_value& shapeval,    // Shape.
		Usecode_value& qualval,     // Quality??
		Usecode_value& frameval,    // Frame.
		Usecode_value& flagval      // Flag??
) {
	ignore_unused_variable_warning(flagval);
	int                 quantity = quantval.need_int_value();
	const int           shapenum = shapeval.get_int_value();
	const int           framenum = frameval.get_int_value();
	const int           quality  = qualval.get_int_value();
	Usecode_value       party    = get_party();
	const int           cnt      = party.get_array_size();
	Usecode_value       all(-357);    // See if they exist.
	const Usecode_value avail = count_objects(all, shapeval, qualval, frameval);
	// Verified. Originally SI-only, allowing for BG too.
	if (quantity == c_any_quantity) {
		quantity = avail.get_int_value();
	} else if (avail.get_int_value() < quantity) {
		return Usecode_value(0);
	}
	const int orig_cnt = quantity;
	// Look through whole party.
	for (int i = 0; i < cnt && quantity > 0; i++) {
		Game_object* obj = get_item(party.get_elem(i));
		if (obj) {
			quantity = obj->remove_quantity(
					quantity, shapenum, quality, framenum);
		}
	}
	return Usecode_value(quantity != orig_cnt);
}

/*
 *  Add a quantity of an item to the party.
 *
 *  Output: List of members who got objects.
 */

Usecode_value Usecode_internal::add_party_items(
		Usecode_value& quantval,    // Quantity to add.
		Usecode_value& shapeval,    // Shape.
		Usecode_value& qualval,     // Quality.
		Usecode_value& frameval,    // Frame.
		Usecode_value& temporary    // If the objects are to be temporary or not
) {
	int quantity = quantval.get_int_value();
	// ++++++First see if there's room.
	const int shapenum = shapeval.get_int_value();
	int       framenum = frameval.get_int_value();
	const int quality  = qualval.get_int_value();
	// Note: the temporary flag only applies to items placed on the
	// ground in SI.
	const bool temp = temporary.get_int_value() != 0;
	// Look through whole party.
	Usecode_value party = get_party();
	const int     cnt   = party.get_array_size();
	Usecode_value result(0, nullptr);    // Start with empty array.
	for (int i = 0; i < cnt && quantity > 0; i++) {
		Game_object* obj = get_item(party.get_elem(i));
		if (!obj) {
			continue;
		}
		const int prev = quantity;
		quantity       = obj->add_quantity(
                quantity, shapenum, quality, framenum, false, GAME_BG && temp);
		if (quantity < prev) {    // Added to this NPC.
			result.concat(party.get_elem(i));
		}
	}
	if (GAME_BG || GAME_SIB) {    // Black gate or Beta SI?  Just return result.
		return result;
	}
	int todo = quantity;    // SI:  Put remaining on the ground.
	if (framenum == c_any_framenum) {
		framenum = 0;
	}
	while (todo > 0) {
		const Tile_coord pos = Map_chunk::find_spot(
				gwin->get_main_actor()->get_tile(), 3, shapenum, framenum, 2);
		if (pos.tx == -1) {    // Hope this rarely happens.
			break;
		}
		const Shape_info& info = ShapeID::get_info(shapenum);
		// Create and place.
		const Game_object_shared newobj
				= gmap->create_ireg_object(info, shapenum, framenum, 0, 0, 0);
		if (quality != c_any_qual) {
			newobj->set_quality(quality);    // set quality
		}
		newobj->set_flag(Obj_flags::okay_to_take);
		if (temp) {    // Mark as temporary.
			newobj->set_flag(Obj_flags::is_temporary);
		}
		newobj->move(pos);
		todo--;
		if (todo > 0) {    // Create quantity if possible.
			todo = newobj->modify_quantity(todo);
		}
	}
	// SI?  Append # left on ground.
	Usecode_value ground(quantity - todo);
	result.concat(ground);
	return result;
}

/*
 *  Add a quantity of an item to a container
 *
 *  Output: Num created
 */

Usecode_value Usecode_internal::add_cont_items(
		Usecode_value& container,    // What do we add to
		Usecode_value& quantval,     // Quantity to add.
		Usecode_value& shapeval,     // Shape.
		Usecode_value& qualval,      // Quality.
		Usecode_value& frameval,     // Frame.
		Usecode_value& temporary    // If the objects are to be temporary or not
) {
	const int  quantity = quantval.get_int_value();
	const int  shapenum = shapeval.get_int_value();
	const int  framenum = frameval.get_int_value();
	int        quality  = qualval.get_int_value();
	const bool temp     = temporary.get_int_value() != 0;
	// e.g., Knight's Test wolf meat.
	if (quality == -359) {
		quality = 0;
	}

	Game_object* obj = get_item(container);
	if (obj) {
		// This fixes teleport storm in SI Beta.
		const int numleft = obj->add_quantity(
				quantity, shapenum, quality, framenum, false, temp);
		if (GAME_SIB) {
			return Usecode_value(quantity - numleft);
		} else {
			return Usecode_value(numleft);
		}
	}
	return Usecode_value(0);
}

/*
 *  Remove a quantity of an item to a container
 *
 *  Output: Num removed
 */

Usecode_value Usecode_internal::remove_cont_items(
		Usecode_value& container,    // What do we add to
		Usecode_value& quantval,     // Quantity to add.
		Usecode_value& shapeval,     // Shape.
		Usecode_value& qualval,      // Quality.
		Usecode_value& frameval,     // Frame.
		Usecode_value& flagval       // Flag??
) {
	Game_object* obj = get_item(container);
	if (!obj) {
		if (container.get_int_value() == -357) {
			return remove_party_items(
					quantval, shapeval, qualval, frameval, flagval);
		}
		return Usecode_value(0);
	}

	int       quantity = quantval.get_int_value();
	const int shapenum = shapeval.get_int_value();
	const int framenum = frameval.get_int_value();
	auto      quality  = static_cast<unsigned int>(qualval.get_int_value());

	if (quantity == c_any_quantity) {
		quantval = count_objects(container, shapeval, qualval, frameval);
		quantity = quantval.get_int_value();
	}

	return Usecode_value(
			quantity
			- obj->remove_quantity(quantity, shapenum, quality, framenum));
}

/*
 *  Create a new object and push it onto the last_created stack.
 */

Game_object_shared Usecode_internal::create_object(
		int  shapenum,
		bool equip    // Equip monsters.
) {
	Game_object_shared obj;    // Create to be written to Ireg.
	const Shape_info&  info = ShapeID::get_info(shapenum);
	modified_map            = true;
	// +++Not sure if 1st test is needed.
	if (info.get_monster_info() || info.is_npc()) {
		// (Wait sched. added for FOV.)
		// don't add equipment (Erethian's transform sequence)
		Game_object_shared new_monster = Monster_actor::create(
				shapenum, Tile_coord(-1, -1, -1), Schedule::wait,
				static_cast<int>(Actor::neutral), true, equip);
		auto* monster = static_cast<Monster_actor*>(new_monster.get());
		// FORCE it to be neutral (dec04,01).
		monster->set_alignment(static_cast<int>(Actor::neutral));
		gwin->add_dirty(monster);
		gwin->add_nearby_npc(monster);
		gwin->show();
		last_created.push_back(monster->shared_from_this());
		return new_monster;
	} else {
		if (info.is_body_shape()) {
			obj = std::make_shared<Dead_body>(shapenum, 0, 0, 0, 0, -1);
		} else {
			obj = gmap->create_ireg_object(shapenum, 0);
			// Be liberal about taking stuff.
			obj->set_flag(Obj_flags::okay_to_take);
		}
	}
	obj->set_invalid();    // Not in world yet.
	obj->set_flag(Obj_flags::okay_to_take);
	last_created.push_back(obj->shared_from_this());
	return obj;
}

/*
 *  Have an NPC walk somewhere and then execute usecode.
 *
 *  Output: true if successful, else false.
 */

bool Usecode_internal::path_run_usecode(
		Usecode_value& npcval,       // # or ref.
		Usecode_value& locval,       // Where to walk to.
		Usecode_value& useval,       // Usecode #.
		Usecode_value& itemval,      // Use as itemref in Usecode fun.
		Usecode_value& eventval,     // Eventid.
		bool           find_free,    // Not sure.  For SI.
		bool           always,       // Always run function, even if failed.
		bool           companions    // For SI:  companions should follow.
) {
	Actor* npc = as_actor(get_item(npcval));
	if (!npc) {
		return false;
	}
	path_npc            = npc;
	const int    usefun = useval.get_elem0().get_int_value();
	Game_object* obj    = get_item(itemval);
	const int    sz     = locval.get_array_size();
	if (!npc || sz < 2) {
		CERR("Path_run_usecode: bad inputs");
		return false;
	}
	const Tile_coord src = npc->get_tile();
	Tile_coord       dest(
            locval.get_elem(0).get_int_value(),
            locval.get_elem(1).get_int_value(),
            sz == 3 ? locval.get_elem(2).get_int_value() : 0);
	if (dest.tz < 0) {    // ++++Don't understand this.
		dest.tz = 0;
	}
	if (find_free) {
		/* Now works with SI lightning platform */
		// Allow rise of 3 (for SI lightning).
		Tile_coord d = Map_chunk::find_spot(dest, 3, npc, 3);
		if (d.tx == -1) {    // No?  Try at source level.
			d = Map_chunk::find_spot(
					Tile_coord(dest.tx, dest.ty, src.tz), 3, npc, 0);
		}
		if (d.tx != -1) {    // Found it?
			dest = d;
		}
		if (usefun == 0x60a &&    // ++++Added 7/21/01 to fix Iron
			src.distance(dest) <= 1) {
			return true;    // Maiden loop in SI.  Kludge+++++++
		}
	}
	if (!obj) {    // Just skip the usecode part.
		const int res = npc->walk_path_to_tile(dest, gwin->get_std_delay(), 0);
		if (res && companions && npc->get_action()) {
			npc->get_action()->set_get_party();
		}
		return res;
	}
	// Walk there and execute.
	auto* action = new If_else_path_actor_action(
			npc, dest,
			new Usecode_actor_action(usefun, obj, eventval.get_int_value()));
	if (companions) {
		action->set_get_party();
	}
	if (always) {    // Set failure to same thing.
		// Note: si_path_run_usecode always uses event 14 for this. Even when
		// it is overridden by UI_set_path_failure. This causes a few bugs in
		// SI usecode because the script developers assumed that the failure
		// event could be overriden by UI_set_path_failure. Most of the time,
		// they just use this value anyway so nothing is lost.
		action->set_failure(new Usecode_actor_action(
				usefun, obj, Usecode_machine::si_path_fail));
	}
	npc->set_action(action);    // Get into time queue.
	npc->start(gwin->get_std_delay(), 0);
	return !action->done_and_failed();
}

/*
 *  See if an actor can go to a given location.
 */

bool Usecode_internal::is_dest_reachable(Actor* npc, const Tile_coord& dest) {
	if (dest.tz < 0) {
		return false;
	}
	if (npc->distance(dest) <= 1) {    // Already OK.
		return true;
	}
	Path_walking_actor_action action(nullptr, 6);
	return action.walk_to_tile(npc, npc->get_tile(), dest, 1) != nullptr;
}

/*
 *  Schedule a script.
 */

void Usecode_internal::create_script(
		Usecode_value& objval, Usecode_value& codeval,
		long delay    // Delay from current time.
) {
	Game_object* obj = get_item(objval);
	// Pure kludge for SI wells:
	if (objval.get_array_size() == 2 && Game::get_game_type() == SERPENT_ISLE
		&& obj && obj->get_shapenum() == 470 && obj->get_lift() == 0) {
		// We want the TOP of the well.
		Usecode_value& v2 = objval.get_elem(1);
		Game_object*   o2 = get_item(v2);
		if (o2->get_shapenum() == obj->get_shapenum() && o2->get_lift() == 2) {
			objval = v2;
			obj    = o2;
		}
	}
	if (!obj) {
		cerr << "Can't create script for nullptr object" << endl;
		return;
	}
	auto* code = new Usecode_value();
	code->steal_array(codeval);    // codeval is undefined after this.
	auto* script = new Usecode_script(obj, code);
	script->start(delay);
}

#ifdef DEBUG
/*
 *  Report unhandled intrinsic.
 */

static void Usecode_Trace(
		const char* name, int intrinsic, int num_parms,
		Usecode_value parms[12]) {
	const boost::io::ios_flags_saver flags(cout);
	const boost::io::ios_fill_saver  fill(cout);
	cout << hex << "    [0x" << setfill('0') << setw(2) << intrinsic
		 << "]: " << name << "(";
	for (int i = 0; i < num_parms; i++) {
		parms[i].print(cout);
		if (i != num_parms - 1) {
			cout << ", ";
		}
	}
	cout << ") = ";
}

static void Usecode_TraceReturn(Usecode_value& v) {
	v.print(cout);
	cout << dec << endl;
}

#endif

Usecode_value no_ret;

Usecode_value Usecode_internal::Execute_Intrinsic(
		UsecodeIntrinsicFn func, const char* name, int intrinsic, int num_parms,
		Usecode_value parms[12]) {
#ifdef XWIN
#	ifdef USECODE_DEBUGGER
	if (usecode_debugging) {
		// Examine the list of intrinsics for function breakpoints.
		if (std::find(
					intrinsic_breakpoints.begin(), intrinsic_breakpoints.end(),
					intrinsic)
			!= intrinsic_breakpoints.end()) {
			raise(SIGIO);    // Breakpoint
		}
	}
#	endif
#endif
#ifndef DEBUG
	ignore_unused_variable_warning(name, intrinsic);
#else
	if (intrinsic_trace) {
		Usecode_Trace(name, intrinsic, num_parms, parms);
		cout.flush();
		Usecode_value u = (this->*func)(num_parms, parms);
		Usecode_TraceReturn(u);
		return u;
	}
#endif
	return (this->*func)(num_parms, parms);
}

using UsecodeIntrinsicFn = Usecode_value (Usecode_internal::*)(
		int num_parms, Usecode_value parms[12]);

// missing from mingw32 header files, so included manually
#ifndef TO_STRING
#	if defined __STDC__ && __STDC__
#		define TO_STRING(x) #x
#	else
#		define TO_STRING(x) "x"
#	endif
#endif

#define USECODE_INTRINSIC_PTR(NAME) \
	{&Usecode_internal::UI_##NAME, TO_STRING(NAME)}

// Black Gate Intrinsic Function Table
Usecode_internal::IntrinsicTableEntry Usecode_internal::intrinsics_bg[] = {
#include "bgintrinsics.h"
};

// Serpent Isle Intrinsic Function Table
Usecode_internal::IntrinsicTableEntry Usecode_internal::intrinsics_si[] = {
#include "siintrinsics.h"
};

// Serpent Isle Beta Intrinsic Function Table
Usecode_internal::IntrinsicTableEntry Usecode_internal::intrinsics_sib[] = {
#include "sibetaintrinsics.h"
};

/*
 *  Call an intrinsic function.
 */

Usecode_value Usecode_internal::call_intrinsic(
		int intrinsic,    // The ID.
		int num_parms     // # parms on stack.
) {
	static_assert(
			std::size(intrinsics_bg) <= std::numeric_limits<uint16>::max());
	static_assert(
			std::size(intrinsics_si) <= std::numeric_limits<uint16>::max());
	static_assert(
			std::size(intrinsics_sib) <= std::numeric_limits<uint16>::max());
	Usecode_value parms[13];    // Get parms.
	for (int i = 0; i < num_parms; i++) {
		const Usecode_value val = pop();
		parms[i]                = val;
	}
	tcb::span<Usecode_internal::IntrinsicTableEntry> table;
	if (Game::get_game_type() == SERPENT_ISLE) {
		if (Game::is_si_beta()) {
			table = intrinsics_sib;
		} else {
			table = intrinsics_si;
		}
	} else {
		table = intrinsics_bg;
	}
	if (static_cast<size_t>(intrinsic) <= table.size()) {
		auto&                    table_entry = table[intrinsic];
		const UsecodeIntrinsicFn func        = table_entry.func;
		const char*              name        = table_entry.name;
		return Execute_Intrinsic(func, name, intrinsic, num_parms, parms);
	}
	return no_ret;
}

/*
 *  Wait for user to click inside a conversation.
 */

void Usecode_internal::click_to_continue() {
	if (!gwin->get_pal()->is_faded_out()) {    // If black screen, skip!
		int  xx;
		int  yy;
		char c;
		gwin->paint();    // Repaint scenery.
		Get_click(xx, yy, Mouse::hand, &c, false, conv, true);
	}
	conv->clear_text_pending();
	//  user_choice = 0;        // Clear it.
}

/*
 *  Set book/scroll to display.
 */

void Usecode_internal::set_book(Text_gump* b    // Book/scroll.
) {
	if (touchui != nullptr) {
		Gump_manager* gumpman = gwin->get_gump_man();
		if ((book && !b) && !gumpman->gump_mode()) {
			touchui->showGameControls();
		} else if (!book && b) {
			touchui->hideGameControls();
		}
	}
	delete book;
	book = b;
}

/*
 *  Get user's choice from among the possible responses.
 *
 *  Output: ->user choice string.
 *      0 if no possible choices or user quit.
 */

const char* Usecode_internal::get_user_choice() {
	if (!conv->get_num_answers()) {
		return nullptr;    // This does happen (Emps-honey).
	}

	//  if (!user_choice)       // May have already been done.
	// (breaks conversation with Cyclops on Dagger Isle ('foul magic' option))

	get_user_choice_num();
	return user_choice;
}

/*
 *  Get user's choice from among the possible responses.
 *
 *  Output: User choice is set, with choice # returned.
 *      -1 if no possible choices.
 */

int Usecode_internal::get_user_choice_num() {
	delete[] user_choice;
	user_choice = nullptr;
	conv->show_avatar_choices();
	int x;
	int y;    // Get click.
	int choice_num;
	do {
		char chr;         // Allow '1', '2', etc.
		gwin->paint();    // Paint scenery.
		const bool result
				= Get_click(x, y, Mouse::hand, &chr, false, conv, true);
		if (!result) {    // ESC pressed, select 'bye' if poss.
			choice_num = conv->locate_answer("bye");
		} else if (chr) {       // key pressed
			choice_num = -1;    // invalid key
			if (std::isalnum(static_cast<unsigned char>(chr))) {
				constexpr static const char optionKeys[]
						= "123456789abcdefghijklmnopqrstuvwxyz";
				const auto* it = std::find(
						std::cbegin(optionKeys), std::cend(optionKeys), chr);
				auto dist = std::distance(std::cbegin(optionKeys), it);
				if (it != std::cend(optionKeys)
					&& dist < conv->get_num_answers()) {
					choice_num = dist;
				}
			}
		} else {
			choice_num = conv->conversation_choice(x, y);
		}
	}
	// Wait for valid choice.
	while (choice_num < 0 || choice_num >= conv->get_num_answers());

	conv->clear_avatar_choices();
	// Store ->answer string.
	user_choice = newstrdup(conv->get_answer(choice_num));
	return choice_num;    // Return choice #.
}

/*
 *  Create for the outside world.
 */

Usecode_machine* Usecode_machine::create() {
	return new Usecode_internal();
}

/*
 *  Create machine from a 'usecode' file.
 */

Usecode_internal::Usecode_internal() : stack(new Usecode_value[1024]) {
	sp = stack;
	// Read in usecode.
	std::cout << "Reading usecode file." << std::endl;
	try {
		auto pFile = U7open_in(USECODE);
		if (!pFile) {
			throw file_open_exception(USECODE);
		}
		auto& file = *pFile;
		read_usecode(file);
	} catch (const file_exception& /*f*/) {
		if (!Game::is_editing()) {    // Ok if map-editing.
			throw;
		}
		std::cerr << "Warning (map-editing): Couldn't open '" << USECODE << "'"
				  << endl;
	}

	// Get custom usecode functions.
	if (is_system_path_defined("<PATCH>") && U7exists(PATCH_USECODE)) {
		auto pFile = U7open_in(PATCH_USECODE);
		if (!pFile) {
			throw file_open_exception(PATCH_USECODE);
		}
		auto& file = *pFile;
		read_usecode(file, true);
	}

	//  set_breakpoint();
}

/*
 *  Read in usecode functions.  These may override previously read
 *  functions.
 */

void Usecode_internal::read_usecode(
		istream& file,
		bool     patch    // True if reading from 'patch'.
) {
	file.seekg(0, ios::end);
	const int size = file.tellg();    // Get file size.
	file.seekg(0);
	if (Usecode_symbol_table::has_symbol_table(file)) {
		delete symtbl;
		symtbl = new Usecode_symbol_table();
		symtbl->read(file);
	}
	// Read in all the functions.
	while (file.tellg() < size) {
		auto*              fun     = new Usecode_function(file);
		const unsigned int slotnum = fun->id / 0x100;
		if (slotnum >= funs.size()) {
			funs.resize(slotnum < 10 ? 10 : slotnum + 1);
		}
		Funs256&           vec = funs[slotnum];
		const unsigned int i   = fun->id % 0x100;
		if (i >= vec.size()) {
			vec.resize(i + 1);
		} else if (vec[i]) {
			// Already have one there.
			if (patch) {    // Patching?
				if (vec[i]->orig) {
					// Patching a patch.
					fun->orig = vec[i]->orig;
					delete vec[i];
				} else {    // Patching fun. from static.
					fun->orig = vec[i];
				}
			} else {
				delete vec[i]->orig;
				delete vec[i];
			}
		}
		vec[i] = fun;
	}
}

/*
 *  Delete.
 */

Usecode_internal::~Usecode_internal() {
	delete[] stack;
	delete[] String;
	delete symtbl;
	const int num_slots = funs.size();
	for (int i = 0; i < num_slots; i++) {
		Funs256&  slot = funs[i];
		const int cnt  = slot.size();
		for (int j = 0; j < cnt; j++) {
			delete slot[j];
		}
	}
	delete book;
}

#ifdef DEBUG
int        debug    = 2;                   // 2 for more stuff.
static int ucbp_fun = -1, ucbp_ip = -1;    // Breakpoint.

void Setbreak(int fun, int ip) {
	ucbp_fun = fun;
	ucbp_ip  = ip;
}

void Clearbreak() {
	ucbp_fun = ucbp_ip = -1;
}
#endif

#define CERR_CURRENT_IP()                                         \
	cerr << " (at function = " << hex << setw(4) << setfill('0')  \
		 << frame->function->id << ", ip = " << current_IP << dec \
		 << setfill(' ') << ")" << endl

#define LOCAL_VAR_ERROR(x)                                 \
	cerr << "Local variable #" << (x) << " out of range!"; \
	CERR_CURRENT_IP()

#define DATA_SEGMENT_ERROR()              \
	cerr << "Data pointer out of range!"; \
	CERR_CURRENT_IP()

#define EXTERN_ERROR()                     \
	cerr << "Extern offset out of range!"; \
	CERR_CURRENT_IP()

#define FLAG_ERROR(x)                                   \
	cerr << "Global flag #" << (x) << " out of range!"; \
	CERR_CURRENT_IP()

#define THIS_ERROR()                  \
	cerr << "nullptr class pointer!"; \
	CERR_CURRENT_IP()

/*
 *  The main usecode interpreter
 *
 *  Output:
 */

int Usecode_internal::run() {
	bool aborted           = false;
	bool initializing_loop = false;

	while ((frame = call_stack.front())) {
		const int num_locals = frame->num_vars + frame->num_args;
		int       offset;
		int       sval;

		bool frame_changed = false;

		// set some variables for use in other member functions
		caller_item = frame->caller_item;

		/*
		 *  Main loop.
		 */
		while (!frame_changed) {
			if ((frame->ip >= frame->endp) || (frame->ip < frame->code)) {
				cerr << "Usecode: jumped outside of code segment of "
					 << "function " << hex << setw(4) << setfill('0')
					 << frame->function->id << dec << setfill(' ')
					 << " ! Aborting." << endl;

				Usecode_value msg("Out of bounds usecode execution!");
				abort_function(msg);
				frame_changed = true;
				continue;
			}

			const auto current_IP = frame->ip - frame->code;
			frame->ins_ip         = frame->ip;

			auto opcode = static_cast<UsecodeOps>(*(frame->ip));

			if (frame->ip + get_opcode_length(static_cast<int>(opcode))
				> frame->endp) {
				cerr << "Operands lie outside of code segment. ";
				CERR_CURRENT_IP();
				continue;
			}

#ifdef DEBUG
			if (usecode_trace == 2) {
				uc_trace_disasm(frame);
			}
#endif

#ifdef USECODE_DEBUGGER
			// check breakpoints

			const int bp = breakpoints.check(frame);
			if (bp != -1) {
				// we hit a breakpoint

				// allow handling extra debugging messages
				on_breakpoint = true;

				cout << "On breakpoint" << endl;

				// signal remote client that we hit a breakpoint
				auto c = static_cast<unsigned char>(
						Exult_server::dbg_on_breakpoint);
				if (client_socket >= 0) {
					Exult_server::Send_data(
							client_socket, Exult_server::usecode_debugging, &c,
							1);
				}

#	ifdef XWIN
				raise(SIGUSR1);    // to allow trapping it in gdb too
#	endif

#	ifdef USECODE_CONSOLE_DEBUGGER
				// little console mode "debugger" (if you can call it that...)
				bool done = false;
				while (!done) {
					char userinput;
					cout << "s=step into, o=step over, f=finish, c=continue, "
						 << "b=stacktrace: ";
					std::cin >> userinput;

					if (userinput == 's') {
						breakpoints.add(new AnywhereBreakpoint());
						cout << "Stepping into..." << endl;
						done = true;
					} else if (userinput == 'o') {
						breakpoints.add(new StepoverBreakpoint(frame));
						cout << "Stepping over..." << endl;
						done = true;
					} else if (userinput == 'f') {
						breakpoints.add(new FinishBreakpoint(frame));
						cout << "Finishing function..." << endl;
						done = true;
					} else if (userinput == 'c') {
						done = true;
					} else if (userinput == 'b') {
						stack_trace(cout);
					}
				}
#	elif (defined(USE_EXULTSTUDIO))
				breakpoint_action = -1;
				while (breakpoint_action == -1) {
					SDL_Delay(20);
					Server_delay(Handle_client_debug_message);
				}
#	endif

				c = static_cast<unsigned char>(Exult_server::dbg_continuing);
				if (client_socket >= 0) {
					Exult_server::Send_data(
							client_socket, Exult_server::usecode_debugging, &c,
							1);
				}
				// disable extra debugging messages again
				on_breakpoint = false;
			}
#endif

			frame->ip++;

			switch (opcode) {
			case UC_CONVERSE:        // start conversation
			case UC_CONVERSE32: {    // (32 bit version)
				if (opcode < UC_EXTOPCODE) {
					offset = little_endian::Read2s(frame->ip);
				} else {
					offset = little_endian::Read4s(frame->ip);
				}

				found_answer = false;
				if (!get_user_choice()) {    // Exit conv. if no choices.
					frame->ip += offset;     // (Emps and honey.)
				}
				break;
			}
			case UC_JNE:        // JNE.
			case UC_JNE32: {    // JNE32
				if (opcode < UC_EXTOPCODE) {
					offset = little_endian::Read2s(frame->ip);
				} else {
					offset = little_endian::Read4s(frame->ip);
				}
				const Usecode_value val = pop();
				if (val.is_false()) {
					frame->ip += offset;
				}
				break;
			}
			case UC_JMP:      // JMP.
			case UC_JMP32:    // JMP32
				if (opcode < UC_EXTOPCODE) {
					offset = little_endian::Read2s(frame->ip);
				} else {
					offset = little_endian::Read4s(frame->ip);
				}
				frame->ip += offset;
				break;
			case UC_CMPS:        // CMPS.
			case UC_CMPS32: {    // (32 bit version)
				int cnt = little_endian::Read2(frame->ip);    // # strings.
				if (opcode < UC_EXTOPCODE) {
					offset = little_endian::Read2s(frame->ip);
				} else {
					offset = little_endian::Read4s(frame->ip);
				}

				bool matched = false;

				// only try to match if we haven't found an answer yet
				while (!matched && !found_answer && cnt-- > 0) {
					const Usecode_value s   = pop();
					const char*         str = s.get_str_value();
					if (str && strcmp(str, user_choice) == 0) {
						matched      = true;
						found_answer = true;
					}
				}
				while (cnt-- > 0) {    // Pop rest of stack.
					pop();
				}
				if (!matched) {    // Jump if no match.
					frame->ip += offset;
				}
			} break;
			case UC_ADD: {    // ADD.
				const Usecode_value v2     = pop();
				const Usecode_value v1     = pop();
				const Usecode_value retval = v1 + v2;
				push(retval);
				break;
			}
			case UC_SUB: {    // SUB.
				const Usecode_value v2     = pop();
				const Usecode_value v1     = pop();
				const Usecode_value retval = v1 - v2;
				push(retval);
				break;
			}
			case UC_DIV: {    // DIV.
				const Usecode_value v2     = pop();
				const Usecode_value v1     = pop();
				const Usecode_value retval = v1 / v2;
				push(retval);
				break;
			}
			case UC_MUL: {    // MUL.
				const Usecode_value v2     = pop();
				const Usecode_value v1     = pop();
				const Usecode_value retval = v1 * v2;
				push(retval);
				break;
			}
			case UC_MOD: {    // MOD.
				const Usecode_value v2     = pop();
				const Usecode_value v1     = pop();
				const Usecode_value retval = v1 % v2;
				push(retval);
				break;
			}
			case UC_AND: {    // AND.
				const Usecode_value v1     = pop();
				const Usecode_value v2     = pop();
				const bool          result = v1.is_true() && v2.is_true();
				pushi(result);
				break;
			}
			case UC_OR: {    // OR.
				const Usecode_value v1     = pop();
				const Usecode_value v2     = pop();
				const bool          result = v1.is_true() || v2.is_true();
				pushi(result);
				break;
			}
			case UC_NOT:    // NOT.
				pushi(!pop().is_true());
				break;
			case UC_POP: {    // POP into a variable.
				offset = little_endian::Read2(frame->ip);
				// Get value.
				const Usecode_value val = pop();
				if (offset < 0 || offset >= num_locals) {
					LOCAL_VAR_ERROR(offset);
				} else {
					frame->locals[offset] = val;
				}
				break;
			}
			case UC_PUSHTRUE:    // PUSH true.
				pushi(1);
				break;
			case UC_PUSHFALSE:    // PUSH false.
				pushi(0);
				break;
			case UC_CMPGT:    // CMPGT.
				sval = popi();
				pushi(popi() > sval);    // Order?
				break;
			case UC_CMPLT:    // CMPLT.
				sval = popi();
				pushi(popi() < sval);
				break;
			case UC_CMPGE:    // CMPGE.
				sval = popi();
				pushi(popi() >= sval);
				break;
			case UC_CMPLE:    // CMPLE.
				sval = popi();
				pushi(popi() <= sval);
				break;
			case UC_CMPNE: {    // CMPNE.
				const Usecode_value val1 = pop();
				const Usecode_value val2 = pop();
				pushi(!(val1 == val2));
				break;
			}
			case UC_ADDSI:      // ADDSI.
			case UC_ADDSI32:    // ADDSI32
				if (opcode < UC_EXTOPCODE) {
					offset = little_endian::Read2(frame->ip);
				} else {
					offset = little_endian::Read4s(frame->ip);
				}
				if (offset < 0 || frame->data + offset >= frame->externs - 6) {
					DATA_SEGMENT_ERROR();
					break;
				}
				append_string(frame->data + offset);
				break;
			case UC_PUSHS:      // PUSHS.
			case UC_PUSHS32:    // PUSHS32
				if (opcode < UC_EXTOPCODE) {
					offset = little_endian::Read2(frame->ip);
				} else {
					offset = little_endian::Read4s(frame->ip);
				}
				if (offset < 0 || frame->data + offset >= frame->externs - 6) {
					DATA_SEGMENT_ERROR();
					break;
				}
				pushs(frame->data + offset);
				break;
			case UC_ARRC: {    // ARRC.
				// Get # values to pop into array.
				const int     num = little_endian::Read2(frame->ip);
				int           cnt = num;
				Usecode_value arr(num, nullptr);
				int           to = 0;    // Store at this index.
				while (cnt--) {
					Usecode_value val = pop();
					to += arr.add_values(to, val);
				}
				if (to < num) {    // 1 or more vals empty arrays?
					arr.resize(to);
				}
				push(arr);
				break;
			}
			case UC_PUSHI:        // PUSHI.
			case UC_PUSHI32: {    // PUSHI32
				// Might be negative.
				int ival;
				if (opcode < UC_EXTOPCODE) {
					ival = little_endian::Read2s(frame->ip);
				} else {
					ival = little_endian::Read4s(frame->ip);
				}
				pushi(ival);
				break;
			}
			case UC_PUSH:    // PUSH.
				offset = little_endian::Read2(frame->ip);
				if (offset < 0 || offset >= num_locals) {
					LOCAL_VAR_ERROR(offset);
					pushi(0);
				} else {
					push(frame->locals[offset]);
				}
				break;
			case UC_CMPEQ: {    // CMPEQ.
				const Usecode_value val1 = pop();
				const Usecode_value val2 = pop();
				pushi(val1 == val2);
				break;
			}
			case UC_CALL: {    // CALL.
				offset = little_endian::Read2(frame->ip);
				if (offset < 0 || offset >= frame->num_externs) {
					EXTERN_ERROR();
					break;
				}

				const uint8* tempptr = frame->externs + 2 * offset;
				const int    funcid  = little_endian::Read2(tempptr);

				call_function(funcid, frame->eventid);
				frame_changed = true;
				break;
			}
			case UC_CALL32: {    // 32-bit CALL.
				offset = little_endian::Read4s(frame->ip);
				call_function(offset, frame->eventid);
				frame_changed = true;
				break;
			}
			case UC_RET:     // RET. (End of procedure reached)
			case UC_RET2:    // RET. (Return from procedure)
				show_pending_text();

				return_from_procedure();
				frame_changed = true;
				break;
			case UC_AIDX:         // AIDX.
			case UC_AIDXS:        // AIDXS.
			case UC_AIDXTHV: {    // AIDXTHV.
				sval = popi();    // Get index into array.
				sval--;           // It's 1 based.
				// Get # of local to index.
				Usecode_value* val;
				if (opcode == UC_AIDX) {
					offset = little_endian::Read2(frame->ip);
					if (offset < 0 || offset >= num_locals) {
						LOCAL_VAR_ERROR(offset);
						pushi(0);
						break;
					}
					val = &(frame->locals[offset]);
				} else if (opcode == UC_AIDXTHV) {
					offset             = little_endian::Read2(frame->ip);
					Usecode_value& ths = frame->get_this();
					if (offset < 0 || offset >= ths.get_class_var_count()) {
						cerr << "Class variable #" << (offset)
							 << " out of range!";
						CERR_CURRENT_IP();
						break;
					}
					val = &(ths.nth_class_var(offset));
				} else {
					offset = little_endian::Read2s(frame->ip);
					if (offset < 0) {    // Global static.
						if (static_cast<unsigned>(-offset) < statics.size()) {
							val = &(statics[-offset]);
						} else {
							cerr << "Global static variable #" << (offset)
								 << " out of range!";
							pushi(0);
							break;
						}
					} else {
						if (static_cast<unsigned>(offset)
							< frame->function->statics.size()) {
							val = &(frame->function->statics[offset]);
						} else {
							cerr << "Local static variable #" << (offset)
								 << " out of range!";
							pushi(0);
							break;
						}
					}
				}
				if (sval < 0) {
					cerr << "AIDX: Negative array index: " << sval << endl;
					pushi(0);
					break;
				}
				if (val->is_array() && size_t(sval) >= val->get_array_size()) {
					pushi(0);              // Matches originals.
				} else if (sval == 0) {    // needed for SS keyring (among
										   // others)
					push(val->get_elem0());
				} else {
					push(val->get_elem(sval));
				}
				break;
			}
			case UC_RETV: {    // RET. (Return from function)
				// ++++ Testing.
				show_pending_text();
				Usecode_value r = pop();

				return_from_function(r);
				frame_changed = true;
				break;
			}
			case UC_LOOP:        // INITLOOP (1st byte of loop)
			case UC_LOOP32: {    // (32 bit version)
				int nextopcode = *(frame->ip);
				// No real reason to have 32-bit version of this instruction;
				// keeping it for backward compatibility only.
				nextopcode &= 0x7f;
				if (nextopcode != UC_LOOPTOP && nextopcode != UC_LOOPTOPS
					&& nextopcode != UC_LOOPTOPTHV) {
					cerr << "Invalid 2nd byte in loop!" << endl;
					break;
				} else {
					initializing_loop = true;
				}
				break;
			}
			case UC_LOOPTOP:       // LOOP (2nd byte of loop)
			case UC_LOOPTOP32:     // (32 bit version)
			case UC_LOOPTOPS:      // LOOP (2nd byte of loop) using static array
			case UC_LOOPTOPS32:    // (32 bit version)
			case UC_LOOPTOPTHV:    // LOOP (2nd byte of loop) using class member
								   // array
			case UC_LOOPTOPTHV32: {    // (32 bit version)
				// Counter (1-based).
				const int local1 = little_endian::Read2(frame->ip);
				// Total count.
				const int local2 = little_endian::Read2(frame->ip);
				// Current value of loop var.
				const int local3 = little_endian::Read2(frame->ip);
				// Array of values to loop over.
				int        local4;
				const bool is_32bit = (opcode >= UC_EXTOPCODE);
				// Mask off 32bit flag.
				opcode &= 0x7f;
				if (opcode == UC_LOOPTOPS) {
					local4 = little_endian::Read2s(frame->ip);
				} else {
					local4 = little_endian::Read2(frame->ip);
				}
				// Get offset to end of loop.
				if (is_32bit) {
					offset = little_endian::Read4s(
							frame->ip);    // 32 bit offset
				} else {
					offset = little_endian::Read2s(frame->ip);
				}

				if (local1 < 0 || local1 >= num_locals) {
					LOCAL_VAR_ERROR(local1);
					break;
				}
				if (local2 < 0 || local2 >= num_locals) {
					LOCAL_VAR_ERROR(local2);
					break;
				}
				if (local3 < 0 || local3 >= num_locals) {
					LOCAL_VAR_ERROR(local3);
					break;
				}
				if (opcode == UC_LOOPTOPS) {
					if (local4 < 0) {    // Global static.
						if (static_cast<unsigned>(-local4) >= statics.size()) {
							cerr << "Global static variable #" << (-local4)
								 << " out of range!";
							CERR_CURRENT_IP();
							break;
						}
					} else {
						if (static_cast<unsigned>(local4)
							>= frame->function->statics.size()) {
							cerr << "Local static variable #" << (local4)
								 << " out of range!";
							CERR_CURRENT_IP();
							break;
						}
					}
				} else if (opcode == UC_LOOPTOPTHV) {
					Usecode_value& ths = frame->get_this();
					if (local4 < 0 || local4 >= ths.get_class_var_count()) {
						cerr << "Class variable #" << (local4)
							 << " out of range!";
						CERR_CURRENT_IP();
						break;
					}
				} else {
					if (local4 < 0 || local4 >= num_locals) {
						LOCAL_VAR_ERROR(local4);
						break;
					}
				}

				// Get array to loop over.
				Usecode_value& arr
						= opcode == UC_LOOPTOPS
								  ? (local4 < 0
											 ? statics[-local4]
											 : frame->function->statics[local4])
								  : (opcode == UC_LOOPTOPTHV
											 ? frame->get_this().nth_class_var(
													   local4)
											 : frame->locals[local4]);
				if (initializing_loop && arr.is_undefined()) {
					// If the local 'array' is not initialized, do not loop
					// (verified in FoV and SS):
					initializing_loop = false;
					frame->ip += offset;
					break;
				}

				int next = frame->locals[local1].get_int_value();

				if (initializing_loop) {
					// Initialize loop.
					initializing_loop = false;
					const int cnt = arr.is_array() ? arr.get_array_size() : 1;
					frame->locals[local2] = Usecode_value(cnt);
					frame->locals[local1] = Usecode_value(0);

					next = 0;
				}

				// in SI, the loop-array can be modified in-loop, it seems
				// (conv. with Spektran, 044D:00BE)

				// so, check for changes of the array size, and adjust
				// total count and next value accordingly.

				// Allowing this for BG too.

				const int cnt = arr.is_array() ? arr.get_array_size() : 1;

				if (cnt != frame->locals[local2].get_int_value()) {
					// update new total count
					frame->locals[local2] = Usecode_value(cnt);

					if (std::abs(cnt - frame->locals[local2].get_int_value())
						== 1) {
						// small change... we can fix this
						const Usecode_value& curval
								= arr.is_array() ? arr.get_elem(next - 1) : arr;

						if (curval != frame->locals[local3]) {
							if (cnt > frame->locals[local2].get_int_value()) {
								// array got bigger, it seems
								// addition occured before the current value
								next++;
							} else {
								// array got smaller
								// deletion occured before the current value
								next--;
							}
						} else {
							// addition/deletion was after the current value
							// so don't need to update 'next'
						}
					} else {
						// big change...
						// just update total count to make sure
						// we don't crash
					}
				}

				if (cnt != frame->locals[local2].get_int_value()) {
					// update new total count
					frame->locals[local2] = Usecode_value(cnt);

					const Usecode_value& curval
							= arr.is_array() ? arr.get_elem(next - 1) : arr;

					if (curval != frame->locals[local3]) {
						if (cnt > frame->locals[local2].get_int_value()) {
							// array got bigger, it seems
							// addition occured before the current value
							next++;
						} else {
							// array got smaller
							// deletion occured before the current value
							next--;
						}
					} else {
						// addition/deletion was after the current value
						// so don't need to update 'next'
					}
				}

				// End of loop?
				if (next >= frame->locals[local2].get_int_value()) {
					frame->ip += offset;
				} else {    // Get next element.
					frame->locals[local3]
							= arr.is_array() ? arr.get_elem(next) : arr;
					frame->locals[local1] = Usecode_value(next + 1);
				}
				break;
			}
			case UC_ADDSV: {    // ADDSV.
				offset = little_endian::Read2(frame->ip);
				if (offset < 0 || offset >= num_locals) {
					LOCAL_VAR_ERROR(offset);
					break;
				}

				const char* str = frame->locals[offset].get_str_value();
				if (str) {
					append_string(str);
				} else {    // Convert integer.
					// 25-09-2001 - Changed to >= 0 to fix money-counting in SI.
					//              if (locals[offset].get_int_value() != 0) {
					if (frame->locals[offset].get_int_value() >= 0) {
						char buf[20];
						snprintf(
								buf, sizeof(buf), "%ld",
								frame->locals[offset].get_int_value());
						append_string(buf);
					}
				}
				break;
			}
			case UC_IN: {    // IN.  Is a val. in an array?
				Usecode_value arr = pop();
				// If an array, use 1st elem.
				const Usecode_value val = pop().get_elem0();
				pushi(arr.find_elem(val) >= 0);
				break;
			}
			case UC_DEFAULT:      // Conversation default.
			case UC_DEFAULT32:    // (32 bit version)
				// This opcode only occurs in the 'audition' usecode function
				// (BG) It is a version of CMPS that pops no values from the
				// stack (ignores the count parameter) and either branches to
				// the next case if an answer has been picked already or marks
				// an answer as being found, so that no more answers can be
				// found after this opcode runs until the next converse loop.
				// This means that it can be dangerous to place it before other
				// CMPS cases, as they will never match.
				frame->ip += 2;
				if (opcode < UC_EXTOPCODE) {
					offset = little_endian::Read2s(frame->ip);
				} else {
					offset = little_endian::Read4s(frame->ip);
				}

				if (!found_answer) {
					found_answer = true;
				} else {
					frame->ip += offset;
				}
				break;

			case UC_RETZ: {    // RET. (End of function reached)
				show_pending_text();

				Usecode_value zero(0);
				return_from_function(zero);
				frame_changed = true;
				break;
			}
			case UC_SAY:    // SAY.
				say_string();
				break;
			case UC_CALLIS: {    // CALLIS.
				offset = little_endian::Read2(frame->ip);
				sval   = *(frame->ip)++;    // # of parameters.
				const Usecode_value ival = call_intrinsic(offset, sval);
				push(ival);
				frame_changed = true;
				break;
			}
			case UC_CALLI:    // CALLI.
				offset = little_endian::Read2(frame->ip);
				sval   = *(frame->ip)++;    // # of parameters.
				call_intrinsic(offset, sval);
				frame_changed = true;
				break;
			case UC_PUSHITEMREF:    // PUSH ITEMREF.
				pushref(frame->caller_item);
				break;
			case UC_ABRT: {    // ABRT.
				show_pending_text();

				Usecode_value msg("abort executed");
				abort_function(msg);
				frame_changed = true;
				aborted       = true;
				break;
			}
			case UC_THROW: {    // THROW.
				show_pending_text();

				Usecode_value r = pop();
				abort_function(r);
				frame_changed = true;
				aborted       = true;
				break;
			}
			case UC_TRYSTART:
			case UC_TRYSTART32:
				if (opcode < UC_EXTOPCODE) {
					offset = little_endian::Read2s(frame->ip);
				} else {
					offset = little_endian::Read4s(frame->ip);
				}
				except_stack[frame] = frame->ip + offset;
				break;
			case UC_TRYEND: {
				auto it = except_stack.find(frame);
				if (it != except_stack.end()) {
					except_stack.erase(it);
				}
				break;
			}
			case UC_CONVERSELOC:    // end conversation
				found_answer = true;
				break;
			case UC_PUSHF:       // PUSHF.
			case UC_PUSHFVAR:    // PUSHF2.
				if (opcode >= UC_EXTOPCODE) {
					offset = popi();
				} else {
					offset = little_endian::Read2(frame->ip);
				}
				if (offset < 0
					|| static_cast<unsigned>(offset) >= sizeof(gflags)) {
					FLAG_ERROR(offset);
					pushi(0);
				} else {
					pushi(gflags[offset]);
				}
				break;
			case UC_POPF:       // POPF.
			case UC_POPFVAR:    // POPF2.
				if (opcode >= UC_EXTOPCODE) {
					offset = popi();
				} else {
					offset = little_endian::Read2(frame->ip);
				}
				if (offset < 0
					|| static_cast<unsigned>(offset) >= sizeof(gflags)) {
					FLAG_ERROR(offset);
				} else {
					gflags[offset] = static_cast<unsigned char>(popi());
					if (gflags[offset]) {
						Notebook_gump::add_gflag_text(offset);
#ifdef DEBUG
						cout << "Setting global flag: " << offset << endl;
#endif
					}
					// ++++KLUDGE for Monk Isle:
					if (offset == 0x272 && GAME_SI) {
						gflags[offset] = 0;
					}
				}
				break;
			case UC_PUSHB:    // PUSHB.
				pushi(*(frame->ip)++);
				break;
			case UC_POPARR:         // Set array element.
			case UC_POPARRS:        // Set static array element.
			case UC_POPARRTHV: {    // Set class member array element.
				Usecode_value* arr;
				if (opcode == UC_POPARR) {
					offset = little_endian::Read2(frame->ip);
					// Get # of local array.
					if (offset < 0 || offset >= num_locals) {
						LOCAL_VAR_ERROR(offset);
						break;
					}
					arr = &(frame->locals[offset]);
				} else if (opcode == UC_POPARRTHV) {
					offset             = little_endian::Read2(frame->ip);
					Usecode_value& ths = frame->get_this();
					if (offset < 0 || offset >= ths.get_class_var_count()) {
						cerr << "Class variable #" << (offset)
							 << " out of range!";
						CERR_CURRENT_IP();
						break;
					}
					arr = &(ths.nth_class_var(offset));
				} else {
					offset = little_endian::Read2s(frame->ip);
					if (offset < 0) {    // Global static.
						if (static_cast<unsigned>(-offset) < statics.size()) {
							arr = &(statics[-offset]);
						} else {
							cerr << "Global static variable #" << (offset)
								 << " out of range!";
							CERR_CURRENT_IP();
							break;
						}
					} else {
						if (static_cast<unsigned>(offset)
							< frame->function->statics.size()) {
							arr = &(frame->function->statics[offset]);
						} else {
							cerr << "Local static variable #" << (offset)
								 << " out of range!";
							CERR_CURRENT_IP();
							break;
						}
					}
				}
				short index = popi();
				index--;    // It's 1-based.
				Usecode_value val  = pop();
				const int     size = arr->get_array_size();
				if (index >= 0 && (index < size || arr->resize(index + 1))) {
					arr->put_elem(index, val);
				}
				break;
			}
			case UC_CALLE:        // CALLE.  Stack has caller_item.
			case UC_CALLE32: {    // 32-bit version.
				Usecode_value ival   = pop();
				Game_object*  caller = get_item(ival);
				if (opcode < UC_EXTOPCODE) {
					offset = little_endian::Read2(frame->ip);
				} else {
					offset = little_endian::Read4s(frame->ip);
				}
				call_function(offset, frame->eventid, caller);
				frame_changed = true;
				break;
			}
			case UC_PUSHEVENTID:    // PUSH EVENTID.
				pushi(frame->eventid);
				break;
			case UC_ARRA: {    // ARRA.
				Usecode_value val = pop();
				Usecode_value arr = pop();
				push(arr.concat(val));
				break;
			}
			case UC_POPEVENTID:    // POP EVENTID.
				frame->eventid = popi();
				break;
			case UC_DBGLINE: {    // debugging opcode from spanish SI (line
								  // number)
				frame->line_number = little_endian::Read2(frame->ip);
				break;
			}
			case UC_DBGFUNC:    // debugging opcode from spanish SI (function
								// init)
			case UC_DBGFUNC32: {    // 32 bit debugging function init
				int funcname;
				int paramnames;
				if (opcode < UC_EXTOPCODE) {
					funcname   = little_endian::Read2(frame->ip);
					paramnames = little_endian::Read2(frame->ip);
				} else {
					funcname   = little_endian::Read4s(frame->ip);
					paramnames = little_endian::Read4s(frame->ip);
				}
				if (funcname < 0
					|| frame->data + funcname >= frame->externs - 6) {
					DATA_SEGMENT_ERROR();
					break;
				}
				if (paramnames < 0
					|| frame->data + paramnames >= frame->externs - 6) {
					DATA_SEGMENT_ERROR();
					break;
				}
				cout << "Debug opcode found at function = " << hex << setw(4)
					 << setfill('0') << frame->function->id
					 << ", ip = " << current_IP << dec << setfill(' ') << "."
					 << endl;
				cout << "Information is: funcname = '"
					 // This is a complete guess:
					 << (frame->data + funcname) << "'." << endl;
				const char* ptr = reinterpret_cast<const char*>(
						frame->data + paramnames);
				// This is an even bigger complete guess:
				if (*ptr) {
					int nargs = frame->num_args;
					if (is_object_fun(frame->function->id)) {
						nargs--;    // Function has an 'item'.
					}
					if (nargs < 0) {    // Just in case.
						nargs = 0;
					}
					std::vector<std::string> names;
					names.resize(nargs);
					int i;
					// Reversed to match the order in which they are
					// passed in UCC.
					for (i = nargs - 1; i >= 0 && *ptr; i--) {
						const std::string name(ptr);
						names[i] = name;
						ptr += name.length() + 1;
					}
					cout << "Parameter names follow: ";
					for (i = 0; i < nargs; i++) {
						cout << "#" << hex << setw(4) << setfill('0') << i
							 << " = ";
						if (names[i].length()) {
							cout << "'" << names[i] << "'";
						} else {
							cout << "(missing)";
						}
						if (i < nargs) {
							cout << ", ";
						}
					}
					cout << endl << "Variable names follow: ";
					for (i = 0; i < frame->num_vars && *ptr; i++) {
						const std::string name(ptr);
						ptr += name.length() + 1;
						cout << "#" << hex << setw(4) << setfill('0')
							 << (i + nargs) << " = ";
						if (name.length()) {
							cout << "'" << name << "'";
						} else {
							cout << "(missing)";
						}
						if (i < frame->num_vars) {
							cout << ", ";
						}
					}
					for (; i < frame->num_vars; i++) {
						cout << "#" << hex << setw(4) << setfill('0')
							 << (i + nargs) << " = (missing)";
						if (i < frame->num_vars) {
							cout << ", ";
						}
					}
				} else {
					cout << endl;
				}
				break;
			}
			case UC_PUSHSTATIC:    // PUSH static.
				offset = little_endian::Read2s(frame->ip);
				if (offset < 0) {    // Global static.
					if (static_cast<unsigned>(-offset) < statics.size()) {
						push(statics[-offset]);
					} else {
						pushi(0);
					}
				} else {
					if (static_cast<unsigned>(offset)
						< frame->function->statics.size()) {
						push(frame->function->statics[offset]);
					} else {
						pushi(0);
					}
				}
				break;
			case UC_POPSTATIC: {    // POP static.
				offset = little_endian::Read2s(frame->ip);
				// Get value.
				const Usecode_value val = pop();
				if (offset < 0) {
					if (static_cast<unsigned>(-offset) >= statics.size()) {
						statics.resize(-offset + 1);
					}
					statics[-offset] = val;
				} else {
					if (static_cast<unsigned>(offset)
						>= frame->function->statics.size()) {
						frame->function->statics.resize(offset + 1);
					}
					frame->function->statics[offset] = val;
				}
				break;
			}
			case UC_CALLO: {    // CALLO (call original).
				// Otherwise, like CALLE.
				Usecode_value ival   = pop();
				Game_object*  caller = get_item(ival);
				offset               = little_endian::Read2(frame->ip);
				call_function(offset, frame->eventid, caller, false, true);
				frame_changed = true;
				break;
			}
			case UC_CALLIND:          // CALLIND:  call indirect.
			case UC_CALLINDEX_OLD:    // CALLINDEX_OLD:  call indirect with
									  // arguments.
			case UC_CALLINDEX: {    // CALLINDEX: call indirect with arguments.
				//  Function # is on stack.
				const Usecode_value funval = pop();
				const int           offset = funval.get_int_value();
				Usecode_value       ival   = pop();
				Game_object*        caller = get_item(ival);
				int                 numargs;
				if (opcode < UC_EXTOPCODE) {
					numargs = 0;
				} else if (opcode == UC_CALLINDEX_OLD) {
					numargs = popi();
				} else {
					numargs = *(frame->ip)++;
				}
				call_function(
						offset, frame->eventid, caller, false, false, numargs);
				frame_changed = true;
				break;
			}
			case UC_PUSHTHV: {    // PUSH class this->var.
				offset             = little_endian::Read2(frame->ip);
				Usecode_value& ths = frame->get_this();
				push(ths.nth_class_var(offset));
				break;
			}
			case UC_POPTHV: {    // POP class this->var.
				// Get value.
				const Usecode_value val   = pop();
				offset                    = little_endian::Read2(frame->ip);
				Usecode_value& ths        = frame->get_this();
				ths.nth_class_var(offset) = val;
				break;
			}
			case UC_CALLM:       // CALLM - call method, use pushed var vtable.
			case UC_CALLMS: {    // CALLMS - call method, use parameter vtable.
				offset = little_endian::Read2(frame->ip);
				Usecode_class_symbol* c;
				if (opcode == UC_CALLM) {
					const Usecode_value thisptr = peek();
					c                           = thisptr.get_class_ptr();
				} else {
					c = get_class(little_endian::Read2(frame->ip));
				}
				if (!c) {
					THIS_ERROR();
					(void)pop();
					break;
				}
				const int index = c->get_method_id(offset);
				call_function(index, frame->eventid);
				frame_changed = true;
				break;
			}
			case UC_CLSCREATE: {    // CLSCREATE
				const int             cnum = little_endian::Read2(frame->ip);
				Usecode_class_symbol* cls  = symtbl->get_class(cnum);
				if (!cls) {
					cerr << "Can't create obj. for class #" << cnum << endl;
					pushi(0);
					break;
				}
				int           cnt       = cls->get_num_vars();
				Usecode_value new_class = Usecode_value(0);
				new_class.class_new(cls, cnt);

				int to = 0;    // Store at this index.
				// We are trusting UCC output here.
				while (cnt--) {
					const Usecode_value val       = pop();
					new_class.nth_class_var(to++) = val;
				}
				push(new_class);
				break;
			}
			case UC_CLASSDEL: {    // CLASSDEL
				Usecode_value cls = pop();
				cls.class_delete();
				break;
			}
			case UC_PUSHCHOICE:    // PUSHCHOICE
				pushs(user_choice);
				break;
			default:
				cerr << "Opcode " << opcode << " not known. ";
				CERR_CURRENT_IP();
				break;
			}
		}
	}

	if (call_stack.front() == nullptr) {
		// pop the nullptr frame from the stack
		call_stack.pop_front();
	}
	if (aborted) {
		return 0;
	}

	return 1;
}

/*
 *  This is the main entry for outside callers.
 *
 *  Output: -1 if not found.
 *      0 if can't execute now or if aborted.
 *      1 otherwise.
 */

int Usecode_internal::call_usecode(
		int            id,      // Function #.
		Game_object*   item,    // Item ref.
		Usecode_events event) {
	conv->clear_answers();

	int ret;
	if (call_function(id, event, item, true)) {
		ret = run();
	} else {
		ret = -1;    // failed to call the function
	}

	set_book(nullptr);

	// Left hanging (BG)?
	if (conv->get_num_faces_on_screen() > 0) {
		conv->init_faces();       // Remove them.
		gwin->set_all_dirty();    // Force repaint.
	}
	if (modified_map) {
		// On a barge, and we changed the map.
		Barge_object* barge = gwin->get_moving_barge();
		if (barge) {
			barge->set_to_gather();    // Refigure what's on barge.
		}
		modified_map = false;
	}
	return ret;
}

/*
 *  Call a 'method'.
 *  Output: Same as input, unless it's 'new', in which we return the new
 *      instance.
 */

bool Usecode_internal::call_method(
		Usecode_value* inst,    // Instance, or nullptr.
		int            id,      // Function # or -1 for free inst.
		Game_object*   item     // Item ref.
) {
	if (id == -1) {
		// Only delete the class for now
		inst->class_delete();
		return false;
	}
	Usecode_function* fun = find_function(id);
	if (!fun) {
		return false;
	}

	auto* frame
			= new Stack_frame(fun, 0, item, Stack_frame::getCallChainID(), 0);

	int oldstack = 0;
	while (frame->num_args > oldstack) {    // Not enough args pushed?
		pushi(0);                           // add zeroes
		oldstack++;
	}

	// Store args in first num_args locals
	int i;
	for (i = 0; i < frame->num_args; i++) {
		const Usecode_value val                = pop();
		frame->locals[frame->num_args - i - 1] = val;
	}

	// save stack pointer
	frame->save_sp = sp;

	// add new stack frame to top of stack
	call_stack.push_front(frame);

#ifdef DEBUG
	Usecode_class_symbol* cls  = inst->get_class_ptr();
	Usecode_symbol*       fsym = cls ? (*cls)[id] : nullptr;
	cout << "Running usecode " << setw(4);
	if (cls) {
		cout << cls->get_name();
	} else {    // Shouldn't happen.
		cout << "Unknown class";
	}
	cout << "::";
	if (fsym) {
		cout << fsym->get_name();
	} else {
		cout << hex << setfill('0') << id << dec << setfill(' ');
	}
	cout << " (";
	for (i = 0; i < frame->num_args; i++) {
		if (i) {
			cout << ", ";
		}
		frame->locals[i].print(cout);
	}
	cout << endl;
#endif

	return true;
}

/*
 *  Lookup function name in symbol table.  Prints error if not found.
 */

int Usecode_internal::find_function(const char* nm, bool noerr) {
	Usecode_symbol* ucsym = symtbl ? (*symtbl)[nm] : nullptr;
	if (!ucsym) {
		if (!noerr) {
			cerr << "Failed to find Usecode symbol '" << nm << "'." << endl;
		}
		return -1;
	}
	return ucsym->get_val();
}

/*
 *  Lookup function id in symbol table.
 */

const char* Usecode_internal::find_function_name(int funcid) {
	Usecode_symbol* ucsym = symtbl ? (*symtbl)[funcid] : nullptr;
	if (!ucsym) {
		return nullptr;
	}
	return ucsym->get_name();
}

/*
 *  Start speech, or show text if speech isn't enabled.
 */

void Usecode_internal::do_speech(int num) {
	speech_track = num;    // Used in Usecode function.
	if (!Audio::get_ptr()->start_speech(num)
		|| Audio::get_ptr()->is_speech_with_subs()) {
		// No speech?  Call text function.
		call_usecode(SpeechUsecode, nullptr, double_click);
	}
}

/*
 *  Are we in a usecode function for a given item and event?
 */

bool Usecode_internal::in_usecode_for(Game_object* item, Usecode_events event) {
	for (auto* frame : call_stack) {
		if (frame->eventid == event && frame->caller_item.get() == item) {
			return true;
		}
	}
	return false;
}

/*
 *  Write out global data to 'gamedat/usecode.dat'.
 *  (and 'gamedat/keyring.dat')
 *
 *  Output: 0 if error.
 */

void Usecode_internal::write() {
	// Assume new games will have keyring.
	if (Game::get_game_type() != BLACK_GATE) {
		keyring->write();    // write keyring data
	}

	{
		OFileDataSource out(FLAGINIT);
		out.write(gflags, sizeof(gflags));
	}
	{
		OFileDataSource out(USEDAT);
		out.write2(partyman->get_count());    // Write party.
		for (int i = 0; i < EXULT_PARTY_MAX; i++) {
			out.write2(partyman->get_member(i));
		}
		// Timers.
		out.write4(0xffffffffU);
		for (auto& timer : timers) {
			if (!timer.second) {    // Don't write unused timers.
				continue;
			}
			out.write2(timer.first);
			out.write4(timer.second);
		}
		out.write2(0xffff);
		out.write2(saved_pos.tx);    // Write saved pos.
		out.write2(saved_pos.ty);
		out.write2(saved_pos.tz);
		out.write2(saved_map);    // Write saved map.
	}
	// Static variables. 1st, globals.
	OFileDataSource nfile(USEVARS);
	nfile.write4(statics.size());    // # globals.
	for (auto& it : statics) {
		if (!it.save(&nfile)) {
			throw file_exception("Could not write static usecode value");
		}
	}
	// Now do the local statics.
	const int num_slots = funs.size();
	for (int i = 0; i < num_slots; i++) {
		const Funs256& slot = funs[i];
		for (auto* fun : slot) {
			if (!fun || fun->statics.empty()) {
				continue;
			}
			Usecode_symbol* fsym = symtbl ? (*symtbl)[fun->id] : nullptr;
			if (fsym) {
				const char* nm = fsym->get_name();
				nfile.write4(0xfffffffeU);
				nfile.write2(static_cast<uint16>(strlen(nm)));
				nfile.write(nm, strlen(nm));
			} else {
				nfile.write4(fun->id);
			}
			nfile.write4(fun->statics.size());
			for (auto& it : fun->statics) {
				if (!it.save(&nfile)) {
					throw file_exception(
							"Could not write static usecode value");
				}
			}
		}
	}
	nfile.write4(0xffffffffU);    // End with -1.
}

/*
 *  Read in global data from 'gamedat/usecode.dat'.
 *  (and 'gamedat/keyring.dat')
 *
 *  Output: 0 if error.
 */

void Usecode_internal::read() {
	if (Game::get_game_type() != BLACK_GATE && !Game::is_si_beta()) {
		keyring->read();    // read keyring data
	}

	try {
		auto pIn = U7open_in(FLAGINIT);    // Read global flags.
		if (!pIn) {
			throw file_read_exception(FLAGINIT);
		}
		auto& in = *pIn;
		in.seekg(0, ios::end);    // Get filesize.
		size_t filesize = in.tellg();
		in.seekg(0, ios::beg);
		if (filesize > sizeof(gflags)) {
			filesize = sizeof(gflags);
		}
		memset(&gflags[0], 0, sizeof(gflags));
		in.read(reinterpret_cast<char*>(gflags), filesize);
	} catch (const exult_exception& /*e*/) {
		if (!Game::is_editing()) {
			throw;
		}
		memset(&gflags[0], 0, sizeof(gflags));
	}

	clear_usevars();    // first clear all statics
	read_usevars();
	std::unique_ptr<std::istream> pIn;
	try {
		pIn = U7open_in(USEDAT);
	} catch (exult_exception& /*e*/) {
		partyman->set_count(0);
		partyman->link_party();    // Still need to do this.
		return;                    // Not an error if no saved game yet.
	}
	if (!pIn) {
		throw file_read_exception(USEDAT);
	}
	auto& in = *pIn;
	partyman->set_count(little_endian::Read2(in));    // Read party.
	size_t i;                                         // Blame MSVC
	for (i = 0; i < EXULT_PARTY_MAX; i++) {
		partyman->set_member(i, little_endian::Read2(in));
	}
	partyman->link_party();
	// Timers.
	const int cnt = little_endian::Read4(in);
	if (cnt == -1) {
		int tmr = 0;
		while ((tmr = little_endian::Read2(in)) != 0xffff) {
			timers[tmr] = little_endian::Read4(in);
		}
	} else {
		timers[0] = cnt;
		for (size_t t = 1; t < 20; t++) {
			timers[t] = little_endian::Read4(in);
		}
	}
	if (!in.good()) {
		throw file_read_exception(USEDAT);
	}
	saved_pos.tx = little_endian::Read2(in);    // Read in saved position.
	saved_pos.ty = little_endian::Read2(in);
	saved_pos.tz = little_endian::Read2(in);
	if (!in.good() ||    // Failed.+++++Can remove this later.
		saved_pos.tz < 0 || saved_pos.tz > 13) {
		saved_pos = Tile_coord(-1, -1, -1);
	}
	saved_map = little_endian::Read2(in);
	if (!in.good()) {    // For compat. with older saves.
		saved_map = -1;
	}
}

/*
 *  Read in static variables from USEVARS.
 */

void Usecode_internal::read_usevars() {
	IFileDataSource nfile(USEVARS);
	if (!nfile.good()) {
		// Okay if this doesn't exist.
		return;
	}
	const int cnt = nfile.read4();    // Global statics.
	statics.resize(cnt);
	int i;
	for (i = 0; i < cnt; i++) {
		statics[i].restore(&nfile);
	}
	unsigned long funid;
	while (!nfile.eof() && (funid = nfile.read4()) != 0xffffffffU) {
		if (funid == 0xfffffffeU) {
			// ++++ FIXME: Write code for the cases when symtbl == 0 or
			// fsym == 0 (neither of which *should* happen...)
			const int len = nfile.read2();
			char*     nm  = new char[len + 1];
			nfile.read(nm, len);
			nm[len]              = 0;
			Usecode_symbol* fsym = symtbl ? (*symtbl)[nm] : nullptr;
			if (fsym) {
				funid = fsym->get_val();
			}
			delete[] nm;
		}
		const int         cnt = nfile.read4();
		Usecode_function* fun = find_function(funid);
		if (!fun) {
			continue;
		}
		fun->statics.resize(cnt);
		for (i = 0; i < cnt; i++) {
			fun->statics[i].restore(&nfile);
		}
	}
}

void Usecode_internal::clear_usevars() {
	statics.clear();
	const int nslots = funs.size();
	for (int i = 0; i < nslots; ++i) {
		const vector<Usecode_function*>& slot = funs[i];
		for (auto* fun : slot) {
			if (fun) {
				fun->statics.clear();
			}
		}
	}
}

#ifdef USECODE_DEBUGGER

int Usecode_internal::get_callstack_size() const {
	return call_stack.size();
}

Stack_frame* Usecode_internal::get_stackframe(int i) {
	if (i >= 0 && static_cast<unsigned>(i) < call_stack.size()) {
		return call_stack[i];
	} else {
		return nullptr;
	}
}

// return current size of the stack
int Usecode_internal::get_stack_size() const {
	return static_cast<int>(sp - stack);
}

// get an(y) element from the stack. (depth == 0 is top element)
Usecode_value* Usecode_internal::peek_stack(int depth) const {
	if (depth < 0 || depth >= get_stack_size()) {
		return nullptr;
	}

	return sp - depth - 1;
}

// modify an(y) element on the stack. (depth == 0 is top element)
void Usecode_internal::poke_stack(int depth, Usecode_value& val) {
	if (depth < 0 || (sp - depth) < stack) {
		return;
	}

	*(sp - depth) = val;
}

void Usecode_internal::set_breakpoint() {
	breakpoints.add(new AnywhereBreakpoint());
}

void Usecode_internal::dbg_stepover() {
	if (on_breakpoint) {
		breakpoints.add(new StepoverBreakpoint(call_stack.front()));
	}
}

void Usecode_internal::dbg_finish() {
	if (on_breakpoint) {
		breakpoints.add(new FinishBreakpoint(call_stack.front()));
	}
}

int Usecode_internal::set_location_breakpoint(int funcid, int ip) {
	Breakpoint* bp = new LocationBreakpoint(funcid, ip);
	breakpoints.add(bp);

	return bp->id;
}

#endif