Package VASSAL.build.module
Class GameState
java.lang.Object
VASSAL.build.module.GameState
- All Implemented Interfaces:
CommandEncoder
The GameState contains methods to track and read/write the complete enumerated game state of the game
currently being played. Its main methods deal with saving/loading games and starting/ending games: see
saveGameAs()
, loadGame()
, setup(boolean)
. In each case, appropriate calls
are made to designated methods in all relevant GameComponents. Can also be queried if a game is in progress
isGameStarted()
and if the game state has been modified since the last save isModified()
.
Only one game can be open at once in a single Player, or in a single Editor/Player pair.- See Also:
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
protected Action
protected DirectoryConfigurer
protected DirectoryConfigurer
static final String
protected List
<GameComponent> protected String
protected File
protected Action
protected Action
protected String
protected Action
protected Action
protected Action
protected boolean
protected Action
static final int
protected DirectoryConfigurer
static final String
protected Action
protected Action
protected List
<GameSetupStep> -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addGameComponent
(GameComponent theComponent) Add aGameComponent
to the list of objects that will be notified when a game is started/endedvoid
Add aGameSetupStep
void
Add aGamePiece
to the current game.void
addTo
(GameModule mod) Expects to be added to a GameModule.protected boolean
void
Closes the game.A GameState recognizes instances ofGameState.SetupCommand
decodeSavedGame
(File saveFile) Read a saved game and translate it into a Command.void
doStartupGlobalKeyCommands
(boolean playerChange) Applies all of the Startup Global Key Commands in order, and then blocks undo past this point if any were applied.void
dropFile
(DropTargetDropEvent dtde) Accepts a saved file dropped onto the main window or a map, attempts to load it as a saved game.A GameState recognizes instances ofGameState.SetupCommand
void
When we're known to be starting a fresh game from a Predefined Setup, freshen the starting-a-new-game flag for all SGKCsgetPieceForId
(String id) Deprecated, for removal: This API element is subject to removal in a future version.boolean
boolean
Return true if a game is currently in progressboolean
boolean
boolean
Return true if starting new games is currently disabled New games are disabled if you are in a multi-player room and are not the ownerboolean
boolean
isSaveMetaDataValid
(File file) boolean
void
void
loadGame()
Read the game from a savefile.void
loadGame
(boolean continuation) Read the game from a savefile.void
loadGame
(boolean continuation, boolean forceForeground) Read the game from a savefile.boolean
Loads a specific file as a saved gameboolean
Loads a specific file as a saved gamevoid
void
loadGameInBackground
(String shortName, InputStream in) void
loadGameInBackground
(String shortName, InputStream in, boolean fromPredefinedSetup) void
void
loadGameInForeground
(String shortName, InputStream in) int
Offers player the chance to save the game if an unsaved one is active and modifiedvoid
removeGameComponent
(GameComponent theComponent) Remove aGameComponent
from the list of objects that will be notified when a game is started/endedvoid
Remove aGameSetupStep
void
removePiece
(String id) Remove aGamePiece
from the current gamevoid
removePiece
(GamePiece piece) void
saveGame()
Saves the game to an existing file, or prompts for a new one.void
void
Prompts the user for a file into which to save the gamevoid
saveGameRefresh
(ZipArchive archive) protected String
void
setModified
(boolean modified) void
setup
(boolean gameStarting) Start/end a game.void
setup
(boolean gameStarting, boolean gameUpdating) Start a game for updating (via editor).void
void
Indicated game update is completed and game is saved.
-
Field Details
-
pieces
-
gameComponents
-
setupSteps
-
loadGame
-
loadGameOld
-
saveGame
-
saveGameAs
-
newGame
-
closeGame
-
loadContinuation
-
loadAndFastForward
-
loadAndAppend
-
lastSave
-
lastSaveFile
-
savedGameDirectoryPreference
-
editorImageDirectoryPreference
-
editorSoundDirectoryPreference
-
loadComments
-
loadingInBackground
protected boolean loadingInBackground -
NO_NEED_TO_SAVE
public static final int NO_NEED_TO_SAVE- See Also:
-
SAVEFILE_ZIP_ENTRY
- See Also:
-
BEGIN_SAVE
- See Also:
-
END_SAVE
- See Also:
-
-
Constructor Details
-
GameState
public GameState()
-
-
Method Details
-
getAttachmentManager
-
isLoadingInBackground
public boolean isLoadingInBackground()- Returns:
- true if currently loading in background
-
isFastForwarding
public boolean isFastForwarding()- Returns:
- true if currently fast-forwarding a log
-
addTo
Expects to be added to a GameModule. AddsNew
,Load
,Close
, andSave
entries to theFile
menu of the controls window -
isModified
public boolean isModified()- Returns:
- true if the game state is different from when it was last saved
-
isSaveEnabled
public boolean isSaveEnabled()- Returns:
- true if saveGame action is enabled (mainly to detect if logging can start)
-
addGameComponent
Add aGameComponent
to the list of objects that will be notified when a game is started/ended -
removeGameComponent
Remove aGameComponent
from the list of objects that will be notified when a game is started/ended -
getGameComponents
- Returns:
- a Collection of all
GameComponent
objects that have been added to this GameState
-
addGameSetupStep
Add aGameSetupStep
-
removeGameSetupStep
Remove aGameSetupStep
-
getUnfinishedSetupSteps
- Returns:
- an iterator of all
GameSetupStep
s that are not yet finished
-
setupRefresh
public void setupRefresh() -
setup
public void setup(boolean gameStarting, boolean gameUpdating) Start a game for updating (via editor). NOTE: This method is not for use in custom code. -
updateDone
public void updateDone()Indicated game update is completed and game is saved. -
isUpdating
public boolean isUpdating() -
freshenStartupGlobalKeyCommands
When we're known to be starting a fresh game from a Predefined Setup, freshen the starting-a-new-game flag for all SGKCs- Parameters:
target
- the Game Module
-
doStartupGlobalKeyCommands
public void doStartupGlobalKeyCommands(boolean playerChange) Applies all of the Startup Global Key Commands in order, and then blocks undo past this point if any were applied. -
maybeSaveGame
public int maybeSaveGame()Offers player the chance to save the game if an unsaved one is active and modified- Returns:
- Whether Yes, No, or Cancel was selected (if Yes was selected, game is saved before returning result). Or NO_NEED_TO_SAVE if game wasn't in a state needing to be saved.
-
setup
public void setup(boolean gameStarting) Start/end a game. Prompt to save if the game state has been modified since last save. InvokeGameComponent.setup(boolean)
on all registeredGameComponent
objects. -
isGameStarted
public boolean isGameStarted()Return true if a game is currently in progress -
isNewGameAllowed
public boolean isNewGameAllowed()Return true if starting new games is currently disabled New games are disabled if you are in a multi-player room and are not the owner -
isSaveMetaDataValid
- Parameters:
file
- to validate as a legitimate save file- Returns:
- true if metadata is valid (or explicitly cleared-for-crash by user) and clear to proceed
-
loadGame
public void loadGame()Read the game from a savefile. The contents of the file is sent toGameModule.decode(java.lang.String)
and translated into aCommand
, which is then executed. The command read from the file should be that returned bygetRestoreCommand()
. This version for binary compatibility w/ old style. -
loadGame
public void loadGame(boolean continuation) Read the game from a savefile. The contents of the file is sent toGameModule.decode(java.lang.String)
and translated into aCommand
, which is then executed. The command read from the file should be that returned bygetRestoreCommand()
.- Parameters:
continuation
- if true then do the "old-style" version of load continuation
-
loadGame
public void loadGame(boolean continuation, boolean forceForeground) Read the game from a savefile. The contents of the file is sent toGameModule.decode(java.lang.String)
and translated into aCommand
, which is then executed. The command read from the file should be that returned bygetRestoreCommand()
.- Parameters:
continuation
- if true then do the "old-style" version of load continuationforceForeground
- if true then force load in foreground
-
loadGame
Loads a specific file as a saved game- Parameters:
f
- File to loadcontinuation
- true if this is to be a continuation- Returns:
- true if a file load was successfully started
-
loadGame
Loads a specific file as a saved game- Parameters:
f
- File to loadcontinuation
- true if this is to be a continuation- Returns:
- true if a file load was successfully started
-
dropFile
Accepts a saved file dropped onto the main window or a map, attempts to load it as a saved game.- Parameters:
dtde
- DropTargetDropEvent from the drop() handler
-
saveString
-
checkForOldSaveFile
-
closeGame
public void closeGame()Closes the game. -
saveGame
public void saveGame()Saves the game to an existing file, or prompts for a new one. -
saveGameAs
public void saveGameAs()Prompts the user for a file into which to save the game -
setModified
public void setModified(boolean modified) -
addPiece
-
getPieceForId
-
removePiece
-
removePiece
-
getNewPieceId
- Returns:
- a String identifier guaranteed to be different from the id of all GamePieces in the game
- See Also:
-
loadContinuation
- Throws:
IOException
-
getPieces
Deprecated, for removal: This API element is subject to removal in a future version.UsegetAllPieces()
instead.- Returns:
- an Enumeration of all
GamePiece
s in the game
-
getAllPieces
- Returns:
- a Collection of all
GamePiece
s in the game
-
getRestoreCommand
- Returns:
- a
Command
that, when executed, will restore the game to its current state -- this command can then be written to a save game file. InvokesGameComponent.getRestoreCommand()
on each registeredGameComponent
, and then creates an AddPiece command for each game piece.
-
encode
A GameState recognizes instances ofGameState.SetupCommand
- Specified by:
encode
in interfaceCommandEncoder
-
decode
A GameState recognizes instances ofGameState.SetupCommand
- Specified by:
decode
in interfaceCommandEncoder
-
saveGameRefresh
- Throws:
IOException
-
saveGame
- Throws:
IOException
-
loadGameInForeground
-
loadGameInForeground
- Throws:
IOException
-
loadGameInBackground
-
loadGameInBackground
-
loadGameInBackground
-
getRestorePiecesCommand
- Returns:
- a Command that, when executed, will add all pieces currently in the game. Used when saving a game. Pieces are grouped by map, and within a map by visual layer.
-
decodeSavedGame
Read a saved game and translate it into a Command. Executing the command will load the saved game.- Parameters:
saveFile
- Save file name- Returns:
- Command
- Throws:
IOException
- I/O Exception
-
decodeSavedGame
- Throws:
IOException
-
getSavedGameDirectoryPreference
-
getEditorImageDirectoryPreference
-
getEditorSoundDirectoryPreference
-
getAllPieces()
instead.