Search in sources :

Example 16 with Mode

use of mage.abilities.Mode in project mage by magefree.

the class ExportJsonGameplayDataTest method exportTest.

@Test
@Ignore
public /**
 * It's export code example for https://github.com/mtgjson/mtgjson4
 */
void exportTest() {
    List<Card> cards = new ArrayList<>();
    Collection<ExpansionSet> sets = Sets.getInstance().values();
    for (ExpansionSet set : sets) {
        for (ExpansionSet.SetCardInfo setInfo : set.getSetCardInfo()) {
            // catch cards creation errors and report (e.g. on wrong card code)
            try {
                Card card = CardImpl.createCard(setInfo.getCardClass(), new CardSetInfo(setInfo.getName(), set.getCode(), setInfo.getCardNumber(), setInfo.getRarity(), setInfo.getGraphicInfo()));
                if (card != null) {
                    cards.add(card);
                }
            } catch (Throwable e) {
                logger.error("Can't create card " + setInfo.getName() + ": " + e.getMessage(), e);
            }
        }
        JsonObject res = new JsonObject();
        for (Card card : cards) {
            try {
                JsonObject resCard = new JsonObject();
                res.add(card.getName(), resCard);
                JsonArray resAbilities = new JsonArray();
                resCard.add("abilities", resAbilities);
                for (Ability ability : card.getAbilities()) {
                    JsonObject resAbility = new JsonObject();
                    resAbilities.add(resAbility);
                    // basic
                    resAbility.addProperty("cost", ability.getManaCosts().getText());
                    resAbility.addProperty("name", ability.toString());
                    resAbility.addProperty("class", ability.getClass().getSimpleName());
                    // resAbility.addProperty("rule", ability.getRule());
                    // modes
                    JsonArray resModes = new JsonArray();
                    resAbility.add("modes", resModes);
                    for (Mode mode : ability.getModes().values()) {
                        JsonObject resMode = new JsonObject();
                        resModes.add(resMode);
                        // basic
                        // resMode.addProperty("name", mode.toString());
                        // effects
                        JsonArray resEffects = new JsonArray();
                        resMode.add("effects", resEffects);
                        for (Effect effect : mode.getEffects()) {
                            JsonObject resEffect = new JsonObject();
                            resEffects.add(resEffect);
                            resEffect.addProperty("class", effect.getClass().getSimpleName());
                            resEffect.addProperty("outcome", effect.getOutcome().toString());
                            resEffect.addProperty("text", effect.getText(mode));
                        }
                        if (resEffects.size() == 0) {
                            resMode.remove("effects");
                        }
                        // targets
                        JsonArray resTargets = new JsonArray();
                        resMode.add("targets", resTargets);
                        for (Target target : mode.getTargets()) {
                            JsonObject resTarget = new JsonObject();
                            resTargets.add(resTarget);
                            resTarget.addProperty("name", target.getTargetName());
                            resTarget.addProperty("class", target.getClass().getSimpleName());
                            resTarget.addProperty("min", target.getMinNumberOfTargets());
                            resTarget.addProperty("max", target.getMaxNumberOfTargets());
                        }
                        if (resTargets.size() == 0) {
                            resMode.remove("targets");
                        }
                        if (resMode.get("effects") == null && resMode.get("targets") == null) {
                            resModes.remove(resMode);
                        }
                    }
                    if (resModes.size() == 0) {
                        resAbility.remove("modes");
                    }
                }
            } catch (Throwable e) {
                logger.error("Inner error for " + card.getName() + ": " + e.getMessage(), e);
                break;
            }
        }
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        if (MTGJSON_WRITE_TO_FILES) {
            String filePath = System.getProperty("user.dir") + "/json/" + set.getCode() + ".json";
            File outputFile = new File(filePath);
            final boolean mkdirs = outputFile.getParentFile().mkdirs();
            try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile, false), StandardCharsets.UTF_8))) {
                writer.write(gson.toJson(res));
                System.out.println("Wrote " + set.getCode() + " to file");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
        // System.out.println(gson.toJson(res));
        }
    }
}
Also used : Ability(mage.abilities.Ability) GsonBuilder(com.google.gson.GsonBuilder) Mode(mage.abilities.Mode) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) JsonArray(com.google.gson.JsonArray) Target(mage.target.Target) Effect(mage.abilities.effects.Effect) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with Mode

use of mage.abilities.Mode in project mage by magefree.

the class ReflectiveGolemTriggeredAbility method checkTrigger.

@Override
public boolean checkTrigger(GameEvent event, Game game) {
    if (!isControlledBy(event.getPlayerId())) {
        return false;
    }
    Spell spell = game.getSpellOrLKIStack(event.getTargetId());
    if (spell == null || !spell.isInstantOrSorcery(game)) {
        return false;
    }
    if (spell.getSpellAbilities().stream().map(AbilityImpl::getModes).flatMap(m -> m.getSelectedModes().stream().map(m::get)).filter(Objects::nonNull).map(Mode::getTargets).flatMap(Collection::stream).filter(t -> !t.isNotTarget()).map(Target::getTargets).flatMap(Collection::stream).filter(Objects::nonNull).distinct().filter(getSourceId()::equals).count() != 1) {
        return false;
    }
    this.getEffects().setTargetPointer(new FixedTarget(spell, game));
    return true;
}
Also used : Target(mage.target.Target) Zone(mage.constants.Zone) CopyTargetSpellEffect(mage.abilities.effects.common.CopyTargetSpellEffect) DoIfCostPaid(mage.abilities.effects.common.DoIfCostPaid) Collection(java.util.Collection) UUID(java.util.UUID) MageInt(mage.MageInt) SubType(mage.constants.SubType) Mode(mage.abilities.Mode) FixedTarget(mage.target.targetpointer.FixedTarget) CardSetInfo(mage.cards.CardSetInfo) Objects(java.util.Objects) TriggeredAbilityImpl(mage.abilities.TriggeredAbilityImpl) Game(mage.game.Game) AbilityImpl(mage.abilities.AbilityImpl) GenericManaCost(mage.abilities.costs.mana.GenericManaCost) GameEvent(mage.game.events.GameEvent) CardImpl(mage.cards.CardImpl) CardType(mage.constants.CardType) Spell(mage.game.stack.Spell) FixedTarget(mage.target.targetpointer.FixedTarget) TriggeredAbilityImpl(mage.abilities.TriggeredAbilityImpl) AbilityImpl(mage.abilities.AbilityImpl) Target(mage.target.Target) FixedTarget(mage.target.targetpointer.FixedTarget) Objects(java.util.Objects) Collection(java.util.Collection) Spell(mage.game.stack.Spell)

Example 18 with Mode

use of mage.abilities.Mode in project mage by magefree.

the class ChangeATargetOfTargetSpellAbilityToSourceEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    StackObject stackObject = game.getStack().getStackObject(source.getFirstTarget());
    MageObject sourceObject = game.getObject(source.getSourceId());
    if (stackObject != null && sourceObject != null) {
        Targets targets = new Targets();
        Ability sourceAbility;
        String oldTargetName = null;
        if (stackObject instanceof Spell) {
            Spell spell = (Spell) stackObject;
            sourceAbility = spell.getSpellAbility();
        } else if (stackObject instanceof StackAbility) {
            StackAbility stackAbility = (StackAbility) stackObject;
            sourceAbility = stackAbility;
        } else {
            return false;
        }
        for (UUID modeId : sourceAbility.getModes().getSelectedModes()) {
            Mode mode = sourceAbility.getModes().get(modeId);
            targets.addAll(mode.getTargets());
        }
        boolean twoTimesTarget = false;
        if (targets.size() == 1 && targets.get(0).getTargets().size() == 1) {
            Target target = targets.get(0);
            if (target.getFirstTarget().equals(source.getSourceId())) {
                // Target was already the same source, so no change / target event to create
                return true;
            }
            if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
                oldTargetName = getTargetName(targets.getFirstTarget(), game);
                target.clearChosen();
                // The source is still the spell on the stack
                target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
            }
        } else {
            Player controller = game.getPlayer(source.getControllerId());
            boolean validTargets = false;
            do {
                for (Target target : targets) {
                    for (UUID targetId : target.getTargets()) {
                        String name = getTargetName(targets.getFirstTarget(), game);
                        if (!targetId.equals(source.getSourceId()) && target.getTargets().contains(source.getSourceId())) {
                            // you can't change this target to source because the source is already another targetId of that target.
                            twoTimesTarget = true;
                            continue;
                        }
                        if (target.canTarget(stackObject.getControllerId(), source.getSourceId(), sourceAbility, game)) {
                            validTargets = true;
                            if (name != null && controller.chooseUse(Outcome.Neutral, "Change target from " + name + " to " + sourceObject.getLogName() + '?', source, game)) {
                                oldTargetName = getTargetName(targetId, game);
                                target.remove(targetId);
                                // The source is still the spell on the stack
                                target.addTarget(source.getSourceId(), stackObject.getStackAbility(), game);
                                break;
                            }
                        }
                    }
                    if (oldTargetName != null) {
                        break;
                    }
                }
                if (oldTargetName == null) {
                    game.informPlayer(controller, "You have to select at least one target to change to " + sourceObject.getIdName() + '!');
                }
            } while (validTargets && oldTargetName == null);
        }
        if (oldTargetName != null) {
            game.informPlayers(sourceObject.getLogName() + ": Changed target of " + stackObject.getLogName() + " from " + oldTargetName + " to " + sourceObject.getLogName());
        } else if (twoTimesTarget) {
            game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its not valid to target it twice for " + stackObject.getLogName());
        } else {
            game.informPlayers(sourceObject.getLogName() + ": Target not changed to " + sourceObject.getLogName() + " because its no valid target for " + stackObject.getLogName());
        }
        return true;
    }
    return false;
}
Also used : StackAbility(mage.game.stack.StackAbility) Ability(mage.abilities.Ability) Target(mage.target.Target) Player(mage.players.Player) Mode(mage.abilities.Mode) StackObject(mage.game.stack.StackObject) MageObject(mage.MageObject) Targets(mage.target.Targets) UUID(java.util.UUID) Spell(mage.game.stack.Spell) StackAbility(mage.game.stack.StackAbility)

Example 19 with Mode

use of mage.abilities.Mode in project mage by magefree.

the class StackAbilityView method updateTargets.

private void updateTargets(Game game, StackAbility ability) {
    List<String> names = new ArrayList<>();
    for (UUID modeId : ability.getModes().getSelectedModes()) {
        Mode mode = ability.getModes().get(modeId);
        if (!mode.getTargets().isEmpty()) {
            addTargets(mode.getTargets(), mode.getEffects(), ability, game);
        } else {
            List<UUID> targetList = new ArrayList<>();
            for (Effect effect : mode.getEffects()) {
                TargetPointer targetPointer = effect.getTargetPointer();
                if (targetPointer instanceof FixedTarget) {
                    targetList.add(((FixedTarget) targetPointer).getTarget());
                }
            }
            if (!targetList.isEmpty()) {
                overrideTargets(targetList);
                for (UUID uuid : targetList) {
                    MageObject mageObject = game.getObject(uuid);
                    if (mageObject != null) {
                        if ((mageObject instanceof Card) && ((Card) mageObject).isFaceDown(game)) {
                            continue;
                        }
                        String newName = GameLog.getColoredObjectIdNameForTooltip(mageObject);
                        if (!names.contains(newName)) {
                            names.add(newName);
                        }
                    }
                }
            }
        }
    }
    if (!names.isEmpty()) {
        getRules().add("<i>Related objects: " + names + "</i>");
    }
    // show for modal ability, which mode was choosen
    if (ability.isModal()) {
        Modes modes = ability.getModes();
        for (UUID modeId : modes.getSelectedModes()) {
            Mode mode = modes.get(modeId);
            this.rules.add("<span color='green'><i>Chosen mode: " + mode.getEffects().getText(mode) + "</i></span>");
        }
    }
    if (HintUtils.ABILITY_HINTS_ENABLE) {
        List<String> abilityHints = new ArrayList<>();
        for (Hint hint : ability.getHints()) {
            abilityHints.add(hint.getText(game, ability));
        }
        // total hints
        if (!abilityHints.isEmpty()) {
            rules.add(HintUtils.HINT_START_MARK);
            HintUtils.appendHints(rules, abilityHints);
        }
    }
}
Also used : FixedTarget(mage.target.targetpointer.FixedTarget) Modes(mage.abilities.Modes) Hint(mage.abilities.hint.Hint) Mode(mage.abilities.Mode) ArrayList(java.util.ArrayList) MageObject(mage.MageObject) Card(mage.cards.Card) Effect(mage.abilities.effects.Effect) UUID(java.util.UUID) TargetPointer(mage.target.targetpointer.TargetPointer)

Aggregations

Mode (mage.abilities.Mode)19 Target (mage.target.Target)16 UUID (java.util.UUID)11 Player (mage.players.Player)10 Spell (mage.game.stack.Spell)8 Ability (mage.abilities.Ability)7 StackObject (mage.game.stack.StackObject)7 Permanent (mage.game.permanent.Permanent)6 Effect (mage.abilities.effects.Effect)5 MageObject (mage.MageObject)4 MageObjectReference (mage.MageObjectReference)4 FixedTarget (mage.target.targetpointer.FixedTarget)4 ArrayList (java.util.ArrayList)3 MageInt (mage.MageInt)3 CardImpl (mage.cards.CardImpl)3 CardSetInfo (mage.cards.CardSetInfo)3 CardType (mage.constants.CardType)3 SubType (mage.constants.SubType)3 java.util (java.util)2 AbilityImpl (mage.abilities.AbilityImpl)2