Permalink
Browse files

MUTATIONOFJB: Add support for IFITEM command and fix parsing conditio…

…nal commands that are right after #ELSE.
  • Loading branch information...
LubomirR committed Mar 10, 2018
1 parent 31c03e9 commit 64488ee796cb32e01f041bcedf10abdf3960e450
@@ -21,10 +21,24 @@
*/

#include "mutationofjb/commands/conditionalcommand.h"
#include "mutationofjb/script.h"
#include "common/scummsys.h"
#include "common/translation.h"

namespace MutationOfJB {

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

ConditionalCommand *const condCommand = static_cast<ConditionalCommand *>(oldCommand);
parseContext.addConditionalCommand(condCommand, _lastTag);
condCommand->setTrueCommand(newCommand);
}


ConditionalCommand::ConditionalCommand() :
_trueCommand(nullptr),
_falseCommand(nullptr),
@@ -28,6 +28,13 @@

namespace MutationOfJB {

class ConditionalCommandParser : public CommandParser {
public:
virtual void transition(ScriptParseContext &parseCtx, Command *oldCommand, Command *newCommand, CommandParser *newCommandParser);
protected:
char _lastTag;
};

class ConditionalCommand : public Command {
public:
ConditionalCommand();
@@ -31,12 +31,12 @@
IF command compares the value of the WX pseudo-register of the object in the specified scene.
If the values match, execution continues to the next line.
Otherwise execution continues after first "#ELSE" with the same <tag>.
Otherwise execution continues after first "#ELSE" or "=ELSE" with the same <tag>.
The logic can be reversed with exclamation mark at the end.
<tag> is always 1 character long, <sceneId> and <objectId> 2 characters long.
Please note that this does not work line you are used to from saner languages.
Please note that this does not work like you are used to from saner languages.
IF does not have any blocks. It only searches for first #ELSE, so you can have stuff like:
IF something
IF something else
@@ -70,20 +70,11 @@ bool IfCommandParser::parse(const Common::String &line, ScriptParseContext &pars
const uint8 value = atoi(cstr + 9);
const bool negative = (line.lastChar() == '!');

IfCommand *ifCommand = new IfCommand(sceneId, objectId, value, negative);
_lastTag = tag;

command = ifCommand;
parseContext.addConditionalCommand(ifCommand, tag);
return true;
}
command = new IfCommand(sceneId, objectId, value, negative);

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

static_cast<IfCommand *>(oldCommand)->setTrueCommand(newCommand);
return true;
}

IfCommand::IfCommand(uint8 sceneId, uint8 objectId, uint16 value, bool negative) :
@@ -30,16 +30,14 @@ namespace MutationOfJB {

class ScriptParseContext;

class IfCommandParser : public CommandParser {
class IfCommandParser : public ConditionalCommandParser {
public:
virtual bool parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command);
virtual void transition(ScriptParseContext &parseCtx, Command *oldCommand, Command *newCommand, CommandParser *newCommandParser);
private:
};

class IfCommand : public ConditionalCommand {
public:
static bool ParseFunc(const Common::String &line, ScriptParseContext &parseContext, Command *&command);

IfCommand(uint8 sceneId, uint8 objectId, uint16 value, bool negative);

virtual ExecuteResult execute(GameData &gameData) override;
@@ -50,8 +48,6 @@ class IfCommand : public ConditionalCommand {
uint8 _objectId;
uint16 _value;
bool _negative;

bool _cachedResult;
};

}
@@ -0,0 +1,90 @@
/* 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/ifitemcommand.h"
#include "mutationofjb/game.h"
#include "mutationofjb/script.h"
#include "mutationofjb/util.h"
#include "common/str.h"
#include "common/translation.h"

/*
"IFITEM" <item> ["!"]
IFITEM command tests whether an item is in the inventory.
If it is, execution continues to the next line.
Otherwise execution continues after first "#ELSE" or "=ELSE".
The logic can be reversed with exclamation mark at the end.
Please note that this does not work like you are used to from saner languages.
IFITEM does not have any blocks. It only searches for first #ELSE, so you can have stuff like:
IFITEM item1
IFITEM item2
#ELSE
...
This is effectively logical AND.
*/

namespace MutationOfJB {

bool IfItemCommandParser::parse(const Common::String &line, ScriptParseContext &parseContext, Command *&command) {
if (line.size() < 8) {
return false;
}

if (!line.hasPrefix("IFITEM")) {
return false;
}

const bool negative = (line.lastChar() == '!');
Common::String item(line.c_str() + 7);
if (negative) {
item.deleteLastChar(); // Remove '!'.
}

_lastTag = 0;
command = new IfItemCommand(item, negative);

return true;
}


IfItemCommand::IfItemCommand(const Common::String &item, bool negative) :
_item(item),
_negative(negative)
{}

Command::ExecuteResult IfItemCommand::execute(GameData &gameData) {
_cachedResult = gameData._inventory.hasItem(_item);
if (_negative) {
_cachedResult = !_cachedResult;
}

return Finished;
}

Common::String IfItemCommand::debugString() const {
return Common::String::format("IFITEM %s%s", _negative ? "NOT " : "", _item.c_str());
}

}

@@ -0,0 +1,53 @@
/* 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_IFITEMCOMMAND_H
#define MUTATIONOFJB_IFITEMCOMMAND_H

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

namespace MutationOfJB {

class ScriptParseContext;

class IfItemCommandParser : public ConditionalCommandParser {
public:
virtual bool parse(const Common::String &line, ScriptParseContext &parseCtx, Command *&command);
};

class IfItemCommand : public ConditionalCommand {
public:
IfItemCommand(const Common::String &item, bool negative);

virtual ExecuteResult execute(GameData &gameData) override;
virtual Common::String debugString() const;

private:
Common::String _item;
bool _negative;
};

}

#endif
@@ -115,7 +115,7 @@ void Console::showIndent(int indentLevel) {
void Console::showCommands(Command *command, int indentLevel) {
while (command) {
showIndent(indentLevel);
debugPrintf("%s\n", command->debugString().c_str());
debugPrintf("%s\n", convertToASCII(command->debugString()).c_str());

if (SeqCommand *const seqCmd = dynamic_cast<SeqCommand *>(command)) {
command = seqCmd->next();
@@ -32,6 +32,11 @@ const Inventory::Items &Inventory::getItems() const {
return _items;
}

bool Inventory::hasItem(const Common::String &item) const {
Items::const_iterator it = find(_items.begin(), _items.end(), item);
return (it != _items.end());
}

void Inventory::addItem(const Common::String &item) {
_items.push_back(item);

@@ -34,6 +34,7 @@ class Inventory {
typedef Common::Array<Common::String> Items;

const Items &getItems() const;
bool hasItem(const Common::String &item) const;
void addItem(const Common::String &item);
void removeItem(const Common::String &item);
void removeAllItems();
@@ -7,6 +7,7 @@ MODULE_OBJS := \
commands/conditionalcommand.o \
commands/endblockcommand.o \
commands/ifcommand.o \
commands/ifitemcommand.o \
commands/removeallitemscommand.o \
commands/removeitemcommand.o \
commands/saycommand.o \
@@ -28,6 +28,7 @@
#include "common/debug.h"
#include "mutationofjb/commands/command.h"
#include "mutationofjb/commands/ifcommand.h"
#include "mutationofjb/commands/ifitemcommand.h"
#include "mutationofjb/commands/endblockcommand.h"
#include "mutationofjb/commands/changecommand.h"
#include "mutationofjb/commands/saycommand.h"
@@ -39,6 +40,7 @@ namespace MutationOfJB {

static CommandParser **getParsers() {
static CommandParser *parsers[] = {
new IfItemCommandParser,
new IfCommandParser,
new EndBlockCommandParser,
new ChangeDoorCommandParser,

0 comments on commit 64488ee

Please sign in to comment.