use of mage.players.PlayerList in project mage by magefree.
the class AetherspoutsEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
game.getPlayerList();
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
PlayerList playerList = game.getPlayerList().copy();
playerList.setCurrent(game.getActivePlayerId());
Player player = game.getPlayer(game.getActivePlayerId());
Player activePlayer = player;
do {
List<Permanent> permanentsToTop = new ArrayList<>();
List<Permanent> permanentsToBottom = new ArrayList<>();
for (Permanent permanent : game.getState().getBattlefield().getActivePermanents(new FilterAttackingCreature(), player.getId(), source.getSourceId(), game)) {
if (permanent.isOwnedBy(player.getId())) {
if (player.chooseUse(outcome, "Put " + permanent.getLogName() + " to the top? (else it goes to bottom)", source, game)) {
permanentsToTop.add(permanent);
game.informPlayers(permanent.getLogName() + " goes to the top of " + player.getLogName() + "'s library");
} else {
permanentsToBottom.add(permanent);
game.informPlayers(permanent.getLogName() + " goes to the bottom of " + player.getLogName() + "'s library");
}
}
}
// cards to top
Cards cards = new CardsImpl();
List<Permanent> toLibrary = new ArrayList<>();
for (Permanent permanent : permanentsToTop) {
if (permanent instanceof PermanentToken) {
toLibrary.add(permanent);
} else {
Card card = game.getCard(permanent.getId());
if (card != null) {
cards.add(card);
}
}
}
TargetCard target = new TargetCard(Zone.BATTLEFIELD, new FilterCard("order to put on the top of library (last choosen will be the top most)"));
while (cards.size() > 1) {
if (!player.canRespond()) {
return false;
}
player.choose(Outcome.Neutral, cards, target, game);
Card card = cards.get(target.getFirstTarget(), game);
if (card != null) {
cards.remove(card);
Permanent permanent = game.getPermanent(card.getId());
if (permanent != null) {
toLibrary.add(permanent);
}
}
target.clearChosen();
}
if (cards.size() == 1) {
Card card = cards.get(cards.iterator().next(), game);
Permanent permanent = game.getPermanent(card.getId());
if (permanent != null) {
toLibrary.add(permanent);
}
}
// move all permanents to lib at the same time
for (Permanent permanent : toLibrary) {
player.moveCardToLibraryWithInfo(permanent, source, game, Zone.BATTLEFIELD, true, false);
}
// cards to bottom
cards.clear();
toLibrary.clear();
for (Permanent permanent : permanentsToBottom) {
if (permanent instanceof PermanentToken) {
toLibrary.add(permanent);
} else {
Card card = game.getCard(permanent.getId());
if (card != null) {
cards.add(card);
}
}
}
target = new TargetCard(Zone.BATTLEFIELD, new FilterCard("order to put on bottom of library (last choosen will be bottommost card)"));
while (player.canRespond() && cards.size() > 1) {
player.choose(Outcome.Neutral, cards, target, game);
Card card = cards.get(target.getFirstTarget(), game);
if (card != null) {
cards.remove(card);
Permanent permanent = game.getPermanent(card.getId());
if (permanent != null) {
toLibrary.add(permanent);
}
}
target.clearChosen();
}
if (cards.size() == 1) {
Card card = cards.get(cards.iterator().next(), game);
Permanent permanent = game.getPermanent(card.getId());
if (permanent != null) {
toLibrary.add(permanent);
}
}
// move all permanents to lib at the same time
for (Permanent permanent : toLibrary) {
player.moveCardToLibraryWithInfo(permanent, source, game, Zone.BATTLEFIELD, false, false);
}
player = playerList.getNext(game, false);
} while (player != null && !player.getId().equals(game.getActivePlayerId()) && activePlayer.canRespond());
return true;
}
return false;
}
use of mage.players.PlayerList in project mage by magefree.
the class AminatouUltimateEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
Choice choice = new ChoiceLeftOrRight();
if (!controller.choose(Outcome.Neutral, choice, game)) {
return false;
}
boolean left = choice.getChoice().equals("Left");
PlayerList playerList = game.getState().getPlayerList().copy();
// set playerlist to controller
while (!playerList.get().equals(source.getControllerId())) {
playerList.getNext();
}
UUID currentPlayer = playerList.get();
UUID nextPlayer;
UUID firstNextPlayer = null;
while (!getNextPlayerInDirection(left, playerList, game).equals(firstNextPlayer)) {
nextPlayer = playerList.get();
if (nextPlayer == null) {
return false;
}
// skip players out of range
if (!game.getState().getPlayersInRange(controller.getId(), game).contains(nextPlayer)) {
continue;
}
// save first next player to check for iteration stop
if (firstNextPlayer == null) {
firstNextPlayer = nextPlayer;
}
FilterNonlandPermanent nextPlayerNonlandPermanentsFilter = new FilterNonlandPermanent();
nextPlayerNonlandPermanentsFilter.add(new ControllerIdPredicate(nextPlayer));
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(nextPlayerNonlandPermanentsFilter, game)) {
if (permanent.getId().equals(source.getSourceId())) {
continue;
}
ContinuousEffect effect = new GainControlTargetEffect(Duration.EndOfGame, currentPlayer);
effect.setTargetPointer(new FixedTarget(permanent, game));
game.addEffect(effect, source);
}
currentPlayer = nextPlayer;
}
return true;
}
return false;
}
use of mage.players.PlayerList in project mage by magefree.
the class HumanPlayer method priority.
@Override
public boolean priority(Game game) {
passed = false;
// TODO: use controlling player in all choose dialogs (and canRespond too, what's with take control of player AI?!)
if (canRespond()) {
HumanPlayer controllingPlayer = this;
if (isGameUnderControl()) {
// TODO: must be ! to get real controlling player
Player player = game.getPlayer(getTurnControlledBy());
if (player instanceof HumanPlayer) {
controllingPlayer = (HumanPlayer) player;
}
}
if (getJustActivatedType() != null && !holdingPriority) {
if (controllingPlayer.getUserData().isPassPriorityCast() && getJustActivatedType() == AbilityType.SPELL) {
setJustActivatedType(null);
pass(game);
return false;
}
if (controllingPlayer.getUserData().isPassPriorityActivation() && getJustActivatedType() == AbilityType.ACTIVATED) {
setJustActivatedType(null);
pass(game);
return false;
}
}
// STOP conditions (temporary stop without skip reset)
boolean quickStop = false;
if (isGameUnderControl()) {
// if was attacked - always stop BEFORE blocker step (to cast extra spells)
if (game.getTurn().getStepType() == PhaseStep.DECLARE_ATTACKERS && game.getCombat().getPlayerDefenders(game).contains(playerId)) {
FilterCreatureForCombatBlock filter = filterCreatureForCombatBlock.copy();
filter.add(new ControllerIdPredicate(playerId));
// stop skip on any/zero permanents available
int possibleBlockersCount = game.getBattlefield().count(filter, null, playerId, game);
boolean canStopOnAny = possibleBlockersCount != 0 && getControllingPlayersUserData(game).getUserSkipPrioritySteps().isStopOnDeclareBlockersWithAnyPermanents();
boolean canStopOnZero = possibleBlockersCount == 0 && getControllingPlayersUserData(game).getUserSkipPrioritySteps().isStopOnDeclareBlockersWithZeroPermanents();
quickStop = canStopOnAny || canStopOnZero;
}
}
// SKIP - use the skip actions only if the player itself controls its turn
if (!quickStop && isGameUnderControl()) {
if (passedAllTurns || passedTurnSkipStack) {
if (passWithManaPoolCheck(game)) {
return false;
}
}
if (passedUntilEndStepBeforeMyTurn) {
if (game.getTurn().getStepType() != PhaseStep.END_TURN) {
// other step
if (passWithManaPoolCheck(game)) {
return false;
}
} else {
// end step - search yourself
PlayerList playerList = game.getState().getPlayerList(playerId);
if (!playerList.getPrevious().equals(game.getActivePlayerId())) {
if (passWithManaPoolCheck(game)) {
return false;
}
} else {
// stop
passedUntilEndStepBeforeMyTurn = false;
}
}
}
if (game.getStack().isEmpty()) {
// empty stack
boolean dontCheckPassStep = false;
if (passedUntilStackResolved) {
// Don't skip to next step with this action. It always only resolves a stack. If stack is empty it does nothing.
passedUntilStackResolved = false;
dontCheckPassStep = true;
}
if (passedTurn || passedTurnSkipStack) {
if (passWithManaPoolCheck(game)) {
return false;
}
}
if (passedUntilNextMain) {
if (game.getTurn().getStepType() == PhaseStep.POSTCOMBAT_MAIN || game.getTurn().getStepType() == PhaseStep.PRECOMBAT_MAIN) {
// it's main step
if (!skippedAtLeastOnce || (!playerId.equals(game.getActivePlayerId()) && !controllingPlayer.getUserData().getUserSkipPrioritySteps().isStopOnAllMainPhases())) {
skippedAtLeastOnce = true;
if (passWithManaPoolCheck(game)) {
return false;
}
} else {
dontCheckPassStep = true;
// reset skip action
passedUntilNextMain = false;
}
} else {
skippedAtLeastOnce = true;
if (passWithManaPoolCheck(game)) {
return false;
}
}
}
if (passedUntilEndOfTurn) {
if (game.getTurn().getStepType() == PhaseStep.END_TURN) {
// it's end of turn step
if (!skippedAtLeastOnce || (playerId.equals(game.getActivePlayerId()) && !controllingPlayer.getUserData().getUserSkipPrioritySteps().isStopOnAllEndPhases())) {
skippedAtLeastOnce = true;
if (passWithManaPoolCheck(game)) {
return false;
}
} else {
dontCheckPassStep = true;
// reset skip action
passedUntilEndOfTurn = false;
}
} else {
skippedAtLeastOnce = true;
if (passWithManaPoolCheck(game)) {
return false;
}
}
}
if (!dontCheckPassStep && checkPassStep(game, controllingPlayer)) {
if (passWithManaPoolCheck(game)) {
return false;
}
}
} else {
// non empty stack
boolean haveNewObjectsOnStack = !Objects.equals(dateLastAddedToStack, game.getStack().getDateLastAdded());
dateLastAddedToStack = game.getStack().getDateLastAdded();
if (passedUntilStackResolved) {
if (haveNewObjectsOnStack && (playerId.equals(game.getActivePlayerId()) && controllingPlayer.getUserData().getUserSkipPrioritySteps().isStopOnStackNewObjects())) {
// new objects on stack -- disable "pass until stack resolved"
passedUntilStackResolved = false;
} else {
// no new objects on stack -- go to next priority
}
}
if (passedUntilStackResolved) {
if (passWithManaPoolCheck(game)) {
return false;
}
}
}
}
while (canRespond()) {
holdingPriority = false;
updateGameStatePriority("priority", game);
prepareForResponse(game);
if (!isExecutingMacro()) {
game.firePriorityEvent(playerId);
}
waitForResponse(game);
if (game.executingRollback()) {
return true;
}
if (response.getBoolean() != null || response.getInteger() != null) {
if (!activatingMacro && passWithManaPoolCheck(game)) {
return false;
} else {
if (activatingMacro) {
synchronized (actionQueue) {
actionQueue.notifyAll();
}
}
continue;
}
}
break;
}
UUID responseId = getFixedResponseUUID(game);
if (response.getString() != null && response.getString().equals("special")) {
activateSpecialAction(game, null);
} else if (responseId != null) {
boolean result = false;
MageObject object = game.getObject(responseId);
if (object != null) {
Zone zone = game.getState().getZone(object.getId());
if (zone != null) {
// look at card or try to cast/activate abilities
LinkedHashMap<UUID, ActivatedAbility> useableAbilities = new LinkedHashMap<>();
Player actingPlayer = null;
if (playerId.equals(game.getPriorityPlayerId())) {
actingPlayer = this;
} else if (getPlayersUnderYourControl().contains(game.getPriorityPlayerId())) {
actingPlayer = game.getPlayer(game.getPriorityPlayerId());
}
if (actingPlayer != null) {
useableAbilities = actingPlayer.getPlayableActivatedAbilities(object, zone, game);
// Enable it on massive broken cards/abilities only or for manual tests
if (ALLOW_USERS_TO_PUT_NON_PLAYABLE_SPELLS_ON_STACK_WORKAROUND) {
if (object instanceof Card) {
for (Ability ability : ((Card) object).getAbilities(game)) {
if (ability instanceof SpellAbility && ((SpellAbility) ability).canActivate(actingPlayer.getId(), game).canActivate() || ability instanceof PlayLandAbility) {
useableAbilities.putIfAbsent(ability.getId(), (ActivatedAbility) ability);
}
}
}
}
}
if (object instanceof Card && ((Card) object).isFaceDown(game) && lookAtFaceDownCard((Card) object, game, useableAbilities.size())) {
result = true;
} else {
if (!useableAbilities.isEmpty()) {
activateAbility(useableAbilities, object, game);
result = true;
}
}
}
}
return result;
} else {
return response.getManaType() == null;
}
return true;
}
return false;
}
Aggregations