Fix faulty logic in AddCounters effects amount computation.

When set with DynamicValue, and that value computes to 0,
the amount of counters added was incorrectly the Counters amount.
This commit is contained in:
Susucre 2024-04-05 11:52:15 +02:00
parent d591a89495
commit 2d7349a7bb
5 changed files with 102 additions and 40 deletions

View file

@ -44,29 +44,30 @@ public class AddCountersAllEffect extends OneShotEffect {
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = game.getObject(source);
if (controller != null && sourceObject != null) {
if (counter != null) {
for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source, game)) {
Counter newCounter = counter.copy();
int calculated = amount.calculate(game, source, this); // 0 -- you must use default counter
if (calculated < 0) {
continue;
} else if (calculated == 0) {
// use original counter
} else {
// increase to calculated value
newCounter.remove(newCounter.getCount());
newCounter.add(calculated);
}
if (controller != null && sourceObject != null && counter != null) {
Counter newCounter = counter.copy();
int calculated = amount.calculate(game, source, this);
if (!(amount instanceof StaticValue) || calculated > 0) {
// If dynamic, or static and set to a > 0 value, we use that instead of the counter's internal amount.
newCounter.remove(newCounter.getCount());
newCounter.add(calculated);
} else {
// StaticValue 0 -- the default counter has the amount, so no adjustment.
}
permanent.addCounters(newCounter, source.getControllerId(), source, game);
if (!game.isSimulation() && newCounter.getCount() > 0) {
game.informPlayers(sourceObject.getLogName() + ": " + controller.getLogName() + " puts " + newCounter.getCount() + ' ' + newCounter.getName()
+ (newCounter.getCount() == 1 ? " counter" : " counters") + " on " + permanent.getLogName());
}
if (newCounter.getCount() <= 0) {
return false; // no need to iterate on targets, no counters will be put on them
}
for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source, game)) {
Counter newCounterForPermanent = counter.copy();
permanent.addCounters(newCounterForPermanent, source.getControllerId(), source, game);
if (!game.isSimulation() && newCounterForPermanent.getCount() > 0) {
game.informPlayers(sourceObject.getLogName() + ": " + controller.getLogName() + " puts " + newCounterForPermanent.getCount() + ' ' + newCounterForPermanent.getName()
+ (newCounterForPermanent.getCount() == 1 ? " counter" : " counters") + " on " + permanent.getLogName());
}
}
return true;
}
return false;
}

View file

@ -56,38 +56,42 @@ public class AddCountersTargetEffect extends OneShotEffect {
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = game.getObject(source);
if (controller != null && sourceObject != null && counter != null) {
Counter newCounter = counter.copy();
int calculated = amount.calculate(game, source, this);
if (!(amount instanceof StaticValue) || calculated > 0) {
// If dynamic, or static and set to a > 0 value, we use that instead of the counter's internal amount.
newCounter.remove(newCounter.getCount());
newCounter.add(calculated);
} else {
// StaticValue 0 -- the default counter has the amount, so no adjustment.
}
if (newCounter.getCount() <= 0) {
return false; // no need to iterate on targets, no counters will be put on them
}
int affectedTargets = 0;
for (UUID uuid : getTargetPointer().getTargets(game, source)) {
Counter newCounter = counter.copy();
int calculated = amount.calculate(game, source, this); // 0 -- you must use default couner
if (calculated < 0) {
continue;
} else if (calculated == 0) {
// use original counter
} else {
// increase to calculated value
newCounter.remove(newCounter.getCount());
newCounter.add(calculated);
}
Counter newCounterForTarget = newCounter.copy();
Permanent permanent = game.getPermanent(uuid);
Player player = game.getPlayer(uuid);
Card card = game.getCard(getTargetPointer().getFirst(game, source));
if (permanent != null) {
permanent.addCounters(newCounter, source.getControllerId(), source, game);
permanent.addCounters(newCounterForTarget, source.getControllerId(), source, game);
affectedTargets++;
game.informPlayers(sourceObject.getLogName() + ": " + controller.getLogName() + " puts "
+ newCounter.getCount() + ' ' + newCounter.getName() + " counters on " + permanent.getLogName());
+ newCounterForTarget.getCount() + ' ' + newCounterForTarget.getName() + " counters on " + permanent.getLogName());
} else if (player != null) {
player.addCounters(newCounter, source.getControllerId(), source, game);
player.addCounters(newCounterForTarget, source.getControllerId(), source, game);
affectedTargets++;
game.informPlayers(sourceObject.getLogName() + ": " + controller.getLogName() + " puts "
+ newCounter.getCount() + ' ' + newCounter.getName() + " counters on " + player.getLogName());
+ newCounterForTarget.getCount() + ' ' + newCounterForTarget.getName() + " counters on " + player.getLogName());
} else if (card != null) {
card.addCounters(newCounter, source.getControllerId(), source, game);
card.addCounters(newCounterForTarget, source.getControllerId(), source, game);
affectedTargets++;
game.informPlayers(sourceObject.getLogName() + ": " + controller.getLogName() + " puts "
+ newCounter.getCount() + ' ' + newCounter.getName() + " counters on " + card.getLogName());
+ newCounterForTarget.getCount() + ' ' + newCounterForTarget.getName() + " counters on " + card.getLogName());
}
}
return affectedTargets > 0;