2009-06-08 22:18:52 +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/debug.h"
|
|
|
|
#include "common/stream.h"
|
2009-06-10 02:59:53 +00:00
|
|
|
#include "common/stack.h"
|
2009-07-04 14:48:36 +00:00
|
|
|
#include "common/queue.h"
|
2009-06-08 22:18:52 +00:00
|
|
|
|
|
|
|
#include "draci/draci.h"
|
2009-07-04 14:48:36 +00:00
|
|
|
#include "draci/script.h"
|
|
|
|
#include "draci/game.h"
|
2009-06-08 22:18:52 +00:00
|
|
|
|
|
|
|
namespace Draci {
|
|
|
|
|
2009-06-10 01:35:47 +00:00
|
|
|
// FIXME: Change parameter types to names once I figure out what they are exactly
|
2009-06-12 09:45:12 +00:00
|
|
|
|
2009-07-04 14:48:36 +00:00
|
|
|
void Script::setupCommandList() {
|
|
|
|
/** A table of all the commands the game player uses */
|
|
|
|
static const GPL2Command gplCommands[] = {
|
|
|
|
{ 0, 0, "gplend", 0, { 0 }, NULL },
|
|
|
|
{ 0, 1, "exit", 0, { 0 }, NULL },
|
2009-07-08 00:34:53 +00:00
|
|
|
{ 1, 1, "goto", 1, { 3 }, &Script::c_Goto },
|
|
|
|
{ 2, 1, "Let", 2, { 3, 4 }, &Script::c_Let },
|
|
|
|
{ 3, 1, "if", 2, { 4, 3 }, &Script::c_If },
|
2009-07-06 17:27:12 +00:00
|
|
|
{ 4, 1, "Start", 2, { 3, 2 }, &Script::start },
|
2009-07-04 18:36:45 +00:00
|
|
|
{ 5, 1, "Load", 2, { 3, 2 }, &Script::load },
|
2009-07-24 05:07:27 +00:00
|
|
|
{ 5, 2, "StartPlay", 2, { 3, 2 }, &Script::start },
|
2009-07-04 14:48:36 +00:00
|
|
|
{ 5, 3, "JustTalk", 0, { 0 }, NULL },
|
|
|
|
{ 5, 4, "JustStay", 0, { 0 }, NULL },
|
|
|
|
{ 6, 1, "Talk", 2, { 3, 2 }, NULL },
|
2009-07-25 02:23:00 +00:00
|
|
|
{ 7, 1, "ObjStat", 2, { 3, 3 }, &Script::objStat },
|
|
|
|
{ 7, 2, "ObjStat_On", 2, { 3, 3 }, &Script::objStatOn },
|
2009-07-04 14:48:36 +00:00
|
|
|
{ 8, 1, "IcoStat", 2, { 3, 3 }, NULL },
|
|
|
|
{ 9, 1, "Dialogue", 1, { 2 }, NULL },
|
|
|
|
{ 9, 2, "ExitDialogue", 0, { 0 }, NULL },
|
|
|
|
{ 9, 3, "ResetDialogue", 0, { 0 }, NULL },
|
|
|
|
{ 9, 4, "ResetDialogueFrom", 0, { 0 }, NULL },
|
|
|
|
{ 9, 5, "ResetBlock", 1, { 3 }, NULL },
|
2009-07-25 04:36:43 +00:00
|
|
|
{ 10, 1, "WalkOn", 3, { 1, 1, 3 }, &Script::walkOn },
|
|
|
|
{ 10, 2, "StayOn", 3, { 1, 1, 3 }, &Script::walkOn }, // HACK: not a proper implementation
|
|
|
|
{ 10, 3, "WalkOnPlay", 3, { 1, 1, 3 }, &Script::walkOn }, // HACK: not a proper implementation
|
2009-07-04 14:48:36 +00:00
|
|
|
{ 11, 1, "LoadPalette", 1, { 2 }, NULL },
|
|
|
|
{ 12, 1, "SetPalette", 0, { 0 }, NULL },
|
|
|
|
{ 12, 2, "BlackPalette", 0, { 0 }, NULL },
|
|
|
|
{ 13, 1, "FadePalette", 3, { 1, 1, 1 }, NULL },
|
|
|
|
{ 13, 2, "FadePalettePlay", 3, { 1, 1, 1 }, NULL },
|
|
|
|
{ 14, 1, "NewRoom", 2, { 3, 1 }, NULL },
|
2009-07-25 03:37:22 +00:00
|
|
|
{ 15, 1, "ExecInit", 1, { 3 }, &Script::execInit },
|
|
|
|
{ 15, 2, "ExecLook", 1, { 3 }, &Script::execLook },
|
|
|
|
{ 15, 3, "ExecUse", 1, { 3 }, &Script::execUse },
|
2009-07-04 14:48:36 +00:00
|
|
|
{ 16, 1, "RepaintInventory", 0, { 0 }, NULL },
|
|
|
|
{ 16, 2, "ExitInventory", 0, { 0 }, NULL },
|
|
|
|
{ 17, 1, "ExitMap", 0, { 0 }, NULL },
|
|
|
|
{ 18, 1, "LoadMusic", 1, { 2 }, NULL },
|
|
|
|
{ 18, 2, "StartMusic", 0, { 0 }, NULL },
|
|
|
|
{ 18, 3, "StopMusic", 0, { 0 }, NULL },
|
|
|
|
{ 18, 4, "FadeOutMusic", 1, { 1 }, NULL },
|
|
|
|
{ 18, 5, "FadeInMusic", 1, { 1 }, NULL },
|
2009-07-24 05:00:53 +00:00
|
|
|
{ 19, 1, "Mark", 0, { 0 }, &Script::mark },
|
|
|
|
{ 19, 2, "Release", 0, { 0 }, &Script::release },
|
2009-07-29 01:02:50 +00:00
|
|
|
{ 20, 1, "Play", 0, { 0 }, &Script::play },
|
2009-07-04 14:48:36 +00:00
|
|
|
{ 21, 1, "LoadMap", 1, { 2 }, NULL },
|
|
|
|
{ 21, 2, "RoomMap", 0, { 0 }, NULL },
|
|
|
|
{ 22, 1, "DisableQuickHero", 0, { 0 }, NULL },
|
|
|
|
{ 22, 2, "EnableQuickHero", 0, { 0 }, NULL },
|
|
|
|
{ 23, 1, "DisableSpeedText", 0, { 0 }, NULL },
|
|
|
|
{ 23, 2, "EnableSpeedText", 0, { 0 }, NULL },
|
|
|
|
{ 24, 1, "QuitGame", 0, { 0 }, NULL },
|
|
|
|
{ 25, 1, "PushNewRoom", 0, { 0 }, NULL },
|
|
|
|
{ 25, 2, "PopNewRoom", 0, { 0 }, NULL },
|
|
|
|
{ 26, 1, "ShowCheat", 0, { 0 }, NULL },
|
|
|
|
{ 26, 2, "HideCheat", 0, { 0 }, NULL },
|
|
|
|
{ 26, 3, "ClearCheat", 1, { 1 }, NULL },
|
|
|
|
{ 27, 1, "FeedPassword", 3, { 1, 1, 1 }, NULL }
|
|
|
|
};
|
|
|
|
|
2009-07-06 17:25:34 +00:00
|
|
|
/** Operators used by the mathematical evaluator */
|
|
|
|
static const GPL2Operator gplOperators[] = {
|
|
|
|
{"&", &Script::operAnd },
|
|
|
|
{"|", &Script::operOr },
|
|
|
|
{"^", &Script::operXor },
|
|
|
|
{"==", &Script::operEqual },
|
|
|
|
{"!=", &Script::operNotEqual },
|
|
|
|
{"<", &Script::operLess },
|
|
|
|
{">", &Script::operGreater },
|
|
|
|
{"<=", &Script::operLessOrEqual },
|
|
|
|
{">=", &Script::operGreaterOrEqual },
|
|
|
|
{"*", &Script::operMul },
|
|
|
|
{"/", &Script::operDiv },
|
|
|
|
{"%", &Script::operMod },
|
|
|
|
{"+", &Script::operAdd },
|
|
|
|
{"-", &Script::operSub }
|
|
|
|
};
|
|
|
|
|
2009-07-06 18:49:51 +00:00
|
|
|
/** Functions used by the mathematical evaluator */
|
|
|
|
static const GPL2Function gplFunctions[] = {
|
2009-07-07 21:24:39 +00:00
|
|
|
{ "Not", &Script::funcNot },
|
2009-07-06 19:26:53 +00:00
|
|
|
{ "Random", &Script::funcRandom },
|
2009-07-13 19:53:53 +00:00
|
|
|
{ "IsIcoOn", &Script::funcIsIcoOn },
|
2009-07-06 18:49:51 +00:00
|
|
|
{ "IsIcoAct", NULL },
|
2009-07-13 19:53:53 +00:00
|
|
|
{ "IcoStat", &Script::funcIcoStat },
|
2009-07-06 18:49:51 +00:00
|
|
|
{ "ActIco", NULL },
|
2009-07-13 19:53:53 +00:00
|
|
|
{ "IsObjOn", &Script::funcIsObjOn },
|
|
|
|
{ "IsObjOff", &Script::funcIsObjOff },
|
|
|
|
{ "IsObjAway", &Script::funcIsObjAway },
|
2009-07-06 18:49:51 +00:00
|
|
|
{ "ObjStat", NULL },
|
|
|
|
{ "LastBlock", NULL },
|
|
|
|
{ "AtBegin", NULL },
|
|
|
|
{ "BlockVar", NULL },
|
|
|
|
{ "HasBeen", NULL },
|
|
|
|
{ "MaxLine", NULL },
|
2009-07-29 01:02:50 +00:00
|
|
|
{ "ActPhase", &Script::funcActPhase },
|
2009-07-06 18:49:51 +00:00
|
|
|
{ "Cheat", NULL },
|
|
|
|
};
|
|
|
|
|
2009-07-04 14:48:36 +00:00
|
|
|
_commandList = gplCommands;
|
2009-07-06 17:25:34 +00:00
|
|
|
_operatorList = gplOperators;
|
2009-07-06 18:49:51 +00:00
|
|
|
_functionList = gplFunctions;
|
2009-07-04 14:48:36 +00:00
|
|
|
}
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-06-12 09:45:12 +00:00
|
|
|
/** Type of mathematical object */
|
2009-06-10 02:59:53 +00:00
|
|
|
enum mathExpressionObject {
|
|
|
|
kMathEnd,
|
|
|
|
kMathNumber,
|
|
|
|
kMathOperator,
|
|
|
|
kMathFunctionCall,
|
|
|
|
kMathVariable
|
|
|
|
};
|
|
|
|
|
2009-07-06 17:25:34 +00:00
|
|
|
/* GPL operators */
|
|
|
|
|
|
|
|
int Script::operAnd(int op1, int op2) {
|
|
|
|
return op1 & op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operOr(int op1, int op2) {
|
|
|
|
return op1 | op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operXor(int op1, int op2) {
|
|
|
|
return op1 ^ op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operEqual(int op1, int op2) {
|
|
|
|
return op1 == op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operNotEqual(int op1, int op2) {
|
|
|
|
return op1 != op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operLess(int op1, int op2) {
|
|
|
|
return op1 < op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operGreater(int op1, int op2) {
|
|
|
|
return op1 > op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operGreaterOrEqual(int op1, int op2) {
|
|
|
|
return op1 >= op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operLessOrEqual(int op1, int op2) {
|
|
|
|
return op1 <= op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operMul(int op1, int op2) {
|
|
|
|
return op1 * op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operAdd(int op1, int op2) {
|
|
|
|
return op1 + op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operSub(int op1, int op2) {
|
|
|
|
return op1 - op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operDiv(int op1, int op2) {
|
|
|
|
return op1 / op2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::operMod(int op1, int op2) {
|
|
|
|
return op1 % op2;
|
|
|
|
}
|
|
|
|
|
2009-07-06 19:26:53 +00:00
|
|
|
/* GPL functions */
|
|
|
|
|
|
|
|
int Script::funcRandom(int n) {
|
|
|
|
|
|
|
|
// The function needs to return numbers in the [0..n-1] range so we need to deduce 1
|
|
|
|
// (RandomSource::getRandomNumber returns a number in the range [0..n])
|
|
|
|
|
|
|
|
n -= 1;
|
|
|
|
return _vm->_rnd.getRandomNumber(n);
|
|
|
|
}
|
|
|
|
|
2009-07-07 21:24:39 +00:00
|
|
|
int Script::funcNot(int n) {
|
|
|
|
return !n;
|
|
|
|
}
|
|
|
|
|
2009-07-13 19:53:53 +00:00
|
|
|
int Script::funcIsIcoOn(int iconID) {
|
|
|
|
iconID -= 1;
|
|
|
|
|
|
|
|
return _vm->_game->getIconStatus(iconID) == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::funcIcoStat(int iconID) {
|
|
|
|
iconID -= 1;
|
|
|
|
|
|
|
|
int status = _vm->_game->getIconStatus(iconID);
|
|
|
|
return (status == 1) ? 1 : 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::funcIsObjOn(int objID) {
|
|
|
|
objID -= 1;
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
|
|
|
|
|
|
|
return obj->_visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::funcIsObjOff(int objID) {
|
|
|
|
objID -= 1;
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
|
|
|
|
|
|
|
// We index locations from 0 (as opposed to the original player where it was from 1)
|
2009-07-25 02:23:00 +00:00
|
|
|
// That's why the "away" location 0 from the data files is converted to -1
|
2009-07-13 19:53:53 +00:00
|
|
|
return !obj->_visible && obj->_location != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Script::funcIsObjAway(int objID) {
|
|
|
|
objID -= 1;
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
|
|
|
|
|
|
|
// see Script::funcIsObjOff
|
|
|
|
return !obj->_visible && obj->_location == -1;
|
|
|
|
}
|
|
|
|
|
2009-07-29 01:02:50 +00:00
|
|
|
int Script::funcActPhase(int objID) {
|
|
|
|
|
|
|
|
objID -= 1;
|
|
|
|
|
|
|
|
// Default return value
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (_vm->_game->getLoopStatus() == kStatusInventory) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
|
|
|
|
|
|
|
bool visible = (objID == kDragonObject || obj->_visible);
|
|
|
|
|
|
|
|
if (visible && (obj->_location == _vm->_game->getRoomNum())) {
|
|
|
|
int animID = obj->_anims[0];
|
|
|
|
Animation *anim = _vm->_anims->getAnimation(animID);
|
|
|
|
ret = anim->currentFrameNum();
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-07-06 17:25:34 +00:00
|
|
|
/* GPL commands */
|
|
|
|
|
2009-07-29 01:02:50 +00:00
|
|
|
void Script::play(Common::Queue<int> ¶ms) {
|
|
|
|
_vm->_game->loop();
|
|
|
|
}
|
|
|
|
|
2009-07-04 18:36:45 +00:00
|
|
|
void Script::load(Common::Queue<int> ¶ms) {
|
2009-07-25 03:28:04 +00:00
|
|
|
if (_vm->_game->getLoopStatus() == kStatusInventory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-04 18:36:45 +00:00
|
|
|
int objID = params.pop() - 1;
|
|
|
|
int animID = params.pop() - 1;
|
2009-07-29 01:02:50 +00:00
|
|
|
|
|
|
|
uint i;
|
2009-07-04 18:36:45 +00:00
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
2009-07-04 14:48:36 +00:00
|
|
|
|
2009-07-29 01:02:50 +00:00
|
|
|
// If the animation is already loaded, return
|
|
|
|
for(i = 0; i < obj->_anims.size(); ++i) {
|
|
|
|
if (obj->_anims[i] == animID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the animation into memory
|
|
|
|
|
2009-07-17 00:20:57 +00:00
|
|
|
_vm->_game->loadAnimation(animID, obj->_z);
|
2009-07-29 01:02:50 +00:00
|
|
|
|
|
|
|
// We insert the ID of the loaded animation into the object's internal array
|
|
|
|
// of owned animation IDs.
|
|
|
|
// Care must be taken to store them sorted (increasing order) as some things
|
|
|
|
// depend on this.
|
|
|
|
|
|
|
|
for(i = 0; i < obj->_anims.size(); ++i) {
|
|
|
|
if (obj->_anims[i] > animID) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj->_anims.insert_at(i, animID);
|
2009-07-04 18:36:45 +00:00
|
|
|
}
|
2009-06-12 09:45:12 +00:00
|
|
|
|
2009-07-06 17:27:12 +00:00
|
|
|
void Script::start(Common::Queue<int> ¶ms) {
|
2009-07-25 03:28:04 +00:00
|
|
|
if (_vm->_game->getLoopStatus() == kStatusInventory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-06 17:27:12 +00:00
|
|
|
int objID = params.pop() - 1;
|
|
|
|
int animID = params.pop() - 1;
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
2009-07-29 01:02:50 +00:00
|
|
|
|
2009-07-07 19:50:12 +00:00
|
|
|
bool visible = (objID == kDragonObject || obj->_visible);
|
2009-07-06 17:27:12 +00:00
|
|
|
|
2009-07-29 01:02:50 +00:00
|
|
|
if (visible && (obj->_location == _vm->_game->getRoomNum())) {
|
2009-07-06 18:49:51 +00:00
|
|
|
_vm->_anims->play(animID);
|
2009-07-29 01:02:50 +00:00
|
|
|
}
|
2009-07-06 17:27:12 +00:00
|
|
|
}
|
|
|
|
|
2009-07-08 00:34:53 +00:00
|
|
|
void Script::c_If(Common::Queue<int> ¶ms) {
|
|
|
|
int expression = params.pop();
|
|
|
|
int jump = params.pop();
|
|
|
|
|
|
|
|
if (expression)
|
|
|
|
_jump = jump;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::c_Goto(Common::Queue<int> ¶ms) {
|
|
|
|
int jump = params.pop();
|
|
|
|
|
|
|
|
_jump = jump;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::c_Let(Common::Queue<int> ¶ms) {
|
|
|
|
int var = params.pop() - 1;
|
|
|
|
int value = params.pop();
|
|
|
|
|
|
|
|
_vm->_game->setVariable(var, value);
|
|
|
|
}
|
|
|
|
|
2009-07-24 05:00:53 +00:00
|
|
|
void Script::mark(Common::Queue<int> ¶ms) {
|
|
|
|
_vm->_game->setMarkedAnimationIndex(_vm->_anims->getLastIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::release(Common::Queue<int> ¶ms) {
|
|
|
|
int markedIndex = _vm->_game->getMarkedAnimationIndex();
|
|
|
|
|
|
|
|
// Also delete those animations from the game's objects
|
|
|
|
for (uint i = 0; i < _vm->_game->getNumObjects(); ++i) {
|
|
|
|
GameObject *obj = _vm->_game->getObject(i);
|
|
|
|
|
|
|
|
for (uint j = 0; j < obj->_anims.size(); ++j) {
|
|
|
|
Animation *anim;
|
|
|
|
|
|
|
|
anim = _vm->_anims->getAnimation(obj->_anims[j]);
|
|
|
|
if (anim != NULL && anim->getIndex() > markedIndex)
|
|
|
|
obj->_anims.remove_at(j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete animations which have an index greater than the marked index
|
|
|
|
_vm->_anims->deleteAfterIndex(markedIndex);
|
|
|
|
}
|
|
|
|
|
2009-07-25 02:23:00 +00:00
|
|
|
void Script::objStatOn(Common::Queue<int> ¶ms) {
|
|
|
|
int objID = params.pop() - 1;
|
|
|
|
int roomID = params.pop() - 1;
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
|
|
|
|
|
|
|
obj->_location = roomID;
|
|
|
|
obj->_visible = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::objStat(Common::Queue<int> ¶ms) {
|
|
|
|
int status = params.pop();
|
|
|
|
int objID = params.pop() - 1;
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
|
|
|
|
|
|
|
if (status == 1) {
|
|
|
|
return;
|
|
|
|
} else if (status == 2) {
|
|
|
|
obj->_visible = false;
|
|
|
|
} else {
|
|
|
|
obj->_visible = false;
|
|
|
|
obj->_location = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = 0; i < obj->_anims.size(); ++i) {
|
|
|
|
_vm->_anims->stop(obj->_anims[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-25 03:37:22 +00:00
|
|
|
void Script::execInit(Common::Queue<int> ¶ms) {
|
|
|
|
if (_vm->_game->getLoopStatus() == kStatusInventory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int objID = params.pop() - 1;
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
|
|
|
run(obj->_program, obj->_init);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::execLook(Common::Queue<int> ¶ms) {
|
|
|
|
if (_vm->_game->getLoopStatus() == kStatusInventory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int objID = params.pop() - 1;
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
|
|
|
run(obj->_program, obj->_look);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Script::execUse(Common::Queue<int> ¶ms) {
|
|
|
|
if (_vm->_game->getLoopStatus() == kStatusInventory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int objID = params.pop() - 1;
|
|
|
|
|
|
|
|
GameObject *obj = _vm->_game->getObject(objID);
|
|
|
|
run(obj->_program, obj->_use);
|
|
|
|
}
|
|
|
|
|
2009-07-25 04:36:43 +00:00
|
|
|
void Script::walkOn(Common::Queue<int> ¶ms) {
|
|
|
|
if (_vm->_game->getLoopStatus() == kStatusInventory) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int x = params.pop();
|
|
|
|
int y = params.pop();
|
|
|
|
params.pop(); // facing direction, not used yet
|
|
|
|
|
|
|
|
_vm->_game->walkHero(x, y);
|
|
|
|
}
|
2009-06-12 09:45:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Evaluates mathematical expressions
|
|
|
|
* @param reader Stream reader set to the beginning of the expression
|
|
|
|
*/
|
|
|
|
|
2009-07-04 14:48:36 +00:00
|
|
|
int Script::handleMathExpression(Common::MemoryReadStream &reader) {
|
2009-07-06 17:25:34 +00:00
|
|
|
Common::Stack<int> stk;
|
2009-06-10 02:59:53 +00:00
|
|
|
mathExpressionObject obj;
|
2009-07-06 17:25:34 +00:00
|
|
|
GPL2Operator oper;
|
2009-07-06 18:49:51 +00:00
|
|
|
GPL2Function func;
|
2009-06-10 02:59:53 +00:00
|
|
|
|
|
|
|
// Read in initial math object
|
2009-07-08 00:34:53 +00:00
|
|
|
obj = (mathExpressionObject)reader.readSint16LE();
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-07-06 17:25:34 +00:00
|
|
|
int value;
|
2009-07-06 18:49:51 +00:00
|
|
|
int arg1, arg2, res;
|
2009-07-06 17:25:34 +00:00
|
|
|
|
2009-06-08 22:18:52 +00:00
|
|
|
while (1) {
|
2009-06-10 02:59:53 +00:00
|
|
|
if (obj == kMathEnd) {
|
2009-06-12 11:32:44 +00:00
|
|
|
// Check whether the expression was evaluated correctly
|
|
|
|
// The stack should contain only one value after the evaluation
|
|
|
|
// i.e. the result of the expression
|
|
|
|
assert(stk.size() == 1 && "Mathematical expression error");
|
2009-06-10 02:59:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (obj) {
|
|
|
|
|
|
|
|
// If the object type is not known, assume that it's a number
|
|
|
|
default:
|
|
|
|
case kMathNumber:
|
2009-07-08 00:34:53 +00:00
|
|
|
value = reader.readSint16LE();
|
2009-06-10 02:59:53 +00:00
|
|
|
stk.push(value);
|
2009-07-06 17:25:34 +00:00
|
|
|
debugC(3, kDraciBytecodeDebugLevel, "\t\tnumber: %d", value);
|
2009-06-10 02:59:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kMathOperator:
|
2009-07-08 00:34:53 +00:00
|
|
|
value = reader.readSint16LE();
|
2009-07-06 18:49:51 +00:00
|
|
|
arg1 = stk.pop();
|
|
|
|
arg2 = stk.pop();
|
2009-06-12 11:32:44 +00:00
|
|
|
|
2009-07-06 17:25:34 +00:00
|
|
|
// Fetch operator
|
|
|
|
oper = _operatorList[value-1];
|
2009-06-12 11:32:44 +00:00
|
|
|
|
2009-07-06 17:25:34 +00:00
|
|
|
// Calculate result
|
2009-07-06 18:49:51 +00:00
|
|
|
res = (this->*(oper._handler))(arg1, arg2);
|
2009-07-06 17:25:34 +00:00
|
|
|
|
|
|
|
// Push result
|
|
|
|
stk.push(res);
|
|
|
|
|
|
|
|
debugC(3, kDraciBytecodeDebugLevel, "\t\t%d %s %d (res: %d)",
|
2009-07-06 18:49:51 +00:00
|
|
|
arg1, oper._name.c_str(), arg2, res);
|
2009-06-10 02:59:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kMathVariable:
|
2009-07-08 00:34:53 +00:00
|
|
|
value = reader.readSint16LE();
|
2009-07-07 20:57:14 +00:00
|
|
|
|
2009-07-07 21:30:36 +00:00
|
|
|
stk.push(_vm->_game->getVariable(value-1));
|
2009-07-07 20:57:14 +00:00
|
|
|
|
|
|
|
debugC(3, kDraciBytecodeDebugLevel, "\t\tvariable: %d (%d)", value,
|
2009-07-07 21:30:36 +00:00
|
|
|
_vm->_game->getVariable(value-1));
|
2009-06-10 02:59:53 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case kMathFunctionCall:
|
2009-07-08 00:34:53 +00:00
|
|
|
value = reader.readSint16LE();
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-07-06 18:49:51 +00:00
|
|
|
// Fetch function
|
|
|
|
func = _functionList[value-1];
|
|
|
|
|
|
|
|
// If not yet implemented
|
|
|
|
if (func._handler == NULL) {
|
|
|
|
stk.pop();
|
|
|
|
|
|
|
|
// FIXME: Pushing dummy value for now, but should push return value
|
|
|
|
stk.push(0);
|
|
|
|
|
|
|
|
debugC(3, kDraciBytecodeDebugLevel, "\t\tcall: %s (not implemented)",
|
|
|
|
func._name.c_str());
|
|
|
|
} else {
|
|
|
|
arg1 = stk.pop();
|
|
|
|
|
|
|
|
// Calculate result
|
|
|
|
res = (this->*(func._handler))(arg1);
|
2009-07-12 18:55:44 +00:00
|
|
|
|
|
|
|
// Push the result on the evaluation stack
|
|
|
|
stk.push(res);
|
2009-07-06 18:49:51 +00:00
|
|
|
|
|
|
|
debugC(3, kDraciBytecodeDebugLevel, "\t\tcall: %s(%d) (res: %d)",
|
|
|
|
func._name.c_str(), arg1, res);
|
|
|
|
}
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-06-08 22:18:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-07-08 00:34:53 +00:00
|
|
|
obj = (mathExpressionObject) reader.readSint16LE();
|
2009-06-08 22:18:52 +00:00
|
|
|
}
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-07-04 14:48:36 +00:00
|
|
|
return stk.pop();
|
2009-06-08 22:18:52 +00:00
|
|
|
}
|
|
|
|
|
2009-06-12 09:45:12 +00:00
|
|
|
/**
|
|
|
|
* @brief Find the current command in the internal table
|
|
|
|
*
|
|
|
|
* @param num Command number
|
|
|
|
* @param subnum Command subnumer
|
|
|
|
*
|
|
|
|
* @return NULL if command is not found. Otherwise, a pointer to a GPL2Command
|
|
|
|
* struct representing the command.
|
|
|
|
*/
|
2009-07-04 14:48:36 +00:00
|
|
|
const GPL2Command *Script::findCommand(byte num, byte subnum) {
|
2009-06-10 01:35:47 +00:00
|
|
|
unsigned int i = 0;
|
|
|
|
while (1) {
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-06-10 01:35:47 +00:00
|
|
|
// Command not found
|
|
|
|
if (i >= kNumCommands) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return found command
|
2009-07-04 14:48:36 +00:00
|
|
|
if (_commandList[i]._number == num &&
|
|
|
|
_commandList[i]._subNumber == subnum) {
|
|
|
|
return &_commandList[i];
|
2009-06-10 01:35:47 +00:00
|
|
|
}
|
2009-06-10 02:59:53 +00:00
|
|
|
|
|
|
|
++i;
|
2009-06-10 01:35:47 +00:00
|
|
|
}
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-06-10 01:35:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-06-12 09:45:12 +00:00
|
|
|
/**
|
2009-07-08 00:34:53 +00:00
|
|
|
* @brief GPL2 bytecode interpreter
|
2009-06-28 16:19:10 +00:00
|
|
|
* @param program GPL program in the form of a GPL2Program struct
|
2009-06-28 16:28:16 +00:00
|
|
|
* offset Offset into the program where execution should begin
|
2009-06-12 09:45:12 +00:00
|
|
|
*
|
|
|
|
* GPL2 is short for Game Programming Language 2 which is the script language
|
2009-07-08 00:34:53 +00:00
|
|
|
* used by Draci Historie. This is the interpreter for the language.
|
2009-06-12 09:45:12 +00:00
|
|
|
*
|
|
|
|
* A compiled GPL2 program consists of a stream of bytes representing commands
|
|
|
|
* and their parameters. The syntax is as follows:
|
|
|
|
*
|
|
|
|
* Syntax of a command:
|
|
|
|
* <name of the command> <number> <sub-number> <list of parameters...>
|
|
|
|
*
|
|
|
|
* Syntax of a parameter:
|
|
|
|
* - 1: integer number literally passed to the program
|
|
|
|
* - 2-1: string stored in the reservouir of game strings (i.e. something to be
|
|
|
|
* displayed) and stored as an index in this list
|
|
|
|
* - 2-2: string resolved by the compiler (i.e., a path to another file) and
|
|
|
|
* replaced by an integer index of this entity in the appropriate namespace
|
|
|
|
* (e.g., the index of the palette, location, ...)
|
|
|
|
* - 3-0: relative jump to a label defined in this code. Each label must be
|
|
|
|
* first declared in the beginning of the program.
|
|
|
|
* - 3-1 .. 3-9: index of an entity in several namespaces, defined in file ident
|
|
|
|
* - 4: mathematical expression compiled into a postfix format
|
|
|
|
*
|
|
|
|
* In the compiled program, parameters of type 1..3 are represented by a single
|
|
|
|
* 16-bit integer. The called command knows by its definition what namespace the
|
|
|
|
* value comes from.
|
|
|
|
*/
|
|
|
|
|
2009-06-28 16:28:16 +00:00
|
|
|
int Script::run(GPL2Program program, uint16 offset) {
|
2009-07-04 14:48:36 +00:00
|
|
|
|
2009-07-25 03:41:28 +00:00
|
|
|
// Mark the last animation index before we do anything so a Release command
|
|
|
|
// doesn't unload too many animations if we forget to use a Mark command first
|
|
|
|
_vm->_game->setMarkedAnimationIndex(_vm->_anims->getLastIndex());
|
|
|
|
|
2009-07-04 14:48:36 +00:00
|
|
|
// Stream reader for the whole program
|
2009-06-28 16:19:10 +00:00
|
|
|
Common::MemoryReadStream reader(program._bytecode, program._length);
|
2009-07-04 14:48:36 +00:00
|
|
|
|
|
|
|
// Parameter queue that is passed to each command
|
|
|
|
Common::Queue<int> params;
|
2009-06-08 22:18:52 +00:00
|
|
|
|
2009-06-28 16:28:16 +00:00
|
|
|
// Offset is given as number of 16-bit integers so we need to convert
|
|
|
|
// it to a number of bytes
|
|
|
|
offset -= 1;
|
|
|
|
offset *= 2;
|
|
|
|
|
|
|
|
// Seek to the requested part of the program
|
|
|
|
reader.seek(offset);
|
|
|
|
|
2009-07-15 19:06:24 +00:00
|
|
|
debugC(3, kDraciBytecodeDebugLevel,
|
|
|
|
"Starting GPL program at offset %d (program length: %d)", offset, program._length);
|
|
|
|
|
2009-07-04 14:48:36 +00:00
|
|
|
const GPL2Command *cmd;
|
2009-06-28 16:19:10 +00:00
|
|
|
do {
|
2009-07-06 17:29:44 +00:00
|
|
|
|
2009-07-08 00:34:53 +00:00
|
|
|
// Account for GPL jump that some commands set
|
2009-07-15 19:06:24 +00:00
|
|
|
if (_jump != 0) {
|
|
|
|
debugC(6, kDraciBytecodeDebugLevel,
|
|
|
|
"Jumping from offset %d to %d (%d bytes)",
|
|
|
|
reader.pos(), reader.pos() + _jump, _jump);
|
2009-07-08 06:14:17 +00:00
|
|
|
reader.seek(_jump, SEEK_CUR);
|
2009-07-15 19:06:24 +00:00
|
|
|
}
|
2009-07-08 00:34:53 +00:00
|
|
|
|
|
|
|
// Reset jump
|
|
|
|
_jump = 0;
|
|
|
|
|
2009-07-06 17:29:44 +00:00
|
|
|
// Clear any parameters left on the stack from the previous command
|
|
|
|
// This likely won't be needed once all commands are implemented
|
|
|
|
params.clear();
|
2009-07-06 17:25:34 +00:00
|
|
|
|
2009-06-08 22:18:52 +00:00
|
|
|
// read in command pair
|
|
|
|
uint16 cmdpair = reader.readUint16BE();
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-06-10 01:35:47 +00:00
|
|
|
// extract high byte, i.e. the command number
|
|
|
|
byte num = (cmdpair >> 8) & 0xFF;
|
2009-06-10 02:59:53 +00:00
|
|
|
|
2009-06-10 01:35:47 +00:00
|
|
|
// extract low byte, i.e. the command subnumber
|
|
|
|
byte subnum = cmdpair & 0xFF;
|
2009-06-08 22:18:52 +00:00
|
|
|
|
2009-06-10 01:35:47 +00:00
|
|
|
if ((cmd = findCommand(num, subnum))) {
|
2009-07-04 14:48:36 +00:00
|
|
|
int tmp;
|
2009-06-10 02:59:53 +00:00
|
|
|
|
|
|
|
// Print command name
|
2009-07-08 00:34:53 +00:00
|
|
|
debugC(1, kDraciBytecodeDebugLevel, "%s", cmd->_name.c_str());
|
2009-06-08 22:18:52 +00:00
|
|
|
|
2009-07-04 14:48:36 +00:00
|
|
|
for (int i = 0; i < cmd->_numParams; ++i) {
|
2009-06-10 01:35:47 +00:00
|
|
|
if (cmd->_paramTypes[i] == 4) {
|
2009-07-08 00:34:53 +00:00
|
|
|
debugC(2, kDraciBytecodeDebugLevel, "\t<MATHEXPR>");
|
2009-07-04 14:48:36 +00:00
|
|
|
params.push(handleMathExpression(reader));
|
2009-06-10 02:59:53 +00:00
|
|
|
}
|
2009-06-10 01:35:47 +00:00
|
|
|
else {
|
2009-07-08 00:34:53 +00:00
|
|
|
tmp = reader.readSint16LE();
|
2009-07-04 14:48:36 +00:00
|
|
|
params.push(tmp);
|
2009-07-08 00:34:53 +00:00
|
|
|
debugC(2, kDraciBytecodeDebugLevel, "\t%d", tmp);
|
2009-06-10 01:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2009-07-08 00:34:53 +00:00
|
|
|
debugC(1, kDraciBytecodeDebugLevel, "Unknown opcode %d, %d",
|
2009-06-10 01:35:47 +00:00
|
|
|
num, subnum);
|
2009-06-08 22:18:52 +00:00
|
|
|
}
|
2009-07-04 14:48:36 +00:00
|
|
|
|
|
|
|
GPLHandler handler = cmd->_handler;
|
|
|
|
|
|
|
|
if (handler != NULL) {
|
|
|
|
// Call the handler for the current command
|
|
|
|
(this->*(cmd->_handler))(params);
|
|
|
|
}
|
|
|
|
|
2009-07-07 20:57:14 +00:00
|
|
|
} while (cmd->_name != "gplend" && cmd->_name != "exit");
|
2009-06-08 22:18:52 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|