aboutsummaryrefslogtreecommitdiff
path: root/engines/scumm/script_v2.cpp
blob: a07d1f7015e2bc591d8e2c6181e1be98ed1cc919 (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
/* 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 "scumm/actor.h"
#include "scumm/charset.h"
#include "scumm/object.h"
#include "scumm/resource.h"
#include "scumm/scumm_v2.h"
#include "scumm/sound.h"
#include "scumm/util.h"
#include "scumm/verbs.h"

namespace Scumm {

    // Helper functions for ManiacMansion workarounds
#define MM_SCRIPT(script)  (script + (_game.version == 0 ? 0 : 5))
#define MM_VALUE(v0,v1)    (_game.version == 0 ? v0 : v1)

#define OPCODE(i, x)	_opcodes[i]._OPCODE(ScummEngine_v2, x)

void ScummEngine_v2::setupOpcodes() {
	/* 00 */
	OPCODE(0x00, o5_stopObjectCode);
	OPCODE(0x01, o2_putActor);
	OPCODE(0x02, o5_startMusic);
	OPCODE(0x03, o5_getActorRoom);
	/* 04 */
	OPCODE(0x04, o2_isGreaterEqual);
	OPCODE(0x05, o2_drawObject);
	OPCODE(0x06, o2_getActorElevation);
	OPCODE(0x07, o2_setState08);
	/* 08 */
	OPCODE(0x08, o5_isNotEqual);
	OPCODE(0x09, o5_faceActor);
	OPCODE(0x0a, o2_assignVarWordIndirect);
	OPCODE(0x0b, o2_setObjPreposition);
	/* 0C */
	OPCODE(0x0c, o2_resourceRoutines);
	OPCODE(0x0d, o5_walkActorToActor);
	OPCODE(0x0e, o2_putActorAtObject);
	OPCODE(0x0f, o2_ifNotState08);
	/* 10 */
	OPCODE(0x10, o5_getObjectOwner);
	OPCODE(0x11, o5_animateActor);
	OPCODE(0x12, o2_panCameraTo);
	OPCODE(0x13, o2_actorOps);
	/* 14 */
	OPCODE(0x14, o5_print);
	OPCODE(0x15, o2_actorFromPos);
	OPCODE(0x16, o5_getRandomNr);
	OPCODE(0x17, o2_clearState02);
	/* 18 */
	OPCODE(0x18, o5_jumpRelative);
	OPCODE(0x19, o2_doSentence);
	OPCODE(0x1a, o5_move);
	OPCODE(0x1b, o2_setBitVar);
	/* 1C */
	OPCODE(0x1c, o5_startSound);
	OPCODE(0x1d, o2_ifClassOfIs);
	OPCODE(0x1e, o2_walkActorTo);
	OPCODE(0x1f, o2_ifState02);
	/* 20 */
	OPCODE(0x20, o5_stopMusic);
	OPCODE(0x21, o2_putActor);
	OPCODE(0x22, o4_saveLoadGame);
	OPCODE(0x23, o2_getActorY);
	/* 24 */
	OPCODE(0x24, o2_loadRoomWithEgo);
	OPCODE(0x25, o2_drawObject);
	OPCODE(0x26, o5_setVarRange);
	OPCODE(0x27, o2_setState04);
	/* 28 */
	OPCODE(0x28, o5_equalZero);
	OPCODE(0x29, o2_setOwnerOf);
	OPCODE(0x2a, o2_addIndirect);
	OPCODE(0x2b, o5_delayVariable);
	/* 2C */
	OPCODE(0x2c, o2_assignVarByte);
	OPCODE(0x2d, o2_putActorInRoom);
	OPCODE(0x2e, o2_delay);
	OPCODE(0x2f, o2_ifNotState04);
	/* 30 */
	OPCODE(0x30, o3_setBoxFlags);
	OPCODE(0x31, o2_getBitVar);
	OPCODE(0x32, o2_setCameraAt);
	OPCODE(0x33, o2_roomOps);
	/* 34 */
	OPCODE(0x34, o5_getDist);
	OPCODE(0x35, o2_findObject);
	OPCODE(0x36, o2_walkActorToObject);
	OPCODE(0x37, o2_setState01);
	/* 38 */
	OPCODE(0x38, o2_isLessEqual);
	OPCODE(0x39, o2_doSentence);
	OPCODE(0x3a, o2_subtract);
	OPCODE(0x3b, o2_waitForActor);
	/* 3C */
	OPCODE(0x3c, o5_stopSound);
	OPCODE(0x3d, o2_setActorElevation);
	OPCODE(0x3e, o2_walkActorTo);
	OPCODE(0x3f, o2_ifNotState01);
	/* 40 */
	OPCODE(0x40, o2_cutscene);
	OPCODE(0x41, o2_putActor);
	OPCODE(0x42, o2_startScript);
	OPCODE(0x43, o2_getActorX);
	/* 44 */
	OPCODE(0x44, o2_isLess);
	OPCODE(0x45, o2_drawObject);
	OPCODE(0x46, o5_increment);
	OPCODE(0x47, o2_clearState08);
	/* 48 */
	OPCODE(0x48, o5_isEqual);
	OPCODE(0x49, o5_faceActor);
	OPCODE(0x4a, o2_chainScript);
	OPCODE(0x4b, o2_setObjPreposition);
	/* 4C */
	OPCODE(0x4c, o2_waitForSentence);
	OPCODE(0x4d, o5_walkActorToActor);
	OPCODE(0x4e, o2_putActorAtObject);
	OPCODE(0x4f, o2_ifState08);
	/* 50 */
	OPCODE(0x50, o2_pickupObject);
	OPCODE(0x51, o5_animateActor);
	OPCODE(0x52, o5_actorFollowCamera);
	OPCODE(0x53, o2_actorOps);
	/* 54 */
	OPCODE(0x54, o5_setObjectName);
	OPCODE(0x55, o2_actorFromPos);
	OPCODE(0x56, o5_getActorMoving);
	OPCODE(0x57, o2_setState02);
	/* 58 */
	OPCODE(0x58, o2_beginOverride);
	OPCODE(0x59, o2_doSentence);
	OPCODE(0x5a, o2_add);
	OPCODE(0x5b, o2_setBitVar);
	/* 5C */
	OPCODE(0x5c, o2_dummy);
	OPCODE(0x5d, o2_ifClassOfIs);
	OPCODE(0x5e, o2_walkActorTo);
	OPCODE(0x5f, o2_ifNotState02);
	/* 60 */
	OPCODE(0x60, o2_cursorCommand);
	OPCODE(0x61, o2_putActor);
	OPCODE(0x62, o2_stopScript);
	OPCODE(0x63, o5_getActorFacing);
	/* 64 */
	OPCODE(0x64, o2_loadRoomWithEgo);
	OPCODE(0x65, o2_drawObject);
	OPCODE(0x66, o5_getClosestObjActor);
	OPCODE(0x67, o2_clearState04);
	/* 68 */
	OPCODE(0x68, o5_isScriptRunning);
	OPCODE(0x69, o2_setOwnerOf);
	OPCODE(0x6a, o2_subIndirect);
	OPCODE(0x6b, o2_dummy);
	/* 6C */
	OPCODE(0x6c, o2_getObjPreposition);
	OPCODE(0x6d, o2_putActorInRoom);
	OPCODE(0x6e, o2_dummy);
	OPCODE(0x6f, o2_ifState04);
	/* 70 */
	OPCODE(0x70, o2_lights);
	OPCODE(0x71, o5_getActorCostume);
	OPCODE(0x72, o5_loadRoom);
	OPCODE(0x73, o2_roomOps);
	/* 74 */
	OPCODE(0x74, o5_getDist);
	OPCODE(0x75, o2_findObject);
	OPCODE(0x76, o2_walkActorToObject);
	OPCODE(0x77, o2_clearState01);
	/* 78 */
	OPCODE(0x78, o2_isGreater);
	OPCODE(0x79, o2_doSentence);
	OPCODE(0x7a, o2_verbOps);
	OPCODE(0x7b, o2_getActorWalkBox);
	/* 7C */
	OPCODE(0x7c, o5_isSoundRunning);
	OPCODE(0x7d, o2_setActorElevation);
	OPCODE(0x7e, o2_walkActorTo);
	OPCODE(0x7f, o2_ifState01);
	/* 80 */
	OPCODE(0x80, o5_breakHere);
	OPCODE(0x81, o2_putActor);
	OPCODE(0x82, o5_startMusic);
	OPCODE(0x83, o5_getActorRoom);
	/* 84 */
	OPCODE(0x84, o2_isGreaterEqual);
	OPCODE(0x85, o2_drawObject);
	OPCODE(0x86, o2_getActorElevation);
	OPCODE(0x87, o2_setState08);
	/* 88 */
	OPCODE(0x88, o5_isNotEqual);
	OPCODE(0x89, o5_faceActor);
	OPCODE(0x8a, o2_assignVarWordIndirect);
	OPCODE(0x8b, o2_setObjPreposition);
	/* 8C */
	OPCODE(0x8c, o2_resourceRoutines);
	OPCODE(0x8d, o5_walkActorToActor);
	OPCODE(0x8e, o2_putActorAtObject);
	OPCODE(0x8f, o2_ifNotState08);
	/* 90 */
	OPCODE(0x90, o5_getObjectOwner);
	OPCODE(0x91, o5_animateActor);
	OPCODE(0x92, o2_panCameraTo);
	OPCODE(0x93, o2_actorOps);
	/* 94 */
	OPCODE(0x94, o5_print);
	OPCODE(0x95, o2_actorFromPos);
	OPCODE(0x96, o5_getRandomNr);
	OPCODE(0x97, o2_clearState02);
	/* 98 */
	OPCODE(0x98, o2_restart);
	OPCODE(0x99, o2_doSentence);
	OPCODE(0x9a, o5_move);
	OPCODE(0x9b, o2_setBitVar);
	/* 9C */
	OPCODE(0x9c, o5_startSound);
	OPCODE(0x9d, o2_ifClassOfIs);
	OPCODE(0x9e, o2_walkActorTo);
	OPCODE(0x9f, o2_ifState02);
	/* A0 */
	OPCODE(0xa0, o5_stopObjectCode);
	OPCODE(0xa1, o2_putActor);
	OPCODE(0xa2, o4_saveLoadGame);
	OPCODE(0xa3, o2_getActorY);
	/* A4 */
	OPCODE(0xa4, o2_loadRoomWithEgo);
	OPCODE(0xa5, o2_drawObject);
	OPCODE(0xa6, o5_setVarRange);
	OPCODE(0xa7, o2_setState04);
	/* A8 */
	OPCODE(0xa8, o5_notEqualZero);
	OPCODE(0xa9, o2_setOwnerOf);
	OPCODE(0xaa, o2_addIndirect);
	OPCODE(0xab, o2_switchCostumeSet);
	/* AC */
	OPCODE(0xac, o2_drawSentence);
	OPCODE(0xad, o2_putActorInRoom);
	OPCODE(0xae, o2_waitForMessage);
	OPCODE(0xaf, o2_ifNotState04);
	/* B0 */
	OPCODE(0xb0, o3_setBoxFlags);
	OPCODE(0xb1, o2_getBitVar);
	OPCODE(0xb2, o2_setCameraAt);
	OPCODE(0xb3, o2_roomOps);
	/* B4 */
	OPCODE(0xb4, o5_getDist);
	OPCODE(0xb5, o2_findObject);
	OPCODE(0xb6, o2_walkActorToObject);
	OPCODE(0xb7, o2_setState01);
	/* B8 */
	OPCODE(0xb8, o2_isLessEqual);
	OPCODE(0xb9, o2_doSentence);
	OPCODE(0xba, o2_subtract);
	OPCODE(0xbb, o2_waitForActor);
	/* BC */
	OPCODE(0xbc, o5_stopSound);
	OPCODE(0xbd, o2_setActorElevation);
	OPCODE(0xbe, o2_walkActorTo);
	OPCODE(0xbf, o2_ifNotState01);
	/* C0 */
	OPCODE(0xc0, o2_endCutscene);
	OPCODE(0xc1, o2_putActor);
	OPCODE(0xc2, o2_startScript);
	OPCODE(0xc3, o2_getActorX);
	/* C4 */
	OPCODE(0xc4, o2_isLess);
	OPCODE(0xc5, o2_drawObject);
	OPCODE(0xc6, o5_decrement);
	OPCODE(0xc7, o2_clearState08);
	/* C8 */
	OPCODE(0xc8, o5_isEqual);
	OPCODE(0xc9, o5_faceActor);
	OPCODE(0xca, o2_chainScript);
	OPCODE(0xcb, o2_setObjPreposition);
	/* CC */
	OPCODE(0xcc, o5_pseudoRoom);
	OPCODE(0xcd, o5_walkActorToActor);
	OPCODE(0xce, o2_putActorAtObject);
	OPCODE(0xcf, o2_ifState08);
	/* D0 */
	OPCODE(0xd0, o2_pickupObject);
	OPCODE(0xd1, o5_animateActor);
	OPCODE(0xd2, o5_actorFollowCamera);
	OPCODE(0xd3, o2_actorOps);
	/* D4 */
	OPCODE(0xd4, o5_setObjectName);
	OPCODE(0xd5, o2_actorFromPos);
	OPCODE(0xd6, o5_getActorMoving);
	OPCODE(0xd7, o2_setState02);
	/* D8 */
	OPCODE(0xd8, o5_printEgo);
	OPCODE(0xd9, o2_doSentence);
	OPCODE(0xda, o2_add);
	OPCODE(0xdb, o2_setBitVar);
	/* DC */
	OPCODE(0xdc, o2_dummy);
	OPCODE(0xdd, o2_ifClassOfIs);
	OPCODE(0xde, o2_walkActorTo);
	OPCODE(0xdf, o2_ifNotState02);
	/* E0 */
	OPCODE(0xe0, o2_cursorCommand);
	OPCODE(0xe1, o2_putActor);
	OPCODE(0xe2, o2_stopScript);
	OPCODE(0xe3, o5_getActorFacing);
	/* E4 */
	OPCODE(0xe4, o2_loadRoomWithEgo);
	OPCODE(0xe5, o2_drawObject);
	OPCODE(0xe6, o5_getClosestObjActor);
	OPCODE(0xe7, o2_clearState04);
	/* E8 */
	OPCODE(0xe8, o5_isScriptRunning);
	OPCODE(0xe9, o2_setOwnerOf);
	OPCODE(0xea, o2_subIndirect);
	OPCODE(0xeb, o2_dummy);
	/* EC */
	OPCODE(0xec, o2_getObjPreposition);
	OPCODE(0xed, o2_putActorInRoom);
	OPCODE(0xee, o2_dummy);
	OPCODE(0xef, o2_ifState04);
	/* F0 */
	OPCODE(0xf0, o2_lights);
	OPCODE(0xf1, o5_getActorCostume);
	OPCODE(0xf2, o5_loadRoom);
	OPCODE(0xf3, o2_roomOps);
	/* F4 */
	OPCODE(0xf4, o5_getDist);
	OPCODE(0xf5, o2_findObject);
	OPCODE(0xf6, o2_walkActorToObject);
	OPCODE(0xf7, o2_clearState01);
	/* F8 */
	OPCODE(0xf8, o2_isGreater);
	OPCODE(0xf9, o2_doSentence);
	OPCODE(0xfa, o2_verbOps);
	OPCODE(0xfb, o2_getActorWalkBox);
	/* FC */
	OPCODE(0xfc, o5_isSoundRunning);
	OPCODE(0xfd, o2_setActorElevation);
	OPCODE(0xfe, o2_walkActorTo);
	OPCODE(0xff, o2_ifState01);
}

#define SENTENCE_SCRIPT 2

int ScummEngine_v2::getVar() {
	return readVar(fetchScriptByte());
}

void ScummEngine_v2::decodeParseString() {
	byte buffer[512];
	byte *ptr = buffer;
	byte c;
	bool insertSpace = false;

	while ((c = fetchScriptByte())) {

		insertSpace = (c & 0x80) != 0;
		c &= 0x7f;

		if (c < 8) {
			// Special codes as seen in CHARSET_1 etc. My guess is that they
			// have a similar function as the corresponding embedded stuff in modern
			// games. Hence for now we convert them to the modern format.
			// This might allow us to reuse the existing code.
			*ptr++ = 0xFF;
			*ptr++ = c;
			if (c > 3) {
				*ptr++ = fetchScriptByte();
				*ptr++ = 0;
			}
		} else
			*ptr++ = c;

		if (insertSpace)
			*ptr++ = ' ';

	}
	*ptr = 0;

	int textSlot = 0;
	_string[textSlot].xpos = 0;
	_string[textSlot].ypos = 0;
	_string[textSlot].right = _screenWidth - 1;
	_string[textSlot].center = false;
	_string[textSlot].overhead = false;

	if (_game.id == GID_MANIAC && _actorToPrintStrFor == 0xFF) {
		if (_game.version == 0) {
			_string[textSlot].color = 14;
		} else if (_game.features & GF_DEMO) {
			_string[textSlot].color = (_game.version == 2) ? 15 : 1;
		}
	}

	actorTalk(buffer);
}

int ScummEngine_v2::readVar(uint var) {
	if (_game.version >= 1 && var >= 14 && var <= 16)
		var = _scummVars[var];

	assertRange(0, var, _numVariables - 1, "variable (reading)");
	debugC(DEBUG_VARS, "readvar(%d) = %d", var, _scummVars[var]);
	return _scummVars[var];
}

void ScummEngine_v2::writeVar(uint var, int value) {
	assertRange(0, var, _numVariables - 1, "variable (writing)");
	debugC(DEBUG_VARS, "writeVar(%d) = %d", var, value);

	if (VAR_CUTSCENEEXIT_KEY != 0xFF && var == VAR_CUTSCENEEXIT_KEY) {
		// Remap the cutscene exit key in earlier games
		if (value == 4 || value == 13 || value == 64)
			value = 27;
	}

	_scummVars[var] = value;
}

void ScummEngine_v2::getResultPosIndirect() {
	_resultVarNumber = _scummVars[fetchScriptByte()];
}

void ScummEngine_v2::getResultPos() {
	_resultVarNumber = fetchScriptByte();
}

int ScummEngine_v2::getActiveObject() {
	return getVarOrDirectWord(PARAM_1);
}

void ScummEngine_v2::setStateCommon(byte type) {
	int obj = getActiveObject();
	putState(obj, getState(obj) | type);
}

void ScummEngine_v2::clearStateCommon(byte type) {
	int obj = getActiveObject();
	putState(obj, getState(obj) & ~type);
}

void ScummEngine_v2::ifStateCommon(byte type) {
	int obj = getActiveObject();

	jumpRelative((getState(obj) & type) != 0);
}

void ScummEngine_v2::ifNotStateCommon(byte type) {
	int obj = getActiveObject();

	jumpRelative((getState(obj) & type) == 0);
}

void ScummEngine_v2::o2_setState08() {
	int obj = getActiveObject();
	putState(obj, getState(obj) | kObjectState_08);
	markObjectRectAsDirty(obj);
	clearDrawObjectQueue();
}

void ScummEngine_v2::o2_clearState08() {
	int obj = getActiveObject();
	putState(obj, getState(obj) & ~kObjectState_08);
	markObjectRectAsDirty(obj);
	clearDrawObjectQueue();
}

void ScummEngine_v2::o2_setState04() {
	setStateCommon(kObjectStateLocked);
}

void ScummEngine_v2::o2_clearState04() {
	clearStateCommon(kObjectStateLocked);
}

void ScummEngine_v2::o2_setState02() {
	setStateCommon(kObjectStateUntouchable);
}

void ScummEngine_v2::o2_clearState02() {
	clearStateCommon(kObjectStateUntouchable);
}

void ScummEngine_v2::o2_setState01() {
	setStateCommon(kObjectStatePickupable);
}

void ScummEngine_v2::o2_clearState01() {
	clearStateCommon(kObjectStatePickupable);
}

void ScummEngine_v2::o2_assignVarWordIndirect() {
	getResultPosIndirect();
	setResult(getVarOrDirectWord(PARAM_1));
}

void ScummEngine_v2::o2_assignVarByte() {
	getResultPos();
	setResult(fetchScriptByte());
}

void ScummEngine_v2::o2_setObjPreposition() {
	int obj = getVarOrDirectWord(PARAM_1);
	int unk = fetchScriptByte();

	if (_game.platform == Common::kPlatformNES)
		return;

	if (whereIsObject(obj) != WIO_NOT_FOUND) {
		// FIXME: this might not work properly the moment we save and restore the game.
		byte *ptr = getOBCDFromObject(obj) + 12;
		*ptr &= 0x1F;
		*ptr |= unk << 5;
	}
}

void ScummEngine_v2::o2_getObjPreposition() {
	getResultPos();
	int obj = getVarOrDirectWord(PARAM_1);

	if (whereIsObject(obj) != WIO_NOT_FOUND) {
		byte *ptr = getOBCDFromObject(obj) + 12;
		setResult(*ptr >> 5);
	} else {
		setResult(0xFF);
	}
}

void ScummEngine_v2::o2_setBitVar() {
	int var = fetchScriptWord();
	byte a = getVarOrDirectByte(PARAM_1);

	int bit_var = var + a;
	int bit_offset = bit_var & 0x0f;
	bit_var >>= 4;

	if (getVarOrDirectByte(PARAM_2))
		_scummVars[bit_var] |= (1 << bit_offset);
	else
		_scummVars[bit_var] &= ~(1 << bit_offset);

}

void ScummEngine_v2::o2_getBitVar() {
	getResultPos();
	int var = fetchScriptWord();
	byte a = getVarOrDirectByte(PARAM_1);

	int bit_var = var + a;
	int bit_offset = bit_var & 0x0f;
	bit_var >>= 4;

	setResult((_scummVars[bit_var] & (1 << bit_offset)) ? 1 : 0);
}

void ScummEngine_v2::o2_ifState08() {
	ifStateCommon(kObjectState_08);
}

void ScummEngine_v2::o2_ifNotState08() {
	ifNotStateCommon(kObjectState_08);
}

void ScummEngine_v2::o2_ifState04() {
	ifStateCommon(kObjectStateLocked);
}

void ScummEngine_v2::o2_ifNotState04() {
	ifNotStateCommon(kObjectStateLocked);
}

void ScummEngine_v2::o2_ifState02() {
	ifStateCommon(kObjectStateUntouchable);
}

void ScummEngine_v2::o2_ifNotState02() {
	ifNotStateCommon(kObjectStateUntouchable);
}

void ScummEngine_v2::o2_ifState01() {
	ifStateCommon(kObjectStatePickupable);
}

void ScummEngine_v2::o2_ifNotState01() {
	ifNotStateCommon(kObjectStatePickupable);
}

void ScummEngine_v2::o2_addIndirect() {
	int a;
	getResultPosIndirect();
	a = getVarOrDirectWord(PARAM_1);
	_scummVars[_resultVarNumber] += a;
}

void ScummEngine_v2::o2_subIndirect() {
	int a;
	getResultPosIndirect();
	a = getVarOrDirectWord(PARAM_1);
	_scummVars[_resultVarNumber] -= a;
}

void ScummEngine_v2::o2_add() {
	int a;
	getResultPos();
	a = getVarOrDirectWord(PARAM_1);
	_scummVars[_resultVarNumber] += a;
}

void ScummEngine_v2::o2_subtract() {
	int a;
	getResultPos();
	a = getVarOrDirectWord(PARAM_1);
	_scummVars[_resultVarNumber] -= a;
}

void ScummEngine_v2::o2_waitForActor() {
	Actor *a = derefActor(getVarOrDirectByte(PARAM_1), "o2_waitForActor");
	if (a->_moving) {
		_scriptPointer -= 2;
		o5_breakHere();
	}
}

void ScummEngine_v2::o2_waitForMessage() {
	if (VAR(VAR_HAVE_MSG)) {
		_scriptPointer--;
		o5_breakHere();
	}
}

void ScummEngine_v2::o2_waitForSentence() {
	if (!_sentenceNum && !isScriptInUse(SENTENCE_SCRIPT))
		return;

	_scriptPointer--;
	o5_breakHere();
}

void ScummEngine_v2::o2_actorOps() {
	int act = getVarOrDirectByte(PARAM_1);
	int arg = getVarOrDirectByte(PARAM_2);
	Actor *a;
	int i;

	_opcode = fetchScriptByte();
	if (act == 0 && _opcode == 5) {
		// This case happens in the Zak/MM bootscripts, to set the default talk color (9).
		_string[0].color = arg;
		return;
	}

	a = derefActor(act, "actorOps");

	switch (_opcode) {
	case 1:		// SO_SOUND
		a->_sound[0] = arg;
		break;
	case 2:		// SO_PALETTE
		if (_game.version == 1)
			i = act;
		else
			i = fetchScriptByte();

		a->setPalette(i, arg);
		break;
	case 3:		// SO_ACTOR_NAME
		loadPtrToResource(rtActorName, a->_number, NULL);
		break;
	case 4:		// SO_COSTUME
		a->setActorCostume(arg);
		break;
	case 5:		// SO_TALK_COLOR
		if (_game.id == GID_MANIAC && _game.version == 2 && (_game.features & GF_DEMO) && arg == 1)
			a->_talkColor = 15;
		else
			a->_talkColor = arg;
		break;
	default:
		error("o2_actorOps: opcode %d not yet supported", _opcode);
	}
}

void ScummEngine_v2::o2_restart() {
	restart();
}

void ScummEngine_v2::o2_drawObject() {
	int obj, idx, i;
	ObjectData *od;
	uint16 x, y, w, h;
	int xpos, ypos;

	obj = getVarOrDirectWord(PARAM_1);
	xpos = getVarOrDirectByte(PARAM_2);
	ypos = getVarOrDirectByte(PARAM_3);

	idx = getObjectIndex(obj);
	if (idx == -1)
		return;

	od = &_objs[idx];
	if (xpos != 0xFF) {
		od->walk_x += (xpos * 8) - od->x_pos;
		od->x_pos = xpos * 8;
		od->walk_y += (ypos * 8) - od->y_pos;
		od->y_pos = ypos * 8;
	}
	addObjectToDrawQue(idx);

	x = od->x_pos;
	y = od->y_pos;
	w = od->width;
	h = od->height;

	i = _numLocalObjects;
	while (i--) {
		if (_objs[i].obj_nr && _objs[i].x_pos == x && _objs[i].y_pos == y && _objs[i].width == w && _objs[i].height == h)
			putState(_objs[i].obj_nr, getState(_objs[i].obj_nr) & ~kObjectState_08);
	}

	putState(obj, getState(od->obj_nr) | kObjectState_08);
}

void ScummEngine_v2::o2_resourceRoutines() {
	const ResType resTypes[] = {
		rtInvalid,
		rtInvalid,
		rtCostume,
		rtRoom,
		rtInvalid,
		rtScript,
		rtSound
	};
	int resid = getVarOrDirectByte(PARAM_1);
	int opcode = fetchScriptByte();

	ResType type = rtInvalid;
	if (0 <= (opcode >> 4) && (opcode >> 4) < (int)ARRAYSIZE(resTypes))
		type = resTypes[opcode >> 4];

	if ((opcode & 0x0f) == 0 || type == rtInvalid)
		return;

	// HACK V2 Maniac Mansion tries to load an invalid sound resource in demo script.
	if (_game.id == GID_MANIAC && _game.version == 2 && vm.slot[_currentScript].number == 9 && type == rtSound && resid == 1)
		return;

	if ((opcode & 0x0f) == 1) {
		ensureResourceLoaded(type, resid);
	} else {
		if (opcode & 1)
			_res->lock(type, resid);
		else
			_res->unlock(type, resid);
	}
}

void ScummEngine_v2::o2_verbOps() {
	int verb = fetchScriptByte();
	int slot, state;

	switch (verb) {
	case 0:		// SO_DELETE_VERBS
		slot = getVarOrDirectByte(PARAM_1) + 1;
		assert(0 < slot && slot < _numVerbs);
		killVerb(slot);
		break;

	case 0xFF:	// Verb On/Off
		verb = fetchScriptByte();
		state = fetchScriptByte();
		slot = getVerbSlot(verb, 0);
		_verbs[slot].curmode = state;
		break;

	default: {	// New Verb
		int x = fetchScriptByte() * 8;
		int y = fetchScriptByte() * 8;
		slot = getVarOrDirectByte(PARAM_1) + 1;
		int prep = fetchScriptByte(); // Only used in V1?
		// V1 Maniac verbs are relative to the 'verb area' - under the sentence
		if (_game.platform == Common::kPlatformNES)
			x += 8;
		else if ((_game.id == GID_MANIAC) && (_game.version == 1))
			y += 8;

		VerbSlot *vs;
		assert(0 < slot && slot < _numVerbs);

		vs = &_verbs[slot];
		vs->verbid = verb;
		if (_game.platform == Common::kPlatformNES) {
			vs->color = 1;
			vs->hicolor = 1;
			vs->dimcolor = 1;
		} else if (_game.version == 1) {
			vs->color = (_game.id == GID_MANIAC && (_game.features & GF_DEMO)) ? 16 : 5;
			vs->hicolor = 7;
			vs->dimcolor = 11;
		} else {
			vs->color = (_game.id == GID_MANIAC && (_game.features & GF_DEMO)) ? 13 : 2;
			vs->hicolor = 14;
			vs->dimcolor = 8;
		}
		vs->type = kTextVerbType;
		vs->charset_nr = _string[0]._default.charset;
		vs->curmode = 1;
		vs->saveid = 0;
		vs->key = 0;
		vs->center = 0;
		vs->imgindex = 0;
		vs->prep = prep;

		vs->curRect.left = x;
		vs->curRect.top = y;

		// FIXME: these keyboard map depends on the language of the game.
		// E.g. a german keyboard has 'z' and 'y' swapped, while a french
		// keyboard starts with "azerty", etc.
		if (_game.platform == Common::kPlatformNES) {
			static const char keyboard[] = {
					'q','w','e','r',
					'a','s','d','f',
					'z','x','c','v'
				};
			if (1 <= slot && slot <= ARRAYSIZE(keyboard))
				vs->key = keyboard[slot - 1];
		} else {
			static const char keyboard[] = {
					'q','w','e','r','t',
					'a','s','d','f','g',
					'z','x','c','v','b'
				};
			if (1 <= slot && slot <= ARRAYSIZE(keyboard))
				vs->key = keyboard[slot - 1];
		}

		// It follows the verb name
		loadPtrToResource(rtVerb, slot, NULL);
		}
		break;
	}

	// Force redraw of the modified verb slot
	drawVerb(slot, 0);
	verbMouseOver(0);
}

void ScummEngine_v2::o2_doSentence() {
	int a;
	SentenceTab *st;

	a = getVarOrDirectByte(PARAM_1);
	if (a == 0xFC) {
		_sentenceNum = 0;
		stopScript(SENTENCE_SCRIPT);
		return;
	}
	if (a == 0xFB) {
		resetSentence();
		return;
	}

	assert(_sentenceNum < NUM_SENTENCE);
	st = &_sentence[_sentenceNum++];

	st->verb = a;
	st->objectA = getVarOrDirectWord(PARAM_2);
	st->objectB = getVarOrDirectWord(PARAM_3);
	st->preposition = (st->objectB != 0);
	st->freezeCount = 0;

	// Execute or print the sentence
	_opcode = fetchScriptByte();
	switch (_opcode) {
	case 0:
		// Do nothing (besides setting up the sentence above)
		break;
	case 1:
		// Execute the sentence
		_sentenceNum--;

		if (st->verb == 254) {
			ScummEngine::stopObjectScript(st->objectA);
		} else {
			bool isBackgroundScript;
			bool isSpecialVerb;
			if (st->verb != 253 && st->verb != 250) {
				VAR(VAR_ACTIVE_VERB) = st->verb;
				VAR(VAR_ACTIVE_OBJECT1) = st->objectA;
				VAR(VAR_ACTIVE_OBJECT2) = st->objectB;

				isBackgroundScript = false;
				isSpecialVerb = false;
			} else {
				isBackgroundScript = (st->verb == 250);
				isSpecialVerb = true;
				st->verb = 253;
			}

			// Check if an object script for this object is already running. If
			// so, reuse its script slot. Note that we abuse two script flags:
			// freezeResistant and recursive. We use them to track two
			// script flags used in V1/V2 games. The main reason we do it this
			// ugly evil way is to avoid having to introduce yet another save
			// game revision.
			int slot = -1;
			ScriptSlot *ss;
			int i;

			ss = vm.slot;
			for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++) {
				if (st->objectA == ss->number &&
					ss->freezeResistant == isBackgroundScript &&
					ss->recursive == isSpecialVerb &&
					(ss->where == WIO_ROOM || ss->where == WIO_INVENTORY || ss->where == WIO_FLOBJECT)) {
					slot = i;
					break;
				}
			}

			runObjectScript(st->objectA, st->verb, isBackgroundScript, isSpecialVerb, NULL, slot);
		}
		break;
	case 2:
		// Print the sentence
		_sentenceNum--;

		VAR(VAR_SENTENCE_VERB) = st->verb;
		VAR(VAR_SENTENCE_OBJECT1) = st->objectA;
		VAR(VAR_SENTENCE_OBJECT2) = st->objectB;

		o2_drawSentence();
		break;
	default:
		error("o2_doSentence: unknown subopcode %d", _opcode);
	}
}

void ScummEngine_v2::drawPreposition(int index) {
		// The prepositions, like the fonts, were hard code in the engine. Thus
		// we have to do that, too, and provde localized versions for all the
		// languages MM/Zak are available in.
		const char *prepositions[][5] = {
			{ " ", " in", " with", " on", " to" },   // English
			{ " ", " mit", " mit", " mit", " zu" },  // German
			{ " ", " dans", " avec", " sur", " <" }, // French
			{ " ", " in", " con", " su", " a" },     // Italian
			{ " ", " en", " con", " en", " a" },     // Spanish
			{ " ", " \x7f", " \x7f", " na", " \x7f" },// Russian
			};
		int lang;
		switch (_language) {
		case Common::DE_DEU:
			lang = 1;
			break;
		case Common::FR_FRA:
			lang = 2;
			break;
		case Common::IT_ITA:
			lang = 3;
			break;
		case Common::ES_ESP:
			lang = 4;
			break;
		case Common::RU_RUS:
			lang = 5;
			break;
		default:
			lang = 0;	// Default to english
		}

		if (_game.platform == Common::kPlatformNES) {
			_sentenceBuf += (const char *)(getResourceAddress(rtCostume, 78) + VAR(VAR_SENTENCE_PREPOSITION) * 8 + 2);
		} else
			_sentenceBuf += prepositions[lang][index];
}

void ScummEngine_v2::o2_drawSentence() {
	Common::Rect sentenceline;
	const byte *temp;
	int slot = getVerbSlot(VAR(VAR_SENTENCE_VERB), 0);

	if (!((_userState & USERSTATE_IFACE_SENTENCE) ||
	      (_game.platform == Common::kPlatformNES && (_userState & USERSTATE_IFACE_ALL))))
		return;

	if (getResourceAddress(rtVerb, slot))
		_sentenceBuf = (char *)getResourceAddress(rtVerb, slot);
	else
		return;

	if (VAR(VAR_SENTENCE_OBJECT1) > 0) {
		temp = getObjOrActorName(VAR(VAR_SENTENCE_OBJECT1));
		if (temp) {
			_sentenceBuf += " ";
			_sentenceBuf += (const char *)temp;
		}

		// For V1 games, the engine must compute the preposition.
		// In all other Scumm versions, this is done by the sentence script.
		if ((_game.id == GID_MANIAC && _game.version == 1 && !(_game.platform == Common::kPlatformNES)) && (VAR(VAR_SENTENCE_PREPOSITION) == 0)) {
			if (_verbs[slot].prep == 0xFF) {
				byte *ptr = getOBCDFromObject(VAR(VAR_SENTENCE_OBJECT1));
				assert(ptr);
				VAR(VAR_SENTENCE_PREPOSITION) = (*(ptr + 12) >> 5);
			} else
				VAR(VAR_SENTENCE_PREPOSITION) = _verbs[slot].prep;
		}
	}

	if (0 < VAR(VAR_SENTENCE_PREPOSITION) && VAR(VAR_SENTENCE_PREPOSITION) <= 4) {
		drawPreposition(VAR(VAR_SENTENCE_PREPOSITION));
	}

	if (VAR(VAR_SENTENCE_OBJECT2) > 0) {
		temp = getObjOrActorName(VAR(VAR_SENTENCE_OBJECT2));
		if (temp) {
			_sentenceBuf += " ";
			_sentenceBuf += (const char *)temp;
		}
	}

	_string[2].charset = 1;
	_string[2].ypos = _virtscr[kVerbVirtScreen].topline;
	_string[2].xpos = 0;
	_string[2].right = _virtscr[kVerbVirtScreen].w - 1;
	if (_game.platform == Common::kPlatformNES) {
		_string[2].xpos = 16;
		_string[2].color = 0;
	} else if (_game.version == 1)
		_string[2].color = 16;
	else
		_string[2].color = 13;

	byte string[80];
	const char *ptr = _sentenceBuf.c_str();
	int i = 0, len = 0;

	// Maximum length of printable characters
	int maxChars = (_game.platform == Common::kPlatformNES) ? 60 : 40;
	while (*ptr) {
		if (*ptr != '@')
			len++;
		if (len > maxChars) {
			break;
		}

		string[i++] = *ptr++;

		if (_game.platform == Common::kPlatformNES && len == 30) {
			string[i++] = 0xFF;
			string[i++] = 8;
		}
	}
	string[i] = 0;

	if (_game.platform == Common::kPlatformNES) {
		sentenceline.top = _virtscr[kVerbVirtScreen].topline;
		sentenceline.bottom = _virtscr[kVerbVirtScreen].topline + 16;
		sentenceline.left = 16;
		sentenceline.right = _virtscr[kVerbVirtScreen].w - 1;
	} else {
		sentenceline.top = _virtscr[kVerbVirtScreen].topline;
		sentenceline.bottom = _virtscr[kVerbVirtScreen].topline + 8;
		sentenceline.left = 0;
		sentenceline.right = _virtscr[kVerbVirtScreen].w - 1;
	}
	restoreBackground(sentenceline);

	drawString(2, (byte *)string);
}

void ScummEngine_v2::o2_ifClassOfIs() {
	int obj = getVarOrDirectWord(PARAM_1);
	int clsop = getVarOrDirectByte(PARAM_2);


	byte *obcd = getOBCDFromObject(obj);

	if (obcd == 0) {
		o5_jumpRelative();
		return;
	}

	byte cls = *(obcd + 6);
	jumpRelative((cls & clsop) == clsop);
}

void ScummEngine_v2::o2_walkActorTo() {
	int x, y;
	Actor *a;

	int act = getVarOrDirectByte(PARAM_1);

	// WORKAROUND bug #1252606
	if (_game.id == GID_ZAK && _game.version == 1 && vm.slot[_currentScript].number == 115 && act == 249) {
		act = VAR(VAR_EGO);
	}

	a = derefActor(act, "o2_walkActorTo");

	x = getVarOrDirectByte(PARAM_2);
	y = getVarOrDirectByte(PARAM_3);

	a->startWalkActor(x, y, -1);
}

void ScummEngine_v2::o2_putActor() {
	int act = getVarOrDirectByte(PARAM_1);
	int x, y;
	Actor *a;

	a = derefActor(act, "o2_putActor");
	x = getVarOrDirectByte(PARAM_2);
	y = getVarOrDirectByte(PARAM_3);

	a->putActor(x, y);
}

void ScummEngine_v2::o2_startScript() {
	int script = getVarOrDirectByte(PARAM_1);

	if (!_copyProtection) {
		// The enhanced version of Zak McKracken included in the
		// SelectWare Classic Collection bundle used CD check instead
		// of the usual key code check at airports.
		if ((_game.id == GID_ZAK) && (script == 15) && (_roomResource == 45))
			return;
	}

	// WORKAROUND bug #1447058: In Maniac Mansion, when the door bell
	// rings, then this normally causes Ted Edison to leave his room.
	// This is controlled by script 87. On the other hand, when the
	// player enters Ted's room while Ted is in it, then Ted captures
	// the player and puts his active ego into the cellar prison.
	//
	// Unfortunately, the two events can collide: If the cutscene is
	// playing in which Ted captures the player (controlled by script
	// 88) and simultaneously the door bell rings (due to package
	// delivery...) then this leads to an assertion (in ScummVM, due to
	// its stricter validity checking), or to unexpected / strange
	// behavior (in the original engine). The script writers apparently
	// anticipated the possibility of the door bell ringing: Before
	// script 91 starts script 88, it explicitly stops script 87.
	// Unfortunately, this is not quite enough, as script 87 can be
	// started while script 88 is already running -- specifically, by
	// the package delivery sequence.
	//
	// Now, one can easily suppress this particular assertion, but then
	// one still gets odd behavior: Ted is in the process of
	// incarcerating the player, when the door bell rings; Ted promptly
	// leaves to get the package, leaving the player alone (!), but then
	// moments later we cut to the cellar, where Ted just put the
	// player. That seems weird and irrational (the Edisons may be mad,
	// but they are not stupid when it comes to putting people into
	// their dungeon ;)
	//
	// To avoid this, we use a somewhat more elaborate workaround: If
	// script 88 or 89 are running (which control the capture resp.
	// imprisonment of the player), then any attempt to start script 87
	// (which makes Ted go answer the door bell) is simply ignored. This
	// way, the door bell still chimes, but Ted ignores it.
	if (_game.id == GID_MANIAC) {
		if (script == MM_SCRIPT(82)) {
			if (isScriptRunning(MM_SCRIPT(83)) || isScriptRunning(MM_SCRIPT(84)))
				return;
		}
	}

    // WORKAROUND bug #4556: Purple Tentacle can appear in the lab, after being
    // chased out and end up stuck in the room. This bug is triggered if the player
    // enters the lab within 45 minutes of first entering the mansion and has chased Purple Tentacle
    // out. Eventually the cutscene with Purple Tentacle chasing Sandy in the lab
    // will play. This script leaves Purple Tentacle in the room causing him to become
    // a permanent resident.
    // Our fix is simply to prevent the Cutscene playing, if the lab has already been stormed
    if (_game.id == GID_MANIAC) {
        if (_game.version >= 1 && script == 155) {
            if (VAR(120) == 1)
                return;
        }
        // Script numbers are different in V0
        if (_game.version == 0 && script == 150) {
            if (VAR(104) == 1)
                return;
        }
    }

	runScript(script, 0, 0, 0);
}

void ScummEngine_v2::stopScriptCommon(int script) {
    // WORKAROUND bug #4112: If you enter the lab while Dr. Fred has the powered turned off
    // to repair the Zom-B-Matic, the script will be stopped and the power will never turn
    // back on. This fix forces the power on, when the player enters the lab,
    // if the script which turned it off is running
    if (_game.id == GID_MANIAC && _roomResource == 4 && isScriptRunning(MM_SCRIPT(138))) {

        if (vm.slot[_currentScript].number == MM_VALUE(130, 163)) {

            if (script == MM_SCRIPT(138)) {

                int obj = MM_VALUE(124, 157);
                putState(obj, getState(obj) & ~kObjectState_08);
            }
        }
    }

	if (_game.id == GID_MANIAC && _roomResource == 26 && vm.slot[_currentScript].number == 10001) {
	// FIXME: Nasty hack for bug #915575
	// Don't let the exit script for room 26 stop the script (116), when
	// switching to the dungeon (script 89)
		if (script == MM_SCRIPT(111) && isScriptRunning(MM_SCRIPT(84)))
			return;
	}

	if (script == 0)
		script = vm.slot[_currentScript].number;

	if (_currentScript != 0 && vm.slot[_currentScript].number == script)
		stopObjectCode();
	else
		stopScript(script);
}

void ScummEngine_v2::o2_stopScript() {
	stopScriptCommon(getVarOrDirectByte(PARAM_1));
}

void ScummEngine_v2::o2_panCameraTo() {
	panCameraTo(getVarOrDirectByte(PARAM_1) * V12_X_MULTIPLIER, 0);
}

void ScummEngine_v2::walkActorToObject(int actor, int obj) {
	int x, y, dir;
	getObjectXYPos(obj, x, y, dir);

	Actor *a = derefActor(actor, "walkActorToObject");
	AdjustBoxResult r = a->adjustXYToBeInBox(x, y);
	x = r.x;
	y = r.y;

	a->startWalkActor(x, y, dir);
}

void ScummEngine_v2::o2_walkActorToObject() {
	int actor = getVarOrDirectByte(PARAM_1);
	int obj = getVarOrDirectWord(PARAM_2);
	if (whereIsObject(obj) != WIO_NOT_FOUND) {
		walkActorToObject(actor, obj);
	}
}

void ScummEngine_v2::o2_putActorAtObject() {
	int obj, x, y;
	Actor *a;

	a = derefActor(getVarOrDirectByte(PARAM_1), "o2_putActorAtObject");
	obj = getVarOrDirectWord(PARAM_2);
	if (whereIsObject(obj) != WIO_NOT_FOUND) {
		getObjectXYPos(obj, x, y);
		AdjustBoxResult r = a->adjustXYToBeInBox(x, y);
		x = r.x;
		y = r.y;
	} else {
		x = 30;
		y = 60;
	}

	a->putActor(x, y);
}

void ScummEngine_v2::o2_putActorInRoom() {
	Actor *a;
	int act = getVarOrDirectByte(PARAM_1);
	int room = getVarOrDirectByte(PARAM_2);

	a = derefActor(act, "o2_putActorInRoom");

	a->_room = room;
	if (!room) {
		if (_game.id == GID_MANIAC && _game.version <= 1 && _game.platform != Common::kPlatformNES)
			a->setFacing(180);

		a->putActor(0, 0, 0);
	}

	// WORKAROUND bug #2285: Caponians dont disguise after using blue crystal
	// This is for a game scripting oversight.
	// After first using the blue crystal, a cutscene of the two Caponians plays (script-96),
	// locking object 344 (which prevents the cutscene playing again) and setting Var[245] to 0x18.
	// script-5 uses this variable to set the Caponian costume
	// On first apperance after using the blue crystal, the Caponians now will have the disguise on
	//
	// If you visit the spacecraft and ring the doorbell, Var[245] will be set to 0x1C (Disguise off)
	// Using the blue crystal again, will result in the Caponian appearing without his disguise
	// as Var[245] is never set back to 0x18. This WORKAROUND fixes the problem by ensuring
	// Var[245] is set to have the Disguise on in most situations
	//
	// We don't touch the variable in the following situations
	//  If the Caponian is being put into the space ship room, or the current room is the
	//  space ship and the Caponian is being put into the backroom of the telephone company (you didnt show your fan club card)
	if (_game.id == GID_ZAK && _game.version <= 2 && act == 7) {
		// Is script-96 cutscene done
		if ((getState(344) & kObjectStateLocked)) {
			// Not 'putting' in the space ship
			if (room != 10) {
				// not putting in telephone back room, and not in space ship
				if (room != 16 && _currentRoom != 10) {
					// Set caponian costume to 'disguise on'
					writeVar(245, 0x18);
				}
			}
		}
	}
}

void ScummEngine_v2::o2_getActorElevation() {
	getResultPos();
	int act = getVarOrDirectByte(PARAM_1);
	Actor *a = derefActor(act, "o2_getActorElevation");
	setResult(a->getElevation());
}

void ScummEngine_v2::o2_setActorElevation() {
	int act = getVarOrDirectByte(PARAM_1);
	int elevation = (int8)getVarOrDirectByte(PARAM_2);

	Actor *a = derefActor(act, "o2_setActorElevation");
	a->setElevation(elevation);
}

void ScummEngine_v2::o2_actorFromPos() {
	int x, y;
	getResultPos();
	x = getVarOrDirectByte(PARAM_1) * V12_X_MULTIPLIER;
	y = getVarOrDirectByte(PARAM_2) * V12_Y_MULTIPLIER;
	setResult(getActorFromPos(x, y));
}

void ScummEngine_v2::o2_findObject() {
	int obj;
	getResultPos();
	int x = getVarOrDirectByte(PARAM_1) * V12_X_MULTIPLIER;
	int y = getVarOrDirectByte(PARAM_2) * V12_Y_MULTIPLIER;
	obj = findObject(x, y);
	if (obj == 0 && (_game.platform == Common::kPlatformNES) && (_userState & USERSTATE_IFACE_INVENTORY)) {
		if (_mouseOverBoxV2 >= 0 && _mouseOverBoxV2 < 4)
			obj = findInventory(VAR(VAR_EGO), _mouseOverBoxV2 + _inventoryOffset + 1);
	}
	setResult(obj);
}

void ScummEngine_v2::o2_getActorX() {
	int a;
	getResultPos();

	a = getVarOrDirectByte(PARAM_1);
	setResult(getObjX(actorToObj(a)));
}

void ScummEngine_v2::o2_getActorY() {
	int a;
	getResultPos();

	a = getVarOrDirectByte(PARAM_1);
	setResult(getObjY(actorToObj(a)));
}

void ScummEngine_v2::o2_isGreater() {
	uint16 a = getVar();
	uint16 b = getVarOrDirectWord(PARAM_1);
	jumpRelative(b > a);
}

void ScummEngine_v2::o2_isGreaterEqual() {
	uint16 a = getVar();
	uint16 b = getVarOrDirectWord(PARAM_1);
	jumpRelative(b >= a);
}

void ScummEngine_v2::o2_isLess() {
	uint16 a = getVar();
	uint16 b = getVarOrDirectWord(PARAM_1);

	jumpRelative(b < a);
}

void ScummEngine_v2::o2_isLessEqual() {
	uint16 a = getVar();
	uint16 b = getVarOrDirectWord(PARAM_1);
	jumpRelative(b <= a);
}

void ScummEngine_v2::o2_lights() {
	int a, b, c;

	a = getVarOrDirectByte(PARAM_1);
	b = fetchScriptByte();
	c = fetchScriptByte();

	if (c == 0) {
		if (_game.id == GID_MANIAC && _game.version == 1 && !(_game.platform == Common::kPlatformNES)) {
			// Convert older light mode values into
			// equivalent values of later games.
			// 0 Darkness
			// 1 Flashlight
			// 2 Lighted area
			if (a == 2)
				VAR(VAR_CURRENT_LIGHTS) = 11;
			else if (a == 1)
				VAR(VAR_CURRENT_LIGHTS) = 4;
			else
				VAR(VAR_CURRENT_LIGHTS) = 0;
		} else
			VAR(VAR_CURRENT_LIGHTS) = a;
	} else if (c == 1) {
		_flashlight.xStrips = a;
		_flashlight.yStrips = b;
	}
	_fullRedraw = true;
}

void ScummEngine_v2::o2_loadRoomWithEgo() {
	Actor *a;
	int obj, room, x, y, x2, y2, dir;

	obj = getVarOrDirectWord(PARAM_1);
	room = getVarOrDirectByte(PARAM_2);

	a = derefActor(VAR(VAR_EGO), "o2_loadRoomWithEgo");

	// The original interpreter sets the actors new room X/Y to the last rooms X/Y
	// This fixes a problem with MM: script 161 in room 12, the 'Oomph!' script
	// This scripts runs before the actor position is set to the correct room entry location
	if ((_game.id == GID_MANIAC) && (_game.platform != Common::kPlatformNES)) {
		a->putActor(a->getPos().x, a->getPos().y, room);
	} else {
		a->putActor(0, 0, room);
	}
	_egoPositioned = false;

	x = (int8)fetchScriptByte();
	y = (int8)fetchScriptByte();

	startScene(a->_room, a, obj);

	getObjectXYPos(obj, x2, y2, dir);
	AdjustBoxResult r = a->adjustXYToBeInBox(x2, y2);
	x2 = r.x;
	y2 = r.y;
	a->putActor(x2, y2, _currentRoom);
	a->setDirection(dir + 180);

	camera._dest.x = camera._cur.x = a->getPos().x;
	setCameraAt(a->getPos().x, a->getPos().y);
	setCameraFollows(a);

	_fullRedraw = true;

	resetSentence();

	if (x >= 0 && y >= 0) {
		a->startWalkActor(x, y, -1);
	}
	runScript(5, 0, 0, 0);
}

void ScummEngine_v2::o2_setOwnerOf() {
	int obj, owner;

	obj = getVarOrDirectWord(PARAM_1);
	owner = getVarOrDirectByte(PARAM_2);

	setOwnerOf(obj, owner);
}

void ScummEngine_v2::o2_delay() {
	int delay = fetchScriptByte();
	delay |= fetchScriptByte() << 8;
	delay |= fetchScriptByte() << 16;
	delay = 0xFFFFFF - delay;

	vm.slot[_currentScript].delay = delay;
	vm.slot[_currentScript].status = ssPaused;
	o5_breakHere();
}

void ScummEngine_v2::o2_setCameraAt() {
	setCameraAtEx(getVarOrDirectByte(PARAM_1) * V12_X_MULTIPLIER);
}

void ScummEngine_v2::o2_roomOps() {
	int a = getVarOrDirectByte(PARAM_1);
	int b = getVarOrDirectByte(PARAM_2);

	_opcode = fetchScriptByte();
	switch (_opcode & 0x1F) {
	case 1:			// SO_ROOM_SCROLL
		a *= 8;
		b *= 8;
		if (a < (_screenWidth / 2))
			a = (_screenWidth / 2);
		if (b < (_screenWidth / 2))
			b = (_screenWidth / 2);
		if (a > _roomWidth - (_screenWidth / 2))
			a = _roomWidth - (_screenWidth / 2);
		if (b > _roomWidth - (_screenWidth / 2))
			b = _roomWidth - (_screenWidth / 2);
		VAR(VAR_CAMERA_MIN_X) = a;
		VAR(VAR_CAMERA_MAX_X) = b;
		break;
	case 2:			// SO_ROOM_COLOR
		if (_game.version == 1) {
			// V1 zak needs to know when room color is changed
			_roomPalette[0] = 255;
			_roomPalette[1] = a;
			_roomPalette[2] = b;
		} else {
			_roomPalette[b] = a;
		}
		_fullRedraw = true;
		break;
	default:
		break;
	}
}

void ScummEngine_v2::o2_cutscene() {
	vm.cutSceneData[0] = _userState | (_userPut ? 16 : 0);
	vm.cutSceneData[1] = (int16)VAR(VAR_CURSORSTATE);
	vm.cutSceneData[2] = _currentRoom;
	vm.cutSceneData[3] = camera._mode;

	VAR(VAR_CURSORSTATE) = 200;

	// Hide inventory, freeze scripts, hide cursor
	setUserState(USERSTATE_SET_IFACE |
		USERSTATE_SET_CURSOR |
		USERSTATE_SET_FREEZE | USERSTATE_FREEZE_ON);

	_sentenceNum = 0;
	stopScript(SENTENCE_SCRIPT);
	resetSentence();

	vm.cutScenePtr[0] = 0;
}

void ScummEngine_v2::o2_endCutscene() {
	vm.cutSceneStackPointer = 0;

	VAR(VAR_OVERRIDE) = 0;
	vm.cutSceneScript[0] = 0;
	vm.cutScenePtr[0] = 0;

	VAR(VAR_CURSORSTATE) = vm.cutSceneData[1];

	// Reset user state to values before cutscene
	setUserState(vm.cutSceneData[0] | USERSTATE_SET_IFACE | USERSTATE_SET_CURSOR | USERSTATE_SET_FREEZE);

	if ((_game.id == GID_MANIAC) && !(_game.platform == Common::kPlatformNES)) {
		camera._mode = (byte) vm.cutSceneData[3];
		if (camera._mode == kFollowActorCameraMode) {
			actorFollowCamera(VAR(VAR_EGO));
		} else if (vm.cutSceneData[2] != _currentRoom) {
			startScene(vm.cutSceneData[2], 0, 0);
		}
	} else {
		actorFollowCamera(VAR(VAR_EGO));
	}
}

void ScummEngine_v2::o2_beginOverride() {
	vm.cutScenePtr[0] = _scriptPointer - _scriptOrgPointer;
	vm.cutSceneScript[0] = _currentScript;

	// Skip the jump instruction following the override instruction
	fetchScriptByte();
	ScummEngine::fetchScriptWord();
}

void ScummEngine_v2::o2_chainScript() {
	int script = getVarOrDirectByte(PARAM_1);
	stopScript(vm.slot[_currentScript].number);
	_currentScript = 0xFF;
	runScript(script, 0, 0, 0);
}

void ScummEngine_v2::o2_pickupObject() {
	int obj = getVarOrDirectWord(PARAM_1);

	if (obj < 1) {
		error("pickupObject received invalid index %d (script %d)", obj, vm.slot[_currentScript].number);
	}

	if (getObjectIndex(obj) == -1)
		return;

	if (whereIsObject(obj) == WIO_INVENTORY)	/* Don't take an */
		return;											/* object twice */

	addObjectToInventory(obj, _roomResource);
	markObjectRectAsDirty(obj);
	putOwner(obj, VAR(VAR_EGO));
	putState(obj, getState(obj) | kObjectState_08 | kObjectStateUntouchable);
	clearDrawObjectQueue();

	runInventoryScript(1);
	if (_game.platform == Common::kPlatformNES)
		_sound->addSoundToQueue(51);	// play 'pickup' sound
}

void ScummEngine_v2::o2_cursorCommand() {	// TODO: Define the magic numbers
	uint16 cmd = getVarOrDirectWord(PARAM_1);
	byte state = cmd >> 8;

	if (cmd & 0xFF) {
		VAR(VAR_CURSORSTATE) = cmd & 0xFF;
	}

	setUserState(state);
}

void ScummEngine_v2::setUserState(byte state) {
	if (state & USERSTATE_SET_IFACE) {			// Userface
		if (_game.platform == Common::kPlatformNES)
			_userState = (_userState & ~USERSTATE_IFACE_ALL) | (state & USERSTATE_IFACE_ALL);
		else
			_userState = state & USERSTATE_IFACE_ALL;
	}

	if (state & USERSTATE_SET_FREEZE) {		// Freeze
		if (state & USERSTATE_FREEZE_ON)
			freezeScripts(0);
		else
			unfreezeScripts();
	}

	if (state & USERSTATE_SET_CURSOR) {			// Cursor Show/Hide
		if (_game.platform == Common::kPlatformNES)
			_userState = (_userState & ~USERSTATE_CURSOR_ON) | (state & USERSTATE_CURSOR_ON);
		if (state & USERSTATE_CURSOR_ON) {
			_userPut = 1;
			_cursor.state = 1;
		} else {
			_userPut = 0;
			_cursor.state = 0;
		}
	}

	// Hide all verbs and inventory
	Common::Rect rect;
	rect.top = _virtscr[kVerbVirtScreen].topline;
	rect.bottom = _virtscr[kVerbVirtScreen].topline + 8 * 88;
	rect.right = _virtscr[kVerbVirtScreen].w - 1;
	if (_game.platform == Common::kPlatformNES) {
		rect.left = 16;
	} else {
		rect.left = 0;
	}
	restoreBackground(rect);

	// Draw all verbs and inventory
	redrawVerbs();
	runInventoryScript(1);
}

void ScummEngine_v2::o2_getActorWalkBox() {
	Actor *a;
	getResultPos();
	a = derefActor(getVarOrDirectByte(PARAM_1), "o2_getActorWalkbox");
	setResult(a->isInCurrentRoom() ? a->_walkbox: 0xFF);
}

void ScummEngine_v2::o2_dummy() {
	// Opcode 0xEE is used in maniac and zak but has no purpose
	if (_opcode != 0xEE)
		warning("o2_dummy invoked (opcode %d)", _opcode);
}

void ScummEngine_v2::o2_switchCostumeSet() {
	// NES version of maniac uses this to switch between the two
	// groups of costumes it has
	if (_game.platform == Common::kPlatformNES)
		NES_loadCostumeSet(fetchScriptByte());
	else if (_game.platform == Common::kPlatformC64)
		fetchScriptByte();
	else
		o2_dummy();
}

void ScummEngine_v2::resetSentence() {
	VAR(VAR_SENTENCE_VERB) = VAR(VAR_BACKUP_VERB);
	VAR(VAR_SENTENCE_OBJECT1) = 0;
	VAR(VAR_SENTENCE_OBJECT2) = 0;
	VAR(VAR_SENTENCE_PREPOSITION) = 0;
}

void ScummEngine_v2::runInventoryScript(int i) {
	redrawV2Inventory();
}

} // End of namespace Scumm