aboutsummaryrefslogtreecommitdiff
path: root/engines/kyra/script_tim.cpp
blob: dab3e365f321f17a020043f629eaf6f2c6812e02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
/* 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "kyra/script_tim.h"
#include "kyra/script.h"
#include "kyra/resource.h"
#include "kyra/sound.h"
#include "kyra/wsamovie.h"

#ifdef ENABLE_LOL
#include "kyra/lol.h"
#include "kyra/screen_lol.h"
#endif // ENABLE_LOL

#include "common/iff_container.h"
#include "common/endian.h"

namespace Kyra {

TIMInterpreter::TIMInterpreter(KyraEngine_v1 *engine, Screen_v2 *screen_v2, OSystem *system) : _vm(engine), _screen(screen_v2), _system(system), _currentTim(0) {
#define COMMAND(x) { &TIMInterpreter::x, #x }
#define COMMAND_UNIMPL() { 0, 0 }
#define cmd_return(n) cmd_return_##n
	static const CommandEntry commandProcs[] = {
		// 0x00
		COMMAND(cmd_initFunc0),
		COMMAND(cmd_stopCurFunc),
		COMMAND(cmd_initWSA),
		COMMAND(cmd_uninitWSA),
		// 0x04
		COMMAND(cmd_initFunc),
		COMMAND(cmd_stopFunc),
		COMMAND(cmd_wsaDisplayFrame),
		COMMAND(cmd_displayText),
		// 0x08
		COMMAND(cmd_loadVocFile),
		COMMAND(cmd_unloadVocFile),
		COMMAND(cmd_playVocFile),
		COMMAND_UNIMPL(),
		// 0x0C
		COMMAND(cmd_loadSoundFile),
		COMMAND(cmd_return(1)),
		COMMAND(cmd_playMusicTrack),
		COMMAND_UNIMPL(),
		// 0x10
		COMMAND(cmd_return(1)),
		COMMAND(cmd_return(1)),
		COMMAND_UNIMPL(),
		COMMAND_UNIMPL(),
		// 0x14
		COMMAND(cmd_setLoopIp),
		COMMAND(cmd_continueLoop),
		COMMAND(cmd_resetLoopIp),
		COMMAND(cmd_resetAllRuntimes),
		// 0x18
		COMMAND(cmd_return(1)),
		COMMAND(cmd_execOpcode),
		COMMAND(cmd_initFuncNow),
		COMMAND(cmd_stopFuncNow),
		// 0x1C
		COMMAND(cmd_return(1)),
		COMMAND(cmd_return(1)),
		COMMAND(cmd_return(n1))
	};
#undef cmd_return
#undef COMMAND_UNIMPL
#undef COMMAND

	_commands = commandProcs;
	_commandsSize = ARRAYSIZE(commandProcs);

	_animations = new Animation[TIM::kWSASlots];
	memset(_animations, 0, TIM::kWSASlots * sizeof(Animation));

	_langData = 0;
	_textDisplayed = false;
	_textAreaBuffer = new uint8[320*40];
	assert(_textAreaBuffer);
	_drawPage2 = (vm()->gameFlags().isDemo && vm()->gameFlags().gameID == GI_LOL) ? 0 : 8;

	_palDelayInc = _palDiff = _palDelayAcc = 0;
	_abortFlag = 0;
}

TIMInterpreter::~TIMInterpreter() {
	delete[] _langData;
	delete[] _textAreaBuffer;

	for (int i = 0; i < TIM::kWSASlots; i++) {
		delete _animations[i].wsa;
		delete[] _animations[i].parts;
	}

	delete[] _animations;
}

TIM *TIMInterpreter::load(const char *filename, const Common::Array<const TIMOpcode *> *opcodes) {
	if (!vm()->resource()->exists(filename))
		return 0;

	Common::SeekableReadStream *stream = vm()->resource()->createReadStream(filename);
	if (!stream)
		error("Couldn't open TIM file '%s'", filename);

	IFFParser iff(*stream);
	Common::IFFChunk *chunk = 0;

	TIM *tim = new TIM;
	assert(tim);
	memset(tim, 0, sizeof(TIM));

	tim->procFunc = -1;
	tim->opcodes = opcodes;

	int avtlChunkSize = 0;

	while ((chunk = iff.nextChunk()) != 0) {
		switch (chunk->id) {
		case MKID_BE('TEXT'):
			tim->text = new byte[chunk->size];
			assert(tim->text);
			if (chunk->read(tim->text, chunk->size) != chunk->size)
				error("Couldn't read TEXT chunk from file '%s'", filename);
			break;

		case MKID_BE('AVTL'):
			avtlChunkSize = chunk->size >> 1;
			tim->avtl = new uint16[avtlChunkSize];
			assert(tim->avtl);
			if (chunk->read(tim->avtl, chunk->size) != chunk->size)
				error("Couldn't read AVTL chunk from file '%s'", filename);

			for (int i = avtlChunkSize - 1; i >= 0; --i)
				tim->avtl[i] = READ_LE_UINT16(&tim->avtl[i]);
			break;

		default:
			warning("Unexpected chunk '%s' of size %d found in file '%s'", Common::ID2string(chunk->id), chunk->size, filename);
		}
	}

	if (!tim->avtl)
		error("No AVTL chunk found in file: '%s'", filename);

	if (stream->err())
		error("Read error while parsing file '%s", filename);

	delete stream;

	int num = (avtlChunkSize < TIM::kCountFuncs) ? avtlChunkSize : (int)TIM::kCountFuncs;
	for (int i = 0; i < num; ++i)
		tim->func[i].avtl = tim->avtl + tim->avtl[i];

	strncpy(tim->filename, filename, 13);
	tim->filename[12] = 0;

	return tim;
}

void TIMInterpreter::unload(TIM *&tim) const {
	if (!tim)
		return;

	delete[] tim->text;
	delete[] tim->avtl;
	delete tim;
	tim = 0;
}

void TIMInterpreter::setLangData(const char *filename) {
	delete[] _langData;
	_langData = vm()->resource()->fileData(filename, 0);
}

int TIMInterpreter::exec(TIM *tim, bool loop) {
	if (!tim)
		return 0;

	_currentTim = tim;
	if (!_currentTim->func[0].ip) {
		_currentTim->func[0].ip = _currentTim->func[0].avtl;
		_currentTim->func[0].nextTime = _currentTim->func[0].lastTime = _system->getMillis();
	}

	do {
		update();

		for (_currentFunc = 0; _currentFunc < TIM::kCountFuncs; ++_currentFunc) {
			TIM::Function &cur = _currentTim->func[_currentFunc];

			if (_currentTim->procFunc != -1)
				execCommand(28, &_currentTim->procParam);

			update();
			checkSpeechProgress();

			bool running = true;
			int cnt = 0;
			while (cur.ip && cur.nextTime <= _system->getMillis() && running) {
				if (cnt++ > 0) {
					if (_currentTim->procFunc != -1)
						execCommand(28, &_currentTim->procParam);
					update();
				}

				int8 opcode = int8(cur.ip[2] & 0xFF);

				switch (execCommand(opcode, cur.ip + 3)) {
				case -1:
					loop = false;
					running = false;
					_currentFunc = 11;
					break;

				case -2:
					running = false;
					break;

				case -3:
					_currentTim->procFunc = _currentFunc;
					_currentTim->dlgFunc = -1;
					break;

				case 22:
					cur.loopIp = 0;
					break;

				default:
					break;
				}

				if (cur.ip) {
					cur.ip += cur.ip[0];
					cur.lastTime = cur.nextTime;
					cur.nextTime += (cur.ip[1] ) * vm()->tickLength();
				}
			}
		}
	} while (loop && !vm()->shouldQuit());

	return _currentTim->clickedButton;
}

void TIMInterpreter::refreshTimersAfterPause(uint32 elapsedTime) {
	if (!_currentTim)
		return;

	for (int i = 0; i < TIM::kCountFuncs; i++) {
		if (_currentTim->func[i].lastTime)
			_currentTim->func[i].lastTime += elapsedTime;
		if (_currentTim->func[i].nextTime)
			_currentTim->func[i].nextTime += elapsedTime;
	}
}

void TIMInterpreter::displayText(uint16 textId, int16 flags) {
	char *text = getTableEntry(textId);

	if (_textDisplayed) {
		screen()->copyBlockToPage(0, 0, 160, 320, 40, _textAreaBuffer);
		_textDisplayed = false;
	}

	if (!text)
		return;
	if (!text[0])
		return;

	char filename[16];
	memset(filename, 0, sizeof(filename));

	if (text[0] == '$') {
		const char *end = strchr(text+1, '$');
		if (end)
			memcpy(filename, text+1, end-1-text);
	}

	if (filename[0])
		vm()->sound()->voicePlay(filename);

	if (text[0] == '$')
		text = strchr(text + 1, '$') + 1;

	setupTextPalette((flags < 0) ? 1 : flags, 0);

	if (flags < 0) {
		static const uint8 colorMap[] = { 0x00, 0xF0, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

		screen()->setFont(Screen::FID_8_FNT);
		screen()->setTextColorMap(colorMap);
		screen()->_charWidth = -2;
	}

	screen()->_charOffset = -4;
	screen()->copyRegionToBuffer(0, 0, 160, 320, 40, _textAreaBuffer);
	_textDisplayed = true;

	char backupChar = 0;
	char *str = text;
	int heightAdd = 0;

	while (str[0]) {
		char *nextLine = strchr(str, '\r');

		backupChar = 0;
		if (nextLine) {
			backupChar = nextLine[0];
			nextLine[0] = '\0';
		}

		int width = screen()->getTextWidth(str);

		if (flags >= 0)
			screen()->printText(str, (320 - width) >> 1, 160 + heightAdd, 0xF0, 0x00);
		else
			screen()->printText(str, (320 - width) >> 1, 188, 0xF0, 0x00);

		heightAdd += screen()->getFontHeight();
		str += strlen(str);

		if (backupChar) {
			nextLine[0] = backupChar;
			++str;
		}
	}

	screen()->_charOffset = 0;

	if (flags < 0) {
		static const uint8 colorMap[] = { 0x00, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0x00, 0x00, 0x00, 0x00 };

		screen()->setFont(Screen::FID_INTRO_FNT);
		screen()->setTextColorMap(colorMap);
		screen()->_charWidth = 0;
	}
}

void TIMInterpreter::setupTextPalette(uint index, int fadePalette) {
	static const uint16 palTable[] = {
		0x00, 0x00, 0x00,
		0x64, 0x64, 0x64,
		0x61, 0x51, 0x30,
		0x29, 0x48, 0x64,
		0x00, 0x4B, 0x3B,
		0x64, 0x1E, 0x1E,
	};

	for (int i = 0; i < 15; ++i) {
		uint8 *palette = screen()->getPalette(0) + (240 + i) * 3;

		uint8 c1 = (((15 - i) << 2) * palTable[index*3+0]) / 100;
		uint8 c2 = (((15 - i) << 2) * palTable[index*3+1]) / 100;
		uint8 c3 = (((15 - i) << 2) * palTable[index*3+2]) / 100;

		palette[0] = c1;
		palette[1] = c2;
		palette[2] = c3;
	}

	if (!fadePalette && !_palDiff) {
		screen()->setScreenPalette(screen()->getPalette(0));
	} else {
		screen()->getFadeParams(screen()->getPalette(0), fadePalette, _palDelayInc, _palDiff);
		_palDelayAcc = 0;
	}
}

KyraEngine_v1 *TIMInterpreter::vm() {
	return _vm;
}

Screen_v2 *TIMInterpreter::screen() {
	return _screen;
}

TIMInterpreter::Animation *TIMInterpreter::initAnimStruct(int index, const char *filename, int x, int y, int, int offscreenBuffer, uint16 wsaFlags) {
	Animation *anim = &_animations[index];
	anim->x = x;
	anim->y = y;
	anim->wsaCopyParams = wsaFlags;
	const bool isLoLDemo = vm()->gameFlags().isDemo && vm()->gameFlags().gameID == GI_LOL;

	_drawPage2 = isLoLDemo ? 0 : 8;

	uint16 wsaOpenFlags = 0;
	if (isLoLDemo) {
		if (!(wsaFlags & 0x10))
			wsaOpenFlags |= 1;
	} else {
		if (wsaFlags & 0x10)
			wsaOpenFlags |= 2;
		wsaOpenFlags |= 1;

		if (offscreenBuffer == 2)
			wsaOpenFlags = 1;
	}

	char file[32];
	snprintf(file, 32, "%s.WSA", filename);

	if (vm()->resource()->exists(file)) {
		if (isLoLDemo)
			anim->wsa = new WSAMovie_v1(_vm);
		else
			anim->wsa = new WSAMovie_v2(_vm, _screen);
		assert(anim->wsa);

		anim->wsa->open(file, wsaOpenFlags, (index == 1) ? screen()->getPalette(0) : 0);
	}

	if (anim->wsa && anim->wsa->opened()) {
		if (isLoLDemo) {
			if (x == -1) {
				int16 t = int8(320 - anim->wsa->width());
				uint8 v = int8(t & 0x00FF) - int8((t & 0xFF00) >> 8);
				v >>= 1;
				anim->x = x = v;
			}

			if (y == -1) {
				int16 t = int8(200 - anim->wsa->height());
				uint8 v = int8(t & 0x00FF) - int8((t & 0xFF00) >> 8);
				v >>= 1;
				anim->y = y = v;
			}
		} else {
			if (x == -1)
				anim->x = x = 0;
			if (y == -1)
				anim->y = y = 0;
		}

		if (wsaFlags & 2) {
			screen()->fadePalette(screen()->getPalette(1), 15, 0);
			screen()->clearPage(_drawPage2);
			if (_drawPage2)
				screen()->checkedPageUpdate(8, 4);
			screen()->updateScreen();
		}

		if (wsaFlags & 4) {
			snprintf(file, 32, "%s.CPS", filename);

			if (vm()->resource()->exists(file)) {
				screen()->loadBitmap(file, 3, 3, screen()->getPalette(0));
				screen()->copyRegion(0, 0, 0, 0, 320, 200, 2, _drawPage2, Screen::CR_NO_P_CHECK);
				if (_drawPage2)
					screen()->checkedPageUpdate(8, 4);
				screen()->updateScreen();
			}

			anim->wsa->displayFrame(0, 0, x, y, 0);
		}

		if (wsaFlags & 2)
			screen()->fadePalette(screen()->getPalette(0), 30, 0);
	} else {
		if (wsaFlags & 2) {
			screen()->fadePalette(screen()->getPalette(1), 15, 0);
			screen()->clearPage(_drawPage2);
			if (_drawPage2)
				screen()->checkedPageUpdate(8, 4);
			screen()->updateScreen();
		}

		snprintf(file, 32, "%s.CPS", filename);

		if (vm()->resource()->exists(file)) {
			screen()->loadBitmap(file, 3, 3, screen()->getPalette(0));
			screen()->copyRegion(0, 0, 0, 0, 320, 200, 2, _drawPage2, Screen::CR_NO_P_CHECK);
			if (_drawPage2)
				screen()->checkedPageUpdate(8, 4);
			screen()->updateScreen();
		}

		if (wsaFlags & 2)
			screen()->fadePalette(screen()->getPalette(0), 30, 0);
	}

	return anim;
}

int TIMInterpreter::freeAnimStruct(int index) {
	Animation *anim = &_animations[index];
	if (!anim)
		return 0;

	delete anim->wsa;
	memset(anim, 0, sizeof(Animation));
	return 1;
}

char *TIMInterpreter::getTableEntry(uint idx) {
	if (!_langData)
		return 0;
	else
		return (char *)(_langData + READ_LE_UINT16(_langData + (idx<<1)));
}

const char *TIMInterpreter::getCTableEntry(uint idx) const {
	if (!_langData)
		return 0;
	else
		return (const char *)(_langData + READ_LE_UINT16(_langData + (idx<<1)));
}

int TIMInterpreter::execCommand(int cmd, const uint16 *param) {
	if (cmd < 0 || cmd >= _commandsSize) {
		warning("Calling unimplemented TIM command %d from file '%s'", cmd, _currentTim->filename);
		return 0;
	}

	if (_commands[cmd].proc == 0) {
		warning("Calling unimplemented TIM command %d from file '%s'", cmd, _currentTim->filename);
		return 0;
	}

	debugC(5, kDebugLevelScript, "TIMInterpreter::%s(%p)", _commands[cmd].desc, (const void* )param);
	return (this->*_commands[cmd].proc)(param);
}

int TIMInterpreter::cmd_initFunc0(const uint16 *param) {
	for (int i = 0; i < TIM::kWSASlots; ++i)
		memset(&_currentTim->wsa[i], 0, sizeof(TIM::WSASlot));

	_currentTim->func[0].ip = _currentTim->func[0].avtl;
	_currentTim->func[0].lastTime = _system->getMillis();
	return 1;
}

int TIMInterpreter::cmd_stopCurFunc(const uint16 *param) {
	if (_currentFunc < TIM::kCountFuncs)
		_currentTim->func[_currentFunc].ip = 0;
	if (!_currentFunc)
		_finished = true;
	return -2;
}

void TIMInterpreter::stopAllFuncs(TIM *tim) {
	for (int i = 0; i < TIM::kCountFuncs; ++i)
		tim->func[i].ip = 0;
}

int TIMInterpreter::cmd_initWSA(const uint16 *param) {
	const int index = param[0];

	TIM::WSASlot &slot = _currentTim->wsa[index];

	slot.x = int16(param[2]);
	slot.y = int16(param[3]);
	slot.offscreen = param[4];
	slot.wsaFlags = param[5];
	const char *filename = (const char *)(_currentTim->text + READ_LE_UINT16(_currentTim->text + (param[1]<<1)));

	slot.anim = initAnimStruct(index, filename, slot.x, slot.y, 10, slot.offscreen, slot.wsaFlags);
	return 1;
}

int TIMInterpreter::cmd_uninitWSA(const uint16 *param) {
	const int index = param[0];

	TIM::WSASlot &slot = _currentTim->wsa[index];

	if (!slot.anim)
		return 0;

	Animation &anim = _animations[index];

	if (slot.offscreen) {
		delete anim.wsa;
		anim.wsa = 0;
		slot.anim = 0;
	} else {
		//XXX

		delete anim.wsa;
		bool hasParts = anim.parts ? true : false;
		delete[] anim.parts;

		memset(&anim, 0, sizeof(Animation));
		memset(&slot, 0, sizeof(TIM::WSASlot));

		if (hasParts) {
			anim.parts = new AnimPart[TIM::kAnimParts];
			memset(anim.parts, 0, TIM::kAnimParts * sizeof(AnimPart));
		}
	}

	return 1;
}

int TIMInterpreter::cmd_initFunc(const uint16 *param) {
	uint16 func = *param;
	assert(func < TIM::kCountFuncs);
	if (_currentTim->func[func].avtl)
		_currentTim->func[func].ip = _currentTim->func[func].avtl;
	else
		_currentTim->func[func].avtl = _currentTim->func[func].ip = _currentTim->avtl + _currentTim->avtl[func];
	return 1;
}

int TIMInterpreter::cmd_stopFunc(const uint16 *param) {
	uint16 func = *param;
	assert(func < TIM::kCountFuncs);
	_currentTim->func[func].ip = 0;
	return 1;
}

int TIMInterpreter::cmd_wsaDisplayFrame(const uint16 *param) {
	Animation &anim = _animations[param[0]];
	const int frame = param[1];
	int page = (anim.wsaCopyParams & 0x4000) != 0 ? 2 : _drawPage2;
	// WORKAROUND for some bugged scripts that will try to display frames of non-existent animations
	if (anim.wsa)
		anim.wsa->displayFrame(frame, page, anim.x, anim.y, anim.wsaCopyParams & 0xF0FF, 0, 0);
	if (!page)
		screen()->updateScreen();
	return 1;
}

int TIMInterpreter::cmd_displayText(const uint16 *param) {
	displayText(param[0], param[1]);
	return 1;
}

int TIMInterpreter::cmd_loadVocFile(const uint16 *param) {
	const int stringId = param[0];
	const int index = param[1];

	_vocFiles[index] = (const char *)(_currentTim->text + READ_LE_UINT16(_currentTim->text + (stringId << 1)));
	for (int i = 0; i < 4; ++i)
		_vocFiles[index].deleteLastChar();
	return 1;
}

int TIMInterpreter::cmd_unloadVocFile(const uint16 *param) {
	const int index = param[0];
	_vocFiles[index].clear();
	return 1;
}

int TIMInterpreter::cmd_playVocFile(const uint16 *param) {
	const int index = param[0];
	const int volume = (param[1] * 255) / 100;

	if (index < ARRAYSIZE(_vocFiles) && !_vocFiles[index].empty())
		vm()->sound()->voicePlay(_vocFiles[index].c_str(), 0, volume, true);
	else if (index == 7 && !_vm->gameFlags().isTalkie)
		vm()->sound()->playTrack(index);
	else
		vm()->sound()->playSoundEffect(index);

	return 1;
}

int TIMInterpreter::cmd_loadSoundFile(const uint16 *param) {
	const char *file = (const char *)(_currentTim->text + READ_LE_UINT16(_currentTim->text + (param[0]<<1)));

	vm()->sound()->loadSoundFile(file);
	if (vm()->gameFlags().gameID == GI_LOL)
		vm()->sound()->loadSfxFile(file);

	return 1;
}

int TIMInterpreter::cmd_playMusicTrack(const uint16 *param) {
	vm()->sound()->playTrack(param[0]);
	return 1;
}

int TIMInterpreter::cmd_setLoopIp(const uint16 *param) {
	_currentTim->func[_currentFunc].loopIp = _currentTim->func[_currentFunc].ip;
	return 1;
}

int TIMInterpreter::cmd_continueLoop(const uint16 *param) {
	TIM::Function &func = _currentTim->func[_currentFunc];

	if (!func.loopIp)
		return -2;

	func.ip = func.loopIp;

	uint16 factor = param[0];
	if (factor) {
		const uint32 random = vm()->_rnd.getRandomNumberRng(0, 0x8000);
		uint32 waitTime = (random * factor) / 0x8000;
		func.nextTime += waitTime * vm()->tickLength();
	}

	return -2;
}

int TIMInterpreter::cmd_resetLoopIp(const uint16 *param) {
	_currentTim->func[_currentFunc].loopIp = 0;
	return 1;
}

int TIMInterpreter::cmd_resetAllRuntimes(const uint16 *param) {
	for (int i = 0; i < TIM::kCountFuncs; ++i) {
		if (_currentTim->func[i].ip)
			_currentTim->func[i].nextTime = _system->getMillis();
	}
	return 1;
}

int TIMInterpreter::cmd_execOpcode(const uint16 *param) {
	const uint16 opcode = *param++;

	if (!_currentTim->opcodes) {
		warning("Trying to execute TIM opcode %d without opcode list (file '%s')", opcode, _currentTim->filename);
		return 0;
	}

	if (opcode > _currentTim->opcodes->size()) {
		warning("Calling unimplemented TIM opcode(0x%.02X/%d) from file '%s'", opcode, opcode, _currentTim->filename);
		return 0;
	}

	if (!(*_currentTim->opcodes)[opcode]->isValid()) {
		warning("Calling unimplemented TIM opcode(0x%.02X/%d) from file '%s'", opcode, opcode, _currentTim->filename);
		return 0;
	}

	return (*(*_currentTim->opcodes)[opcode])(_currentTim, param);
}

int TIMInterpreter::cmd_initFuncNow(const uint16 *param) {
	uint16 func = *param;
	assert(func < TIM::kCountFuncs);
	_currentTim->func[func].ip = _currentTim->func[func].avtl;
	_currentTim->func[func].lastTime = _currentTim->func[func].nextTime = _system->getMillis();
	return 1;
}

int TIMInterpreter::cmd_stopFuncNow(const uint16 *param) {
	uint16 func = *param;
	assert(func < TIM::kCountFuncs);
	_currentTim->func[func].ip = 0;
	_currentTim->func[func].lastTime = _currentTim->func[func].nextTime = _system->getMillis();
	return 1;
}

int TIMInterpreter::cmd_stopAllFuncs(const uint16 *param) {
	while (_currentTim->dlgFunc == -1 && _currentTim->clickedButton == 0 && !vm()->shouldQuit()) {
		update();
		_currentTim->clickedButton = processDialogue();
	}

	for (int i = 0; i < TIM::kCountFuncs; ++i)
		_currentTim->func[i].ip = 0;

	return -1;
}

// TODO: Consider moving to another file

#ifdef ENABLE_LOL
// LOL version of the TIM interpreter

TIMInterpreter_LoL::TIMInterpreter_LoL(LoLEngine *engine, Screen_v2 *screen_v2, OSystem *system) :
	TIMInterpreter(engine, screen_v2, system), _vm(engine)  {
	#define COMMAND(x) { &TIMInterpreter_LoL::x, #x }
	#define COMMAND_UNIMPL() { 0, 0 }
	#define cmd_return(n) cmd_return_##n
	static const CommandEntry commandProcs[] = {
		// 0x00
		COMMAND(cmd_initFunc0),
		COMMAND(cmd_stopAllFuncs),
		COMMAND(cmd_initWSA),
		COMMAND(cmd_uninitWSA),
		// 0x04
		COMMAND(cmd_initFunc),
		COMMAND(cmd_stopFunc),
		COMMAND(cmd_wsaDisplayFrame),
		COMMAND_UNIMPL(),
		// 0x08
		COMMAND(cmd_loadVocFile),
		COMMAND(cmd_unloadVocFile),
		COMMAND(cmd_playVocFile),
		COMMAND_UNIMPL(),
		// 0x0C
		COMMAND(cmd_loadSoundFile),
		COMMAND(cmd_return(1)),
		COMMAND(cmd_playMusicTrack),
		COMMAND_UNIMPL(),
		// 0x10
		COMMAND(cmd_return(1)),
		COMMAND(cmd_return(1)),
		COMMAND_UNIMPL(),
		COMMAND_UNIMPL(),
		// 0x14
		COMMAND(cmd_setLoopIp),
		COMMAND(cmd_continueLoop),
		COMMAND(cmd_resetLoopIp),
		COMMAND(cmd_resetAllRuntimes),
		// 0x18
		COMMAND(cmd_return(1)),
		COMMAND(cmd_execOpcode),
		COMMAND(cmd_initFuncNow),
		COMMAND(cmd_stopFuncNow),
		// 0x1C
		COMMAND(cmd_processDialogue),
		COMMAND(cmd_dialogueBox),
		COMMAND(cmd_return(n1))
	};
	#undef cmd_return
	#undef COMMAND_UNIMPL
	#undef COMMAND

	_commands = commandProcs ;
	_commandsSize = ARRAYSIZE(commandProcs);

	_screen = engine->_screen;

	for (int i = 0; i < TIM::kWSASlots; i++) {
		_animations[i].parts = new AnimPart[TIM::kAnimParts];
		memset(_animations[i].parts, 0, TIM::kAnimParts * sizeof(AnimPart));
	}

	_drawPage2 = 0;

	memset(_dialogueButtonString, 0, 3 * sizeof(const char *));
	_dialogueButtonPosX = _dialogueButtonPosY = _dialogueNumButtons = _dialogueButtonXoffs = _dialogueHighlightedButton = 0;
}

TIMInterpreter::Animation *TIMInterpreter_LoL::initAnimStruct(int index, const char *filename, int x, int y, int frameDelay, int, uint16 wsaFlags) {
	Animation *anim = &_animations[index];
	anim->x = x;
	anim->y = y;
	anim->frameDelay = frameDelay;
	anim->wsaCopyParams = wsaFlags;
	anim->enable = 0;
	anim->lastPart = -1;

	uint16 wsaOpenFlags = 0;
	if (wsaFlags & 0x10)
		wsaOpenFlags |= 2;
	if (wsaFlags & 8)
		wsaOpenFlags |= 1;

	char file[32];
	snprintf(file, 32, "%s.WSA", filename);

	if (_vm->resource()->exists(file)) {
		anim->wsa = new WSAMovie_v2(_vm, TIMInterpreter::_screen);
		assert(anim->wsa);
		anim->wsa->open(file, wsaOpenFlags, _screen->getPalette(3));
	}

	if (wsaFlags & 1) {
		if (_screen->_fadeFlag != 1)
			_screen->fadeClearSceneWindow(10);
		memcpy(_screen->getPalette(3) + 384, _screen->_currentPalette + 384, 384);
	} else if (wsaFlags & 2) {
		_screen->fadeToBlack(10);
	}

	if (wsaFlags & 7)
		anim->wsa->displayFrame(0, 0, x, y, 0);

	if (wsaFlags & 3) {
		_screen->loadSpecialColors(_screen->getPalette(3));
		_screen->fadePalette(_screen->getPalette(3), 10);
		_screen->_fadeFlag = 0;
	}

	return anim;
}

int TIMInterpreter_LoL::freeAnimStruct(int index) {
	Animation *anim = &_animations[index];
	if (!anim)
		return 0;

	delete anim->wsa;
	delete[] anim->parts;
	memset(anim, 0, sizeof(Animation));

	anim->parts = new AnimPart[TIM::kAnimParts];
	memset(anim->parts, 0, TIM::kAnimParts * sizeof(AnimPart));

	return 1;
}

KyraEngine_v1 *TIMInterpreter_LoL::vm() {
	return _vm;
}

Screen_v2 *TIMInterpreter_LoL::screen() {
	return _screen;
}

void TIMInterpreter_LoL::advanceToOpcode(int opcode) {
	TIM::Function *f = &_currentTim->func[_currentTim->dlgFunc];
	uint16 len = f->ip[0];

	while ((f->ip[2] & 0xFF) != opcode) {
		if ((f->ip[2] & 0xFF) == 1) {
			f->ip[0] = len;
			break;
		}
		len = f->ip[0];
		f->ip += len;
	}

	f->nextTime = _system->getMillis();
}

void TIMInterpreter_LoL::drawDialogueBox(int numStr, const char *s1, const char *s2, const char *s3) {
	_screen->setScreenDim(5);

	if (numStr == 1 && _vm->_speechFlag) {
		_dialogueNumButtons = 0;
		_dialogueButtonString[0] = _dialogueButtonString[1] = _dialogueButtonString[2] = 0;
	} else {
		_dialogueNumButtons = numStr;
		_dialogueButtonString[0] = s1;
		_dialogueButtonString[1] = s2;
		_dialogueButtonString[2] = s3;
		_dialogueHighlightedButton = 0;

		const ScreenDim *d = _screen->getScreenDim(5);
		_dialogueButtonPosY = d->sy + d->h - 9;

		if (numStr == 1) {
			_dialogueButtonXoffs = 0;
			_dialogueButtonPosX = d->sx + d->w - 77;
		} else {
			_dialogueButtonXoffs = d->w / numStr;
			_dialogueButtonPosX = d->sx + (_dialogueButtonXoffs >> 1) - 37;
		}

		drawDialogueButtons();
	}

	if (!_vm->shouldQuit())
		_vm->removeInputTop();
}

void TIMInterpreter_LoL::setupBackgroundAnimationPart(int animIndex, int part, int firstFrame, int lastFrame, int cycles, int nextPart, int partDelay, int f, int sfxIndex, int sfxFrame) {
	AnimPart *a = &_animations[animIndex].parts[part];
	a->firstFrame = firstFrame;
	a->lastFrame = lastFrame;
	a->cycles = cycles;
	a->nextPart = nextPart;
	a->partDelay = partDelay;
	a->field_A = f;
	a->sfxIndex = sfxIndex;
	a->sfxFrame = sfxFrame;
}

void TIMInterpreter_LoL::startBackgroundAnimation(int animIndex, int part) {
	Animation *anim = &_animations[animIndex];
	anim->curPart = part;
	AnimPart *p = &anim->parts[part];
	anim->enable = 1;
	anim->nextFrame = _system->getMillis() + anim->frameDelay * _vm->_tickLength;
	anim->curFrame = p->firstFrame;
	anim->cyclesCompleted = 0;

	// WORKAROUND for some bugged scripts that will try to display frames of non-existent animations
	if (anim->wsa)
		anim->wsa->displayFrame(anim->curFrame - 1, 0, anim->x, anim->y, 0);
}

void TIMInterpreter_LoL::stopBackgroundAnimation(int animIndex) {
	Animation *anim = &_animations[animIndex];
	anim->enable = 0;
	anim->field_D = 0;
	if (animIndex == 5) {
		delete anim->wsa;
		anim->wsa = 0;
	}
}

void TIMInterpreter_LoL::updateBackgroundAnimation(int animIndex) {
	Animation *anim = &_animations[animIndex];
	if (!anim->enable || anim->nextFrame >= _system->getMillis())
		return;

	AnimPart *p = &anim->parts[anim->curPart];
	anim->nextFrame = 0;

	int step = 0;
	if (p->lastFrame >= p->firstFrame) {
		step = 1;
		anim->curFrame++;
	} else {
		step = -1;
		anim->curFrame--;
	}

	if (anim->curFrame == (p->lastFrame + step)) {
		anim->cyclesCompleted++;

		if ((anim->cyclesCompleted > p->cycles) || anim->field_D) {
			anim->lastPart = anim->curPart;

			if ((p->nextPart == -1) || (anim->field_D && p->field_A)) {
				anim->enable = 0;
				anim->field_D = 0;
				return;
			}

			anim->nextFrame += (p->partDelay * _vm->_tickLength);
			anim->curPart = p->nextPart;

			p = &anim->parts[anim->curPart];
			anim->curFrame = p->firstFrame;
			anim->cyclesCompleted = 0;

		} else {
			anim->curFrame = p->firstFrame;
		}
	}

	if (p->sfxIndex != -1 && p->sfxFrame == anim->curFrame)
		_vm->snd_playSoundEffect(p->sfxIndex, -1);

	anim->nextFrame += (anim->frameDelay * _vm->_tickLength);

	anim->wsa->displayFrame(anim->curFrame - 1, 0, anim->x, anim->y, 0);
	anim->nextFrame += _system->getMillis();
}

void TIMInterpreter_LoL::playAnimationPart(int animIndex, int firstFrame, int lastFrame, int delay) {
	Animation *anim = &_animations[animIndex];

	int step = (lastFrame >= firstFrame) ? 1 : -1;
	for (int i = firstFrame; i != (lastFrame + step) ; i += step) {
		uint32 next = _system->getMillis() + delay * _vm->_tickLength;
		if (anim->wsaCopyParams & 0x4000) {
			_screen->copyRegion(112, 0, 112, 0, 176, 120, 6, 2);
			anim->wsa->displayFrame(i - 1, 2, anim->x, anim->y, anim->wsaCopyParams & 0x1000 ? 0x5000 : 0x4000, _vm->_trueLightTable1, _vm->_trueLightTable2);
			_screen->copyRegion(112, 0, 112, 0, 176, 120, 2, 0);
			_screen->updateScreen();
		} else {
			anim->wsa->displayFrame(i - 1, 0, anim->x, anim->y, 0);
			_screen->updateScreen();
		}
		_vm->delayUntil(next);
	}
}

void TIMInterpreter_LoL::drawDialogueButtons() {
	int cp = _screen->setCurPage(0);
	Screen::FontId of = _screen->setFont(Screen::FID_6_FNT);

	int x = _dialogueButtonPosX;

	for (int i = 0; i < _dialogueNumButtons; i++) {
		_vm->gui_drawBox(x, _dialogueButtonPosY, 74, 9, 136, 251, -1);
		_screen->printText(_dialogueButtonString[i], x + 37 - (_screen->getTextWidth(_dialogueButtonString[i])) / 2,
			_dialogueButtonPosY + 2, _dialogueHighlightedButton == i ? 144 : 254, 0);
		x += _dialogueButtonXoffs;
	}
	_screen->setFont(of);
	_screen->setCurPage(cp);
}

uint16 TIMInterpreter_LoL::processDialogue() {
	int df = _dialogueHighlightedButton;
	int res = 0;
	int x = _dialogueButtonPosX;

	for (int i = 0; i < _dialogueNumButtons; i++) {
		if (_vm->posWithinRect(_vm->_mouseX, _vm->_mouseY, x, _dialogueButtonPosY, x + 74, _dialogueButtonPosY + 9)) {
			_dialogueHighlightedButton = i;
			break;
		}
		x += _dialogueButtonXoffs;
	}

	if (_dialogueNumButtons == 0) {
		int e = _vm->checkInput(0, false) & 0xFF;
		_vm->removeInputTop();

		if (e) {
			_vm->gui_notifyButtonListChanged();

			if (e == 43 || e == 61) {
				_vm->snd_stopSpeech(true);
				//_dlgTimer = 0;
			}
		}

		if (_vm->snd_updateCharacterSpeech() != 2) {
			//if (_dlgTimer < _system->getMillis()) {
				res = 1;
				if (!_vm->shouldQuit()) {
					_vm->removeInputTop();
					_vm->gui_notifyButtonListChanged();
				}
			//}
		}
	} else {
		int e = _vm->checkInput(0, false) & 0xFF;
		_vm->removeInputTop();
		if (e)
			_vm->gui_notifyButtonListChanged();

		switch (e) {
		case 43:
		case 61:
			_vm->snd_stopSpeech(true);
			//_dlgTimer = 0;
			res = _dialogueHighlightedButton + 1;
			break;

		case 92:
		case 97:
			if (_dialogueNumButtons > 1 && _dialogueHighlightedButton > 0)
				_dialogueHighlightedButton--;
			break;

		case 96:
		case 102:
			if (_dialogueNumButtons > 1 && _dialogueHighlightedButton < (_dialogueNumButtons - 1))
				_dialogueHighlightedButton++;
			break;

		case 200:
		case 202:
			x = _dialogueButtonPosX;

			for (int i = 0; i < _dialogueNumButtons; i++) {
				if (_vm->posWithinRect(_vm->_mouseX, _vm->_mouseY, x, _dialogueButtonPosY, x + 74, _dialogueButtonPosY + 9)) {
					_dialogueHighlightedButton = i;
					res = _dialogueHighlightedButton + 1;
					break;
				}
				x += _dialogueButtonXoffs;
			}
			break;

		default:
			break;
		}
	}

	if (df != _dialogueHighlightedButton)
		drawDialogueButtons();

	if (res == 0)
		return 0;

	_vm->updatePortraits();

	if (!_vm->textEnabled() && _vm->_currentControlMode) {
		_screen->setScreenDim(5);
		const ScreenDim *d = _screen->getScreenDim(5);
		_screen->fillRect(d->sx, d->sy + d->h - 9, d->sx + d->w - 1, d->sy + d->h - 1, d->unkA);
	} else {
		const ScreenDim *d = _screen->_curDim;
		_screen->fillRect(d->sx, d->sy, d->sx + d->w - 2, d->sy + d->h - 1, d->unkA);
		_vm->_txt->clearDim(4);
		_vm->_txt->resetDimTextPositions(4);
	}

	return res;
}

void TIMInterpreter_LoL::resetDialogueState(TIM *tim) {
	if (!tim)
		return;

	tim->procFunc = 0;
	tim->procParam = _dialogueNumButtons ? _dialogueNumButtons : 1;
	tim->clickedButton = 0;
	tim->dlgFunc = -1;
}

void TIMInterpreter_LoL::update() {
	_vm->update();
}

void TIMInterpreter_LoL::checkSpeechProgress() {
	if (_vm->speechEnabled() && _currentTim->procParam > 1 && _currentTim->func[_currentFunc].loopIp) {
		if (_vm->snd_updateCharacterSpeech() != 2) {
			_currentTim->func[_currentFunc].loopIp = 0;
			_currentTim->dlgFunc = _currentFunc;
			advanceToOpcode(21);
			_currentTim->dlgFunc = -1;
			_animations[5].field_D = 0;
			_animations[5].enable = 0;
			delete _animations[5].wsa;
			_animations[5].wsa = 0;
		}
	}
}

char *TIMInterpreter_LoL::getTableString(int id) {
	return _vm->getLangString(id);
}

int TIMInterpreter_LoL::execCommand(int cmd, const uint16 *param) {
	if (cmd < 0 || cmd >= _commandsSize) {
		warning("Calling unimplemented TIM command %d from file '%s'", cmd, _currentTim->filename);
		return 0;
	}

	if (_commands[cmd].proc == 0) {
		warning("Calling unimplemented TIM command %d from file '%s'", cmd, _currentTim->filename);
		return 0;
	}

	debugC(5, kDebugLevelScript, "TIMInterpreter::%s(%p)", _commands[cmd].desc, (const void* )param);
	return (this->*_commands[cmd].proc)(param);
}

int TIMInterpreter_LoL::cmd_setLoopIp(const uint16 *param) {
	if (_vm->speechEnabled()) {
		if (_vm->snd_updateCharacterSpeech() == 2)
			_currentTim->func[_currentFunc].loopIp = _currentTim->func[_currentFunc].ip;
		else
			advanceToOpcode(21);
	} else {
		_currentTim->func[_currentFunc].loopIp = _currentTim->func[_currentFunc].ip;
	}
	return 1;
}

int TIMInterpreter_LoL::cmd_continueLoop(const uint16 *param) {
	TIM::Function &func = _currentTim->func[_currentFunc];

	if (!func.loopIp)
		return -2;

	func.ip = func.loopIp;

	if (_vm->snd_updateCharacterSpeech() != 2) {
		uint16 factor = param[0];
		if (factor) {
			const uint32 random = _vm->_rnd.getRandomNumberRng(0, 0x8000);
			uint32 waitTime = (random * factor) / 0x8000;
			func.nextTime += waitTime * _vm->tickLength();
		}
	}

	return -2;
}

int TIMInterpreter_LoL::cmd_processDialogue(const uint16 *param) {
	int res = processDialogue();
	if (!res || !_currentTim->procParam)
		return res;

	_vm->snd_stopSpeech(false);

	_currentTim->func[_currentTim->procFunc].loopIp = 0;
	_currentTim->dlgFunc = _currentTim->procFunc;
	_currentTim->procFunc = -1;
	_currentTim->clickedButton = res;

	_animations[5].field_D = 0;
	_animations[5].enable = 0;
	delete _animations[5].wsa;
	_animations[5].wsa = 0;

	if (_currentTim->procParam)
		advanceToOpcode(21);

	return res;
}

int TIMInterpreter_LoL::cmd_dialogueBox(const uint16 *param) {
	uint16 func = *param;
	assert(func < TIM::kCountFuncs);
	_currentTim->procParam = func;
	_currentTim->clickedButton = 0;

	const char *tmpStr[3];
	int cnt = 0;

	for (int i = 1; i < 4; i++) {
		if (param[i] != 0xffff) {
			tmpStr[i-1] = getTableString(param[i]);
			cnt++;
		} else {
			tmpStr[i-1] = 0;
		}
	}

	drawDialogueBox(cnt, tmpStr[0], tmpStr[1], tmpStr[2]);
	_vm->gui_notifyButtonListChanged();

	return -3;
}
#endif // ENABLE_LOL

} // end of namespace Kyra