aboutsummaryrefslogtreecommitdiff
path: root/engines/macventure/script.cpp
blob: 5b86c31e8473deb4e7f3447e85d012fb35168c64 (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
/* 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/system.h"

#include "macventure/macventure.h"
#include "macventure/script.h"
#include "macventure/world.h"
#include "macventure/container.h"

namespace MacVenture {

ScriptEngine::ScriptEngine(MacVentureEngine * engine, World * world) {
	_engine = engine;
	_world = world;
	_scripts = new Container("Shadowgate II/Shadow Filter");
}

ScriptEngine::~ScriptEngine() {
	if (_scripts)
		delete _scripts;
}

bool ScriptEngine::runControl(ControlAction action, ObjID source, ObjID destination, Common::Point delta) {
	EngineFrame frame;
	frame.action = action;
	frame.src = source;
	frame.dest = destination;
	frame.x = delta.x;
	frame.y = delta.y;
	frame.haltedInSaves = false;
	frame.haltedInFirst = false;
	frame.haltedInFamily = false;
	_frames.push_back(frame);
	debug(2, "SCRIPT: Stored frame %d, action: %d src: %d dest: %d point: (%d, %d)",
		_frames.size() - 1, frame.action, frame.src, frame.dest, frame.x, frame.y);

	return resume(true);
}

bool ScriptEngine::resume(bool execAll) {
	debug(2, "SCRIPT: Resume");
	while (_frames.size()) {
		bool fail = execFrame(execAll);
		if (fail) return true;
	}
	return false;
}

void ScriptEngine::reset() {
	_frames.clear();
}

bool ScriptEngine::execFrame(bool execAll) {
	bool doFirst = execAll;
	bool doFamily = false;
	bool fail;

	EngineFrame *frame = &_frames.front();

	// Do first dispatch script (script 0)
	if (frame->haltedInFirst || doFirst) { // We were stuck or it's the first time
		frame->haltedInFirst = false;
		if (doFirst) { fail = loadScript(frame, 0); }
		else { fail = resumeFunc(frame); }
		if (fail) {
			frame->haltedInFirst = true;
			_engine->preparedToRun();
			return true;
		}
		doFamily = true;
		frame->familyIdx = 0;
	}

	// Do scripts in the family of player (ObjID 1)
	if (frame->haltedInFamily || doFamily) { // We have to do the family or we were stuck here
		frame->haltedInFamily = false;
		Common::Array<ObjID> family = _world->getFamily(_world->getObjAttr(1, kAttrParentObject), false);
		uint32 i = frame->familyIdx;
		for (; i < family.size(); i++) {
			if (doFamily) { fail = loadScript(frame, family[i]); }
			else { fail = resumeFunc(frame); }
			if (fail) { // We are stuck, so we don't shift the frame
				frame->haltedInFamily = true;
				frame->familyIdx = i;
				_engine->preparedToRun();
				return true;
			}
			doFamily = true;
		}
	}

	// Halted in saves
	if (frame->haltedInSaves) {
		frame->haltedInSaves = false;
		if (resumeFunc(frame)) {
			frame->haltedInSaves = true;
			_engine->preparedToRun();
			return true;
		}
	}

	uint highest = 0;
	uint localHigh = 0;
	do { // Saved function calls
		highest = 0;
		for (uint i = 0; i <frame->saves.size(); i++)
		{
			if (highest < frame->saves[i].rank) {
				highest = frame->saves[i].rank;
				localHigh = i;
			}
		}
		if (highest) {
			frame->saves[localHigh].rank = 0;
			if (loadScript(frame, frame->saves[localHigh].func)) {
				frame->haltedInSaves = true;
				_engine->preparedToRun();
				return true;
			}
		}
	} while (highest);

	_frames.pop_front();
	return false;
}

bool ScriptEngine::loadScript(EngineFrame * frame, uint32 scriptID) {
	if (_scripts->getItemByteSize(scriptID) > 0) {
		debug(2, "SCRIPT: Loading function %d", scriptID);
		// Insert the new script at the front
		frame->scripts.push_front(ScriptAsset(scriptID, _scripts));
		return runFunc(frame);
	}
	return false;
}

bool ScriptEngine::resumeFunc(EngineFrame * frame) {
	bool fail = runFunc(frame);
	if (fail) return fail;
	frame->scripts.pop_front();
	if (frame->scripts.size())
		return resumeFunc(frame);
	return false;
}

bool ScriptEngine::runFunc(EngineFrame *frame) {
	ScriptAsset &script = frame->scripts.front();
	debug(2, "SCRIPT: Executing function %d", script.getId());
	EngineState *state = &frame->state;
	byte op;
	while (script.hasNext()) {
		op = script.fetch();
		debug(3, "SCRIPT: I'm running operation %d", op);
		if (!(op & 0x80)) {
			state->push(op);
		} else {
			switch (op) {
			case 0x80: //get attribute
				op80GATT(state, frame);
				break;
			case 0x81: //set attribute
				op81SATT(state, frame);
				break;
			case 0x82: //sum children attribute
				op82SUCH(state, frame);
				break;
			case 0x83: //push selected control
				op83PUCT(state, frame);
				break;
			case 0x84: //push selected object
				op84PUOB(state, frame);
				break;
			case 0x85: //push target
				op85PUTA(state, frame);
				break;
			case 0x86: //push deltax
				op86PUDX(state, frame);
				break;
			case 0x87: //push deltay
				op87PUDY(state, frame);
				break;
			case 0x88: //push immediate.b
				op88PUIB(state, frame, &script);
				break;
			case 0x89: //push immediate
				op89PUI(state, frame, &script);
				break;
			case 0x8a: //get global
				op8aGGLO(state, frame);
				break;
			case 0x8b: //set global
				op8bSGLO(state, frame);
				break;
			case 0x8c: //random
				op8cRAND(state, frame);
				break;
			case 0x8d: //copy
				op8dCOPY(state, frame);
				break;
			case 0x8e: //copyn
				op8eCOPYN(state, frame);
				break;
			case 0x8f: //swap
				op8fSWAP(state, frame);
				break;
			case 0x90: //swapn
				op90SWAPN(state, frame);
				break;
			case 0x91: //pop
				op91POP(state, frame);
				break;
			case 0x92: //copy+1
				op92COPYP(state, frame);
				break;
			case 0x93: //copy+n
				op93COPYPN(state, frame);
				break;
			case 0x94: //shuffle
				op94SHUFF(state, frame);
				break;
			case 0x95: //sort
				op95SORT(state, frame);
				break;
			case 0x96: //clear stack
				op96CLEAR(state, frame);
				break;
			case 0x97: //get stack size
				op97SIZE(state, frame);
				break;
			case 0x98: //add
				op98ADD(state, frame);
				break;
			case 0x99: //subtract
				op99SUB(state, frame);
				break;
			case 0x9a: //multiply
				op9aMUL(state, frame);
				break;
			case 0x9b: //divide
				op9bDIV(state, frame);
				break;
			case 0x9c: //mod
				op9cMOD(state, frame);
				break;
			case 0x9d: //divmod
				op9dDMOD(state, frame);
				break;
			case 0x9e: //abs
				op9eABS(state, frame);
				break;
			case 0x9f: //neg
				op9fNEG(state, frame);
				break;
			case 0xa0: //and
				opa0AND(state, frame);
				break;
			case 0xa1: //or
				opa1OR(state, frame);
				break;
			case 0xa2: //xor
				opa2XOR(state, frame);
				break;
			case 0xa3: //not
				opa3NOT(state, frame);
				break;
			case 0xa4: //logical and
				opa4LAND(state, frame);
				break;
			case 0xa5: //logical or
				opa5LOR(state, frame);
				break;
			case 0xa6: //logical xor
				opa6LXOR(state, frame);
				break;
			case 0xa7: //logical not
				opa7LNOT(state, frame);
				break;
			case 0xa8: //gt? unsigned
				opa8GTU(state, frame);
				break;
			case 0xa9: //lt? unsigned
				opa9LTU(state, frame);
				break;
			case 0xaa: //gt? signed
				opaaGTS(state, frame);
				break;
			case 0xab: //lt? signed
				opabLTS(state, frame);
				break;
			case 0xac: //eq?
				opacEQ(state, frame);
				break;
			case 0xad: //eq string?
				opadEQS(state, frame);
				break;
			case 0xae: //contains
				opaeCONT(state, frame);
				break;
			case 0xaf: //contains word
				opafCONTW(state, frame);
				break;
			case 0xb0: //bra
				opb0BRA(state, frame, &script);
				break;
			case 0xb1: //bra.b
				opb1BRAB(state, frame, &script);
				break;
			case 0xb2: //beq
				opb2BEQ(state, frame, &script);
				break;
			case 0xb3: //beq.b
				opb3BEQB(state, frame, &script);
				break;
			case 0xb4: //bne
				opb4BNE(state, frame, &script);
				break;
			case 0xb5: //bne.b
				opb5BNEB(state, frame, &script);
				break;
			case 0xb6: //call later
				opb6CLAT(state, frame);
				break;
			case 0xb7: //cancel call
				opb7CCA(state, frame);
				break;
			case 0xb8: //cancel low priority
				opb8CLOW(state, frame);
				break;
			case 0xb9: //cancel high priority
				opb9CHI(state, frame);
				break;
			case 0xba: //cancel priority range
				opbaCRAN(state, frame);
				break;
			case 0xbb: //fork
				if (opbbFORK(state, frame))
					return true;
				break;
			case 0xbc: //call
				if (opbcCALL(state, frame, script))
					return true;
				break;
			case 0xbd: //focus object
				opbdFOOB(state, frame);
				break;
			case 0xbe: //swap objects
				opbeSWOB(state, frame);
				break;
			case 0xbf: //snap object
				opbfSNOB(state, frame);
				break;
			case 0xc0: //toggle exits
				opc0TEXI(state, frame);
				break;
			case 0xc1: //print text
				opc1PTXT(state, frame);
				break;
			case 0xc2: //print newline
				opc2PNEW(state, frame);
				break;
			case 0xc3: //print text+nl
				opc3PTNE(state, frame);
				break;
			case 0xc4: //print nl+text+nl
				opc4PNTN(state, frame);
				break;
			case 0xc5: //print number
				opc5PNUM(state, frame);
				break;
			case 0xc6: //push 2
				opc6P2(state, frame);
				break;
			case 0xc7: //play sound in background
				opc7PLBG(state, frame);
				break;
			case 0xc8: //play sound and wait
				opc8PLAW(state, frame);
				break;
			case 0xc9: //wait for sound to finish?
				opc9WAIT(state, frame);
				break;
			case 0xca: //get current time
				opcaTIME(state, frame);
				break;
			case 0xcb: //get current day
				opcbDAY(state, frame);
				break;
			case 0xcc: //get children
				opccCHLD(state, frame);
				break;
			case 0xcd: //get num children
				opcdNCHLD(state, frame);
				break;
			case 0xce: //get engine version
				opceVERS(state, frame);
				break;
			case 0xcf: //push scenario number
				opcfPSCE(state, frame);
				break;
			case 0xd0: //push 1
				opd0P1(state, frame);
				break;
			case 0xd1: //get object dimensions
				opd1GOBD(state, frame);
				break;
			case 0xd2: //get overlap percent
				opd2GOVP(state, frame);
				break;
			case 0xd3: //capture children
				opd3CAPC(state, frame);
				break;
			case 0xd4: //release children
				opd4RELC(state, frame);
				break;
			case 0xd5: //show speech dialog
				opd5DLOG(state, frame);
				return true;
			case 0xd6: //activate command
				opd6ACMD(state, frame);
				break;
			case 0xd7: //lose game
				opd7LOSE(state, frame);
				break;
			case 0xd8: //win game
				opd8WIN(state, frame);
				break;
			case 0xd9: //sleep
				opd9SLEEP(state, frame);
				return true;
			case 0xda: //click to continue
				opdaCLICK(state, frame);
				return true;
			case 0xdb: //run queue
				opdbROBQ(state, frame);
				break;
			case 0xdc: //run sound queue
				opdcRSQ(state, frame);
				break;
			case 0xdd: //run text queue
				opddRTQ(state, frame);
				break;
			case 0xde: //update screen
				opdeUPSC(state, frame);
				break;
			case 0xdf: //flash main window
				opdfFMAI(state, frame);
				return true;
			case 0xe0: //cache graphic and object
				ope0CHGR(state, frame);
				break;
			case 0xe1: //cache sound
				ope1CHSO(state, frame);
				break;
			case 0xe2: //muldiv
				ope2MDIV(state, frame);
				break;
			case 0xe3: //update object
				ope3UPOB(state, frame);
				break;
			case 0xe4: //currently playing event?
				ope4PLEV(state, frame);
				break;
			case 0xe5: //wait for event to finish
				ope5WEV(state, frame);
				break;
			case 0xe6: //get fibonacci (joke)
				ope6GFIB(state, frame);
				break;
			case 0xe7: //calc fibonacci
				ope7CFIB(state, frame);
				break;
			default:
				op00NOOP(op);
			}
		}
	}
	return false;
}

word ScriptEngine::neg16(word val) {
	if (val & 0x8000)
		val = -((val ^ 0xFFFF) + 1);
	return val;
}

word ScriptEngine::neg8(word val) {
	if (val & 0x80)
		val = -((val ^ 0xff) + 1);
	return val;
}

word ScriptEngine::sumChildrenAttr(word obj, word attr, bool recursive) {
	word sum = 0;
	Common::Array<ObjID> children = _world->getChildren(obj, recursive);
	for (Common::Array<ObjID>::const_iterator it = children.begin(); it != children.end(); it++) {
		sum += _world->getObjAttr(*it, attr);
	}
	return sum;
}

void MacVenture::ScriptEngine::op80GATT(EngineState * state, EngineFrame * frame) {
	word obj = state->pop();
	word attr = state->pop();
	state->push(_world->getObjAttr(obj, attr));
}

void ScriptEngine::op81SATT(EngineState * state, EngineFrame * frame) {
	word obj = state->pop();
	word attr = state->pop();
	word val = neg16(state->pop());
	_world->setObjAttr(obj, attr, val);
}

void ScriptEngine::op82SUCH(EngineState * state, EngineFrame * frame) {
	word obj = state->pop();
	word attr = state->pop();
	word recursive = neg16(state->pop());
	state->push(sumChildrenAttr(obj, attr, recursive));
}

void ScriptEngine::op83PUCT(EngineState * state, EngineFrame * frame) {
	state->push(frame->action);
}

void ScriptEngine::op84PUOB(EngineState * state, EngineFrame * frame) {
	state->push(frame->src);
}

void ScriptEngine::op85PUTA(EngineState * state, EngineFrame * frame) {
	state->push(frame->dest);
}

void ScriptEngine::op86PUDX(EngineState * state, EngineFrame * frame) {
	state->push(frame->x);
}

void ScriptEngine::op87PUDY(EngineState * state, EngineFrame * frame) {
	state->push(frame->y);
}

void ScriptEngine::op88PUIB(EngineState * state, EngineFrame * frame, ScriptAsset *script) {
	state->push(script->fetch());
}

void ScriptEngine::op89PUI(EngineState * state, EngineFrame * frame, ScriptAsset * script) {
	word val = script->fetch();
	val <<= 8;
	val = val | script->fetch();
	state->push(val);
}

void ScriptEngine::op8aGGLO(EngineState * state, EngineFrame * frame) {
	word idx = state->pop();
	state->push(_world->getGlobal(idx));
}

void ScriptEngine::op8bSGLO(EngineState * state, EngineFrame * frame) {
	word idx = state->pop();
	word val = neg16(state->pop());
	_world->setGlobal(idx, val);
	_engine->gameChanged();
}

void ScriptEngine::op8cRAND(EngineState * state, EngineFrame * frame) {
	word max = state->pop();
	state->push(_engine->randBetween(0, max));
}

void ScriptEngine::op8dCOPY(EngineState * state, EngineFrame * frame) {
	word val = state->pop();
	state->push(val);
	state->push(val);
}

void ScriptEngine::op8eCOPYN(EngineState * state, EngineFrame * frame) {
	word n = state->pop();
	word offs = n - 1;
	word val;
	while (n) {
		val = state->peek(offs);
		state->push(val);
		n--;
	}
}

void ScriptEngine::op8fSWAP(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push(b);
	state->push(a);
}

void ScriptEngine::op90SWAPN(EngineState * state, EngineFrame * frame) {
	word idx = state->pop();
	word a = state->peek(idx);
	word b = state->peek(0);
	state->poke(idx, b);
	state->poke(0, a);
}

void ScriptEngine::op91POP(EngineState * state, EngineFrame * frame) {
	state->pop();
}

void ScriptEngine::op92COPYP(EngineState * state, EngineFrame * frame) {
	word val = state->peek(1);
	state->push(val);
}

void ScriptEngine::op93COPYPN(EngineState * state, EngineFrame * frame) {
	word idx = state->pop();
	word val = state->peek(idx);
	state->push(val);
}

void ScriptEngine::op94SHUFF(EngineState * state, EngineFrame * frame) {
	word a = state->pop();
	word b = state->pop();
	word c = state->pop();
	state->push(a);
	state->push(c);
	state->push(b);
}

void ScriptEngine::op95SORT(EngineState * state, EngineFrame * frame) {
	word step = neg16(state->pop());
	word num = neg16(state->pop());
	step %= num;
	if (step<0) step += num;
	word end = 0;
	word start = 0;
	for (word i = 1;i<num;i++)
	{
		start += step;
		if (start >= num) start -= num;
		if (start == end)
		{
			end++;
			start = end;
		}
		else
		{
			word a = state->peek(end);
			word b = state->peek(start);
			state->poke(end, b);
			state->poke(start, a);
		}
	}
}

void ScriptEngine::op96CLEAR(EngineState * state, EngineFrame * frame) {
	state->clear();
}

void ScriptEngine::op97SIZE(EngineState * state, EngineFrame * frame) {
	state->push(state->size());
}

void ScriptEngine::op98ADD(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push(a + b);
}

void ScriptEngine::op99SUB(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push(a - b);
}

void ScriptEngine::op9aMUL(EngineState * state, EngineFrame * frame) {
	int16 b = state->pop();
	int16 a = state->pop();
	state->push(a * b);
}

void ScriptEngine::op9bDIV(EngineState * state, EngineFrame * frame) {
	int16 b = state->pop();
	int16 a = state->pop();
	state->push((a / b) | 0);
}

void ScriptEngine::op9cMOD(EngineState * state, EngineFrame * frame) {
	int16 b = state->pop();
	int16 a = state->pop();
	state->push(a % b);
}

void ScriptEngine::op9dDMOD(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push(a % b);
	state->push((a / b) | 0);
}

void ScriptEngine::op9eABS(EngineState * state, EngineFrame * frame) {
	word val = neg16(state->pop());
	if (val<0) val = -val;
	state->push(val);
}

void ScriptEngine::op9fNEG(EngineState * state, EngineFrame * frame) {
	word val = -neg16(state->pop());
	state->push(val);
}

void ScriptEngine::opa0AND(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push(a & b);
}

void ScriptEngine::opa1OR(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push(a | b);
}

void ScriptEngine::opa2XOR(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push(a ^ b);
}

void ScriptEngine::opa3NOT(EngineState * state, EngineFrame * frame) {
	word a = state->pop();
	state->push(a ^ 0xFFFF);
}

void ScriptEngine::opa4LAND(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push((a && b) ? 0xFFFF : 0);
}

void ScriptEngine::opa5LOR(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push((a || b) ? 0xFFFF : 0);
}

void ScriptEngine::opa6LXOR(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push((!a != !b) ? 0xFFFF : 0);
}

void ScriptEngine::opa7LNOT(EngineState * state, EngineFrame * frame) {
	word a = state->pop();
	state->push((a == 0) ? 0xFFFF : 0);
}

void ScriptEngine::opa8GTU(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push((a > b) ? 0xFFFF : 0);
}

void ScriptEngine::opa9LTU(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push((a < b) ? 0xFFFF : 0);
}

void ScriptEngine::opaaGTS(EngineState * state, EngineFrame * frame) {
	word b = neg16(state->pop());
	word a = neg16(state->pop());
	state->push((a > b) ? 0xFFFF : 0);
}

void ScriptEngine::opabLTS(EngineState * state, EngineFrame * frame) {
	word b = neg16(state->pop());
	word a = neg16(state->pop());
	state->push((a < b) ? 0xFFFF : 0);
}

void ScriptEngine::opacEQ(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push((a == b) ? 0xFFFF : 0);
}

void ScriptEngine::opadEQS(EngineState * state, EngineFrame * frame) {
	Common::String b = _world->getText(state->pop(), 0, 0); // HACK, these destinations might be wrong
	Common::String a = _world->getText(state->pop(), 0, 0);
	state->push((a == b) ? 1 : 0);
}

void ScriptEngine::opaeCONT(EngineState * state, EngineFrame * frame) {
	Common::String needle = _world->getText(state->pop(), 0, 0);
	Common::String haystack = _world->getText(state->pop(), 0, 0);
	haystack.toLowercase();
	state->push(haystack.contains(needle) ? 1 : 0);
}

void ScriptEngine::opafCONTW(EngineState * state, EngineFrame * frame) {
	Common::String needle = _world->getText(state->pop(), 0, 0);
	Common::String haystack = _world->getText(state->pop(), 0, 0);
	haystack.toLowercase();
	state->push(haystack.contains(needle) ? 1 : 0);
}

void ScriptEngine::opb0BRA(EngineState * state, EngineFrame * frame, ScriptAsset *script) {
	word val = script->fetch();
	val <<= 8;
	val = val | script->fetch();
	val = neg16(val);
	script->branch(val);
}

void ScriptEngine::opb1BRAB(EngineState * state, EngineFrame * frame, ScriptAsset *script) {
	word val = script->fetch();
	val = neg8(val);
	script->branch(val);
}

void ScriptEngine::opb2BEQ(EngineState * state, EngineFrame * frame, ScriptAsset *script) {
	word val = script->fetch();
	val <<= 8;
	val = val | script->fetch();
	val = neg16(val);
	word b = state->pop();
	if (b != 0) script->branch(val);
}

void ScriptEngine::opb3BEQB(EngineState * state, EngineFrame * frame, ScriptAsset *script) {
	word val = script->fetch();
	val = neg8(val);
	word b = state->pop();
	if (b != 0) script->branch(val);
}

void ScriptEngine::opb4BNE(EngineState * state, EngineFrame * frame, ScriptAsset *script) {
	word val = script->fetch();
	val <<= 8;
	val = val | script->fetch();
	val = neg16(val);
	word b = state->pop();
	if (b == 0) script->branch(val);
}

void ScriptEngine::opb5BNEB(EngineState * state, EngineFrame * frame, ScriptAsset *script) {
	word val = script->fetch();
	val = neg8(val);
	word b = state->pop();
	if (b == 0) script->branch(val);
}

void ScriptEngine::opb6CLAT(EngineState * state, EngineFrame * frame) {
	word rank = state->pop();
	word func = state->pop();
	frame->saves.push_back(FunCall(func, rank));
}

void ScriptEngine::opb7CCA(EngineState * state, EngineFrame * frame) {
	word func = state->pop();
	for (uint i = 0; i < frame->saves.size(); i++) {
		if (frame->saves[i].func == func)
			frame->saves[i].rank = 0;
	}
}

void ScriptEngine::opb8CLOW(EngineState * state, EngineFrame * frame) {
	word hi = state->pop();
	for (uint i = 0;i<frame->saves.size();i++)
		if (frame->saves[i].rank <= hi)
			frame->saves[i].rank = 0;
}

void ScriptEngine::opb9CHI(EngineState * state, EngineFrame * frame) {
	word lo = state->pop();
	for (uint i = 0;i<frame->saves.size();i++)
		if (frame->saves[i].rank >= lo)
			frame->saves[i].rank = 0;
}

void ScriptEngine::opbaCRAN(EngineState * state, EngineFrame * frame) {
	word hi = state->pop();
	word lo = state->pop();
	for (uint i = 0;i<frame->saves.size();i++)
		if (frame->saves[i].rank >= lo &&
			frame->saves[i].rank <= hi)
			frame->saves[i].rank = 0;
}

bool ScriptEngine::opbbFORK(EngineState * state, EngineFrame * frame) {
	EngineFrame newframe;
	newframe.action = (ControlAction)state->pop();
	newframe.src = state->pop();
	newframe.dest = state->pop();
	newframe.x = state->pop();
	newframe.y = state->pop();
	newframe.haltedInFamily = false;
	newframe.haltedInFirst = false;
	newframe.haltedInSaves = false;
	_frames.push_front(newframe);
	if (execFrame(true)) {
		return true;
	}
}

bool ScriptEngine::opbcCALL(EngineState * state, EngineFrame * frame, ScriptAsset &script) {
	word id = state->pop();
	ScriptAsset newfun = ScriptAsset(id, _scripts);
	ScriptAsset current = script;
	if (loadScript(frame, id))
		return true;
	frame->scripts.pop_front();
	script = frame->scripts.front();
	debug(2, "SCRIPT: Return from fuction %d", id);
}

void ScriptEngine::opbdFOOB(EngineState * state, EngineFrame * frame) {
	word obj = state->pop();
	_engine->enqueueObject(kFocusWindow, obj);
}

void ScriptEngine::opbeSWOB(EngineState * state, EngineFrame * frame) {
	ObjID from = state->pop();
	ObjID to = state->pop();
	_engine->enqueueObject(kUpdateWindow, from, to);
	_world->setObjAttr(to, kAttrContainerOpen, _world->getObjAttr(from, 6));
	_world->setObjAttr(from, kAttrContainerOpen, 0);
	Common::Array<ObjID> children = _world->getChildren(from, true);
	for (uint i = 0; i < children.size(); i++)
		_world->setObjAttr(children[i], 0, to);
}

void ScriptEngine::opbfSNOB(EngineState * state, EngineFrame * frame) {
	_engine->enqueueObject(kAnimateBack, frame->src);
}

void ScriptEngine::opc0TEXI(EngineState * state, EngineFrame * frame) {
	_engine->enqueueObject(kHightlightExits, 0);
}

void ScriptEngine::opc1PTXT(EngineState * state, EngineFrame * frame) {
	word tid = state->pop();
	_engine->enqueueText(kTextPlain, frame->dest, frame->src, tid);
}

void ScriptEngine::opc2PNEW(EngineState * state, EngineFrame * frame) {
	_engine->enqueueText(kTextNewLine, frame->dest, frame->src, 0);
}

void ScriptEngine::opc3PTNE(EngineState * state, EngineFrame * frame) {
	word tid = state->pop();
	_engine->enqueueText(kTextPlain, frame->dest, frame->src, tid);
	_engine->enqueueText(kTextNewLine, frame->dest, frame->src, 0);
}

void ScriptEngine::opc4PNTN(EngineState * state, EngineFrame * frame) {
	word tid = state->pop();
	_engine->enqueueText(kTextNewLine, frame->dest, frame->src, 0);
	_engine->enqueueText(kTextPlain, frame->dest, frame->src, tid);
	_engine->enqueueText(kTextNewLine, frame->dest, frame->src, 0);
}

void ScriptEngine::opc5PNUM(EngineState * state, EngineFrame * frame) {
	word tid = state->pop();
	_engine->enqueueText(kTextNumber, frame->dest, frame->src, tid);
}

void ScriptEngine::opc6P2(EngineState * state, EngineFrame * frame) {
	state->push(2);
}

void ScriptEngine::opc7PLBG(EngineState * state, EngineFrame * frame) {
	state->pop();
	op00NOOP(0xc7);
}

void ScriptEngine::opc8PLAW(EngineState * state, EngineFrame * frame) {
	state->pop();
	op00NOOP(0xc8);
}

void ScriptEngine::opc9WAIT(EngineState * state, EngineFrame * frame) {
	op00NOOP(0xc9);
}

void ScriptEngine::opcaTIME(EngineState * state, EngineFrame * frame) {
	for (uint i = 0; i < 3; i++) // We skip year, month and date
		state->push(0x00);

	uint32 totalPlayTime = _engine->getTotalPlayTime() / 1000; // In seconds
	word hours = totalPlayTime / 3600;
	totalPlayTime %= 3600;
	state->push(hours);
	word minutes = totalPlayTime / 60;
	totalPlayTime %= 60;
	state->push(minutes);
	state->push(totalPlayTime);
	debug("Saved time: h[%d] m[%d] s[%d]", hours, minutes, totalPlayTime);
}

void ScriptEngine::opcbDAY(EngineState * state, EngineFrame * frame) {
	// Probaby irrelevant, so we push Day [9]
	state->push(9);
}

void ScriptEngine::opccCHLD(EngineState * state, EngineFrame * frame) {
	bool recursive = state->pop() != 0;
	word obj = state->pop();
	Common::Array<ObjID> children = _world->getChildren(obj, recursive);
	for (Common::Array<ObjID>::const_iterator it = children.begin(); it != children.end(); it++) {
		state->push(*it);
	}
	state->push(children.size());
}

void ScriptEngine::opcdNCHLD(EngineState * state, EngineFrame * frame) {
	bool recursive = state->pop() != 0;
	word obj = state->pop();
	Common::Array<ObjID> children = _world->getChildren(obj, recursive);
	state->push(children.size());
}

void ScriptEngine::opceVERS(EngineState * state, EngineFrame * frame) {
	state->push(86);
}

void ScriptEngine::opcfPSCE(EngineState * state, EngineFrame * frame) {
	state->push(0); //Not release
}

void ScriptEngine::opd0P1(EngineState * state, EngineFrame * frame) {
	state->push(1);
}

void ScriptEngine::opd1GOBD(EngineState * state, EngineFrame * frame) {
	word obj = state->pop();
	Common::Rect bounds = _engine->getObjBounds(obj);
	state->push(bounds.width());
	state->push(bounds.height());
}

void ScriptEngine::opd2GOVP(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	state->push(_engine->getOverlapPercent(b, a));
}

void ScriptEngine::opd3CAPC(EngineState * state, EngineFrame * frame) {
	word obj = state->pop();
	_world->captureChildren(obj);
}

void ScriptEngine::opd4RELC(EngineState * state, EngineFrame * frame) {
	word obj = state->pop();
	_world->releaseChildren(obj);
}

void ScriptEngine::opd5DLOG(EngineState * state, EngineFrame * frame) {
	word txt = state->pop();
	op00NOOP(0xd5);
}

void ScriptEngine::opd6ACMD(EngineState * state, EngineFrame * frame) {
	_engine->activateCommand((ControlAction)state->pop());
}

void ScriptEngine::opd7LOSE(EngineState * state, EngineFrame * frame) {
	_engine->loseGame();
}

void ScriptEngine::opd8WIN(EngineState * state, EngineFrame * frame) {
	_engine->winGame();
}

void ScriptEngine::opd9SLEEP(EngineState * state, EngineFrame * frame) {
	word ticks = state->pop();
	g_system->delayMillis((ticks / 60) * 1000);
	_engine->preparedToRun();
}

void ScriptEngine::opdaCLICK(EngineState * state, EngineFrame * frame) {
	_engine->updateState();
	_engine->clickToContinue();
}

void ScriptEngine::opdbROBQ(EngineState * state, EngineFrame * frame) {
	_engine->runObjQueue();
}

void ScriptEngine::opdcRSQ(EngineState * state, EngineFrame * frame) {
	op00NOOP(0xdc);
}

void ScriptEngine::opddRTQ(EngineState * state, EngineFrame * frame) {
	_engine->printTexts();
}

void ScriptEngine::opdeUPSC(EngineState * state, EngineFrame * frame) {
	_engine->updateState();
}

void ScriptEngine::opdfFMAI(EngineState * state, EngineFrame * frame) {
	word ticks = state->pop();
	g_system->delayMillis((ticks / 60) * 1000);
	_engine->revert();
}

void ScriptEngine::ope0CHGR(EngineState * state, EngineFrame * frame) {
	state->pop();
}

void ScriptEngine::ope1CHSO(EngineState * state, EngineFrame * frame) {
	state->pop();
}

void ScriptEngine::ope2MDIV(EngineState * state, EngineFrame * frame) {
	word b = state->pop();
	word a = state->pop();
	a *= b;
	word c = state->pop();
	a /= c;
	state->push(a | 0);
}

void ScriptEngine::ope3UPOB(EngineState * state, EngineFrame * frame) {
	word obj = state->pop();
	_world->updateObj(obj);
}

void ScriptEngine::ope4PLEV(EngineState * state, EngineFrame * frame) {
	state->push(0);
}

void ScriptEngine::ope5WEV(EngineState * state, EngineFrame * frame) {
	op00NOOP(0xe5);
}

void ScriptEngine::ope6GFIB(EngineState * state, EngineFrame * frame) {
	state->push(0);
	op00NOOP(0xe6);
}

void ScriptEngine::ope7CFIB(EngineState * state, EngineFrame * frame) {
	state->pop();
	op00NOOP(0xe7);
}

void ScriptEngine::op00NOOP(byte op) {
	debug("SCRIPT: Opcode not implemented => %x", op);
}



ScriptAsset::ScriptAsset(ObjID id, Container * container) {
	_id = id;
	_container = container;
	_ip = 0x0;
	loadInstructions();
}

void ScriptAsset::reset() {
	_ip = 0x0;
}

uint8 ScriptAsset::fetch() {
	uint8 ins = _instructions[_ip];
	_ip++;
	return ins;
}

bool ScriptAsset::hasNext() {
	return _ip < _instructions.size();
}

void ScriptAsset::branch(word amount) {
	_ip += amount;
}

ObjID ScriptAsset::getId() {
	return _id;
}

void ScriptAsset::loadInstructions() {
	uint32 amount = _container->getItemByteSize(_id);
	Common::SeekableReadStream *res = _container->getItem(_id);
	for (uint i = 0; i < amount; i++) {
		_instructions.push_back(res->readByte());
	}
	debug(2, "SCRIPT: Load %d instructions for script %d", amount, _id);
}

} // End of namespace MacVenture