Package net.sf.freecol.common.model
Class Game
- java.lang.Object
-
- net.sf.freecol.common.model.FreeColObject
-
- net.sf.freecol.common.model.FreeColGameObject
-
- net.sf.freecol.common.model.Game
-
- All Implemented Interfaces:
java.lang.Comparable<FreeColObject>
,ObjectWithId
- Direct Known Subclasses:
ServerGame
public class Game extends FreeColGameObject
The main component of the game model.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Game.FcgoState
State for the FCGO iterator, out here because it has to be static.static class
Game.LogoutReason
Reasons for logging out.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
CIBOLA_TAG
private static java.lang.String
CLIENT_USER_NAME_TAG
private java.lang.String
clientUserName
The client player name, null in the server.protected CombatModel
combatModel
The combat model this game uses.private static java.lang.String
CURRENT_PLAYER_TAG
protected Player
currentPlayer
The player whose turn it is.protected FreeColGameObjectListener
freeColGameObjectListener
A FreeColGameObjectListener to watch the objects in the game.protected java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<FreeColGameObject>>
freeColGameObjects
References to all objects created in this game.private static java.lang.String
INITIAL_ACTIVE_UNIT_ID
private java.lang.String
initialActiveUnitId
Initial active unit identifier.private static java.util.Map<java.lang.String,java.lang.Class<? extends FreeColGameObject>>
locationClasses
Map of class name to class for the location classes, to speed up game loading.private static java.util.logging.Logger
logger
protected Map
map
The map of the New World.private NationOptions
nationOptions
The current nation options.private static java.lang.String
NEXT_ID_TAG
protected int
nextId
The next available identifier that can be given to a newFreeColGameObject
.protected java.util.List<Player>
players
All the players in the game.private static int
REMOVE_GC_THRESHOLD
The number of removed FCGOs that should trigger a cache clean.private int
removeCount
The number of FCGOs removed since last cache clean.private static java.util.Map<java.lang.Class<? extends FreeColObject>,java.lang.Class<? extends FreeColObject>>
serverClasses
Map of all classes with corresponding server classes.private static java.lang.String
SPANISH_SUCCESSION_TAG
private boolean
spanishSuccession
Whether the War of Spanish Succession has already taken place.private Specification
specification
The Specification this game uses.static java.lang.String
TAG
private Turn
turn
The current turn.private static java.lang.String
TURN_TAG
private Player
unknownEnemy
A virtual player to use for enemy privateers.private java.util.UUID
uuid
Game UUID, persistent in savegame filesprivate static java.lang.String
UUID_TAG
-
Fields inherited from class net.sf.freecol.common.model.FreeColGameObject
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
-
-
Constructor Summary
Constructors Modifier Constructor Description Game()
Root constructor for games.Game(Game game, FreeColXMLReader xr)
Creates a newGame
instance from a stream.protected
Game(Specification specification)
Constructor used by the ServerGame constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFreeColGameObject(java.lang.String id, FreeColGameObject fcgo)
Add a newFreeColGameObject
with a given identifier.void
addPlayer(Player player)
Adds the specified player to the game.void
addPlayers(java.util.List<Player> players)
Add a list of players to this game.boolean
allPlayersReadyToLaunch()
Checks if all players are ready to launch.boolean
canAddNewPlayer()
Can a new player be added to this game?void
changeMap(Map newMap)
Change the map in this game, fixing player destinations.Constants.IntegrityType
checkIntegrity(boolean fix, LogBuilder lb)
Checks the integrity of this game object.void
checkOwners(Ownable o, Player oldOwner)
Maintain the player containers for certain ownables.<T extends FreeColObject>
booleancopyIn(T other)
Copy another FreeColObject into this one if it is compatible.boolean
equals(java.lang.Object o)
Location
findFreeColLocation(java.lang.String id)
Convenience wrapper to find a location (which is an interface, precluding using the typed version of getFreeColGameObject()) by identifier.int
getAge()
Get the age for the current turn.java.util.stream.Stream<Colony>
getAllColonies(Player player)
Get all the colonies in the game.java.util.List<Colony>
getAllColoniesList(Player player)
Get a list of all the colonies in the game.Player
getClientPlayer()
Get the client player this thread is operating for.java.lang.String
getClientUserName()
Get the client user name.CombatModel
getCombatModel()
Get the combat model in this game.Player
getCurrentPlayer()
Gets the current player.OptionGroup
getDifficultyOptionGroup()
Get the difficulty level of this game.Player
getFirstPlayer()
Get the first player in this game.FreeColGameObject
getFreeColGameObject(java.lang.String id)
Gets theFreeColGameObject
with the given identifier.<T extends FreeColGameObject>
TgetFreeColGameObject(java.lang.String id, java.lang.Class<T> returnClass)
Gets theFreeColGameObject
with the specified identifier and class.private java.util.Iterator<FreeColGameObject>
getFreeColGameObjectIterator()
Gets anIterator
over every registeredFreeColGameObject
.java.util.List<FreeColGameObject>
getFreeColGameObjectList()
Get a list of all theFreeColGameObjects
.OptionGroup
getGameOptions()
Gets the game options associated with this game.Unit
getInitialActiveUnit()
Get the initial active unit.java.lang.String
getInitialActiveUnitId()
Get the identifier for the initial active unit.java.util.List<Player>
getLiveEuropeanPlayerList(Player... exclude)
Get a list of the live European players in this game.java.util.stream.Stream<Player>
getLiveEuropeanPlayers(Player... exclude)
Get a stream of the live European players in this game.java.util.List<Player>
getLiveNativePlayerList(Player... exclude)
Get a list of the live native players in this game.java.util.stream.Stream<Player>
getLiveNativePlayers(Player... exclude)
Get a stream of the live native players in this game.java.util.List<Player>
getLivePlayerList(Player... exclude)
Get a list of the live players in the game, optionally excluding supplied ones.java.util.stream.Stream<Player>
getLivePlayers(Player... exclude)
Get a stream of the live players in the game, optionally excluding supplied ones.static java.lang.Class<? extends FreeColGameObject>
getLocationClass(java.lang.String id)
Get a location class from an identifier.Map
getMap()
Gets the map that is being used in this game.OptionGroup
getMapGeneratorOptions()
Gets the map generator options associated with this game.FreeColObject
getMessageDisplay(ModelMessage message)
Helper function to get the object to display with a message in this game.FreeColGameObject
getMessageSource(ModelMessage message)
Helper function to get the source object of a message in this game.NationOptions
getNationOptions()
Get the current nation options.int
getNextId()
Stub for routine only meaningful in the server.Player
getNextPlayer()
Gets the next current player.Player
getPlayer(java.util.function.Predicate<? super Player> predicate)
Get a particular player in the game by a predicate.private Player
getPlayerAfter(Player beforePlayer)
Gets the live player after the given player.Player
getPlayerByName(java.lang.String name)
Gets a player specified by a name.Player
getPlayerByNation(Nation nation)
Get aPlayer
identified by its nation.Player
getPlayerByNationId(java.lang.String nationId)
Get aPlayer
identified by its nation identifier.protected java.util.List<Player>
getPlayerList(java.util.function.Predicate<? super Player> predicate)
Get players in the game.java.util.stream.Stream<Player>
getPlayers(java.util.function.Predicate<? super Player> predicate)
Get players in the game.Settlement
getSettlementByName(java.lang.String name)
Finds a settlement by name.boolean
getSpanishSuccession()
Has the Spanish Succession event occured?Specification
getSpecification()
Get the specification.java.util.Map<java.lang.String,java.lang.String>
getStatistics()
Gets the statistics of this game.Turn
getTurn()
Gets the current turn in this game.Player
getUnknownEnemy()
Gets the unknown enemy player, which is used for privateers.java.util.UUID
getUUID()
Gets the unique identifier for this game.Nation
getVacantNation()
Find an available (i.e.java.util.List<Nation>
getVacantNations()
Get the currently available nations.java.lang.String
getXMLTagName()
Get the serialization tag for this object.int
hashCode()
boolean
isInClient()
Are we executing in a client?boolean
isInRevengeMode()
Is this game in revenge mode?boolean
isInServer()
Are we executing in the server?static <T extends FreeColObject>
TnewInstance(Game game, java.lang.Class<T> returnClass, boolean server)
Instantiate an uninitialized FreeColGameObject within a game.void
notifyOwnerChanged(FreeColGameObject source, Player oldOwner, Player newOwner)
Notify a listener (if any) of that a game object has changed owner.void
notifyRemoveFreeColGameObject(java.lang.String id)
Notify a listener (if any) of that a game object has gone.void
notifySetFreeColGameObject(java.lang.String id, FreeColGameObject fcgo)
Notify a listener (if any) of a new game object.boolean
preGameUpdate(Game game)
Special update handler for the pre-game update.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.void
removeFreeColGameObject(java.lang.String id, java.lang.String reason)
Removes theFreeColGameObject
with the specified identifier.boolean
removePlayer(Player player)
Removes the specified player from the game.void
setCombatModel(CombatModel newCombatModel)
Set the game combat model.void
setCurrentPlayer(Player newCurrentPlayer)
Sets the current player.void
setFreeColGameObject(java.lang.String id, FreeColGameObject fcgo)
Set the mapping between object identifier and object.void
setFreeColGameObjectListener(FreeColGameObjectListener fcgol)
Sets theFreeColGameObjectListener
attached to this game.void
setGameOptions(OptionGroup go)
Sets the game options associated with this game.void
setInitialActiveUnitId(java.lang.String initialActiveUnitId)
Set the identifier for the initial active unit.Map
setMap(Map newMap)
Sets the game map.void
setMapGeneratorOptions(OptionGroup mgo)
Sets the map generator options associated with this game.void
setNationOptions(NationOptions newNationOptions)
Set the current nation options.private void
setPlayers(java.util.List<Player> players)
Set the players in the game.void
setSpanishSuccession(boolean spanishSuccession)
Set the Spanish Succession value.void
setSpecification(Specification specification)
Sets the specification for this object.void
setTurn(Turn newTurn)
Sets the current turn in this game.void
setUnknownEnemy(Player player)
Sets the unknown enemy player.void
sortPlayers(java.util.Comparator<Player> comparator)
Sort the players list.<T extends FreeColObject>
Tunserialize(java.lang.String xml, java.lang.Class<T> returnClass)
Unserialize from XML to a FreeColObject in this game.<T extends FreeColGameObject>
java.util.List<T>update(java.util.Collection<T> other, boolean create)
Convenience wrapper to update severalFreeColGameObject
s.<T extends FreeColGameObject>
Tupdate(T other, boolean create)
Update aFreeColGameObject
from another.private <T extends FreeColGameObject>
Tupdate(T other, java.lang.Class<T> returnClass, boolean create)
Update aFreeColGameObject
from another, optionally allowing missing objects to be created.Location
updateLocationRef(Location loc)
Update aLocation
from a reference to it in an update.<T extends FreeColGameObject>
java.util.List<T>updateRef(java.util.Collection<T> other)
Update severalFreeColGameObject
s from a list of references to it in an update.<T extends FreeColGameObject>
TupdateRef(T other)
Update aFreeColGameObject
from a reference to it in an update.private <T extends FreeColGameObject>
TupdateRef(T other, java.lang.Class<T> returnClass)
Update aFreeColGameObject
from a reference to it in an update.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.FreeColGameObject
dispose, disposeResources, fundamentalDispose, getClassIndex, getDisposables, getGame, getLinkTarget, intern, internId, isDisposed, isInitialized, isInternable, readFromXML, setGame
-
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, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAbilities, getAbilities, getAbilities, getAbilities, getDefenceModifiers, getDisplayObject, getFeatureContainer, getFreeColObjectClass, getFreeColObjectClassByName, getId, getIdNumber, getIdType, getIdTypeByName, getModifiers, getModifiers, getModifiers, getModifiers, getObjectClassIndex, getPropertyChangeListeners, getPropertyChangeListeners, getPropertyChangeSupport, getSortedAbilities, getSortedModifiers, getSuffix, getSuffix, hasAbility, hasAbility, hasAbility, hasListeners, hasModifier, hasModifier, hasModifier, idEquals, invokeMethod, logFreeColObjects, readFromXMLPartial, removeAbilities, removeAbility, removeFeatures, removeModifier, removeModifiers, removePropertyChangeListener, removePropertyChangeListener, save, save, save, save, serialize, serialize, serialize, serialize, setId, toString, toXML, toXML, toXMLPartial, toXMLPartial
-
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
TAG
public static final java.lang.String TAG
- See Also:
- Constant Field Values
-
serverClasses
private static final java.util.Map<java.lang.Class<? extends FreeColObject>,java.lang.Class<? extends FreeColObject>> serverClasses
Map of all classes with corresponding server classes.
-
locationClasses
private static final java.util.Map<java.lang.String,java.lang.Class<? extends FreeColGameObject>> locationClasses
Map of class name to class for the location classes, to speed up game loading.
-
specification
private Specification specification
The Specification this game uses.
-
nextId
protected int nextId
The next available identifier that can be given to a newFreeColGameObject
.
-
uuid
private java.util.UUID uuid
Game UUID, persistent in savegame files
-
clientUserName
private java.lang.String clientUserName
The client player name, null in the server.
-
players
protected final java.util.List<Player> players
All the players in the game.
-
unknownEnemy
private Player unknownEnemy
A virtual player to use for enemy privateers.
-
map
protected Map map
The map of the New World.
-
nationOptions
private NationOptions nationOptions
The current nation options. Mainly used to see if a player nation is available.
-
currentPlayer
protected Player currentPlayer
The player whose turn it is.
-
turn
private Turn turn
The current turn.
-
spanishSuccession
private boolean spanishSuccession
Whether the War of Spanish Succession has already taken place.
-
initialActiveUnitId
private java.lang.String initialActiveUnitId
Initial active unit identifier.
-
freeColGameObjects
protected final java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<FreeColGameObject>> freeColGameObjects
References to all objects created in this game. Serialization is not needed directly as these must be completely within { players, unknownEnemy, map } which are directly serialized.
-
combatModel
protected CombatModel combatModel
The combat model this game uses. At the moment, the only combat model available is the SimpleCombatModel, which strives to implement the combat model of the original game. However, it is anticipated that other, more complex combat models will be implemented in future. As soon as that happens, we will also have to make the combat model selectable.
-
REMOVE_GC_THRESHOLD
private static final int REMOVE_GC_THRESHOLD
The number of removed FCGOs that should trigger a cache clean.- See Also:
- Constant Field Values
-
removeCount
private int removeCount
The number of FCGOs removed since last cache clean.
-
freeColGameObjectListener
protected FreeColGameObjectListener freeColGameObjectListener
A FreeColGameObjectListener to watch the objects in the game. Usually this is the AIMain instance. FIXME: is this better done with a property change listener?
-
CIBOLA_TAG
private static final java.lang.String CIBOLA_TAG
- See Also:
- Constant Field Values
-
CLIENT_USER_NAME_TAG
private static final java.lang.String CLIENT_USER_NAME_TAG
- See Also:
- Constant Field Values
-
CURRENT_PLAYER_TAG
private static final java.lang.String CURRENT_PLAYER_TAG
- See Also:
- Constant Field Values
-
INITIAL_ACTIVE_UNIT_ID
private static final java.lang.String INITIAL_ACTIVE_UNIT_ID
- See Also:
- Constant Field Values
-
NEXT_ID_TAG
private static final java.lang.String NEXT_ID_TAG
- See Also:
- Constant Field Values
-
SPANISH_SUCCESSION_TAG
private static final java.lang.String SPANISH_SUCCESSION_TAG
- See Also:
- Constant Field Values
-
TURN_TAG
private static final java.lang.String TURN_TAG
- See Also:
- Constant Field Values
-
UUID_TAG
private static final java.lang.String UUID_TAG
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Game
public Game()
Root constructor for games. Game.newInstance uses this so it must be public.
-
Game
protected Game(Specification specification)
Constructor used by the ServerGame constructor.- Parameters:
specification
- TheSpecification
for this game.
-
Game
public Game(Game game, FreeColXMLReader xr) throws javax.xml.stream.XMLStreamException
Creates a newGame
instance from a stream.- Parameters:
game
- A baseGame
(not used here, this is a very special case).xr
- TheFreeColXMLReader
to read from.- Throws:
javax.xml.stream.XMLStreamException
- if an error occurs
-
-
Method Detail
-
preGameUpdate
public boolean preGameUpdate(Game game)
Special update handler for the pre-game update. When starting a new game the server sends an update that contains the game map. This must be synchronized so that the client does not race ahead and launch into the game before the update completes. We used to check integrity here, but 1) the server has already done that and 2) the unexplored Tiles have no type which makes a lot of integrity failures.- Parameters:
game
- The update for thisGame
.- Returns:
- True if the update succeeds.
-
newInstance
public static <T extends FreeColObject> T newInstance(Game game, java.lang.Class<T> returnClass, boolean server)
Instantiate an uninitialized FreeColGameObject within a game.- Type Parameters:
T
- The actual return type.- Parameters:
game
- TheGame
to instantiate in.returnClass
- The requiredFreeColObject
class.server
- Create a server object if possible.- Returns:
- The new uninitialized object, or null on error.
-
getDifficultyOptionGroup
public final OptionGroup getDifficultyOptionGroup()
Get the difficulty level of this game.- Returns:
- An
OptionGroup
containing the difficulty settings.
-
getGameOptions
public OptionGroup getGameOptions()
Gets the game options associated with this game.- Returns:
- An
OptionGroup
containing the game options.
-
setGameOptions
public void setGameOptions(OptionGroup go)
Sets the game options associated with this game.- Parameters:
go
- AnOptionGroup
containing the game options.
-
getMapGeneratorOptions
public OptionGroup getMapGeneratorOptions()
Gets the map generator options associated with this game.- Returns:
- An
OptionGroup
containing the map generator options.
-
setMapGeneratorOptions
public void setMapGeneratorOptions(OptionGroup mgo)
Sets the map generator options associated with this game.- Parameters:
mgo
- AnOptionGroup
containing the map generator options.
-
getNextId
public int getNextId()
Stub for routine only meaningful in the server.- Returns:
- Nothing.
-
getFreeColGameObject
public FreeColGameObject getFreeColGameObject(java.lang.String id)
Gets theFreeColGameObject
with the given identifier.- Parameters:
id
- The object identifier.- Returns:
- The game object, or null if not found.
-
getFreeColGameObject
public <T extends FreeColGameObject> T getFreeColGameObject(java.lang.String id, java.lang.Class<T> returnClass)
Gets theFreeColGameObject
with the specified identifier and class.- Type Parameters:
T
- The actual return type.- Parameters:
id
- The object identifier.returnClass
- The expected class of the object.- Returns:
- The game object, or null if not found.
-
setFreeColGameObject
public void setFreeColGameObject(java.lang.String id, FreeColGameObject fcgo)
Set the mapping between object identifier and object.- Parameters:
id
- The object identifier.fcgo
- TheFreeColGameObject
to add to thisGame
.
-
addFreeColGameObject
public void addFreeColGameObject(java.lang.String id, FreeColGameObject fcgo)
Add a newFreeColGameObject
with a given identifier.- Parameters:
id
- The object identifier.fcgo
- TheFreeColGameObject
to add to thisGame
.
-
removeFreeColGameObject
public void removeFreeColGameObject(java.lang.String id, java.lang.String reason)
Removes theFreeColGameObject
with the specified identifier.- Parameters:
id
- The object identifier.reason
- A reason to remove the object.- Throws:
java.lang.IllegalArgumentException
- If the identifier is null or empty.
-
update
public <T extends FreeColGameObject> T update(T other, boolean create)
Update aFreeColGameObject
from another.- Type Parameters:
T
- The type of object to update.- Parameters:
other
- The other object.create
- If true, create the object if it is missing.- Returns:
- The resulting object after update.
-
update
private <T extends FreeColGameObject> T update(T other, java.lang.Class<T> returnClass, boolean create)
Update aFreeColGameObject
from another, optionally allowing missing objects to be created.- Type Parameters:
T
- The type of object to update.- Parameters:
other
- The other object.returnClass
- The expected class of the object.create
- If true, create missing objects.- Returns:
- The resulting object after update.
-
update
public <T extends FreeColGameObject> java.util.List<T> update(java.util.Collection<T> other, boolean create)
Convenience wrapper to update severalFreeColGameObject
s.- Type Parameters:
T
- The type of object to update.- Parameters:
other
- The collection of objects to update.create
- If true, create missing objects.- Returns:
- The resulting list of updated objects.
-
updateRef
public <T extends FreeColGameObject> T updateRef(T other)
Update aFreeColGameObject
from a reference to it in an update.- Type Parameters:
T
- The type of object to update.- Parameters:
other
- The other object.- Returns:
- The resulting object after update.
-
updateRef
private <T extends FreeColGameObject> T updateRef(T other, java.lang.Class<T> returnClass)
Update aFreeColGameObject
from a reference to it in an update.- Type Parameters:
T
- The type of object to update.- Parameters:
other
- The other object.returnClass
- The expected class of the object.- Returns:
- The resulting object after update.
-
updateRef
public <T extends FreeColGameObject> java.util.List<T> updateRef(java.util.Collection<T> other)
Update severalFreeColGameObject
s from a list of references to it in an update.- Type Parameters:
T
- The type of object to update.- Parameters:
other
- The other object.- Returns:
- The resulting object after update.
-
updateLocationRef
public Location updateLocationRef(Location loc)
Update aLocation
from a reference to it in an update.- Parameters:
loc
- TheLocation
.- Returns:
- The resulting location after update.
-
findFreeColLocation
public Location findFreeColLocation(java.lang.String id)
Convenience wrapper to find a location (which is an interface, precluding using the typed version of getFreeColGameObject()) by identifier. Use this routine when the object should already be present in the game.- Parameters:
id
- The object identifier.- Returns:
- The
Location
if any.
-
getFreeColGameObjectIterator
private java.util.Iterator<FreeColGameObject> getFreeColGameObjectIterator()
Gets anIterator
over every registeredFreeColGameObject
. ThisIterator
should be iterated once in a while since it cleans theFreeColGameObject
cache. Very few routines call this any more, so there is a thresholded call in removeFreeColGameObject to ensure the cache is still cleaned. Reconsider this if the situation changes. Lock freeColGameObjects when using this.- Returns:
- An
Iterator
containing every registeredFreeColGameObject
.
-
getFreeColGameObjectList
public java.util.List<FreeColGameObject> getFreeColGameObjectList()
Get a list of all theFreeColGameObjects
.- Returns:
- A suitable list.
-
getUUID
public java.util.UUID getUUID()
Gets the unique identifier for this game. A game UUID persists in save game files.- Returns:
- The game
UUID
.
-
getPlayerList
protected java.util.List<Player> getPlayerList(java.util.function.Predicate<? super Player> predicate)
Get players in the game.- Parameters:
predicate
- APredicate
to select suitable players with.- Returns:
- A list of
Player
s.
-
getPlayers
public java.util.stream.Stream<Player> getPlayers(java.util.function.Predicate<? super Player> predicate)
Get players in the game.- Parameters:
predicate
- APredicate
to select suitable players with.- Returns:
- The stream of
Player
s.
-
getPlayer
public Player getPlayer(java.util.function.Predicate<? super Player> predicate)
Get a particular player in the game by a predicate.- Parameters:
predicate
- APredicate
to select suitable players with.- Returns:
- The
Player
found or null if not present.
-
setPlayers
private void setPlayers(java.util.List<Player> players)
Set the players in the game.- Parameters:
players
- The newPlayer
s to add.
-
getPlayerAfter
private Player getPlayerAfter(Player beforePlayer)
Gets the live player after the given player.- Parameters:
beforePlayer
- ThePlayer
before thePlayer
to be returned.- Returns:
- The
Player
after thebeforePlayer
in the list which determines the order each player becomes the current player. - See Also:
getNextPlayer()
-
addPlayer
public void addPlayer(Player player)
Adds the specified player to the game.- Parameters:
player
- ThePlayer
to add.
-
removePlayer
public boolean removePlayer(Player player)
Removes the specified player from the game. We do not really remove the full FCGO from the game, just from the active players list.- Parameters:
player
- ThePlayer
to remove.- Returns:
- True if the player was removed.
-
getFirstPlayer
public Player getFirstPlayer()
Get the first player in this game.- Returns:
- The first player, or null if none present.
-
sortPlayers
public void sortPlayers(java.util.Comparator<Player> comparator)
Sort the players list.- Parameters:
comparator
- TheComparator
to sort with.
-
getPlayerByName
public Player getPlayerByName(java.lang.String name)
Gets a player specified by a name.- Parameters:
name
- The name identifying thePlayer
.- Returns:
- The
Player
or null if none found.
-
getPlayerByNation
public Player getPlayerByNation(Nation nation)
Get aPlayer
identified by its nation.- Parameters:
nation
- TheNation
to search for.- Returns:
- The
Player
of the given nation, or null if not found.
-
getPlayerByNationId
public Player getPlayerByNationId(java.lang.String nationId)
Get aPlayer
identified by its nation identifier.- Parameters:
nationId
- The nation identifier to search for.- Returns:
- The
Player
of the given nation, or null if not found.
-
getLivePlayerList
public java.util.List<Player> getLivePlayerList(Player... exclude)
Get a list of the live players in the game, optionally excluding supplied ones.- Parameters:
exclude
- ThePlayer
s to exclude.- Returns:
- A list of live
Player
s, without the excluded ones.
-
getLivePlayers
public java.util.stream.Stream<Player> getLivePlayers(Player... exclude)
Get a stream of the live players in the game, optionally excluding supplied ones.- Parameters:
exclude
- ThePlayer
s to exclude.- Returns:
- A stream of live
Player
s, without the excluded ones.
-
getLiveEuropeanPlayerList
public java.util.List<Player> getLiveEuropeanPlayerList(Player... exclude)
Get a list of the live European players in this game.- Parameters:
exclude
-Player
s to exclude.- Returns:
- A list of live European
Player
s in this game, without the excluded ones.
-
getLiveEuropeanPlayers
public java.util.stream.Stream<Player> getLiveEuropeanPlayers(Player... exclude)
Get a stream of the live European players in this game.- Parameters:
exclude
-Player
s to exclude.- Returns:
- A stream of live European
Player
s in this game, without the excluded ones.
-
getLiveNativePlayerList
public java.util.List<Player> getLiveNativePlayerList(Player... exclude)
Get a list of the live native players in this game.- Parameters:
exclude
-Player
s to exclude.- Returns:
- A list of live native
Player
s in this game.
-
getLiveNativePlayers
public java.util.stream.Stream<Player> getLiveNativePlayers(Player... exclude)
Get a stream of the live native players in this game.- Parameters:
exclude
-Player
s to exclude.- Returns:
- A stream of live native
Player
s in this game.
-
getNextPlayer
public Player getNextPlayer()
Gets the next current player.- Returns:
- The
Player
whose turn follows the current player.
-
addPlayers
public void addPlayers(java.util.List<Player> players)
Add a list of players to this game. Called from the pre and in-game controllers when players change. ATM we never remove players, even dead ones. Must tolerate player being both present and not present.- Parameters:
players
- The list ofplayers
to add.
-
getUnknownEnemy
public Player getUnknownEnemy()
Gets the unknown enemy player, which is used for privateers.- Returns:
- The unknown enemy
Player
.
-
setUnknownEnemy
public void setUnknownEnemy(Player player)
Sets the unknown enemy player.- Parameters:
player
- ThePlayer
to serve as the unknown enemy.
-
getClientUserName
public java.lang.String getClientUserName()
Get the client user name.- Returns:
- The client user name.
-
isInClient
public boolean isInClient()
Are we executing in a client?- Returns:
- True in a client.
-
isInServer
public boolean isInServer()
Are we executing in the server?- Returns:
- True in the server.
-
getClientPlayer
public Player getClientPlayer()
Get the client player this thread is operating for. If in the server there will be none.- Returns:
- The client
Player
.
-
isInRevengeMode
public boolean isInRevengeMode()
Is this game in revenge mode?- Returns:
- True if an undead player is present.
-
getCurrentPlayer
public Player getCurrentPlayer()
Gets the current player.- Returns:
- The current player.
-
setCurrentPlayer
public void setCurrentPlayer(Player newCurrentPlayer)
Sets the current player.- Parameters:
newCurrentPlayer
- The new currentPlayer
.
-
getMap
public Map getMap()
Gets the map that is being used in this game.- Returns:
- The game
Map
.
-
setMap
public Map setMap(Map newMap)
Sets the game map.- Parameters:
newMap
- The newMap
to use.- Returns:
- The old
Map
.
-
changeMap
public void changeMap(Map newMap)
Change the map in this game, fixing player destinations.- Parameters:
newMap
- The newMap
to use.
-
getNationOptions
public final NationOptions getNationOptions()
Get the current nation options.- Returns:
- The current
NationOptions
.
-
setNationOptions
public final void setNationOptions(NationOptions newNationOptions)
Set the current nation options. Public for the test suite.- Parameters:
newNationOptions
- The newNationOptions
value.
-
getVacantNation
public Nation getVacantNation()
Find an available (i.e. vacant) nation.- Returns:
- A vacant
Nation
or null if none found.
-
getVacantNations
public final java.util.List<Nation> getVacantNations()
Get the currently available nations.- Returns:
- A list of available
Nation
s.
-
canAddNewPlayer
public boolean canAddNewPlayer()
Can a new player be added to this game?- Returns:
- True if a new player can be added.
-
getTurn
public Turn getTurn()
Gets the current turn in this game.- Returns:
- The current
Turn
.
-
setTurn
public void setTurn(Turn newTurn)
Sets the current turn in this game.- Parameters:
newTurn
- The newTurn
to set.
-
getAge
public int getAge()
Get the age for the current turn.- Returns:
- The age (0-2).
-
getCombatModel
public final CombatModel getCombatModel()
Get the combat model in this game.- Returns:
- The
CombatModel
.
-
setCombatModel
public final void setCombatModel(CombatModel newCombatModel)
Set the game combat model.- Parameters:
newCombatModel
- The newCombatModel
value.
-
getSpanishSuccession
public final boolean getSpanishSuccession()
Has the Spanish Succession event occured?- Returns:
- True if the Spanish Succession has occurred.
-
setSpanishSuccession
public final void setSpanishSuccession(boolean spanishSuccession)
Set the Spanish Succession value.- Parameters:
spanishSuccession
- The new Spanish Succession value.
-
getInitialActiveUnitId
public java.lang.String getInitialActiveUnitId()
Get the identifier for the initial active unit.- Returns:
- The active unit identifier, if any.
-
getInitialActiveUnit
public Unit getInitialActiveUnit()
Get the initial active unit.- Returns:
- The initial active
Unit
or null if none.
-
setInitialActiveUnitId
public void setInitialActiveUnitId(java.lang.String initialActiveUnitId)
Set the identifier for the initial active unit.- Parameters:
initialActiveUnitId
- The identifier for the current active unit.
-
setFreeColGameObjectListener
public void setFreeColGameObjectListener(FreeColGameObjectListener fcgol)
Sets theFreeColGameObjectListener
attached to this game.- Parameters:
fcgol
- The newFreeColGameObjectListener
.
-
notifySetFreeColGameObject
public void notifySetFreeColGameObject(java.lang.String id, FreeColGameObject fcgo)
Notify a listener (if any) of a new game object.- Parameters:
id
- The object identifier.fcgo
- The newFreeColGameObject
.
-
notifyRemoveFreeColGameObject
public void notifyRemoveFreeColGameObject(java.lang.String id)
Notify a listener (if any) of that a game object has gone.- Parameters:
id
- The object identifier.
-
notifyOwnerChanged
public void notifyOwnerChanged(FreeColGameObject source, Player oldOwner, Player newOwner)
Notify a listener (if any) of that a game object has changed owner.- Parameters:
source
- TheFreeColGameObject
that changed owner.oldOwner
- The old owningPlayer
.newOwner
- The new owningPlayer
.
-
checkOwners
public void checkOwners(Ownable o, Player oldOwner)
Maintain the player containers for certain ownables. Mainly useful in the client, informing the player that it has gained or lost an ownable.- Parameters:
o
- TheOwnable
that may have changed.oldOwner
- The previous (possible unchanged) owningPlayer
.
-
allPlayersReadyToLaunch
public boolean allPlayersReadyToLaunch()
Checks if all players are ready to launch.- Returns:
- True if all players are ready to launch.
-
getAllColonies
public java.util.stream.Stream<Colony> getAllColonies(Player player)
Get all the colonies in the game.- Parameters:
player
- An optionalPlayer
to omit.- Returns:
- A stream of all the
Colony
s in the game.
-
getAllColoniesList
public java.util.List<Colony> getAllColoniesList(Player player)
Get a list of all the colonies in the game.- Parameters:
player
- An optionalPlayer
to omit.- Returns:
- A list of all the
Colony
s in the game.
-
getSettlementByName
public Settlement getSettlementByName(java.lang.String name)
Finds a settlement by name.- Parameters:
name
- The name of theSettlement
.- Returns:
- The
Settlement
found, ornull
if there is no knownSettlement
with the specified name (the settlement might not be visible to a client).
-
getMessageSource
public FreeColGameObject getMessageSource(ModelMessage message)
Helper function to get the source object of a message in this game.- Parameters:
message
- TheModelMessage
to find the object in.- Returns:
- The source object.
-
getMessageDisplay
public FreeColObject getMessageDisplay(ModelMessage message)
Helper function to get the object to display with a message in this game.- Parameters:
message
- TheModelMessage
to find the object in.- Returns:
- An object to display.
-
getStatistics
public java.util.Map<java.lang.String,java.lang.String> getStatistics()
Gets the statistics of this game.- Returns:
- A
Map
of the statistics.
-
getLocationClass
public static java.lang.Class<? extends FreeColGameObject> getLocationClass(java.lang.String id)
Get a location class from an identifier.- Parameters:
id
- The identifier to dissect.- Returns:
- The location class.
-
unserialize
public <T extends FreeColObject> T unserialize(java.lang.String xml, java.lang.Class<T> returnClass) throws javax.xml.stream.XMLStreamException
Unserialize from XML to a FreeColObject in this game.- Type Parameters:
T
- The actual return type.- Parameters:
xml
- The xml serialized version of an object.returnClass
- The expected object class.- Returns:
- The unserialized object.
- Throws:
javax.xml.stream.XMLStreamException
- if there are any problems reading from the stream.
-
checkIntegrity
public Constants.IntegrityType checkIntegrity(boolean fix, LogBuilder lb)
Checks the integrity of this game object.- Overrides:
checkIntegrity
in classFreeColGameObject
- Parameters:
fix
- If true, fix problems if possible.lb
- ALogBuilder
to log to.- Returns:
- A suitable
IntegrityType
.
-
getSpecification
public Specification getSpecification()
Get the specification.- Overrides:
getSpecification
in classFreeColGameObject
- Returns:
- The
Specification
used by this object.
-
setSpecification
public final void setSpecification(Specification specification)
Sets the specification for this object.- Overrides:
setSpecification
in classFreeColGameObject
- Parameters:
specification
- TheSpecification
to use.
-
copyIn
public <T extends FreeColObject> boolean copyIn(T other)
Copy another FreeColObject into this one if it is compatible.- Overrides:
copyIn
in classFreeColGameObject
- 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 classFreeColObject
- 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 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 classFreeColGameObject
- 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 classFreeColObject
- 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 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.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equals
in classFreeColGameObject
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classFreeColGameObject
-
-