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
 
Fields inherited from class net.sf.freecol.common.model.FreeColObject
ID_ATTRIBUTE_TAG
 
Constructor Summary
Game(ModelController modelController, javax.xml.stream.XMLStreamReader in, java.lang.String viewOwnerUsername)
          Initiate a new Game object from an XML-representation.
 
Method Summary
 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 the have the same id.
 CombatModel getCombatModel()
          Get the CombatModel value.
 Player getCurrentPlayer()
          Gets the current player.
 java.util.List<Player> getEuropeanPlayers()
          Returns all the European players known by the player of this game.
 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.
 GameOptions getGameOptions()
          Gets the GameOptions that is associated with this Game.
 java.util.HashMap<java.lang.String,java.lang.Long> getGameStatistics()
          Computes how many objects of each class have been created, to track memory leaks over time
static int getInciteAmount(Player payingPlayer, Player targetPlayer, Player attackingPlayer)
          Gets the amount of gold needed for inciting.
 Map getMap()
          Gets the Map that is being used in this game.
 java.util.HashMap<java.lang.String,java.lang.Long> getMemoryStatistics()
           
 ModelController getModelController()
          Returns the Game's ModelController.
 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)
          Returns the first Settlement with the given name.
 boolean getSpanishSuccession()
          Get the SpanishSuccession value.
 Turn getTurn()
           
 Player getUnknownEnemy()
          Returns the "Unknown Enemy" Player, which is used for privateers.
 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.
 boolean isAllPlayersReadyToLaunch()
          Checks if all players are ready to launch.
 boolean isClientTrusted()
          Check if the clients are trusted or if the server should keep secrets in order to prevent cheating.
 boolean isNextPlayerInNewTurn()
          Checks if the next player is in a new turn.
 boolean playerNameInUse(java.lang.String username)
          Checks if the specified name is in use.
 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)
           
 void setUnknownEnemy(Player player)
          Sets the "Unknown Enemy" Player, which is used for privateers.
 
Methods inherited from class net.sf.freecol.common.model.FreeColGameObject
addToRemoveElement, dispose, equals, getFreeColGameObject, getFreeColGameObject, getGame, getIntegerID, hashCode, hasID, isDisposed, isUninitialized, readFromXML, setGame, setId, toSavedXML, toString, toXML, updateFreeColGameObject, updateID
 
Methods inherited from class net.sf.freecol.common.model.FreeColObject
addPropertyChangeListener, addPropertyChangeListener, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAttribute, getAttribute, getAttribute, getAttribute, getId, getPropertyChangeListeners, getPropertyChangeListeners, hasAbility, hasAttribute, hasListeners, readFromXMLElement, removePropertyChangeListener, removePropertyChangeListener, toXML, toXML, toXMLElement, toXMLElement, toXMLElement, toXMLElement, toXMLElementPartial, writeAttribute, writeFreeColGameObject
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Game

public Game(ModelController modelController,
            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:
modelController - A controller object the model can use to make actions not allowed from the model (generate random numbers etc).
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

getModelController

public ModelController getModelController()
Returns the Game's ModelController.

Returns:
a ModelController value

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

isClientTrusted

public boolean isClientTrusted()
Check if the clients are trusted or if the server should keep secrets in order to prevent cheating.

Returns:
true if clients are to be trusted.

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)
Returns the first Settlement with the given 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()

setTurn

public void setTurn(Turn newTurn)

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.

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()

isNextPlayerInNewTurn

public boolean isNextPlayerInNewTurn()
Checks if the next player is in a new turn.

Returns:
true if changing to the Player given by getNextPlayer() would increase the current number of turns by one.

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()

getEuropeanPlayers

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

Returns:
All the 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.

getGameOptions

public GameOptions getGameOptions()
Gets the GameOptions that is associated with this Game.

Overrides:
getGameOptions in class FreeColGameObject
Returns:
The same GameOptions-object as returned by getGame().getGameOptions().

getInciteAmount

public static int getInciteAmount(Player payingPlayer,
                                  Player targetPlayer,
                                  Player attackingPlayer)
Gets the amount of gold needed for inciting. This method should NEVER be randomized: it should always return the same amount if given the same three parameters.

Parameters:
payingPlayer - The Player paying for the incite.
targetPlayer - The Player to be attacked by the attackingPlayer.
attackingPlayer - The player that would be receiving the money for incite.
Returns:
The amount of gold that should be payed by payingPlayer to attackingPlayer in order for attackingPlayer to attack targetPlayer.

getXMLElementTagName

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

Returns:
the tag name.

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 the 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.

getGameStatistics

public java.util.HashMap<java.lang.String,java.lang.Long> getGameStatistics()
Computes how many objects of each class have been created, to track memory leaks over time


getMemoryStatistics

public java.util.HashMap<java.lang.String,java.lang.Long> getMemoryStatistics()