aboutsummaryrefslogtreecommitdiff
path: root/engines/toon/script_func.cpp
blob: 761852b66fa20e6fa5c9a08bb9bed1f7f5585325 (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
/* 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/debug.h"
#include "common/system.h"

#include "toon/script_func.h"
#include "toon/script.h"
#include "toon/state.h"
#include "toon/toon.h"
#include "toon/anim.h"
#include "toon/hotspot.h"
#include "toon/drew.h"
#include "toon/flux.h"

namespace Toon {

#define SetOpcodeTable(x) table = &x;
#define Opcode(x) table->push_back(new OpcodeV2(this, &ScriptFunc::x))
#define OpcodeUnImpl() table->push_back(new OpcodeV2(this, 0))

ScriptFunc::ScriptFunc(ToonEngine *vm) {
	Common::Array<const OpcodeV2 *> *table = 0;

	_vm = vm;
	_opcodes.reserve(176);
	SetOpcodeTable(_opcodes);

	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Change_Actor_X_And_Y);                   //     dd offset sub_2A710
	Opcode(sys_Cmd_Init_Talking_Character);             //     dd offset sub_2A4E0
	Opcode(sys_Cmd_Draw_Actor_Standing);                    //     dd offset sub_2A650
	Opcode(sys_Cmd_Get_Actor_X);                            //     dd offset sub_2ADC0
	Opcode(sys_Cmd_Get_Actor_Y);                            //     dd offset sub_2ADD0
	Opcode(sys_Cmd_Get_Actor_Facing);                       //     dd offset sub_2A790
	Opcode(sys_Cmd_Get_Last_Scene);                     //     dd offset sub_29F80
	Opcode(sys_Cmd_Debug_Print);                            //     dd offset sub_2A510
	Opcode(sys_Cmd_Flip_Screens);                           //     dd offset sub_2A180
	Opcode(sys_Cmd_Play_Flic);                              //     dd offset sub_2A080
	Opcode(sys_Cmd_Force_Facing);                           //     dd offset sub_29F90
	Opcode(sys_Cmd_Restart_Thread);                     //     dd offset sub_29F30
	Opcode(sys_Cmd_Walk_Actor_To_Point);                    //     dd offset sub_2A440
	Opcode(sys_Cmd_Set_Sack_Visible);                       //     dd offset sub_29920
	Opcode(sys_Cmd_Set_Actor_Facing);                       //     dd offset sub_2AD60
	Opcode(sys_Cmd_Confiscate_Inventory);                   //     dd offset sub_29EB0
	Opcode(sys_Cmd_Character_Talks);                        //     dd offset sub_29F00
	Opcode(sys_Cmd_Visited_Scene);                          //     dd offset sub_29E80
	Opcode(sys_Cmd_Query_Rif_Flag);                     //     dd offset sub_29D20
	Opcode(sys_Cmd_Query_Scroll);                           //     dd offset sub_29D60
	Opcode(sys_Cmd_Set_Initial_Location);                   //     dd offset sub_2AD80
	Opcode(sys_Cmd_Make_Line_Non_Walkable);             //     dd offset sub_29FC0
	Opcode(sys_Cmd_Make_Line_Walkable);                 //     dd offset sub_2A050
	Opcode(sys_Cmd_Walk_Actor_On_Condition);                //     dd offset sub_29D70
	Opcode(sys_Cmd_Set_Actor_Facing_Point);             //     dd offset sub_29E60
	Opcode(sys_Cmd_Set_Inventory_Slot);                 //     dd offset sub_2B0D0
	Opcode(sys_Cmd_Get_Inventory_Slot);                 //     dd offset sub_2B0F0
	Opcode(sys_Cmd_Add_Item_To_Inventory);                  //     dd offset sub_2AE50
	Opcode(sys_Cmd_Set_Actor_RGB_Modifiers);                //     dd offset sub_29CA0
	Opcode(sys_Cmd_Init_Conversation_AP);                   //     dd offset sub_2B130
	Opcode(sys_Cmd_Actor_Talks);                            //     dd offset sub_2ADA0
	Opcode(sys_Cmd_Say_Lines);                              //     dd offset sub_29B20
	Opcode(sys_Cmd_Set_Rif_Flag);                           //     dd offset sub_2A320
	Opcode(sys_Cmd_Empty_Inventory);                        //     dd offset sub_2AE10
	Opcode(sys_Cmd_Set_Anim_Scale_Size);                    //     dd offset sub_29BD0
	Opcode(sys_Cmd_Delete_Item_From_Inventory);         //     dd offset sub_2AE70
	Opcode(sys_Cmd_Specific_Item_In_Inventory);         //     dd offset sub_2A740
	Opcode(sys_Cmd_Run_Script);                         //     dd offset sub_29AF0
	Opcode(sys_Cmd_Query_Game_Flag);                        //     dd offset sub_2A3E0
	Opcode(sys_Cmd_Reset_Game_Flag);                        //     dd offset sub_2A420
	Opcode(sys_Cmd_Set_Game_Flag);                          //     dd offset sub_2A400
	Opcode(sys_Cmd_Create_Mouse_Item);                      //     dd offset sub_2A4B0
	Opcode(sys_Cmd_Destroy_Mouse_Item);                 //     dd offset sub_2A4D0
	Opcode(sys_Cmd_Get_Mouse_State);                        //     dd offset sub_2A860
	Opcode(sys_Cmd_Hide_Mouse);                         //     dd offset sub_2A5D0
	Opcode(sys_Cmd_Exit_Conversation);                      //     dd offset sub_29AE0
	Opcode(sys_Cmd_Set_Mouse_Pos);                          //     dd offset sub_2A810
	Opcode(sys_Cmd_Show_Mouse);                         //     dd offset sub_2A5F0
	Opcode(sys_Cmd_In_Close_Up);                            //     dd offset sub_29FB0
	Opcode(sys_Cmd_Set_Scroll_Lock);                        //     dd offset sub_298B0
	Opcode(sys_Cmd_Fill_Area_Non_Walkable);             //     dd offset sub_29FF0
	Opcode(sys_Cmd_Set_Scroll_Coords);                      //     dd offset sub_298D0
	Opcode(sys_Cmd_Hide_Cutaway);                           //     dd offset sub_2A0F0
	Opcode(sys_Cmd_Show_Cutaway);                           //     dd offset sub_2A100
	Opcode(sys_Cmd_Pause_Ticks);                            //     dd offset sub_2A360
	Opcode(sys_Cmd_In_Conversation);                        //     dd offset sub_29C60
	Opcode(sys_Cmd_Character_Talking);                      //     dd offset sub_29C70
	Opcode(sys_Cmd_Set_Flux_Facing_Point);                  //     dd offset sub_29980
	Opcode(sys_Cmd_Set_Flux_Facing);                        //     dd offset sub_299A0
	Opcode(sys_Cmd_Set_Flux_Coords);                        //     dd offset sub_299C0
	Opcode(sys_Cmd_Set_Flux_Visible);                       //     dd offset sub_299F0
	Opcode(sys_Cmd_Get_Flux_X);                         //     dd offset sub_29A40
	Opcode(sys_Cmd_Get_Flux_Y);                         //     dd offset sub_29A50
	Opcode(sys_Cmd_Get_Flux_Facing);                        //     dd offset sub_29A60
	Opcode(sys_Cmd_Get_Flux_Flags);                     //     dd offset sub_29A70
	Opcode(sys_Cmd_Query_Flux_Coords);                      //     dd offset sub_29A90
	Opcode(sys_Cmd_Have_A_Conversation);                    //     dd offset sub_2B110
	Opcode(sys_Cmd_Walk_Flux_To_Point);                 //     dd offset sub_29AC0
	Opcode(sys_Cmd_Get_Actor_Final_X);                      //     dd offset sub_29940
	Opcode(sys_Cmd_Get_Actor_Final_Y);                      //     dd offset sub_29960
	Opcode(sys_Cmd_Query_Scene_Anim_Loaded);                //     dd offset sub_29870
	Opcode(sys_Cmd_Play_Flux_Anim);                     //     dd offset sub_29820
	Opcode(sys_Cmd_Set_Anim_Priority);                      //     dd offset sub_29790
	Opcode(sys_Cmd_Place_Scene_Anim);                       //     dd offset sub_2A7A0
	Opcode(sys_Cmd_Update_Scene_Animations);                //     dd offset sub_2AE30
	Opcode(sys_Cmd_Get_Drew_Scale);                     //     dd offset sub_297E0
	Opcode(sys_Cmd_Query_Drew_Flags);                       //     dd offset sub_29800
	Opcode(sys_Cmd_Set_Music);                              //     dd offset sub_29720
	Opcode(sys_Cmd_Query_Speech);                           //     dd offset sub_296D0
	Opcode(sys_Cmd_Enter_New_Scene);                        //     dd offset sub_2A550
	Opcode(sys_Cmd_Enter_Same_Scene);                       //     dd offset sub_2ADE0
	Opcode(sys_Cmd_Is_Pixel_Walkable);                      //     dd offset sub_2A4F0
	Opcode(sys_Cmd_Show_Screen);                            //     dd offset sub_2A0C0
	Opcode(sys_Cmd_Hide_Screen);                            //     dd offset sub_2A0F0
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_295D0
	Opcode(sys_Cmd_Set_Special_Enter_X_And_Y);              //     dd offset sub_2A590
	Opcode(sys_Cmd_Get_Mouse_X);                            //     dd offset sub_296B0
	Opcode(sys_Cmd_Get_Mouse_Y);                            //     dd offset sub_296C0
	Opcode(sys_Cmd_Fade_Palette);                           //     dd offset sub_29650
	Opcode(sys_Cmd_Music_Enabled);                          //     dd offset sub_29620
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_295F0
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_29610
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Random);                             //     dd offset sub_2A600
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Wait_Key);                               //     dd offset sub_2AE20
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Draw_Scene_Anim_WSA_Frame_To_Back);      //     dd offset sub_2A940
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Set_Scene_Anim_Wait);                    //     dd offset sub_2A870
	Opcode(sys_Cmd_Init_Scene_Anim);                        //     dd offset sub_2AC60
	Opcode(sys_Cmd_Set_Scene_Animation_Active_Flag);        //     dd offset sub_2AB10
	Opcode(sys_Cmd_Draw_Scene_Anim_WSA_Frame);              //     dd offset sub_2A8D0
	Opcode(sys_Cmd_Move_Scene_Anim);                        //     dd offset sub_2AA90
	Opcode(sys_Cmd_Run_Actor_Default_Script);               //     dd offset sub_2A4E0
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Set_Location_Data);                      //     dd offset sub_2AE90
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Set_CountDown_Timer);                    //     dd offset sub_2AFC0
	Opcode(sys_Cmd_Query_CountDown_Timer);                  //     dd offset sub_2AFE0
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Proceed_To_Next_Chapter);                //     dd offset sub_2AFF0
	Opcode(sys_Cmd_Play_Sfx_Plus);                          //     dd offset sub_2A1D0
	Opcode(sys_Cmd_Play_Sfx);                               //     dd offset sub_2A1A0
	Opcode(sys_Cmd_Set_Ambient_Sfx);                        //     dd offset sub_2A260
	Opcode(sys_Cmd_Kill_Ambient_Sfx);                       //     dd offset sub_2A300
	Opcode(sys_Cmd_Set_Ambient_Sfx_Plus);                   //     dd offset sub_2A290
	Opcode(sys_Cmd_Set_Ambient_Volume);                 //     dd offset sub_2A240
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Freeze_Scene_Animation);             //     dd offset sub_2AB90
	Opcode(sys_Cmd_Unfreeze_Scene_Animation);               //     dd offset sub_2ABB0
	Opcode(sys_Cmd_Scene_Animation_Frozen);             //     dd offset sub_2ABD0
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Set_Script_Game_Data_Global);            //     dd offset sub_2ABF0
	Opcode(sys_Cmd_Get_Script_Game_Data_Global);            //     dd offset sub_2AC30
	Opcode(sys_Cmd_Say_Line);                               //     dd offset loc_2A190
	Opcode(sys_Cmd_Knight_Puzzle_Get_Coord);                //     dd offset sub_2A110
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Add_Scene_Anim);                     //     dd offset sub_2AC60
	Opcode(sys_Cmd_Remove_Scene_Anim);                      //     dd offset sub_2ACE0
	Opcode(sys_Cmd_Disable_Timer);                          //     dd offset sub_2AD00
	Opcode(sys_Cmd_Enable_Timer);                           //     dd offset sub_2AD20
	Opcode(sys_Cmd_Set_Timer);                              //     dd offset sub_2AD40
	Opcode(sys_Cmd_Set_Palette_Color);                      //     dd offset sub_2B020
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Number_Of_NPCs);                     //     dd offset loc_2A190
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
	Opcode(sys_Cmd_Get_Config_Language);                    //     dd offset sub_2B0C0
	Opcode(sys_Cmd_Dummy);                                  //     dd offset sub_2B160
}

ScriptFunc::~ScriptFunc(void) {
	while (!_opcodes.empty()) {
		const OpcodeV2 *temp = _opcodes.back();
		_opcodes.pop_back();
		delete temp;
	}
}

char *GetText(int32 i, EMCState *state) {
	short stack = stackPos(i);
	unsigned short textoffset = READ_BE_UINT16(&((unsigned short *)(state->dataPtr->text))[stack]);
	char *text = (char *)&state->dataPtr->text[textoffset];
	return text;
}

int32 ScriptFunc::sys_Cmd_Dummy(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Change_Actor_X_And_Y(EMCState *state) {
	_vm->getDrew()->forcePosition(stackPos(0), stackPos(1));
	//_vm->getDrew()->resetScale();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Init_Talking_Character(EMCState *state) {
	// really does nothing in original
	return 0;
}

int32 ScriptFunc::sys_Cmd_Draw_Actor_Standing(EMCState *state) {

	int32 arg1 = stackPos(0);
	int32 arg2 = stackPos(1);
	int32 arg3 = stackPos(2);

	// WORKAROUND: In scene 19 (transformed barn), Drew disappears when it shouldn't. It seems like a script bug
	//				even if the game works correctly at this point
	//				We need a special case for it then.
	if (_vm->state()->_currentScene == 19 && arg3 == 1 && arg1 < 0) {
		arg1 = 1;
	}

	if (arg2 > -1)
		_vm->getDrew()->forceFacing(arg2);

	if (arg1 < 0) {
		_vm->getDrew()->setVisible(false);
	} else {
		_vm->getDrew()->setVisible(true);
		_vm->getDrew()->playStandingAnim();
	}
	return 0;
}

int32 ScriptFunc::sys_Cmd_Get_Actor_X(EMCState *state) {
	return _vm->getDrew()->getX();
}

int32 ScriptFunc::sys_Cmd_Get_Actor_Y(EMCState *state) {
	return _vm->getDrew()->getY();
}

int32 ScriptFunc::sys_Cmd_Get_Actor_Final_X(EMCState *state) {
	return _vm->getDrew()->getFinalX();
}

int32 ScriptFunc::sys_Cmd_Get_Actor_Final_Y(EMCState *state) {
	return _vm->getDrew()->getFinalY();
}

int32 ScriptFunc::sys_Cmd_Get_Actor_Facing(EMCState *state) {
	return _vm->getDrew()->getFacing();
}

int32 ScriptFunc::sys_Cmd_Get_Last_Scene(EMCState *state) {
	return _vm->state()->_lastVisitedScene;
}

int32 ScriptFunc::sys_Cmd_Debug_Print(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Flip_Screens(EMCState *state) {
	_vm->flipScreens();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Play_Flic(EMCState *state) {
	Common::String stateText = GetText(0, state);
	Common::String name;

	// workaround for the video of the beginning
	if (stateText.contains("209")) {
		name = stateText;
	} else {
		name = _vm->createRoomFilename(stateText.c_str());
	}

	int32 stopMusic = stackPos(2);
	_vm->getMoviePlayer()->play(name, stopMusic);
	return 0;
}

int32 ScriptFunc::sys_Cmd_Force_Facing(EMCState *state) {
	_vm->getDrew()->setFacing(stackPos(0));
	_vm->getDrew()->playStandingAnim();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Restart_Thread(EMCState *state) {

	int32 sceneId = stackPos(0);
	_vm->getScript()->init(&_vm->getSceneAnimationScript(sceneId)->_state, _vm->getSceneAnimationScript(sceneId)->_data);
	_vm->getScript()->start(&_vm->getSceneAnimationScript(sceneId)->_state, 9 + sceneId);

	if (!stackPos(1))
		_vm->setSceneAnimationScriptUpdate(false);

	return 0;
}

int32 ScriptFunc::sys_Cmd_Walk_Actor_To_Point(EMCState *state) {
	return _vm->getDrew()->walkTo(stackPos(0), stackPos(1));
}

int32 ScriptFunc::sys_Cmd_Set_Sack_Visible(EMCState *state) {
	_vm->state()->_sackVisible = stackPos(0) > 0;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Actor_Facing(EMCState *state) {
	_vm->getDrew()->forceFacing(stackPos(0));
	_vm->getDrew()->playStandingAnim();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Confiscate_Inventory(EMCState *state) {
	for (int32 i = 0; i < _vm->state()->_numInventoryItems; i++) {
		_vm->state()->_confiscatedInventory[_vm->state()->_numConfiscatedInventoryItems] = _vm->state()->_inventory[i];
		_vm->state()->_numConfiscatedInventoryItems++;
	}
	_vm->state()->_numInventoryItems = 0;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Character_Talks(EMCState *state) {
	_vm->characterTalk(stackPos(0), false);
	//_vm->characterTalk(stackPos(0));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Visited_Scene(EMCState *state) {
	return _vm->state()->_locations[stackPos(0)]._visited ? 1 : 0;
}

int32 ScriptFunc::sys_Cmd_Query_Rif_Flag(EMCState *state) {

	int32 hs = _vm->getHotspots()->findBasedOnCorner(stackPos(0), stackPos(1));
	if (hs >= 0)
		return _vm->getHotspots()->get(hs)->getData(stackPos(2));

	return 0;
}

int32 ScriptFunc::sys_Cmd_Query_Scroll(EMCState *state) {
	return _vm->state()->_currentScrollValue;
}

int32 ScriptFunc::sys_Cmd_Set_Initial_Location(EMCState *state) {
	int32 initialLocation = stackPos(0);
	_vm->state()->_currentScene = initialLocation;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Make_Line_Non_Walkable(EMCState *state) {
	_vm->makeLineNonWalkable(stackPos(0), stackPos(1), stackPos(2), stackPos(3));

	// we have to store some info for savegame
	_vm->getSaveBufferStream()->writeSint16BE(2); // 2 = sys_Cmd_Make_Line_Non_Walkable
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(0));
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(1));
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(2));
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(3));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Make_Line_Walkable(EMCState *state) {
	_vm->makeLineWalkable(stackPos(0), stackPos(1), stackPos(2), stackPos(3));

	// we have to store some info for savegame
	_vm->getSaveBufferStream()->writeSint16BE(3); // 3 = sys_Cmd_Make_Line_Walkable
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(0));
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(1));
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(2));
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(3));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Walk_Actor_On_Condition(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Actor_Facing_Point(EMCState *state) {
	int32 fx = stackPos(0);
	int32 fy = stackPos(1);
	_vm->getDrew()->setFacing(_vm->getDrew()->getFacingFromDirection(fx - _vm->getDrew()->getX(), fy - _vm->getDrew()->getY()));
	return 1;
}

int32 ScriptFunc::sys_Cmd_Set_Inventory_Slot(EMCState *state) {
	_vm->state()->_inventory[stackPos(1)] = stackPos(0);
	return 0;
}

int32 ScriptFunc::sys_Cmd_Get_Inventory_Slot(EMCState *state) {
	return _vm->state()->_inventory[stackPos(0)];
}

int32 ScriptFunc::sys_Cmd_Add_Item_To_Inventory(EMCState *state) {
	_vm->addItemToInventory(stackPos(0));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Actor_RGB_Modifiers(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Init_Conversation_AP(EMCState *state) {
	_vm->initCharacter(stackPos(0), stackPos(1), stackPos(2), stackPos(3));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Actor_Talks(EMCState *state) {
	_vm->characterTalk(stackPos(0), false);
	return 0;
}

int32 ScriptFunc::sys_Cmd_Say_Lines(EMCState *state) {

	// WORKAROUND: In the scene 4 (Castle), if you click twice on the closed door, Drew disappears
	//				the script makes him disappear for the custom animation and not reappear.
	if (_vm->state()->_currentScene == 4 && stackPos(1) == 562) {
		_vm->getDrew()->setVisible(true);
	}

	_vm->sayLines(stackPos(0), stackPos(1));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Rif_Flag(EMCState *state) {
	int32 hs = _vm->getHotspots()->findBasedOnCorner(stackPos(0), stackPos(1));
	if (hs >= 0)
		_vm->getHotspots()->get(hs)->setData(stackPos(2), stackPos(3));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Empty_Inventory(EMCState *state) {

	for (int32 i = 0; i < _vm->state()->_numInventoryItems; i++)
		_vm->state()->_inventory[i] = 0;

	_vm->state()->_numInventoryItems = 0;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Anim_Scale_Size(EMCState *state) {
	int32 animID = stackPos(0);
	int32 scale = stackPos(1);

	SceneAnimation *sceneAnim = _vm->getSceneAnimation(animID);
	if (sceneAnim) {
		sceneAnim->_animInstance->setUseMask(true);
		sceneAnim->_animInstance->setScale(scale, true);
	}
	return 0;
}
int32 ScriptFunc::sys_Cmd_Delete_Item_From_Inventory(EMCState *state) {
	for (int32 i = 0; i < _vm->state()->_numInventoryItems; i++) {
		if (stackPos(0) == _vm->state()->_inventory[i])
			_vm->state()->_inventory[i] = 0;
	}
	_vm->rearrangeInventory();
	return 0;
}
int32 ScriptFunc::sys_Cmd_Specific_Item_In_Inventory(EMCState *state) {
	for (int32 i = 0; i < _vm->state()->_numInventoryItems; i++) {
		if (_vm->state()->_inventory[i] == stackPos(0))
			return 1;
	}
	if (_vm->state()->_mouseState == stackPos(0))
		return 1;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Run_Script(EMCState *state) {
	return _vm->runEventScript(_vm->getMouseX(), _vm->getMouseY(), 2, stackPos(0), 0);
}

int32 ScriptFunc::sys_Cmd_Query_Game_Flag(EMCState *state) {
	int32 arg = stackPos(0);
	return (_vm->state()->_gameFlag[arg >> 3] & (1 << (arg & 7))) != 0;
}

int32 ScriptFunc::sys_Cmd_Reset_Game_Flag(EMCState *state) {
	int32 arg = stackPos(0);
	_vm->state()->_gameFlag[arg >> 3] &= ~(1 << (arg & 7));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Game_Flag(EMCState *state) {
	int32 arg = stackPos(0);
	_vm->state()->_gameFlag[arg >> 3] |= (1 << (arg & 7));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Create_Mouse_Item(EMCState *state) {
	_vm->createMouseItem(stackPos(0));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Destroy_Mouse_Item(EMCState *state) {
	_vm->deleteMouseItem();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Get_Mouse_State(EMCState *state) {
	return _vm->state()->_mouseState;
}

int32 ScriptFunc::sys_Cmd_Hide_Mouse(EMCState *state) {
	_vm->state()->_mouseHidden = true;
	//if (Game.MouseHiddenCount > 0) Game.MouseHiddenCount = 1;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Exit_Conversation(EMCState *state) {
	_vm->state()->_exitConversation = true;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Mouse_Pos(EMCState *state) {
	if (_vm->state()->_inCloseUp) {
		_vm->_system->warpMouse(stackPos(0), stackPos(1));
	} else {
		_vm->_system->warpMouse(stackPos(0) - _vm->state()->_currentScrollValue, stackPos(1));
	}
	return 0;
}

int32 ScriptFunc::sys_Cmd_Show_Mouse(EMCState *state) {
	_vm->state()->_mouseHidden = false;
	//if (Game.MouseHiddenCount < 0) Game.MouseHiddenCount = 0;
	return 0;
}

int32 ScriptFunc::sys_Cmd_In_Close_Up(EMCState *state) {
	return _vm->state()->_inCloseUp;
}

int32 ScriptFunc::sys_Cmd_Set_Scroll_Lock(EMCState *state) {
	_vm->state()->_currentScrollLock = stackPos(0) > 0;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Fill_Area_Non_Walkable(EMCState *state) {
	_vm->getMask()->floodFillNotWalkableOnMask(stackPos(0), stackPos(1));

	// we have to store some info for savegame
	_vm->getSaveBufferStream()->writeSint16BE(4); // 4 = sys_Cmd_Make_Line_Walkable
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(0));
	_vm->getSaveBufferStream()->writeSint16BE(stackPos(1));

	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Scroll_Coords(EMCState *state) {
	_vm->state()->_currentScrollValue = stackPos(0);
	return 0;
}

int32 ScriptFunc::sys_Cmd_Hide_Cutaway(EMCState *state) {
	_vm->hideCutaway();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Show_Cutaway(EMCState *state) {
	_vm->showCutaway("");
	return 0;
}

int32 ScriptFunc::sys_Cmd_Pause_Ticks(EMCState *state) {

	if (!_vm->isUpdatingSceneAnimation() || _vm->getScriptRegionNested() > 0) {
		if (stackPos(1))
			_vm->waitTicks(stackPos(0), true);
		else
			_vm->waitTicks(stackPos(0), false);
	} else {
		uint32 sceneId = _vm->getCurrentUpdatingSceneAnimation();
		uint32 waitTicks = stackPos(0);
		if (waitTicks < 1) waitTicks = 1;

		waitTicks *= _vm->getTickLength();

		if (sceneId < 40) {
			int32 nextTicks = waitTicks + _vm->getSceneAnimationScript(sceneId)->_lastTimer;
			if (nextTicks < _vm->getOldMilli())
				_vm->getSceneAnimationScript(sceneId)->_lastTimer = _vm->getOldMilli() + waitTicks;
			else
				_vm->getSceneAnimationScript(sceneId)->_lastTimer = nextTicks;
		}
		return 0;
	}

	return 0;
}

int32 ScriptFunc::sys_Cmd_In_Conversation(EMCState *state) {
	return _vm->state()->_inConversation;
}

int32 ScriptFunc::sys_Cmd_Character_Talking(EMCState *state) {
	int32 characterId = stackPos(0);
	Character *character = _vm->getCharacterById(characterId);
	if (character)
		return character->isTalking();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Flux_Facing_Point(EMCState *state) {
	int32 fx = stackPos(0);
	int32 fy = stackPos(1);
	_vm->getFlux()->setFacing(_vm->getFlux()->getFacingFromDirection(fx - _vm->getFlux()->getX(), fy - _vm->getFlux()->getY()));
	if (_vm->getFlux()->getFlag() == 0)  // don't reset the animation unless Flux is in idle mode
		_vm->getFlux()->playStandingAnim();
	return 1;
}

int32 ScriptFunc::sys_Cmd_Set_Flux_Facing(EMCState *state) {
	_vm->getFlux()->forceFacing(stackPos(0));
	if (_vm->getFlux()->getFlag() == 0) // don't reset the animation unless Flux is in idle mode
		_vm->getFlux()->playStandingAnim();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Flux_Coords(EMCState *state) {
	_vm->getFlux()->stopWalk();
	_vm->getFlux()->forcePosition(stackPos(0), stackPos(1));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Flux_Visible(EMCState *state) {
	_vm->getFlux()->setVisible(stackPos(0) > 0);
	return 0;
}

int32 ScriptFunc::sys_Cmd_Get_Flux_X(EMCState *state) {
	return _vm->getFlux()->getX();
}

int32 ScriptFunc::sys_Cmd_Get_Flux_Y(EMCState *state) {
	return _vm->getFlux()->getY();
}

int32 ScriptFunc::sys_Cmd_Get_Flux_Facing(EMCState *state) {
	return _vm->getFlux()->getFacing();
}

int32 ScriptFunc::sys_Cmd_Get_Flux_Flags(EMCState *state) {
	return (_vm->getFlux()->getFlag() & stackPos(0)) != 0;
}

int32 ScriptFunc::sys_Cmd_Query_Flux_Coords(EMCState *state) {
	return (stackPos(0) == _vm->getFlux()->getX()) && (stackPos(1) == _vm->getFlux()->getY());
}

int32 ScriptFunc::sys_Cmd_Have_A_Conversation(EMCState *state) {
	_vm->haveAConversation(stackPos(0));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Walk_Flux_To_Point(EMCState *state) {
	_vm->getFlux()->walkTo(stackPos(0), stackPos(1));
	return 1;
}

int32 ScriptFunc::sys_Cmd_Query_Scene_Anim_Loaded(EMCState *state) {
	return _vm->getSceneAnimation(stackPos(0))->_active;
}

int32 ScriptFunc::sys_Cmd_Play_Flux_Anim(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Anim_Priority(EMCState *state) {

	SceneAnimation *sceneAnim = _vm->getSceneAnimation(stackPos(0));
	sceneAnim->_animInstance->setLayerZ(stackPos(1));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Place_Scene_Anim(EMCState *state) {
	int32 sceneId = stackPos(0);
	int32 x = stackPos(1);
	int32 y = stackPos(2);
	int32 frame = stackPos(5);

	SceneAnimation *sceneAnim = _vm->getSceneAnimation(sceneId);
	sceneAnim->_animInstance->setPosition(x, y, sceneAnim->_animInstance->getZ(), false);
	sceneAnim->_animInstance->forceFrame(frame);
	_vm->setSceneAnimationScriptUpdate(false);
	return 0;
}

int32 ScriptFunc::sys_Cmd_Update_Scene_Animations(EMCState *state) {
	//debugC(0, 0xfff, "UpdateAnimations");
	return 0;
}

int32 ScriptFunc::sys_Cmd_Get_Drew_Scale(EMCState *state) {
	int32 scale = _vm->getDrew()->getScale();
	if (!scale)
		return 1024;
	return scale;
}

int32 ScriptFunc::sys_Cmd_Query_Drew_Flags(EMCState *state) {
	return (_vm->getDrew()->getFlag() & stackPos(0)) != 0;
}

int32 ScriptFunc::sys_Cmd_Set_Music(EMCState *state) {

	char *newMus = GetText(0, state);
	_vm->getAudioManager()->playMusic(_vm->state()->_locations[_vm->state()->_currentScene]._name, newMus);
	return 0;
}

int32 ScriptFunc::sys_Cmd_Query_Speech(EMCState *state) {
	if (_vm->getAudioManager()->voiceStillPlaying())
		return 1;
	else
		return 0;
}

int32 ScriptFunc::sys_Cmd_Enter_New_Scene(EMCState *state) {
	_vm->exitScene();
	_vm->getDrew()->forceFacing(stackPos(1));
	_vm->loadScene(stackPos(0));
	_vm->setSceneAnimationScriptUpdate(false);

	return 0;
}

int32 ScriptFunc::sys_Cmd_Enter_Same_Scene(EMCState *state) {
	_vm->exitScene();
	_vm->loadScene(_vm->state()->_currentScene);
	_vm->setSceneAnimationScriptUpdate(false);

	return 0;
}

int32 ScriptFunc::sys_Cmd_Is_Pixel_Walkable(EMCState *state) {
	return (_vm->getMask()->getData(stackPos(0), stackPos(1)) & 0x1f) > 0;
}

int32 ScriptFunc::sys_Cmd_Show_Screen(EMCState *state) {
	_vm->showCutaway(GetText(0, state));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Hide_Screen(EMCState *state) {
	_vm->hideCutaway();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Special_Enter_X_And_Y(EMCState *state) {
	_vm->state()->_nextSpecialEnterX = stackPos(0);
	_vm->state()->_nextSpecialEnterY = stackPos(1);
	return 0;
}

int32 ScriptFunc::sys_Cmd_Get_Mouse_X(EMCState *state) {
	return _vm->getMouseX();
}

int32 ScriptFunc::sys_Cmd_Get_Mouse_Y(EMCState *state) {
	return _vm->getMouseY();
}

int32 ScriptFunc::sys_Cmd_Fade_Palette(EMCState *state) {
	debugC(0, 0xfff, "fadePalette %d %d", stackPos(0), stackPos(1));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Music_Enabled(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Random(EMCState *state) {
	int32 randMin = stackPos(0);
	int32 randMax = stackPos(1);
	int32 t = 0;

	if (randMin > randMax) {
		t = randMin;
		randMin = randMax;
		randMax = t;
	}
	return _vm->randRange(randMin, randMax);
}

int32 ScriptFunc::sys_Cmd_Wait_Key(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Draw_Scene_Anim_WSA_Frame_To_Back(EMCState *state) {
	// draw the frame in the backbuffer (picture then)

	int32 animId = stackPos(0);
	int32 frame = stackPos(1);

	if (frame < 0)
		return 0;

	SceneAnimation *sceneAnim = _vm->getSceneAnimation(animId);

	if (sceneAnim->_active) {
		sceneAnim->_animInstance->setFrame(frame);
		sceneAnim->_animInstance->setAnimationRange(frame, frame);
		sceneAnim->_animInstance->stopAnimation();
		sceneAnim->_animInstance->renderOnPicture();

		// we have to store some info for savegame
		_vm->getSaveBufferStream()->writeSint16BE(1); // 1 = Draw_Scene_Anim_WSA_Frame_To_Back
		_vm->getSaveBufferStream()->writeSint16BE(frame);
		_vm->getSaveBufferStream()->writeSint16BE(strlen(sceneAnim->_animInstance->getAnimation()->_name) + 1);
		_vm->getSaveBufferStream()->write(sceneAnim->_animInstance->getAnimation()->_name, strlen(sceneAnim->_animInstance->getAnimation()->_name) + 1);
		_vm->getSaveBufferStream()->writeSint16BE(sceneAnim->_animInstance->getX());
		_vm->getSaveBufferStream()->writeSint16BE(sceneAnim->_animInstance->getY());
		_vm->getSaveBufferStream()->writeSint16BE(sceneAnim->_animInstance->getZ());
		_vm->getSaveBufferStream()->writeSint16BE(sceneAnim->_animInstance->getLayerZ());

	}
	return 1;
}

int32 ScriptFunc::sys_Cmd_Set_Scene_Anim_Wait(EMCState *state) {
	int32 sceneId = stackPos(0);
	int32 waitTicks = stackPos(1);
	if (waitTicks < 1) waitTicks = 1;

	// WORKAROUND : To fix the timing problem in the Gift-O-Matic
	//              The animation was too fast and the player was unable to click fast enough to get objects
	//              Here we slow down the animation
	if (_vm->state()->_currentScene == 24) {
		if (_vm->getCurrentUpdatingSceneAnimation() == 6) {
			if (waitTicks == 1) {
				waitTicks = 10;
				_vm->setSceneAnimationScriptUpdate(false);
			}
		}
	}

	// WORKAROUND : In Wolf place, the animation to move the pot was too fast and the player was unable to
	//              progress into the game.
	if (_vm->state()->_currentScene == 29) {
		if (_vm->getCurrentUpdatingSceneAnimation() == 8 || _vm->getCurrentUpdatingSceneAnimation() == 7) {
			if (waitTicks == 1) {
				waitTicks = 5;
				_vm->setSceneAnimationScriptUpdate(false);
			}
		}
	}

	// WORKAROUND : In transformed hangar, everything is too fast..
	if (_vm->state()->_currentScene == 19) {
		waitTicks = 10;
		_vm->setSceneAnimationScriptUpdate(false);
	}

	// WORKAROUND : Slow down just a little the guards dance animation so that the voices don't cut
	if (_vm->state()->_currentScene == 2 && (sceneId == 2 || sceneId == 3)) {
		waitTicks = 7;
		_vm->setSceneAnimationScriptUpdate(false);
	}

	waitTicks *= _vm->getTickLength();

	if (sceneId >= 0 && sceneId < 40) {
		int32 nextTicks = waitTicks + _vm->getSceneAnimationScript(sceneId)->_lastTimer;
		if (nextTicks < _vm->getOldMilli())
			_vm->getSceneAnimationScript(sceneId)->_lastTimer = _vm->getOldMilli() + waitTicks;
		else
			_vm->getSceneAnimationScript(sceneId)->_lastTimer = nextTicks;
	}
	return 0;
}

int32 ScriptFunc::sys_Cmd_Init_Scene_Anim(EMCState *state) {
	int32 animId = stackPos(0);
	int32 flags = stackPos(1);

	SceneAnimation *sceneAnim = _vm->getSceneAnimation(animId);
	if (sceneAnim->_active)
		return 0;

	delete sceneAnim->_animation;
	delete sceneAnim->_animInstance;

	sceneAnim->_animation = new Animation(_vm);
	sceneAnim->_animation->loadAnimation(GetText(12, state));
	sceneAnim->_animInstance = _vm->getAnimationManager()->createNewInstance(kAnimationScene);
	sceneAnim->_originalAnimInstance = sceneAnim->_animInstance;
	sceneAnim->_animInstance->setAnimation(sceneAnim->_animation);
	sceneAnim->_animInstance->setVisible((flags & 1) != 0);
	sceneAnim->_animInstance->setAnimationRange(stackPos(11), stackPos(11));
	sceneAnim->_animInstance->setFrame(stackPos(11));

	debugC(0, 0xfff, "Init Anim %s %d %d %d %d %d %d %d %d %d %d %d %d %d\n", GetText(12, state), stackPos(0), stackPos(1), stackPos(2), stackPos(3),
	       stackPos(4), stackPos(5), stackPos(6), stackPos(7), stackPos(8), stackPos(9), stackPos(10), stackPos(11),  stackPos(12));

	int32 dx = stackPos(4);
	int32 dy = stackPos(5);
	int32 x = stackPos(2);
	int32 layerZ = stackPos(3);

	if (dx == -2)
		sceneAnim->_animInstance->moveRelative(TOON_SCREEN_WIDTH, 0, 0);
	else if (dx < 0) {
		dx = sceneAnim->_animation->_x1;
	}
	else if (dx >= 0)
		sceneAnim->_animInstance->setX(dx);

	if (dy >= 0)
		sceneAnim->_animInstance->setY(dy);
	else
		dy = sceneAnim->_animation->_y1;

	if (flags & 0x20) {
		sceneAnim->_animInstance->setZ(_vm->getLayerAtPoint(x, layerZ));
		sceneAnim->_animInstance->setUseMask(true);
	}

	if (layerZ >= 0) {
		sceneAnim->_animInstance->setLayerZ(layerZ);
	} else {
		dy = dy + sceneAnim->_animation->_y2 - sceneAnim->_animation->_y1 - 1;
		sceneAnim->_animInstance->setLayerZ(dy);
	}

	sceneAnim->_animInstance->setId(stackPos(0));
	sceneAnim->_active = true;

	// add the animation to the list only if it's already visible
	if (flags & 1) {
		_vm->getAnimationManager()->addInstance(sceneAnim->_animInstance);
	}
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Scene_Animation_Active_Flag(EMCState *state) {
	int32 animId = stackPos(0);
	int32 activeFlag = stackPos(1);

	SceneAnimation *sceneAnim = _vm->getSceneAnimation(animId);

	if (sceneAnim->_active) {
		sceneAnim->_animInstance->setVisible(activeFlag > 0);

		if (activeFlag) {
			_vm->getAnimationManager()->addInstance(sceneAnim->_animInstance);
		}
	}

	return 0;
}

int32 ScriptFunc::sys_Cmd_Draw_Scene_Anim_WSA_Frame(EMCState *state) {
	int32 animId = stackPos(0);
	int32 frame = stackPos(1);

	if (frame < 0)
		return 0;

	SceneAnimation *sceneAnim = _vm->getSceneAnimation(animId);

	if (sceneAnim->_active) {
		sceneAnim->_animInstance->setAnimation(sceneAnim->_animation);
		sceneAnim->_animInstance->setFrame(frame);
		sceneAnim->_animInstance->setAnimationRange(frame, frame);
		sceneAnim->_animInstance->stopAnimation();
	}
	_vm->setSceneAnimationScriptUpdate(false);

	// WORKAROUND : Too fast animations...
	if (_vm->state()->_currentScene == 26 && animId == 22)
		_vm->pauseSceneAnimationScript(_vm->getCurrentUpdatingSceneAnimation(), 3);

	if (_vm->state()->_currentScene == 14) {
		if (animId == 3 || animId == 2 || animId == 4)
			_vm->pauseSceneAnimationScript(_vm->getCurrentUpdatingSceneAnimation(), 2);
		else if (animId == 20 || animId == 15 || animId == 21 || animId == 16 || animId == 17 || animId == 18)
			_vm->pauseSceneAnimationScript(_vm->getCurrentUpdatingSceneAnimation(), 1);
		else if (animId == 9) {
			_vm->pauseSceneAnimationScript(_vm->getCurrentUpdatingSceneAnimation(), 3);
		}
	}

	if (_vm->state()->_currentScene == 19 && _vm->getCurrentUpdatingSceneAnimation() == 0 ) {
		_vm->pauseSceneAnimationScript(_vm->getCurrentUpdatingSceneAnimation(), 6);
	}

	if (_vm->state()->_currentScene == 29) {
		if (animId == 16 || animId == 26 || animId == 36)
			_vm->pauseSceneAnimationScript(_vm->getCurrentUpdatingSceneAnimation(), 2);
	}

	return 0;
}

int32 ScriptFunc::sys_Cmd_Move_Scene_Anim(EMCState *state) {
	int32 animId = stackPos(0);

	SceneAnimation *sceneAnim = _vm->getSceneAnimation(animId);
	sceneAnim->_animInstance->moveRelative(stackPos(1), stackPos(2), 0);
	_vm->setSceneAnimationScriptUpdate(false);
	return 0;
}

int32 ScriptFunc::sys_Cmd_Run_Actor_Default_Script(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Location_Data(EMCState *state) {
	// initial setup of locations
	int32 locationId = stackPos(0);
	debugC(0, 0, "setlocationdata(%d) %s %x %s %s %d %d", locationId, GetText(1, state), stackPos(2), GetText(3, state), GetText(4, state), stackPos(5), stackPos(6));
	Common::strlcpy(_vm->state()->_locations[locationId]._name, GetText(1, state), 64);
	Common::strlcpy(_vm->state()->_locations[locationId]._music, GetText(3, state), 64);
	Common::strlcpy(_vm->state()->_locations[locationId]._cutaway, GetText(4, state), 64);
	_vm->state()->_locations[locationId]._flags = stackPos(2);
	_vm->state()->_locations[locationId]._visited = false;
	_vm->state()->_locations[locationId]._numSceneAnimations = stackPos(5);

	return 0;

}

int32 ScriptFunc::sys_Cmd_Set_CountDown_Timer(EMCState *state) {
	// game timer is in ticks
	_vm->state()->_gameTimer = stackPos(0) * _vm->getTickLength();
	return 0;
}

int32 ScriptFunc::sys_Cmd_Query_CountDown_Timer(EMCState *state) {
	return _vm->state()->_gameTimer;
}

int32 ScriptFunc::sys_Cmd_Proceed_To_Next_Chapter(EMCState *state) {

	_vm->state()->_currentChapter = stackPos(0);
	_vm->exitScene();
	_vm->loadScene(stackPos(1));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Play_Sfx_Plus(EMCState *state) {
	//debugC(0, 0xfff, "playSfx ( %d , %d, %d, %d, %d )", stackPos(0), stackPos(1), stackPos(2), stackPos(3), stackPos(4));
	_vm->playSFX(stackPos(0), stackPos(1));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Play_Sfx(EMCState *state) {
	//debugC(0, 0xfff, "playSfx ( %d , %d)", stackPos(0), stackPos(1));
	_vm->playSFX(stackPos(0), stackPos(1));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Ambient_Sfx(EMCState *state) {
	//debug("Ambient Sfx : %d %d %d %d", stackPos(0), stackPos(1), stackPos(2), stackPos(3));
	_vm->getAudioManager()->startAmbientSFX(stackPos(0), stackPos(1), stackPos(2), stackPos(3));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Kill_Ambient_Sfx(EMCState *state) {
	//debug("Kill Sfx : %d", stackPos(0));
	_vm->getAudioManager()->killAmbientSFX(stackPos(0));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Ambient_Sfx_Plus(EMCState *state) {
	//debug("Ambient Sfx Plus: %d %d %d %d %d %d %d %d", stackPos(0), stackPos(1), stackPos(2), stackPos(3), stackPos(4), stackPos(5), stackPos(6), stackPos(7));
	_vm->getAudioManager()->startAmbientSFX(stackPos(0), stackPos(1), stackPos(2), stackPos(3));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Ambient_Volume(EMCState *state) {
	//debug("Ambient Volume : %d %d", stackPos(0), stackPos(1));
	_vm->getAudioManager()->setAmbientSFXVolume(stackPos(0), stackPos(1));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Freeze_Scene_Animation(EMCState *state) {

	_vm->getSceneAnimationScript(stackPos(0))->_frozen = true;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Unfreeze_Scene_Animation(EMCState *state) {
	_vm->getSceneAnimationScript(stackPos(0))->_frozen = false;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Scene_Animation_Frozen(EMCState *state) {
	return _vm->getSceneAnimationScript(stackPos(0))->_frozen ? 1 : 0;
}

int32 ScriptFunc::sys_Cmd_Set_Script_Game_Data_Global(EMCState *state) {
	_vm->state()->_gameGlobalData[stackPos(0)] = stackPos(1);
	return stackPos(1);
}

int32 ScriptFunc::sys_Cmd_Get_Script_Game_Data_Global(EMCState *state) {
	return _vm->state()->_gameGlobalData[stackPos(0)];
}

int32 ScriptFunc::sys_Cmd_Say_Line(EMCState *state) {
	_vm->sayLines(1 , stackPos(0));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Knight_Puzzle_Get_Coord(EMCState *state) {
	static const uint16 knightCoords[] = {
		0x327, 0x0fa, 0x354, 0x0f8, 0x383, 0x0f4, 0x3a4, 0x0f0, 0x3d3, 0x0ed,
		0x3fd, 0x0ef, 0x2fe, 0x12d, 0x2fe, 0x12d, 0x310, 0x109, 0x349, 0x103,
		0x378, 0x103, 0x3a4, 0x102, 0x3d5, 0x102, 0x403, 0x103, 0x2fe, 0x12d,
		0x2fe, 0x12d, 0x2fe, 0x12d, 0x2fe, 0x12d, 0x369, 0x123, 0x3a4, 0x123,
		0x3d8, 0x121, 0x410, 0x124, 0x2fe, 0x12d, 0x2fe, 0x12d, 0x2fe, 0x12d,
		0x2fe, 0x12d, 0x35d, 0x14f, 0x3a2, 0x149, 0x3db, 0x149, 0x415, 0x14a,
		0x2fe, 0x12d, 0x2fe, 0x12d
	};

	return knightCoords[stackPos(2) + 2 * (8 * stackPos(1) + stackPos(0))];
}

int32 ScriptFunc::sys_Cmd_Add_Scene_Anim(EMCState *state) {
	return sys_Cmd_Init_Scene_Anim(state);
}

int32 ScriptFunc::sys_Cmd_Remove_Scene_Anim(EMCState *state) {
	int32 sceneId = stackPos(0);

	if (!_vm->getSceneAnimation(sceneId)->_active)
		return 0;

	SceneAnimation *sceneAnim = _vm->getSceneAnimation(sceneId);
	sceneAnim->_active = false;
	_vm->getAnimationManager()->removeInstance(sceneAnim->_animInstance);
	delete sceneAnim->_animation;
	sceneAnim->_animation = NULL;

	// see if one character shares this instance
	for (int32 c = 0; c < 32; c++) {
		if (_vm->getCharacter(c) && _vm->getCharacter(c)->getAnimationInstance() == sceneAnim->_originalAnimInstance) {
			_vm->getCharacter(c)->setAnimationInstance(NULL);
		}
	}
	delete sceneAnim->_originalAnimInstance;
	sceneAnim->_originalAnimInstance = NULL;
	sceneAnim->_animInstance = NULL;
	return 0;
}

int32 ScriptFunc::sys_Cmd_Disable_Timer(EMCState *state) {
	_vm->disableTimer(stackPos(0));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Enable_Timer(EMCState *state) {
	_vm->enableTimer(stackPos(0));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Timer(EMCState *state) {
	_vm->setTimer(stackPos(0), stackPos(1));
	return 0;
}

int32 ScriptFunc::sys_Cmd_Set_Palette_Color(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Number_Of_NPCs(EMCState *state) {
	return 0;
}

int32 ScriptFunc::sys_Cmd_Get_Config_Language(EMCState *state) {
	return 0;
}

} // End of namespace Toon