2004-04-12 21:40:49 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2005-01-01 16:20:17 +00:00
|
|
|
* Copyright (C) 2004-2005 The ScummVM project
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
|
|
|
* 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$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Scripting module thread management component
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/saga.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/gfx.h"
|
|
|
|
#include "saga/actor.h"
|
2004-08-10 18:31:33 +00:00
|
|
|
#include "saga/console.h"
|
2005-01-06 14:02:53 +00:00
|
|
|
#include "saga/interface.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-12 21:39:11 +00:00
|
|
|
#include "saga/script.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
#include "saga/stream.h"
|
2004-12-21 06:49:07 +00:00
|
|
|
#include "saga/scene.h"
|
|
|
|
#include "saga/resnames.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
namespace Saga {
|
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
ScriptThread *Script::createThread(uint16 scriptModuleNumber, uint16 scriptEntryPointNumber) {
|
2005-01-15 20:12:49 +00:00
|
|
|
ScriptThread *newThread;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
loadModule(scriptModuleNumber);
|
|
|
|
if (_modules[scriptModuleNumber].entryPointsCount <= scriptEntryPointNumber) {
|
|
|
|
error("Script::createThread wrong scriptEntryPointNumber");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2005-01-21 21:55:54 +00:00
|
|
|
|
2005-01-15 20:12:49 +00:00
|
|
|
newThread = _threadList.pushFront().operator->();
|
2005-01-21 21:55:54 +00:00
|
|
|
newThread->_flags = kTFlagNone;
|
|
|
|
newThread->_stackSize = DEFAULT_THREAD_STACK_SIZE;
|
|
|
|
newThread->_stackBuf = (uint16 *)malloc(newThread->_stackSize * sizeof(*newThread->_stackBuf));
|
|
|
|
newThread->_stackTopIndex = newThread->_stackSize - 1; // or 2 - as in original
|
|
|
|
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;
|
2004-10-22 07:18:01 +00:00
|
|
|
|
2005-01-15 20:12:49 +00:00
|
|
|
return newThread;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-12-24 10:22:01 +00:00
|
|
|
void Script::wakeUpActorThread(int waitType, void *threadObj) {
|
2005-01-15 20:12:49 +00:00
|
|
|
ScriptThread *thread;
|
2004-12-24 10:22:01 +00:00
|
|
|
ScriptThreadList::iterator threadIterator;
|
|
|
|
|
|
|
|
for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
|
|
|
|
thread = threadIterator.operator->();
|
2005-01-21 21:55:54 +00:00
|
|
|
if ((thread->_flags & kTFlagWaiting) && (thread->_waitType == waitType) && (thread->_threadObj == threadObj)) {
|
|
|
|
thread->_flags &= ~kTFlagWaiting;
|
2004-12-24 10:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-21 06:49:07 +00:00
|
|
|
void Script::wakeUpThreads(int waitType) {
|
2005-01-15 20:12:49 +00:00
|
|
|
ScriptThread *thread;
|
2004-12-21 06:49:07 +00:00
|
|
|
ScriptThreadList::iterator threadIterator;
|
|
|
|
|
|
|
|
for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
|
|
|
|
thread = threadIterator.operator->();
|
2005-01-21 21:55:54 +00:00
|
|
|
if ((thread->_flags & kTFlagWaiting) && (thread->_waitType == waitType)) {
|
|
|
|
thread->_flags &= ~kTFlagWaiting;
|
2004-12-21 06:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-12-21 06:49:07 +00:00
|
|
|
void Script::wakeUpThreadsDelayed(int waitType, int sleepTime) {
|
2005-01-15 20:12:49 +00:00
|
|
|
ScriptThread *thread;
|
2004-12-21 06:49:07 +00:00
|
|
|
ScriptThreadList::iterator threadIterator;
|
|
|
|
|
|
|
|
for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
|
|
|
|
thread = threadIterator.operator->();
|
2005-01-21 21:55:54 +00:00
|
|
|
if ((thread->_flags & kTFlagWaiting) && (thread->_waitType == waitType)) {
|
|
|
|
thread->_waitType = kWaitTypeDelay;
|
|
|
|
thread->_sleepTime = sleepTime;
|
2004-12-21 06:49:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::executeThreads(uint msec) {
|
2005-01-15 20:12:49 +00:00
|
|
|
ScriptThread *thread;
|
2004-12-21 06:49:07 +00:00
|
|
|
ScriptThreadList::iterator threadIterator;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
if (!isInitialized()) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-12-21 06:49:07 +00:00
|
|
|
threadIterator = _threadList.begin();
|
2004-10-27 02:27:54 +00:00
|
|
|
|
2004-12-21 06:49:07 +00:00
|
|
|
while (threadIterator != _threadList.end()) {
|
|
|
|
thread = threadIterator.operator->();
|
2004-10-27 02:27:54 +00:00
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
if (thread->_flags & (kTFlagFinished | kTFlagAborted)) {
|
|
|
|
if (thread->_flags & kTFlagFinished)
|
2005-01-15 20:12:49 +00:00
|
|
|
setPointerVerb();
|
2004-12-15 00:24:12 +00:00
|
|
|
|
2004-12-21 06:49:07 +00:00
|
|
|
threadIterator = _threadList.erase(threadIterator);
|
2004-10-27 02:27:54 +00:00
|
|
|
continue;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-10-27 02:27:54 +00:00
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
if (thread->_flags & kTFlagWaiting) {
|
2004-12-24 10:22:01 +00:00
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
if (thread->_waitType == kWaitTypeDelay) {
|
|
|
|
if (thread->_sleepTime < msec) {
|
|
|
|
thread->_sleepTime = 0;
|
2004-12-24 10:22:01 +00:00
|
|
|
} else {
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_sleepTime -= msec;
|
2004-12-24 10:22:01 +00:00
|
|
|
}
|
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
if (thread->_sleepTime == 0)
|
|
|
|
thread->_flags &= ~kTFlagWaiting;
|
2004-12-21 06:49:07 +00:00
|
|
|
} else {
|
2005-01-21 21:55:54 +00:00
|
|
|
if (thread->_waitType == kWaitTypeWalk) {
|
2004-12-24 10:22:01 +00:00
|
|
|
ActorData *actor;
|
2005-01-21 21:55:54 +00:00
|
|
|
actor = (ActorData *)thread->_threadObj;
|
2004-12-24 10:22:01 +00:00
|
|
|
if (actor->currentAction == kActionWait) {
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_flags &= ~kTFlagWaiting;
|
2004-12-24 10:22:01 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-27 02:27:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
if (!(thread->_flags & kTFlagWaiting))
|
2005-01-03 21:17:32 +00:00
|
|
|
runThread(thread, STHREAD_TIMESLICE);
|
2004-10-27 02:27:54 +00:00
|
|
|
|
2004-12-21 06:49:07 +00:00
|
|
|
++threadIterator;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-15 20:12:49 +00:00
|
|
|
void Script::completeThread(void) {
|
2004-12-15 00:24:12 +00:00
|
|
|
for (int i = 0; i < 40 && !_threadList.isEmpty() ; i++)
|
2004-12-21 06:49:07 +00:00
|
|
|
executeThreads(0);
|
2004-08-12 01:11:11 +00:00
|
|
|
}
|
|
|
|
|
2004-08-12 23:57:45 +00:00
|
|
|
int Script::SThreadDebugStep() {
|
|
|
|
if (_dbg_singlestep) {
|
|
|
|
_dbg_dostep = 1;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
void Script::runThread(ScriptThread *thread, uint instructionLimit) {
|
|
|
|
uint instructionCount;
|
2005-01-21 23:25:54 +00:00
|
|
|
uint16 savedInstructionOffset;
|
|
|
|
|
|
|
|
byte *addr;
|
2005-01-21 21:55:54 +00:00
|
|
|
uint16 param1;
|
|
|
|
uint16 param2;
|
2005-01-21 23:25:54 +00:00
|
|
|
int16 iparam1;
|
|
|
|
int16 iparam2;
|
2004-04-12 21:40:49 +00:00
|
|
|
long iresult;
|
|
|
|
|
2005-01-21 23:25:54 +00:00
|
|
|
byte argumentsCount;
|
|
|
|
uint16 functionNumber;
|
|
|
|
int scriptFunctionReturnValue;
|
|
|
|
ScriptFunctionType scriptFunction;
|
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
uint16 data;
|
|
|
|
uint16 scriptRetVal = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
int debug_print = 0;
|
|
|
|
int n_buf;
|
2005-01-21 21:55:54 +00:00
|
|
|
// int bitstate;
|
2004-12-21 06:49:07 +00:00
|
|
|
int operandChar;
|
2004-04-12 21:40:49 +00:00
|
|
|
int i;
|
|
|
|
int unhandled = 0;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Handle debug single-stepping
|
2004-08-12 23:57:45 +00:00
|
|
|
if ((thread == _dbg_thread) && _dbg_singlestep) {
|
|
|
|
if (_dbg_dostep) {
|
2004-04-12 21:40:49 +00:00
|
|
|
debug_print = 1;
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_sleepTime = 0;
|
|
|
|
instructionLimit = 1;
|
2004-08-12 23:57:45 +00:00
|
|
|
_dbg_dostep = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
2005-01-03 21:17:32 +00:00
|
|
|
return;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
MemoryReadStream scriptS(thread->_moduleBase, thread->_moduleBaseSize);
|
2004-09-19 14:49:00 +00:00
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
scriptS.seek(thread->_instructionOffset);
|
2004-10-09 07:39:46 +00:00
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
for (instructionCount = 0; instructionCount < instructionLimit; instructionCount++) {
|
|
|
|
if (thread->_flags & (kTFlagAsleep))
|
2004-12-21 06:49:07 +00:00
|
|
|
break;
|
|
|
|
|
2005-01-21 23:25:54 +00:00
|
|
|
savedInstructionOffset = thread->_instructionOffset;
|
2004-12-21 06:49:07 +00:00
|
|
|
operandChar = scriptS.readByte();
|
2005-01-21 23:25:54 +00:00
|
|
|
|
|
|
|
#define CASEOP(opName) case opName: \
|
|
|
|
if (operandChar == opName) { \
|
|
|
|
debug(8, #opName); \
|
|
|
|
_vm->_console->DebugPrintf(#opName); \
|
|
|
|
}
|
|
|
|
|
2005-01-03 21:17:32 +00:00
|
|
|
// debug print (opCode name etc) should be placed here
|
|
|
|
// SDebugPrintInstr(thread)
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
debug(8, "Executing thread offset: %lu (%x) stack: %d", thread->_instructionOffset, operandChar, thread->pushedSize());
|
2004-12-21 06:49:07 +00:00
|
|
|
switch (operandChar) {
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opNextBlock)
|
2004-09-21 06:35:00 +00:00
|
|
|
// Some sort of "jump to the start of the next memory
|
|
|
|
// page" instruction, I think.
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = 1024 * ((thread->_instructionOffset / 1024) + 1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// STACK INSTRUCTIONS
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opDup)
|
|
|
|
thread->push(thread->stackTop());
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opDrop)
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opZero)
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(0);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opOne)
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opConstint)
|
|
|
|
CASEOP(opStrlit)
|
2005-01-21 21:55:54 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// DATA INSTRUCTIONS
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opGetFlag)
|
|
|
|
addr = thread->baseAddress(scriptS.readByte());
|
2005-01-21 21:55:54 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 23:25:54 +00:00
|
|
|
addr += (param1 >> 3);
|
|
|
|
param1 = (1 << (param1 & 7));
|
|
|
|
thread->push((*addr) & param1 ? 1 : 0);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opGetInt)
|
|
|
|
addr = thread->baseAddress(scriptS.readByte());
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 23:25:54 +00:00
|
|
|
addr += param1;
|
|
|
|
thread->push(*((uint16*)addr));
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opPutFlag)
|
|
|
|
addr = thread->baseAddress(scriptS.readByte());
|
2005-01-21 21:55:54 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 23:25:54 +00:00
|
|
|
addr += (param1 >> 3);
|
|
|
|
param1 = (1 << (param1 & 7));
|
|
|
|
if (thread->stackTop()) {
|
|
|
|
*addr |= param1;
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
2005-01-21 23:25:54 +00:00
|
|
|
*addr &= ~param1;
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opPutInt)
|
|
|
|
addr = thread->baseAddress(scriptS.readByte());
|
2005-01-21 21:55:54 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 23:25:54 +00:00
|
|
|
addr += param1;
|
|
|
|
*(uint16*)addr = thread->stackTop();
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opPutFlagV)
|
|
|
|
addr = thread->baseAddress(scriptS.readByte());
|
2005-01-21 21:55:54 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 23:25:54 +00:00
|
|
|
addr += (param1 >> 3);
|
|
|
|
param1 = (1 << (param1 & 7));
|
|
|
|
if (thread->pop()) {
|
|
|
|
*addr |= param1;
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
2005-01-21 23:25:54 +00:00
|
|
|
*addr &= ~param1;
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opPutIntV)
|
|
|
|
addr = thread->baseAddress(scriptS.readByte());
|
2005-01-21 21:55:54 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 23:25:54 +00:00
|
|
|
addr += param1;
|
|
|
|
*(uint16*)addr = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// CONTROL INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opCall)
|
|
|
|
argumentsCount = scriptS.readByte();
|
|
|
|
param1 = scriptS.readByte();
|
|
|
|
if (param1 != kAddressModule) {
|
|
|
|
error("Script::runThread param1 != kAddressModule");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2005-01-21 23:25:54 +00:00
|
|
|
addr = thread->baseAddress(param1);
|
|
|
|
param1 = scriptS.readUint16LE();
|
|
|
|
addr += param1;
|
|
|
|
thread->push(argumentsCount);
|
|
|
|
|
|
|
|
param2 = 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(param2);
|
|
|
|
thread->_instructionOffset = param1;
|
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-21 23:25:54 +00:00
|
|
|
CASEOP(opCcall)
|
|
|
|
CASEOP(opCcallV)
|
|
|
|
argumentsCount = scriptS.readByte();
|
|
|
|
functionNumber = scriptS.readUint16LE();
|
|
|
|
if (functionNumber >= SCRIPT_FUNCTION_MAX) {
|
|
|
|
error("Script::runThread() Invalid script function number");
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-21 23:25:54 +00:00
|
|
|
debug(8, "Calling 0x%X %s", functionNumber, _scriptFunctionsList[functionNumber].scriptFunctionName);
|
|
|
|
scriptFunction = _scriptFunctionsList[functionNumber].scriptFunction;
|
|
|
|
scriptFunctionReturnValue = (this->*scriptFunction)(thread, argumentsCount);
|
|
|
|
if (scriptFunctionReturnValue != SUCCESS) {
|
|
|
|
_vm->_console->DebugPrintf(S_WARN_PREFIX "%X: Script function %d failed.\n", thread->_instructionOffset, scriptFunctionReturnValue);
|
|
|
|
}
|
2004-10-03 17:11:23 +00:00
|
|
|
|
2005-01-21 23:25:54 +00:00
|
|
|
if (scriptFunction == SF_gotoScene) { // SF_gotoScene
|
|
|
|
instructionCount = instructionLimit; // break the loop
|
|
|
|
break;
|
|
|
|
}
|
2004-10-27 02:27:54 +00:00
|
|
|
|
2005-01-21 23:25:54 +00:00
|
|
|
if (operandChar == opCcall) // CALL function
|
|
|
|
thread->push(thread->_returnValue);
|
2004-10-27 02:27:54 +00:00
|
|
|
|
2005-01-21 23:25:54 +00:00
|
|
|
if (thread->_flags & kTFlagAsleep)
|
|
|
|
instructionCount = instructionLimit; // break out of loop!
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-03 21:17:32 +00:00
|
|
|
case opEnter: // Enter a function
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->push(thread->_frameIndex);
|
|
|
|
thread->_frameIndex = thread->_stackTopIndex;
|
|
|
|
thread->_stackTopIndex -= (scriptS.readUint16LE() / 2);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-03 21:17:32 +00:00
|
|
|
case opReturn: // Return with value
|
2004-10-03 17:11:23 +00:00
|
|
|
scriptRetVal = thread->pop();
|
2005-01-16 17:07:27 +00:00
|
|
|
// Fall through
|
2005-01-03 21:17:32 +00:00
|
|
|
case opReturnV: // Return with void
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_stackTopIndex = thread->_frameIndex;
|
|
|
|
thread->_frameIndex = thread->pop();
|
|
|
|
if (thread->pushedSize() == 0) {
|
2004-12-03 19:15:44 +00:00
|
|
|
_vm->_console->DebugPrintf("Script execution complete.\n");
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_flags |= kTFlagFinished;
|
2005-01-03 21:17:32 +00:00
|
|
|
return;
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = thread->pop();
|
2004-09-23 06:46:44 +00:00
|
|
|
/* int n_args = */ thread->pop();
|
2005-01-03 21:17:32 +00:00
|
|
|
if (operandChar == opReturn)
|
2004-10-03 17:11:23 +00:00
|
|
|
thread->push(scriptRetVal);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// BRANCH INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JMP): Unconditional jump
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x1D:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = (unsigned long)param1;
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JNZP): Jump if nonzero + POP
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x1E:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (data) {
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = (unsigned long)param1;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JZP): Jump if zero + POP
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x1F:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (!data) {
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = (unsigned long)param1;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JNZ): Jump if nonzero
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x20:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->stackTop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (data) {
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = (unsigned long)param1;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (JZ): Jump if zero
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x21:
|
2004-08-22 18:28:42 +00:00
|
|
|
param1 = scriptS.readUint16LE();
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->stackTop();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (!data) {
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = (unsigned long)param1;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (SWCH): Switch
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x22:
|
|
|
|
{
|
|
|
|
int n_switch;
|
2005-01-21 21:55:54 +00:00
|
|
|
uint16 switch_num;
|
2004-04-12 21:40:49 +00:00
|
|
|
unsigned int switch_jmp;
|
|
|
|
unsigned int default_jmp;
|
|
|
|
int case_found = 0;
|
|
|
|
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2004-08-22 18:28:42 +00:00
|
|
|
n_switch = scriptS.readUint16LE();
|
2005-01-19 11:29:29 +00:00
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < n_switch; i++) {
|
2004-08-22 18:28:42 +00:00
|
|
|
switch_num = scriptS.readUint16LE();
|
|
|
|
switch_jmp = scriptS.readUint16LE();
|
2004-05-01 16:15:55 +00:00
|
|
|
// Found the specified case
|
2005-01-21 21:55:54 +00:00
|
|
|
if (data == switch_num) {
|
|
|
|
thread->_instructionOffset = switch_jmp;
|
2004-04-12 21:40:49 +00:00
|
|
|
case_found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Jump to default case
|
2004-04-12 21:40:49 +00:00
|
|
|
if (!case_found) {
|
2004-08-22 18:28:42 +00:00
|
|
|
default_jmp = scriptS.readUint16LE();
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = default_jmp;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (RJMP): Random branch
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x24:
|
|
|
|
{
|
2005-01-14 10:10:23 +00:00
|
|
|
// Supposedly the number of possible branches.
|
|
|
|
// The original interpreter ignores it.
|
2004-08-22 18:28:42 +00:00
|
|
|
scriptS.readUint16LE();
|
2005-01-14 10:10:23 +00:00
|
|
|
|
|
|
|
uint16 probability = _vm->_rnd.getRandomNumber(scriptS.readUint16LE() - 1);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
uint16 branch_probability = scriptS.readUint16LE();
|
|
|
|
uint16 offset = scriptS.readUint16LE();
|
|
|
|
|
|
|
|
if (branch_probability > probability) {
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = offset;
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-01-14 10:10:23 +00:00
|
|
|
|
|
|
|
probability -= branch_probability;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-08-26 23:28:10 +00:00
|
|
|
// UNARY INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (NEG) Negate stack by 2's complement
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x25:
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2005-01-18 11:55:31 +00:00
|
|
|
thread->push(-(int)data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (TSTZ) Test for zero
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x26:
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2005-01-16 17:07:27 +00:00
|
|
|
thread->push(!data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (NOT) Binary not
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x27:
|
2004-09-23 06:46:44 +00:00
|
|
|
data = thread->pop();
|
2005-01-16 17:07:27 +00:00
|
|
|
thread->push(~data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x28: // inc_v increment, don't push
|
2005-01-16 17:07:27 +00:00
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 21:55:54 +00:00
|
|
|
//getWord(n_buf, param1, &data);
|
|
|
|
//putWord(n_buf, param1, data + 1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x29: // dec_v decrement, don't push
|
2005-01-16 17:07:27 +00:00
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 21:55:54 +00:00
|
|
|
//getWord(n_buf, param1, &data);
|
|
|
|
//putWord(n_buf, param1, data - 1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x2A: // postinc
|
2005-01-16 17:07:27 +00:00
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 21:55:54 +00:00
|
|
|
//getWord(n_buf, param1, &data);
|
|
|
|
// thread->push(data);
|
|
|
|
//putWord(n_buf, param1, data + 1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x2B: // postdec
|
2005-01-16 17:07:27 +00:00
|
|
|
n_buf = scriptS.readByte();
|
|
|
|
param1 = scriptS.readUint16LE();
|
2005-01-21 21:55:54 +00:00
|
|
|
//getWord(n_buf, param1, &data);
|
|
|
|
// thread->push(data);
|
|
|
|
//putWord(n_buf, param1, data - 1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// ARITHMETIC INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (ADD): Addition
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x2C:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 + iparam2;
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->push( iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (SUB): Subtraction
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x2D:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 - iparam2;
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->push( iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (MULT): Integer multiplication
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x2E:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 * iparam2;
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->push( iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-13 01:41:23 +00:00
|
|
|
// (DIV): Integer division
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x2F:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 / iparam2;
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->push( iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (MOD) Modulus
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x30:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
iresult = iparam1 % iparam2;
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->push( iresult);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (EQU) Test equality
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x33:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 == iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (NEQU) Test inequality
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x34:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 != iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (GRT) Test Greater-than
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x35:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 > iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LST) Test Less-than
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x36:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 < iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (GRTE) Test Greater-than or Equal to
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x37:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 >= iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LSTE) Test Less-than or Equal to
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x38:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
|
|
|
iparam1 = (long)param1;
|
|
|
|
data = (iparam1 <= iparam2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-13 01:41:23 +00:00
|
|
|
|
|
|
|
// BITWISE INSTRUCTIONS
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (SHR): Arithmetic binary shift right
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x3F:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
iparam2 = (long)param2;
|
2004-05-01 16:15:55 +00:00
|
|
|
// Preserve most significant bit
|
2005-01-02 14:52:11 +00:00
|
|
|
data = (0x01 << ((sizeof(param1) * CHAR_BIT) - 1)) & param1;
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < (int)iparam2; i++) {
|
|
|
|
param1 >>= 1;
|
|
|
|
param1 |= data;
|
|
|
|
}
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (SHL) Binary shift left
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x40:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
param1 <<= param2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (AND) Binary AND
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x41:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
param1 &= param2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (OR) Binary OR
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x42:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
param1 |= param2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (XOR) Binary XOR
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x43:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
param1 ^= param2;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(param1);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// BOOLEAN LOGIC INSTRUCTIONS
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LAND): Logical AND
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x44:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
data = (param1 && param2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LOR): Logical OR
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x45:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
data = (param1 || param2) ? 1 : 0;
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2004-05-01 16:15:55 +00:00
|
|
|
// (LXOR): Logical XOR
|
2004-04-12 21:40:49 +00:00
|
|
|
case 0x46:
|
2004-09-23 06:46:44 +00:00
|
|
|
param2 = thread->pop();
|
|
|
|
param1 = thread->pop();
|
2004-04-12 21:40:49 +00:00
|
|
|
data = ((param1) ? !(param2) : !!(param2));
|
2004-09-23 06:46:44 +00:00
|
|
|
thread->push(data);
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// GAME INSTRUCTIONS
|
2004-12-22 21:04:50 +00:00
|
|
|
case opSpeak: { // (opSpeak): Play Character Speech
|
2004-12-21 06:49:07 +00:00
|
|
|
int stringsCount;
|
2004-12-17 11:18:56 +00:00
|
|
|
uint16 actorId;
|
2004-12-21 06:49:07 +00:00
|
|
|
int speechFlags;
|
|
|
|
int sampleResourceId = -1;
|
|
|
|
int first;
|
|
|
|
const char *strings[ACTOR_SPEECH_STRING_MAX];
|
|
|
|
|
|
|
|
if (_vm->_actor->isSpeaking()) {
|
|
|
|
thread->wait(kWaitTypeSpeech);
|
2005-01-03 21:17:32 +00:00
|
|
|
return;
|
2004-12-21 06:49:07 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-12-21 06:49:07 +00:00
|
|
|
stringsCount = scriptS.readByte();
|
|
|
|
actorId = scriptS.readUint16LE();
|
|
|
|
speechFlags = scriptS.readByte();
|
|
|
|
scriptS.readUint16LE(); // x,y skip
|
|
|
|
|
|
|
|
if (stringsCount == 0)
|
|
|
|
error("opSpeak stringsCount == 0");
|
|
|
|
|
2004-12-22 21:04:50 +00:00
|
|
|
if (stringsCount > ACTOR_SPEECH_STRING_MAX)
|
2004-12-21 06:49:07 +00:00
|
|
|
error("opSpeak stringsCount=0x%X exceed ACTOR_SPEECH_STRING_MAX", stringsCount);
|
|
|
|
|
|
|
|
data = first = thread->stackTop();
|
|
|
|
for (i = 0; i < stringsCount; i++) {
|
|
|
|
data = thread->pop();
|
2005-01-21 21:55:54 +00:00
|
|
|
strings[i] = thread->_strings->getString(data);
|
2004-12-21 06:49:07 +00:00
|
|
|
}
|
|
|
|
// now data contains last string index
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
if (_vm->getGameId() == GID_ITE_DISK_G) { // special ITE dos
|
2004-12-22 21:04:50 +00:00
|
|
|
if ((_vm->_scene->currentSceneNumber() == ITE_DEFAULT_SCENE) && (data >= 288) && (data <= (RID_SCENE1_VOICE_138 - RID_SCENE1_VOICE_009 + 288))) {
|
|
|
|
sampleResourceId = RID_SCENE1_VOICE_009 + data - 288;
|
2004-12-21 06:49:07 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-01-21 21:55:54 +00:00
|
|
|
if (thread->_voiceLUT->voicesCount > first) {
|
|
|
|
sampleResourceId = thread->_voiceLUT->voices[first];
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-12-21 06:49:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_vm->_actor->actorSpeech(actorId, strings, stringsCount, sampleResourceId, speechFlags);
|
|
|
|
|
|
|
|
if (!(speechFlags & kSpeakAsync)) {
|
|
|
|
thread->wait(kWaitTypeSpeech);
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = scriptS.pos();
|
2005-01-03 21:17:32 +00:00
|
|
|
return;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2005-01-17 20:17:06 +00:00
|
|
|
|
|
|
|
case opDialogBegin: // (DLGS): Initialize dialogue interface
|
|
|
|
if (_conversingThread) {
|
|
|
|
thread->wait(kWaitTypeDialogBegin);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_conversingThread = thread;
|
|
|
|
_vm->_interface->converseClear();
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-17 20:17:06 +00:00
|
|
|
|
|
|
|
case opDialogEnd: // (DLGX): Run dialogue interface
|
|
|
|
if (thread == _conversingThread) {
|
2005-01-06 14:02:53 +00:00
|
|
|
_vm->_interface->activate();
|
|
|
|
_vm->_interface->setMode(kPanelConverse);
|
2005-01-17 20:17:06 +00:00
|
|
|
thread->wait(kWaitTypeDialogEnd);
|
|
|
|
return;
|
2005-01-06 14:02:53 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
break;
|
2005-01-17 20:17:06 +00:00
|
|
|
|
|
|
|
case opReply: // (DLGO): Add a dialogue option to interface
|
2004-04-12 21:40:49 +00:00
|
|
|
{
|
2005-01-21 21:55:54 +00:00
|
|
|
uint16 n = 0;
|
2005-01-17 20:17:06 +00:00
|
|
|
const char *str;
|
|
|
|
int replyNum = scriptS.readByte();
|
|
|
|
int flags = scriptS.readByte();
|
|
|
|
|
|
|
|
if (flags & kReplyOnce) {
|
|
|
|
n = scriptS.readUint16LE();
|
|
|
|
// TODO:
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2005-01-17 20:17:06 +00:00
|
|
|
|
2005-01-21 21:55:54 +00:00
|
|
|
str = thread->_strings->getString(thread->pop());
|
2005-01-17 20:17:06 +00:00
|
|
|
if (_vm->_interface->converseAddText(str, replyNum, flags, n))
|
|
|
|
warning("Error adding ConverseText (%s, %d, %d, %d)", str, replyNum, flags, n);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-08-26 23:28:10 +00:00
|
|
|
case 0x57: // animate
|
|
|
|
scriptS.readUint16LE();
|
|
|
|
scriptS.readUint16LE();
|
|
|
|
iparam1 = (long)scriptS.readByte();
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset += iparam1;
|
2004-08-26 23:28:10 +00:00
|
|
|
break;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// End instruction list
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
default:
|
2005-01-03 21:17:32 +00:00
|
|
|
scriptError(thread, "Invalid opcode encountered");
|
|
|
|
return;
|
2004-05-01 16:15:55 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 16:15:55 +00:00
|
|
|
// Set instruction offset only if a previous instruction didn't branch
|
2005-01-21 23:25:54 +00:00
|
|
|
if (savedInstructionOffset == thread->_instructionOffset) {
|
2005-01-21 21:55:54 +00:00
|
|
|
thread->_instructionOffset = scriptS.pos();
|
2004-09-19 14:49:00 +00:00
|
|
|
} else {
|
2005-01-21 21:55:54 +00:00
|
|
|
if (thread->_instructionOffset >= scriptS.size()) {
|
2005-01-03 21:17:32 +00:00
|
|
|
scriptError(thread, "Out of range script execution");
|
|
|
|
return;
|
|
|
|
}
|
2005-01-21 23:25:54 +00:00
|
|
|
|
|
|
|
scriptS.seek(thread->_instructionOffset);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2005-01-03 21:17:32 +00:00
|
|
|
|
|
|
|
if (unhandled) { // TODO: remove it
|
|
|
|
scriptError(thread, "Unhandled opcode");
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Saga
|
|
|
|
|