/*
* This file is part of the Illarion project.
*
* Copyright © 2014 - Illarion e.V.
*
* Illarion is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Illarion 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.
*/
package illarion.easynpc.parsed.talk.conditions;
import illarion.common.data.Skill;
import illarion.easynpc.data.CompareOperators;
import illarion.easynpc.parsed.talk.AdvancedNumber;
import illarion.easynpc.parsed.talk.TalkCondition;
import illarion.easynpc.writer.LuaRequireTable;
import illarion.easynpc.writer.LuaWriter;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.Writer;
/**
* This class is used to store all required values for the skill condition.
*
* @author Martin Karing <nitram@illarion.org>
*/
public final class ConditionSkill implements TalkCondition {
/**
* The LUA code needed for this consequence to work.
*/
private static final String LUA_CODE =
"talkEntry:addCondition(%1$s(Character.%2$s, \"%3$s\", %4$s))" + LuaWriter.NL;
/**
* The LUA module required for this condition to work.
*/
private static final String LUA_MODULE = BASE_LUA_MODULE + "skill";
/**
* The operator that is used to compare the skill value.
*/
private final CompareOperators operator;
/**
* The skill that is checked by this skill condition.
*/
private final Skill skill;
/**
* The value of the skill that is used for the compare operation.
*/
private final AdvancedNumber value;
/**
* Constructor that allows setting the parameters of this skill test.
*
* @param skillData the skill used for this condition
* @param op the compare operator used for the condition
* @param newValue the value used to compare against
*/
public ConditionSkill(Skill skillData, CompareOperators op, AdvancedNumber newValue) {
skill = skillData;
operator = op;
value = newValue;
}
/**
* Get the LUA module needed for this condition.
*/
@Nonnull
@Override
public String getLuaModule() {
return LUA_MODULE;
}
/**
* Write the LUA code needed for this race condition.
*/
@Override
public void writeLua(@Nonnull Writer target, @Nonnull LuaRequireTable requires) throws IOException {
target.write(String.format(LUA_CODE, requires.getStorage(LUA_MODULE), skill.getName(), operator.getLuaComp(),
value.getLua()));
}
}