mirror of
https://github.com/magefree/mage.git
synced 2025-12-30 07:22:03 -08:00
make batchs for milling cards (per player, all)
Rework and test the couple of existing cards triggering on mill.
This commit is contained in:
parent
4edb9ce270
commit
7c3bbed8f3
17 changed files with 452 additions and 107 deletions
|
|
@ -9,6 +9,7 @@ import mage.constants.Zone;
|
|||
import mage.filter.FilterCard;
|
||||
import mage.game.Game;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.game.events.MilledCardEvent;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
|
@ -58,7 +59,7 @@ public class MillTriggeredAbility extends TriggeredAbilityImpl {
|
|||
default:
|
||||
throw new IllegalArgumentException("Wrong code usage. targetController not yet supported: " + targetController);
|
||||
}
|
||||
Card card = game.getCard(event.getTargetId());
|
||||
Card card = ((MilledCardEvent) event).getCard();
|
||||
return card != null && filter.match(card, getControllerId(), this, game);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
|
||||
package mage.abilities.common;
|
||||
|
||||
import mage.abilities.TriggeredAbilityImpl;
|
||||
import mage.abilities.dynamicvalue.common.SavedMilledValue;
|
||||
import mage.abilities.effects.Effect;
|
||||
import mage.constants.Zone;
|
||||
import mage.filter.FilterCard;
|
||||
import mage.game.Game;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.game.events.MilledBatchAllEvent;
|
||||
|
||||
/**
|
||||
* @author Susucr
|
||||
*/
|
||||
public class OneOrMoreMilledTriggeredAbility extends TriggeredAbilityImpl {
|
||||
|
||||
private final FilterCard filter;
|
||||
|
||||
public OneOrMoreMilledTriggeredAbility(FilterCard filter, Effect effect) {
|
||||
this(filter, effect, false);
|
||||
}
|
||||
|
||||
public OneOrMoreMilledTriggeredAbility(FilterCard filter, Effect effect, boolean optional) {
|
||||
super(Zone.BATTLEFIELD, effect, optional);
|
||||
this.setTriggerPhrase("Whenever one or more " + filter.getMessage() + " are milled, ");
|
||||
this.filter = filter;
|
||||
}
|
||||
|
||||
private OneOrMoreMilledTriggeredAbility(final OneOrMoreMilledTriggeredAbility ability) {
|
||||
super(ability);
|
||||
this.filter = ability.filter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public OneOrMoreMilledTriggeredAbility copy() {
|
||||
return new OneOrMoreMilledTriggeredAbility(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkEventType(GameEvent event, Game game) {
|
||||
return event.getType() == GameEvent.EventType.MILLED_CARDS_BATCH_FOR_ALL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkTrigger(GameEvent event, Game game) {
|
||||
int count = ((MilledBatchAllEvent) event).getCards().count(filter, getControllerId(), this, game);
|
||||
if (count <= 0) {
|
||||
return false;
|
||||
}
|
||||
getEffects().setValue(SavedMilledValue.VALUE_KEY, count);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
package mage.abilities.dynamicvalue.common;
|
||||
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.dynamicvalue.DynamicValue;
|
||||
import mage.abilities.effects.Effect;
|
||||
import mage.game.Game;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author Susucr
|
||||
*/
|
||||
public enum SavedMilledValue implements DynamicValue {
|
||||
MANY("many"),
|
||||
MUCH("much");
|
||||
|
||||
private final String message;
|
||||
|
||||
public static final String VALUE_KEY = "SavedMilled";
|
||||
|
||||
SavedMilledValue(String message) {
|
||||
this.message = "that " + message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int calculate(Game game, Ability sourceAbility, Effect effect) {
|
||||
return Optional.ofNullable((Integer) effect.getValue(VALUE_KEY)).orElse(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SavedMilledValue copy() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
|
@ -901,6 +901,33 @@ public class GameState implements Serializable, Copyable<GameState> {
|
|||
}
|
||||
}
|
||||
|
||||
public void addSimultaneousMilledCardToBatch(MilledCardEvent milledEvent, Game game) {
|
||||
// Combine multiple mill cards events in the single event (batch)
|
||||
// see GameEvent.MILLED_CARDS_BATCH_FOR_ONE_PLAYER and GameEvent.MILLED_CARDS_BATCH_FOR_ALL
|
||||
|
||||
// existing batch
|
||||
boolean isBatchUsed = false;
|
||||
boolean isBatchForPlayerUsed = false;
|
||||
for (GameEvent event : simultaneousEvents) {
|
||||
if (event instanceof MilledBatchAllEvent) {
|
||||
((MilledBatchAllEvent) event).addEvent(milledEvent);
|
||||
isBatchUsed = true;
|
||||
} else if (event instanceof MilledBatchForOnePlayerEvent
|
||||
&& event.getPlayerId().equals(milledEvent.getPlayerId())) {
|
||||
((MilledBatchForOnePlayerEvent) event).addEvent(milledEvent);
|
||||
isBatchForPlayerUsed = true;
|
||||
}
|
||||
}
|
||||
|
||||
// new batch
|
||||
if (!isBatchUsed) {
|
||||
addSimultaneousEvent(new MilledBatchAllEvent(milledEvent), game);
|
||||
}
|
||||
if (!isBatchForPlayerUsed) {
|
||||
addSimultaneousEvent(new MilledBatchForOnePlayerEvent(milledEvent), game);
|
||||
}
|
||||
}
|
||||
|
||||
public void addSimultaneousLifeLossToBatch(LifeLostEvent lifeLossEvent, Game game) {
|
||||
// Combine multiple life loss events in the single event (batch)
|
||||
// see GameEvent.LOST_LIFE_BATCH
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ public abstract class BatchEvent<T extends GameEvent> extends GameEvent {
|
|||
private final Set<T> events = new HashSet<>();
|
||||
private final boolean singleTargetId;
|
||||
private final boolean singleSourceId;
|
||||
private final boolean singlePlayerId;
|
||||
|
||||
/**
|
||||
* @param eventType specific type of event
|
||||
|
|
@ -25,9 +26,25 @@ public abstract class BatchEvent<T extends GameEvent> extends GameEvent {
|
|||
* @param firstEvent added to initialize the batch (batch is never empty)
|
||||
*/
|
||||
protected BatchEvent(EventType eventType, boolean singleTargetId, boolean singleSourceId, T firstEvent) {
|
||||
super(eventType, (singleTargetId ? firstEvent.getTargetId() : null), null, null);
|
||||
this(eventType, singleTargetId, singleSourceId, false, firstEvent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param eventType specific type of event
|
||||
* @param singleSourceId if true, all included events must have same source id
|
||||
* @param singleTargetId if true, all included events must have same target id
|
||||
* @param singlePlayerId if true, all included events must have same player id
|
||||
* @param firstEvent added to initialize the batch (batch is never empty)
|
||||
*/
|
||||
protected BatchEvent(EventType eventType, boolean singleTargetId, boolean singleSourceId, boolean singlePlayerId, T firstEvent) {
|
||||
super(eventType,
|
||||
(singleTargetId ? firstEvent.getTargetId() : null),
|
||||
null,
|
||||
(singlePlayerId ? firstEvent.getPlayerId() : null)
|
||||
);
|
||||
this.singleTargetId = singleTargetId;
|
||||
this.singleSourceId = singleSourceId;
|
||||
this.singlePlayerId = singlePlayerId;
|
||||
this.setSourceId(singleSourceId ? firstEvent.getSourceId() : null);
|
||||
if (firstEvent instanceof BatchEvent) { // sanity check, if you need it then think twice and research carefully
|
||||
throw new UnsupportedOperationException("Wrong code usage: nesting batch events not supported");
|
||||
|
|
@ -42,6 +59,7 @@ public abstract class BatchEvent<T extends GameEvent> extends GameEvent {
|
|||
super(eventType, null, null, null);
|
||||
this.singleTargetId = false;
|
||||
this.singleSourceId = false;
|
||||
this.singlePlayerId = false;
|
||||
}
|
||||
|
||||
public void addEvent(T event) {
|
||||
|
|
@ -104,8 +122,10 @@ public abstract class BatchEvent<T extends GameEvent> extends GameEvent {
|
|||
}
|
||||
|
||||
@Override // events can store a diff value, so search it from events list instead
|
||||
@Deprecated // no use case currently supported
|
||||
public UUID getPlayerId() {
|
||||
if (singlePlayerId) {
|
||||
return super.getPlayerId();
|
||||
}
|
||||
throw new IllegalStateException("Wrong code usage. Must search value from a getEvents list");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -102,13 +102,27 @@ public class GameEvent implements Serializable {
|
|||
*/
|
||||
CLASH, CLASHED,
|
||||
DAMAGE_PLAYER,
|
||||
|
||||
/* MILL_CARDS
|
||||
playerId the id of the player milling the card (not the source's controller)
|
||||
targetId the id of the card milled
|
||||
*/
|
||||
MILL_CARDS,
|
||||
/* MILLED_CARDS
|
||||
playerId the id of the player milling the card (not the source's controller)
|
||||
targetId the id of the card milled
|
||||
*/
|
||||
MILLED_CARD,
|
||||
MILLED_CARDS,
|
||||
/* MILLED_CARDS_BATCH_FOR_ONE_PLAYER,
|
||||
combines all MILLED_CARD events for a player milling card at the same time in a single batch
|
||||
playerId the id of the player whose batch it is
|
||||
*/
|
||||
MILLED_CARDS_BATCH_FOR_ONE_PLAYER,
|
||||
/* MILLED_CARDS_BATCH_FOR_ALL,
|
||||
combines all MILLED_CARD events for any player in a single batch
|
||||
*/
|
||||
MILLED_CARDS_BATCH_FOR_ALL,
|
||||
|
||||
/* DAMAGED_PLAYER
|
||||
targetId the id of the damaged player
|
||||
sourceId sourceId of the ability which caused the damage
|
||||
|
|
|
|||
26
Mage/src/main/java/mage/game/events/MilledBatchAllEvent.java
Normal file
26
Mage/src/main/java/mage/game/events/MilledBatchAllEvent.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package mage.game.events;
|
||||
|
||||
import mage.cards.Cards;
|
||||
import mage.cards.CardsImpl;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Susucr
|
||||
*/
|
||||
public class MilledBatchAllEvent extends BatchEvent<MilledCardEvent> {
|
||||
|
||||
public MilledBatchAllEvent(MilledCardEvent event) {
|
||||
super(EventType.MILLED_CARDS_BATCH_FOR_ALL, false, false, false, event);
|
||||
}
|
||||
|
||||
public Cards getCards() {
|
||||
return new CardsImpl(getEvents()
|
||||
.stream()
|
||||
.map(MilledCardEvent::getCard)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package mage.game.events;
|
||||
|
||||
import mage.cards.Cards;
|
||||
import mage.cards.CardsImpl;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Susucr
|
||||
*/
|
||||
public class MilledBatchForOnePlayerEvent extends BatchEvent<MilledCardEvent> {
|
||||
|
||||
public MilledBatchForOnePlayerEvent(MilledCardEvent event) {
|
||||
super(EventType.MILLED_CARDS_BATCH_FOR_ONE_PLAYER, false, false, true, event);
|
||||
}
|
||||
|
||||
public Cards getCards() {
|
||||
return new CardsImpl(getEvents()
|
||||
.stream()
|
||||
.map(MilledCardEvent::getCard)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet())
|
||||
);
|
||||
}
|
||||
}
|
||||
26
Mage/src/main/java/mage/game/events/MilledCardEvent.java
Normal file
26
Mage/src/main/java/mage/game/events/MilledCardEvent.java
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package mage.game.events;
|
||||
|
||||
import mage.abilities.Ability;
|
||||
import mage.cards.Card;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* Event for an individual card being milled.
|
||||
* Stores the card at the moment it is milled.
|
||||
*
|
||||
* @author Susucr
|
||||
*/
|
||||
public class MilledCardEvent extends GameEvent {
|
||||
|
||||
private final Card card;
|
||||
|
||||
public MilledCardEvent(Card card, UUID playerId, Ability source) {
|
||||
super(EventType.MILLED_CARD, card.getId(), source, playerId);
|
||||
this.card = card;
|
||||
}
|
||||
|
||||
public Card getCard() {
|
||||
return card;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
package mage.game.events;
|
||||
|
||||
import mage.abilities.Ability;
|
||||
import mage.cards.Cards;
|
||||
import mage.cards.CardsImpl;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author TheElk801
|
||||
*/
|
||||
public class MilledCardsEvent extends GameEvent {
|
||||
|
||||
private final Cards cards = new CardsImpl();
|
||||
|
||||
public MilledCardsEvent(Ability source, UUID playerId, Cards cards) {
|
||||
super(EventType.MILLED_CARDS, playerId, source, playerId);
|
||||
this.cards.addAll(cards);
|
||||
}
|
||||
|
||||
public Cards getCards() {
|
||||
return cards;
|
||||
}
|
||||
}
|
||||
|
|
@ -5093,9 +5093,10 @@ public abstract class PlayerImpl implements Player, Serializable {
|
|||
Cards cards = new CardsImpl(this.getLibrary().getTopCards(game, event.getAmount()));
|
||||
this.moveCards(cards, Zone.GRAVEYARD, source, game);
|
||||
for (Card card : cards.getCards(game)) {
|
||||
game.fireEvent(GameEvent.getEvent(GameEvent.EventType.MILLED_CARD, card.getId(), source, getId()));
|
||||
MilledCardEvent milledEvent = new MilledCardEvent(card, getId(), source);
|
||||
game.fireEvent(milledEvent);
|
||||
game.getState().addSimultaneousMilledCardToBatch(milledEvent, game);
|
||||
}
|
||||
game.fireEvent(new MilledCardsEvent(source, getId(), cards));
|
||||
return cards;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import mage.constants.WatcherScope;
|
|||
import mage.constants.Zone;
|
||||
import mage.game.Game;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.game.events.MilledCardEvent;
|
||||
import mage.watchers.Watcher;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -29,7 +30,7 @@ public class CardsMilledWatcher extends Watcher {
|
|||
if (event.getType() != GameEvent.EventType.MILLED_CARD) {
|
||||
return;
|
||||
}
|
||||
Card card = game.getCard(event.getTargetId());
|
||||
Card card = ((MilledCardEvent) event).getCard();
|
||||
if (card == null) {
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue