	define sRTCStatusFlags, 0xc60
	define sStableRNGData, 0x4be
	define EVENT_FLAGS_OFFSET, 0x5f7
	define TM_FLAGS_OFFSET, 0x3de

	define EVENT_POKEONLY_FINISHED_CATERPIE_QUEST, 82
	define EVENT_POKEONLY_MOTHERBUTTERFREE_IN_PARTY, 81
	define EVENT_POKEONLY_CATERPIE_IN_PARTY, 75
	define EVENT_MAGMA_ITEMROOM_TM, 777
	define EVENT_ROUTE_63_TM, 1717

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 158
	dw FixCaterpieQuestFlags
	
	dh 161
	dw FixIronHeadTMFlag
	
	dh 164
	dw FixSwaggerTMFlags
	
	dh 164
	dw CreateRandomData

	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

FixCaterpieQuestFlags:
	set #1, EVENT_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #1, EVENT_POKEONLY_FINISHED_CATERPIE_QUEST
	set #2, #result ;minor optimization, since we know #result > 2
	call FlagAction
	retz #result
	set #1, .flags
	set #2, #zero
	call EventFlagCheck
	retz #result
	print .caterpie_error_text
	menu #result, YesNoMenu
	retnz #result
	set #1, .flags
	set #2, #zero
	call EventFlagPatch
	jump FixSavefileChecksum
	
.caterpie_error_text
	string "It seems like the Caterpie quest in Laurel Forest is done, but the event flags are incorrectly set. Do you want to fix those flags?"
.flags
	dh EVENT_POKEONLY_CATERPIE_IN_PARTY, EVENT_POKEONLY_MOTHERBUTTERFREE_IN_PARTY, -1

FixIronHeadTMFlag:
	set #1, TM_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #1, 57 ;TM58 - 1
	set #2, #result
	call FlagAction
	retnz #result
	set #1, EVENT_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #1, EVENT_ROUTE_63_TM
	set #2, #result
	push #1
	call FlagAction
	pop #1
	retz #result
	print .restored_item_text
	set #2, #zero
	call FlagAction
	jump FixSavefileChecksum

.restored_item_text
	string "You seemed to have already picked up the item ball on Route 63, but the item was changed, so the ball was restored for you."

FixSwaggerTMFlags:
	set #1, EVENT_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #1, EVENT_MAGMA_ITEMROOM_TM
	set #2, #result
	push #1
	call FlagAction
	pop #1
	retz #result
	set #2, #zero
	call FlagAction
	print .restored_item_text
	jump FixSavefileChecksum

.restored_item_text
	string "You seemed to have already picked up the TM in a room in Firelight Caverns, but the TM was changed, so the item ball was restored for you."

CreateRandomData:
	seek sStableRNGData
	writehalfword #zero
	set #temp, 8
.loop
	call Random
	writebyte #result
	decrement #temp
	jumpnz #temp, .loop
	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

EventFlagCheck:
	; #1: list of event flag numbers, terminated by -1
	; #2: kind of check (0: any, nonzero: all)
	iflt #2, 2, .ok
	set #2, 1
.ok
	set #address, #1
	set #1, EVENT_FLAGS_OFFSET
	call GetGameDataPointer
	seek #result
	set #result, #2
	jump .handle_loop
.loop
	push #2
	set #2, -1
	call FlagAction
	pop #2
	ifne #result, #2, .done
.handle_loop
	gethalfwordinc #1, #address
	ifne #1, 0xffff, .loop
.done
	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
