Package net.sf.freecol.common.model
Class Role
- java.lang.Object
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,Named
,ObjectWithId
public class Role extends BuildableType
The role of a unit.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Role.RoleChange
Container for valid role changes.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
CAPTURE_TAG
private Role
downgrade
The Role to downgrade to after losing a battle.private static java.lang.String
DOWNGRADE_TAG
private static java.lang.String
EXPERT_UNIT_TAG
private UnitType
expertUnit
The expert unit for this Role, e.g.private static java.lang.String
FROM_TAG
private static java.lang.String
MAXIMUM_COUNT_TAG
private int
maximumCount
The maximum multiple of required goods this Role may carry.static java.util.Comparator<Role>
militaryComparator
A comparator to order roles by descending total military effectiveness.private static java.lang.String
OLD_EXPERT_UNIT_TAG
private static java.lang.String
OLD_MAXIMUM_COUNT_TAG
private static java.lang.String
ROLE_CHANGE_TAG
private java.util.List<Role.RoleChange>
roleChanges
The role changes by capture available for this role.static java.lang.String
TAG
-
Fields inherited from class net.sf.freecol.common.model.BuildableType
REQUIRED_POPULATION_TAG
-
Fields inherited from class net.sf.freecol.common.model.FreeColSpecObjectType
DELETE_TAG, EXTENDS_TAG, PRESERVE_TAG
-
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE_TAG, DEFAULT_CLASS_INDEX, fcoComparator, ID_ATTRIBUTE_TAG, logger, PARTIAL_ATTRIBUTE_TAG, VALUE_TAG
-
-
Constructor Summary
Constructors Constructor Description Role(java.lang.String id, Specification specification)
Creates a newRole
instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
addRoleChange(java.lang.String from, java.lang.String capture)
Add a new role change.Colony.NoBuildReason
canBeBuiltInColony(Colony colony, java.util.List<BuildableType> assumeBuilt)
Check to see if this buildable type can be built in a colony based on the buildings or units available.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.static java.util.List<Role>
getAvailableRoles(Player player, UnitType type, java.util.List<Role> roles)
Filter a list of proposed roles by availability.private double
getDefence()
Get the defence value for this role.Role
getDowngrade()
Get the downgraded role from this one.UnitType
getExpertUnit()
Gets the expert profession for this role.static java.util.List<AbstractGoods>
getGoodsDifference(Role from, int fromCount, Role to, int toCount)
Gets a list of goods required to change from the first role to the second.int
getMaximumCount()
Gets the maximum goods count for the role.double
getOffence()
Get the offense value for this role.java.util.stream.Stream<AbstractGoods>
getRequiredGoods(int roleCount)
Get the required goods for this role, considering also the role count, as a stream.java.util.List<AbstractGoods>
getRequiredGoodsList(int roleCount)
Get the required goods for this role, considering also the role count.int
getRequiredGoodsPrice(Market market)
Get the price of the required goods in a given market.java.util.List<Role.RoleChange>
getRoleChanges()
Get the role changes that can allow a unit to assume this role.static java.lang.String
getRoleIdSuffix(java.lang.String roleId)
Get the last part of a role identifier.int
getRoleIndex()
Establish a simple ordering.java.lang.String
getRoleSuffix()
Get the last part of a role identifier.java.lang.String
getXMLTagName()
Get the serialization tag for this object.boolean
isCompatibleWith(Role other)
Is this role compatible with another?boolean
isDefaultRole()
Is this the default role?static boolean
isDefaultRoleId(java.lang.String roleId)
Is this the default role?boolean
isDefensive()
Is this an defensive role?boolean
isOffensive()
Is this an offensive role?protected void
readAttributes(FreeColXMLReader xr)
Reads the attributes of this object from an XML stream.protected void
readChild(FreeColXMLReader xr)
Reads a single child object.protected void
readChildren(FreeColXMLReader xr)
Reads the children of this object from an XML stream.static boolean
rolesCompatible(Role role1, Role role2)
Are two roles compatible.void
setDowngrade(Role newDowngrade)
Sets the downgraded role.void
setExpertUnit(UnitType newExpertUnit)
Sets the expert profession for this role.void
setMaximumCount(int newMaximumCount)
Sets the maximum goods count for this role.protected void
setRoleChanges(java.util.List<Role.RoleChange> roleChanges)
Set the role change list.java.lang.String
toFullString()
Longer format debug helper.protected void
writeAttributes(FreeColXMLWriter xw)
Write the attributes of this object to a stream.protected void
writeChildren(FreeColXMLWriter xw)
Write the children of this object to a stream.-
Methods inherited from class net.sf.freecol.common.model.BuildableType
addRequiredAbility, getCurrentlyBuildingLabel, getLimits, getMaximumIndex, getMinimumIndex, getRequiredAbilities, getRequiredAmountOf, getRequiredGoods, getRequiredGoodsList, getRequiredPopulation, isAvailableTo, needsGoodsToBuild, removeRequiredAbility, requiresAbility, setLimits, setRequiredAbilities, setRequiredGoods, setRequiredPopulation
-
Methods inherited from class net.sf.freecol.common.model.FreeColSpecObjectType
addScope, appliesTo, copyScopes, getDescriptionKey, getFeatureContainer, getIndex, getNameKey, getScopeList, getScopes, isAbstractType, removeScope, setIndex, toString
-
Methods inherited from class net.sf.freecol.common.model.FreeColSpecObject
getSpecification, 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, getFreeColObjectClass, getFreeColObjectClassByName, getGame, 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, readFromXML, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setGame, setId, toXML, toXML, toXMLPartial, toXMLPartial
-
-
-
-
Field Detail
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
militaryComparator
public static final java.util.Comparator<Role> militaryComparator
A comparator to order roles by descending total military effectiveness.
-
downgrade
private Role downgrade
The Role to downgrade to after losing a battle. Defaults tonull
. Note that some UnitTypes and Roles may be disposed instead of downgraded when losing a battle.
-
maximumCount
private int maximumCount
The maximum multiple of required goods this Role may carry. Defaults to1
.
-
expertUnit
private UnitType expertUnit
The expert unit for this Role, e.g. a hardy pioneer is an expert for the pioneer role.
-
roleChanges
private java.util.List<Role.RoleChange> roleChanges
The role changes by capture available for this role.
-
CAPTURE_TAG
private static final java.lang.String CAPTURE_TAG
- See Also:
- Constant Field Values
-
DOWNGRADE_TAG
private static final java.lang.String DOWNGRADE_TAG
- See Also:
- Constant Field Values
-
FROM_TAG
private static final java.lang.String FROM_TAG
- See Also:
- Constant Field Values
-
EXPERT_UNIT_TAG
private static final java.lang.String EXPERT_UNIT_TAG
- See Also:
- Constant Field Values
-
MAXIMUM_COUNT_TAG
private static final java.lang.String MAXIMUM_COUNT_TAG
- See Also:
- Constant Field Values
-
ROLE_CHANGE_TAG
private static final java.lang.String ROLE_CHANGE_TAG
- See Also:
- Constant Field Values
-
OLD_EXPERT_UNIT_TAG
private static final java.lang.String OLD_EXPERT_UNIT_TAG
- See Also:
- Constant Field Values
-
OLD_MAXIMUM_COUNT_TAG
private static final java.lang.String OLD_MAXIMUM_COUNT_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Role
public Role(java.lang.String id, Specification specification)
Creates a newRole
instance.- Parameters:
id
- The object identifier.specification
- TheSpecification
to refer to.
-
-
Method Detail
-
isDefaultRoleId
public static boolean isDefaultRoleId(java.lang.String roleId)
Is this the default role?- Parameters:
roleId
- The role identifier to test.- Returns:
- True if this is the default role.
-
isDefaultRole
public boolean isDefaultRole()
Is this the default role?- Returns:
- True if this is the default role.
-
getRoleSuffix
public java.lang.String getRoleSuffix()
Get the last part of a role identifier.- Returns:
- The role suffix.
-
getRoleIdSuffix
public static java.lang.String getRoleIdSuffix(java.lang.String roleId)
Get the last part of a role identifier.- Parameters:
roleId
- A role identifier.- Returns:
- The role suffix.
-
getDowngrade
public final Role getDowngrade()
Get the downgraded role from this one.- Returns:
- The downgraded role.
-
setDowngrade
public final void setDowngrade(Role newDowngrade)
Sets the downgraded role.- Parameters:
newDowngrade
- The new downgraded role.
-
getMaximumCount
public final int getMaximumCount()
Gets the maximum goods count for the role.- Returns:
- The maximum goods count.
-
setMaximumCount
public final void setMaximumCount(int newMaximumCount)
Sets the maximum goods count for this role.- Parameters:
newMaximumCount
- The new maximum goods count.
-
getExpertUnit
public final UnitType getExpertUnit()
Gets the expert profession for this role.- Returns:
- The expert type.
-
setExpertUnit
public final void setExpertUnit(UnitType newExpertUnit)
Sets the expert profession for this role.- Parameters:
newExpertUnit
- The new expert type.
-
getRequiredGoodsList
public java.util.List<AbstractGoods> getRequiredGoodsList(int roleCount)
Get the required goods for this role, considering also the role count.- Parameters:
roleCount
- The role count.- Returns:
- A list of required goods.
-
getRequiredGoods
public java.util.stream.Stream<AbstractGoods> getRequiredGoods(int roleCount)
Get the required goods for this role, considering also the role count, as a stream.- Parameters:
roleCount
- The role count.- Returns:
- A stream of required goods.
-
getRequiredGoodsPrice
public int getRequiredGoodsPrice(Market market)
Get the price of the required goods in a given market.- Parameters:
market
- TheMarket
to evaluate in.- Returns:
- The price of the goods for this role.
-
getRoleChanges
public final java.util.List<Role.RoleChange> getRoleChanges()
Get the role changes that can allow a unit to assume this role.- Returns:
- A list of
RoleChange
s.
-
setRoleChanges
protected final void setRoleChanges(java.util.List<Role.RoleChange> roleChanges)
Set the role change list.- Parameters:
roleChanges
- The new list ofRoleChange
s.
-
addRoleChange
private void addRoleChange(java.lang.String from, java.lang.String capture)
Add a new role change.- Parameters:
from
- The source role identifier.capture
- The identifier for the role to capture.
-
getOffence
public double getOffence()
Get the offense value for this role.- Returns:
- The offense value.
-
isOffensive
public boolean isOffensive()
Is this an offensive role?- Returns:
- True if this is an offensive role.
-
getDefence
private double getDefence()
Get the defence value for this role.- Returns:
- The defence value.
-
isDefensive
public boolean isDefensive()
Is this an defensive role?- Returns:
- True if this is an defensive role.
-
isCompatibleWith
public boolean isCompatibleWith(Role other)
Is this role compatible with another?- Parameters:
other
- The otherRole
to compare with.- Returns:
- True if the other role is compatible.
-
rolesCompatible
public static boolean rolesCompatible(Role role1, Role role2)
Are two roles compatible.- Parameters:
role1
- ARole
to compare.role2
- The otherRole
to compare.- Returns:
- True if the roles are compatible.
-
getGoodsDifference
public static java.util.List<AbstractGoods> getGoodsDifference(Role from, int fromCount, Role to, int toCount)
Gets a list of goods required to change from the first role to the second. The first role may benull
implying the default role, the second must not. Note that excess goods that are left over after the change will appear on the list with negative amounts.- Parameters:
from
- The currentRole
.fromCount
- The role count for the current role.to
- The newRole
to assume.toCount
- The role count for the new role.- Returns:
- A list of
AbstractGoods
required to make the change.
-
getRoleIndex
public int getRoleIndex()
Establish a simple ordering. Normal roles, then REF-specific roles, then native-specific roles. Reduce by role-specific equipment amounts to further separate the levels such that the heavier armed roles sort first.- Returns:
- A role index.
-
getAvailableRoles
public static java.util.List<Role> getAvailableRoles(Player player, UnitType type, java.util.List<Role> roles)
Filter a list of proposed roles by availability.- Parameters:
player
- ThePlayer
to own the unit.type
- TheUnitType
to check.roles
- A list of proposedRole
s.- Returns:
- A list of available
Role
s.
-
toFullString
public java.lang.String toFullString()
Longer format debug helper.- Returns:
- A more detailed description of this role.
-
canBeBuiltInColony
public Colony.NoBuildReason canBeBuiltInColony(Colony colony, java.util.List<BuildableType> assumeBuilt)
Check to see if this buildable type can be built in a colony based on the buildings or units available.- Specified by:
canBeBuiltInColony
in classBuildableType
- Parameters:
colony
- TheColony
to check.assumeBuilt
- A list ofBuildableType
s.- Returns:
- The reason the for failure, or NoBuildReason.NONE on success.
-
copyIn
public <T extends FreeColObject> boolean copyIn(T other)
Copy another FreeColObject into this one if it is compatible.- Overrides:
copyIn
in classBuildableType
- Type Parameters:
T
- TheFreeColObject
subclass of the object to copy in.- Parameters:
other
- The other object.- Returns:
- True if the copy in is succesful.
-
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 classBuildableType
- Parameters:
xw
- TheFreeColXMLWriter
to write to.- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems writing to the stream.
-
writeChildren
protected void writeChildren(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write the children of this object to a stream. To be overridden if required by any object that has children and uses the toXML(FreeColXMLWriter, String) call.- Overrides:
writeChildren
in classBuildableType
- 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 classBuildableType
- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readChildren
protected void readChildren(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads the children of this object from an XML stream.- Overrides:
readChildren
in classBuildableType
- Parameters:
xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if there is a problem reading the stream.
-
readChild
protected void readChild(FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Reads a single child object. Subclasses must override to read their enclosed elements. This particular instance of the routine always throws XMLStreamException because we should never arrive here. However it is very useful to always call super.readChild() when an unexpected tag is encountered, as the exception thrown here provides some useful debugging context.- Overrides:
readChild
in classBuildableType
- 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.
-
-