wrapped long lines, reduce nestedness of logic

master
melvin 2011-06-18 10:54:20 +08:00
parent 8d3025482e
commit 5d3c78106c
1 changed files with 92 additions and 86 deletions

View File

@ -120,7 +120,7 @@ public class MagicGame {
artificial=true; artificial=true;
sound=false; sound=false;
//copy the reference //copy the reference, these are singletons
this.tournament=game.tournament; this.tournament=game.tournament;
this.gameplay=game.gameplay; this.gameplay=game.gameplay;
@ -375,11 +375,12 @@ public class MagicGame {
/** Tells gameplay that is can skip certain parts during AI processing. */ /** Tells gameplay that is can skip certain parts during AI processing. */
public boolean canSkip() { public boolean canSkip() {
return stack.isEmpty()&&artificial; return stack.isEmpty() && artificial;
} }
public boolean isFinished() { public boolean isFinished() {
return losingPlayer!=null||mainPhaseCount<=0; return losingPlayer !=null ||
mainPhaseCount <= 0;
} }
public MagicLogBook getLogBook() { public MagicLogBook getLogBook() {
@ -452,14 +453,12 @@ public class MagicGame {
} }
public void gotoLastUndoPoint() { public void gotoLastUndoPoint() {
final MagicAction markerAction=undoPoints.removeLast(); final MagicAction markerAction = undoPoints.removeLast();
while (true) { MagicAction action;
final MagicAction action=actions.removeLast(); do {
action = actions.removeLast();
action.undoAction(this); action.undoAction(this);
if (action==markerAction) { } while (!(action == markerAction));
break;
}
}
} }
public int getNrOfUndoPoints() { public int getNrOfUndoPoints() {
@ -479,34 +478,41 @@ public class MagicGame {
} }
public void logMessages() { public void logMessages() {
if (logMessageBuilder!=null) { if (logMessageBuilder == null) {
logMessageBuilder.logMessages(); return;
} }
logMessageBuilder.logMessages();
} }
public void logAppendEvent(final MagicEvent event,final Object choiceResults[]) { public void logAppendEvent(final MagicEvent event,final Object choiceResults[]) {
if (logMessageBuilder!=null) { if (logMessageBuilder == null) {
return;
}
final String message=event.getDescription(choiceResults); final String message=event.getDescription(choiceResults);
if (message!=null) { if (message == null) {
return;
}
logMessageBuilder.appendMessage(event.getPlayer(),message); logMessageBuilder.appendMessage(event.getPlayer(),message);
} }
}
}
public void logAppendMessage(final MagicPlayer player,final String message) { public void logAppendMessage(final MagicPlayer player,final String message) {
if (logMessageBuilder!=null) { if (logMessageBuilder == null) {
logMessageBuilder.appendMessage(player,message); return;
} }
logMessageBuilder.appendMessage(player,message);
} }
public void logMessage(final MagicPlayer player,final String message) { public void logMessage(final MagicPlayer player,final String message) {
if (logBook!=null) { if (logBook == null) {
logBook.add(new MagicMessage(this,player,message)); return;
} }
logBook.add(new MagicMessage(this,player,message));
} }
public void logAttackers(final MagicPlayer player,final MagicDeclareAttackersResult result) { public void logAttackers(final MagicPlayer player,final MagicDeclareAttackersResult result) {
if (logBook!=null&&!result.isEmpty()) { if (logBook == null || result.isEmpty()) {
return;
}
final SortedSet<String> names=new TreeSet<String>(); final SortedSet<String> names=new TreeSet<String>();
for (final MagicPermanent attacker : result) { for (final MagicPermanent attacker : result) {
names.add(attacker.getName()); names.add(attacker.getName());
@ -516,27 +522,25 @@ public class MagicGame {
builder.append('.'); builder.append('.');
logBook.add(new MagicMessage(this,player,builder.toString())); logBook.add(new MagicMessage(this,player,builder.toString()));
} }
}
public void logBlockers(final MagicPlayer player,final MagicDeclareBlockersResult result) { public void logBlockers(final MagicPlayer player,final MagicDeclareBlockersResult result) {
if (logBook == null) {
if (logBook!=null) { return;
}
final SortedSet<String> names=new TreeSet<String>(); final SortedSet<String> names=new TreeSet<String>();
for (final MagicCombatCreature[] creatures : result) { for (final MagicCombatCreature[] creatures : result) {
for (int index=1;index<creatures.length;index++) { for (int index=1;index<creatures.length;index++) {
names.add(creatures[index].getName()); names.add(creatures[index].getName());
} }
} }
if (!names.isEmpty()) { if (names.isEmpty()) {
return;
}
final StringBuilder builder=new StringBuilder("You block with "); final StringBuilder builder=new StringBuilder("You block with ");
MagicMessage.addNames(builder,names); MagicMessage.addNames(builder,names);
builder.append('.'); builder.append('.');
logBook.add(new MagicMessage(this,player,builder.toString())); logBook.add(new MagicMessage(this,player,builder.toString()));
} }
}
}
public void executeEvent(final MagicEvent event,final Object choiceResults[]) { public void executeEvent(final MagicEvent event,final Object choiceResults[]) {
assert choiceResults != null : "ERROR! choiceResults is null in executeEvent"; assert choiceResults != null : "ERROR! choiceResults is null in executeEvent";
@ -763,14 +767,13 @@ public class MagicGame {
} }
public void checkLegendRule(final MagicPermanent permanent) { public void checkLegendRule(final MagicPermanent permanent) {
final MagicCardDefinition cardDefinition=permanent.getCardDefinition(); final MagicCardDefinition cardDefinition=permanent.getCardDefinition();
if (cardDefinition.hasType(MagicType.Legendary)&&getCount(cardDefinition.getIndex())>1) { if (cardDefinition.hasType(MagicType.Legendary) &&
getCount(cardDefinition.getIndex()) > 1) {
final String message="Put "+cardDefinition.getName()+" into its owner's graveyard (legend rule)."; final String message="Put "+cardDefinition.getName()+" into its owner's graveyard (legend rule).";
final MagicTargetFilter targetFilter=new MagicTargetFilter.CardTargetFilter(cardDefinition); final MagicTargetFilter targetFilter=new MagicTargetFilter.CardTargetFilter(cardDefinition);
final Collection<MagicTarget> targets=filterTargets(permanent.getController(),targetFilter); final Collection<MagicTarget> targets=filterTargets(permanent.getController(),targetFilter);
for (final MagicTarget target : targets) { for (final MagicTarget target : targets) {
final MagicPermanent targetPermanent=(MagicPermanent)target; final MagicPermanent targetPermanent=(MagicPermanent)target;
logAppendMessage(targetPermanent.getController(),message); logAppendMessage(targetPermanent.getController(),message);
doAction(new MagicRemoveFromPlayAction(targetPermanent,MagicLocationType.Graveyard)); doAction(new MagicRemoveFromPlayAction(targetPermanent,MagicLocationType.Graveyard));
@ -779,11 +782,9 @@ public class MagicGame {
} }
public Object[] map(final Object data[]) { public Object[] map(final Object data[]) {
final int length=data.length; final int length=data.length;
final Object mappedData[]=new Object[length]; final Object mappedData[]=new Object[length];
for (int index=0;index<length;index++) { for (int index=0;index<length;index++) {
final Object obj=data[index]; final Object obj=data[index];
if (obj!=null&&obj instanceof MagicMappable) { if (obj!=null&&obj instanceof MagicMappable) {
mappedData[index]=((MagicMappable)obj).map(this); mappedData[index]=((MagicMappable)obj).map(this);
@ -796,7 +797,10 @@ public class MagicGame {
// ***** TARGETS ***** // ***** TARGETS *****
public List<MagicTarget> filterTargets(final MagicPlayer player,final MagicTargetFilter targetFilter,final MagicTargetHint targetHint) { public List<MagicTarget> filterTargets(
final MagicPlayer player,
final MagicTargetFilter targetFilter,
final MagicTargetHint targetHint) {
final List<MagicTarget> targets=new ArrayList<MagicTarget>(); final List<MagicTarget> targets=new ArrayList<MagicTarget>();
@ -857,18 +861,21 @@ public class MagicGame {
} }
public List<MagicTarget> filterTargets(final MagicPlayer player,final MagicTargetFilter targetFilter) { public List<MagicTarget> filterTargets(final MagicPlayer player,final MagicTargetFilter targetFilter) {
return filterTargets(player,targetFilter,MagicTargetHint.None); return filterTargets(player,targetFilter,MagicTargetHint.None);
} }
public boolean hasLegalTargets(final MagicPlayer player,final MagicSource source,final MagicTargetChoice targetChoice,final boolean hints) { public boolean hasLegalTargets(
final MagicPlayer player,
final MagicSource source,
final MagicTargetChoice targetChoice,
final boolean hints) {
final Collection<MagicTarget> targets=filterTargets(player,targetChoice.getTargetFilter(),targetChoice.getTargetHint(hints)); final Collection<MagicTarget> targets =
filterTargets(player,targetChoice.getTargetFilter(),targetChoice.getTargetHint(hints));
if (!targetChoice.isTargeted()) { if (!targetChoice.isTargeted()) {
return !targets.isEmpty(); return !targets.isEmpty();
} }
for (final MagicTarget target : targets) { for (final MagicTarget target : targets) {
if (target.isValidTarget(this,source)) { if (target.isValidTarget(this,source)) {
return true; return true;
} }
@ -876,15 +883,17 @@ public class MagicGame {
return false; return false;
} }
public List<Object> getLegalTargets(final MagicPlayer player,final MagicSource source, public List<Object> getLegalTargets(
final MagicTargetChoice targetChoice,final MagicTargetHint targetHint) { final MagicPlayer player,
final MagicSource source,
final MagicTargetChoice targetChoice,
final MagicTargetHint targetHint) {
final Collection<MagicTarget> targets=filterTargets(player,targetChoice.getTargetFilter(),targetHint); final Collection<MagicTarget> targets=filterTargets(player,targetChoice.getTargetFilter(),targetHint);
final List<Object> options; final List<Object> options;
if (targetChoice.isTargeted()) { if (targetChoice.isTargeted()) {
options=new ArrayList<Object>(); options=new ArrayList<Object>();
for (final MagicTarget target : targets) { for (final MagicTarget target : targets) {
if (target.isValidTarget(this,source)) { if (target.isValidTarget(this,source)) {
options.add(target); options.add(target);
} }
@ -901,7 +910,9 @@ public class MagicGame {
public boolean filterTarget(final MagicPlayer player,final MagicTargetFilter targetFilter,final MagicTarget target) { public boolean filterTarget(final MagicPlayer player,final MagicTargetFilter targetFilter,final MagicTarget target) {
if (target==null||target==MagicTargetNone.getInstance()||!targetFilter.accept(this,player,target)) { if (target==null ||
target==MagicTargetNone.getInstance() ||
!targetFilter.accept(this,player,target)) {
return false; return false;
} }
@ -919,12 +930,14 @@ public class MagicGame {
// Card // Card
if (target instanceof MagicCard) { if (target instanceof MagicCard) {
// Card in graveyard // Card in graveyard
if (targetFilter.acceptType(MagicTargetType.Graveyard)&&player.getGraveyard().contains(target)) { if (targetFilter.acceptType(MagicTargetType.Graveyard) &&
player.getGraveyard().contains(target)) {
return true; return true;
} }
// Card in opponent's graveyard // Card in opponent's graveyard
if (targetFilter.acceptType(MagicTargetType.OpponentsGraveyard)&&getOpponent(player).getGraveyard().contains(target)) { if (targetFilter.acceptType(MagicTargetType.OpponentsGraveyard) &&
getOpponent(player).getGraveyard().contains(target)) {
return true; return true;
} }
@ -939,7 +952,11 @@ public class MagicGame {
return false; return false;
} }
public boolean isLegalTarget(final MagicPlayer player,final MagicSource source,final MagicTargetChoice targetChoice,final MagicTarget target) { public boolean isLegalTarget(
final MagicPlayer player,
final MagicSource source,
final MagicTargetChoice targetChoice,
final MagicTarget target) {
if (filterTarget(player,targetChoice.getTargetFilter(),target)) { if (filterTarget(player,targetChoice.getTargetFilter(),target)) {
return !targetChoice.isTargeted()||target.isValidTarget(this,source); return !targetChoice.isTargeted()||target.isValidTarget(this,source);
@ -951,12 +968,10 @@ public class MagicGame {
/** Executes triggers immediately when they have no choices, otherwise ignore them. */ /** Executes triggers immediately when they have no choices, otherwise ignore them. */
public void setImmediate(final boolean immediate) { public void setImmediate(final boolean immediate) {
this.immediate=immediate; this.immediate=immediate;
} }
public MagicPermanentTrigger addTrigger(final MagicPermanent permanent,final MagicTrigger trigger) { public MagicPermanentTrigger addTrigger(final MagicPermanent permanent,final MagicTrigger trigger) {
final long id=createIdentifier(MagicIdentifierType.PermanentTrigger); final long id=createIdentifier(MagicIdentifierType.PermanentTrigger);
final MagicPermanentTrigger permanentTrigger=new MagicPermanentTrigger(id,permanent,trigger); final MagicPermanentTrigger permanentTrigger=new MagicPermanentTrigger(id,permanent,trigger);
triggers.get(trigger.getType()).add(permanentTrigger); triggers.get(trigger.getType()).add(permanentTrigger);
@ -964,40 +979,33 @@ public class MagicGame {
} }
public void addTrigger(MagicPermanentTrigger permanentTrigger) { public void addTrigger(MagicPermanentTrigger permanentTrigger) {
triggers.get(permanentTrigger.getTrigger().getType()).add(permanentTrigger); triggers.get(permanentTrigger.getTrigger().getType()).add(permanentTrigger);
} }
public MagicPermanentTrigger addTurnTrigger(final MagicPermanent permanent,final MagicTrigger trigger) { public MagicPermanentTrigger addTurnTrigger(final MagicPermanent permanent,final MagicTrigger trigger) {
final MagicPermanentTrigger permanentTrigger=addTrigger(permanent,trigger); final MagicPermanentTrigger permanentTrigger=addTrigger(permanent,trigger);
turnTriggers.add(permanentTrigger); turnTriggers.add(permanentTrigger);
return permanentTrigger; return permanentTrigger;
} }
public void addTurnTriggers(final List<MagicPermanentTrigger> triggersList) { public void addTurnTriggers(final List<MagicPermanentTrigger> triggersList) {
for (final MagicPermanentTrigger permanentTrigger : triggersList) { for (final MagicPermanentTrigger permanentTrigger : triggersList) {
triggers.get(permanentTrigger.getTrigger().getType()).add(permanentTrigger); triggers.get(permanentTrigger.getTrigger().getType()).add(permanentTrigger);
} }
turnTriggers.addAll(triggersList); turnTriggers.addAll(triggersList);
} }
public void removeTurnTrigger(final MagicPermanentTrigger permanentTrigger) { public void removeTurnTrigger(final MagicPermanentTrigger permanentTrigger) {
triggers.get(permanentTrigger.getTrigger().getType()).remove(permanentTrigger); triggers.get(permanentTrigger.getTrigger().getType()).remove(permanentTrigger);
turnTriggers.remove(permanentTrigger); turnTriggers.remove(permanentTrigger);
} }
public List<MagicPermanentTrigger> removeTurnTriggers() { public List<MagicPermanentTrigger> removeTurnTriggers() {
if (turnTriggers.isEmpty()) { if (turnTriggers.isEmpty()) {
return Collections.<MagicPermanentTrigger>emptyList(); return Collections.<MagicPermanentTrigger>emptyList();
} }
final MagicPermanentTriggerList removedTriggers=new MagicPermanentTriggerList(turnTriggers); final MagicPermanentTriggerList removedTriggers=new MagicPermanentTriggerList(turnTriggers);
for (final MagicPermanentTrigger permanentTrigger : turnTriggers) { for (final MagicPermanentTrigger permanentTrigger : turnTriggers) {
triggers.get(permanentTrigger.getTrigger().getType()).remove(permanentTrigger); triggers.get(permanentTrigger.getTrigger().getType()).remove(permanentTrigger);
} }
turnTriggers.clear(); turnTriggers.clear();
@ -1005,11 +1013,8 @@ public class MagicGame {
} }
public void removeTriggers(final MagicPermanent permanent,final Collection<MagicPermanentTrigger> removedTriggers) { public void removeTriggers(final MagicPermanent permanent,final Collection<MagicPermanentTrigger> removedTriggers) {
for (final MagicTriggerType type : triggers.keySet()) { for (final MagicTriggerType type : triggers.keySet()) {
for (final Iterator<MagicPermanentTrigger> iterator=triggers.get(type).iterator();iterator.hasNext();) { for (final Iterator<MagicPermanentTrigger> iterator=triggers.get(type).iterator();iterator.hasNext();) {
final MagicPermanentTrigger permanentTrigger=iterator.next(); final MagicPermanentTrigger permanentTrigger=iterator.next();
if (permanentTrigger.getPermanent()==permanent) { if (permanentTrigger.getPermanent()==permanent) {
iterator.remove(); iterator.remove();
@ -1021,7 +1026,11 @@ public class MagicGame {
} }
} }
public void executeTrigger(final MagicTrigger trigger,final MagicPermanent permanent,final MagicSource source,final Object data) { public void executeTrigger(
final MagicTrigger trigger,
final MagicPermanent permanent,
final MagicSource source,
final Object data) {
final MagicEvent event=trigger.executeTrigger(this,permanent,data); final MagicEvent event=trigger.executeTrigger(this,permanent,data);
if (event!=null) { if (event!=null) {
@ -1038,16 +1047,13 @@ public class MagicGame {
} }
public void executeTrigger(final MagicTriggerType type,final Object data) { public void executeTrigger(final MagicTriggerType type,final Object data) {
final SortedSet<MagicPermanentTrigger> typeTriggers=triggers.get(type); final SortedSet<MagicPermanentTrigger> typeTriggers=triggers.get(type);
if (typeTriggers.isEmpty()) { if (typeTriggers.isEmpty()) {
return; return;
} }
final Collection<MagicPermanentTrigger> copiedTriggers=new ArrayList<MagicPermanentTrigger>(typeTriggers); final Collection<MagicPermanentTrigger> copiedTriggers=new ArrayList<MagicPermanentTrigger>(typeTriggers);
for (final MagicPermanentTrigger permanentTrigger : copiedTriggers) { for (final MagicPermanentTrigger permanentTrigger : copiedTriggers) {
final MagicPermanent permanent=permanentTrigger.getPermanent(); final MagicPermanent permanent=permanentTrigger.getPermanent();
executeTrigger(permanentTrigger.getTrigger(),permanent,permanent,data); executeTrigger(permanentTrigger.getTrigger(),permanent,permanent,data);
} }