mirror of
https://github.com/magefree/mage.git
synced 2025-12-20 02:30:08 -08:00
moved Server plugins to their own folder
This commit is contained in:
parent
b131b836a1
commit
89a2e01068
35 changed files with 1 additions and 6 deletions
50
Mage.Server.Plugins/Mage.Deck.Constructed/pom.xml
Normal file
50
Mage.Server.Plugins/Mage.Deck.Constructed/pom.xml
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.mage</groupId>
|
||||
<artifactId>mage-root</artifactId>
|
||||
<version>0.5</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>Mage-Deck-Constructed</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Mage Deck Constructed</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>Mage</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
<finalName>mage-deck-constructed</finalName>
|
||||
</build>
|
||||
|
||||
<properties/>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.deck;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.cards.decks.DeckValidatorImpl;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class Constructed extends DeckValidatorImpl {
|
||||
|
||||
public Constructed() {
|
||||
super("Constructed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(Deck deck) {
|
||||
//20091005 - 100.2a
|
||||
if (deck.getCards().size() < 60)
|
||||
return false;
|
||||
//20091005 - 100.4a
|
||||
if (deck.getSideboard().size() != 0 && deck.getSideboard().size() != 15)
|
||||
return false;
|
||||
|
||||
List<String> basicLandNames = new ArrayList<String>(Arrays.asList("Forest", "Island", "Mountain", "Swamp", "Plains"));
|
||||
Map<String, Integer> counts = new HashMap<String, Integer>();
|
||||
countCards(counts, deck.getCards());
|
||||
countCards(counts, deck.getSideboard());
|
||||
for (Entry<String, Integer> entry: counts.entrySet()) {
|
||||
if (entry.getValue() > 4) {
|
||||
if (!basicLandNames.contains(entry.getKey()) && !entry.getKey().equals("Relentless Rats")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
50
Mage.Server.Plugins/Mage.Game.FreeForAll/pom.xml
Normal file
50
Mage.Server.Plugins/Mage.Game.FreeForAll/pom.xml
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.mage</groupId>
|
||||
<artifactId>mage-root</artifactId>
|
||||
<version>0.5</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>Mage-Game-FreeForAll</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Mage Game Free For All</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>Mage</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
<finalName>mage-game-freeforall</finalName>
|
||||
</build>
|
||||
|
||||
<properties/>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.game;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import mage.Constants.MultiplayerAttackOption;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.players.Player;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class FreeForAll extends GameImpl<FreeForAll> {
|
||||
|
||||
private int numPlayers;
|
||||
private List<UUID> mulliganed = new ArrayList<UUID>();
|
||||
|
||||
public FreeForAll(MultiplayerAttackOption attackOption, RangeOfInfluence range) {
|
||||
super(attackOption, range);
|
||||
}
|
||||
|
||||
public FreeForAll(final FreeForAll game) {
|
||||
super(game);
|
||||
this.numPlayers = game.numPlayers;
|
||||
for (UUID playerId: game.mulliganed) {
|
||||
mulliganed.add(playerId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameType getGameType() {
|
||||
return new FreeForAllType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumPlayers() {
|
||||
return numPlayers;
|
||||
}
|
||||
|
||||
public void setNumPlayers(int numPlayers) {
|
||||
this.numPlayers = numPlayers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLife() {
|
||||
return 20;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getOpponents(UUID playerId) {
|
||||
Set<UUID> opponents = new HashSet<UUID>();
|
||||
for (UUID opponentId: this.getPlayer(playerId).getInRange()) {
|
||||
if (!opponentId.equals(playerId))
|
||||
opponents.add(opponentId);
|
||||
}
|
||||
return opponents;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mulligan(UUID playerId) {
|
||||
Player player = getPlayer(playerId);
|
||||
int numCards = player.getHand().size();
|
||||
//record first mulligan and increment card count
|
||||
if (!mulliganed.contains(playerId)) {
|
||||
numCards += 1;
|
||||
mulliganed.add(playerId);
|
||||
}
|
||||
player.getLibrary().addAll(player.getHand().getCards(this), this);
|
||||
player.getHand().clear();
|
||||
player.shuffleLibrary(this);
|
||||
player.drawCards(numCards - 1, this);
|
||||
fireInformEvent(player.getName() + " mulligans down to " + Integer.toString(numCards - 1) + " cards");
|
||||
}
|
||||
|
||||
@Override
|
||||
public FreeForAll copy() {
|
||||
return new FreeForAll(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.game;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class FreeForAllType extends GameType {
|
||||
|
||||
public FreeForAllType() {
|
||||
this.name = "Free For All";
|
||||
this.maxPlayers = 10;
|
||||
this.minPlayers = 3;
|
||||
this.numTeams = 0;
|
||||
this.useAttackOption = true;
|
||||
this.useRange = true;
|
||||
}
|
||||
}
|
||||
51
Mage.Server.Plugins/Mage.Game.TwoPlayerDuel/pom.xml
Normal file
51
Mage.Server.Plugins/Mage.Game.TwoPlayerDuel/pom.xml
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.mage</groupId>
|
||||
<artifactId>mage-root</artifactId>
|
||||
<version>0.5</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>Mage-Game-TwoPlayerDuel</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Mage Game Two Player</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>Mage</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
<finalName>mage-game-twoplayerduel</finalName>
|
||||
</build>
|
||||
|
||||
<properties/>
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.game;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import mage.Constants.MultiplayerAttackOption;
|
||||
import mage.Constants.PhaseStep;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.game.turn.TurnMod;
|
||||
|
||||
public class TwoPlayerDuel extends GameImpl<TwoPlayerDuel> {
|
||||
|
||||
public TwoPlayerDuel(MultiplayerAttackOption attackOption, RangeOfInfluence range) {
|
||||
super(MultiplayerAttackOption.LEFT, RangeOfInfluence.ALL);
|
||||
}
|
||||
|
||||
public TwoPlayerDuel(final TwoPlayerDuel game) {
|
||||
super(game);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameType getGameType() {
|
||||
return new TwoPlayerDuelType();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNumPlayers() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLife() {
|
||||
return 20;
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public boolean playDrawStep(UUID activePlayerId) {
|
||||
// //20091005 - 103.7a
|
||||
// if (getTurnNum() != 1 || !activePlayerId.equals(startingPlayerId)) {
|
||||
// return super.playDrawStep(activePlayerId);
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
super.init();
|
||||
state.getTurnMods().add(new TurnMod(startingPlayerId, PhaseStep.DRAW));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void quit(UUID playerId) {
|
||||
super.quit(playerId);
|
||||
end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<UUID> getOpponents(UUID playerId) {
|
||||
Set<UUID> opponents = new HashSet<UUID>();
|
||||
for (UUID opponentId: this.getPlayer(playerId).getInRange()) {
|
||||
if (!opponentId.equals(playerId))
|
||||
opponents.add(opponentId);
|
||||
}
|
||||
return opponents;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TwoPlayerDuel copy() {
|
||||
return new TwoPlayerDuel(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.game;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class TwoPlayerDuelType extends GameType {
|
||||
|
||||
public TwoPlayerDuelType() {
|
||||
this.name = "Two Player Duel";
|
||||
this.maxPlayers = 2;
|
||||
this.minPlayers = 2;
|
||||
this.numTeams = 0;
|
||||
this.useAttackOption = false;
|
||||
this.useRange = false;
|
||||
}
|
||||
|
||||
}
|
||||
3
Mage.Server.Plugins/Mage.Player.AI/manifest.mf
Normal file
3
Mage.Server.Plugins/Mage.Player.AI/manifest.mf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
X-COMMENT: Main-Class will be added automatically by build
|
||||
|
||||
51
Mage.Server.Plugins/Mage.Player.AI/pom.xml
Normal file
51
Mage.Server.Plugins/Mage.Player.AI/pom.xml
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.mage</groupId>
|
||||
<artifactId>mage-root</artifactId>
|
||||
<version>0.5</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>Mage-Player-AI</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Mage Player AI</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>Mage</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
<finalName>mage-player-ai</finalName>
|
||||
</build>
|
||||
|
||||
<properties/>
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
import mage.game.permanent.Permanent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class Attackers extends TreeMap<Integer, List<Permanent>> {
|
||||
|
||||
public List<Permanent> getAttackers() {
|
||||
List<Permanent> attackers = new ArrayList<Permanent>();
|
||||
for (List<Permanent> l: this.values()) {
|
||||
for (Permanent permanent: l) {
|
||||
attackers.add(permanent);
|
||||
}
|
||||
}
|
||||
return attackers;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import mage.abilities.keyword.DoubleStrikeAbility;
|
||||
import mage.abilities.keyword.FirstStrikeAbility;
|
||||
import mage.abilities.keyword.TrampleAbility;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class CombatEvaluator {
|
||||
|
||||
//preserve calculations for efficiency
|
||||
private Map<UUID, Integer> values = new HashMap<UUID, Integer>();
|
||||
|
||||
public int evaluate(Permanent creature, Game game) {
|
||||
if (!values.containsKey(creature.getId())) {
|
||||
int value = 0;
|
||||
if (creature.canAttack(game))
|
||||
value += 2;
|
||||
value += creature.getPower().getValue();
|
||||
value += creature.getToughness().getValue();
|
||||
value += creature.getAbilities().getEvasionAbilities().size();
|
||||
value += creature.getAbilities().getProtectionAbilities().size();
|
||||
value += creature.getAbilities().containsKey(FirstStrikeAbility.getInstance().getId())?1:0;
|
||||
value += creature.getAbilities().containsKey(DoubleStrikeAbility.getInstance().getId())?2:0;
|
||||
value += creature.getAbilities().containsKey(TrampleAbility.getInstance().getId())?1:0;
|
||||
values.put(creature.getId(), value);
|
||||
}
|
||||
return values.get(creature.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,982 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.Constants.CardType;
|
||||
import mage.Constants.Outcome;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.Constants.Zone;
|
||||
import mage.MageObject;
|
||||
import mage.Mana;
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.ActivatedAbility;
|
||||
import mage.abilities.TriggeredAbilities;
|
||||
import mage.abilities.TriggeredAbility;
|
||||
import mage.abilities.costs.mana.ColoredManaCost;
|
||||
import mage.abilities.costs.mana.GenericManaCost;
|
||||
import mage.abilities.costs.mana.HybridManaCost;
|
||||
import mage.abilities.costs.mana.ManaCost;
|
||||
import mage.abilities.costs.mana.ManaCosts;
|
||||
import mage.abilities.costs.mana.MonoHybridManaCost;
|
||||
import mage.abilities.costs.mana.VariableManaCost;
|
||||
import mage.abilities.effects.Effect;
|
||||
import mage.abilities.effects.ReplacementEffect;
|
||||
import mage.abilities.effects.common.BecomesCreatureSourceEOTEffect;
|
||||
import mage.abilities.effects.common.DamageTargetEffect;
|
||||
import mage.abilities.keyword.DoubleStrikeAbility;
|
||||
import mage.abilities.keyword.EquipAbility;
|
||||
import mage.abilities.keyword.FirstStrikeAbility;
|
||||
import mage.abilities.keyword.TrampleAbility;
|
||||
import mage.abilities.mana.ManaAbility;
|
||||
import mage.abilities.mana.ManaOptions;
|
||||
import mage.player.ai.simulators.CombatGroupSimulator;
|
||||
import mage.player.ai.simulators.CombatSimulator;
|
||||
import mage.player.ai.simulators.CreatureSimulator;
|
||||
import mage.cards.Card;
|
||||
import mage.cards.Cards;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.choices.Choice;
|
||||
import mage.filter.Filter;
|
||||
import mage.filter.FilterPermanent;
|
||||
import mage.filter.common.FilterCreatureForCombat;
|
||||
import mage.filter.common.FilterCreaturePermanent;
|
||||
import mage.filter.common.FilterLandCard;
|
||||
import mage.filter.common.FilterNonlandCard;
|
||||
import mage.game.Game;
|
||||
import mage.game.combat.CombatGroup;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.players.Player;
|
||||
import mage.players.PlayerImpl;
|
||||
import mage.target.Target;
|
||||
import mage.target.TargetAmount;
|
||||
import mage.target.TargetCard;
|
||||
import mage.target.TargetPermanent;
|
||||
import mage.target.TargetPlayer;
|
||||
import mage.target.common.TargetDiscard;
|
||||
import mage.target.common.TargetControlledPermanent;
|
||||
import mage.target.common.TargetCreatureOrPlayerAmount;
|
||||
import mage.util.Copier;
|
||||
import mage.util.Logging;
|
||||
import mage.util.TreeNode;
|
||||
|
||||
/**
|
||||
*
|
||||
* suitable for two player games and some multiplayer games
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class ComputerPlayer<T extends ComputerPlayer<T>> extends PlayerImpl<T> implements Player {
|
||||
|
||||
private final static transient Logger logger = Logging.getLogger(ComputerPlayer.class.getName());
|
||||
private boolean abort;
|
||||
private transient Map<Mana, Card> unplayable = new TreeMap<Mana, Card>();
|
||||
private transient List<Card> playableNonInstant = new ArrayList<Card>();
|
||||
private transient List<Card> playableInstant = new ArrayList<Card>();
|
||||
private transient List<ActivatedAbility> playableAbilities = new ArrayList<ActivatedAbility>();
|
||||
|
||||
public ComputerPlayer(String name, Deck deck, RangeOfInfluence range) {
|
||||
super(name, deck, range);
|
||||
human = false;
|
||||
}
|
||||
|
||||
protected ComputerPlayer(UUID id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
public ComputerPlayer(final ComputerPlayer player) {
|
||||
super(player);
|
||||
this.abort = player.abort;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseMulligan(Game game) {
|
||||
logger.fine("chooseMulligan");
|
||||
if (hand.size() < 6)
|
||||
return false;
|
||||
Set<Card> lands = hand.getCards(new FilterLandCard(), game);
|
||||
if (lands.size() < 2 || lands.size() > hand.size() - 2)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean choose(Outcome outcome, Target target, Game game) {
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("chooseTarget: " + outcome.toString() + ":" + target.toString());
|
||||
UUID opponentId = game.getOpponents(playerId).iterator().next();
|
||||
if (target instanceof TargetPlayer) {
|
||||
if (outcome.isGood()) {
|
||||
if (target.canTarget(playerId, game)) {
|
||||
target.add(playerId, game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (target.canTarget(playerId, game)) {
|
||||
target.add(opponentId, game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (target instanceof TargetDiscard) {
|
||||
findPlayables(game);
|
||||
if (unplayable.size() > 0) {
|
||||
for (int i = unplayable.size() - 1; i >= 0; i--) {
|
||||
if (target.canTarget(unplayable.values().toArray(new Card[0])[i].getId(), game)) {
|
||||
target.add(unplayable.values().toArray(new Card[0])[i].getId(), game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hand.size() > 0) {
|
||||
if (target.canTarget(hand.toArray(new UUID[0])[0], game)) {
|
||||
target.add(hand.toArray(new UUID[0])[0], game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (target instanceof TargetControlledPermanent) {
|
||||
List<Permanent> targets;
|
||||
targets = threats(playerId, ((TargetPermanent)target).getFilter(), game);
|
||||
if (!outcome.isGood())
|
||||
Collections.reverse(targets);
|
||||
for (Permanent permanent: targets) {
|
||||
if (target.canTarget(permanent.getId(), game) && !target.getTargets().contains(permanent.getId())) {
|
||||
target.add(permanent.getId(), game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (target instanceof TargetPermanent) {
|
||||
List<Permanent> targets;
|
||||
if (outcome.isGood()) {
|
||||
targets = threats(playerId, ((TargetPermanent)target).getFilter(), game);
|
||||
}
|
||||
else {
|
||||
targets = threats(opponentId, ((TargetPermanent)target).getFilter(), game);
|
||||
}
|
||||
for (Permanent permanent: targets) {
|
||||
if (target.canTarget(permanent.getId(), game) && !target.getTargets().contains(permanent.getId())) {
|
||||
target.add(permanent.getId(), game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseTarget(Outcome outcome, Target target, Ability source, Game game) {
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("chooseTarget: " + outcome.toString() + ":" + target.toString());
|
||||
UUID opponentId = game.getOpponents(playerId).iterator().next();
|
||||
if (target instanceof TargetPlayer) {
|
||||
if (outcome.isGood()) {
|
||||
if (target.canTarget(playerId, source, game)) {
|
||||
target.addTarget(playerId, source, game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (target.canTarget(playerId, source, game)) {
|
||||
target.addTarget(opponentId, source, game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (target instanceof TargetDiscard) {
|
||||
findPlayables(game);
|
||||
if (unplayable.size() > 0) {
|
||||
for (int i = unplayable.size() - 1; i >= 0; i--) {
|
||||
if (target.canTarget(unplayable.values().toArray(new Card[0])[i].getId(), source, game)) {
|
||||
target.addTarget(unplayable.values().toArray(new Card[0])[i].getId(), source, game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hand.size() > 0) {
|
||||
if (target.canTarget(hand.toArray(new UUID[0])[0], source, game)) {
|
||||
target.addTarget(hand.toArray(new UUID[0])[0], source, game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (target instanceof TargetControlledPermanent) {
|
||||
List<Permanent> targets;
|
||||
targets = threats(playerId, ((TargetPermanent)target).getFilter(), game);
|
||||
if (!outcome.isGood())
|
||||
Collections.reverse(targets);
|
||||
for (Permanent permanent: targets) {
|
||||
if (target.canTarget(permanent.getId(), source, game)) {
|
||||
target.addTarget(permanent.getId(), source, game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (target instanceof TargetPermanent) {
|
||||
List<Permanent> targets;
|
||||
if (outcome.isGood()) {
|
||||
targets = threats(playerId, ((TargetPermanent)target).getFilter(), game);
|
||||
}
|
||||
else {
|
||||
targets = threats(opponentId, ((TargetPermanent)target).getFilter(), game);
|
||||
}
|
||||
for (Permanent permanent: targets) {
|
||||
if (target.canTarget(permanent.getId(), source, game)) {
|
||||
target.addTarget(permanent.getId(), source, game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability source, Game game) {
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("chooseTarget: " + outcome.toString() + ":" + target.toString());
|
||||
UUID opponentId = game.getOpponents(playerId).iterator().next();
|
||||
if (target instanceof TargetCreatureOrPlayerAmount) {
|
||||
if (game.getPlayer(opponentId).getLife() <= target.getAmountRemaining()) {
|
||||
target.addTarget(opponentId, target.getAmountRemaining(), source, game);
|
||||
return true;
|
||||
}
|
||||
List<Permanent> targets;
|
||||
if (outcome.isGood()) {
|
||||
targets = threats(playerId, FilterCreaturePermanent.getDefault(), game);
|
||||
}
|
||||
else {
|
||||
targets = threats(opponentId, FilterCreaturePermanent.getDefault(), game);
|
||||
}
|
||||
for (Permanent permanent: targets) {
|
||||
if (target.canTarget(permanent.getId(), source, game)) {
|
||||
if (permanent.getToughness().getValue() <= target.getAmountRemaining()) {
|
||||
target.addTarget(permanent.getId(), permanent.getToughness().getValue(), source, game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void priority(Game game) {
|
||||
logger.fine("priority");
|
||||
UUID opponentId = game.getOpponents(playerId).iterator().next();
|
||||
if (game.getActivePlayerId().equals(playerId)) {
|
||||
if (game.isMainPhase() && game.getStack().isEmpty()) {
|
||||
playLand(game);
|
||||
}
|
||||
switch (game.getTurn().getStepType()) {
|
||||
case UPKEEP:
|
||||
findPlayables(game);
|
||||
break;
|
||||
case DRAW:
|
||||
logState(game);
|
||||
case PRECOMBAT_MAIN:
|
||||
findPlayables(game);
|
||||
if (playableAbilities.size() > 0) {
|
||||
for (ActivatedAbility ability: playableAbilities) {
|
||||
if (ability.canActivate(playerId, game)) {
|
||||
if (ability.getEffects().hasOutcome(Outcome.PutLandInPlay)) {
|
||||
if (this.activateAbility(ability, game))
|
||||
return;
|
||||
}
|
||||
if (ability.getEffects().hasOutcome(Outcome.PutCreatureInPlay)) {
|
||||
if (getOpponentBlockers(opponentId, game).size() <= 1)
|
||||
if (this.activateAbility(ability, game))
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DECLARE_BLOCKERS:
|
||||
playRemoval(game.getCombat().getBlockers(), game);
|
||||
playDamage(game.getCombat().getBlockers(), game);
|
||||
case END_COMBAT:
|
||||
playDamage(game.getCombat().getBlockers(), game);
|
||||
case POSTCOMBAT_MAIN:
|
||||
findPlayables(game);
|
||||
if (game.getStack().isEmpty()) {
|
||||
if (playableNonInstant.size() > 0) {
|
||||
for (Card card: playableNonInstant) {
|
||||
if (card.getSpellAbility().canActivate(playerId, game)) {
|
||||
if (this.activateAbility(card.getSpellAbility(), game))
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (playableAbilities.size() > 0) {
|
||||
for (ActivatedAbility ability: playableAbilities) {
|
||||
if (ability.canActivate(playerId, game)) {
|
||||
if (!(ability.getEffects().get(0) instanceof BecomesCreatureSourceEOTEffect)) {
|
||||
if (this.activateAbility(ability, game))
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
//respond to opponent events
|
||||
switch (game.getTurn().getStepType()) {
|
||||
case UPKEEP:
|
||||
findPlayables(game);
|
||||
case DECLARE_ATTACKERS:
|
||||
playRemoval(game.getCombat().getAttackers(), game);
|
||||
playDamage(game.getCombat().getAttackers(), game);
|
||||
case END_COMBAT:
|
||||
playDamage(game.getCombat().getAttackers(), game);
|
||||
}
|
||||
}
|
||||
pass();
|
||||
}
|
||||
|
||||
protected void playLand(Game game) {
|
||||
logger.fine("playLand");
|
||||
Set<Card> lands = hand.getCards(new FilterLandCard(), game);
|
||||
while (lands.size() > 0 && this.landsPlayed < this.landsPerTurn) {
|
||||
if (lands.size() == 1)
|
||||
this.playLand(lands.iterator().next(), game);
|
||||
else {
|
||||
playALand(lands, game);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void playALand(Set<Card> lands, Game game) {
|
||||
logger.fine("playALand");
|
||||
//play a land that will allow us to play an unplayable
|
||||
for (Mana mana: unplayable.keySet()) {
|
||||
for (Card card: lands) {
|
||||
for (ManaAbility ability: card.getAbilities().getManaAbilities(Zone.BATTLEFIELD)) {
|
||||
if (ability.getNetMana(game).enough(mana)) {
|
||||
this.playLand(card, game);
|
||||
lands.remove(card);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//play a land that will get us closer to playing an unplayable
|
||||
for (Mana mana: unplayable.keySet()) {
|
||||
for (Card card: lands) {
|
||||
for (ManaAbility ability: card.getAbilities().getManaAbilities(Zone.BATTLEFIELD)) {
|
||||
if (mana.contains(ability.getNetMana(game))) {
|
||||
this.playLand(card, game);
|
||||
lands.remove(card);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//play first available land
|
||||
this.playLand(lands.iterator().next(), game);
|
||||
lands.remove(lands.iterator().next());
|
||||
}
|
||||
|
||||
protected void findPlayables(Game game) {
|
||||
playableInstant.clear();
|
||||
playableNonInstant.clear();
|
||||
unplayable.clear();
|
||||
playableAbilities.clear();
|
||||
Set<Card> nonLands = hand.getCards(new FilterNonlandCard(), game);
|
||||
ManaOptions available = getManaAvailable(game);
|
||||
available.addMana(manaPool.getMana());
|
||||
|
||||
for (Card card: nonLands) {
|
||||
ManaOptions options = card.getManaCost().getOptions();
|
||||
if (card.getManaCost().getVariableCosts().size() > 0) {
|
||||
//don't use variable mana costs unless there is at least 3 extra mana for X
|
||||
for (Mana option: options) {
|
||||
option.add(Mana.ColorlessMana(3));
|
||||
}
|
||||
}
|
||||
for (Mana mana: options) {
|
||||
for (Mana avail: available) {
|
||||
if (mana.enough(avail)) {
|
||||
if (card.getCardType().contains(CardType.INSTANT))
|
||||
playableInstant.add(card);
|
||||
else
|
||||
playableNonInstant.add(card);
|
||||
}
|
||||
else {
|
||||
if (!playableInstant.contains(card) && !playableNonInstant.contains(card))
|
||||
unplayable.put(mana.needed(avail), card);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Permanent permanent: game.getBattlefield().getAllActivePermanents(playerId)) {
|
||||
for (ActivatedAbility ability: permanent.getAbilities().getActivatedAbilities(Zone.BATTLEFIELD)) {
|
||||
if (!(ability instanceof ManaAbility) && ability.canActivate(playerId, game)) {
|
||||
if (ability instanceof EquipAbility && permanent.getAttachedTo() != null)
|
||||
continue;
|
||||
ManaOptions abilityOptions = ability.getManaCosts().getOptions();
|
||||
if (ability.getManaCosts().getVariableCosts().size() > 0) {
|
||||
//don't use variable mana costs unless there is at least 3 extra mana for X
|
||||
for (Mana option: abilityOptions) {
|
||||
option.add(Mana.ColorlessMana(3));
|
||||
}
|
||||
}
|
||||
if (abilityOptions.size() == 0) {
|
||||
playableAbilities.add(ability);
|
||||
}
|
||||
else {
|
||||
for (Mana mana: abilityOptions) {
|
||||
for (Mana avail: available) {
|
||||
if (mana.enough(avail)) {
|
||||
playableAbilities.add(ability);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Card card: graveyard.getCards(game)) {
|
||||
for (ActivatedAbility ability: card.getAbilities().getActivatedAbilities(Zone.GRAVEYARD)) {
|
||||
if (ability.canActivate(playerId, game)) {
|
||||
ManaOptions abilityOptions = ability.getManaCosts().getOptions();
|
||||
if (abilityOptions.size() == 0) {
|
||||
playableAbilities.add(ability);
|
||||
}
|
||||
else {
|
||||
for (Mana mana: abilityOptions) {
|
||||
for (Mana avail: available) {
|
||||
if (mana.enough(avail)) {
|
||||
playableAbilities.add(ability);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("findPlayables: " + playableInstant.toString() + "---" + playableNonInstant.toString() + "---" + playableAbilities.toString() );
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ManaOptions getManaAvailable(Game game) {
|
||||
// logger.fine("getManaAvailable");
|
||||
return super.getManaAvailable(game);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean playMana(ManaCost unpaid, Game game) {
|
||||
// logger.fine("playMana");
|
||||
ManaCost cost;
|
||||
List<Permanent> producers;
|
||||
if (unpaid instanceof ManaCosts) {
|
||||
cost = ((ManaCosts<ManaCost>)unpaid).get(0);
|
||||
producers = getSortedProducers((ManaCosts)unpaid, game);
|
||||
}
|
||||
else {
|
||||
cost = unpaid;
|
||||
producers = this.getAvailableManaProducers(game);
|
||||
}
|
||||
for (Permanent perm: producers) {
|
||||
// pay all colored costs first
|
||||
for (ManaAbility ability: perm.getAbilities().getManaAbilities(Zone.BATTLEFIELD)) {
|
||||
if (cost instanceof ColoredManaCost) {
|
||||
if (cost.testPay(ability.getNetMana(game))) {
|
||||
if (activateAbility(ability, game))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// then pay hybrid
|
||||
for (ManaAbility ability: perm.getAbilities().getManaAbilities(Zone.BATTLEFIELD)) {
|
||||
if (cost instanceof HybridManaCost) {
|
||||
if (cost.testPay(ability.getNetMana(game))) {
|
||||
if (activateAbility(ability, game))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// then pay mono hybrid
|
||||
for (ManaAbility ability: perm.getAbilities().getManaAbilities(Zone.BATTLEFIELD)) {
|
||||
if (cost instanceof MonoHybridManaCost) {
|
||||
if (cost.testPay(ability.getNetMana(game))) {
|
||||
if (activateAbility(ability, game))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// finally pay generic
|
||||
for (ManaAbility ability: perm.getAbilities().getManaAbilities(Zone.BATTLEFIELD)) {
|
||||
if (cost instanceof GenericManaCost) {
|
||||
if (cost.testPay(ability.getNetMana(game))) {
|
||||
if (activateAbility(ability, game))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* returns a list of Permanents that produce mana sorted by the number of mana the Permanent produces
|
||||
* that match the unpaid costs in ascending order
|
||||
*
|
||||
* the idea is that we should pay costs first from mana producers that produce only one type of mana
|
||||
* and save the multi-mana producers for those costs that can't be paid by any other producers
|
||||
*
|
||||
* @param unpaid - the amount of unpaid mana costs
|
||||
* @param game
|
||||
* @return List<Permanent>
|
||||
*/
|
||||
private List<Permanent> getSortedProducers(ManaCosts<ManaCost> unpaid, Game game) {
|
||||
List<Permanent> unsorted = this.getAvailableManaProducers(game);
|
||||
Map<Permanent, Integer> scored = new HashMap<Permanent, Integer>();
|
||||
for (Permanent permanent: unsorted) {
|
||||
int score = 0;
|
||||
for (ManaCost cost: unpaid) {
|
||||
for (ManaAbility ability: permanent.getAbilities().getManaAbilities(Zone.BATTLEFIELD)) {
|
||||
if (cost.testPay(ability.getNetMana(game))) {
|
||||
score++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (score > 0) // score mana producers that produce other types higher
|
||||
score += permanent.getAbilities().getManaAbilities(Zone.BATTLEFIELD).size();
|
||||
scored.put(permanent, score);
|
||||
}
|
||||
return sortByValue(scored);
|
||||
}
|
||||
|
||||
private List<Permanent> sortByValue(Map<Permanent, Integer> map) {
|
||||
List<Entry<Permanent, Integer>> list = new LinkedList<Entry<Permanent, Integer>>(map.entrySet());
|
||||
Collections.sort(list, new Comparator<Entry<Permanent, Integer>>() {
|
||||
@Override
|
||||
public int compare(Entry<Permanent, Integer> o1, Entry<Permanent, Integer> o2) {
|
||||
return (o1.getValue().compareTo(o2.getValue()));
|
||||
}
|
||||
});
|
||||
List<Permanent> result = new ArrayList<Permanent>();
|
||||
for (Iterator it = list.iterator(); it.hasNext();) {
|
||||
Entry<Permanent, Integer> entry = (Entry<Permanent, Integer>)it.next();
|
||||
result.add(entry.getKey());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean playXMana(VariableManaCost cost, Game game) {
|
||||
logger.fine("playXMana");
|
||||
//put everything into X
|
||||
for (Permanent perm: this.getAvailableManaProducers(game)) {
|
||||
for (ManaAbility ability: perm.getAbilities().getManaAbilities(Zone.BATTLEFIELD)) {
|
||||
if (activateAbility(ability, game))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
cost.setPaid();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abort() {
|
||||
abort = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseUse(Outcome outcome, String message, Game game) {
|
||||
logger.fine("chooseUse");
|
||||
//TODO: improve this
|
||||
return outcome.isGood();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean choose(Outcome outcome, Choice choice, Game game) {
|
||||
logger.fine("choose");
|
||||
//TODO: improve this
|
||||
choice.setChoice(choice.getChoices().iterator().next());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseTarget(Cards cards, TargetCard target, Ability source, Game game) {
|
||||
logger.fine("chooseTarget");
|
||||
//TODO: improve this
|
||||
//return first match
|
||||
if (!target.doneChosing()) {
|
||||
for (Card card: cards.getCards(target.getFilter(), game)) {
|
||||
target.addTarget(card.getId(), source, game);
|
||||
if (target.doneChosing())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean choose(Cards cards, TargetCard target, Game game) {
|
||||
logger.fine("choose");
|
||||
//TODO: improve this
|
||||
//return first match
|
||||
if (!target.doneChosing()) {
|
||||
for (Card card: cards.getCards(target.getFilter(), game)) {
|
||||
target.add(card.getId(), game);
|
||||
if (target.doneChosing())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectAttackers(Game game) {
|
||||
logger.fine("selectAttackers");
|
||||
UUID opponentId = game.getCombat().getDefenders().iterator().next();
|
||||
Attackers attackers = getPotentialAttackers(game);
|
||||
List<Permanent> blockers = getOpponentBlockers(opponentId, game);
|
||||
List<Permanent> actualAttackers = new ArrayList<Permanent>();
|
||||
if (blockers.isEmpty()) {
|
||||
actualAttackers = attackers.getAttackers();
|
||||
}
|
||||
else if (attackers.size() - blockers.size() >= game.getPlayer(opponentId).getLife()) {
|
||||
actualAttackers = attackers.getAttackers();
|
||||
}
|
||||
else {
|
||||
CombatSimulator combat = simulateAttack(attackers, blockers, opponentId, game);
|
||||
if (combat.rating > 2) {
|
||||
for (CombatGroupSimulator group: combat.groups) {
|
||||
this.declareAttacker(group.attackers.get(0).id, group.defenderId, game);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Permanent attacker: actualAttackers) {
|
||||
this.declareAttacker(attacker.getId(), opponentId, game);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectBlockers(Game game) {
|
||||
logger.fine("selectBlockers");
|
||||
|
||||
List<Permanent> blockers = getAvailableBlockers(game);
|
||||
|
||||
CombatSimulator sim = simulateBlock(CombatSimulator.load(game), blockers, game);
|
||||
|
||||
List<CombatGroup> groups = game.getCombat().getGroups();
|
||||
for (int i = 0; i< groups.size(); i++) {
|
||||
for (CreatureSimulator creature: sim.groups.get(i).blockers) {
|
||||
groups.get(i).addBlocker(creature.id, playerId, game);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chooseEffect(List<ReplacementEffect> rEffects, Game game) {
|
||||
logger.fine("chooseEffect");
|
||||
//TODO: implement this
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TriggeredAbility chooseTriggeredAbility(TriggeredAbilities abilities, Game game) {
|
||||
logger.fine("chooseTriggeredAbility");
|
||||
//TODO: improve this
|
||||
if (abilities.size() > 0)
|
||||
return abilities.get(0);
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void assignDamage(int damage, List<UUID> targets, UUID sourceId, Game game) {
|
||||
logger.fine("assignDamage");
|
||||
//TODO: improve this
|
||||
game.getPermanent(targets.get(0)).damage(damage, sourceId, game, true, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAmount(int min, int max, String message, Game game) {
|
||||
logger.fine("getAmount");
|
||||
//TODO: improve this
|
||||
return min;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<Permanent> getAvailableManaProducers(Game game) {
|
||||
// logger.fine("getAvailableManaProducers");
|
||||
return super.getAvailableManaProducers(game);
|
||||
}
|
||||
|
||||
protected Attackers getPotentialAttackers(Game game) {
|
||||
logger.fine("getAvailableAttackers");
|
||||
Attackers attackers = new Attackers();
|
||||
List<Permanent> creatures = super.getAvailableAttackers(game);
|
||||
for (Permanent creature: creatures) {
|
||||
int potential = combatPotential(creature, game);
|
||||
if (potential > 0 && creature.getPower().getValue() > 0) {
|
||||
List<Permanent> l = attackers.get(potential);
|
||||
if (l == null)
|
||||
attackers.put(potential, l = new ArrayList<Permanent>());
|
||||
l.add(creature);
|
||||
}
|
||||
}
|
||||
return attackers;
|
||||
}
|
||||
|
||||
protected int combatPotential(Permanent creature, Game game) {
|
||||
logger.fine("combatPotential");
|
||||
if (!creature.canAttack(game))
|
||||
return 0;
|
||||
int potential = creature.getPower().getValue();
|
||||
potential += creature.getAbilities().getEvasionAbilities().size();
|
||||
potential += creature.getAbilities().getProtectionAbilities().size();
|
||||
potential += creature.getAbilities().containsKey(FirstStrikeAbility.getInstance().getId())?1:0;
|
||||
potential += creature.getAbilities().containsKey(DoubleStrikeAbility.getInstance().getId())?2:0;
|
||||
potential += creature.getAbilities().containsKey(TrampleAbility.getInstance().getId())?1:0;
|
||||
return potential;
|
||||
}
|
||||
|
||||
// protected List<Permanent> getAvailableBlockers(Game game) {
|
||||
// logger.fine("getAvailableBlockers");
|
||||
// FilterCreatureForCombat blockFilter = new FilterCreatureForCombat();
|
||||
// List<Permanent> blockers = game.getBattlefield().getAllActivePermanents(blockFilter, playerId);
|
||||
// return blockers;
|
||||
// }
|
||||
|
||||
protected List<Permanent> getOpponentBlockers(UUID opponentId, Game game) {
|
||||
logger.fine("getOpponentBlockers");
|
||||
FilterCreatureForCombat blockFilter = new FilterCreatureForCombat();
|
||||
List<Permanent> blockers = game.getBattlefield().getAllActivePermanents(blockFilter, opponentId);
|
||||
return blockers;
|
||||
}
|
||||
|
||||
protected CombatSimulator simulateAttack(Attackers attackers, List<Permanent> blockers, UUID opponentId, Game game) {
|
||||
logger.fine("simulateAttack");
|
||||
List<Permanent> attackersList = attackers.getAttackers();
|
||||
CombatSimulator best = new CombatSimulator();
|
||||
int bestResult = 0;
|
||||
//use binary digits to calculate powerset of attackers
|
||||
int powerElements = (int) Math.pow(2, attackersList.size());
|
||||
for (int i = 1; i < powerElements; i++) {
|
||||
String binary = Integer.toBinaryString(i);
|
||||
while(binary.length() < attackersList.size()) {
|
||||
binary = "0" + binary;
|
||||
}
|
||||
List<Permanent> trialAttackers = new ArrayList<Permanent>();
|
||||
for (int j = 0; j < attackersList.size(); j++) {
|
||||
if (binary.charAt(j) == '1')
|
||||
trialAttackers.add(attackersList.get(j));
|
||||
}
|
||||
CombatSimulator combat = new CombatSimulator();
|
||||
for (Permanent permanent: trialAttackers) {
|
||||
combat.groups.add(new CombatGroupSimulator(opponentId, Arrays.asList(permanent.getId()), new ArrayList<UUID>(), game));
|
||||
}
|
||||
CombatSimulator test = simulateBlock(combat, blockers, game);
|
||||
if (test.evaluate() > bestResult) {
|
||||
best = test;
|
||||
bestResult = test.evaluate();
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
protected CombatSimulator simulateBlock(CombatSimulator combat, List<Permanent> blockers, Game game) {
|
||||
logger.fine("simulateBlock");
|
||||
|
||||
TreeNode<CombatSimulator> simulations;
|
||||
|
||||
simulations = new TreeNode<CombatSimulator>(combat);
|
||||
addBlockSimulations(blockers, simulations, game);
|
||||
combat.simulate();
|
||||
|
||||
return getWorstSimulation(simulations);
|
||||
|
||||
}
|
||||
|
||||
protected void addBlockSimulations(List<Permanent> blockers, TreeNode<CombatSimulator> node, Game game) {
|
||||
int numGroups = node.getData().groups.size();
|
||||
Copier<CombatSimulator> copier = new Copier<CombatSimulator>();
|
||||
for (Permanent blocker: blockers) {
|
||||
List<Permanent> subList = remove(blockers, blocker);
|
||||
for (int i = 0; i < numGroups; i++) {
|
||||
if (node.getData().groups.get(i).canBlock(blocker, game)) {
|
||||
CombatSimulator combat = copier.copy(node.getData());
|
||||
combat.groups.get(i).blockers.add(new CreatureSimulator(blocker));
|
||||
TreeNode<CombatSimulator> child = new TreeNode<CombatSimulator>(combat);
|
||||
node.addChild(child);
|
||||
addBlockSimulations(subList, child, game);
|
||||
combat.simulate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected List<Permanent> remove(List<Permanent> source, Permanent element) {
|
||||
List<Permanent> newList = new ArrayList<Permanent>();
|
||||
for (Permanent permanent: source) {
|
||||
if (!permanent.equals(element)) {
|
||||
newList.add(permanent);
|
||||
}
|
||||
}
|
||||
return newList;
|
||||
}
|
||||
|
||||
protected CombatSimulator getBestSimulation(TreeNode<CombatSimulator> simulations) {
|
||||
CombatSimulator best = simulations.getData();
|
||||
int bestResult = best.evaluate();
|
||||
for (TreeNode<CombatSimulator> node: simulations.getChildren()) {
|
||||
CombatSimulator bestSub = getBestSimulation(node);
|
||||
if (bestSub.evaluate() > bestResult) {
|
||||
best = node.getData();
|
||||
bestResult = best.evaluate();
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
protected CombatSimulator getWorstSimulation(TreeNode<CombatSimulator> simulations) {
|
||||
CombatSimulator worst = simulations.getData();
|
||||
int worstResult = worst.evaluate();
|
||||
for (TreeNode<CombatSimulator> node: simulations.getChildren()) {
|
||||
CombatSimulator worstSub = getWorstSimulation(node);
|
||||
if (worstSub.evaluate() < worstResult) {
|
||||
worst = node.getData();
|
||||
worstResult = worst.evaluate();
|
||||
}
|
||||
}
|
||||
return worst;
|
||||
}
|
||||
|
||||
protected List<Permanent> threats(UUID playerId, FilterPermanent filter, Game game) {
|
||||
List<Permanent> threats = game.getBattlefield().getAllActivePermanents(filter, playerId);
|
||||
Collections.sort(threats, new PermanentComparator(game));
|
||||
return threats;
|
||||
}
|
||||
|
||||
protected void logState(Game game) {
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logList("computer player hand: ", new ArrayList(hand.getCards(game)));
|
||||
}
|
||||
|
||||
protected void logList(String message, List<MageObject> list) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(message).append(": ");
|
||||
for (MageObject object: list) {
|
||||
sb.append(object.getName()).append(",");
|
||||
}
|
||||
logger.fine(sb.toString());
|
||||
}
|
||||
|
||||
protected void logAbilityList(String message, List<Ability> list) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(message).append(": ");
|
||||
for (Ability ability: list) {
|
||||
sb.append(ability.getRule()).append(",");
|
||||
}
|
||||
logger.fine(sb.toString());
|
||||
}
|
||||
|
||||
private void playRemoval(List<UUID> creatures, Game game) {
|
||||
for (UUID creatureId: creatures) {
|
||||
for (Card card: this.playableInstant) {
|
||||
if (card.getSpellAbility().canActivate(playerId, game)) {
|
||||
for (Effect effect: card.getSpellAbility().getEffects()) {
|
||||
if (effect.getOutcome().equals(Outcome.DestroyPermanent) || effect.getOutcome().equals(Outcome.ReturnToHand)) {
|
||||
if (card.getSpellAbility().getTargets().get(0).canTarget(creatureId, card.getSpellAbility(), game)) {
|
||||
if (this.activateAbility(card.getSpellAbility(), game))
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void playDamage(List<UUID> creatures, Game game) {
|
||||
for (UUID creatureId: creatures) {
|
||||
Permanent creature = game.getPermanent(creatureId);
|
||||
for (Card card: this.playableInstant) {
|
||||
if (card.getSpellAbility().canActivate(playerId, game)) {
|
||||
for (Effect effect: card.getSpellAbility().getEffects()) {
|
||||
if (effect instanceof DamageTargetEffect) {
|
||||
if (card.getSpellAbility().getTargets().get(0).canTarget(creatureId, card.getSpellAbility(), game)) {
|
||||
if (((DamageTargetEffect)effect).getAmount() > (creature.getPower().getValue() - creature.getDamage())) {
|
||||
if (this.activateAbility(card.getSpellAbility(), game))
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||
in.defaultReadObject();
|
||||
unplayable = new TreeMap<Mana, Card>();
|
||||
playableNonInstant = new ArrayList<Card>();
|
||||
playableInstant = new ArrayList<Card>();
|
||||
playableAbilities = new ArrayList<ActivatedAbility>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T copy() {
|
||||
return (T)new ComputerPlayer(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.Comparator;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class PermanentComparator implements Comparator<Permanent> {
|
||||
|
||||
private Game game;
|
||||
private PermanentEvaluator evaluator = new PermanentEvaluator();
|
||||
|
||||
public PermanentComparator(Game game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(Permanent o1, Permanent o2) {
|
||||
return evaluator.evaluate(o1, game) - evaluator.evaluate(o2, game);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import mage.Constants.CardType;
|
||||
import mage.Constants.Zone;
|
||||
import mage.abilities.keyword.DoubleStrikeAbility;
|
||||
import mage.abilities.keyword.FirstStrikeAbility;
|
||||
import mage.abilities.keyword.TrampleAbility;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class PermanentEvaluator {
|
||||
|
||||
//preserve calculations for efficiency
|
||||
private Map<UUID, Integer> values = new HashMap<UUID, Integer>();
|
||||
private CombatEvaluator combat = new CombatEvaluator();
|
||||
|
||||
public int evaluate(Permanent permanent, Game game) {
|
||||
if (!values.containsKey(permanent.getId())) {
|
||||
int value = 0;
|
||||
if (permanent.getCardType().contains(CardType.CREATURE)) {
|
||||
value += combat.evaluate(permanent, game);
|
||||
}
|
||||
value += permanent.getAbilities().getManaAbilities(Zone.BATTLEFIELD).size();
|
||||
value += permanent.getAbilities().getActivatedAbilities(Zone.BATTLEFIELD).size();
|
||||
values.put(permanent.getId(), value);
|
||||
}
|
||||
return values.get(permanent.getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai.simulators;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import mage.abilities.ActivatedAbility;
|
||||
import mage.cards.Card;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.player.ai.ComputerPlayer;
|
||||
import mage.player.ai.PermanentEvaluator;
|
||||
import mage.players.Player;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class ActionSimulator {
|
||||
|
||||
private ComputerPlayer player;
|
||||
private List<Card> playableInstants = new ArrayList<Card>();
|
||||
private List<ActivatedAbility> playableAbilities = new ArrayList<ActivatedAbility>();
|
||||
|
||||
private Game game;
|
||||
|
||||
public ActionSimulator(ComputerPlayer player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public void simulate(Game game) {
|
||||
|
||||
}
|
||||
|
||||
public int evaluateState() {
|
||||
Player opponent = game.getPlayer(game.getOpponents(player.getId()).iterator().next());
|
||||
if (game.isGameOver()) {
|
||||
if (player.hasLost() || opponent.hasWon())
|
||||
return Integer.MIN_VALUE;
|
||||
if (opponent.hasLost() || player.hasWon())
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
int value = player.getLife();
|
||||
value -= opponent.getLife();
|
||||
PermanentEvaluator evaluator = new PermanentEvaluator();
|
||||
for (Permanent permanent: game.getBattlefield().getAllActivePermanents(player.getId())) {
|
||||
value += evaluator.evaluate(permanent, game);
|
||||
}
|
||||
for (Permanent permanent: game.getBattlefield().getAllActivePermanents(player.getId())) {
|
||||
value -= evaluator.evaluate(permanent, game);
|
||||
}
|
||||
value += player.getHand().size();
|
||||
value -= opponent.getHand().size();
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai.simulators;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class CombatGroupSimulator implements Serializable {
|
||||
public List<CreatureSimulator> attackers = new ArrayList<CreatureSimulator>();
|
||||
public List<CreatureSimulator> blockers = new ArrayList<CreatureSimulator>();
|
||||
public UUID defenderId;
|
||||
public boolean defenderIsPlaneswalker;
|
||||
public int unblockedDamage;
|
||||
private CreatureSimulator attacker;
|
||||
|
||||
public CombatGroupSimulator(UUID defenderId, List<UUID> attackers, List<UUID> blockers, Game game) {
|
||||
this.defenderId = defenderId;
|
||||
for (UUID attackerId: attackers) {
|
||||
Permanent permanent = game.getPermanent(attackerId);
|
||||
this.attackers.add(new CreatureSimulator(permanent));
|
||||
}
|
||||
for (UUID blockerId: blockers) {
|
||||
Permanent permanent = game.getPermanent(blockerId);
|
||||
this.blockers.add(new CreatureSimulator(permanent));
|
||||
}
|
||||
//NOTE: assumes no banding
|
||||
attacker = this.attackers.get(0);
|
||||
}
|
||||
|
||||
private boolean hasFirstOrDoubleStrike() {
|
||||
for (CreatureSimulator creature: attackers) {
|
||||
if (creature.hasDoubleStrike || creature.hasFirstStrike)
|
||||
return true;
|
||||
}
|
||||
for (CreatureSimulator creature: blockers) {
|
||||
if (creature.hasDoubleStrike || creature.hasFirstStrike)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean canBlock(Permanent blocker, Game game) {
|
||||
return blocker.canBlock(attacker.id, game);
|
||||
}
|
||||
|
||||
public void simulateCombat() {
|
||||
unblockedDamage = 0;
|
||||
|
||||
if (hasFirstOrDoubleStrike())
|
||||
assignDamage(true);
|
||||
assignDamage(false);
|
||||
}
|
||||
|
||||
private void assignDamage(boolean first) {
|
||||
if (blockers.size() == 0) {
|
||||
if (canDamage(attacker, first))
|
||||
unblockedDamage += attacker.power;
|
||||
}
|
||||
else if (blockers.size() == 1) {
|
||||
CreatureSimulator blocker = blockers.get(0);
|
||||
if (canDamage(attacker, first)) {
|
||||
if (attacker.hasTrample) {
|
||||
int lethalDamage = blocker.getLethalDamage();
|
||||
if (attacker.power > lethalDamage) {
|
||||
blocker.damage += lethalDamage;
|
||||
unblockedDamage += attacker.power - lethalDamage;
|
||||
}
|
||||
else {
|
||||
blocker.damage += attacker.power;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (canDamage(blocker, first)) {
|
||||
attacker.damage += blocker.power;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int damage = attacker.power;
|
||||
for (CreatureSimulator blocker: blockers) {
|
||||
if (damage > 0 && canDamage(attacker, first)) {
|
||||
int lethalDamage = blocker.getLethalDamage();
|
||||
if (damage > lethalDamage) {
|
||||
blocker.damage += lethalDamage;
|
||||
damage -= lethalDamage;
|
||||
}
|
||||
else {
|
||||
blocker.damage += damage;
|
||||
damage = 0;
|
||||
}
|
||||
}
|
||||
if (canDamage(blocker, first)) {
|
||||
attacker.damage += blocker.power;
|
||||
}
|
||||
}
|
||||
if (damage > 0) {
|
||||
if (attacker.hasTrample) {
|
||||
unblockedDamage += damage;
|
||||
}
|
||||
else {
|
||||
blockers.get(0).damage += damage;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean canDamage(CreatureSimulator creature, boolean first) {
|
||||
if (first && (creature.hasFirstStrike || creature.hasDoubleStrike))
|
||||
return true;
|
||||
if (!first && (!creature.hasFirstStrike || creature.hasDoubleStrike))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns 3 attacker survives blockers destroyed
|
||||
* returns 2 both destroyed
|
||||
* returns 1 both survive
|
||||
* returns 0 attacker destroyed blockers survive
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public int evaluateCombat() {
|
||||
int survivingBlockers = 0;
|
||||
for (CreatureSimulator blocker: blockers) {
|
||||
if (blocker.damage < blocker.toughness)
|
||||
survivingBlockers++;
|
||||
}
|
||||
if (attacker.isDead()) {
|
||||
if (survivingBlockers > 0) {
|
||||
return 0;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
else {
|
||||
if (survivingBlockers > 0) {
|
||||
return 1;
|
||||
}
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai.simulators;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import mage.game.Game;
|
||||
import mage.game.combat.CombatGroup;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.players.Player;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class CombatSimulator implements Serializable {
|
||||
|
||||
public List<CombatGroupSimulator> groups = new ArrayList<CombatGroupSimulator>();
|
||||
public List<UUID> defenders = new ArrayList<UUID>();
|
||||
public Map<UUID, Integer> playersLife = new HashMap<UUID, Integer>();
|
||||
public Map<UUID, Integer> planeswalkerLoyalty = new HashMap<UUID, Integer>();
|
||||
public UUID attackerId;
|
||||
public int rating = 0;
|
||||
|
||||
public static CombatSimulator load(Game game) {
|
||||
CombatSimulator simCombat = new CombatSimulator();
|
||||
for (CombatGroup group: game.getCombat().getGroups()) {
|
||||
simCombat.groups.add(new CombatGroupSimulator(group.getDefenderId(), group.getAttackers(), group.getBlockers(), game));
|
||||
}
|
||||
for (UUID defenderId: game.getCombat().getDefenders()) {
|
||||
simCombat.defenders.add(defenderId);
|
||||
Player player = game.getPlayer(defenderId);
|
||||
if (player != null) {
|
||||
simCombat.playersLife.put(defenderId, player.getLife());
|
||||
}
|
||||
else {
|
||||
Permanent permanent = game.getPermanent(defenderId);
|
||||
simCombat.planeswalkerLoyalty.put(defenderId, permanent.getLoyalty().getValue());
|
||||
}
|
||||
}
|
||||
return simCombat;
|
||||
}
|
||||
|
||||
public CombatSimulator() {}
|
||||
|
||||
public void clear() {
|
||||
groups.clear();
|
||||
defenders.clear();
|
||||
attackerId = null;
|
||||
}
|
||||
|
||||
public void simulate() {
|
||||
for (CombatGroupSimulator group: groups) {
|
||||
group.simulateCombat();
|
||||
}
|
||||
}
|
||||
|
||||
public int evaluate() {
|
||||
Map<UUID, Integer> damage = new HashMap<UUID, Integer>();
|
||||
int result = 0;
|
||||
for (CombatGroupSimulator group: groups) {
|
||||
if (!damage.containsKey(group.defenderId)) {
|
||||
damage.put(group.defenderId, group.unblockedDamage);
|
||||
}
|
||||
else {
|
||||
damage.put(group.defenderId, damage.get(group.defenderId) + group.unblockedDamage);
|
||||
}
|
||||
}
|
||||
//check for lethal damage to player
|
||||
for (Entry<UUID, Integer> entry: playersLife.entrySet()) {
|
||||
if (damage.containsKey(entry.getKey()) && entry.getValue() <= damage.get(entry.getKey())) {
|
||||
//TODO: check for protection
|
||||
//NOTE: not applicable for mulitplayer games
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
for (CombatGroupSimulator group: groups) {
|
||||
result += group.evaluateCombat();
|
||||
}
|
||||
|
||||
rating = result;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai.simulators;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
import mage.abilities.keyword.DoubleStrikeAbility;
|
||||
import mage.abilities.keyword.FirstStrikeAbility;
|
||||
import mage.abilities.keyword.TrampleAbility;
|
||||
import mage.game.permanent.Permanent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class CreatureSimulator implements Serializable {
|
||||
public UUID id;
|
||||
public int damage;
|
||||
public int power;
|
||||
public int toughness;
|
||||
public boolean hasFirstStrike;
|
||||
public boolean hasDoubleStrike;
|
||||
public boolean hasTrample;
|
||||
|
||||
public CreatureSimulator(Permanent permanent) {
|
||||
this.id = permanent.getId();
|
||||
this.damage = permanent.getDamage();
|
||||
this.power = permanent.getPower().getValue();
|
||||
this.toughness = permanent.getToughness().getValue();
|
||||
this.hasDoubleStrike = permanent.getAbilities().containsKey(DoubleStrikeAbility.getInstance().getId());
|
||||
this.hasFirstStrike = permanent.getAbilities().containsKey(FirstStrikeAbility.getInstance().getId());
|
||||
this.hasTrample = permanent.getAbilities().containsKey(TrampleAbility.getInstance().getId());
|
||||
}
|
||||
|
||||
public boolean isDead() {
|
||||
return damage >= toughness;
|
||||
}
|
||||
|
||||
public int getLethalDamage() {
|
||||
return toughness - damage;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
maxDepth=10
|
||||
maxNodes=5000
|
||||
evaluatorLifeFactor=2
|
||||
evaluatorPermanentFactor=1
|
||||
evaluatorCreatureFactor=1
|
||||
evaluatorHandFactor=1
|
||||
maxThinkSeconds=30
|
||||
56
Mage.Server.Plugins/Mage.Player.AIMinimax/pom.xml
Normal file
56
Mage.Server.Plugins/Mage.Player.AIMinimax/pom.xml
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.mage</groupId>
|
||||
<artifactId>mage-root</artifactId>
|
||||
<version>0.5</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>Mage-Player-AIMinimax</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Mage Player AI Minimax</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>Mage</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>Mage-Player-AI</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
<finalName>mage-player-aiminimax</finalName>
|
||||
</build>
|
||||
|
||||
<properties/>
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.TreeMap;
|
||||
import mage.game.permanent.Permanent;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class Attackers extends TreeMap<Integer, List<Permanent>> {
|
||||
|
||||
public List<Permanent> getAttackers() {
|
||||
List<Permanent> attackers = new ArrayList<Permanent>();
|
||||
for (List<Permanent> l: this.values()) {
|
||||
for (Permanent permanent: l) {
|
||||
attackers.add(permanent);
|
||||
}
|
||||
}
|
||||
return attackers;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,648 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.FutureTask;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.Constants.Outcome;
|
||||
import mage.Constants.PhaseStep;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.ActivatedAbility;
|
||||
import mage.abilities.effects.Effect;
|
||||
import mage.abilities.effects.SearchEffect;
|
||||
import mage.cards.Cards;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.choices.Choice;
|
||||
import mage.filter.FilterAbility;
|
||||
import mage.game.Game;
|
||||
import mage.game.combat.Combat;
|
||||
import mage.game.combat.CombatGroup;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.game.stack.StackAbility;
|
||||
import mage.game.stack.StackObject;
|
||||
import mage.game.turn.BeginCombatStep;
|
||||
import mage.game.turn.BeginningPhase;
|
||||
import mage.game.turn.CleanupStep;
|
||||
import mage.game.turn.CombatDamageStep;
|
||||
import mage.game.turn.CombatPhase;
|
||||
import mage.game.turn.DeclareAttackersStep;
|
||||
import mage.game.turn.DeclareBlockersStep;
|
||||
import mage.game.turn.DrawStep;
|
||||
import mage.game.turn.EndOfCombatStep;
|
||||
import mage.game.turn.EndPhase;
|
||||
import mage.game.turn.EndStep;
|
||||
import mage.game.turn.Phase;
|
||||
import mage.game.turn.PostCombatMainPhase;
|
||||
import mage.game.turn.PostCombatMainStep;
|
||||
import mage.game.turn.PreCombatMainPhase;
|
||||
import mage.game.turn.PreCombatMainStep;
|
||||
import mage.game.turn.UntapStep;
|
||||
import mage.game.turn.UpkeepStep;
|
||||
import mage.players.Player;
|
||||
import mage.target.Target;
|
||||
import mage.target.TargetCard;
|
||||
import mage.util.Logging;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class ComputerPlayer2 extends ComputerPlayer<ComputerPlayer2> implements Player {
|
||||
|
||||
private static final transient Logger logger = Logging.getLogger(ComputerPlayer2.class.getName());
|
||||
private static final ExecutorService pool = Executors.newFixedThreadPool(1);
|
||||
|
||||
protected int maxDepth;
|
||||
protected int maxNodes;
|
||||
protected LinkedList<Ability> actions = new LinkedList<Ability>();
|
||||
protected List<UUID> targets = new ArrayList<UUID>();
|
||||
protected List<String> choices = new ArrayList<String>();
|
||||
protected Combat combat;
|
||||
protected int currentScore;
|
||||
protected SimulationNode root;
|
||||
|
||||
public ComputerPlayer2(String name, Deck deck, RangeOfInfluence range) {
|
||||
super(name, deck, range);
|
||||
maxDepth = Config.maxDepth;
|
||||
maxNodes = Config.maxNodes;
|
||||
}
|
||||
|
||||
public ComputerPlayer2(final ComputerPlayer2 player) {
|
||||
super(player);
|
||||
this.maxDepth = player.maxDepth;
|
||||
this.currentScore = player.currentScore;
|
||||
if (player.combat != null)
|
||||
this.combat = player.combat.copy();
|
||||
for (Ability ability: player.actions) {
|
||||
actions.add(ability);
|
||||
}
|
||||
for (UUID targetId: player.targets) {
|
||||
targets.add(targetId);
|
||||
}
|
||||
for (String choice: player.choices) {
|
||||
choices.add(choice);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComputerPlayer2 copy() {
|
||||
return new ComputerPlayer2(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void priority(Game game) {
|
||||
logState(game);
|
||||
game.firePriorityEvent(playerId);
|
||||
switch (game.getTurn().getStepType()) {
|
||||
case UPKEEP:
|
||||
case DRAW:
|
||||
pass();
|
||||
break;
|
||||
case PRECOMBAT_MAIN:
|
||||
case BEGIN_COMBAT:
|
||||
case DECLARE_ATTACKERS:
|
||||
case DECLARE_BLOCKERS:
|
||||
case COMBAT_DAMAGE:
|
||||
case END_COMBAT:
|
||||
case POSTCOMBAT_MAIN:
|
||||
if (actions.size() == 0) {
|
||||
calculateActions(game);
|
||||
}
|
||||
act(game);
|
||||
break;
|
||||
case END_TURN:
|
||||
case CLEANUP:
|
||||
pass();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected void act(Game game) {
|
||||
if (actions == null || actions.size() == 0)
|
||||
pass();
|
||||
else {
|
||||
boolean usedStack = false;
|
||||
while (actions.peek() != null) {
|
||||
Ability ability = actions.poll();
|
||||
this.activateAbility((ActivatedAbility) ability, game);
|
||||
if (ability.isUsesStack())
|
||||
usedStack = true;
|
||||
}
|
||||
if (usedStack)
|
||||
pass();
|
||||
}
|
||||
}
|
||||
|
||||
protected void calculateActions(Game game) {
|
||||
currentScore = GameStateEvaluator.evaluate(playerId, game);
|
||||
if (!getNextAction(game)) {
|
||||
Game sim = createSimulation(game);
|
||||
SimulationNode.resetCount();
|
||||
root = new SimulationNode(sim, maxDepth, playerId);
|
||||
logger.fine("simulating actions");
|
||||
addActionsTimed(new FilterAbility());
|
||||
if (root.children.size() > 0) {
|
||||
root = root.children.get(0);
|
||||
actions = new LinkedList<Ability>(root.abilities);
|
||||
combat = root.combat;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean getNextAction(Game game) {
|
||||
if (root != null && root.children.size() > 0) {
|
||||
SimulationNode test = root;
|
||||
root = root.children.get(0);
|
||||
while (root.children.size() > 0 && !root.playerId.equals(playerId)) {
|
||||
test = root;
|
||||
root = root.children.get(0);
|
||||
}
|
||||
logger.fine("simlating -- game value:" + game.getState().getValue() + " test value:" + test.gameValue);
|
||||
if (root.playerId.equals(playerId) && root.abilities != null && game.getState().getValue() == test.gameValue) {
|
||||
logger.fine("simulating -- continuing previous action chain");
|
||||
actions = new LinkedList<Ability>(root.abilities);
|
||||
combat = root.combat;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected int minimaxAB(SimulationNode node, FilterAbility filter, int depth, int alpha, int beta) {
|
||||
UUID currentPlayerId = node.getGame().getPlayerList().get();
|
||||
SimulationNode bestChild = null;
|
||||
for (SimulationNode child: node.getChildren()) {
|
||||
if (alpha >= beta) {
|
||||
logger.fine("alpha beta pruning");
|
||||
break;
|
||||
}
|
||||
if (SimulationNode.nodeCount > maxNodes) {
|
||||
logger.fine("simulating -- reached end-state");
|
||||
break;
|
||||
}
|
||||
int val = addActions(child, filter, depth-1, alpha, beta);
|
||||
if (!currentPlayerId.equals(playerId)) {
|
||||
if (val < beta) {
|
||||
beta = val;
|
||||
bestChild = child;
|
||||
if (node.getCombat() == null)
|
||||
node.setCombat(child.getCombat());
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (val > alpha) {
|
||||
alpha = val;
|
||||
bestChild = child;
|
||||
if (node.getCombat() == null)
|
||||
node.setCombat(child.getCombat());
|
||||
}
|
||||
}
|
||||
}
|
||||
node.children.clear();
|
||||
if (bestChild != null)
|
||||
node.children.add(bestChild);
|
||||
if (!currentPlayerId.equals(playerId)) {
|
||||
logger.fine("returning minimax beta: " + beta);
|
||||
return beta;
|
||||
}
|
||||
else {
|
||||
logger.fine("returning minimax alpha: " + alpha);
|
||||
return alpha;
|
||||
}
|
||||
}
|
||||
|
||||
protected SearchEffect getSearchEffect(StackAbility ability) {
|
||||
for (Effect effect: ability.getEffects()) {
|
||||
if (effect instanceof SearchEffect) {
|
||||
return (SearchEffect) effect;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected void resolve(SimulationNode node, int depth, Game game) {
|
||||
StackObject ability = game.getStack().pop();
|
||||
if (ability instanceof StackAbility) {
|
||||
SearchEffect effect = getSearchEffect((StackAbility) ability);
|
||||
if (effect != null && ability.getControllerId().equals(playerId)) {
|
||||
Target target = effect.getTarget();
|
||||
if (!target.doneChosing()) {
|
||||
for (UUID targetId: target.possibleTargets(ability.getSourceId(), ability.getControllerId(), game)) {
|
||||
Game sim = game.copy();
|
||||
StackAbility newAbility = (StackAbility) ability.copy();
|
||||
SearchEffect newEffect = getSearchEffect((StackAbility) newAbility);
|
||||
newEffect.getTarget().addTarget(targetId, newAbility, sim);
|
||||
sim.getStack().push(newAbility);
|
||||
SimulationNode newNode = new SimulationNode(sim, depth, ability.getControllerId());
|
||||
node.children.add(newNode);
|
||||
newNode.getTargets().add(targetId);
|
||||
logger.fine("simulating search -- node#: " + SimulationNode.getCount() + "for player: " + sim.getPlayer(ability.getControllerId()).getName());
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.fine("simulating resolve ");
|
||||
ability.resolve(game);
|
||||
game.applyEffects();
|
||||
game.getPlayers().resetPassed();
|
||||
game.getPlayerList().setCurrent(game.getActivePlayerId());
|
||||
}
|
||||
|
||||
protected void addActionsTimed(final FilterAbility filter) {
|
||||
FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
|
||||
public Integer call() throws Exception
|
||||
{
|
||||
return addActions(root, filter, maxDepth, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
||||
}
|
||||
});
|
||||
pool.execute(task);
|
||||
try {
|
||||
task.get(Config.maxThinkSeconds, TimeUnit.SECONDS);
|
||||
} catch (TimeoutException e) {
|
||||
logger.fine("simulating - timed out");
|
||||
task.cancel(true);
|
||||
} catch (ExecutionException e) {
|
||||
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected int addActions(SimulationNode node, FilterAbility filter, int depth, int alpha, int beta) {
|
||||
Game game = node.getGame();
|
||||
int val;
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
if (depth <= 0 || SimulationNode.nodeCount > maxNodes || game.isGameOver()) {
|
||||
logger.fine("simulating -- reached end state");
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
else if (node.getChildren().size() > 0) {
|
||||
logger.fine("simulating -- somthing added children:" + node.getChildren().size());
|
||||
val = minimaxAB(node, filter, depth-1, alpha, beta);
|
||||
}
|
||||
else {
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("simulating -- alpha: " + alpha + " beta: " + beta + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + (node.getPlayerId().equals(playerId)?"yes":"no"));
|
||||
if (allPassed(game)) {
|
||||
if (!game.getStack().isEmpty()) {
|
||||
resolve(node, depth, game);
|
||||
}
|
||||
else {
|
||||
// int testScore = GameStateEvaluator.evaluate(playerId, game);
|
||||
// if (testScore < currentScore) {
|
||||
// // if score at end of step is worse than original score don't check any further
|
||||
// logger.fine("simulating -- abandoning current check, no immediate benefit");
|
||||
// return testScore;
|
||||
// }
|
||||
game.getPlayers().resetPassed();
|
||||
playNext(game, game.getActivePlayerId(), node);
|
||||
}
|
||||
}
|
||||
|
||||
if (game.isGameOver()) {
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
else if (node.getChildren().size() > 0) {
|
||||
//declared attackers or blockers or triggered abilities
|
||||
logger.fine("simulating -- attack/block/trigger added children:" + node.getChildren().size());
|
||||
val = minimaxAB(node, filter, depth-1, alpha, beta);
|
||||
}
|
||||
else {
|
||||
val = simulatePriority(node, game, filter, depth, alpha, beta);
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("returning -- score: " + val + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||
return val;
|
||||
|
||||
}
|
||||
|
||||
protected int simulatePriority(SimulationNode node, Game game, FilterAbility filter, int depth, int alpha, int beta) {
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
node.setGameValue(game.getState().getValue());
|
||||
SimulatedPlayer currentPlayer = (SimulatedPlayer) game.getPlayer(game.getPlayerList().get());
|
||||
SimulationNode bestNode = null;
|
||||
List<Ability> allActions = currentPlayer.simulatePriority(game, filter);
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("simulating -- adding " + allActions.size() + " children:" + allActions);
|
||||
for (Ability action: allActions) {
|
||||
Game sim = game.copy();
|
||||
if (sim.getPlayer(playerId).activateAbility((ActivatedAbility) action.copy(), sim)) {
|
||||
sim.applyEffects();
|
||||
if (!sim.isGameOver() && action.isUsesStack()) {
|
||||
// only pass if the last action uses the stack
|
||||
sim.getPlayer(currentPlayer.getId()).pass();
|
||||
sim.getPlayerList().getNext();
|
||||
}
|
||||
SimulationNode newNode = new SimulationNode(sim, action, depth, currentPlayer.getId());
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("simulating -- node #:" + SimulationNode.getCount() + " actions:" + action);
|
||||
sim.checkStateAndTriggered();
|
||||
int val = addActions(newNode, filter, depth-1, alpha, beta);
|
||||
if (!currentPlayer.getId().equals(playerId)) {
|
||||
if (val < beta) {
|
||||
beta = val;
|
||||
bestNode = newNode;
|
||||
node.setCombat(newNode.getCombat());
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (val > alpha) {
|
||||
alpha = val;
|
||||
bestNode = newNode;
|
||||
node.setCombat(newNode.getCombat());
|
||||
if (node.getTargets().size() > 0)
|
||||
targets = node.getTargets();
|
||||
if (node.getChoices().size() > 0)
|
||||
choices = node.getChoices();
|
||||
}
|
||||
}
|
||||
if (alpha >= beta) {
|
||||
logger.fine("simulating -- pruning");
|
||||
break;
|
||||
}
|
||||
if (SimulationNode.nodeCount > maxNodes) {
|
||||
logger.fine("simulating -- reached end-state");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (bestNode != null) {
|
||||
node.children.clear();
|
||||
node.children.add(bestNode);
|
||||
}
|
||||
if (!currentPlayer.getId().equals(playerId)) {
|
||||
logger.fine("returning priority beta: " + beta);
|
||||
return beta;
|
||||
}
|
||||
else {
|
||||
logger.fine("returning priority alpha: " + alpha);
|
||||
return alpha;
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean allPassed(Game game) {
|
||||
for (Player player: game.getPlayers().values()) {
|
||||
if (!player.isPassed() && !player.hasLost() && !player.hasLeft())
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean choose(Outcome outcome, Choice choice, Game game) {
|
||||
if (choices.size() == 0)
|
||||
return super.choose(outcome, choice, game);
|
||||
if (!choice.isChosen()) {
|
||||
for (String achoice: choices) {
|
||||
choice.setChoice(achoice);
|
||||
if (choice.isChosen()) {
|
||||
choices.clear();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseTarget(Cards cards, TargetCard target, Ability source, Game game) {
|
||||
if (targets.size() == 0)
|
||||
return super.chooseTarget(cards, target, source, game);
|
||||
if (!target.doneChosing()) {
|
||||
for (UUID targetId: targets) {
|
||||
target.addTarget(targetId, source, game);
|
||||
if (target.doneChosing()) {
|
||||
targets.clear();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean choose(Cards cards, TargetCard target, Game game) {
|
||||
if (targets.size() == 0)
|
||||
return super.choose(cards, target, game);
|
||||
if (!target.doneChosing()) {
|
||||
for (UUID targetId: targets) {
|
||||
target.add(targetId, game);
|
||||
if (target.doneChosing()) {
|
||||
targets.clear();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void playNext(Game game, UUID activePlayerId, SimulationNode node) {
|
||||
boolean skip = false;
|
||||
while (true) {
|
||||
Phase currentPhase = game.getPhase();
|
||||
if (!skip)
|
||||
currentPhase.getStep().endStep(game, activePlayerId);
|
||||
game.applyEffects();
|
||||
switch (currentPhase.getStep().getType()) {
|
||||
case UNTAP:
|
||||
game.getPhase().setStep(new UpkeepStep());
|
||||
break;
|
||||
case UPKEEP:
|
||||
game.getPhase().setStep(new DrawStep());
|
||||
break;
|
||||
case DRAW:
|
||||
game.getTurn().setPhase(new PreCombatMainPhase());
|
||||
game.getPhase().setStep(new PreCombatMainStep());
|
||||
break;
|
||||
case PRECOMBAT_MAIN:
|
||||
game.getTurn().setPhase(new CombatPhase());
|
||||
game.getPhase().setStep(new BeginCombatStep());
|
||||
break;
|
||||
case BEGIN_COMBAT:
|
||||
game.getPhase().setStep(new DeclareAttackersStep());
|
||||
break;
|
||||
case DECLARE_ATTACKERS:
|
||||
game.getPhase().setStep(new DeclareBlockersStep());
|
||||
break;
|
||||
case DECLARE_BLOCKERS:
|
||||
game.getPhase().setStep(new CombatDamageStep(true));
|
||||
break;
|
||||
case COMBAT_DAMAGE:
|
||||
if (((CombatDamageStep)currentPhase.getStep()).getFirst())
|
||||
game.getPhase().setStep(new CombatDamageStep(false));
|
||||
else
|
||||
game.getPhase().setStep(new EndOfCombatStep());
|
||||
break;
|
||||
case END_COMBAT:
|
||||
game.getTurn().setPhase(new PostCombatMainPhase());
|
||||
game.getPhase().setStep(new PostCombatMainStep());
|
||||
break;
|
||||
case POSTCOMBAT_MAIN:
|
||||
game.getTurn().setPhase(new EndPhase());
|
||||
game.getPhase().setStep(new EndStep());
|
||||
break;
|
||||
case END_TURN:
|
||||
game.getPhase().setStep(new CleanupStep());
|
||||
break;
|
||||
case CLEANUP:
|
||||
game.getPhase().getStep().beginStep(game, activePlayerId);
|
||||
if (!game.checkStateAndTriggered() && !game.isGameOver()) {
|
||||
game.getState().setActivePlayerId(game.getState().getPlayerList(game.getActivePlayerId()).getNext());
|
||||
game.getTurn().setPhase(new BeginningPhase());
|
||||
game.getPhase().setStep(new UntapStep());
|
||||
}
|
||||
}
|
||||
if (!game.getStep().skipStep(game, game.getActivePlayerId())) {
|
||||
if (game.getTurn().getStepType() == PhaseStep.DECLARE_ATTACKERS) {
|
||||
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_PRE, null, null, activePlayerId));
|
||||
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, activePlayerId, activePlayerId))) {
|
||||
for (Combat engagement: ((SimulatedPlayer)game.getPlayer(activePlayerId)).addAttackers(game)) {
|
||||
Game sim = game.copy();
|
||||
UUID defenderId = game.getOpponents(playerId).iterator().next();
|
||||
for (CombatGroup group: engagement.getGroups()) {
|
||||
for (UUID attackerId: group.getAttackers()) {
|
||||
sim.getPlayer(activePlayerId).declareAttacker(attackerId, defenderId, sim);
|
||||
}
|
||||
}
|
||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, playerId, playerId));
|
||||
SimulationNode newNode = new SimulationNode(sim, node.getDepth()-1, activePlayerId);
|
||||
logger.fine("simulating -- node #:" + SimulationNode.getCount() + " declare attakers");
|
||||
newNode.setCombat(sim.getCombat());
|
||||
node.children.add(newNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (game.getTurn().getStepType() == PhaseStep.DECLARE_BLOCKERS) {
|
||||
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_PRE, null, null, activePlayerId));
|
||||
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_BLOCKERS, activePlayerId, activePlayerId))) {
|
||||
for (UUID defenderId: game.getCombat().getDefenders()) {
|
||||
//check if defender is being attacked
|
||||
if (game.getCombat().isAttacked(defenderId, game)) {
|
||||
for (Combat engagement: ((SimulatedPlayer)game.getPlayer(defenderId)).addBlockers(game)) {
|
||||
Game sim = game.copy();
|
||||
for (CombatGroup group: engagement.getGroups()) {
|
||||
for (UUID blockerId: group.getBlockers()) {
|
||||
group.addBlocker(blockerId, defenderId, sim);
|
||||
}
|
||||
}
|
||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
|
||||
SimulationNode newNode = new SimulationNode(sim, node.getDepth()-1, defenderId);
|
||||
logger.fine("simulating -- node #:" + SimulationNode.getCount() + " declare blockers");
|
||||
newNode.setCombat(sim.getCombat());
|
||||
node.children.add(newNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
game.getStep().beginStep(game, activePlayerId);
|
||||
}
|
||||
if (game.getStep().getHasPriority())
|
||||
break;
|
||||
}
|
||||
else {
|
||||
skip = true;
|
||||
}
|
||||
}
|
||||
game.checkStateAndTriggered();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectAttackers(Game game) {
|
||||
logger.fine("selectAttackers");
|
||||
if (combat != null) {
|
||||
UUID opponentId = game.getCombat().getDefenders().iterator().next();
|
||||
for (UUID attackerId: combat.getAttackers()) {
|
||||
this.declareAttacker(attackerId, opponentId, game);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectBlockers(Game game) {
|
||||
logger.fine("selectBlockers");
|
||||
if (combat != null && combat.getGroups().size() > 0) {
|
||||
List<CombatGroup> groups = game.getCombat().getGroups();
|
||||
for (int i = 0; i< groups.size(); i++) {
|
||||
for (UUID blockerId: combat.getGroups().get(i).getBlockers()) {
|
||||
this.declareBlocker(blockerId, groups.get(i).getAttackers().get(0), game);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies game and replaces all players in copy with simulated players
|
||||
*
|
||||
* @param game
|
||||
* @return a new game object with simulated players
|
||||
*/
|
||||
protected Game createSimulation(Game game) {
|
||||
Game sim = game.copy();
|
||||
|
||||
for (Player copyPlayer: sim.getState().getPlayers().values()) {
|
||||
Player origPlayer = game.getState().getPlayers().get(copyPlayer.getId());
|
||||
SimulatedPlayer newPlayer = new SimulatedPlayer(copyPlayer.getId(), copyPlayer.getId().equals(playerId));
|
||||
newPlayer.restore(origPlayer);
|
||||
sim.getState().getPlayers().put(copyPlayer.getId(), newPlayer);
|
||||
}
|
||||
return sim;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,536 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.Constants.AbilityType;
|
||||
import mage.Constants.PhaseStep;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.Constants.Zone;
|
||||
import mage.abilities.Ability;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.filter.FilterAbility;
|
||||
import mage.game.Game;
|
||||
import mage.game.combat.Combat;
|
||||
import mage.game.combat.CombatGroup;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.game.turn.BeginCombatStep;
|
||||
import mage.game.turn.BeginningPhase;
|
||||
import mage.game.turn.CleanupStep;
|
||||
import mage.game.turn.CombatDamageStep;
|
||||
import mage.game.turn.CombatPhase;
|
||||
import mage.game.turn.DeclareAttackersStep;
|
||||
import mage.game.turn.DeclareBlockersStep;
|
||||
import mage.game.turn.DrawStep;
|
||||
import mage.game.turn.EndOfCombatStep;
|
||||
import mage.game.turn.EndPhase;
|
||||
import mage.game.turn.EndStep;
|
||||
import mage.game.turn.PostCombatMainPhase;
|
||||
import mage.game.turn.PostCombatMainStep;
|
||||
import mage.game.turn.Step;
|
||||
import mage.game.turn.UntapStep;
|
||||
import mage.game.turn.UpkeepStep;
|
||||
import mage.players.Player;
|
||||
import mage.util.Logging;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class ComputerPlayer3 extends ComputerPlayer2 implements Player {
|
||||
|
||||
private static final transient Logger logger = Logging.getLogger(ComputerPlayer3.class.getName());
|
||||
|
||||
private static FilterAbility filterLand = new FilterAbility();
|
||||
private static FilterAbility filterNotLand = new FilterAbility();
|
||||
|
||||
static {
|
||||
filterLand.getTypes().add(AbilityType.PLAY_LAND);
|
||||
filterLand.setZone(Zone.HAND);
|
||||
|
||||
filterNotLand.getTypes().add(AbilityType.PLAY_LAND);
|
||||
filterNotLand.setZone(Zone.HAND);
|
||||
filterNotLand.setNotFilter(true);
|
||||
|
||||
}
|
||||
|
||||
public ComputerPlayer3(String name, Deck deck, RangeOfInfluence range) {
|
||||
super(name, deck, range);
|
||||
maxDepth = Config.maxDepth;
|
||||
maxNodes = Config.maxNodes;
|
||||
}
|
||||
|
||||
public ComputerPlayer3(final ComputerPlayer3 player) {
|
||||
super(player);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ComputerPlayer3 copy() {
|
||||
return new ComputerPlayer3(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void priority(Game game) {
|
||||
logState(game);
|
||||
game.firePriorityEvent(playerId);
|
||||
switch (game.getTurn().getStepType()) {
|
||||
case UPKEEP:
|
||||
case DRAW:
|
||||
pass();
|
||||
break;
|
||||
case PRECOMBAT_MAIN:
|
||||
if (game.getActivePlayerId().equals(playerId)) {
|
||||
if (actions.size() == 0) {
|
||||
calculatePreCombatActions(game);
|
||||
}
|
||||
act(game);
|
||||
}
|
||||
else
|
||||
pass();
|
||||
break;
|
||||
case BEGIN_COMBAT:
|
||||
pass();
|
||||
break;
|
||||
case DECLARE_ATTACKERS:
|
||||
if (!game.getActivePlayerId().equals(playerId)) {
|
||||
if (actions.size() == 0) {
|
||||
calculatePreCombatActions(game);
|
||||
}
|
||||
act(game);
|
||||
}
|
||||
else
|
||||
pass();
|
||||
break;
|
||||
case DECLARE_BLOCKERS:
|
||||
case COMBAT_DAMAGE:
|
||||
case END_COMBAT:
|
||||
pass();
|
||||
break;
|
||||
case POSTCOMBAT_MAIN:
|
||||
if (game.getActivePlayerId().equals(playerId)) {
|
||||
if (actions.size() == 0) {
|
||||
calculatePostCombatActions(game);
|
||||
}
|
||||
act(game);
|
||||
}
|
||||
else
|
||||
pass();
|
||||
break;
|
||||
case END_TURN:
|
||||
case CLEANUP:
|
||||
pass();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected void calculatePreCombatActions(Game game) {
|
||||
if (!getNextAction(game)) {
|
||||
currentScore = GameStateEvaluator.evaluate(playerId, game);
|
||||
Game sim = createSimulation(game);
|
||||
SimulationNode.resetCount();
|
||||
root = new SimulationNode(sim, maxDepth, playerId);
|
||||
logger.fine("simulating pre combat actions -----------------------------------------------------------------------------------------");
|
||||
|
||||
addActionsTimed(new FilterAbility());
|
||||
if (root.children.size() > 0) {
|
||||
root = root.children.get(0);
|
||||
actions = new LinkedList<Ability>(root.abilities);
|
||||
combat = root.combat;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void calculatePostCombatActions(Game game) {
|
||||
if (!getNextAction(game)) {
|
||||
currentScore = GameStateEvaluator.evaluate(playerId, game);
|
||||
Game sim = createSimulation(game);
|
||||
SimulationNode.resetCount();
|
||||
root = new SimulationNode(sim, maxDepth, playerId);
|
||||
logger.fine("simulating post combat actions ----------------------------------------------------------------------------------------");
|
||||
addActionsTimed(new FilterAbility());
|
||||
if (root.children.size() > 0) {
|
||||
root = root.children.get(0);
|
||||
actions = new LinkedList<Ability>(root.abilities);
|
||||
combat = root.combat;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int addActions(SimulationNode node, FilterAbility filter, int depth, int alpha, int beta) {
|
||||
boolean stepFinished = false;
|
||||
int val;
|
||||
Game game = node.getGame();
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
if (depth <= 0 || SimulationNode.nodeCount > maxNodes || game.isGameOver()) {
|
||||
logger.fine("simulating -- reached end state");
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
else if (node.getChildren().size() > 0) {
|
||||
logger.fine("simulating -- somthing added children:" + node.getChildren().size());
|
||||
val = minimaxAB(node, filter, depth-1, alpha, beta);
|
||||
}
|
||||
else {
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("simulating -- alpha: " + alpha + " beta: " + beta + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(game.getPlayerList().get()).getName());
|
||||
if (allPassed(game)) {
|
||||
if (!game.getStack().isEmpty()) {
|
||||
resolve(node, depth, game);
|
||||
}
|
||||
else {
|
||||
stepFinished = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (game.isGameOver()) {
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
else if (stepFinished) {
|
||||
logger.fine("step finished");
|
||||
int testScore = GameStateEvaluator.evaluate(playerId, game);
|
||||
if (game.getActivePlayerId().equals(playerId)) {
|
||||
if (testScore < currentScore) {
|
||||
// if score at end of step is worse than original score don't check further
|
||||
logger.fine("simulating -- abandoning check, no immediate benefit");
|
||||
val = testScore;
|
||||
}
|
||||
else {
|
||||
switch (game.getTurn().getStepType()) {
|
||||
case PRECOMBAT_MAIN:
|
||||
val = simulateCombat(game, node, depth-1, alpha, beta, false);
|
||||
break;
|
||||
case POSTCOMBAT_MAIN:
|
||||
val = simulateCounterAttack(game, node, depth-1, alpha, beta);
|
||||
break;
|
||||
default:
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (game.getTurn().getStepType() == PhaseStep.DECLARE_ATTACKERS)
|
||||
val = simulateBlockers(game, node, playerId, depth-1, alpha, beta, true);
|
||||
else
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
}
|
||||
else if (node.getChildren().size() > 0) {
|
||||
logger.fine("simulating -- trigger added children:" + node.getChildren().size());
|
||||
val = minimaxAB(node, filter, depth, alpha, beta);
|
||||
}
|
||||
else {
|
||||
val = simulatePriority(node, game, filter, depth, alpha, beta);
|
||||
}
|
||||
}
|
||||
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("returning -- score: " + val + " depth:" + depth + " step:" + game.getTurn().getStepType() + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||
return val;
|
||||
|
||||
}
|
||||
|
||||
protected int simulateCombat(Game game, SimulationNode node, int depth, int alpha, int beta, boolean counter) {
|
||||
Integer val = null;
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
if (game.getTurn().getStepType() != PhaseStep.DECLARE_BLOCKERS) {
|
||||
game.getTurn().setPhase(new CombatPhase());
|
||||
if (game.getPhase().beginPhase(game, game.getActivePlayerId())) {
|
||||
simulateStep(game, new BeginCombatStep());
|
||||
game.getPhase().setStep(new DeclareAttackersStep());
|
||||
if (!game.getStep().skipStep(game, game.getActivePlayerId())) {
|
||||
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_PRE, null, null, game.getActivePlayerId()));
|
||||
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_ATTACKERS, game.getActivePlayerId(), game.getActivePlayerId()))) {
|
||||
val = simulateAttackers(game, node, game.getActivePlayerId(), depth, alpha, beta, counter);
|
||||
}
|
||||
}
|
||||
else if (!counter) {
|
||||
simulateToEnd(game);
|
||||
val = simulatePostCombatMain(game, node, depth, alpha, beta);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!game.getStep().skipStep(game, game.getActivePlayerId())) {
|
||||
game.fireEvent(new GameEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_PRE, null, null, game.getActivePlayerId()));
|
||||
if (!game.replaceEvent(GameEvent.getEvent(GameEvent.EventType.DECLARING_BLOCKERS, game.getActivePlayerId(), game.getActivePlayerId()))) {
|
||||
//only suitable for two player games - only simulates blocks for 1st defender
|
||||
val = simulateBlockers(game, node, game.getCombat().getDefenders().iterator().next(), depth, alpha, beta, counter);
|
||||
}
|
||||
}
|
||||
else if (!counter) {
|
||||
finishCombat(game);
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
// val = simulateCounterAttack(game, node, depth, alpha, beta);
|
||||
}
|
||||
}
|
||||
if (val == null)
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("returning -- combat score: " + val + " depth:" + depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
protected int simulateAttackers(Game game, SimulationNode node, UUID attackerId, int depth, int alpha, int beta, boolean counter) {
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
Integer val = null;
|
||||
SimulationNode bestNode = null;
|
||||
SimulatedPlayer attacker = (SimulatedPlayer) game.getPlayer(attackerId);
|
||||
|
||||
for (Combat engagement: attacker.addAttackers(game)) {
|
||||
if (alpha >= beta) {
|
||||
logger.fine("simulating -- pruning attackers");
|
||||
break;
|
||||
}
|
||||
Game sim = game.copy();
|
||||
UUID defenderId = game.getOpponents(playerId).iterator().next();
|
||||
for (CombatGroup group: engagement.getGroups()) {
|
||||
for (UUID attackId: group.getAttackers()) {
|
||||
sim.getPlayer(attackerId).declareAttacker(attackId, defenderId, sim);
|
||||
}
|
||||
}
|
||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_ATTACKERS, playerId, playerId));
|
||||
SimulationNode newNode = new SimulationNode(sim, depth, game.getActivePlayerId());
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("simulating attack -- node#: " + SimulationNode.getCount());
|
||||
sim.checkStateAndTriggered();
|
||||
while (!sim.getStack().isEmpty()) {
|
||||
sim.getStack().resolve(sim);
|
||||
logger.fine("resolving triggered abilities");
|
||||
sim.applyEffects();
|
||||
}
|
||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARE_ATTACKERS_STEP_POST, sim.getActivePlayerId(), sim.getActivePlayerId()));
|
||||
Combat simCombat = sim.getCombat().copy();
|
||||
sim.getPhase().setStep(new DeclareBlockersStep());
|
||||
val = simulateCombat(sim, newNode, depth-1, alpha, beta, counter);
|
||||
if (!attackerId.equals(playerId)) {
|
||||
if (val < beta) {
|
||||
beta = val;
|
||||
bestNode = newNode;
|
||||
node.setCombat(simCombat);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (val > alpha) {
|
||||
alpha = val;
|
||||
bestNode = newNode;
|
||||
node.setCombat(simCombat);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (val == null)
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
if (bestNode != null) {
|
||||
node.children.clear();
|
||||
node.children.add(bestNode);
|
||||
}
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("returning -- combat attacker score: " + val + " depth:" + depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||
return val;
|
||||
}
|
||||
|
||||
protected int simulateBlockers(Game game, SimulationNode node, UUID defenderId, int depth, int alpha, int beta, boolean counter) {
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
Integer val = null;
|
||||
SimulationNode bestNode = null;
|
||||
//check if defender is being attacked
|
||||
if (game.getCombat().isAttacked(defenderId, game)) {
|
||||
SimulatedPlayer defender = (SimulatedPlayer) game.getPlayer(defenderId);
|
||||
for (Combat engagement: defender.addBlockers(game)) {
|
||||
if (alpha >= beta) {
|
||||
logger.fine("simulating -- pruning blockers");
|
||||
break;
|
||||
}
|
||||
Game sim = game.copy();
|
||||
for (CombatGroup group: engagement.getGroups()) {
|
||||
UUID attackerId = group.getAttackers().get(0);
|
||||
for (UUID blockerId: group.getBlockers()) {
|
||||
sim.getPlayer(defenderId).declareBlocker(blockerId, attackerId, sim);
|
||||
}
|
||||
}
|
||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
|
||||
SimulationNode newNode = new SimulationNode(sim, depth, defenderId);
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("simulating block -- node#: " + SimulationNode.getCount());
|
||||
sim.checkStateAndTriggered();
|
||||
while (!sim.getStack().isEmpty()) {
|
||||
sim.getStack().resolve(sim);
|
||||
logger.fine("resolving triggered abilities");
|
||||
sim.applyEffects();
|
||||
}
|
||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARE_BLOCKERS_STEP_POST, sim.getActivePlayerId(), sim.getActivePlayerId()));
|
||||
Combat simCombat = sim.getCombat().copy();
|
||||
finishCombat(sim);
|
||||
if (sim.isGameOver()) {
|
||||
val = GameStateEvaluator.evaluate(playerId, sim);
|
||||
}
|
||||
else if (!counter) {
|
||||
val = simulatePostCombatMain(sim, newNode, depth-1, alpha, beta);
|
||||
}
|
||||
else
|
||||
val = GameStateEvaluator.evaluate(playerId, sim);
|
||||
if (!defenderId.equals(playerId)) {
|
||||
if (val < beta) {
|
||||
beta = val;
|
||||
bestNode = newNode;
|
||||
node.setCombat(simCombat);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (val > alpha) {
|
||||
alpha = val;
|
||||
bestNode = newNode;
|
||||
node.setCombat(simCombat);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (val == null)
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
if (bestNode != null) {
|
||||
node.children.clear();
|
||||
node.children.add(bestNode);
|
||||
}
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("returning -- combat blocker score: " + val + " depth:" + depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||
return val;
|
||||
}
|
||||
|
||||
protected int simulateCounterAttack(Game game, SimulationNode node, int depth, int alpha, int beta) {
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
Integer val = null;
|
||||
if (!game.isGameOver()) {
|
||||
logger.fine("simulating -- counter attack");
|
||||
simulateToEnd(game);
|
||||
game.getState().setActivePlayerId(game.getState().getPlayerList(game.getActivePlayerId()).getNext());
|
||||
game.getTurn().setPhase(new BeginningPhase());
|
||||
if (game.getPhase().beginPhase(game, game.getActivePlayerId())) {
|
||||
simulateStep(game, new UntapStep());
|
||||
simulateStep(game, new UpkeepStep());
|
||||
simulateStep(game, new DrawStep());
|
||||
game.getPhase().endPhase(game, game.getActivePlayerId());
|
||||
}
|
||||
val = simulateCombat(game, node, depth-1, alpha, beta, true);
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("returning -- counter attack score: " + val + " depth:" + depth + " for player:" + game.getPlayer(node.getPlayerId()).getName());
|
||||
}
|
||||
if (val == null)
|
||||
val = GameStateEvaluator.evaluate(playerId, game);
|
||||
return val;
|
||||
}
|
||||
|
||||
protected void simulateStep(Game game, Step step) {
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return;
|
||||
}
|
||||
if (!game.isGameOver()) {
|
||||
game.getPhase().setStep(step);
|
||||
if (!step.skipStep(game, game.getActivePlayerId())) {
|
||||
step.beginStep(game, game.getActivePlayerId());
|
||||
game.checkStateAndTriggered();
|
||||
while (!game.getStack().isEmpty()) {
|
||||
game.getStack().resolve(game);
|
||||
game.applyEffects();
|
||||
}
|
||||
step.endStep(game, game.getActivePlayerId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void finishCombat(Game game) {
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return;
|
||||
}
|
||||
simulateStep(game, new CombatDamageStep(true));
|
||||
simulateStep(game, new CombatDamageStep(false));
|
||||
simulateStep(game, new EndOfCombatStep());
|
||||
}
|
||||
|
||||
protected int simulatePostCombatMain(Game game, SimulationNode node, int depth, int alpha, int beta) {
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return GameStateEvaluator.evaluate(playerId, game);
|
||||
}
|
||||
logger.fine("simulating -- post combat main");
|
||||
game.getTurn().setPhase(new PostCombatMainPhase());
|
||||
if (game.getPhase().beginPhase(game, game.getActivePlayerId())) {
|
||||
game.getPhase().setStep(new PostCombatMainStep());
|
||||
game.getStep().beginStep(game, playerId);
|
||||
game.getPlayers().resetPassed();
|
||||
return addActions(node, new FilterAbility(), depth, alpha, beta);
|
||||
}
|
||||
return simulateCounterAttack(game, node, depth, alpha, beta);
|
||||
}
|
||||
|
||||
protected void simulateToEnd(Game game) {
|
||||
if (Thread.interrupted()) {
|
||||
Thread.currentThread().interrupt();
|
||||
logger.fine("interrupted");
|
||||
return;
|
||||
}
|
||||
if (!game.isGameOver()) {
|
||||
game.getTurn().getPhase().endPhase(game, game.getActivePlayerId());
|
||||
game.getTurn().setPhase(new EndPhase());
|
||||
if (game.getTurn().getPhase().beginPhase(game, game.getActivePlayerId())) {
|
||||
simulateStep(game, new EndStep());
|
||||
simulateStep(game, new CleanupStep());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Properties;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.util.Logging;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class Config {
|
||||
|
||||
private final static Logger logger = Logging.getLogger(Config.class.getName());
|
||||
|
||||
public static final int maxDepth;
|
||||
public static final int maxNodes;
|
||||
public static final int evaluatorLifeFactor;
|
||||
public static final int evaluatorPermanentFactor;
|
||||
public static final int evaluatorCreatureFactor;
|
||||
public static final int evaluatorHandFactor;
|
||||
public static final int maxThinkSeconds;
|
||||
|
||||
static {
|
||||
Properties p = new Properties();
|
||||
try {
|
||||
File file = new File(Config.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
|
||||
p.load(new FileInputStream(new File(file.getParent() + File.separator + "AIMinimax.properties")));
|
||||
} catch (IOException ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
} catch (URISyntaxException ex) {
|
||||
Logger.getLogger(Config.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
maxDepth = Integer.parseInt(p.getProperty("maxDepth"));
|
||||
maxNodes = Integer.parseInt(p.getProperty("maxNodes"));
|
||||
evaluatorLifeFactor = Integer.parseInt(p.getProperty("evaluatorLifeFactor"));
|
||||
evaluatorPermanentFactor = Integer.parseInt(p.getProperty("evaluatorPermanentFactor"));
|
||||
evaluatorCreatureFactor = Integer.parseInt(p.getProperty("evaluatorCreatureFactor"));
|
||||
evaluatorHandFactor = Integer.parseInt(p.getProperty("evaluatorHandFactor"));
|
||||
maxThinkSeconds = Integer.parseInt(p.getProperty("maxThinkSeconds"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.Constants.CardType;
|
||||
import mage.Constants.Zone;
|
||||
import mage.abilities.ActivatedAbility;
|
||||
import mage.abilities.keyword.DoubleStrikeAbility;
|
||||
import mage.abilities.keyword.FirstStrikeAbility;
|
||||
import mage.abilities.keyword.TrampleAbility;
|
||||
import mage.abilities.mana.ManaAbility;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.players.Player;
|
||||
import mage.util.Logging;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*
|
||||
* this evaluator is only good for two player games
|
||||
*
|
||||
*/
|
||||
public class GameStateEvaluator {
|
||||
|
||||
private final static transient Logger logger = Logging.getLogger(GameStateEvaluator.class.getName());
|
||||
|
||||
private static final int LIFE_FACTOR = Config.evaluatorLifeFactor;
|
||||
private static final int PERMANENT_FACTOR = Config.evaluatorPermanentFactor;
|
||||
private static final int CREATURE_FACTOR = Config.evaluatorCreatureFactor;
|
||||
private static final int HAND_FACTOR = Config.evaluatorHandFactor;
|
||||
|
||||
public static int evaluate(UUID playerId, Game game) {
|
||||
Player player = game.getPlayer(playerId);
|
||||
Player opponent = game.getPlayer(game.getOpponents(playerId).iterator().next());
|
||||
if (game.isGameOver()) {
|
||||
if (player.hasLost() || opponent.hasWon())
|
||||
return Integer.MIN_VALUE;
|
||||
if (opponent.hasLost() || player.hasWon())
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
int lifeScore = (player.getLife() - opponent.getLife()) * LIFE_FACTOR;
|
||||
int permanentScore = 0;
|
||||
for (Permanent permanent: game.getBattlefield().getAllActivePermanents(playerId)) {
|
||||
permanentScore += evaluatePermanent(permanent, game);
|
||||
}
|
||||
for (Permanent permanent: game.getBattlefield().getAllActivePermanents(opponent.getId())) {
|
||||
permanentScore -= evaluatePermanent(permanent, game);
|
||||
}
|
||||
permanentScore *= PERMANENT_FACTOR;
|
||||
|
||||
int handScore = 0;
|
||||
handScore = 7 - opponent.getHand().size();
|
||||
handScore = Math.min(7, player.getHand().size());
|
||||
handScore *= HAND_FACTOR;
|
||||
|
||||
int score = lifeScore + permanentScore + handScore;
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("game state evaluated to- lifeScore:" + lifeScore + " permanentScore:" + permanentScore + " handScore:" + handScore + " total:" + score);
|
||||
return score;
|
||||
}
|
||||
|
||||
public static int evaluatePermanent(Permanent permanent, Game game) {
|
||||
int value = permanent.isTapped()?1:2;
|
||||
if (permanent.getCardType().contains(CardType.CREATURE)) {
|
||||
value += evaluateCreature(permanent, game) * CREATURE_FACTOR;
|
||||
}
|
||||
value += permanent.getAbilities().getManaAbilities(Zone.BATTLEFIELD).size();
|
||||
for (ActivatedAbility ability: permanent.getAbilities().getActivatedAbilities(Zone.BATTLEFIELD)) {
|
||||
if (!(ability instanceof ManaAbility) && ability.canActivate(ability.getControllerId(), game))
|
||||
value += ability.getEffects().getOutcomeTotal();
|
||||
}
|
||||
value += permanent.getAbilities().getStaticAbilities(Zone.BATTLEFIELD).getOutcomeTotal();
|
||||
value += permanent.getAbilities().getTriggeredAbilities(Zone.BATTLEFIELD).getOutcomeTotal();
|
||||
value += permanent.getManaCost().convertedManaCost();
|
||||
//TODO: add a difficulty to calculation to ManaCost - sort permanents by difficulty for casting when evaluating game states
|
||||
return value;
|
||||
}
|
||||
|
||||
public static int evaluateCreature(Permanent creature, Game game) {
|
||||
int value = 0;
|
||||
value += creature.getPower().getValue();
|
||||
value += creature.getToughness().getValue();
|
||||
// if (creature.canAttack(game))
|
||||
// value += creature.getPower().getValue();
|
||||
// if (!creature.isTapped())
|
||||
// value += 2;
|
||||
value += creature.getAbilities().getEvasionAbilities().getOutcomeTotal();
|
||||
value += creature.getAbilities().getProtectionAbilities().getOutcomeTotal();
|
||||
value += creature.getAbilities().containsKey(FirstStrikeAbility.getInstance().getId())?1:0;
|
||||
value += creature.getAbilities().containsKey(DoubleStrikeAbility.getInstance().getId())?2:0;
|
||||
value += creature.getAbilities().containsKey(TrampleAbility.getInstance().getId())?1:0;
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.game.Game;
|
||||
import mage.util.Logging;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class SimulateBlockWorker implements Callable {
|
||||
|
||||
private final static Logger logger = Logging.getLogger(SimulationWorker.class.getName());
|
||||
|
||||
private SimulationNode node;
|
||||
private ComputerPlayer3 player;
|
||||
|
||||
public SimulateBlockWorker(ComputerPlayer3 player, SimulationNode node) {
|
||||
this.player = player;
|
||||
this.node = node;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call() {
|
||||
try {
|
||||
// player.simulateBlock(node);
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.List;
|
||||
import mage.abilities.Ability;
|
||||
import mage.game.Game;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class SimulatedAction {
|
||||
|
||||
private Game game;
|
||||
private List<Ability> abilities;
|
||||
|
||||
public SimulatedAction(Game game, List<Ability> abilities) {
|
||||
this.game = game;
|
||||
this.abilities = abilities;
|
||||
}
|
||||
|
||||
public Game getGame() {
|
||||
return this.game;
|
||||
}
|
||||
|
||||
public List<Ability> getAbilities() {
|
||||
return this.abilities;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.abilities.toString();
|
||||
}
|
||||
|
||||
public boolean usesStack() {
|
||||
if (abilities != null && abilities.size() > 0) {
|
||||
return abilities.get(abilities.size() -1).isUsesStack();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,250 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.ActivatedAbility;
|
||||
import mage.abilities.TriggeredAbility;
|
||||
import mage.abilities.common.PassAbility;
|
||||
import mage.abilities.mana.ManaOptions;
|
||||
import mage.choices.Choice;
|
||||
import mage.filter.FilterAbility;
|
||||
import mage.game.Game;
|
||||
import mage.game.combat.Combat;
|
||||
import mage.game.events.GameEvent;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.game.stack.StackAbility;
|
||||
import mage.target.Target;
|
||||
import mage.util.Copier;
|
||||
import mage.util.Logging;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class SimulatedPlayer extends ComputerPlayer<SimulatedPlayer> {
|
||||
|
||||
private final static transient Logger logger = Logging.getLogger(SimulatedPlayer.class.getName());
|
||||
private boolean isSimulatedPlayer;
|
||||
private FilterAbility filter;
|
||||
private transient ConcurrentLinkedQueue<Ability> allActions;
|
||||
private static PassAbility pass = new PassAbility();
|
||||
|
||||
public SimulatedPlayer(UUID id, boolean isSimulatedPlayer) {
|
||||
super(id);
|
||||
pass.setControllerId(playerId);
|
||||
this.isSimulatedPlayer = isSimulatedPlayer;
|
||||
}
|
||||
|
||||
public SimulatedPlayer(final SimulatedPlayer player) {
|
||||
super(player);
|
||||
this.isSimulatedPlayer = player.isSimulatedPlayer;
|
||||
if (player.filter != null)
|
||||
this.filter = player.filter.copy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SimulatedPlayer copy() {
|
||||
return new SimulatedPlayer(this);
|
||||
}
|
||||
|
||||
public List<Ability> simulatePriority(Game game, FilterAbility filter) {
|
||||
allActions = new ConcurrentLinkedQueue<Ability>();
|
||||
Game sim = game.copy();
|
||||
this.filter = filter;
|
||||
|
||||
simulateOptions(sim, pass);
|
||||
|
||||
ArrayList<Ability> list = new ArrayList<Ability>(allActions);
|
||||
Collections.reverse(list);
|
||||
return list;
|
||||
}
|
||||
|
||||
protected void simulateOptions(Game game, Ability previousActions) {
|
||||
allActions.add(previousActions);
|
||||
ManaOptions available = getManaAvailable(game);
|
||||
available.addMana(manaPool.getMana());
|
||||
List<Ability> playables = game.getPlayer(playerId).getPlayable(game, filter, available, isSimulatedPlayer);
|
||||
for (Ability ability: playables) {
|
||||
List<Ability> options = game.getPlayer(playerId).getPlayableOptions(ability, game);
|
||||
if (options.size() == 0) {
|
||||
allActions.add(ability);
|
||||
// simulateAction(game, previousActions, ability);
|
||||
}
|
||||
else {
|
||||
// ExecutorService simulationExecutor = Executors.newFixedThreadPool(4);
|
||||
for (Ability option: options) {
|
||||
allActions.add(option);
|
||||
// SimulationWorker worker = new SimulationWorker(game, this, previousActions, option);
|
||||
// simulationExecutor.submit(worker);
|
||||
}
|
||||
// simulationExecutor.shutdown();
|
||||
// while(!simulationExecutor.isTerminated()) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// protected void simulateAction(Game game, SimulatedAction previousActions, Ability action) {
|
||||
// List<Ability> actions = new ArrayList<Ability>(previousActions.getAbilities());
|
||||
// actions.add(action);
|
||||
// Game sim = game.copy();
|
||||
// if (sim.getPlayer(playerId).activateAbility((ActivatedAbility) action.copy(), sim)) {
|
||||
// sim.applyEffects();
|
||||
// sim.getPlayers().resetPassed();
|
||||
// allActions.add(new SimulatedAction(sim, actions));
|
||||
// }
|
||||
// }
|
||||
|
||||
public List<Combat> addAttackers(Game game) {
|
||||
Map<Integer, Combat> engagements = new HashMap<Integer, Combat>();
|
||||
//useful only for two player games - will only attack first opponent
|
||||
UUID defenderId = game.getOpponents(playerId).iterator().next();
|
||||
List<Permanent> attackersList = super.getAvailableAttackers(game);
|
||||
//use binary digits to calculate powerset of attackers
|
||||
int powerElements = (int) Math.pow(2, attackersList.size());
|
||||
StringBuilder binary = new StringBuilder();
|
||||
for (int i = powerElements - 1; i >= 0; i--) {
|
||||
Game sim = game.copy();
|
||||
binary.setLength(0);
|
||||
binary.append(Integer.toBinaryString(i));
|
||||
while (binary.length() < attackersList.size()) {
|
||||
binary.insert(0, "0");
|
||||
}
|
||||
for (int j = 0; j < attackersList.size(); j++) {
|
||||
if (binary.charAt(j) == '1')
|
||||
sim.getCombat().declareAttacker(attackersList.get(j).getId(), defenderId, sim);
|
||||
}
|
||||
if (engagements.put(sim.getCombat().getValue(sim), sim.getCombat()) != null) {
|
||||
logger.fine("simulating -- found redundant attack combination");
|
||||
}
|
||||
else if (logger.isLoggable(Level.FINE)) {
|
||||
logger.fine("simulating -- attack:" + sim.getCombat().getGroups().size());
|
||||
}
|
||||
}
|
||||
return new ArrayList<Combat>(engagements.values());
|
||||
}
|
||||
|
||||
public List<Combat> addBlockers(Game game) {
|
||||
Map<Integer, Combat> engagements = new HashMap<Integer, Combat>();
|
||||
int numGroups = game.getCombat().getGroups().size();
|
||||
if (numGroups == 0) return new ArrayList<Combat>();
|
||||
|
||||
//add a node with no blockers
|
||||
Game sim = game.copy();
|
||||
engagements.put(sim.getCombat().getValue(sim), sim.getCombat());
|
||||
sim.fireEvent(GameEvent.getEvent(GameEvent.EventType.DECLARED_BLOCKERS, playerId, playerId));
|
||||
|
||||
List<Permanent> blockers = getAvailableBlockers(game);
|
||||
addBlocker(game, blockers, engagements);
|
||||
|
||||
return new ArrayList<Combat>(engagements.values());
|
||||
}
|
||||
|
||||
protected void addBlocker(Game game, List<Permanent> blockers, Map<Integer, Combat> engagements) {
|
||||
if (blockers.size() == 0)
|
||||
return;
|
||||
int numGroups = game.getCombat().getGroups().size();
|
||||
//try to block each attacker with each potential blocker
|
||||
Permanent blocker = blockers.get(0);
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("simulating -- block:" + blocker);
|
||||
List<Permanent> remaining = remove(blockers, blocker);
|
||||
for (int i = 0; i < numGroups; i++) {
|
||||
if (game.getCombat().getGroups().get(i).canBlock(blocker, game)) {
|
||||
Game sim = game.copy();
|
||||
sim.getCombat().getGroups().get(i).addBlocker(blocker.getId(), playerId, sim);
|
||||
if (engagements.put(sim.getCombat().getValue(sim), sim.getCombat()) != null)
|
||||
logger.fine("simulating -- found redundant block combination");
|
||||
addBlocker(sim, remaining, engagements); // and recurse minus the used blocker
|
||||
}
|
||||
}
|
||||
addBlocker(game, remaining, engagements);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean triggerAbility(TriggeredAbility source, Game game) {
|
||||
Ability ability = source.copy();
|
||||
List<Ability> options = getPlayableOptions(ability, game);
|
||||
if (options.size() == 0) {
|
||||
if (logger.isLoggable(Level.FINE))
|
||||
logger.fine("simulating -- triggered ability:" + ability);
|
||||
game.getStack().push(new StackAbility(ability, playerId));
|
||||
ability.activate(game, false);
|
||||
game.applyEffects();
|
||||
game.getPlayers().resetPassed();
|
||||
}
|
||||
else {
|
||||
SimulationNode parent = (SimulationNode) game.getCustomData();
|
||||
int depth = parent.getDepth() - 1;
|
||||
if (depth == 0) return true;
|
||||
logger.fine("simulating -- triggered ability - adding children:" + options.size());
|
||||
for (Ability option: options) {
|
||||
addAbilityNode(parent, option, depth, game);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void addAbilityNode(SimulationNode parent, Ability ability, int depth, Game game) {
|
||||
Game sim = game.copy();
|
||||
sim.getStack().push(new StackAbility(ability, playerId));
|
||||
ability.activate(sim, false);
|
||||
sim.applyEffects();
|
||||
SimulationNode newNode = new SimulationNode(sim, depth, playerId);
|
||||
logger.fine("simulating -- node #:" + SimulationNode.getCount() + " triggered ability option");
|
||||
for (Target target: ability.getTargets()) {
|
||||
for (UUID targetId: target.getTargets()) {
|
||||
newNode.getTargets().add(targetId);
|
||||
}
|
||||
}
|
||||
for (Choice choice: ability.getChoices()) {
|
||||
newNode.getChoices().add(choice.getChoice());
|
||||
}
|
||||
parent.children.add(newNode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void priority(Game game) {
|
||||
//should never get here
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,127 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import mage.abilities.Ability;
|
||||
import mage.game.Game;
|
||||
import mage.game.combat.Combat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class SimulationNode implements Serializable {
|
||||
|
||||
protected static int nodeCount;
|
||||
|
||||
protected Game game;
|
||||
protected int gameValue;
|
||||
protected List<Ability> abilities;
|
||||
protected int depth;
|
||||
protected List<SimulationNode> children = new ArrayList<SimulationNode>();
|
||||
protected List<UUID> targets = new ArrayList<UUID>();
|
||||
protected List<String> choices = new ArrayList<String>();
|
||||
protected UUID playerId;
|
||||
protected Combat combat;
|
||||
|
||||
public SimulationNode(Game game, int depth, UUID playerId) {
|
||||
this.game = game;
|
||||
this.depth = depth;
|
||||
this.playerId = playerId;
|
||||
game.setCustomData(this);
|
||||
nodeCount++;
|
||||
}
|
||||
|
||||
public SimulationNode(Game game, List<Ability> abilities, int depth, UUID playerId) {
|
||||
this(game, depth, playerId);
|
||||
this.abilities = abilities;
|
||||
}
|
||||
|
||||
public SimulationNode(Game game, Ability ability, int depth, UUID playerId) {
|
||||
this(game, depth, playerId);
|
||||
this.abilities = new ArrayList<Ability>();
|
||||
abilities.add(ability);
|
||||
}
|
||||
|
||||
public static void resetCount() {
|
||||
nodeCount = 0;
|
||||
}
|
||||
|
||||
public static int getCount() {
|
||||
return nodeCount;
|
||||
}
|
||||
|
||||
public Game getGame() {
|
||||
return this.game;
|
||||
}
|
||||
|
||||
public int getGameValue() {
|
||||
return this.gameValue;
|
||||
}
|
||||
|
||||
public void setGameValue(int value) {
|
||||
this.gameValue = value;
|
||||
}
|
||||
|
||||
public List<Ability> getAbilities() {
|
||||
return this.abilities;
|
||||
}
|
||||
|
||||
public List<SimulationNode> getChildren() {
|
||||
return this.children;
|
||||
}
|
||||
|
||||
public int getDepth() {
|
||||
return this.depth;
|
||||
}
|
||||
|
||||
public UUID getPlayerId() {
|
||||
return this.playerId;
|
||||
}
|
||||
|
||||
public Combat getCombat() {
|
||||
return this.combat;
|
||||
}
|
||||
|
||||
public void setCombat(Combat combat) {
|
||||
this.combat = combat;
|
||||
}
|
||||
|
||||
public List<UUID> getTargets() {
|
||||
return this.targets;
|
||||
}
|
||||
|
||||
public List<String> getChoices() {
|
||||
return this.choices;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.ai;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import mage.abilities.Ability;
|
||||
import mage.game.Game;
|
||||
import mage.util.Logging;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class SimulationWorker implements Callable {
|
||||
|
||||
private final static Logger logger = Logging.getLogger(SimulationWorker.class.getName());
|
||||
|
||||
private Game game;
|
||||
private SimulatedAction previousActions;
|
||||
private Ability action;
|
||||
private SimulatedPlayer player;
|
||||
|
||||
public SimulationWorker(Game game, SimulatedPlayer player, SimulatedAction previousActions, Ability action) {
|
||||
this.game = game;
|
||||
this.player = player;
|
||||
this.previousActions = previousActions;
|
||||
this.action = action;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call() {
|
||||
try {
|
||||
// player.simulateAction(game, previousActions, action);
|
||||
} catch (Exception ex) {
|
||||
logger.log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
51
Mage.Server.Plugins/Mage.Player.Human/pom.xml
Normal file
51
Mage.Server.Plugins/Mage.Player.Human/pom.xml
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.mage</groupId>
|
||||
<artifactId>mage-root</artifactId>
|
||||
<version>0.5</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>Mage-Player-Human</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>Mage Player Human</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mage</groupId>
|
||||
<artifactId>Mage</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<configuration>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
<finalName>mage-player-human</finalName>
|
||||
</build>
|
||||
|
||||
<properties/>
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,535 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.human;
|
||||
|
||||
import java.util.List;
|
||||
import mage.abilities.TriggeredAbilities;
|
||||
import mage.abilities.TriggeredAbility;
|
||||
import mage.abilities.effects.ReplacementEffect;
|
||||
import mage.cards.Cards;
|
||||
import mage.choices.Choice;
|
||||
import mage.players.*;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import mage.Constants.Outcome;
|
||||
import mage.Constants.RangeOfInfluence;
|
||||
import mage.Constants.TargetController;
|
||||
import mage.Constants.Zone;
|
||||
import mage.MageObject;
|
||||
import mage.abilities.Ability;
|
||||
import mage.abilities.ActivatedAbility;
|
||||
import mage.abilities.SpecialAction;
|
||||
import mage.abilities.costs.common.SacrificeSourceCost;
|
||||
import mage.abilities.costs.mana.ManaCost;
|
||||
import mage.abilities.costs.mana.VariableManaCost;
|
||||
import mage.cards.decks.Deck;
|
||||
import mage.choices.ChoiceImpl;
|
||||
import mage.filter.common.FilterCreatureForCombat;
|
||||
import mage.game.Game;
|
||||
import mage.game.permanent.Permanent;
|
||||
import mage.target.Target;
|
||||
import mage.target.TargetAmount;
|
||||
import mage.target.TargetCard;
|
||||
import mage.target.common.TargetAttackingCreature;
|
||||
import mage.target.common.TargetCreatureOrPlayer;
|
||||
import mage.target.common.TargetCreaturePermanent;
|
||||
import mage.target.common.TargetDefender;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class HumanPlayer extends PlayerImpl<HumanPlayer> {
|
||||
|
||||
final transient PlayerResponse response = new PlayerResponse();
|
||||
|
||||
private boolean abort;
|
||||
|
||||
protected static FilterCreatureForCombat filter = new FilterCreatureForCombat();
|
||||
protected static Choice replacementEffectChoice = new ChoiceImpl(true);
|
||||
|
||||
static {
|
||||
filter.setTargetController(TargetController.YOU);
|
||||
replacementEffectChoice.setMessage("Choose replacement effect");
|
||||
}
|
||||
protected transient TargetCreaturePermanent targetCombat = new TargetCreaturePermanent(filter);
|
||||
|
||||
public HumanPlayer(String name, Deck deck, RangeOfInfluence range) {
|
||||
super(name, deck, range);
|
||||
human = true;
|
||||
}
|
||||
|
||||
public HumanPlayer(final HumanPlayer player) {
|
||||
super(player);
|
||||
this.abort = player.abort;
|
||||
}
|
||||
|
||||
protected void waitForResponse() {
|
||||
response.clear();
|
||||
synchronized(response) {
|
||||
try {
|
||||
response.wait();
|
||||
} catch (InterruptedException ex) { }
|
||||
}
|
||||
}
|
||||
|
||||
protected void waitForBooleanResponse() {
|
||||
do {
|
||||
waitForResponse();
|
||||
} while (response.getBoolean() == null && !abort);
|
||||
}
|
||||
|
||||
protected void waitForUUIDResponse() {
|
||||
do {
|
||||
waitForResponse();
|
||||
} while (response.getUUID() == null && !abort);
|
||||
}
|
||||
|
||||
protected void waitForStringResponse() {
|
||||
do {
|
||||
waitForResponse();
|
||||
} while (response.getString() == null && !abort);
|
||||
}
|
||||
|
||||
protected void waitForIntegerResponse() {
|
||||
do {
|
||||
waitForResponse();
|
||||
} while (response.getInteger() == null && !abort);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseMulligan(Game game) {
|
||||
game.fireAskPlayerEvent(playerId, "Do you want to take a mulligan?");
|
||||
waitForBooleanResponse();
|
||||
if (!abort)
|
||||
return response.getBoolean();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseUse(Outcome outcome, String message, Game game) {
|
||||
game.fireAskPlayerEvent(playerId, message);
|
||||
waitForBooleanResponse();
|
||||
if (!abort)
|
||||
return response.getBoolean();
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int chooseEffect(List<ReplacementEffect> rEffects, Game game) {
|
||||
replacementEffectChoice.getChoices().clear();
|
||||
for (ReplacementEffect effect: rEffects) {
|
||||
replacementEffectChoice.getChoices().add(effect.getText(null));
|
||||
}
|
||||
if (replacementEffectChoice.getChoices().size() == 1)
|
||||
return 0;
|
||||
while (!abort) {
|
||||
game.fireChooseEvent(playerId, replacementEffectChoice);
|
||||
waitForResponse();
|
||||
if (response.getString() != null) {
|
||||
replacementEffectChoice.setChoice(response.getString());
|
||||
for (int i = 0; i < rEffects.size(); i++) {
|
||||
if (replacementEffectChoice.getChoice().equals(rEffects.get(i).getText(null)))
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean choose(Outcome outcome, Choice choice, Game game) {
|
||||
while (!abort) {
|
||||
game.fireChooseEvent(playerId, choice);
|
||||
waitForResponse();
|
||||
if (response.getString() != null) {
|
||||
choice.setChoice(response.getString());
|
||||
return true;
|
||||
} else if (!choice.isRequired()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean choose(Outcome outcome, Target target, Game game) {
|
||||
while (!abort) {
|
||||
game.fireSelectTargetEvent(playerId, target.getMessage(), target.possibleTargets(null, playerId, game), target.isRequired());
|
||||
waitForResponse();
|
||||
if (response.getUUID() != null) {
|
||||
if (target.canTarget(response.getUUID(), game)) {
|
||||
target.add(response.getUUID(), game);
|
||||
return true;
|
||||
}
|
||||
} else if (!target.isRequired()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseTarget(Outcome outcome, Target target, Ability source, Game game) {
|
||||
while (!abort) {
|
||||
game.fireSelectTargetEvent(playerId, target.getMessage(), target.possibleTargets(source==null?null:source.getId(), playerId, game), target.isRequired());
|
||||
waitForResponse();
|
||||
if (response.getUUID() != null) {
|
||||
if (target.canTarget(response.getUUID(), source, game)) {
|
||||
target.addTarget(response.getUUID(), source, game);
|
||||
return true;
|
||||
}
|
||||
} else if (!target.isRequired()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean choose(Cards cards, TargetCard target, Game game) {
|
||||
while (!abort) {
|
||||
game.fireSelectTargetEvent(playerId, target.getMessage(), cards, target.isRequired());
|
||||
waitForResponse();
|
||||
if (response.getUUID() != null) {
|
||||
if (target.canTarget(response.getUUID(), cards, game)) {
|
||||
target.add(response.getUUID(), game);
|
||||
return true;
|
||||
}
|
||||
} else if (!target.isRequired()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseTarget(Cards cards, TargetCard target, Ability source, Game game) {
|
||||
while (!abort) {
|
||||
game.fireSelectTargetEvent(playerId, target.getMessage(), cards, target.isRequired());
|
||||
waitForResponse();
|
||||
if (response.getUUID() != null) {
|
||||
if (target.canTarget(response.getUUID(), cards, game)) {
|
||||
target.addTarget(response.getUUID(), source, game);
|
||||
return true;
|
||||
}
|
||||
} else if (!target.isRequired()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean chooseTargetAmount(Outcome outcome, TargetAmount target, Ability source, Game game) {
|
||||
while (!abort) {
|
||||
game.fireSelectTargetEvent(playerId, target.getMessage() + "\n Amount remaining:" + target.getAmountRemaining(), target.possibleTargets(source==null?null:source.getId(), playerId, game), target.isRequired());
|
||||
waitForResponse();
|
||||
if (response.getUUID() != null) {
|
||||
if (target.canTarget(response.getUUID(), source, game)) {
|
||||
UUID targetId = response.getUUID();
|
||||
int amountSelected = getAmount(1, target.getAmountRemaining(), "Select amount", game);
|
||||
target.addTarget(targetId, amountSelected, source, game);
|
||||
return true;
|
||||
}
|
||||
} else if (!target.isRequired()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void priority(Game game) {
|
||||
passed = false;
|
||||
if (!abort) {
|
||||
if (passedTurn && game.getStack().isEmpty()) {
|
||||
pass();
|
||||
return;
|
||||
}
|
||||
game.firePriorityEvent(playerId);
|
||||
waitForResponse();
|
||||
if (response.getBoolean() != null) {
|
||||
pass();
|
||||
} else if (response.getInteger() != null) {
|
||||
pass();
|
||||
passedTurn = true;
|
||||
} else if (response.getString() != null && response.getString().equals("special")) {
|
||||
specialAction(game);
|
||||
} else if (response.getUUID() != null) {
|
||||
MageObject object = game.getObject(response.getUUID());
|
||||
if (object != null) {
|
||||
Map<UUID, ActivatedAbility> useableAbilities = null;
|
||||
switch (game.getZone(object.getId())) {
|
||||
case HAND:
|
||||
useableAbilities = getUseableAbilities(object.getAbilities().getActivatedAbilities(Zone.HAND), game);
|
||||
break;
|
||||
case BATTLEFIELD:
|
||||
useableAbilities = getUseableAbilities(object.getAbilities().getActivatedAbilities(Zone.BATTLEFIELD), game);
|
||||
break;
|
||||
case GRAVEYARD:
|
||||
useableAbilities = getUseableAbilities(object.getAbilities().getActivatedAbilities(Zone.GRAVEYARD), game);
|
||||
break;
|
||||
}
|
||||
if (useableAbilities != null && useableAbilities.size() > 0) {
|
||||
activateAbility(useableAbilities, game);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TriggeredAbility chooseTriggeredAbility(TriggeredAbilities abilities, Game game) {
|
||||
while (!abort) {
|
||||
game.fireSelectTargetEvent(playerId, "Pick triggered ability", abilities, true);
|
||||
waitForResponse();
|
||||
if (response.getUUID() != null) {
|
||||
for (TriggeredAbility ability: abilities) {
|
||||
if (ability.getId().equals(response.getUUID()))
|
||||
return ability;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean playMana(ManaCost unpaid, Game game) {
|
||||
game.firePlayManaEvent(playerId, "Pay " + unpaid.getText());
|
||||
waitForResponse();
|
||||
if (response.getBoolean() != null) {
|
||||
return false;
|
||||
} else if (response.getUUID() != null) {
|
||||
playManaAbilities(game);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean playXMana(VariableManaCost cost, Game game) {
|
||||
game.firePlayXManaEvent(playerId, "Pay {X}: {X}=" + cost.getAmount());
|
||||
waitForResponse();
|
||||
if (response.getBoolean() != null) {
|
||||
if (!response.getBoolean())
|
||||
return false;
|
||||
cost.setPaid();
|
||||
} else if (response.getUUID() != null) {
|
||||
playManaAbilities(game);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected void playManaAbilities(Game game) {
|
||||
MageObject object = game.getObject(response.getUUID());
|
||||
Map<UUID, ActivatedAbility> useableAbilities;
|
||||
switch (game.getZone(object.getId())) {
|
||||
case HAND:
|
||||
useableAbilities = getUseableAbilities(object.getAbilities().getManaAbilities(Zone.HAND), game);
|
||||
if (useableAbilities.size() > 0) {
|
||||
activateAbility(useableAbilities, game);
|
||||
}
|
||||
break;
|
||||
case BATTLEFIELD:
|
||||
useableAbilities = getUseableAbilities(object.getAbilities().getManaAbilities(Zone.BATTLEFIELD), game);
|
||||
if (useableAbilities.size() > 0) {
|
||||
activateAbility(useableAbilities, game);
|
||||
}
|
||||
break;
|
||||
case GRAVEYARD:
|
||||
useableAbilities = getUseableAbilities(object.getAbilities().getManaAbilities(Zone.GRAVEYARD), game);
|
||||
if (useableAbilities.size() > 0) {
|
||||
activateAbility(useableAbilities, game);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectAttackers(Game game) {
|
||||
while (!abort) {
|
||||
targetCombat.getTargets().clear();
|
||||
game.fireSelectEvent(playerId, "Select attackers");
|
||||
waitForResponse();
|
||||
if (response.getBoolean() != null) {
|
||||
return;
|
||||
} else if (response.getUUID() != null) {
|
||||
if (targetCombat.canTarget(playerId, response.getUUID(), null, game)) {
|
||||
selectDefender(game.getCombat().getDefenders(), response.getUUID(), game);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean selectDefender(Set<UUID> defenders, UUID attackerId, Game game) {
|
||||
if (defenders.size() == 1) {
|
||||
declareAttacker(attackerId, defenders.iterator().next(), game);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
TargetDefender target = new TargetDefender(defenders, attackerId);
|
||||
if (chooseTarget(Outcome.Damage, target, null, game)) {
|
||||
declareAttacker(attackerId, response.getUUID(), game);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void selectBlockers(Game game) {
|
||||
while (!abort) {
|
||||
targetCombat.getTargets().clear();
|
||||
game.fireSelectEvent(playerId, "Select blockers");
|
||||
waitForResponse();
|
||||
if (response.getBoolean() != null) {
|
||||
return;
|
||||
} else if (response.getUUID() != null) {
|
||||
if (targetCombat.canTarget(playerId, response.getUUID(), null, game)) {
|
||||
selectCombatGroup(response.getUUID(), game);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void selectCombatGroup(UUID blockerId, Game game) {
|
||||
TargetAttackingCreature target = new TargetAttackingCreature();
|
||||
game.fireSelectTargetEvent(playerId, "Select attacker to block", target.possibleTargets(null, playerId, game), target.isRequired());
|
||||
waitForResponse();
|
||||
if (response.getBoolean() != null) {
|
||||
return;
|
||||
} else if (response.getUUID() != null) {
|
||||
declareBlocker(blockerId, response.getUUID(), game);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void assignDamage(int damage, List<UUID> targets, UUID sourceId, Game game) {
|
||||
int remainingDamage = damage;
|
||||
while (remainingDamage > 0) {
|
||||
Target target = new TargetCreatureOrPlayer();
|
||||
choose(Outcome.Damage, target, game);
|
||||
if (targets.size() == 0 || targets.contains(target.getFirstTarget())) {
|
||||
int damageAmount = getAmount(0, remainingDamage, "Select amount", game);
|
||||
Permanent permanent = game.getPermanent(target.getFirstTarget());
|
||||
if (permanent != null) {
|
||||
permanent.damage(damageAmount, sourceId, game, true, false);
|
||||
remainingDamage -= damageAmount;
|
||||
}
|
||||
else {
|
||||
Player player = game.getPlayer(target.getFirstTarget());
|
||||
if (player != null) {
|
||||
player.damage(damageAmount, sourceId, game, false, true);
|
||||
remainingDamage -= damageAmount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAmount(int min, int max, String message, Game game) {
|
||||
game.fireGetAmountEvent(playerId, message, min, max);
|
||||
waitForIntegerResponse();
|
||||
return response.getInteger();
|
||||
}
|
||||
|
||||
protected void specialAction(Game game) {
|
||||
Map<UUID, SpecialAction> specialActions = game.getState().getSpecialActions().getControlledBy(playerId);
|
||||
game.fireGetChoiceEvent(playerId, name, specialActions.values());
|
||||
waitForResponse();
|
||||
if (response.getUUID() != null) {
|
||||
if (specialActions.containsKey(response.getUUID()))
|
||||
activateAbility(specialActions.get(response.getUUID()), game);
|
||||
}
|
||||
}
|
||||
|
||||
protected void activateAbility(Map<UUID, ? extends ActivatedAbility> abilities, Game game) {
|
||||
if (abilities.size() == 1) {
|
||||
ActivatedAbility ability = abilities.values().iterator().next();
|
||||
if (ability.getTargets().size() != 0 || !(ability.getCosts().size() == 1 && ability.getCosts().get(0) instanceof SacrificeSourceCost)) {
|
||||
activateAbility(ability, game);
|
||||
return;
|
||||
}
|
||||
}
|
||||
game.fireGetChoiceEvent(playerId, name, abilities.values());
|
||||
waitForResponse();
|
||||
if (response.getUUID() != null) {
|
||||
if (abilities.containsKey(response.getUUID()))
|
||||
activateAbility(abilities.get(response.getUUID()), game);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResponseString(String responseString) {
|
||||
synchronized(response) {
|
||||
response.setString(responseString);
|
||||
response.notify();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResponseUUID(UUID responseUUID) {
|
||||
synchronized(response) {
|
||||
response.setUUID(responseUUID);
|
||||
response.notify();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResponseBoolean(Boolean responseBoolean) {
|
||||
synchronized(response) {
|
||||
response.setBoolean(responseBoolean);
|
||||
response.notify();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResponseInteger(Integer responseInteger) {
|
||||
synchronized(response) {
|
||||
response.setInteger(responseInteger);
|
||||
response.notify();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void abort() {
|
||||
abort = true;
|
||||
synchronized(response) {
|
||||
response.notify();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public HumanPlayer copy() {
|
||||
return new HumanPlayer(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are
|
||||
* permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
||||
* conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
||||
* of conditions and the following disclaimer in the documentation and/or other materials
|
||||
* provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
|
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* The views and conclusions contained in the software and documentation are those of the
|
||||
* authors and should not be interpreted as representing official policies, either expressed
|
||||
* or implied, of BetaSteward_at_googlemail.com.
|
||||
*/
|
||||
|
||||
package mage.player.human;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author BetaSteward_at_googlemail.com
|
||||
*/
|
||||
public class PlayerResponse implements Serializable {
|
||||
|
||||
private String responseString;
|
||||
private UUID responseUUID;
|
||||
private Boolean responseBoolean;
|
||||
private Integer responseInteger;
|
||||
|
||||
public void clear() {
|
||||
responseString = null;
|
||||
responseUUID = null;
|
||||
responseBoolean = null;
|
||||
responseInteger = null;
|
||||
}
|
||||
|
||||
public String getString() {
|
||||
return responseString;
|
||||
}
|
||||
|
||||
public void setString(String responseString) {
|
||||
this.responseString = responseString;
|
||||
}
|
||||
|
||||
public UUID getUUID() {
|
||||
return responseUUID;
|
||||
}
|
||||
|
||||
public void setUUID(UUID responseUUID) {
|
||||
this.responseUUID = responseUUID;
|
||||
}
|
||||
|
||||
public Boolean getBoolean() {
|
||||
return responseBoolean;
|
||||
}
|
||||
|
||||
public void setBoolean(Boolean responseBoolean) {
|
||||
this.responseBoolean = responseBoolean;
|
||||
}
|
||||
|
||||
public Integer getInteger() {
|
||||
return responseInteger;
|
||||
}
|
||||
|
||||
public void setInteger(Integer responseInteger) {
|
||||
this.responseInteger = responseInteger;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue