Package net.sf.freecol.server.ai
Class AIPlayer
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.server.ai.AIObject
-
- net.sf.freecol.server.ai.AIPlayer
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,ObjectWithId
- Direct Known Subclasses:
MissionAIPlayer
public abstract class AIPlayer extends AIObject
Objects of this class contains AI-information for a singlePlayer
and is used for controlling this player. The methodstartWorking()
gets called by theAIInGameInputHandler
when it is this player's turn.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Random
aiRandom
The PRNG to use for this AI player.private static java.util.Comparator<AIUnit>
aiUnitLocationComparator
A comparator to sort AI units by location.private static java.util.logging.Logger
logger
private static java.lang.Runnable
nullRunnable
Do nothing!private Player
player
The FreeColGameObject this AIObject contains AI-information for.private static java.lang.String
RANDOM_STATE_TAG
private AIServerAPI
serverAPI
The wrapper for the server.static java.lang.String
TAG
-
Fields inherited from class net.sf.freecol.server.ai.AIObject
initialized
-
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE_TAG, DEFAULT_CLASS_INDEX, fcoComparator, ID_ATTRIBUTE_TAG, PARTIAL_ATTRIBUTE_TAG, VALUE_TAG
-
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description DiplomaticTrade.TradeStatus
acceptDiplomaticTrade(DiplomaticTrade agreement)
Resolves a diplomatic trade offer.boolean
acceptMercenaries()
Decides to accept an offer of mercenaries or not.boolean
acceptTax(int tax)
Decides to accept a tax raise or not.AIServerAPI
askServer()
Meaningfully named access to the server API.Constants.IntegrityType
checkIntegrity(boolean fix, LogBuilder lb)
AIObjects need integrity checking too.void
chooseFoundingFatherHandler(java.util.List<FoundingFather> fathers)
Choose a founding father.abstract void
completeWish(Wish w)
Notify that a wish has been completed.protected Stance
determineStance(Player other)
Standard stance change determination.void
diplomacyHandler(FreeColGameObject our, FreeColGameObject other, DiplomaticTrade agreement)
Handle a diplomacy message.void
firstContactHandler(Player contactor, Player contactee, Tile tile)
Handle a first contact.void
fountainOfYouthHandler(int n)
Handle a fountain of youth.protected java.lang.String
getAIAdvantage()
Gets the advantage of this AI player from the nation type.java.util.List<AIColony>
getAIColonies()
Gets a list of the players AI colonies.AIColony
getAIColony(Colony colony)
Gets the AI colony corresponding to a given colony, if any.java.util.Random
getAIRandom()
Gets the PRNG to use for this player.protected AIUnit
getAIUnit(Unit unit)
Gets the AI unit corresponding to a given unit, if any.protected java.util.List<AIUnit>
getAIUnits()
Gets a list of AIUnits for the player.Connection
getConnection()
Gets the connection to the server.abstract int
getNeededWagons(Tile tile)
Gets the needed wagons for a tile/contiguity.Player
getPlayer()
Gets thePlayer
thisAIPlayer
is controlling.java.lang.String
getXMLTagName()
Get the serialization tag for this object.abstract NativeTrade.NativeTradeAction
handleTrade(NativeTrade.NativeTradeAction action, NativeTrade nt)
Handle a native trade request.Constants.IndianDemandAction
indianDemand(Unit unit, Colony colony, GoodsType type, int amount, Constants.IndianDemandAction accept)
Decide whether to accept an Indian demand, or not.void
indianDemandHandler(Unit unit, Colony colony, GoodsType type, int amount, Constants.IndianDemandAction initial)
Handle a native demand.protected void
invoke(java.lang.Runnable runnable)
Do some work in a subthread.void
lootCargoHandler(Unit unit, java.util.List<Goods> initialGoods, java.lang.String defenderId)
Handle a looting.void
monarchActionHandler(Monarch.MonarchAction action, int tax)
Handle the monarch.void
nationSummaryHandler(Player other, NationSummary ns)
Handle an incoming nation summary.void
nativeTradeHandler(NativeTrade.NativeTradeAction action, NativeTrade nt)
Handle a native trade.void
newLandNameHandler(Unit unit, java.lang.String name)
Handle a new land name.void
newRegionNameHandler(Region region, Tile tile, Unit unit, java.lang.String name)
Handle a new region naming.abstract int
pioneersNeeded()
How many pioneers should we have? This is the desired total number, not the actual number which would take into account the number of existing PioneeringMissions.protected void
readAttributes(FreeColXMLReader xr)
Reads the attributes of this object from an XML stream.void
reconnectHandler()
Handle reconnect.void
removeAIObject(AIObject ao)
Remove one of our owned objects.abstract int
scoutsNeeded()
How many scouts should we have? This is the desired total number, not the actual number which would take into account the number of existing ScoutingMissions.FoundingFather
selectFoundingFather(java.util.List<FoundingFather> ffs)
Selects the most useful founding father offered.void
setAIHandler(Player player, boolean ai)
Handle setting the AI state of a player.void
setCurrentPlayerHandler(Player currentPlayer)
Handle a current player setting.void
setInitialized()
Set the initialized flag in this object.abstract void
startWorking()
Tells thisAIPlayer
to make decisions.protected void
writeAttributes(FreeColXMLWriter xw)
Write the attributes of this object to a stream.-
Methods inherited from class net.sf.freecol.server.ai.AIObject
addAIObjectWithId, checkIntegrity, copyIn, dispose, getAIMain, getGame, getSpecification, isDisposed, setGame, setSpecification
-
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addAbility, addFeatures, addModifier, addPropertyChangeListener, addPropertyChangeListener, apply, apply, applyModifiers, applyModifiers, arrayKey, compareIds, compareTo, containsAbilityKey, containsModifierKey, copy, copy, copy, copy, copyInCast, dumpObject, equals, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAbilities, getAbilities, getAbilities, getAbilities, getClassIndex, getDefenceModifiers, getDisplayObject, getFeatureContainer, getFreeColObjectClass, getFreeColObjectClassByName, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSuffix, getSuffix, hasAbility, hasAbility, hasAbility, hashCode, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readChild, readChildren, readFromXML, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setId, toString, toXML, toXML, toXMLPartial, toXMLPartial, writeChildren
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
nullRunnable
private static final java.lang.Runnable nullRunnable
Do nothing!
-
aiUnitLocationComparator
private static final java.util.Comparator<AIUnit> aiUnitLocationComparator
A comparator to sort AI units by location.
-
player
private Player player
The FreeColGameObject this AIObject contains AI-information for.
-
aiRandom
private java.util.Random aiRandom
The PRNG to use for this AI player.
-
serverAPI
private AIServerAPI serverAPI
The wrapper for the server.
-
RANDOM_STATE_TAG
private static final java.lang.String RANDOM_STATE_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AIPlayer
protected AIPlayer(AIMain aiMain, Player player)
Creates a new AI player.- Parameters:
aiMain
- TheAIMain
the player exists within.player
- ThePlayer
to associate this AI player with.
-
AIPlayer
public AIPlayer(AIMain aiMain, FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Creates a newAIPlayer
from the given XML-representation.- Parameters:
aiMain
- The main AI-object.xr
- The input stream containing the XML.- Throws:
javax.xml.stream.XMLStreamException
- if a problem was encountered during parsing.
-
-
Method Detail
-
setInitialized
public final void setInitialized()
Set the initialized flag in this object. To be implemented by leaf classes, and called in their constructors plus the special case in readChild below where we resolve forward references.- Specified by:
setInitialized
in classAIObject
-
getPlayer
public Player getPlayer()
Gets thePlayer
thisAIPlayer
is controlling.- Returns:
- The
Player
.
-
getAIRandom
public java.util.Random getAIRandom()
Gets the PRNG to use for this player.- Returns:
- A
Random
to use for this player.
-
getAIAdvantage
protected java.lang.String getAIAdvantage()
Gets the advantage of this AI player from the nation type.- Returns:
- A short string stating the national advantage.
-
getConnection
public Connection getConnection()
Gets the connection to the server.- Returns:
- The connection that can be used when communication with the server.
-
askServer
public AIServerAPI askServer()
Meaningfully named access to the server API.- Returns:
- The
AIServerAPI
wrapper.
-
removeAIObject
public void removeAIObject(AIObject ao)
Remove one of our owned objects. Subclasses to override.- Parameters:
ao
- TheAIObject
to remove.
-
getAIColony
public AIColony getAIColony(Colony colony)
Gets the AI colony corresponding to a given colony, if any.- Parameters:
colony
- TheColony
to look up.- Returns:
- The corresponding AI colony or null if not found.
-
getAIColonies
public java.util.List<AIColony> getAIColonies()
Gets a list of the players AI colonies.- Returns:
- A list of AI colonies.
-
getAIUnit
protected AIUnit getAIUnit(Unit unit)
Gets the AI unit corresponding to a given unit, if any.- Parameters:
unit
- TheUnit
to look up.- Returns:
- The corresponding AI unit or null if not found.
-
getAIUnits
protected java.util.List<AIUnit> getAIUnits()
Gets a list of AIUnits for the player.- Returns:
- A list of AIUnits.
-
determineStance
protected Stance determineStance(Player other)
Standard stance change determination.- Parameters:
other
- ThePlayer
wrt consider stance.- Returns:
- The new
Stance
.
-
checkIntegrity
public Constants.IntegrityType checkIntegrity(boolean fix, LogBuilder lb)
AIObjects need integrity checking too.- Overrides:
checkIntegrity
in classAIObject
- Parameters:
fix
- If true, fix problems if possible.lb
- ALogBuilder
to log to.- Returns:
- -1 if there are problems remaining, zero if problems were fixed, +1 if no problems found at all.
-
invoke
protected void invoke(java.lang.Runnable runnable)
Do some work in a subthread.- Parameters:
runnable
- TheRunnable
work to do.
-
chooseFoundingFatherHandler
public void chooseFoundingFatherHandler(java.util.List<FoundingFather> fathers)
Choose a founding father.- Parameters:
fathers
- A list ofFoundingFather
s to choose from.
-
diplomacyHandler
public void diplomacyHandler(FreeColGameObject our, FreeColGameObject other, DiplomaticTrade agreement)
Handle a diplomacy message.- Parameters:
our
- OurFreeColGameObject
.other
- TheirFreeColGameObject
.agreement
- TheDiplomaticTrade
being negotiated.
-
firstContactHandler
public void firstContactHandler(Player contactor, Player contactee, Tile tile)
Handle a first contact.- Parameters:
contactor
- The contactingPlayer
.contactee
- The contactedPlayer
.tile
- TheTile
where contact occurs.
-
fountainOfYouthHandler
public void fountainOfYouthHandler(int n)
Handle a fountain of youth.- Parameters:
n
- The number of emigrating units.
-
indianDemandHandler
public void indianDemandHandler(Unit unit, Colony colony, GoodsType type, int amount, Constants.IndianDemandAction initial)
Handle a native demand.- Parameters:
unit
- TheUnit
making demands.colony
- TheColony
where demands are being made.type
- TheGoodsType
demanded.amount
- The amount of gold demanded.initial
- The acceptance state of the demand.
-
lootCargoHandler
public void lootCargoHandler(Unit unit, java.util.List<Goods> initialGoods, java.lang.String defenderId)
Handle a looting.- Parameters:
unit
- The lootingUnit
.initialGoods
- A list ofGoods
to choose from.defenderId
- The identifier for the defending unit (may be gone!).
-
monarchActionHandler
public void monarchActionHandler(Monarch.MonarchAction action, int tax)
Handle the monarch.- Parameters:
action
- TheMonarchAction
to respond to.tax
- The tax change if any.
-
nationSummaryHandler
public void nationSummaryHandler(Player other, NationSummary ns)
Handle an incoming nation summary.- Parameters:
other
- ThePlayer
the summary applies to.ns
- TheNationSummary
itself.
-
nativeTradeHandler
public void nativeTradeHandler(NativeTrade.NativeTradeAction action, NativeTrade nt)
Handle a native trade.- Parameters:
action
- TheNativeTradeAction
to perform.nt
- TheNativeTrade
itself.
-
newLandNameHandler
public void newLandNameHandler(Unit unit, java.lang.String name)
Handle a new land name.- Parameters:
unit
- TheUnit
discovering the new world.name
- The default name.
-
newRegionNameHandler
public void newRegionNameHandler(Region region, Tile tile, Unit unit, java.lang.String name)
Handle a new region naming.- Parameters:
region
- TheRegion
to name.tile
- TheTile
the tile where the region was found.unit
- TheUnit
that discovers the region.name
- The default name.
-
reconnectHandler
public void reconnectHandler()
Handle reconnect.
-
setAIHandler
public void setAIHandler(Player player, boolean ai)
Handle setting the AI state of a player.- Parameters:
player
- ThePlayer
to set.ai
- The new AI state.
-
setCurrentPlayerHandler
public void setCurrentPlayerHandler(Player currentPlayer)
Handle a current player setting.- Parameters:
currentPlayer
- The new currentPlayer
.
-
startWorking
public abstract void startWorking()
Tells thisAIPlayer
to make decisions. TheAIPlayer
is done doing work this turn when this method returns.
-
indianDemand
public Constants.IndianDemandAction indianDemand(Unit unit, Colony colony, GoodsType type, int amount, Constants.IndianDemandAction accept)
Decide whether to accept an Indian demand, or not. Or for native players, return the result of the demand.- Parameters:
unit
- TheUnit
making demands.colony
- TheColony
where demands are being made.type
- TheGoodsType
demanded.amount
- The amount of gold demanded.accept
- The acceptance state of the demand.- Returns:
- The result of the demand.
-
acceptDiplomaticTrade
public DiplomaticTrade.TradeStatus acceptDiplomaticTrade(DiplomaticTrade agreement)
Resolves a diplomatic trade offer.- Parameters:
agreement
- The proposedDiplomaticTrade
.- Returns:
- The
TradeStatus
to apply to the agreement.
-
handleTrade
public abstract NativeTrade.NativeTradeAction handleTrade(NativeTrade.NativeTradeAction action, NativeTrade nt)
Handle a native trade request.- Parameters:
action
- TheNativeTradeAction
to perform.nt
- TheNativeTrade
to handle.- Returns:
- The action in response.
-
acceptTax
public boolean acceptTax(int tax)
Decides to accept a tax raise or not. Overridden by the European player.- Parameters:
tax
- The tax raise.- Returns:
- True if the raise is accepted.
-
acceptMercenaries
public boolean acceptMercenaries()
Decides to accept an offer of mercenaries or not. Overridden by the European player.- Returns:
- True if the mercenaries are accepted.
-
selectFoundingFather
public FoundingFather selectFoundingFather(java.util.List<FoundingFather> ffs)
Selects the most useful founding father offered. Overridden by EuropeanAIPlayers.- Parameters:
ffs
- The founding fathers on offer.- Returns:
- The founding father selected.
-
getNeededWagons
public abstract int getNeededWagons(Tile tile)
Gets the needed wagons for a tile/contiguity.- Parameters:
tile
- TheTile
to derive the contiguity from.- Returns:
- The number of wagons needed.
-
pioneersNeeded
public abstract int pioneersNeeded()
How many pioneers should we have? This is the desired total number, not the actual number which would take into account the number of existing PioneeringMissions.- Returns:
- The desired number of pioneers for this player.
-
scoutsNeeded
public abstract int scoutsNeeded()
How many scouts should we have? This is the desired total number, not the actual number which would take into account the number of existing ScoutingMissions. Current scheme for European AIs is to use up to three scouts in the early part of the game, then one.- Returns:
- The desired number of scouts for this player.
-
completeWish
public abstract void completeWish(Wish w)
Notify that a wish has been completed. Called from AIColony.- Parameters:
w
- TheWish
to complete.
-
writeAttributes
protected void writeAttributes(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the attributes of this object to a stream. To be overridden if required by any object that has attributes and uses the toXML(FreeColXMLWriter, String) call.- Overrides:
writeAttributes
in classFreeColObject
- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
readAttributes
protected void readAttributes(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the attributes of this object from an XML stream.- Overrides:
readAttributes
in classFreeColObject
- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
getXMLTagName
public java.lang.String getXMLTagName()
Get the serialization tag for this object.- Specified by:
getXMLTagName
in classFreeColObject
- Returns:
- The tag.
-
-