package mage.abilities.effects; import java.util.ArrayList; import mage.abilities.Ability; import mage.abilities.Mode; import mage.constants.Outcome; import mage.target.targetpointer.TargetPointer; /** * @author BetaSteward_at_googlemail.com */ public class Effects extends ArrayList { public Effects(Effect... effects) { for (Effect effect : effects) { this.add(effect); } } public Effects(final Effects effects) { for (Effect effect : effects) { this.add(effect.copy()); } } public Effects copy() { return new Effects(this); } public String getTextStartingUpperCase(Mode mode) { String text = getText(mode); if (text.length() > 3) { return Character.toUpperCase(text.charAt(0)) + text.substring(1); } return text; } public String getText(Mode mode) { StringBuilder sbText = new StringBuilder(); String lastRule = null; int effectNum = 0; for (Effect effect : this) { String endString = ""; String nextRule = effect.getText(mode); // ignore empty rules if (nextRule == null || nextRule.isEmpty()) { continue; } effectNum++; // concat effects (default: each effect with a new sentence) String concatPrefix = effect.getConcatPrefix(); if (effectNum > 1 && !concatPrefix.isEmpty() && !concatPrefix.equals(".")) { nextRule = concatPrefix + " " + nextRule; } if (nextRule != null) { if (nextRule.startsWith("and ") || nextRule.startsWith("with ") || nextRule.startsWith("then ")) { endString = " "; } else if (nextRule.startsWith(",") || nextRule.startsWith(" ")) { endString = ""; } else if (lastRule != null && lastRule.length() > 3) { if (!lastRule.endsWith(".") && !lastRule.endsWith("
")) { endString = ". "; } if (nextRule.length() > 3) { nextRule = Character.toUpperCase(nextRule.charAt(0)) + nextRule.substring(1); } } String currentRule = endString + nextRule; // fix dot in the combined effect like IfDoCost if (sbText.length() > 0 && currentRule.length() > 0) { boolean prevTextEndsWithDot = sbText.charAt(sbText.length() - 1) == '.'; boolean currentTextStartsWithDot = currentRule.startsWith(",") || currentRule.startsWith("."); if (prevTextEndsWithDot && currentTextStartsWithDot) { sbText.delete(sbText.length() - 1, sbText.length()); } } sbText.append(currentRule); } lastRule = nextRule; } if (lastRule != null && lastRule.length() > 3 && !lastRule.endsWith(".") && !lastRule.endsWith("\"") && !lastRule.startsWith("Level ") && !lastRule.endsWith(".)") && !lastRule.endsWith("")) { sbText.append('.'); } return sbText.toString(); } public boolean hasOutcome(Ability source, Outcome outcome) { Outcome realOutcome = (source == null ? null : source.getCustomOutcome()); if (realOutcome != null) { return realOutcome == outcome; } for (Effect effect : this) { if (effect.getOutcome() == outcome) { return true; } } return false; } /** * @param source source ability for effects * @return real outcome of ability */ public Outcome getOutcome(Ability source) { return getOutcome(source, Outcome.Detriment); } public Outcome getOutcome(Ability source, Outcome defaultOutcome) { Outcome realOutcome = (source == null ? null : source.getCustomOutcome()); if (realOutcome != null) { return realOutcome; } if (!this.isEmpty()) { return this.get(0).getOutcome(); } return defaultOutcome; } /** * @param source source ability for effects * @return total score of outcome effects (plus/minus) */ public int getOutcomeScore(Ability source) { int total = 0; for (Effect effect : this) { // custom ability outcome must "rewrite" effect's outcome (it uses for AI desisions and card score... hmm, getOutcomeTotal used on 28.01.2020) Outcome realOutcome = (source == null ? null : source.getCustomOutcome()); if (realOutcome == null) { realOutcome = effect.getOutcome(); } if (realOutcome.isGood()) { total++; } else { total--; } } return total; } public void newId() { for (Effect effect : this) { effect.newId(); } } public void setTargetPointer(TargetPointer targetPointer) { if (targetPointer == null) { return; } for (Effect effect : this) { effect.setTargetPointer(targetPointer.copy()); } } }