Permalink
Browse files

MUTATIONOFJB: Parse startup sections in scripts and fix change scene …

…command.
  • Loading branch information...
LubomirR committed Mar 24, 2018
1 parent 4f6aebd commit f1c4c1e74daafd50ce1c74fa848be1522727b75e
@@ -37,16 +37,30 @@ namespace MutationOfJB {
// <ii> 2B Entity ID.
// <val> VL Value.

bool ChangeCommandParser::parseValueString(const Common::String &valueString, uint8 &sceneId, uint8 &entityId, ChangeCommand::ChangeRegister &reg, ChangeCommand::ChangeOperation &op, ChangeCommandValue &ccv) {
if (valueString.size() < 8) {
return false;
bool ChangeCommandParser::parseValueString(const Common::String &valueString, bool changeEntity, uint8 &sceneId, uint8 &entityId, ChangeCommand::ChangeRegister &reg, ChangeCommand::ChangeOperation &op, ChangeCommandValue &ccv) {
if (changeEntity) {
if (valueString.size() < 8) {
return false;
}
} else {
if (valueString.size() < 7) {
return false;
}
}

sceneId = atoi(valueString.c_str() + 3);
entityId = atoi(valueString.c_str() + 6);
if (changeEntity) {
entityId = atoi(valueString.c_str() + 6);
}
const char *val = nullptr;
if (valueString.size() >= 9) {
val = valueString.c_str() + 9;
if (changeEntity) {
if (valueString.size() >= 9) {
val = valueString.c_str() + 9;
}
} else {
if (valueString.size() >= 6) {
val = valueString.c_str() + 6;
}
}

if (valueString.hasPrefix("NM")) {
@@ -137,7 +151,7 @@ bool ChangeDoorCommandParser::parse(const Common::String &line, ScriptParseConte
ChangeCommand::ChangeRegister reg;
ChangeCommand::ChangeOperation op;
ChangeCommandValue val;
if (!parseValueString(line.c_str() + 8, sceneId, objectId, reg, op, val)) {
if (!parseValueString(line.c_str() + 8, true, sceneId, objectId, reg, op, val)) {
return false;
}

@@ -154,7 +168,7 @@ bool ChangeObjectCommandParser::parse(const Common::String &line, ScriptParseCon
ChangeCommand::ChangeRegister reg;
ChangeCommand::ChangeOperation op;
ChangeCommandValue val;
if (!parseValueString(line.c_str() + 8, sceneId, objectId, reg, op, val)) {
if (!parseValueString(line.c_str() + 8, true, sceneId, objectId, reg, op, val)) {
return false;
}

@@ -171,7 +185,7 @@ bool ChangeStaticCommandParser::parse(const Common::String &line, ScriptParseCon
ChangeCommand::ChangeRegister reg;
ChangeCommand::ChangeOperation op;
ChangeCommandValue val;
if (!parseValueString(line.c_str() + 8, sceneId, objectId, reg, op, val)) {
if (!parseValueString(line.c_str() + 8, true, sceneId, objectId, reg, op, val)) {
return false;
}

@@ -188,7 +202,7 @@ bool ChangeSceneCommandParser::parse(const Common::String &line, ScriptParseCont
ChangeCommand::ChangeRegister reg;
ChangeCommand::ChangeOperation op;
ChangeCommandValue val;
if (!parseValueString(line.c_str() + 8, sceneId, objectId, reg, op, val)) {
if (!parseValueString(line.c_str() + 7, false, sceneId, objectId, reg, op, val)) {
return false;
}

@@ -351,7 +365,7 @@ Command::ExecuteResult ChangeDoorCommand::execute(ScriptExecutionContext &script
}

Common::String ChangeDoorCommand::debugString() const {
return Common::String::format("scene%d.door%d.%s %s %s", _sceneId, _entityId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
return Common::String::format("SCENE%d.DOOR%d.%s %s %s", _sceneId, _entityId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
}

Command::ExecuteResult ChangeObjectCommand::execute(ScriptExecutionContext &scriptExecCtx) {
@@ -414,7 +428,7 @@ Command::ExecuteResult ChangeObjectCommand::execute(ScriptExecutionContext &scri
}

Common::String ChangeObjectCommand::debugString() const {
return Common::String::format("scene%d.object%d.%s %s %s", _sceneId, _entityId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
return Common::String::format("SCENE%d.OBJECT%d.%s %s %s", _sceneId, _entityId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
}

Command::ExecuteResult ChangeStaticCommand::execute(ScriptExecutionContext &scriptExecCtx) {
@@ -465,7 +479,7 @@ Command::ExecuteResult ChangeStaticCommand::execute(ScriptExecutionContext &scri
}

Common::String ChangeStaticCommand::debugString() const {
return Common::String::format("scene%d.static%d.%s %s %s", _sceneId, _entityId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
return Common::String::format("SCENE%d.STATIC%d.%s %s %s", _sceneId, _entityId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
}

Command::ExecuteResult ChangeSceneCommand::execute(ScriptExecutionContext &scriptExecCtx) {
@@ -508,6 +522,6 @@ Command::ExecuteResult ChangeSceneCommand::execute(ScriptExecutionContext &scrip
}

Common::String ChangeSceneCommand::debugString() const {
return Common::String::format("scene%d.%s %s %s", _sceneId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
return Common::String::format("SCENE%d.%s %s %s", _sceneId, getRegisterAsString(), getOperationAsString(), getValueAsString().c_str());
}
}
@@ -84,7 +84,7 @@ class ChangeCommand : public SeqCommand {

class ChangeCommandParser : public SeqCommandParser {
protected:
bool parseValueString(const Common::String &valueString, uint8 &sceneId, uint8 &entityId, ChangeCommand::ChangeRegister &reg, ChangeCommand::ChangeOperation &op, ChangeCommandValue &ccv);
bool parseValueString(const Common::String &valueString, bool changeEntity, uint8 &sceneId, uint8 &entityId, ChangeCommand::ChangeRegister &reg, ChangeCommand::ChangeOperation &op, ChangeCommandValue &ccv);
int parseInteger(const char *val, ChangeCommand::ChangeOperation &op);
};

@@ -25,6 +25,7 @@
#include "mutationofjb/commands/conditionalcommand.h"
#include "common/str.h"
#include "common/debug.h"
#include "common/translation.h"

/*
("#L " | "-L ") <object>
@@ -107,6 +108,8 @@ bool EndBlockCommandParser::parse(const Common::String &line, ScriptParseContext
}
} else if (line.size() >= 8 && line.hasPrefix("#MACRO")) {
_foundMacro = line.c_str() + 7;
} else if (line.size() >= 10 && line.hasPrefix("#STARTUP")) {
_foundStartup = line.c_str() + 9;
}

if (firstChar == '#') {
@@ -138,11 +141,26 @@ void EndBlockCommandParser::transition(ScriptParseContext &parseCtx, Command *,
_ifTag = 0;
}

if (!_foundMacro.empty() && newCommand) {
if (!parseCtx._macros.contains(_foundMacro)) {
parseCtx._macros[_foundMacro] = newCommand;
if (!_foundMacro.empty()) {
if (newCommand) {
if (!parseCtx._macros.contains(_foundMacro)) {
parseCtx._macros[_foundMacro] = newCommand;
} else {
warning(_("Macro '%s' already exists."), _foundMacro.c_str());
}
}
_foundMacro.clear();
}
if (!_foundStartup.empty()) {
if (newCommand) {
const uint8 startupId = atoi(_foundStartup.c_str());
if (!parseCtx._startups.contains(startupId)) {
parseCtx._startups[startupId] = newCommand;
} else {
warning(_("Startup %u already exists."), (unsigned int) startupId);
}
}
_foundMacro = "";
_foundStartup.clear();
}

if (newCommandParser != this) {
@@ -45,6 +45,7 @@ class EndBlockCommandParser : public CommandParser {

Common::Array<uint> _pendingActionInfos;
Common::String _foundMacro;
Common::String _foundStartup;
};

class EndBlockCommand : public Command {
@@ -55,24 +55,37 @@ static Common::String convertToASCII(const Common::String &str) {
}

Console::Console(MutationOfJBEngine *vm) : _vm(vm) {
registerCmd("showallcommands", WRAP_METHOD(Console, cmd_showallcommands));
registerCmd("listsections", WRAP_METHOD(Console, cmd_listsections));
registerCmd("showsection", WRAP_METHOD(Console, cmd_showsection));
registerCmd("listmacros", WRAP_METHOD(Console, cmd_listmacros));
registerCmd("showmacro", WRAP_METHOD(Console, cmd_showmacro));
registerCmd("liststartups", WRAP_METHOD(Console, cmd_liststartups));
registerCmd("showstartup", WRAP_METHOD(Console, cmd_showstartup));
registerCmd("changescene", WRAP_METHOD(Console, cmd_changescene));
}

bool Console::cmd_showallcommands(int argc, const char **argv) {
if (argc == 2) {
Script *const script = getScriptFromArg(argv[1]);
if (script) {
const Commands &commands = script->getAllCommands();

for (Commands::const_iterator it = commands.begin(); it != commands.end(); ++it) {
debugPrintf("%s\n", convertToASCII((*it)->debugString()).c_str());
}
}
} else {
debugPrintf(_("showallcommands <G|L>\n"));
}

return true;
}

bool Console::cmd_listsections(int argc, const char **argv) {
if (argc == 3) {
Script *script = nullptr;
if (strcmp(argv[1], "G") == 0) {
script = _vm->getGame().getGlobalScript();
} else if (strcmp(argv[1], "L") == 0) {
script = _vm->getGame().getLocalScript();
}
if (!script) {
debugPrintf(_("Choose 'G' (global) or 'L' (local) script.\n"));
} else {
Script *const script = getScriptFromArg(argv[1]);
if (script) {
if (strcmp(argv[2], "L") == 0) {
const ActionInfos &actionInfos = script->getLookActionInfos();
for (ActionInfos::const_iterator it = actionInfos.begin(); it != actionInfos.end(); ++it) {
@@ -140,15 +153,8 @@ void Console::showCommands(Command *command, int indentLevel) {

bool Console::cmd_showsection(int argc, const char **argv) {
if (argc >= 4) {
Script *script = nullptr;
if (strcmp(argv[1], "G") == 0) {
script = _vm->getGame().getGlobalScript();
} else if (strcmp(argv[1], "L") == 0) {
script = _vm->getGame().getLocalScript();
}
if (!script) {
debugPrintf(_("Choose 'G' (global) or 'L' (local) script.\n"));
} else {
Script *const script = getScriptFromArg(argv[1]);
if (script) {
Command *command = nullptr;
bool found = false;
if (strcmp(argv[2], "L") == 0) {
@@ -212,15 +218,8 @@ bool Console::cmd_showsection(int argc, const char **argv) {

bool Console::cmd_listmacros(int argc, const char **argv) {
if (argc == 2) {
Script *script = nullptr;
if (strcmp(argv[1], "G") == 0) {
script = _vm->getGame().getGlobalScript();
} else if (strcmp(argv[1], "L") == 0) {
script = _vm->getGame().getLocalScript();
}
if (!script) {
debugPrintf(_("Choose 'G' (global) or 'L' (local) script.\n"));
} else {
Script *const script = getScriptFromArg(argv[1]);
if (script) {
const Macros &macros = script->getMacros();
for (Macros::const_iterator it = macros.begin(); it != macros.end(); ++it) {
debugPrintf("%s\n", it->_key.c_str());
@@ -261,6 +260,43 @@ bool Console::cmd_showmacro(int argc, const char **argv) {
return true;
}

bool Console::cmd_liststartups(int argc, const char **argv) {
if (argc == 2) {
Script *const script = getScriptFromArg(argv[1]);
if (script) {
const Startups &startups = script->getStartups();
for (Startups::const_iterator it = startups.begin(); it != startups.end(); ++it) {
debugPrintf("%u\n", (unsigned int) it->_key);
}
}
} else {
debugPrintf(_("liststartups <G|L>\n"));
}

return true;
}

bool Console::cmd_showstartup(int argc, const char **argv) {
if (argc == 3) {
Script *const script = getScriptFromArg(argv[1]);
if (script) {
const Startups &startups = script->getStartups();
Startups::const_iterator itMacro = startups.find((uint8) atoi(argv[2]));
if (itMacro != startups.end()) {
if (itMacro->_value) {
showCommands(itMacro->_value);
}
} else {
debugPrintf("Startup not found.\n");
}
}
} else {
debugPrintf(_("showstartup <G|L> <startupid>\n"));
}

return true;
}

bool Console::cmd_changescene(int argc, const char **argv) {
if (argc == 2) {
const uint8 sceneId = atoi(argv[1]);
@@ -274,4 +310,18 @@ bool Console::cmd_changescene(int argc, const char **argv) {
return true;
}

Script *Console::getScriptFromArg(const char *arg) {
Script *script = nullptr;
if (strcmp(arg, "G") == 0) {
script = _vm->getGame().getGlobalScript();
} else if (strcmp(arg, "L") == 0) {
script = _vm->getGame().getLocalScript();
}
if (!script) {
debugPrintf(_("Choose 'G' (global) or 'L' (local) script.\n"));
}

return script;
}

}
@@ -29,20 +29,25 @@ namespace MutationOfJB {

class MutationOfJBEngine;
class Command;
class Script;

class Console : public GUI::Debugger {
public:
Console(MutationOfJBEngine *vm);
virtual ~Console(void) {}
private:
bool cmd_showallcommands(int argc, const char **argv);
bool cmd_listsections(int argc, const char **argv);
bool cmd_showsection(int argc, const char **argv);
bool cmd_listmacros(int argc, const char **argv);
bool cmd_showmacro(int argc, const char **argv);
bool cmd_liststartups(int argc, const char **argv);
bool cmd_showstartup(int argc, const char **argv);
bool cmd_changescene(int argc, const char **argv);

void showIndent(int indentLevel);
void showCommands(Command *command, int indentLevel = 0);
Script *getScriptFromArg(const char *arg);

MutationOfJBEngine *_vm;
};
@@ -221,6 +221,7 @@ bool Script::loadFromStream(Common::SeekableReadStream &stream) {
}

_macros = parseCtx._macros;
_startups = parseCtx._startups;

return true;
}
@@ -252,6 +253,10 @@ const ActionInfos &Script::getUseActionInfos() const {
return _useActionInfos;
}

const Commands &Script::getAllCommands() const {
return _allCommands;
}

const Macros &Script::getMacros() const {
return _macros;
}
@@ -265,4 +270,17 @@ Command *Script::getMacro(const Common::String &name) const {
return it->_value;
}

const Startups &Script::getStartups() const {
return _startups;
}

Command *Script::getStartup(uint8 startupId) const {
Startups::const_iterator it = _startups.find(startupId);
if (it == _startups.end()) {
return nullptr;
}

return it->_value;
}

}
Oops, something went wrong.

0 comments on commit f1c4c1e

Please sign in to comment.