Merge remote-tracking branch 'origin/master'

This commit is contained in:
Oleg Agafonov 2019-01-16 21:04:00 +04:00
commit 975cff93e3
70 changed files with 290 additions and 201 deletions

View file

@ -5,10 +5,10 @@ import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.Effect;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.CoinFlippedEvent;
import mage.game.events.GameEvent;
/**
*
* @author TheElk801
*/
public class WinsCoinFlipTriggeredAbility extends TriggeredAbilityImpl {
@ -33,7 +33,8 @@ public class WinsCoinFlipTriggeredAbility extends TriggeredAbilityImpl {
@Override
public boolean checkTrigger(GameEvent event, Game game) {
return event.getFlag();
CoinFlippedEvent flipEvent = (CoinFlippedEvent) event;
return flipEvent.isWinnable() && (flipEvent.getChosen() == flipEvent.getResult());
}
@Override

View file

@ -60,7 +60,7 @@ public class FlipCoinEffect extends OneShotEffect {
MageObject mageObject = game.getObject(source.getSourceId());
if (controller != null && mageObject != null) {
boolean result = true;
for (Effect effect : controller.flipCoin(game) ? executingEffectsWon : executingEffectsLost) {
for (Effect effect : controller.flipCoin(source, game, true) ? executingEffectsWon : executingEffectsLost) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);

View file

@ -34,7 +34,7 @@ public class FlipUntilLoseEffect extends OneShotEffect {
return false;
}
while (true) {
if (!player.flipCoin(game)) {
if (!player.flipCoin(source, game, true)) {
return true;
}
}

View file

@ -0,0 +1,41 @@
package mage.game.events;
import mage.util.CardUtil;
import java.util.UUID;
/**
* @author TheElk801
**/
public class CoinFlippedEvent extends GameEvent {
private final boolean result;
private final boolean chosen;
private final boolean winnable;
CoinFlippedEvent(UUID playerId, UUID sourceId, boolean result, boolean chosen, boolean winnable) {
super(EventType.COIN_FLIPPED, playerId, sourceId, playerId);
this.result = result;
this.chosen = chosen;
this.winnable = winnable;
}
public boolean getResult() {
return result;
}
public String getResultName() {
return CardUtil.booleanToFlipName(result);
}
public boolean getChosen() {
return chosen;
}
public String getChosenName() {
return CardUtil.booleanToFlipName(chosen);
}
public boolean isWinnable() {
return winnable;
}
}

View file

@ -0,0 +1,49 @@
package mage.game.events;
import mage.util.CardUtil;
import java.util.UUID;
/**
* @author TheElk801
**/
public class FlipCoinEvent extends GameEvent {
private boolean result;
private final boolean chosen;
private final boolean winnable;
public FlipCoinEvent(UUID playerId, UUID sourceId, boolean result, boolean chosen, boolean winnable) {
super(EventType.FLIP_COIN, playerId, sourceId, playerId);
this.result = result;
this.chosen = chosen;
this.winnable = winnable;
}
public boolean getResult() {
return result;
}
public String getResultName() {
return CardUtil.booleanToFlipName(result);
}
public void setResult(boolean result) {
this.result = result;
}
public boolean getChosen() {
return chosen;
}
public String getChosenName() {
return CardUtil.booleanToFlipName(chosen);
}
public boolean isWinnable() {
return winnable;
}
public CoinFlippedEvent getFlippedEvent() {
return new CoinFlippedEvent(playerId, sourceId, result, chosen, winnable);
}
}

View file

@ -395,9 +395,9 @@ public interface Player extends MageItem, Copyable<Player> {
boolean hasProtectionFrom(MageObject source, Game game);
boolean flipCoin(Game game);
boolean flipCoin(Ability source, Game game, boolean winnable);
boolean flipCoin(Game game, ArrayList<UUID> appliedEffects);
boolean flipCoin(Ability source, Game game, boolean winnable, ArrayList<UUID> appliedEffects);
int rollDice(Game game, int numSides);

View file

@ -42,11 +42,8 @@ import mage.filter.predicate.permanent.PermanentIdPredicate;
import mage.game.*;
import mage.game.combat.CombatGroup;
import mage.game.command.CommandObject;
import mage.game.events.DamagePlayerEvent;
import mage.game.events.DamagedPlayerEvent;
import mage.game.events.GameEvent;
import mage.game.events.*;
import mage.game.events.GameEvent.EventType;
import mage.game.events.ZoneChangeEvent;
import mage.game.match.MatchPlayer;
import mage.game.permanent.Permanent;
import mage.game.permanent.PermanentCard;
@ -2560,27 +2557,38 @@ public abstract class PlayerImpl implements Player, Serializable {
}
@Override
public boolean flipCoin(Game game) {
return this.flipCoin(game, null);
public boolean flipCoin(Ability source, Game game, boolean winnable) {
return this.flipCoin(source, game, winnable, null);
}
/**
* @param source
* @param game
* @param winnable
* @param appliedEffects
* @return true if player won the toss
* @return if winnable, true if player won the toss, if not winnable, true for heads and false for tails
*/
@Override
public boolean flipCoin(Game game, ArrayList<UUID> appliedEffects) {
public boolean flipCoin(Ability source, Game game, boolean winnable, ArrayList<UUID> appliedEffects) {
boolean chosen = false;
if (winnable) {
chosen = this.chooseUse(Outcome.Benefit, "Heads or tails?", "", "Heads", "Tails", source, game);
game.informPlayers(getLogName() + " chose " + (chosen ? "heads." : "tails."));
}
boolean result = RandomUtil.nextBoolean();
if (!game.isSimulation()) {
game.informPlayers("[Flip a coin] " + getLogName() + (result ? " won (head)." : " lost (tail)."));
FlipCoinEvent event = new FlipCoinEvent(playerId, source.getSourceId(), result, chosen, winnable);
event.addAppliedEffects(appliedEffects);
game.replaceEvent(event);
game.informPlayers(getLogName() + " got " + (event.getResult() ? "heads" : "tails"));
if (event.isWinnable()) {
game.informPlayers(getLogName() + " " + (event.getResult() == event.getChosen() ? "won" : "lost") + " the flip");
}
GameEvent event = new GameEvent(GameEvent.EventType.FLIP_COIN, playerId, null, playerId, 0, result);
event.setAppliedEffects(appliedEffects);
if (!game.replaceEvent(event)) {
game.fireEvent(new GameEvent(GameEvent.EventType.COIN_FLIPPED, playerId, null, playerId, 0, event.getFlag()));
game.fireEvent(event.getFlippedEvent());
if (event.isWinnable()) {
return event.getResult() == event.getChosen();
}
return event.getFlag();
return event.getResult();
}
@Override
@ -2598,7 +2606,7 @@ public abstract class PlayerImpl implements Player, Serializable {
public int rollDice(Game game, ArrayList<UUID> appliedEffects, int numSides) {
int result = RandomUtil.nextInt(numSides) + 1;
if (!game.isSimulation()) {
game.informPlayers("[Roll a die] " + getLogName() + " rolled a " + result + " on a " + numSides + " sided dice");
game.informPlayers("[Roll a die] " + getLogName() + " rolled a " + result + " on a " + numSides + " sided die");
}
GameEvent event = new GameEvent(GameEvent.EventType.ROLL_DICE, playerId, null, playerId, result, true);
event.setAppliedEffects(appliedEffects);

View file

@ -354,6 +354,13 @@ public final class CardUtil {
return message;
}
public static String booleanToFlipName(boolean flip) {
if (flip) {
return "Heads";
}
return "Tails";
}
public static boolean checkNumeric(String s) {
return s.chars().allMatch(Character::isDigit);