aboutsummaryrefslogtreecommitdiff
path: root/sky/logic.cpp
blob: e96937670b2177ef4bd222cd37af67036a72e99b (plain)
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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2003 The ScummVM project
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

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

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

#include "stdafx.h"
#include "sky/logic.h"
#include "sky/debug.h"
#include "sky/compact.h"
#include "sky/skydefs.h"
#include "sky/talks.h"

uint32 SkyLogic::_scriptVariables[838];

typedef void (SkyLogic::*LogicTable) ();
static const LogicTable logicTable[] = {
	&SkyLogic::nop,
	&SkyLogic::logicScript,	 // 1  script processor
	&SkyLogic::autoRoute,	 // 2  Make a route
	&SkyLogic::arAnim,	 // 3  Follow a route
	&SkyLogic::arTurn,	 // 4  Mega turns araound
	&SkyLogic::alt,		 // 5  Set up new get-to script
	&SkyLogic::anim,	 // 6  Follow a sequence
	&SkyLogic::turn,	 // 7  Mega turning
	&SkyLogic::cursor,	 // 8  id tracks the pointer
	&SkyLogic::talk,	 // 9  count down and animate
	&SkyLogic::listen,	 // 10 player waits for talking id
	&SkyLogic::stopped,	 // 11 wait for id to move
	&SkyLogic::choose,	 // 12 wait for player to click
	&SkyLogic::frames,	 // 13 animate just frames
	&SkyLogic::pause,	 // 14 Count down to 0 and go
	&SkyLogic::waitSync,	 // 15 Set to l_script when sync!=0
	&SkyLogic::simpleAnim,	 // 16 Module anim without x,y's
};

SkyLogic::SkyLogic(SkyScreen *skyScreen, SkyDisk *skyDisk, SkyGrid *skyGrid, SkyText *skyText, SkyMusicBase *skyMusic, SkyMouse *skyMouse, SkySound *skySound, uint32 gameVersion) {
	_skyScreen = skyScreen;
	_skyDisk = skyDisk;
	_skyGrid = skyGrid;
	_skyText = skyText;
	_skyMusic = skyMusic;
	_skySound = skySound;
	_skyMouse = skyMouse;
	_gameVersion = gameVersion;
	_skyAutoRoute = new SkyAutoRoute(_skyGrid);

	for (int i = 0; i < ARRAYSIZE(_moduleList); i++)
		_moduleList[i] = 0;
	_stackPtr = 0;
	
	initScriptVariables();
}

void SkyLogic::engine() {
	uint16 *logicList = (uint16 *)SkyState::fetchCompact(_scriptVariables[LOGIC_LIST_NO]);

	while (uint16 id = *logicList++) { // 0 means end of list
		if (id == 0xffff) {
			// Change logic data address
			logicList = (uint16 *)SkyState::fetchCompact(*logicList);
			continue;
		}

		_scriptVariables[CUR_ID] = id;
		_compact = SkyState::fetchCompact(id);

		// check the id actually wishes to be processed
		if (!(_compact->status & (1 << 6)))
			continue;

		// ok, here we process the logic bit system

		if (_compact->status & (1 << 7))
			_skyGrid->removeObjectFromWalk(_compact);

		SkyDebug::logic(_compact->logic);
		(this->*logicTable[_compact->logic]) ();

		if (_compact->status & (1 << 7))
			_skyGrid->objectToWalk(_compact);

		// a sync sent to the compact is available for one cycle
		// only. that cycle has just ended so remove the sync.
		// presumably the mega has just reacted to it.
		_compact->sync = 0;
	}
}

void SkyLogic::nop() {}

/**
 * This function is basicly a wrapper around the real script engine. It runs
 * the script engine until a script has finished.
 * @see script()
 */
void SkyLogic::logicScript() {
	/// Process the current mega's script
	/// If the script finishes then drop back a level

	for (;;) {
		uint16 mode = _compact->mode; // get pointer to current script
		uint16 *scriptNo = SkyCompact::getSub(_compact, mode);
		uint16 *offset   = SkyCompact::getSub(_compact, mode + 2);

		*offset = script(*scriptNo, *offset);

		if (!*offset) // script finished
			_compact->mode -= 4;
		else if (_compact->mode == mode)
			return;
	}
}

void SkyLogic::autoRoute() {
	uint16 *route = 0;
	uint16 ret = _skyAutoRoute->autoRoute(_compact, &route);

	_compact->logic = L_SCRIPT; // continue the script

	if (ret != 1) // route failed
		_compact->downFlag = 1; // return fail to script
	else if (!route) // zero route
		_compact->downFlag = 2; // return fail to script
	else {
		_compact->grafixProg = route; // put graphic prog in
		_compact->downFlag = 0; // route ok
	}

	logicScript();
	return;
}

void SkyLogic::arAnim() {
	/// Follow a route
	/// Mega should be in getToMode

	// only check collisions on character boundaries
	if ((_compact->xcood & 7) || (_compact->ycood & 7)) {
		mainAnim();
		return;
	}

	// On character boundary. Have we been told to wait?
	// if not - are WE colliding?

	if (_compact->extCompact->waitingFor == 0xffff) { // 1st cycle of re-route does
		mainAnim();
		return;
	}

	if (_compact->extCompact->waitingFor) {
		// ok, we've been told we've hit someone
		// we will wait until we are no longer colliding
		// with them. here we check to see if we are (still) colliding.
		// if we are then run the stop script. if not clear the flag
		// and continue.

		// remember - this could be the first ar cycle for some time,
		// we might have been told to wait months ago. if we are
		// waiting for one person then another hits us then
		// c_waiting_for will be replaced by the new mega - this is
		// fine because the later collision will almost certainly
		// take longer to clear than the earlier one.

		if (collide(SkyState::fetchCompact(_compact->extCompact->waitingFor))) {
			stopAndWait();
			return;
		}

		// we are not in fact hitting this person so clr & continue
		// it must have registered some time ago

		_compact->extCompact->waitingFor = 0; // clear id flag
	}

	// ok, our turn to check for collisions

	uint16 *logicList = (uint16 *)SkyState::fetchCompact(_scriptVariables[LOGIC_LIST_NO]);
	Compact *cpt = 0;

	while (uint16 id = *logicList++) { // get an id

		if (id == 0xffff) { // address change?
			logicList = (uint16 *)SkyState::fetchCompact(*logicList); // get new logic list
			continue;
		}

		if (id == (uint16)(_scriptVariables[CUR_ID] & 0xffff)) // is it us?
			continue;

		_scriptVariables[HIT_ID] = id; // save target id for any possible c_mini_bump
		cpt = SkyState::fetchCompact(id); // let's have a closer look

		if (!(cpt->status & (1 << ST_COLLISION_BIT))) // can it collide?
			continue;

		if (cpt->screen != _compact->screen) // is it on our screen?
			continue;

		if (collide(cpt)) { // check for a hit
			// ok, we've hit a mega
			// is it moving... or something else?

			if (cpt->logic != L_AR_ANIM) { // check for following route
				// it is doing something else
				// we restart our get-to script
				// first tell it to wait for us - in case it starts moving
				// ( *it may have already hit us and stopped to wait )

				_compact->extCompact->waitingFor = 0xffff; // effect 1 cycle collision skip
				// tell it it is waiting for us
				cpt->extCompact->waitingFor = (uint16)(_scriptVariables[CUR_ID] & 0xffff);
				// restart current script
				*SkyCompact::getSub(_compact, _compact->mode + 2) = 0;
				_compact->logic = L_SCRIPT;
				logicScript();
				return;
			}

			script(_compact->extCompact->miniBump, 0);
			return;
		}
	}

	// ok, there was no collisions
	// now check for interaction request
	// *note: the interaction is always set up as an action script

	if (_compact->extCompact->request) {
		_compact->mode = C_ACTION_MODE; // put into action mode
		_compact->extCompact->actionSub = _compact->extCompact->request;
		_compact->extCompact->actionSub_off = 0;
		_compact->extCompact->request = 0; // trash request
		_compact->logic = L_SCRIPT;
		logicScript();
		return;
	}

	// any flag? - or any change?
	// if change then re-run the current script, which must be
	// a position independent get-to		 ----

	if (!_compact->extCompact->atWatch) { // any flag set?
		mainAnim();
		return;
	}

	// ok, there is an at watch - see if it's changed

	if (_compact->extCompact->atWas == _scriptVariables[_compact->extCompact->atWatch/4]) { // still the same?
		mainAnim();
		return;
	}

	// changed so restart the current script
	// *not suitable for base initiated ARing
	*SkyCompact::getSub(_compact, _compact->mode + 2) = 0;

	_compact->logic = L_SCRIPT;
	logicScript();
}

void SkyLogic::mainAnim() {
	/// Extension of arAnim()
	_compact->extCompact->waitingFor = 0; // clear possible zero-zero skip

	uint16 *sequence = _compact->grafixProg;
	if (!*sequence) {
		// ok, move to new anim segment
		sequence += 2;
		if (!*sequence) { // end of route?
			// ok, sequence has finished

			// will start afresh if new sequence continues in last direction
			_compact->extCompact->arAnimIndex = 0;

			_compact->downFlag = 0; // pass back ok to script
			_compact->logic = L_SCRIPT;
			logicScript();
			return;
		}

		_compact->grafixProg = sequence;
		_compact->extCompact->arAnimIndex = 0; // reset position
	}

	uint16 dir;
	while ((dir = _compact->extCompact->dir) != *(sequence + 1)) {
		// ok, setup turning
		_compact->extCompact->dir = *(sequence + 1);

		uint16 **tt = SkyCompact::getTurnTable(_compact,_compact->extCompact->megaSet, dir);
		if (tt[_compact->extCompact->dir]) {
			_compact->extCompact->turnProg = tt[_compact->extCompact->dir];
			_compact->logic = L_AR_TURNING;
			arTurn();
			return;
		}
	};

	uint16 *animList = *(uint16 **)SkyCompact::getCompactElem(_compact,
			C_ANIM_UP + _compact->extCompact->megaSet + dir * 4);

	uint16 arAnimIndex = _compact->extCompact->arAnimIndex;
	if (!animList[arAnimIndex/2]) {
		 arAnimIndex = 0;
		_compact->extCompact->arAnimIndex = 0; // reset
	}

	_compact->extCompact->arAnimIndex += S_LENGTH;

	*sequence       -= animList[(S_COUNT + arAnimIndex)/2]; // reduce the distance to travel
	_compact->frame  = animList[(S_FRAME + arAnimIndex)/2]; // new graphic frame
	_compact->xcood += animList[(S_AR_X  + arAnimIndex)/2]; // update x coordinate
	_compact->ycood += animList[(S_AR_Y  + arAnimIndex)/2]; // update y coordinate
}

void SkyLogic::arTurn() {
	_compact->frame = *_compact->extCompact->turnProg++;

	if (!*_compact->extCompact->turnProg) { // turn done?
		// Back to ar mode
		_compact->extCompact->arAnimIndex = 0;
		_compact->logic = L_AR_ANIM;
	}
}

void SkyLogic::alt() {
	/// change the current script
	_compact->logic = L_SCRIPT;
	*SkyCompact::getSub(_compact, _compact->mode) = _compact->extCompact->alt;
	*SkyCompact::getSub(_compact, _compact->mode + 2) = 0;
	logicScript();
}

void SkyLogic::anim() {
	/// Follow an animation sequence

	uint16 *grafixProg = _compact->grafixProg;

	while (*grafixProg) {
		if (*grafixProg == LF_START_FX) { // do fx
			grafixProg++;
			uint16 sound = *grafixProg++;
			uint16 volume = *grafixProg++;

			// channel 0
			fnStartFx(sound, 0, volume);
		} else if (*grafixProg >= LF_START_FX) { // do sync
			grafixProg++;

			Compact *cpt = SkyState::fetchCompact(*grafixProg++);

			cpt->sync = *grafixProg++;
		} else { // put coordinates and frame in
			_compact->xcood = *grafixProg++;
			_compact->ycood = *grafixProg++;

			_compact->frame = *grafixProg++ | _compact->offset;
			_compact->grafixProg = grafixProg;
			return;
		}
	}

	_compact->downFlag = 0;
	_compact->logic = L_SCRIPT;
	logicScript();
}

void SkyLogic::turn() {
	if (*_compact->extCompact->turnProg) {
		_compact->frame = *_compact->extCompact->turnProg++;
		return;
	}

	// turn_to_script:
	_compact->extCompact->arAnimIndex = 0;
	_compact->logic = L_SCRIPT;

	logicScript();
}

void SkyLogic::cursor() {
	error("Stub: SkyLogic::cursor");
}

void SkyLogic::talk() {
	error("Stub: SkyLogic::talk");
}

void SkyLogic::listen() {
	/// Stay in this mode until id in getToFlag leaves L_TALK mode

	Compact *cpt = SkyState::fetchCompact(_compact->flag);

	if (cpt->logic == L_TALK)
		return;

	_compact->logic = L_SCRIPT;
	logicScript();
}

void SkyLogic::stopped() {
	/// waiting for another mega to move or give-up trying
	///
	/// this mode will always be set up from a special script
	/// that will be one level higher than the script we
	/// would wish to restart from

	Compact *cpt = SkyState::fetchCompact(_compact->extCompact->waitingFor);

	if (!cpt->mood && collide(cpt))
		return;

	// we are free, continue processing the script

	// restart script one level below
	*SkyCompact::getSub(_compact, _compact->mode - 2) = 0;
	_compact->extCompact->waitingFor = 0xffff;

	_compact->logic = L_SCRIPT;
	logicScript();
}

void SkyLogic::choose() {
	// Remain in this mode until player selects some text
	if (!_scriptVariables[THE_CHOSEN_ONE])
		return;

	fnNoHuman(0, 0, 0); // kill mouse again

	// system_flags &= ~(1 << SF_CHOOSING); // restore save/restore

	_compact->logic = L_SCRIPT; // and continue script
	logicScript();
}

void SkyLogic::frames() {
	if (!_compact->sync)
		simpleAnim();

	_compact->downFlag = 0; // return 'ok' to script
	_compact->logic = L_SCRIPT;
	logicScript();
}

void SkyLogic::pause() {
	if (--_compact->flag)
		return;

	_compact->logic = L_SCRIPT;
	logicScript();
	return;
}

void SkyLogic::waitSync() {
	/// checks c_sync, when its non 0
	/// the id is put back into script mode
	// use this instead of loops in the script

	if (!_compact->sync)
		return;

	_compact->logic = L_SCRIPT;
	logicScript();
}

void SkyLogic::simpleAnim() {
	/// follow an animation sequence module whilst ignoring the coordinate data

	uint16 *grafixProg = _compact->grafixProg;

	// *grafix_prog: command
	while (*grafixProg) {
		if (*grafixProg != SEND_SYNC) {
			grafixProg++;
			grafixProg++; // skip coordinates

			// *grafix_prog: frame
			if (*grafixProg >= 64)
				_compact->frame = *grafixProg;
			else
				_compact->frame = *grafixProg + _compact->offset;

			grafixProg++;
			_compact->grafixProg = grafixProg;
			return;
		}

		grafixProg++;
		// *grafix_prog: id to sync
		Compact *compact2 = SkyState::fetchCompact(*grafixProg);
		grafixProg++;

		// *grafix_prog: sync
		compact2->sync = *grafixProg;
		grafixProg++;
	}

	_compact->downFlag = 0; // return 'ok' to script
	_compact->logic = L_SCRIPT;
	logicScript();
}

bool SkyLogic::collide(Compact *cpt) {
	MegaSet *m1 = SkyCompact::getMegaSet(_compact, _compact->extCompact->megaSet);
	MegaSet *m2 = SkyCompact::getMegaSet(cpt, cpt->extCompact->megaSet);

	// target's base coordinates
	uint16 x = cpt->xcood & 0xfff8;
	uint16 y = cpt->ycood & 0xfff8;

	// The collision is direction dependant
	switch (_compact->extCompact->dir) {
	case 0: // looking up
		x -= m1->colOffset; // compensate for inner x offsets
		x += m2->colOffset;

		if ((x + m2->colWidth) < _compact->xcood) // their rightmost
			return false;

		x -= m1->colWidth; // our left, their right
		if (x >= _compact->xcood)
			return false;

		y += 8; // bring them down a line
		if (y == _compact->ycood)
			return true;

		y += 8; // bring them down a line
		if (y == _compact->ycood)
			return true;

		return false;
	case 1: // looking down
		x -= m1->colOffset; // compensate for inner x offsets
		x += m2->colOffset;

		if ((x + m2->colWidth) >= _compact->xcood) // their rightmoast
			return false;

		x -= m1->colWidth; // our left, their right
		if (x >= _compact->xcood)
			return false;

		y -= 8; // bring them up a line
		if (y == _compact->ycood)
			return true;

		y -= 8; // bring them up a line
		if (y == _compact->ycood)
			return true;
		
		return false;
	case 2: // looking left

		if (y != _compact->ycood)
			return false;

		x += m2->lastChr;
		if (x == _compact->xcood)
			return true;

		x -= 8; // out another one
		if (x == _compact->xcood)
			return true;

		return false;
	case 3: // looking right
	case 4: // talking (not sure if this makes sense...) 

		if (y != _compact->ycood)
			return false;

		x -= m1->lastChr; // last block
		if (x == _compact->xcood)
			return true;

		x -= 8; // out another block
		if (x != _compact->xcood)
			return false;

		return true;
	default:
		error("Unknown Direction: %d", _compact->extCompact->dir);
	}
}

void SkyLogic::runGetOff() {
	uint32 getOff = _scriptVariables[GET_OFF];
	_scriptVariables[GET_OFF] = 0;
	if (getOff)
		script((uint16)(getOff & 0xffff), (uint16)(getOff >> 16));
}

void SkyLogic::stopAndWait() {
	_compact->mode += 4;

	uint16 *scriptNo = SkyCompact::getSub(_compact, _compact->mode);
	uint16 *offset   = SkyCompact::getSub(_compact, _compact->mode + 2);

	*scriptNo = (uint16)(_compact->extCompact->stopScript & 0xffff);
	*offset   = (uint16)(_compact->extCompact->stopScript >> 16);

	_compact->logic = L_SCRIPT;
	logicScript();
}

void SkyLogic::checkModuleLoaded(uint16 moduleNo) {
	if (!_moduleList[moduleNo])
		_moduleList[moduleNo] = (uint16 *)_skyDisk->loadFile((uint16)moduleNo + F_MODULE_0, NULL);
}

void SkyLogic::push(uint32 a) {
	if (_stackPtr > ARRAYSIZE(_stack) - 2)
		error("Stack overflow");
	_stack[_stackPtr++] = a;
}

uint32 SkyLogic::pop() {
	if (_stackPtr < 1 || _stackPtr > ARRAYSIZE(_stack) - 1)
		error("No items on Stack to pop");
	return _stack[--_stackPtr];
}

typedef bool (SkyLogic::*McodeTable) (uint32, uint32, uint32);
static  McodeTable mcodeTable[] = {
	&SkyLogic::fnCacheChip,
	&SkyLogic::fnCacheFast,
	&SkyLogic::fnDrawScreen,
	&SkyLogic::fnAr,
	&SkyLogic::fnArAnimate,
	&SkyLogic::fnIdle,
	&SkyLogic::fnInteract,
	&SkyLogic::fnStartSub,
	&SkyLogic::fnTheyStartSub,
	&SkyLogic::fnAssignBase,
	&SkyLogic::fnDiskMouse,
	&SkyLogic::fnNormalMouse,
	&SkyLogic::fnBlankMouse,
	&SkyLogic::fnCrossMouse,
	&SkyLogic::fnCursorRight,
	&SkyLogic::fnCursorLeft,
	&SkyLogic::fnCursorDown,
	&SkyLogic::fnOpenHand,
	&SkyLogic::fnCloseHand,
	&SkyLogic::fnGetTo,
	&SkyLogic::fnSetToStand,
	&SkyLogic::fnTurnTo,
	&SkyLogic::fnArrived,
	&SkyLogic::fnLeaving,
	&SkyLogic::fnSetAlternate,
	&SkyLogic::fnAltSetAlternate,
	&SkyLogic::fnKillId,
	&SkyLogic::fnNoHuman,
	&SkyLogic::fnAddHuman,
	&SkyLogic::fnAddButtons,
	&SkyLogic::fnNoButtons,
	&SkyLogic::fnSetStop,
	&SkyLogic::fnClearStop,
	&SkyLogic::fnPointerText,
	&SkyLogic::fnQuit,
	&SkyLogic::fnSpeakMe,
	&SkyLogic::fnSpeakMeDir,
	&SkyLogic::fnSpeakWait,
	&SkyLogic::fnSpeakWaitDir,
	&SkyLogic::fnChooser,
	&SkyLogic::fnHighlight,
	&SkyLogic::fnTextKill,
	&SkyLogic::fnStopMode,
	&SkyLogic::fnWeWait,
	&SkyLogic::fnSendSync,
	&SkyLogic::fnSendFastSync,
	&SkyLogic::fnSendRequest,
	&SkyLogic::fnClearRequest,
	&SkyLogic::fnCheckRequest,
	&SkyLogic::fnStartMenu,
	&SkyLogic::fnUnhighlight,
	&SkyLogic::fnFaceId,
	&SkyLogic::fnForeground,
	&SkyLogic::fnBackground,
	&SkyLogic::fnNewBackground,
	&SkyLogic::fnSort,
	&SkyLogic::fnNoSpriteEngine,
	&SkyLogic::fnNoSpritesA6,
	&SkyLogic::fnResetId,
	&SkyLogic::fnToggleGrid,
	&SkyLogic::fnPause,
	&SkyLogic::fnRunAnimMod,
	&SkyLogic::fnSimpleMod,
	&SkyLogic::fnRunFrames,
	&SkyLogic::fnAwaitSync,
	&SkyLogic::fnIncMegaSet,
	&SkyLogic::fnDecMegaSet,
	&SkyLogic::fnSetMegaSet,
	&SkyLogic::fnMoveItems,
	&SkyLogic::fnNewList,
	&SkyLogic::fnAskThis,
	&SkyLogic::fnRandom,
	&SkyLogic::fnPersonHere,
	&SkyLogic::fnToggleMouse,
	&SkyLogic::fnMouseOn,
	&SkyLogic::fnMouseOff,
	&SkyLogic::fnFetchX,
	&SkyLogic::fnFetchY,
	&SkyLogic::fnTestList,
	&SkyLogic::fnFetchPlace,
	&SkyLogic::fnCustomJoey,
	&SkyLogic::fnSetPalette,
	&SkyLogic::fnTextModule,
	&SkyLogic::fnChangeName,
	&SkyLogic::fnMiniLoad,
	&SkyLogic::fnFlushBuffers,
	&SkyLogic::fnFlushChip,
	&SkyLogic::fnSaveCoods,
	&SkyLogic::fnPlotGrid,
	&SkyLogic::fnRemoveGrid,
	&SkyLogic::fnEyeball,
	&SkyLogic::fnCursorUp,
	&SkyLogic::fnLeaveSection,
	&SkyLogic::fnEnterSection,
	&SkyLogic::fnRestoreGame,
	&SkyLogic::fnRestartGame,
	&SkyLogic::fnNewSwingSeq,
	&SkyLogic::fnWaitSwingEnd,
	&SkyLogic::fnSkipIntroCode,
	&SkyLogic::fnBlankScreen,
	&SkyLogic::fnPrintCredit,
	&SkyLogic::fnLookAt,
	&SkyLogic::fnLincTextModule,
	&SkyLogic::fnTextKill2,
	&SkyLogic::fnSetFont,
	&SkyLogic::fnStartFx,
	&SkyLogic::fnStopFx,
	&SkyLogic::fnStartMusic,
	&SkyLogic::fnStopMusic,
	&SkyLogic::fnFadeDown,
	&SkyLogic::fnFadeUp,
	&SkyLogic::fnQuitToDos,
	&SkyLogic::fnPauseFx,
	&SkyLogic::fnUnPauseFx,
	&SkyLogic::fnPrintf
};

static const uint32 forwardList1b[] = {
	JOBS_SPEECH,
	JOBS_S4,
	JOBS_ALARMED,
	JOEY_RECYCLE,
	SHOUT_SSS,
	JOEY_MISSION,
	TRANS_MISSION,
	SLOT_MISSION,
	CORNER_MISSION,
	JOEY_LOGIC,
	GORDON_SPEECH,
	JOEY_BUTTON_MISSION,
	LOB_DAD_SPEECH,
	LOB_SON_SPEECH,
	GUARD_SPEECH,
	MANTRACH_SPEECH,
	WRECK_SPEECH,
	ANITA_SPEECH,
	LAMB_FACTORY,
	FORE_SPEECH,
	JOEY_42_MISS,
	JOEY_JUNCTION_MISS,
	WELDER_MISSION,
	JOEY_WELD_MISSION,
	RADMAN_SPEECH,
	LINK_7_29,
	LINK_29_7,
	LAMB_TO_3,
	LAMB_TO_2,
	BURKE_SPEECH,
	BURKE_1,
	BURKE_2,
	DR_BURKE_1,
	JASON_SPEECH,
	JOEY_BELLEVUE,
	ANCHOR_SPEECH,
	ANCHOR_MISSION,
	JOEY_PC_MISSION,
	HOOK_MISSION,
	TREVOR_SPEECH,
	JOEY_FACTORY,
	HELGA_SPEECH,
	JOEY_HELGA_MISSION,
	GALL_BELLEVUE,
	GLASS_MISSION,
	LAMB_FACT_RETURN,
	LAMB_LEAVE_GARDEN,
	LAMB_START_29,
	LAMB_BELLEVUE,
	CABLE_MISSION,
	FOSTER_TOUR,
	LAMB_TOUR,
	FOREMAN_LOGIC,
	LAMB_LEAVE_FACTORY,
	LAMB_BELL_LOGIC,
	LAMB_FACT_2,
	START90,
	0,
	0,
	LINK_28_31,
	LINK_31_28,
	EXIT_LINC,
	DEATH_SCRIPT
};

static const uint32 forwardList2b[] = {
	STD_ON,
	STD_EXIT_LEFT_ON,
	STD_EXIT_RIGHT_ON,
	ADVISOR_188,
	SHOUT_ACTION,
	MEGA_CLICK,
	MEGA_ACTION
};

static const uint32 forwardList3b[] = {
	DANI_SPEECH,
	DANIELLE_GO_HOME,
	SPUNKY_GO_HOME,
	HENRI_SPEECH,
	BUZZER_SPEECH,
	FOSTER_VISIT_DANI,
	DANIELLE_LOGIC,
	JUKEBOX_SPEECH,
	VINCENT_SPEECH,
	EDDIE_SPEECH,
	BLUNT_SPEECH,
	DANI_ANSWER_PHONE,
	SPUNKY_SEE_VIDEO,
	SPUNKY_BARK_AT_FOSTER,
	SPUNKY_SMELLS_FOOD,
	BARRY_SPEECH,
	COLSTON_SPEECH,
	GALL_SPEECH,
	BABS_SPEECH,
	CHUTNEY_SPEECH,
	FOSTER_ENTER_COURT
};

static const uint32 forwardList4b[] = {
	WALTER_SPEECH,
	JOEY_MEDIC,
	JOEY_MED_LOGIC,
	JOEY_MED_MISSION72,
	KEN_LOGIC,
	KEN_SPEECH,
	KEN_MISSION_HAND,
	SC70_IRIS_OPENED,
	SC70_IRIS_CLOSED,
	FOSTER_ENTER_BOARDROOM,
	BORED_ROOM,
	FOSTER_ENTER_NEW_BOARDROOM,
	HOBS_END,
	SC82_JOBS_SSS
};

static const uint32 forwardList5b[] = {
	SET_UP_INFO_WINDOW,
	SLAB_ON,
	UP_MOUSE,
	DOWN_MOUSE,
	LEFT_MOUSE,
	RIGHT_MOUSE
};

void SkyLogic::initScriptVariables() {
	for (int i = 0; i < ARRAYSIZE(_scriptVariables); i++)
		_scriptVariables[i] = 0;

	_scriptVariables[2] = 141;
	_scriptVariables[109] = 62;
	_scriptVariables[145] = 1;
	_scriptVariables[146] = 2;
	_scriptVariables[450] = 8371;
	_scriptVariables[640] = 1;
	_scriptVariables[678] = 1;
	_scriptVariables[693] = 3;
	_scriptVariables[704] = 1;
	_scriptVariables[709] = 1;
	_scriptVariables[710] = 1;
	_scriptVariables[711] = 1;
	_scriptVariables[79] = 1;
	_scriptVariables[720] = 1;
	_scriptVariables[793] = 1;
	_scriptVariables[797] = 1;
	_scriptVariables[798] = 1;
	_scriptVariables[804] = 1;
	_scriptVariables[805] = 1;
	_scriptVariables[806] = 16731;
	_scriptVariables[807] = 1;
	_scriptVariables[808] = 2;
	_scriptVariables[817] = 1;
	_scriptVariables[818] = 1;
	_scriptVariables[819] = 1;
	_scriptVariables[820] = 1;
	_scriptVariables[821] = 1;

	memcpy(_scriptVariables + 352, forwardList1b, sizeof(forwardList1b));
	memcpy(_scriptVariables + 656, forwardList2b, sizeof(forwardList2b));
	memcpy(_scriptVariables + 721, forwardList3b, sizeof(forwardList3b));
	memcpy(_scriptVariables + 663, forwardList4b, sizeof(forwardList4b));
	memcpy(_scriptVariables + 505, forwardList5b, sizeof(forwardList5b));
}

/**
 * \fn uint32 SkyLogic::script(uint16 scriptNo, uint16 offset)
 * \brief This is the actual script engine. 
 *        It interprets script \a scriptNo starting at \a offset
 *
 * \param scriptNo The script to interpret.
 * 	\li \arg Bits 0-11 - Script number
 * 	\li \arg Bits 12-15 - Module number
 * \param offset At which offset to start interpreting the script.
 *
 * @return 0 if script finished. Else offset where to continue.
 */
uint16 SkyLogic::script(uint16 scriptNo, uint16 offset) {
script:
	/// process a script
	/// low level interface to interpreter

	uint16 moduleNo = (uint16)((scriptNo & 0xff00) >> 12);
	debug(3, "Doing Script %x\n", (offset << 16) | scriptNo);
	uint16 *scriptData = _moduleList[moduleNo]; // get module address

	if (!scriptData) { // The module has not been loaded
		scriptData = (uint16 *)_skyDisk->loadFile(moduleNo + F_MODULE_0, NULL);
		_moduleList[moduleNo] = scriptData; // module has been loaded
	}

	uint16 *moduleStart = scriptData;

	// Check whether we have an offset or what
	if (offset)
		scriptData = moduleStart + offset;
	else
		scriptData += READ_LE_UINT16(scriptData + (scriptNo & 0x0fff));

	uint32 a, b, c;
	uint16 command, s;

	for (;;) {
		command = READ_LE_UINT16(scriptData++); // get a command
		SkyDebug::script(command, scriptData);

		switch (command) {
		case 0: // push_variable
			push( _scriptVariables[READ_LE_UINT16(scriptData++)/4] );
			break;
		case 1: // less_than
			a = pop();
			b = pop();
			if (a < b)
				push(1);
			else
				push(0);
			break;
		case 2: // push_number
			push(READ_LE_UINT16(scriptData++));
			break;
		case 3: // not_equal
			a = pop();
			b = pop();
			if (a != b)
				push(1);
			else
				push(0);
			break;
		case 4: // if_and
			a = pop();
			b = pop();
			if (a && b)
				push(1);
			else
				push(0);
			break;
		case 5: // skip_zero
			s = READ_LE_UINT16(scriptData++);

			a = pop();
			if (!a)
				scriptData += s/2;
			break;
		case 6: // pop_var
			_scriptVariables[READ_LE_UINT16(scriptData++)/4] = pop();
			break;
		case 7: // minus
			a = pop();
			b = pop();
			push(b-a);
			break;
		case 8: // plus
			a = pop();
			b = pop();
			push(b+a);
			break;
		case 9: // skip_always
			s = READ_LE_UINT16(scriptData++);
			scriptData += s/2;
			break;
		case 10: // if_or
			a = pop();
			b = pop();
			if (a || b)
				push(1);
			else
				push(0);
			break;
		case 11: // call_mcode
			{
				a = READ_LE_UINT16(scriptData++);
				b = c = 0;
				assert(a <= 3);
				// No, I did not forget the "break"s
				switch (a) {
				case 3:
					c = pop();
				case 2:
					b = pop();
				case 1:
					a = pop();
				}

				uint16 mcode = READ_LE_UINT16(scriptData++)/4; // get mcode number
				SkyDebug::mcode(mcode, a, b, c);

				Compact *saveCpt = _compact;
				bool ret = (this->*mcodeTable[mcode]) (a, b, c);
				_compact = saveCpt;

				if (!ret)
					return (scriptData - moduleStart);
			}
			break;
		case 12: // more_than
			a = pop();
			b = pop();
			if (a > b)
				push(1);
			else
				push(0);
			break;
		case 14: // switch
			s = READ_LE_UINT16(scriptData++); // get number of cases

			a = pop(); // and value to switch on

			do {
				if (a == *scriptData) {
					scriptData += READ_LE_UINT16(scriptData + 1)/2;
					scriptData++;
					break;
				}
				scriptData += 2;
			} while (--s);

			s = READ_LE_UINT16(scriptData++);
			scriptData += s; // use the default
			scriptData--;
			break;
		case 15: // push_offset
			push( *(uint16 *)SkyCompact::getCompactElem(_compact, READ_LE_UINT16(scriptData++)) );
			break;
		case 16: // pop_offset
			// pop a value into a compact
			*(uint16 *)SkyCompact::getCompactElem(_compact, READ_LE_UINT16(scriptData++)) = (uint16)pop();
			break;
		case 17: // is_equal
			a = pop();
			b = pop();
			if (a == b)
				push(1);
			else
				push(0);
			break;
		case 18: { // skip_nz
				int16 t = READ_LE_UINT16(scriptData++);
				a = pop();
				if (a)
					scriptData += t/2;
				break;
			}
		case 13:
		case 19: // script_exit
			return 0;
		case 20: // restart_script
			goto script;
		default:
			error("Unknown script command: %d", command);
		}
	}
}

bool SkyLogic::fnCacheChip(uint32 a, uint32 b, uint32 c) {
	debug(5, "SkyDisk::fnCacheChip(%d);\n",a);
	_skyDisk->fnCacheChip(a);
	return true;
}

bool SkyLogic::fnCacheFast(uint32 a, uint32 b, uint32 c) {
	debug(5, "SkyDisk::fnCacheFast(%d);\n",a);
	_skyDisk->fnCacheFast(a);
	return true;
}

bool SkyLogic::fnDrawScreen(uint32 a, uint32 b, uint32 c) {
	debug(5, "Call: fnDrawScreen(%X, %X)\n",a,b);
	_skyScreen->fnDrawScreen(a, b);
	return true;
}

bool SkyLogic::fnAr(uint32 x, uint32 y, uint32 c) {
	_compact->downFlag = 1; // assume failure in-case logic is interupted by speech (esp Joey)

	_compact->extCompact->arTargetX = (uint16)x;
	_compact->extCompact->arTargetY = (uint16)y;
	_compact->logic = L_AR; // Set to AR mode

	_compact->xcood &= 0xfff8;
	_compact->ycood &= 0xfff8;

	return false; // drop out of script
}

bool SkyLogic::fnArAnimate(uint32 a, uint32 b, uint32 c) {
	_compact->mood = 0; // high level 'not stood still'
	_compact->logic = L_AR_ANIM;
	return false; // drop out of script
}

bool SkyLogic::fnIdle(uint32 a, uint32 b, uint32 c) {
	// set the player idling
	_compact->logic = 0;
	return true;
}

bool SkyLogic::fnInteract(uint32 targetId, uint32 b, uint32 c) {
	_compact->mode += 4; // next level up
	_compact->logic = L_SCRIPT;
	Compact *cpt = SkyState::fetchCompact(targetId);

	*SkyCompact::getSub(_compact, _compact->mode) = cpt->actionScript;
	*SkyCompact::getSub(_compact, _compact->mode + 2) = 0;

	return false;
}

bool SkyLogic::fnStartSub(uint32 scr, uint32 b, uint32 c) {
	_compact->mode += 4;
	*SkyCompact::getSub(_compact, _compact->mode) = (uint16)(scr & 0xffff);
	*SkyCompact::getSub(_compact, _compact->mode + 2) = (uint16)(scr >> 16);
	return false;
}

bool SkyLogic::fnTheyStartSub(uint32 mega, uint32 scr, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(mega);
	cpt->mode += 4;
	*SkyCompact::getSub(cpt, _compact->mode) = (uint16)(scr & 0xffff);
	*SkyCompact::getSub(cpt, _compact->mode + 2) = (uint16)(scr >> 16);
	return true;
}

bool SkyLogic::fnAssignBase(uint32 id, uint32 scr, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(id);
	cpt->mode = C_BASE_MODE;
	cpt->logic = L_SCRIPT;
	cpt->baseSub     = (uint16)(scr & 0xffff);
	cpt->baseSub_off = (uint16)(scr >> 16);
	return true;
}

bool SkyLogic::fnDiskMouse(uint32 a, uint32 b, uint32 c) {
	warning("Stub: fnDiskMouse");
	return true;
}

bool SkyLogic::fnNormalMouse(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnNormalMouse");
}

bool SkyLogic::fnBlankMouse(uint32 a, uint32 b, uint32 c) {
	return (_skyMouse->fnBlankMouse() != 0);
}

bool SkyLogic::fnCrossMouse(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnCrossMouse");
}

bool SkyLogic::fnCursorRight(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnCursorRight");
}

bool SkyLogic::fnCursorLeft(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnCursorLeft");
}

bool SkyLogic::fnCursorDown(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnCursorDown");
}

bool SkyLogic::fnOpenHand(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnOpenHand");
}

bool SkyLogic::fnCloseHand(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnCloseHand");
}

bool SkyLogic::fnGetTo(uint32 targetPlaceId, uint32 mode, uint32 c) {
	_compact->upFlag = (uint16)mode; // save mode for action script
	_compact->mode += 4; // next level up
	Compact *cpt = SkyState::fetchCompact(_compact->place);
	uint16 *getToTable = cpt->getToTable;

	while (*getToTable != targetPlaceId)
		getToTable += 2;

	// get new script
	*SkyCompact::getSub(_compact, _compact->mode) = *(getToTable + 1);
	*SkyCompact::getSub(_compact, _compact->mode + 2) = 0;

	return false; // drop out of script
}

bool SkyLogic::fnSetToStand(uint32 a, uint32 b, uint32 c) {
	_compact->mood = 1; // high level stood still

	uint16 *standList = *(uint16 **)SkyCompact::getCompactElem(_compact, C_STAND_UP
			+ _compact->extCompact->megaSet + _compact->extCompact->dir * 4); 

	_compact->offset = *standList++; // get frames offset
	_compact->grafixProg = standList;
	_compact->logic = L_SIMPLE_MOD;
	simpleAnim();
	return false; // drop out of script
}

bool SkyLogic::fnTurnTo(uint32 dir, uint32 b, uint32 c) {
	/// turn compact to direction dir

	uint16 curDir = _compact->extCompact->dir; // get current direction
	_compact->extCompact->dir = (uint16)(dir & 0xffff); // set new direction

	uint16 **tt = SkyCompact::getTurnTable(_compact, _compact->extCompact->megaSet, curDir);

	if (!tt[dir])
		return true; // keep going

	_compact->extCompact->turnProg = tt[dir]; // put turn program in
	_compact->logic = L_TURNING;

	turn();

	return false; // drop out of script
}

bool SkyLogic::fnArrived(uint32 scriptVar, uint32 b, uint32 c) {
	_compact->extCompact->leaving = (uint16)(scriptVar & 0xffff);
	_scriptVariables[scriptVar/4]++;
	return true;
}

bool SkyLogic::fnLeaving(uint32 a, uint32 b, uint32 c) {
	_compact->extCompact->atWatch = 0;

	if (_compact->extCompact->leaving) {
		_scriptVariables[_compact->extCompact->leaving/4]--;
		_compact->extCompact->leaving = 0; // I shall do this only once
	}

	return true; // keep going
}

bool SkyLogic::fnSetAlternate(uint32 scr, uint32 b, uint32 c) {
	_compact->extCompact->alt = (uint16)(scr & 0xffff);
	_compact->logic = L_ALT;
	return false;
}

bool SkyLogic::fnAltSetAlternate(uint32 target, uint32 scr, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(target);
	cpt->extCompact->alt = (uint16)(scr & 0xffff);
	cpt->logic = L_ALT;
	return false;
}

bool SkyLogic::fnKillId(uint32 id, uint32 b, uint32 c) {
	if (id) {
		Compact *cpt = SkyState::fetchCompact(id);
		if (cpt->status & (1 << 7))
			_skyGrid->removeObjectFromWalk(cpt);
		cpt->status = 0;
	}
	return true;
}

bool SkyLogic::fnNoHuman(uint32 a, uint32 b, uint32 c) {
	if (!_scriptVariables[MOUSE_STOP]) {
		_scriptVariables[MOUSE_STOP] &= 1;
		runGetOff();
		fnBlankMouse(0, 0, 0);
	}
	return true;
}

bool SkyLogic::fnAddHuman(uint32 a, uint32 b, uint32 c) {
	warning("Stub: fnAddHuman");
	return true;
}

bool SkyLogic::fnAddButtons(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnAddButtons");
}

bool SkyLogic::fnNoButtons(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnNoButtons");
}

bool SkyLogic::fnSetStop(uint32 a, uint32 b, uint32 c) {
	_scriptVariables[MOUSE_STOP] |= 1;
	return true;
}

bool SkyLogic::fnClearStop(uint32 a, uint32 b, uint32 c) {
	_scriptVariables[MOUSE_STOP] = 0;
	return true;
}

bool SkyLogic::fnPointerText(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnPointerText");
}

bool SkyLogic::fnQuit(uint32 a, uint32 b, uint32 c) {
	return false;
}

bool SkyLogic::fnSpeakMe(uint32 targetId, uint32 mesgNum, uint32 animNum) {
	stdSpeak(SkyState::fetchCompact(targetId), mesgNum, animNum, 0);
	return false; 	//drop out of script
}

bool SkyLogic::fnSpeakMeDir(uint32 targetId, uint32 mesgNum, uint32 animNum) {
	//must be player so don't cause script to drop out
	//this function sets the directional option whereby
	//the anim chosen is linked to c_dir
	animNum += _compact->extCompact->dir << 1;	//2 sizes (large and small)
	return fnSpeakMe(targetId, mesgNum, animNum);
}

bool SkyLogic::fnSpeakWait(uint32 id, uint32 message, uint32 animation) {
	_compact->flag = (uint16)(id & 0xffff);
	_compact->logic = L_LISTEN;
	return fnSpeakMe(id, message, animation);
}

bool SkyLogic::fnSpeakWaitDir(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnSpeakWaitDir");
}

bool SkyLogic::fnChooser(uint32 a, uint32 b, uint32 c) {
	
	// setup the text questions to be clicked on
	// read from TEXT1 until 0

//	systemFlags |= 1 << SF_CHOOSING; // can't save/restore while choosing

	_scriptVariables[THE_CHOSEN_ONE] = 0; // clear result

	uint32 *p = _scriptVariables + TEXT1;
	uint16 ycood = TOP_LEFT_Y; // rolling coordinate

	while (*p) {
		uint32 textNum = *p++;

		struct lowTextManager_t lowText = _skyText->lowTextManager(textNum, GAME_SCREEN_WIDTH, 0, 241, 0);
		
		uint8 *data = lowText.textData;

		// stipple the text
		uint16 height = ((dataFileHeader *)data)->s_height;
		uint16 width = ((dataFileHeader *)data)->s_width;
		width >>= 1;

		for (uint16 i = height; i > 0; i--) {
			for (uint16 j = width; j > 0; j--) {
				if (!*data) // only change 0's
					*data = 1;
				*data += 2;
			}
			data++;
		}

		_compact->getToFlag = (uint16)(textNum & 0xffff);
		_compact->downFlag = (uint16)(*p++ & 0xffff); // get animation number

		_compact->status |= ST_MOUSE; // mouse detects

		_compact->xcood = TOP_LEFT_X; // set coordinates
		_compact->ycood = ycood;
		ycood += 12;
	}

	if (p == _scriptVariables + TEXT1)
		return true;

	_compact->logic = L_CHOOSE; // player frozen until choice made
	fnAddHuman(0, 0, 0); // bring back mouse

	return false;
}

bool SkyLogic::fnHighlight(uint32 itemNo, uint32 pen, uint32 c) {
	error("Stub: fnHighlight");
}

bool SkyLogic::fnTextKill(uint32 a, uint32 b, uint32 c) {
	/// Kill of text items that are mouse detectable

	uint32 id = FIRST_TEXT_COMPACT;

	for (int i = 10; i > 0; i--) {
		Compact *cpt = SkyState::fetchCompact(id);
		if (cpt->status & (1 << 4))
			cpt->status = 0;
		id++;
	}
	return true;
}

bool SkyLogic::fnStopMode(uint32 a, uint32 b, uint32 c) {
	_compact->logic = L_STOPPED;
	return false;
}

bool SkyLogic::fnWeWait(uint32 id, uint32 b, uint32 c) {
	/// We have hit another mega
	/// we are going to wait for it to move

	_compact->extCompact->waitingFor = (uint16) id;
	stopAndWait();
	return true; // not sure about this
}

bool SkyLogic::fnSendSync(uint32 mega, uint32 sync, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(mega);
	cpt->sync = (uint16)(sync & 0xffff);
	return false;
}

bool SkyLogic::fnSendFastSync(uint32 mega, uint32 sync, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(mega);
	cpt->sync = (uint16)(sync & 0xffff);
	return true;
}

bool SkyLogic::fnSendRequest(uint32 target, uint32 scr, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(target);
	cpt->extCompact->request = (uint16)(scr & 0xffff);
	return false;
}

bool SkyLogic::fnClearRequest(uint32 target, uint32 b, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(target);
	cpt->extCompact->request = 0;
	return true;
}

bool SkyLogic::fnCheckRequest(uint32 a, uint32 b, uint32 c) {
	/// check for interaction request
	
	if (!_compact->extCompact->request)
		return true;

	_compact->mode = C_ACTION_MODE; // into action mode

	_compact->extCompact->actionSub = _compact->extCompact->request;
	_compact->extCompact->actionSub_off = 0;

	_compact->extCompact->request = 0; // trash request
	return false; // drop from script
}

bool SkyLogic::fnStartMenu(uint32 firstObject, uint32 b, uint32 c) {
	/// initialise the top menu bar
	// firstObject is o0 for game menu, k0 for linc

	uint i;

	// (1) FIRST, SET UP THE 2 ARROWS SO THEY APPEAR ON SCREEN

	Compact *cpt = SkyState::fetchCompact(47);
	cpt->status = ST_MOUSE + ST_FOREGROUND + ST_LOGIC + ST_RECREATE;
	cpt->screen = (uint16)(_scriptVariables[SCREEN] & 0xffff);

	cpt = SkyState::fetchCompact(48);
	cpt->status = ST_MOUSE + ST_FOREGROUND + ST_LOGIC + ST_RECREATE;
	cpt->screen = (uint16)(_scriptVariables[SCREEN] & 0xffff);

	// (2) COPY OBJECTS FROM NON-ZERO INVENTORY VARIABLES INTO OBJECT DISPLAY LIST (& COUNT THEM)

	// sort the objects and pad with blanks

	uint32 menuLength = 0;
	for (i = firstObject; i < firstObject + ARRAYSIZE(_objectList); i++) {
		if ( _scriptVariables[i] )
			_objectList[menuLength++] = _scriptVariables[i];
	}
	_scriptVariables[MENU_LENGTH] = menuLength;

	// (3) OK, NOW TOP UP THE LIST WITH THE REQUIRED NO. OF BLANK OBJECTS (for min display length 11)

	uint32 blankID = 51;
	for (i = menuLength; i < 11; i++)
		_objectList[i] = blankID++;

	// (4) KILL ID's OF ALL 20 OBJECTS SO UNWANTED ICONS (SCROLLED OFF) DON'T REMAIN ON SCREEN
	// (There should be a better way of doing this - only kill id of 12th item when menu has scrolled right)

	for (i = 0; i < ARRAYSIZE(_objectList); i++) {
		if (_objectList[i])
			(SkyState::fetchCompact(_objectList[i]))->status = ST_LOGIC;
	}

	// (5) NOW FIND OUT WHICH OBJECT TO START THE DISPLAY FROM (depending on scroll offset)

	if (menuLength < 11) // check we can scroll
		_scriptVariables[SCROLL_OFFSET] = 0;
	else if (menuLength < _scriptVariables[SCROLL_OFFSET] + 11)
		_scriptVariables[SCROLL_OFFSET] = menuLength - 11;

	// (6) AND FINALLY, INITIALISE THE 11 OBJECTS SO THEY APPEAR ON SCREEEN

	uint16 rollingX = 128 + 28;
	for (i = 0; i < 11; i++) {
		cpt = SkyState::fetchCompact(
				_objectList[_scriptVariables[SCROLL_OFFSET] + i]);

		cpt->status = ST_MOUSE + ST_FOREGROUND + ST_LOGIC + ST_RECREATE;
		cpt->screen = (uint16)(_scriptVariables[SCREEN] & 0xffff);

		cpt->xcood = rollingX;
		rollingX += 24;

		if (_scriptVariables[MENU] == 2)
			cpt->ycood = 136;
		else
			cpt->ycood = 112;
	}

	return true;
}

bool SkyLogic::fnUnhighlight(uint32 item, uint32 b, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(item);
	cpt->frame--;
	cpt->getToFlag = 0;
	return true;
}

bool SkyLogic::fnFaceId(uint32 otherId, uint32 b, uint32 c) {
	/// return the direction to turn to face another id
	/// pass back result in c_just_flag

	Compact *cpt = SkyState::fetchCompact(otherId);

	int16 x = _compact->xcood - cpt->xcood;

	if (x < 0) { // we're to the left
		x = -x;
		_compact->getToFlag = 3;
	} else { // it's to the left
		_compact->getToFlag = 2;
	}

	// now check y

	// we must find the true bottom of the sprite
	// it is not enough to use y coord because changing
	// sprite offsets can ruin the formula - instead we
	// will use the bottom of the mouse collision area

	int16 y = _compact->ycood - (cpt->ycood - cpt->mouseRelY - cpt->mouseSizeY);

	if (y < 0) { // it's below
		y = -y;
		if (y >= x)
			_compact->getToFlag = 1;
	} else { // it's above
		if (y >= x)
			_compact->getToFlag = 0;
	}
	return true;
}

bool SkyLogic::fnForeground(uint32 sprite, uint32 b, uint32 c) {
	/// Make sprite a foreground sprite
	Compact *cpt = SkyState::fetchCompact(sprite);
	cpt->status &= 0xfff8;
	cpt->status |= ST_FOREGROUND;
	return true;
}

bool SkyLogic::fnBackground(uint32 a, uint32 b, uint32 c) {
	/// Make us a background sprite
	_compact->status &= 0xfff8;
	_compact->status |= ST_BACKGROUND;
	return true;
}

bool SkyLogic::fnNewBackground(uint32 sprite, uint32 b, uint32 c) {
	/// Make sprite a background sprite
	Compact *cpt = SkyState::fetchCompact(sprite);
	cpt->status &= 0xfff8;
	cpt->status |= ST_BACKGROUND;
	return true;
}

bool SkyLogic::fnSort(uint32 mega, uint32 b, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(mega);
	cpt->status &= 0xfff8;
	cpt->status |= ST_SORT;
	return true;
}

bool SkyLogic::fnNoSpriteEngine(uint32 a, uint32 b, uint32 c) {
	/// stop the compact printing
	/// remove foreground, background & sort
	_compact->status &= 0xfff8;
	return true;	
}

bool SkyLogic::fnNoSpritesA6(uint32 us, uint32 b, uint32 c) {
	/// stop the compact printing
	/// remove foreground, background & sort
	Compact *cpt = SkyState::fetchCompact(us);
	cpt->status &= 0xfff8;
	return true;	
}

bool SkyLogic::fnResetId(uint32 id, uint32 resetBlock, uint32 c) {
	/// used when a mega is to be restarted
	/// eg - when a smaller mega turn to larger
	/// - a mega changes rooms...

	Compact *cpt = SkyState::fetchCompact(id);
	uint16 *rst = (uint16 *)SkyState::fetchCompact(resetBlock);

	uint16 off;
	while ((off = *rst++) != 0xffff)
		*(uint16 *)SkyCompact::getCompactElem(cpt, off) = *rst++;
	return true;
}

bool SkyLogic::fnToggleGrid(uint32 a, uint32 b, uint32 c) {
	/// Toggle a mega's grid plotting
	_compact->status ^= ST_GRID_PLOT;
	return true;
}

bool SkyLogic::fnPause(uint32 cycles, uint32 b, uint32 c) {
	/// Set mega to L_PAUSE
	_compact->flag = (uint16)(cycles & 0xffff);
	_compact->logic = L_PAUSE;
	return false; // drop out of script
}

bool SkyLogic::fnRunAnimMod(uint32 animNo, uint32 b, uint32 c) {
	uint16 *animation = (uint16 *)SkyState::fetchCompact(animNo);
	uint16 sprite = *animation++; // get sprite set
	_compact->offset = sprite;
	_compact->grafixProg = animation;
	_compact->logic = L_MOD_ANIMATE;
	anim();
	return false; // drop from script
}

bool SkyLogic::fnSimpleMod(uint32 animSeqNo, uint32 b, uint32 c) {
	uint16 *animSeq = (uint16 *)SkyState::fetchCompact(animSeqNo);

	_compact->offset = *animSeq++;
	assert(*animSeq != 0);
	_compact->grafixProg = animSeq;
	_compact->logic = L_SIMPLE_MOD;
	simpleAnim();
	return false;
}

bool SkyLogic::fnRunFrames(uint32 sequenceNo, uint32 b, uint32 c) {
	uint16 *sequence = (uint16 *)SkyState::fetchCompact(sequenceNo);

	_compact->logic = L_FRAMES;
	_compact->offset = *sequence++;
	_compact->grafixProg = sequence;
	simpleAnim();
	return false;
}

bool SkyLogic::fnAwaitSync(uint32 a, uint32 b, uint32 c) {
	if (_compact->sync)
		return true;

	_compact->logic = L_WAIT_SYNC;
	return false;
}

bool SkyLogic::fnIncMegaSet(uint32 a, uint32 b, uint32 c) {
	_compact->extCompact->megaSet += NEXT_MEGA_SET;
	return true;
}

bool SkyLogic::fnDecMegaSet(uint32 a, uint32 b, uint32 c) {
	_compact->extCompact->megaSet -= NEXT_MEGA_SET;
	return true;
}

bool SkyLogic::fnSetMegaSet(uint32 mega, uint32 setNo, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(mega);
	cpt->extCompact->megaSet = (uint16) (setNo * NEXT_MEGA_SET);
	return true;
}

bool SkyLogic::fnMoveItems(uint32 listNo, uint32 screenNo, uint32 c) {
	/// Move a list of id's to another screen
	uint16 *p = SkyCompact::move_list[listNo];
	for (int i = 0; i < 2; i++) {
		if (!*p)
			return true;
		Compact *cpt = SkyState::fetchCompact(*p++);
		cpt->screen = (uint16)(screenNo & 0xffff);
	}
	return true;
}

bool SkyLogic::fnNewList(uint32 a, uint32 b, uint32 c) {
	/// Reset the chooser list
	for (int i = 0; i < 16; i++)
		_scriptVariables[TEXT1 + i] = 0;
	return true;
}

bool SkyLogic::fnAskThis(uint32 textNo, uint32 animNo, uint32 c) {
	// find first free position
	uint32 *p = _scriptVariables + TEXT1;
	while (*p)
		p += 2;
	*p++ = textNo;
	*p = animNo;
	return true;
}

bool SkyLogic::fnRandom(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnRandom");
}

bool SkyLogic::fnPersonHere(uint32 id, uint32 room, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(id);
	_scriptVariables[RESULT] = cpt->screen == room ? 1 : 0;
	return true;
}

bool SkyLogic::fnToggleMouse(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnToggleMouse");
}

bool SkyLogic::fnMouseOn(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnMouseOn");
}

bool SkyLogic::fnMouseOff(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnMouseOff");
}

bool SkyLogic::fnFetchX(uint32 id, uint32 b, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(id);
	_scriptVariables[RESULT] = cpt->xcood;
	return true;
}

bool SkyLogic::fnFetchY(uint32 id, uint32 b, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(id);
	_scriptVariables[RESULT] = cpt->ycood;
	return true;
}

bool SkyLogic::fnTestList(uint32 id, uint32 x, uint32 y) {
	_scriptVariables[RESULT] = 0; // assume fail
	uint16 *list = (uint16 *)SkyState::fetchCompact(id);

	while (*list) { // end of list?
		if (*list++ >= x) // left x
			continue;
			
		if (*list++ < x) // right x
			continue;

		if (*list++ >= y) // top y
			continue;

		if (*list++ < y) // bottom y
			continue;

		// get value
		_scriptVariables[RESULT] = *list++;
	}
	return true;
}

bool SkyLogic::fnFetchPlace(uint32 id, uint32 b, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(id);
	_scriptVariables[RESULT] = cpt->place;
	return true;
}

bool SkyLogic::fnCustomJoey(uint32 id, uint32 b, uint32 c) {
	/// return id's x & y coordinate & c_mood (i.e. stood still yes/no)
	/// used by Joey-Logic - done in code like this because scripts can't
	/// get access to another megas compact as easily

	Compact *cpt = SkyState::fetchCompact(id);
	
	_scriptVariables[PLAYER_X] = cpt->xcood;
	_scriptVariables[PLAYER_Y] = cpt->ycood;
	_scriptVariables[PLAYER_MOOD] = cpt->mood;
	_scriptVariables[PLAYER_SCREEN] = cpt->screen;
	return true;
}

bool SkyLogic::fnSetPalette(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnSetPalette");
}

bool SkyLogic::fnTextModule(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnTextModule");
}

bool SkyLogic::fnChangeName(uint32 id, uint32 textNo, uint32 c) {
	Compact *cpt = SkyState::fetchCompact(id);
	cpt->cursorText = (uint16) textNo;
	return true;
}

bool SkyLogic::fnMiniLoad(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnMiniLoad");
}

bool SkyLogic::fnFlushBuffers(uint32 a, uint32 b, uint32 c) {
	_skyDisk->fnFlushBuffers();
	return true;
}

bool SkyLogic::fnFlushChip(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnFlushChip");
}

bool SkyLogic::fnSaveCoods(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnSaveCoods");
}

bool SkyLogic::fnPlotGrid(uint32 x, uint32 y, uint32 width) {
	_skyGrid->plotGrid(x, y, width, _compact);
	return true;
}

bool SkyLogic::fnRemoveGrid(uint32 x, uint32 y, uint32 width) {
	_skyGrid->removeGrid(x, y, width, _compact);
	return true;
}

bool SkyLogic::fnEyeball(uint32 id, uint32 b, uint32 c) {
	// set 'result' to frame no. pointing to foster, according to table used
	// eg. FN_eyeball (id_eye_90_table);

	uint16 *eyeTable = (uint16 *)SkyState::fetchCompact(id);
	Compact *cpt = SkyState::fetchCompact(ID_BLUE_FOSTER);

	uint32 x = cpt->xcood; // 168 < x < 416
	x -= 168;
	x >>= 3;

	uint32 y = cpt->ycood; // 256 < y < 296
	y -= 256;
	y <<= 2;

	_scriptVariables[RESULT] = eyeTable[x + y] + S91;
	return true;
}

bool SkyLogic::fnCursorUp(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnCursorUp");
}

bool SkyLogic::fnLeaveSection(uint32 sectionNo, uint32 b, uint32 c) {
	if (SkyState::isDemo(_gameVersion))
		error("End of demo");
	
	if (sectionNo == 5) //linc section - has different mouse icons
		_skyMouse->replaceMouseCursors(60302);

	return true;
}

bool SkyLogic::fnEnterSection(uint32 sectionNo, uint32 b, uint32 c) {

	if (SkyState::isDemo(_gameVersion))
		if (sectionNo > 2)
			error("End of demo");

	_scriptVariables[CUR_SECTION] = sectionNo;

	if (sectionNo == 5) //linc section - has different mouse icons
		_skyMouse->replaceMouseCursors(60302);

	else
		if (sectionNo != _currentSection) {

			_currentSection = sectionNo;
			_saveCurrentSection = sectionNo;

			sectionNo++;
			_skyMusic->loadSection((byte)sectionNo);
			_skySound->loadSection((byte)sectionNo);
			_skyGrid->loadGrids();

		}
			
	return true;
}

bool SkyLogic::fnRestoreGame(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnRestoreGame");
}

bool SkyLogic::fnRestartGame(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnRestartGame");
}

bool SkyLogic::fnNewSwingSeq(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnNewSwingSeq");
}

bool SkyLogic::fnWaitSwingEnd(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnWaitSwingEnd");
}

bool SkyLogic::fnSkipIntroCode(uint32 a, uint32 b, uint32 c) {
	warning("Stub: fnSkipIntroCode");
	return true;
}

bool SkyLogic::fnBlankScreen(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnBlankScreen");
}

bool SkyLogic::fnPrintCredit(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnPrintCredit");
}

bool SkyLogic::fnLookAt(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnLookAt");
}

bool SkyLogic::fnLincTextModule(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnLincTextModule");
}

bool SkyLogic::fnTextKill2(uint32 a, uint32 b, uint32 c) {
	/// Kill all text items

	uint32 id = FIRST_TEXT_COMPACT;

	for (int i = 10; i > 0; i--) {
		Compact *cpt = SkyState::fetchCompact(id);
		cpt->status = 0;
		id++;
	}
	return true;
}

bool SkyLogic::fnSetFont(uint32 font, uint32 b, uint32 c) {
	_skyText->fnSetFont(font);
	return true;
}

bool SkyLogic::fnStartFx(uint32 sound, uint32 b, uint32 c) {
	return _skySound->fnStartFx(sound);
}

bool SkyLogic::fnStopFx(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnStopFx");
}

bool SkyLogic::fnStartMusic(uint32 a, uint32 b, uint32 c) {
	_skyMusic->startMusic((uint16)a);
	return true;
}

bool SkyLogic::fnStopMusic(uint32 a, uint32 b, uint32 c) {
	_skyMusic->startMusic(0);
	return true;
}

bool SkyLogic::fnFadeDown(uint32 a, uint32 b, uint32 c) {
	_skyScreen->fnFadeDown(a);
	return true;
}

bool SkyLogic::fnFadeUp(uint32 a, uint32 b, uint32 c) {
	_skyScreen->fnFadeUp(a,b);
	return true;
}

bool SkyLogic::fnQuitToDos(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnQuitToDos");
}

bool SkyLogic::fnPauseFx(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnPauseFx");
}

bool SkyLogic::fnUnPauseFx(uint32 a, uint32 b, uint32 c) {
	error("Stub: fnUnPauseFx");
}

bool SkyLogic::fnPrintf(uint32 a, uint32 b, uint32 c) {
	printf("fnPrintf: %d\n", a);
	return true;
}

void SkyLogic::stdSpeak(Compact *target, uint32 textNum, uint32 animNum, uint32 base) {
	//animNum == -1 (0x??FF) means directional
	
	uint8 offset = (uint8)(target->extCompact->megaSet / NEXT_MEGA_SET);
	uint16 *animPtr = 0;
	
	if (animNum > 0xFF)
		warning("animNum > 255! - tell joostp when/where this happens");
	
	//FIXME: Is this correct?
	if (animNum >= 0xFF)
		offset += -1;
	else
		offset += (uint8)(animNum & 0xFF);   //get correct anim no

	if (SkyTalkAnims::animTalkTableIsPointer[offset]) //is it a pointer?
		animPtr = (uint16 *)SkyTalkAnims::animTalkTablePtr[offset];
	else { 	//then it must be a value
		animPtr = (uint16 *)SkyState::fetchCompact(SkyTalkAnims::animTalkTableVal[offset]);
		target->offset = *animPtr++;
		target->getToFlag = *animPtr++;
	}

	target->grafixProg = animPtr;

	//now form the text sprite
	struct lowTextManager_t textInfo;
	textInfo = _skyText->lowTextManager(textNum, FIXED_TEXT_WIDTH, 0, (uint8)target->extCompact->spColour, true);    
	target->extCompact->spTextId = textInfo.compactNum;	//So we know what text to kill
	byte *textGfx = textInfo.textData;

	//create the x coordinate for the speech text
	//we need the talkers sprite information

	_compact->screen = target->screen;	//put our screen in

	if (_scriptVariables[SCREEN] == target->screen) { // Only use coordinates if we are on the current screen 
		//talking on-screen
		byte *targetGfx = (byte *)SkyState::fetchItem(target->frame >> 6);
		uint16 xPos = target->xcood + ((struct dataFileHeader *)targetGfx)->s_offset_x;
		uint16 width = (((struct dataFileHeader *)targetGfx)->s_width >> 1);

		xPos += width - (FIXED_TEXT_WIDTH / 2);	//middle of talker

		if (xPos > TOP_LEFT_X)
			xPos = TOP_LEFT_X;

		width += FIXED_TEXT_WIDTH;
		if ((TOP_LEFT_X + FULL_SCREEN_WIDTH) <= width) {
			xPos = TOP_LEFT_X + FULL_SCREEN_WIDTH;
			xPos -= FIXED_TEXT_WIDTH;
		}
			
		_compact->xcood = xPos;
		uint16 yPos = target->ycood + ((struct dataFileHeader *)targetGfx)->s_offset_y - 6 - ((struct dataFileHeader *)textGfx)->s_height;
		
		if (yPos > TOP_LEFT_Y)
			yPos = TOP_LEFT_Y;

		_compact->ycood = yPos;
		//_logicTalkButtonRelease = 1;  
			
	} else {
		//talking off-screen
		target->extCompact->spTextId = 0; 	//don't kill any text 'cos none was made
		_compact->status = 0;	//don't display text
		//_logicTalkButtonRelease = 1; 
	}

	target->extCompact->spTime = (uint16)_skyText->_dtLetters + 5;
	target->logic = L_TALK; 
}