net.sf.freecol.common.model
Class Game

java.lang.Object
  extended by net.sf.freecol.common.model.FreeColObject
      extended by net.sf.freecol.common.model.FreeColGameObject
          extended by net.sf.freecol.common.model.Game
Direct Known Subclasses:
ServerGame

public class Game
extends FreeColGameObject

The main component of the game model.

If an object of this class returns a non-null result to getViewOwner(), then this object just represents a view of the game from a single player's perspective. In that case, some information might be missing from the model.


Field Summary
static java.lang.String CIBOLA_TAG
           
private  java.util.List<java.lang.String> citiesOfCibola
          The cities of Cibola remaining in this game.
protected  CombatModel combatModel
          The combat model this game uses.
protected  Player currentPlayer
          The name of the player whose turn it is.
protected  FreeColGameObjectListener freeColGameObjectListener
           
protected  java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<FreeColGameObject>> freeColGameObjects
          Contains references to all objects created in this game.
private static java.util.logging.Logger logger
           
private  Map map
           
private  NationOptions nationOptions
          Describe nationOptions here.
protected  int nextId
          The next available ID, that can be given to a new FreeColGameObject.
protected  java.util.List<Player> players
          Contains all the players in the game.
private  boolean spanishSuccession
          Whether the War of Spanish Succession has already taken place.
private  Specification specification
          The Specification this game uses.
private  Turn turn
           
private  Player unknownEnemy
          A virtual player to use with enemy privateers
private  java.util.UUID uuid
          Game UUID, persistent in savegame files
protected  Player viewOwner
          The owner of this view of the game, or null if this game has all the information.
 
Fields inherited from class net.sf.freecol.common.model.FreeColGameObject
UNITS_TAG_NAME
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ARRAY_SIZE, ID_ATTRIBUTE, ID_ATTRIBUTE_TAG, INFINITY, NO_ID, PARTIAL_ATTRIBUTE, UNDEFINED, VALUE_TAG
 
Constructor Summary
protected Game(Game game, javax.xml.stream.XMLStreamReader in)
          Minimal constructor, Just necessary to call parent constructor
protected Game(Specification specification)
          This constructor is used by the Server to create a new Game with the given Specification.
  Game(javax.xml.stream.XMLStreamReader in, java.lang.String viewOwnerUsername)
          Initiate a new Game object from an XML-representation.
 
Method Summary
private  void addOldOptions(OptionGroup group)
           
 void addPlayer(Player player)
          Adds the specified player to the game.
 boolean canAddNewPlayer()
          Checks if a new Player can be added.
 boolean checkIntegrity()
          Checks the integrity of this Gameuninitialized objects.
 boolean equals(java.lang.Object o)
          Need to overwrite behavior of equals inherited from FreeColGameObject, since two games are not the same if they have the same id.
private  boolean fixIntegrity(java.util.List<java.lang.String> list)
          Try to fix integrity problems
 java.lang.String getCityOfCibola()
          Get the next name for a city of Cibola.
 CombatModel getCombatModel()
          Get the CombatModel value.
 Player getCurrentPlayer()
          Gets the current player.
 OptionGroup getDifficultyLevel()
          Get the OptionGroup value.
 Player getFirstPlayer()
          Gets the first player in this game.
 FreeColGameObject getFreeColGameObject(java.lang.String id)
          Gets the FreeColGameObject with the specified ID.
 java.util.Iterator<FreeColGameObject> getFreeColGameObjectIterator()
          Gets an Iterator of every registered FreeColGameObject.
 FreeColGameObjectListener getFreeColGameObjectListener()
           
 FreeColGameObject getFreeColGameObjectSafely(java.lang.String id)
          Get the FreeColGameObject with the given id or null.
 java.util.List<Player> getLiveEuropeanPlayers()
          Returns all the live European players known by the player of this game.
 Map getMap()
          Gets the Map that is being used in this game.
 OptionGroup getMapGeneratorOptions()
          Gets the MapGeneratorOptions that is 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 NationOptions value.
 Player getNextPlayer()
          Gets the next current player.
 int getNumberOfPlayers()
           
 Player getPlayer(java.lang.String nationID)
          Return a Player identified by it's nation.
 Player getPlayerAfter(Player beforePlayer)
          Gets the player after the given player.
 Player getPlayerByName(java.lang.String name)
          Gets a Player specified by a name.
 java.util.Iterator<Player> getPlayerIterator()
          Gets an Iterator of every Player in this game.
 java.util.List<Player> getPlayers()
          Gets an Vector containing every Player in this game.
 Settlement getSettlement(java.lang.String name)
          Finds a settlement by name.
 boolean getSpanishSuccession()
          Get the SpanishSuccession value.
 Specification getSpecification()
          Return the specification for this Game.
 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()
          Returns the "Unknown Enemy" Player, which is used for privateers.
 java.util.UUID getUUID()
          Returns the unique identifier for this game.
 Nation getVacantNation()
          Returns a vacant nation.
 java.util.List<Nation> getVacantNations()
          Get the VacantNations value.
 Player getViewOwner()
          Returns the owner of this view of the game, or null if this game has all the information.
static java.lang.String getXMLElementTagName()
          Returns the tag name of the root element representing this object.
private  void initializeCitiesOfCibola()
          Initialize the list of cities of Cibola.
 boolean isAllPlayersReadyToLaunch()
          Checks if all players are ready to launch.
 boolean playerNameInUse(java.lang.String username)
          Checks if the specified name is in use.
protected  void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
          Initialize this object from an XML-representation of this object.
protected  void readFromXMLPartialImpl(javax.xml.stream.XMLStreamReader in)
          Partial reader, so that simple updates can be brief.
 FreeColGameObject removeFreeColGameObject(java.lang.String id)
          Removes the FreeColGameObject with the specified ID.
 void removePlayer(Player player)
          Removes the specified player from the game.
 void setCombatModel(CombatModel newCombatModel)
          Set the CombatModel value.
 void setCurrentPlayer(Player newCp)
          Sets the current player.
 void setFreeColGameObject(java.lang.String id, FreeColGameObject freeColGameObject)
          Registers a new FreeColGameObject with the specified ID.
 void setFreeColGameObjectListener(FreeColGameObjectListener freeColGameObjectListener)
           
 void setMap(Map map)
          Sets the Map that is going to be used in this game.
 void setNationOptions(NationOptions newNationOptions)
          Set the NationOptions value.
 void setSpanishSuccession(boolean newSpanishSuccession)
          Set the SpanishSuccession value.
 void setTurn(Turn newTurn)
          Sets the current turn in this game.
 void setUnknownEnemy(Player player)
          Sets the "Unknown Enemy" Player, which is used for privateers.
protected  void toXMLImpl(javax.xml.stream.XMLStreamWriter out, Player player, boolean showAll, boolean toSavedGame)
          This method writes an XML-representation of this object to the given stream.
protected  void toXMLPartialImpl(javax.xml.stream.XMLStreamWriter out, java.lang.String[] fields)
          Partial writer, so that simple updates can be brief.
 
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
dispose, disposeList, equals, fundamentalDispose, getFreeColGameObject, getFreeColGameObject, getGame, getIntegerID, hashCode, isDisposed, isUninitialized, newLocation, readFromXML, readFromXMLPartialByClass, setDefaultId, setGame, setId, toString, toXML, toXMLImpl, toXMLPartialByClass, updateFreeColGameObject
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, dumpObject, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readAttributes, readAttributes, readChild, readChild, readChildren, readChildren, readFromArrayElement, readFromArrayElement, readFromListElement, readFromXMLElement, readFromXMLImpl, removePropertyChangeListener, removePropertyChangeListener, save, save, setSpecification, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, writeAttribute, writeAttributes, writeChildren
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

CIBOLA_TAG

public static final java.lang.String CIBOLA_TAG
See Also:
Constant Field Values

logger

private static final java.util.logging.Logger logger

uuid

private java.util.UUID uuid
Game UUID, persistent in savegame files


unknownEnemy

private Player unknownEnemy
A virtual player to use with enemy privateers


players

protected java.util.List<Player> players
Contains all the players in the game.


map

private Map map

currentPlayer

protected Player currentPlayer
The name of the player whose turn it is.


viewOwner

protected Player viewOwner
The owner of this view of the game, or null if this game has all the information.


freeColGameObjects

protected java.util.HashMap<java.lang.String,java.lang.ref.WeakReference<FreeColGameObject>> freeColGameObjects
Contains references to all objects created in this game.


nextId

protected int nextId
The next available ID, that can be given to a new FreeColGameObject.


turn

private Turn turn

nationOptions

private NationOptions nationOptions
Describe nationOptions here.


spanishSuccession

private boolean spanishSuccession
Whether the War of Spanish Succession has already taken place.


freeColGameObjectListener

protected FreeColGameObjectListener freeColGameObjectListener

citiesOfCibola

private java.util.List<java.lang.String> citiesOfCibola
The cities of Cibola remaining in this game.


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.


specification

private Specification specification
The Specification this game uses.

Constructor Detail

Game

protected Game(Specification specification)
This constructor is used by the Server to create a new Game with the given Specification.

Parameters:
specification -

Game

protected Game(Game game,
               javax.xml.stream.XMLStreamReader in)
        throws javax.xml.stream.XMLStreamException
Minimal constructor, Just necessary to call parent constructor

Parameters:
game - Game
in - XMLStreamReader
Throws:
javax.xml.stream.XMLStreamException

Game

public Game(javax.xml.stream.XMLStreamReader in,
            java.lang.String viewOwnerUsername)
     throws javax.xml.stream.XMLStreamException
Initiate a new Game object from an XML-representation.

Note that this is used on the client side; the game is really a partial view of the server-side game.

Parameters:
in - The XML stream to read the data from.
viewOwnerUsername - The username of the owner of this view of the game.
Throws:
javax.xml.stream.XMLStreamException - if an error occured during parsing.
See Also:
ConnectController.login(String, String, int)
Method Detail

getUUID

public java.util.UUID getUUID()
Returns the unique identifier for this game. (A game UUID persists in save game files.)

Returns:
java.util.UUID

getUnknownEnemy

public Player getUnknownEnemy()
Returns the "Unknown Enemy" Player, which is used for privateers.

Returns:
a Player value

setUnknownEnemy

public void setUnknownEnemy(Player player)
Sets the "Unknown Enemy" Player, which is used for privateers.

Parameters:
player - a Player value

getVacantNations

public final java.util.List<Nation> getVacantNations()
Get the VacantNations value.

Returns:
a List value

getViewOwner

public Player getViewOwner()
Returns the owner of this view of the game, or null if this game has all the information.

If this value is null, then it means that this Game object has access to all information (ie is the server model).

Returns:
The Player using this Game-object as a view.

getSettlement

public Settlement getSettlement(java.lang.String name)
Finds a settlement by name.

Parameters:
name - The name of the Settlement.
Returns:
The Settlement or null if there is no known Settlement with the specified name (the settlement might not be visible to a client).

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 new Turn to set.

getCombatModel

public final CombatModel getCombatModel()
Get the CombatModel value.

Returns:
a CombatModel value

setCombatModel

public final void setCombatModel(CombatModel newCombatModel)
Set the CombatModel value.

Parameters:
newCombatModel - The new CombatModel value.

getDifficultyLevel

public final OptionGroup getDifficultyLevel()
Get the OptionGroup value.

Returns:
a OptionGroup value

addPlayer

public void addPlayer(Player player)
Adds the specified player to the game.

Parameters:
player - The Player that shall be added to this Game.

removePlayer

public void removePlayer(Player player)
Removes the specified player from the game.

Parameters:
player - The Player that shall be removed from this Game.

setFreeColGameObject

public void setFreeColGameObject(java.lang.String id,
                                 FreeColGameObject freeColGameObject)
Registers a new FreeColGameObject with the specified ID.

Parameters:
id - The unique ID of the FreeColGameObject.
freeColGameObject - The FreeColGameObject that shall be added to this Game.
Throws:
java.lang.IllegalArgumentException - If either id or freeColGameObject are null.

setFreeColGameObjectListener

public void setFreeColGameObjectListener(FreeColGameObjectListener freeColGameObjectListener)

getFreeColGameObjectListener

public FreeColGameObjectListener getFreeColGameObjectListener()

getFreeColGameObject

public FreeColGameObject getFreeColGameObject(java.lang.String id)
Gets the FreeColGameObject with the specified ID.

Parameters:
id - The identifier of the FreeColGameObject.
Returns:
The FreeColGameObject.
Throws:
java.lang.IllegalArgumentException - If id == null, or id = "".

getFreeColGameObjectSafely

public FreeColGameObject getFreeColGameObjectSafely(java.lang.String id)
Get the FreeColGameObject with the given id or null. This method does NOT throw if the id is invalid.

Parameters:
id - The id, may be null or invalid.
Returns:
game object with id or null.

removeFreeColGameObject

public FreeColGameObject removeFreeColGameObject(java.lang.String id)
Removes the FreeColGameObject with the specified ID.

Parameters:
id - The identifier of the FreeColGameObject that shall be removed from this Game.
Returns:
The FreeColGameObject that has been removed.
Throws:
java.lang.IllegalArgumentException - If id == null, or id = "".

getMap

public Map getMap()
Gets the Map that is being used in this game.

Returns:
The Map that is being used in this game or null if no Map has been created.

setMap

public void setMap(Map map)
Sets the Map that is going to be used in this game.

Parameters:
map - The Map that is going to be used in this game.

getNationOptions

public final NationOptions getNationOptions()
Get the NationOptions value.

Returns:
a NationOptions value

setNationOptions

public final void setNationOptions(NationOptions newNationOptions)
Set the NationOptions value.

Parameters:
newNationOptions - The new NationOptions value.

getVacantNation

public Nation getVacantNation()
Returns a vacant nation.

Returns:
A vacant nation.

getPlayer

public Player getPlayer(java.lang.String nationID)
Return a Player identified by it's nation.

Parameters:
nationID - The nation.
Returns:
The Player of the given nation.

setCurrentPlayer

public void setCurrentPlayer(Player newCp)
Sets the current player.

Parameters:
newCp - The new current player.

getCurrentPlayer

public Player getCurrentPlayer()
Gets the current player. This is the Player currently playing the Game.

Returns:
The current player.

getNextPlayer

public Player getNextPlayer()
Gets the next current player.

Returns:
The player that will start its turn as soon as the current player is ready.
See Also:
getCurrentPlayer()

getPlayerAfter

public Player getPlayerAfter(Player beforePlayer)
Gets the player after the given player.

Parameters:
beforePlayer - The Player before the Player to be returned.
Returns:
The Player after the beforePlayer in the list which determines the order each player becomes the current player.
See Also:
getNextPlayer()

getFirstPlayer

public Player getFirstPlayer()
Gets the first player in this game.

Returns:
the Player that was first added to this Game.

getFreeColGameObjectIterator

public java.util.Iterator<FreeColGameObject> getFreeColGameObjectIterator()
Gets an Iterator of every registered FreeColGameObject. This Iterator should be iterated at least once in a while since it cleans the FreeColGameObject cache.

Returns:
an Iterator containing every registered FreeColGameObject.
See Also:
setFreeColGameObject(java.lang.String, net.sf.freecol.common.model.FreeColGameObject)

getPlayerByName

public Player getPlayerByName(java.lang.String name)
Gets a Player specified by a name.

Parameters:
name - The name identifying the Player.
Returns:
The Player.

playerNameInUse

public boolean playerNameInUse(java.lang.String username)
Checks if the specified name is in use.

Parameters:
username - The name.
Returns:
true if the name is already in use and false otherwise.

getPlayerIterator

public java.util.Iterator<Player> getPlayerIterator()
Gets an Iterator of every Player in this game.

Returns:
The Iterator.

getPlayers

public java.util.List<Player> getPlayers()
Gets an Vector containing every Player in this game.

Returns:
The Vector.

getNumberOfPlayers

public int getNumberOfPlayers()

getLiveEuropeanPlayers

public java.util.List<Player> getLiveEuropeanPlayers()
Returns all the live European players known by the player of this game.

Returns:
All the live European players known by the player of this game.

canAddNewPlayer

public boolean canAddNewPlayer()
Checks if a new Player can be added.

Returns:
true if a new player can be added and false otherwise.

isAllPlayersReadyToLaunch

public boolean isAllPlayersReadyToLaunch()
Checks if all players are ready to launch.

Returns:
true if all players are ready to launch and false otherwise.

getSpanishSuccession

public final boolean getSpanishSuccession()
Get the SpanishSuccession value.

Returns:
a boolean value

setSpanishSuccession

public final void setSpanishSuccession(boolean newSpanishSuccession)
Set the SpanishSuccession value.

Parameters:
newSpanishSuccession - The new SpanishSuccession value.

checkIntegrity

public boolean checkIntegrity()
Checks the integrity of this Gameuninitialized objects. Detected problems gets written to the log.

Returns:
true if the Game has been loaded properly.

fixIntegrity

private boolean fixIntegrity(java.util.List<java.lang.String> list)
Try to fix integrity problems


getMapGeneratorOptions

public OptionGroup getMapGeneratorOptions()
Gets the MapGeneratorOptions that is associated with this Game.

Returns:
OptionGroup

initializeCitiesOfCibola

private void initializeCitiesOfCibola()
Initialize the list of cities of Cibola. Pull them out of the message file and randomize the order.


getCityOfCibola

public java.lang.String getCityOfCibola()
Get the next name for a city of Cibola.

Returns:
The next name for a city of Cibola, or null if none available.

getMessageSource

public FreeColGameObject getMessageSource(ModelMessage message)
Helper function to get the source object of a message in this game.

Parameters:
message - The ModelMessage 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 - The ModelMessage to find the object in.
Returns:
An object to display.

getSpecification

public Specification getSpecification()
Return the specification for this Game.

Overrides:
getSpecification in class FreeColGameObject
Returns:
a Specification value

equals

public boolean equals(java.lang.Object o)
Need to overwrite behavior of equals inherited from FreeColGameObject, since two games are not the same if they have the same id.

Overrides:
equals in class FreeColGameObject
Parameters:
o - The FreeColGameObject to compare against this object.
Returns:
true if the two FreeColGameObject are equal and false otherwise.

getStatistics

public java.util.Map<java.lang.String,java.lang.String> getStatistics()
Gets the statistics of this game.

Returns:
A Map of the statistics.

toXMLImpl

protected void toXMLImpl(javax.xml.stream.XMLStreamWriter out,
                         Player player,
                         boolean showAll,
                         boolean toSavedGame)
                  throws javax.xml.stream.XMLStreamException
This method writes an XML-representation of this object to the given stream.

Only attributes visible to the given Player will be added to that representation if showAll is set to false.

Specified by:
toXMLImpl in class FreeColGameObject
Parameters:
out - The target stream.
player - The Player this XML-representation should be made for, or null if showAll == true.
showAll - Only attributes visible to player will be added to the representation if showAll is set to false.
toSavedGame - If true then information that is only needed when saving a game is added.
Throws:
javax.xml.stream.XMLStreamException - if there are any problems writing to the stream.

readFromXMLImpl

protected void readFromXMLImpl(javax.xml.stream.XMLStreamReader in)
                        throws javax.xml.stream.XMLStreamException
Initialize this object from an XML-representation of this object.

Overrides:
readFromXMLImpl in class FreeColObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - if a problem was encountered during parsing.

addOldOptions

private void addOldOptions(OptionGroup group)

toXMLPartialImpl

protected void toXMLPartialImpl(javax.xml.stream.XMLStreamWriter out,
                                java.lang.String[] fields)
                         throws javax.xml.stream.XMLStreamException
Partial writer, so that simple updates can be brief.

Overrides:
toXMLPartialImpl in class FreeColObject
Parameters:
out - The target stream.
fields - The fields to write.
Throws:
javax.xml.stream.XMLStreamException - If there are problems writing the stream.

readFromXMLPartialImpl

protected void readFromXMLPartialImpl(javax.xml.stream.XMLStreamReader in)
                               throws javax.xml.stream.XMLStreamException
Partial reader, so that simple updates can be brief.

Overrides:
readFromXMLPartialImpl in class FreeColObject
Parameters:
in - The input stream with the XML.
Throws:
javax.xml.stream.XMLStreamException - If there are problems reading the stream.

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
Returns the tag name of the root element representing this object.

Returns:
"game".