use of mage.players.PlayerList in project mage by magefree.
the class TemptWithReflectionsEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent permanent = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
if (permanent != null) {
Effect effect = new CreateTokenCopyTargetEffect();
effect.setTargetPointer(getTargetPointer());
effect.apply(game, source);
Set<UUID> playersSaidYes = new HashSet<>();
PlayerList playerList = game.getPlayerList().copy();
playerList.setCurrent(game.getActivePlayerId());
Player player = game.getPlayer(game.getActivePlayerId());
do {
if (game.getOpponents(source.getControllerId()).contains(player.getId())) {
String decision;
if (player.chooseUse(outcome, "Create a copy of target creature for you?", source, game)) {
playersSaidYes.add(player.getId());
decision = " chooses to copy ";
} else {
decision = " won't copy ";
}
game.informPlayers((player.getLogName() + decision + permanent.getName()));
}
player = playerList.getNext(game, false);
} while (player != null && !player.getId().equals(game.getActivePlayerId()));
for (UUID playerId : playersSaidYes) {
effect = new CreateTokenCopyTargetEffect(playerId);
effect.setTargetPointer(getTargetPointer());
effect.apply(game, source);
// create a token for the source controller as well
effect = new CreateTokenCopyTargetEffect();
effect.setTargetPointer(getTargetPointer());
effect.apply(game, source);
}
return true;
}
return false;
}
use of mage.players.PlayerList in project mage by magefree.
the class Watcher method copy.
public <T extends Watcher> T copy() {
try {
// use getDeclaredConstructors to allow for package-private constructors (i.e. omit public)
List<?> constructors = Arrays.asList(this.getClass().getDeclaredConstructors());
if (constructors.size() > 1) {
logger.error(getClass().getSimpleName() + " has multiple constructors");
return null;
}
Constructor<? extends Watcher> constructor = (Constructor<? extends Watcher>) constructors.get(0);
constructor.setAccessible(true);
Object[] args = new Object[constructor.getParameterCount()];
for (int index = 0; index < constructor.getParameterTypes().length; index++) {
Class<?> parameterType = constructor.getParameterTypes()[index];
if (parameterType.isPrimitive()) {
if (parameterType.getSimpleName().equalsIgnoreCase("boolean")) {
args[index] = false;
}
} else {
args[index] = null;
}
}
T watcher = (T) constructor.newInstance(args);
List<Field> allFields = new ArrayList<>();
allFields.addAll(Arrays.asList(getClass().getDeclaredFields()));
allFields.addAll(Arrays.asList(getClass().getSuperclass().getDeclaredFields()));
for (Field field : allFields) {
if (!Modifier.isStatic(field.getModifiers())) {
field.setAccessible(true);
if (field.getType() == Set.class) {
((Set) field.get(watcher)).clear();
((Set) field.get(watcher)).addAll((Set) field.get(this));
} else if (field.getType() == Map.class || field.getType() == HashMap.class) {
ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
Type valueType = parameterizedType.getActualTypeArguments()[1];
if (valueType.getTypeName().contains("Set")) {
Map<Object, Set<Object>> source = (Map<Object, Set<Object>>) field.get(this);
Map<Object, Set<Object>> target = (Map<Object, Set<Object>>) field.get(watcher);
target.clear();
for (Map.Entry<Object, Set<Object>> e : source.entrySet()) {
Set<Object> set = new HashSet<>();
set.addAll(e.getValue());
target.put(e.getKey(), set);
}
} else if (valueType.getTypeName().contains("PlayerList")) {
Map<Object, PlayerList> source = (Map<Object, PlayerList>) field.get(this);
Map<Object, PlayerList> target = (Map<Object, PlayerList>) field.get(watcher);
target.clear();
for (Map.Entry<Object, PlayerList> e : source.entrySet()) {
PlayerList list = e.getValue().copy();
target.put(e.getKey(), list);
}
} else if (valueType.getTypeName().endsWith("Cards")) {
Map<Object, Cards> source = (Map<Object, Cards>) field.get(this);
Map<Object, Cards> target = (Map<Object, Cards>) field.get(watcher);
target.clear();
for (Map.Entry<Object, Cards> e : source.entrySet()) {
Cards list = e.getValue().copy();
target.put(e.getKey(), list);
}
} else if (valueType instanceof Class && Arrays.stream(((Class) valueType).getInterfaces()).anyMatch(c -> c.equals(Copyable.class))) {
Map<Object, Copyable> source = (Map<Object, Copyable>) field.get(this);
Map<Object, Copyable> target = (Map<Object, Copyable>) field.get(watcher);
target.clear();
for (Map.Entry<Object, Copyable> e : source.entrySet()) {
Copyable object = (Copyable) e.getValue().copy();
target.put(e.getKey(), object);
}
} else if (valueType.getTypeName().contains("List")) {
Map<Object, List<Object>> source = (Map<Object, List<Object>>) field.get(this);
Map<Object, List<Object>> target = (Map<Object, List<Object>>) field.get(watcher);
target.clear();
for (Map.Entry<Object, List<Object>> e : source.entrySet()) {
List<Object> list = new ArrayList<>();
list.addAll(e.getValue());
target.put(e.getKey(), list);
}
} else if (valueType.getTypeName().contains("Map")) {
Map<Object, Map<Object, Object>> source = (Map<Object, Map<Object, Object>>) field.get(this);
Map<Object, Map<Object, Object>> target = (Map<Object, Map<Object, Object>>) field.get(watcher);
target.clear();
for (Map.Entry<Object, Map<Object, Object>> e : source.entrySet()) {
Map<Object, Object> map = new HashMap<>();
map.putAll(e.getValue());
target.put(e.getKey(), map);
}
} else {
// TODO: add additional tests to find unsupported watcher data
((Map) field.get(watcher)).putAll((Map) field.get(this));
}
} else if (field.getType() == List.class) {
((List) field.get(watcher)).clear();
((List) field.get(watcher)).addAll((List) field.get(this));
} else {
field.set(watcher, field.get(this));
}
}
}
return watcher;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
logger.error("Can't copy watcher: " + e.getMessage(), e);
}
return null;
}
use of mage.players.PlayerList in project mage by magefree.
the class CreatureEvaluator method init.
@Override
public void init(Ability source, Game game) {
Player controller = game.getPlayer(source.getControllerId());
Permanent targetCreature = game.getPermanent(source.getFirstTarget());
if (controller != null && targetCreature != null) {
PlayerList playerList = game.getState().getPlayersInRange(controller.getId(), game);
Player winner = game.getPlayer(controller.getId());
int highBid = 0;
game.informPlayers(winner.getLogName() + " has bet 0 lifes");
Player currentPlayer = playerList.getNext(game, false);
while (currentPlayer != null && !Objects.equals(currentPlayer, winner)) {
String text = winner.getLogName() + " has bet " + highBid + " life" + (highBid > 1 ? "s" : "") + ". Top the bid?";
if (currentPlayer.canRespond() && currentPlayer.chooseUse(Outcome.GainControl, text, source, game)) {
int newBid = 0;
if (currentPlayer.isComputer()) {
// AI hint
// AI will evaluate the creature and bid
CreatureEvaluator eval = new CreatureEvaluator();
int computerLife = currentPlayer.getLife();
int creatureValue = eval.evaluate(targetCreature, game);
newBid = Math.max(creatureValue % 2, computerLife - 100);
} else {
if (currentPlayer.canRespond()) {
newBid = currentPlayer.getAmount(highBid + 1, Integer.MAX_VALUE, "Choose bid", game);
}
}
if (newBid > highBid) {
highBid = newBid;
winner = currentPlayer;
game.informPlayers(currentPlayer.getLogName() + " bet " + newBid + " life" + (newBid > 1 ? "s" : ""));
}
}
currentPlayer = playerList.getNext(game, false);
// stops loop on all players quite
if (game.getState().getPlayersInRange(controller.getId(), game).isEmpty()) {
break;
}
}
game.informPlayers(winner.getLogName() + " won the auction with a bid of " + highBid + " life" + (highBid > 1 ? "s" : ""));
winner.loseLife(highBid, game, source, false);
super.controllingPlayerId = winner.getId();
}
super.init(source, game);
}
use of mage.players.PlayerList in project mage by magefree.
the class KynaiosAndTirosEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
controller.drawCards(1, source, game);
PlayerList playerList = game.getState().getPlayerList().copy();
while (!playerList.get().equals(source.getControllerId()) && controller.canRespond()) {
playerList.getNext();
}
Player currentPlayer = game.getPlayer(playerList.get());
UUID firstInactivePlayer = null;
Target target = new TargetCardInHand(filter);
List<UUID> noLandPlayers = new ArrayList<>();
while (controller.canRespond()) {
if (currentPlayer != null && currentPlayer.canRespond() && game.getState().getPlayersInRange(controller.getId(), game).contains(currentPlayer.getId())) {
if (firstInactivePlayer == null) {
firstInactivePlayer = currentPlayer.getId();
}
target.clearChosen();
boolean playedLand = false;
if (target.canChoose(source.getSourceId(), currentPlayer.getId(), game) && currentPlayer.chooseUse(outcome, "Put a land card from your hand onto the battlefield?", source, game)) {
if (target.chooseTarget(outcome, currentPlayer.getId(), source, game)) {
Card card = game.getCard(target.getFirstTarget());
if (card != null) {
currentPlayer.moveCards(card, Zone.BATTLEFIELD, source, game);
playedLand = true;
}
}
}
if (!playedLand && !Objects.equals(currentPlayer, controller)) {
noLandPlayers.add(currentPlayer.getId());
}
}
// get next player
playerList.getNext();
currentPlayer = game.getPlayer(playerList.get());
// if all player since this player didn't put permanent in play finish the process
if (currentPlayer.getId().equals(firstInactivePlayer)) {
break;
}
}
for (UUID playerId : noLandPlayers) {
Player player = game.getPlayer(playerId);
if (player != null) {
player.drawCards(1, source, game);
}
}
return true;
}
return false;
}
use of mage.players.PlayerList in project mage by magefree.
the class PramikonSkyRampartReplacementEffect method applies.
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (game.getPlayers().size() > 2) {
Player controller = game.getPlayer(source.getControllerId());
if (controller == null) {
return false;
}
if (!game.getState().getPlayersInRange(controller.getId(), game).contains(event.getPlayerId())) {
return false;
}
String allowedDirection = (String) game.getState().getValue(source.getSourceId() + "_modeChoice");
if (allowedDirection == null) {
return false;
}
Player defender = game.getPlayer(event.getTargetId());
if (defender == null) {
Permanent planeswalker = game.getPermanent(event.getTargetId());
if (planeswalker != null) {
defender = game.getPlayer(planeswalker.getControllerId());
}
}
if (defender == null) {
return false;
}
PlayerList playerList = game.getState().getPlayerList(event.getPlayerId());
if (allowedDirection.equals(PramikonSkyRampart.ALLOW_ATTACKING_LEFT) && !playerList.getNext().equals(defender.getId())) {
// the defender is not the player to the left
Player attacker = game.getPlayer(event.getPlayerId());
if (attacker != null) {
game.informPlayer(attacker, "You can only attack to the left!");
}
return true;
}
if (allowedDirection.equals(PramikonSkyRampart.ALLOW_ATTACKING_RIGHT) && !playerList.getPrevious().equals(defender.getId())) {
// the defender is not the player to the right
Player attacker = game.getPlayer(event.getPlayerId());
if (attacker != null) {
game.informPlayer(attacker, "You can only attack to the right!");
}
return true;
}
}
return false;
}
Aggregations