aboutsummaryrefslogtreecommitdiff
path: root/engines/cine/various.cpp
blob: cd877a32958c4386d3880459d8138443e77a931f (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
/* 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.
 *
 */


#include "common/config-manager.h"
#include "common/endian.h"
#include "common/events.h"
#include "common/textconsole.h"
#include "common/translation.h"

#include "graphics/cursorman.h"

#include "gui/saveload.h"

#include "cine/cine.h"
#include "cine/main_loop.h"
#include "cine/object.h"
#include "cine/sound.h"
#include "cine/bg_list.h"
#include "cine/various.h"

namespace Cine {

int16 disableSystemMenu = 0;
bool inMenu;

int16 commandVar3[4];
int16 commandVar1;
int16 commandVar2;

//Message messageTable[NUM_MAX_MESSAGE];

uint16 var2;
uint16 var3;
uint16 var4;
uint16 var5;

int16 buildObjectListCommand(int16 param);
int16 canUseOnObject = 0;

void waitPlayerInput() {
	uint16 button;

	do {
		manageEvents();
		getMouseData(mouseUpdateStatus, &button, &dummyU16, &dummyU16);
	} while (!button && !g_cine->shouldQuit());
}

void setTextWindow(uint16 param1, uint16 param2, uint16 param3, uint16 param4) {
}

uint16 errorVar;
byte menuVar;

bool fadeRequired;
uint16 allowPlayerInput;
uint16 checkForPendingDataLoadSwitch;
uint16 isDrawCommandEnabled;
uint16 waitForPlayerClick;
uint16 menuCommandLen;
bool _paletteNeedUpdate;
uint16 _messageLen;

int16 playerCommand;

char currentPrcName[20];
char currentRelName[20];
char currentObjectName[20];
char currentMsgName[20];
char newPrcName[20];
char newRelName[20];
char newObjectName[20];
char newMsgName[20];
char currentCtName[15];
char currentPartName[15];
char currentDatName[30];
uint16 musicIsPlaying;

byte isInPause = 0;

/**
 * Bit masks for mouse buttons' states
 * Bit on = mouse button down
 * Bit off = mouse button up
 */
enum MouseButtonState {
	kLeftMouseButton  = (1 << 0),
	kRightMouseButton = (1 << 1)
};

/** Values used by the xMoveKeyb variable */
enum xMoveKeybEnums {
	kKeybMoveCenterX = 0,
	kKeybMoveRight = 1,
	kKeybMoveLeft = 2
};

/** Values used by the yMoveKeyb variable */
enum yMoveKeybEnums {
	kKeybMoveCenterY = 0,
	kKeybMoveDown = 1,
	kKeybMoveUp = 2
};

uint16 xMoveKeyb = kKeybMoveCenterX;
bool egoMovedWithKeyboard = false;
uint16 yMoveKeyb = kKeybMoveCenterY;

SelectedObjStruct currentSelectedObject;

CommandeType currentSaveName[10];

static const int16 choiceResultTable[] = { 1, 1, 1, 2, 1, 1, 1 };
static const int16 subObjectUseTable[] = { 3, 3, 3, 3, 3, 0, 0 };
static const int16 canUseOnItemTable[] = { 1, 0, 0, 1, 1, 0, 0 };

CommandeType objectListCommand[20];
int16 objListTab[20];

/**
 * Move the player character using the keyboard
 * @param x Negative values move left, positive right, zero not at all
 * @param y Negative values move down, positive up, zero not at all
 * NOTE: If both x and y are zero then the character stops
 * FIXME: This seems to only work in Operation Stealth. May need code changes somewhere else...
 */
void moveUsingKeyboard(int x, int y) {
	if (x > 0) {
		xMoveKeyb = kKeybMoveRight;
	} else if (x < 0) {
		xMoveKeyb = kKeybMoveLeft;
	} else {
		xMoveKeyb = kKeybMoveCenterX;
	}

	if (y > 0) {
		yMoveKeyb = kKeybMoveUp;
	} else if (y < 0) {
		yMoveKeyb = kKeybMoveDown;
	} else {
		yMoveKeyb = kKeybMoveCenterY;
	}

	egoMovedWithKeyboard = x || y;
}

void stopMusicAfterFadeOut() {
//	if (g_sfxPlayer->_fadeOutCounter != 0 && g_sfxPlayer->_fadeOutCounter < 100) {
//		g_sfxPlayer->stop();
//	}
}

void runObjectScript(int16 entryIdx) {
	ScriptPtr tmp(scriptInfo->create(*g_cine->_relTable[entryIdx], entryIdx));
	assert(tmp);
	g_cine->_objectScripts.push_back(tmp);
}

/**
 * Add action result message to overlay list
 * @param cmd Message description
 * @todo Why are x, y, width and color left uninitialized?
 */
void addPlayerCommandMessage(int16 cmd) {
	overlay tmp;
	memset(&tmp, 0, sizeof(tmp));
	tmp.objIdx = cmd;
	tmp.type = 3;

	g_cine->_overlayList.push_back(tmp);
}

int16 getRelEntryForObject(uint16 param1, uint16 param2, SelectedObjStruct *pSelectedObject) {
	int16 i;
	int16 found = -1;

	for (i = 0; i < (int16)g_cine->_relTable.size(); i++) {
		if (g_cine->_relTable[i]->_param1 == param1 && g_cine->_relTable[i]->_param2 == pSelectedObject->idx) {
			if (param2 == 1) {
				found = i;
			} else if (param2 == 2) {
				if (g_cine->_relTable[i]->_param3 == pSelectedObject->param) {
					found = i;
				}
			}
		}

		if (found != -1)
			break;
	}

	return found;
}

/**
 * Find index of the object under cursor
 * @param x Mouse cursor coordinate
 * @param y Mouse cursor coordinate
 * @todo Fix displaced type 1 objects
 */
int16 getObjectUnderCursor(uint16 x, uint16 y) {
	Common::List<overlay>::iterator it;

	int16 objX, objY, frame, part, threshold, height, xdif, ydif;
	int width;

	// reverse_iterator would be nice
	for (it = g_cine->_overlayList.reverse_begin(); it != g_cine->_overlayList.end(); --it) {
		if (it->type >= 2 || !g_cine->_objectTable[it->objIdx].name[0]) {
			continue;
		}

		objX = g_cine->_objectTable[it->objIdx].x;
		objY = g_cine->_objectTable[it->objIdx].y;

		frame = ABS((int16)(g_cine->_objectTable[it->objIdx].frame));
		part = g_cine->_objectTable[it->objIdx].part;

		// Additional case for negative frame values in Operation Stealth
		if (g_cine->getGameType() == Cine::GType_OS && g_cine->_objectTable[it->objIdx].frame < 0) {
			if ((it->type == 1) && (x >= objX) && (objX + frame >= x) && (y >= objY) && (objY + part >= y)) {
				return it->objIdx;
			} else {
				continue;
			}
		}

		if (it->type == 0) {
			threshold = g_cine->_animDataTable[frame]._var1;
		} else {
			threshold = g_cine->_animDataTable[frame]._width / 2;
		}

		height = g_cine->_animDataTable[frame]._height;
		width = g_cine->_animDataTable[frame]._realWidth;

		xdif = x - objX;
		ydif = y - objY;

		if ((xdif < 0) || ((threshold << 4) <= xdif) || (ydif <= 0) || (ydif >= height) || !g_cine->_animDataTable[frame].data()) {
			continue;
		}

		if (g_cine->getGameType() == Cine::GType_OS) {
			// This test isn't present in Operation Stealth's PC version's disassembly
			// but removing it makes things crash sometimes (e.g. when selecting a verb
			// and moving the mouse cursor around the floor in the airport's bathroom).
			if (xdif >= width) {
				continue;
			}

			if (it->type == 0 && g_cine->_animDataTable[frame].getColor(xdif, ydif) != (part & 0x0F)) {
				return it->objIdx;
			} else if (it->type == 1 && gfxGetBit(xdif, ydif, g_cine->_animDataTable[frame].data(), g_cine->_animDataTable[frame]._width * 4)) {
				return it->objIdx;
			}
		} else if (it->type == 0) { // use generated mask
			if (gfxGetBit(xdif, ydif, g_cine->_animDataTable[frame].mask(), g_cine->_animDataTable[frame]._width)) {
				return it->objIdx;
			}
		} else if (it->type == 1) { // is mask
			if (gfxGetBit(xdif, ydif, g_cine->_animDataTable[frame].data(), g_cine->_animDataTable[frame]._width * 4)) {
				return it->objIdx;
			}
		}
	}

	return -1;
}

void CineEngine::resetEngine() {
	g_sound->stopMusic();
	freeAnimDataTable();
	g_cine->_overlayList.clear();
	g_cine->_bgIncrustList.clear();
	closePart();

	g_cine->_objectScripts.clear();
	g_cine->_globalScripts.clear();
	g_cine->_relTable.clear();
	g_cine->_scriptTable.clear();
	g_cine->_messageTable.clear();
	resetObjectTable();

	g_cine->_globalVars.reset();

	var2 = var3 = var4 = var5 = 0;

	strcpy(newPrcName, "");
	strcpy(newRelName, "");
	strcpy(newObjectName, "");
	strcpy(newMsgName, "");
	strcpy(currentCtName, "");

	allowPlayerInput = 0;
	waitForPlayerClick = 0;
	playerCommand = -1;
	isDrawCommandEnabled = 0;

	g_cine->_commandBuffer = "";

	g_cine->_globalVars[VAR_MOUSE_X_POS] = 0;
	g_cine->_globalVars[VAR_MOUSE_Y_POS] = 0;

	fadeRequired = false;

	renderer->clear();

	checkForPendingDataLoadSwitch = 0;

	if (g_cine->getGameType() == Cine::GType_OS) {
		g_cine->_seqList.clear();
		currentAdditionalBgIdx = 0;
		currentAdditionalBgIdx2 = 0;
		// TODO: Add resetting of the following variables
		// adBgVar1 = 0;
		// adBgVar0 = 0;
		// gfxFadeOutCompleted = 0;
	}
}

int CineEngine::scummVMSaveLoadDialog(bool isSave) {
	GUI::SaveLoadChooser *dialog;
	Common::String desc;
	int slot;

	if (isSave) {
		dialog = new GUI::SaveLoadChooser(_("Save game:"), _("Save"), true);

		slot = dialog->runModalWithCurrentTarget();
		desc = dialog->getResultString();

		if (desc.empty()) {
			// create our own description for the saved game, the user didnt enter it
			desc = dialog->createDefaultSaveDescription(slot);
		}
	}
	else {
		dialog = new GUI::SaveLoadChooser(_("Restore game:"), _("Restore"), false);
		slot = dialog->runModalWithCurrentTarget();
	}

	delete dialog;

	if (slot < 0)
		return true;

	Common::String saveFileName(Common::String::format("%s.%1d", _targetName.c_str(), slot));

	if (isSave) {
		Common::String tmp = Common::String::format("%s.dir", _targetName.c_str());

		Common::OutSaveFile *fHandle = _saveFileMan->openForSaving(tmp);
		if (!fHandle) {
			warning("Unable to open file %s for saving", tmp.c_str());
			return false;
		}

		Common::strlcpy(currentSaveName[slot], desc.c_str(), 20);

		fHandle->write(currentSaveName, 200);
		delete fHandle;

		makeSave(saveFileName);
		return true;
	} else {
		return makeLoad(saveFileName);
	}
}

void CineEngine::makeSystemMenu() {
	int16 numEntry, systemCommand;
	int16 mouseX, mouseY, mouseButton;
	int16 selectedSave;

	if (disableSystemMenu != 1) {
		inMenu = true;

		do {
			manageEvents();
			getMouseData(mouseUpdateStatus, (uint16 *)&mouseButton, (uint16 *)&mouseX, (uint16 *)&mouseY);
		} while (mouseButton);

		numEntry = 6;

		if (!allowPlayerInput) {
			numEntry--;
		}

		systemCommand = makeMenuChoice(systemMenu, numEntry, mouseX, mouseY, 140);

		switch (systemCommand) {
		case 0: { // Pause
			renderer->drawString(otherMessages[2], 0);
			waitPlayerInput();
			break;
		}
		case 1: { // Restart Game
			getMouseData(mouseUpdateStatus, (uint16 *)&mouseButton, (uint16 *)&mouseX, (uint16 *)&mouseY);
			if (!makeMenuChoice(confirmMenu, 2, mouseX, mouseY + 8, 100)) {
				_restartRequested = true;
			}
			break;
		}
		case 2: { // Quit
			getMouseData(mouseUpdateStatus, (uint16 *)&mouseButton, (uint16 *)&mouseX, (uint16 *)&mouseY);
			if (!makeMenuChoice(confirmMenu, 2, mouseX, mouseY + 8, 100)) {
				quitGame();
			}
			break;
		}
		case 3: { // Select save drive... change ?
			break;
		}
		case 4: { // load game
			if (loadSaveDirectory()) {
				if (!ConfMan.getBool("originalsaveload")) {
					scummVMSaveLoadDialog(false);
					inMenu = false;
					return;
				}

				getMouseData(mouseUpdateStatus, (uint16 *)&mouseButton, (uint16 *)&mouseX, (uint16 *)&mouseY);
				selectedSave = makeMenuChoice(currentSaveName, 10, mouseX, mouseY + 8, 180);

				if (selectedSave >= 0) {
					char saveNameBuffer[256];
					sprintf(saveNameBuffer, "%s.%1d", _targetName.c_str(), selectedSave);

					getMouseData(mouseUpdateStatus, (uint16 *)&mouseButton, (uint16 *)&mouseX, (uint16 *)&mouseY);
					if (!makeMenuChoice(confirmMenu, 2, mouseX, mouseY + 8, 100)) {
						char loadString[256];

						sprintf(loadString, otherMessages[3], currentSaveName[selectedSave]);
						renderer->drawString(loadString, 0);

						makeLoad(saveNameBuffer);
					} else {
						renderer->drawString(otherMessages[4], 0);
						waitPlayerInput();
						checkDataDisk(-1);
					}
				} else {
					renderer->drawString(otherMessages[4], 0);
					waitPlayerInput();
					checkDataDisk(-1);
				}
			} else {
				renderer->drawString(otherMessages[5], 0);
				waitPlayerInput();
				checkDataDisk(-1);
			}
			break;
		}
		case 5: { // Save game
			loadSaveDirectory();

			if (!ConfMan.getBool("originalsaveload")) {
				scummVMSaveLoadDialog(true);
				inMenu = false;
				return;
			}

			selectedSave = makeMenuChoice(currentSaveName, 10, mouseX, mouseY + 8, 180);

			if (selectedSave >= 0) {
				char saveFileName[256];
				char saveName[20];
				saveName[0] = 0;

				if (!makeTextEntryMenu(otherMessages[6], saveName, 20, 120))
					break;

				Common::strlcpy(currentSaveName[selectedSave], saveName, 20);

				sprintf(saveFileName, "%s.%1d", _targetName.c_str(), selectedSave);

				getMouseData(mouseUpdateStatus, (uint16 *)&mouseButton, (uint16 *)&mouseX, (uint16 *)&mouseY);
				if (!makeMenuChoice(confirmMenu, 2, mouseX, mouseY + 8, 100)) {
					char saveString[256];
					Common::String tmp = Common::String::format("%s.dir", _targetName.c_str());

					Common::OutSaveFile *fHandle = _saveFileMan->openForSaving(tmp);
					if (!fHandle) {
						warning("Unable to open file %s for saving", tmp.c_str());
						break;
					}

					fHandle->write(currentSaveName, 200);
					delete fHandle;

					sprintf(saveString, otherMessages[3], currentSaveName[selectedSave]);
					renderer->drawString(saveString, 0);

					makeSave(saveFileName);

					checkDataDisk(-1);
				} else {
					renderer->drawString(otherMessages[4], 0);
					waitPlayerInput();
					checkDataDisk(-1);
				}
			}
			break;
		}
		}

		inMenu = false;
	}
}

void drawMessageBox(int16 x, int16 y, int16 width, int16 currentY, int16 offset, int16 color, byte *page) {
	gfxDrawLine(x + offset, y + offset, x + width - offset, y + offset, color, page);   // top
	gfxDrawLine(x + offset, currentY + 4 - offset, x + width - offset, currentY + 4 - offset, color, page); // bottom
	gfxDrawLine(x + offset, y + offset, x + offset, currentY + 4 - offset, color, page);    // left
	gfxDrawLine(x + width - offset, y + offset, x + width - offset, currentY + 4 - offset, color, page);    // right
}

void drawDoubleMessageBox(int16 x, int16 y, int16 width, int16 currentY, int16 color, byte *page) {
	drawMessageBox(x, y, width, currentY, 1, 0, page);
	drawMessageBox(x, y, width, currentY, 0, color, page);
}

void processInventory(int16 x, int16 y) {
	uint16 button;
	int menuWidth;
	int listSize;
	int commandParam;

	if (g_cine->getGameType() == Cine::GType_FW) {
		menuWidth = 140;
		commandParam = -2;
	} else { // Operation Stealth
		menuWidth = 160;
		commandParam = -3;
	}

	listSize = buildObjectListCommand(commandParam);

	if (!listSize)
		return;

	Common::StringArray list;
	for (int i = 0; i < listSize; ++i)
		list.push_back(objectListCommand[i]);
	SelectionMenu *menu = new SelectionMenu(Common::Point(x, y), menuWidth, list);
	renderer->pushMenu(menu);
	renderer->drawFrame();
	renderer->popMenu();
	delete menu;
	menu = 0;

	do {
		manageEvents();
		getMouseData(mouseUpdateStatus, &button, &dummyU16, &dummyU16);
	} while (!button);

	do {
		manageEvents();
		getMouseData(mouseUpdateStatus, &button, &dummyU16, &dummyU16);
	} while (button);
}

int16 buildObjectListCommand(int16 param) {
	int16 i = 0, j = 0;

	for (i = 0; i < 20; i++) {
		objectListCommand[i][0] = 0;
	}

	for (i = 0; i < 255; i++) {
		if (g_cine->_objectTable[i].name[0] && g_cine->_objectTable[i].costume == param) {
			strcpy(objectListCommand[j], g_cine->_objectTable[i].name);
			objListTab[j] = i;
			j++;
		}
	}

	return j;
}

int16 selectSubObject(int16 x, int16 y, int16 param) {
	int16 listSize = buildObjectListCommand(param);
	int16 selectedObject = -1;
	bool osExtras = g_cine->getGameType() == Cine::GType_OS;

	if (!listSize) {
		return -2;
	}

	if (disableSystemMenu == 0) {
		selectedObject = makeMenuChoice(objectListCommand, listSize, x, y, 140, osExtras);
	}

	if (selectedObject == -1)
		return -1;

	if (osExtras) {
		if (selectedObject >= 8000) {
			return objListTab[selectedObject - 8000] + 8000;
		}
	}

	if (selectedObject >= 20)
		error("Invalid value for selectedObject: %d", selectedObject);
	return objListTab[selectedObject];
}

void makeCommandLine() {
	if (g_cine->getGameType() == Cine::GType_FW)
		makeFWCommandLine();
	else
		makeOSCommandLine();
}

// TODO: Add support for using the different prepositions for different verbs (Doesn't work currently)
void makeOSCommandLine() {
	uint16 x, y;

	commandVar1 = 0;
	commandVar2 = -10;

	if (playerCommand != -1) {
		g_cine->_commandBuffer = defaultActionCommand[playerCommand];
	} else {
		g_cine->_commandBuffer = "";
	}

	if ((playerCommand != -1) && (choiceResultTable[playerCommand] == 2)) { // need object selection?
		int16 si;

		getMouseData(mouseUpdateStatus, &dummyU16, &x, &y);
		si = selectSubObject(x, y + 8, -subObjectUseTable[playerCommand]);

		if (si < 0) {
			canUseOnObject = 0;
		} else {
			if (si >= 8000) {
				si -= 8000;
				canUseOnObject = canUseOnItemTable[playerCommand];
			} else {
				canUseOnObject = 0;
			}

			commandVar3[0] = si;
			commandVar1 = 1;
			g_cine->_commandBuffer += " ";
			g_cine->_commandBuffer += g_cine->_objectTable[commandVar3[0]].name;
			g_cine->_commandBuffer += " ";
			g_cine->_commandBuffer += commandPrepositionTable[playerCommand];
		}
	}

	if (playerCommand == 2) {
		getMouseData(mouseUpdateStatus, &dummyU16, &x, &y);
		CursorMan.showMouse(false);
		processInventory(x, y + 8);
		playerCommand = -1;
		commandVar1 = 0;
		g_cine->_commandBuffer = "";
		CursorMan.showMouse(true);
	}

	if (playerCommand != 2) {
		if (playerCommand != -1 && canUseOnObject != 0) { // call use on sub object
			int16 si;

			getMouseData(mouseUpdateStatus, &dummyU16, &x, &y);

			si = selectSubObject(x, y + 8, -subObjectUseTable[playerCommand]);

			if (si >= 0) {
				if (si >= 8000) {
					si -= 8000;
				}

				commandVar3[commandVar1] = si;
				commandVar1++;
				g_cine->_commandBuffer += " ";
				g_cine->_commandBuffer += g_cine->_objectTable[si].name;
			}
		}

		isDrawCommandEnabled = 1;

		if (playerCommand != -1 && choiceResultTable[playerCommand] == commandVar1) {
			SelectedObjStruct obj;
			obj.idx = commandVar3[0];
			obj.param = commandVar3[1];
			int16 di = getRelEntryForObject(playerCommand, commandVar1, &obj);

			if (di != -1) {
				runObjectScript(di);
			} // TODO: else addFailureMessage(playerCommand)

			playerCommand = -1;
			commandVar1 = 0;
			g_cine->_commandBuffer = "";
		}
	}

	isDrawCommandEnabled = 1;
	renderer->setCommand(g_cine->_commandBuffer);
}

// TODO: Add support for using the different prepositions for different verbs (Doesn't work currently)
void makeFWCommandLine() {
	uint16 x, y;

	commandVar1 = 0;
	commandVar2 = -10;

	if (playerCommand != -1) {
		g_cine->_commandBuffer = defaultActionCommand[playerCommand];
	} else {
		g_cine->_commandBuffer = "";
	}

	if ((playerCommand != -1) && (choiceResultTable[playerCommand] == 2)) { // need object selection?
		int16 si;

		getMouseData(mouseUpdateStatus, &dummyU16, &x, &y);
		si = selectSubObject(x, y + 8, -2);

		if (si < 0) {
			playerCommand = -1;
			g_cine->_commandBuffer = "";
		} else {
			commandVar3[0] = si;
			commandVar1 = 1;
			g_cine->_commandBuffer += " ";
			g_cine->_commandBuffer += g_cine->_objectTable[commandVar3[0]].name;
			g_cine->_commandBuffer += " ";
			g_cine->_commandBuffer += defaultCommandPreposition;
		}
	}

	if (!(playerCommand != -1 && choiceResultTable[playerCommand] == 2)) {
		if (playerCommand == 2) {
			getMouseData(mouseUpdateStatus, &dummyU16, &x, &y);
			CursorMan.showMouse(false);
			processInventory(x, y + 8);
			playerCommand = -1;
			commandVar1 = 0;
			g_cine->_commandBuffer = "";
			CursorMan.showMouse(true);
		}
	}

	if (!disableSystemMenu) {
		isDrawCommandEnabled = 1;
		renderer->setCommand(g_cine->_commandBuffer);
	}
}

uint16 needMouseSave = 0;

uint16 menuVar4 = 0;
uint16 menuVar5 = 0;

int16 makeMenuChoice(const CommandeType commandList[], uint16 height, uint16 X, uint16 Y, uint16 width, bool recheckValue) {
	int16 paramY;
	uint16 button;
	int16 var_A;
	uint16 j;
	int16 mouseX, mouseY;
	int16 currentSelection, oldSelection;
	int16 var_4;
	SelectionMenu *menu;

	paramY = (height * 9) + 10;

	if (X + width > 319) {
		X = 319 - width;
	}

	if (Y + paramY > 199) {
		Y = 199 - paramY;
	}

	Common::StringArray list;
	for (uint16 i = 0; i < height; ++i)
		list.push_back(commandList[i]);
	menu = new SelectionMenu(Common::Point(X, Y), width, list);
	renderer->pushMenu(menu);
	renderer->drawFrame();

	do {
		manageEvents();
		getMouseData(mouseUpdateStatus, &button, &dummyU16, &dummyU16);
	} while (button && !g_cine->shouldQuit());

	var_A = 0;

	currentSelection = 0;

	menu->setSelection(currentSelection);
	renderer->drawFrame();

	manageEvents();
	getMouseData(mouseUpdateStatus, &button, (uint16 *)&mouseX, (uint16 *)&mouseY);

	menuVar = 0;

	do {
		manageEvents();
		getMouseData(mouseUpdateStatus, &button, (uint16 *)&mouseX, (uint16 *)&mouseY);

		if (button) {
			var_A = 1;
		}

		oldSelection = currentSelection;

		if (needMouseSave) {
			for (j = 0; j < 3; j++) {
				mainLoopSub6();
			}

			if (menuVar4 && currentSelection > 0) { // go up
				currentSelection--;
			}

			if (menuVar5) { // go down
				if (height - 1 > currentSelection) {
					currentSelection++;
				}
			}
		} else {
			if (mouseX > X && mouseX < X + width && mouseY > Y && mouseY < Y + height * 9) {
				currentSelection = (mouseY - (Y + 4)) / 9;

				if (currentSelection < 0)
					currentSelection = 0;

				if (currentSelection >= height)
					currentSelection = height - 1;
			}
		}

		if (currentSelection != oldSelection) { // old != new
			if (needMouseSave) {
				hideMouse();
			}

			menu->setSelection(currentSelection);
			renderer->drawFrame();

//			if (needMouseSave) {
//				gfxRedrawMouseCursor();
//			}
		}

	} while (!var_A && !g_cine->shouldQuit());

	assert(!needMouseSave);

	var_4 = button;

	menuVar = 0;

	do {
		manageEvents();
		getMouseData(mouseUpdateStatus, &button, &dummyU16, &dummyU16);
	} while (button && !g_cine->shouldQuit());

	if (var_4 == 2) { // recheck
		if (!recheckValue)
			return -1;
		else
			return currentSelection + 8000;
	}

	return currentSelection;
}

void makeActionMenu() {
	uint16 mouseButton;
	uint16 mouseX;
	uint16 mouseY;

	inMenu = true;

	getMouseData(mouseUpdateStatus, &mouseButton, &mouseX, &mouseY);

	if (g_cine->getGameType() == Cine::GType_OS) {
		if (disableSystemMenu == 0) {
			playerCommand = makeMenuChoice(defaultActionCommand, 6, mouseX, mouseY, 70, true);
		}

		if (playerCommand >= 8000) {
			playerCommand -= 8000;
			canUseOnObject = canUseOnItemTable[playerCommand];
		}
	} else {
		if (disableSystemMenu == 0) {
			playerCommand = makeMenuChoice(defaultActionCommand, 6, mouseX, mouseY, 70);
		}
	}

	inMenu = false;
}

uint16 executePlayerInput() {
	uint16 var_5E;
	uint16 var_2;
	uint16 mouseX, mouseY, mouseButton;
	uint16 currentEntry = 0;
	uint16 di = 0;
	bool limitMouseCheckCount = false;

	canUseOnObject = 0;

	if (isInPause) {
		renderer->drawString(otherMessages[2], 0);
		waitPlayerInput();
		isInPause = 0;
	}

	if (allowPlayerInput) { // Player input is allowed
		if (isDrawCommandEnabled) {
			renderer->setCommand(g_cine->_commandBuffer);
		}
		isDrawCommandEnabled = 0;
		limitMouseCheckCount = true;
	}

	// Get mouse position and button states
	di = 0;
	currentEntry = 0;
	getMouseData(mouseUpdateStatus, &mouseButton, &mouseX, &mouseY);

	while (mouseButton && (!limitMouseCheckCount || currentEntry < 200) && !g_cine->shouldQuit()) {
		di |= (mouseButton & (kLeftMouseButton | kRightMouseButton));
		if (!limitMouseCheckCount) {
			manageEvents();
		}
		getMouseData(mouseUpdateStatus, &mouseButton, &mouseX, &mouseY);
		currentEntry++;
	}

	if (di) {
		mouseButton = di;
	}

	if ((mouseButton & kLeftMouseButton) && (mouseButton & kRightMouseButton)) {
		// Left and right mouse buttons are down
		g_cine->makeSystemMenu();
	} else if (allowPlayerInput) { // Player input is allowed
		if (!(mouseButton & kLeftMouseButton) && (mouseButton & kRightMouseButton)) {
			// Player input is allowed, left mouse button is up, right mouse button is down
			makeActionMenu();
			makeCommandLine();
		} else if (playerCommand != -1) { // A player command is given
			if (mouseButton & kLeftMouseButton) { // Left mouse button is down
				if (!(mouseButton & kRightMouseButton)) { // Right mouse button is up
					// A player command is given, left mouse button is down, right mouse button is up
					int16 si;
					while (mouseButton && !g_cine->shouldQuit()) {
						manageEvents();
						getMouseData(mouseUpdateStatus, &mouseButton, &dummyU16, &dummyU16);
					}

					si = getObjectUnderCursor(mouseX, mouseY);

					if (si != -1) {
						commandVar3[commandVar1] = si;
						commandVar1++;

						g_cine->_commandBuffer += " ";
						g_cine->_commandBuffer += g_cine->_objectTable[si].name;

						isDrawCommandEnabled = 1;

						if (choiceResultTable[playerCommand] == commandVar1) {
							int16 relEntry;

							SelectedObjStruct obj;
							obj.idx = commandVar3[0];
							obj.param = commandVar3[1];

							relEntry = getRelEntryForObject(playerCommand, commandVar1, &obj);

							if (relEntry != -1) {
								runObjectScript(relEntry);
							} else {
								addPlayerCommandMessage(playerCommand);
							}

							playerCommand = -1;

							commandVar1 = 0;
							g_cine->_commandBuffer = "";
						} else if (g_cine->getGameType() == Cine::GType_OS) {
							isDrawCommandEnabled = 1;
							g_cine->_commandBuffer += commandPrepositionTable[playerCommand];
						}

						renderer->setCommand(g_cine->_commandBuffer);
					} else {
						g_cine->_globalVars[VAR_MOUSE_X_POS] = mouseX;
						if (!mouseX) {
							g_cine->_globalVars[VAR_MOUSE_X_POS]++;
						}

						g_cine->_globalVars[VAR_MOUSE_Y_POS] = mouseY;

						if (g_cine->getGameType() == Cine::GType_OS) {
							if (!mouseY) {
								g_cine->_globalVars[VAR_MOUSE_Y_POS]++;
							}
							g_cine->_globalVars[VAR_MOUSE_X_POS_2ND] = g_cine->_globalVars[VAR_MOUSE_X_POS];
							g_cine->_globalVars[VAR_MOUSE_Y_POS_2ND] = g_cine->_globalVars[VAR_MOUSE_Y_POS];
						}
					}
				}
			} else if (!(mouseButton & kRightMouseButton)) { // Right mouse button is up
				// A player command is given, left and right mouse buttons are up
				int16 objIdx;

				objIdx = getObjectUnderCursor(mouseX, mouseY);

				if (g_cine->getGameType() == Cine::GType_OS || commandVar2 != objIdx) {
					if (objIdx != -1) {
						renderer->setCommand(g_cine->_commandBuffer + " " + g_cine->_objectTable[objIdx].name);
					} else {
						isDrawCommandEnabled = 1;
					}
				}

				commandVar2 = objIdx;
			}
		} else { // No player command is given
			if (!(mouseButton & kRightMouseButton)) { // Right mouse button is up
				if (mouseButton & kLeftMouseButton) { // Left mouse button is down
					// No player command is given, left mouse button is down, right mouse button is up
					int16 objIdx;
					int16 relEntry;

					g_cine->_globalVars[VAR_MOUSE_X_POS] = mouseX;
					if (!mouseX) {
						g_cine->_globalVars[VAR_MOUSE_X_POS]++;
					}

					g_cine->_globalVars[VAR_MOUSE_Y_POS] = mouseY;

					if (g_cine->getGameType() == Cine::GType_OS) {
						if (!mouseY) {
							g_cine->_globalVars[VAR_MOUSE_Y_POS]++;
						}
						g_cine->_globalVars[VAR_MOUSE_X_POS_2ND] = g_cine->_globalVars[VAR_MOUSE_X_POS];
						g_cine->_globalVars[VAR_MOUSE_Y_POS_2ND] = g_cine->_globalVars[VAR_MOUSE_Y_POS];
					}

					objIdx = getObjectUnderCursor(mouseX, mouseY);

					if (objIdx != -1) {
						currentSelectedObject.idx = objIdx;
						currentSelectedObject.param = -1;

						relEntry = getRelEntryForObject(6, 1, &currentSelectedObject);

						if (relEntry != -1) {
							runObjectScript(relEntry);
						}
					}
				}
			}
		}
	}

	var_2 = menuVar & 0x7F;
	var_5E = var_2;

	if (menuVar & 0x80) {
		var_5E = 0;
		var_2 = 0;
	}

	if (g_cine->getGameType() == Cine::GType_OS) { // OS: Move using keyboard
		// Handle possible horizontal movement by keyboard
		if (xMoveKeyb != kKeybMoveCenterX && allowPlayerInput) {
			if (xMoveKeyb == kKeybMoveRight) { // moving right
				const int16 playerFrame = g_cine->_objectTable[1].frame;
				const int16 playerX = g_cine->_objectTable[1].x;
				// TODO: Check if multiplying _width by two here is correct or not
				const int16 newX = g_cine->_animDataTable[playerFrame]._width * 2 + playerX + 8;
				g_cine->_globalVars[VAR_MOUSE_X_POS] = g_cine->_globalVars[VAR_MOUSE_X_POS_2ND] = newX;
			} else { // moving left
				const int16 playerX = g_cine->_objectTable[1].x;
				const int16 newX = playerX - 8;
				g_cine->_globalVars[VAR_MOUSE_X_POS] = g_cine->_globalVars[VAR_MOUSE_X_POS_2ND] = newX;
			}

			// Restrain horizontal position to range 0-319
			if (g_cine->_globalVars[VAR_MOUSE_X_POS] < 0) {
				g_cine->_globalVars[VAR_MOUSE_X_POS] = g_cine->_globalVars[VAR_MOUSE_X_POS_2ND] = 0;
			} else if (g_cine->_globalVars[VAR_MOUSE_X_POS] > 319) {
				g_cine->_globalVars[VAR_MOUSE_X_POS] = g_cine->_globalVars[VAR_MOUSE_X_POS_2ND] = 319;
			}
		}

		// Handle possible vertical movement by keyboard
		if (yMoveKeyb != kKeybMoveCenterY && allowPlayerInput) {
			if (yMoveKeyb == kKeybMoveDown) { // moving down
				const int16 playerFrame = g_cine->_objectTable[1].frame;
				const int16 playerY = g_cine->_objectTable[1].y;
				// TODO: Check if multiplying _height by two here is correct or not
				const int16 newY = g_cine->_animDataTable[playerFrame]._height * 2 + playerY - 1;
				g_cine->_globalVars[VAR_MOUSE_Y_POS] = g_cine->_globalVars[VAR_MOUSE_Y_POS_2ND] = newY;
			} else { // moving up
				const int16 playerY = g_cine->_objectTable[1].y;
				const int16 newY = playerY - 8;
				g_cine->_globalVars[VAR_MOUSE_Y_POS] = g_cine->_globalVars[VAR_MOUSE_Y_POS_2ND] = newY;
			}

			// Restrain vertical position to range 0-199
			if (g_cine->_globalVars[VAR_MOUSE_Y_POS] < 0) {
				g_cine->_globalVars[VAR_MOUSE_Y_POS] = g_cine->_globalVars[VAR_MOUSE_Y_POS_2ND] = 0;
			} else if (g_cine->_globalVars[VAR_MOUSE_Y_POS] > 199) {
				g_cine->_globalVars[VAR_MOUSE_Y_POS] = g_cine->_globalVars[VAR_MOUSE_Y_POS_2ND] = 199;
			}
		}
	} else if (egoMovedWithKeyboard && allowPlayerInput) { // FW: Move using keyboard
		egoMovedWithKeyboard = false;

		switch (g_cine->_globalVars[VAR_MOUSE_X_MODE]) {
		case 1:
			mouseX = g_cine->_objectTable[1].x + 12;
			break;
		case 2:
			mouseX = g_cine->_objectTable[1].x + 7;
			break;
		default:
			mouseX = g_cine->_globalVars[VAR_MOUSE_X_POS];
			break;
		}

		switch (g_cine->_globalVars[VAR_MOUSE_Y_MODE]) {
		case 1:
			mouseY = g_cine->_objectTable[1].y + 34;
			break;
		case 2:
			mouseY = g_cine->_objectTable[1].y + 28;
			break;
		default:
			mouseY = g_cine->_globalVars[VAR_MOUSE_Y_POS];
			break;
		}

		if (var_5E == bgVar0) {
			var_5E = 0;

			g_cine->_globalVars[VAR_MOUSE_X_POS] = mouseX;
			g_cine->_globalVars[VAR_MOUSE_Y_POS] = mouseY;
		} else {
			if (xMoveKeyb) {
				if (xMoveKeyb == kKeybMoveLeft) {
					g_cine->_globalVars[VAR_MOUSE_X_POS] = 1;
				} else {
					g_cine->_globalVars[VAR_MOUSE_X_POS] = 320;
				}
			} else {
				g_cine->_globalVars[VAR_MOUSE_X_POS] = mouseX;
			}

			if (yMoveKeyb) {
				if (yMoveKeyb == kKeybMoveUp) {
					g_cine->_globalVars[VAR_MOUSE_Y_POS] = 1;
				} else {
					g_cine->_globalVars[VAR_MOUSE_Y_POS] = 200;
				}
			} else {
				g_cine->_globalVars[VAR_MOUSE_Y_POS] = mouseY;
			}
		}

		bgVar0 = var_5E;
	}

	if (g_cine->getGameType() == Cine::GType_OS || !(egoMovedWithKeyboard && allowPlayerInput)) {
		getMouseData(mouseUpdateStatus, &mouseButton, &mouseX, &mouseY);

		// TODO: Investigate why some of these buttons don't work (Maybe main_loop.cpp's processEvent has something to do with it?)
		switch (var_2 - 59) {
		case 0: // F1 = EXAMINE
		case 1: // F2 = TAKE
		case 2: // F3 = INVENTORY
		case 3: // F4 = USE
		case 4: // F5 = OPERATE
		case 5: // F6 = SPEAK
			if (allowPlayerInput) {
				playerCommand = var_2 - 59;
				// TODO: Operation Stealth uses shift key here for handling canUseOnObject differently... investigate and implement.
				makeCommandLine();
			}
			break;
		case 6: // F7
		case 7: // F8
		case 8: // F9
		case 23: // Keypad-0/Ins
			// TODO: Restrict this case only to F7 for Operation Stealth
			if (allowPlayerInput) {
				makeActionMenu();
				makeCommandLine();
			}
			break;
		case 9: // F10
		case 24: // Keypad-./Del
			// TODO: Restrict this case only to F10 for Operation Stealth
			g_cine->makeSystemMenu();
			break;
		default:
			break;
		}
	}

	renderer->clearMenuStack();

	return var_5E;
}

void drawSprite(Common::List<overlay>::iterator it, const byte *spritePtr, const byte *maskPtr, uint16 width, uint16 height, byte *page, int16 x, int16 y) {
	byte *msk = NULL;
	int16 maskX, maskY, maskWidth, maskHeight;
	uint16 maskSpriteIdx;

	msk = (byte *)malloc(width * height);

	if (g_cine->getGameType() == Cine::GType_OS) {
		generateMask(spritePtr, msk, width * height, g_cine->_objectTable[it->objIdx].part);
	} else {
		memcpy(msk, maskPtr, width * height);
	}

	for (++it; it != g_cine->_overlayList.end(); ++it) {
		if (it->type != 5) {
			continue;
		}

		maskX = g_cine->_objectTable[it->objIdx].x;
		maskY = g_cine->_objectTable[it->objIdx].y;

		maskSpriteIdx = ABS((int16)(g_cine->_objectTable[it->objIdx].frame));

		maskWidth = g_cine->_animDataTable[maskSpriteIdx]._realWidth;
		maskHeight = g_cine->_animDataTable[maskSpriteIdx]._height;
		gfxUpdateSpriteMask(msk, x, y, width, height, g_cine->_animDataTable[maskSpriteIdx].data(), maskX, maskY, maskWidth, maskHeight);

#ifdef DEBUG_SPRITE_MASK
		gfxFillSprite(g_cine->_animDataTable[maskSpriteIdx].data(), maskWidth, maskHeight, page, maskX, maskY, 1);
#endif
	}

	gfxDrawMaskedSprite(spritePtr, msk, width, height, page, x, y);
	free(msk);
}

void removeMessages() {
	Common::List<overlay>::iterator it;
	bool remove;

	for (it = g_cine->_overlayList.begin(); it != g_cine->_overlayList.end();) {
		if (g_cine->getGameType() == Cine::GType_OS) {
			// NOTE: These are really removeOverlay calls that have been deferred.
			// In Operation Stealth's disassembly elements are removed from the
			// overlay list right in the drawOverlays function (And actually in
			// some other places too) and that's where incrementing a the overlay's
			// last parameter by one if it's negative and testing it for positivity
			// comes from too.
			remove = it->type == 3 || (it->type == 2 && (it->color >= 0 || ++it->color >= 0));
		} else { // Future Wars
			remove = it->type == 2 || it->type == 3;
		}

		if (remove) {
			it = g_cine->_overlayList.erase(it);
		} else {
			++it;
		}
	}
}

uint16 processKeyboard(uint16 param) {
	return 0;
}

void mainLoopSub6() {
}

void checkForPendingDataLoad() {
	if (newPrcName[0] != 0) {
		bool loadPrcOk = loadPrc(newPrcName);

		strcpy(currentPrcName, newPrcName);
		strcpy(newPrcName, "");

		// Check that the loading of the script file was successful before
		// trying to add script 1 from it to the global scripts list. This
		// fixes a crash when failing copy protection in Amiga or Atari ST
		// versions of Future Wars.
		if (loadPrcOk) {
			addScriptToGlobalScripts(1);
		} else if (scumm_stricmp(currentPrcName, COPY_PROT_FAIL_PRC_NAME)) {
			// We only show an error here for other files than the file that
			// is loaded if copy protection fails (i.e. L201.ANI).
			warning("checkForPendingDataLoad: loadPrc(%s) failed", currentPrcName);
		}
	}

	if (newRelName[0] != 0) {
		loadRel(newRelName);

		strcpy(currentRelName, newRelName);
		strcpy(newRelName, "");
	}

	if (newObjectName[0] != 0) {
		g_cine->_overlayList.clear();

		loadObject(newObjectName);

		strcpy(currentObjectName, newObjectName);
		strcpy(newObjectName, "");
	}

	if (newMsgName[0] != 0) {
		loadMsg(newMsgName);

		strcpy(currentMsgName, newMsgName);
		strcpy(newMsgName, "");
	}
}

void hideMouse() {
}

void removeExtention(char *dest, const char *source) {
	strcpy(dest, source);

	byte *ptr = (byte *) strchr(dest, '.');

	if (ptr) {
		*ptr = 0;
	}
}

void addMessage(byte param1, int16 param2, int16 param3, int16 param4, int16 param5) {
	overlay tmp;

	tmp.objIdx = param1;
	tmp.type = 2;
	tmp.x = param2;
	tmp.y = param3;
	tmp.width = param4;
	tmp.color = param5;

	g_cine->_overlayList.push_back(tmp);
}

void removeSeq(uint16 param1, uint16 param2, uint16 param3) {
	Common::List<SeqListElement>::iterator it;

	for (it = g_cine->_seqList.begin(); it != g_cine->_seqList.end(); ++it) {
		if (it->objIdx == param1 && it->var4 == param2 && it->varE == param3) {
			it->var4 = -1;
			break;
		}
	}
}

bool isSeqRunning(uint16 param1, uint16 param2, uint16 param3) {
	Common::List<SeqListElement>::iterator it;

	for (it = g_cine->_seqList.begin(); it != g_cine->_seqList.end(); ++it) {
		if (it->objIdx == param1 && it->var4 == param2 && it->varE == param3) {
			// Just to be on the safe side there's a restriction of the
			// addition's result to 16-bit arithmetic here like in the
			// original. It's possible that it's not strictly needed.
			return ((it->var14 + it->var16) & 0xFFFF) == 0;
		}
	}

	return true;
}

void addSeqListElement(uint16 objIdx, int16 param1, int16 param2, int16 frame, int16 param4, int16 param5, int16 param6, int16 param7, int16 param8) {
	Common::List<SeqListElement>::iterator it;
	SeqListElement tmp;

	for (it = g_cine->_seqList.begin(); it != g_cine->_seqList.end() && it->varE < param7; ++it)
		;

	tmp.objIdx = objIdx;
	tmp.var4 = param1;
	tmp.var8 = param2;
	tmp.frame = frame;
	tmp.varC = param4;
	tmp.var14 = 0;
	tmp.var16 = 0;
	tmp.var18 = param5;
	tmp.var1A = param6;
	tmp.varE = param7;
	tmp.var10 = param8;
	tmp.var12 = param8;
	tmp.var1C = 0;
	tmp.var1E = 0;

	g_cine->_seqList.insert(it, tmp);
}

void modifySeqListElement(uint16 objIdx, int16 var4Test, int16 param1, int16 param2, int16 param3, int16 param4) {
	// Find a suitable list element and modify it
	for (Common::List<SeqListElement>::iterator it = g_cine->_seqList.begin(); it != g_cine->_seqList.end(); ++it) {
		if (it->objIdx == objIdx && it->var4 == var4Test) {
			it->varC  = param1;
			it->var18 = param2;
			it->var1A = param3;
			it->var10 = it->var12 = param4;
			break;
		}
	}
}

void computeMove1(SeqListElement &element, int16 x, int16 y, int16 param1,
                  int16 param2, int16 x2, int16 y2) {
	element.var16 = 0;
	element.var14 = 0;

	if (y2) {
		if (y - param2 > y2) {
			element.var16 = 2;
		}

		if (y + param2 < y2) {
			element.var16 = 1;
		}
	}

	if (x2) {
		if (x - param1 > x2) {
			element.var14 = 2;
		}

		if (x + param1 < x2) {
			element.var14 = 1;
		}
	}
}

uint16 computeMove2(SeqListElement &element) {
	int16 returnVar = 0;

	if (element.var16 == 1) {
		returnVar = 4;
	} else if (element.var16 == 2) {
		returnVar = 3;
	}

	if (element.var14 == 1) {
		returnVar = 1;
	} else if (element.var14 == 2) {
		returnVar = 2;
	}

	return returnVar;
}

uint16 addAni(uint16 param1, uint16 objIdx, const int8 *ptr, SeqListElement &element, uint16 param3, int16 *param4) {
	const int8 *ptrData;
	const int8 *ptr2;
	int16 di;

	debug(5, "addAni: param1 = %d, objIdx = %d, ptr = %p, element.var8 = %d, element.var14 = %d param3 = %d",
	      param1, objIdx, ptr, element.var8, element.var14, param3);

	// In the original an error string is set and 0 is returned if the following doesn't hold
	assert(ptr);

	// We probably could just use a local variable here instead of the dummyU16 but
	// haven't checked if this has any side-effects so keeping it this way still.
	dummyU16 = READ_BE_UINT16(ptr + param1 * 2 + 8);
	ptrData = ptr + dummyU16;

	// In the original an error string is set and 0 is returned if the following doesn't hold
	assert(*ptrData);

	di = (g_cine->_objectTable[objIdx].costume + 1) % (*ptrData);
	++ptrData; // Jump over the just read byte
	// Here ptr2 seems to be indexing a table of structs (8 bytes per struct):
	// struct {
	//      int8 x;         // 0 (Used with checkCollision)
	//      int8 y;         // 1 (Used with checkCollision)
	//      int8 numZones;  // 2 (Used with checkCollision)
	//      int8 var3;      // 3 (Not used in this function)
	//      int8 xAdd;      // 4 (Used with an object)
	//      int8 yAdd;      // 5 (Used with an object)
	//      int8 maskAdd;   // 6 (Used with an object)
	//      int8 frameAdd;  // 7 (Used with an object)
	// };
	ptr2 = ptrData + di * 8;

	// We might probably safely discard the AND by 1 here because
	// at least in the original checkCollision returns always 0 or 1.
	if ((checkCollision(objIdx, ptr2[0], ptr2[1], ptr2[2], ptr[0]) & 1)) {
		return 0;
	}

	g_cine->_objectTable[objIdx].x += ptr2[4];
	g_cine->_objectTable[objIdx].y += ptr2[5];
	g_cine->_objectTable[objIdx].mask += ptr2[6];

	if (ptr2[6]) {
		resetGfxEntityEntry(objIdx);
	}

	g_cine->_objectTable[objIdx].frame = ptr2[7] + element.var8;

	if (param3 || !element.var14) {
		g_cine->_objectTable[objIdx].costume = di;
	} else {
		assert(param4);
		*param4 = di;
	}

	return 1;
}

/**
 * Permutates the overlay list into a different order according to some logic.
 * @todo Check this function for correctness (Wasn't very easy to reverse engineer so there may be errors)
 */
void resetGfxEntityEntry(uint16 objIdx) {
	Common::List<overlay>::iterator it, bObjsCutPoint;
	Common::List<overlay> aReverseObjs, bObjs;
	bool foundCutPoint = false;

	// Go through the overlay list and partition the whole list into two categories (Type A and type B objects)
	for (it = g_cine->_overlayList.begin(); it != g_cine->_overlayList.end(); ++it) {
		if (it->objIdx == objIdx && it->type != 2 && it->type != 3) { // Type A object
			aReverseObjs.push_front(*it);
		} else { // Type B object
			bObjs.push_back(*it);
			uint16 objectMask;
			if (it->type == 2 || it->type == 3) {
				objectMask = 10000;
			} else {
				objectMask = g_cine->_objectTable[it->objIdx].mask;
			}

			if (g_cine->_objectTable[objIdx].mask > objectMask) { // Check for B objects' cut point
				bObjsCutPoint = bObjs.reverse_begin();
				foundCutPoint = true;
			}
		}
	}

	// Recreate the overlay list in a different order.
	g_cine->_overlayList.clear();
	if (foundCutPoint) {
		// If a cut point was found the order is:
		// B objects before the cut point, the cut point, A objects in reverse order, B objects after cut point.
		++bObjsCutPoint; // Include the cut point in the first list insertion
		g_cine->_overlayList.insert(g_cine->_overlayList.end(), bObjs.begin(), bObjsCutPoint);
		g_cine->_overlayList.insert(g_cine->_overlayList.end(), aReverseObjs.begin(), aReverseObjs.end());
		g_cine->_overlayList.insert(g_cine->_overlayList.end(), bObjsCutPoint, bObjs.end());
	} else {
		// If no cut point was found the order is:
		// A objects in reverse order, B objects.
		g_cine->_overlayList.insert(g_cine->_overlayList.end(), aReverseObjs.begin(), aReverseObjs.end());
		g_cine->_overlayList.insert(g_cine->_overlayList.end(), bObjs.begin(), bObjs.end());
	}
}

void processSeqListElement(SeqListElement &element) {
	int16 x = g_cine->_objectTable[element.objIdx].x;
	int16 y = g_cine->_objectTable[element.objIdx].y;
	const int8 *ptr1 = (const int8 *) g_cine->_animDataTable[element.frame].data();
	int16 var_10;
	int16 var_4;
	int16 var_2;

	// Initial interpretations for variables addressed through ptr1 (8-bit addressing):
	// These may be inaccurate!
	// 0: ?
	// 1: xRadius
	// 2: yRadius
	// 3: ?
	// 4: xAdd
	// 5: yAdd
	// 6: ?
	// 7: ?
	// After this come (At least at positions 0, 1 and 3 in 16-bit addressing)
	// 16-bit big-endian values used for addressing through ptr1.

	if (element.var12 < element.var10) {
		element.var12++;
		return;
	}

	element.var12 = 0;

	if (ptr1) {
		int16 param1 = ptr1[1];
		int16 param2 = ptr1[2];

		if (element.varC != 255) {
			int16 x2 = element.var18;
			int16 y2 = element.var1A;
			if (element.varC) {
				x2 += g_cine->_objectTable[element.varC].x;
				y2 += g_cine->_objectTable[element.varC].y;
			}
			computeMove1(element, ptr1[4] + x, ptr1[5] + y, param1, param2, x2, y2);
		} else {
			if (xMoveKeyb && allowPlayerInput) {
				int16 adder = param1 + 1;
				if (xMoveKeyb != kKeybMoveRight) {
					adder = -adder;
				}
				g_cine->_globalVars[VAR_MOUSE_X_POS] = g_cine->_globalVars[VAR_MOUSE_X_POS_2ND] = ptr1[4] + x + adder;
			}

			if (yMoveKeyb && allowPlayerInput) {
				int16 adder = param2 + 1;
				if (yMoveKeyb != kKeybMoveDown) {
					adder = -adder;
				}
				g_cine->_globalVars[VAR_MOUSE_Y_POS] = g_cine->_globalVars[VAR_MOUSE_Y_POS_2ND] = ptr1[5] + y + adder;
			}

			if (g_cine->_globalVars[VAR_MOUSE_X_POS] || g_cine->_globalVars[VAR_MOUSE_Y_POS]) {
				computeMove1(element, ptr1[4] + x, ptr1[5] + y, param1, param2, g_cine->_globalVars[VAR_MOUSE_X_POS], g_cine->_globalVars[VAR_MOUSE_Y_POS]);
			} else {
				element.var16 = 0;
				element.var14 = 0;
			}
		}

		var_10 = computeMove2(element);

		if (var_10) {
			element.var1C = var_10;
			element.var1E = var_10;
		}

		var_4 = -1;

		if ((element.var16 == 1
			&& !addAni(3, element.objIdx, ptr1, element, 0, &var_4)) || (element.var16 == 2 && !addAni(2, element.objIdx, ptr1, element, 0, &var_4))) {
			if (element.varC == 255) {
				g_cine->_globalVars[VAR_MOUSE_Y_POS] = 0;
			}
		}

		if ((element.var14 == 1
			&& !addAni(0, element.objIdx, ptr1, element, 1, &var_2))) {
			if (element.varC == 255) {
				g_cine->_globalVars[VAR_MOUSE_X_POS] = 0;

				if (var_4 != -1) {
					g_cine->_objectTable[element.objIdx].costume = var_4;
				}
			}
		}

		if ((element.var14 == 2 && !addAni(1, element.objIdx, ptr1, element, 1, &var_2))) {
			if (element.varC == 255) {
				g_cine->_globalVars[VAR_MOUSE_X_POS] = 0;

				if (var_4 != -1) {
					g_cine->_objectTable[element.objIdx].costume = var_4;
				}
			}
		}

		if (element.var16 + element.var14 == 0) {
			if (element.var1C) {
				if (element.var1E) {
					g_cine->_objectTable[element.objIdx].costume = 0;
					element.var1E = 0;
				}

				addAni(element.var1C + 3, element.objIdx, ptr1, element, 1, &var_2);

			}
		}

	}
}

void processSeqList() {
	Common::List<SeqListElement>::iterator it;

	for (it = g_cine->_seqList.begin(); it != g_cine->_seqList.end(); ++it) {
		if (it->var4 == -1) {
			continue;
		}

		processSeqListElement(*it);
	}
}


bool makeTextEntryMenu(const char *messagePtr, char *inputString, int stringMaxLength, int y) {
	int len = strlen(messagePtr);
	int16 width = 6 * len + 20;

	width = CLIP((int)width, 180, 250);

	int16 x = (320 - width) / 2;

	getKeyData(); // clear input key

	int quit = 0;
	bool redraw = true;
	CommandeType tempString;
	int inputLength = strlen(inputString);
	int inputPos = inputLength + 1;

	TextInputMenu *inputBox = new TextInputMenu(Common::Point(x - 16, y), width + 32, messagePtr);
	renderer->pushMenu(inputBox);

	while (!quit) {
		if (redraw) {
			inputBox->setInput(inputString, inputPos);
			renderer->drawFrame();
			redraw = false;
		}

		char ch[2];
		memset(tempString, 0, stringMaxLength);
		ch[1] = 0;

		manageEvents();
		int keycode = getKeyData();
		uint16 mouseButton, mouseX, mouseY;

		getMouseData(0, &mouseButton, &mouseX, &mouseY);

		if ((mouseButton & kRightMouseButton) || g_cine->shouldQuit())
			quit = kRightMouseButton;
		else if (mouseButton & kLeftMouseButton)
			quit = kLeftMouseButton;

		switch (keycode) {
		case Common::KEYCODE_BACKSPACE:
			if (inputPos <= 1) {
				break;
			}
			inputPos--;
			redraw = true;
		case Common::KEYCODE_DELETE:
			if (inputPos <= inputLength) {
				if (inputPos != 1) {
					strncpy(tempString, inputString, inputPos - 1);
				}
				if (inputPos != inputLength) {
					strncat(tempString, &inputString[inputPos], inputLength - inputPos);
				}
				strcpy(inputString, tempString);
				inputLength = strlen(inputString);
				redraw = true;
			}
			break;
		case Common::KEYCODE_LEFT:
			if (inputPos > 1) {
				inputPos--;
				redraw = true;
			}
			break;
		case Common::KEYCODE_RIGHT:
			if (inputPos <= inputLength) {
				inputPos++;
				redraw = true;
			}
			break;
		default:
			if (((keycode >= 'a') && (keycode <= 'z')) ||
				((keycode >= '0') && (keycode <= '9')) ||
				((keycode >= 'A') && (keycode <= 'Z')) ||
				(keycode == ' ')) {
				if (inputLength < stringMaxLength - 1) {
					ch[0] = keycode;
					if (inputPos != 1) {
						strncpy(tempString, inputString, inputPos - 1);
						strcat(tempString, ch);
					}
					if ((inputLength == 0) || (inputPos == 1)) {
						strcpy(tempString, ch);
					}
					if ((inputLength != 0) && (inputPos != inputLength)) {
						strncat(tempString, &inputString[inputPos - 1], inputLength - inputPos + 1);
					}

					strcpy(inputString, tempString);
					inputLength = strlen(inputString);
					inputPos++;
					redraw = true;
				}
			}
			break;
		}
	}

	renderer->popMenu();
	delete inputBox;

	if (quit == kRightMouseButton)
		return false;

	return true;
}

} // End of namespace Cine