pokecrystal-board/engine/menus/save.asm

1002 lines
18 KiB
NASM
Raw Normal View History

2018-06-24 07:09:41 -07:00
SaveMenu:
call LoadStandardMenuHeader
2017-12-24 09:47:30 -08:00
farcall DisplaySaveInfoOnSave
call SpeechTextbox2bpp
2015-08-19 10:13:48 -07:00
call UpdateSprites
farcall CopyTilemapAtOnce
ld hl, WouldYouLikeToSaveTheGameText
2015-08-19 10:13:48 -07:00
call SaveTheGame_yesorno
jr nz, .refused
2015-11-17 20:29:14 -08:00
call AskOverwriteSaveFile
2015-08-19 10:13:48 -07:00
jr c, .refused
call PauseGameLogic
2015-08-19 10:13:48 -07:00
call _SavingDontTurnOffThePower
call ResumeGameLogic
2015-08-19 10:13:48 -07:00
call ExitMenu
and a
ret
2015-11-17 20:29:14 -08:00
2015-08-19 10:13:48 -07:00
.refused
call ExitMenu
farcall CopyTilemapAtOnce
2015-08-19 10:13:48 -07:00
scf
ret
AutoSaveGameInOverworld:
call PauseGameLogic
; Prevent joypad interrupts
xor a
ldh [hJoypadReleased], a
ldh [hJoypadPressed], a
ldh [hJoypadSum], a
ldh [hJoypadDown], a
; Signal that we're saving inside a level
ld a, TRUE
ld [wSaveFileInOverworld], a
call SaveGameData
call ResumeGameLogic
ret
AutoSaveGameOutsideOverworld:
call PauseGameLogic
; Prevent joypad interrupts
xor a
ldh [hJoypadReleased], a
ldh [hJoypadPressed], a
ldh [hJoypadSum], a
ldh [hJoypadDown], a
; Signal that we're not saving inside a level
xor a ; FALSE
ld [wSaveFileInOverworld], a
call SaveGameData
call ResumeGameLogic
ret
2018-06-24 07:09:41 -07:00
SaveAfterLinkTrade:
call PauseGameLogic
2015-08-19 21:14:15 -07:00
call SavePokemonData
2015-11-17 20:29:14 -08:00
call SaveChecksum
2015-08-19 21:14:15 -07:00
call SaveBackupPokemonData
call SaveBackupChecksum
2017-12-24 09:47:30 -08:00
farcall BackupPartyMonMail
call ResumeGameLogic
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
ChangeBoxSaveGame:
2015-08-19 10:13:48 -07:00
push de
ld hl, ChangeBoxSaveText
2019-04-08 05:15:10 -07:00
call MenuTextbox
2015-08-19 10:13:48 -07:00
call YesNoBox
call ExitMenu
jr c, .refused
2015-11-17 20:29:14 -08:00
call AskOverwriteSaveFile
2015-08-19 10:13:48 -07:00
jr c, .refused
call PauseGameLogic
2015-08-19 10:13:48 -07:00
call SavingDontTurnOffThePower
2015-08-19 21:14:15 -07:00
call SaveBox
2015-08-19 10:13:48 -07:00
pop de
ld a, e
ld [wCurBox], a
2015-08-19 21:14:15 -07:00
call LoadBox
2015-08-19 10:13:48 -07:00
call SavedTheGame
call ResumeGameLogic
2015-08-19 10:13:48 -07:00
and a
ret
.refused
pop de
ret
2018-06-24 07:09:41 -07:00
Link_SaveGame:
2015-11-17 20:29:14 -08:00
call AskOverwriteSaveFile
2015-08-19 10:13:48 -07:00
jr c, .refused
call PauseGameLogic
2015-08-19 10:13:48 -07:00
call _SavingDontTurnOffThePower
call ResumeGameLogic
2015-08-19 10:13:48 -07:00
and a
.refused
ret
2018-06-24 07:09:41 -07:00
MoveMonWOMail_SaveGame:
call PauseGameLogic
2015-08-19 10:13:48 -07:00
push de
2015-08-19 21:14:15 -07:00
call SaveBox
2015-08-19 10:13:48 -07:00
pop de
ld a, e
ld [wCurBox], a
2015-08-19 21:14:15 -07:00
call LoadBox
call ResumeGameLogic
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
MoveMonWOMail_InsertMon_SaveGame:
call PauseGameLogic
2015-08-19 10:13:48 -07:00
push de
2015-08-19 21:14:15 -07:00
call SaveBox
2015-08-19 10:13:48 -07:00
pop de
ld a, e
ld [wCurBox], a
2018-07-03 23:50:27 -07:00
ld a, TRUE
2015-11-17 20:29:14 -08:00
ld [wSaveFileExists], a
2015-08-19 21:14:15 -07:00
call ValidateSave
call SaveOptions
2015-08-19 10:13:48 -07:00
call SavePlayerData
2015-08-19 21:14:15 -07:00
call SavePokemonData
2015-11-17 20:29:14 -08:00
call SaveChecksum
2015-08-19 21:14:15 -07:00
call ValidateBackupSave
call SaveBackupOptions
call SaveBackupPlayerData
call SaveBackupPokemonData
call SaveBackupChecksum
2017-12-24 09:47:30 -08:00
farcall BackupPartyMonMail
2015-08-19 21:14:15 -07:00
call LoadBox
call ResumeGameLogic
2015-08-19 10:13:48 -07:00
ld de, SFX_SAVE
call PlaySFX
2015-11-17 20:29:14 -08:00
ld c, 24
2015-08-19 10:13:48 -07:00
call DelayFrames
ret
2018-06-24 07:09:41 -07:00
StartMoveMonWOMail_SaveGame:
ld hl, MoveMonWOMailSaveText
2019-04-08 05:15:10 -07:00
call MenuTextbox
2015-08-19 10:13:48 -07:00
call YesNoBox
call ExitMenu
2015-11-17 20:29:14 -08:00
jr c, .refused
call AskOverwriteSaveFile
jr c, .refused
call PauseGameLogic
2015-08-19 10:13:48 -07:00
call _SavingDontTurnOffThePower
call ResumeGameLogic
2015-08-19 10:13:48 -07:00
and a
ret
2015-11-17 20:29:14 -08:00
.refused
2015-08-19 10:13:48 -07:00
scf
ret
2018-06-24 07:09:41 -07:00
PauseGameLogic:
ld a, TRUE
ld [wGameLogicPaused], a
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
ResumeGameLogic:
xor a ; FALSE
ld [wGameLogicPaused], a
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
AddHallOfFameEntry:
2015-08-19 21:14:15 -07:00
ld a, BANK(sHallOfFame)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, sHallOfFame + HOF_LENGTH * (NUM_HOF_TEAMS - 1) - 1
ld de, sHallOfFame + HOF_LENGTH * NUM_HOF_TEAMS - 1
ld bc, HOF_LENGTH * (NUM_HOF_TEAMS - 1)
2015-08-19 10:13:48 -07:00
.loop
ld a, [hld]
ld [de], a
dec de
dec bc
ld a, c
or b
jr nz, .loop
ld hl, wHallOfFamePokemonList
2015-08-19 21:14:15 -07:00
ld de, sHallOfFame
ld bc, wHallOfFamePokemonListEnd - wHallOfFamePokemonList + 1
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
; This vc_hook causes the Virtual Console to set [sMobileEventIndex] and [sMobileEventIndexBackup]
2022-04-16 17:42:06 -07:00
; to MOBILE_EVENT_OBJECT_GS_BALL, which enables you to get the GS Ball, take it to Kurt, and
; encounter Celebi. It assumes that sMobileEventIndex and sMobileEventIndexBackup are at their
; original addresses.
2022-03-12 19:38:29 -08:00
vc_hook Enable_GS_Ball_mobile_event
vc_assert BANK(sMobileEventIndex) == $1 && sMobileEventIndex == $be3c, \
"sMobileEventIndex is no longer located at 01:be3c."
vc_assert BANK(sMobileEventIndexBackup) == $1 && sMobileEventIndexBackup == $be44, \
"sMobileEventIndexBackup is no longer located at 01:be44."
2022-04-16 17:42:06 -07:00
vc_assert MOBILE_EVENT_OBJECT_GS_BALL == $0b, \
"MOBILE_EVENT_OBJECT_GS_BALL is no longer equal to $0b."
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
AskOverwriteSaveFile:
2015-11-17 20:29:14 -08:00
ld a, [wSaveFileExists]
2015-08-19 10:13:48 -07:00
and a
2015-08-19 21:14:15 -07:00
jr z, .erase
2015-11-26 18:05:32 -08:00
call CompareLoadedAndSavedPlayerID
2015-08-19 10:13:48 -07:00
jr z, .yoursavefile
ld hl, AnotherSaveFileText
2015-08-19 10:13:48 -07:00
call SaveTheGame_yesorno
jr nz, .refused
2015-08-19 21:14:15 -07:00
jr .erase
2015-08-19 10:13:48 -07:00
.yoursavefile
ld hl, AlreadyASaveFileText
2015-08-19 10:13:48 -07:00
call SaveTheGame_yesorno
jr nz, .refused
jr .ok
2015-08-19 21:14:15 -07:00
.erase
call ErasePreviousSave
2015-08-19 10:13:48 -07:00
.ok
and a
ret
.refused
scf
ret
2018-06-24 07:09:41 -07:00
SaveTheGame_yesorno:
ld b, BANK(WouldYouLikeToSaveTheGameText)
2015-08-19 10:13:48 -07:00
call MapTextbox
2019-04-08 05:15:10 -07:00
call LoadMenuTextbox
2015-08-19 10:13:48 -07:00
lb bc, 0, 7
call PlaceYesNoBox
2015-12-15 15:59:49 -08:00
ld a, [wMenuCursorY]
2015-08-19 10:13:48 -07:00
dec a
call CloseWindow
2015-08-19 10:13:48 -07:00
push af
pop af
and a
ret
2018-06-24 07:09:41 -07:00
CompareLoadedAndSavedPlayerID:
2015-08-19 21:14:15 -07:00
ld a, BANK(sPlayerData)
call OpenSRAM
2018-01-23 14:39:09 -08:00
ld hl, sPlayerData + (wPlayerID - wPlayerData)
2015-08-19 10:13:48 -07:00
ld a, [hli]
ld c, [hl]
ld b, a
call CloseSRAM
2018-01-23 14:39:09 -08:00
ld a, [wPlayerID]
2015-08-19 10:13:48 -07:00
cp b
ret nz
2018-01-23 14:39:09 -08:00
ld a, [wPlayerID + 1]
2015-08-19 10:13:48 -07:00
cp c
ret
2018-06-24 07:09:41 -07:00
_SavingDontTurnOffThePower:
2015-08-19 10:13:48 -07:00
call SavingDontTurnOffThePower
2018-06-24 07:09:41 -07:00
SavedTheGame:
ld a, TRUE
ld [wSaveFileInOverworld], a
call SaveGameData
2015-08-19 10:13:48 -07:00
; wait 32 frames
ld c, 32
2015-08-19 10:13:48 -07:00
call DelayFrames
; copy the original text speed setting to the stack
2018-01-23 14:39:09 -08:00
ld a, [wOptions]
2015-08-19 10:13:48 -07:00
push af
; set text speed to medium
ld a, TEXT_DELAY_MED
2018-01-23 14:39:09 -08:00
ld [wOptions], a
2015-08-19 10:13:48 -07:00
; <PLAYER> saved the game!
ld hl, SavedTheGameText
call PrintText1bpp
2015-08-19 10:13:48 -07:00
; restore the original text speed setting
pop af
2018-01-23 14:39:09 -08:00
ld [wOptions], a
2015-08-19 10:13:48 -07:00
ld de, SFX_SAVE
call WaitPlaySFX
call WaitSFX
; wait 30 frames
ld c, 30
2015-08-19 10:13:48 -07:00
call DelayFrames
ret
SaveGameData:
2018-07-03 23:50:27 -07:00
ld a, TRUE
2015-11-17 20:29:14 -08:00
ld [wSaveFileExists], a
2015-08-19 21:14:15 -07:00
call ValidateSave
call SaveOptions
2015-08-19 10:13:48 -07:00
call SavePlayerData
2015-08-19 21:14:15 -07:00
call SavePokemonData
call SaveBox
2015-11-17 20:29:14 -08:00
call SaveChecksum
2015-08-19 21:14:15 -07:00
call ValidateBackupSave
call SaveBackupOptions
call SaveBackupPlayerData
call SaveBackupPokemonData
call SaveBackupChecksum
call UpdateStackTop
2017-12-24 09:47:30 -08:00
farcall BackupPartyMonMail
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
UpdateStackTop:
2015-08-19 21:14:15 -07:00
; sStackTop appears to be unused.
; It could have been used to debug stack overflow during saving.
call FindStackTop
ld a, BANK(sStackTop)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld a, [sStackTop + 0]
2015-08-19 10:13:48 -07:00
ld e, a
2015-08-19 21:14:15 -07:00
ld a, [sStackTop + 1]
2015-08-19 10:13:48 -07:00
ld d, a
or e
2015-08-19 21:14:15 -07:00
jr z, .update
2015-08-19 10:13:48 -07:00
ld a, e
sub l
ld a, d
sbc h
2015-08-19 21:14:15 -07:00
jr c, .done
2015-08-19 10:13:48 -07:00
2015-08-19 21:14:15 -07:00
.update
2015-08-19 10:13:48 -07:00
ld a, l
2015-08-19 21:14:15 -07:00
ld [sStackTop + 0], a
2015-08-19 10:13:48 -07:00
ld a, h
2015-08-19 21:14:15 -07:00
ld [sStackTop + 1], a
2015-08-19 10:13:48 -07:00
2015-08-19 21:14:15 -07:00
.done
2015-08-19 10:13:48 -07:00
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
FindStackTop:
2015-08-19 21:14:15 -07:00
; Find the furthest point that sp has traversed to.
; This is distinct from the current value of sp.
2020-06-15 20:28:24 -07:00
ld hl, wStackBottom
2015-08-19 21:14:15 -07:00
.loop
2015-08-19 10:13:48 -07:00
ld a, [hl]
or a
ret nz
inc hl
2015-08-19 21:14:15 -07:00
jr .loop
2015-08-19 10:13:48 -07:00
2018-06-24 07:09:41 -07:00
SavingDontTurnOffThePower:
2015-08-19 10:13:48 -07:00
; Prevent joypad interrupts
xor a
ldh [hJoypadReleased], a
ldh [hJoypadPressed], a
ldh [hJoypadSum], a
ldh [hJoypadDown], a
2015-08-19 10:13:48 -07:00
; Save the text speed setting to the stack
2018-01-23 14:39:09 -08:00
ld a, [wOptions]
2015-08-19 10:13:48 -07:00
push af
; Set the text speed to medium
ld a, TEXT_DELAY_MED
2018-01-23 14:39:09 -08:00
ld [wOptions], a
2015-08-19 10:13:48 -07:00
; SAVING... DON'T TURN OFF THE POWER.
ld hl, SavingDontTurnOffThePowerText
call PrintText1bpp
2015-08-19 10:13:48 -07:00
; Restore the text speed setting
pop af
2018-01-23 14:39:09 -08:00
ld [wOptions], a
2015-08-19 10:13:48 -07:00
; Wait for 16 frames
ld c, 16
2015-08-19 10:13:48 -07:00
call DelayFrames
ret
2018-06-24 07:09:41 -07:00
ErasePreviousSave:
2015-08-19 21:14:15 -07:00
call EraseBoxes
call EraseHallOfFame
call EraseLinkBattleStats
ld a, BANK(sStackTop)
call OpenSRAM
2015-08-19 10:13:48 -07:00
xor a
2015-08-19 21:14:15 -07:00
ld [sStackTop + 0], a
ld [sStackTop + 1], a
2015-08-19 10:13:48 -07:00
call CloseSRAM
ld a, $1
2015-10-16 10:35:43 -07:00
ld [wSavedAtLeastOnce], a
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
EraseLinkBattleStats:
2015-08-19 21:14:15 -07:00
ld a, BANK(sLinkBattleStats)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, sLinkBattleStats
ld bc, sLinkBattleStatsEnd - sLinkBattleStats
2015-08-19 10:13:48 -07:00
xor a
call ByteFill
jp CloseSRAM
2018-06-24 07:09:41 -07:00
EraseHallOfFame:
2015-08-19 21:14:15 -07:00
ld a, BANK(sHallOfFame)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, sHallOfFame
ld bc, sHallOfFameEnd - sHallOfFame
2015-08-19 10:13:48 -07:00
xor a
call ByteFill
jp CloseSRAM
2018-06-24 07:09:41 -07:00
HallOfFame_InitSaveIfNeeded:
2015-10-16 10:35:43 -07:00
ld a, [wSavedAtLeastOnce]
2015-08-19 10:13:48 -07:00
and a
ret nz
2015-08-19 21:14:15 -07:00
call ErasePreviousSave
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
ValidateSave:
ld a, BANK(sCheckValue1) ; aka BANK(sCheckValue2)
call OpenSRAM
ld a, SAVE_CHECK_VALUE_1
ld [sCheckValue1], a
ld a, SAVE_CHECK_VALUE_2
ld [sCheckValue2], a
2015-08-19 10:13:48 -07:00
jp CloseSRAM
2018-06-24 07:09:41 -07:00
SaveOptions:
2015-08-19 21:14:15 -07:00
ld a, BANK(sOptions)
call OpenSRAM
2018-01-23 14:39:09 -08:00
ld hl, wOptions
2015-08-19 21:14:15 -07:00
ld de, sOptions
2018-01-23 14:39:09 -08:00
ld bc, wOptionsEnd - wOptions
2015-08-19 10:13:48 -07:00
call CopyBytes
2018-01-23 14:39:09 -08:00
ld a, [wOptions]
2021-11-23 17:37:18 -08:00
and ~(1 << NO_TEXT_SCROLL)
2015-08-19 21:14:15 -07:00
ld [sOptions], a
2015-08-19 10:13:48 -07:00
jp CloseSRAM
2018-06-24 07:09:41 -07:00
SavePlayerData:
2015-08-19 21:14:15 -07:00
ld a, BANK(sPlayerData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, wPlayerData
ld de, sPlayerData
ld bc, wPlayerDataEnd - wPlayerData
2015-08-19 10:13:48 -07:00
call CopyBytes
2018-09-09 12:09:51 -07:00
ld hl, wCurMapData
ld de, sCurMapData
ld bc, wCurMapDataEnd - wCurMapData
2015-08-19 10:13:48 -07:00
call CopyBytes
jp CloseSRAM
2018-06-24 07:09:41 -07:00
SavePokemonData:
2015-08-19 21:14:15 -07:00
ld a, BANK(sPokemonData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, wPokemonData
ld de, sPokemonData
ld bc, wPokemonDataEnd - wPokemonData
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
SaveBox:
2015-08-19 21:14:15 -07:00
call GetBoxAddress
call SaveBoxAddress
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
SaveChecksum:
2015-08-19 21:14:15 -07:00
ld hl, sGameData
ld bc, sGameDataEnd - sGameData
ld a, BANK(sGameData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
call Checksum
2015-08-19 10:13:48 -07:00
ld a, e
2015-08-19 21:14:15 -07:00
ld [sChecksum + 0], a
2015-08-19 10:13:48 -07:00
ld a, d
2015-08-19 21:14:15 -07:00
ld [sChecksum + 1], a
2015-08-19 10:13:48 -07:00
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
ValidateBackupSave:
ld a, BANK(sBackupCheckValue1) ; aka BANK(sBackupCheckValue2)
call OpenSRAM
ld a, SAVE_CHECK_VALUE_1
ld [sBackupCheckValue1], a
ld a, SAVE_CHECK_VALUE_2
ld [sBackupCheckValue2], a
2015-08-19 10:13:48 -07:00
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
SaveBackupOptions:
2015-08-19 21:14:15 -07:00
ld a, BANK(sBackupOptions)
call OpenSRAM
2018-01-23 14:39:09 -08:00
ld hl, wOptions
2015-08-19 21:14:15 -07:00
ld de, sBackupOptions
2018-01-23 14:39:09 -08:00
ld bc, wOptionsEnd - wOptions
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
SaveBackupPlayerData:
2015-08-19 21:14:15 -07:00
ld a, BANK(sBackupPlayerData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, wPlayerData
ld de, sBackupPlayerData
ld bc, wPlayerDataEnd - wPlayerData
2015-08-19 10:13:48 -07:00
call CopyBytes
2018-09-09 12:09:51 -07:00
ld hl, wCurMapData
ld de, sBackupCurMapData
ld bc, wCurMapDataEnd - wCurMapData
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
SaveBackupPokemonData:
2015-08-19 21:14:15 -07:00
ld a, BANK(sBackupPokemonData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, wPokemonData
ld de, sBackupPokemonData
ld bc, wPokemonDataEnd - wPokemonData
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
SaveBackupChecksum:
2015-08-19 21:14:15 -07:00
ld hl, sBackupGameData
ld bc, sBackupGameDataEnd - sBackupGameData
ld a, BANK(sBackupGameData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
call Checksum
2015-08-19 10:13:48 -07:00
ld a, e
2015-08-19 21:14:15 -07:00
ld [sBackupChecksum + 0], a
2015-08-19 10:13:48 -07:00
ld a, d
2015-08-19 21:14:15 -07:00
ld [sBackupChecksum + 1], a
2015-08-19 10:13:48 -07:00
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
TryLoadSaveFile:
2015-08-19 21:14:15 -07:00
call VerifyChecksum
jr nz, .backup
call LoadPlayerData
call LoadPokemonData
call LoadBox
2017-12-24 09:47:30 -08:00
farcall RestorePartyMonMail
2015-08-19 21:14:15 -07:00
call ValidateBackupSave
call SaveBackupOptions
call SaveBackupPlayerData
call SaveBackupPokemonData
call SaveBackupChecksum
2015-08-19 10:13:48 -07:00
and a
ret
2015-08-19 21:14:15 -07:00
.backup
call VerifyBackupChecksum
2015-08-19 10:13:48 -07:00
jr nz, .corrupt
2015-08-19 21:14:15 -07:00
call LoadBackupPlayerData
call LoadBackupPokemonData
call LoadBox
2017-12-24 09:47:30 -08:00
farcall RestorePartyMonMail
2015-08-19 21:14:15 -07:00
call ValidateSave
call SaveOptions
2015-08-19 10:13:48 -07:00
call SavePlayerData
2015-08-19 21:14:15 -07:00
call SavePokemonData
2015-11-17 20:29:14 -08:00
call SaveChecksum
2015-08-19 10:13:48 -07:00
and a
ret
2015-08-19 21:14:15 -07:00
2015-08-19 10:13:48 -07:00
.corrupt
2018-01-23 14:39:09 -08:00
ld a, [wOptions]
2015-08-19 10:13:48 -07:00
push af
2015-11-16 08:53:26 -08:00
set NO_TEXT_SCROLL, a
2018-01-23 14:39:09 -08:00
ld [wOptions], a
ld hl, SaveFileCorruptedText
call PrintText1bpp
2015-08-19 10:13:48 -07:00
pop af
2018-01-23 14:39:09 -08:00
ld [wOptions], a
2015-08-19 10:13:48 -07:00
scf
ret
2018-06-24 07:09:41 -07:00
TryLoadSaveData:
2018-07-03 23:50:27 -07:00
xor a ; FALSE
2015-11-17 20:29:14 -08:00
ld [wSaveFileExists], a
call CheckPrimarySaveFile
ld a, [wSaveFileExists]
2015-08-19 10:13:48 -07:00
and a
2015-08-19 21:14:15 -07:00
jr z, .backup
ld a, BANK(sPlayerData)
call OpenSRAM
2018-01-23 14:39:09 -08:00
ld hl, sPlayerData + wStartDay - wPlayerData
ld de, wStartDay
2015-08-19 21:14:15 -07:00
ld bc, 8
2015-08-19 10:13:48 -07:00
call CopyBytes
ld hl, sPlayerData + wStatusFlags - wPlayerData
ld de, wStatusFlags
2015-08-19 10:13:48 -07:00
ld a, [hl]
ld [de], a
call CloseSRAM
ret
2015-08-19 21:14:15 -07:00
.backup
2015-11-17 20:29:14 -08:00
call CheckBackupSaveFile
ld a, [wSaveFileExists]
2015-08-19 10:13:48 -07:00
and a
2015-08-19 21:14:15 -07:00
jr z, .corrupt
ld a, BANK(sBackupPlayerData)
call OpenSRAM
2018-01-23 14:39:09 -08:00
ld hl, sBackupPlayerData + wStartDay - wPlayerData
ld de, wStartDay
2015-08-19 10:13:48 -07:00
ld bc, 8
call CopyBytes
ld hl, sBackupPlayerData + wStatusFlags - wPlayerData
ld de, wStatusFlags
2015-08-19 10:13:48 -07:00
ld a, [hl]
ld [de], a
call CloseSRAM
ret
2015-08-19 21:14:15 -07:00
.corrupt
2015-08-19 10:13:48 -07:00
ld hl, DefaultOptions
2018-01-23 14:39:09 -08:00
ld de, wOptions
ld bc, wOptionsEnd - wOptions
2015-08-19 10:13:48 -07:00
call CopyBytes
ret
INCLUDE "data/default_options.asm"
2018-06-24 07:09:41 -07:00
CheckPrimarySaveFile:
ld a, BANK(sCheckValue1) ; aka BANK(sCheckValue2)
call OpenSRAM
ld a, [sCheckValue1]
cp SAVE_CHECK_VALUE_1
2015-08-19 10:13:48 -07:00
jr nz, .nope
ld a, [sCheckValue2]
cp SAVE_CHECK_VALUE_2
2015-08-19 10:13:48 -07:00
jr nz, .nope
2015-08-19 21:14:15 -07:00
ld hl, sOptions
2018-01-23 14:39:09 -08:00
ld de, wOptions
ld bc, wOptionsEnd - wOptions
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
2018-07-03 23:50:27 -07:00
ld a, TRUE
2015-11-17 20:29:14 -08:00
ld [wSaveFileExists], a
2015-08-19 10:13:48 -07:00
.nope
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
CheckBackupSaveFile:
ld a, BANK(sBackupCheckValue1) ; aka BANK(sBackupCheckValue2)
call OpenSRAM
ld a, [sBackupCheckValue1]
cp SAVE_CHECK_VALUE_1
2015-08-19 10:13:48 -07:00
jr nz, .nope
ld a, [sBackupCheckValue2]
cp SAVE_CHECK_VALUE_2
2015-08-19 10:13:48 -07:00
jr nz, .nope
2015-08-19 21:14:15 -07:00
ld hl, sBackupOptions
2018-01-23 14:39:09 -08:00
ld de, wOptions
ld bc, wOptionsEnd - wOptions
2015-08-19 10:13:48 -07:00
call CopyBytes
ld a, $2
2015-11-17 20:29:14 -08:00
ld [wSaveFileExists], a
2015-08-19 10:13:48 -07:00
.nope
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
LoadPlayerData:
2015-08-19 21:14:15 -07:00
ld a, BANK(sPlayerData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, sPlayerData
ld de, wPlayerData
ld bc, wPlayerDataEnd - wPlayerData
2015-08-19 10:13:48 -07:00
call CopyBytes
2018-09-09 12:09:51 -07:00
ld hl, sCurMapData
ld de, wCurMapData
ld bc, wCurMapDataEnd - wCurMapData
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
LoadPokemonData:
2015-08-19 21:14:15 -07:00
ld a, BANK(sPokemonData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, sPokemonData
ld de, wPokemonData
ld bc, wPokemonDataEnd - wPokemonData
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
LoadBox:
2015-08-19 21:14:15 -07:00
call GetBoxAddress
call LoadBoxAddress
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
VerifyChecksum:
2015-08-19 21:14:15 -07:00
ld hl, sGameData
ld bc, sGameDataEnd - sGameData
ld a, BANK(sGameData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
call Checksum
ld a, [sChecksum + 0]
2015-08-19 10:13:48 -07:00
cp e
2015-11-17 20:29:14 -08:00
jr nz, .fail
2015-08-19 21:14:15 -07:00
ld a, [sChecksum + 1]
2015-08-19 10:13:48 -07:00
cp d
2015-11-17 20:29:14 -08:00
.fail
2015-08-19 10:13:48 -07:00
push af
call CloseSRAM
pop af
ret
2018-06-24 07:09:41 -07:00
LoadBackupPlayerData:
2015-08-19 21:14:15 -07:00
ld a, BANK(sBackupPlayerData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, sBackupPlayerData
ld de, wPlayerData
ld bc, wPlayerDataEnd - wPlayerData
2015-08-19 10:13:48 -07:00
call CopyBytes
2018-09-09 12:09:51 -07:00
ld hl, sBackupCurMapData
ld de, wCurMapData
ld bc, wCurMapDataEnd - wCurMapData
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
LoadBackupPokemonData:
2015-08-19 21:14:15 -07:00
ld a, BANK(sBackupPokemonData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, sBackupPokemonData
ld de, wPokemonData
ld bc, wPokemonDataEnd - wPokemonData
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
ret
2018-06-24 07:09:41 -07:00
VerifyBackupChecksum:
2015-08-19 21:14:15 -07:00
ld hl, sBackupGameData
ld bc, sBackupGameDataEnd - sBackupGameData
ld a, BANK(sBackupGameData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
call Checksum
ld a, [sBackupChecksum + 0]
2015-08-19 10:13:48 -07:00
cp e
2015-11-17 20:29:14 -08:00
jr nz, .fail
2015-08-19 21:14:15 -07:00
ld a, [sBackupChecksum + 1]
2015-08-19 10:13:48 -07:00
cp d
2015-11-17 20:29:14 -08:00
.fail
2015-08-19 10:13:48 -07:00
push af
call CloseSRAM
pop af
ret
2018-06-24 07:09:41 -07:00
GetBoxAddress:
2015-08-19 10:13:48 -07:00
ld a, [wCurBox]
cp NUM_BOXES
2015-08-19 21:14:15 -07:00
jr c, .ok
2015-08-19 10:13:48 -07:00
xor a
ld [wCurBox], a
2015-08-19 21:14:15 -07:00
.ok
2015-08-19 10:13:48 -07:00
ld e, a
ld d, 0
2015-08-19 21:14:15 -07:00
ld hl, BoxAddresses
2015-08-19 10:13:48 -07:00
rept 5
add hl, de
endr
ld a, [hli]
push af
ld a, [hli]
ld e, a
ld a, [hli]
ld d, a
ld a, [hli]
ld h, [hl]
ld l, a
pop af
ret
2018-06-24 07:09:41 -07:00
SaveBoxAddress:
; Save box via wBoxPartialData.
; We do this in three steps because the size of wBoxPartialData is less than
2015-12-14 17:06:41 -08:00
; the size of sBox.
2015-08-19 10:13:48 -07:00
push hl
2015-12-14 17:06:41 -08:00
; Load the first part of the active box.
2015-08-19 10:13:48 -07:00
push af
push de
2015-08-19 21:14:15 -07:00
ld a, BANK(sBox)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, sBox
ld de, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
pop de
pop af
2015-12-14 17:06:41 -08:00
; Save it to the target box.
2015-08-19 10:13:48 -07:00
push af
push de
call OpenSRAM
ld hl, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
2015-12-14 17:06:41 -08:00
; Load the second part of the active box.
2015-08-19 21:14:15 -07:00
ld a, BANK(sBox)
call OpenSRAM
ld hl, sBox + (wBoxPartialDataEnd - wBoxPartialData)
ld de, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
pop de
pop af
2015-08-19 21:14:15 -07:00
ld hl, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
add hl, de
ld e, l
ld d, h
2015-12-14 17:06:41 -08:00
; Save it to the next part of the target box.
2015-08-19 10:13:48 -07:00
push af
push de
call OpenSRAM
ld hl, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
2015-12-14 17:06:41 -08:00
; Load the third and final part of the active box.
2015-08-19 21:14:15 -07:00
ld a, BANK(sBox)
call OpenSRAM
ld hl, sBox + (wBoxPartialDataEnd - wBoxPartialData) * 2
ld de, wBoxPartialData
ld bc, sBoxEnd - (sBox + (wBoxPartialDataEnd - wBoxPartialData) * 2) ; $8e
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
pop de
pop af
2015-08-19 21:14:15 -07:00
ld hl, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
add hl, de
ld e, l
ld d, h
2015-12-14 17:06:41 -08:00
; Save it to the final part of the target box.
call OpenSRAM
ld hl, wBoxPartialData
ld bc, sBoxEnd - (sBox + (wBoxPartialDataEnd - wBoxPartialData) * 2) ; $8e
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
2015-08-19 21:14:15 -07:00
2015-08-19 10:13:48 -07:00
pop hl
ret
2018-06-24 07:09:41 -07:00
LoadBoxAddress:
; Load box via wBoxPartialData.
; We do this in three steps because the size of wBoxPartialData is less than
2015-12-14 17:06:41 -08:00
; the size of sBox.
2015-08-19 10:13:48 -07:00
push hl
ld l, e
ld h, d
2015-12-14 17:06:41 -08:00
; Load part 1
2015-08-19 10:13:48 -07:00
push af
push hl
call OpenSRAM
ld de, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
2015-08-19 21:14:15 -07:00
ld a, BANK(sBox)
call OpenSRAM
ld hl, wBoxPartialData
2015-08-19 21:14:15 -07:00
ld de, sBox
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
pop hl
pop af
2015-08-19 21:14:15 -07:00
ld de, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
add hl, de
2015-12-14 17:06:41 -08:00
; Load part 2
2015-08-19 10:13:48 -07:00
push af
push hl
call OpenSRAM
ld de, wBoxPartialData
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
2015-08-19 21:14:15 -07:00
ld a, BANK(sBox)
call OpenSRAM
ld hl, wBoxPartialData
ld de, sBox + (wBoxPartialDataEnd - wBoxPartialData)
ld bc, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
pop hl
pop af
2015-12-14 17:06:41 -08:00
; Load part 3
ld de, (wBoxPartialDataEnd - wBoxPartialData)
2015-08-19 10:13:48 -07:00
add hl, de
call OpenSRAM
ld de, wBoxPartialData
ld bc, sBoxEnd - (sBox + (wBoxPartialDataEnd - wBoxPartialData) * 2) ; $8e
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
2015-08-19 21:14:15 -07:00
ld a, BANK(sBox)
call OpenSRAM
ld hl, wBoxPartialData
ld de, sBox + (wBoxPartialDataEnd - wBoxPartialData) * 2
ld bc, sBoxEnd - (sBox + (wBoxPartialDataEnd - wBoxPartialData) * 2) ; $8e
2015-08-19 10:13:48 -07:00
call CopyBytes
call CloseSRAM
2015-08-19 21:14:15 -07:00
2015-08-19 10:13:48 -07:00
pop hl
ret
2018-06-24 07:09:41 -07:00
EraseBoxes:
2015-08-19 21:14:15 -07:00
ld hl, BoxAddresses
ld c, NUM_BOXES
.next
2015-08-19 10:13:48 -07:00
push bc
ld a, [hli]
call OpenSRAM
2015-08-19 10:13:48 -07:00
ld a, [hli]
ld e, a
ld a, [hli]
ld d, a
xor a
ld [de], a
inc de
ld a, -1
ld [de], a
inc de
2015-08-19 21:14:15 -07:00
ld bc, sBoxEnd - (sBox + 2)
.clear
2015-08-19 10:13:48 -07:00
xor a
ld [de], a
inc de
dec bc
ld a, b
or c
2015-08-19 21:14:15 -07:00
jr nz, .clear
2015-08-19 10:13:48 -07:00
ld a, [hli]
ld e, a
ld a, [hli]
ld d, a
ld a, -1
ld [de], a
inc de
xor a
ld [de], a
call CloseSRAM
pop bc
dec c
2015-08-19 21:14:15 -07:00
jr nz, .next
2015-08-19 10:13:48 -07:00
ret
2018-06-24 07:09:41 -07:00
BoxAddresses:
table_width 5, BoxAddresses
for n, 1, NUM_BOXES + 1
db BANK(sBox{d:n}) ; aka BANK(sBox{d:n}End)
dw sBox{d:n}, sBox{d:n}End
endr
assert_table_length NUM_BOXES
2015-08-19 10:13:48 -07:00
2018-06-24 07:09:41 -07:00
Checksum:
2015-08-19 10:13:48 -07:00
ld de, 0
.loop
ld a, [hli]
add e
ld e, a
ld a, 0
adc d
ld d, a
dec bc
ld a, b
or c
jr nz, .loop
ret
WouldYouLikeToSaveTheGameText:
text_far _WouldYouLikeToSaveTheGameText
text_end
2015-08-19 10:13:48 -07:00
SavingDontTurnOffThePowerText:
text_far _SavingDontTurnOffThePowerText
text_end
2015-08-19 10:13:48 -07:00
SavedTheGameText:
text_far _SavedTheGameText
text_end
2015-08-19 10:13:48 -07:00
AlreadyASaveFileText:
text_far _AlreadyASaveFileText
text_end
2015-08-19 10:13:48 -07:00
AnotherSaveFileText:
text_far _AnotherSaveFileText
text_end
2015-08-19 10:13:48 -07:00
SaveFileCorruptedText:
text_far _SaveFileCorruptedText
text_end
2015-08-19 10:13:48 -07:00
ChangeBoxSaveText:
text_far _ChangeBoxSaveText
text_end
2015-08-19 10:13:48 -07:00
MoveMonWOMailSaveText:
text_far _MoveMonWOMailSaveText
text_end