* Some changes to improve mana use of AI.

This commit is contained in:
LevelX2 2018-05-21 11:50:49 +02:00
parent c3d0297045
commit 3972695428
7 changed files with 297 additions and 62 deletions

View file

@ -31,9 +31,13 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import mage.Mana;
import mage.abilities.Ability;
import mage.abilities.costs.Cost;
import mage.abilities.costs.common.TapSourceCost;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.ManaEvent;
/**
*
@ -64,43 +68,51 @@ public class ManaOptions extends ArrayList<Mana> {
//if there is only one mana option available add it to all the existing options
List<Mana> netManas = abilities.get(0).getNetMana(game);
if (netManas.size() == 1) {
addMana(netManas.get(0));
if (!hasTapCost(abilities.get(0)) || checkTappedForManaReplacement(abilities.get(0), game, netManas.get(0))) {
addMana(netManas.get(0));
}
} else {
List<Mana> copy = copy();
this.clear();
boolean hasTapCost = hasTapCost(abilities.get(0));
for (Mana netMana : netManas) {
for (Mana mana : copy) {
Mana newMana = new Mana();
newMana.add(mana);
newMana.add(netMana);
this.add(newMana);
if (!hasTapCost || checkTappedForManaReplacement(abilities.get(0), game, netMana)) {
Mana newMana = new Mana();
newMana.add(mana);
newMana.add(netMana);
this.add(newMana);
}
}
}
}
} else if (abilities.size() > 1) {
} else { // mana source has more than 1 ability
//perform a union of all existing options and the new options
List<Mana> copy = copy();
this.clear();
for (ActivatedManaAbilityImpl ability : abilities) {
boolean hasTapCost = hasTapCost(ability);
for (Mana netMana : ability.getNetMana(game)) {
SkipAddMana:
for (Mana mana : copy) {
Mana newMana = new Mana();
newMana.add(mana);
newMana.add(netMana);
for (Mana existingMana : this) {
if (existingMana.equalManaValue(newMana)) {
continue SkipAddMana;
}
Mana moreValuable = Mana.getMoreValuableMana(newMana, existingMana);
if (moreValuable != null) {
// only keep the more valuable mana
existingMana.setToMana(moreValuable);
continue SkipAddMana;
if (!hasTapCost || checkTappedForManaReplacement(ability, game, netMana)) {
SkipAddMana:
for (Mana mana : copy) {
Mana newMana = new Mana();
newMana.add(mana);
newMana.add(netMana);
for (Mana existingMana : this) {
if (existingMana.equalManaValue(newMana)) {
continue SkipAddMana;
}
Mana moreValuable = Mana.getMoreValuableMana(newMana, existingMana);
if (moreValuable != null) {
// only keep the more valuable mana
existingMana.setToMana(moreValuable);
continue SkipAddMana;
}
}
this.add(newMana);
}
this.add(newMana);
}
}
}
@ -108,6 +120,23 @@ public class ManaOptions extends ArrayList<Mana> {
}
}
private boolean checkTappedForManaReplacement(Ability ability, Game game, Mana mana) {
ManaEvent event = new ManaEvent(GameEvent.EventType.TAPPED_FOR_MANA, ability.getSourceId(), ability.getSourceId(), ability.getControllerId(), mana);
if (!game.replaceEvent(event)) {
return true;
}
return false;
}
private boolean hasTapCost(Ability ability) {
for (Cost cost : ability.getCosts()) {
if (cost instanceof TapSourceCost) {
return true;
}
}
return false;
}
public void addManaWithCost(List<ActivatedManaAbilityImpl> abilities, Game game) {
if (isEmpty()) {
this.add(new Mana());
@ -153,37 +182,41 @@ public class ManaOptions extends ArrayList<Mana> {
List<Mana> copy = copy();
this.clear();
for (ActivatedManaAbilityImpl ability : abilities) {
boolean hasTapCost = hasTapCost(ability);
List<Mana> netManas = ability.getNetMana(game);
if (ability.getManaCosts().isEmpty()) {
for (Mana netMana : netManas) {
for (Mana mana : copy) {
Mana newMana = new Mana();
newMana.add(mana);
newMana.add(netMana);
this.add(newMana);
if (!hasTapCost || checkTappedForManaReplacement(ability, game, netMana)) {
for (Mana mana : copy) {
Mana newMana = new Mana();
newMana.add(mana);
newMana.add(netMana);
this.add(newMana);
}
}
}
} else {
for (Mana netMana : netManas) {
for (Mana previousMana : copy) {
CombineWithExisting:
for (Mana manaOption : ability.getManaCosts().getManaOptions()) {
Mana newMana = new Mana(previousMana);
if (previousMana.includesMana(manaOption)) { // costs can be paid
newMana.subtractCost(manaOption);
newMana.add(netMana);
// if the new mana is in all colors more than another already existing than replace
for (Mana existingMana : this) {
Mana moreValuable = Mana.getMoreValuableMana(newMana, existingMana);
if (moreValuable != null) {
existingMana.setToMana(moreValuable);
continue CombineWithExisting;
if (!hasTapCost || checkTappedForManaReplacement(ability, game, netMana)) {
for (Mana previousMana : copy) {
CombineWithExisting:
for (Mana manaOption : ability.getManaCosts().getManaOptions()) {
Mana newMana = new Mana(previousMana);
if (previousMana.includesMana(manaOption)) { // costs can be paid
newMana.subtractCost(manaOption);
newMana.add(netMana);
// if the new mana is in all colors more than another already existing than replace
for (Mana existingMana : this) {
Mana moreValuable = Mana.getMoreValuableMana(newMana, existingMana);
if (moreValuable != null) {
existingMana.setToMana(moreValuable);
continue CombineWithExisting;
}
}
// no existing Mana includes this new mana so add
this.add(newMana);
}
// no existing Mana includes this new mana so add
this.add(newMana);
}
}
}
@ -330,16 +363,15 @@ public class ManaOptions extends ArrayList<Mana> {
payCombinationsStrings.add(newMana.toString());
}
public void removeDuplicated(){
public void removeDuplicated() {
Set<String> list = new HashSet<>();
for(int i = this.size() - 1; i >= 0; i--){
for (int i = this.size() - 1; i >= 0; i--) {
String s = this.get(i).toString();
if (list.contains(s)){
if (list.contains(s)) {
// remove duplicated
this.remove(i);
}else{
} else {
list.add(s);
}
}