Skip to content

Commit

Permalink
Merge pull request #6047 from Jetz72/code-cleanup
Browse files Browse the repository at this point in the history
Migrate Guava Predicates and Functions to Java implementations
  • Loading branch information
kevlahnota authored Dec 15, 2024
2 parents 01e1a8f + 5a3409a commit 1b493b0
Show file tree
Hide file tree
Showing 585 changed files with 3,715 additions and 4,944 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public class RewardEdit extends FormPanel {
TextListEdit colors =new TextListEdit(new String[] { "White", "Blue", "Black", "Red", "Green" });
TextListEdit rarity =new TextListEdit(new String[] { "Basic Land", "Common", "Uncommon", "Rare", "Mythic Rare" });
TextListEdit subTypes =new TextListEdit();
TextListEdit cardTypes =new TextListEdit(Arrays.asList(CardType.CoreType.values()).stream().map(CardType.CoreType::toString).toArray(String[]::new));
TextListEdit superTypes =new TextListEdit(Arrays.asList(CardType.Supertype.values()).stream().map(CardType.Supertype::toString).toArray(String[]::new));
TextListEdit cardTypes =new TextListEdit(Arrays.stream(CardType.CoreType.values()).map(CardType.CoreType::toString).toArray(String[]::new));
TextListEdit superTypes =new TextListEdit(Arrays.stream(CardType.Supertype.values()).map(CardType.Supertype::toString).toArray(String[]::new));
TextListEdit manaCosts =new TextListEdit();
TextListEdit keyWords =new TextListEdit(Arrays.asList(Keyword.values()).stream().map(Keyword::toString).toArray(String[]::new));
TextListEdit keyWords =new TextListEdit(Arrays.stream(Keyword.values()).map(Keyword::toString).toArray(String[]::new));
JComboBox colorType =new JComboBox(new String[] { "Any", "Colorless", "MultiColor", "MonoColor"});
JTextField cardText =new JTextField();
private boolean updating=false;
Expand Down
27 changes: 13 additions & 14 deletions forge-ai/src/main/java/forge/ai/AiAttackController.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
*/
package forge.ai;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import forge.ai.ability.AnimateAi;
Expand All @@ -42,14 +40,13 @@
import forge.game.trigger.Trigger;
import forge.game.trigger.TriggerType;
import forge.game.zone.ZoneType;
import forge.util.Aggregates;
import forge.util.Expressions;
import forge.util.MyRandom;
import forge.util.*;
import forge.util.collect.FCollection;
import forge.util.collect.FCollectionView;
import org.apache.commons.lang3.tuple.Pair;

import java.util.*;
import java.util.function.Predicate;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -140,7 +137,7 @@ public static List<Card> getOpponentCreatures(final Player defender) {

CardCollection tappedDefenders = new CardCollection();
for (Card c : CardLists.filter(defender.getCardsIn(ZoneType.Battlefield), canAnimate)) {
for (SpellAbility sa : Iterables.filter(c.getSpellAbilities(), SpellAbilityPredicates.isApi(ApiType.Animate))) {
for (SpellAbility sa : IterableUtil.filter(c.getSpellAbilities(), SpellAbilityPredicates.isApi(ApiType.Animate))) {
if (sa.usesTargeting() || !sa.getParamOrDefault("Defined", "Self").equals("Self")) {
continue;
}
Expand All @@ -164,7 +161,7 @@ public static List<Card> getOpponentCreatures(final Player defender) {
defenders.removeAll(tappedDefenders);

// Transform (e.g. Incubator tokens)
for (SpellAbility sa : Iterables.filter(c.getSpellAbilities(), SpellAbilityPredicates.isApi(ApiType.SetState))) {
for (SpellAbility sa : IterableUtil.filter(c.getSpellAbilities(), SpellAbilityPredicates.isApi(ApiType.SetState))) {
Card transformedCopy = ComputerUtilCombat.canTransform(c);
if (transformedCopy.isCreature()) {
int saCMC = sa.getPayCosts() != null && sa.getPayCosts().hasManaCost() ?
Expand Down Expand Up @@ -626,9 +623,9 @@ private boolean doAssault() {
// TODO: the AI should ideally predict how many times it can activate
// for now, unless the opponent is tapped out, break at this point
// and do not predict the blocker limit (which is safer)
if (Iterables.any(defendingOpponent.getLandsInPlay(), CardPredicates.Presets.UNTAPPED)) {
if (defendingOpponent.getLandsInPlay().anyMatch(CardPredicates.UNTAPPED)) {
maxBlockersAfterCrew += CardLists.count(CardLists.getNotType(defendingOpponent.getCardsIn(ZoneType.Battlefield), "Creature"),
Predicates.and(CardPredicates.isType("Vehicle"), CardPredicates.Presets.UNTAPPED));
CardPredicates.isType("Vehicle").and(CardPredicates.UNTAPPED));
}
}

Expand Down Expand Up @@ -1343,7 +1340,7 @@ && isEffectiveAttacker(ai, attacker, combat, finalDefender)) {
if (left.isEmpty() || possibleDefenders.isEmpty()) {
break;
}
CardCollection pwDefending = new CardCollection(Iterables.filter(possibleDefenders, Card.class));
CardCollection pwDefending = new CardCollection(IterableUtil.filter(possibleDefenders, Card.class));
if (pwDefending.isEmpty()) {
// TODO for now only looks at same player as we'd have to check the others from start too
//defender = new PlayerCollection(Iterables.filter(possibleDefenders, Player.class)).min(PlayerPredicates.compareByLife());
Expand Down Expand Up @@ -1393,9 +1390,11 @@ private void calculate(final List<Card> defenders, final Combat combat) {
canTrampleOverDefenders = attacker.hasKeyword(Keyword.TRAMPLE) && attacker.getNetCombatDamage() > Aggregates.sum(validBlockers, Card::getNetToughness);

// used to check that CanKillAllDangerous check makes sense in context where creatures with dangerous abilities are present
dangerousBlockersPresent = Iterables.any(validBlockers, Predicates.or(
CardPredicates.hasKeyword(Keyword.WITHER), CardPredicates.hasKeyword(Keyword.INFECT),
CardPredicates.hasKeyword(Keyword.LIFELINK)));
dangerousBlockersPresent = validBlockers.anyMatch(
CardPredicates.hasKeyword(Keyword.WITHER)
.or(CardPredicates.hasKeyword(Keyword.INFECT))
.or(CardPredicates.hasKeyword(Keyword.LIFELINK))
);

// total power of the defending creatures, used in predicting whether a gang block can kill the attacker
defPower = CardLists.getTotalPower(validBlockers, true, false);
Expand Down Expand Up @@ -1616,7 +1615,7 @@ public static List<Card> exertAttackers(final List<Card> attackers, int aggressi
if (validTargets.isEmpty()) {
missTarget = true;
break;
} else if (sa.isCurse() && !Iterables.any(validTargets,
} else if (sa.isCurse() && validTargets.stream().noneMatch(
CardPredicates.isControlledByAnyOf(c.getController().getOpponents()))) {
// e.g. Ahn-Crop Crasher - the effect is only good when aimed at opponent's creatures
missTarget = true;
Expand Down
18 changes: 8 additions & 10 deletions forge-ai/src/main/java/forge/ai/AiBlockController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
package forge.ai;

import java.util.*;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import java.util.function.Predicate;

import forge.card.CardStateName;
import forge.game.GameEntity;
Expand Down Expand Up @@ -327,7 +325,7 @@ private void makeGoodBlocks(final Combat combat) {
}

private Predicate<Card> rampagesOrNeedsManyToBlock(final Combat combat) {
return Predicates.or(CardPredicates.hasKeyword(Keyword.RAMPAGE), input -> {
return CardPredicates.hasKeyword(Keyword.RAMPAGE).or(input -> {
// select creature that has a max blocker
return StaticAbilityCantAttackBlock.getMinMaxBlocker(input, combat.getDefenderPlayerByAttacker(input)).getRight() < Integer.MAX_VALUE;
});
Expand Down Expand Up @@ -368,7 +366,7 @@ private Predicate<Card> changesPTWhenBlocked(final boolean onlyForDefVsTrample)
* @param combat a {@link forge.game.combat.Combat} object.
*/
private void makeGangBlocks(final Combat combat) {
List<Card> currentAttackers = CardLists.filter(attackersLeft, Predicates.not(rampagesOrNeedsManyToBlock(combat)));
List<Card> currentAttackers = CardLists.filter(attackersLeft, rampagesOrNeedsManyToBlock(combat).negate());
List<Card> blockers;

// Try to block an attacker without first strike with a gang of first strikers
Expand Down Expand Up @@ -740,11 +738,11 @@ private void reinforceBlockersAgainstTrample(final Combat combat) {
List<Card> chumpBlockers;

List<Card> tramplingAttackers = CardLists.getKeyword(attackers, Keyword.TRAMPLE);
tramplingAttackers = CardLists.filter(tramplingAttackers, Predicates.not(rampagesOrNeedsManyToBlock(combat)));
tramplingAttackers = CardLists.filter(tramplingAttackers, rampagesOrNeedsManyToBlock(combat).negate());

// TODO - Instead of filtering out rampage-like and similar triggers, make the AI properly count P/T and
// reinforce when actually possible without losing material.
tramplingAttackers = CardLists.filter(tramplingAttackers, Predicates.not(changesPTWhenBlocked(true)));
tramplingAttackers = CardLists.filter(tramplingAttackers, changesPTWhenBlocked(true).negate());

for (final Card attacker : tramplingAttackers) {
if (CombatUtil.getMinNumBlockersForAttacker(attacker, combat.getDefenderPlayerByAttacker(attacker)) > combat.getBlockers(attacker).size()) {
Expand Down Expand Up @@ -795,11 +793,11 @@ private void reinforceBlockersAgainstTrample(final Combat combat) {
private void reinforceBlockersToKill(final Combat combat) {
List<Card> safeBlockers;
List<Card> blockers;
List<Card> targetAttackers = CardLists.filter(blockedButUnkilled, Predicates.not(rampagesOrNeedsManyToBlock(combat)));
List<Card> targetAttackers = CardLists.filter(blockedButUnkilled, rampagesOrNeedsManyToBlock(combat).negate());

// TODO - Instead of filtering out rampage-like and similar triggers, make the AI properly count P/T and
// reinforce when actually possible without losing material.
targetAttackers = CardLists.filter(targetAttackers, Predicates.not(changesPTWhenBlocked(false)));
targetAttackers = CardLists.filter(targetAttackers, changesPTWhenBlocked(false).negate());

for (final Card attacker : targetAttackers) {
blockers = getPossibleBlockers(combat, attacker, blockersLeft, false);
Expand Down Expand Up @@ -1345,7 +1343,7 @@ private boolean wouldLikeToRandomlyTrade(Card attacker, Card blocker, Combat com
boolean creatureParityOrAllowedDiff = aiCreatureCount
+ (randomTradeIfBehindOnBoard ? maxCreatDiff : 0) >= oppCreatureCount;
boolean wantToTradeWithCreatInHand = !checkingOther && randomTradeIfCreatInHand
&& ai.getZone(ZoneType.Hand).contains(CardPredicates.Presets.CREATURES)
&& ai.getZone(ZoneType.Hand).contains(CardPredicates.CREATURES)
&& aiCreatureCount + maxCreatDiffWithRepl >= oppCreatureCount;
boolean wantToSavePlaneswalker = MyRandom.percentTrue(chanceToSavePW)
&& combat.getDefenderByAttacker(attacker) instanceof Card
Expand Down
1 change: 1 addition & 0 deletions forge-ai/src/main/java/forge/ai/AiCardMemory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import forge.game.card.Card;
import forge.game.player.Player;


/**
* <p>
* AiCardMemory class.
Expand Down
Loading

0 comments on commit 1b493b0

Please sign in to comment.