2005-01-09 16:06:29 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2004-2005 The ScummVM project
|
2004-10-15 06:06:47 +00:00
|
|
|
*
|
|
|
|
* 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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-10-15 06:06:47 +00:00
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2005-08-19 22:12:09 +00:00
|
|
|
#include "common/stream.h"
|
|
|
|
#include "common/util.h"
|
2005-10-13 18:05:04 +00:00
|
|
|
#include "common/system.h"
|
2005-06-24 16:01:42 +00:00
|
|
|
#include "kyra/kyra.h"
|
|
|
|
#include "kyra/resource.h"
|
2005-10-13 18:05:04 +00:00
|
|
|
#include "kyra/script.h"
|
2004-10-15 06:06:47 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
#define FORM_CHUNK 0x4D524F46
|
|
|
|
#define TEXT_CHUNK 0x54584554
|
|
|
|
#define DATA_CHUNK 0x41544144
|
|
|
|
#define ORDR_CHUNK 0x5244524F
|
2004-10-15 06:06:47 +00:00
|
|
|
|
|
|
|
namespace Kyra {
|
2005-10-13 18:05:04 +00:00
|
|
|
ScriptHelper::ScriptHelper(KyraEngine *vm) : _vm(vm) {
|
|
|
|
#define COMMAND(x) { &ScriptHelper::x, #x }
|
2004-11-14 20:11:22 +00:00
|
|
|
// now we create a list of all Command/Opcode procs and so
|
|
|
|
static CommandEntry commandProcs[] = {
|
|
|
|
// 0x00
|
2005-10-13 18:05:04 +00:00
|
|
|
COMMAND(c1_jmpTo),
|
|
|
|
COMMAND(c1_setRetValue),
|
|
|
|
COMMAND(c1_pushRetOrPos),
|
2004-11-14 20:11:22 +00:00
|
|
|
COMMAND(c1_push),
|
|
|
|
// 0x04
|
2005-07-30 21:11:48 +00:00
|
|
|
COMMAND(c1_push),
|
2004-11-14 20:11:22 +00:00
|
|
|
COMMAND(c1_pushVar),
|
2005-10-13 18:05:04 +00:00
|
|
|
COMMAND(c1_pushBPNeg),
|
|
|
|
COMMAND(c1_pushBPAdd),
|
2004-11-14 20:11:22 +00:00
|
|
|
// 0x08
|
2005-10-13 18:05:04 +00:00
|
|
|
COMMAND(c1_popRetOrPos),
|
2004-11-14 20:11:22 +00:00
|
|
|
COMMAND(c1_popVar),
|
2005-10-13 18:05:04 +00:00
|
|
|
COMMAND(c1_popBPNeg),
|
|
|
|
COMMAND(c1_popBPAdd),
|
2004-11-14 20:11:22 +00:00
|
|
|
// 0x0C
|
2005-10-13 18:05:04 +00:00
|
|
|
COMMAND(c1_addSP),
|
|
|
|
COMMAND(c1_subSP),
|
2004-11-14 20:11:22 +00:00
|
|
|
COMMAND(c1_execOpcode),
|
2005-10-13 18:05:04 +00:00
|
|
|
COMMAND(c1_ifNotJmp),
|
2004-11-14 20:11:22 +00:00
|
|
|
// 0x10
|
|
|
|
COMMAND(c1_negate),
|
2005-10-13 18:05:04 +00:00
|
|
|
COMMAND(c1_eval),
|
|
|
|
COMMAND(c1_setRetAndJmp)
|
2004-11-14 20:11:22 +00:00
|
|
|
};
|
|
|
|
_commands = commandProcs;
|
2005-10-13 18:05:04 +00:00
|
|
|
#undef COMMAND
|
2004-11-14 20:11:22 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
ScriptHelper::~ScriptHelper() {
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-29 18:37:40 +00:00
|
|
|
bool ScriptHelper::loadScript(const char *filename, ScriptData *scriptData, KyraEngine::OpcodeProc *opcodes, int opcodeSize, byte *specialPtr) {
|
2005-10-13 18:05:04 +00:00
|
|
|
uint32 size = 0;
|
|
|
|
uint8 *data = _vm->resource()->fileData(filename, &size);
|
|
|
|
byte *curData = data;
|
|
|
|
|
|
|
|
uint32 formBlockSize = getFORMBlockSize(curData);
|
|
|
|
if (formBlockSize == (uint32)-1) {
|
|
|
|
delete [] data;
|
|
|
|
error("No FORM chunk found in file: '%s'", filename);
|
|
|
|
return false;
|
2004-11-14 20:11:22 +00:00
|
|
|
}
|
2005-10-13 18:05:04 +00:00
|
|
|
|
|
|
|
uint32 chunkSize = getIFFBlockSize(data, curData, size, TEXT_CHUNK);
|
|
|
|
if (chunkSize != (uint32)-1) {
|
|
|
|
if (specialPtr) {
|
|
|
|
scriptData->mustBeFreed = 0;
|
|
|
|
scriptData->text = specialPtr;
|
|
|
|
specialPtr += chunkSize;
|
2004-11-14 20:11:22 +00:00
|
|
|
} else {
|
2005-10-13 18:05:04 +00:00
|
|
|
scriptData->mustBeFreed = 1;
|
|
|
|
scriptData->text = new byte[chunkSize];
|
|
|
|
}
|
|
|
|
if (!loadIFFBlock(data, curData, size, TEXT_CHUNK, scriptData->text, chunkSize)) {
|
|
|
|
delete [] data;
|
|
|
|
unloadScript(scriptData);
|
|
|
|
error("Couldn't load TEXT chunk from file: '%s'", filename);
|
|
|
|
return false;
|
2004-10-15 06:06:47 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-13 18:05:04 +00:00
|
|
|
|
|
|
|
chunkSize = getIFFBlockSize(data, curData, size, ORDR_CHUNK);
|
|
|
|
if (chunkSize == (uint32)-1) {
|
|
|
|
delete [] data;
|
|
|
|
unloadScript(scriptData);
|
|
|
|
error("No ORDR chunk found in file: '%s'", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (specialPtr) {
|
|
|
|
scriptData->mustBeFreed = 0;
|
|
|
|
scriptData->ordr = specialPtr;
|
|
|
|
specialPtr += chunkSize;
|
|
|
|
} else {
|
|
|
|
scriptData->mustBeFreed = 1;
|
|
|
|
scriptData->ordr = new byte[chunkSize];
|
|
|
|
}
|
|
|
|
if (!loadIFFBlock(data, curData, size, ORDR_CHUNK, scriptData->ordr, chunkSize)) {
|
|
|
|
delete [] data;
|
|
|
|
unloadScript(scriptData);
|
|
|
|
error("Couldn't load ORDR chunk from file: '%s'", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
chunkSize = chunkSize / 2;
|
|
|
|
while (chunkSize--) {
|
|
|
|
((uint16*)scriptData->ordr)[chunkSize] = READ_BE_UINT16(&((uint16*)scriptData->ordr)[chunkSize]);
|
|
|
|
}
|
|
|
|
|
|
|
|
chunkSize = getIFFBlockSize(data, curData, size, DATA_CHUNK);
|
|
|
|
if (chunkSize == (uint32)-1) {
|
|
|
|
delete [] data;
|
|
|
|
unloadScript(scriptData);
|
|
|
|
error("No DATA chunk found in file: '%s'", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (specialPtr) {
|
|
|
|
scriptData->mustBeFreed = 0;
|
|
|
|
scriptData->data = specialPtr;
|
|
|
|
specialPtr += chunkSize;
|
|
|
|
} else {
|
|
|
|
scriptData->mustBeFreed = 1;
|
|
|
|
scriptData->data = new byte[chunkSize];
|
|
|
|
}
|
|
|
|
if (!loadIFFBlock(data, curData, size, DATA_CHUNK, scriptData->data, chunkSize)) {
|
|
|
|
delete [] data;
|
|
|
|
unloadScript(scriptData);
|
|
|
|
error("Couldn't load DATA chunk from file: '%s'", filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
scriptData->dataSize = chunkSize / 2;
|
2005-10-29 18:37:40 +00:00
|
|
|
scriptData->opcodes = opcodes;
|
|
|
|
scriptData->opcodeSize = opcodeSize;
|
2005-10-13 18:05:04 +00:00
|
|
|
|
|
|
|
delete [] data;
|
|
|
|
return true;
|
2004-11-14 20:11:22 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
void ScriptHelper::unloadScript(ScriptData *data) {
|
|
|
|
if (data->mustBeFreed) {
|
|
|
|
delete [] data->text;
|
|
|
|
delete [] data->ordr;
|
|
|
|
delete [] data->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->mustBeFreed = 0;
|
|
|
|
data->text = data->ordr = data->data = 0;
|
2004-11-14 20:11:22 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
void ScriptHelper::initScript(ScriptState *scriptStat, ScriptData *data) {
|
|
|
|
scriptStat->dataPtr = data;
|
|
|
|
scriptStat->ip = 0;
|
|
|
|
scriptStat->stack[60] = 0;
|
|
|
|
scriptStat->bp = 62;
|
|
|
|
scriptStat->sp = 60;
|
2004-11-14 20:11:22 +00:00
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
bool ScriptHelper::startScript(ScriptState *script, int function) {
|
|
|
|
if (!script->dataPtr) {
|
2004-11-14 20:11:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2005-10-13 18:05:04 +00:00
|
|
|
uint16 functionOffset = ((uint16*)script->dataPtr->ordr)[function];
|
|
|
|
if (functionOffset == (uint16)-1) {
|
|
|
|
return false;
|
2004-10-15 06:06:47 +00:00
|
|
|
}
|
2005-11-27 00:37:36 +00:00
|
|
|
script->ip = &script->dataPtr->data[functionOffset*2];
|
2004-11-14 20:11:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
bool ScriptHelper::validScript(ScriptState *script) {
|
|
|
|
if (!script->ip || !script->dataPtr)
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
bool ScriptHelper::runScript(ScriptState *script) {
|
|
|
|
_parameter = 0;
|
|
|
|
_continue = true;
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
if (!script->ip) {
|
2005-10-13 18:05:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
int16 code = READ_BE_UINT16(script->ip); script->ip += 2;
|
2005-10-13 18:05:04 +00:00
|
|
|
int16 opcode = (code >> 8) & 0x1F;
|
|
|
|
|
|
|
|
if (code & 0x8000) {
|
|
|
|
opcode = 0;
|
|
|
|
_parameter = code & 0x7FFF;
|
|
|
|
} else if (code & 0x4000) {
|
2005-11-13 19:35:01 +00:00
|
|
|
_parameter = (int8)(code);
|
2005-10-13 18:05:04 +00:00
|
|
|
} else if (code & 0x2000) {
|
2006-01-08 16:15:34 +00:00
|
|
|
_parameter = READ_BE_UINT16(script->ip); script->ip += 2;
|
2005-10-13 18:05:04 +00:00
|
|
|
} else {
|
|
|
|
_parameter = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opcode > 18) {
|
|
|
|
error("Script unknown command: %d", opcode);
|
|
|
|
} else {
|
|
|
|
debug(5, "%s(%d)", _commands[opcode].desc, _parameter);
|
2006-01-08 16:15:34 +00:00
|
|
|
(this->*(_commands[opcode].proc))(script);
|
2005-10-13 18:05:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return _continue;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
uint32 ScriptHelper::getFORMBlockSize(byte *&data) const {
|
|
|
|
static const uint32 chunkName = FORM_CHUNK;
|
|
|
|
if (READ_LE_UINT32(data) != chunkName) {
|
|
|
|
return (uint32)-1;
|
|
|
|
}
|
|
|
|
data += 4;
|
|
|
|
uint32 retValue = READ_BE_UINT32(data); data += 4;
|
|
|
|
return retValue;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
uint32 ScriptHelper::getIFFBlockSize(byte *start, byte *&data, uint32 maxSize, const uint32 chunkName) const {
|
|
|
|
uint32 size = (uint32)-1;
|
|
|
|
bool special = false;
|
|
|
|
|
|
|
|
if (data == (start + maxSize)) {
|
|
|
|
data = start + 0x0C;
|
|
|
|
}
|
|
|
|
while (data < (start + maxSize)) {
|
|
|
|
uint32 chunk = READ_LE_UINT32(data); data += 4;
|
|
|
|
uint32 size_temp = READ_BE_UINT32(data); data += 4;
|
|
|
|
if (chunk != chunkName) {
|
|
|
|
if (special) {
|
|
|
|
data += (size_temp + 1) & 0xFFFFFFFE;
|
|
|
|
} else {
|
|
|
|
data = start + 0x0C;
|
|
|
|
special = true;
|
|
|
|
}
|
2004-11-14 20:11:22 +00:00
|
|
|
} else {
|
2005-10-13 18:05:04 +00:00
|
|
|
// kill our data
|
|
|
|
data = start;
|
|
|
|
size = size_temp;
|
2004-11-14 20:11:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-10-13 18:05:04 +00:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2005-10-13 18:05:04 +00:00
|
|
|
bool ScriptHelper::loadIFFBlock(byte *start, byte *&data, uint32 maxSize, const uint32 chunkName, byte *loadTo, uint32 ptrSize) const {
|
|
|
|
bool special = false;
|
|
|
|
|
|
|
|
if (data == (start + maxSize)) {
|
|
|
|
data = start + 0x0C;
|
|
|
|
}
|
|
|
|
while (data < (start + maxSize)) {
|
|
|
|
uint32 chunk = READ_LE_UINT32(data); data += 4;
|
|
|
|
uint32 chunkSize = READ_BE_UINT32(data); data += 4;
|
|
|
|
if (chunk != chunkName) {
|
|
|
|
if (special) {
|
|
|
|
data += (chunkSize + 1) & 0xFFFFFFFE;
|
|
|
|
} else {
|
|
|
|
data = start + 0x0C;
|
|
|
|
special = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint32 loadSize = 0;
|
|
|
|
if (chunkSize < ptrSize)
|
|
|
|
loadSize = chunkSize;
|
|
|
|
else
|
|
|
|
loadSize = ptrSize;
|
|
|
|
memcpy(loadTo, data, loadSize);
|
|
|
|
chunkSize = (chunkSize + 1) & 0xFFFFFFFE;
|
|
|
|
if (chunkSize > loadSize) {
|
|
|
|
data += (chunkSize - loadSize);
|
|
|
|
}
|
|
|
|
return true;
|
2004-10-15 06:06:47 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-13 18:05:04 +00:00
|
|
|
return false;
|
2004-11-14 20:11:22 +00:00
|
|
|
}
|
2006-01-02 22:58:59 +00:00
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark - Command implementations
|
|
|
|
#pragma mark -
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_jmpTo(ScriptState* script) {
|
|
|
|
script->ip = script->dataPtr->data + (_parameter << 1);
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_setRetValue(ScriptState* script) {
|
|
|
|
script->retValue = _parameter;
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_pushRetOrPos(ScriptState* script) {
|
2006-01-02 22:58:59 +00:00
|
|
|
switch (_parameter) {
|
|
|
|
case 0:
|
2006-01-08 16:15:34 +00:00
|
|
|
script->stack[--script->sp] = script->retValue;
|
2006-01-02 22:58:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2006-01-08 16:15:34 +00:00
|
|
|
script->stack[--script->sp] = (script->ip - script->dataPtr->data) / 2 + 1;
|
|
|
|
script->stack[--script->sp] = script->bp;
|
|
|
|
script->bp = script->sp + 2;
|
2006-01-02 22:58:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
_continue = false;
|
2006-01-08 16:15:34 +00:00
|
|
|
script->ip = 0;
|
2006-01-02 22:58:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_push(ScriptState* script) {
|
|
|
|
script->stack[--script->sp] = _parameter;
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_pushVar(ScriptState* script) {
|
|
|
|
script->stack[--script->sp] = script->variables[_parameter];
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_pushBPNeg(ScriptState* script) {
|
|
|
|
script->stack[--script->sp] = script->stack[(-(int32)(_parameter + 2)) + script->bp];
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_pushBPAdd(ScriptState* script) {
|
|
|
|
script->stack[--script->sp] = script->stack[(_parameter - 1) + script->bp];
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_popRetOrPos(ScriptState* script) {
|
2006-01-02 22:58:59 +00:00
|
|
|
switch (_parameter) {
|
|
|
|
case 0:
|
2006-01-08 16:15:34 +00:00
|
|
|
script->retValue = script->stack[script->sp++];
|
2006-01-02 22:58:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2006-01-08 16:15:34 +00:00
|
|
|
if (script->sp >= 60) {
|
2006-01-02 22:58:59 +00:00
|
|
|
_continue = false;
|
2006-01-08 16:15:34 +00:00
|
|
|
script->ip = 0;
|
2006-01-02 22:58:59 +00:00
|
|
|
} else {
|
2006-01-08 16:15:34 +00:00
|
|
|
script->bp = script->stack[script->sp++];
|
|
|
|
script->ip = script->dataPtr->data + (script->stack[script->sp++] << 1);
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
_continue = false;
|
2006-01-08 16:15:34 +00:00
|
|
|
script->ip = 0;
|
2006-01-02 22:58:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_popVar(ScriptState* script) {
|
|
|
|
script->variables[_parameter] = script->stack[script->sp++];
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_popBPNeg(ScriptState* script) {
|
|
|
|
script->stack[(-(int32)(_parameter + 2)) + script->bp] = script->stack[script->sp++];
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_popBPAdd(ScriptState* script) {
|
|
|
|
script->stack[(_parameter - 1) + script->bp] = script->stack[script->sp++];
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_addSP(ScriptState* script) {
|
|
|
|
script->sp += _parameter;
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_subSP(ScriptState* script) {
|
|
|
|
script->sp -= _parameter;
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_execOpcode(ScriptState* script) {
|
|
|
|
assert((uint8)_parameter < script->dataPtr->opcodeSize);
|
|
|
|
if (script->dataPtr->opcodes[(uint8)_parameter] == &KyraEngine::cmd_dummy)
|
2006-01-02 22:58:59 +00:00
|
|
|
debug("calling unimplemented opcode(0x%.02X)", (uint8)_parameter);
|
2006-01-08 16:15:34 +00:00
|
|
|
int val = (_vm->*script->dataPtr->opcodes[(uint8)_parameter])(script);
|
|
|
|
assert(script);
|
|
|
|
script->retValue = val;
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_ifNotJmp(ScriptState* script) {
|
|
|
|
if (!script->stack[script->sp++]) {
|
2006-01-02 22:58:59 +00:00
|
|
|
_parameter &= 0x7FFF;
|
2006-01-08 16:15:34 +00:00
|
|
|
script->ip = script->dataPtr->data + (_parameter << 1);
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_negate(ScriptState* script) {
|
|
|
|
int16 value = script->stack[script->sp];
|
2006-01-02 22:58:59 +00:00
|
|
|
switch (_parameter) {
|
|
|
|
case 0:
|
|
|
|
if (!value) {
|
2006-01-08 16:15:34 +00:00
|
|
|
script->stack[script->sp] = 1;
|
2006-01-02 22:58:59 +00:00
|
|
|
} else {
|
2006-01-08 16:15:34 +00:00
|
|
|
script->stack[script->sp] = 0;
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
2006-01-08 16:15:34 +00:00
|
|
|
script->stack[script->sp] = -value;
|
2006-01-02 22:58:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2006-01-08 16:15:34 +00:00
|
|
|
script->stack[script->sp] = ~value;
|
2006-01-02 22:58:59 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
_continue = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_eval(ScriptState* script) {
|
2006-01-02 22:58:59 +00:00
|
|
|
int16 ret = 0;
|
|
|
|
bool error = false;
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
int16 val1 = script->stack[script->sp++];
|
|
|
|
int16 val2 = script->stack[script->sp++];
|
2006-01-02 22:58:59 +00:00
|
|
|
|
|
|
|
switch (_parameter) {
|
|
|
|
case 0:
|
|
|
|
if (!val2 || !val1) {
|
|
|
|
ret = 0;
|
|
|
|
} else {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
if (val2 || val1) {
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if (val1 == val2) {
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
if (val1 != val2) {
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
if (val1 > val2) {
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
if (val1 >= val2) {
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
if (val1 < val2) {
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
if (val1 <= val2) {
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
ret = val1 + val2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 9:
|
|
|
|
ret = val2 - val1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 10:
|
|
|
|
ret = val1 * val2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 11:
|
|
|
|
ret = val2 / val1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 12:
|
|
|
|
ret = val2 >> val1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 13:
|
|
|
|
ret = val2 << val1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 14:
|
|
|
|
ret = val1 & val2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 15:
|
|
|
|
ret = val1 | val2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 16:
|
|
|
|
ret = val2 % val1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 17:
|
|
|
|
ret = val1 ^ val2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
warning("Unknown evaluate func: %d", _parameter);
|
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2006-01-08 16:15:34 +00:00
|
|
|
script->ip = 0;
|
2006-01-02 22:58:59 +00:00
|
|
|
_continue = false;
|
|
|
|
} else {
|
2006-01-08 16:15:34 +00:00
|
|
|
script->stack[--script->sp] = ret;
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-08 16:15:34 +00:00
|
|
|
void ScriptHelper::c1_setRetAndJmp(ScriptState* script) {
|
|
|
|
if (script->sp >= 60) {
|
2006-01-02 22:58:59 +00:00
|
|
|
_continue = false;
|
2006-01-08 16:15:34 +00:00
|
|
|
script->ip = 0;
|
2006-01-02 22:58:59 +00:00
|
|
|
} else {
|
2006-01-08 16:15:34 +00:00
|
|
|
script->retValue = script->stack[script->sp++];
|
|
|
|
uint16 temp = script->stack[script->sp++];
|
|
|
|
script->stack[60] = 0;
|
|
|
|
script->ip = &script->dataPtr->data[temp*2];
|
2006-01-02 22:58:59 +00:00
|
|
|
}
|
|
|
|
}
|
2004-10-15 06:06:47 +00:00
|
|
|
} // end of namespace Kyra
|