use of mage.players.PlayerList in project mage by magefree.
the class TriskaidekaphobiaLoseLifeEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
int life;
PlayerList playerList = game.getState().getPlayersInRange(source.getControllerId(), game);
for (UUID pid : playerList) {
Player player = game.getPlayer(pid);
if (player != null) {
life = player.getLife();
if (life == 13) {
player.lost(game);
}
}
}
for (UUID pid : playerList) {
Player player = game.getPlayer(pid);
if (player != null) {
player.gainLife(1, game, source);
}
}
return true;
}
use of mage.players.PlayerList in project mage by magefree.
the class TargetSecondPilePermanent method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
// Map of players and their piles (1,2,3) with values of UUID of the permanents
Map<UUID, Map<Integer, Set<UUID>>> playerPermanents = new LinkedHashMap<>();
PlayerList playerList = game.getState().getPlayerList().copy();
while (!playerList.get().equals(source.getControllerId()) && controller.canRespond()) {
playerList.getNext();
}
Player currentPlayer = game.getPlayer(playerList.get());
Player nextPlayer;
UUID firstNextPlayer = null;
while (!getNextPlayerInDirection(true, playerList, game).equals(firstNextPlayer) && controller.canRespond()) {
nextPlayer = game.getPlayer(playerList.get());
if (nextPlayer == null) {
return false;
}
if (firstNextPlayer == null) {
firstNextPlayer = nextPlayer.getId();
}
if (!nextPlayer.canRespond()) {
continue;
}
// if player is in range of controller they choose 3 piles with all their permanents
if (currentPlayer != null && game.getState().getPlayersInRange(controller.getId(), game).contains(currentPlayer.getId())) {
Map<Integer, Set<UUID>> playerPiles = new HashMap<>();
for (int i = 1; i < 4; i++) {
playerPiles.put(i, new LinkedHashSet<>());
}
playerPermanents.put(currentPlayer.getId(), playerPiles);
for (int i = 1; i < 3; i++) {
FilterPermanent filter = new FilterPermanent("the permanents for the " + (i == 1 ? "first " : "second ") + "pile");
filter.add(new ControllerIdPredicate(currentPlayer.getId()));
Target target;
if (i == 1) {
target = new TargetPermanent(0, Integer.MAX_VALUE, filter, true);
} else {
target = new TargetSecondPilePermanent(playerPiles.get(1), filter);
}
target.setRequired(false);
currentPlayer.chooseTarget(outcome, target, source, game);
StringBuilder message = new StringBuilder(currentPlayer.getLogName()).append(" pile ").append(i).append(": ");
if (target.getTargets().isEmpty()) {
message.append(" (empty)");
} else {
for (UUID permanentId : target.getTargets()) {
Permanent permanent = game.getPermanent(permanentId);
if (permanent != null) {
message.append(permanent.getName()).append(' ');
}
}
}
game.informPlayers(message.toString());
playerPiles.get(i).addAll(target.getTargets());
}
// add all permanents not targeted yet to the third pile
StringBuilder message = new StringBuilder(currentPlayer.getLogName()).append(" pile 3: ");
for (Permanent permanent : game.getState().getBattlefield().getAllActivePermanents(currentPlayer.getId())) {
if (!playerPiles.get(1).contains(permanent.getId()) && !playerPiles.get(2).contains(permanent.getId())) {
playerPiles.get(3).add(permanent.getId());
message.append(permanent.getName()).append(' ');
}
}
if (playerPiles.get(3).isEmpty()) {
message.append(" (empty)");
}
game.informPlayers(message.toString());
}
currentPlayer = nextPlayer;
}
// Sacrifice all permanents from a pile randomly selected
for (Map.Entry<UUID, Map<Integer, Set<UUID>>> playerPiles : playerPermanents.entrySet()) {
Player player = game.getPlayer(playerPiles.getKey());
if (player != null) {
// decide which pile to sacrifice
// random number from 1 - 3
int sacrificePile = RandomUtil.nextInt(3) + 1;
game.informPlayers(player.getLogName() + " sacrifices pile number " + sacrificePile);
for (UUID permanentId : playerPiles.getValue().get(sacrificePile)) {
Permanent permanent = game.getPermanent(permanentId);
if (permanent != null) {
permanent.sacrifice(source, game);
}
}
}
}
return true;
}
return false;
}
use of mage.players.PlayerList in project mage by magefree.
the class ClashEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = source.getSourceObject(game);
if (controller == null || sourceObject == null || game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.CLASH, controller.getId(), source, controller.getId()))) {
return false;
}
// choose opponent
Target target = new TargetOpponent(true);
target.setTargetName("an opponent to clash with");
target.setNotTarget(true);
if (!controller.choose(Outcome.Benefit, target, source.getSourceId(), game)) {
return false;
}
Player opponent = game.getPlayer(target.getFirstTarget());
if (opponent == null) {
return false;
}
int cmcController = Integer.MIN_VALUE;
Card cardController = null;
boolean topController = true;
int cmcOpponent = Integer.MIN_VALUE;
Card cardOpponent = null;
boolean topOpponent = true;
// Reveal top cards of involved players
StringBuilder message = new StringBuilder("Clash: ");
message.append(controller.getLogName());
if (controller.getLibrary().hasCards()) {
Cards cards = new CardsImpl();
cardController = controller.getLibrary().getFromTop(game);
cards.add(cardController);
controller.revealCards(sourceObject.getIdName() + ": Clash card of " + controller.getName(), cards, game);
cmcController = cardController.getManaValue();
message.append(" (").append(cmcController).append(')');
} else {
message.append(" no card");
}
message.append(" vs. ").append(opponent.getLogName());
if (opponent.getLibrary().hasCards()) {
Cards cards = new CardsImpl();
cardOpponent = opponent.getLibrary().getFromTop(game);
cards.add(cardOpponent);
opponent.revealCards(sourceObject.getIdName() + ": Clash card of " + opponent.getName(), cards, game);
cmcOpponent = cardOpponent.getManaValue();
message.append(" (").append(cmcOpponent).append(')');
} else {
message.append(" no card");
}
message.append(" - ");
if (!game.isSimulation()) {
if (cmcController > cmcOpponent) {
message.append(controller.getLogName()).append(" won the clash");
game.informPlayer(controller, "You won the clash!");
} else if (cmcController < cmcOpponent) {
message.append(opponent.getLogName()).append(" won the clash");
game.informPlayer(controller, opponent.getLogName() + " won the clash!");
} else {
message.append(" no winner ");
}
game.informPlayers(message.toString());
}
// decide to put the cards on top or on the buttom of library in turn order beginning with the active player in turn order
PlayerList playerList = game.getPlayerList().copy();
playerList.setCurrent(game.getActivePlayerId());
Player nextPlayer;
do {
Player current = playerList.getCurrent(game);
if (cardController != null && current.getId().equals(controller.getId())) {
topController = current.chooseUse(Outcome.Detriment, "Put " + cardController.getLogName() + " back on top of your library? (otherwise it goes to bottom)", source, game);
}
if (cardOpponent != null && current.getId().equals(opponent.getId())) {
topOpponent = current.chooseUse(Outcome.Detriment, "Put " + cardOpponent.getLogName() + " back on top of your library? (otherwise it goes to bottom)", source, game);
}
nextPlayer = playerList.getNext(game, false);
} while (nextPlayer != null && !nextPlayer.getId().equals(game.getActivePlayerId()));
// put the cards back to library
if (cardController != null) {
controller.moveCardToLibraryWithInfo(cardController, source, game, Zone.LIBRARY, topController, true);
}
if (cardOpponent != null) {
opponent.moveCardToLibraryWithInfo(cardOpponent, source, game, Zone.LIBRARY, topOpponent, true);
}
// fire CLASHED event with info about who won
game.fireEvent(new GameEvent(GameEvent.EventType.CLASHED, controller.getId(), source, opponent.getId(), 0, cmcController > cmcOpponent));
game.fireEvent(new GameEvent(GameEvent.EventType.CLASHED, opponent.getId(), source, controller.getId(), 0, cmcOpponent > cmcController));
// set opponent to DoIfClashWonEffect
for (Effect effect : source.getEffects()) {
if (effect instanceof DoIfClashWonEffect) {
effect.setValue("clashOpponent", opponent);
}
}
return cmcController > cmcOpponent;
}
use of mage.players.PlayerList in project mage by magefree.
the class Combat method getAttackablePlayers.
public List<UUID> getAttackablePlayers(Game game) {
List<UUID> attackablePlayers = new ArrayList<>();
Player attackingPlayer = game.getPlayer(attackingPlayerId);
if (attackingPlayer != null) {
PlayerList players;
Player opponent;
switch(game.getAttackOption()) {
case LEFT:
players = game.getState().getPlayerList(attackingPlayerId);
opponent = players.getNext(game, false);
while (opponent != null && attackingPlayer.isInGame()) {
if (attackingPlayer.hasOpponent(opponent.getId(), game)) {
attackablePlayers.add(opponent.getId());
break;
}
opponent = players.getNext(game, false);
}
break;
case RIGHT:
players = game.getState().getPlayerList(attackingPlayerId);
opponent = players.getPrevious(game);
while (opponent != null && attackingPlayer.isInGame()) {
if (attackingPlayer.hasOpponent(opponent.getId(), game)) {
attackablePlayers.add(opponent.getId());
break;
}
opponent = players.getPrevious(game);
}
break;
case MULTIPLE:
attackablePlayers.addAll(game.getOpponents(attackingPlayerId));
break;
}
}
return attackablePlayers;
}
use of mage.players.PlayerList in project mage by magefree.
the class BendOrBreakEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
// Map of players and their piles
Map<UUID, List<List<Permanent>>> playerPermanents = new LinkedHashMap<>();
PlayerList playerList = game.getState().getPlayerList().copy();
while (!playerList.get().equals(source.getControllerId()) && controller.canRespond()) {
playerList.getNext();
}
Player currentPlayer = game.getPlayer(playerList.get());
Player nextPlayer;
UUID firstNextPlayer = null;
while (!getNextPlayerInDirection(true, playerList).equals(firstNextPlayer) && controller.canRespond()) {
nextPlayer = game.getPlayer(playerList.get());
if (nextPlayer == null) {
return false;
}
if (firstNextPlayer == null) {
firstNextPlayer = nextPlayer.getId();
}
if (!nextPlayer.canRespond()) {
continue;
}
// Each player separates all nontoken lands they control into two piles
if (currentPlayer != null && game.getState().getPlayersInRange(controller.getId(), game).contains(currentPlayer.getId())) {
List<Permanent> firstPile = new ArrayList<>();
List<Permanent> secondPile = new ArrayList<>();
FilterControlledLandPermanent filter = new FilterControlledLandPermanent("lands you control to assign to the first pile (lands not chosen will be assigned to the second pile)");
TargetPermanent target = new TargetControlledPermanent(0, Integer.MAX_VALUE, filter, true);
if (target.canChoose(source.getSourceId(), currentPlayer.getId(), game)) {
// TODO: add support for AI (50/50), need AI hints mechanic here
currentPlayer.chooseTarget(Outcome.Neutral, target, source, game);
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(filter, currentPlayer.getId(), game)) {
if (target.getTargets().contains(permanent.getId())) {
firstPile.add(permanent);
} else {
secondPile.add(permanent);
}
}
StringBuilder sb = new StringBuilder("First pile of ").append(currentPlayer.getLogName()).append(": ");
sb.append(firstPile.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
game.informPlayers(sb.toString());
sb = new StringBuilder("Second pile of ").append(currentPlayer.getLogName()).append(": ");
sb.append(secondPile.stream().map(Permanent::getLogName).collect(Collectors.joining(", ")));
game.informPlayers(sb.toString());
}
List<List<Permanent>> playerPiles = new ArrayList<>();
playerPiles.add(firstPile);
playerPiles.add(secondPile);
playerPermanents.put(currentPlayer.getId(), playerPiles);
}
currentPlayer = nextPlayer;
}
// For each player, one of their piles is chosen by one of their opponents of their choice
for (Map.Entry<UUID, List<List<Permanent>>> playerPiles : playerPermanents.entrySet()) {
Player player = game.getPlayer(playerPiles.getKey());
if (player != null) {
FilterPlayer filter = new FilterPlayer("opponent");
List<PlayerIdPredicate> opponentPredicates = new ArrayList<>();
for (UUID opponentId : game.getOpponents(player.getId())) {
opponentPredicates.add(new PlayerIdPredicate(opponentId));
}
filter.add(Predicates.or(opponentPredicates));
Target target = new TargetPlayer(1, 1, true, filter);
target.setTargetController(player.getId());
target.setAbilityController(source.getControllerId());
if (player.chooseTarget(outcome, target, source, game)) {
Player chosenOpponent = game.getPlayer(target.getFirstTarget());
if (chosenOpponent != null) {
List<Permanent> firstPile = playerPiles.getValue().get(0);
List<Permanent> secondPile = playerPiles.getValue().get(1);
game.informPlayers(player.getLogName() + " chose " + chosenOpponent.getLogName() + " to choose their pile");
if (chosenOpponent.choosePile(outcome, "Piles of " + player.getName(), firstPile, secondPile, game)) {
List<List<Permanent>> lists = playerPiles.getValue();
lists.clear();
lists.add(firstPile);
lists.add(secondPile);
game.informPlayers(player.getLogName() + " will have their first pile destroyed");
} else {
List<List<Permanent>> lists = playerPiles.getValue();
lists.clear();
lists.add(secondPile);
lists.add(firstPile);
game.informPlayers(player.getLogName() + " will have their second pile destroyed");
}
}
}
}
}
// Destroy all lands in the chosen piles. Tap all lands in the other piles
for (Map.Entry<UUID, List<List<Permanent>>> playerPiles : playerPermanents.entrySet()) {
Player player = game.getPlayer(playerPiles.getKey());
if (player != null) {
List<Permanent> pileToSac = playerPiles.getValue().get(0);
List<Permanent> pileToTap = playerPiles.getValue().get(1);
for (Permanent permanent : pileToSac) {
if (permanent != null) {
permanent.destroy(source, game, false);
}
}
for (Permanent permanent : pileToTap) {
if (permanent != null) {
permanent.tap(source, game);
}
}
}
}
return true;
}
return false;
}
Aggregations