pokecrystal-board/engine/menus/save.asm

1128 lines
20 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
2019-04-08 05:15:10 -07:00
call SpeechTextbox
2015-08-19 10:13:48 -07:00
call UpdateSprites
farcall SaveMenu_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
call GSReloadPalettes
farcall SaveMenu_CopyTilemapAtOnce
2015-08-19 10:13:48 -07:00
scf
ret
2018-06-24 07:09:41 -07:00
SaveAfterLinkTrade:
call PauseGameLogic
2017-12-24 09:47:30 -08:00
farcall StageRTCTimeForSave
farcall BackupMysteryGift
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
farcall SaveRTC
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
2017-12-24 09:47:30 -08:00
farcall StageRTCTimeForSave
farcall BackupMysteryGift
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
farcall BackupMobileEventIndex
farcall SaveRTC
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
ret
2018-06-24 07:09:41 -07:00
SaveGameData:
call _SaveGameData
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
call GSReloadPalettes
2015-08-19 10:13:48 -07:00
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:
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
2015-08-19 10:13:48 -07:00
call PrintText
; 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
2017-12-24 09:47:30 -08:00
farcall StageRTCTimeForSave
farcall BackupMysteryGift
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
farcall BackupMobileEventIndex
farcall SaveRTC
ld a, BANK(sBattleTowerChallengeState)
call OpenSRAM
ld a, [sBattleTowerChallengeState]
2015-11-26 18:05:32 -08:00
cp BATTLETOWER_RECEIVED_REWARD
2015-08-19 21:14:15 -07:00
jr nz, .ok
2015-08-19 10:13:48 -07:00
xor a
ld [sBattleTowerChallengeState], a
2015-08-19 21:14:15 -07:00
.ok
2015-08-19 10:13:48 -07:00
call CloseSRAM
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
2015-08-19 10:13:48 -07:00
call PrintText
; 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
call EraseMysteryGift
2015-11-17 20:29:14 -08:00
call SaveData
2015-11-26 18:05:32 -08:00
call EraseBattleTowerStatus
2015-08-19 21:14:15 -07:00
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
EraseMysteryGift:
2015-11-17 20:29:14 -08:00
ld a, BANK(sBackupMysteryGiftItem)
call OpenSRAM
2015-11-17 20:29:14 -08:00
ld hl, sBackupMysteryGiftItem
ld bc, sBackupMysteryGiftItemEnd - sBackupMysteryGiftItem
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
Function14d18: ; unreferenced
ld a, BANK(s4_a007)
call OpenSRAM
ld hl, .Data
ld de, s4_a007
ld bc, .DataEnd - .Data
2015-08-19 10:13:48 -07:00
call CopyBytes
jp CloseSRAM
2018-06-24 07:09:41 -07:00
.Data:
2015-08-19 10:13:48 -07:00
db $0d, $02, $00, $05, $00, $00
db $22, $02, $01, $05, $00, $00
db $03, $04, $05, $08, $03, $05
db $0e, $06, $03, $02, $00, $00
db $39, $07, $07, $04, $00, $05
db $04, $07, $01, $05, $00, $00
db $0f, $05, $14, $07, $05, $05
db $11, $0c, $0c, $06, $06, $04
.DataEnd
2015-08-19 10:13:48 -07:00
2018-06-24 07:09:41 -07:00
EraseBattleTowerStatus:
ld a, BANK(sBattleTowerChallengeState)
call OpenSRAM
2015-08-19 10:13:48 -07:00
xor a
ld [sBattleTowerChallengeState], a
2015-08-19 10:13:48 -07:00
jp CloseSRAM
2018-06-24 07:09:41 -07:00
SaveData:
2015-11-17 20:29:14 -08:00
call _SaveData
2015-08-19 10:13:48 -07:00
ret
Function14d6c: ; unreferenced
ld a, BANK(s4_a60b) ; MBC30 bank used by JP Crystal; inaccessible by MBC3
call OpenSRAM
ld a, [s4_a60b] ; address of MBC30 bank
2015-08-19 10:13:48 -07:00
ld b, $0
and a
jr z, .ok
ld b, $2
.ok
ld a, b
ld [s4_a60b], a ; address of MBC30 bank
2015-08-19 10:13:48 -07:00
call CloseSRAM
ret
Function14d83: ; unreferenced
ld a, BANK(s4_a60c) ; aka BANK(s4_a60d) ; MBC30 bank used by JP Crystal; inaccessible by MBC3
call OpenSRAM
2015-08-19 10:13:48 -07:00
xor a
ld [s4_a60c], a ; address of MBC30 bank
ld [s4_a60d], a ; address of MBC30 bank
2015-08-19 10:13:48 -07:00
call CloseSRAM
ret
Function14d93: ; unreferenced
ld a, BANK(s7_a000) ; MBC30 bank used by JP Crystal; inaccessible by MBC3
call OpenSRAM
2015-08-19 10:13:48 -07:00
xor a
ld [s7_a000], a ; address of MBC30 bank
2015-08-19 10:13:48 -07:00
call CloseSRAM
ret
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]
2015-11-26 18:05:32 -08:00
and $ff ^ (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
farcall RestoreMobileEventIndex
farcall RestoreMysteryGift
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
farcall RestoreMobileEventIndex
farcall RestoreMysteryGift
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
2015-08-19 10:13:48 -07:00
call PrintText
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
call ClearClock
2015-08-19 10:13:48 -07:00
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
ld a, BANK(sBattleTowerChallengeState)
call OpenSRAM
ld a, [sBattleTowerChallengeState]
2015-11-26 18:05:32 -08:00
cp BATTLETOWER_RECEIVED_REWARD
2015-11-17 20:29:14 -08:00
jr nz, .not_4
2015-11-26 18:05:32 -08:00
ld a, BATTLETOWER_WON_CHALLENGE
ld [sBattleTowerChallengeState], a
2015-11-17 20:29:14 -08:00
.not_4
2015-08-19 10:13:48 -07:00
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
_SaveData:
2018-01-08 19:48:58 -08:00
; This is called within two scenarios:
; a) ErasePreviousSave (the process of erasing the save from a previous game file)
; b) unused mobile functionality
; It is not part of a regular save.
2018-01-18 18:48:52 -08:00
2015-08-19 21:14:15 -07:00
ld a, BANK(sCrystalData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, wCrystalData
ld de, sCrystalData
ld bc, wCrystalDataEnd - wCrystalData
2015-08-19 10:13:48 -07:00
call CopyBytes
2015-08-19 21:14:15 -07:00
2018-01-08 19:48:58 -08:00
; This block originally had some mobile functionality, but since we're still in
2020-06-21 09:21:47 -07:00
; BANK(sCrystalData), it instead overwrites the sixteen wEventFlags starting at 1:s4_a60e with
2018-01-08 19:48:58 -08:00
; garbage from wd479. This isn't an issue, since ErasePreviousSave is followed by a regular
; save that unwrites the garbage.
2018-01-18 18:48:52 -08:00
2015-08-19 10:13:48 -07:00
ld hl, wd479
ld a, [hli]
ld [s4_a60e + 0], a
2015-08-19 10:13:48 -07:00
ld a, [hli]
ld [s4_a60e + 1], a
2015-08-19 21:14:15 -07:00
2015-08-19 10:13:48 -07:00
jp CloseSRAM
2018-06-24 07:09:41 -07:00
_LoadData:
2015-08-19 21:14:15 -07:00
ld a, BANK(sCrystalData)
call OpenSRAM
2015-08-19 21:14:15 -07:00
ld hl, sCrystalData
ld de, wCrystalData
ld bc, wCrystalDataEnd - wCrystalData
2015-08-19 10:13:48 -07:00
call CopyBytes
2018-01-18 18:48:52 -08:00
2018-01-08 19:48:58 -08:00
; This block originally had some mobile functionality to mirror _SaveData above, but instead it
2018-01-23 14:39:09 -08:00
; (harmlessly) writes the aforementioned wEventFlags to the unused wd479.
2018-01-18 18:48:52 -08:00
2015-08-19 10:13:48 -07:00
ld hl, wd479
ld a, [s4_a60e + 0]
2015-08-19 10:13:48 -07:00
ld [hli], a
ld a, [s4_a60e + 1]
2015-08-19 10:13:48 -07:00
ld [hli], a
2015-08-19 21:14:15 -07:00
2015-08-19 10:13:48 -07:00
jp CloseSRAM
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:
2015-08-19 10:13:48 -07:00
; dbww bank, address, address
2015-08-19 21:14:15 -07:00
dbww BANK(sBox1), sBox1, sBox1End
dbww BANK(sBox2), sBox2, sBox2End
dbww BANK(sBox3), sBox3, sBox3End
dbww BANK(sBox4), sBox4, sBox4End
dbww BANK(sBox5), sBox5, sBox5End
dbww BANK(sBox6), sBox6, sBox6End
dbww BANK(sBox7), sBox7, sBox7End
dbww BANK(sBox8), sBox8, sBox8End
dbww BANK(sBox9), sBox9, sBox9End
dbww BANK(sBox10), sBox10, sBox10End
dbww BANK(sBox11), sBox11, sBox11End
dbww BANK(sBox12), sBox12, sBox12End
dbww BANK(sBox13), sBox13, sBox13End
dbww BANK(sBox14), sBox14, sBox14End
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