2008-04-20 14:43:56 +00:00
|
|
|
/* 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 "common/endian.h"
|
|
|
|
#include "common/util.h"
|
|
|
|
|
|
|
|
#include "made/made.h"
|
|
|
|
#include "made/script.h"
|
|
|
|
#include "made/database.h"
|
|
|
|
#include "made/scriptfuncs.h"
|
2008-10-04 21:40:14 +00:00
|
|
|
#include "made/screen.h"
|
2008-04-20 14:43:56 +00:00
|
|
|
|
|
|
|
namespace Made {
|
|
|
|
|
|
|
|
/* ScriptInterpreter */
|
|
|
|
|
|
|
|
ScriptInterpreter::ScriptInterpreter(MadeEngine *vm) : _vm(vm) {
|
2008-05-20 20:00:10 +00:00
|
|
|
#ifdef DUMP_SCRIPTS
|
|
|
|
#define COMMAND(x, sig) { &ScriptInterpreter::x, #x, sig }
|
|
|
|
#else
|
|
|
|
#define COMMAND(x, sig) { &ScriptInterpreter::x, #x}
|
|
|
|
#endif
|
2008-04-20 14:43:56 +00:00
|
|
|
static CommandEntry commandProcs[] = {
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 01 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_branchTrue, "W"),
|
|
|
|
COMMAND(cmd_branchFalse, "W"),
|
|
|
|
COMMAND(cmd_branch, "W"),
|
|
|
|
COMMAND(cmd_true, ""),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 05 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_false, ""),
|
|
|
|
COMMAND(cmd_push, ""),
|
|
|
|
COMMAND(cmd_not, ""),
|
|
|
|
COMMAND(cmd_add, ""),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 09 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_sub, ""),
|
|
|
|
COMMAND(cmd_mul, ""),
|
|
|
|
COMMAND(cmd_div, ""),
|
|
|
|
COMMAND(cmd_mod, ""),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 13 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_band, ""),
|
|
|
|
COMMAND(cmd_bor, ""),
|
|
|
|
COMMAND(cmd_bnot, ""),
|
|
|
|
COMMAND(cmd_lt, ""),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 17 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_eq, ""),
|
|
|
|
COMMAND(cmd_gt, ""),
|
|
|
|
COMMAND(cmd_loadConstant, "w"),
|
|
|
|
COMMAND(cmd_loadVariable, "w"),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 21 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_getObjectProperty, ""),
|
|
|
|
COMMAND(cmd_setObjectProperty, ""),
|
|
|
|
COMMAND(cmd_set, "w"),
|
|
|
|
COMMAND(cmd_print, ""),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 25 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_terpri, ""),
|
|
|
|
COMMAND(cmd_printNumber, ""),
|
|
|
|
COMMAND(cmd_vref, ""),
|
|
|
|
COMMAND(cmd_vset, ""),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 29 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_vsize, ""),
|
|
|
|
COMMAND(cmd_exit, ""),
|
|
|
|
COMMAND(cmd_return, ""),
|
|
|
|
COMMAND(cmd_call, "b"),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 33 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_svar, ""),
|
|
|
|
COMMAND(cmd_sset, ""),
|
|
|
|
COMMAND(cmd_split, ""),
|
|
|
|
COMMAND(cmd_snlit, ""),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 37 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_yorn, ""),
|
|
|
|
COMMAND(cmd_save, ""),
|
|
|
|
COMMAND(cmd_restore, ""),
|
|
|
|
COMMAND(cmd_arg, "b"),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 41 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_aset, "b"),
|
|
|
|
COMMAND(cmd_tmp, "b"),
|
|
|
|
COMMAND(cmd_tset, "b"),
|
|
|
|
COMMAND(cmd_tspace, "b"),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 45 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_class, ""),
|
|
|
|
COMMAND(cmd_objectp, ""),
|
|
|
|
COMMAND(cmd_vectorp, ""),
|
|
|
|
COMMAND(cmd_restart, ""),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 49 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_rand, ""),
|
|
|
|
COMMAND(cmd_randomize, ""),
|
|
|
|
COMMAND(cmd_send, "b"),
|
|
|
|
COMMAND(cmd_extend, "Eb"),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 53 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_catch, ""),
|
|
|
|
COMMAND(cmd_cdone, ""),
|
|
|
|
COMMAND(cmd_throw, ""),
|
|
|
|
COMMAND(cmd_functionp, ""),
|
2008-04-20 15:36:40 +00:00
|
|
|
/* 57 */
|
2008-05-20 20:00:10 +00:00
|
|
|
COMMAND(cmd_le, ""),
|
|
|
|
COMMAND(cmd_ge, ""),
|
|
|
|
COMMAND(cmd_varx, ""),
|
|
|
|
COMMAND(cmd_setx, "")
|
2008-04-20 14:43:56 +00:00
|
|
|
};
|
|
|
|
_commands = commandProcs;
|
|
|
|
_commandsMax = ARRAYSIZE(commandProcs) + 1;
|
2008-05-05 10:45:11 +00:00
|
|
|
|
2008-05-14 11:19:31 +00:00
|
|
|
_functions = new ScriptFunctions(_vm);
|
2008-04-20 14:43:56 +00:00
|
|
|
_functions->setupExternalsTable();
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2008-04-20 14:43:56 +00:00
|
|
|
#undef COMMAND
|
|
|
|
}
|
|
|
|
|
|
|
|
ScriptInterpreter::~ScriptInterpreter() {
|
|
|
|
delete _functions;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::runScript(int16 scriptObjectIndex) {
|
2008-10-04 21:40:14 +00:00
|
|
|
|
|
|
|
uint32 opcodeSleepCounter = 0;
|
|
|
|
|
2008-04-20 15:36:40 +00:00
|
|
|
_runningScriptObjectIndex = scriptObjectIndex;
|
2008-04-20 14:43:56 +00:00
|
|
|
|
2008-04-20 15:36:40 +00:00
|
|
|
_localStackPos = _stack.getStackPos();
|
2008-04-20 14:43:56 +00:00
|
|
|
|
2008-04-20 15:36:40 +00:00
|
|
|
_codeBase = _vm->_dat->getObject(_runningScriptObjectIndex)->getData();
|
2008-04-20 14:43:56 +00:00
|
|
|
_codeIp = _codeBase;
|
2008-10-04 21:40:14 +00:00
|
|
|
|
2008-11-07 21:59:25 +00:00
|
|
|
while (!_vm->shouldQuit()) {
|
2008-05-21 10:07:33 +00:00
|
|
|
|
|
|
|
_vm->handleEvents();
|
|
|
|
|
2008-04-20 15:36:40 +00:00
|
|
|
byte opcode = readByte();
|
2008-10-04 21:40:14 +00:00
|
|
|
|
2008-04-20 15:36:40 +00:00
|
|
|
if (opcode >= 1 && opcode <= _commandsMax) {
|
2008-05-18 10:57:58 +00:00
|
|
|
debug(4, "[%04X:%04X] %s", _runningScriptObjectIndex, (uint) (_codeIp - _codeBase), _commands[opcode - 1].desc);
|
2008-04-20 15:36:40 +00:00
|
|
|
(this->*_commands[opcode - 1].proc)();
|
2008-04-20 14:43:56 +00:00
|
|
|
} else {
|
2008-05-06 11:45:23 +00:00
|
|
|
warning("ScriptInterpreter::runScript(%d) Unknown opcode %02X", _runningScriptObjectIndex, opcode);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
2008-10-04 21:40:14 +00:00
|
|
|
|
|
|
|
/* We sleep a little after 500 opcodes to reduce the CPU load.
|
|
|
|
*/
|
|
|
|
if (++opcodeSleepCounter > 500) {
|
|
|
|
_vm->_screen->updateScreenAndWait(5);
|
|
|
|
opcodeSleepCounter = 0;
|
|
|
|
}
|
|
|
|
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
2008-05-17 13:35:57 +00:00
|
|
|
}
|
2008-04-20 14:43:56 +00:00
|
|
|
|
|
|
|
byte ScriptInterpreter::readByte() {
|
|
|
|
return *_codeIp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
int16 ScriptInterpreter::readInt16() {
|
|
|
|
int16 temp = (int16)READ_LE_UINT16(_codeIp);
|
|
|
|
_codeIp += 2;
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "readInt16() value = %04X", temp);
|
2008-04-20 14:43:56 +00:00
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_branchTrue() {
|
|
|
|
int16 ofs = readInt16();
|
|
|
|
if (_stack.top() != 0)
|
|
|
|
_codeIp = _codeBase + ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_branchFalse() {
|
|
|
|
int16 ofs = readInt16();
|
|
|
|
if (_stack.top() == 0)
|
|
|
|
_codeIp = _codeBase + ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_branch() {
|
|
|
|
int16 ofs = readInt16();
|
|
|
|
_codeIp = _codeBase + ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_true() {
|
|
|
|
_stack.setTop(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_false() {
|
|
|
|
_stack.setTop(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_push() {
|
|
|
|
_stack.push();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_not() {
|
|
|
|
if (_stack.top() == 0)
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.setTop(-1);
|
2008-04-20 14:43:56 +00:00
|
|
|
else
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_add() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
_stack.setTop(_stack.top() + value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_sub() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
_stack.setTop(_stack.top() - value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_mul() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
_stack.setTop(_stack.top() * value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_div() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
if (value == 0)
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
else
|
|
|
|
_stack.setTop(_stack.top() / value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_mod() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
if (value == 0)
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
else
|
|
|
|
_stack.setTop(_stack.top() % value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_band() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
_stack.setTop(_stack.top() & value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_bor() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
_stack.setTop(_stack.top() | value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_bnot() {
|
|
|
|
_stack.setTop(~_stack.top());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_lt() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
if (_stack.top() < value)
|
|
|
|
_stack.setTop(-1);
|
|
|
|
else
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_eq() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
if (_stack.top() == value)
|
|
|
|
_stack.setTop(-1);
|
|
|
|
else
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_gt() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
if (_stack.top() > value)
|
|
|
|
_stack.setTop(-1);
|
|
|
|
else
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_loadConstant() {
|
|
|
|
int16 value = readInt16();
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "value = %04X (%d)", value, value);
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.setTop(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_loadVariable() {
|
|
|
|
int16 variable = readInt16();
|
|
|
|
int16 value = _vm->_dat->getVar(variable);
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "variable = %d; value = %d (%04X)", variable, value, value);
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.setTop(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_getObjectProperty() {
|
|
|
|
int16 propertyId = _stack.pop();
|
|
|
|
int16 objectIndex = _stack.top();
|
|
|
|
int16 value = _vm->_dat->getObjectProperty(objectIndex, propertyId);
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "value = %04X(%d)", value, value);
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.setTop(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_setObjectProperty() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
int16 propertyId = _stack.pop();
|
|
|
|
int16 objectIndex = _stack.top();
|
|
|
|
value = _vm->_dat->setObjectProperty(objectIndex, propertyId, value);
|
|
|
|
_stack.setTop(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_set() {
|
|
|
|
int16 variable = readInt16();
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "var(%d) = %04d (%d)", variable, _stack.top(), _stack.top());
|
2008-04-20 14:43:56 +00:00
|
|
|
_vm->_dat->setVar(variable, _stack.top());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_print() {
|
|
|
|
// TODO: This opcode was used for printing debug messages
|
2008-06-12 11:01:08 +00:00
|
|
|
const char *text = _vm->_dat->getObjectString(_stack.top());
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "%s", text);
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.setTop(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_terpri() {
|
|
|
|
// TODO: This opcode was used for printing debug messages
|
|
|
|
debug(4, "\n");
|
|
|
|
_stack.setTop(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_printNumber() {
|
|
|
|
// TODO: This opcode was used for printing debug messages
|
|
|
|
debug(4, "%d", _stack.top());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_vref() {
|
2008-04-25 11:07:56 +00:00
|
|
|
int16 value = 0;
|
2008-04-20 14:43:56 +00:00
|
|
|
int16 index = _stack.pop();
|
|
|
|
int16 objectIndex = _stack.top();
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "index = %d; objectIndex = %d", index, objectIndex);
|
2008-04-20 14:43:56 +00:00
|
|
|
if (objectIndex > 0) {
|
2008-04-20 15:36:40 +00:00
|
|
|
Object *obj = _vm->_dat->getObject(objectIndex);
|
|
|
|
value = obj->getVectorItem(index);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
_stack.setTop(value);
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "--> value = %d", value);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_vset() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
int16 index = _stack.pop();
|
|
|
|
int16 objectIndex = _stack.top();
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "index = %d; objectIndex = %d; value = %d", index, objectIndex, value);
|
2008-04-20 14:43:56 +00:00
|
|
|
if (objectIndex > 0) {
|
2008-04-20 15:36:40 +00:00
|
|
|
Object *obj = _vm->_dat->getObject(objectIndex);
|
|
|
|
obj->setVectorItem(index, value);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
_stack.setTop(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_vsize() {
|
|
|
|
int16 objectIndex = _stack.top();
|
2008-04-25 11:07:56 +00:00
|
|
|
int16 size = 0;
|
|
|
|
if (objectIndex > 0) {
|
|
|
|
Object *obj = _vm->_dat->getObject(objectIndex);
|
|
|
|
size = obj->getVectorSize();
|
|
|
|
}
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.setTop(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_exit() {
|
2008-11-07 21:59:25 +00:00
|
|
|
_vm->quitGame();
|
2009-01-17 04:50:13 +00:00
|
|
|
// Make sure the "quit" event is handled immediately
|
|
|
|
_vm->handleEvents();
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_return() {
|
2008-05-07 17:28:38 +00:00
|
|
|
|
|
|
|
// Check if returning from main function
|
|
|
|
if (_localStackPos == kScriptStackSize) {
|
2008-11-07 21:59:25 +00:00
|
|
|
_vm->quitGame();
|
2009-01-17 04:50:13 +00:00
|
|
|
// Make sure the "quit" event is handled immediately
|
|
|
|
_vm->handleEvents();
|
2008-05-07 17:28:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-20 14:43:56 +00:00
|
|
|
int16 funcResult = _stack.top();
|
|
|
|
_stack.setStackPos(_localStackPos);
|
|
|
|
_localStackPos = kScriptStackLimit - _stack.pop();
|
|
|
|
_runningScriptObjectIndex = _stack.pop();
|
2008-04-20 15:36:40 +00:00
|
|
|
_codeBase = _vm->_dat->getObject(_runningScriptObjectIndex)->getData();
|
2008-04-20 14:43:56 +00:00
|
|
|
_codeIp = _codeBase + _stack.pop();
|
|
|
|
byte argc = _stack.pop();
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.free(argc);
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.setTop(funcResult);
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "LEAVE: stackPtr = %d; _localStackPos = %d\n", _stack.getStackPos(), _localStackPos);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_call() {
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "\nENTER: stackPtr = %d; _localStackPos = %d", _stack.getStackPos(), _localStackPos);
|
2008-04-20 14:43:56 +00:00
|
|
|
byte argc = readByte();
|
2008-05-18 10:57:58 +00:00
|
|
|
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.push(argc);
|
|
|
|
_stack.push(_codeIp - _codeBase);
|
|
|
|
_stack.push(_runningScriptObjectIndex);
|
|
|
|
_stack.push(kScriptStackLimit - _localStackPos);
|
|
|
|
_localStackPos = _stack.getStackPos();
|
2008-04-20 15:36:40 +00:00
|
|
|
_runningScriptObjectIndex = _stack.peek(_localStackPos + argc + 4);
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "argc = %d; _runningScriptObjectIndex = %04X", argc, _runningScriptObjectIndex);
|
2008-04-20 15:36:40 +00:00
|
|
|
_codeBase = _vm->_dat->getObject(_runningScriptObjectIndex)->getData();
|
2008-04-20 14:43:56 +00:00
|
|
|
_codeIp = _codeBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_svar() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_svar");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_sset() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_sset");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_split() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_split");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_snlit() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_snlit");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_yorn() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_yorn");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_save() {
|
2008-05-13 08:21:28 +00:00
|
|
|
int16 result = 0;
|
|
|
|
int16 stringOfs = _stack.top();
|
|
|
|
const char *filename = _vm->_dat->getString(stringOfs);
|
|
|
|
result = _vm->_dat->savegame(filename, "", 0);
|
|
|
|
_stack.setTop(result);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_restore() {
|
2008-05-13 08:21:28 +00:00
|
|
|
int16 result = 0;
|
|
|
|
int16 stringOfs = _stack.top();
|
|
|
|
const char *filename = _vm->_dat->getString(stringOfs);
|
|
|
|
result = _vm->_dat->loadgame(filename, 0);
|
|
|
|
_stack.setTop(result);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_arg() {
|
|
|
|
int16 argIndex = readByte();
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "argIndex = %d; value = %04X (%d)", argIndex, _stack.peek(_localStackPos + 4 + argIndex), _stack.peek(_localStackPos + 4 + argIndex));
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.setTop(_stack.peek(_localStackPos + 4 + argIndex));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_aset() {
|
|
|
|
int16 argIndex = readByte();
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "argIndex = %d; value = %d", argIndex, _stack.peek(_localStackPos + 4 + argIndex));
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.poke(_localStackPos + 4 + argIndex, _stack.top());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_tmp() {
|
|
|
|
int16 tempIndex = readByte();
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "tempIndex = %d; value = %d", tempIndex, _stack.peek(_localStackPos - tempIndex - 1));
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.setTop(_stack.peek(_localStackPos - tempIndex - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_tset() {
|
|
|
|
int16 tempIndex = readByte();
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "tempIndex = %d; value = %d", tempIndex, _stack.top());
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.poke(_localStackPos - tempIndex - 1, _stack.top());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_tspace() {
|
|
|
|
int16 tempCount = readByte();
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "tempCount = %d", tempCount);
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.alloc(tempCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_class() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_class");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_objectp() {
|
2008-05-06 11:45:23 +00:00
|
|
|
Object *obj = _vm->_dat->getObject(_stack.top());
|
|
|
|
if (obj->isObject())
|
|
|
|
_stack.setTop(-1);
|
|
|
|
else
|
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_vectorp() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_vectorp");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_restart() {
|
2008-12-15 09:01:43 +00:00
|
|
|
_vm->_dat->reload();
|
|
|
|
_vm->_screen->clearChannels();
|
|
|
|
_vm->resetAllTimers();
|
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_rand() {
|
2008-04-25 11:07:56 +00:00
|
|
|
_stack.setTop(_vm->_rnd->getRandomNumber(_stack.top() - 1));
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_randomize() {
|
2008-04-25 11:07:56 +00:00
|
|
|
_vm->_rnd->setSeed(g_system->getMillis());
|
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_send() {
|
|
|
|
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "\nENTER: stackPtr = %d; _localStackPos = %d", _stack.getStackPos(), _localStackPos);
|
2008-04-20 14:43:56 +00:00
|
|
|
|
|
|
|
byte argc = readByte();
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "argc = %d", argc);
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.push(argc);
|
|
|
|
_stack.push(_codeIp - _codeBase);
|
|
|
|
_stack.push(_runningScriptObjectIndex);
|
|
|
|
_stack.push(kScriptStackLimit - _localStackPos);
|
2009-03-07 19:24:31 +00:00
|
|
|
_localStackPos = _stack.getStackPos();
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2009-03-07 19:24:31 +00:00
|
|
|
int16 propertyId = _stack.peek(_localStackPos + argc + 2);
|
2008-04-20 14:43:56 +00:00
|
|
|
int16 objectIndex = _stack.peek(_localStackPos + argc + 4);
|
|
|
|
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "objectIndex = %d (%04X); propertyId = %d(%04X)", objectIndex, objectIndex, propertyId, propertyId);
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2008-04-20 14:43:56 +00:00
|
|
|
if (objectIndex != 0) {
|
2008-04-20 15:36:40 +00:00
|
|
|
objectIndex = _vm->_dat->getObject(objectIndex)->getClass();
|
2008-04-20 14:43:56 +00:00
|
|
|
} else {
|
|
|
|
objectIndex = _stack.peek(_localStackPos + argc + 3);
|
|
|
|
}
|
|
|
|
|
2008-05-06 11:45:23 +00:00
|
|
|
debug(4, "--> objectIndex = %d(%04X)", objectIndex, objectIndex);
|
2008-04-20 14:43:56 +00:00
|
|
|
|
|
|
|
if (objectIndex != 0) {
|
2008-04-20 15:36:40 +00:00
|
|
|
_runningScriptObjectIndex = _vm->_dat->getObjectProperty(objectIndex, propertyId);
|
|
|
|
if (_runningScriptObjectIndex != 0) {
|
|
|
|
_codeBase = _vm->_dat->getObject(_runningScriptObjectIndex)->getData();
|
2008-04-20 14:43:56 +00:00
|
|
|
_codeIp = _codeBase;
|
|
|
|
} else {
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.push(0);
|
|
|
|
cmd_return();
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.push(0);
|
|
|
|
cmd_return();
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_extend() {
|
|
|
|
|
|
|
|
byte func = readByte();
|
|
|
|
|
|
|
|
byte argc = readByte();
|
|
|
|
int16 *argv = _stack.getStackPtr();
|
|
|
|
|
2008-05-18 10:57:58 +00:00
|
|
|
debug(4, "func = %d (%s); argc = %d", func, _functions->getFuncName(func), argc);
|
2008-04-20 14:43:56 +00:00
|
|
|
for (int i = 0; i < argc; i++)
|
2008-05-13 08:21:28 +00:00
|
|
|
debug(2, "argv[%02d] = %04X (%d)", i, argv[i], argv[i]);
|
2008-04-20 14:43:56 +00:00
|
|
|
|
|
|
|
int16 result = _functions->callFunction(func, argc, argv);
|
2008-05-13 08:21:28 +00:00
|
|
|
debug(2, "result = %04X (%d)", result, result);
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.free(argc);
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2008-04-20 14:43:56 +00:00
|
|
|
_stack.setTop(result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_catch() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_catch");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_cdone() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_cdone");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_throw() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_throw");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_functionp() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_functionp");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_le() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
if (_stack.top() <= value)
|
|
|
|
_stack.setTop(-1);
|
|
|
|
else
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_ge() {
|
|
|
|
int16 value = _stack.pop();
|
|
|
|
if (_stack.top() >= value)
|
|
|
|
_stack.setTop(-1);
|
|
|
|
else
|
2008-04-20 15:36:40 +00:00
|
|
|
_stack.setTop(0);
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_varx() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_varx");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::cmd_setx() {
|
2009-01-25 10:46:48 +00:00
|
|
|
// Never used in LGOP2, RTZ, Manhole:NE, Rodney
|
2008-04-24 14:39:01 +00:00
|
|
|
warning("Unimplemented command: cmd_setx");
|
2008-04-20 14:43:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-20 20:00:10 +00:00
|
|
|
#ifdef DUMP_SCRIPTS
|
|
|
|
void ScriptInterpreter::dumpScript(int16 objectIndex, int *opcodeStats, int *externStats) {
|
|
|
|
|
|
|
|
debug(1, "Dumping code for object %04X", objectIndex);
|
|
|
|
|
|
|
|
Object *obj = _vm->_dat->getObject(objectIndex);
|
2008-06-12 11:01:08 +00:00
|
|
|
byte *code = obj->getData(), *codeStart = code, *codeEnd = code + obj->getSize();
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2008-05-20 20:00:10 +00:00
|
|
|
while (code < codeEnd) {
|
|
|
|
byte opcode = *code++;
|
|
|
|
if (opcode >= 1 && opcode <= _commandsMax) {
|
|
|
|
Common::String codeLine;
|
|
|
|
const char *desc = _commands[opcode - 1].desc;
|
|
|
|
const char *sig = _commands[opcode - 1].sig;
|
|
|
|
int valueType; /* 0: dec; 1: hex; 2: extended function */
|
|
|
|
int16 value;
|
|
|
|
char tempStr[32];
|
2008-05-21 07:16:12 +00:00
|
|
|
opcodeStats[opcode - 1]++;
|
2008-06-12 11:01:08 +00:00
|
|
|
snprintf(tempStr, 32, "[%04X] ", (uint16)(code - codeStart - 1));
|
|
|
|
codeLine += tempStr;
|
2008-05-20 20:00:10 +00:00
|
|
|
codeLine += desc;
|
|
|
|
for (; *sig != '\0'; sig++) {
|
|
|
|
codeLine += " ";
|
|
|
|
switch (*sig) {
|
|
|
|
case 'b':
|
|
|
|
valueType = 0;
|
2009-03-07 19:24:31 +00:00
|
|
|
value = *code++;
|
2008-05-20 20:00:10 +00:00
|
|
|
break;
|
|
|
|
case 'B':
|
2009-03-07 19:24:31 +00:00
|
|
|
valueType = 1;
|
|
|
|
value = *code++;
|
2008-05-20 20:00:10 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
valueType = 0;
|
2009-03-07 19:24:31 +00:00
|
|
|
value = READ_LE_UINT16(code);
|
|
|
|
code += 2;
|
2008-05-20 20:00:10 +00:00
|
|
|
break;
|
|
|
|
case 'W':
|
2009-03-07 19:24:31 +00:00
|
|
|
valueType = 1;
|
|
|
|
value = READ_LE_UINT16(code);
|
|
|
|
code += 2;
|
2008-05-20 20:00:10 +00:00
|
|
|
break;
|
|
|
|
case 'E':
|
2009-03-07 19:24:31 +00:00
|
|
|
valueType = 2;
|
|
|
|
value = *code++;
|
2008-05-20 20:00:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
switch (valueType) {
|
|
|
|
case 0:
|
|
|
|
snprintf(tempStr, 32, "%d", value);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
snprintf(tempStr, 32, "0x%X", value);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (value < _functions->getCount()) {
|
|
|
|
snprintf(tempStr, 32, "%s", _functions->getFuncName(value));
|
|
|
|
externStats[value]++;
|
|
|
|
} else {
|
|
|
|
snprintf(tempStr, 32, "invalid: %d", value);
|
2009-03-07 19:24:31 +00:00
|
|
|
}
|
2008-05-20 20:00:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
codeLine += tempStr;
|
|
|
|
}
|
|
|
|
debug(1, "%s", codeLine.c_str());
|
|
|
|
} else {
|
|
|
|
error("ScriptInterpreter::dumpScript(%d) Unknown opcode %02X", objectIndex, opcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug(1, "-------------------------------------------");
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptInterpreter::dumpAllScripts() {
|
|
|
|
int *opcodeStats = new int[_commandsMax - 1];
|
|
|
|
int *externStats = new int[_functions->getCount()];
|
|
|
|
|
|
|
|
for (int i = 0; i < _commandsMax; i++)
|
|
|
|
opcodeStats[i] = 0;
|
|
|
|
for (int i = 0; i < _functions->getCount(); i++)
|
|
|
|
externStats[i] = 0;
|
|
|
|
|
|
|
|
for (uint objectIndex = 1; objectIndex <= _vm->_dat->getObjectCount(); objectIndex++) {
|
|
|
|
Object *obj = _vm->_dat->getObject(objectIndex);
|
|
|
|
// Check if it's a byte array which might contain code
|
|
|
|
if (obj->getClass() != 0x7FFF)
|
|
|
|
continue;
|
|
|
|
// Code objects aren't excplicitly marked as such, we need to check if
|
|
|
|
// the last byte is a cmd_return opcode.
|
|
|
|
byte *retByte = obj->getData() + obj->getSize() - 1;
|
|
|
|
if (*retByte == 0x1F) {
|
|
|
|
dumpScript(objectIndex, opcodeStats, externStats);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(1, "OPCODE statistics:");
|
|
|
|
for (int i = 0; i < _commandsMax - 1; i++)
|
|
|
|
if (opcodeStats[i] > 0)
|
|
|
|
debug(1, "%-30s: %d", _commands[i].desc, opcodeStats[i]);
|
|
|
|
debug(1, "UNUSED OPCODE statistics:");
|
|
|
|
for (int i = 0; i < _commandsMax - 1; i++)
|
|
|
|
if (opcodeStats[i] == 0)
|
|
|
|
debug(1, "%-30s: %d", _commands[i].desc, opcodeStats[i]);
|
|
|
|
debug(1, ".");
|
|
|
|
|
|
|
|
debug(1, "EXTERN statistics (%d):", _functions->getCount());
|
|
|
|
for (int i = 0; i < _functions->getCount(); i++)
|
|
|
|
if (externStats[i] > 0)
|
|
|
|
debug(1, "%-30s: %d", _functions->getFuncName(i), externStats[i]);
|
|
|
|
debug(1, "UNUSED EXTERN statistics (%d):", _functions->getCount());
|
|
|
|
for (int i = 0; i < _functions->getCount(); i++)
|
|
|
|
if (externStats[i] == 0)
|
|
|
|
debug(1, "%-30s: %d", _functions->getFuncName(i), externStats[i]);
|
|
|
|
debug(1, ".");
|
|
|
|
|
|
|
|
delete[] opcodeStats;
|
|
|
|
delete[] externStats;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-04-20 14:43:56 +00:00
|
|
|
} // End of namespace Made
|