aboutsummaryrefslogtreecommitdiff
path: root/saga/sthread.cpp
blob: 9b831506a8ca90c5ef0e5242ab1031e14f99def8 (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004-2005 The ScummVM project
 *
 * The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Header$
 *
 */

// Scripting module thread management component
#include "saga/saga.h"

#include "saga/gfx.h"
#include "saga/actor.h"
#include "saga/console.h"
#include "saga/interface.h"

#include "saga/script.h"

#include "saga/stream.h"
#include "saga/scene.h"
#include "saga/resnames.h"

namespace Saga {

ScriptThread *Script::createThread(uint16 scriptModuleNumber, uint16 scriptEntryPointNumber) {
	ScriptThread *newThread;

	loadModule(scriptModuleNumber);
	if (_modules[scriptModuleNumber].entryPointsCount <= scriptEntryPointNumber) {
		error("Script::createThread wrong scriptEntryPointNumber");
	}
		
	newThread = _threadList.pushFront().operator->();
	newThread->_flags = kTFlagNone;
	newThread->_stackSize = DEFAULT_THREAD_STACK_SIZE;
	newThread->_stackBuf = (uint16 *)malloc(newThread->_stackSize * sizeof(*newThread->_stackBuf));
	newThread->_stackTopIndex = newThread->_stackSize - 2; 
	newThread->_instructionOffset = _modules[scriptModuleNumber].entryPoints[scriptEntryPointNumber].offset;
	newThread->_commonBase = _commonBuffer;
	newThread->_staticBase = _commonBuffer + _modules[scriptModuleNumber].staticOffset;
	newThread->_moduleBase = _modules[scriptModuleNumber].moduleBase;
	newThread->_moduleBaseSize = _modules[scriptModuleNumber].moduleBaseSize;
	
	newThread->_strings = &_modules[scriptModuleNumber].strings;
	newThread->_voiceLUT = &_modules[scriptModuleNumber].voiceLUT;

	return newThread;
}

void Script::wakeUpActorThread(int waitType, void *threadObj) {
	ScriptThread *thread;
	ScriptThreadList::iterator threadIterator;

	for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
		thread = threadIterator.operator->();
		if ((thread->_flags & kTFlagWaiting) && (thread->_waitType == waitType) && (thread->_threadObj == threadObj)) {
			thread->_flags &= ~kTFlagWaiting;
		}
	}
}

void Script::wakeUpThreads(int waitType) {
	ScriptThread *thread;
	ScriptThreadList::iterator threadIterator;
	
	for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
		thread = threadIterator.operator->();
		if ((thread->_flags & kTFlagWaiting) && (thread->_waitType == waitType)) {
			thread->_flags &= ~kTFlagWaiting;
		}
	}
}

void Script::wakeUpThreadsDelayed(int waitType, int sleepTime) {
	ScriptThread *thread;
	ScriptThreadList::iterator threadIterator;

	for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
		thread = threadIterator.operator->();
		if ((thread->_flags & kTFlagWaiting) && (thread->_waitType == waitType)) {
			thread->_waitType = kWaitTypeDelay;
			thread->_sleepTime = sleepTime;
		}
	}
}

int Script::executeThreads(uint msec) {
	ScriptThread *thread;
	ScriptThreadList::iterator threadIterator;

	if (!isInitialized()) {
		return FAILURE;
	}

	threadIterator = _threadList.begin();

	while (threadIterator != _threadList.end()) {
		thread = threadIterator.operator->();

		if (thread->_flags & (kTFlagFinished | kTFlagAborted)) {
			if (thread->_flags & kTFlagFinished)
				setPointerVerb();
			
			threadIterator = _threadList.erase(threadIterator);
			continue;
		}

		if (thread->_flags & kTFlagWaiting) {
			
			if (thread->_waitType == kWaitTypeDelay) {
				if (thread->_sleepTime < msec) {
					thread->_sleepTime = 0;
				} else {
					thread->_sleepTime -= msec;
				}

				if (thread->_sleepTime == 0)
					thread->_flags &= ~kTFlagWaiting;			
			} else {
				if (thread->_waitType == kWaitTypeWalk) {
					ActorData *actor;
					actor = (ActorData *)thread->_threadObj;
					if (actor->currentAction == kActionWait) {
						thread->_flags &= ~kTFlagWaiting;			
					}
				}
			}
		}

		if (!(thread->_flags & kTFlagWaiting)) {
			if (runThread(thread, STHREAD_TIMESLICE)) {
				break;
			}
		}

		++threadIterator;
	}

	return SUCCESS;
}

void Script::abortAllThreads(void) {
	ScriptThread *thread;
	ScriptThreadList::iterator threadIterator;

	threadIterator = _threadList.begin();

	while (threadIterator != _threadList.end()) {
		thread = threadIterator.operator->();
		thread->_flags |= kTFlagAborted;
		++threadIterator;
	}
	executeThreads(0);
}

void Script::completeThread(void) {
	for (int i = 0; i < 40 &&  !_threadList.isEmpty() ; i++)
		executeThreads(0);
}

int Script::SThreadDebugStep() {
	if (_dbg_singlestep) {
		_dbg_dostep = 1;
	}

	return SUCCESS;
}

bool Script::runThread(ScriptThread *thread, uint instructionLimit) {
	const char*operandName;
	uint instructionCount;
	uint16 savedInstructionOffset;

	byte *addr;
	uint16 jmpOffset1;
	int16 iparam1;
	int16 iparam2;
	int16 iparam3;

	byte argumentsCount;
	uint16 functionNumber;
	ScriptFunctionType scriptFunction;

	int debug_print = 0;
	int operandChar;
	int i;

	// Handle debug single-stepping
	if ((thread == _dbg_thread) && _dbg_singlestep) {
		if (_dbg_dostep) {
			debug_print = 1;
			thread->_sleepTime = 0;
			instructionLimit = 1;
			_dbg_dostep = 0;
		} else {
			return false;
		}
	}

	MemoryReadStream scriptS(thread->_moduleBase, thread->_moduleBaseSize);

	scriptS.seek(thread->_instructionOffset);

	for (instructionCount = 0; instructionCount < instructionLimit; instructionCount++) {
		if (thread->_flags & (kTFlagAsleep))
			break;

		savedInstructionOffset = thread->_instructionOffset;
		operandChar = scriptS.readByte();


#define CASEOP(opName)	case opName:												\
							if (operandChar == opName) {							\
								operandName = #opName;								\
								debug(8, operandName);								\
								_vm->_console->DebugPrintf("%s\n", operandName);	\
							}
						
//		debug(8, "Executing thread offset: %lu (%x) stack: %d", thread->_instructionOffset, operandChar, thread->pushedSize());
		operandName="";
		switch (operandChar) {
		CASEOP(opNextBlock)
			// Some sort of "jump to the start of the next memory
			// page" instruction, I think.
			thread->_instructionOffset = 1024 * ((thread->_instructionOffset / 1024) + 1);
			break;

// STACK INSTRUCTIONS
		CASEOP(opDup)
			thread->push(thread->stackTop());
			break;
		CASEOP(opDrop)
			thread->pop();
			break;
		CASEOP(opZero)
			thread->push(0);
			break;
		CASEOP(opOne)
			thread->push(1);
			break;
		CASEOP(opConstint)
		CASEOP(opStrlit)
			iparam1 = scriptS.readSint16LE();
			thread->push(iparam1);
			debug(8, "0x%X", iparam1);
			break;

// DATA INSTRUCTIONS  
		CASEOP(opGetFlag)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += (iparam1 >> 3);
			iparam1 = (1 << (iparam1 & 7));
			thread->push((*addr) & iparam1 ? 1 : 0);
			break;
		CASEOP(opGetInt)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += iparam1;
			thread->push(*((uint16*)addr));
			debug(8, "0x%X", *((uint16*)addr));
			break;
		CASEOP(opPutFlag)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += (iparam1 >> 3);
			iparam1 = (1 << (iparam1 & 7));
			if (thread->stackTop()) {
				*addr |= iparam1;
			} else {
				*addr &= ~iparam1;
			}
			break;
		CASEOP(opPutInt)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += iparam1;
			*(uint16*)addr =  thread->stackTop();
			break;
		CASEOP(opPutFlagV)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += (iparam1 >> 3);
			iparam1 = (1 << (iparam1 & 7));
			if (thread->pop()) {
				*addr |= iparam1;
			} else {
				*addr &= ~iparam1;
			}
			break;
		CASEOP(opPutIntV)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += iparam1;
			*(uint16*)addr =  thread->pop();
			break;

// FUNCTION CALL INSTRUCTIONS    
		CASEOP(opCall)
			argumentsCount = scriptS.readByte();
			iparam1 = scriptS.readByte();
			if (iparam1 != kAddressModule) {
				error("Script::runThread iparam1 != kAddressModule");
			}
			addr = thread->baseAddress(iparam1);
			iparam1 = scriptS.readSint16LE();
			addr += iparam1;
			thread->push(argumentsCount);

			jmpOffset1 = scriptS.pos();
			// NOTE: The original pushes the program
			// counter as a pointer here. But I don't think
			// we will have to do that.
			thread->push(jmpOffset1);
			// NOTE2: program counter is 32bit - so we should "emulate" it size - because kAddressStack relies on it
			thread->push(0);
			thread->_instructionOffset = iparam1;
			
			break;
		CASEOP(opCcall)
		CASEOP(opCcallV)
			argumentsCount = scriptS.readByte();
			functionNumber = scriptS.readUint16LE();
			if (functionNumber >= SCRIPT_FUNCTION_MAX) {
				error("Script::runThread() Invalid script function number (%d)", functionNumber);
			}

			debug(8, "Calling 0x%X %s", functionNumber, _scriptFunctionsList[functionNumber].scriptFunctionName);
			scriptFunction = _scriptFunctionsList[functionNumber].scriptFunction;
			(this->*scriptFunction)(thread, argumentsCount);

			if (scriptFunction == &Saga::Script::sfScriptGotoScene) {			
				return true; // cause abortAllThreads called and _this_ thread destroyed
			}

			if (operandChar == opCcall) {// CALL function
				thread->push(thread->_returnValue);
			}

			if (thread->_flags & kTFlagAsleep)
				instructionCount = instructionLimit;	// break out of loop!
			break;
		CASEOP(opEnter)
			thread->push(thread->_frameIndex);
			thread->_frameIndex = thread->_stackTopIndex;
			thread->_stackTopIndex -= (scriptS.readSint16LE() / 2);
			break;
		CASEOP(opReturn)
			thread->_returnValue = thread->pop();
		CASEOP(opReturnV)
			thread->_stackTopIndex = thread->_frameIndex;
			thread->_frameIndex = thread->pop();
			if (thread->pushedSize() == 0) {
				thread->_flags |= kTFlagFinished;
				return true;
			} else {
				thread->pop(); //cause it 0
				thread->_instructionOffset = thread->pop();
				iparam1 = thread->pop();
				iparam1 += iparam1;
				while (iparam1--) {
					thread->pop();
				}

				if (operandChar == opReturn) {
					thread->push(thread->_returnValue);
				}
			}
			break;

// BRANCH INSTRUCTIONS    
		CASEOP(opJmp)
			jmpOffset1 = scriptS.readUint16LE();
			thread->_instructionOffset = jmpOffset1;
			break;
		CASEOP(opJmpTrueV)
			jmpOffset1 = scriptS.readUint16LE();
			if (thread->pop()) {
				thread->_instructionOffset = jmpOffset1;
			}
			break;
		CASEOP(opJmpFalseV)
			jmpOffset1 = scriptS.readUint16LE();
			if (!thread->pop()) {
				thread->_instructionOffset = jmpOffset1;
			}
			break;
		CASEOP(opJmpTrue)
			jmpOffset1 = scriptS.readUint16LE();
			if (thread->stackTop()) {
				thread->_instructionOffset = jmpOffset1;
			}
			break;
		CASEOP(opJmpFalse)
			jmpOffset1 = scriptS.readUint16LE();
			if (!thread->stackTop()) {
				thread->_instructionOffset = jmpOffset1;
			}
			break;
		CASEOP(opJmpSwitch)
			iparam1 = scriptS.readSint16LE();
			iparam2 = thread->pop();
			while (iparam1--) {
				iparam3 = scriptS.readUint16LE();
				thread->_instructionOffset = scriptS.readUint16LE();
				if (iparam3 == iparam2) {
					break;
				}
			}
			if (iparam1 < 0) {
				thread->_instructionOffset = scriptS.readUint16LE();
			}
			break;
		CASEOP(opJmpRandom)
			// Supposedly the number of possible branches.
			// The original interpreter ignores it.
			scriptS.readUint16LE();
			iparam1 = scriptS.readSint16LE();
			iparam1 = _vm->_rnd.getRandomNumber(iparam1 - 1);
			while (1) {
				iparam2 = scriptS.readSint16LE();
				thread->_instructionOffset = scriptS.readUint16LE();
				
				iparam1 -= iparam2;
				if (iparam1 < 0) {
					break;
				}
			}
			break;

// UNARY INSTRUCTIONS
		CASEOP(opNegate)
			thread->push(-thread->pop());
			break;
		CASEOP(opNot)
			thread->push(!thread->pop());
			break;
		CASEOP(opCompl)
			thread->push(~thread->pop());
			break;

		CASEOP(opIncV)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += iparam1;
			*(uint16*)addr += 1;
			break;
		CASEOP(opDecV)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += iparam1;
			*(uint16*)addr -= 1;
			break;
		CASEOP(opPostInc)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += iparam1;
			thread->push(*(int16*)addr);
			*(uint16*)addr += 1;
			break;
		CASEOP(opPostDec)
			addr = thread->baseAddress(scriptS.readByte());
			iparam1 = scriptS.readSint16LE();
			addr += iparam1;
			thread->push(*(int16*)addr);
			*(uint16*)addr -= 1;
			break;

// ARITHMETIC INSTRUCTIONS    
		CASEOP(opAdd)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 += iparam2;
			thread->push(iparam1);
			break;
		CASEOP(opSub)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 -= iparam2;
			thread->push(iparam1);
			break;
		CASEOP(opMul)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 *= iparam2;
			thread->push(iparam1);
			break;
		CASEOP(opDiv)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 /= iparam2;
			thread->push(iparam1);
			break;
		CASEOP(opMod)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 %= iparam2;
			thread->push(iparam1);
			break;

// COMPARISION INSTRUCTIONS    
		CASEOP(opEq)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			thread->push((iparam1 == iparam2) ? 1 : 0);
			debug(8, "0x%X 0x%X", iparam1, iparam2);
			break;
		CASEOP(opNe)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			thread->push((iparam1 != iparam2) ? 1 : 0);
			break;
		CASEOP(opGt)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			thread->push((iparam1 > iparam2) ? 1 : 0);
			break;
		CASEOP(opLt)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			thread->push((iparam1 < iparam2) ? 1 : 0);
			break;
		CASEOP(opGe)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			thread->push((iparam1 >= iparam2) ? 1 : 0);
			break;
		CASEOP(opLe)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			thread->push((iparam1 <= iparam2) ? 1 : 0);
			break;

// SHIFT INSTRUCTIONS    
		CASEOP(opRsh)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 >>= iparam2;
			thread->push(iparam1);
			break;
		CASEOP(opLsh)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 <<= iparam2;
			thread->push(iparam1);
			break;

// BITWISE INSTRUCTIONS   
		CASEOP(opAnd)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 &= iparam2;
			thread->push(iparam1);
			break;
		CASEOP(opOr)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 |= iparam2;
			thread->push(iparam1);
			break;
		CASEOP(opXor)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			iparam1 ^= iparam2;
			thread->push(iparam1);
			break;

// LOGICAL INSTRUCTIONS     
		CASEOP(opLAnd)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			thread->push((iparam1 && iparam2) ? 1 : 0);
			break;
		CASEOP(opLOr)
			iparam2 = thread->pop();
			iparam1 = thread->pop();
			thread->push((iparam1 || iparam2) ? 1 : 0);
			break;
		CASEOP(opLXor)
			iparam2 = thread->pop();
			iparam1 = thread->pop();			
			thread->push(((iparam1 && !iparam2) || (!iparam1 && iparam2)) ? 1 : 0);
			break;

// GAME INSTRUCTIONS  
		CASEOP(opSpeak) {
				int stringsCount;
				uint16 actorId;
				uint16 speechFlags;
				int sampleResourceId = -1;
				int16 first;
				const char *strings[ACTOR_SPEECH_STRING_MAX];

				if (_vm->_actor->isSpeaking()) {
					thread->wait(kWaitTypeSpeech);
					return false;
				}

				stringsCount = scriptS.readByte();
				actorId =  scriptS.readUint16LE();
				speechFlags = scriptS.readByte();
				scriptS.readUint16LE(); // x,y skip
				
				if (stringsCount == 0)
					error("opSpeak stringsCount == 0");

				if (stringsCount > ACTOR_SPEECH_STRING_MAX)
					error("opSpeak stringsCount=0x%X exceed ACTOR_SPEECH_STRING_MAX", stringsCount);
				
				iparam1 = first = thread->stackTop();
				for (i = 0; i < stringsCount; i++) {
					 iparam1 = thread->pop();
					 strings[i] = thread->_strings->getString(iparam1);
				}
				// now data contains last string index

				if (_vm->getGameId() == GID_ITE_DISK_G) { // special ITE dos
					if ((_vm->_scene->currentSceneNumber() == ITE_DEFAULT_SCENE) && 
						(iparam1 >= 288) && (iparam1 <= (RID_SCENE1_VOICE_138 - RID_SCENE1_VOICE_009 + 288))) {
						sampleResourceId = RID_SCENE1_VOICE_009 + iparam1 - 288;
					}
				} else {
					if (thread->_voiceLUT->voicesCount > first) {
						sampleResourceId = thread->_voiceLUT->voices[first];
					}
				}

				_vm->_actor->actorSpeech(actorId, strings, stringsCount, sampleResourceId, speechFlags);				

				if (!(speechFlags & kSpeakAsync)) {
					thread->wait(kWaitTypeSpeech);
				}
			}
			break;
		CASEOP(opDialogBegin)
			if (_conversingThread) {
				thread->wait(kWaitTypeDialogBegin);
				return false;
			}
			_conversingThread = thread;
			_vm->_interface->converseClear();
			break;
		CASEOP(opDialogEnd)
			if (thread == _conversingThread) {
				_vm->_interface->activate();
				_vm->_interface->setMode(kPanelConverse);
				thread->wait(kWaitTypeDialogEnd);
				return false;
			}
			break;
		CASEOP(opReply) {
				const char *str;
				byte replyNum;
				byte flags;
				replyNum = scriptS.readByte();
				flags = scriptS.readByte();
				iparam1 = 0;

				if (flags & kReplyOnce) {
					iparam1 = scriptS.readSint16LE();
					addr = thread->_staticBase + (iparam1 >> 3);
					if (*addr & (1 << (iparam1 & 7))) {
						break;
					}
				}

				str = thread->_strings->getString(thread->pop());
				if (_vm->_interface->converseAddText(str, replyNum, flags, iparam1))
					warning("Error adding ConverseText (%s, %d, %d, %d)", str, replyNum, flags, iparam1);
			}
			break;
		CASEOP(opAnimate)
			scriptS.readUint16LE();
			scriptS.readUint16LE();
			jmpOffset1 = scriptS.readByte();
			thread->_instructionOffset += jmpOffset1;
			break;

		default:
			error("Script::runThread() Invalid opcode encountered 0x%X", operandChar);
		}

		if (thread->_flags & (kTFlagFinished | kTFlagAborted)) {
			error("Wrong flags %d in thread", thread->_flags);
		} 

		// Set instruction offset only if a previous instruction didn't branch
		if (savedInstructionOffset == thread->_instructionOffset) {
			thread->_instructionOffset = scriptS.pos();
		} else {
			if (thread->_instructionOffset >= scriptS.size()) {
				error("Script::runThread() Out of range script execution");
			}

			scriptS.seek(thread->_instructionOffset);
		}	
	}
	return false;
}

} // End of namespace Saga