|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectnet.sf.freecol.common.model.FreeColObject
net.sf.freecol.server.ai.AIObject
net.sf.freecol.server.ai.goal.Goal
public abstract class Goal
A Goal is used to encapsulate a specific part of the decision-making process of an AI.
Using a top-down approach, every AIPlayer has a set of Goals which,
in turn, may have further subgoals. In combination, this tree of goals
and subgoals reflects the current strategy of the AIPlayer.
Units (each one wrapped in an AIUnit object) will be moved between
existing Goal objects.
TODO: Turn AIUnit into a simple wrapper for individual units.
Specific AI goals are created by extending this class; some of these could also be used to assist the human player (i.e. GoTo, Scouting, Trade, Piracy).
| Field Summary | |
|---|---|
protected java.util.List<AIUnit> |
availableUnitsList
|
protected boolean |
isFinished
|
private static java.util.logging.Logger |
logger
|
protected boolean |
needsPlanning
|
private Goal |
parentGoal
|
protected AIPlayer |
player
|
private float |
relativeWeight
|
private int |
turnCreated
|
private int |
turnLastUnitAdded
|
| Fields inherited from class net.sf.freecol.server.ai.AIObject |
|---|
uninitialized |
| Fields inherited from class net.sf.freecol.common.model.FreeColObject |
|---|
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, PARTIAL_ATTRIBUTE |
| Fields inherited from interface net.sf.freecol.server.ai.goal.GoalConstants |
|---|
MAX_SEARCH_RADIUS |
| Constructor Summary | |
|---|---|
Goal(AIPlayer p,
Goal g,
float w)
Standard constructor |
|
Goal(AIPlayer p,
Goal g,
float w,
AIUnit u)
Alternate constructor - directly add a unit to this Goal. |
|
| Method Summary | |
|---|---|
void |
addUnit(AIUnit u)
Adds a unit to this goal. |
protected void |
addUnitToParent(AIUnit u)
Adds a unit to the parent goal. |
java.util.List<AIUnit> |
cancelGoal()
Cancels a goal and all of its subgoals. |
boolean |
canYieldUnit(UnitType ut,
AIObject o)
Used by a parent goal to check whether this goal, including subgoals, can yield a specific unit. |
void |
doPlanning()
Recursively calls doPlanning() in subgoals that needsPlanning(),
then calls its own planning method. |
float |
getAbsoluteWeight()
Returns the absolute weight of this goal. |
java.lang.String |
getDebugDescription()
Build and return a string describing this goal including its parent goal. |
java.lang.String |
getGoalDescription()
Returns a string describing just this goal. |
protected abstract java.util.Iterator<AIUnit> |
getOwnedAIUnitsIterator()
Since internal implementation details may vary, each Goal will define an iterator over all of its units. |
float |
getParentWeight()
Gets the weight of the parent goal, or 1 if there is no parent goal. |
protected abstract java.util.Iterator<Goal> |
getSubGoalIterator()
Since internal implementation details may vary, each Goal will define an iterator over all of its subgoals. |
float |
getWeight()
Returns the relativeWeight this goal has been weighted with by its parent. |
static java.lang.String |
getXMLElementTagName()
Returns the tag name of the root element representing this object. |
float |
getYieldedUnitWeight(UnitType ut,
AIObject o)
Returns the absolute weight of the unit which would be yielded by yieldUnit(UnitType,AIObject). |
boolean |
isFinished()
Determines whether this goal is finished. |
boolean |
needsPlanning()
Determines whether this or a subgoal needsPlanning. |
void |
normalizeSubGoalWeights()
Calling this ensures that the relative weights given to subgoals add up to 1. |
protected abstract void |
plan()
This is the method that actually does the planning for this goal. |
protected abstract void |
readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
Reads information for this object from an XML stream. |
protected abstract void |
removeUnit(AIUnit u)
Ensures that a unit moved to another Goal is properly removed from this. |
protected void |
requestWorker(GoodsType gt,
int minProduction)
Wrapper method for a worker request sent to the AIPlayer. |
void |
setNeedsPlanningRecursive(boolean p)
Sets the needsPlanning status of this Goal and all its subgoals. |
void |
setWeight(float w)
Sets a relative weight for this goal. |
protected abstract void |
toXMLImpl(javax.xml.stream.XMLStreamWriter out)
Writes this object to an XML stream. |
protected void |
validateOwnedUnits()
Checks all owned AIUnits for validity, and removes invalid ones. |
AIUnit |
yieldUnit(UnitType ut,
AIObject o)
Removes a unit from the goal, potentially from a subgoal, and yields it to the caller. |
| Methods inherited from class net.sf.freecol.server.ai.AIObject |
|---|
dispose, getAIMain, getAIRandom, getGame, isUninitialized, readFromXML |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
private static final java.util.logging.Logger logger
private float relativeWeight
private int turnCreated
private int turnLastUnitAdded
protected boolean needsPlanning
protected boolean isFinished
protected java.util.List<AIUnit> availableUnitsList
protected AIPlayer player
private Goal parentGoal
| Constructor Detail |
|---|
public Goal(AIPlayer p,
Goal g,
float w)
p - The AIPlayer this goal belongs tog - The parent goal; may be null if we're a direct goal of the AIPlayerw - The relativeWeight of this goal
public Goal(AIPlayer p,
Goal g,
float w,
AIUnit u)
p - The AIPlayer this goal belongs tog - The parent goal; may be null if we're a direct goal of the AIPlayerw - The relativeWeight of this goalu - An initial AIUnit given to this goal| Method Detail |
|---|
public boolean isFinished()
public java.util.List<AIUnit> cancelGoal()
NOTE: Preferably, only the direct parent should call this.
AIUnit being freed up by this actionpublic void doPlanning()
doPlanning() in subgoals that needsPlanning(),
then calls its own planning method.
public boolean needsPlanning()
needsPlanning.
public void setNeedsPlanningRecursive(boolean p)
needsPlanning status of this Goal and all its subgoals.
Should be called by the AIPlayer once for each of its subgoals
at the start of a turn. The Goal will handle all other instances of this
flag needing to be reset internally.
p - Boolean determining whether to set needsPlanning =true or =falsepublic float getWeight()
NOTE: In many cases, you will want to use getAbsoluteWeight() instead.
relativeWeight of this goalpublic float getParentWeight()
public float getAbsoluteWeight()
The absolute weight is the weight of this goal in relation to the AIPlayer.
This is used when making requests, to allow the AIPlayer to find the
"most important" request.
public void setWeight(float w)
w - A relative weight, should be in range [0;1]public void normalizeSubGoalWeights()
NOTE: This allows for a small margin of error (+/- 0.05), to avoid recalculating too often.
protected void requestWorker(GoodsType gt,
int minProduction)
AIPlayer.
Each Goal can request necessary units from the AIPlayer.
Here, such a request is wrapped in a private method for convenience.
Each request contains a weight, which is getAbsoluteWeight()
of this goal, and the number of turns since a unit request has last been granted.
The latter should be taken into account as a "bonus weight" by the AIPlayer.
TODO: AIPlayer#addUnitWish(Goal,GoodsType,int,float,int); should add
requests to a set-like structure, so that there's only one active request
per Goal at any time. Since fulfilling a request using addUnit(AIUnit)
means that plan() will be called again during the turn,
the Goal will be able to request again.
gt - The GoodsType we're requesting a worker for.minProduction - The minimum a unit needs to produce to be considered.
Should be 0 if any worker will do, or the value of
UnitType.getProductionFor(GoodsType,int) if an existing worker
unit is supposed to be replaced.public void addUnit(AIUnit u)
AIPlayer fulfilling a unit request,
by the parent goal, or by a subgoal that no longer needs the unit.
Possible TODO: If the unit we're requesting is a high-interest one,
such as a Galleon, AIUnit#setLoaningGoal() may be used to
signal that this unit may _only_ be moved to subgoals, or back to
AIPlayer, but not further up the hierarchy or to any other requesting Goal.
u - The AIUnit being added to this goalprotected void addUnitToParent(AIUnit u)
AIPlayer instead.
u - The AIUnit to be added to the parent
public boolean canYieldUnit(UnitType ut,
AIObject o)
This recursively checks its subgoals, if there's no match among the own units.
Possible TODO: Check whether AIUnit#isOnLoan() - in which case, we mustn't
yield a unit unless it's the AIPlayer that requests.
ut - The UnitType wanted by the parento - The AIObject (should be AIPlayer or another Goal) calling this
UnitType, false otherwise
public float getYieldedUnitWeight(UnitType ut,
AIObject o)
yieldUnit(UnitType,AIObject).
This is the same as getAbsoluteWeight() of the yielding goal.
ut - The UnitType wanted by the parento - The AIObject (should be AIPlayer or another Goal) calling this
canYieldUnit(UnitType,AIObject) first,
or is responsible to sanitize this result itself before trying
to yieldUnit(UnitType,AIObject) based on it.
public AIUnit yieldUnit(UnitType ut,
AIObject o)
Returned unit should be the one with minimum absolute weight,
see getYieldedUnitWeight(UnitType,AIObject).
ut - The UnitType wanted by the parento - The AIObject (should be AIPlayer or another Goal) calling this
AIUnit with minimal absolute weight.
Note that this may be null if no matching unit is found!protected void validateOwnedUnits()
NOTE: The assumption here is that AIUnit#isValid() will return true
as long as the Unit wrapped in it exists.
public java.lang.String getGoalDescription()
getDebugDescription.
public java.lang.String getDebugDescription()
public static java.lang.String getXMLElementTagName()
protected abstract java.util.Iterator<AIUnit> getOwnedAIUnitsIterator()
AIUnit currently managed by this goal.protected abstract java.util.Iterator<Goal> getSubGoalIterator()
protected abstract void removeUnit(AIUnit u)
yieldUnit(UnitType,AIObject),
or if the Unit contained in the AIUnit no longer is valid,
this method is called to clean up any remaining references to the unit.
Any implementation of this will need to iterate over all AIUnit object references used by this goal, and remove those that equal the given parameter.
u - The AIUnit supposed to be removed from this goal.protected abstract void plan()
validateOwnedUnits() to remove AIUnits no longer
containing a valid unitavailableUnitsList to work
AIPlayer, orrequestWorker(GoodsType,int))setWeight(float)
protected abstract void toXMLImpl(javax.xml.stream.XMLStreamWriter out)
throws javax.xml.stream.XMLStreamException
toXMLImpl in class FreeColObjectout - The target stream.
javax.xml.stream.XMLStreamException - if there are any problems writing to the
stream.
protected abstract void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
throws javax.xml.stream.XMLStreamException
readFromXMLImpl in class FreeColObjectin - The input stream with the XML.
javax.xml.stream.XMLStreamException - if there are any problems reading from the
stream.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||