Class GameState

java.lang.Object
VASSAL.build.module.GameState
All Implemented Interfaces:
CommandEncoder

public class GameState extends Object implements 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:
  • Field Details

  • Constructor Details

    • GameState

      public GameState()
  • Method Details

    • getAttachmentManager

      public AttachmentManager 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

      public void addTo(GameModule mod)
      Expects to be added to a GameModule. Adds New, Load, Close, and Save entries to the File 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

      public void addGameComponent(GameComponent theComponent)
      Add a GameComponent to the list of objects that will be notified when a game is started/ended
    • removeGameComponent

      public void removeGameComponent(GameComponent theComponent)
      Remove a GameComponent from the list of objects that will be notified when a game is started/ended
    • getGameComponents

      public Collection<GameComponent> getGameComponents()
      Returns:
      a Collection of all GameComponent objects that have been added to this GameState
    • addGameSetupStep

      public void addGameSetupStep(GameSetupStep step)
    • removeGameSetupStep

      public void removeGameSetupStep(GameSetupStep step)
      Remove a GameSetupStep
    • getUnfinishedSetupSteps

      public Iterator<GameSetupStep> getUnfinishedSetupSteps()
      Returns:
      an iterator of all GameSetupSteps 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

      public void freshenStartupGlobalKeyCommands(AbstractBuildable target)
      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. Invoke GameComponent.setup(boolean) on all registered GameComponent 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

      public boolean isSaveMetaDataValid(File file)
      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 to GameModule.decode(java.lang.String) and translated into a Command, which is then executed. The command read from the file should be that returned by getRestoreCommand(). 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 to GameModule.decode(java.lang.String) and translated into a Command, which is then executed. The command read from the file should be that returned by getRestoreCommand().
      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 to GameModule.decode(java.lang.String) and translated into a Command, which is then executed. The command read from the file should be that returned by getRestoreCommand().
      Parameters:
      continuation - if true then do the "old-style" version of load continuation
      forceForeground - if true then force load in foreground
    • loadGame

      public boolean loadGame(File f, boolean continuation)
      Loads a specific file as a saved game
      Parameters:
      f - File to load
      continuation - true if this is to be a continuation
      Returns:
      true if a file load was successfully started
    • loadGame

      public boolean loadGame(File f, boolean continuation, boolean forceForeground)
      Loads a specific file as a saved game
      Parameters:
      f - File to load
      continuation - true if this is to be a continuation
      Returns:
      true if a file load was successfully started
    • dropFile

      public void dropFile(DropTargetDropEvent dtde)
      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

      protected String saveString()
    • checkForOldSaveFile

      protected boolean checkForOldSaveFile(File f)
    • 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

      public void addPiece(GamePiece p)
      Add a GamePiece to the current game. The GameState keeps track of all GamePieces in the system, regardless of which Map they belong to (if any)
    • getPieceForId

      public GamePiece getPieceForId(String id)
      Returns:
      the GamePiece in the current game with the given id
    • removePiece

      public void removePiece(String id)
      Remove a GamePiece from the current game
    • removePiece

      public void removePiece(GamePiece piece)
    • getNewPieceId

      public String getNewPieceId()
      Returns:
      a String identifier guaranteed to be different from the id of all GamePieces in the game
      See Also:
    • loadContinuation

      public void loadContinuation(File f) throws IOException
      Throws:
      IOException
    • getPieces

      @Deprecated(since="2020-08-06", forRemoval=true) public Enumeration<GamePiece> getPieces()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Use getAllPieces() instead.
      Returns:
      an Enumeration of all GamePieces in the game
    • getAllPieces

      public Collection<GamePiece> getAllPieces()
      Returns:
      a Collection of all GamePieces in the game
    • getRestoreCommand

      public Command 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. Invokes GameComponent.getRestoreCommand() on each registered GameComponent, and then creates an AddPiece command for each game piece.
    • encode

      public String encode(Command c)
      A GameState recognizes instances of GameState.SetupCommand
      Specified by:
      encode in interface CommandEncoder
    • decode

      public Command decode(String theCommand)
      A GameState recognizes instances of GameState.SetupCommand
      Specified by:
      decode in interface CommandEncoder
    • saveGameRefresh

      public void saveGameRefresh(ZipArchive archive) throws IOException
      Throws:
      IOException
    • saveGame

      public void saveGame(File f) throws IOException
      Throws:
      IOException
    • loadGameInForeground

      public void loadGameInForeground(File f)
    • loadGameInForeground

      public void loadGameInForeground(String shortName, InputStream in) throws IOException
      Throws:
      IOException
    • loadGameInBackground

      public void loadGameInBackground(File f)
    • loadGameInBackground

      public void loadGameInBackground(String shortName, InputStream in)
    • loadGameInBackground

      public void loadGameInBackground(String shortName, InputStream in, boolean fromPredefinedSetup)
    • getRestorePiecesCommand

      public Command 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

      public Command decodeSavedGame(File saveFile) throws IOException
      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

      public Command decodeSavedGame(InputStream in) throws IOException
      Throws:
      IOException
    • getSavedGameDirectoryPreference

      public DirectoryConfigurer getSavedGameDirectoryPreference()
    • getEditorImageDirectoryPreference

      public DirectoryConfigurer getEditorImageDirectoryPreference()
    • getEditorSoundDirectoryPreference

      public DirectoryConfigurer getEditorSoundDirectoryPreference()