Permalink
Browse files

MUTATIONOFJB: Support for running commands.

  • Loading branch information...
LubomirR committed Mar 24, 2018
1 parent 422e179 commit 4f6aebd619ba43958b13599ae4d1466cf0e6ab79
@@ -62,8 +62,7 @@ Command *CallMacroCommand::getReturnCommand() const {
}

Command::ExecuteResult CallMacroCommand::execute(ScriptExecutionContext &scriptExecCtx) {
Game &game = scriptExecCtx.getGame();
_callCommand = game.getMacro(_macroName);
_callCommand = scriptExecCtx.getMacro(_macroName);
if (_callCommand) {
scriptExecCtx.pushReturnCommand(_returnCommand);
} else {
@@ -27,12 +27,15 @@
#include "mutationofjb/room.h"
#include "mutationofjb/script.h"
#include "mutationofjb/util.h"
#include "mutationofjb/commands/command.h"
#include "common/util.h"
#include "common/str.h"
#include "common/translation.h"

namespace MutationOfJB {

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

@@ -105,18 +108,9 @@ 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;
void Game::update() {
_scriptExecCtx.runActiveCommand();
}

}
@@ -24,6 +24,7 @@
#define MUTATIONOFJB_GAME_H

#include "common/scummsys.h"
#include "mutationofjb/script.h"

namespace Common {
class String;
@@ -47,17 +48,21 @@ class Game {

void changeScene(uint8 sceneId, bool partB);

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

private:
bool loadGameData(bool partB);
void runActiveCommand();
void startCommand(Command *cmd);

MutationOfJBEngine *_vm;

GameData *_gameData;
Script *_globalScript;
Script *_localScript;
Room *_room;

ScriptExecutionContext _scriptExecCtx;
};

}
@@ -111,6 +111,7 @@ Common::Error MutationOfJBEngine::run() {
}

_console->onFrame();
_game->update();
_system->delayMillis(40);
_screen->update();
}
@@ -26,6 +26,7 @@
#include "common/hash-str.h"
#include "common/stream.h"
#include "common/debug.h"
#include "common/translation.h"
#include "mutationofjb/commands/command.h"
#include "mutationofjb/commands/ifcommand.h"
#include "mutationofjb/commands/ifitemcommand.h"
@@ -100,15 +101,15 @@ void ScriptParseContext::addConditionalCommand(ConditionalCommand *command, char


void ScriptExecutionContext::pushReturnCommand(Command *cmd) {
_stack.push(cmd);
_callStack.push(cmd);
}

Command *ScriptExecutionContext::popReturnCommand() {
if (_stack.empty()) {
if (_callStack.empty()) {
return nullptr;
}

return _stack.pop();
return _callStack.pop();
}

Game &ScriptExecutionContext::getGame() {
@@ -119,6 +120,49 @@ GameData &ScriptExecutionContext::getGameData() {
return _game.getGameData();
}

void ScriptExecutionContext::clear() {
_callStack.clear();
}

Command::ExecuteResult ScriptExecutionContext::runActiveCommand() {
while (_activeCommand) {
const Command::ExecuteResult result = _activeCommand->execute(*this);
if (result == Command::Finished) {
_activeCommand = _activeCommand->next();
} else {
return result;
}
}

return Command::Finished;
}

Command::ExecuteResult ScriptExecutionContext::startCommand(Command *cmd) {
if (_activeCommand) {
warning(_("Trying to start command while another one is running."));
return Command::Finished;
}
clear();
_activeCommand = cmd;
return runActiveCommand();
}

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

Script *const localScript = _localScriptOverride ? _localScriptOverride : _game.getLocalScript();
Script *const globalScript = _game.getGlobalScript();

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

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

return cmd;
}

bool Script::loadFromStream(Common::SeekableReadStream &stream) {
destroy();
@@ -23,6 +23,7 @@
#ifndef MUTATIONOFJB_SCRIPT_H
#define MUTATIONOFJB_SCRIPT_H

#include "mutationofjb/commands/command.h"
#include "common/array.h"
#include "common/hashmap.h"
#include "common/hash-str.h"
@@ -41,6 +42,7 @@ class Game;
class GameData;
class GotoCommand;
class ConditionalCommand;
class Script;
typedef Common::Array<Command *> Commands;


@@ -96,15 +98,23 @@ class ScriptParseContext {

class ScriptExecutionContext {
public:
ScriptExecutionContext(Game &game) : _game(game) {}
ScriptExecutionContext(Game &game, Script *localScriptOverride = nullptr) : _game(game), _activeCommand(nullptr), _localScriptOverride(localScriptOverride) {}
void clear();

Command::ExecuteResult runActiveCommand();
Command::ExecuteResult startCommand(Command *cmd);

void pushReturnCommand(Command *);
Command *popReturnCommand();
Game &getGame();
GameData &getGameData();
Command *getMacro(const Common::String &name) const;

private:
Game &_game;
Common::Stack<Command *> _stack;
Command *_activeCommand;
Common::Stack<Command *> _callStack;
Script *_localScriptOverride;
};

class Script {

0 comments on commit 4f6aebd

Please sign in to comment.