diff --git a/Mage.Sets/src/mage/cards/s/ScholarshipSponsor.java b/Mage.Sets/src/mage/cards/s/ScholarshipSponsor.java new file mode 100644 index 0000000000..b2dbf27f6b --- /dev/null +++ b/Mage.Sets/src/mage/cards/s/ScholarshipSponsor.java @@ -0,0 +1,108 @@ +package mage.cards.s; + +import mage.MageInt; +import mage.abilities.Ability; +import mage.abilities.common.EntersBattlefieldTriggeredAbility; +import mage.abilities.effects.OneShotEffect; +import mage.cards.CardImpl; +import mage.cards.CardSetInfo; +import mage.cards.Cards; +import mage.cards.CardsImpl; +import mage.constants.CardType; +import mage.constants.Outcome; +import mage.constants.SubType; +import mage.constants.Zone; +import mage.filter.StaticFilters; +import mage.game.Controllable; +import mage.game.Game; +import mage.players.Player; +import mage.target.common.TargetCardInLibrary; + +import java.util.Map; +import java.util.UUID; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * @author TheElk801 + */ +public final class ScholarshipSponsor extends CardImpl { + + public ScholarshipSponsor(UUID ownerId, CardSetInfo setInfo) { + super(ownerId, setInfo, new CardType[]{CardType.CREATURE}, "{3}{W}"); + + this.subtype.add(SubType.HUMAN); + this.subtype.add(SubType.ADVISOR); + this.power = new MageInt(3); + this.toughness = new MageInt(3); + + // When Scholarship Sponsor enters the battlefield, each player who controls fewer lands than the player who controls the most lands searches their library for a number of basic land cards less than or equal to the difference, puts those cards onto the battlefield tapped, then shuffles. + this.addAbility(new EntersBattlefieldTriggeredAbility(new ScholarshipSponsorEffect())); + } + + private ScholarshipSponsor(final ScholarshipSponsor card) { + super(card); + } + + @Override + public ScholarshipSponsor copy() { + return new ScholarshipSponsor(this); + } +} + +class ScholarshipSponsorEffect extends OneShotEffect { + + ScholarshipSponsorEffect() { + super(Outcome.Benefit); + staticText = "each player who controls fewer lands than the player who controls the most lands " + + "searches their library for a number of basic land cards less than or equal to the difference, " + + "puts those cards onto the battlefield tapped, then shuffles"; + } + + private ScholarshipSponsorEffect(final ScholarshipSponsorEffect effect) { + super(effect); + } + + @Override + public ScholarshipSponsorEffect copy() { + return new ScholarshipSponsorEffect(this); + } + + @Override + public boolean apply(Game game, Ability source) { + Map playerMap = game + .getBattlefield() + .getActivePermanents( + StaticFilters.FILTER_LAND, source.getControllerId(), source.getSourceId(), game + ) + .stream() + .map(Controllable::getControllerId) + .collect(Collectors.toMap(Function.identity(), x -> 1, Integer::sum)); + int maxValue = playerMap + .values() + .stream() + .mapToInt(x -> x) + .max() + .orElse(0); + if (maxValue < 1) { + return false; + } + for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) { + Player player = game.getPlayer(playerId); + int diff = maxValue - playerMap.getOrDefault(playerId, 0); + if (player == null || diff < 1) { + continue; + } + TargetCardInLibrary target = new TargetCardInLibrary(diff, StaticFilters.FILTER_CARD_BASIC_LAND); + player.searchLibrary(target, source, game); + Cards cards = new CardsImpl(target.getTargets()); + cards.retainZone(Zone.LIBRARY, game); + player.moveCards( + cards.getCards(game), Zone.BATTLEFIELD, source, game, + true, false, false, null + ); + player.shuffleLibrary(source, game); + } + return true; + } +} diff --git a/Mage.Sets/src/mage/sets/Commander2021Edition.java b/Mage.Sets/src/mage/sets/Commander2021Edition.java index f501048c16..9138942e87 100644 --- a/Mage.Sets/src/mage/sets/Commander2021Edition.java +++ b/Mage.Sets/src/mage/sets/Commander2021Edition.java @@ -247,6 +247,7 @@ public final class Commander2021Edition extends ExpansionSet { cards.add(new SetCardInfo("Sapling of Colfenor", 228, Rarity.RARE, mage.cards.s.SaplingOfColfenor.class)); cards.add(new SetCardInfo("Sapseep Forest", 313, Rarity.UNCOMMON, mage.cards.s.SapseepForest.class)); cards.add(new SetCardInfo("Scavenger Grounds", 314, Rarity.RARE, mage.cards.s.ScavengerGrounds.class)); + cards.add(new SetCardInfo("Scholarship Sponsor", 22, Rarity.RARE, mage.cards.s.ScholarshipSponsor.class)); cards.add(new SetCardInfo("Scrap Trawler", 260, Rarity.RARE, mage.cards.s.ScrapTrawler.class)); cards.add(new SetCardInfo("Sculpting Steel", 261, Rarity.RARE, mage.cards.s.SculptingSteel.class)); cards.add(new SetCardInfo("Secluded Steppe", 315, Rarity.COMMON, mage.cards.s.SecludedSteppe.class)); diff --git a/Mage/src/main/java/mage/cards/Cards.java b/Mage/src/main/java/mage/cards/Cards.java index 43601cda35..44e0ea223d 100644 --- a/Mage/src/main/java/mage/cards/Cards.java +++ b/Mage/src/main/java/mage/cards/Cards.java @@ -1,5 +1,6 @@ package mage.cards; +import mage.constants.Zone; import mage.filter.FilterCard; import mage.game.Game; @@ -42,4 +43,6 @@ public interface Cards extends Set, Serializable { int count(FilterCard filter, UUID sourceId, UUID playerId, Game game); Cards copy(); + + void retainZone(Zone zone, Game game); } diff --git a/Mage/src/main/java/mage/cards/CardsImpl.java b/Mage/src/main/java/mage/cards/CardsImpl.java index 1823a8f3f1..4c7b2a725c 100644 --- a/Mage/src/main/java/mage/cards/CardsImpl.java +++ b/Mage/src/main/java/mage/cards/CardsImpl.java @@ -2,6 +2,7 @@ package mage.cards; import mage.MageItem; import mage.MageObject; +import mage.constants.Zone; import mage.filter.FilterCard; import mage.game.Game; import mage.util.RandomUtil; @@ -206,4 +207,8 @@ public class CardsImpl extends LinkedHashSet implements Cards, Serializabl return cards.values(); } + @Override + public void retainZone(Zone zone, Game game) { + removeIf(uuid -> game.getState().getZone(uuid) != zone); + } }