diff options
author | Ľubomír Remák | 2018-03-18 15:28:46 +0100 |
---|---|---|
committer | Eugene Sandulenko | 2018-08-25 23:12:01 +0200 |
commit | 54c2c0aee6e371f8058c50e0a4113f5b5492169e (patch) | |
tree | ae551cedc20fa03f179340aae9f7497193c80b6e /engines/mutationofjb | |
parent | 523f973e0a4ba16b278d546da9267a693924f957 (diff) | |
download | scummvm-rg350-54c2c0aee6e371f8058c50e0a4113f5b5492169e.tar.gz scummvm-rg350-54c2c0aee6e371f8058c50e0a4113f5b5492169e.tar.bz2 scummvm-rg350-54c2c0aee6e371f8058c50e0a4113f5b5492169e.zip |
MUTATIONOFJB: Add support for labels and goto.
Diffstat (limited to 'engines/mutationofjb')
-rw-r--r-- | engines/mutationofjb/commands/gotocommand.cpp | 80 | ||||
-rw-r--r-- | engines/mutationofjb/commands/gotocommand.h | 55 | ||||
-rw-r--r-- | engines/mutationofjb/commands/labelcommand.cpp | 78 | ||||
-rw-r--r-- | engines/mutationofjb/commands/labelcommand.h | 51 | ||||
-rw-r--r-- | engines/mutationofjb/module.mk | 2 | ||||
-rw-r--r-- | engines/mutationofjb/script.cpp | 4 | ||||
-rw-r--r-- | engines/mutationofjb/script.h | 16 |
7 files changed, 283 insertions, 3 deletions
diff --git a/engines/mutationofjb/commands/gotocommand.cpp b/engines/mutationofjb/commands/gotocommand.cpp new file mode 100644 index 0000000000..77c474b653 --- /dev/null +++ b/engines/mutationofjb/commands/gotocommand.cpp @@ -0,0 +1,80 @@ +/* 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/gotocommand.h" +#include "mutationofjb/commands/labelcommand.h" +#include "mutationofjb/game.h" +#include "mutationofjb/script.h" + +/* + GOTO <label> + + Jumps to a label. +*/ + +namespace MutationOfJB { + +bool GotoCommandParser::parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command) { + if (line.firstChar() != '_') { + return false; + } + + Common::String label = line.c_str() + 1; + GotoCommand *gotoCmd = new GotoCommand(); + + if (parseCtx._labels.contains(label)) { + // We already have the label, set it. + gotoCmd->setLabelCommand(parseCtx._labels[label]); + } else { + // Label is after goto, add to pending list. + parseCtx._pendingGotos[label].push_back(gotoCmd); + } + + command = gotoCmd; + + return true; +} + + +void GotoCommand::setLabelCommand(LabelCommand *labelCmd) { + _labelCommand = labelCmd; +} + +Command::ExecuteResult GotoCommand::execute(GameData &) { + // Intentionally empty. + + return Finished; +} + +Command *GotoCommand::next() const { + return _labelCommand; +} + +Common::String GotoCommand::debugString() const { + if (!_labelCommand) { + return "GOTO (null)"; + } + + return Common::String::format("GOTO %s", _labelCommand->getName().c_str()); +} + +} diff --git a/engines/mutationofjb/commands/gotocommand.h b/engines/mutationofjb/commands/gotocommand.h new file mode 100644 index 0000000000..436dd44fb3 --- /dev/null +++ b/engines/mutationofjb/commands/gotocommand.h @@ -0,0 +1,55 @@ +/* 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_GOTOCOMMAND_H +#define MUTATIONOFJB_GOTOCOMMAND_H + +#include "mutationofjb/commands/command.h" +#include "common/str.h" + +namespace MutationOfJB { + +class LabelCommand; + +class GotoCommandParser : public CommandParser { +public: + GotoCommandParser() {} + + virtual bool parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command); +}; + +class GotoCommand : public Command { +public: + GotoCommand() : _labelCommand(nullptr) {} + + void setLabelCommand(LabelCommand *labelCmd); + + virtual ExecuteResult execute(GameData &gameData) override; + virtual Command *next() const override; + virtual Common::String debugString() const override; +private: + LabelCommand *_labelCommand; +}; + +} + +#endif diff --git a/engines/mutationofjb/commands/labelcommand.cpp b/engines/mutationofjb/commands/labelcommand.cpp new file mode 100644 index 0000000000..4540a83aa3 --- /dev/null +++ b/engines/mutationofjb/commands/labelcommand.cpp @@ -0,0 +1,78 @@ +/* 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/labelcommand.h" +#include "mutationofjb/commands/gotocommand.h" +#include "mutationofjb/game.h" +#include "mutationofjb/script.h" + +/* + <label> ":" + + Creates a label. +*/ + +namespace MutationOfJB { + +bool LabelCommandParser::parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command) { + if (line.lastChar() != ':') { + return false; + } + + Common::String label = line; + label.deleteLastChar(); + + LabelCommand *labelCmd = new LabelCommand(label); + if (!parseCtx._labels.contains(line)) { + parseCtx._labels[line] = labelCmd; + } else { + warning("Label '%s' already exists", label.c_str()); + } + + if (parseCtx._pendingGotos.contains(label)) { + GotoCommands &gotos = parseCtx._pendingGotos[label]; + for (GotoCommands::const_iterator it = gotos.begin(); it != gotos.end(); ++it) { + (*it)->setLabelCommand(labelCmd); + } + gotos.clear(); + } + + command = labelCmd; + return true; +} + +const Common::String &LabelCommand::getName() const +{ + return _name; +} + +Command::ExecuteResult LabelCommand::execute(GameData &) { + // Intentionally empty. + + return Finished; +} + +Common::String LabelCommand::debugString() const { + return Common::String::format("LABEL %s", _name.c_str()); +} + +} diff --git a/engines/mutationofjb/commands/labelcommand.h b/engines/mutationofjb/commands/labelcommand.h new file mode 100644 index 0000000000..389c759946 --- /dev/null +++ b/engines/mutationofjb/commands/labelcommand.h @@ -0,0 +1,51 @@ +/* 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_LABELCOMMAND_H +#define MUTATIONOFJB_LABELCOMMAND_H + +#include "mutationofjb/commands/seqcommand.h" +#include "common/str.h" + +namespace MutationOfJB { + +class LabelCommandParser : public SeqCommandParser { +public: + LabelCommandParser() {} + + virtual bool parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command); +}; + +class LabelCommand : public SeqCommand { +public: + LabelCommand(const Common::String &name) : _name(name) {} + const Common::String &getName() const; + + virtual ExecuteResult execute(GameData &gameData) override; + virtual Common::String debugString() const override; +private: + Common::String _name; +}; + +} + +#endif diff --git a/engines/mutationofjb/module.mk b/engines/mutationofjb/module.mk index e66a4c8ee3..8ad1507c3c 100644 --- a/engines/mutationofjb/module.mk +++ b/engines/mutationofjb/module.mk @@ -6,9 +6,11 @@ MODULE_OBJS := \ commands/command.o \ commands/conditionalcommand.o \ commands/endblockcommand.o \ + commands/gotocommand.o \ commands/ifcommand.o \ commands/ifitemcommand.o \ commands/ifpiggycommand.o \ + commands/labelcommand.o \ commands/removeallitemscommand.o \ commands/removeitemcommand.o \ commands/saycommand.o \ diff --git a/engines/mutationofjb/script.cpp b/engines/mutationofjb/script.cpp index 771a9908bf..95c2bdc7ca 100644 --- a/engines/mutationofjb/script.cpp +++ b/engines/mutationofjb/script.cpp @@ -36,6 +36,8 @@ #include "mutationofjb/commands/additemcommand.h" #include "mutationofjb/commands/removeitemcommand.h" #include "mutationofjb/commands/removeallitemscommand.h" +#include "mutationofjb/commands/labelcommand.h" +#include "mutationofjb/commands/gotocommand.h" namespace MutationOfJB { @@ -53,6 +55,8 @@ static CommandParser **getParsers() { new AddItemCommandParser, new RemoveItemCommandParser, new RemoveAllItemsCommandParser, + new GotoCommandParser, + new LabelCommandParser, nullptr }; diff --git a/engines/mutationofjb/script.h b/engines/mutationofjb/script.h index 563dbc74c2..7c5e569702 100644 --- a/engines/mutationofjb/script.h +++ b/engines/mutationofjb/script.h @@ -24,15 +24,19 @@ #define MUTATIONOFJB_SCRIPT_H #include "common/array.h" +#include "common/hashmap.h" +#include "common/hash-str.h" namespace Common { - class SeekableReadStream; - class String; +class SeekableReadStream; +class String; } namespace MutationOfJB { class Command; +class LabelCommand; +class GotoCommand; class ConditionalCommand; typedef Common::Array<Command *> Commands; @@ -53,6 +57,7 @@ struct ActionInfo { }; typedef Common::Array<ActionInfo> ActionInfos; +typedef Common::Array<GotoCommand *> GotoCommands; class ScriptParseContext { @@ -71,9 +76,14 @@ public: char _tag; }; typedef Common::Array<ConditionalCommandInfo> ConditionalCommandInfos; - ConditionalCommandInfos _pendingCondCommands; + typedef Common::HashMap<Common::String, LabelCommand *> LabelMap; + LabelMap _labels; + + typedef Common::HashMap<Common::String, GotoCommands> PendingGotoMap; + PendingGotoMap _pendingGotos; + ActionInfos _actionInfos; private: |