	define sRTCStatusFlags, 0xc60
	define EVENT_FLAGS_OFFSET, 0x5f7

SavePatches:
	; if build < (2 byte value), apply patch indicated by 4 byte pointer; 0 ends the list
	dh 148
	dw SaveRTCPatch
	
	dh 150
	dw CreateExtraDataSaveArea
	
	dh 151
	dw FixSavefileChecksum
	
	dh 0

SaveRTCPatch:
	seek sRTCStatusFlags
	lockpos
	readbyte #temp
	or #temp, 0x80
	writebyte #temp
	unlockpos
	return

CreateExtraDataSaveArea:
	seek sExtraData
	fillbyte EXTRA_DATA_SIZE, #zero
	seek sBackupExtraData
	fillbyte EXTRA_DATA_SIZE, #zero
	return

GetGameDataPointer:
	add #result, #1, sGameData
	ifeq #savefile, 1, .done
	add #result, #1, sBackupGameData
.done
	return

EventFlagPatch:
	; #1: list of event flag numbers, terminated by -1
	; #2: action (0: clear, 1: set, 2: toggle)
	set #address, #1
	set #1, EVENT_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	jump .handle_loop
.loop
	push #2
	call FlagAction
	pop #2
.handle_loop
	gethalfwordinc #1, #address
	ifne #1, 0xffff, .loop
	return

FixSavefileChecksum:
	set #1, #zero
	call GetGameDataPointer
	seek #result
	call CalculateSavefileChecksum
	seek sChecksum
	ifeq #savefile, 1, .ok
	seek sBackupChecksum
.ok
	writehalfword #result
	subtract #1, sExtraData, sGameData
	call GetGameDataPointer
	seek #result
	call CalculateExtraChecksum
	seek sExtraChecksum
	ifeq #savefile, 1, .extra_ok
	seek sBackupExtraChecksum
.extra_ok
	writehalfword #result
	return
