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
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001 Ludvig Strigeus
* Copyright (C) 2001-2003 The ScummVM project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Header$
*
*/
#include "stdafx.h"
#include "scumm.h"
#include "actor.h"
#include "resource.h"
#include "common/util.h"
/* Script status type (slot.status) */
enum {
ssDead = 0,
ssPaused = 1,
ssRunning = 2
};
/* Start executing script 'script' with the given parameters */
void Scumm::runScript(int script, bool freezeResistant, bool recursive, int *lvarptr) {
ScriptSlot *s;
byte *scriptPtr;
uint32 scriptOffs;
byte scriptType;
int slot;
if (!script)
return;
if (!recursive)
stopScript(script);
if (script < _numGlobalScripts) {
scriptPtr = getResourceAddress(rtScript, script);
scriptOffs = _resourceHeaderSize;
scriptType = WIO_GLOBAL;
} else {
scriptOffs = _localScriptList[script - _numGlobalScripts];
if (scriptOffs == 0)
error("Local script %d is not in room %d", script, _roomResource);
scriptType = WIO_LOCAL;
}
slot = getScriptSlot();
s = &vm.slot[slot];
s->number = script;
s->offs = scriptOffs;
s->status = ssRunning;
s->where = scriptType;
s->freezeResistant = freezeResistant;
s->recursive = recursive;
s->freezeCount = 0;
s->delayFrameCount = 0;
initializeLocals(slot, lvarptr);
runScriptNested(slot);
}
void Scumm::runObjectScript(int object, int entry, bool freezeResistant, bool recursive, int *vars) {
ScriptSlot *s;
uint32 obcd;
int slot, where, offs;
if (!object)
return;
if (!recursive)
stopObjectScript(object);
where = whereIsObject(object);
if (where == WIO_NOT_FOUND) {
warning("Code for object %d not in room %d", object, _roomResource);
return;
}
obcd = getOBCDOffs(object);
slot = getScriptSlot();
offs = getVerbEntrypoint(object, entry);
if (offs == 0)
return;
s = &vm.slot[slot];
s->number = object;
s->offs = obcd + offs;
s->status = ssRunning;
s->where = where;
s->freezeResistant = freezeResistant;
s->recursive = recursive;
s->freezeCount = 0;
s->delayFrameCount = 0;
initializeLocals(slot, vars);
runScriptNested(slot);
}
#ifndef BYPASS_COPY_PROT
#define BYPASS_COPY_PROT
#endif
void Scumm::initializeLocals(int slot, int *vars) {
int i;
if (!vars) {
for (i = 0; i < 16; i++)
vm.localvar[slot][i] = 0;
} else {
for (i = 0; i < 16; i++)
vm.localvar[slot][i] = vars[i];
}
}
int Scumm::getVerbEntrypoint(int obj, int entry) {
const byte *objptr, *verbptr;
int verboffs;
if (whereIsObject(obj) == WIO_NOT_FOUND)
return 0;
objptr = getOBCDFromObject(obj);
assert(objptr);
if (_version <= 2)
verbptr = objptr + 15;
else if (_features & GF_OLD_BUNDLE)
verbptr = objptr + 17;
else if (_features & GF_SMALL_HEADER)
verbptr = objptr + 19;
else
verbptr = findResource(MKID('VERB'), objptr);
assert(verbptr);
verboffs = verbptr - objptr;
if (!(_features & GF_SMALL_HEADER))
verbptr += _resourceHeaderSize;
if (_version == 8) {
const uint32 *ptr = (const uint32 *)verbptr;
uint32 verb;
do {
verb = READ_LE_UINT32(ptr);
if (!verb)
return 0;
if (verb == (uint32)entry || verb == 0xFFFFFFFF)
break;
ptr += 2;
} while (1);
return verboffs + 8 + READ_LE_UINT32(ptr + 1);
} if (_version <= 2) {
do {
if (!*verbptr)
return 0;
if (*verbptr == entry || *verbptr == 0xFF)
break;
verbptr += 2;
} while (1);
return *(verbptr + 1);
} else {
do {
if (!*verbptr)
return 0;
if (*verbptr == entry || *verbptr == 0xFF)
break;
verbptr += 3;
} while (1);
if (_features & GF_SMALL_HEADER)
return READ_LE_UINT16(verbptr + 1);
else
return verboffs + READ_LE_UINT16(verbptr + 1);
}
}
/* Stop script 'script' */
void Scumm::stopScript(int script) {
ScriptSlot *ss;
NestedScript *nest;
int i, num;
if (script == 0)
return;
ss = vm.slot;
for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++) {
if (script == ss->number && ss->status != ssDead &&
(ss->where == WIO_GLOBAL || ss->where == WIO_LOCAL)) {
if (ss->cutsceneOverride)
error("Script %d stopped with active cutscene/override", script);
ss->number = 0;
ss->status = ssDead;
if (_currentScript == i)
_currentScript = 0xFF;
}
}
if (_numNestedScripts == 0)
return;
nest = vm.nest;
num = _numNestedScripts;
do {
if (nest->number == script &&
(nest->where == WIO_GLOBAL || nest->where == WIO_LOCAL)) {
nest->number = 0xFF;
nest->slot = 0xFF;
nest->where = 0xFF;
}
} while (nest++, --num);
}
/* Stop an object script 'script'*/
void Scumm::stopObjectScript(int script) {
ScriptSlot *ss;
NestedScript *nest;
int i, num;
if (script == 0)
return;
ss = vm.slot;
for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++) {
if (script == ss->number && ss->status != ssDead &&
(ss->where == WIO_ROOM || ss->where == WIO_INVENTORY || ss->where == WIO_FLOBJECT)) {
if (ss->cutsceneOverride)
error("Object %d stopped with active cutscene/override", script);
ss->number = 0;
ss->status = ssDead;
if (_currentScript == i)
_currentScript = 0xFF;
}
}
if (_numNestedScripts == 0)
return;
nest = vm.nest;
num = _numNestedScripts;
do {
if (nest->number == script &&
(nest->where == WIO_ROOM || nest->where == WIO_INVENTORY || nest->where == WIO_FLOBJECT)) {
nest->number = 0xFF;
nest->slot = 0xFF;
nest->where = 0xFF;
}
} while (nest++, --num);
}
/* Return a free script slot */
int Scumm::getScriptSlot() {
ScriptSlot *ss;
int i;
ss = vm.slot;
for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++) {
if (ss->status == ssDead)
return i;
}
error("Too many scripts running, %d max", NUM_SCRIPT_SLOT);
return -1;
}
/* Run script 'script' nested - eg, within the parent script.*/
void Scumm::runScriptNested(int script) {
NestedScript *nest;
ScriptSlot *slot;
updateScriptPtr();
nest = &vm.nest[_numNestedScripts];
if (_currentScript == 0xFF) {
nest->number = 0xFF;
nest->where = 0xFF;
} else {
// Store information about the currently running script
slot = &vm.slot[_currentScript];
nest->number = slot->number;
nest->where = slot->where;
nest->slot = _currentScript;
}
_numNestedScripts++;
if (_numNestedScripts > ARRAYSIZE(vm.nest))
error("Too many nested scripts");
_currentScript = script;
getScriptBaseAddress();
getScriptEntryPoint();
executeScript();
_numNestedScripts--;
if (nest->number != 0xFF) {
// Try to resume the script which called us, if its status has not changed
// since it invoked us. In particular, we only resume it if it hasn't been
// stopped in the meantime, and if it did not already move on.
slot = &vm.slot[nest->slot];
if (slot->number == nest->number && slot->where == nest->where &&
slot->status != ssDead && slot->freezeCount == 0) {
_currentScript = nest->slot;
getScriptBaseAddress();
getScriptEntryPoint();
return;
}
}
_currentScript = 0xFF;
}
void Scumm::updateScriptPtr() {
if (_currentScript == 0xFF)
return;
vm.slot[_currentScript].offs = _scriptPointer - _scriptOrgPointer;
}
/* Get the code pointer to a script */
void Scumm::getScriptBaseAddress() {
ScriptSlot *ss;
int idx;
if (_currentScript == 0xFF)
return;
ss = &vm.slot[_currentScript];
switch (ss->where) {
case WIO_INVENTORY: /* inventory script * */
for (idx = 0; idx < _maxInventoryItems; idx++)
if (_inventory[idx] == ss->number)
break;
_scriptOrgPointer = getResourceAddress(rtInventory, idx);
assert(idx < _maxInventoryItems);
_lastCodePtr = &_baseInventoryItems[idx];
break;
case 3:
case WIO_ROOM: /* room script */
if (_version == 8) {
_scriptOrgPointer = getResourceAddress(rtRoomScripts, _roomResource);
assert(_roomResource < res.num[rtRoomScripts]);
_lastCodePtr = &res.address[rtRoomScripts][_roomResource];
} else {
_scriptOrgPointer = getResourceAddress(rtRoom, _roomResource);
assert(_roomResource < _maxRooms);
_lastCodePtr = &_baseRooms[_roomResource];
}
break;
case WIO_GLOBAL: /* global script */
_scriptOrgPointer = getResourceAddress(rtScript, ss->number);
assert(ss->number < _maxScripts);
_lastCodePtr = &_baseScripts[ss->number];
break;
case WIO_FLOBJECT: /* flobject script */
idx = getObjectIndex(ss->number);
_scriptOrgPointer = getResourceAddress(rtFlObject, _objs[idx].fl_object_index);
assert(idx < _maxFLObject);
_lastCodePtr = &_baseFLObject[idx];
break;
default:
error("Bad type while getting base address");
}
}
void Scumm::getScriptEntryPoint() {
if (_currentScript == 0xFF)
return;
_scriptPointer = _scriptOrgPointer + vm.slot[_currentScript].offs;
}
/* Execute a script - Read opcode, and execute it from the table */
void Scumm::executeScript() {
int c;
while (_currentScript != 0xFF) {
if (_showStack == 1) {
printf("Stack:");
for (c=0; c < _scummStackPos; c++) {
printf(" %d", _scummStack[c]);
}
printf("\n");
}
_opcode = fetchScriptByte();
vm.slot[_currentScript].didexec = 1;
debug(7, "Script %d, offset 0x%x: [%X] %s()",
vm.slot[_currentScript].number,
_scriptPointer - _scriptOrgPointer,
_opcode,
getOpcodeDesc(_opcode));
if (_hexdumpScripts == true) {
for (c= -1; c < 15; c++) {
printf(" %02x", *(_scriptPointer + c));
}
printf("\n");
}
executeOpcode(_opcode);
}
CHECK_HEAP;
}
byte Scumm::fetchScriptByte() {
if (*_lastCodePtr + sizeof(MemBlkHeader) != _scriptOrgPointer) {
uint32 oldoffs = _scriptPointer - _scriptOrgPointer;
getScriptBaseAddress();
_scriptPointer = _scriptOrgPointer + oldoffs;
}
return *_scriptPointer++;
}
uint Scumm::fetchScriptWord() {
int a;
if (*_lastCodePtr + sizeof(MemBlkHeader) != _scriptOrgPointer) {
uint32 oldoffs = _scriptPointer - _scriptOrgPointer;
getScriptBaseAddress();
_scriptPointer = _scriptOrgPointer + oldoffs;
}
a = READ_LE_UINT16(_scriptPointer);
_scriptPointer += 2;
return a;
}
int Scumm::fetchScriptWordSigned() {
return (int16)fetchScriptWord();
}
int Scumm::readVar(uint var) {
int a;
#if defined(BYPASS_COPY_PROT)
static byte copyprotbypassed = false;
#endif
debug(9, "readvar(%d)", var);
if (var & 0x2000 && !(_features & GF_NEW_OPCODES)) {
a = fetchScriptWord();
if (a & 0x2000)
var += readVar(a & ~0x2000);
else
var += a & 0xFFF;
var &= ~0x2000;
}
if (!(var & 0xF000)) {
#if defined(BYPASS_COPY_PROT)
if (var == 490 && _gameId == GID_MONKEY2 && !copyprotbypassed) {
copyprotbypassed = true;
var = 518;
} else if (var == 179 && (_gameId == GID_MONKEY_VGA || _gameId == GID_MONKEY_EGA) && !copyprotbypassed) {
copyprotbypassed = true;
var = 266;
}
#endif
if (_gameId == GID_LOOM256 && var == VAR_NOSUBTITLES) {
return _noSubtitles;
}
checkRange(_numVariables - 1, 0, var, "Variable %d out of range(r)");
return _scummVars[var];
}
if (var & 0x8000) {
if ((_gameId == GID_ZAK256) || (_features & GF_OLD_BUNDLE)) {
int bit = var & 0xF;
var = (var >> 4) & 0xFF;
#if defined(BYPASS_COPY_PROT)
// INDY3, EGA Loom and, apparently, Zak256 check this
// during the game...
if (_gameId == GID_INDY3 && var == 94 && bit == 4) {
return 0;
// } else if (_gameId == GID_LOOM && var == 221 && bit == 14) { // For Mac Loom
} else if (_gameId == GID_LOOM && var == 214 && bit == 15) { // For PC Loom
return 0;
} else if (_gameId == GID_ZAK256 && var == 151 && bit == 8) {
return 0;
}
#endif
checkRange(_numVariables - 1, 0, var, "Variable %d out of range(rzb)");
return (_scummVars[ var ] & ( 1 << bit ) ) ? 1 : 0;
} else {
var &= 0x7FFF;
checkRange(_numBitVariables - 1, 0, var, "Bit variable %d out of range(r)");
return (_bitVars[var >> 3] & (1 << (var & 7))) ? 1 : 0;
}
}
if (var & 0x4000) {
if (_gameId == GID_INDY3_256) {
var &= 0xF;
} else {
var &= 0xFFF;
}
checkRange(20, 0, var, "Local variable %d out of range(r)");
return vm.localvar[_currentScript][var];
}
error("Illegal varbits (r)");
return -1;
}
void Scumm::writeVar(uint var, int value) {
if (!(var & 0xF000)) {
checkRange(_numVariables - 1, 0, var, "Variable %d out of range(w)");
// FIXME: Find some better place to put this.
if (var == VAR_CHARINC)
VAR(VAR_CHARINC) = _defaultTalkDelay / 20;
else
_scummVars[var] = value;
// stay in sync with loom cd subtitle var
if (_gameId == GID_LOOM256 && var == VAR_NOSUBTITLES && (value == 0 || value == 1))
_noSubtitles = (value != 0);
if ((_varwatch == (int)var) || (_varwatch == 0)) {
if (vm.slot[_currentScript].number < 100)
debug(1, "vars[%d] = %d (via script-%d)", var, value, vm.slot[_currentScript].number);
else
debug(1, "vars[%d] = %d (via room-%d-%d)", var, value, _currentRoom,
vm.slot[_currentScript].number);
}
return;
}
if (var & 0x8000) {
if ((_gameId == GID_ZAK256) || (_features & GF_OLD_BUNDLE)) {
// In the old games, the bit variables were using the same memory
// as the normal variables!
int bit = var & 0xF;
var = (var >> 4) & 0xFF;
checkRange(_numVariables - 1, 0, var, "Variable %d out of range(wzb)");
if (value)
_scummVars[var] |= ( 1 << bit );
else
_scummVars[var] &= ~( 1 << bit );
} else {
var &= 0x7FFF;
checkRange(_numBitVariables - 1, 0, var, "Bit variable %d out of range(w)");
// FIXME: Enable Indy4 mousefighting by default.
// is there a better place to put this?
if (_gameId == GID_INDY4 && var == 107 && vm.slot[_currentScript].number == 1)
value = 1;
if (value)
_bitVars[var >> 3] |= (1 << (var & 7));
else
_bitVars[var >> 3] &= ~(1 << (var & 7));
}
return;
}
if (var & 0x4000) {
if (_gameId == GID_INDY3_256) {
var &= 0xF;
} else {
var &= 0xFFF;
}
checkRange(20, 0, var, "Local variable %d out of range(w)");
vm.localvar[_currentScript][var] = value;
return;
}
error("Illegal varbits (w)");
}
void Scumm::getResultPos() {
int a;
_resultVarNumber = fetchScriptWord();
if (_resultVarNumber & 0x2000) {
a = fetchScriptWord();
if (a & 0x2000) {
_resultVarNumber += readVar(a & ~0x2000);
} else {
_resultVarNumber += a & 0xFFF;
}
_resultVarNumber &= ~0x2000;
}
}
void Scumm::setResult(int value) {
writeVar(_resultVarNumber, value);
}
void Scumm::push(int a) {
assert(_scummStackPos >= 0 && _scummStackPos < ARRAYSIZE(_scummStack));
_scummStack[_scummStackPos++] = a;
}
int Scumm::pop() {
if (_scummStackPos < 1 || _scummStackPos > ARRAYSIZE(_scummStack)) {
error("No items on stack to pop() for %s (0x%X) at [%d-%d]", getOpcodeDesc(_opcode), _opcode, _roomResource, vm.slot[_currentScript].number);
}
return _scummStack[--_scummStackPos];
}
void Scumm::stopObjectCode() {
ScriptSlot *ss;
ss = &vm.slot[_currentScript];
if (ss->cutsceneOverride == 255) { /* FIXME: What does this? */
warning("Cutscene for script %d has overflown. Resetting.", ss->number);
ss->cutsceneOverride = 0;
}
if (ss->where != WIO_GLOBAL && ss->where != WIO_LOCAL) {
if (ss->cutsceneOverride) {
warning("Object %d ending with active cutscene/override (%d)", ss->number, ss->cutsceneOverride);
ss->cutsceneOverride = 0;
}
} else {
if (ss->cutsceneOverride) {
warning("Script %d ending with active cutscene/override (%d)", ss->number, ss->cutsceneOverride);
ss->cutsceneOverride = 0;
}
}
ss->number = 0;
ss->status = ssDead;
_currentScript = 0xFF;
}
void Scumm::runInventoryScript(int i) {
if (_version <= 2) {
redrawV2Inventory();
} else {
int tmp[16];
tmp[0] = i;
if (VAR(VAR_INVENTORY_SCRIPT)) {
runScript(VAR(VAR_INVENTORY_SCRIPT), 0, 0, tmp);
}
}
}
void Scumm::freezeScripts(int flag) {
int i;
if (_version <= 2) {
for (i = 0; i < NUM_SCRIPT_SLOT; i++) {
if (_currentScript != i && vm.slot[i].status != ssDead && !vm.slot[i].freezeResistant) {
vm.slot[i].status |= 0x80;
vm.slot[i].freezeCount = 1;
}
}
return;
}
for (i = 0; i < NUM_SCRIPT_SLOT; i++) {
if (_currentScript != i && vm.slot[i].status != ssDead && (!vm.slot[i].freezeResistant || flag >= 0x80)) {
vm.slot[i].status |= 0x80;
vm.slot[i].freezeCount++;
}
}
for (i = 0; i < NUM_SENTENCE; i++)
_sentence[i].freezeCount++;
if (vm.cutSceneScriptIndex != 0xFF) {
vm.slot[vm.cutSceneScriptIndex].status &= 0x7F;
vm.slot[vm.cutSceneScriptIndex].freezeCount = 0;
}
}
void Scumm::unfreezeScripts() {
int i;
if (_version <= 2) {
for (i = 0; i < NUM_SCRIPT_SLOT; i++) {
vm.slot[i].status &= 0x7F;
vm.slot[i].freezeCount = 0;
}
return;
}
for (i = 0; i < NUM_SCRIPT_SLOT; i++) {
if (vm.slot[i].status & 0x80) {
if (!--vm.slot[i].freezeCount) {
vm.slot[i].status &= 0x7F;
}
}
}
for (i = 0; i < NUM_SENTENCE; i++) {
if (_sentence[i].freezeCount > 0)
_sentence[i].freezeCount--;
}
}
void Scumm::runAllScripts() {
int i;
for (i = 0; i < NUM_SCRIPT_SLOT; i++)
vm.slot[i].didexec = 0;
// FIXME - why is _curExecScript?!? The only place it is ever set is here.
// The outer world will only see it as consequence of the calls made in the following
// for loop. But in that case, _curExecScript will be equal to _currentScript. Hence
// it would seem we can replace all occurances of _curExecScript by _currentScript.
_currentScript = 0xFF;
for (_curExecScript = 0; _curExecScript < NUM_SCRIPT_SLOT; _curExecScript++) {
if (vm.slot[_curExecScript].status == ssRunning && vm.slot[_curExecScript].didexec == 0) {
_currentScript = (byte)_curExecScript;
getScriptBaseAddress();
getScriptEntryPoint();
executeScript();
}
}
}
void Scumm::runExitScript() {
if (_version > 2 && VAR(VAR_EXIT_SCRIPT))
runScript(VAR(VAR_EXIT_SCRIPT), 0, 0, 0);
if (_EXCD_offs) {
int slot = getScriptSlot();
vm.slot[slot].status = ssRunning;
vm.slot[slot].number = 10001;
vm.slot[slot].where = WIO_ROOM;
vm.slot[slot].offs = _EXCD_offs;
vm.slot[slot].freezeResistant = false;
vm.slot[slot].recursive = false;
vm.slot[slot].freezeCount = 0;
vm.slot[slot].delayFrameCount = 0;
// FIXME: the exit script of room 7 in indy3 only seems to have a size and tag
// not actual data not even a 00 (stop code)
// maybe we should be limiting ourselves to strictly reading the size
// described in the header?
if (_gameId == GID_INDY3_256) {
// FIXME: Oddly, Indy3 seems to contain exit scripts with only a size
// and a tag - not even a terminating NULL!
byte *roomptr = getResourceAddress(rtRoom, _roomResource);
const byte *excd = findResourceData(MKID('EXCD'), roomptr) - _resourceHeaderSize;
if (!excd || (getResourceDataSize(excd) < 1)) {
debug(2, "Exit-%d is empty", _roomResource);
return;
}
}
runScriptNested(slot);
}
if (_version > 2 && VAR(VAR_EXIT_SCRIPT2))
runScript(VAR(VAR_EXIT_SCRIPT2), 0, 0, 0);
}
void Scumm::runEntryScript() {
if (_version > 2 && VAR(VAR_ENTRY_SCRIPT))
runScript(VAR(VAR_ENTRY_SCRIPT), 0, 0, 0);
if (_ENCD_offs) {
int slot = getScriptSlot();
vm.slot[slot].status = ssRunning;
vm.slot[slot].number = 10002;
vm.slot[slot].where = WIO_ROOM;
vm.slot[slot].offs = _ENCD_offs;
vm.slot[slot].freezeResistant = false;
vm.slot[slot].recursive = false;
vm.slot[slot].freezeCount = 0;
vm.slot[slot].delayFrameCount = 0;
runScriptNested(slot);
}
if (_version > 2 && VAR(VAR_ENTRY_SCRIPT2))
runScript(VAR(VAR_ENTRY_SCRIPT2), 0, 0, 0);
}
void Scumm::killScriptsAndResources() {
ScriptSlot *ss;
int i;
ss = vm.slot;
for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++) {
if (ss->where == WIO_ROOM || ss->where == WIO_FLOBJECT) {
if (ss->cutsceneOverride != 0) {
warning("Object %d stopped with active cutscene/override in exit", ss->number);
ss->cutsceneOverride = 0;
}
ss->status = ssDead;
} else if (ss->where == WIO_LOCAL) {
// HACK to make Indy3 Demo work
if (ss->cutsceneOverride != 0 && !(_gameId == GID_INDY3 && _roomResource == 3)) {
warning("Script %d stopped with active cutscene/override in exit", ss->number);
ss->cutsceneOverride = 0;
}
ss->status = ssDead;
}
}
/* Nuke local object names */
if (_newNames) {
for (i = 0; i < _numNewNames; i++) {
int j = _newNames[i];
if (j && getOwner(j) == 0) {
_newNames[i] = 0;
nukeResource(rtObjectName, i);
}
}
}
}
void Scumm::killAllScriptsExceptCurrent() {
for (int i = 0; i < NUM_SCRIPT_SLOT; i++) {
if (i != _currentScript)
vm.slot[i].status = ssDead;
}
}
void Scumm::doSentence(int verb, int objectA, int objectB) {
SentenceTab *st;
if (_version >= 7) {
if (objectA == objectB)
return;
if (_sentenceNum) {
st = &_sentence[_sentenceNum - 1];
// Check if this doSentence request is identical to the previous one;
// if yes, ignore this invocation.
if (_sentenceNum && st->verb == verb && st->objectA == objectA && st->objectB == objectB)
return;
}
}
st = &_sentence[_sentenceNum++];
st->verb = verb;
st->objectA = objectA;
st->objectB = objectB;
st->preposition = (objectB != 0);
st->freezeCount = 0;
}
void Scumm::checkAndRunSentenceScript() {
int i;
ScriptSlot *ss;
int sentenceScript;
if (_version <= 2)
sentenceScript = 2;
else
sentenceScript = VAR(VAR_SENTENCE_SCRIPT);
memset(_localParamList, 0, sizeof(_localParamList));
if (isScriptInUse(sentenceScript)) {
ss = vm.slot;
for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++)
if (ss->number == sentenceScript && ss->status != ssDead && ss->freezeCount == 0)
return;
}
if (!_sentenceNum || _sentence[_sentenceNum - 1].freezeCount)
return;
_sentenceNum--;
if (_version < 7)
if (_sentence[_sentenceNum].preposition && _sentence[_sentenceNum].objectB == _sentence[_sentenceNum].objectA)
return;
if (_version <= 2) {
_scummVars[VAR_ACTIVE_VERB] = _sentence[_sentenceNum].verb;
_scummVars[VAR_ACTIVE_OBJECT1] = _sentence[_sentenceNum].objectA;
_scummVars[VAR_ACTIVE_OBJECT2] = _sentence[_sentenceNum].objectB;
_scummVars[VAR_VERB_ALLOWED] = (0 != getVerbEntrypoint(_sentence[_sentenceNum].objectA, _sentence[_sentenceNum].verb));
} else {
_localParamList[0] = _sentence[_sentenceNum].verb;
_localParamList[1] = _sentence[_sentenceNum].objectA;
_localParamList[2] = _sentence[_sentenceNum].objectB;
}
_currentScript = 0xFF;
if (sentenceScript)
runScript(sentenceScript, 0, 0, _localParamList);
}
void Scumm::runInputScript(int a, int cmd, int mode) {
int args[16];
int verbScript;
if (_version <= 2) {
verbScript = 4;
_scummVars[VAR_CLICK_AREA] = a;
switch(a) {
case 1: // Verb clicked
_scummVars[33] = cmd;
break;
case 3: // Inventory clicked
_scummVars[35] = cmd;
break;
}
} else {
verbScript = VAR(VAR_VERB_SCRIPT);
}
memset(args, 0, sizeof(args));
args[0] = a;
args[1] = cmd;
args[2] = mode;
if (verbScript)
runScript(verbScript, 0, 0, args);
}
void Scumm::decreaseScriptDelay(int amount) {
ScriptSlot *ss = vm.slot;
int i;
for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++) {
if (ss->status == ssPaused) {
ss->delay -= amount;
if (ss->delay < 0) {
ss->status = ssRunning;
ss->delay = 0;
}
}
}
}
bool Scumm::isScriptInUse(int script) const {
int i;
const ScriptSlot *ss = vm.slot;
for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++)
if (ss->number == script)
return true;
return false;
}
bool Scumm::isScriptRunning(int script) const {
int i;
const ScriptSlot *ss = vm.slot;
for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++)
if (ss->number == script && (ss->where == WIO_GLOBAL || ss->where == WIO_LOCAL) && ss->status != ssDead)
return true;
return false;
}
bool Scumm::isRoomScriptRunning(int script) const {
int i;
const ScriptSlot *ss = vm.slot;
for (i = 0; i < NUM_SCRIPT_SLOT; i++, ss++)
if (ss->number == script && ss->where == WIO_ROOM && ss->status != ssDead)
return true;
return false;
}
int Scumm::defineArray(int array, int type, int dim2, int dim1) {
int id;
int size;
ArrayHeader *ah;
if (type != 5 && type != 4)
type = 5;
nukeArray(array);
id = getArrayId();
if (_version == 8) {
if (array & 0x40000000) {
}
if (array & 0x80000000) {
error("Can't define bit variable as array pointer");
}
size = (type == 5) ? 32 : 8;
} else {
if (array & 0x4000) {
}
if (array & 0x8000) {
error("Can't define bit variable as array pointer");
}
size = (type == 5) ? 16 : 8;
}
writeVar(array, id);
size *= dim2 + 1;
size *= dim1 + 1;
size >>= 3;
ah = (ArrayHeader *)createResource(rtString, id, size + sizeof(ArrayHeader));
ah->type = type;
ah->dim1_size = dim1 + 1;
ah->dim2_size = dim2 + 1;
return id;
}
void Scumm::nukeArray(int a) {
int data;
data = readVar(a);
if (data)
nukeResource(rtString, data);
writeVar(a, 0);
}
int Scumm::getArrayId() {
byte **addr = _baseArrays;
int i;
for (i = 1; i < _numArray; i++) {
if (!addr[i])
return i;
}
error("Out of array pointers, %d max", _numArray);
return -1;
}
void Scumm::copyScriptString(byte *dst) {
int len = resStrLen(_scriptPointer) + 1;
while (len--)
*dst++ = fetchScriptByte();
}
//
// Given a pointer to a Scumm string, this function returns the total byte length
// of the string data in that resource. To do so it has to understand certain
// special characters embedded into the string. The reason for this function is that
// sometimes this embedded data contains zero bytes, thus we can't just use strlen.
//
int Scumm::resStrLen(const byte *src) const {
int num = 0;
byte chr;
if (src == NULL)
src = _scriptPointer;
while ((chr = *src++) != 0) {
num++;
if (chr == 255) {
chr = *src++;
num++;
if (chr != 1 && chr != 2 && chr != 3 && chr != 8) {
if (_version == 8) {
src += 4;
num += 4;
} else {
src += 2;
num += 2;
}
}
}
}
return num;
}
void Scumm::beginCutscene(int *args) {
int scr = _currentScript;
vm.slot[scr].cutsceneOverride++;
if (++vm.cutSceneStackPointer > ARRAYSIZE(vm.cutSceneData))
error("Cutscene stack overflow");
vm.cutSceneData[vm.cutSceneStackPointer] = args[0];
vm.cutSceneScript[vm.cutSceneStackPointer] = 0;
vm.cutScenePtr[vm.cutSceneStackPointer] = 0;
vm.cutSceneScriptIndex = scr;
if (VAR(VAR_CUTSCENE_START_SCRIPT))
runScript(VAR(VAR_CUTSCENE_START_SCRIPT), 0, 0, args);
vm.cutSceneScriptIndex = 0xFF;
}
void Scumm::endCutscene() {
ScriptSlot *ss = &vm.slot[_currentScript];
int args[16];
memset(args, 0, sizeof(args));
if (ss->cutsceneOverride > 0) // Only terminate if active
ss->cutsceneOverride--;
args[0] = vm.cutSceneData[vm.cutSceneStackPointer];
VAR(VAR_OVERRIDE) = 0;
if (vm.cutScenePtr[vm.cutSceneStackPointer] && (ss->cutsceneOverride > 0)) // Only terminate if active
ss->cutsceneOverride--;
vm.cutSceneScript[vm.cutSceneStackPointer] = 0;
vm.cutScenePtr[vm.cutSceneStackPointer] = 0;
vm.cutSceneStackPointer--;
if (VAR(VAR_CUTSCENE_END_SCRIPT))
runScript(VAR(VAR_CUTSCENE_END_SCRIPT), 0, 0, args);
}
void Scumm::abortCutscene() {
uint32 offs = vm.cutScenePtr[vm.cutSceneStackPointer];
if (offs) {
ScriptSlot *ss = &vm.slot[vm.cutSceneScript[vm.cutSceneStackPointer]];
ss->offs = offs;
ss->status = ssRunning;
ss->freezeCount = 0;
if (ss->cutsceneOverride > 0)
ss->cutsceneOverride--;
VAR(VAR_OVERRIDE) = 1;
vm.cutScenePtr[vm.cutSceneStackPointer] = 0;
}
}
void Scumm::beginOverride() {
int idx;
idx = vm.cutSceneStackPointer;
assert(idx < 5);
vm.cutScenePtr[idx] = _scriptPointer - _scriptOrgPointer;
vm.cutSceneScript[idx] = _currentScript;
// Skip the jump instruction following the override instruction
// (the jump is responsible for "skipping" cutscenes, and the reason
// why we record the current script position in vm.cutScenePtr).
fetchScriptByte();
fetchScriptWord();
// FIXME: why is this here? it doesn't seem to belong here?
VAR(VAR_OVERRIDE) = 0;
}
void Scumm::endOverride() {
int idx;
idx = vm.cutSceneStackPointer;
assert(idx < 5);
vm.cutScenePtr[idx] = 0;
vm.cutSceneScript[idx] = 0;
if (_version > 3)
VAR(VAR_OVERRIDE) = 0;
}
|