From 7a1898730155dce824451d98bbe65b430832d575 Mon Sep 17 00:00:00 2001 From: Ľubomír Remák Date: Sat, 24 Mar 2018 01:43:33 +0100 Subject: MUTATIONOFJB: Support for running commands. --- engines/mutationofjb/commands/callmacrocommand.cpp | 3 +- engines/mutationofjb/game.cpp | 18 +++----- engines/mutationofjb/game.h | 7 ++- engines/mutationofjb/mutationofjb.cpp | 1 + engines/mutationofjb/script.cpp | 50 ++++++++++++++++++++-- engines/mutationofjb/script.h | 14 +++++- 6 files changed, 73 insertions(+), 20 deletions(-) diff --git a/engines/mutationofjb/commands/callmacrocommand.cpp b/engines/mutationofjb/commands/callmacrocommand.cpp index b1c59eba21..49b948c48d 100644 --- a/engines/mutationofjb/commands/callmacrocommand.cpp +++ b/engines/mutationofjb/commands/callmacrocommand.cpp @@ -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 { diff --git a/engines/mutationofjb/game.cpp b/engines/mutationofjb/game.cpp index 047971b83d..6436c7ad74 100644 --- a/engines/mutationofjb/game.cpp +++ b/engines/mutationofjb/game.cpp @@ -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(); } } diff --git a/engines/mutationofjb/game.h b/engines/mutationofjb/game.h index 5ed65f1b51..b44929df45 100644 --- a/engines/mutationofjb/game.h +++ b/engines/mutationofjb/game.h @@ -24,6 +24,7 @@ #define MUTATIONOFJB_GAME_H #include "common/scummsys.h" +#include "mutationofjb/script.h" namespace Common { class String; @@ -47,10 +48,12 @@ public: 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; @@ -58,6 +61,8 @@ private: Script *_globalScript; Script *_localScript; Room *_room; + + ScriptExecutionContext _scriptExecCtx; }; } diff --git a/engines/mutationofjb/mutationofjb.cpp b/engines/mutationofjb/mutationofjb.cpp index 66de1f7a9f..1bd08dd706 100644 --- a/engines/mutationofjb/mutationofjb.cpp +++ b/engines/mutationofjb/mutationofjb.cpp @@ -111,6 +111,7 @@ Common::Error MutationOfJBEngine::run() { } _console->onFrame(); + _game->update(); _system->delayMillis(40); _screen->update(); } diff --git a/engines/mutationofjb/script.cpp b/engines/mutationofjb/script.cpp index 1291bd8d97..e3aa3e6785 100644 --- a/engines/mutationofjb/script.cpp +++ b/engines/mutationofjb/script.cpp @@ -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(); diff --git a/engines/mutationofjb/script.h b/engines/mutationofjb/script.h index 940a274c29..823210675f 100644 --- a/engines/mutationofjb/script.h +++ b/engines/mutationofjb/script.h @@ -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 Commands; @@ -96,15 +98,23 @@ private: 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 _stack; + Command *_activeCommand; + Common::Stack _callStack; + Script *_localScriptOverride; }; class Script { -- cgit v1.2.3