aboutsummaryrefslogtreecommitdiff
path: root/engines/tony/mpal/mpal.cpp
blob: 1de5c6850c731f2d8ee46f1787b7aa8cac0a21f9 (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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *
 */
/*
 * This code is based on original Tony Tough source code
 *
 * Copyright (c) 1997-2003 Nayma Software
 */

#include "common/scummsys.h"
#include "common/file.h"
#include "common/savefile.h"
#include "common/system.h"
#include "tony/tony.h"
#include "tony/mpal/lzo.h"
#include "tony/mpal/mpal.h"
#include "tony/mpal/mpaldll.h"

namespace Tony {

namespace MPAL {

/****************************************************************************\
*       Internal functions
\****************************************************************************/

/**
 * Locks the variables for access
 */
void lockVar() {
	GLOBALS._lpmvVars = (LpMpalVar)globalLock(GLOBALS._hVars);
}

/**
 * Unlocks variables after use
 */
void unlockVar() {
	globalUnlock(GLOBALS._hVars);
}

/**
 * Locks the messages for access
 */
static void LockMsg() {
#ifdef NEED_LOCK_MSGS
	GLOBALS._lpmmMsgs = (LpMpalMsg)globalLock(GLOBALS._hMsgs);
#endif
}

/**
 * Unlocks the messages after use
 */
static void UnlockMsg() {
#ifdef NEED_LOCK_MSGS
	globalUnlock(GLOBALS._hMsgs);
#endif
}

/**
 * Locks the dialogs for access
 */
static void lockDialogs() {
	GLOBALS._lpmdDialogs = (LpMpalDialog)globalLock(GLOBALS._hDialogs);
}

/**
 * Unlocks the dialogs after use
 */
static void unlockDialogs() {
	globalUnlock(GLOBALS._hDialogs);
}

/**
 * Locks the location data structures for access
 */
static void lockLocations() {
	GLOBALS._lpmlLocations = (LpMpalLocation)globalLock(GLOBALS._hLocations);
}

/**
 * Unlocks the location structures after use
 */
static void unlockLocations() {
	globalUnlock(GLOBALS._hLocations);
}

/**
 * Locks the items structures for use
 */
static void lockItems() {
	GLOBALS._lpmiItems = (LpMpalItem)globalLock(GLOBALS._hItems);
}

/**
 * Unlocks the items structures after use
 */
static void unlockItems() {
	globalUnlock(GLOBALS._hItems);
}

/**
 * Locks the script data structures for use
 */
static void LockScripts() {
	GLOBALS._lpmsScripts = (LpMpalScript)globalLock(GLOBALS._hScripts);
}

/**
 * Unlocks the script data structures after use
 */
static void unlockScripts() {
	globalUnlock(GLOBALS._hScripts);
}

/**
 * Returns the current value of a global variable
 *
 * @param lpszVarName		Name of the variable
 * @returns		Current value
 * @remarks		Before using this method, you must call lockVar() to
 * lock the global variablves for use. Then afterwards, you will
 * need to remember to call UnlockVar()
 */
int32 varGetValue(const char *lpszVarName) {
	LpMpalVar v = GLOBALS._lpmvVars;

	for (int i = 0; i < GLOBALS._nVars; v++, i++)
		if (strcmp(lpszVarName, v->_lpszVarName) == 0)
			return v->_dwVal;

	GLOBALS._mpalError = 1;
	return 0;
}

/**
 * Sets the value of a MPAL global variable
 * @param lpszVarName       Name of the variable
 * @param val				Value to set
 */
void varSetValue(const char *lpszVarName, int32 val) {
	LpMpalVar v = GLOBALS._lpmvVars;

	for (uint i = 0; i < GLOBALS._nVars; v++, i++)
		if (strcmp(lpszVarName, v->_lpszVarName) == 0) {
			v->_dwVal = val;
			if (GLOBALS._lpiifCustom != NULL && strncmp(v->_lpszVarName, "Pattern.", 8) == 0) {
				i = 0;
				sscanf(v->_lpszVarName, "Pattern.%u", &i);
				GLOBALS._lpiifCustom(i, val, -1);
			} else if (GLOBALS._lpiifCustom != NULL && strncmp(v->_lpszVarName, "Status.", 7) == 0) {
				i = 0;
				sscanf(v->_lpszVarName,"Status.%u", &i);
				GLOBALS._lpiifCustom(i, -1, val);
			}
			return;
		}

	GLOBALS._mpalError = 1;
	return;
}

/**
 * Find the index of a location within the location array. Remember to call LockLoc() beforehand.
 *
 * @param nLoc				Location number to search for
 * @returns		Index, or -1 if the location is not present
 * @remarks		This function requires the location list to have
 * first been locked with a call to LockLoc().
 */
static int locGetOrderFromNum(uint32 nLoc) {
	LpMpalLocation loc = GLOBALS._lpmlLocations;

	for (int i = 0; i < GLOBALS._nLocations; i++, loc++)
		if (loc->_nObj == nLoc)
			return i;

	return -1;
}

/**
 * Find the index of a message within the messages array
 * @param nMsg				Message number to search for
 * @returns		Index, or -1 if the message is not present
 * @remarks		This function requires the message list to have
 * first been locked with a call to LockMsg()
 */
static int msgGetOrderFromNum(uint32 nMsg) {
	LpMpalMsg msg = GLOBALS._lpmmMsgs;

	for (int i = 0; i < GLOBALS._nMsgs; i++, msg++) {
		if (msg->_wNum == nMsg)
			return i;
	}

	return -1;
}

/**
 * Find the index of an item within the items array
 * @param nItem				Item number to search for
 * @returns		Index, or -1 if the item is not present
 * @remarks		This function requires the item list to have
 * first been locked with a call to LockItems()
 */
static int itemGetOrderFromNum(uint32 nItem) {
	LpMpalItem item = GLOBALS._lpmiItems;

	for (int i = 0; i < GLOBALS._nItems; i++, item++) {
		if (item->_nObj == nItem)
			return i;
	}

	return -1;
}

/**
 * Find the index of a script within the scripts array
 * @param nScript			Script number to search for
 * @returns		Index, or -1 if the script is not present
 * @remarks		This function requires the script list to have
 * first been locked with a call to LockScripts()
 */
static int scriptGetOrderFromNum(uint32 nScript) {
	LpMpalScript script = GLOBALS._lpmsScripts;

	for (int i = 0; i < GLOBALS._nScripts; i++, script++) {
		if (script->_nObj == nScript)
			return i;
	}

	return -1;
}

/**
 * Find the index of a dialog within the dialogs array
 * @param nDialog			Dialog number to search for
 * @returns		Index, or -1 if the dialog is not present
 * @remarks		This function requires the dialog list to have
 * first been locked with a call to LockDialogs()
 */
static int dialogGetOrderFromNum(uint32 nDialog) {
	LpMpalDialog dialog = GLOBALS._lpmdDialogs;

	for (int i = 0; i < GLOBALS._nDialogs; i++, dialog++) {
		if (dialog->_nObj == nDialog)
			return i;
	}

	return -1;
}

/**
 * Duplicates a message
 * @param nMsgOrd			Index of the message inside the messages array
 * @returns		Pointer to the duplicated message.
 * @remarks		Remember to free the duplicated message when done with it.
 */
static char *DuplicateMessage(uint32 nMsgOrd) {
	const char *origmsg;
	char *clonemsg;

	if (nMsgOrd == (uint32)-1)
		return NULL;

	origmsg = (const char *)globalLock(GLOBALS._lpmmMsgs[nMsgOrd]._hText);

	int j = 0;
	while (origmsg[j] != '\0' || origmsg[j + 1] != '\0')
		j++;
	j += 2;

	clonemsg = (char *)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, j);
	if (clonemsg == NULL)
		return NULL;

	memcpy(clonemsg, origmsg, j);
	globalUnlock(GLOBALS._lpmmMsgs[nMsgOrd]._hText);

	return clonemsg;
}

/**
 * Duplicate a sentence of a dialog
 * @param nDlgOrd			Index of the dialog in the dialogs array
 * @param nPeriod           Sentence number to be duplicated.
 * @returns		Pointer to the duplicated phrase. Remember to free it
 * when done with it.
 */
static char *duplicateDialogPeriod(uint32 nPeriod) {
	const char *origmsg;
	char *clonemsg;
	LpMpalDialog dialog = GLOBALS._lpmdDialogs + GLOBALS._nExecutingDialog;

	for (int j = 0; dialog->_periods[j] != NULL; j++) {
		if (dialog->_periodNums[j] == nPeriod) {
			// Found the phrase, it should be duplicated
			origmsg = (const char *)globalLock(dialog->_periods[j]);

			// Calculate the length and allocate memory
			int i = 0;
			while (origmsg[i] != '\0')
				i++;

			clonemsg = (char *)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, i + 1);
			if (clonemsg == NULL)
				return NULL;

			memcpy(clonemsg, origmsg, i);

			globalUnlock(dialog->_periods[j]);

			return clonemsg;
		}
	}

	return NULL;
}

/**
 * Load a resource from the MPR file
 *
 * @param dwId				ID of the resource to load
 * @returns		Handle to the loaded resource
 */
MpalHandle resLoad(uint32 dwId) {
	MpalHandle h;
	char head[4];
	byte *temp, *buf;

	for (int i = 0; i < GLOBALS._nResources; i++)
		if (GLOBALS._lpResources[i * 2] == dwId) {
			GLOBALS._hMpr.seek(GLOBALS._lpResources[i * 2 + 1]);
			uint32 nBytesRead = GLOBALS._hMpr.read(head, 4);
			if (nBytesRead != 4)
				return NULL;
			if (head[0] != 'R' || head[1] != 'E' || head[2] != 'S' || head[3] != 'D')
				return NULL;

			uint32 nSizeDecomp = GLOBALS._hMpr.readUint32LE();
			if (GLOBALS._hMpr.err())
				return NULL;

			uint32 nSizeComp = GLOBALS._hMpr.readUint32LE();
			if (GLOBALS._hMpr.err())
				return NULL;

			h = globalAllocate(GMEM_MOVEABLE | GMEM_ZEROINIT, nSizeDecomp + (nSizeDecomp / 1024) * 16);
			buf = (byte *)globalLock(h);
			temp = (byte *)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, nSizeComp);

			nBytesRead = GLOBALS._hMpr.read(temp, nSizeComp);
			if (nBytesRead != nSizeComp)
				return NULL;

			lzo1x_decompress(temp, nSizeComp, buf, &nBytesRead);
			if (nBytesRead != nSizeDecomp)
				return NULL;

			globalDestroy(temp);
			globalUnlock(h);
			return h;
		}

	return NULL;
}

static uint32 *getSelectList(uint32 i) {
	uint32 *sl;
	LpMpalDialog dialog = GLOBALS._lpmdDialogs + GLOBALS._nExecutingDialog;

	// Count how many are active selects
	int num = 0;
	for (int j = 0; dialog->_choice[i]._select[j]._dwData != 0; j++) {
		if (dialog->_choice[i]._select[j]._curActive)
			num++;
	}

	// If there are 0, it's a mistake
	if (num == 0)
		return NULL;

	sl = (uint32 *)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizeof(uint32) * (num + 1));
	if (sl == NULL)
		return NULL;

	// Copy all the data inside the active select list
	int k = 0;
	for (int j = 0; dialog->_choice[i]._select[j]._dwData != 0; j++) {
		if (dialog->_choice[i]._select[j]._curActive)
			sl[k++] = dialog->_choice[i]._select[j]._dwData;
	}

	sl[k] = (uint32)NULL;
	return sl;
}

static uint32 *GetItemList(uint32 nLoc) {
	uint32 *il;
	LpMpalVar v = GLOBALS._lpmvVars;

	uint32 num = 0;
	for (uint32 i = 0; i < GLOBALS._nVars; i++, v++) {
		if (strncmp(v->_lpszVarName, "Location", 8) == 0 && v->_dwVal == nLoc)
			num++;
	}

	il = (uint32 *)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizeof(uint32) * (num + 1));
	if (il == NULL)
		return NULL;

	v = GLOBALS._lpmvVars;
	uint32 j = 0;
	for (uint32 i = 0; i < GLOBALS._nVars; i++, v++) {
		if (strncmp(v->_lpszVarName, "Location", 8) == 0 && v->_dwVal == nLoc) {
			sscanf(v->_lpszVarName, "Location.%u", &il[j]);
			j++;
		}
	}

	il[j] = (uint32)NULL;
	return il;
}

static LpItem getItemData(uint32 nOrdItem) {
	LpMpalItem curitem = GLOBALS._lpmiItems + nOrdItem;
	char *dat;
	char *patlength;

	// Zeroing out the allocated memory is required!!!
	LpItem ret = (LpItem)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizeof(Item));
	if (ret == NULL)
		return NULL;
	ret->_speed = 150;

	MpalHandle hDat = resLoad(curitem->_dwRes);
	dat = (char *)globalLock(hDat);

	if (dat[0] == 'D' && dat[1] == 'A' && dat[2] == 'T') {
		int i = dat[3]; // For version 1.0!!
		dat += 4;

		if (i >= 0x10) { // From 1.0, there's a destination point for each object
			ret->_destX = (int16)READ_LE_UINT16(dat);
			ret->_destY = (int16)READ_LE_UINT16(dat + 2);
			dat += 4;
		}

		if (i >= 0x11) { // From 1.1, there's animation speed
			ret->_speed = READ_LE_UINT16(dat);
			dat += 2;
		} else
			ret->_speed = 150;
	}

	ret->_numframe = *dat++;
	ret->_numpattern = *dat++;
	ret->_destZ = *dat++;

	// Upload the left & top co-ordinates of each frame
	for (int i = 0; i < ret->_numframe; i++) {
		ret->_frameslocations[i].left = (int16)READ_LE_UINT16(dat);
		ret->_frameslocations[i].top = (int16)READ_LE_UINT16(dat + 2);
		dat += 4;
	}

	// Upload the size of each frame and calculate the right & bottom
	for (int i = 0; i < ret->_numframe; i++) {
		ret->_frameslocations[i].right = (int16)READ_LE_UINT16(dat) + ret->_frameslocations[i].left;
		ret->_frameslocations[i].bottom = (int16)READ_LE_UINT16(dat + 2) + ret->_frameslocations[i].top;
		dat += 4;
	}

	// Upload the bounding boxes of each frame
	for (int i = 0; i < ret->_numframe; i++) {
		ret->_bbox[i].left = (int16)READ_LE_UINT16(dat);
		ret->_bbox[i].top = (int16)READ_LE_UINT16(dat + 2);
		ret->_bbox[i].right = (int16)READ_LE_UINT16(dat + 4);
		ret->_bbox[i].bottom = (int16)READ_LE_UINT16(dat + 6);
		dat += 8;
	}

	// Load the animation pattern
	patlength = dat;
	dat += ret->_numpattern;

	for (int i = 1; i < ret->_numpattern; i++) {
		for (int j = 0; j < patlength[i]; j++)
			ret->_pattern[i][j] = dat[j];
		ret->_pattern[i][(int)patlength[i]] = 255; // Terminate pattern
		dat += patlength[i];
	}

	// Upload the individual frames of animations
	for (int i = 1; i < ret->_numframe; i++) {
		uint32 dim = (uint32)(ret->_frameslocations[i].right - ret->_frameslocations[i].left) *
			(uint32)(ret->_frameslocations[i].bottom - ret->_frameslocations[i].top);
		ret->_frames[i] = (char *)globalAlloc(GMEM_FIXED, dim);

		if (ret->_frames[i] == NULL)
			return NULL;
		memcpy(ret->_frames[i], dat, dim);
		dat += dim;
	}

	int i = READ_LE_UINT16(dat);

	globalUnlock(hDat);
	globalFree(hDat);

	// Check if we've got to the end of the file
	if (i != 0xABCD)
		return NULL;

	return ret;
}

/**
 * Thread that calls a custom function. It is used in scripts, so that each script
 * function is executed without delaying the others.
 *
 * @param param				pointer to a pointer to the structure that defines the call.
 * @remarks		The passed structure is freed when the process finishes.
 */
void CustomThread(CORO_PARAM, const void *param) {
	CORO_BEGIN_CONTEXT;
		LpCfCall p;
	CORO_END_CONTEXT(_ctx);

	CORO_BEGIN_CODE(_ctx);

	_ctx->p = *(const LpCfCall *)param;

	CORO_INVOKE_4(GLOBALS._lplpFunctions[_ctx->p->_nCf], _ctx->p->_arg1, _ctx->p->_arg2, _ctx->p->_arg3, _ctx->p->_arg4);

	globalFree(_ctx->p);

	CORO_END_CODE;
}

/**
 * Main process for running a script.
 *
 * @param param				Pointer to a pointer to a structure containing the script data.
 * @remarks		The passed structure is freed when the process finishes.
 */
void ScriptThread(CORO_PARAM, const void *param) {
	CORO_BEGIN_CONTEXT;
		uint i, j, k;
		uint32 dwStartTime;
		uint32 dwCurTime;
		uint32 dwId;
		int numHandles;
		LpCfCall p;
	CORO_END_CONTEXT(_ctx);

	static uint32 cfHandles[MAX_COMMANDS_PER_MOMENT];
	LpMpalScript s = *(const LpMpalScript *)param;

	CORO_BEGIN_CODE(_ctx);

	_ctx->dwStartTime = g_vm->getTime();
	_ctx->numHandles = 0;

	//debugC(DEBUG_BASIC, kTonyDebugMPAL, "PlayScript(): Moments: %u\n", s->_nMoments);
	for (_ctx->i = 0; _ctx->i < s->_nMoments; _ctx->i++) {
		// Sleep for the required time
		if (s->_moment[_ctx->i]._dwTime == -1) {
			CORO_INVOKE_4(CoroScheduler.waitForMultipleObjects, _ctx->numHandles, cfHandles, true, CORO_INFINITE);
			_ctx->dwStartTime = g_vm->getTime();
		} else {
			_ctx->dwCurTime = g_vm->getTime();
			if (_ctx->dwCurTime < _ctx->dwStartTime + (s->_moment[_ctx->i]._dwTime * 100)) {
				//debugC(DEBUG_BASIC, kTonyDebugMPAL, "PlayScript(): Sleeping %lums\n",_ctx->dwStartTime + (s->_moment[_ctx->i]._dwTime*100) - _ctx->dwCurTime);
				CORO_INVOKE_1(CoroScheduler.sleep, _ctx->dwStartTime + (s->_moment[_ctx->i]._dwTime * 100) - _ctx->dwCurTime);
			}
		}

		_ctx->numHandles = 0;
		for (_ctx->j = 0; _ctx->j < s->_moment[_ctx->i]._nCmds; _ctx->j++) {
			_ctx->k = s->_moment[_ctx->i]._cmdNum[_ctx->j];

			if (s->_command[_ctx->k]._type == 1) {
				_ctx->p = (LpCfCall)globalAlloc(GMEM_FIXED, sizeof(CfCall));
				if (_ctx->p == NULL) {
					GLOBALS._mpalError = 1;

					CORO_KILL_SELF();
					return;
				}

				_ctx->p->_nCf  = s->_command[_ctx->k]._nCf;
				_ctx->p->_arg1 = s->_command[_ctx->k]._arg1;
				_ctx->p->_arg2 = s->_command[_ctx->k]._arg2;
				_ctx->p->_arg3 = s->_command[_ctx->k]._arg3;
				_ctx->p->_arg4 = s->_command[_ctx->k]._arg4;

				// !!! New process management
				if ((cfHandles[_ctx->numHandles++] = CoroScheduler.createProcess(CustomThread, &_ctx->p, sizeof(LpCfCall))) == 0) {
					GLOBALS._mpalError = 1;

					CORO_KILL_SELF();
					return;
				}
			} else if (s->_command[_ctx->k]._type == 2) {
				lockVar();
				varSetValue(
					s->_command[_ctx->k]._lpszVarName,
					evaluateExpression(s->_command[_ctx->k]._expr)
				);
				unlockVar();

			} else {
				GLOBALS._mpalError = 1;
				globalFree(s);

				CORO_KILL_SELF();
				return;
			}

			// WORKAROUND: Wait for events to pulse.
			CORO_SLEEP(1);
		}
	}

	globalFree(s);

	CORO_KILL_SELF();

	CORO_END_CODE;
}

/**
 * Thread that performs an action on an item. the thread always executes the action,
 * so it should create a new item in which the action is the one required.
 * Furthermore, the expression is not checked, but it is always performed the action.
 *
 * @param param				Pointer to a pointer to a structure containing the action.
 */
void ActionThread(CORO_PARAM, const void *param) {
	// COROUTINE
	CORO_BEGIN_CONTEXT;
		int j, k;
		LpMpalItem item;

		~CoroContextTag() {
			if (item)
				globalDestroy(item);
		}
	CORO_END_CONTEXT(_ctx);

	CORO_BEGIN_CODE(_ctx);

	// The ActionThread owns the data block pointed to, so we need to make sure it's
	// freed when the process exits
	_ctx->item = *(const LpMpalItem *)param;

	GLOBALS._mpalError = 0;
	for (_ctx->j = 0; _ctx->j < _ctx->item->_action[_ctx->item->_dwRes]._nCmds; _ctx->j++) {
		_ctx->k = _ctx->item->_action[_ctx->item->_dwRes]._cmdNum[_ctx->j];

		if (_ctx->item->_command[_ctx->k]._type == 1) {
			// Custom function
			debugC(DEBUG_DETAILED, kTonyDebugActions, "Action Process %d Call=%s params=%d,%d,%d,%d",
				CoroScheduler.getCurrentPID(), GLOBALS._lplpFunctionStrings[_ctx->item->_command[_ctx->k]._nCf].c_str(),
				_ctx->item->_command[_ctx->k]._arg1, _ctx->item->_command[_ctx->k]._arg2,
				_ctx->item->_command[_ctx->k]._arg3, _ctx->item->_command[_ctx->k]._arg4
			);

			CORO_INVOKE_4(GLOBALS._lplpFunctions[_ctx->item->_command[_ctx->k]._nCf],
				_ctx->item->_command[_ctx->k]._arg1,
				_ctx->item->_command[_ctx->k]._arg2,
				_ctx->item->_command[_ctx->k]._arg3,
				_ctx->item->_command[_ctx->k]._arg4

			);
		} else if (_ctx->item->_command[_ctx->k]._type == 2) {
			// Variable assign
			debugC(DEBUG_DETAILED, kTonyDebugActions, "Action Process %d Variable=%s",
				CoroScheduler.getCurrentPID(), _ctx->item->_command[_ctx->k]._lpszVarName);

			lockVar();
			varSetValue(_ctx->item->_command[_ctx->k]._lpszVarName, evaluateExpression(_ctx->item->_command[_ctx->k]._expr));
			unlockVar();

		} else {
			GLOBALS._mpalError = 1;
			break;
		}

		// WORKAROUND: Wait for events to pulse.
		CORO_SLEEP(1);
	}

	globalDestroy(_ctx->item);
	_ctx->item = NULL;

	debugC(DEBUG_DETAILED, kTonyDebugActions, "Action Process %d ended", CoroScheduler.getCurrentPID());

	CORO_END_CODE;
}

/**
 * This thread monitors a created action to detect when it ends.
 * @remarks		Since actions can spawn sub-actions, this needs to be a
 * separate thread to determine when the outer action is done
 */
void ShutUpActionThread(CORO_PARAM, const void *param) {
	// COROUTINE
	CORO_BEGIN_CONTEXT;
		int slotNumber;
	CORO_END_CONTEXT(_ctx);

	uint32 pid = *(const uint32 *)param;

	CORO_BEGIN_CODE(_ctx);

	CORO_INVOKE_2(CoroScheduler.waitForSingleObject, pid, CORO_INFINITE);

	GLOBALS._bExecutingAction = false;

	if (g_vm->_initialLoadSlotNumber != -1) {
		_ctx->slotNumber = g_vm->_initialLoadSlotNumber;
		g_vm->_initialLoadSlotNumber = -1;

		CORO_INVOKE_1(g_vm->loadState, _ctx->slotNumber);
	}

	CORO_END_CODE;
}

/**
 * Polls one location (starting point of a process)
 *
 * @param param				Pointer to an index in the array of polling locations.
 */
void LocationPollThread(CORO_PARAM, const void *param) {
	typedef struct {
		uint32 _nItem, _nAction;

		uint16 _wTime;
		byte _perc;
		MpalHandle _when;
		byte _nCmds;
		uint16 _cmdNum[MAX_COMMANDS_PER_ACTION];
		uint32 _dwLastTime;
	} MYACTION;

	typedef struct {
		uint32 _nItem;
		uint32 _hThread;
	} MYTHREAD;

	CORO_BEGIN_CONTEXT;
		uint32 *il;
		int i, j, k;
		int numitems;
		int nRealItems;
		LpMpalItem curItem, newItem;
		int nIdleActions;
		uint32 curTime;
		uint32 dwSleepTime;
		uint32 dwId;
		int ord;
		bool delayExpired;
		bool expired;

		MYACTION *myActions;
		MYTHREAD *myThreads;

		~CoroContextTag() {
			// Free data blocks
			if (myThreads)
				globalDestroy(myThreads);
			if (myActions)
				globalDestroy(myActions);
		}
	CORO_END_CONTEXT(_ctx);

	uint32 id = *((const uint32 *)param);

	CORO_BEGIN_CODE(_ctx);

	// Initialize data pointers
	_ctx->myActions = NULL;
	_ctx->myThreads = NULL;

	// To begin with, we need to request the item list from the location
	_ctx->il = mpalQueryItemList(GLOBALS._nPollingLocations[id]);

	// Count the items
	for (_ctx->numitems = 0; _ctx->il[_ctx->numitems] != 0; _ctx->numitems++)
		;

	// We look for items without idle actions, and eliminate them from the list
	lockItems();
	_ctx->nIdleActions = 0;
	_ctx->nRealItems = 0;
	for (_ctx->i = 0; _ctx->i < _ctx->numitems; _ctx->i++) {
		_ctx->ord = itemGetOrderFromNum(_ctx->il[_ctx->i]);

		if (_ctx->ord == -1)
			continue;

		_ctx->curItem = GLOBALS._lpmiItems + _ctx->ord;

		_ctx->k = 0;
		for (_ctx->j = 0; _ctx->j < _ctx->curItem->_nActions; _ctx->j++) {
			if (_ctx->curItem->_action[_ctx->j]._num == 0xFF)
				_ctx->k++;
		}

		_ctx->nIdleActions += _ctx->k;

		if (_ctx->k == 0)
			// We can remove this item from the list
			_ctx->il[_ctx->i] = (uint32)NULL;
		else
			_ctx->nRealItems++;
	}
	unlockItems();

	// If there is nothing left, we can exit
	if (_ctx->nRealItems == 0) {
		globalDestroy(_ctx->il);
		CORO_KILL_SELF();
		return;
	}

	_ctx->myThreads = (MYTHREAD *)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, _ctx->nRealItems * sizeof(MYTHREAD));
	if (_ctx->myThreads == NULL) {
		globalDestroy(_ctx->il);
		CORO_KILL_SELF();
		return;
	}

	// We have established that there is at least one item that contains idle actions.
	// Now we created the mirrored copies of the idle actions.
	_ctx->myActions = (MYACTION *)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, _ctx->nIdleActions * sizeof(MYACTION));
	if (_ctx->myActions == NULL) {
		globalDestroy(_ctx->myThreads);
		globalDestroy(_ctx->il);
		CORO_KILL_SELF();
		return;
	}

	lockItems();
	_ctx->k = 0;

	for (_ctx->i = 0; _ctx->i < _ctx->numitems; _ctx->i++) {
		if (_ctx->il[_ctx->i] == 0)
			continue;

		_ctx->curItem = GLOBALS._lpmiItems + itemGetOrderFromNum(_ctx->il[_ctx->i]);

		for (_ctx->j = 0; _ctx->j < _ctx->curItem->_nActions; _ctx->j++) {
			if (_ctx->curItem->_action[_ctx->j]._num == 0xFF) {
				_ctx->myActions[_ctx->k]._nItem = _ctx->il[_ctx->i];
				_ctx->myActions[_ctx->k]._nAction = _ctx->j;

				_ctx->myActions[_ctx->k]._wTime = _ctx->curItem->_action[_ctx->j]._wTime;
				_ctx->myActions[_ctx->k]._perc = _ctx->curItem->_action[_ctx->j]._perc;
				_ctx->myActions[_ctx->k]._when = _ctx->curItem->_action[_ctx->j]._when;
				_ctx->myActions[_ctx->k]._nCmds = _ctx->curItem->_action[_ctx->j]._nCmds;
				memcpy(_ctx->myActions[_ctx->k]._cmdNum, _ctx->curItem->_action[_ctx->j]._cmdNum,
				MAX_COMMANDS_PER_ACTION * sizeof(uint16));

				_ctx->myActions[_ctx->k]._dwLastTime = g_vm->getTime();
				_ctx->k++;
			}
		}
	}

	unlockItems();

	// We don't need the item list anymore
	globalDestroy(_ctx->il);

	// Here's the main loop
	while (1) {
		// Searching for idle actions requiring time to execute
		_ctx->curTime = g_vm->getTime();
		_ctx->dwSleepTime = (uint32)-1L;

		for (_ctx->k = 0;_ctx->k<_ctx->nIdleActions;_ctx->k++) {
			if (_ctx->curTime >= _ctx->myActions[_ctx->k]._dwLastTime + _ctx->myActions[_ctx->k]._wTime) {
				_ctx->dwSleepTime = 0;
				break;
			} else
				_ctx->dwSleepTime = MIN(_ctx->dwSleepTime, _ctx->myActions[_ctx->k]._dwLastTime + _ctx->myActions[_ctx->k]._wTime - _ctx->curTime);
		}

		// We fall alseep, but always checking that the event is set when prompted for closure
		CORO_INVOKE_3(CoroScheduler.waitForSingleObject, GLOBALS._hEndPollingLocations[id], _ctx->dwSleepTime, &_ctx->expired);

		//if (_ctx->k == WAIT_OBJECT_0)
		if (!_ctx->expired)
			break;

		for (_ctx->i = 0; _ctx->i < _ctx->nRealItems; _ctx->i++) {
			if (_ctx->myThreads[_ctx->i]._nItem != 0) {
				CORO_INVOKE_3(CoroScheduler.waitForSingleObject, _ctx->myThreads[_ctx->i]._hThread, 0, &_ctx->delayExpired);

				// if result == WAIT_OBJECT_0)
				if (!_ctx->delayExpired)
					_ctx->myThreads[_ctx->i]._nItem = 0;
			}
		}

		_ctx->curTime = g_vm->getTime();

		// Loop through all the necessary idle actions
		for (_ctx->k = 0; _ctx->k < _ctx->nIdleActions; _ctx->k++) {
			if (_ctx->curTime >= _ctx->myActions[_ctx->k]._dwLastTime + _ctx->myActions[_ctx->k]._wTime) {
				_ctx->myActions[_ctx->k]._dwLastTime += _ctx->myActions[_ctx->k]._wTime;

				// It's time to check to see if fortune is on the side of the idle action
				byte randomVal = (byte)g_vm->_randomSource.getRandomNumber(99);
				if (randomVal < _ctx->myActions[_ctx->k]._perc) {
					// Check if there is an action running on the item
					if ((GLOBALS._bExecutingAction) && (GLOBALS._nExecutingAction == _ctx->myActions[_ctx->k]._nItem))
						continue;

					// Check to see if there already another idle funning running on the item
					for (_ctx->i = 0; _ctx->i < _ctx->nRealItems; _ctx->i++) {
						if (_ctx->myThreads[_ctx->i]._nItem == _ctx->myActions[_ctx->k]._nItem)
							break;
					}

					if (_ctx->i < _ctx->nRealItems)
						continue;

					// Ok, we are the only ones :)
					lockItems();
					_ctx->curItem = GLOBALS._lpmiItems + itemGetOrderFromNum(_ctx->myActions[_ctx->k]._nItem);

					// Check if there is a WhenExecute expression
					_ctx->j=_ctx->myActions[_ctx->k]._nAction;
					if (_ctx->curItem->_action[_ctx->j]._when != NULL) {
						if (!evaluateExpression(_ctx->curItem->_action[_ctx->j]._when)) {
							unlockItems();
							continue;
						}
					}

					// Ok, we can perform the action. For convenience, we do it in a new process
					_ctx->newItem = (LpMpalItem)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizeof(MpalItem));
					if (_ctx->newItem == false) {
						globalDestroy(_ctx->myThreads);
						globalDestroy(_ctx->myActions);

						CORO_KILL_SELF();
						return;
					}

					memcpy(_ctx->newItem,_ctx->curItem, sizeof(MpalItem));
					unlockItems();

					// We copy the action in #0
					//_ctx->newItem->Action[0].nCmds = _ctx->curItem->Action[_ctx->j].nCmds;
					//memcpy(_ctx->newItem->Action[0].CmdNum,_ctx->curItem->Action[_ctx->j].CmdNum,_ctx->newItem->Action[0].nCmds*sizeof(_ctx->newItem->Action[0].CmdNum[0]));
					_ctx->newItem->_dwRes = _ctx->j;

					// We will create an action, and will provide the necessary details
					for (_ctx->i = 0; _ctx->i < _ctx->nRealItems; _ctx->i++) {
						if (_ctx->myThreads[_ctx->i]._nItem == 0)
							break;
					}

					_ctx->myThreads[_ctx->i]._nItem = _ctx->myActions[_ctx->k]._nItem;

					// Create the process
					if ((_ctx->myThreads[_ctx->i]._hThread = CoroScheduler.createProcess(ActionThread, &_ctx->newItem, sizeof(LpMpalItem))) == CORO_INVALID_PID_VALUE) {
					//if ((_ctx->myThreads[_ctx->i]._hThread = (void*)_beginthread(ActionThread, 10240, (void *)_ctx->newItem)) == (void*)-1)
						globalDestroy(_ctx->newItem);
						globalDestroy(_ctx->myThreads);
						globalDestroy(_ctx->myActions);

						CORO_KILL_SELF();
						return;
					}

					// Skip all idle actions of the same item
				}
			}
		}
	}

	// Set idle skip on
	CORO_INVOKE_4(GLOBALS._lplpFunctions[200], 0, 0, 0, 0);

	for (_ctx->i = 0; _ctx->i < _ctx->nRealItems; _ctx->i++) {
		if (_ctx->myThreads[_ctx->i]._nItem != 0) {
			CORO_INVOKE_3(CoroScheduler.waitForSingleObject, _ctx->myThreads[_ctx->i]._hThread, 5000, &_ctx->delayExpired);

			//if (result != WAIT_OBJECT_0)
			//if (_ctx->delayExpired)
			//	TerminateThread(_ctx->MyThreads[_ctx->i].hThread, 0);

			CoroScheduler.killMatchingProcess(_ctx->myThreads[_ctx->i]._hThread);
		}
	}

	// Set idle skip off
	CORO_INVOKE_4(GLOBALS._lplpFunctions[201], 0, 0, 0, 0);

	CORO_END_CODE;
}

/**
 * Wait for the end of the dialog execution thread, and then restore global
 * variables indicating that the dialogue has finished.
 *
 * @param param				Pointer to a handle to the dialog
 * @remarks		This additional process is used, instead of clearing variables
 * within the same dialog thread, because due to the recursive nature of a dialog,
 * it would be difficult to know within it when the dialog is actually ending.
 */
void ShutUpDialogThread(CORO_PARAM, const void *param) {
	CORO_BEGIN_CONTEXT;
	CORO_END_CONTEXT(_ctx);

	uint32 pid = *(const uint32 *)param;

	CORO_BEGIN_CODE(_ctx);

	CORO_INVOKE_2(CoroScheduler.waitForSingleObject, pid, CORO_INFINITE);

	GLOBALS._bExecutingDialog = false;
	GLOBALS._nExecutingDialog = 0;
	GLOBALS._nExecutingChoice = 0;

	CoroScheduler.setEvent(GLOBALS._hAskChoice);

	CORO_KILL_SELF();

	CORO_END_CODE;
}

void doChoice(CORO_PARAM, uint32 nChoice);

/**
 * Executes a group of the current dialog. Can 'be the Starting point of a process.
 * @parm nGroup				Number of the group to perform
 */
void GroupThread(CORO_PARAM, const void *param) {
	CORO_BEGIN_CONTEXT;
		LpMpalDialog dialog;
		int i, j, k;
		int type;
	CORO_END_CONTEXT(_ctx);

	uint32 nGroup = *(const uint32 *)param;

	CORO_BEGIN_CODE(_ctx);

	// Lock the _ctx->dialog
	lockDialogs();

	// Find the pointer to the current _ctx->dialog
	_ctx->dialog = GLOBALS._lpmdDialogs + GLOBALS._nExecutingDialog;

	// Search inside the group requesting the _ctx->dialog
	for (_ctx->i = 0; _ctx->dialog->_group[_ctx->i]._num != 0; _ctx->i++) {
		if (_ctx->dialog->_group[_ctx->i]._num == nGroup) {
			// Cycle through executing the commands of the group
			for (_ctx->j = 0; _ctx->j < _ctx->dialog->_group[_ctx->i]._nCmds; _ctx->j++) {
				_ctx->k = _ctx->dialog->_group[_ctx->i]._cmdNum[_ctx->j];

				_ctx->type = _ctx->dialog->_command[_ctx->k]._type;
				if (_ctx->type == 1) {
					// Call custom function
					CORO_INVOKE_4(GLOBALS._lplpFunctions[_ctx->dialog->_command[_ctx->k]._nCf],
						_ctx->dialog->_command[_ctx->k]._arg1,
						_ctx->dialog->_command[_ctx->k]._arg2,
						_ctx->dialog->_command[_ctx->k]._arg3,
						_ctx->dialog->_command[_ctx->k]._arg4
					);

				} else if (_ctx->type == 2) {
					// Set a variable
					lockVar();
					varSetValue(_ctx->dialog->_command[_ctx->k]._lpszVarName, evaluateExpression(_ctx->dialog->_command[_ctx->k]._expr));
					unlockVar();

				} else if (_ctx->type == 3) {
					// DoChoice: call the chosen function
					CORO_INVOKE_1(doChoice, (uint32)_ctx->dialog->_command[_ctx->k]._nChoice);

				} else {
					GLOBALS._mpalError = 1;
					unlockDialogs();

					CORO_KILL_SELF();
					return;
				}

				// WORKAROUND: Wait for events to pulse.
				CORO_SLEEP(1);
			}

			// The gruop is finished, so we can return to the calling function.
			// If the group was the first called, then the process will automatically
			// end. Otherwise it returns to the caller method

			return;
		}
	}

	// If we are here, it means that we have not found the requested group
	GLOBALS._mpalError = 1;
	unlockDialogs();

	CORO_KILL_SELF();

	CORO_END_CODE;
}

/**
 * Make a choice in the current dialog.
 *
 * @param nChoice			Number of choice to perform
 */
void doChoice(CORO_PARAM, uint32 nChoice) {
	CORO_BEGIN_CONTEXT;
		LpMpalDialog dialog;
		int i, j, k;
		uint32 nGroup;
	CORO_END_CONTEXT(_ctx);

	CORO_BEGIN_CODE(_ctx);

	// Lock the dialogs
	lockDialogs();

	// Get a pointer to the current dialog
	_ctx->dialog = GLOBALS._lpmdDialogs + GLOBALS._nExecutingDialog;

	// Search the choice between those required in the dialog
	for (_ctx->i = 0; _ctx->dialog->_choice[_ctx->i]._nChoice != 0; _ctx->i++) {
		if (_ctx->dialog->_choice[_ctx->i]._nChoice == nChoice)
			break;
	}

	// If nothing has been found, exit with an error
	if (_ctx->dialog->_choice[_ctx->i]._nChoice == 0) {
		// If we're here, we did not find the required choice
		GLOBALS._mpalError = 1;
		unlockDialogs();

		CORO_KILL_SELF();
		return;
	}

	// We've found the requested choice. Remember what in global variables
	GLOBALS._nExecutingChoice = _ctx->i;

	while (1) {
		GLOBALS._nExecutingChoice = _ctx->i;

		_ctx->k = 0;
		// Calculate the expression of each selection, to see if they're active or inactive
		for (_ctx->j = 0; _ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._dwData != 0; _ctx->j++) {
			if (_ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._when == NULL) {
				_ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._curActive = 1;
				_ctx->k++;
			} else if (evaluateExpression(_ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._when)) {
				_ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._curActive = 1;
				_ctx->k++;
			} else
				_ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._curActive = 0;
		}

		// If there are no choices activated, then the dialog is finished.
		if (_ctx->k == 0) {
			unlockDialogs();
			break;
		}

		// There are choices available to the user, so wait for them to make one
		CoroScheduler.resetEvent(GLOBALS._hDoneChoice);
		CoroScheduler.setEvent(GLOBALS._hAskChoice);
		CORO_INVOKE_2(CoroScheduler.waitForSingleObject, GLOBALS._hDoneChoice, CORO_INFINITE);

		// Now that the choice has been made, we can run the groups associated with the choice tbontbtitq
		_ctx->j = GLOBALS._nSelectedChoice;
		for (_ctx->k = 0; _ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._wPlayGroup[_ctx->k] != 0; _ctx->k++) {
			_ctx->nGroup = _ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._wPlayGroup[_ctx->k];
			CORO_INVOKE_1(GroupThread, &_ctx->nGroup);
		}

		// Control attribute
		if (_ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._attr & (1 << 0)) {
			// Bit 0 set: the end of the choice
			unlockDialogs();
			break;
		}

		if (_ctx->dialog->_choice[_ctx->i]._select[_ctx->j]._attr & (1 << 1)) {
			// Bit 1 set: the end of the dialog
			unlockDialogs();

			CORO_KILL_SELF();
			return;
		}

		// End of choic ewithout attributes. We must do it again
	}

	// If we're here, we found an end choice. Return to the caller group
	return;

	CORO_END_CODE;
}

/**
 * Perform an action on a certain item.
 *
 * @param nAction			Action number
 * @param ordItem           Index of the item in the items list
 * @param dwParam			Any parameter for the action.
 * @returns		Id of the process that was launched to perform the action, or
 * CORO_INVALID_PID_VALUE if the action was not defined, or the item was inactive.
 * @remarks		You can get the index of an item from its number by using
 * the itemGetOrderFromNum() function. The items list must first be locked
 * by calling LockItem().
 */
static uint32 doAction(uint32 nAction, uint32 ordItem, uint32 dwParam) {
	LpMpalItem item = GLOBALS._lpmiItems;
	LpMpalItem newitem;

	item+=ordItem;
	Common::String buf = Common::String::format("Status.%u", item->_nObj);
	if (varGetValue(buf.c_str()) <= 0)
		return CORO_INVALID_PID_VALUE;

	for (int i = 0; i < item->_nActions; i++) {
		if (item->_action[i]._num != nAction)
			continue;

		if (item->_action[i]._wParm != dwParam)
			continue;

		if (item->_action[i]._when != NULL) {
			if (!evaluateExpression(item->_action[i]._when))
				continue;
		}

		// Now we find the right action to be performed
		// Duplicate the item and copy the current action in #i into #0
		newitem = (LpMpalItem)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizeof(MpalItem));
		if (newitem == NULL)
			return CORO_INVALID_PID_VALUE;

		// In the new version number of the action in writing dwRes
		Common::copy((byte *)item, (byte *)item + sizeof(MpalItem), (byte *)newitem);

		//newitem->_action[0]._nCmds=item->_action[i]._nCmds;
		//memcpy(newitem->_action[0]._cmdNum, item->_action[i]._cmdNum, newitem->Action[0].nCmds * sizeof(newitem->_action[0]._cmdNum[0]));

		newitem->_dwRes = i;

		// And finally we can laucnh the process that will execute the action,
		// and a second process to free up the memory when the action is finished.

		// !!! New process management
		uint32 h;
		if ((h = CoroScheduler.createProcess(ActionThread, &newitem, sizeof(LpMpalItem))) == CORO_INVALID_PID_VALUE)
			return CORO_INVALID_PID_VALUE;

		if (CoroScheduler.createProcess(ShutUpActionThread, &h, sizeof(uint32)) == CORO_INVALID_PID_VALUE)
			return CORO_INVALID_PID_VALUE;

		GLOBALS._nExecutingAction = item->_nObj;
		GLOBALS._bExecutingAction = true;

		return h;
	}

	return CORO_INVALID_PID_VALUE;
}

/**
 * Shows a dialog in a separate process.
 *
 * @param nDlgOrd				The index of the dialog in the dialog list
 * @param nGroup				Number of the group to perform
 * @returns						The process Id of the process running the dialog
 *								or CORO_INVALID_PID_VALUE on error
 * @remarks						The dialogue runs in a thread created on purpose,
 * so that must inform through an event and when 'necessary to you make a choice.
 * The data on the choices may be obtained through various queries.
 */
static uint32 doDialog(uint32 nDlgOrd, uint32 nGroup) {
	// Store the running dialog in a global variable
	GLOBALS._nExecutingDialog = nDlgOrd;

	// Enables the flag to indicate that there is' a running dialogue
	GLOBALS._bExecutingDialog = true;

	CoroScheduler.resetEvent(GLOBALS._hAskChoice);
	CoroScheduler.resetEvent(GLOBALS._hDoneChoice);

	// Create a thread that performs the dialogue group

	// Create the process
	uint32 h;
	if ((h = CoroScheduler.createProcess(GroupThread, &nGroup, sizeof(uint32))) == CORO_INVALID_PID_VALUE)
		return CORO_INVALID_PID_VALUE;

	// Create a thread that waits until the end of the dialog process, and will restore the global variables
	if (CoroScheduler.createProcess(ShutUpDialogThread, &h, sizeof(uint32)) == CORO_INVALID_PID_VALUE) {
		// Something went wrong, so kill the previously started dialog process
		CoroScheduler.killMatchingProcess(h);
		return CORO_INVALID_PID_VALUE;
	}

	return h;
}

/**
 * Takes note of the selection chosen by the user, and warns the process that was running
 * the box that it can continue.
 *
 * @param nChoice           Number of choice that was in progress
 * @param dwData			Since combined with select selection
 * @returns		True if everything is OK, false on failure
 */
bool doSelection(uint32 i, uint32 dwData) {
	LpMpalDialog dialog = GLOBALS._lpmdDialogs + GLOBALS._nExecutingDialog;
	int j;

	for (j = 0; dialog->_choice[i]._select[j]._dwData != 0; j++) {
		if (dialog->_choice[i]._select[j]._dwData == dwData && dialog->_choice[i]._select[j]._curActive != 0)
			break;
	}

	if (dialog->_choice[i]._select[j]._dwData == 0)
		return false;

	GLOBALS._nSelectedChoice = j;
	CoroScheduler.setEvent(GLOBALS._hDoneChoice);
	return true;
}

/**
 * @defgroup Exported functions
 */
//@{

/**
 * Initializes the MPAL library and opens the .MPC file, which will be used for all queries.
 *
 * @param lpszMpcFileName   Name of the MPC file
 * @param lpszMprFileName   Name of the MPR file
 * @param lplpcfArray		Array of pointers to custom functions.
 * @returns		True if everything is OK, false on failure
 */
bool mpalInit(const char *lpszMpcFileName, const char *lpszMprFileName,
			  LPLPCUSTOMFUNCTION lplpcfArray, Common::String *lpcfStrings) {
	byte buf[5];
	byte *cmpbuf;

	// Save the array of custom functions
	GLOBALS._lplpFunctions = lplpcfArray;
	GLOBALS._lplpFunctionStrings = lpcfStrings;

	// OPen the MPC file for reading
	Common::File hMpc;
	if (!hMpc.open(lpszMpcFileName))
		return false;

	// Read and check the header
	uint32 nBytesRead = hMpc.read(buf, 5);
	if (nBytesRead != 5)
		return false;

	if (buf[0] != 'M' || buf[1] != 'P' || buf[2] != 'C' || buf[3] != 0x20)
		return false;

	bool bCompress = buf[4];

	// Reads the size of the uncompressed file, and allocate memory
	uint32 dwSizeDecomp = hMpc.readUint32LE();
	if (hMpc.err())
		return false;

	byte *lpMpcImage = (byte *)globalAlloc(GMEM_FIXED, dwSizeDecomp + 16);
	if (lpMpcImage == NULL)
		return false;

	if (bCompress) {
		// Get the compressed size and read the data in
		uint32 dwSizeComp = hMpc.readUint32LE();
		if (hMpc.err())
			return false;

		cmpbuf = (byte *)globalAlloc(GMEM_FIXED, dwSizeComp);
		if (cmpbuf == NULL)
			return false;

		nBytesRead = hMpc.read(cmpbuf, dwSizeComp);
		if (nBytesRead != dwSizeComp)
			return false;

		// Decompress the data
		lzo1x_decompress(cmpbuf, dwSizeComp, lpMpcImage, &nBytesRead);
		if (nBytesRead != dwSizeDecomp)
			return false;

		globalDestroy(cmpbuf);
	} else {
		// If the file is not compressed, we directly read in the data
		nBytesRead = hMpc.read(lpMpcImage, dwSizeDecomp);
		if (nBytesRead != dwSizeDecomp)
			return false;
	}

	// Close the file
	hMpc.close();

	// Process the data
	if (parseMpc(lpMpcImage) == false)
		return false;

	globalDestroy(lpMpcImage);

	// Open the MPR file
	if (!GLOBALS._hMpr.open(lpszMprFileName))
		return false;

	// Seek to the end of the file to read overall information
	GLOBALS._hMpr.seek(-12, SEEK_END);

	uint32 dwSizeComp = GLOBALS._hMpr.readUint32LE();
	if (GLOBALS._hMpr.err())
		return false;

	GLOBALS._nResources = GLOBALS._hMpr.readUint32LE();
	if (GLOBALS._hMpr.err())
		return false;

	nBytesRead = GLOBALS._hMpr.read(buf, 4);
	if (GLOBALS._hMpr.err())
		return false;

	if (buf[0] !='E' || buf[1] != 'N' || buf[2] != 'D' || buf[3] != '0')
		return false;

	// Move to the start of the resources header
	GLOBALS._hMpr.seek(-(12 + (int)dwSizeComp), SEEK_END);

	GLOBALS._lpResources = (uint32 *)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, GLOBALS._nResources * 8);
	if (GLOBALS._lpResources == NULL)
		return false;

	cmpbuf = (byte *)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, dwSizeComp);
	if (cmpbuf == NULL)
		return false;

	nBytesRead = GLOBALS._hMpr.read(cmpbuf, dwSizeComp);
	if (nBytesRead != dwSizeComp)
		return false;

	lzo1x_decompress((const byte *)cmpbuf, dwSizeComp, (byte *)GLOBALS._lpResources, (uint32 *)&nBytesRead);
	if (nBytesRead != (uint32)GLOBALS._nResources * 8)
		return false;
	for (int i = 0; i < 2*GLOBALS._nResources; ++i)
		GLOBALS._lpResources[i] = FROM_LE_32(GLOBALS._lpResources[i]);

	globalDestroy(cmpbuf);

	// Reset back to the start of the file, leaving it open
	GLOBALS._hMpr.seek(0, SEEK_SET);

	// There is no action or dialog running by default
	GLOBALS._bExecutingAction = false;
	GLOBALS._bExecutingDialog = false;

	// There's no polling location
	Common::fill(GLOBALS._nPollingLocations, GLOBALS._nPollingLocations + MAXPOLLINGLOCATIONS, 0);

	// Create the event that will be used to co-ordinate making choices and choices finishing
	GLOBALS._hAskChoice = CoroScheduler.createEvent(true, false);
	GLOBALS._hDoneChoice = CoroScheduler.createEvent(true, false);

	return true;
}

/**
 * Frees resources allocated by the MPAL subsystem
 */
void mpalFree() {
	// Free the resource list
	globalDestroy(GLOBALS._lpResources);
}

/**
 * This is a general function to communicate with the library, to request information
 * about what is in the .MPC file
 *
 * @param wQueryType		Type of query. The list is in the QueryTypes enum.
 * @returns		4 bytes depending on the type of query
 * @remarks		This is the specialised version of the original single mpalQuery
 * method that returns numeric results.
 */
uint32 mpalQueryDWORD(uint16 wQueryType, ...) {
	Common::String buf;
	uint32 dwRet = 0;
	char *n;

	va_list v;
	va_start(v, wQueryType);

	GLOBALS._mpalError = OK;

	if (wQueryType == MPQ_VERSION) {

		/*
		 *  uint32 mpalQuery(MPQ_VERSION);
		 */
		dwRet = HEX_VERSION;

	} else if (wQueryType == MPQ_GLOBAL_VAR) {
		/*
		 *  uint32 mpalQuery(MPQ_GLOBAL_VAR, char * lpszVarName);
		 */
		lockVar();
		dwRet = (uint32)varGetValue(GETARG(char *));
		unlockVar();

	} else if (wQueryType == MPQ_MESSAGE) {
		/*
		 *  char * mpalQuery(MPQ_MESSAGE, uint32 nMsg);
		 */
		error("mpalQuery(MPQ_MESSAGE, uint32 nMsg) used incorrect method variant");


	} else if (wQueryType == MPQ_ITEM_PATTERN) {
		/*
		 *  uint32 mpalQuery(MPQ_ITEM_PATTERN, uint32 nItem);
		 */
		lockVar();
		buf = Common::String::format("Pattern.%u", GETARG(uint32));
		dwRet = (uint32)varGetValue(buf.c_str());
		unlockVar();

	} else if (wQueryType == MPQ_LOCATION_SIZE) {
		/*
		 *  uint32 mpalQuery(MPQ_LOCATION_SIZE, uint32 nLoc, uint32 dwCoord);
		 */
		lockLocations();
		int x = locGetOrderFromNum(GETARG(uint32));
		int y = GETARG(uint32);
		if (x != -1) {
			if (y == MPQ_X)
				dwRet = GLOBALS._lpmlLocations[x]._dwXlen;
			else if (y == MPQ_Y)
				dwRet = GLOBALS._lpmlLocations[x]._dwYlen;
			else
				GLOBALS._mpalError = 1;
		} else
			GLOBALS._mpalError = 1;

		unlockLocations();

	} else if (wQueryType == MPQ_LOCATION_IMAGE) {
		/*
		 *  HGLOBAL mpalQuery(MPQ_LOCATION_IMAGE, uint32 nLoc);
		 */
		error("mpalQuery(MPQ_LOCATION_IMAGE, uint32 nLoc) used incorrect variant");

	} else if (wQueryType == MPQ_RESOURCE) {
		/*
		 *  HGLOBAL mpalQuery(MPQ_RESOURCE, uint32 dwRes);
		 */
		error("mpalQuery(MPQ_RESOURCE, uint32 dwRes) used incorrect variant");

	} else if (wQueryType == MPQ_ITEM_LIST) {
		/*
		 *  uint32 mpalQuery(MPQ_ITEM_LIST, uint32 nLoc);
		 */
		error("mpalQuery(MPQ_ITEM_LIST, uint32 nLoc) used incorrect variant");

	} else if (wQueryType == MPQ_ITEM_DATA) {
		/*
		 *  LpItem mpalQuery(MPQ_ITEM_DATA, uint32 nItem);
		 */
		error("mpalQuery(MPQ_ITEM_DATA, uint32 nItem) used incorrect variant");

	} else if (wQueryType == MPQ_ITEM_IS_ACTIVE) {
		/*
		 *  bool mpalQuery(MPQ_ITEM_IS_ACTIVE, uint32 nItem);
		 */
		lockVar();
		int x = GETARG(uint32);
		buf = Common::String::format("Status.%u", x);
		if (varGetValue(buf.c_str()) <= 0)
			dwRet = (uint32)false;
		else
			dwRet = (uint32)true;

		unlockVar();

	} else if (wQueryType == MPQ_ITEM_NAME) {
		/*
		 *  uint32 mpalQuery(MPQ_ITEM_NAME, uint32 nItem, char * lpszName);
		 */
		lockVar();
		int x = GETARG(uint32);
		n = GETARG(char *);
		buf = Common::String::format("Status.%u", x);
		if (varGetValue(buf.c_str()) <= 0)
			n[0]='\0';
		else {
			lockItems();
			int y = itemGetOrderFromNum(x);
			memcpy(n, (char *)(GLOBALS._lpmiItems + y)->_lpszDescribe, MAX_DESCRIBE_SIZE);
			unlockItems();
		}

		unlockVar();

	} else if (wQueryType == MPQ_DIALOG_PERIOD) {
		/*
		 *  char *mpalQuery(MPQ_DIALOG_PERIOD, uint32 nDialog, uint32 nPeriod);
		 */
		error("mpalQuery(MPQ_DIALOG_PERIOD, uint32 nDialog, uint32 nPeriod) used incorrect variant");

	} else if (wQueryType == MPQ_DIALOG_WAITFORCHOICE) {
		/*
		 *  void mpalQuery(MPQ_DIALOG_WAITFORCHOICE);
		 */
		error("mpalQuery(MPQ_DIALOG_WAITFORCHOICE) used incorrect variant");

	} else if (wQueryType == MPQ_DIALOG_SELECTLIST) {
		/*
		 *  uint32 *mpalQuery(MPQ_DIALOG_SELECTLIST, uint32 nChoice);
		 */
		error("mpalQuery(MPQ_DIALOG_SELECTLIST, uint32 nChoice) used incorrect variant");

	} else if (wQueryType == MPQ_DIALOG_SELECTION) {
		/*
		 *  bool mpalQuery(MPQ_DIALOG_SELECTION, uint32 nChoice, uint32 dwData);
		 */
		lockDialogs();
		int x = GETARG(uint32);
		int y = GETARG(uint32);
		dwRet = (uint32)doSelection(x, y);

		unlockDialogs();

	} else if (wQueryType == MPQ_DO_ACTION) {
		/*
		 *  int mpalQuery(MPQ_DO_ACTION, uint32 nAction, uint32 nItem, uint32 dwParam);
		 */
		lockItems();
		lockVar();
		int x = GETARG(uint32);
		int z = GETARG(uint32);
		int y = itemGetOrderFromNum(z);
		if (y != -1) {
			dwRet = doAction(x, y, GETARG(uint32));
		} else {
			dwRet = CORO_INVALID_PID_VALUE;
			GLOBALS._mpalError = 1;
		}

		unlockVar();
		unlockItems();

	} else if (wQueryType == MPQ_DO_DIALOG) {
		/*
		 *  int mpalQuery(MPQ_DO_DIALOG, uint32 nDialog, uint32 nGroup);
		 */
		if (!GLOBALS._bExecutingDialog) {
			lockDialogs();

			int x = dialogGetOrderFromNum(GETARG(uint32));
			int y = GETARG(uint32);
			dwRet = doDialog(x, y);
			unlockDialogs();
		}
	} else {
		/*
		 *  DEFAULT -> ERROR
		 */
		GLOBALS._mpalError = 1;
	}

	va_end(v);
	return dwRet;
}

/**
 * This is a general function to communicate with the library, to request information
 * about what is in the .MPC file
 *
 * @param wQueryType		Type of query. The list is in the QueryTypes enum.
 * @returns		4 bytes depending on the type of query
 * @remarks		This is the specialised version of the original single mpalQuery
 * method that returns a pointer or handle.
 */
MpalHandle mpalQueryHANDLE(uint16 wQueryType, ...) {
	char *n;
	Common::String buf;
	va_list v;
	va_start(v, wQueryType);
	void *hRet = NULL;

	GLOBALS._mpalError = OK;

	if (wQueryType == MPQ_VERSION) {
		/*
		 *  uint32 mpalQuery(MPQ_VERSION);
		 */
		error("mpalQuery(MPQ_VERSION) used incorrect variant");

	} else if (wQueryType == MPQ_GLOBAL_VAR) {
		/*
		 *  uint32 mpalQuery(MPQ_GLOBAL_VAR, char * lpszVarName);
		 */
		error("mpalQuery(MPQ_GLOBAL_VAR, char * lpszVarName) used incorrect variant");

	} else if (wQueryType == MPQ_MESSAGE) {
		/*
		 *  char * mpalQuery(MPQ_MESSAGE, uint32 nMsg);
		 */
		LockMsg();
		hRet = DuplicateMessage(msgGetOrderFromNum(GETARG(uint32)));
		UnlockMsg();

	} else if (wQueryType == MPQ_ITEM_PATTERN) {
		/*
		 *  uint32 mpalQuery(MPQ_ITEM_PATTERN, uint32 nItem);
		 */
		error("mpalQuery(MPQ_ITEM_PATTERN, uint32 nItem) used incorrect variant");

	} else if (wQueryType == MPQ_LOCATION_SIZE) {
		/*
		 *  uint32 mpalQuery(MPQ_LOCATION_SIZE, uint32 nLoc, uint32 dwCoord);
		 */
		error("mpalQuery(MPQ_LOCATION_SIZE, uint32 nLoc, uint32 dwCoord) used incorrect variant");

	} else if (wQueryType == MPQ_LOCATION_IMAGE) {
		/*
		 *  HGLOBAL mpalQuery(MPQ_LOCATION_IMAGE, uint32 nLoc);
		 */
		lockLocations();
		int x = locGetOrderFromNum(GETARG(uint32));
		hRet = resLoad(GLOBALS._lpmlLocations[x]._dwPicRes);
		unlockLocations();

	} else if (wQueryType == MPQ_RESOURCE) {
		/*
		 *  HGLOBAL mpalQuery(MPQ_RESOURCE, uint32 dwRes);
		 */
		hRet = resLoad(GETARG(uint32));

	} else if (wQueryType == MPQ_ITEM_LIST) {
		/*
		 *  uint32 mpalQuery(MPQ_ITEM_LIST, uint32 nLoc);
		 */
		lockVar();
		hRet = GetItemList(GETARG(uint32));
		lockVar();

	} else if (wQueryType == MPQ_ITEM_DATA) {
		/*
		 *  LpItem mpalQuery(MPQ_ITEM_DATA, uint32 nItem);
		 */
		lockItems();
		hRet = getItemData(itemGetOrderFromNum(GETARG(uint32)));
		unlockItems();

	} else if (wQueryType == MPQ_ITEM_IS_ACTIVE) {
		/*
		 *  bool mpalQuery(MPQ_ITEM_IS_ACTIVE, uint32 nItem);
		 */
		error("mpalQuery(MPQ_ITEM_IS_ACTIVE, uint32 nItem) used incorrect variant");

	} else if (wQueryType == MPQ_ITEM_NAME) {
		/*
		 *  uint32 mpalQuery(MPQ_ITEM_NAME, uint32 nItem, char *lpszName);
		 */
		lockVar();
		int x = GETARG(uint32);
		n = GETARG(char *);
		buf = Common::String::format("Status.%u", x);
		if (varGetValue(buf.c_str()) <= 0)
			n[0] = '\0';
		else {
			lockItems();
			int y = itemGetOrderFromNum(x);
			memcpy(n, (char *)(GLOBALS._lpmiItems + y)->_lpszDescribe, MAX_DESCRIBE_SIZE);
			unlockItems();
		}

		unlockVar();

	} else if (wQueryType == MPQ_DIALOG_PERIOD) {
		/*
		 *  char * mpalQuery(MPQ_DIALOG_PERIOD, uint32 nDialog, uint32 nPeriod);
		 */
		lockDialogs();
		int y = GETARG(uint32);
		hRet = duplicateDialogPeriod(y);
		unlockDialogs();

	} else if (wQueryType == MPQ_DIALOG_WAITFORCHOICE) {
		/*
		 *  void mpalQuery(MPQ_DIALOG_WAITFORCHOICE);
		 */
		error("mpalQuery(MPQ_DIALOG_WAITFORCHOICE) used incorrect variant");

	} else if (wQueryType == MPQ_DIALOG_SELECTLIST) {
		/*
		 *  uint32 *mpalQuery(MPQ_DIALOG_SELECTLIST, uint32 nChoice);
		 */
		lockDialogs();
		hRet = getSelectList(GETARG(uint32));
		unlockDialogs();

	} else if (wQueryType == MPQ_DIALOG_SELECTION) {
		/*
		 *  bool mpalQuery(MPQ_DIALOG_SELECTION, uint32 nChoice, uint32 dwData);
		 */
		error("mpalQuery(MPQ_DIALOG_SELECTION, uint32 nChoice, uint32 dwData) used incorrect variant");

	} else if (wQueryType == MPQ_DO_ACTION) {
		/*
		 *  int mpalQuery(MPQ_DO_ACTION, uint32 nAction, uint32 nItem, uint32 dwParam);
		 */
		error("mpalQuery(MPQ_DO_ACTION, uint32 nAction, uint32 nItem, uint32 dwParam) used incorrect variant");

	} else if (wQueryType == MPQ_DO_DIALOG) {
		/*
		 *  int mpalQuery(MPQ_DO_DIALOG, uint32 nDialog, uint32 nGroup);
		 */
		error("mpalQuery(MPQ_DO_DIALOG, uint32 nDialog, uint32 nGroup) used incorrect variant");
	} else {
		/*
		 *  DEFAULT -> ERROR
		 */
		GLOBALS._mpalError = 1;
	}

	va_end(v);
	return hRet;
}

/**
 * This is a general function to communicate with the library, to request information
 * about what is in the .MPC file
 *
 * @param wQueryType		Type of query. The list is in the QueryTypes enum.
 * @returns		4 bytes depending on the type of query
 * @remarks		This is the specialised version of the original single mpalQuery
 * method that needs to run within a co-routine context.
 */
void mpalQueryCORO(CORO_PARAM, uint16 wQueryType, uint32 *dwRet, ...) {
	CORO_BEGIN_CONTEXT;
		uint32 dwRet;
	CORO_END_CONTEXT(_ctx);

	va_list v;
	va_start(v, dwRet);

	CORO_BEGIN_CODE(_ctx);

	if (wQueryType == MPQ_DIALOG_WAITFORCHOICE) {
		/*
		 *  void mpalQuery(MPQ_DIALOG_WAITFORCHOICE);
		 */
		CORO_INVOKE_2(CoroScheduler.waitForSingleObject, GLOBALS._hAskChoice, CORO_INFINITE);

		// WORKAROUND: Introduce a single frame delay so that if there are multiple actions running,
		// they all have time to be signalled before resetting the event. This fixes a problem where
		// if you try to use the 'shrimp' on the parrot a second time after trying to first use it
		// whilst the parrot was talking, the cursor wouldn't be re-enabled afterwards
		CORO_SLEEP(1);

		CoroScheduler.resetEvent(GLOBALS._hAskChoice);

		if (GLOBALS._bExecutingDialog)
			*dwRet = (uint32)GLOBALS._nExecutingChoice;
		else
			*dwRet = (uint32)((int)-1);
	} else {
		error("mpalQueryCORO called with unsupported query type");
	}

	CORO_END_CODE;

	va_end(v);
}

/**
 * Returns the current MPAL error code
 *
 * @returns		Error code
 */
uint32 mpalGetError() {
	return GLOBALS._mpalError;
}

/**
 * Execute a script. The script runs on multitasking by a thread.
 *
 * @param nScript			Script number to run
 * @returns		TRUE if the script 'was launched, FALSE on failure
 */
bool mpalExecuteScript(int nScript) {
	LockScripts();
	int n = scriptGetOrderFromNum(nScript);
	LpMpalScript s = (LpMpalScript)globalAlloc(GMEM_FIXED | GMEM_ZEROINIT, sizeof(MpalScript));
	if (s == NULL)
		return false;

	memcpy(s, GLOBALS._lpmsScripts + n, sizeof(MpalScript));
	unlockScripts();

	// !!! New process management
	if (CoroScheduler.createProcess(ScriptThread, &s, sizeof(LpMpalScript)) == CORO_INVALID_PID_VALUE)
 		return false;

	return true;
}

/**
 * Install a custom routine That will be called by MPAL every time the pattern
 * of an item has been changed.
 *
 * @param lpiifCustom		Custom function to install
 */
void mpalInstallItemIrq(LPITEMIRQFUNCTION lpiifCus) {
	GLOBALS._lpiifCustom = lpiifCus;
}

/**
 * Process the idle actions of the items on one location.
 *
 * @param nLoc				Number of the location whose items must be processed
 * for idle actions.
 * @returns		TRUE if all OK, and FALSE if it exceeded the maximum limit.
 * @remarks		The maximum number of locations that can be polled
 * simultaneously is defined defined by MAXPOLLINGFUNCIONS
 */
bool mpalStartIdlePoll(int nLoc) {
	for (uint32 i = 0; i < MAXPOLLINGLOCATIONS; i++) {
		if (GLOBALS._nPollingLocations[i] == (uint32)nLoc)
			return false;
	}

	for (uint32 i = 0; i < MAXPOLLINGLOCATIONS; i++) {
		if (GLOBALS._nPollingLocations[i] == 0) {
			GLOBALS._nPollingLocations[i] = nLoc;

			GLOBALS._hEndPollingLocations[i] = CoroScheduler.createEvent(true, false);
// !!! New process management
			if ((GLOBALS._pollingThreads[i] = CoroScheduler.createProcess(LocationPollThread, &i, sizeof(uint32))) == CORO_INVALID_PID_VALUE)
//			 if ((GLOBALS.hEndPollingLocations[i] = (void*)_beginthread(LocationPollThread, 10240, (void *)i))= = (void*)-1)
				return false;

			return true;
		}
	}

	return false;
}

/**
 * Stop processing the idle actions of the items on one location.
 *
 * @param nLo				Number of the location
 * @returns		TRUE if all OK, FALSE if the specified location was not
 * in the process of polling
 */
void mpalEndIdlePoll(CORO_PARAM, int nLoc, bool *result) {
	CORO_BEGIN_CONTEXT;
		int i;
	CORO_END_CONTEXT(_ctx);

	CORO_BEGIN_CODE(_ctx);

	for (_ctx->i = 0; _ctx->i < MAXPOLLINGLOCATIONS; _ctx->i++) {
		if (GLOBALS._nPollingLocations[_ctx->i] == (uint32)nLoc) {
			CoroScheduler.setEvent(GLOBALS._hEndPollingLocations[_ctx->i]);

			CORO_INVOKE_2(CoroScheduler.waitForSingleObject, GLOBALS._pollingThreads[_ctx->i], CORO_INFINITE);

			CoroScheduler.closeEvent(GLOBALS._hEndPollingLocations[_ctx->i]);
			GLOBALS._nPollingLocations[_ctx->i] = 0;

			if (result)
				*result = true;
			return;
		}
	}

	if (result)
		*result = false;

	CORO_END_CODE;
}

/**
 * Retrieve the length of a save state
 *
 * @returns		Length in bytes
 */
int mpalGetSaveStateSize() {
	return GLOBALS._nVars * sizeof(MpalVar) + 4;
}

/**
 * Store the save state into a buffer. The buffer must be
 * length at least the size specified with mpalGetSaveStateSize
 *
 * @param buf				Buffer where to store the state
 */
void mpalSaveState(byte *buf) {
	lockVar();
	WRITE_LE_UINT32(buf, GLOBALS._nVars);
	memcpy(buf + 4, (byte *)GLOBALS._lpmvVars, GLOBALS._nVars * sizeof(MpalVar));
	unlockVar();
}

/**
 * Load a save state from a buffer.
 *
 * @param buf				Buffer where to store the state
 * @returns		Length of the state buffer in bytes
 */
int mpalLoadState(byte *buf) {
	// We must destroy and recreate all the variables
	globalFree(GLOBALS._hVars);

	GLOBALS._nVars = READ_LE_UINT32(buf);

	GLOBALS._hVars = globalAllocate(GMEM_ZEROINIT | GMEM_MOVEABLE, GLOBALS._nVars * sizeof(MpalVar));
	lockVar();
	memcpy((byte *)GLOBALS._lpmvVars, buf + 4, GLOBALS._nVars * sizeof(MpalVar));
	unlockVar();

	return GLOBALS._nVars * sizeof(MpalVar) + 4;
}

} // end of namespace MPAL

} // end of namespace Tony