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 singlePlayerand is used for controlling this player. The methodstartWorking()gets called by theAIInGameInputHandlerwhen it is this player's turn.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.RandomaiRandomThe PRNG to use for this AI player.private static java.util.Comparator<AIUnit>aiUnitLocationComparatorA comparator to sort AI units by location.private static java.util.logging.Loggerloggerprivate static java.lang.RunnablenullRunnableDo nothing!private PlayerplayerThe FreeColGameObject this AIObject contains AI-information for.private static java.lang.StringRANDOM_STATE_TAGprivate AIServerAPIserverAPIThe wrapper for the server.static java.lang.StringTAG-
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.TradeStatusacceptDiplomaticTrade(DiplomaticTrade agreement)Resolves a diplomatic trade offer.booleanacceptMercenaries()Decides to accept an offer of mercenaries or not.booleanacceptTax(int tax)Decides to accept a tax raise or not.AIServerAPIaskServer()Meaningfully named access to the server API.Constants.IntegrityTypecheckIntegrity(boolean fix, LogBuilder lb)AIObjects need integrity checking too.voidchooseFoundingFatherHandler(java.util.List<FoundingFather> fathers)Choose a founding father.abstract voidcompleteWish(Wish w)Notify that a wish has been completed.protected StancedetermineStance(Player other)Standard stance change determination.voiddiplomacyHandler(FreeColGameObject our, FreeColGameObject other, DiplomaticTrade agreement)Handle a diplomacy message.voidfirstContactHandler(Player contactor, Player contactee, Tile tile)Handle a first contact.voidfountainOfYouthHandler(int n)Handle a fountain of youth.protected java.lang.StringgetAIAdvantage()Gets the advantage of this AI player from the nation type.java.util.List<AIColony>getAIColonies()Gets a list of the players AI colonies.AIColonygetAIColony(Colony colony)Gets the AI colony corresponding to a given colony, if any.java.util.RandomgetAIRandom()Gets the PRNG to use for this player.protected AIUnitgetAIUnit(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.ConnectiongetConnection()Gets the connection to the server.abstract intgetNeededWagons(Tile tile)Gets the needed wagons for a tile/contiguity.PlayergetPlayer()Gets thePlayerthisAIPlayeris controlling.java.lang.StringgetXMLTagName()Get the serialization tag for this object.abstract NativeTrade.NativeTradeActionhandleTrade(NativeTrade.NativeTradeAction action, NativeTrade nt)Handle a native trade request.Constants.IndianDemandActionindianDemand(Unit unit, Colony colony, GoodsType type, int amount, Constants.IndianDemandAction accept)Decide whether to accept an Indian demand, or not.voidindianDemandHandler(Unit unit, Colony colony, GoodsType type, int amount, Constants.IndianDemandAction initial)Handle a native demand.protected voidinvoke(java.lang.Runnable runnable)Do some work in a subthread.voidlootCargoHandler(Unit unit, java.util.List<Goods> initialGoods, java.lang.String defenderId)Handle a looting.voidmonarchActionHandler(Monarch.MonarchAction action, int tax)Handle the monarch.voidnationSummaryHandler(Player other, NationSummary ns)Handle an incoming nation summary.voidnativeTradeHandler(NativeTrade.NativeTradeAction action, NativeTrade nt)Handle a native trade.voidnewLandNameHandler(Unit unit, java.lang.String name)Handle a new land name.voidnewRegionNameHandler(Region region, Tile tile, Unit unit, java.lang.String name)Handle a new region naming.abstract intpioneersNeeded()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 voidreadAttributes(FreeColXMLReader xr)Reads the attributes of this object from an XML stream.voidreconnectHandler()Handle reconnect.voidremoveAIObject(AIObject ao)Remove one of our owned objects.abstract intscoutsNeeded()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.FoundingFatherselectFoundingFather(java.util.List<FoundingFather> ffs)Selects the most useful founding father offered.voidsetAIHandler(Player player, boolean ai)Handle setting the AI state of a player.voidsetCurrentPlayerHandler(Player currentPlayer)Handle a current player setting.voidsetInitialized()Set the initialized flag in this object.abstract voidstartWorking()Tells thisAIPlayerto make decisions.protected voidwriteAttributes(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- TheAIMainthe player exists within.player- ThePlayerto associate this AI player with.
-
AIPlayer
public AIPlayer(AIMain aiMain, FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Creates a newAIPlayerfrom 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:
setInitializedin classAIObject
-
getPlayer
public Player getPlayer()
Gets thePlayerthisAIPlayeris controlling.- Returns:
- The
Player.
-
getAIRandom
public java.util.Random getAIRandom()
Gets the PRNG to use for this player.- Returns:
- A
Randomto 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
AIServerAPIwrapper.
-
removeAIObject
public void removeAIObject(AIObject ao)
Remove one of our owned objects. Subclasses to override.- Parameters:
ao- TheAIObjectto remove.
-
getAIColony
public AIColony getAIColony(Colony colony)
Gets the AI colony corresponding to a given colony, if any.- Parameters:
colony- TheColonyto 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- TheUnitto 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- ThePlayerwrt consider stance.- Returns:
- The new
Stance.
-
checkIntegrity
public Constants.IntegrityType checkIntegrity(boolean fix, LogBuilder lb)
AIObjects need integrity checking too.- Overrides:
checkIntegrityin classAIObject- Parameters:
fix- If true, fix problems if possible.lb- ALogBuilderto 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- TheRunnablework to do.
-
chooseFoundingFatherHandler
public void chooseFoundingFatherHandler(java.util.List<FoundingFather> fathers)
Choose a founding father.- Parameters:
fathers- A list ofFoundingFathers to choose from.
-
diplomacyHandler
public void diplomacyHandler(FreeColGameObject our, FreeColGameObject other, DiplomaticTrade agreement)
Handle a diplomacy message.- Parameters:
our- OurFreeColGameObject.other- TheirFreeColGameObject.agreement- TheDiplomaticTradebeing negotiated.
-
firstContactHandler
public void firstContactHandler(Player contactor, Player contactee, Tile tile)
Handle a first contact.- Parameters:
contactor- The contactingPlayer.contactee- The contactedPlayer.tile- TheTilewhere 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- TheUnitmaking demands.colony- TheColonywhere demands are being made.type- TheGoodsTypedemanded.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 ofGoodsto 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- TheMonarchActionto respond to.tax- The tax change if any.
-
nationSummaryHandler
public void nationSummaryHandler(Player other, NationSummary ns)
Handle an incoming nation summary.- Parameters:
other- ThePlayerthe summary applies to.ns- TheNationSummaryitself.
-
nativeTradeHandler
public void nativeTradeHandler(NativeTrade.NativeTradeAction action, NativeTrade nt)
Handle a native trade.- Parameters:
action- TheNativeTradeActionto perform.nt- TheNativeTradeitself.
-
newLandNameHandler
public void newLandNameHandler(Unit unit, java.lang.String name)
Handle a new land name.- Parameters:
unit- TheUnitdiscovering 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- TheRegionto name.tile- TheTilethe tile where the region was found.unit- TheUnitthat 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- ThePlayerto 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 thisAIPlayerto make decisions. TheAIPlayeris 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- TheUnitmaking demands.colony- TheColonywhere demands are being made.type- TheGoodsTypedemanded.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
TradeStatusto apply to the agreement.
-
handleTrade
public abstract NativeTrade.NativeTradeAction handleTrade(NativeTrade.NativeTradeAction action, NativeTrade nt)
Handle a native trade request.- Parameters:
action- TheNativeTradeActionto perform.nt- TheNativeTradeto 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- TheTileto 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- TheWishto 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:
writeAttributesin classFreeColObject- Parameters:
xw- TheFreeColXMLWriterto 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:
readAttributesin classFreeColObject- Parameters:
xr- TheFreeColXMLReaderto 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:
getXMLTagNamein classFreeColObject- Returns:
- The tag.
-
-