done with Dependencies

added a class to hold all game rules (type, gamesToWinMatch, manaburn, etc)
This commit is contained in:
Maxmtg
2014-01-24 06:19:09 +00:00
parent e4fb939e91
commit aa4f3de30c
22 changed files with 138 additions and 116 deletions

View File

@@ -1,10 +0,0 @@
package forge;
public class Dependencies {
public static PreferencesMethods preferences;
public interface PreferencesMethods {
@Deprecated public abstract boolean getCloneModeSource();
@Deprecated public abstract boolean isManaBurnEnabled();
}
}

View File

@@ -57,7 +57,7 @@ import forge.util.Aggregates;
* Represents the state of a <i>single game</i>, a new instance is created for each game.
*/
public class Game {
private final GameType type;
private final GameRules rules;
private final List<Player> roIngamePlayers;
private final List<Player> allPlayers;
private final List<Player> ingamePlayers = new ArrayList<Player>();
@@ -89,8 +89,8 @@ public class Game {
* Constructor.
* @param match0
*/
public Game(List<RegisteredPlayer> players0, GameType t, Match match0) { /* no more zones to map here */
type = t;
public Game(List<RegisteredPlayer> players0, GameRules rules, Match match0) { /* no more zones to map here */
this.rules = rules;
match = match0;
List<Player> players = new ArrayList<Player>();
allPlayers = Collections.unmodifiableList(players);
@@ -515,8 +515,8 @@ public class Game {
/**
* @return the type of game (Constructed/Limited/Planechase/etc...)
*/
public GameType getType() {
return type;
public GameRules getRules() {
return rules;
}
/**

View File

@@ -798,7 +798,7 @@ public class GameAction {
}
// Max: I don't know where to put this! - but since it's a state based action, it must be in check state effects
if (game.getType() == GameType.Archenemy) {
if (game.getRules().getGameType() == GameType.Archenemy) {
game.archenemy904_10();
}
@@ -1474,25 +1474,26 @@ public class GameAction {
public void startGame(GameOutcome lastGameOutcome) {
Player first = determineFirstTurnPlayer(lastGameOutcome);
GameType gameType = game.getRules().getGameType();
do {
if (game.isGameOver()) { break; } // conceded during "play or draw"
// FControl should determine now if there are any human players.
// Where there are none, it should bring up speed controls
game.fireEvent(new GameEventGameStarted(game.getType(), first, game.getPlayers()));
game.fireEvent(new GameEventGameStarted(gameType, first, game.getPlayers()));
game.setAge(GameStage.Mulligan);
for (final Player p1 : game.getPlayers()) {
p1.drawCards(p1.getMaxHandSize());
}
performMulligans(first, game.getType() == GameType.Commander);
performMulligans(first, gameType == GameType.Commander);
if (game.isGameOver()) { break; } // conceded during "mulligan" prompt
game.setAge(GameStage.Play);
//<THIS CODE WILL WORK WITH PHASE = NULL>
if (game.getType() == GameType.Planechase) {
if (gameType == GameType.Planechase) {
first.initPlane();
}
@@ -1515,7 +1516,7 @@ public class GameAction {
Player goesFirst = null;
// 904.6: in Archenemy games the Archenemy goes first
if (game != null && game.getType() == GameType.Archenemy) {
if (game != null && game.getRules().getGameType() == GameType.Archenemy) {
for (Player p : game.getPlayers()) {
if (p.isArchenemy()) {
return p;

View File

@@ -0,0 +1,67 @@
package forge.game;
public class GameRules {
private GameType gameType;
private boolean manaBurn;
private int poisonCountersToLose = 10; // is commonly 10, but turns into 15 for 2HG
private int gamesPerMatch = 3;
private int gamesToWinMatch = 2;
private boolean playForAnte = false;
public GameRules(GameType type) {
this.gameType = type;
}
public GameType getGameType() {
return gameType;
}
/**
* @return the manaBurn
*/
public boolean hasManaBurn() {
return manaBurn;
}
/**
* @param manaBurn the manaBurn to set
*/
public void setManaBurn(boolean manaBurn) {
this.manaBurn = manaBurn;
}
/**
* @return the poisonCountersToLose
*/
public int getPoisonCountersToLose() {
return poisonCountersToLose;
}
/**
* @param poisonCountersToLose the poisonCountersToLose to set
*/
public void setPoisonCountersToLose(int amount) {
this.poisonCountersToLose = amount;
}
public int getGamesPerMatch() {
return gamesPerMatch;
}
public void setGamesPerMatch(int gamesPerMatch) {
this.gamesPerMatch = gamesPerMatch;
this.gamesToWinMatch = (int)Math.ceil((gamesPerMatch+1)/2);
}
public boolean useAnte() {
return playForAnte;
}
public void setPlayForAnte(boolean useAnte) {
this.playForAnte = useAnte;
}
public int getGamesToWinMatch() {
return gamesToWinMatch;
}
// it's a preference, not rule... but I could hardly find a better place for it
public boolean canCloneUseTargetsImage;
}

View File

@@ -33,29 +33,19 @@ import forge.util.MyRandom;
public class Match {
private final List<RegisteredPlayer> players;
private final GameType gameType;
private int gamesPerMatch = 3;
private int gamesToWinMatch = 2;
private final boolean useAnte;
private final GameRules rules;
private final List<GameOutcome> gamesPlayed = new ArrayList<GameOutcome>();
private final List<GameOutcome> gamesPlayedRo;
public Match(GameType type, List<RegisteredPlayer> players0, boolean useAnte) {
this(type, players0, useAnte, 3);
}
public Match(GameType type, List<RegisteredPlayer> players0, boolean useAnte, int games) {
gameType = type;
public Match(GameRules rules, List<RegisteredPlayer> players0) {
gamesPlayedRo = Collections.unmodifiableList(gamesPlayed);
players = Collections.unmodifiableList(Lists.newArrayList(players0));
this.rules = rules;
}
gamesPerMatch = games;
gamesToWinMatch = (int)Math.ceil((gamesPerMatch+1)/2);
this.useAnte = useAnte;
public GameRules getRules() {
return rules;
}
/**
@@ -67,15 +57,6 @@ public class Match {
return this.gamesPlayedRo;
}
/** @return int */
public int getGamesPerMatch() {
return gamesPerMatch;
}
/** @return int */
public int getGamesToWinMatch() {
return gamesToWinMatch;
}
public void addGamePlayed(Game finished) {
if (!finished.isGameOver()) {
@@ -88,7 +69,7 @@ public class Match {
* TODO: Write javadoc for this method.
*/
public Game createGame() {
Game game = new Game(players, gameType, this);
Game game = new Game(players, rules, this);
return game;
}
@@ -103,7 +84,7 @@ public class Match {
@Override
public void run() {
prepareAllZones(game);
if (useAnte) { // Deciding which cards go to ante
if (rules.useAnte()) { // Deciding which cards go to ante
Multimap<Player, Card> list = game.chooseCardsForAnte();
for (Entry<Player, Card> kv : list.entries()) {
Player p = kv.getKey();
@@ -116,7 +97,7 @@ public class Match {
GameOutcome lastOutcome = gamesPlayed.isEmpty() ? null : gamesPlayed.get(gamesPlayed.size() - 1);
game.getAction().startGame(lastOutcome);
if (useAnte) {
if (rules.useAnte()) {
executeAnte(game);
}
@@ -141,15 +122,6 @@ public class Match {
gamesPlayed.remove(gamesPlayed.size() - 1);
}
/**
* TODO: Write javadoc for this method.
*
* @return
*/
public GameType getGameType() {
return gameType;
}
public Iterable<GameOutcome> getOutcomes() {
return gamesPlayedRo;
}
@@ -174,11 +146,11 @@ public class Match {
}
for (int score : victories) {
if (score >= gamesToWinMatch) {
if (score >= rules.getGamesToWinMatch()) {
return true;
}
}
return gamesPlayed.size() >= gamesPerMatch;
return gamesPlayed.size() >= rules.getGamesPerMatch();
}
/**
@@ -204,7 +176,7 @@ public class Match {
* @return
*/
public boolean isWonBy(LobbyPlayer questPlayer) {
return getGamesWonBy(questPlayer) >= gamesToWinMatch;
return getGamesWonBy(questPlayer) >= rules.getGamesToWinMatch();
}
public List<RegisteredPlayer> getPlayers() {
@@ -261,9 +233,8 @@ public class Match {
Multimap<Player, PaperCard> rAICards = HashMultimap.create();
Multimap<Player, PaperCard> removedAnteCards = ArrayListMultimap.create();
GameType gameType = game.getType();
boolean isFirstGame = game.getMatch().getPlayedGames().isEmpty();
boolean canSideBoard = !isFirstGame && gameType.isSideboardingAllowed();
boolean canSideBoard = !isFirstGame && rules.getGameType().isSideboardingAllowed();
final List<RegisteredPlayer> playersConditions = game.getMatch().getPlayers();
for (int i = 0; i < playersConditions.size(); i++) {
@@ -274,7 +245,7 @@ public class Match {
if (canSideBoard) {
Deck toChange = psc.getDeck();
List<PaperCard> newMain = player.getController().sideboard(toChange, gameType);
List<PaperCard> newMain = player.getController().sideboard(toChange, rules.getGameType());
if (null != newMain) {
CardPool allCards = new CardPool();
allCards.addAll(toChange.get(DeckSection.Main));
@@ -292,7 +263,7 @@ public class Match {
Deck myDeck = psc.getDeck();
Set<PaperCard> myRemovedAnteCards = null;
if (!useAnte) {
if (!rules.useAnte()) {
myRemovedAnteCards = getRemovedAnteCards(myDeck);
for (PaperCard cp: myRemovedAnteCards) {
for (Entry<DeckSection, CardPool> ds : myDeck) {
@@ -321,8 +292,7 @@ public class Match {
}
}
boolean isLimitedGame = GameType.Quest == game.getType() || GameType.Sealed == game.getType() || GameType.Draft == game.getType();
if (!rAICards.isEmpty() && !isLimitedGame) {
if (!rAICards.isEmpty() && !rules.getGameType().isCardpoolLimited()) {
game.getAction().revealAnte("AI can't play these cards well", rAICards);
}
@@ -405,7 +375,7 @@ public class Match {
outcome.anteResult.put(fromGame, GameOutcome.AnteResult.won(losses));
}
if (gameType.canAddWonCardsMidgame()) {
if (rules.getGameType().canAddWonCardsMidgame()) {
// But only certain game types lets you swap midgame
List<PaperCard> chosen = fromGame.getController().chooseCardsYouWonToAddToDeck(losses);
if (null != chosen) {

View File

@@ -6,7 +6,6 @@ import java.util.List;
import java.util.Map;
import forge.Command;
import forge.Dependencies;
import forge.card.CardCharacteristicName;
import forge.game.Game;
import forge.game.ability.AbilityUtils;
@@ -84,7 +83,7 @@ public class CloneEffect extends SpellAbilityEffect {
}
// determine the image to be used for the clone
String imageFileName = Dependencies.preferences.getCloneModeSource() ? tgtCard.getImageKey() : cardToCopy.getImageKey();
String imageFileName = cardToCopy.getGame().getRules().canCloneUseTargetsImage ? tgtCard.getImageKey() : cardToCopy.getImageKey();
if (sa.hasParam("ImageSource")) { // Allow the image to be stipulated by using a defined card source
List<Card> cloneImgSources = AbilityUtils.getDefinedCards(host, sa.getParam("ImageSource"), sa);
if (!cloneImgSources.isEmpty()) {

View File

@@ -71,7 +71,7 @@ public class CostAddMana extends CostPart {
Card source = sa.getSourceCard();
ColorSet cid = null;
if (ai.getGame().getType() == GameType.Commander) {
if (ai.getGame().getRules().getGameType() == GameType.Commander) {
cid = ai.getCommander().getRules().getColorIdentity();
}
ArrayList<Mana> manaProduced = new ArrayList<Mana>();

View File

@@ -28,7 +28,6 @@ import org.apache.commons.lang3.time.StopWatch;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import forge.Dependencies;
import forge.card.mana.ManaCost;
import forge.game.GameEntity;
import forge.game.GameStage;
@@ -434,14 +433,14 @@ public class PhaseHandler implements java.io.Serializable {
for (Player p : game.getPlayers()) {
int burn = p.getManaPool().clearPool(true);
boolean dealDamage = Dependencies.preferences.isManaBurnEnabled();
if (dealDamage) {
boolean manaBurns = game.getRules().hasManaBurn();
if (manaBurns) {
p.loseLife(burn);
}
// Play the Mana Burn sound
if (burn > 0) {
game.fireEvent(new GameEventManaBurn(burn, dealDamage));
game.fireEvent(new GameEventManaBurn(burn, manaBurns));
}
}
@@ -752,7 +751,7 @@ public class PhaseHandler implements java.io.Serializable {
Player next = getNextActivePlayer();
if (game.getType() == GameType.Planechase) {
if (game.getRules().getGameType() == GameType.Planechase) {
for (Card p :game.getActivePlanes()) {
if (p != null) {
p.setController(next, 0);

View File

@@ -2170,7 +2170,7 @@ public class Player extends GameEntity implements Comparable<Player> {
return this.loseConditionMet(GameLossReason.Poisoned, null);
}
if(game.getType() == GameType.Commander)
if(game.getRules().getGameType() == GameType.Commander)
{
Map<Card,Integer> cmdDmg = getCommanderDamage();
for(Card c : cmdDmg.keySet())

View File

@@ -131,7 +131,7 @@ public class AbilityManaPart implements java.io.Serializable {
}
ColorSet CID = null;
if (player.getGame().getType() == GameType.Commander) {
if (player.getGame().getRules().getGameType() == GameType.Commander) {
CID = player.getCommander().getRules().getColorIdentity();
}
//clear lastProduced