Alchemy

save.c

Save files and the save file menu.

games/tbs/src/window/battle_command/save.c on GitHub

Save files and the save file menu. Part of battle_command.c, the menu object, compiled only through it.

Functions

Text_FormatPlayTime

void *Text_FormatPlayTime(s32 frames, u8 *out)

Writes the play time frames (at 60 frames a second) into out as hours and minutes, "hhh:mm" with the hours right-aligned in three characters, and returns out. The time stops at 999:59.

Used by SaveMenu_DrawFileSummary.

Line 26

SaveState_FindFreeSummarySlot

u32 SaveState_FindFreeSummarySlot(void)

Returns the first save file whose summary shows it empty (level 0), or SAVE_NO_FREE_FILE when all three are in use. The summaries must be loaded.

Used by SaveState_CopySlotPair.

Line 65

SaveState_CountRecordsExcludingFlagged

s32 SaveState_CountRecordsExcludingFlagged(s32 excludeSent)

Returns how many save files there are, leaving out those whose summary has its send flag set (the files saved at the end of the game) when excludeSent is nonzero, or SAVE_ERROR_NO_MEMORY when the backup memory cannot be read. It opens and releases the save workspace itself.

Line 81

SaveState_ScanRecordFlags

s32 SaveState_ScanRecordFlags(void)

Loads the save file summaries for the title menu and returns how many files there are: plus 100 when every file has its send flag set, or SAVE_ERROR_NO_MEMORY when the backup memory cannot be read. Sets gTitleExtraOptionEnabled, which offers Battle, when a file has any of game flags 48-127 set, and gTitleSendOptionEnabled, which offers Send, when a file has its send flag set and R and Left are held. Menu_SelectSaveSlotAction reads all three.

Used by Menu_SelectSaveSlotAction, SaveMenu_Run, Title_Run.

Line 113

SaveState_BuildSummaryHeader

u32 SaveState_BuildSummaryHeader(void)

Prepares the save buffer for writing: stamps the game state with the time and options, then fills the file summary shown on the file menus (the leader's name, level and class, the play time, area, coins, Djinn of each element, the party, the send flag and how many of game flags 48-127 are set) and its checksum, the sum of the 968 words from gGameFlagBytes on. Returns the checksum.

Used by SaveState_WriteCurrentSlotPair, Save_WriteSelectedSlot.

Line 160

SaveState_WriteCurrentSlotPair

s16 SaveState_WriteCurrentSlotPair(void)

Saves the game to the current file (gSaveSlot): builds the summary and writes both halves of the save buffer, as records gSaveSlot and gSaveSlot + SAVE_FILE_COUNT. Returns 0, gSaveSlot itself (-1) when no file is chosen, or a SAVE_ERROR_* after showing its message. Unlike Save_WriteSelectedSlot it asks nothing and does not capture the object table first (SaveState_CaptureObjectTableFar).

Line 256

SaveState_WriteSlotPair

s32 SaveState_WriteSlotPair(s32 file)

Writes both halves of the save buffer, as they are, to save file file. Returns 0, or a SAVE_ERROR_* after showing its message.

Line 292

SaveState_ProcessSelectedSlot

s32 SaveState_ProcessSelectedSlot(void)

Writes the 64-byte save stamp (gSaveStamp, which lies in the first half of the save buffer) into the current file's first record, leaving the rest of the record as it was saved. Returns 0, -1 when no file is chosen, or a SAVE_ERROR_* after showing its message. The link lobby saves its battle results this way (LinkLobby_SaveBattleResults). The record's 4 KB buffer is taken from the alternate pool and freed before it returns, except when no file is chosen.

Used by SaveState_ConfirmAndProcessSelectedSlot.

Line 322

SaveState_ConfirmAndProcessSelectedSlot

UNUSED s32 SaveState_ConfirmAndProcessSelectedSlot(void)

Asks whether to save over the current file and, on Yes, writes the save stamp into it (SaveState_ProcessSelectedSlot) and says so. "OK to save over this file?" and "Your game was saved." Returns that result. Nothing calls it.

Line 364

SaveState_CopySlotPair

s32 SaveState_CopySlotPair(void)

The file menu's Copy (menu/clear.c): lets the player choose a file, reads both of its records into the save buffer, overwriting it, and writes them to the first empty file. Returns 0, -1 when cancelled, or a SAVE_ERROR_* after showing its message (the game says "Failed to erase." both when no file is empty and when the write fails).

Used by SaveMenu_Run.

Line 395

SaveState_DeleteSelectedSlot

s32 SaveState_DeleteSelectedSlot(void)

The file menu's Erase (menu/clear.c): lets the player choose a file, asks whether to erase it and on Yes deletes both of its records. "OK to erase this?" Returns 0 (also when the player answers No), -1 when cancelled, or a SAVE_ERROR_* after showing its message.

Used by SaveMenu_Run.

Line 447

PaletteGlow_UpdateSine

void PaletteGlow_UpdateSine(void)

A scheduler callback that makes colours 8-11 of BG palette 14 glow: each follows a sine wave of the frame counter, eight frames behind the one before. With n the sine as a whole number from -4 to 4 (Trig_Sin's 16.16 result over 16384, rounded toward zero), the colour is red 22 + 2n, green 16 + n and blue 20 + n (of 31): red 14 to 30, green 12 to 20 and blue 16 to 24. The step of 0x300 a frame, in 1/65536 turns, makes a cycle of about 85 frames. PaletteGlow_Start runs it while the save file menu is open.

Used by PaletteGlow_Start, PaletteGlow_Stop.

Line 494

PaletteGlow_Start

void PaletteGlow_Start(void)

Starts the palette glow (PaletteGlow_UpdateSine) in the scheduler.

Used by SaveMenu_SelectSlot.

Line 548

UiWindow_FillTilemapRect

void UiWindow_FillTilemapRect(struct UiWindow *window, s32 x, s32 y, s32 width, s32 height)

Fills the width x height tile rectangle at (x, y) inside window, clipped to the screen as UiWindow_SetTileAttributeBitRect clips, with the tile entry 0xe006: tile 6 in BG palette 14, the palette PaletteGlow_UpdateSine animates. The save file menu marks the file under the cursor with it.

Used by SaveMenu_SelectSlot.

Line 561

SaveMenu_ShowPartySprites

void SaveMenu_ShowPartySprites(struct UiWindow *window, s32 x, s32 y, struct SaveSummary *summary)

Shows the party of save file summary (up to four members) as sprites in a row from tile (x, y) inside window, and starts placing them every frame (SaveMenu_PlacePartySprites).

Used by SaveMenu_SelectSlot.

Line 618

SaveMenu_HidePartySprites

void SaveMenu_HidePartySprites(void)

Stops placing the party sprites of the save file menu and releases them.

Used by SaveMenu_SelectSlot.

Line 649

SaveMenu_PlacePartySprites

void SaveMenu_PlacePartySprites(void)

A scheduler callback: draws each party sprite of the save file menu at its place and scale for this frame.

Used by SaveMenu_HidePartySprites, SaveMenu_ShowPartySprites.

Line 664

SaveMenu_ShowDjinnSprites

void SaveMenu_ShowDjinnSprites(struct UiWindow *window, s32 x, s32 y)

Shows the four Djinn sprites, one for each element, in a row from tile (x, y) inside window, and starts placing them every frame (SaveMenu_PlaceDjinnSprites). Does nothing when window is NULL.

Used by SaveMenu_SelectSlot.

Line 693

SaveMenu_HideDjinnSprites

void SaveMenu_HideDjinnSprites(void)

Stops placing the Djinn sprites of the save file menu and releases them.

Used by SaveMenu_SelectSlot.

Line 720

SaveMenu_PlaceDjinnSprites

void SaveMenu_PlaceDjinnSprites(void)

A scheduler callback: draws each Djinn sprite of the save file menu at its place, at scale 1.0, for this frame.

Used by SaveMenu_HideDjinnSprites, SaveMenu_ShowDjinnSprites.

Line 735

SaveMenu_DrawDjinnCounts

void SaveMenu_DrawDjinnCounts(struct UiWindow *window, struct SaveSummary *summary)

Draws the Djinn counts of save file summary, one for each element, two digits each, 24 pixels apart on row 16 of window, under the Djinn sprites. Does nothing when window is NULL.

Used by SaveMenu_SelectSlot.

Line 764

SaveMenu_DrawFileSummary

void SaveMenu_DrawFileSummary(struct UiWindow *window, struct SaveSummary *summary)

Draws the summary of save file summary into window: the leader's name, level and class, the play time and the coins. Does nothing when window is 0.

Used by SaveMenu_SelectSlot.

Line 793

Save_WriteSelectedSlot

s32 Save_WriteSelectedSlot(void)

The Save command: lets the player choose a file (the current one first), asks whether to save over it when it has data, then saves the game to it, object table included, and makes it the current file. The question is "OK to save over this file?" Returns 0 (also when the player answers No), -1 when cancelled, or a SAVE_ERROR_* after showing its message.

Used by SaveMenu_Run.

Line 1207

SaveState_LoadRecordIntoWork

s32 SaveState_LoadRecordIntoWork(s32 mode)

Lets the player choose a file in save menu mode (see SaveMenuMode) and loads it into the save buffer as the current file, restoring its play time and options. Returns 0, -1 when cancelled, or a SAVE_ERROR_* after showing its message. The title's Continue (mode 1), Battle (4) and Send (5) load through here (menu/clear.c).

Used by SaveMenu_Run.

Line 1266

Resource_LoadFixedBlockBIntoFreeSlot

s32 Resource_LoadFixedBlockBIntoFreeSlot(void)

Loads the pointing hand cursor's tiles (gResource_FixedBlockBTiles, 128 bytes) into a free VRAM slot and returns the slot, which the caller frees with Resource_ResetEntry. The slot is not checked: with the cache full the load is given VRAM_CACHE_ENTRY_COUNT. Colosso's markers (scene/common/colosso_marker.h) and, by far call, the world map's cursor (object/psynergy_effects/world_map.c) use it.

Used by Colosso_EnsureMarkerSlot.

Line 1307

Also, functions without a comment: PaletteGlow_Stop, SaveMenu_SelectSlot.

Enumerations

SaveMenuMode

enum SaveMenuMode

What the save file menu is choosing a file for, which decides the files the cursor may stop on. Each mode also indexes the heading the menu shows (gRenderResource_PairSourceTable: Save, Continue, Copy, Erase, Battle, Link).

Value=
SAVE_MENU_SAVEsaving: any file, empty ones too
SAVE_MENU_CONTINUEcontinuing: files not yet cleared
SAVE_MENU_COPYcopying: any file with data
SAVE_MENU_ERASEerasing: any file with data
SAVE_MENU_EXTRAThe title's Battle: files with any of game flags 48-127 set.
SAVE_MENU_CLEAREDthe title's Send: cleared files only

Line 840

Macros

SAVE_ERROR_WRITE

#define SAVE_ERROR_WRITE -3

Writing failed. "Failed to save." (or "Failed to erase.")

Used by SaveState_CopySlotPair, SaveState_ProcessSelectedSlot, SaveState_WriteCurrentSlotPair, SaveState_WriteSlotPair, Save_WriteSelectedSlot.

Line 17

SAVE_ERROR_NO_MEMORY

#define SAVE_ERROR_NO_MEMORY -9

No backup memory. "Unable to detect backup memory."

Used by SaveState_CopySlotPair, SaveState_CountRecordsExcludingFlagged, SaveState_DeleteSelectedSlot, SaveState_LoadRecordIntoWork, SaveState_ProcessSelectedSlot, SaveState_ScanRecordFlags, SaveState_WriteCurrentSlotPair, SaveState_WriteSlotPair, Save_WriteSelectedSlot.

Line 21

SUMMARY_DISTANCE

#define SUMMARY_DISTANCE(from, to)

The distance in bytes from one field of a save summary to another.

Used by SaveState_ScanRecordFlags.

Line 103

SAVE_MENU_SPRITE_SPACING

#define SAVE_MENU_SPRITE_SPACING 3

The save file menu shows the chosen file's party as walking sprites in a row, and four Djinn sprites, one for each element, over the file's Djinn counts. Each sprite stands three tiles (24 pixels) from the one before.

Used by SaveMenu_DrawDjinnCounts, SaveMenu_ShowDjinnSprites, SaveMenu_ShowPartySprites.

Line 611

SAVE_MENU_SPRITE_PRIORITY

#define SAVE_MENU_SPRITE_PRIORITY (200 << 4)

Scheduler priority of the callbacks that place the sprites every frame.

Used by SaveMenu_ShowDjinnSprites.

Line 613

SAVE_MENU_NO_FILE

#define SAVE_MENU_NO_FILE -2

SaveMenu_SelectSlot's result when no file can be chosen in its mode.

Used by SaveMenu_SelectSlot.

Line 851

Also, macros without a comment: SAVE_FILE_COUNT, SAVE_RECORD_BYTES, SAVE_NO_FREE_FILE, SAVE_ERROR_READ, SAVE_ERROR_ERASE, SAVE_ERROR_NO_FILE, SUMMARY_LEVEL_X, SUMMARY_COINS_X, SUMMARY_DRAW_LEVEL, SUMMARY_LEVEL_X, SUMMARY_COINS_X, SUMMARY_DRAW_LEVEL, SUMMARY_LEVEL_X, SUMMARY_COINS_X, SUMMARY_DRAW_LEVEL.

On this page