Permalink
Browse files

MUTATIONOFJB: Add support for calling macros.

  • Loading branch information...
LubomirR authored and sev- committed Mar 22, 2018
1 parent e1d173e commit 938f222d4857b45e0f1f7e4726040ab0bf1d9b67
@@ -32,7 +32,7 @@ class AddItemCommandParser : public SeqCommandParser {
public:
AddItemCommandParser() {}

virtual bool parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command);
virtual bool parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command) override;
};

class AddItemCommand : public SeqCommand {
@@ -0,0 +1,85 @@
/* 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.
*
*/

#include "mutationofjb/commands/callmacrocommand.h"
#include "mutationofjb/script.h"
#include "mutationofjb/game.h"
#include "common/translation.h"

/*
"_" <name>
Calls macro with the specified name.
*/

namespace MutationOfJB {

bool CallMacroCommandParser::parse(const Common::String &line, ScriptParseContext &, Command *&command) {
if (line.size() < 2 || line.firstChar() != '_') {
return false;
}

const Common::String macroName = line.c_str() + 1;
command = new CallMacroCommand(macroName);
return true;
}

void CallMacroCommandParser::transition(ScriptParseContext &, Command *oldCommand, Command *newCommand, CommandParser *) {
if (!oldCommand || !newCommand) {
warning(_("Unexpected empty command in transition"));
return;
}

static_cast<CallMacroCommand *>(oldCommand)->setReturnCommand(newCommand);
}


void CallMacroCommand::setReturnCommand(Command *cmd) {
_returnCommand = cmd;
}

Command *CallMacroCommand::getReturnCommand() const {
return _returnCommand;
}

Command::ExecuteResult CallMacroCommand::execute(ScriptExecutionContext &scriptExecCtx) {
Game &game = scriptExecCtx.getGame();
_callCommand = game.getMacro(_macroName);
if (_callCommand) {
scriptExecCtx.pushReturnCommand(_returnCommand);
} else {
warning("Macro '%s' not found.", _macroName.c_str());
}

return Finished;
}

Command *CallMacroCommand::next() const {
return _callCommand;
}

Common::String CallMacroCommand::debugString() const {
return Common::String::format("CALL '%s'", _macroName.c_str());
}

}

@@ -0,0 +1,58 @@
/* 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.
*
*/

#ifndef MUTATIONOFJB_CALLMACROCOMMAND_H
#define MUTATIONOFJB_CALLMACROCOMMAND_H

#include "mutationofjb/commands/command.h"
#include "common/scummsys.h"
#include "common/str.h"

namespace MutationOfJB {

class CallMacroCommandParser : public CommandParser {
public:
CallMacroCommandParser() {}

virtual bool parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command) override;
virtual void transition(ScriptParseContext &parseCtx, Command *oldCommand, Command *newCommand, CommandParser *newCommandParser) override;
};

class CallMacroCommand : public Command {
public:
CallMacroCommand(const Common::String &macroName) : _macroName(macroName), _returnCommand(nullptr), _callCommand(nullptr) {}
void setReturnCommand(Command *);

Command *getReturnCommand() const;

virtual ExecuteResult execute(ScriptExecutionContext &scriptExecCtx) override;
Command *next() const override;
virtual Common::String debugString() const override;
private:
Common::String _macroName;
Command *_returnCommand;
Command *_callCommand;
};

}

#endif
@@ -27,6 +27,7 @@
#include "mutationofjb/commands/command.h"
#include "mutationofjb/commands/seqcommand.h"
#include "mutationofjb/commands/conditionalcommand.h"
#include "mutationofjb/commands/callmacrocommand.h"
#include "common/debug-channels.h"
#include "common/translation.h"
#include "common/scummsys.h"
@@ -129,6 +130,8 @@ void Console::showCommands(Command *command, int indentLevel) {
debugPrintf("ELSE\n");
showCommands(condCmd->getFalseCommand(), indentLevel + 1);
command = nullptr;
} else if (CallMacroCommand* const callMacroCmd = dynamic_cast<CallMacroCommand *>(command)) {
command = callMacroCmd->getReturnCommand();
} else {
command = nullptr;
}
@@ -28,10 +28,11 @@
#include "mutationofjb/script.h"
#include "mutationofjb/util.h"
#include "common/util.h"
#include "common/str.h"

namespace MutationOfJB {

Game::Game(MutationOfJBEngine* vm) : _vm(vm) {
Game::Game(MutationOfJBEngine *vm) : _vm(vm) {
_gameData = new GameData;
loadGameData(false);

@@ -104,4 +105,18 @@ void Game::changeScene(uint8 sceneId, bool partB) {
scriptFile.close();
}

Command *Game::getMacro(const Common::String &name) const {
Command *cmd = nullptr;

if (_localScript) {
cmd = _localScript->getMacro(name);
}

if (!cmd && _globalScript) {
cmd = _globalScript->getMacro(name);
}

return cmd;
}

}
@@ -25,23 +25,30 @@

#include "common/scummsys.h"

namespace Common {
class String;
}

namespace MutationOfJB {

class Command;
class MutationOfJBEngine;
class GameData;
class Script;
class Room;

class Game {
public:
Game(MutationOfJBEngine* vm);
Game(MutationOfJBEngine *vm);
GameData &getGameData();

Script *getGlobalScript() const;
Script *getLocalScript() const;

void changeScene(uint8 sceneId, bool partB);

Command *getMacro(const Common::String &name) const;

private:
bool loadGameData(bool partB);

@@ -2,6 +2,7 @@ MODULE := engines/mutationofjb

MODULE_OBJS := \
commands/additemcommand.o \
commands/callmacrocommand.o \
commands/camefromcommand.o \
commands/changecommand.o \
commands/command.o \
@@ -39,6 +39,7 @@
#include "mutationofjb/commands/labelcommand.h"
#include "mutationofjb/commands/gotocommand.h"
#include "mutationofjb/commands/camefromcommand.h"
#include "mutationofjb/commands/callmacrocommand.h"
#include "mutationofjb/game.h"

namespace MutationOfJB {
@@ -49,6 +50,7 @@ static CommandParser **getParsers() {
new IfItemCommandParser,
new IfCommandParser,
new CameFromCommandParser,
new CallMacroCommandParser,
new EndBlockCommandParser,
new ChangeDoorCommandParser,
new ChangeObjectCommandParser,
@@ -109,6 +111,10 @@ Command *ScriptExecutionContext::popReturnCommand() {
return _stack.pop();
}

Game &ScriptExecutionContext::getGame() {
return _game;
}

GameData &ScriptExecutionContext::getGameData() {
return _game.getGameData();
}
@@ -206,4 +212,13 @@ const Macros &Script::getMacros() const {
return _macros;
}

Command *Script::getMacro(const Common::String &name) const {
Macros::const_iterator it = _macros.find(name);
if (it == _macros.end()) {
return nullptr;
}

return it->_value;
}

}
@@ -99,6 +99,7 @@ class ScriptExecutionContext {
ScriptExecutionContext(Game &game) : _game(game) {}
void pushReturnCommand(Command *);
Command *popReturnCommand();
Game &getGame();
GameData &getGameData();

private:
@@ -116,6 +117,7 @@ class Script {
const ActionInfos &getTalkActionInfos() const;
const ActionInfos &getUseActionInfos() const;
const Macros &getMacros() const;
Command *getMacro(const Common::String &name) const;

private:
void destroy();

0 comments on commit 938f222

Please sign in to comment.