File: intro.lua

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (3186 lines) | stat: -rw-r--r-- 87,509 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
SDLK_UP           = 82 + 1073741824
SDLK_DOWN         = 81 + 1073741824
SDLK_RIGHT        = 79 + 1073741824
SDLK_LEFT         = 80 + 1073741824

g_img_tbl = {}

local function poll_for_esc()
	local input = input_poll()
	if input ~= nil and input == 27 then
		return true
	end

	if engine_should_quit() == 1 then
		return true
	end

	return false
end

local function wait_for_input()
	local input = nil
	while input == nil do
		canvas_update()
		input = input_poll()
			if input ~= nil then
				break
			end

			if engine_should_quit() == 1 then
				break
			end
	end

	return input
end

local function should_exit(input)
	if engine_should_quit() == 1 then
		return true
	end

	if input ~=nil and input == 27 then
		return true
	end

	return false
end

local function fade_out()
	local i
	for i=0xff,0,-3 do
		canvas_set_opacity(i)
		canvas_update()

		if engine_should_quit() == 1 then
			break
		end
	end

	return false
end

local function fade_out_sprite(sprite, speed)
	local i
	if speed ~= nil then
		speed = -speed
	else
		speed = -3
	end

	for i=0xff,0,speed do
		sprite.opacity = i
		canvas_update()

		if engine_should_quit() == 1 then
			break
		end
	end

	return false
end

local function fade_in()
	local i
	for i=0x0,0xff,3 do
		canvas_set_opacity(i)
		canvas_update()

		if engine_should_quit() == 1 then
			break
		end
	end

	return false
end

local function fade_in_sprite(sprite, speed)
	local i
	if speed ~= nil then
		speed = speed
	else
		speed = 3
	end

	for i=0,0xff,speed do
		sprite.opacity = i
		canvas_update()

		if engine_should_quit() == 1 then
			break
		end
	end

	return false
end

local function load_images(filename)
	g_img_tbl = image_load_all(filename)
end

g_clock_tbl = {}

local function load_clock()
	g_clock_tbl["h1"] = sprite_new(nil, 0xdd, 0x14, true)
	g_clock_tbl["h2"] = sprite_new(nil, 0xdd+4, 0x14, true)
	g_clock_tbl["m1"] = sprite_new(nil, 0xdd+0xa, 0x14, true)
	g_clock_tbl["m2"] = sprite_new(nil, 0xdd+0xe, 0x14, true)
end

local function display_clock()
	local t = os.date("*t")
	local hour = t.hour
	local minute = t.min

	if hour > 12 then
		hour = hour - 12
	end

	if hour < 10 then
		g_clock_tbl["h1"].image = g_img_tbl[12]
	else
		g_clock_tbl["h1"].image = g_img_tbl[3]
	end

	if hour >= 10 then
		hour = hour - 10
	end

	g_clock_tbl["h2"].image = g_img_tbl[hour+2]

	g_clock_tbl["m1"].image = g_img_tbl[math.floor(minute / 10) + 2]
	g_clock_tbl["m2"].image = g_img_tbl[(minute % 10) + 2]

end

g_lounge_tbl = {}

g_tv_base_x = 0xe5
g_tv_base_y = 0x32

local function load_lounge()

	g_lounge_tbl["bg0"] = sprite_new(g_img_tbl[15], 210, 0, true)
	g_lounge_tbl["bg"] = sprite_new(g_img_tbl[0], 0, 0, true)
	g_lounge_tbl["bg1"] = sprite_new(g_img_tbl[1], 320, 0, true)
	g_lounge_tbl["avatar"] = sprite_new(g_img_tbl[0xd], 0, 63, true)

	g_lounge_tbl["tv"] = {sprite_new(nil, g_tv_base_x ,g_tv_base_y, true), sprite_new(nil, g_tv_base_x ,g_tv_base_y, true), sprite_new(nil, g_tv_base_x ,g_tv_base_y, true), sprite_new(nil, g_tv_base_x ,g_tv_base_y, true), sprite_new(nil, g_tv_base_x ,g_tv_base_y, true)}

	for i=1,5 do
		local sprite = g_lounge_tbl["tv"][i]
		sprite.clip_x = g_tv_base_x
		sprite.clip_y = g_tv_base_y
		sprite.clip_w = 57
		sprite.clip_h = 37
	end

	g_lounge_tbl["finger"] = sprite_new(g_img_tbl[0xe], 143, 91, true)

	g_lounge_tbl["tv_static"] = image_new(57,37)

	load_clock()
end

g_tv_loop_pos = 0
g_tv_loop_cnt = 0
g_tv_cur_pos = 1
g_tv_cur_program = 3

g_tv_news_image = 0
g_tv_news_image_tbl = {0x5,0x6,0x7,0x0D,0x0E,0x18,0x19,0x1F,0x20}

g_tv_programs = {
{0x82,0x82,0x80,0x3,0x2,0x8A,0x2,0x8A,0x1,0x8A,0x1,0x8A,0x0,0x8A,0x0,0x8A,0x1,0x8A,0x1,0x81}, --breathing
{0x82,0x82,0x80,0x28,0x3,0x8B,0x81}, --intestines
{0x82,0x82,0x80,0x4,0x4,0x81,0x80,0x4,0x8,0x81,0x80,0x4,0x9,0x81,0x80,0x4,0x0A,0x81,0x80,0x4,0x0B,0x81,0x80,0x4,0x0C,0x81}, --vacuum
{0x82,0x82,0x87,0x80,0x46,0x0F,0x86,0x84,0x9,0x10,0x10,0x10,0x11,0x11,0x11,0x12,0x12,0x12,0x81}, --tv anchor man
{0x82,0x82,0x80,0x32,0x83,0x81}, --endless road
{0x82,0x82,0x80,0x5,0x27,0x8A,0x28,0x8A,0x29,0x81,0x80,0x6,0x2A,0x8A,0x2A,0x8A,0x2B,0x8A,0x2B,0x8A,0x2C,0x8A,0x2C,0x8A,0x2D,0x8A,0x2D,0x81,0x80,0x0A,0x2E,0x8A,0x2F,0x8A,0x30,0x8A,0x84,0x9,0x2E,0x2E,0x2E,0x2F,0x2F,0x2F,0x30,0x30,0x30,0x8A,0x2E,0x8A,0x2F,0x8A,0x30,0x81}, --rock band
{0x82,0x82,0x80,0x55,0x16,0x17,0x84,0x0C,0x13,0x13,0x13,0x13,0x14,0x14,0x14,0x14,0x15,0x15,0x15,0x15,0x88,0x81,0x80,0x0F,0x16,0x84,0x2,0x1A,0x1B,0x89,0x88,0x81,0x80,0x3,0x16,0x1A,0x89,0x88,0x81,0x80,0x3,0x16,0x1C,0x88,0x81,0x80,0x3,0x16,0x1D,0x88,0x81,0x80,0x3,0x16,0x1E,0x88,0x81,0x80,0x3,0x16,0x23,0x88,0x81,0x80,0x3,0x16,0x24,0x88,0x81,0x80,0x32,0x16,0x88,0x81} --pledge now!
}


g_tv_x_off = {
0x0,0x0,0x0,0x0,0x0,0x1F,0x1F,0x1F,0x0,0x0,0x0,0x0,0x0,0x1F,0x1F,
0x0,0x9,0x9,0x9,0x0C,0x0C,0x0C,0x0,0x4,0x1F,0x1F,0x4,0x0,0x4,
0x4,0x4,0x1F,0x1F,0x0,0x0,0x6,0x6,0x8,0x4,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0
}

g_tv_y_off = {
0x0,0x0,0x0,0x0,0x0,0x2,0x2,0x2,0x0,0x0,0x0,0x0,0x0,0x2,0x2,0x0,0x7,0x7,
0x7,0x3,0x3,0x3,0x0,0x2,0x2,0x2,0x0,0x0,0x0,0x0,0x1,0x2,0x2,0x0,0x0,0x3,
0x8,0x1D,0x1C,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,
0x0,0x0
}

g_tv_pledge_counter = 0
g_tv_pledge_image = 37

g_tv_road_offset = 0xe

local function display_tv_sprite(s_idx, image_num)
	local sprite = g_lounge_tbl.tv[s_idx]
	if sprite ~= nil then
		sprite.image = g_img_tbl[0x10+image_num]
		sprite.x = g_tv_base_x + g_tv_x_off[image_num+1]
		sprite.y = g_tv_base_y + g_tv_y_off[image_num+1]
		if g_tv_base_x < 0 then
			sprite.clip_x = 0
		else
			sprite.clip_x = g_tv_base_x
		end
		sprite.clip_y = g_tv_base_y
		sprite.visible = true
	end
end

local function display_tv()
	local should_exit = false
	local s_idx = 1
	for i=1,5 do
		g_lounge_tbl["tv"][i].visible = false
	end

	g_lounge_tbl.finger.visible = false

	while should_exit == false do
		local item = g_tv_programs[g_tv_cur_program][g_tv_cur_pos]
		if item < 0x80 then
			display_tv_sprite(s_idx, item)
			s_idx = s_idx + 1
		elseif item == 0x82 then
			--static
			local sprite = g_lounge_tbl.tv[s_idx]
			if sprite ~= nil then
				sprite.image = g_lounge_tbl["tv_static"]
				image_static(sprite.image)
				sprite.x = g_tv_base_x
				sprite.y = g_tv_base_y
				sprite.clip_x = g_tv_base_x
				sprite.clip_y = g_tv_base_y
				sprite.visible = true
			end
			g_lounge_tbl.finger.visible = true
			should_exit = true
		elseif item == 0x80 then --loop start
			g_tv_cur_pos = g_tv_cur_pos + 1
			g_tv_loop_cnt = g_tv_programs[g_tv_cur_program][g_tv_cur_pos]
			g_tv_loop_pos = g_tv_cur_pos
		elseif item == 0x81 then --loop end
			if g_tv_loop_cnt > 0 then
				g_tv_cur_pos = g_tv_loop_pos
				g_tv_loop_cnt = g_tv_loop_cnt - 1
			end
			should_exit = true
		elseif item == 0x83 then --endless road
			g_tv_road_offset = g_tv_road_offset - 1
			if g_tv_road_offset == 0 then
				g_tv_road_offset = 0xe
			end
			g_tv_y_off[0x23] = 0x15 - g_tv_road_offset
			display_tv_sprite(s_idx, 0x22)
			s_idx = s_idx + 1
			g_tv_y_off[0x23] = 0x24 - g_tv_road_offset
			display_tv_sprite(s_idx, 0x22)
			s_idx = s_idx + 1
			display_tv_sprite(s_idx, 0x21)
			s_idx = s_idx + 1
		elseif item == 0x84 then --select random image from list.
			local rand_len = g_tv_programs[g_tv_cur_program][g_tv_cur_pos+1]
			item = g_tv_programs[g_tv_cur_program][g_tv_cur_pos+math.random(1,rand_len)+1]
			display_tv_sprite(s_idx, item)
			s_idx = s_idx + 1
			g_tv_cur_pos = g_tv_cur_pos + 1 + rand_len
		elseif item == 0x86 then --display news image
			display_tv_sprite(s_idx, g_tv_news_image)
			s_idx = s_idx + 1
		elseif item == 0x87 then --select news image
			g_tv_news_image = g_tv_news_image_tbl[math.random(1, 9)]
		elseif item == 0x88 then --pledge now!
			g_tv_pledge_counter = g_tv_pledge_counter + 1
			if g_tv_pledge_counter % 4 == 0 then
				display_tv_sprite(s_idx, g_tv_pledge_image)
				s_idx = s_idx + 1
			end

			if g_tv_pledge_counter == 16 then
				if g_tv_pledge_image == 37 then
					g_tv_pledge_image = 38
				else
					g_tv_pledge_image = 37
				end
				g_tv_pledge_counter = 0
			end
		elseif item == 0x89 then
			display_tv_sprite(s_idx, math.random(50, 52))
			s_idx = s_idx + 1
		elseif item == 0x8a then
			should_exit = true
		elseif item == 0x8b then
			canvas_rotate_palette(0xe0, 5)
		end

		g_tv_cur_pos = g_tv_cur_pos + 1
		if g_tv_programs[g_tv_cur_program][g_tv_cur_pos] == nil then
			g_tv_cur_program = g_tv_cur_program + 1
			g_tv_cur_pos = 1

			if g_tv_programs[g_tv_cur_program] == nil then
				g_tv_cur_program = 1
			end
		end
	end
end

g_tv_update = true

local function display_lounge()
	display_clock()
	if g_tv_update == true then
		display_tv()
		g_tv_update = false
	else
		g_tv_update = true
	end
end

local function scroll_clock()
g_clock_tbl["h1"].x = g_clock_tbl["h1"].x - 1
g_clock_tbl["h2"].x = g_clock_tbl["h2"].x - 1
g_clock_tbl["m1"].x = g_clock_tbl["m1"].x - 1
g_clock_tbl["m2"].x = g_clock_tbl["m2"].x - 1
end

local function scroll_lounge()
g_lounge_tbl.bg.x = g_lounge_tbl.bg.x - 1
g_lounge_tbl.bg1.x = g_lounge_tbl.bg1.x - 1
g_tv_base_x = g_tv_base_x - 1
g_lounge_tbl.avatar.x = g_lounge_tbl.avatar.x - 2
g_lounge_tbl.finger.x = g_lounge_tbl.finger.x - 2

if g_lounge_tbl.bg1.x < 166 and g_lounge_tbl.bg1.x % 2 == 0 then
	g_lounge_tbl.bg0.x = g_lounge_tbl.bg0.x - 1
end

scroll_clock()
end

local function hide_lounge()

	g_lounge_tbl["bg0"].visible = false
	g_lounge_tbl["bg"].visible = false
	g_lounge_tbl["bg1"].visible = false
	g_lounge_tbl["avatar"].visible = false
	g_lounge_tbl["tv"].visible = false
end

local function lounge_sequence()

	load_lounge()

	canvas_set_palette("palettes.int", 1)

	--for i=0,0xff do
		--display_lounge()
		--canvas_update()
	--end
	local scroll_img = image_load("blocks.shp", 1)
	local scroll = sprite_new(scroll_img, 1, 0x9f, true)

	local x, y = image_print(scroll_img, "Upon your world, five seasons have passed since your ", 8, 312, 8, 10, 0x3e)
	image_print(scroll_img, "triumphant homecoming from Britannia.", 8, 312, x, y, 0x3e)

	local input = input_poll()

	while input == nil do
		display_lounge()
		canvas_update()
		input = input_poll()
	end

	if should_exit(input) then
		return false
	end

	scroll_img = image_load("blocks.shp", 2)
	x, y = image_print(scroll_img, "You have traded the Avatar's life of peril and adventure ", 7, 310, 7, 8, 0x3e)
	x, y = image_print(scroll_img, "for the lonely serenity of a world at peace. But ", 7, 310, x, y, 0x3e)
	x, y = image_print(scroll_img, "television supermen cannot take the place of friends ", 7, 310, x, y, 0x3e)
	image_print(scroll_img, "who died at your side!", 7, 310, x, y, 0x3e)

	scroll.image = scroll_img
	scroll.x = 1
	scroll.y = 0x98

	input = input_poll()

	while input == nil do
		display_lounge()
		canvas_update()
		input = input_poll()
	end

	if should_exit(input) then
		return false
	end

	scroll_img = image_load("blocks.shp", 0)
	image_print(scroll_img, "Outside, a chill wind rises...", 39, 200, 39, 8, 0x3e)

	scroll.image = scroll_img
	scroll.x = 0x21
	scroll.y = 0x9d

	for i=0,319 do
		scroll_lounge()
		display_lounge()
		canvas_update()
		input = input_poll()
		if should_exit(input) then
			return false
		end
	end

	input = input_poll()
	while input == nil do
		canvas_update()
		input = input_poll()
	end

	if should_exit(input) then
		return false
	end

	hide_lounge()
	scroll.visible = false

	return true
end

g_window_tbl = {}
local function load_window()
	local rand = math.random
	g_window_tbl["cloud_x"] = -400

	g_window_tbl["sky"] = sprite_new(g_img_tbl[1], 0, 0, true)
	g_window_tbl["cloud1"] = sprite_new(g_img_tbl[0], g_window_tbl["cloud_x"], -5, true)
	g_window_tbl["cloud2"] = sprite_new(g_img_tbl[0], g_window_tbl["cloud_x"], -5, true)
	g_window_tbl["clouds"] = {}

	local i
	for i=1,5 do
		table.insert(g_window_tbl["clouds"], sprite_new(g_img_tbl[rand(2,3)], rand(0,320) - 260, rand(0, 30), true))
	end

	g_window_tbl["lightning"] = sprite_new(nil, 0, 0, false)
	g_window_tbl["ground"] = sprite_new(g_img_tbl[10], 0, 0x4c, true)
	g_window_tbl["trees"] = sprite_new(g_img_tbl[8], 0, 0, true)

	g_window_tbl["strike"] = sprite_new(g_img_tbl[rand(19,23)], 158, 114, false)



	--FIXME rain here.
	local rain = {}
	local i
	for i = 0,100 do
		rain[i] = sprite_new(g_img_tbl[math.random(4,7)], math.random(0,320), math.random(0,200), false)
	end
	g_window_tbl["rain"] = rain

	g_window_tbl["frame"] = sprite_new(g_img_tbl[28], 0, 0, true)
	g_window_tbl["window"] = sprite_new(g_img_tbl[26], 0x39, 0, true)
	g_window_tbl["door_left"] = sprite_new(g_img_tbl[24], 320, 0, true)
	g_window_tbl["door_right"] = sprite_new(g_img_tbl[25], 573, 0, true)

	g_window_tbl["flash"] = 0
	g_window_tbl["drops"] = 0
	g_window_tbl["rain_delay"] = 20
	g_window_tbl["lightning_counter"] = 0

end

local function hide_window()
	g_window_tbl["sky"].visible = false
	g_window_tbl["cloud1"].visible = false
	g_window_tbl["cloud2"].visible = false

	local i
	for i=1,5 do
		g_window_tbl["clouds"][i].visible = false
	end

	g_window_tbl["lightning"].visible = false
	g_window_tbl["ground"].visible = false
	g_window_tbl["trees"].visible = false

	g_window_tbl["strike"].visible = false

	local i
	for i = 0,100 do
		g_window_tbl["rain"][i].visible = false
	end

	g_window_tbl["frame"].visible = false
	g_window_tbl["window"].visible = false
	g_window_tbl["door_left"].visible = false
	g_window_tbl["door_right"].visible = false

end

local function display_window()

	local i
	local rain = g_window_tbl["rain"]
	local rand = math.random
	local c_num

	--FIXME display clouds here.
	local cloud
	for i,cloud in ipairs(g_window_tbl["clouds"]) do
		if cloud.x > 320 then
			cloud.x = rand(0, 320) - 320
			cloud.y = rand(0, 30)
		end

		cloud.x = cloud.x + 2
	end

	g_window_tbl["cloud_x"] = g_window_tbl["cloud_x"] + 1
	if g_window_tbl["cloud_x"] == 320 then
		g_window_tbl["cloud_x"] = 0
	end

	g_window_tbl["cloud1"].x = g_window_tbl["cloud_x"]
	g_window_tbl["cloud2"].x = g_window_tbl["cloud_x"] - 320

	if rand(0, 6) == 0 and g_window_tbl["lightning_counter"] == 0 then --fixme var_1a, var_14
		g_window_tbl["lightning_counter"] = rand(1, 4)
		g_window_tbl["lightning"].image = g_img_tbl[rand(11,18)]
		g_window_tbl["lightning"].visible = true
		g_window_tbl["lightning_x"] = rand(-5,320)
		g_window_tbl["lightning_y"] = rand(-5,200)
	end

	if g_window_tbl["lightning_counter"] > 0 then
		g_window_tbl["lightning"].x = g_window_tbl["lightning_x"] + rand(0, 3)
		g_window_tbl["lightning"].y = g_window_tbl["lightning_y"] + rand(0, 3)
	end

	if (g_window_tbl["lightning_counter"] > 0 and rand(0,3) == 0) or g_window_tbl["strike"].visible == true then
		canvas_set_palette_entry(0x58, 0x40,0x94,0xfc)
		canvas_set_palette_entry(0x5a, 0x40,0x94,0xfc)
		canvas_set_palette_entry(0x5c, 0x40,0x94,0xfc)
	else
		canvas_set_palette_entry(0x58, 0x20,0xa4,0x80)
		canvas_set_palette_entry(0x5a, 0x14,0x8c,0x74)
		canvas_set_palette_entry(0x5c, 0x0c,0x74,0x68)
	end

	if rand(0,1) == 0 then
		g_window_tbl["strike"].image = g_img_tbl[rand(19,23)]
	end

	if g_window_tbl["flash"] > 0 then
		g_window_tbl["flash"] = g_window_tbl["flash"] - 1
	else
		if rand(0,5) == 0 or g_window_tbl["strike"].visible == true then
			g_window_tbl["window"].image = g_img_tbl[27]
			g_window_tbl["flash"] = rand(1, 5)
		else
			g_window_tbl["window"].image = g_img_tbl[26]
		end
	end

	if g_window_tbl["drops"] < 100 then
		if rand(0,g_window_tbl["rain_delay"]) == 0 then
			g_window_tbl["rain_delay"] = g_window_tbl["rain_delay"] - 2
			if g_window_tbl["rain_delay"] < 1 then
				g_window_tbl["rain_delay"] = 1
			end
			i = g_window_tbl["drops"]
			rain[i].visible = true
			rain[i].image = g_img_tbl[rand(4,7)]
			rain[i].y = -4
			rain[i].x = rand(0,320)
			g_window_tbl["drops"] = i + 1
		end
	end

	for i = 0,g_window_tbl["drops"] do

		if rain[i].visible == true then
			rain[i].x = rain[i].x + 2
			rain[i].y = rain[i].y + 8
			if rain[i].x > 320 or rain[i].y > 200 then
				rain[i].visible = false
			end
		else
			rain[i].visible = true
			rain[i].image = g_img_tbl[rand(4,7)]
			rain[i].y = -4
			rain[i].x = rand(0,320)
		end
	end

	if g_window_tbl["lightning_counter"] > 0 then
		g_window_tbl["lightning_counter"] = g_window_tbl["lightning_counter"] - 1
	end
end

local function window_update()
	local input = input_poll()

	while input == nil do
		display_window()
		canvas_update()
		input = input_poll()
		if input ~= nil then
			break
		end
		canvas_update()
	end

	return should_exit(input)
end

local function window_sequence()
	load_images("intro_2.shp")

	load_window()

	canvas_set_palette("palettes.int", 2)

	local i = 0
	local input
	while i < 20 do
		display_window()
		canvas_update()
		input = input_poll()
		if should_exit(input) then
			return false
		end

		i = i + 1
		canvas_update()
	end

	local scroll_img = image_load("blocks.shp", 1)
	local scroll = sprite_new(scroll_img, 1, 0x98, true)

	local x, y = image_print(scroll_img, "...and in moments, the storm is upon you.", 8, 312, 36, 14, 0x3e)

	if window_update() == true then
		return false
	end

	scroll_img = image_load("blocks.shp", 1)
	x, y = image_print(scroll_img, "Tongues of lightning lash the sky, conducting an unceasing ", 8, 310, 8, 10, 0x3e)
	image_print(scroll_img, "crescendo of thunder....", 8, 310, x, y, 0x3e)
	scroll.image = scroll_img

	if window_update() == true then
		return false
	end

	scroll_img = image_load("blocks.shp", 1)
	x, y = image_print(scroll_img, "In a cataclysm of sound and light, a bolt of searing ", 8, 310, 8, 10, 0x3e)
	image_print(scroll_img, "blue fire strikes the earth!", 8, 310, x, y, 0x3e)
	scroll.image = scroll_img

	g_window_tbl["strike"].visible = true

	if window_update() == true then
		return false
	end

	scroll_img = image_load("blocks.shp", 1)
	x, y = image_print(scroll_img, "Lightning among the stones!", 8, 310, 73, 10, 0x3e)
	image_print(scroll_img, "Is this a sign from distant Britannia?", 8, 310, 41, 18, 0x3e)
	scroll.image = scroll_img

	--scroll window.
	i = 0
	while i < 320 do

		display_window()
		canvas_update()
		input = input_poll()
		if should_exit(input) then
			return false
		end

		canvas_update()

		g_window_tbl["window"].x = g_window_tbl["window"].x - 2
		g_window_tbl["frame"].x = g_window_tbl["frame"].x - 2
		g_window_tbl["door_left"].x = g_window_tbl["door_left"].x - 2
		g_window_tbl["door_right"].x = g_window_tbl["door_right"].x - 2
		i = i + 2
		if i > 160 and g_window_tbl["strike"].visible == true then
			g_window_tbl["strike"].visible = false
		end
	end

	if window_update() == true then
		return false
	end

	scroll.visible = false
	i = 0
	while i < 68 do

		display_window()
		canvas_update()
		input = input_poll()
		if should_exit(input) then
			return false
		end

		canvas_update()
		input = input_poll()

		g_window_tbl["door_left"].x = g_window_tbl["door_left"].x - 1
		g_window_tbl["door_right"].x = g_window_tbl["door_right"].x + 1
		i = i + 1
	end

	scroll_img = image_load("blocks.shp", 2)
	x, y = image_print(scroll_img, "You bolt from your house, stumbling, running blind in the", 7, 310, 8, 12, 0x3e)
	x, y = image_print(scroll_img, " storm. Into the forest, down the path, through the ", 7, 310, x, y, 0x3e)
	image_print(scroll_img, "rain... to the stones.", 7, 310, x, y, 0x3e)
	scroll.image = scroll_img
	scroll.visible = true

	if window_update() == true then
		return false
	end

	scroll.visible = false

end

local function stones_rotate_palette()
	if g_pal_counter == 4 then
		canvas_rotate_palette(144, 16)
		g_pal_counter = 0
	else
		g_pal_counter = g_pal_counter + 1
	end
end

local function stones_update()
	local input = input_poll()

	while input == nil do
		stones_rotate_palette()
		canvas_update()
		input = input_poll()
		if input ~= nil then
			break
		end
	end

	return should_exit(input)
end

local function stones_shake_moongate()
	local input = input_poll()

	while input == nil do
		stones_rotate_palette()
		g_stones_tbl["moon_gate"].x = 0x7c + math.random(0, 1)
		g_stones_tbl["moon_gate"].y = 5 + math.random(0, 3)
		canvas_update()
		stones_rotate_palette()
		g_stones_tbl["moon_gate"].x = 0x7c + math.random(0, 1)
		g_stones_tbl["moon_gate"].y = 5 + math.random(0, 3)
		canvas_update()
		input = input_poll()
		if input ~= nil then
			break
		end
	end

	return should_exit(input)
end

g_pal_counter = 0

g_stones_tbl = {}

local function stones_sequence()

	load_images("intro_3.shp")

	canvas_set_palette("palettes.int", 3)

	g_stones_tbl["bg"] = sprite_new(g_img_tbl[0], 0, 0, true)
	g_stones_tbl["stone_cover"] = sprite_new(g_img_tbl[3], 0x96, 0x64, false)
	g_stones_tbl["gate_cover"] = sprite_new(g_img_tbl[4], 0x5e, 0x66, false)
	g_stones_tbl["hand"] = sprite_new(g_img_tbl[1], 0xbd, 0xc7, false)
	g_stones_tbl["moon_gate"] = sprite_new(g_img_tbl[2], 0x7c, 0x64, false)
	g_stones_tbl["moon_gate"].clip_x = 0
	g_stones_tbl["moon_gate"].clip_y = 0
	g_stones_tbl["moon_gate"].clip_w = 320
	g_stones_tbl["moon_gate"].clip_h = 0x66
	g_stones_tbl["avatar"] = sprite_new(g_img_tbl[7], -2, 0x12, false)

	local scroll_img = image_load("blocks.shp", 2)
	local scroll = sprite_new(scroll_img, 1, 0xc, true)

	local x, y = image_print(scroll_img, "Near the stones, the smell of damp, blasted earth hangs ", 7, 303, 7, 8, 0x3e)
	x, y = image_print(scroll_img, "in the air. In a frozen moment of lightning-struck ", 7, 303, x, y, 0x3e)
	x, y = image_print(scroll_img, "daylight, you glimpse a tiny obsidian stone in the ", 7, 303, x, y, 0x3e)
	x, y = image_print(scroll_img, "midst of the circle!", 7, 303, x, y, 0x3e)

	fade_in()

	if stones_update() == true then
		return false
	end

	g_stones_tbl["stone_cover"].visible = true
	g_stones_tbl["hand"].visible = true

	scroll_img = image_load("blocks.shp", 0)
	image_print(scroll_img, "Wondering, you pick it up....", 8, 234, 0x2a, 8, 0x3e)
	scroll.image = scroll_img
	scroll.x = 0x21
	scroll.y = 0x1e

	local i
	for i=0xc7,0x54,-2  do
		--		display_stones()
		g_stones_tbl["hand"].y = i
		canvas_update()
		local input = input_poll()
		if input ~= nil and should_exit(input) then
			return false
		end
	end

	if stones_update() == true then
		return false
	end

	g_stones_tbl["bg"].image = g_img_tbl[5]
	g_stones_tbl["stone_cover"].visible = false
	g_stones_tbl["gate_cover"].visible = true

	scroll_img = image_load("blocks.shp", 1)
	x, y = image_print(scroll_img, "...and from the heart of the stones, a softly glowing door ", 7, 303, 7, 10, 0x3e)
	image_print(scroll_img, "ascends in silence!", 7, 303, x, y, 0x3e)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0xa0

	g_stones_tbl["moon_gate"].visible = true

	for i=0x64,0x5,-1  do
		--		display_stones()
		stones_rotate_palette()
		g_stones_tbl["moon_gate"].y = i
		canvas_update()
		local input = input_poll()
		if input ~= nil and should_exit(input) then
			return false
		end
	end

	for i=0x54,0xc7,2  do
		--		display_stones()
		stones_rotate_palette()
		g_stones_tbl["hand"].y = i
		canvas_update()
		local input = input_poll()
		if input ~= nil and should_exit(input) then
			return false
		end
	end

	g_stones_tbl["hand"].visible = false

	if stones_update() == true then
		return false
	end

	g_stones_tbl["hand"].image = g_img_tbl[6]
	g_stones_tbl["hand"].x = 0x9b
	g_stones_tbl["hand"].visible = true
	scroll.visible = false

	for i=0xc7,0x44,-2  do
		stones_rotate_palette()
		g_stones_tbl["hand"].y = i
		g_stones_tbl["bg"].y = g_stones_tbl["bg"].y - 1
		g_stones_tbl["gate_cover"].y = g_stones_tbl["gate_cover"].y - 1
		g_stones_tbl["moon_gate"].y = g_stones_tbl["moon_gate"].y - 1
		canvas_update()
		local input = input_poll()
		if input ~= nil and should_exit(input) then
			return false
		end
	end

	scroll_img = image_load("blocks.shp", 2)
	x, y = image_print(scroll_img, "Exultant memories wash over you as you clutch the stone. ", 7, 303, 7, 8, 0x3e)
	x, y = image_print(scroll_img, "When last you saw an orb such as this, it was cast down ", 7, 303, x, y, 0x3e)
	image_print(scroll_img, "by Lord British to banish the tyrant Blackthorn!", 7, 303, x, y, 0x3e)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0x98
	scroll.visible = true

	if stones_update() == true then
		return false
	end

	scroll.visible = false

	for i=0x44,0xc7,2  do
		stones_rotate_palette()
		g_stones_tbl["hand"].y = i
		g_stones_tbl["bg"].y = g_stones_tbl["bg"].y + 1
		g_stones_tbl["gate_cover"].y = g_stones_tbl["gate_cover"].y + 1
		g_stones_tbl["moon_gate"].y = g_stones_tbl["moon_gate"].y + 1
		canvas_update()
		local input = input_poll()
		if input ~= nil and should_exit(input) then
			return false
		end
	end

	g_stones_tbl["hand"].visible = false

	scroll_img = image_load("blocks.shp", 2)
	image_print(scroll_img, "But your joy soon gives way to apprehension.", 7, 303, 16, 8, 0x3e)
	image_print(scroll_img, "The gate to Britannia has always been blue...", 7, 303, 18, 24, 0x3e)
	image_print(scroll_img, "as blue as the morning sky.", 7, 303, 76, 32, 0x3e)
	scroll.image = scroll_img
	scroll.visible = true

	if stones_update() == true then
		return false
	end

	scroll_img = image_load("blocks.shp", 1)
	x,y = image_print(scroll_img, "Abruptly, the portal quivers and begins to sink ", 7, 303, 7, 10, 0x3e)
	image_print(scroll_img, "into the ground.  Its crimson light wanes!", 7, 303, x, y, 0x3e)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0xa0

	if stones_shake_moongate() == true then
		return false
	end

	scroll_img = image_load("blocks.shp", 1)
	x,y = image_print(scroll_img, "Desperation makes the decision an easy one.", 7, 303, 22, 14, 0x3e)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0xa0

	if stones_shake_moongate() == true then
		return false
	end

	scroll.visible = false

	g_stones_tbl["avatar"].visible = true

	canvas_set_palette_entry(0x19, 0, 0, 0)

	for i=0,19,1 do
		g_stones_tbl["avatar"].image = g_img_tbl[7+i]

		local j
		for j=0,4 do
			canvas_update()
			stones_rotate_palette()
			g_stones_tbl["moon_gate"].x = 0x7c + math.random(0, 1)
			g_stones_tbl["moon_gate"].y = 5 + math.random(0, 3)
		end

		g_stones_tbl["avatar"].y = g_stones_tbl["avatar"].y - 3

		local input = input_poll()
		if input ~= nil and should_exit(input) then
			return false
		end
	end


	for i=0xff,0,-3 do
		canvas_update()
		stones_rotate_palette()
		g_stones_tbl["moon_gate"].x = 0x7c + math.random(0, 1)
		g_stones_tbl["moon_gate"].y = 5 + math.random(0, 3)

		g_stones_tbl["avatar"].opacity = i

		local input = input_poll()
		if input ~= nil and should_exit(input) then
			return false
		end
	end

	canvas_set_palette_entry(0x19, 0x74, 0x74, 0x74)

	g_stones_tbl["moon_gate"].x = 0x7c

	for i=0x5,0x64,1  do
		stones_rotate_palette()
		g_stones_tbl["moon_gate"].y = i
		canvas_update()
		local input = input_poll()
		if input ~= nil and should_exit(input) then
			return false
		end
	end

	g_stones_tbl["bg"].image = g_img_tbl[0]
	g_stones_tbl["moon_gate"].visible = false
	g_stones_tbl["gate_cover"].visible = false
	g_stones_tbl["stone_cover"].visible = true

	if stones_update() == true then
		return false
	end

	return true
end

local function play()
mouse_cursor_set_pointer(0)
mouse_cursor_visible(false)
load_images("intro_1.shp")
music_play("bootup.m")
--[ [
canvas_set_palette("palettes.int", 0)
canvas_set_update_interval(25)

local img = g_img_tbl[0x45]

local background = sprite_new(img)

background.x = 0
background.y = 0
background.opacity = 0
background.visible = true

music_play("bootup.m")
for i=0,0xff,3 do
	background.opacity = i
	if poll_for_esc() == true then return end
	canvas_update()
end

for i=0xff,0,-3 do
background.opacity = i
	if poll_for_esc() == true then return end

--if input ~= nil and input == ESCAPE_KEY then
--	return
--end
canvas_update()
end

img = g_img_tbl[0x46]

background.image = img

for i=0,0xff,3 do
	background.opacity = i
		if poll_for_esc() == true then return end

	canvas_update()
end

for i=0xff,0,-3 do
background.opacity = i
	if poll_for_esc() == true then return end

canvas_update()
end

background.visible = false



if lounge_sequence() == false then
	return
end

if window_sequence() == false then
	return
end

fade_out()
hide_window()
--] ]
stones_sequence()
end

local function gypsy_update_bubbles(bubble_image)
	if g_gypsy_tbl["bubble_counter"] == 0 then
		image_bubble_effect(bubble_image)
		g_gypsy_tbl["bubble_counter"] = 3
	else
		g_gypsy_tbl["bubble_counter"] = g_gypsy_tbl["bubble_counter"] - 1
	end
end


local function gypsy_ab_select(question)
	local a_lookup_tbl = {
	0, 0, 0, 0, 0, 0, 0, 1,
	1, 1, 1, 1, 1, 2, 2, 2,
	2, 2, 3, 3, 3, 3, 4, 4,
	4, 5, 5, 6,
	}

	local b_lookup_tbl = {
	1, 2, 3, 4, 5, 6, 7, 2,
	3, 4, 5, 6, 7, 3, 4, 5,
	6, 7, 4, 5, 6, 7, 5, 6,
	7, 6, 7, 7,
	}
	if g_highlight_index == nil then
		g_highlight_index = 0
	end

	local input = nil
	while input == nil do
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if input == 65 or input == 97 or (input == 13 and g_highlight_index == 0) then
				return a_lookup_tbl[question - 104] + 1
			elseif input == 66 or input == 98 or input == 13 then
				return b_lookup_tbl[question - 104] + 1
			elseif input == SDLK_RIGHT and g_highlight_index == 0 then -- right
				g_highlight_index = 1
				g_ab_highlight.x = g_ab_highlight.x + 17
			elseif input == SDLK_LEFT and g_highlight_index == 1 then -- left
				g_highlight_index = 0
				g_ab_highlight.x = g_ab_highlight.x - 17
			elseif input == 0 then
				local y = get_mouse_y()
				if(y > 173 and y < 190) then
					local x = get_mouse_x()
					if x > 278 and x < 296 then
						return a_lookup_tbl[question - 104] + 1
					elseif x > 296 and x < 313 then
						return b_lookup_tbl[question - 104] + 1
					end
				end
			end
			input = nil
		end
		if g_gypsy_tbl["jar_liquid"].visible == true then
			gypsy_update_bubbles(g_gypsy_tbl["jar_liquid"].image)
		end
	end
end


local gypsy_question_text = {
"\"Entrusted to deliver an uncounted purse of gold, thou dost meet a poor beggar. Dost thou A) deliver the gold Honestly, knowing the trust in thee was well-placed; or B) show Compassion and give the beggar a coin, knowing it won't be missed?\127",
"\"Thou has been prohibited by thy absent Lord from joining thy friends in a close-pitched battle. Dost thou A) refrain, so thou may Honestly claim obedience; or B) show Valor and aid thy comrades, knowing thou may deny it later?\127",
"\"A merchant owes thy friend money, now long past due. Thou dost see the same merchant drop a purse of gold. Dost thou A) Honestly return the purse intact; or B) Justly give thy friend a portion of the gold first?\127",
"\"Thee and thy friend are valiant but penniless warriors. Thou both set forth to slay a mighty dragon. Thy friend dost think he slew it, but the killing blow was thine. Dost thou A) Honestly claim the reward; or B) Sacrifice the gold for the sake of thy friendship?\127",
"\"Thou art sworn to protect thy Lord at any cost, yet thou knowest he hath committed a crime. Authorities ask thee of the affair. Dost thou A) Break thine oath by Honestly speaking; or B) uphold Honor by silently keeping thine oath?\127",
"\"Thy friend doth seek admittance to thy spiritual order. Thou art asked to vouch for his purity of spirit, of which thou art uncertain. Dost thou A) Honestly express thy doubt; or B) Vouch for him, hoping for his Spiritual improvement?\127",
"\"Thy Lord doth mistakenly believe he slew a dragon. Thou hast proof that thy lance felled the beast. When asked, dost thou A) Honestly claim the kill; or B) Humbly permit thy Lord his belief?\127",
"\"Thou dost manage to disarm thy mortal enemy in a duel. He is at thy mercy. Dost thou A) Show Compassion by permitting him to yield; or B) Slay him, as expected of a Valiant duelist?\127",
"\"After twenty years thou hast found the slayer of thy best friends. The villain proves to be a man who provides the sole support for a young girl. Dost thou A) Spare him in Compassion for the child; or B) Slay him in the name of Justice?\127",
"\"Thee and thy friends hath been routed and ordered to retreat. In defiance of thy orders, dost thou A) Stop in Compassion to aid a wounded comrade; or B) Sacrifice thyself to slow the pursuing enemy so that others might escape?\127",
"\"Thou art sworn to uphold a Lord who doth participate in the forbidden torture of prisoners. Each night their cries of pain reach thee. Dost thou A) Show Compassion by reporting the deeds; or B) Honor thy oath and ignore the deeds?\127",
"\"Thou hast been taught to preserve all life as sacred. A man lies fatally stung by a venomous serpent. He prays for a merciful death. Dost thou A) Show Compassion and end his pain; or B) heed thy Spiritual beliefs and turn away?\127",
"\"The Captain of the King's Guard asks that one among thee visit a hospital to cheer the children with tales of valiant personal deeds. Dost thou A) Show thy Compassion and play the braggart; or B) Humbly let another go?\127",
"\"Thou hast been sent to secure a needed treaty with a distant Lord. Thy host is agreeable to thy proposal but insults thy country at dinner. Dost thou A) Valiantly bear the slurs; or B) Justly rise and demand an apology?\127",
"\"A burly knight accosts thee and demands thy food. Dost thou A) Valiantly refuse and engage the knight, or B) Sacrifice thy rations unto the hungry knight?\127",
"\"During battle thou art ordered to guard thy commander's empty tent. The battle goes poorly and thou dost yearn to aid thy fellows. Dost thou A) Valiantly enter the battle; or B) Honor thy post as guard?\127",
"\"A local bully pushes for a fight. Dost thou A) Valiantly trounce the rogue; or B) Decline, knowing in thy Spirit that no lasting good will come of it?\127",
"\"Although a simple fisherman, thou art also a skillful swordsman. Thy Lord dost seek to assemble a peacetime guard. Dost thou A) Answer the call, so that all may witness thy Valor; or B) Humbly decline the offer to join thy Lord's largely ceremonial knighthood?\127",
"\"During a pitched battle, thou dost see a fellow desert his post, endangering many. As he flees, he is set upon by several enemies. Dost thou A) Justly let him fight alone; or B) Risk the Sacrifice of thine own life to aid him?\127",
"\"Thou hast sworn to do thy Lord's bidding in all. He covets a piece of land and orders its owner removed. Dost thou A) Serve Justice, refusing to act, thus being disgraced; or B) Honor thine oath and evict the landowner?\127",
"\"Thou dost believe that virtue resides in all people. Thou dost see a rogue steal from thy Lord. Dost thou A) Call him to Justice; or B) Personally try to sway him back to the Spiritual path of good?\127",
"\"Unwitnessed, thou hast slain a mighty dragon in defense of thy life. A poor warrior claims the offered reward. Dost thou A) Justly step forward to claim the bounty; or B) Humbly go about thy life, secure in thy self-esteem?\127",
"\"Thou art a bounty hunter sworn to return an alleged murderer. After his capture, thou dost come to believe him innocent. Dost thou A) Sacrifice thy sizable bounty for thy belief; or B) Honor thine oath to return him as promised?\127",
"\"Thou hast spent thy life in charitable and righteous work. Thine uncle the innkeeper lies ill and asks thee to take over his tavern. Dost thou A) Sacrifice thy life of purity to aid thy kin; or B) Decline and follow the call of Spirituality?\127",
"\"Thou art an elderly, wealthy merchant. Thy end is near. Dost thou A) Sacrifice all thy wealth to feed hundreds of starving children, receiving public adulation; or B) Humbly live out thy life, willing thy fortune to thy heirs?\127",
"\"In thy youth thou didst pledge to marry thy sweetheart. Now thou art on a sacred quest in distant lands. Thy sweetheart doth ask thee to keep thy vow. Dost thou A) Honor thy pledge to wed; or B) Follow thy Spiritual crusade?\127",
"\"Though thou art but a peasant shepherd, thou art discovered to be the sole descendant of a noble family long thought extinct. Dost thou A) Honorably take up the arms of thy ancestors; or B) Humbly resume thy life of simplicity and peace?\127",
"\"Thy parents wish thee to become an apprentice. Two positions are available. Dost thou A) Become an acolyte in a worthy Spiritual order; or B) Become an assistant to a humble village cobbler?\127",
}

	local gypsy_questions = {
	-1, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
	0x69, -1, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
	0x6A, 0x70, -1, 0x76, 0x77, 0x78, 0x79, 0x7A,
	0x6B, 0x71, 0x76, -1, 0x7B, 0x7C, 0x7D, 0x7E,
	0x6C, 0x72, 0x77, 0x7B, -1, 0x7F, 0x80, 0x81,
	0x6D, 0x73, 0x78, 0x7C, 0x7F, -1, 0x82, 0x83,
	0x6E, 0x74, 0x79, 0x7D, 0x80, 0x82, -1, 0x84,
	0x6F, 0x75, 0x7A, 0x7E, 0x81, 0x83, 0x84, -1,
	}

	g_str = 0xf
	g_dex = 0xf
	g_int = 0xf

	g_question_tbl = {0, 1, 2, 3, 4, 5, 6, 7}

local function shuffle_question_tbl(shuffle_len)
	local random = math.random
	local c = random(0, (shuffle_len * shuffle_len)-1) + shuffle_len

	for i=0,c,1 do
		local j = random(1, shuffle_len)
		local k = random(1, shuffle_len)

		local tmp = g_question_tbl[j]

		g_question_tbl[j] = g_question_tbl[k]
		g_question_tbl[k] = tmp
	end

end

local function gypsy_start_pouring(vial_num, vial_level)
	local pour_img_tbl =
	{
	 0x2B, 0x43, 0x56, 0x6B, 0x24, 0x74, 0x19, 0x2C,
	 0x2C, 0x44, 0x57, 0x6C, 0x23, 0x73, 0x18, 0x2B,
	 0x2E, 0x45, 0x58, 0x6D, 0x22, 0x72, 0x17, 0x2A,
	}

	if vial_level <= 3 and vial_level > 0 then
		g_gypsy_tbl["pour"].visible = true
		local img1 = pour_img_tbl[(3 - vial_level) * 8 + vial_num]
		--io.stderr:write("pour: "..vial_level.." img1="..img1.."vial_num="..vial_num.."\n")
		if vial_num > 6 then
			img1 = img1 + 9
		else
			img1 = img1 + 0x42
		end

		g_gypsy_tbl["pour"].image = g_gypsy_img_tbl[img1]

		local pour_y_tbl = {0x32, 0x37, 0x40}
		g_gypsy_tbl["pour"].y = pour_y_tbl[3 - vial_level + 1] - 20

		local pour_x_tbl =
		{
		 0x92, 0x92, 0x92, 0x92, 0x0A9, 0x0A9, 0x0A9, 0x0A9,
		 0x91, 0x91, 0x91, 0x91, 0x0A9, 0x0A9, 0x0A9, 0x0A9,
		 0x94, 0x94, 0x94, 0x94, 0x0AA, 0x0AA, 0x0AA, 0x0AA,
		}

		g_gypsy_tbl["pour"].x = pour_x_tbl[(3 - vial_level) * 8 + vial_num]
	end

	g_gypsy_tbl["jar_level"] = g_gypsy_tbl["jar_level"] + 1

	g_gypsy_tbl["jar_liquid"].visible = true
	g_gypsy_tbl["jar_liquid"].image = g_gypsy_img_tbl[8 + g_gypsy_tbl["jar_level"]]
	g_gypsy_tbl["jar_liquid"].y = g_gypsy_tbl["jar_liquid"].y - 1

	local vial_colors = {239, 14, 231, 103, 228, 5, 15, 219}

	image_bubble_effect_add_color(vial_colors[vial_num])
	g_gypsy_tbl["bubble_counter"] = 0

end

local function gypsy_stop_pouring()
	g_gypsy_tbl["pour"].visible = false
end

local function gypsy_vial_anim_liquid(img_num, vial_num, vial_level, hand_x, hand_y)

	--io.stderr:write(img_num..", "..vial_num..", "..vial_level..", "..hand_x..", "..hand_y.."\n")

	if vial_level == 3 then
		vial_level = 2
	end

	if vial_level <= 0 then
		g_gypsy_tbl["vial_liquid"][vial_num].visible = false
		return
	end

	local si = 0
	if img_num == 0xf then return end


	if img_num > 0xf then
		if img_num == 0x12 then
			si = 1
		else
			if img_num > 0x12 then
				if img_num == 0x25 then return end
				if img_num == 0x2e then
					si = 2
				end
			else
				if img_num == 0x10 then
					si = 2
				else
					if img_num == 0x11 then
						si = 0
					end
				end
			end
		end

	else

		img_num = img_num - 9
		if img_num == 0 then
			si = 0
		elseif img_num == 1 then
			si = 1
		elseif img_num >= 2 and img_num <= 5 then
			si = 3
		end

	end

	local vial_liquid_tbl = {
	0x3A, 0x4F, 0x64, 0x7C, 0x28, 0x7E, 0x20, 0x34,
	0x36, 0x4B, 0x60, 0x76, 0x26, 0x78, 0x1C, 0x30,
	0x36, 0x4B, 0x60, 0x76, 0x26, 0x78, 0x1C, 0x30,
	0x3C, 0x51, 0x66, 0x7F, 0x14, 0x5F, 0x22, 0x36,
	0x38, 0x4D, 0x62, 0x79, 0x27, 0x7B, 0x1E, 0x32,
	0x38, 0x4D, 0x62, 0x79, 0x27, 0x7B, 0x1E, 0x32,
	0x40, 0x53, 0x68, 0x81, 0x29, 0x83, 0x24, 0x38,
	0x40, 0x53, 0x68, 0x81, 0x29, 0x83, 0x24, 0x38,
	0x40, 0x53, 0x68, 0x81, 0x29, 0x83, 0x24, 0x38,
	0x0, 0x3, 0x6, 0x9, 0x16, 0x19, 0x1C, 0x1F,
	0x1, 0x4, 0x7, 0x0A, 0x17, 0x1A, 0x1D, 0x20,
	0x2, 0x5, 0x8, 0x0B, 0x18, 0x1B, 0x1E, 0x21
	}

	local img_offset

	if vial_num > 6 and si ~= 3 then
		img_offset = 9
	else
		img_offset = 0x42
	end

	--io.stderr:write("si ="..si.."\n")

	if vial_level > 0 and vial_level < 3 then
		--vial_liquid_tbl[vial_level * 2 + si * 24 + vial_num] + img_offset
		local img_idx = vial_liquid_tbl[(vial_level-1) * 8 + si * 24 + vial_num]
		g_gypsy_tbl["vial_liquid"][vial_num].image = g_gypsy_img_tbl[img_idx + img_offset]

		local hand_y_tbl = {0x1B, 0x13, 0x13, 0x16, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0B, 0x0B, 0x0B}
		g_gypsy_tbl["vial_liquid"][vial_num].y = hand_y + hand_y_tbl[si * 3 + vial_level] - 20

		local hand_x_tbl =
		{
		0x4, 0x4, 0x4, 0x4, 0x14, 0x14, 0x14, 0x14,
		0x4, 0x4, 0x4, 0x4, 0x13, 0x13, 0x13, 0x13,
		0x4, 0x4, 0x4, 0x4, 0x13, 0x13, 0x13, 0x13,
		0x4, 0x4, 0x4, 0x4, 0x17, 0x17, 0x17, 0x17,
		0x3, 0x3, 0x3, 0x3, 0x17, 0x17, 0x17, 0x17,
		0x3, 0x3, 0x3, 0x3, 0x17, 0x17, 0x17, 0x17,
		0x3, 0x3, 0x3, 0x3, 0x1B, 0x1B, 0x1B, 0x1B,
		0x3, 0x3, 0x3, 0x3, 0x1B, 0x1B, 0x1B, 0x1B,
		0x3, 0x3, 0x3, 0x3, 0x1B, 0x1B, 0x1B, 0x1B,
		0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
		0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
		0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
		}

		local hand_x_tbl1 = {10, 10, 10, 10, -13, -13, -13, -13}

		g_gypsy_tbl["vial_liquid"][vial_num].x = hand_x + hand_x_tbl[si * 24 + (vial_level-1) * 8 + vial_num] + hand_x_tbl1[vial_num]
	end

end

local function gypsy_vial_anim(vial)

	local vial_level = g_gypsy_tbl["vial_level"]
	local vial_img_off = {2, 5, 8, 0xB, 0x18, 0x1B, 0x1E, 0x21}

	local vial_x_offset = {41, 62, 83, 104, 200, 221, 242, 263}

	local idx

	--g_gypsy_tbl["vial"][vial].visible = false
	--g_gypsy_tbl["vial_liquid"][vial].visible = false


	local arm_img_tbl = {0, 0, 0, 0, 5, 5, 5, 5}
	local arm_img_tbl1 = {1, 1, 1, 1, 4, 4, 4, 4}
	local arm_img_tbl2 = {2, 2, 2, 2, 3, 3, 3, 3}

	local hand_img_tbl = {12, 12, 12, 12, 13, 13, 13, 13}
	local hand_img_tbl1 = {9, 9, 9, 9, 17, 17, 17, 17}
	local hand_img_tbl2 = {10, 10, 10, 10, 18, 18, 18, 18}
	local hand_img_tbl3 = {16, 16, 16, 16, 46, 46, 46, 46}
	local hand_img_tbl4 = {15, 15, 15, 15, 37, 37, 37, 37}

	local arm_x_offset = {93, 93, 93, 93, 172, 172, 172, 172}

	local hand_x_offset = {29, 50, 71, 92, 202, 223, 244, 264}
	local hand_x_offset1 = {107, 107, 107, 107, 170, 170, 170, 170}
	local hand_x_offset2 = {109, 109, 109, 109, 168, 168, 168, 168}
	local hand_x_offset3 = {112, 112, 112, 112, 165, 165, 165, 165}
	local hand_x_offset4 = {10, 10, 10, 10, -13, -13, -13, -13}

	local hand_img_num
	local should_update
	for idx=0,16 do
		should_update = true
		if idx == 0 then
			--
			hand_img_num = 0xf
			should_update = false
		elseif idx == 1 or idx == 15 then
			--
			g_gypsy_tbl["arm"].visible = true
			g_gypsy_tbl["hand"].visible = true
			g_gypsy_tbl["vial"][vial].visible = false
			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl[vial]]
			g_gypsy_tbl["arm"].x = vial_x_offset[vial] - 6
			g_gypsy_tbl["arm"].y = 21

			hand_img_num = hand_img_tbl[vial]
			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
			g_gypsy_tbl["hand"].x = vial_x_offset[vial]
			g_gypsy_tbl["hand"].y = 66
			g_gypsy_tbl["hand"].visible = true
		elseif idx == 2 or idx == 14 then
			--
			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl1[vial]]
			g_gypsy_tbl["arm"].x = vial_x_offset[vial] - 6
			g_gypsy_tbl["arm"].y = 21

			hand_img_num = hand_img_tbl[vial]
			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
			g_gypsy_tbl["hand"].x = vial_x_offset[vial]
			g_gypsy_tbl["hand"].y = 29

		elseif idx == 3 or idx == 13 then
			--
			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl1[vial]]
			g_gypsy_tbl["arm"].x = vial_x_offset[vial] - 6
			g_gypsy_tbl["arm"].y = 21

			hand_img_num = hand_img_tbl1[vial]
			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
			g_gypsy_tbl["hand"].x = hand_x_offset[vial]
			g_gypsy_tbl["hand"].y = 25

		elseif idx == 4 or idx == 12 then
			--
			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl2[vial]]
			g_gypsy_tbl["arm"].x = arm_x_offset[vial]
			g_gypsy_tbl["arm"].y = 21

			hand_img_num = hand_img_tbl1[vial]
			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
			g_gypsy_tbl["hand"].x = hand_x_offset1[vial]
			g_gypsy_tbl["hand"].y = 20

		elseif idx == 5 or idx == 11 then
			--
			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl2[vial]]
			g_gypsy_tbl["arm"].x = arm_x_offset[vial]
			g_gypsy_tbl["arm"].y = 21

			hand_img_num = hand_img_tbl2[vial]
			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
			g_gypsy_tbl["hand"].x = hand_x_offset1[vial]
			g_gypsy_tbl["hand"].y = 20

			if idx == 11 then
				vial_level[vial] = vial_level[vial] - 1
				if vial_level[vial] == 2 then
					gypsy_stop_pouring()
				end
			end

			if vial_level[vial] == 3 then
				gypsy_start_pouring(vial, vial_level[vial])
			end
		elseif idx == 6 or idx == 10 then
			--
			if vial_level[vial] > 2 then
				should_update = false
			end
			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl2[vial]]
			g_gypsy_tbl["arm"].x = arm_x_offset[vial]
			g_gypsy_tbl["arm"].y = 21

			hand_img_num = hand_img_tbl3[vial]
			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
			g_gypsy_tbl["hand"].x = hand_x_offset2[vial]
			g_gypsy_tbl["hand"].y = 21

			if vial_level[vial] == 2 then
				if idx == 6 then
					gypsy_start_pouring(vial, vial_level[vial])
				elseif idx == 10 then
					gypsy_stop_pouring()
				end
			end
		elseif idx == 7 or idx == 8 or idx == 9 then
			--
			if vial_level[vial] > 1 then
				should_update = false
			end
			g_gypsy_tbl["arm"].image = g_gypsy_img_tbl[1 + arm_img_tbl2[vial]]
			g_gypsy_tbl["arm"].x = arm_x_offset[vial]
			g_gypsy_tbl["arm"].y = 21

			hand_img_num = hand_img_tbl4[vial]
			g_gypsy_tbl["hand"].image = g_gypsy_img_tbl[9 + hand_img_num]
			g_gypsy_tbl["hand"].x = hand_x_offset3[vial]
			g_gypsy_tbl["hand"].y = 13

			if vial_level[vial] == 1 then
				if idx == 7 then
					gypsy_start_pouring(vial, vial_level[vial])
					g_gypsy_tbl["vial_liquid"][vial].visible = false
				elseif idx == 9 then
					gypsy_stop_pouring()
				end
			end
		end

		--if idx > 3 and idx < 9 or idx > 8 and idx < 13 then
		--	g_gypsy_tbl["hand"].x = g_gypsy_tbl["hand"].x - hand_x_offset4[vial]
		--end

		--io.stderr:write("idx ="..idx.."\n")
		if should_update == true then
			gypsy_vial_anim_liquid(hand_img_num, vial, vial_level[vial], g_gypsy_tbl["hand"].x - hand_x_offset4[vial], g_gypsy_tbl["hand"].y + 20)

			--g_gypsy_tbl["hand"].x = g_gypsy_tbl["hand"].x + hand_x_offset4[vial]

			local j
			for j = 1,8 do
				if g_gypsy_tbl["jar_level"] > 0 then
					gypsy_update_bubbles(g_gypsy_tbl["jar_liquid"].image)
				end
				canvas_update()
				input_poll()
			end
		end
	--[[
		local j
		for j = 1,8 do
			local img = g_gypsy_tbl["jar_liquid"].image
			if img ~= nil then
				gypsy_update_bubbles(g_gypsy_tbl["jar_liquid"].image)
			end
			canvas_update()
			input_poll()
		end
	--]]
	end

	g_gypsy_tbl["hand"].visible = false

	--io.stderr:write("vial #"..vial.." level="..vial_level[vial].."\n")



	g_gypsy_tbl["hand"].visible = false
	g_gypsy_tbl["arm"].visible = false

	g_gypsy_tbl["vial"][vial].visible = true
	--g_gypsy_tbl["vial_liquid"][vial].visible = true
end

local function gypsy_ask_questions(num_questions, scroll)

	local strength_adjustment_tbl = { 0, 0, 2, 0, 1, 1, 1, 0 }
	local dex_adjustment_tbl = { 0, 2, 0, 1, 1, 0, 1, 0 }
	local int_adjustment_tbl = { 2, 0, 0, 1, 0, 1, 1, 0 }

	for i=0,num_questions-1,1 do
		local q = gypsy_questions[g_question_tbl[i*2+1]*8 + g_question_tbl[i*2+2] + 1]

		local scroll_img = image_load("blocks.shp", 3)
		scroll.image = scroll_img
		image_print(scroll_img, gypsy_question_text[q - 104], 7, 303, 8, 9, 0x3e)

		local vial = gypsy_ab_select(q)

		gypsy_vial_anim(vial)

		g_str = g_str + strength_adjustment_tbl[vial]
		g_dex = g_dex + dex_adjustment_tbl[vial]
		g_int = g_int + int_adjustment_tbl[vial]

		g_question_tbl[i+1] = vial-1

		--io.stderr:write(q.." "..vial.."("..g_str..","..g_dex..","..g_int..")\n")
	end
end



g_keycode_tbl =
{
[32]=" ",
[39]="'",
[44]=",",
[45]="-",
[46]=".",
[48]="0",
[49]="1",
[50]="2",
[51]="3",
[52]="4",
[53]="5",
[54]="6",
[55]="7",
[56]="8",
[57]="9",
[65]="A",
[66]="B",
[67]="C",
[68]="D",
[69]="E",
[70]="F",
[71]="G",
[72]="H",
[73]="I",
[74]="J",
[75]="K",
[76]="L",
[77]="M",
[78]="N",
[79]="O",
[80]="P",
[81]="Q",
[82]="R",
[83]="S",
[84]="T",
[85]="U",
[86]="V",
[87]="W",
[88]="X",
[89]="Y",
[90]="Z",

[97]="a",
[98]="b",
[99]="c",
[100]="d",
[101]="e",
[102]="f",
[103]="g",
[104]="h",
[105]="i",
[106]="j",
[107]="k",
[108]="l",
[109]="m",
[110]="n",
[111]="o",
[112]="p",
[113]="q",
[114]="r",
[115]="s",
[116]="t",
[117]="u",
[118]="v",
[119]="w",
[120]="x",
[121]="y",
[122]="z",
}
local function create_character()
	music_play("create.m")

	local bubbles = sprite_new(image_new(100,100, 0), 110, 30, false)
	local bg = sprite_new(image_load("vellum1.shp", 0), 0x10, 0x50, true)
	image_print(bg.image, "By what name shalt thou be called?", 7, 303, 36, 24, 0x48)

	local name = sprite_new(nil, 0x34, 0x78, true)
	name.text = ""
	local char_index = 0
	local input = nil

	while input == nil do
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) == true then
				bg.visible = false
				name.visible = false
				return false
			end
			local name_text = name.text
			local len = string.len(name_text)
			if (input == 8 or input == SDLK_LEFT) and len > 0 then
				name.text = string.sub(name_text, 1, len - 1)
				if len == 1 then -- old len
					char_index = 0
				else
					char_index = string.byte(name_text, len -1)
				end
			elseif input == 13 and len > 0 then --return
				break;
			elseif g_keycode_tbl[input] ~= nil and len < 13 then
				char_index = input
				name.text = name_text..g_keycode_tbl[input]
			elseif input == SDLK_UP then --up
				if char_index == 0 then
					if len > 0 then
						char_index = 97 --a
					else
						char_index = 65 --A
					end
				elseif char_index <= 32 then --gap in characters
					char_index = 48
				elseif char_index >= 57 and  char_index < 65 then --gap in characters
					char_index = 65
				elseif char_index >= 90 and char_index < 97 then --gap in characters
					char_index = 97
				elseif char_index >= 122 then --last char
					char_index = 32
				else
					char_index = char_index + 1
				end

				if len > 0 then -- erase char
					name_text = string.sub(name_text, 1, len - 1)
				end
				name.text = name_text..g_keycode_tbl[char_index]
			elseif input == SDLK_DOWN then --down
				if char_index == 0 then
					if len > 0 then
						char_index = 122 --z
					else
						char_index = 90 --Z
					end
				elseif char_index == 65 then --gap in characters
					char_index = 57
				elseif char_index == 97 then --gap in characters
					char_index = 90
				elseif char_index <= 32 then --first char
					char_index = 122
				elseif char_index <= 48 then --gap in characters
					char_index = 32
				else
					char_index = char_index - 1
				end

				if len > 0 then -- erase char
					name_text = string.sub(name_text, 1, len - 1)
				end
				name.text = name_text..g_keycode_tbl[char_index]
			elseif input == SDLK_RIGHT and len < 13 then --right
				char_index = 97 --a
				name.text = name_text.."a"
			end
			input = nil
		end
	end

	name.x = 0x10 + (284 - canvas_string_length(name.text)) / 2

	image_print(bg.image, "And art thou Male, or Female?", 7, 303, 52, 56, 0x48)
	local gender_sprite = sprite_new(nil, 154, 152, true)
	gender_sprite.text = ""

	input = nil
	local gender = 0
	while input == nil do
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) == true then
				bg.visible = false
				name.visible = false
				gender_sprite.visible = false
				return false
			end
			if input == 77 or input == 109 then
				gender = 1 --male
				break
			elseif input == 70 or input == 102 then
				gender = 0 --female
				break
			elseif input == SDLK_UP or input == SDLK_DOWN then --up and down
				if gender == 0 then
					gender = 1 --male
					gender_sprite.text = "M"
				else
					gender = 0 --female
					gender_sprite.text = "F"
				end
			elseif input == 13 and gender_sprite.text ~= "" then --return
				break;
			end

			input = nil
		end
	end
	gender_sprite.visible = false
	load_images("vellum1.shp")
	bg.image = g_img_tbl[0]

	name.y = 0x59

	image_draw_line(bg.image, 14, 19, 277, 19, 0x48)

	local new_sex = sprite_new(g_img_tbl[3], 0x5e, 0x70, true)
	local new_portrait = sprite_new(g_img_tbl[0x12], 0x3e, 0x81, true)
	local continue = sprite_new(g_img_tbl[0x10], 0x56, 0x92, true)
	local esc_abort = sprite_new(g_img_tbl[4], 0x50, 0xa3, true)

	local montage_img_tbl = image_load_all("montage.shp")
	local portrait_num = 0
	local avatar = sprite_new(montage_img_tbl[gender*6+portrait_num], 0xc3, 0x76, true)
	local button_index = 0
	local old_button_index = 0

	local sex_highlight = sprite_new(image_new(58, 2), new_sex.x, new_sex.y +14, true)
	image_draw_line(sex_highlight.image, 0, 0, 59, 0, 248)
	image_draw_line(sex_highlight.image, 0, 1, 59, 1, 248)

	local portrait_highlight = sprite_new(image_new(90, 2), new_portrait.x, new_portrait.y +14, false)
	image_draw_line(portrait_highlight.image, 0, 0, 91, 0, 248)
	image_draw_line(portrait_highlight.image, 0, 1, 91, 1, 248)

	local continue_highlight = sprite_new(image_new(66, 2), continue.x, continue.y +14, false)
	image_draw_line(continue_highlight.image, 0, 0, 67, 0, 248)
	image_draw_line(continue_highlight.image, 0, 1, 67, 1, 248)

	local esc_abort_highlight = sprite_new(image_new(72, 2), esc_abort.x, esc_abort.y +14, false)
	image_draw_line(esc_abort_highlight.image, 0, 0, 73, 0, 248)
	image_draw_line(esc_abort_highlight.image, 0, 1, 73, 1, 248)

	input = nil
	local exiting = false

	while input == nil do
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) == true  or (input == 13 and button_index == 3) then
				exiting = true
			end
			if input == 112 or input == 80 or (input == 13 and button_index == 1) then
				if portrait_num == 5 then
					portrait_num = 0
				else
					portrait_num = portrait_num + 1
				end

				avatar.image = montage_img_tbl[gender*6+portrait_num]
			elseif input == 115 or input == 83 or (input == 13 and button_index == 0) then
				if gender == 0 then
					gender = 1
				else
					gender = 0
				end

				avatar.image = montage_img_tbl[gender*6+portrait_num]
			elseif input == 99 or input == 67 or (input == 13 and button_index == 2) then
				break
			elseif input == SDLK_UP and button_index > 0 then --up
				button_index = button_index -1;
			elseif input == SDLK_DOWN and button_index < 3 then --down
				button_index = button_index +1;
			elseif input == 0 then -- FIXME redundant stuff
				local x = get_mouse_x()
				if(x < 152) then
					local y = get_mouse_y()
					if x > 93 and y > 111 and y < 128 then
						if gender == 0 then
							gender = 1
						else
							gender = 0
						end
						avatar.image = montage_img_tbl[gender*6+portrait_num]
					elseif x > 61 and y > 128 and y < 145 then
						if portrait_num == 5 then
							portrait_num = 0
						else
							portrait_num = portrait_num + 1
						end
						avatar.image = montage_img_tbl[gender*6+portrait_num]
					elseif x > 85 and y > 145 and y < 162 then
						break
					elseif x > 79 and y > 162 and y < 179 then
						exiting = true
					end
				end
			end
			if button_index ~= old_button_index or exiting == true then

				if old_button_index == 0 then
					sex_highlight.visible = false
				elseif old_button_index == 1 then
					portrait_highlight.visible = false
				elseif old_button_index == 2 then
					continue_highlight.visible = false
				else
					esc_abort_highlight.visible = false
				end

				if exiting == true then
					bg.visible = false
					name.visible = false
					new_sex.visible = false
					new_portrait.visible = false
					continue.visible = false
					esc_abort.visible = false
					avatar.visible = false
					return false
				end

				if button_index == 0 then
					sex_highlight.visible = true
				elseif button_index == 1 then
					portrait_highlight.visible = true
				elseif button_index == 2 then
					continue_highlight.visible = true
				else
					esc_abort_highlight.visible = true
				end

				old_button_index = button_index
			end
			input = nil
		end
	end
	mouse_cursor_visible(false)
	fade_out()
	canvas_hide_all_sprites()

	canvas_set_palette("palettes.int", 4)
	local woods_img_tbl = image_load_all("woods.shp")
	local woods = sprite_new(woods_img_tbl[0], 0, 0, true)
	local woods1 = sprite_new(woods_img_tbl[1], 0x140, 0, true)

	fade_in()

	local scroll_img = image_load("blocks.shp", 1)
	local scroll = sprite_new(scroll_img, 1, 0xa0, true)

	local x, y = image_print(scroll_img, "\"Welcome, O Seeker!\127", 7, 303, 96, 14, 0x3e)

	wait_for_input()

	scroll.visible = false

	input = nil
	for i=0,0xbd,2 do
		woods.x = woods.x - 2
		woods1.x = woods1.x - 2
		canvas_update()
		input = input_poll()
		if input ~= nil then
			break
		end
	end

	woods.x = -190
	woods1.x = 130

	scroll_img = image_load("blocks.shp", 2)
	scroll.x = 1
	scroll.y = 0x98
	image_print(scroll_img, "A lonely stroll along an unfamiliar forest path brings you upon a curious gypsy wagon, its exotic colors dappled in the summer shade.", 7, 303, 8, 12, 0x3e)
	scroll.image = scroll_img
	scroll.visible = true

	wait_for_input()

	scroll_img = image_load("blocks.shp", 2)
	scroll.x = 1
	scroll.y = 0x98
	image_print(scroll_img, "A woman's voice rings out with friendship, beckoning you into across the wagon's threshold and, as it happens, into another life....", 7, 303, 8, 12, 0x3e)
	scroll.image = scroll_img
	scroll.visible = true

	wait_for_input()

	scroll.visible = false

	fade_out()
	canvas_hide_all_sprites()
	canvas_set_palette("palettes.int", 5)

	g_gypsy_img_tbl = image_load_all("gypsy.shp")

	bg.image = g_gypsy_img_tbl[0]
	bg.x = 0
	bg.y = -20
	bg.visible = true

	g_gypsy_tbl = {}
	g_gypsy_tbl["arm"] = sprite_new(nil, 0, 0, false)
	g_gypsy_tbl["pour"] = sprite_new(nil, 0, 0, false)


	g_gypsy_tbl["jar_level"] = 0
	g_gypsy_tbl["jar_liquid"] = sprite_new(nil, 0x92, 0x53, false)
	g_gypsy_tbl["jar_liquid"].clip_x = 0
	g_gypsy_tbl["jar_liquid"].clip_y = 0
	g_gypsy_tbl["jar_liquid"].clip_w = 320
	g_gypsy_tbl["jar_liquid"].clip_h = 0x6d

	g_gypsy_tbl["jar"] = sprite_new(g_gypsy_img_tbl[16], 0x8e, 0x38, true)

	g_gypsy_tbl["bubble_counter"] = 0

	g_gypsy_tbl["vial_level"] = {3,3,3,3,3,3,3,3}
	local vial_level = g_gypsy_tbl["vial_level"]

	local vial_img_off = {2, 5, 8, 0xB, 0x18, 0x1B, 0x1E, 0x21}

	g_gypsy_tbl["vial_liquid"] = {
		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[1] + vial_level[1] - 3], 44, 0x4d, true),
		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[2] + vial_level[2] - 3], 65, 0x4d, true),
		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[3] + vial_level[3] - 3], 86, 0x4d, true),
		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[4] + vial_level[4] - 3], 107, 0x4d, true),
		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[5] + vial_level[5] - 3], 203, 0x4d, true),
		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[6] + vial_level[6] - 3], 224, 0x4d, true),
		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[7] + vial_level[7] - 3], 245, 0x4d, true),
		sprite_new(g_gypsy_img_tbl[0x42 + vial_img_off[8] + vial_level[8] - 3], 266, 0x4d, true),
	}

	g_gypsy_tbl["hand"] = sprite_new(nil, 0, 0, false)

	g_gypsy_tbl["vial"] = {
		sprite_new(g_gypsy_img_tbl[20], 41, 0x42, true),
		sprite_new(g_gypsy_img_tbl[20], 62, 0x42, true),
		sprite_new(g_gypsy_img_tbl[20], 83, 0x42, true),
		sprite_new(g_gypsy_img_tbl[20], 104, 0x42, true),
		sprite_new(g_gypsy_img_tbl[23], 200, 0x42, true),
		sprite_new(g_gypsy_img_tbl[23], 221, 0x42, true),
		sprite_new(g_gypsy_img_tbl[23], 242, 0x42, true),
		sprite_new(g_gypsy_img_tbl[23], 263, 0x42, true),
	}

	fade_in()

	scroll.x = 1
	scroll.y = 0x7c
	scroll.visible = true

	local scroll_img = image_load("blocks.shp", 3)
	scroll.image = scroll_img
	x, y = image_print(scroll_img, "\"At last thou hast come to fulfill thy destiny,\127 the gypsy says. She smiles, as if in great relief.", 7, 303, 8, 19, 0x3e)
	image_print(scroll_img, "\"Sit before me now, and I shall pour the light of Virtue into the shadows of thy future.\127", 7, 303, 8, y+16, 0x3e)

	wait_for_input()

	scroll_img = image_load("blocks.shp", 3)
	scroll.image = scroll_img
	x, y = image_print(scroll_img, "On a wooden table eight bottles stand, a rainbow of bubbling liquids.", 7, 303, 8, 19, 0x3e)
	image_print(scroll_img, "\"Behold the Virtues of the Avatar,\127 the woman says. \"Let us begin the casting!\127", 7, 303, 8, y+16, 0x3e)

	wait_for_input()
	canvas_set_palette_entry(19, 200, 200, 200) -- fix mouse cursor
	canvas_set_palette_entry(27, 68, 68, 68) -- fix mouse cursor

	mouse_cursor_visible(true)
	local a_button = sprite_new(g_gypsy_img_tbl[7], 0x117, 0xae, true)
	local b_button = sprite_new(g_gypsy_img_tbl[8], 0x128, 0xae, true)
	g_ab_highlight = sprite_new(image_new(16, 2), a_button.x, a_button.y + 13, true)
	image_draw_line(g_ab_highlight.image, 0, 0, 17, 0, 248)
	image_draw_line(g_ab_highlight.image, 0, 1, 17, 1, 248)

	g_str = 0xf
	g_dex = 0xf
	g_int = 0xf



	shuffle_question_tbl(8)
	gypsy_ask_questions(4, scroll)

	shuffle_question_tbl(4)

	gypsy_ask_questions(2, scroll)
	gypsy_ask_questions(1, scroll)

	a_button.visible = false
	b_button.visible = false
	g_ab_highlight.visible = false
	mouse_cursor_visible(false)

	scroll_img = image_load("blocks.shp", 3)
	scroll.image = scroll_img
	image_print(scroll_img, "\"The path of the Avatar lies beneath thy feet, worthy "..name.text..",\127 the gypsy intones. With a mysterious smile, she passes you the flask of shimmering liquids. \"Drink of these waters and go forth among our people, who shall receive thee in joy!\127", 7, 303, 8, 16, 0x3e)

	-- wait_for_input()

	input = nil
	while input == nil do
		gypsy_update_bubbles(g_gypsy_tbl["jar_liquid"].image)
		canvas_update()
		input = input_poll()
		if input ~= nil then
			break
		end
	end

	fade_out()

	canvas_hide_all_sprites()
	canvas_set_palette("palettes.int", 6)


	--local big_flask = sprite_new(g_gypsy_img_tbl[198], 0, 0, true)
	bubbles.visible = true
	bg.x = 0
	bg.y = 0
	bg.visible = true
	bg.image = g_gypsy_img_tbl[198]
	g_gypsy_tbl["bubble_counter"] = 0
	gypsy_update_bubbles(bubbles.image)

	fade_in()

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.x = 1
	scroll.y = 0x98
	scroll.visible = true
	image_print(scroll_img, "As you drink from the flask, vertigo overwhelms you. A soothing mist obscures the gypsy's face, and you sink without fear into an untroubled sleep.", 7, 303, 8, 8, 0x3e)

	--wait_for_input()

	input = nil
	while input == nil do
		gypsy_update_bubbles(bubbles.image)
		canvas_update()
		input = input_poll()
		if input ~= nil then
			break
		end
	end

	scroll.visible = false

	canvas_set_bg_color(0x75)
	fade_out()

	bubbles.visible = false

	bg.image = g_gypsy_img_tbl[199]
	bg.x = 0
	bg.y = 0
	bg.visible = true

	scroll.visible = false

	fade_in()

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.visible = true
	image_print(scroll_img, "You wake in a different time, upon another world's shore. Though the Avatar's quests bring you both triumph and tragedy, never do you stray from the path of the Eight Virtues.", 7, 303, 8, 8, 0x3e)

	wait_for_input()

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.visible = true
	image_print(scroll_img, "The sagas of Ultima IV and Ultima V chronicle your perilous travels, and your name and your deeds are written forever among Britannia's legends....", 7, 303, 8, 8, 0x3e)

	wait_for_input()

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.visible = true
	image_print(scroll_img, "Finally, tempered by your struggles against the enemies of Virtue, you are proven ready to answer the epic challenge of Ultima VI!", 7, 303, 8, 12, 0x3e)

	wait_for_input()
	scroll.visible = false

	canvas_set_bg_color(0)
	fade_out()

	config_set("config/newgame", true)

	config_set("config/newgamedata/name", name.text)
	config_set("config/newgamedata/gender", gender)
	config_set("config/newgamedata/portrait", portrait_num)
	config_set("config/newgamedata/str", g_str)
	config_set("config/newgamedata/dex", g_dex)
	config_set("config/newgamedata/int", g_int)

	g_gypsy_img_tbl = nil
	g_gypsy_tbl = nil

	return true
end

local function ack_header(scroll_img)
	image_print(scroll_img, "Ultima VI", 7, 303, 115, 9, 0x3d)
	image_print(scroll_img, "A Lord British Production", 7, 303, 63, 17, 0x3d)
	image_draw_line(scroll_img, 9, 26, 274, 26, 0x3d)
end

local function acknowledgements()

	local bg = sprite_new(image_load("vellum1.shp", 0), 0x10, 0x50, true)

	ack_header(bg.image)

	image_print(bg.image, "Produced by", 7, 303, 106, 32, 0xC)
	image_print(bg.image, "Richard Garriott and Warren Spector", 7, 303, 28, 40, 0x48)

	image_print(bg.image, "Executive Producer", 7, 303, 82, 56, 0xC)
	image_print(bg.image, "Dallas Snell", 7, 303, 106, 64, 0x48)

	image_print(bg.image, "Programming", 7, 303, 104, 80, 0xC)
	image_print(bg.image, "Cheryl Chen    John Miles", 7, 303, 67, 88, 0x48)
	image_print(bg.image, "Herman Miller    Gary Scott Smith", 7, 303, 40, 96, 0x48)

	fade_in_sprite(bg)
	local input

	wait_for_input()

	bg.image = image_load("vellum1.shp", 0)

	ack_header(bg.image)

	image_print(bg.image, "Writing", 7, 303, 120, 47, 0xC)
	image_print(bg.image, "Stephen Beeman    Dr. Cat    \39Manda Dee", 7, 303, 25, 55, 0x48)
	image_print(bg.image, "Richard Garriott    Greg Malone", 7, 303, 46, 63, 0x48)
	image_print(bg.image, "John Miles    Herman Miller", 7, 303, 61, 71, 0x48)
	image_print(bg.image, "Todd Porter    Warren Spector", 7, 303, 50, 79, 0x48)

	wait_for_input()

	bg.image = image_load("vellum1.shp", 0)

	ack_header(bg.image)

	image_print(bg.image, "Art", 7, 303, 132, 31, 0xC)
	image_print(bg.image, "Keith Berdak    Daniel Bourbonnais", 7, 303, 37, 39, 0x48)
	image_print(bg.image, "Jeff Dee    \39Manda Dee", 7, 303, 75, 47, 0x48)
	image_print(bg.image, "Glen Johnson    Denis Loubet", 7, 303, 56, 55, 0x48)

	image_print(bg.image, "Music", 7, 303, 126, 71, 0xC)
	image_print(bg.image, "Ken Arnold    Iolo Fitzowen", 7, 303, 61, 79, 0x48)
	image_print(bg.image, "Herman Miller    Todd Porter", 7, 303, 56, 87, 0x48)

	wait_for_input()

	bg.image = image_load("vellum1.shp", 0)

	ack_header(bg.image)

	image_print(bg.image, "Quality Assurance", 7, 303, 87, 31, 0xC)
	image_print(bg.image, "Paul Malone    Mike Romero", 7, 303, 62, 39, 0x48)
	image_print(bg.image, "", 7, 303, 49, 47, 0x48)

	image_print(bg.image, "Additional Support", 7, 303, 84, 63, 0xC)
	image_print(bg.image, "Michelle Caddel    Melanie Fleming", 7, 303, 39, 71, 0x48)
	image_print(bg.image, "Alan Gardner    Jeff Hillhouse", 7, 303, 51, 79, 0x48)
	image_print(bg.image, "Sherry Hunter    Steve Muchow", 7, 303, 49, 87, 0x48)
	image_print(bg.image, "Cheryl Neeld", 7, 303, 104, 95, 0x48)

	wait_for_input()

	fade_out_sprite(bg, 4)

	bg.visible = false

	return true
end


local function intro_sway_gargs(sprite, idx, angry_flag)
	if math.random(0, 3) == 0 then
		return idx
	end

	local movement_tbl = {1,1,1, -1,-1,-1,  -1,-1,-1,  1, 1, 1}

	if idx == 12 then
		idx = 1
	else
		idx = idx + 1
	end

	sprite.x = sprite.x + movement_tbl[idx]

	return idx
end

local function moongate_rotate_palette(idx, num)
	if g_pal_counter == 3 then
		canvas_rotate_palette(232, 8)
		g_pal_counter = 0
	else
		g_pal_counter = g_pal_counter + 1
	end
end

local function intro_exit()
	fade_out()
	canvas_hide_all_sprites()
	canvas_set_palette("palettes.int", 0)
end

local function intro_wait()
	if should_exit(wait_for_input()) == true then
		intro_exit()
		return false
	end

	return true
end

local function intro()
	local input
	local intro_img_tbl = image_load_all("intro.shp")

	local bg = sprite_new(intro_img_tbl[6], 0, 0, true)
	local moongate = sprite_new(intro_img_tbl[7], 0x78, 0x3a, false)
	local gargs_left = sprite_new(intro_img_tbl[3], -84, 0x6d, true)
	local gargs_right = sprite_new(intro_img_tbl[4], 326, 0xc7, true)
	local garg_body = sprite_new(intro_img_tbl[8], 0xd5, 0x8d, false)
	local garg_head = sprite_new(intro_img_tbl[11], 0x123, 0x9b, false)
	local iolo = sprite_new(intro_img_tbl[1], 0xa8, 0xca, false)
	local shamino = sprite_new(intro_img_tbl[2], 0x44, 0x7a, false)
	local dupre = sprite_new(intro_img_tbl[0], -0x20, 0x7a, false)

	local avatar = sprite_new(intro_img_tbl[9], 0x31, 0x44, false)
	local alter = sprite_new(intro_img_tbl[5], 0, 0x70, true)
	local ropes = sprite_new(intro_img_tbl[12], 0xd2, 0x84, false)

	canvas_set_palette("palettes.int", 7)
	music_play("intro.m")

	fade_in()

	local scroll_img = image_load("blocks.shp", 2)
	local scroll = sprite_new(scroll_img, 1, 0x98, true)

	image_print(scroll_img, "Dazed, you emerge from the portal to find yourself standing on a desolate plain. Nearby rests a massive rune-struck altar, shrouded in moonlit fog.", 7, 308, 8, 8, 0x3e)

	if should_exit(wait_for_input()) == true then intro_exit() return end

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	image_print(scroll_img, "At first the plain is still. Then a hundred voices raise a slow, deathlike song, drawing closer and closer with each passing moment. You are seized by an urge to run...", 7, 308, 8, 8, 0x3e)

	if intro_wait() == false then return end

	local l_move_tbl_x = {1, 1, 0, 1, 1, 1}
	local l_move_tbl_y = {0, 0, -1, 0, 0, -1}

	local r_move_tbl_x = {-1, -1, -1, -1, -1, -1}
	local r_move_tbl_y = {-1, -1, 0, -1, -1, -1}

	scroll.visible = false

	local i
	for i=0,95,1 do
		gargs_left.x = gargs_left.x + l_move_tbl_x[(i%6)+1]
		gargs_left.y = gargs_left.y + l_move_tbl_y[(i%6)+1]

		if i > 23 then
			gargs_right.x = gargs_right.x + r_move_tbl_x[(i%6)+1] * 2
			gargs_right.y = gargs_right.y + r_move_tbl_y[(i%6)+1] * 2
		end

		input = input_poll()
		if input ~= nil then
			if should_exit(input) ==  true then
				intro_exit()
				return
			end

			gargs_left.x = -4
			gargs_left.y = 77
			gargs_right.x = 182
			gargs_right.y = 79

			break
		end
		canvas_update()
		canvas_update()
	end

	scroll_img = image_load("blocks.shp", 0)
	scroll.image = scroll_img
	scroll.x = 0x21
	scroll.y = 0x9d
	scroll.visible = true
	image_print(scroll_img, "...but you have no place to go.", 7, 308, 35, 8, 0x3e)

	if intro_wait() == false then return end

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0x98
	image_print(scroll_img, "Before you can offer a protest to the creatures who surround you, scaly claws grasp your body.", 7, 308, 8, 12, 0x3e)

	if intro_wait() == false then return end

	scroll_img = image_load("blocks.shp", 1)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0xa0
	image_print(scroll_img, "With unearthly strength, the monsters bind you to the altar stone!", 7, 308, 11, 10, 0x3e)

	avatar.visible = true
	ropes.visible = true

	if intro_wait() == false then return end

	gargs_left.y = gargs_left.y + 4
	gargs_right.y = gargs_right.y + 4
	scroll.visible = false

	garg_body.visible = true
	garg_head.visible = true

	for i=0,22,1 do
		garg_body.x = garg_body.x - 3
		garg_body.y = garg_body.y - 3
		garg_head.x = garg_head.x - 3
		garg_head.y = garg_head.y - 3

		input = input_poll()
		if input ~= nil then
			if should_exit(input) ==  true then
				intro_exit()
				return
			end

			break
		end
		canvas_update()
		canvas_update()
	end

	if input == nil then
		for i=0,13,1 do
			garg_body.y = garg_body.y - 3
			garg_head.y = garg_head.y - 3

			input = input_poll()
			if input ~= nil then
				if should_exit(input) ==  true then
					intro_exit()
					return
				end

				break
			end

			canvas_update()
			canvas_update()
		end
	end

	garg_body.x = 144
	garg_body.y = 30
	garg_head.x = 222
	garg_head.y = 44

	scroll_img = image_load("blocks.shp", 1)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0xa0
	scroll.visible = true
	image_print(scroll_img, "Kneeling, the hordes sway and chant as a stately winged nightmare steps forward.", 32, 262, 33, 10, 0x3e)

	local input = nil
	local left_idx = 1
	local right_idx = 6
	while input == nil do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		canvas_update()
		input = input_poll()
			if input ~= nil then
				if should_exit(input) ==  true then
					intro_exit()
					return
				end
				break
			end
		canvas_update()
	end

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0x98
	scroll.visible = true
	image_print(scroll_img, "The leader unwraps a velvet covered, brass-bound book and recites from it in a formal, stilted tongue.", 7, 308, 8, 12, 0x3e)

	input = nil
	while input == nil do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) ==  true then
				intro_exit()
				return
			end
			break
		end
		canvas_update()
	end

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	image_print(scroll_img, "Shouts and jeers explode from the masses as the priest slams shut the book. In his hand a malignant dagger drips with moonlight.", 7, 308, 8, 12, 0x3e)

	input = nil
	while input == nil do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) ==  true then
				intro_exit()
				return
			end
			break
		end
		canvas_update()
	end

	scroll_img = image_load("blocks.shp", 1)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0xa0
	image_print(scroll_img, "You close your eyes. A dying scream, certainly your own, curdles the air.", 80, 228, 16, 10, 0x3e)

	moongate.visible = true
	g_pal_counter = 0

	for i=0x78,0x3a,-1 do
		moongate.y = i
		moongate_rotate_palette()
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) then
				intro_exit()
				return
			end
			moongate.y = 0x3a
			break
		end
	end

	input = nil
	while input == nil do
		moongate_rotate_palette()
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) then
				intro_exit()
				return
			end
			break
		end
	end

	for i=0xff,0,-3 do
		moongate_rotate_palette()
		canvas_set_opacity(i)
		canvas_update()
		input_poll()
	end

	canvas_hide_all_sprites()

	scroll_img = image_load("blocks.shp", 1)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0x50
	scroll.visible = true
	image_print(scroll_img, "Catcalls, the dagger, a scream, Death....", 7, 308, 39, 14, 0x3e)

	fade_in()

	if intro_wait() == false then return end

	fade_out()

	scroll_img = image_load("blocks.shp", 1)
	scroll.image = scroll_img
	image_print(scroll_img, "Pandemonium. Shrieks of rage, of terror.", 7, 308, 34, 14, 0x3e)

	fade_in()

	if intro_wait() == false then return end

	fade_out()

	scroll_img = image_load("blocks.shp", 1)
	scroll.image = scroll_img
	image_print(scroll_img, "From the inevitable, an impossibility emerges.", 7, 308, 16, 14, 0x3e)

	fade_in()

	if intro_wait() == false then return end

	fade_out()

	scroll_img = image_load("blocks.shp", 1)
	scroll.image = scroll_img
	image_print(scroll_img, "You are still alive.", 7, 308, 101, 14, 0x3e)

	fade_in()

	if intro_wait() == false then return end

	fade_out()

	bg.visible = true
	moongate.visible = true
	gargs_left.visible = true
	gargs_right.visible = true
	garg_body.visible = true
	garg_head.visible = true
	alter.visible = true
	avatar.visible = true
	ropes.visible = true

	scroll.visible = false

	for i=0,0xff,3 do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		moongate_rotate_palette()
		canvas_set_opacity(i)
		canvas_update()
		input_poll()
	end

	garg_head.image = intro_img_tbl[10]

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0x98
	scroll.visible = true
	image_print(scroll_img, "Silent red light fills the darkness. There is the wooden clack of a crossbow, and a violet- fletched rose blooms in the priest\39s barren forehead.", 7, 308, 8, 8, 0x3e)

	input = nil
	while input == nil do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		moongate_rotate_palette()
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) then
				intro_exit()
				return
			end
			break
		end
	end

	scroll.visible = false

	for i=0,42,1 do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		moongate_rotate_palette()

		local x =  math.random(-1, 2)
		garg_body.x = garg_body.x + x
		garg_body.y = garg_body.y + 3
		garg_head.x = garg_head.x + x
		garg_head.y = garg_head.y + 3

		input = input_poll()
		if input ~= nil then
			if should_exit(input) ==  true then
				intro_exit()
				return
			end
			garg_body.visible = false
			garg_head.visible = false
			break
		end
		canvas_update()
	end

	garg_body.visible = false
	garg_head.visible = false

	iolo.visible = true
	dupre.visible = true
	shamino.visible = true

	for i=0,82,1 do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		moongate_rotate_palette()


		if i > 33 then
			dupre.x = dupre.x + 2
		else
			dupre.x = dupre.x + 1
		end

		dupre.y = dupre.y - 1

		if i > 13 then
			if i > 36 then
				shamino.x = shamino.x + 2
			else
				shamino.x = shamino.x + 1
			end
			shamino.y = shamino.y - 1
		end

		if i > 10 then
			iolo.x = iolo.x + 1
			if i > 14 then
				iolo.y = iolo.y - 2
			else
				iolo.y = iolo.y - 1
			end
		end

		input = input_poll()
		if input ~= nil then
			if should_exit(input) ==  true then
				intro_exit()
				return
			end
			dupre.x = 99
			dupre.y = 40
			shamino.x = 183
			shamino.y = 53
			iolo.x = 240
			iolo.y = 62
			break
		end
		canvas_update()
	end

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0x98
	scroll.visible = true
	image_print(scroll_img, "Friendly faces vault from a newborn moongate, while a rain of quarrels holds the furious mob at bay. The knight Dupre\39s sword flashes twice in the darkness, slicing away your bonds!", 7, 308, 8, 8, 0x3e)

	input = nil
	while input == nil do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		moongate_rotate_palette()
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) then
				intro_exit()
				return
			end
			break
		end
	end

	scroll.visible = false
	ropes.visible = false

	for i=0,82,1 do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		moongate_rotate_palette()

		dupre.x = dupre.x - 1
		dupre.y = dupre.y + 2


		shamino.x = shamino.x - 1
		shamino.y = shamino.y + 2

		iolo.x = iolo.x - 1
		iolo.y = iolo.y + 2

		avatar.y = avatar.y + 1

		input = input_poll()
		if input ~= nil then
			if should_exit(input) ==  true then
				intro_exit()
				return
			end
			dupre.visible = false
			shamino.visible = false
			iolo.visible = false
			avatar.visible = false
			break
		end
		canvas_update()
	end

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.visible = true
	image_print(scroll_img, "\"Quickly, old friend! To the gate!\127 Accompanied by the swordsman Shamino and a grinning, crossbow-wielding Iolo the Bard, Dupre thrusts a spare sword into your hand.", 7, 308, 8, 8, 0x3e)

	input = nil
	while input == nil do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		moongate_rotate_palette()
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) then
				intro_exit()
				return
			end
			break
		end
	end

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	image_print(scroll_img, "Snatching the fallen priest\39s book, Iolo dives into the redness with Shamino at his heels. The howling throng surges forward, all of one terrible mind.", 7, 308, 8, 8, 0x3e)

	input = nil
	while input == nil do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		moongate_rotate_palette()
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) then
				intro_exit()
				return
			end
			break
		end
	end

	scroll_img = image_load("blocks.shp", 1)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0xa0
	image_print(scroll_img, "The gate wanes rapidly as you and Dupre charge through...", 130, 178, 12, 10, 0x3e)

	for i=0,150,1 do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		if i % 2 == 0 then
			moongate.y = moongate.y + 1
		end
		moongate_rotate_palette()
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) then
				intro_exit()
				return
			end
			moongate.visible = false
			break
		end
	end

	scroll_img = image_load("blocks.shp", 0)
	scroll.image = scroll_img
	scroll.x = 0x21
	scroll.y = 0x9d
	image_print(scroll_img, "...but not rapidly enough.", 7, 308, 50, 8, 0x3e)

	input = nil
	while input == nil do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) then
				intro_exit()
				return
			end
			break
		end
	end

	scroll_img = image_load("blocks.shp", 2)
	scroll.image = scroll_img
	scroll.x = 0x1
	scroll.y = 0x98
	image_print(scroll_img, "From the mob\39s vanguard, three of the abominations scramble toward the gate. Driven by fury, the creatures hurl their bodies into the portal\39s last handspan of light!", 7, 308, 8, 8, 0x3e)

	input = nil
	while input == nil do
		left_idx = intro_sway_gargs(gargs_left, left_idx, false)
		right_idx = intro_sway_gargs(gargs_right, right_idx, false)
		canvas_update()
		input = input_poll()
		if input ~= nil then
			if should_exit(input) then
				intro_exit()
				return
			end
			break
		end
	end

	scroll.visible = false

	intro_exit()
end

g_menu_pal =
{
	{232,96,0},
	{236,128,0},
	{244,164,0},
	{248,200,0},
	{252,252,84},
	{248,200,0},
	{244,164,0},
	{236,128,0},
	{232,96,0}
}

g_menu_pal_idx = { 14, 33, 34, 35, 36 }
local function main_menu_set_pal(idx)
	local i

	for i = 1,5,1 do
		local colour = g_menu_pal[5+(i-1)-idx]
		canvas_set_palette_entry(g_menu_pal_idx[i], colour[1], colour[2], colour[3])
	end
end

local function main_menu_load()
	g_menu = {}

	if engine_should_quit() == 1 then
		return
	end

	music_play("ultima.m")

	canvas_set_palette("palettes.int", 0)

	local title_img_tbl = image_load_all("titles.shp")
	g_menu["title"] = sprite_new(title_img_tbl[0], 0x13, 0, true)
	g_menu["subtitle"] = sprite_new(title_img_tbl[1], 0x3b, 0x2f, false)

	g_menu["menu"] = sprite_new(image_load("mainmenu.shp", 0), 0x31, 0x53, false)

	fade_in()

	g_menu["subtitle"].visible = true
	g_menu["menu"].visible = true

	fade_in_sprite(g_menu["menu"])
	mouse_cursor_visible(true)
end

	g_menu_idx = 0

local function selected_intro()
	mouse_cursor_visible(false)
	main_menu_set_pal(0)
	fade_out()
	canvas_hide_all_sprites()
	intro()
	music_play("ultima.m")
	g_menu["title"].visible = true
	fade_in()
	g_menu["subtitle"].visible = true
	g_menu["menu"].visible = true

	fade_in_sprite(g_menu["menu"])
	mouse_cursor_visible(true)
end

local function selected_create_character()
	main_menu_set_pal(1)
	fade_out_sprite(g_menu["menu"],6)
	if create_character() == true then
		return true
	end
	canvas_set_palette("palettes.int", 0)
	g_menu_idx=0
	main_menu_set_pal(g_menu_idx)
	music_play("ultima.m")
	fade_in_sprite(g_menu["menu"])
	return false
end

local function selected_acknowledgments()
	main_menu_set_pal(3)
	fade_out_sprite(g_menu["menu"],6)
	acknowledgements()
	canvas_set_palette("palettes.int", 0)
	g_menu_idx=0
	main_menu_set_pal(g_menu_idx)
	fade_in_sprite(g_menu["menu"])
end

local function main_menu()
	g_menu["title"].visible = true
	g_menu["subtitle"].visible = true
	g_menu["menu"].visible = true

	local input

	while true do
		canvas_update()

		if engine_should_quit() == 1 then
			return "Q"
		end

		while true do
			input = input_poll(true)
			if input == nil then
				break
			elseif input == 113 then     --q quit
				return "Q"
			elseif input == 105 or input == 13 and g_menu_idx == 0 then --i
				selected_intro()
			elseif input == 99 or input == 13 and g_menu_idx == 1 then  --c
				if selected_create_character()== true then
					return "J"
				end
			elseif input == 116 or input == 13 and g_menu_idx == 2 then --t
				--transfer a character
			elseif input == 97 or input == 13 and g_menu_idx == 3 then  --a
				selected_acknowledgments()
			elseif input == 106 or input == 13 and g_menu_idx == 4 then --j
				main_menu_set_pal(4)
				fade_out()
				return "J"
			elseif input == SDLK_DOWN then --down key
				if g_menu_idx < 4 then
					g_menu_idx = g_menu_idx + 1
					main_menu_set_pal(g_menu_idx)
				end
			elseif input == SDLK_UP then --up key
				if g_menu_idx > 0 then
					g_menu_idx = g_menu_idx - 1
					main_menu_set_pal(g_menu_idx)
				end
			elseif input >= 48 and input <= 57 or input == 45 or input == 61 then --play music play pressing number keys or '-' or '='
				if input == 45 then
					input = 11
				elseif input == 48 then
					input = 10
				elseif input == 61 then
					input = 12
				else
					input = input - 48
				end

				local song_names = {
					"ultima.m",
					"bootup.m",
					"intro.m",
					"create.m",
					"forest.m",
					"hornpipe.m",
					"engage.m",
					"stones.m",
					"dungeon.m",
					"brit.m",
					"gargoyle.m",
					"end.m"
				}
				music_play(song_names[input])
			elseif input == 0 then --mouse click
				local x = get_mouse_x()
				if x > 56 and x < 264 then
					local y = get_mouse_y()
					if y > 86 and y < 108 then
						selected_intro()
					elseif y > 107 and y < 128 then
						if selected_create_character() == true then
							return "J"
						end
					elseif y > 127 and y < 149 then
						--transfer a character
					elseif y > 148 and y < 170 then
						selected_acknowledgments()
					elseif y > 169 and y < 196 then
						main_menu_set_pal(4)
						fade_out()
						return "J" -- journey onward
					end
				end
			elseif input == 1 then --mouse movement
				local x = get_mouse_x()
				if x > 56 and x < 264 then
					local old_menu_idx = g_menu_idx
					local y = get_mouse_y()
					if y > 86 and y < 108 then
						g_menu_idx = 0
					elseif y > 107 and y < 128 then
						g_menu_idx = 1
					elseif y > 127 and y < 149 then
						g_menu_idx = 2
					elseif y > 148 and y < 170 then
						g_menu_idx = 3
					elseif y > 169 and y < 196 then
						g_menu_idx = 4
					end
					if g_menu_idx ~= old_menu_idx then
						main_menu_set_pal(g_menu_idx)
					end
				end
			end
		end
	end
end


play()
fade_out()

g_img_tbl = nil
g_clock_tbl = nil
g_lounge_tbl = nil
g_window_tbl = nil
g_stones_tbl = nil

canvas_hide_all_sprites()

main_menu_load()

if main_menu() == "Q" then -- returns "Q" for quit or "J" for Journey Onward
	config_set("config/quit", true)
end

music_stop()
canvas_hide_all_sprites()
canvas_hide()